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

Side by Side Diff: src/gpu/gl/GrGLProgram.cpp

Issue 302663006: Incremental refactoring of GrGLProgram and GrGLShaderBuilder (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: rebase Created 6 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « src/gpu/gl/GrGLProgram.h ('k') | src/gpu/gl/GrGLShaderBuilder.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 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 "GrGLProgram.h" 8 #include "GrGLProgram.h"
9 9
10 #include "GrAllocator.h" 10 #include "GrAllocator.h"
11 #include "GrEffect.h" 11 #include "GrEffect.h"
12 #include "GrCoordTransform.h" 12 #include "GrCoordTransform.h"
13 #include "GrDrawEffect.h" 13 #include "GrDrawEffect.h"
14 #include "GrGLEffect.h" 14 #include "GrGLEffect.h"
15 #include "GrGpuGL.h" 15 #include "GrGpuGL.h"
16 #include "GrGLShaderVar.h" 16 #include "GrGLShaderVar.h"
17 #include "GrGLSL.h" 17 #include "GrGLSL.h"
18 #include "SkXfermode.h" 18 #include "SkXfermode.h"
19 19
20 #define GL_CALL(X) GR_GL_CALL(fGpu->glInterface(), X) 20 #define GL_CALL(X) GR_GL_CALL(fGpu->glInterface(), X)
21 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fGpu->glInterface(), R, X) 21 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fGpu->glInterface(), R, X)
22 22
23 GrGLProgram* GrGLProgram::Create(GrGpuGL* gpu, 23 GrGLProgram* GrGLProgram::Create(GrGpuGL* gpu,
24 const GrGLProgramDesc& desc, 24 const GrGLProgramDesc& desc,
25 const GrEffectStage* colorStages[], 25 const GrEffectStage* colorStages[],
26 const GrEffectStage* coverageStages[]) { 26 const GrEffectStage* coverageStages[]) {
27 GrGLProgram* program = SkNEW_ARGS(GrGLProgram, (gpu, desc, colorStages, cove rageStages)); 27 GrGLShaderBuilder::GenProgramOutput output;
28 if (!program->succeeded()) { 28 SkAutoTUnref<GrGLUniformManager> uman(SkNEW_ARGS(GrGLUniformManager, (gpu))) ;
29 delete program; 29 if (GrGLShaderBuilder::GenProgram(gpu, uman, desc, colorStages, coverageStag es,
30 program = NULL; 30 &output)) {
31 SkASSERT(0 != output.fProgramID);
32 return SkNEW_ARGS(GrGLProgram, (gpu, desc, uman, output));
31 } 33 }
32 return program; 34 return NULL;
33 } 35 }
34 36
35 GrGLProgram::GrGLProgram(GrGpuGL* gpu, 37 GrGLProgram::GrGLProgram(GrGpuGL* gpu,
36 const GrGLProgramDesc& desc, 38 const GrGLProgramDesc& desc,
37 const GrEffectStage* colorStages[], 39 GrGLUniformManager* uman,
38 const GrEffectStage* coverageStages[]) 40 const GrGLShaderBuilder::GenProgramOutput& builderOutpu t)
39 : fGpu(gpu) 41 : fProgramID(builderOutput.fProgramID)
40 , fUniformManager(gpu) 42 , fColor(GrColor_ILLEGAL)
41 , fHasVertexShader(false) 43 , fCoverage(GrColor_ILLEGAL)
42 , fNumTexCoordSets(0) { 44 , fDstCopyTexUnit(-1)
43 fDesc = desc; 45 , fColorEffects(builderOutput.fColorEffects)
44 fProgramID = 0; 46 , fCoverageEffects(builderOutput.fCoverageEffects)
45 47 , fDesc(desc)
46 fDstCopyTexUnit = -1; 48 , fGpu(gpu)
47 49 , fUniformManager(SkRef(uman))
48 fColor = GrColor_ILLEGAL; 50 , fUniformHandles(builderOutput.fUniformHandles)
49 51 , fHasVertexShader(builderOutput.fHasVS)
50 GrGLShaderBuilder::GenProgramOutput output; 52 , fNumTexCoordSets(builderOutput.fNumTexCoordSets) {
51 53 this->initSamplerUniforms();
52 if (GrGLShaderBuilder::GenProgram(gpu, fUniformManager, desc, colorStages, c overageStages,
53 &output)) {
54 fProgramID = output.fProgramID;
55 fUniformHandles = output.fUniformHandles;
56 fColorEffects.reset(output.fColorEffects);
57 fCoverageEffects.reset(output.fCoverageEffects);
58 fHasVertexShader = output.fHasVS;
59 fNumTexCoordSets = output.fNumTexCoordSets;
60 fGpu = gpu;
61 this->initSamplerUniforms();
62 }
63 } 54 }
64 55
65 GrGLProgram::~GrGLProgram() { 56 GrGLProgram::~GrGLProgram() {
66 if (fProgramID) { 57 if (fProgramID) {
67 GL_CALL(DeleteProgram(fProgramID)); 58 GL_CALL(DeleteProgram(fProgramID));
68 } 59 }
69 } 60 }
70 61
71 void GrGLProgram::abandon() { 62 void GrGLProgram::abandon() {
72 fProgramID = 0; 63 fProgramID = 0;
(...skipping 18 matching lines...) Expand all
91 default: 82 default:
92 SkFAIL("Unexpected coverage output"); 83 SkFAIL("Unexpected coverage output");
93 break; 84 break;
94 } 85 }
95 } 86 }
96 87
97 void GrGLProgram::initSamplerUniforms() { 88 void GrGLProgram::initSamplerUniforms() {
98 GL_CALL(UseProgram(fProgramID)); 89 GL_CALL(UseProgram(fProgramID));
99 GrGLint texUnitIdx = 0; 90 GrGLint texUnitIdx = 0;
100 if (fUniformHandles.fDstCopySamplerUni.isValid()) { 91 if (fUniformHandles.fDstCopySamplerUni.isValid()) {
101 fUniformManager.setSampler(fUniformHandles.fDstCopySamplerUni, texUnitId x); 92 fUniformManager->setSampler(fUniformHandles.fDstCopySamplerUni, texUnitI dx);
102 fDstCopyTexUnit = texUnitIdx++; 93 fDstCopyTexUnit = texUnitIdx++;
103 } 94 }
104 fColorEffects->initSamplers(fUniformManager, &texUnitIdx); 95 fColorEffects->initSamplers(*fUniformManager, &texUnitIdx);
105 fCoverageEffects->initSamplers(fUniformManager, &texUnitIdx); 96 fCoverageEffects->initSamplers(*fUniformManager, &texUnitIdx);
106 } 97 }
107 98
108 /////////////////////////////////////////////////////////////////////////////// 99 ///////////////////////////////////////////////////////////////////////////////
109 100
110 void GrGLProgram::setData(GrDrawState::BlendOptFlags blendOpts, 101 void GrGLProgram::setData(GrDrawState::BlendOptFlags blendOpts,
111 const GrEffectStage* colorStages[], 102 const GrEffectStage* colorStages[],
112 const GrEffectStage* coverageStages[], 103 const GrEffectStage* coverageStages[],
113 const GrDeviceCoordTexture* dstCopy, 104 const GrDeviceCoordTexture* dstCopy,
114 SharedGLState* sharedState) { 105 SharedGLState* sharedState) {
115 const GrDrawState& drawState = fGpu->getDrawState(); 106 const GrDrawState& drawState = fGpu->getDrawState();
(...skipping 10 matching lines...) Expand all
126 color = drawState.getColor(); 117 color = drawState.getColor();
127 coverage = drawState.getCoverageColor(); 118 coverage = drawState.getCoverageColor();
128 } 119 }
129 120
130 this->setColor(drawState, color, sharedState); 121 this->setColor(drawState, color, sharedState);
131 this->setCoverage(drawState, coverage, sharedState); 122 this->setCoverage(drawState, coverage, sharedState);
132 this->setMatrixAndRenderTargetHeight(drawState); 123 this->setMatrixAndRenderTargetHeight(drawState);
133 124
134 if (NULL != dstCopy) { 125 if (NULL != dstCopy) {
135 if (fUniformHandles.fDstCopyTopLeftUni.isValid()) { 126 if (fUniformHandles.fDstCopyTopLeftUni.isValid()) {
136 fUniformManager.set2f(fUniformHandles.fDstCopyTopLeftUni, 127 fUniformManager->set2f(fUniformHandles.fDstCopyTopLeftUni,
137 static_cast<GrGLfloat>(dstCopy->offset().fX), 128 static_cast<GrGLfloat>(dstCopy->offset().fX),
138 static_cast<GrGLfloat>(dstCopy->offset().fY)); 129 static_cast<GrGLfloat>(dstCopy->offset().fY)) ;
139 fUniformManager.set2f(fUniformHandles.fDstCopyScaleUni, 130 fUniformManager->set2f(fUniformHandles.fDstCopyScaleUni,
140 1.f / dstCopy->texture()->width(), 131 1.f / dstCopy->texture()->width(),
141 1.f / dstCopy->texture()->height()); 132 1.f / dstCopy->texture()->height());
142 GrGLTexture* texture = static_cast<GrGLTexture*>(dstCopy->texture()) ; 133 GrGLTexture* texture = static_cast<GrGLTexture*>(dstCopy->texture()) ;
143 static GrTextureParams kParams; // the default is clamp, nearest fil tering. 134 static GrTextureParams kParams; // the default is clamp, nearest fil tering.
144 fGpu->bindTexture(fDstCopyTexUnit, kParams, texture); 135 fGpu->bindTexture(fDstCopyTexUnit, kParams, texture);
145 } else { 136 } else {
146 SkASSERT(!fUniformHandles.fDstCopyScaleUni.isValid()); 137 SkASSERT(!fUniformHandles.fDstCopyScaleUni.isValid());
147 SkASSERT(!fUniformHandles.fDstCopySamplerUni.isValid()); 138 SkASSERT(!fUniformHandles.fDstCopySamplerUni.isValid());
148 } 139 }
149 } else { 140 } else {
150 SkASSERT(!fUniformHandles.fDstCopyTopLeftUni.isValid()); 141 SkASSERT(!fUniformHandles.fDstCopyTopLeftUni.isValid());
151 SkASSERT(!fUniformHandles.fDstCopyScaleUni.isValid()); 142 SkASSERT(!fUniformHandles.fDstCopyScaleUni.isValid());
152 SkASSERT(!fUniformHandles.fDstCopySamplerUni.isValid()); 143 SkASSERT(!fUniformHandles.fDstCopySamplerUni.isValid());
153 } 144 }
154 145
155 fColorEffects->setData(fGpu, fUniformManager, colorStages); 146 fColorEffects->setData(fGpu, *fUniformManager, colorStages);
156 fCoverageEffects->setData(fGpu, fUniformManager, coverageStages); 147 fCoverageEffects->setData(fGpu, *fUniformManager, coverageStages);
157 148
158 149
159 // PathTexGen state applies to the the fixed function vertex shader. For 150 // PathTexGen state applies to the the fixed function vertex shader. For
160 // custom shaders, it's ignored, so we don't need to change the texgen 151 // custom shaders, it's ignored, so we don't need to change the texgen
161 // settings in that case. 152 // settings in that case.
162 if (!fHasVertexShader) { 153 if (!fHasVertexShader) {
163 fGpu->flushPathTexGenSettings(fNumTexCoordSets); 154 fGpu->flushPathTexGenSettings(fNumTexCoordSets);
164 } 155 }
165 } 156 }
166 157
(...skipping 13 matching lines...) Expand all
180 GL_CALL(VertexAttrib4fv(header.fColorAttributeIndex, c)); 171 GL_CALL(VertexAttrib4fv(header.fColorAttributeIndex, c));
181 sharedState->fConstAttribColor = color; 172 sharedState->fConstAttribColor = color;
182 sharedState->fConstAttribColorIndex = header.fColorAttribute Index; 173 sharedState->fConstAttribColorIndex = header.fColorAttribute Index;
183 } 174 }
184 break; 175 break;
185 case GrGLProgramDesc::kUniform_ColorInput: 176 case GrGLProgramDesc::kUniform_ColorInput:
186 if (fColor != color && fUniformHandles.fColorUni.isValid()) { 177 if (fColor != color && fUniformHandles.fColorUni.isValid()) {
187 // OpenGL ES doesn't support unsigned byte varieties of glUn iform 178 // OpenGL ES doesn't support unsigned byte varieties of glUn iform
188 GrGLfloat c[4]; 179 GrGLfloat c[4];
189 GrColorToRGBAFloat(color, c); 180 GrColorToRGBAFloat(color, c);
190 fUniformManager.set4fv(fUniformHandles.fColorUni, 1, c); 181 fUniformManager->set4fv(fUniformHandles.fColorUni, 1, c);
191 fColor = color; 182 fColor = color;
192 } 183 }
193 sharedState->fConstAttribColorIndex = -1; 184 sharedState->fConstAttribColorIndex = -1;
194 break; 185 break;
195 case GrGLProgramDesc::kSolidWhite_ColorInput: 186 case GrGLProgramDesc::kSolidWhite_ColorInput:
196 case GrGLProgramDesc::kTransBlack_ColorInput: 187 case GrGLProgramDesc::kTransBlack_ColorInput:
197 sharedState->fConstAttribColorIndex = -1; 188 sharedState->fConstAttribColorIndex = -1;
198 break; 189 break;
199 default: 190 default:
200 SkFAIL("Unknown color type."); 191 SkFAIL("Unknown color type.");
(...skipping 18 matching lines...) Expand all
219 GL_CALL(VertexAttrib4fv(header.fCoverageAttributeIndex, c)); 210 GL_CALL(VertexAttrib4fv(header.fCoverageAttributeIndex, c));
220 sharedState->fConstAttribCoverage = coverage; 211 sharedState->fConstAttribCoverage = coverage;
221 sharedState->fConstAttribCoverageIndex = header.fCoverageAtt ributeIndex; 212 sharedState->fConstAttribCoverageIndex = header.fCoverageAtt ributeIndex;
222 } 213 }
223 break; 214 break;
224 case GrGLProgramDesc::kUniform_ColorInput: 215 case GrGLProgramDesc::kUniform_ColorInput:
225 if (fCoverage != coverage) { 216 if (fCoverage != coverage) {
226 // OpenGL ES doesn't support unsigned byte varieties of glUn iform 217 // OpenGL ES doesn't support unsigned byte varieties of glUn iform
227 GrGLfloat c[4]; 218 GrGLfloat c[4];
228 GrColorToRGBAFloat(coverage, c); 219 GrColorToRGBAFloat(coverage, c);
229 fUniformManager.set4fv(fUniformHandles.fCoverageUni, 1, c); 220 fUniformManager->set4fv(fUniformHandles.fCoverageUni, 1, c);
230 fCoverage = coverage; 221 fCoverage = coverage;
231 } 222 }
232 sharedState->fConstAttribCoverageIndex = -1; 223 sharedState->fConstAttribCoverageIndex = -1;
233 break; 224 break;
234 case GrGLProgramDesc::kSolidWhite_ColorInput: 225 case GrGLProgramDesc::kSolidWhite_ColorInput:
235 case GrGLProgramDesc::kTransBlack_ColorInput: 226 case GrGLProgramDesc::kTransBlack_ColorInput:
236 sharedState->fConstAttribCoverageIndex = -1; 227 sharedState->fConstAttribCoverageIndex = -1;
237 break; 228 break;
238 default: 229 default:
239 SkFAIL("Unknown coverage type."); 230 SkFAIL("Unknown coverage type.");
240 } 231 }
241 } else { 232 } else {
242 sharedState->fConstAttribCoverageIndex = -1; 233 sharedState->fConstAttribCoverageIndex = -1;
243 } 234 }
244 } 235 }
245 236
246 void GrGLProgram::setMatrixAndRenderTargetHeight(const GrDrawState& drawState) { 237 void GrGLProgram::setMatrixAndRenderTargetHeight(const GrDrawState& drawState) {
247 const GrRenderTarget* rt = drawState.getRenderTarget(); 238 const GrRenderTarget* rt = drawState.getRenderTarget();
248 SkISize size; 239 SkISize size;
249 size.set(rt->width(), rt->height()); 240 size.set(rt->width(), rt->height());
250 241
251 // Load the RT height uniform if it is needed to y-flip gl_FragCoord. 242 // Load the RT height uniform if it is needed to y-flip gl_FragCoord.
252 if (fUniformHandles.fRTHeightUni.isValid() && 243 if (fUniformHandles.fRTHeightUni.isValid() &&
253 fMatrixState.fRenderTargetSize.fHeight != size.fHeight) { 244 fMatrixState.fRenderTargetSize.fHeight != size.fHeight) {
254 fUniformManager.set1f(fUniformHandles.fRTHeightUni, SkIntToScalar(size.f Height)); 245 fUniformManager->set1f(fUniformHandles.fRTHeightUni, SkIntToScalar(size. fHeight));
255 } 246 }
256 247
257 if (!fHasVertexShader) { 248 if (!fHasVertexShader) {
258 SkASSERT(!fUniformHandles.fViewMatrixUni.isValid()); 249 SkASSERT(!fUniformHandles.fViewMatrixUni.isValid());
259 SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid()); 250 SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid());
260 fGpu->setProjectionMatrix(drawState.getViewMatrix(), size, rt->origin()) ; 251 fGpu->setProjectionMatrix(drawState.getViewMatrix(), size, rt->origin()) ;
261 } else if (fMatrixState.fRenderTargetOrigin != rt->origin() || 252 } else if (fMatrixState.fRenderTargetOrigin != rt->origin() ||
262 fMatrixState.fRenderTargetSize != size || 253 fMatrixState.fRenderTargetSize != size ||
263 !fMatrixState.fViewMatrix.cheapEqualTo(drawState.getViewMatrix()) ) { 254 !fMatrixState.fViewMatrix.cheapEqualTo(drawState.getViewMatrix()) ) {
264 SkASSERT(fUniformHandles.fViewMatrixUni.isValid()); 255 SkASSERT(fUniformHandles.fViewMatrixUni.isValid());
265 256
266 fMatrixState.fViewMatrix = drawState.getViewMatrix(); 257 fMatrixState.fViewMatrix = drawState.getViewMatrix();
267 fMatrixState.fRenderTargetSize = size; 258 fMatrixState.fRenderTargetSize = size;
268 fMatrixState.fRenderTargetOrigin = rt->origin(); 259 fMatrixState.fRenderTargetOrigin = rt->origin();
269 260
270 GrGLfloat viewMatrix[3 * 3]; 261 GrGLfloat viewMatrix[3 * 3];
271 fMatrixState.getGLMatrix<3>(viewMatrix); 262 fMatrixState.getGLMatrix<3>(viewMatrix);
272 fUniformManager.setMatrix3f(fUniformHandles.fViewMatrixUni, viewMatrix); 263 fUniformManager->setMatrix3f(fUniformHandles.fViewMatrixUni, viewMatrix) ;
273 264
274 GrGLfloat rtAdjustmentVec[4]; 265 GrGLfloat rtAdjustmentVec[4];
275 fMatrixState.getRTAdjustmentVec(rtAdjustmentVec); 266 fMatrixState.getRTAdjustmentVec(rtAdjustmentVec);
276 fUniformManager.set4fv(fUniformHandles.fRTAdjustmentUni, 1, rtAdjustment Vec); 267 fUniformManager->set4fv(fUniformHandles.fRTAdjustmentUni, 1, rtAdjustmen tVec);
277 } 268 }
278 } 269 }
OLDNEW
« no previous file with comments | « src/gpu/gl/GrGLProgram.h ('k') | src/gpu/gl/GrGLShaderBuilder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698