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