| 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 GrGLVertexBuilder::GrGLVertexBuilder(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* GrGLVertexBuilder::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 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 } |
| 59 | 52 |
| 60 void GrGLVertexShaderBuilder::bindProgramLocations(GrGLuint programId) { | 53 void GrGLVertexBuilder::transformGLToSkiaCoords() { |
| 61 const GrGLProgramDesc::KeyHeader& header = fProgramBuilder->desc().getHeader
(); | 54 const char* viewMName; |
| 62 GrGpuGL* gpu = fProgramBuilder->gpu(); | 55 fProgramBuilder->fUniformHandles.fViewMatrixUni = |
| 56 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility, |
| 57 kMat33f_GrSLType, |
| 58 "ViewM", |
| 59 &viewMName); |
| 63 | 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 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) { |
| 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 const GrOptDrawState& optState = fProgramBuilder->optState(); | 122 const GrOptDrawState& optState = fProgramBuilder->optState(); |
| 86 const GrVertexAttrib* vaPtr = optState.getVertexAttribs(); | 123 const GrVertexAttrib* vaPtr = optState.getVertexAttribs(); |
| 87 const int vaCount = optState.getVertexAttribCount(); | 124 const int vaCount = optState.getVertexAttribCount(); |
| 88 | 125 |
| 126 // We start binding attributes after builtins |
| 89 int i = fEffectAttribOffset; | 127 int i = fEffectAttribOffset; |
| 90 for (int index = 0; index < vaCount; index++) { | 128 for (int index = 0; index < vaCount; index++) { |
| 91 if (kGeometryProcessor_GrVertexAttribBinding != vaPtr[index].fBinding) { | 129 if (kGeometryProcessor_GrVertexAttribBinding != vaPtr[index].fBinding) { |
| 92 continue; | 130 continue; |
| 93 } | 131 } |
| 94 SkASSERT(index != header.fPositionAttributeIndex && | 132 SkASSERT(index != header.fPositionAttributeIndex && |
| 95 index != header.fLocalCoordAttributeIndex && | 133 index != header.fLocalCoordAttributeIndex && |
| 96 index != header.fColorAttributeIndex && | 134 index != header.fColorAttributeIndex && |
| 97 index != header.fCoverageAttributeIndex); | 135 index != header.fCoverageAttributeIndex); |
| 98 // We should never find another effect attribute if we have bound everyt
hing | 136 // We should never find another effect attribute if we have bound everyt
hing |
| 99 SkASSERT(i < fInputs.count()); | 137 SkASSERT(i < fInputs.count()); |
| 100 GL_CALL(BindAttribLocation(programId, index, fInputs[i].c_str())); | 138 GL_CALL(BindAttribLocation(programID, index, fInputs[i].c_str())); |
| 101 i++; | 139 i++; |
| 102 } | 140 } |
| 103 // Make sure we bound everything | 141 // Make sure we bound everything |
| 104 SkASSERT(fInputs.count() == i); | 142 SkASSERT(fInputs.count() == i); |
| 105 } | 143 } |
| 106 | 144 |
| 107 bool GrGLVertexShaderBuilder::compileAndAttachShaders(GrGLuint programId, | 145 bool GrGLVertexBuilder::compileAndAttachShaders(GrGLuint programId, |
| 108 SkTDArray<GrGLuint>* shaderIds) const { | 146 SkTDArray<GrGLuint>* shaderIds) const { |
| 109 GrGpuGL* gpu = fProgramBuilder->gpu(); | 147 GrGpuGL* gpu = fProgramBuilder->gpu(); |
| 110 const GrGLContext& glCtx = gpu->glContext(); | 148 const GrGLContext& glCtx = gpu->glContext(); |
| 111 const GrGLContextInfo& ctxInfo = gpu->ctxInfo(); | 149 const GrGLContextInfo& ctxInfo = gpu->ctxInfo(); |
| 112 SkString vertShaderSrc(GrGetGLSLVersionDecl(ctxInfo)); | 150 SkString vertShaderSrc(GrGetGLSLVersionDecl(ctxInfo)); |
| 113 fProgramBuilder->appendUniformDecls(GrGLProgramBuilder::kVertex_Visibility,
&vertShaderSrc); | 151 fProgramBuilder->appendUniformDecls(GrGLProgramBuilder::kVertex_Visibility,
&vertShaderSrc); |
| 114 fProgramBuilder->appendDecls(fInputs, &vertShaderSrc); | 152 this->appendDecls(fInputs, &vertShaderSrc); |
| 115 fProgramBuilder->appendDecls(fOutputs, &vertShaderSrc); | 153 this->appendDecls(fOutputs, &vertShaderSrc); |
| 116 vertShaderSrc.append("void main() {"); | 154 vertShaderSrc.append("void main() {"); |
| 117 vertShaderSrc.append(fCode); | 155 vertShaderSrc.append(fCode); |
| 118 vertShaderSrc.append("}\n"); | 156 vertShaderSrc.append("}\n"); |
| 119 GrGLuint vertShaderId = GrGLCompileAndAttachShader(glCtx, programId, | 157 GrGLuint vertShaderId = GrGLCompileAndAttachShader(glCtx, programId, |
| 120 GR_GL_VERTEX_SHADER, vert
ShaderSrc, | 158 GR_GL_VERTEX_SHADER, vert
ShaderSrc, |
| 121 gpu->gpuStats()); | 159 gpu->gpuStats()); |
| 122 if (!vertShaderId) { | 160 if (!vertShaderId) { |
| 123 return false; | 161 return false; |
| 124 } | 162 } |
| 125 *shaderIds->append() = vertShaderId; | 163 *shaderIds->append() = vertShaderId; |
| 126 return true; | 164 return true; |
| 127 } | 165 } |
| 128 | 166 |
| 129 void GrGLVertexShaderBuilder::emitCodeAfterEffects() { | 167 bool GrGLVertexBuilder::addAttribute(const GrShaderVar& var) { |
| 130 const char* rtAdjustName; | 168 SkASSERT(GrShaderVar::kAttribute_TypeModifier == var.getTypeModifier()); |
| 131 fProgramBuilder->fUniformHandles.fRTAdjustmentUni = | 169 for (int i = 0; i < fInputs.count(); ++i) { |
| 132 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility, | 170 const GrGLShaderVar& attr = fInputs[i]; |
| 133 kVec4f_GrSLType, | 171 // if attribute already added, don't add it again |
| 134 "rtAdjustment", | 172 if (attr.getName().equals(var.getName())) { |
| 135 &rtAdjustName); | 173 return false; |
| 136 | 174 } |
| 137 // Transform from Skia's device coords to GL's normalized device coords. | 175 } |
| 138 this->codeAppendf( | 176 fInputs.push_back(var); |
| 139 "gl_Position = vec4(dot(pos3.xz, %s.xy), dot(pos3.yz, %s.zw), 0, pos3.z)
;", | 177 return true; |
| 140 rtAdjustName, rtAdjustName); | |
| 141 } | 178 } |
| 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; | |
| 155 } | |
| 156 | |
| 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(); | |
| 197 } | |
| OLD | NEW |