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