| 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 "gl/GrGLProgram.h" | 8 #include "gl/GrGLProgram.h" |
| 9 #include "gl/GrGLSLPrettyPrint.h" | 9 #include "gl/GrGLSLPrettyPrint.h" |
| 10 #include "gl/GrGLUniformHandle.h" | 10 #include "gl/GrGLUniformHandle.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 // number of each input/output type in a single allocation block | 24 // number of each input/output type in a single allocation block |
| 25 static const int kVarsPerBlock = 8; | 25 static const int kVarsPerBlock = 8; |
| 26 | 26 |
| 27 // ES2 FS only guarantees mediump and lowp support | 27 // ES2 FS only guarantees mediump and lowp support |
| 28 static const GrGLShaderVar::Precision kDefaultFragmentPrecision = GrGLShaderVar:
:kMedium_Precision; | 28 static const GrGLShaderVar::Precision kDefaultFragmentPrecision = GrGLShaderVar:
:kMedium_Precision; |
| 29 } | 29 } |
| 30 | 30 |
| 31 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 31 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 32 | 32 |
| 33 bool GrGLProgramBuilder::genProgram(const GrEffectStage* geometryProcessor, | 33 bool GrGLProgramBuilder::genProgram(const GrGeometryStage* geometryProcessor, |
| 34 const GrEffectStage* colorStages[], | 34 const GrFragmentStage* colorStages[], |
| 35 const GrEffectStage* coverageStages[]) { | 35 const GrFragmentStage* coverageStages[]) { |
| 36 const GrGLProgramDesc::KeyHeader& header = this->desc().getHeader(); | 36 const GrGLProgramDesc::KeyHeader& header = this->desc().getHeader(); |
| 37 | 37 |
| 38 fFS.emitCodeBeforeEffects(); | 38 fFS.emitCodeBeforeEffects(); |
| 39 | 39 |
| 40 /////////////////////////////////////////////////////////////////////////// | 40 /////////////////////////////////////////////////////////////////////////// |
| 41 // get the initial color and coverage to feed into the first effect in each
effect chain | 41 // get the initial color and coverage to feed into the first effect in each
effect chain |
| 42 | 42 |
| 43 GrGLSLExpr4 inputColor; | 43 GrGLSLExpr4 inputColor; |
| 44 GrGLSLExpr4 inputCoverage; | 44 GrGLSLExpr4 inputCoverage; |
| 45 | 45 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 return false; | 77 return false; |
| 78 } | 78 } |
| 79 | 79 |
| 80 return true; | 80 return true; |
| 81 } | 81 } |
| 82 | 82 |
| 83 ////////////////////////////////////////////////////////////////////////////// | 83 ////////////////////////////////////////////////////////////////////////////// |
| 84 | 84 |
| 85 GrGLProgramBuilder::GrGLProgramBuilder(GrGpuGL* gpu, | 85 GrGLProgramBuilder::GrGLProgramBuilder(GrGpuGL* gpu, |
| 86 const GrGLProgramDesc& desc) | 86 const GrGLProgramDesc& desc) |
| 87 : fFragOnly(!desc.getHeader().fRequiresVertexShader && | 87 : fEffectEmitter(NULL) |
| 88 , fFragOnly(!desc.getHeader().fRequiresVertexShader && |
| 88 gpu->glCaps().pathRenderingSupport() && | 89 gpu->glCaps().pathRenderingSupport() && |
| 89 gpu->glPathRendering()->texturingMode() == GrGLPathRendering::Fi
xedFunction_TexturingMode) | 90 gpu->glPathRendering()->texturingMode() == GrGLPathRendering::Fi
xedFunction_TexturingMode) |
| 90 , fTexCoordSetCnt(0) | 91 , fTexCoordSetCnt(0) |
| 91 , fProgramID(0) | 92 , fProgramID(0) |
| 92 , fFS(this, desc) | 93 , fFS(this, desc) |
| 93 , fSeparableVaryingInfos(kVarsPerBlock) | 94 , fSeparableVaryingInfos(kVarsPerBlock) |
| 95 , fGrProcessorEmitter(this) |
| 94 , fDesc(desc) | 96 , fDesc(desc) |
| 95 , fGpu(gpu) | 97 , fGpu(gpu) |
| 96 , fUniforms(kVarsPerBlock) { | 98 , fUniforms(kVarsPerBlock) { |
| 97 } | 99 } |
| 98 | 100 |
| 99 void GrGLProgramBuilder::nameVariable(SkString* out, char prefix, const char* na
me) { | 101 void GrGLProgramBuilder::nameVariable(SkString* out, char prefix, const char* na
me) { |
| 100 if ('\0' == prefix) { | 102 if ('\0' == prefix) { |
| 101 *out = name; | 103 *out = name; |
| 102 } else { | 104 } else { |
| 103 out->printf("%c%s", prefix, name); | 105 out->printf("%c%s", prefix, name); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 void GrGLProgramBuilder::appendUniformDecls(ShaderVisibility visibility, | 154 void GrGLProgramBuilder::appendUniformDecls(ShaderVisibility visibility, |
| 153 SkString* out) const { | 155 SkString* out) const { |
| 154 for (int i = 0; i < fUniforms.count(); ++i) { | 156 for (int i = 0; i < fUniforms.count(); ++i) { |
| 155 if (fUniforms[i].fVisibility & visibility) { | 157 if (fUniforms[i].fVisibility & visibility) { |
| 156 fUniforms[i].fVariable.appendDecl(this->ctxInfo(), out); | 158 fUniforms[i].fVariable.appendDecl(this->ctxInfo(), out); |
| 157 out->append(";\n"); | 159 out->append(";\n"); |
| 158 } | 160 } |
| 159 } | 161 } |
| 160 } | 162 } |
| 161 | 163 |
| 162 void GrGLProgramBuilder::createAndEmitEffects(const GrEffectStage* effectStages[
], | 164 void GrGLProgramBuilder::createAndEmitEffects(const GrFragmentStage* effectStage
s[], |
| 163 int effectCnt, | 165 int effectCnt, |
| 164 const GrGLProgramDesc::EffectKeyPr
ovider& keyProvider, | 166 const GrGLProgramDesc::EffectKeyPr
ovider& keyProvider, |
| 165 GrGLSLExpr4* fsInOutColor) { | 167 GrGLSLExpr4* fsInOutColor) { |
| 166 bool effectEmitted = false; | 168 bool effectEmitted = false; |
| 167 | 169 |
| 168 GrGLSLExpr4 inColor = *fsInOutColor; | 170 GrGLSLExpr4 inColor = *fsInOutColor; |
| 169 GrGLSLExpr4 outColor; | 171 GrGLSLExpr4 outColor; |
| 170 | 172 |
| 171 for (int e = 0; e < effectCnt; ++e) { | 173 for (int e = 0; e < effectCnt; ++e) { |
| 174 fGrProcessorEmitter.set(effectStages[e]->getFragmentProcessor()); |
| 175 fEffectEmitter = &fGrProcessorEmitter; |
| 172 // calls into the subclass to emit the actual effect into the program ef
fect object | 176 // calls into the subclass to emit the actual effect into the program ef
fect object |
| 173 this->emitEffect(*effectStages[e], e, keyProvider, &inColor, &outColor); | 177 this->emitEffect(*effectStages[e], e, keyProvider, &inColor, &outColor); |
| 174 effectEmitted = true; | 178 effectEmitted = true; |
| 175 } | 179 } |
| 176 | 180 |
| 177 if (effectEmitted) { | 181 if (effectEmitted) { |
| 178 *fsInOutColor = outColor; | 182 *fsInOutColor = outColor; |
| 179 } | 183 } |
| 180 } | 184 } |
| 181 | 185 |
| 182 void GrGLProgramBuilder::emitEffect(const GrEffectStage& effectStage, | 186 void GrGLProgramBuilder::emitEffect(const GrProcessorStage& effectStage, |
| 183 int effectIndex, | 187 int effectIndex, |
| 184 const GrGLProgramDesc::EffectKeyProvider& ke
yProvider, | 188 const GrGLProgramDesc::EffectKeyProvider& ke
yProvider, |
| 185 GrGLSLExpr4* inColor, | 189 GrGLSLExpr4* inColor, |
| 186 GrGLSLExpr4* outColor) { | 190 GrGLSLExpr4* outColor) { |
| 187 SkASSERT(effectStage.getEffect()); | 191 SkASSERT(effectStage.getProcessor()); |
| 188 CodeStage::AutoStageRestore csar(&fCodeStage, &effectStage); | 192 CodeStage::AutoStageRestore csar(&fCodeStage, &effectStage); |
| 189 | 193 |
| 190 if (inColor->isZeros()) { | 194 if (inColor->isZeros()) { |
| 191 SkString inColorName; | 195 SkString inColorName; |
| 192 | 196 |
| 193 // Effects have no way to communicate zeros, they treat an empty string
as ones. | 197 // Effects have no way to communicate zeros, they treat an empty string
as ones. |
| 194 this->nameVariable(&inColorName, '\0', "input"); | 198 this->nameVariable(&inColorName, '\0', "input"); |
| 195 fFS.codeAppendf("\tvec4 %s = %s;\n", inColorName.c_str(), inColor->c_str
()); | 199 fFS.codeAppendf("\tvec4 %s = %s;\n", inColorName.c_str(), inColor->c_str
()); |
| 196 *inColor = inColorName; | 200 *inColor = inColorName; |
| 197 } | 201 } |
| 198 | 202 |
| 199 // create var to hold stage result | 203 // create var to hold stage result |
| 200 SkString outColorName; | 204 SkString outColorName; |
| 201 this->nameVariable(&outColorName, '\0', "output"); | 205 this->nameVariable(&outColorName, '\0', "output"); |
| 202 fFS.codeAppendf("\tvec4 %s;\n", outColorName.c_str()); | 206 fFS.codeAppendf("\tvec4 %s;\n", outColorName.c_str()); |
| 203 *outColor = outColorName; | 207 *outColor = outColorName; |
| 204 | 208 |
| 205 this->emitEffect(effectStage, keyProvider.get(effectIndex), outColor->c_str(
), | 209 this->emitEffect(effectStage, keyProvider.get(effectIndex), outColor->c_str(
), |
| 206 inColor->isOnes() ? NULL : inColor->c_str(), fCodeStage.sta
geIndex()); | 210 inColor->isOnes() ? NULL : inColor->c_str(), fCodeStage.sta
geIndex()); |
| 207 | 211 |
| 208 *inColor = *outColor; | 212 *inColor = *outColor; |
| 209 } | 213 } |
| 210 | 214 |
| 211 void GrGLProgramBuilder::emitSamplers(const GrEffect& effect, | 215 void GrGLProgramBuilder::emitSamplers(const GrProcessor& effect, |
| 212 GrGLEffect::TextureSamplerArray* outSample
rs) { | 216 GrGLProcessor::TextureSamplerArray* outSam
plers) { |
| 213 SkTArray<GrGLProgramEffects::Sampler, true>& samplers = | 217 SkTArray<GrGLProgramEffects::Sampler, true>& samplers = |
| 214 this->getProgramEffects()->addSamplers(); | 218 this->getProgramEffects()->addSamplers(); |
| 215 int numTextures = effect.numTextures(); | 219 int numTextures = effect.numTextures(); |
| 216 samplers.push_back_n(numTextures); | 220 samplers.push_back_n(numTextures); |
| 217 SkString name; | 221 SkString name; |
| 218 for (int t = 0; t < numTextures; ++t) { | 222 for (int t = 0; t < numTextures; ++t) { |
| 219 name.printf("Sampler%d", t); | 223 name.printf("Sampler%d", t); |
| 220 samplers[t].fUniform = this->addUniform(GrGLProgramBuilder::kFragment_Vi
sibility, | 224 samplers[t].fUniform = this->addUniform(GrGLProgramBuilder::kFragment_Vi
sibility, |
| 221 kSampler2D_GrSLType, | 225 kSampler2D_GrSLType, |
| 222 name.c_str()); | 226 name.c_str()); |
| 223 SkNEW_APPEND_TO_TARRAY(outSamplers, GrGLEffect::TextureSampler, | 227 SkNEW_APPEND_TO_TARRAY(outSamplers, GrGLProcessor::TextureSampler, |
| 224 (samplers[t].fUniform, effect.textureAccess(t))); | 228 (samplers[t].fUniform, effect.textureAccess(t))); |
| 225 } | 229 } |
| 226 } | 230 } |
| 227 | 231 |
| 228 bool GrGLProgramBuilder::finish() { | 232 bool GrGLProgramBuilder::finish() { |
| 229 SkASSERT(0 == fProgramID); | 233 SkASSERT(0 == fProgramID); |
| 230 GL_CALL_RET(fProgramID, CreateProgram()); | 234 GL_CALL_RET(fProgramID, CreateProgram()); |
| 231 if (!fProgramID) { | 235 if (!fProgramID) { |
| 232 return false; | 236 return false; |
| 233 } | 237 } |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 GetProgramResourceLocation(programId, | 323 GetProgramResourceLocation(programId, |
| 320 GR_GL_FRAGMENT_INPUT, | 324 GR_GL_FRAGMENT_INPUT, |
| 321 fSeparableVaryingInfos[i].fVariab
le.c_str())); | 325 fSeparableVaryingInfos[i].fVariab
le.c_str())); |
| 322 fSeparableVaryingInfos[i].fLocation = location; | 326 fSeparableVaryingInfos[i].fLocation = location; |
| 323 } | 327 } |
| 324 } | 328 } |
| 325 | 329 |
| 326 const GrGLContextInfo& GrGLProgramBuilder::ctxInfo() const { | 330 const GrGLContextInfo& GrGLProgramBuilder::ctxInfo() const { |
| 327 return fGpu->ctxInfo(); | 331 return fGpu->ctxInfo(); |
| 328 } | 332 } |
| OLD | NEW |