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

Side by Side Diff: src/gpu/gl/builders/GrGLVertexShaderBuilder.cpp

Issue 611653002: Cleanup of shader building system (Closed) Base URL: https://skia.googlesource.com/skia.git@solo_gp
Patch Set: name changes Created 6 years, 2 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 "GrGLVertexShaderBuilder.h" 8 #include "GrGLVertexShaderBuilder.h"
9 #include "GrGLFullProgramBuilder.h" 9 #include "GrGLProgramBuilder.h"
10 #include "GrGLShaderStringBuilder.h" 10 #include "GrGLShaderStringBuilder.h"
11 #include "../GrGpuGL.h" 11 #include "../GrGpuGL.h"
12 #include "../../GrOptDrawState.h"
13 12
14 #define GL_CALL(X) GR_GL_CALL(gpu->glInterface(), X) 13 #define GL_CALL(X) GR_GL_CALL(fProgramBuilder->gpu()->glInterface(), X)
15 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(gpu->glInterface(), R, X) 14 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fProgramBuilder->gpu()->glInterface(), R, X)
16 15
17 namespace { 16 static const char* color_attribute_name() { return "inColor"; }
18 inline const char* color_attribute_name() { return "inColor"; } 17 static const char* coverage_attribute_name() { return "inCoverage"; }
19 inline const char* coverage_attribute_name() { return "inCoverage"; } 18
19 GrGLVertexShaderBuilder::GrGLVertexShaderBuilder(GrGLProgramBuilder* program)
20 : INHERITED(program)
21 , fPositionVar(NULL)
22 , fLocalCoordsVar(NULL)
23 , fEffectAttribOffset(0) {
20 } 24 }
21 25
22 GrGLVertexShaderBuilder::GrGLVertexShaderBuilder(GrGLFullProgramBuilder* program ) 26 SkString* GrGLVertexShaderBuilder::addVarying(GrSLType type, const char* name,
23 : INHERITED(program) 27 const char** vsOutName) {
24 , fPositionVar(NULL) 28 fOutputs.push_back();
25 , fLocalCoordsVar(NULL) { 29 fOutputs.back().setType(type);
30 fOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier);
31 fProgramBuilder->nameVariable(fOutputs.back().accessName(), 'v', name);
32
33 if (vsOutName) {
34 *vsOutName = fOutputs.back().getName().c_str();
35 }
36 return fOutputs.back().accessName();
26 } 37 }
27 bool GrGLVertexShaderBuilder::addAttribute(const GrShaderVar& var) { 38
28 SkASSERT(GrShaderVar::kAttribute_TypeModifier == var.getTypeModifier()); 39 void GrGLVertexShaderBuilder::setupLocalCoords() {
29 for (int i = 0; i < fInputs.count(); ++i) { 40 fPositionVar = &fInputs.push_back();
30 const GrGLShaderVar& attr = fInputs[i]; 41 fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, " inPosition");
31 // if attribute already added, don't add it again 42 if (-1 != fProgramBuilder->header().fLocalCoordAttributeIndex) {
32 if (attr.getName().equals(var.getName())) { 43 fLocalCoordsVar = &fInputs.push_back();
33 return false; 44 fLocalCoordsVar->set(kVec2f_GrSLType,
34 } 45 GrGLShaderVar::kAttribute_TypeModifier,
46 "inLocalCoords");
47 } else {
48 fLocalCoordsVar = fPositionVar;
35 } 49 }
36 fInputs.push_back(var); 50 fEffectAttribOffset = fInputs.count();
37 return true; 51 }
52
53 void GrGLVertexShaderBuilder::transformGLToSkiaCoords() {
54 const char* viewMName;
55 fProgramBuilder->fUniformHandles.fViewMatrixUni =
56 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility,
57 kMat33f_GrSLType,
58 "ViewM",
59 &viewMName);
60
61 // Transform the position into Skia's device coords.
62 this->codeAppendf("vec3 pos3 = %s * vec3(%s, 1);", viewMName, fPositionVar-> c_str());
63 }
64
65 void GrGLVertexShaderBuilder::setupBuiltinVertexAttribute(const char* inName, Gr GLSLExpr4* out) {
66 SkString name(inName);
67 const char *vsName, *fsName;
68 fProgramBuilder->addVarying(kVec4f_GrSLType, name.c_str(), &vsName, &fsName) ;
69 name.prepend("in");
70 this->addAttribute(GrShaderVar(name.c_str(),
71 kVec4f_GrSLType,
72 GrShaderVar::kAttribute_TypeModifier));
73 this->codeAppendf("%s = %s;", vsName, name.c_str());
74 *out = fsName;
75 fEffectAttribOffset++;
38 } 76 }
39 77
40 void GrGLVertexShaderBuilder::emitAttributes(const GrGeometryProcessor& gp) { 78 void GrGLVertexShaderBuilder::emitAttributes(const GrGeometryProcessor& gp) {
41 const GrGeometryProcessor::VertexAttribArray& vars = gp.getVertexAttribs(); 79 const GrGeometryProcessor::VertexAttribArray& vars = gp.getVertexAttribs();
42 int numAttributes = vars.count(); 80 int numAttributes = vars.count();
43 for (int a = 0; a < numAttributes; ++a) { 81 for (int a = 0; a < numAttributes; ++a) {
44 this->addAttribute(vars[a]); 82 this->addAttribute(vars[a]);
45 } 83 }
46 } 84 }
47 85
48 void GrGLVertexShaderBuilder::addVarying(GrSLType type, const char* name, const char** vsOutName) { 86 void GrGLVertexShaderBuilder::transformSkiaToGLCoords() {
49 fOutputs.push_back(); 87 const char* rtAdjustName;
50 fOutputs.back().setType(type); 88 fProgramBuilder->fUniformHandles.fRTAdjustmentUni =
51 fOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); 89 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility,
52 fProgramBuilder->nameVariable(fOutputs.back().accessName(), 'v', name); 90 kVec4f_GrSLType,
91 "rtAdjustment",
92 &rtAdjustName);
53 93
54 if (vsOutName) { 94 // Transform from Skia's device coords to GL's normalized device coords.
55 *vsOutName = fOutputs.back().getName().c_str(); 95 this->codeAppendf("gl_Position = vec4(dot(pos3.xz, %s.xy), dot(pos3.yz, %s.z w), 0, pos3.z);",
56 } 96 rtAdjustName, rtAdjustName);
57 } 97 }
58 98
59 99 void GrGLVertexShaderBuilder::bindVertexAttributes(GrGLuint programID) {
60 void GrGLVertexShaderBuilder::bindProgramLocations(GrGLuint programId) {
61 const GrGLProgramDesc::KeyHeader& header = fProgramBuilder->desc().getHeader ();
62 GrGpuGL* gpu = fProgramBuilder->gpu();
63
64 // Bind the attrib locations to same values for all shaders 100 // Bind the attrib locations to same values for all shaders
101 const GrGLProgramDesc::KeyHeader& header = fProgramBuilder->header();
65 SkASSERT(-1 != header.fPositionAttributeIndex); 102 SkASSERT(-1 != header.fPositionAttributeIndex);
66 GL_CALL(BindAttribLocation(programId, 103 GL_CALL(BindAttribLocation(programID,
67 header.fPositionAttributeIndex, 104 header.fPositionAttributeIndex,
68 fPositionVar->c_str())); 105 fPositionVar->c_str()));
69 if (-1 != header.fLocalCoordAttributeIndex) { 106 if (-1 != header.fLocalCoordAttributeIndex) {
70 GL_CALL(BindAttribLocation(programId, 107 GL_CALL(BindAttribLocation(programID,
71 header.fLocalCoordAttributeIndex, 108 header.fLocalCoordAttributeIndex,
72 fLocalCoordsVar->c_str())); 109 fLocalCoordsVar->c_str()));
73 } 110 }
74 if (-1 != header.fColorAttributeIndex) { 111 if (-1 != header.fColorAttributeIndex) {
75 GL_CALL(BindAttribLocation(programId, 112 GL_CALL(BindAttribLocation(programID,
76 header.fColorAttributeIndex, 113 header.fColorAttributeIndex,
77 color_attribute_name())); 114 color_attribute_name()));
78 } 115 }
79 if (-1 != header.fCoverageAttributeIndex) { 116 if (-1 != header.fCoverageAttributeIndex) {
80 GL_CALL(BindAttribLocation(programId, 117 GL_CALL(BindAttribLocation(programID,
81 header.fCoverageAttributeIndex, 118 header.fCoverageAttributeIndex,
82 coverage_attribute_name())); 119 coverage_attribute_name()));
83 } 120 }
84 121
85 // We pull the current state of attributes off of drawstate's optimized stat e and bind them in 122 // We pull the current state of attributes off of drawstate's optimized stat e and bind them in
86 // order. This assumes that the drawState has not changed since we called fl ushGraphicsState() 123 // order. This assumes that the drawState has not changed since we called fl ushGraphicsState()
87 // higher up in the stack. 124 // higher up in the stack.
88 const GrDrawTargetCaps* caps = fProgramBuilder->gpu()->caps(); 125 const GrDrawTargetCaps* caps = fProgramBuilder->gpu()->caps();
89 const GrDrawState& drawState = *fProgramBuilder->gpu()->drawState(); 126 const GrDrawState& drawState = *fProgramBuilder->gpu()->drawState();
90 SkAutoTUnref<GrOptDrawState> optState(drawState.createOptState(*caps)); 127 SkAutoTUnref<GrOptDrawState> optState(drawState.createOptState(*caps));
91 const GrVertexAttrib* vaPtr = optState->getVertexAttribs(); 128 const GrVertexAttrib* vaPtr = optState->getVertexAttribs();
92 const int vaCount = optState->getVertexAttribCount(); 129 const int vaCount = optState->getVertexAttribCount();
93 130
131 // We start binding attributes after builtins
94 int i = fEffectAttribOffset; 132 int i = fEffectAttribOffset;
95 for (int index = 0; index < vaCount; index++) { 133 for (int index = 0; index < vaCount; index++) {
96 if (kGeometryProcessor_GrVertexAttribBinding != vaPtr[index].fBinding) { 134 if (kGeometryProcessor_GrVertexAttribBinding != vaPtr[index].fBinding) {
97 continue; 135 continue;
98 } 136 }
99 SkASSERT(index != header.fPositionAttributeIndex && 137 SkASSERT(index != header.fPositionAttributeIndex &&
100 index != header.fLocalCoordAttributeIndex && 138 index != header.fLocalCoordAttributeIndex &&
101 index != header.fColorAttributeIndex && 139 index != header.fColorAttributeIndex &&
102 index != header.fCoverageAttributeIndex); 140 index != header.fCoverageAttributeIndex);
103 // We should never find another effect attribute if we have bound everyt hing 141 // We should never find another effect attribute if we have bound everyt hing
104 SkASSERT(i < fInputs.count()); 142 SkASSERT(i < fInputs.count());
105 GL_CALL(BindAttribLocation(programId, index, fInputs[i].c_str())); 143 GL_CALL(BindAttribLocation(programID, index, fInputs[i].c_str()));
106 i++; 144 i++;
107 } 145 }
108 // Make sure we bound everything 146 // Make sure we bound everything
109 SkASSERT(fInputs.count() == i); 147 SkASSERT(fInputs.count() == i);
110 } 148 }
111 149
112 bool GrGLVertexShaderBuilder::compileAndAttachShaders(GrGLuint programId, 150 bool GrGLVertexShaderBuilder::compileAndAttachShaders(GrGLuint programId,
113 SkTDArray<GrGLuint>* shaderIds) const { 151 SkTDArray<GrGLuint>* shaderIds) const {
114 GrGpuGL* gpu = fProgramBuilder->gpu(); 152 GrGpuGL* gpu = fProgramBuilder->gpu();
115 const GrGLContext& glCtx = gpu->glContext(); 153 const GrGLContext& glCtx = gpu->glContext();
116 const GrGLContextInfo& ctxInfo = gpu->ctxInfo(); 154 const GrGLContextInfo& ctxInfo = gpu->ctxInfo();
117 SkString vertShaderSrc(GrGetGLSLVersionDecl(ctxInfo)); 155 SkString vertShaderSrc(GrGetGLSLVersionDecl(ctxInfo));
118 fProgramBuilder->appendUniformDecls(GrGLProgramBuilder::kVertex_Visibility, &vertShaderSrc); 156 fProgramBuilder->appendUniformDecls(GrGLProgramBuilder::kVertex_Visibility, &vertShaderSrc);
119 fProgramBuilder->appendDecls(fInputs, &vertShaderSrc); 157 this->appendDecls(fInputs, &vertShaderSrc);
120 fProgramBuilder->appendDecls(fOutputs, &vertShaderSrc); 158 this->appendDecls(fOutputs, &vertShaderSrc);
121 vertShaderSrc.append("void main() {"); 159 vertShaderSrc.append("void main() {");
122 vertShaderSrc.append(fCode); 160 vertShaderSrc.append(fCode);
123 vertShaderSrc.append("}\n"); 161 vertShaderSrc.append("}\n");
124 GrGLuint vertShaderId = GrGLCompileAndAttachShader(glCtx, programId, 162 GrGLuint vertShaderId = GrGLCompileAndAttachShader(glCtx, programId,
125 GR_GL_VERTEX_SHADER, vert ShaderSrc, 163 GR_GL_VERTEX_SHADER, vert ShaderSrc,
126 gpu->gpuStats()); 164 gpu->gpuStats());
127 if (!vertShaderId) { 165 if (!vertShaderId) {
128 return false; 166 return false;
129 } 167 }
130 *shaderIds->append() = vertShaderId; 168 *shaderIds->append() = vertShaderId;
131 return true; 169 return true;
132 } 170 }
133 171
134 void GrGLVertexShaderBuilder::emitCodeAfterEffects() { 172 bool GrGLVertexShaderBuilder::addAttribute(const GrShaderVar& var) {
135 const char* rtAdjustName; 173 SkASSERT(GrShaderVar::kAttribute_TypeModifier == var.getTypeModifier());
136 fProgramBuilder->fUniformHandles.fRTAdjustmentUni = 174 for (int i = 0; i < fInputs.count(); ++i) {
137 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility, 175 const GrGLShaderVar& attr = fInputs[i];
138 kVec4f_GrSLType, 176 // if attribute already added, don't add it again
139 "rtAdjustment", 177 if (attr.getName().equals(var.getName())) {
140 &rtAdjustName); 178 return false;
141 179 }
142 // Transform from Skia's device coords to GL's normalized device coords. 180 }
143 this->codeAppendf( 181 fInputs.push_back(var);
144 "gl_Position = vec4(dot(pos3.xz, %s.xy), dot(pos3.yz, %s.zw), 0, pos3.z) ;", 182 return true;
145 rtAdjustName, rtAdjustName);
146 } 183 }
147
148 void GrGLVertexShaderBuilder::emitCodeBeforeEffects(GrGLSLExpr4* color, GrGLSLEx pr4* coverage) {
149 const GrGLProgramDesc::KeyHeader& header = fProgramBuilder->desc().getHeader ();
150
151 fPositionVar = &fInputs.push_back();
152 fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, " inPosition");
153 if (-1 != header.fLocalCoordAttributeIndex) {
154 fLocalCoordsVar = &fInputs.push_back();
155 fLocalCoordsVar->set(kVec2f_GrSLType,
156 GrGLShaderVar::kAttribute_TypeModifier,
157 "inLocalCoords");
158 } else {
159 fLocalCoordsVar = fPositionVar;
160 }
161
162 const char* viewMName;
163 fProgramBuilder->fUniformHandles.fViewMatrixUni =
164 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility,
165 kMat33f_GrSLType,
166 "ViewM",
167 &viewMName);
168
169 // Transform the position into Skia's device coords.
170 this->codeAppendf("vec3 pos3 = %s * vec3(%s, 1);",
171 viewMName, fPositionVar->c_str());
172
173 // we output point size in the GS if present
174 if (header.fEmitsPointSize
175 #if GR_GL_EXPERIMENTAL_GS
176 && !header.fExperimentalGS
177 #endif
178 ) {
179 this->codeAppend("gl_PointSize = 1.0;");
180 }
181
182 if (GrGLProgramDesc::kAttribute_ColorInput == header.fColorInput) {
183 this->addAttribute(GrShaderVar(color_attribute_name(),
184 kVec4f_GrSLType,
185 GrShaderVar::kAttribute_TypeModifier));
186 const char *vsName, *fsName;
187 fFullProgramBuilder->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsNa me);
188 this->codeAppendf("%s = %s;", vsName, color_attribute_name());
189 *color = fsName;
190 }
191
192 if (GrGLProgramDesc::kAttribute_ColorInput == header.fCoverageInput) {
193 this->addAttribute(GrShaderVar(coverage_attribute_name(),
194 kVec4f_GrSLType,
195 GrShaderVar::kAttribute_TypeModifier));
196 const char *vsName, *fsName;
197 fFullProgramBuilder->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &f sName);
198 this->codeAppendf("%s = %s;", vsName, coverage_attribute_name());
199 *coverage = fsName;
200 }
201 fEffectAttribOffset = fInputs.count();
202 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698