| 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 "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) | |
| 25 , fLocalCoordsVar(NULL) { | |
| 26 } | |
| 27 bool GrGLVertexShaderBuilder::addAttribute(const GrShaderVar& var) { | |
| 28 SkASSERT(GrShaderVar::kAttribute_TypeModifier == var.getTypeModifier()); | |
| 29 for (int i = 0; i < fInputs.count(); ++i) { | |
| 30 const GrGLShaderVar& attr = fInputs[i]; | |
| 31 // if attribute already added, don't add it again | |
| 32 if (attr.getName().equals(var.getName())) { | |
| 33 return false; | |
| 34 } | |
| 35 } | |
| 36 fInputs.push_back(var); | |
| 37 return true; | |
| 38 } | |
| 39 | |
| 40 void GrGLVertexShaderBuilder::emitAttributes(const GrGeometryProcessor& gp) { | |
| 41 const GrGeometryProcessor::VertexAttribArray& vars = gp.getVertexAttribs(); | |
| 42 int numAttributes = vars.count(); | |
| 43 for (int a = 0; a < numAttributes; ++a) { | |
| 44 this->addAttribute(vars[a]); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 void GrGLVertexShaderBuilder::addVarying(GrSLType type, const char* name, const
char** vsOutName) { | |
| 49 fOutputs.push_back(); | 28 fOutputs.push_back(); |
| 50 fOutputs.back().setType(type); | 29 fOutputs.back().setType(type); |
| 51 fOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); | 30 fOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); |
| 52 fProgramBuilder->nameVariable(fOutputs.back().accessName(), 'v', name); | 31 fProgramBuilder->nameVariable(fOutputs.back().accessName(), 'v', name); |
| 53 | 32 |
| 54 if (vsOutName) { | 33 if (vsOutName) { |
| 55 *vsOutName = fOutputs.back().getName().c_str(); | 34 *vsOutName = fOutputs.back().getName().c_str(); |
| 56 } | 35 } |
| 36 return fOutputs.back().accessName(); |
| 57 } | 37 } |
| 58 | 38 |
| 39 void GrGLVertexShaderBuilder::setupLocalCoords() { |
| 40 fPositionVar = &fInputs.push_back(); |
| 41 fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, "
inPosition"); |
| 42 if (-1 != fProgramBuilder->header().fLocalCoordAttributeIndex) { |
| 43 fLocalCoordsVar = &fInputs.push_back(); |
| 44 fLocalCoordsVar->set(kVec2f_GrSLType, |
| 45 GrGLShaderVar::kAttribute_TypeModifier, |
| 46 "inLocalCoords"); |
| 47 } else { |
| 48 fLocalCoordsVar = fPositionVar; |
| 49 } |
| 50 fEffectAttribOffset = fInputs.count(); |
| 51 } |
| 59 | 52 |
| 60 void GrGLVertexShaderBuilder::bindProgramLocations(GrGLuint programId) { | 53 void GrGLVertexShaderBuilder::setupBuiltinVertexAttribute(const char* inName, Gr
GLSLExpr4* out) { |
| 61 const GrGLProgramDesc::KeyHeader& header = fProgramBuilder->desc().getHeader
(); | 54 SkString name(inName); |
| 62 GrGpuGL* gpu = fProgramBuilder->gpu(); | 55 const char *vsName, *fsName; |
| 56 fProgramBuilder->addVarying(kVec4f_GrSLType, name.c_str(), &vsName, &fsName)
; |
| 57 name.prepend("in"); |
| 58 this->addAttribute(GrShaderVar(name.c_str(), |
| 59 kVec4f_GrSLType, |
| 60 GrShaderVar::kAttribute_TypeModifier)); |
| 61 this->codeAppendf("%s = %s;", vsName, name.c_str()); |
| 62 *out = fsName; |
| 63 fEffectAttribOffset++; |
| 64 } |
| 63 | 65 |
| 66 void GrGLVertexShaderBuilder::emitAttributes(const GrGeometryProcessor& gp) { |
| 67 const GrGeometryProcessor::VertexAttribArray& vars = gp.getVertexAttribs(); |
| 68 int numAttributes = vars.count(); |
| 69 for (int a = 0; a < numAttributes; ++a) { |
| 70 this->addAttribute(vars[a]); |
| 71 } |
| 72 } |
| 73 |
| 74 void GrGLVertexShaderBuilder::bindVertexAttributes(GrGLuint programID) { |
| 64 // Bind the attrib locations to same values for all shaders | 75 // Bind the attrib locations to same values for all shaders |
| 76 const GrGLProgramDesc::KeyHeader& header = fProgramBuilder->header(); |
| 65 SkASSERT(-1 != header.fPositionAttributeIndex); | 77 SkASSERT(-1 != header.fPositionAttributeIndex); |
| 66 GL_CALL(BindAttribLocation(programId, | 78 GL_CALL(BindAttribLocation(programID, |
| 67 header.fPositionAttributeIndex, | 79 header.fPositionAttributeIndex, |
| 68 fPositionVar->c_str())); | 80 fPositionVar->c_str())); |
| 69 if (-1 != header.fLocalCoordAttributeIndex) { | 81 if (-1 != header.fLocalCoordAttributeIndex) { |
| 70 GL_CALL(BindAttribLocation(programId, | 82 GL_CALL(BindAttribLocation(programID, |
| 71 header.fLocalCoordAttributeIndex, | 83 header.fLocalCoordAttributeIndex, |
| 72 fLocalCoordsVar->c_str())); | 84 fLocalCoordsVar->c_str())); |
| 73 } | 85 } |
| 74 if (-1 != header.fColorAttributeIndex) { | 86 if (-1 != header.fColorAttributeIndex) { |
| 75 GL_CALL(BindAttribLocation(programId, | 87 GL_CALL(BindAttribLocation(programID, |
| 76 header.fColorAttributeIndex, | 88 header.fColorAttributeIndex, |
| 77 color_attribute_name())); | 89 color_attribute_name())); |
| 78 } | 90 } |
| 79 if (-1 != header.fCoverageAttributeIndex) { | 91 if (-1 != header.fCoverageAttributeIndex) { |
| 80 GL_CALL(BindAttribLocation(programId, | 92 GL_CALL(BindAttribLocation(programID, |
| 81 header.fCoverageAttributeIndex, | 93 header.fCoverageAttributeIndex, |
| 82 coverage_attribute_name())); | 94 coverage_attribute_name())); |
| 83 } | 95 } |
| 84 | 96 |
| 85 // We pull the current state of attributes off of drawstate's optimized stat
e and bind them in | 97 // 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() | 98 // order. This assumes that the drawState has not changed since we called fl
ushGraphicsState() |
| 87 // higher up in the stack. | 99 // higher up in the stack. |
| 88 const GrDrawTargetCaps* caps = fProgramBuilder->gpu()->caps(); | 100 const GrDrawTargetCaps* caps = fProgramBuilder->gpu()->caps(); |
| 89 const GrDrawState& drawState = *fProgramBuilder->gpu()->drawState(); | 101 const GrDrawState& drawState = *fProgramBuilder->gpu()->drawState(); |
| 90 SkAutoTUnref<GrOptDrawState> optState(drawState.createOptState(*caps)); | 102 SkAutoTUnref<GrOptDrawState> optState(drawState.createOptState(*caps)); |
| 91 const GrVertexAttrib* vaPtr = optState->getVertexAttribs(); | 103 const GrVertexAttrib* vaPtr = optState->getVertexAttribs(); |
| 92 const int vaCount = optState->getVertexAttribCount(); | 104 const int vaCount = optState->getVertexAttribCount(); |
| 93 | 105 |
| 106 // We start binding attributes after builtins |
| 94 int i = fEffectAttribOffset; | 107 int i = fEffectAttribOffset; |
| 95 for (int index = 0; index < vaCount; index++) { | 108 for (int index = 0; index < vaCount; index++) { |
| 96 if (kGeometryProcessor_GrVertexAttribBinding != vaPtr[index].fBinding) { | 109 if (kGeometryProcessor_GrVertexAttribBinding != vaPtr[index].fBinding) { |
| 97 continue; | 110 continue; |
| 98 } | 111 } |
| 99 SkASSERT(index != header.fPositionAttributeIndex && | 112 SkASSERT(index != header.fPositionAttributeIndex && |
| 100 index != header.fLocalCoordAttributeIndex && | 113 index != header.fLocalCoordAttributeIndex && |
| 101 index != header.fColorAttributeIndex && | 114 index != header.fColorAttributeIndex && |
| 102 index != header.fCoverageAttributeIndex); | 115 index != header.fCoverageAttributeIndex); |
| 103 // We should never find another effect attribute if we have bound everyt
hing | 116 // We should never find another effect attribute if we have bound everyt
hing |
| 104 SkASSERT(i < fInputs.count()); | 117 SkASSERT(i < fInputs.count()); |
| 105 GL_CALL(BindAttribLocation(programId, index, fInputs[i].c_str())); | 118 GL_CALL(BindAttribLocation(programID, index, fInputs[i].c_str())); |
| 106 i++; | 119 i++; |
| 107 } | 120 } |
| 108 // Make sure we bound everything | 121 // Make sure we bound everything |
| 109 SkASSERT(fInputs.count() == i); | 122 SkASSERT(fInputs.count() == i); |
| 110 } | 123 } |
| 111 | 124 |
| 112 bool GrGLVertexShaderBuilder::compileAndAttachShaders(GrGLuint programId, | 125 bool GrGLVertexShaderBuilder::compileAndAttachShaders(GrGLuint programId, |
| 113 SkTDArray<GrGLuint>* shaderIds) const { | 126 SkTDArray<GrGLuint>* shaderIds) const { |
| 114 GrGpuGL* gpu = fProgramBuilder->gpu(); | 127 GrGpuGL* gpu = fProgramBuilder->gpu(); |
| 115 const GrGLContext& glCtx = gpu->glContext(); | 128 const GrGLContext& glCtx = gpu->glContext(); |
| 116 const GrGLContextInfo& ctxInfo = gpu->ctxInfo(); | 129 const GrGLContextInfo& ctxInfo = gpu->ctxInfo(); |
| 117 SkString vertShaderSrc(GrGetGLSLVersionDecl(ctxInfo)); | 130 SkString vertShaderSrc(GrGetGLSLVersionDecl(ctxInfo)); |
| 118 fProgramBuilder->appendUniformDecls(GrGLProgramBuilder::kVertex_Visibility,
&vertShaderSrc); | 131 fProgramBuilder->appendUniformDecls(GrGLProgramBuilder::kVertex_Visibility,
&vertShaderSrc); |
| 119 fProgramBuilder->appendDecls(fInputs, &vertShaderSrc); | 132 this->appendDecls(fInputs, &vertShaderSrc); |
| 120 fProgramBuilder->appendDecls(fOutputs, &vertShaderSrc); | 133 this->appendDecls(fOutputs, &vertShaderSrc); |
| 121 vertShaderSrc.append("void main() {"); | 134 vertShaderSrc.append("void main() {"); |
| 122 vertShaderSrc.append(fCode); | 135 vertShaderSrc.append(fCode); |
| 123 vertShaderSrc.append("}\n"); | 136 vertShaderSrc.append("}\n"); |
| 124 GrGLuint vertShaderId = GrGLCompileAndAttachShader(glCtx, programId, | 137 GrGLuint vertShaderId = GrGLCompileAndAttachShader(glCtx, programId, |
| 125 GR_GL_VERTEX_SHADER, vert
ShaderSrc, | 138 GR_GL_VERTEX_SHADER, vert
ShaderSrc, |
| 126 gpu->gpuStats()); | 139 gpu->gpuStats()); |
| 127 if (!vertShaderId) { | 140 if (!vertShaderId) { |
| 128 return false; | 141 return false; |
| 129 } | 142 } |
| 130 *shaderIds->append() = vertShaderId; | 143 *shaderIds->append() = vertShaderId; |
| 131 return true; | 144 return true; |
| 132 } | 145 } |
| 133 | 146 |
| 134 void GrGLVertexShaderBuilder::emitCodeAfterEffects() { | 147 bool GrGLVertexShaderBuilder::addAttribute(const GrShaderVar& var) { |
| 135 const char* rtAdjustName; | 148 SkASSERT(GrShaderVar::kAttribute_TypeModifier == var.getTypeModifier()); |
| 136 fProgramBuilder->fUniformHandles.fRTAdjustmentUni = | 149 for (int i = 0; i < fInputs.count(); ++i) { |
| 137 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility, | 150 const GrGLShaderVar& attr = fInputs[i]; |
| 138 kVec4f_GrSLType, | 151 // if attribute already added, don't add it again |
| 139 "rtAdjustment", | 152 if (attr.getName().equals(var.getName())) { |
| 140 &rtAdjustName); | 153 return false; |
| 141 | 154 } |
| 142 // Transform from Skia's device coords to GL's normalized device coords. | 155 } |
| 143 this->codeAppendf( | 156 fInputs.push_back(var); |
| 144 "gl_Position = vec4(dot(pos3.xz, %s.xy), dot(pos3.yz, %s.zw), 0, pos3.z)
;", | 157 return true; |
| 145 rtAdjustName, rtAdjustName); | |
| 146 } | 158 } |
| 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 } | |
| OLD | NEW |