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 "GrGLProgramBuilder.h" | 8 #include "GrGLProgramBuilder.h" |
9 #include "gl/GrGLProgram.h" | 9 #include "gl/GrGLProgram.h" |
10 #include "gl/GrGLSLPrettyPrint.h" | 10 #include "gl/GrGLSLPrettyPrint.h" |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 } | 133 } |
134 if (!fOutOfStage) { | 134 if (!fOutOfStage) { |
135 if (out->endsWith('_')) { | 135 if (out->endsWith('_')) { |
136 // Names containing "__" are reserved. | 136 // Names containing "__" are reserved. |
137 out->append("x"); | 137 out->append("x"); |
138 } | 138 } |
139 out->appendf("_Stage%d", fStageIndex); | 139 out->appendf("_Stage%d", fStageIndex); |
140 } | 140 } |
141 } | 141 } |
142 | 142 |
143 GrGLProgramDataManager::UniformHandle GrGLProgramBuilder::addUniformArray(uint32
_t visibility, | 143 GrGLProgramDataManager::UniformHandle GrGLProgramBuilder::addUniformArray( |
144 GrSLTy
pe type, | 144 uint32_t visibil
ity, |
145 const
char* name, | 145 GrSLType type, |
146 int co
unt, | 146 GrSLPrecision pr
ecision, |
147 const
char** outName) { | 147 const char* name
, |
| 148 int count, |
| 149 const char** out
Name) { |
148 SkASSERT(name && strlen(name)); | 150 SkASSERT(name && strlen(name)); |
149 SkDEBUGCODE(static const uint32_t kVisibilityMask = kVertex_Visibility | kFr
agment_Visibility); | 151 SkDEBUGCODE(static const uint32_t kVisibilityMask = kVertex_Visibility | kFr
agment_Visibility); |
150 SkASSERT(0 == (~kVisibilityMask & visibility)); | 152 SkASSERT(0 == (~kVisibilityMask & visibility)); |
151 SkASSERT(0 != visibility); | 153 SkASSERT(0 != visibility); |
| 154 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type)); |
152 | 155 |
153 UniformInfo& uni = fUniforms.push_back(); | 156 UniformInfo& uni = fUniforms.push_back(); |
154 uni.fVariable.setType(type); | 157 uni.fVariable.setType(type); |
155 uni.fVariable.setTypeModifier(GrGLShaderVar::kUniform_TypeModifier); | 158 uni.fVariable.setTypeModifier(GrGLShaderVar::kUniform_TypeModifier); |
156 // TODO this is a bit hacky, lets think of a better way. Basically we need
to be able to use | 159 // TODO this is a bit hacky, lets think of a better way. Basically we need
to be able to use |
157 // the uniform view matrix name in the GP, and the GP is immutable so it has
to tell the PB | 160 // the uniform view matrix name in the GP, and the GP is immutable so it has
to tell the PB |
158 // exactly what name it wants to use for the uniform view matrix. If we pre
fix anythings, then | 161 // exactly what name it wants to use for the uniform view matrix. If we pre
fix anythings, then |
159 // the names will mismatch. I think the correct solution is to have all GPs
which need the | 162 // the names will mismatch. I think the correct solution is to have all GPs
which need the |
160 // uniform view matrix, they should upload the view matrix in their setData
along with regular | 163 // uniform view matrix, they should upload the view matrix in their setData
along with regular |
161 // uniforms. | 164 // uniforms. |
162 char prefix = 'u'; | 165 char prefix = 'u'; |
163 if ('u' == name[0]) { | 166 if ('u' == name[0]) { |
164 prefix = '\0'; | 167 prefix = '\0'; |
165 } | 168 } |
166 this->nameVariable(uni.fVariable.accessName(), prefix, name); | 169 this->nameVariable(uni.fVariable.accessName(), prefix, name); |
167 uni.fVariable.setArrayCount(count); | 170 uni.fVariable.setArrayCount(count); |
168 uni.fVisibility = visibility; | 171 uni.fVisibility = visibility; |
169 | 172 uni.fVariable.setPrecision(precision); |
170 // If it is visible in both the VS and FS, the precision must match. | |
171 // We declare a default FS precision, but not a default VS. So set the var | |
172 // to use the default FS precision. | |
173 if ((kVertex_Visibility | kFragment_Visibility) == visibility) { | |
174 // the fragment and vertex precisions must match | |
175 uni.fVariable.setPrecision(kDefault_GrSLPrecision); | |
176 } | |
177 | 173 |
178 if (outName) { | 174 if (outName) { |
179 *outName = uni.fVariable.c_str(); | 175 *outName = uni.fVariable.c_str(); |
180 } | 176 } |
181 return GrGLProgramDataManager::UniformHandle::CreateFromUniformIndex(fUnifor
ms.count() - 1); | 177 return GrGLProgramDataManager::UniformHandle::CreateFromUniformIndex(fUnifor
ms.count() - 1); |
182 } | 178 } |
183 | 179 |
184 void GrGLProgramBuilder::appendUniformDecls(ShaderVisibility visibility, | 180 void GrGLProgramBuilder::appendUniformDecls(ShaderVisibility visibility, |
185 SkString* out) const { | 181 SkString* out) const { |
186 for (int i = 0; i < fUniforms.count(); ++i) { | 182 for (int i = 0; i < fUniforms.count(); ++i) { |
187 if (fUniforms[i].fVisibility & visibility) { | 183 if (fUniforms[i].fVisibility & visibility) { |
188 fUniforms[i].fVariable.appendDecl(this->ctxInfo(), out); | 184 fUniforms[i].fVariable.appendDecl(this->ctxInfo(), out); |
189 out->append(";\n"); | 185 out->append(";\n"); |
190 } | 186 } |
191 } | 187 } |
192 } | 188 } |
193 | 189 |
194 const GrGLContextInfo& GrGLProgramBuilder::ctxInfo() const { | 190 const GrGLContextInfo& GrGLProgramBuilder::ctxInfo() const { |
195 return fGpu->ctxInfo(); | 191 return fGpu->ctxInfo(); |
196 } | 192 } |
197 | 193 |
198 void GrGLProgramBuilder::setupUniformColorAndCoverageIfNeeded(GrGLSLExpr4* input
Color, | 194 void GrGLProgramBuilder::setupUniformColorAndCoverageIfNeeded(GrGLSLExpr4* input
Color, |
199 GrGLSLExpr1* input
Coverage) { | 195 GrGLSLExpr1* input
Coverage) { |
200 const GrProgramDesc::KeyHeader& header = this->header(); | 196 const GrProgramDesc::KeyHeader& header = this->header(); |
201 if (GrProgramDesc::kUniform_ColorInput == header.fColorInput) { | 197 if (GrProgramDesc::kUniform_ColorInput == header.fColorInput) { |
202 const char* name; | 198 const char* name; |
203 fUniformHandles.fColorUni = | 199 fUniformHandles.fColorUni = |
204 this->addUniform(GrGLProgramBuilder::kFragment_Visibility, | 200 this->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
205 kVec4f_GrSLType, | 201 kVec4f_GrSLType, kDefault_GrSLPrecision, |
206 "Color", | 202 "Color", &name); |
207 &name); | |
208 *inputColor = GrGLSLExpr4(name); | 203 *inputColor = GrGLSLExpr4(name); |
209 } else if (GrProgramDesc::kAllOnes_ColorInput == header.fColorInput) { | 204 } else if (GrProgramDesc::kAllOnes_ColorInput == header.fColorInput) { |
210 *inputColor = GrGLSLExpr4(1); | 205 *inputColor = GrGLSLExpr4(1); |
211 } | 206 } |
212 if (GrProgramDesc::kUniform_ColorInput == header.fCoverageInput) { | 207 if (GrProgramDesc::kUniform_ColorInput == header.fCoverageInput) { |
213 const char* name; | 208 const char* name; |
214 fUniformHandles.fCoverageUni = | 209 fUniformHandles.fCoverageUni = |
215 this->addUniform(GrGLProgramBuilder::kFragment_Visibility, | 210 this->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
216 kFloat_GrSLType, | 211 kFloat_GrSLType, kDefault_GrSLPrecision, |
217 "Coverage", | 212 "Coverage",&name); |
218 &name); | |
219 *inputCoverage = GrGLSLExpr1(name); | 213 *inputCoverage = GrGLSLExpr1(name); |
220 } else if (GrProgramDesc::kAllOnes_ColorInput == header.fCoverageInput) { | 214 } else if (GrProgramDesc::kAllOnes_ColorInput == header.fCoverageInput) { |
221 *inputCoverage = GrGLSLExpr1(1); | 215 *inputCoverage = GrGLSLExpr1(1); |
222 } | 216 } |
223 } | 217 } |
224 | 218 |
225 void GrGLProgramBuilder::emitAndInstallProcs(GrGLSLExpr4* inputColor, GrGLSLExpr
4* inputCoverage) { | 219 void GrGLProgramBuilder::emitAndInstallProcs(GrGLSLExpr4* inputColor, GrGLSLExpr
4* inputCoverage) { |
226 if (fOptState.hasGeometryProcessor()) { | 220 if (fOptState.hasGeometryProcessor()) { |
227 fVS.setupUniformViewMatrix(); | 221 fVS.setupUniformViewMatrix(); |
228 | 222 |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 const char* uniName = "StageMatrix"; | 384 const char* uniName = "StageMatrix"; |
391 GrSLType varyingType = stage.isPerspectiveCoordTransform(t) ? kVec3f_GrS
LType : | 385 GrSLType varyingType = stage.isPerspectiveCoordTransform(t) ? kVec3f_GrS
LType : |
392 kVec2f_GrS
LType; | 386 kVec2f_GrS
LType; |
393 SkString suffixedUniName; | 387 SkString suffixedUniName; |
394 if (0 != t) { | 388 if (0 != t) { |
395 suffixedUniName.append(uniName); | 389 suffixedUniName.append(uniName); |
396 suffixedUniName.appendf("_%i", t); | 390 suffixedUniName.appendf("_%i", t); |
397 uniName = suffixedUniName.c_str(); | 391 uniName = suffixedUniName.c_str(); |
398 } | 392 } |
399 ifp->fTransforms[t].fHandle = this->addUniform(GrGLProgramBuilder::kVert
ex_Visibility, | 393 ifp->fTransforms[t].fHandle = this->addUniform(GrGLProgramBuilder::kVert
ex_Visibility, |
400 kMat33f_GrSLType, | 394 kMat33f_GrSLType, kDefaul
t_GrSLPrecision, |
401 uniName, | 395 uniName, |
402 &uniName).toShaderBuilder
Index(); | 396 &uniName).toShaderBuilder
Index(); |
403 | 397 |
404 const char* varyingName = "MatrixCoord"; | 398 const char* varyingName = "MatrixCoord"; |
405 SkString suffixedVaryingName; | 399 SkString suffixedVaryingName; |
406 if (0 != t) { | 400 if (0 != t) { |
407 suffixedVaryingName.append(varyingName); | 401 suffixedVaryingName.append(varyingName); |
408 suffixedVaryingName.appendf("_%i", t); | 402 suffixedVaryingName.appendf("_%i", t); |
409 varyingName = suffixedVaryingName.c_str(); | 403 varyingName = suffixedVaryingName.c_str(); |
410 } | 404 } |
(...skipping 13 matching lines...) Expand all Loading... |
424 | 418 |
425 void GrGLProgramBuilder::emitSamplers(const GrProcessor& processor, | 419 void GrGLProgramBuilder::emitSamplers(const GrProcessor& processor, |
426 GrGLProcessor::TextureSamplerArray* outSam
plers, | 420 GrGLProcessor::TextureSamplerArray* outSam
plers, |
427 GrGLInstalledProc* ip) { | 421 GrGLInstalledProc* ip) { |
428 int numTextures = processor.numTextures(); | 422 int numTextures = processor.numTextures(); |
429 ip->fSamplers.push_back_n(numTextures); | 423 ip->fSamplers.push_back_n(numTextures); |
430 SkString name; | 424 SkString name; |
431 for (int t = 0; t < numTextures; ++t) { | 425 for (int t = 0; t < numTextures; ++t) { |
432 name.printf("Sampler%d", t); | 426 name.printf("Sampler%d", t); |
433 ip->fSamplers[t].fUniform = this->addUniform(GrGLProgramBuilder::kFragme
nt_Visibility, | 427 ip->fSamplers[t].fUniform = this->addUniform(GrGLProgramBuilder::kFragme
nt_Visibility, |
434 kSampler2D_GrSLType, | 428 kSampler2D_GrSLType, kDefau
lt_GrSLPrecision, |
435 name.c_str()); | 429 name.c_str()); |
436 SkNEW_APPEND_TO_TARRAY(outSamplers, GrGLProcessor::TextureSampler, | 430 SkNEW_APPEND_TO_TARRAY(outSamplers, GrGLProcessor::TextureSampler, |
437 (ip->fSamplers[t].fUniform, processor.textureAcce
ss(t))); | 431 (ip->fSamplers[t].fUniform, processor.textureAcce
ss(t))); |
438 } | 432 } |
439 } | 433 } |
440 | 434 |
441 GrGLProgram* GrGLProgramBuilder::finalize() { | 435 GrGLProgram* GrGLProgramBuilder::finalize() { |
442 // verify we can get a program id | 436 // verify we can get a program id |
443 GrGLuint programID; | 437 GrGLuint programID; |
444 GL_CALL_RET(programID, CreateProgram()); | 438 GL_CALL_RET(programID, CreateProgram()); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
547 } | 541 } |
548 | 542 |
549 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 543 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
550 | 544 |
551 GrGLInstalledFragProcs::~GrGLInstalledFragProcs() { | 545 GrGLInstalledFragProcs::~GrGLInstalledFragProcs() { |
552 int numProcs = fProcs.count(); | 546 int numProcs = fProcs.count(); |
553 for (int e = 0; e < numProcs; ++e) { | 547 for (int e = 0; e < numProcs; ++e) { |
554 SkDELETE(fProcs[e]); | 548 SkDELETE(fProcs[e]); |
555 } | 549 } |
556 } | 550 } |
OLD | NEW |