Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(287)

Side by Side Diff: src/gpu/glsl/GrGLSLFragmentShaderBuilder.cpp

Issue 1717393002: Add "sample locations" feature to GrProcessor (Closed) Base URL: https://skia.googlesource.com/skia.git@upload_getmultisamp
Patch Set: move into GrProcessor Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2014 Google Inc. 2 * Copyright 2014 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "GrGLSLFragmentShaderBuilder.h" 8 #include "GrGLSLFragmentShaderBuilder.h"
9 #include "GrRenderTarget.h" 9 #include "GrRenderTarget.h"
10 #include "gl/GrGLGpu.h" 10 #include "gl/GrGLGpu.h"
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 59
60 GrGLSLFragmentShaderBuilder::FragPosKey 60 GrGLSLFragmentShaderBuilder::FragPosKey
61 GrGLSLFragmentShaderBuilder::KeyForFragmentPosition(const GrRenderTarget* dst) { 61 GrGLSLFragmentShaderBuilder::KeyForFragmentPosition(const GrRenderTarget* dst) {
62 if (kTopLeft_GrSurfaceOrigin == dst->origin()) { 62 if (kTopLeft_GrSurfaceOrigin == dst->origin()) {
63 return kTopLeftFragPosRead_FragPosKey; 63 return kTopLeftFragPosRead_FragPosKey;
64 } else { 64 } else {
65 return kBottomLeftFragPosRead_FragPosKey; 65 return kBottomLeftFragPosRead_FragPosKey;
66 } 66 }
67 } 67 }
68 68
69 static const char* sampleOffsetArrays[] = {
70 "deviceSpaceSampleOffsets",
71 "windowSpaceSampleOffsets"
72 };
73
74 GR_STATIC_ASSERT(0 == GrGLSLFPFragmentBuilder::kSkiaDevice_Coordinates);
75 GR_STATIC_ASSERT(1 == GrGLSLFPFragmentBuilder::kGLSLWindow_Coordinates);
76 GR_STATIC_ASSERT(SK_ARRAY_COUNT(sampleOffsetArrays) ==
77 GrGLSLFPFragmentBuilder::kLast_Coordinates + 1);
78
69 GrGLSLFragmentShaderBuilder::GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* p rogram, 79 GrGLSLFragmentShaderBuilder::GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* p rogram,
70 uint8_t fragPosKey) 80 uint8_t fragPosKey)
71 : GrGLSLFragmentBuilder(program) 81 : GrGLSLFragmentBuilder(program)
72 , fSetupFragPosition(false) 82 , fSetupFragPosition(false)
73 , fTopLeftFragPosRead(kTopLeftFragPosRead_FragPosKey == fragPosKey) 83 , fTopLeftFragPosRead(kTopLeftFragPosRead_FragPosKey == fragPosKey)
74 , fHasCustomColorOutput(false) 84 , fHasCustomColorOutput(false)
75 , fCustomColorOutputIndex(-1) 85 , fCustomColorOutputIndex(-1)
76 , fHasSecondaryOutput(false) 86 , fHasSecondaryOutput(false)
87 , fUsedSampleOffsetArrays(0)
77 , fHasInitializedSampleMask(false) { 88 , fHasInitializedSampleMask(false) {
78 fSubstageIndices.push_back(0); 89 fSubstageIndices.push_back(0);
79 #ifdef SK_DEBUG 90 #ifdef SK_DEBUG
80 fUsedProcessorFeatures = GrProcessor::kNone_RequiredFeatures; 91 fUsedProcessorFeatures = GrProcessor::kNone_RequiredFeatures;
81 fHasReadDstColor = false; 92 fHasReadDstColor = false;
82 #endif 93 #endif
83 } 94 }
84 95
85 bool GrGLSLFragmentShaderBuilder::hasFragmentPosition() const {
86 return 0 != fProgramBuilder->header().fFragPosKey;
87 }
88
89 bool GrGLSLFragmentShaderBuilder::enableFeature(GLSLFeature feature) { 96 bool GrGLSLFragmentShaderBuilder::enableFeature(GLSLFeature feature) {
90 switch (feature) { 97 switch (feature) {
91 case kStandardDerivatives_GLSLFeature: { 98 case kStandardDerivatives_GLSLFeature: {
92 if (!fProgramBuilder->glslCaps()->shaderDerivativeSupport()) { 99 if (!fProgramBuilder->glslCaps()->shaderDerivativeSupport()) {
93 return false; 100 return false;
94 } 101 }
95 const char* extension = fProgramBuilder->glslCaps()->shaderDerivativ eExtensionString(); 102 const char* extension = fProgramBuilder->glslCaps()->shaderDerivativ eExtensionString();
96 if (extension) { 103 if (extension) {
97 this->addFeature(1 << kStandardDerivatives_GLSLFeature, extensio n); 104 this->addFeature(1 << kStandardDerivatives_GLSLFeature, extensio n);
98 } 105 }
(...skipping 23 matching lines...) Expand all
122 SkString coords2D("coords2D"); 129 SkString coords2D("coords2D");
123 if (0 != index) { 130 if (0 != index) {
124 coords2D.appendf("_%i", index); 131 coords2D.appendf("_%i", index);
125 } 132 }
126 this->codeAppendf("\tvec2 %s = %s.xy / %s.z;", 133 this->codeAppendf("\tvec2 %s = %s.xy / %s.z;",
127 coords2D.c_str(), coords[index].c_str(), coords[index].c_s tr()); 134 coords2D.c_str(), coords[index].c_str(), coords[index].c_s tr());
128 return coords2D; 135 return coords2D;
129 } 136 }
130 137
131 const char* GrGLSLFragmentShaderBuilder::fragmentPosition() { 138 const char* GrGLSLFragmentShaderBuilder::fragmentPosition() {
132 SkASSERT(this->hasFragmentPosition()); 139 SkASSERT(fProgramBuilder->canReadFragmentPosition());
133 SkDEBUGCODE(fUsedProcessorFeatures |= GrProcessor::kFragmentPosition_Require dFeature;) 140 SkDEBUGCODE(fUsedProcessorFeatures |= GrProcessor::kFragmentPosition_Require dFeature;)
134 141
135 const GrGLSLCaps* glslCaps = fProgramBuilder->glslCaps(); 142 const GrGLSLCaps* glslCaps = fProgramBuilder->glslCaps();
136 // We only declare "gl_FragCoord" when we're in the case where we want to us e layout qualifiers 143 // We only declare "gl_FragCoord" when we're in the case where we want to us e layout qualifiers
137 // to reverse y. Otherwise it isn't necessary and whether the "in" qualifier appears in the 144 // to reverse y. Otherwise it isn't necessary and whether the "in" qualifier appears in the
138 // declaration varies in earlier GLSL specs. So it is simpler to omit it. 145 // declaration varies in earlier GLSL specs. So it is simpler to omit it.
139 if (fTopLeftFragPosRead) { 146 if (fTopLeftFragPosRead) {
140 fSetupFragPosition = true; 147 fSetupFragPosition = true;
141 return "gl_FragCoord"; 148 return "gl_FragCoord";
142 } else if (const char* extension = glslCaps->fragCoordConventionsExtensionSt ring()) { 149 } else if (const char* extension = glslCaps->fragCoordConventionsExtensionSt ring()) {
(...skipping 27 matching lines...) Expand all
170 this->codePrependf("\t%svec4 %s = vec4(%s.x, %s - %s.y, 1.0, 1.0);\n ", 177 this->codePrependf("\t%svec4 %s = vec4(%s.x, %s - %s.y, 1.0, 1.0);\n ",
171 precision, kCoordName, kTempName, rtHeightName, k TempName); 178 precision, kCoordName, kTempName, rtHeightName, k TempName);
172 this->codePrependf("%svec2 %s = gl_FragCoord.xy;", precision, kTempN ame); 179 this->codePrependf("%svec2 %s = gl_FragCoord.xy;", precision, kTempN ame);
173 fSetupFragPosition = true; 180 fSetupFragPosition = true;
174 } 181 }
175 SkASSERT(fProgramBuilder->fUniformHandles.fRTHeightUni.isValid()); 182 SkASSERT(fProgramBuilder->fUniformHandles.fRTHeightUni.isValid());
176 return kCoordName; 183 return kCoordName;
177 } 184 }
178 } 185 }
179 186
187 void GrGLSLFragmentShaderBuilder::appendOffsetToSample(const char* sampleIdx, Co ordinates coords) {
188 SkASSERT(fProgramBuilder->canUseSampleLocations());
Chris Dalton 2016/02/29 20:52:50 Is an assert here good enough? Or do we prefer an
bsalomon 2016/03/01 14:28:55 An assert seems fine.
189 SkDEBUGCODE(fUsedProcessorFeatures |= GrProcessor::kSampleLocations_Required Feature);
190 this->codeAppendf("%s[%s]", sampleOffsetArrays[coords], sampleIdx);
191 fUsedSampleOffsetArrays |= (1 << coords);
192 }
193
180 void GrGLSLFragmentShaderBuilder::maskSampleCoverage(const char* mask, bool inve rt) { 194 void GrGLSLFragmentShaderBuilder::maskSampleCoverage(const char* mask, bool inve rt) {
181 const GrGLSLCaps& glslCaps = *fProgramBuilder->glslCaps(); 195 const GrGLSLCaps& glslCaps = *fProgramBuilder->glslCaps();
182 if (!glslCaps.sampleVariablesSupport()) { 196 if (!glslCaps.sampleVariablesSupport()) {
183 SkDEBUGFAIL("Attempted to mask sample coverage without support."); 197 SkDEBUGFAIL("Attempted to mask sample coverage without support.");
184 return; 198 return;
185 } 199 }
186 if (const char* extension = glslCaps.sampleVariablesExtensionString()) { 200 if (const char* extension = glslCaps.sampleVariablesExtensionString()) {
187 this->addFeature(1 << kSampleVariables_GLSLPrivateFeature, extension); 201 this->addFeature(1 << kSampleVariables_GLSLPrivateFeature, extension);
188 } 202 }
189 if (!fHasInitializedSampleMask) { 203 if (!fHasInitializedSampleMask) {
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 const GrGLSLCaps& caps = *fProgramBuilder->glslCaps(); 320 const GrGLSLCaps& caps = *fProgramBuilder->glslCaps();
307 return caps.mustDeclareFragmentShaderOutput() ? DeclaredSecondaryColorOutput Name() 321 return caps.mustDeclareFragmentShaderOutput() ? DeclaredSecondaryColorOutput Name()
308 : "gl_SecondaryFragColorEXT"; 322 : "gl_SecondaryFragColorEXT";
309 } 323 }
310 324
311 void GrGLSLFragmentShaderBuilder::onFinalize() { 325 void GrGLSLFragmentShaderBuilder::onFinalize() {
312 fProgramBuilder->varyingHandler()->getFragDecls(&this->inputs(), &this->outp uts()); 326 fProgramBuilder->varyingHandler()->getFragDecls(&this->inputs(), &this->outp uts());
313 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, 327 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision,
314 *fProgramBuilder->glslCaps(), 328 *fProgramBuilder->glslCaps(),
315 &this->precisionQualifier()); 329 &this->precisionQualifier());
330 if (fUsedSampleOffsetArrays & (1 << kSkiaDevice_Coordinates)) {
331 this->defineSampleOffsetArray(sampleOffsetArrays[kSkiaDevice_Coordinates ],
332 SkMatrix::MakeTrans(-0.5f, -0.5f));
333 }
334 if (fUsedSampleOffsetArrays & (1 << kGLSLWindow_Coordinates)) {
335 SkMatrix m;
336 m.setScale(1, -1);
337 m.preTranslate(-0.5f, -0.5f);
338 this->defineSampleOffsetArray(sampleOffsetArrays[kGLSLWindow_Coordinates ], m);
339 }
340 }
341
342 void GrGLSLFragmentShaderBuilder::defineSampleOffsetArray(const char* name, cons t SkMatrix& m) {
343 const GrPipeline& pipeline = fProgramBuilder->pipeline();
344 const GrRenderTargetPriv& rtp = pipeline.getRenderTarget()->renderTargetPriv ();
345 int effectiveSampleCnt = rtp.getEffectiveSampleCount(pipeline.getStencil());
346 SkSTArray<16, SkPoint, true> offsets;
347 offsets.push_back_n(effectiveSampleCnt);
348 m.mapPoints(offsets.begin(), rtp.getSampleLocations(pipeline.getStencil()), effectiveSampleCnt);
349 this->definitions().append("const ");
350 if (fProgramBuilder->glslCaps()->usesPrecisionModifiers()) {
351 this->definitions().append("highp ");
352 }
353 this->definitions().appendf("vec2 %s[] = vec2[](", name);
354 for (int i = 0; i < effectiveSampleCnt; ++i) {
355 this->definitions().appendf("vec2(%f, %f)", offsets[i].x(), offsets[i].y ());
356 this->definitions().append(i + 1 != effectiveSampleCnt ? ", " : ");\n");
357 }
316 } 358 }
317 359
318 void GrGLSLFragmentShaderBuilder::onBeforeChildProcEmitCode() { 360 void GrGLSLFragmentShaderBuilder::onBeforeChildProcEmitCode() {
319 SkASSERT(fSubstageIndices.count() >= 1); 361 SkASSERT(fSubstageIndices.count() >= 1);
320 fSubstageIndices.push_back(0); 362 fSubstageIndices.push_back(0);
321 // second-to-last value in the fSubstageIndices stack is the index of the ch ild proc 363 // second-to-last value in the fSubstageIndices stack is the index of the ch ild proc
322 // at that level which is currently emitting code. 364 // at that level which is currently emitting code.
323 fMangleString.appendf("_c%d", fSubstageIndices[fSubstageIndices.count() - 2] ); 365 fMangleString.appendf("_c%d", fSubstageIndices[fSubstageIndices.count() - 2] );
324 } 366 }
325 367
326 void GrGLSLFragmentShaderBuilder::onAfterChildProcEmitCode() { 368 void GrGLSLFragmentShaderBuilder::onAfterChildProcEmitCode() {
327 SkASSERT(fSubstageIndices.count() >= 2); 369 SkASSERT(fSubstageIndices.count() >= 2);
328 fSubstageIndices.pop_back(); 370 fSubstageIndices.pop_back();
329 fSubstageIndices.back()++; 371 fSubstageIndices.back()++;
330 int removeAt = fMangleString.findLastOf('_'); 372 int removeAt = fMangleString.findLastOf('_');
331 fMangleString.remove(removeAt, fMangleString.size() - removeAt); 373 fMangleString.remove(removeAt, fMangleString.size() - removeAt);
332 } 374 }
333 375
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698