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"; } | 16 static const char* color_attribute_name() { return "inColor"; } |
17 static const char* coverage_attribute_name() { return "inCoverage"; } | 17 static const char* coverage_attribute_name() { return "inCoverage"; } |
18 | 18 |
19 GrGLVertexBuilder::GrGLVertexBuilder(GrGLProgramBuilder* program) | 19 GrGLVertexBuilder::GrGLVertexBuilder(GrGLProgramBuilder* program) |
20 : INHERITED(program) | 20 : INHERITED(program) |
21 , fPositionVar(NULL) | 21 , fPositionVar(NULL) |
22 , fLocalCoordsVar(NULL) | 22 , fLocalCoordsVar(NULL) |
23 , fRtAdjustName(NULL) | |
24 , fEffectAttribOffset(0) { | 23 , fEffectAttribOffset(0) { |
25 } | 24 } |
26 | 25 |
27 void GrGLVertexBuilder::addVarying(const char* name, GrGLVarying* v) { | 26 void GrGLVertexBuilder::addVarying(const char* name, GrGLVarying* v) { |
28 fOutputs.push_back(); | 27 fOutputs.push_back(); |
29 fOutputs.back().setType(v->fType); | 28 fOutputs.back().setType(v->fType); |
30 fOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); | 29 fOutputs.back().setTypeModifier(GrGLShaderVar::kVaryingOut_TypeModifier); |
31 fProgramBuilder->nameVariable(fOutputs.back().accessName(), 'v', name); | 30 fProgramBuilder->nameVariable(fOutputs.back().accessName(), 'v', name); |
32 v->fVsOut = fOutputs.back().getName().c_str(); | 31 v->fVsOut = fOutputs.back().getName().c_str(); |
33 } | 32 } |
34 | 33 |
35 void GrGLVertexBuilder::setupUniformViewMatrix() { | 34 void GrGLVertexBuilder::setupLocalCoords() { |
36 fProgramBuilder->fUniformHandles.fViewMatrixUni = | |
37 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility, | |
38 kMat33f_GrSLType, | |
39 this->uViewM()); | |
40 } | |
41 | |
42 void GrGLVertexBuilder::setupPositionAndLocalCoords() { | |
43 // Setup position | |
44 this->codeAppendf("vec3 %s;", this->glPosition()); | |
45 | |
46 // setup position and local coords attribute | |
47 fPositionVar = &fInputs.push_back(); | 35 fPositionVar = &fInputs.push_back(); |
48 fPositionVar->set(kVec2f_GrSLType, | 36 fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, "
inPosition"); |
49 GrGLShaderVar::kAttribute_TypeModifier, | |
50 this->inPosition()); | |
51 if (-1 != fProgramBuilder->header().fLocalCoordAttributeIndex) { | 37 if (-1 != fProgramBuilder->header().fLocalCoordAttributeIndex) { |
52 fLocalCoordsVar = &fInputs.push_back(); | 38 fLocalCoordsVar = &fInputs.push_back(); |
53 fLocalCoordsVar->set(kVec2f_GrSLType, | 39 fLocalCoordsVar->set(kVec2f_GrSLType, |
54 GrGLShaderVar::kAttribute_TypeModifier, | 40 GrGLShaderVar::kAttribute_TypeModifier, |
55 "inLocalCoords"); | 41 "inLocalCoords"); |
56 } else { | 42 } else { |
57 fLocalCoordsVar = fPositionVar; | 43 fLocalCoordsVar = fPositionVar; |
58 } | 44 } |
59 fEffectAttribOffset = fInputs.count(); | 45 fEffectAttribOffset = fInputs.count(); |
60 } | 46 } |
61 | 47 |
| 48 void GrGLVertexBuilder::transformGLToSkiaCoords() { |
| 49 const char* viewMName; |
| 50 fProgramBuilder->fUniformHandles.fViewMatrixUni = |
| 51 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility, |
| 52 kMat33f_GrSLType, |
| 53 "ViewM", |
| 54 &viewMName); |
| 55 |
| 56 // Transform the position into Skia's device coords. |
| 57 this->codeAppendf("vec3 pos3 = %s * vec3(%s, 1);", viewMName, fPositionVar->
c_str()); |
| 58 } |
| 59 |
62 void GrGLVertexBuilder::setupBuiltinVertexAttribute(const char* inName, GrGLSLEx
pr1* out) { | 60 void GrGLVertexBuilder::setupBuiltinVertexAttribute(const char* inName, GrGLSLEx
pr1* out) { |
63 GrGLVertToFrag v(kFloat_GrSLType); | 61 GrGLVertToFrag v(kFloat_GrSLType); |
64 fProgramBuilder->addVarying(inName, &v); | 62 fProgramBuilder->addVarying(inName, &v); |
65 SkString name(inName); | 63 SkString name(inName); |
66 name.prepend("in"); | 64 name.prepend("in"); |
67 this->addAttribute(GrShaderVar(name.c_str(), | 65 this->addAttribute(GrShaderVar(name.c_str(), |
68 kFloat_GrSLType, | 66 kFloat_GrSLType, |
69 GrShaderVar::kAttribute_TypeModifier)); | 67 GrShaderVar::kAttribute_TypeModifier)); |
70 this->codeAppendf("%s = %s;", v.vsOut(), name.c_str()); | 68 this->codeAppendf("%s = %s;", v.vsOut(), name.c_str()); |
71 *out = v.fsIn(); | 69 *out = v.fsIn(); |
(...skipping 14 matching lines...) Expand all Loading... |
86 } | 84 } |
87 | 85 |
88 void GrGLVertexBuilder::emitAttributes(const GrGeometryProcessor& gp) { | 86 void GrGLVertexBuilder::emitAttributes(const GrGeometryProcessor& gp) { |
89 const GrGeometryProcessor::VertexAttribArray& vars = gp.getVertexAttribs(); | 87 const GrGeometryProcessor::VertexAttribArray& vars = gp.getVertexAttribs(); |
90 int numAttributes = vars.count(); | 88 int numAttributes = vars.count(); |
91 for (int a = 0; a < numAttributes; ++a) { | 89 for (int a = 0; a < numAttributes; ++a) { |
92 this->addAttribute(vars[a]); | 90 this->addAttribute(vars[a]); |
93 } | 91 } |
94 } | 92 } |
95 | 93 |
96 void GrGLVertexBuilder::transformToNormalizedDeviceSpace() { | 94 void GrGLVertexBuilder::transformSkiaToGLCoords() { |
97 // setup RT Uniform | 95 const char* rtAdjustName; |
98 fProgramBuilder->fUniformHandles.fRTAdjustmentUni = | 96 fProgramBuilder->fUniformHandles.fRTAdjustmentUni = |
99 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility, | 97 fProgramBuilder->addUniform(GrGLProgramBuilder::kVertex_Visibility, |
100 kVec4f_GrSLType, | 98 kVec4f_GrSLType, |
101 fProgramBuilder->rtAdjustment(), | 99 "rtAdjustment", |
102 &fRtAdjustName); | 100 &rtAdjustName); |
103 // Wire transforms | |
104 SkTArray<GrGLProgramBuilder::TransformVarying, true>& transVs = fProgramBuil
der->fCoordVaryings; | |
105 int transformCount = transVs.count(); | |
106 for (int i = 0; i < transformCount; i++) { | |
107 const char* coords = transVs[i].fSourceCoords.c_str(); | |
108 | |
109 // varying = matrix * coords (logically) | |
110 const GrGLVarying& v = transVs[i].fV; | |
111 if (kVec2f_GrSLType == v.fType) { | |
112 this->codeAppendf("%s = (%s * vec3(%s, 1)).xy;", v.fVsOut, transVs[i
].fUniName.c_str(), | |
113 coords); | |
114 } else { | |
115 this->codeAppendf("%s = %s * vec3(%s, 1);", v.fVsOut, transVs[i].fUn
iName.c_str(), | |
116 coords); | |
117 } | |
118 } | |
119 | 101 |
120 // Transform from Skia's device coords to GL's normalized device coords. | 102 // 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);", | 103 this->codeAppendf("gl_Position = vec4(dot(pos3.xz, %s.xy), dot(pos3.yz, %s.z
w), 0, pos3.z);", |
122 this->glPosition(), fRtAdjustName, this->glPosition(), fRt
AdjustName, | 104 rtAdjustName, rtAdjustName); |
123 this->glPosition()); | |
124 } | 105 } |
125 | 106 |
126 void GrGLVertexBuilder::bindVertexAttributes(GrGLuint programID) { | 107 void GrGLVertexBuilder::bindVertexAttributes(GrGLuint programID) { |
127 // Bind the attrib locations to same values for all shaders | 108 // Bind the attrib locations to same values for all shaders |
128 const GrProgramDesc::KeyHeader& header = fProgramBuilder->header(); | 109 const GrProgramDesc::KeyHeader& header = fProgramBuilder->header(); |
129 SkASSERT(-1 != header.fPositionAttributeIndex); | 110 SkASSERT(-1 != header.fPositionAttributeIndex); |
130 GL_CALL(BindAttribLocation(programID, | 111 GL_CALL(BindAttribLocation(programID, |
131 header.fPositionAttributeIndex, | 112 header.fPositionAttributeIndex, |
132 fPositionVar->c_str())); | 113 fPositionVar->c_str())); |
133 if (-1 != header.fLocalCoordAttributeIndex) { | 114 if (-1 != header.fLocalCoordAttributeIndex) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 for (int i = 0; i < fInputs.count(); ++i) { | 177 for (int i = 0; i < fInputs.count(); ++i) { |
197 const GrGLShaderVar& attr = fInputs[i]; | 178 const GrGLShaderVar& attr = fInputs[i]; |
198 // if attribute already added, don't add it again | 179 // if attribute already added, don't add it again |
199 if (attr.getName().equals(var.getName())) { | 180 if (attr.getName().equals(var.getName())) { |
200 return false; | 181 return false; |
201 } | 182 } |
202 } | 183 } |
203 fInputs.push_back(var); | 184 fInputs.push_back(var); |
204 return true; | 185 return true; |
205 } | 186 } |
OLD | NEW |