OLD | NEW |
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 "GrGLFragmentShaderBuilder.h" | 8 #include "GrGLFragmentShaderBuilder.h" |
| 9 #include "GrRenderTarget.h" |
9 #include "GrGLProgramBuilder.h" | 10 #include "GrGLProgramBuilder.h" |
10 #include "gl/GrGLGpu.h" | 11 #include "gl/GrGLGpu.h" |
11 #include "glsl/GrGLSL.h" | 12 #include "glsl/GrGLSL.h" |
12 #include "glsl/GrGLSLCaps.h" | 13 #include "glsl/GrGLSLCaps.h" |
13 | 14 |
14 #define GL_CALL(X) GR_GL_CALL(fProgramBuilder->gpu()->glInterface(), X) | 15 #define GL_CALL(X) GR_GL_CALL(fProgramBuilder->gpu()->glInterface(), X) |
15 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fProgramBuilder->gpu()->glInterface(),
R, X) | 16 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fProgramBuilder->gpu()->glInterface(),
R, X) |
16 | 17 |
17 const char* GrGLFragmentShaderBuilder::kDstTextureColorName = "_dstColor"; | 18 const char* GrGLFragmentShaderBuilder::kDstTextureColorName = "_dstColor"; |
18 static const char* declared_color_output_name() { return "fsColorOut"; } | 19 static const char* declared_color_output_name() { return "fsColorOut"; } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 GR_STATIC_ASSERT(9 == kExclusion_GrBlendEquation - kFirstAdvancedGrBlendEqua
tion); | 53 GR_STATIC_ASSERT(9 == kExclusion_GrBlendEquation - kFirstAdvancedGrBlendEqua
tion); |
53 GR_STATIC_ASSERT(10 == kMultiply_GrBlendEquation - kFirstAdvancedGrBlendEqua
tion); | 54 GR_STATIC_ASSERT(10 == kMultiply_GrBlendEquation - kFirstAdvancedGrBlendEqua
tion); |
54 GR_STATIC_ASSERT(11 == kHSLHue_GrBlendEquation - kFirstAdvancedGrBlendEquati
on); | 55 GR_STATIC_ASSERT(11 == kHSLHue_GrBlendEquation - kFirstAdvancedGrBlendEquati
on); |
55 GR_STATIC_ASSERT(12 == kHSLSaturation_GrBlendEquation - kFirstAdvancedGrBlen
dEquation); | 56 GR_STATIC_ASSERT(12 == kHSLSaturation_GrBlendEquation - kFirstAdvancedGrBlen
dEquation); |
56 GR_STATIC_ASSERT(13 == kHSLColor_GrBlendEquation - kFirstAdvancedGrBlendEqua
tion); | 57 GR_STATIC_ASSERT(13 == kHSLColor_GrBlendEquation - kFirstAdvancedGrBlendEqua
tion); |
57 GR_STATIC_ASSERT(14 == kHSLLuminosity_GrBlendEquation - kFirstAdvancedGrBlen
dEquation); | 58 GR_STATIC_ASSERT(14 == kHSLLuminosity_GrBlendEquation - kFirstAdvancedGrBlen
dEquation); |
58 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kLayoutQualifierNames) == | 59 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kLayoutQualifierNames) == |
59 kGrBlendEquationCnt - kFirstAdvancedGrBlendEquation); | 60 kGrBlendEquationCnt - kFirstAdvancedGrBlendEquation); |
60 } | 61 } |
61 | 62 |
62 GrGLFragmentShaderBuilder::DstReadKey | |
63 GrGLFragmentShaderBuilder::KeyForDstRead(const GrTexture* dstTexture, const GrGL
Caps& caps) { | |
64 uint32_t key = kYesDstRead_DstReadKeyBit; | |
65 if (caps.glslCaps()->fbFetchSupport()) { | |
66 return key; | |
67 } | |
68 SkASSERT(dstTexture); | |
69 if (!caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(dstTexture->co
nfig())) { | |
70 // The fact that the config is alpha-only must be considered when genera
ting code. | |
71 key |= kUseAlphaConfig_DstReadKeyBit; | |
72 } | |
73 if (kTopLeft_GrSurfaceOrigin == dstTexture->origin()) { | |
74 key |= kTopLeftOrigin_DstReadKeyBit; | |
75 } | |
76 SkASSERT(static_cast<DstReadKey>(key) == key); | |
77 return static_cast<DstReadKey>(key); | |
78 } | |
79 | |
80 GrGLFragmentShaderBuilder::FragPosKey | 63 GrGLFragmentShaderBuilder::FragPosKey |
81 GrGLFragmentShaderBuilder::KeyForFragmentPosition(const GrRenderTarget* dst, con
st GrGLCaps&) { | 64 GrGLFragmentShaderBuilder::KeyForFragmentPosition(const GrRenderTarget* dst) { |
82 if (kTopLeft_GrSurfaceOrigin == dst->origin()) { | 65 if (kTopLeft_GrSurfaceOrigin == dst->origin()) { |
83 return kTopLeftFragPosRead_FragPosKey; | 66 return kTopLeftFragPosRead_FragPosKey; |
84 } else { | 67 } else { |
85 return kBottomLeftFragPosRead_FragPosKey; | 68 return kBottomLeftFragPosRead_FragPosKey; |
86 } | 69 } |
87 } | 70 } |
88 | 71 |
89 GrGLFragmentShaderBuilder::GrGLFragmentShaderBuilder(GrGLProgramBuilder* program
, | 72 GrGLFragmentShaderBuilder::GrGLFragmentShaderBuilder(GrGLProgramBuilder* program
, |
90 uint8_t fragPosKey) | 73 uint8_t fragPosKey) |
91 : INHERITED(program) | 74 : INHERITED(program) |
92 , fHasCustomColorOutput(false) | 75 , fHasCustomColorOutput(false) |
93 , fHasSecondaryOutput(false) | 76 , fHasSecondaryOutput(false) |
94 , fSetupFragPosition(false) | 77 , fSetupFragPosition(false) |
95 , fTopLeftFragPosRead(kTopLeftFragPosRead_FragPosKey == fragPosKey) | 78 , fTopLeftFragPosRead(kTopLeftFragPosRead_FragPosKey == fragPosKey) |
96 , fCustomColorOutputIndex(-1) | 79 , fCustomColorOutputIndex(-1) |
97 , fHasReadDstColor(false) | 80 , fHasReadDstColor(false) |
98 , fHasReadFragmentPosition(false) { | 81 , fHasReadFragmentPosition(false) { |
99 } | 82 } |
100 | 83 |
101 bool GrGLFragmentShaderBuilder::enableFeature(GLSLFeature feature) { | 84 bool GrGLFragmentShaderBuilder::enableFeature(GLSLFeature feature) { |
102 switch (feature) { | 85 switch (feature) { |
103 case kStandardDerivatives_GLSLFeature: { | 86 case kStandardDerivatives_GLSLFeature: { |
104 GrGLGpu* gpu = fProgramBuilder->gpu(); | 87 if (!fProgramBuilder->glslCaps()->shaderDerivativeSupport()) { |
105 if (!gpu->glCaps().shaderCaps()->shaderDerivativeSupport()) { | |
106 return false; | 88 return false; |
107 } | 89 } |
108 if (kGLES_GrGLStandard == gpu->glStandard() && | 90 const char* extension = fProgramBuilder->glslCaps()->shaderDerivativ
eExtensionString(); |
109 k110_GrGLSLGeneration == gpu->glslGeneration()) { | 91 if (extension) { |
110 this->addFeature(1 << kStandardDerivatives_GLSLFeature, | 92 this->addFeature(1 << kStandardDerivatives_GLSLFeature, extensio
n); |
111 "GL_OES_standard_derivatives"); | |
112 } | 93 } |
113 return true; | 94 return true; |
114 } | 95 } |
115 default: | 96 default: |
116 SkFAIL("Unexpected GLSLFeature requested."); | 97 SkFAIL("Unexpected GLSLFeature requested."); |
117 return false; | 98 return false; |
118 } | 99 } |
119 } | 100 } |
120 | 101 |
121 SkString GrGLFragmentShaderBuilder::ensureFSCoords2D(const GrGLSLTransformedCoor
dsArray& coords, | 102 SkString GrGLFragmentShaderBuilder::ensureFSCoords2D(const GrGLSLTransformedCoor
dsArray& coords, |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 SkASSERT(!fHasSecondaryOutput); | 222 SkASSERT(!fHasSecondaryOutput); |
242 fHasSecondaryOutput = true; | 223 fHasSecondaryOutput = true; |
243 if (kGLES_GrGLStandard == fProgramBuilder->gpu()->ctxInfo().standard()) { | 224 if (kGLES_GrGLStandard == fProgramBuilder->gpu()->ctxInfo().standard()) { |
244 this->addFeature(1 << kBlendFuncExtended_GLSLPrivateFeature, "GL_EXT_ble
nd_func_extended"); | 225 this->addFeature(1 << kBlendFuncExtended_GLSLPrivateFeature, "GL_EXT_ble
nd_func_extended"); |
245 } | 226 } |
246 | 227 |
247 // If the primary output is declared, we must declare also the secondary out
put | 228 // If the primary output is declared, we must declare also the secondary out
put |
248 // and vice versa, since it is not allowed to use a built-in gl_FragColor an
d a custom | 229 // and vice versa, since it is not allowed to use a built-in gl_FragColor an
d a custom |
249 // output. The condition also co-incides with the condition in whici GLES SL
2.0 | 230 // output. The condition also co-incides with the condition in whici GLES SL
2.0 |
250 // requires the built-in gl_SecondaryFragColorEXT, where as 3.0 requires a c
ustom output. | 231 // requires the built-in gl_SecondaryFragColorEXT, where as 3.0 requires a c
ustom output. |
251 const GrGLSLCaps& caps = *fProgramBuilder->gpu()->glCaps().glslCaps(); | 232 const GrGLSLCaps& caps = *fProgramBuilder->glslCaps(); |
252 if (caps.mustDeclareFragmentShaderOutput()) { | 233 if (caps.mustDeclareFragmentShaderOutput()) { |
253 fOutputs.push_back().set(kVec4f_GrSLType, GrGLSLShaderVar::kOut_TypeModi
fier, | 234 fOutputs.push_back().set(kVec4f_GrSLType, GrGLSLShaderVar::kOut_TypeModi
fier, |
254 declared_secondary_color_output_name()); | 235 declared_secondary_color_output_name()); |
255 } | 236 } |
256 } | 237 } |
257 | 238 |
258 const char* GrGLFragmentShaderBuilder::getPrimaryColorOutputName() const { | 239 const char* GrGLFragmentShaderBuilder::getPrimaryColorOutputName() const { |
259 return fHasCustomColorOutput ? declared_color_output_name() : "gl_FragColor"
; | 240 return fHasCustomColorOutput ? declared_color_output_name() : "gl_FragColor"
; |
260 } | 241 } |
261 | 242 |
262 const char* GrGLFragmentShaderBuilder::getSecondaryColorOutputName() const { | 243 const char* GrGLFragmentShaderBuilder::getSecondaryColorOutputName() const { |
263 const GrGLSLCaps& caps = *fProgramBuilder->gpu()->glCaps().glslCaps(); | 244 const GrGLSLCaps& caps = *fProgramBuilder->gpu()->glCaps().glslCaps(); |
264 return caps.mustDeclareFragmentShaderOutput() ? declared_secondary_color_out
put_name() | 245 return caps.mustDeclareFragmentShaderOutput() ? declared_secondary_color_out
put_name() |
265 : "gl_SecondaryFragColorEXT"; | 246 : "gl_SecondaryFragColorEXT"; |
266 } | 247 } |
267 | 248 |
268 bool GrGLFragmentShaderBuilder::compileAndAttachShaders(GrGLuint programId, | 249 void GrGLFragmentShaderBuilder::onFinalize() { |
269 SkTDArray<GrGLuint>* sha
derIds) { | |
270 this->versionDecl() = fProgramBuilder->glslCaps()->versionDeclString(); | |
271 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, | 250 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, |
272 *fProgramBuilder->glslCaps(), | 251 *fProgramBuilder->glslCaps(), |
273 &this->precisionQualifier()); | 252 &this->precisionQualifier()); |
274 this->compileAndAppendLayoutQualifiers(); | |
275 fProgramBuilder->appendUniformDecls(GrGLProgramBuilder::kFragment_Visibility
, | |
276 &this->uniforms()); | |
277 this->appendDecls(fInputs, &this->inputs()); | |
278 // We shouldn't have declared outputs on 1.10 | |
279 SkASSERT(k110_GrGLSLGeneration != fProgramBuilder->gpu()->glslGeneration() |
| fOutputs.empty()); | |
280 this->appendDecls(fOutputs, &this->outputs()); | |
281 return this->finalize(programId, GR_GL_FRAGMENT_SHADER, shaderIds); | |
282 } | 253 } |
283 | 254 |
284 void GrGLFragmentShaderBuilder::bindFragmentShaderLocations(GrGLuint programID)
{ | 255 void GrGLFragmentShaderBuilder::bindFragmentShaderLocations(GrGLuint programID)
{ |
285 const GrGLCaps& caps = fProgramBuilder->gpu()->glCaps(); | 256 const GrGLCaps& caps = fProgramBuilder->gpu()->glCaps(); |
286 if (fHasCustomColorOutput && caps.bindFragDataLocationSupport()) { | 257 if (fHasCustomColorOutput && caps.bindFragDataLocationSupport()) { |
287 GL_CALL(BindFragDataLocation(programID, 0, declared_color_output_name())
); | 258 GL_CALL(BindFragDataLocation(programID, 0, declared_color_output_name())
); |
288 } | 259 } |
289 if (fHasSecondaryOutput && caps.glslCaps()->mustDeclareFragmentShaderOutput(
)) { | 260 if (fHasSecondaryOutput && caps.glslCaps()->mustDeclareFragmentShaderOutput(
)) { |
290 GL_CALL(BindFragDataLocationIndexed(programID, 0, 1, | 261 GL_CALL(BindFragDataLocationIndexed(programID, 0, 1, |
291 declared_secondary_color_output_name
())); | 262 declared_secondary_color_output_name
())); |
(...skipping 16 matching lines...) Expand all Loading... |
308 fMangleString.appendf("_c%d", fSubstageIndices[fSubstageIndices.count() - 2]
); | 279 fMangleString.appendf("_c%d", fSubstageIndices[fSubstageIndices.count() - 2]
); |
309 } | 280 } |
310 | 281 |
311 void GrGLFragmentBuilder::onAfterChildProcEmitCode() { | 282 void GrGLFragmentBuilder::onAfterChildProcEmitCode() { |
312 SkASSERT(fSubstageIndices.count() >= 2); | 283 SkASSERT(fSubstageIndices.count() >= 2); |
313 fSubstageIndices.pop_back(); | 284 fSubstageIndices.pop_back(); |
314 fSubstageIndices.back()++; | 285 fSubstageIndices.back()++; |
315 int removeAt = fMangleString.findLastOf('_'); | 286 int removeAt = fMangleString.findLastOf('_'); |
316 fMangleString.remove(removeAt, fMangleString.size() - removeAt); | 287 fMangleString.remove(removeAt, fMangleString.size() - removeAt); |
317 } | 288 } |
OLD | NEW |