| 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 "GrGLProgramBuilder.h" |
| 10 #include "GrGLShaderStringBuilder.h" | 10 #include "GrGLShaderStringBuilder.h" |
| 11 #include "../GrGpuGL.h" | 11 #include "../GrGpuGL.h" |
| 12 | 12 |
| 13 #define GL_CALL(X) GR_GL_CALL(fProgramBuilder->gpu()->glInterface(), X) | 13 #define GL_CALL(X) GR_GL_CALL(fProgramBuilder->gpu()->glInterface(), X) |
| 14 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fProgramBuilder->gpu()->glInterface(),
R, X) | 14 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fProgramBuilder->gpu()->glInterface(),
R, X) |
| 15 | 15 |
| 16 static const char* color_attribute_name() { return "inColor"; } | |
| 17 static const char* coverage_attribute_name() { return "inCoverage"; } | |
| 18 | |
| 19 GrGLVertexBuilder::GrGLVertexBuilder(GrGLProgramBuilder* program) | 16 GrGLVertexBuilder::GrGLVertexBuilder(GrGLProgramBuilder* program) |
| 20 : INHERITED(program) | 17 : INHERITED(program) |
| 21 , fPositionVar(NULL) | 18 , fRtAdjustName(NULL) { |
| 22 , fLocalCoordsVar(NULL) | |
| 23 , fRtAdjustName(NULL) | |
| 24 , fEffectAttribOffset(0) { | |
| 25 } | 19 } |
| 26 | 20 |
| 27 void GrGLVertexBuilder::addVarying(const char* name, GrGLVarying* v) { | 21 void GrGLVertexBuilder::addVarying(const char* name, GrGLVarying* v) { |
| 28 fOutputs.push_back(); | 22 fOutputs.push_back(); |
| 29 fOutputs.back().setType(v->fType); | 23 fOutputs.back().setType(v->fType); |
| 30 fOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); | 24 fOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); |
| 31 fProgramBuilder->nameVariable(fOutputs.back().accessName(), 'v', name); | 25 fProgramBuilder->nameVariable(fOutputs.back().accessName(), 'v', name); |
| 32 v->fVsOut = fOutputs.back().getName().c_str(); | 26 v->fVsOut = fOutputs.back().getName().c_str(); |
| 33 } | 27 } |
| 34 | 28 |
| 35 void GrGLVertexBuilder::setupUniformViewMatrix() { | 29 void GrGLVertexBuilder::setupUniformViewMatrix() { |
| 36 fProgramBuilder->fUniformHandles.fViewMatrixUni = | 30 fProgramBuilder->fUniformHandles.fViewMatrixUni = |
| 37 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility, | 31 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility, |
| 38 kMat33f_GrSLType, | 32 kMat33f_GrSLType, |
| 39 this->uViewM()); | 33 this->uViewM()); |
| 40 } | 34 } |
| 41 | 35 |
| 42 void GrGLVertexBuilder::setupPositionAndLocalCoords() { | 36 void GrGLVertexBuilder::emitAttributes(const GrGeometryProcessor& gp) { |
| 43 // Setup position | 37 const GrGeometryProcessor::VertexAttribArray& v = gp.getAttribs(); |
| 44 this->codeAppendf("vec3 %s;", this->glPosition()); | 38 int vaCount = v.count(); |
| 45 | 39 for (int i = 0; i < vaCount; i++) { |
| 46 // setup position and local coords attribute | 40 this->addAttribute(&v[i]); |
| 47 fPositionVar = &fInputs.push_back(); | |
| 48 fPositionVar->set(kVec2f_GrSLType, | |
| 49 GrGLShaderVar::kAttribute_TypeModifier, | |
| 50 this->inPosition()); | |
| 51 if (-1 != fProgramBuilder->header().fLocalCoordAttributeIndex) { | |
| 52 fLocalCoordsVar = &fInputs.push_back(); | |
| 53 fLocalCoordsVar->set(kVec2f_GrSLType, | |
| 54 GrGLShaderVar::kAttribute_TypeModifier, | |
| 55 "inLocalCoords"); | |
| 56 } else { | |
| 57 fLocalCoordsVar = fPositionVar; | |
| 58 } | 41 } |
| 59 fEffectAttribOffset = fInputs.count(); | 42 return; |
| 60 } | |
| 61 | |
| 62 void GrGLVertexBuilder::setupBuiltinVertexAttribute(const char* inName, GrGLSLEx
pr1* out) { | |
| 63 GrGLVertToFrag v(kFloat_GrSLType); | |
| 64 fProgramBuilder->addVarying(inName, &v); | |
| 65 SkString name(inName); | |
| 66 name.prepend("in"); | |
| 67 this->addAttribute(GrShaderVar(name.c_str(), | |
| 68 kFloat_GrSLType, | |
| 69 GrShaderVar::kAttribute_TypeModifier)); | |
| 70 this->codeAppendf("%s = %s;", v.vsOut(), name.c_str()); | |
| 71 *out = v.fsIn(); | |
| 72 fEffectAttribOffset++; | |
| 73 } | |
| 74 | |
| 75 void GrGLVertexBuilder::setupBuiltinVertexAttribute(const char* inName, GrGLSLEx
pr4* out) { | |
| 76 GrGLVertToFrag v(kVec4f_GrSLType); | |
| 77 fProgramBuilder->addVarying(inName, &v); | |
| 78 SkString name(inName); | |
| 79 name.prepend("in"); | |
| 80 this->addAttribute(GrShaderVar(name.c_str(), | |
| 81 kVec4f_GrSLType, | |
| 82 GrShaderVar::kAttribute_TypeModifier)); | |
| 83 this->codeAppendf("%s = %s;", v.vsOut(), name.c_str()); | |
| 84 *out = v.fsIn(); | |
| 85 fEffectAttribOffset++; | |
| 86 } | |
| 87 | |
| 88 void GrGLVertexBuilder::emitAttributes(const GrGeometryProcessor& gp) { | |
| 89 const GrGeometryProcessor::VertexAttribArray& vars = gp.getVertexAttribs(); | |
| 90 int numAttributes = vars.count(); | |
| 91 for (int a = 0; a < numAttributes; ++a) { | |
| 92 this->addAttribute(vars[a]); | |
| 93 } | |
| 94 } | 43 } |
| 95 | 44 |
| 96 void GrGLVertexBuilder::transformToNormalizedDeviceSpace() { | 45 void GrGLVertexBuilder::transformToNormalizedDeviceSpace() { |
| 97 // setup RT Uniform | 46 // setup RT Uniform |
| 98 fProgramBuilder->fUniformHandles.fRTAdjustmentUni = | 47 fProgramBuilder->fUniformHandles.fRTAdjustmentUni = |
| 99 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility, | 48 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility, |
| 100 kVec4f_GrSLType, | 49 kVec4f_GrSLType, |
| 101 fProgramBuilder->rtAdjustment(), | 50 fProgramBuilder->rtAdjustment(), |
| 102 &fRtAdjustName); | 51 &fRtAdjustName); |
| 103 // Wire transforms | 52 // Wire transforms |
| (...skipping 13 matching lines...) Expand all Loading... |
| 117 } | 66 } |
| 118 } | 67 } |
| 119 | 68 |
| 120 // Transform from Skia's device coords to GL's normalized device coords. | 69 // Transform from Skia's device coords to GL's normalized device coords. |
| 121 this->codeAppendf("gl_Position = vec4(dot(%s.xz, %s.xy), dot(%s.yz, %s.zw),
0, %s.z);", | 70 this->codeAppendf("gl_Position = vec4(dot(%s.xz, %s.xy), dot(%s.yz, %s.zw),
0, %s.z);", |
| 122 this->glPosition(), fRtAdjustName, this->glPosition(), fRt
AdjustName, | 71 this->glPosition(), fRtAdjustName, this->glPosition(), fRt
AdjustName, |
| 123 this->glPosition()); | 72 this->glPosition()); |
| 124 } | 73 } |
| 125 | 74 |
| 126 void GrGLVertexBuilder::bindVertexAttributes(GrGLuint programID) { | 75 void GrGLVertexBuilder::bindVertexAttributes(GrGLuint programID) { |
| 127 // Bind the attrib locations to same values for all shaders | 76 const GrGeometryProcessor* gp = fProgramBuilder->fOptState.getGeometryProces
sor(); |
| 128 const GrProgramDesc::KeyHeader& header = fProgramBuilder->header(); | 77 |
| 129 SkASSERT(-1 != header.fPositionAttributeIndex); | 78 const GrGeometryProcessor::VertexAttribArray& v = gp->getAttribs(); |
| 130 GL_CALL(BindAttribLocation(programID, | 79 int vaCount = v.count(); |
| 131 header.fPositionAttributeIndex, | 80 for (int i = 0; i < vaCount; i++) { |
| 132 fPositionVar->c_str())); | 81 GL_CALL(BindAttribLocation(programID, i, v[i].fName)); |
| 133 if (-1 != header.fLocalCoordAttributeIndex) { | |
| 134 GL_CALL(BindAttribLocation(programID, | |
| 135 header.fLocalCoordAttributeIndex, | |
| 136 fLocalCoordsVar->c_str())); | |
| 137 } | 82 } |
| 138 if (-1 != header.fColorAttributeIndex) { | 83 return; |
| 139 GL_CALL(BindAttribLocation(programID, | |
| 140 header.fColorAttributeIndex, | |
| 141 color_attribute_name())); | |
| 142 } | |
| 143 if (-1 != header.fCoverageAttributeIndex) { | |
| 144 GL_CALL(BindAttribLocation(programID, | |
| 145 header.fCoverageAttributeIndex, | |
| 146 coverage_attribute_name())); | |
| 147 } | |
| 148 | |
| 149 const GrOptDrawState& optState = fProgramBuilder->optState(); | |
| 150 const GrVertexAttrib* vaPtr = optState.getVertexAttribs(); | |
| 151 const int vaCount = optState.getVertexAttribCount(); | |
| 152 | |
| 153 // We start binding attributes after builtins | |
| 154 int i = fEffectAttribOffset; | |
| 155 for (int index = 0; index < vaCount; index++) { | |
| 156 if (kGeometryProcessor_GrVertexAttribBinding != vaPtr[index].fBinding) { | |
| 157 continue; | |
| 158 } | |
| 159 SkASSERT(index != header.fPositionAttributeIndex && | |
| 160 index != header.fLocalCoordAttributeIndex && | |
| 161 index != header.fColorAttributeIndex && | |
| 162 index != header.fCoverageAttributeIndex); | |
| 163 // We should never find another effect attribute if we have bound everyt
hing | |
| 164 SkASSERT(i < fInputs.count()); | |
| 165 GL_CALL(BindAttribLocation(programID, index, fInputs[i].c_str())); | |
| 166 i++; | |
| 167 } | |
| 168 // Make sure we bound everything | |
| 169 SkASSERT(fInputs.count() == i); | |
| 170 } | 84 } |
| 171 | 85 |
| 172 bool GrGLVertexBuilder::compileAndAttachShaders(GrGLuint programId, | 86 bool GrGLVertexBuilder::compileAndAttachShaders(GrGLuint programId, |
| 173 SkTDArray<GrGLuint>* shaderIds) const { | 87 SkTDArray<GrGLuint>* shaderIds) const { |
| 174 GrGpuGL* gpu = fProgramBuilder->gpu(); | 88 GrGpuGL* gpu = fProgramBuilder->gpu(); |
| 175 const GrGLContext& glCtx = gpu->glContext(); | 89 const GrGLContext& glCtx = gpu->glContext(); |
| 176 const GrGLContextInfo& ctxInfo = gpu->ctxInfo(); | 90 const GrGLContextInfo& ctxInfo = gpu->ctxInfo(); |
| 177 SkString vertShaderSrc(GrGetGLSLVersionDecl(ctxInfo)); | 91 SkString vertShaderSrc(GrGetGLSLVersionDecl(ctxInfo)); |
| 178 fProgramBuilder->appendUniformDecls(GrGLProgramBuilder::kVertex_Visibility,
&vertShaderSrc); | 92 fProgramBuilder->appendUniformDecls(GrGLProgramBuilder::kVertex_Visibility,
&vertShaderSrc); |
| 179 this->appendDecls(fInputs, &vertShaderSrc); | 93 this->appendDecls(fInputs, &vertShaderSrc); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 196 for (int i = 0; i < fInputs.count(); ++i) { | 110 for (int i = 0; i < fInputs.count(); ++i) { |
| 197 const GrGLShaderVar& attr = fInputs[i]; | 111 const GrGLShaderVar& attr = fInputs[i]; |
| 198 // if attribute already added, don't add it again | 112 // if attribute already added, don't add it again |
| 199 if (attr.getName().equals(var.getName())) { | 113 if (attr.getName().equals(var.getName())) { |
| 200 return false; | 114 return false; |
| 201 } | 115 } |
| 202 } | 116 } |
| 203 fInputs.push_back(var); | 117 fInputs.push_back(var); |
| 204 return true; | 118 return true; |
| 205 } | 119 } |
| OLD | NEW |