reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2010 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #include "GrGLConfig.h" |
| 19 | |
| 20 | #if GR_SUPPORT_GLES2 || GR_SUPPORT_GLDESKTOP |
| 21 | |
| 22 | #include "GrGpuGLShaders.h" |
| 23 | #include "GrGpuVertex.h" |
| 24 | #include "GrMemory.h" |
| 25 | |
| 26 | #define ATTRIBUTE_MATRIX 0 |
| 27 | |
| 28 | #define ATTRIBUTE_TEXT_COLOR 1 |
| 29 | |
| 30 | #if ATTRIBUTE_MATRIX |
| 31 | #define DECL_MATRIX(name) "attribute mat3 " #name ";\n" |
| 32 | #else |
| 33 | #define DECL_MATRIX(name) "uniform mat3 " #name ";\n" |
| 34 | #endif |
| 35 | |
| 36 | #define SKIP_CACHE_CHECK true |
| 37 | |
| 38 | #if GR_SUPPORT_GLES2 |
| 39 | #define GR_PRECISION "mediump" |
| 40 | #define GR_SHADER_PRECISION "precision mediump float;\n" |
| 41 | #else |
| 42 | #define GR_PRECISION "" |
| 43 | #define GR_SHADER_PRECISION "" |
| 44 | #endif |
| 45 | |
| 46 | static const char* gvshad[] = { |
| 47 | // 0: kTextureVertCoords_Program, kTextureVertCoordsProj_Program, |
| 48 | // kRadialTextureVertCoords_Program, kSweepTextureVertCoords_Program |
| 49 | "attribute vec2 aPosition;\n" |
| 50 | "attribute vec4 aColor;\n" |
| 51 | "varying vec3 vTexture;\n" |
| 52 | "varying vec4 vColor;\n" |
| 53 | DECL_MATRIX(viewM) |
| 54 | DECL_MATRIX(texM) |
| 55 | "void main() {\n" |
| 56 | " vec3 pos3 = viewM*vec3(aPosition,1);\n" |
| 57 | " gl_Position = vec4(pos3.xy,0,pos3.z);\n" |
| 58 | " gl_PointSize = 1.0;\n" |
| 59 | " vTexture = texM * vec3(aPosition,1);\n" |
| 60 | " vColor = aColor;\n" |
| 61 | "}\n", |
| 62 | |
| 63 | // 1: kTextureTexCoords_Program, kTextureTexCoordsProj_Program, |
| 64 | // kRadialTextureTexCoords_Program, kSweepTextureTexCoords_Program |
| 65 | "attribute vec2 aPosition;\n" |
| 66 | "attribute vec2 aTexture;\n" |
| 67 | "attribute vec4 aColor;\n" |
| 68 | "varying vec3 vTexture;\n" |
| 69 | "varying vec4 vColor;\n" |
| 70 | DECL_MATRIX(viewM) |
| 71 | DECL_MATRIX(texM) |
| 72 | "void main() {\n" |
| 73 | " vec3 pos3 = viewM*vec3(aPosition,1);\n" |
| 74 | " gl_Position = vec4(pos3.xy,0,pos3.z);\n" |
| 75 | " gl_PointSize = 1.0;\n" |
| 76 | " vTexture = texM * vec3(aTexture,1);\n" |
| 77 | " vColor = aColor;\n" |
| 78 | "}\n", |
| 79 | |
| 80 | // 2: kText_Program |
| 81 | "attribute vec2 aPosition;\n" |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 82 | "attribute vec2 aTexture;\n" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 83 | "varying vec2 vTexture;\n" |
| 84 | DECL_MATRIX(viewM) |
| 85 | #if ATTRIBUTE_TEXT_COLOR |
| 86 | "varying vec4 vColor;\n" |
| 87 | "attribute vec4 aColor;\n" |
| 88 | #endif |
| 89 | "void main() {\n" |
| 90 | " vec3 pos3 = viewM*vec3(aPosition,1);\n" |
| 91 | " gl_Position = vec4(pos3.xy,0,pos3.z);\n" |
| 92 | " vTexture = aTexture;\n" |
| 93 | #if ATTRIBUTE_TEXT_COLOR |
| 94 | " vColor = aColor;\n" |
| 95 | #endif |
| 96 | "}\n", |
| 97 | |
| 98 | // 3: kNoTexture_Program |
| 99 | "attribute vec2 aPosition;\n" |
| 100 | "attribute vec4 aColor;\n" |
| 101 | "varying vec4 vColor;\n" |
| 102 | DECL_MATRIX(viewM) |
| 103 | "void main() {\n" |
| 104 | " vec3 pos3 = viewM*vec3(aPosition,1);\n" |
| 105 | " gl_Position = vec4(pos3.xy,0,pos3.z);\n" |
| 106 | " gl_PointSize = 1.0;\n" |
| 107 | " vColor = aColor;\n" |
| 108 | "}\n", |
| 109 | |
| 110 | // 4: kTextureVertCoordsNoColor_Program |
| 111 | "attribute vec2 aPosition;\n" |
| 112 | "attribute vec4 aColor;\n" |
| 113 | "varying vec3 vTexture;\n" |
| 114 | DECL_MATRIX(viewM) |
| 115 | DECL_MATRIX(texM) |
| 116 | "void main() {\n" |
| 117 | " vec3 pos3 = viewM*vec3(aPosition,1);\n" |
| 118 | " gl_Position = vec4(pos3.xy,0,pos3.z);\n" |
| 119 | " vTexture = texM * vec3(aPosition,1);\n" |
| 120 | "}\n", |
| 121 | |
| 122 | // 5: kTextureTexCoordsNoColor_Program |
| 123 | "attribute vec2 aPosition;\n" |
| 124 | "attribute vec2 aTexture;\n" |
| 125 | "varying vec3 vTexture;\n" |
| 126 | DECL_MATRIX(viewM) |
| 127 | DECL_MATRIX(texM) |
| 128 | "void main() {\n" |
| 129 | " vec3 pos3 = viewM*vec3(aPosition,1);\n" |
| 130 | " gl_Position = vec4(pos3.xy,0,pos3.z);\n" |
| 131 | " gl_PointSize = 1.0;\n" |
| 132 | " vTexture = texM * vec3(aTexture,1);\n" |
| 133 | "}\n", |
| 134 | |
| 135 | // 6: kTwoPointRadialTextureVertCoords_Program |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 136 | "uniform " GR_PRECISION " float uParams[6];\n" |
| 137 | // 0 is t^2 term of quadratic |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 138 | // 1 is one-half the inverse of above |
| 139 | // 2 is x offset of the second circle (post tex-matrix) |
| 140 | // 3 is the radius of the first circle (post tex-matrix) |
| 141 | // 4 is the first circle radius squared |
| 142 | // 5 is 1 to use + in the quadratic eq or -1 to use - |
| 143 | DECL_MATRIX(viewM) |
| 144 | DECL_MATRIX(texM) |
| 145 | "attribute vec2 aPosition;\n" |
| 146 | "attribute vec4 aColor;\n" |
| 147 | "varying vec4 vColor;\n" |
| 148 | "varying float vB;\n" // t coeffecient of quadratic. |
| 149 | "varying vec2 t;\n" // coordinates in canonical space |
| 150 | "void main() {\n" |
| 151 | " vec3 pos3 = viewM*vec3(aPosition,1);\n" |
| 152 | " gl_Position = vec4(pos3.xy,0,pos3.z);\n" |
| 153 | " t = vec2(texM * vec3(aPosition,1));\n" |
| 154 | " vColor = aColor;\n" |
| 155 | " vB = 2.0 * (uParams[2] * t.x - uParams[3]);\n" |
| 156 | "}\n", |
| 157 | |
| 158 | // 6: kTwoPointRadialTextureVertCoords_Program |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 159 | "uniform " GR_PRECISION " float uParams[6];\n" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 160 | DECL_MATRIX(viewM) |
| 161 | DECL_MATRIX(texM) |
| 162 | "attribute vec2 aPosition;\n" |
| 163 | "attribute vec2 aTexture;\n" |
| 164 | "attribute vec4 aColor;\n" |
| 165 | "varying vec4 vColor;\n" |
| 166 | "varying float vB;\n" // t coeffecient of quadratic. |
| 167 | "varying vec2 t;\n" // coordinates in canonical space |
| 168 | "void main() {\n" |
| 169 | " vec3 pos3 = viewM*vec3(aPosition,1);\n" |
| 170 | " gl_Position = vec4(pos3.xy,0,pos3.z);\n" |
| 171 | " t = vec2(texM * vec3(aTexture,1));\n" |
| 172 | " vColor = aColor;\n" |
| 173 | " vB = 2.0 * (uParams[2] * t.x - uParams[3]);\n" |
| 174 | "}\n", |
| 175 | }; |
| 176 | |
| 177 | static const char* gfshad[] = { |
| 178 | // 0: kTextureVertCoords_Program, kTextureTexCoords_Program |
| 179 | GR_SHADER_PRECISION |
| 180 | "varying vec3 vTexture;\n" |
| 181 | "varying vec4 vColor;\n" |
| 182 | "uniform sampler2D sTexture;\n" |
| 183 | "void main() {\n" |
| 184 | " gl_FragColor = vColor * texture2D(sTexture, vTexture.xy);\n" |
| 185 | "}\n", |
| 186 | |
| 187 | // 1: kTextureVertCoordsProj_Program, kTextureTexCoordsProj_Program |
| 188 | GR_SHADER_PRECISION |
| 189 | "varying vec3 vTexture;\n" |
| 190 | "varying vec4 vColor;\n" |
| 191 | "uniform sampler2D sTexture;\n" |
| 192 | "void main() {\n" |
| 193 | // On Brian's PC laptop with Intel Gfx texture2DProj seems to be broken |
| 194 | // but it works everywhere else tested. |
| 195 | #if GR_GLSL_2DPROJ_BROKEN |
| 196 | " gl_FragColor = vColor * texture2D(sTexture, vTexture.xy / vTexture.z);\n" |
| 197 | #else |
| 198 | " gl_FragColor = vColor * texture2DProj(sTexture, vTexture);\n" |
| 199 | #endif |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 200 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 201 | "}\n", |
| 202 | |
| 203 | // 2: kText_Program |
| 204 | GR_SHADER_PRECISION |
| 205 | "varying vec2 vTexture;\n" |
| 206 | #if ATTRIBUTE_TEXT_COLOR |
| 207 | "varying vec4 vColor;\n" |
| 208 | #else |
| 209 | "uniform vec4 uColor;\n" |
| 210 | #endif |
| 211 | "uniform sampler2D sTexture;\n" |
| 212 | "void main() {\n" |
| 213 | #if ATTRIBUTE_TEXT_COLOR |
| 214 | " gl_FragColor = vColor * texture2D(sTexture, vTexture).a;\n" |
| 215 | #else |
| 216 | " gl_FragColor = uColor * texture2D(sTexture, vTexture).a;\n" |
| 217 | #endif |
| 218 | "}\n", |
| 219 | |
| 220 | // 3: kNoTexture_Program |
| 221 | GR_SHADER_PRECISION |
| 222 | "varying vec4 vColor;\n" |
| 223 | "void main() {\n" |
| 224 | " gl_FragColor = vColor;\n" |
| 225 | "}\n", |
| 226 | |
| 227 | // 4: kTextureVertCoordsNoColor_Program |
| 228 | GR_SHADER_PRECISION |
| 229 | "varying vec3 vTexture;\n" |
| 230 | "uniform sampler2D sTexture;\n" |
| 231 | "void main() {\n" |
| 232 | " gl_FragColor = texture2D(sTexture, vTexture.xy);\n" |
| 233 | "}\n", |
| 234 | |
| 235 | // 5: kRadialTextureVertCoords_Program, kRadialTextureTexCoords_Program |
| 236 | GR_SHADER_PRECISION |
| 237 | "varying vec3 vTexture;\n" |
| 238 | "varying vec4 vColor;\n" |
| 239 | "uniform sampler2D sTexture;\n" |
| 240 | "void main() {\n" |
| 241 | " gl_FragColor = vColor * texture2D(sTexture, vec2(length(vTexture.xy), 0.5));\n" |
| 242 | "}\n", |
| 243 | |
| 244 | // 6: kSweepTextureVertCoords_Program, kSweepTextureTexCoords_Program |
| 245 | GR_SHADER_PRECISION |
| 246 | "varying vec3 vTexture;\n" |
| 247 | "varying vec4 vColor;\n" |
| 248 | "uniform sampler2D sTexture;\n" |
| 249 | "void main() {\n" |
| 250 | " vec2 t = vec2(atan(-vTexture.y, -vTexture.x)*0.1591549430918 + 0.5,\n" |
| 251 | " 0.5);\n" |
| 252 | " gl_FragColor = vColor * texture2D(sTexture, t);\n" |
| 253 | "}\n", |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 254 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 255 | // 7: kTwoPointRadialTextureVertCoords_Program, kTwoPointRadialTextureTexCoords_Program |
| 256 | GR_SHADER_PRECISION |
| 257 | "varying vec4 vColor;\n" |
| 258 | "varying float vB;\n" // t coeffecient of quadratic. |
| 259 | "varying vec2 t;\n" // coordinates in canonical radial gradient space |
| 260 | "uniform sampler2D sTexture;\n" |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 261 | "uniform float uParams[6];\n" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 262 | "void main() {\n" |
| 263 | "float c = t.x*t.x + t.y*t.y - uParams[4];\n" |
| 264 | "float ac4 = uParams[0] * c * 4.0;\n" |
| 265 | "float root = sqrt(abs(vB * vB - ac4));\n" |
| 266 | "float t = (-vB + uParams[5] * root) * uParams[1];\n" |
| 267 | "gl_FragColor = vColor * texture2D(sTexture, vec2(t,0.5))\n;" |
| 268 | "}\n", |
| 269 | }; |
| 270 | |
| 271 | // determines which frag/vert shaders are used for each program in Programs enum |
| 272 | |
| 273 | static const struct { |
| 274 | int fVShaderIdx; |
| 275 | int fFShaderIdx; |
| 276 | bool fHasTexMatrix; |
| 277 | bool fHasTexCoords; |
| 278 | bool fTwoPointRadial; |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 279 | GrGpuGLShaders::ColorType fColorType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 280 | } gProgramLoadData[] = { |
| 281 | // kTextureVertCoords_Program |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 282 | {0, 0, true, false, false, GrGpuGLShaders::kAttrib_ColorType }, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 283 | // kTextureVertCoordsProj_Program |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 284 | {0, 1, true, false, false, GrGpuGLShaders::kAttrib_ColorType }, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 285 | // kTextureTexCoords_Program |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 286 | {1, 0, true, true, false, GrGpuGLShaders::kAttrib_ColorType }, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 287 | // kTextureTexCoordsProj_Program |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 288 | {1, 1, true, true, false, GrGpuGLShaders::kAttrib_ColorType }, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 289 | // kTextureVertCoordsNoColor_Program |
| 290 | {4, 4, true, false, false, GrGpuGLShaders::kNone_ColorType }, |
| 291 | // kTextureTexCoordsNoColor_Program |
| 292 | {5, 4, true, false, false, GrGpuGLShaders::kNone_ColorType }, |
| 293 | // kText_Program |
| 294 | #if ATTRIBUTE_TEXT_COLOR |
| 295 | {2, 2, false, true, false, GrGpuGLShaders::kAttrib_ColorType }, |
| 296 | #else |
| 297 | {2, 2, false, true, false, GrGpuGLShaders::kUniform_ColorType }, |
| 298 | #endif |
| 299 | // kRadialTextureVertCoords_Program |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 300 | {0, 5, true, false, false, GrGpuGLShaders::kAttrib_ColorType }, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 301 | // kRadialTextureTexCoords_Program |
| 302 | {1, 5, true, true, false, GrGpuGLShaders::kAttrib_ColorType }, |
| 303 | // kSweepTextureVertCoords_Program |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 304 | {0, 6, true, false, false, GrGpuGLShaders::kAttrib_ColorType }, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 305 | // kSweepTextureTexCoords_Program |
| 306 | {1, 6, true, true, false, GrGpuGLShaders::kAttrib_ColorType }, |
| 307 | // kTwoPointRadialTextureVertCoords_Program |
| 308 | {6, 7, true, false, true, GrGpuGLShaders::kAttrib_ColorType }, |
| 309 | // kTwoPointRadialTextureTexCoords_Program |
| 310 | {7, 7, true, true, true, GrGpuGLShaders::kAttrib_ColorType }, |
| 311 | // kNoTexture_Program |
| 312 | {3, 3, false, false, false, GrGpuGLShaders::kAttrib_ColorType }, |
| 313 | }; |
| 314 | |
| 315 | #define GR_GL_POS_ATTR_LOCATION 0 |
| 316 | #define GR_GL_TEX_ATTR_LOCATION 1 |
| 317 | #define GR_GL_COL_ATTR_LOCATION 2 |
| 318 | #if ATTRIBUTE_MATRIX |
| 319 | #define GR_GL_MAT_ATTR_LOCATION 3 |
| 320 | #define GR_GL_TEXMAT_ATTR_LOCATION 6 |
| 321 | #endif |
| 322 | |
| 323 | GLuint GrGpuGLShaders::loadShader(GLenum type, const char* src) { |
| 324 | GLuint shader = GR_GL(CreateShader(type)); |
| 325 | if (0 == shader) { |
| 326 | return 0; |
| 327 | } |
| 328 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 329 | GR_GL(ShaderSource(shader, 1, &src, NULL)); |
| 330 | GR_GL(CompileShader(shader)); |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 331 | |
| 332 | GLint compiled = GR_GL_INIT_ZERO; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 333 | GR_GL(GetShaderiv(shader, GL_COMPILE_STATUS, &compiled)); |
| 334 | |
| 335 | if (!compiled) { |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 336 | GLint infoLen = GR_GL_INIT_ZERO; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 337 | GR_GL(GetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen)); |
| 338 | GrAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 339 | if (infoLen > 0) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 340 | GR_GL(GetShaderInfoLog(shader, infoLen+1, NULL, (char*)log.get())); |
| 341 | GrPrintf((char*)log.get()); |
| 342 | } |
| 343 | GrAssert(!"Shader compilation failed!"); |
| 344 | GR_GL(DeleteShader(shader)); |
| 345 | return 0; |
| 346 | } |
| 347 | return shader; |
| 348 | } |
| 349 | |
| 350 | bool GrGpuGLShaders::createProgram(GLuint vshader, GLuint fshader, |
| 351 | bool hasTexMatrix, |
| 352 | bool hasTexCoords, |
| 353 | GrGpuGLShaders::ColorType colorType, |
| 354 | bool twoPointRadial, |
| 355 | ProgramData* program) { |
| 356 | program->fProgramID = GR_GL(CreateProgram()); |
| 357 | program->fVShaderID = vshader; |
| 358 | program->fFShaderID = fshader; |
| 359 | |
| 360 | GrAssert(0 != program->fProgramID); |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 361 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 362 | GR_GL(AttachShader(program->fProgramID, vshader)); |
| 363 | GR_GL(AttachShader(program->fProgramID, fshader)); |
| 364 | |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 365 | GR_GL(BindAttribLocation(program->fProgramID, |
| 366 | GR_GL_POS_ATTR_LOCATION, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 367 | "aPosition")); |
| 368 | if (hasTexCoords) { |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 369 | GR_GL(BindAttribLocation(program->fProgramID, |
| 370 | GR_GL_TEX_ATTR_LOCATION, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 371 | "aTexture")); |
| 372 | } |
| 373 | #if ATTRIBUTE_MATRIX |
| 374 | if (hasTexMatrix) { |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 375 | GR_GL(BindAttribLocation(program->fProgramID, |
| 376 | GR_GL_TEXMAT_ATTR_LOCATION, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 377 | "texM")); |
| 378 | // set to something arbitrary to signal to flush that program |
| 379 | // uses the texture matrix. |
| 380 | program->fTexMatrixLocation = 1000; |
| 381 | } |
| 382 | #endif |
| 383 | if (colorType == kAttrib_ColorType) { |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 384 | GR_GL(BindAttribLocation(program->fProgramID, |
| 385 | GR_GL_COL_ATTR_LOCATION, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 386 | "aColor")); |
| 387 | } |
| 388 | #if ATTRIBUTE_MATRIX |
| 389 | GR_GL(BindAttribLocation(program->fProgramID, |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 390 | GR_GL_MAT_ATTR_LOCATION, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 391 | "viewM")); |
| 392 | #endif |
| 393 | |
| 394 | GR_GL(LinkProgram(program->fProgramID)); |
| 395 | |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 396 | GLint linked = GR_GL_INIT_ZERO; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 397 | GR_GL(GetProgramiv(program->fProgramID, GL_LINK_STATUS, &linked)); |
| 398 | if (!linked) { |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 399 | GLint infoLen = GR_GL_INIT_ZERO; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 400 | GR_GL(GetProgramiv(program->fProgramID, GL_INFO_LOG_LENGTH, &infoLen)); |
| 401 | GrAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 402 | if (infoLen > 0) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 403 | GR_GL(GetProgramInfoLog(program->fProgramID, |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 404 | infoLen+1, |
| 405 | NULL, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 406 | (char*)log.get())); |
| 407 | GrPrintf((char*)log.get()); |
| 408 | } |
| 409 | GrAssert(!"Error linking program"); |
| 410 | GR_GL(DeleteProgram(program->fProgramID)); |
| 411 | program->fProgramID = 0; |
| 412 | return false; |
| 413 | } |
| 414 | program->fColorType = colorType; |
| 415 | |
| 416 | #if !ATTRIBUTE_MATRIX |
| 417 | program->fMatrixLocation = |
| 418 | GR_GL(GetUniformLocation(program->fProgramID, "viewM")); |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 419 | program->fTexMatrixLocation = |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 420 | GR_GL(GetUniformLocation(program->fProgramID, "texM")); |
| 421 | #endif |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 422 | program->fColorLocation = |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 423 | GR_GL(GetUniformLocation(program->fProgramID, "uColor")); |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 424 | program->fTwoPointParamsLocation = |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 425 | GR_GL(GetUniformLocation(program->fProgramID, "uParams")); |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 426 | |
| 427 | GLint samplerLocation = |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 428 | GR_GL(GetUniformLocation(program->fProgramID, "sTexture")); |
| 429 | |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 430 | #if !ATTRIBUTE_MATRIX |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 431 | if (-1 == program->fMatrixLocation) { |
| 432 | GrAssert(!"Cannot find matrix uniform in program"); |
| 433 | GR_GL(DeleteProgram(program->fProgramID)); |
| 434 | program->fProgramID = 0; |
| 435 | return false; |
| 436 | } |
| 437 | #endif |
| 438 | |
| 439 | bool hasTexture = hasTexCoords || hasTexMatrix; |
| 440 | |
| 441 | if (-1 == samplerLocation && hasTexture) { |
| 442 | GrAssert(!"Expected to find texture sampler"); |
| 443 | GR_GL(DeleteProgram(program->fProgramID)); |
| 444 | program->fProgramID = 0; |
| 445 | return false; |
| 446 | } else if (-1 != samplerLocation && !hasTexture) { |
| 447 | GrAssert(!"unexpectedly found texture sampler"); |
| 448 | } |
reed@google.com | f44aa37 | 2011-01-19 13:15:36 +0000 | [diff] [blame] | 449 | #if !ATTRIBUTE_MATRIX && !defined(GR_SKIP_2POINTRADIAL_PROGRAMS) |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 450 | if (-1 == program->fTexMatrixLocation && hasTexMatrix) { |
| 451 | GrAssert(!"Expected to find texture matrix"); |
| 452 | GR_GL(DeleteProgram(program->fProgramID)); |
| 453 | program->fProgramID = 0; |
| 454 | return false; |
| 455 | } else if (-1 != program->fTexMatrixLocation && !hasTexMatrix) { |
| 456 | GrAssert(!"unexpectedly found texture matrix"); |
| 457 | } |
| 458 | #endif |
| 459 | |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 460 | if (-1 == program->fColorLocation && |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 461 | (kUniform_ColorType == colorType)) { |
| 462 | GR_GL(DeleteProgram(program->fProgramID)); |
| 463 | program->fProgramID = 0; |
| 464 | return false; |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 465 | } else if (-1 != program->fColorLocation && |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 466 | (kUniform_ColorType != colorType)) { |
| 467 | GrAssert(!"Unexpectedly found color uniform"); |
| 468 | } |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 469 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 470 | if (twoPointRadial) { |
| 471 | if (-1 == program->fTwoPointParamsLocation) { |
| 472 | GrAssert(!"Didn't find expected uniform for 2pt radial gradient"); |
| 473 | GR_GL(DeleteProgram(program->fProgramID)); |
| 474 | program->fProgramID = 0; |
| 475 | return false; |
| 476 | } |
| 477 | } else { |
| 478 | GrAssert(-1 == program->fTwoPointParamsLocation); |
| 479 | } |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 480 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 481 | GR_GL(UseProgram(program->fProgramID)); |
| 482 | if (-1 != samplerLocation) { |
| 483 | GR_GL(Uniform1i(samplerLocation, 0)); |
| 484 | } |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 485 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 486 | return true; |
| 487 | } |
| 488 | |
| 489 | GrGpuGLShaders::GrGpuGLShaders() { |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 490 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 491 | resetContextHelper(); |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 492 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 493 | GLuint vshadIDs[GR_ARRAY_COUNT(gvshad)]; |
| 494 | for (size_t s = 0; s < GR_ARRAY_COUNT(gvshad); ++s) { |
| 495 | vshadIDs[s] = loadShader(GL_VERTEX_SHADER, gvshad[s]); |
| 496 | } |
| 497 | |
| 498 | GLuint fshadIDs[GR_ARRAY_COUNT(gfshad)]; |
| 499 | for (size_t s = 0; s < GR_ARRAY_COUNT(gfshad); ++s) { |
| 500 | fshadIDs[s] = loadShader(GL_FRAGMENT_SHADER, gfshad[s]); |
| 501 | } |
| 502 | |
| 503 | GR_STATIC_ASSERT(kProgramCount == GR_ARRAY_COUNT(gProgramLoadData)); |
| 504 | for (int p = 0; p < kProgramCount; ++p) { |
reed@google.com | f44aa37 | 2011-01-19 13:15:36 +0000 | [diff] [blame] | 505 | #ifdef GR_SKIP_2POINTRADIAL_PROGRAMS |
| 506 | if (11 == p || 12 == p) continue; |
| 507 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 508 | GR_DEBUGCODE(bool result = ) |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 509 | createProgram(vshadIDs[gProgramLoadData[p].fVShaderIdx], |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 510 | fshadIDs[gProgramLoadData[p].fFShaderIdx], |
| 511 | gProgramLoadData[p].fHasTexMatrix, |
| 512 | gProgramLoadData[p].fHasTexCoords, |
| 513 | gProgramLoadData[p].fColorType, |
| 514 | gProgramLoadData[p].fTwoPointRadial, |
| 515 | &fPrograms[p]); |
| 516 | GR_DEBUGASSERT(result); |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 517 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 518 | fPrograms[p].fViewMatrix.setScale(GR_ScalarMax, GR_ScalarMax); |
| 519 | for (int s = 0; s < kNumStages; ++s) { |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 520 | fPrograms[p].fTextureMatrices[s].setScale(GR_ScalarMax, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 521 | GR_ScalarMax); // illegal |
| 522 | }; |
| 523 | fPrograms[p].fColor = GrColor_ILLEGAL; |
| 524 | fPrograms[p].fTextureOrientation = (GrGLTexture::Orientation)-1; // illegal |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 525 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 526 | // these aren't strictly invalid, just really unlikely. |
| 527 | fPrograms[p].fRadial2CenterX1 = GR_ScalarMin; |
| 528 | fPrograms[p].fRadial2Radius0 = GR_ScalarMin; |
| 529 | fPrograms[p].fRadial2PosRoot = true; // arbitrary |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | GrGpuGLShaders::~GrGpuGLShaders() { |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 534 | // shaders get deleted once for each program that uses them, do we care? |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 535 | // probably not |
| 536 | for (int i = 0; i < kProgramCount; ++i) { |
| 537 | GR_GL(DeleteProgram(fPrograms[i].fProgramID)); |
| 538 | GR_GL(DeleteShader(fPrograms[i].fVShaderID)); |
| 539 | GR_GL(DeleteShader(fPrograms[i].fFShaderID)); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | void GrGpuGLShaders::resetContext() { |
| 544 | INHERITED::resetContext(); |
| 545 | resetContextHelper(); |
| 546 | } |
| 547 | |
| 548 | void GrGpuGLShaders::resetContextHelper() { |
| 549 | fHWProgram = (Programs)-1; |
| 550 | fTextureOrientation = (GrGLTexture::Orientation)-1; // illegal |
| 551 | |
| 552 | fHWGeometryState.fVertexLayout = 0; |
| 553 | fHWGeometryState.fPositionPtr = (void*) ~0; |
| 554 | GR_GL(DisableVertexAttribArray(GR_GL_COL_ATTR_LOCATION)); |
| 555 | GR_GL(DisableVertexAttribArray(GR_GL_TEX_ATTR_LOCATION)); |
| 556 | GR_GL(EnableVertexAttribArray(GR_GL_POS_ATTR_LOCATION)); |
| 557 | } |
| 558 | |
| 559 | |
| 560 | void GrGpuGLShaders::flushMatrix(GLint location) { |
| 561 | GrAssert(NULL != fCurrDrawState.fRenderTarget); |
| 562 | GrMatrix m ( |
| 563 | GrIntToScalar(2) / fCurrDrawState.fRenderTarget->width(), 0, -GR_Scalar1, |
| 564 | 0,-GrIntToScalar(2) / fCurrDrawState.fRenderTarget->height(), GR_Scalar1, |
| 565 | 0, 0, GrMatrix::I()[8]); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 566 | m.setConcat(m, fCurrDrawState.fViewMatrix); |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 567 | |
| 568 | // ES doesn't allow you to pass true to the transpose param, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 569 | // so do our own transpose |
| 570 | GrScalar mt[] = { |
| 571 | m[GrMatrix::kScaleX], |
| 572 | m[GrMatrix::kSkewY], |
| 573 | m[GrMatrix::kPersp0], |
| 574 | m[GrMatrix::kSkewX], |
| 575 | m[GrMatrix::kScaleY], |
| 576 | m[GrMatrix::kPersp1], |
| 577 | m[GrMatrix::kTransX], |
| 578 | m[GrMatrix::kTransY], |
| 579 | m[GrMatrix::kPersp2] |
| 580 | }; |
| 581 | #if ATTRIBUTE_MATRIX |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 582 | GR_GL(VertexAttrib4fv(GR_GL_MAT_ATTR_LOCATION+0, mt+0)); |
| 583 | GR_GL(VertexAttrib4fv(GR_GL_MAT_ATTR_LOCATION+1, mt+3)); |
| 584 | GR_GL(VertexAttrib4fv(GR_GL_MAT_ATTR_LOCATION+2, mt+6)); |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 585 | #else |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 586 | GR_GL(UniformMatrix3fv(location,1,false,mt)); |
| 587 | #endif |
| 588 | } |
| 589 | |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 590 | void GrGpuGLShaders::flushTexMatrix(GLint location, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 591 | GrGLTexture::Orientation orientation) { |
| 592 | GrMatrix* m; |
| 593 | GrMatrix temp; |
| 594 | if (GrGLTexture::kBottomUp_Orientation == orientation) { |
| 595 | temp.setAll( |
| 596 | GR_Scalar1, 0, 0, |
| 597 | 0, -GR_Scalar1, GR_Scalar1, |
| 598 | 0, 0, GrMatrix::I()[8] |
| 599 | ); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 600 | temp.preConcat(fCurrDrawState.fTextureMatrices[0]); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 601 | m = &temp; |
| 602 | } else { |
| 603 | GrAssert(GrGLTexture::kTopDown_Orientation == orientation); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 604 | m = &fCurrDrawState.fTextureMatrices[0]; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | // ES doesn't allow you to pass true to the transpose param, |
| 608 | // so do our own transpose |
| 609 | GrScalar mt[] = { |
| 610 | (*m)[GrMatrix::kScaleX], |
| 611 | (*m)[GrMatrix::kSkewY], |
| 612 | (*m)[GrMatrix::kPersp0], |
| 613 | (*m)[GrMatrix::kSkewX], |
| 614 | (*m)[GrMatrix::kScaleY], |
| 615 | (*m)[GrMatrix::kPersp1], |
| 616 | (*m)[GrMatrix::kTransX], |
| 617 | (*m)[GrMatrix::kTransY], |
| 618 | (*m)[GrMatrix::kPersp2] |
| 619 | }; |
| 620 | #if ATTRIBUTE_MATRIX |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 621 | GR_GL(VertexAttrib4fv(GR_GL_TEXMAT_ATTR_LOCATION+0, mt+0)); |
| 622 | GR_GL(VertexAttrib4fv(GR_GL_TEXMAT_ATTR_LOCATION+1, mt+3)); |
| 623 | GR_GL(VertexAttrib4fv(GR_GL_TEXMAT_ATTR_LOCATION+2, mt+6)); |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 624 | #else |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 625 | GR_GL(UniformMatrix3fv(location,1,false,mt)); |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 626 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | void GrGpuGLShaders::flushTwoPointRadial(GLint paramsLocation, |
| 630 | const GrSamplerState& state) { |
| 631 | GrScalar centerX1 = state.getRadial2CenterX1(); |
| 632 | GrScalar radius0 = state.getRadial2Radius0(); |
| 633 | |
| 634 | GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1; |
| 635 | |
| 636 | float unis[6] = { |
| 637 | GrScalarToFloat(a), |
| 638 | 1 / (2.f * unis[0]), |
| 639 | GrScalarToFloat(centerX1), |
| 640 | GrScalarToFloat(radius0), |
| 641 | GrScalarToFloat(GrMul(radius0, radius0)), |
| 642 | state.isRadial2PosRoot() ? 1.f : -1.f |
| 643 | }; |
| 644 | GR_GL(Uniform1fv(paramsLocation, 6, unis)); |
| 645 | } |
| 646 | |
| 647 | void GrGpuGLShaders::flushProgram(PrimitiveType type) { |
| 648 | |
| 649 | Programs nextProgram = kNoTexture_Program; |
| 650 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 651 | GrTexture* texture = fCurrDrawState.fTextures[0]; |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 652 | bool posAsTex = |
bsalomon@google.com | 42ab7ea | 2011-01-19 17:19:40 +0000 | [diff] [blame^] | 653 | !!(StagePosAsTexCoordVertexLayoutBit(0) & fGeometrySrc.fVertexLayout); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 654 | |
| 655 | if (!VertexUsesStage(0, fGeometrySrc.fVertexLayout)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 656 | goto HAVE_NEXT_PROGRAM; |
| 657 | } |
| 658 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 659 | GrAssert(NULL != texture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 660 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 661 | switch (fCurrDrawState.fSamplerStates[0].getSampleMode()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 662 | case GrSamplerState::kRadial_SampleMode: |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 663 | GrAssert(!fCurrDrawState.fTextureMatrices[0].hasPerspective()); |
| 664 | GrAssert(GrTexture::kAlpha_8_PixelConfig != texture->config()); |
| 665 | if (posAsTex) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 666 | nextProgram = kRadialTextureVertCoords_Program; |
| 667 | } else { |
| 668 | nextProgram = kRadialTextureTexCoords_Program; |
| 669 | } |
| 670 | break; |
| 671 | case GrSamplerState::kSweep_SampleMode: |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 672 | GrAssert(!fCurrDrawState.fTextureMatrices[0].hasPerspective()); |
| 673 | GrAssert(GrTexture::kAlpha_8_PixelConfig != texture->config()); |
| 674 | if (posAsTex) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 675 | nextProgram = kSweepTextureVertCoords_Program; |
| 676 | } else { |
| 677 | nextProgram = kSweepTextureTexCoords_Program; |
| 678 | } |
| 679 | break; |
| 680 | case GrSamplerState::kRadial2_SampleMode: |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 681 | GrAssert(!fCurrDrawState.fTextureMatrices[0].hasPerspective()); |
| 682 | GrAssert(GrTexture::kAlpha_8_PixelConfig != texture->config()); |
| 683 | if (posAsTex) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 684 | nextProgram = kTwoPointRadialTextureVertCoords_Program; |
| 685 | } else { |
| 686 | nextProgram = kTwoPointRadialTextureTexCoords_Program; |
| 687 | } |
| 688 | break; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 689 | case GrSamplerState::kNormal_SampleMode: |
| 690 | if (GrTexture::kAlpha_8_PixelConfig == texture->config()) { |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 691 | GrAssert(((GrGLTexture*)texture)->orientation() == |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 692 | GrGLTexture::kTopDown_Orientation); |
| 693 | GrAssert(!posAsTex); |
| 694 | nextProgram = kText_Program; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 695 | } else { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 696 | bool persp = fCurrDrawState.fTextureMatrices[0].hasPerspective(); |
| 697 | if (posAsTex) { |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 698 | nextProgram = persp ? kTextureVertCoordsProj_Program : |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 699 | kTextureVertCoords_Program; |
| 700 | } else { |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 701 | nextProgram = persp ? kTextureTexCoordsProj_Program : |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 702 | kTextureTexCoords_Program; |
| 703 | } |
| 704 | // check for case when frag shader can skip the color modulation |
| 705 | if (!persp && !(fGeometrySrc.fVertexLayout |
| 706 | & kColor_VertexLayoutBit) && |
| 707 | 0xffffffff == fCurrDrawState.fColor) { |
| 708 | switch (nextProgram) { |
| 709 | case kTextureVertCoords_Program: |
| 710 | nextProgram = kTextureVertCoordsNoColor_Program; |
| 711 | break; |
| 712 | case kTextureTexCoords_Program: |
| 713 | nextProgram = kTextureTexCoordsNoColor_Program; |
| 714 | break; |
| 715 | default: |
| 716 | GrAssert("Unexpected"); |
| 717 | break; |
| 718 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 719 | } |
| 720 | } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 721 | break; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 722 | default: |
| 723 | GrAssert(!"Unknown samplemode"); |
| 724 | break; |
| 725 | } |
| 726 | |
| 727 | HAVE_NEXT_PROGRAM: |
| 728 | if (fHWProgram != nextProgram) { |
| 729 | GR_GL(UseProgram(fPrograms[nextProgram].fProgramID)); |
| 730 | fHWProgram = nextProgram; |
| 731 | #if GR_COLLECT_STATS |
| 732 | ++fStats.fProgChngCnt; |
| 733 | #endif |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | bool GrGpuGLShaders::flushGraphicsState(PrimitiveType type) { |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 738 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 739 | for (int s = 1; s < kNumStages; ++s) { |
| 740 | if (VertexUsesStage(s, fGeometrySrc.fVertexLayout)) { |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 741 | unimpl("the hard-coded shaders used by this " |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 742 | "class only support 1 stage"); |
| 743 | return false; |
| 744 | } |
| 745 | } |
| 746 | |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 747 | if (!flushGLStateCommon(type)) { |
| 748 | return false; |
| 749 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 750 | |
| 751 | if (fRenderTargetChanged) { |
| 752 | // our coords are in pixel space and the GL matrices map to NDC |
| 753 | // so if the viewport changed, our matrix is now wrong. |
| 754 | #if ATTRIBUTE_MATRIX |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 755 | fHWDrawState.fViewMatrix.setScale(GR_ScalarMax, GR_ScalarMax); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 756 | #else |
| 757 | // we assume all shader matrices may be wrong after viewport changes |
| 758 | for (int p = 0; p < kProgramCount; ++p) { |
| 759 | // set to illegal matrix |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 760 | fPrograms[p].fViewMatrix.setScale(GR_ScalarMax, GR_ScalarMax); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 761 | } |
| 762 | #endif |
| 763 | fRenderTargetChanged = false; |
| 764 | } |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 765 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 766 | flushProgram(type); |
| 767 | |
| 768 | if (fGeometrySrc.fVertexLayout & kColor_VertexLayoutBit) { |
| 769 | // invalidate the immediate mode color |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 770 | fHWDrawState.fColor = GrColor_ILLEGAL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 771 | } else { |
| 772 | // if we don't have per-vert colors either set the color attr |
| 773 | // or color uniform (depending on which program). |
| 774 | if (-1 != fPrograms[fHWProgram].fColorLocation) { |
| 775 | GrAssert(kUniform_ColorType == fPrograms[fHWProgram].fColorType); |
| 776 | if (fPrograms[fHWProgram].fColor != fCurrDrawState.fColor) { |
| 777 | float c[] = { |
| 778 | GrColorUnpackR(fCurrDrawState.fColor) / 255.f, |
| 779 | GrColorUnpackG(fCurrDrawState.fColor) / 255.f, |
| 780 | GrColorUnpackB(fCurrDrawState.fColor) / 255.f, |
| 781 | GrColorUnpackA(fCurrDrawState.fColor) / 255.f |
| 782 | }; |
| 783 | GR_GL(Uniform4fv(fPrograms[fHWProgram].fColorLocation, 1, c)); |
| 784 | fPrograms[fHWProgram].fColor = fCurrDrawState.fColor; |
| 785 | } |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 786 | } else if (kAttrib_ColorType == fPrograms[fHWProgram].fColorType && |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 787 | fHWDrawState.fColor != fCurrDrawState.fColor) { |
| 788 | // OpenGL ES only supports the float varities of glVertexAttrib |
| 789 | float c[] = { |
| 790 | GrColorUnpackR(fCurrDrawState.fColor) / 255.f, |
| 791 | GrColorUnpackG(fCurrDrawState.fColor) / 255.f, |
| 792 | GrColorUnpackB(fCurrDrawState.fColor) / 255.f, |
| 793 | GrColorUnpackA(fCurrDrawState.fColor) / 255.f |
| 794 | }; |
| 795 | GR_GL(VertexAttrib4fv(GR_GL_COL_ATTR_LOCATION, c)); |
| 796 | fHWDrawState.fColor = fCurrDrawState.fColor; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | #if ATTRIBUTE_MATRIX |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 801 | GrMatrix& currentViewMatrix = fHWDrawState.fViewMatrix; |
| 802 | GrMatrix& currentTexMatrix = fHWDrawState.fTextureMatrices[0]; |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 803 | GrGLTexture::Orientation& orientation = fTextureOrientation; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 804 | #else |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 805 | GrMatrix& currentViewMatrix = fPrograms[fHWProgram].fViewMatrix; |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 806 | GrMatrix& currentTexMatrix = fPrograms[fHWProgram].fTextureMatrices[0]; |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 807 | GrGLTexture::Orientation& orientation = |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 808 | fPrograms[fHWProgram].fTextureOrientation; |
| 809 | #endif |
| 810 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 811 | if (currentViewMatrix != |
| 812 | fCurrDrawState.fViewMatrix) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 813 | flushMatrix(fPrograms[fHWProgram].fMatrixLocation); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 814 | currentViewMatrix = fCurrDrawState.fViewMatrix; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 815 | } |
| 816 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 817 | GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[0]; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 818 | if (NULL != texture) { |
| 819 | if (-1 != fPrograms[fHWProgram].fTexMatrixLocation && |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 820 | (currentTexMatrix != fCurrDrawState.fTextureMatrices[0] || |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 821 | orientation != texture->orientation())) { |
| 822 | flushTexMatrix(fPrograms[fHWProgram].fTexMatrixLocation, |
| 823 | texture->orientation()); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 824 | currentTexMatrix = fCurrDrawState.fTextureMatrices[0]; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 825 | orientation = texture->orientation(); |
| 826 | } |
| 827 | } |
| 828 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 829 | const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[0]; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 830 | if (-1 != fPrograms[fHWProgram].fTwoPointParamsLocation && |
| 831 | (fPrograms[fHWProgram].fRadial2CenterX1 != sampler.getRadial2CenterX1() || |
| 832 | fPrograms[fHWProgram].fRadial2Radius0 != sampler.getRadial2Radius0() || |
| 833 | fPrograms[fHWProgram].fRadial2PosRoot != sampler.isRadial2PosRoot())) { |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 834 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 835 | flushTwoPointRadial(fPrograms[fHWProgram].fTwoPointParamsLocation, |
| 836 | sampler); |
| 837 | fPrograms[fHWProgram].fRadial2CenterX1 = sampler.getRadial2CenterX1(); |
| 838 | fPrograms[fHWProgram].fRadial2Radius0 = sampler.getRadial2Radius0(); |
| 839 | fPrograms[fHWProgram].fRadial2PosRoot = sampler.isRadial2PosRoot(); |
| 840 | } |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 841 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 842 | return true; |
| 843 | } |
| 844 | |
| 845 | void GrGpuGLShaders::setupGeometry(uint32_t startVertex, |
| 846 | uint32_t startIndex, |
| 847 | uint32_t vertexCount, |
| 848 | uint32_t indexCount) { |
| 849 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 850 | int newColorOffset; |
| 851 | int newTexCoordOffsets[kNumStages]; |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 852 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 853 | GLsizei newStride = VertexSizeAndOffsetsByStage(fGeometrySrc.fVertexLayout, |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 854 | newTexCoordOffsets, |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 855 | &newColorOffset); |
| 856 | int oldColorOffset; |
| 857 | int oldTexCoordOffsets[kNumStages]; |
| 858 | GLsizei oldStride = VertexSizeAndOffsetsByStage(fHWGeometryState.fVertexLayout, |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 859 | oldTexCoordOffsets, |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 860 | &oldColorOffset); |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 861 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 862 | const GLvoid* posPtr = (GLvoid*)(newStride * startVertex); |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 863 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 864 | if (kBuffer_GeometrySrcType == fGeometrySrc.fVertexSrc) { |
| 865 | GrAssert(NULL != fGeometrySrc.fVertexBuffer); |
| 866 | GrAssert(!fGeometrySrc.fVertexBuffer->isLocked()); |
| 867 | if (fHWGeometryState.fVertexBuffer != fGeometrySrc.fVertexBuffer) { |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 868 | GrGLVertexBuffer* buf = |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 869 | (GrGLVertexBuffer*)fGeometrySrc.fVertexBuffer; |
| 870 | GR_GL(BindBuffer(GL_ARRAY_BUFFER, buf->bufferID())); |
| 871 | fHWGeometryState.fVertexBuffer = fGeometrySrc.fVertexBuffer; |
| 872 | } |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 873 | } else { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 874 | if (kArray_GeometrySrcType == fGeometrySrc.fVertexSrc) { |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 875 | posPtr = (void*)((intptr_t)fGeometrySrc.fVertexArray + |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 876 | (intptr_t)posPtr); |
| 877 | } else { |
| 878 | GrAssert(kReserved_GeometrySrcType == fGeometrySrc.fVertexSrc); |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 879 | posPtr = (void*)((intptr_t)fVertices.get() + (intptr_t)posPtr); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 880 | } |
| 881 | if (NULL != fHWGeometryState.fVertexBuffer) { |
| 882 | GR_GL(BindBuffer(GL_ARRAY_BUFFER, 0)); |
| 883 | fHWGeometryState.fVertexBuffer = NULL; |
| 884 | } |
| 885 | } |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 886 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 887 | if (kBuffer_GeometrySrcType == fGeometrySrc.fIndexSrc) { |
| 888 | GrAssert(NULL != fGeometrySrc.fIndexBuffer); |
| 889 | GrAssert(!fGeometrySrc.fIndexBuffer->isLocked()); |
| 890 | if (fHWGeometryState.fIndexBuffer != fGeometrySrc.fIndexBuffer) { |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 891 | GrGLIndexBuffer* buf = |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 892 | (GrGLIndexBuffer*)fGeometrySrc.fIndexBuffer; |
| 893 | GR_GL(BindBuffer(GL_ELEMENT_ARRAY_BUFFER, buf->bufferID())); |
| 894 | fHWGeometryState.fIndexBuffer = fGeometrySrc.fIndexBuffer; |
| 895 | } |
| 896 | } else if (NULL != fHWGeometryState.fIndexBuffer) { |
| 897 | GR_GL(BindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)); |
| 898 | fHWGeometryState.fIndexBuffer = NULL; |
| 899 | } |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 900 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 901 | GLenum scalarType; |
| 902 | bool texCoordNorm; |
| 903 | if (fGeometrySrc.fVertexLayout & kTextFormat_VertexLayoutBit) { |
| 904 | scalarType = GrGLTextType; |
| 905 | texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED; |
| 906 | } else { |
| 907 | scalarType = GrGLType; |
| 908 | texCoordNorm = false; |
| 909 | } |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 910 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 911 | bool baseChange = posPtr != fHWGeometryState.fPositionPtr; |
| 912 | bool scalarChange = (GrGLTextType != GrGLType) && |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 913 | (kTextFormat_VertexLayoutBit & |
| 914 | (fHWGeometryState.fVertexLayout ^ |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 915 | fGeometrySrc.fVertexLayout)); |
| 916 | bool strideChange = newStride != oldStride; |
| 917 | bool posChange = baseChange || scalarChange || strideChange; |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 918 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 919 | if (posChange) { |
| 920 | GR_GL(VertexAttribPointer(GR_GL_POS_ATTR_LOCATION, 2, scalarType, |
| 921 | false, newStride, posPtr)); |
| 922 | fHWGeometryState.fPositionPtr = posPtr; |
| 923 | } |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 924 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 925 | // this class only supports one stage. |
| 926 | if (newTexCoordOffsets[0] > 0) { |
| 927 | GLvoid* texCoordPtr = (int8_t*)posPtr + newTexCoordOffsets[0]; |
| 928 | if (oldTexCoordOffsets[0] <= 0) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 929 | GR_GL(EnableVertexAttribArray(GR_GL_TEX_ATTR_LOCATION)); |
| 930 | } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 931 | if (posChange || newTexCoordOffsets[0] != oldTexCoordOffsets[0]) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 932 | GR_GL(VertexAttribPointer(GR_GL_TEX_ATTR_LOCATION, 2, scalarType, |
| 933 | texCoordNorm, newStride, texCoordPtr)); |
| 934 | } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 935 | } else if (oldTexCoordOffsets[0] > 0) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 936 | GR_GL(DisableVertexAttribArray(GR_GL_TEX_ATTR_LOCATION)); |
| 937 | } |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 938 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 939 | if (newColorOffset > 0) { |
| 940 | GLvoid* colorPtr = (int8_t*)posPtr + newColorOffset; |
| 941 | if (oldColorOffset <= 0) { |
| 942 | GR_GL(EnableVertexAttribArray(GR_GL_COL_ATTR_LOCATION)); |
| 943 | } |
| 944 | if (posChange || newColorOffset != oldColorOffset) { |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 945 | GR_GL(VertexAttribPointer(GR_GL_COL_ATTR_LOCATION, 4, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 946 | GL_UNSIGNED_BYTE, |
| 947 | true, newStride, colorPtr)); |
| 948 | } |
| 949 | } else if (oldColorOffset > 0) { |
| 950 | GR_GL(DisableVertexAttribArray(GR_GL_COL_ATTR_LOCATION)); |
| 951 | } |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 952 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 953 | fHWGeometryState.fVertexLayout = fGeometrySrc.fVertexLayout; |
| 954 | } |
| 955 | #endif |