1 /* 2 * Copyright 2012 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #include "SkMatrix.h" 9 #include "gl/GrGLProgramDataManager.h" 10 #include "gl/GrGLGpu.h" 11 #include "glsl/GrGLSLUniformHandler.h" 12 13 #define ASSERT_ARRAY_UPLOAD_IN_BOUNDS(UNI, COUNT) \ 14 SkASSERT((COUNT) <= (UNI).fArrayCount || \ 15 (1 == (COUNT) && GrShaderVar::kNonArray == (UNI).fArrayCount)) 16 17 GrGLProgramDataManager::GrGLProgramDataManager(GrGLGpu* gpu, GrGLuint programID, 18 const UniformInfoArray& uniforms, 19 const VaryingInfoArray& pathProcVaryings) 20 : fGpu(gpu) 21 , fProgramID(programID) { 22 int count = uniforms.count(); 23 fUniforms.push_back_n(count); 24 for (int i = 0; i < count; i++) { 25 Uniform& uniform = fUniforms[i]; 26 const UniformInfo& builderUniform = uniforms[i]; 27 SkASSERT(GrShaderVar::kNonArray == builderUniform.fVariable.getArrayCount() || 28 builderUniform.fVariable.getArrayCount() > 0); 29 SkDEBUGCODE( 30 uniform.fArrayCount = builderUniform.fVariable.getArrayCount(); 31 uniform.fType = builderUniform.fVariable.getType(); 32 ) 33 uniform.fLocation = builderUniform.fLocation; 34 } 35 36 // NVPR programs have separable varyings 37 count = pathProcVaryings.count(); 38 fPathProcVaryings.push_back_n(count); 39 for (int i = 0; i < count; i++) { 40 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport()); 41 PathProcVarying& pathProcVarying = fPathProcVaryings[i]; 42 const VaryingInfo& builderPathProcVarying = pathProcVaryings[i]; 43 SkASSERT(GrShaderVar::kNonArray == builderPathProcVarying.fVariable.getArrayCount() || 44 builderPathProcVarying.fVariable.getArrayCount() > 0); 45 SkDEBUGCODE( 46 pathProcVarying.fArrayCount = builderPathProcVarying.fVariable.getArrayCount(); 47 pathProcVarying.fType = builderPathProcVarying.fVariable.getType(); 48 ) 49 pathProcVarying.fLocation = builderPathProcVarying.fLocation; 50 } 51 } 52 53 void GrGLProgramDataManager::setSamplerUniforms(const UniformInfoArray& samplers, 54 int startUnit) const { 55 for (int i = 0; i < samplers.count(); ++i) { 56 const UniformInfo& sampler = samplers[i]; 57 SkASSERT(sampler.fVisibility); 58 if (kUnusedUniform != sampler.fLocation) { 59 GR_GL_CALL(fGpu->glInterface(), Uniform1i(sampler.fLocation, i + startUnit)); 60 } 61 } 62 } 63 64 void GrGLProgramDataManager::set1i(UniformHandle u, int32_t i) const { 65 const Uniform& uni = fUniforms[u.toIndex()]; 66 SkASSERT(uni.fType == kInt_GrSLType || uni.fType == kShort_GrSLType); 67 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount); 68 if (kUnusedUniform != uni.fLocation) { 69 GR_GL_CALL(fGpu->glInterface(), Uniform1i(uni.fLocation, i)); 70 } 71 } 72 73 void GrGLProgramDataManager::set1iv(UniformHandle u, 74 int arrayCount, 75 const int32_t v[]) const { 76 const Uniform& uni = fUniforms[u.toIndex()]; 77 SkASSERT(uni.fType == kInt_GrSLType || uni.fType == kShort_GrSLType); 78 SkASSERT(arrayCount > 0); 79 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount); 80 if (kUnusedUniform != uni.fLocation) { 81 GR_GL_CALL(fGpu->glInterface(), Uniform1iv(uni.fLocation, arrayCount, v)); 82 } 83 } 84 85 void GrGLProgramDataManager::set1f(UniformHandle u, float v0) const { 86 const Uniform& uni = fUniforms[u.toIndex()]; 87 SkASSERT(uni.fType == kFloat_GrSLType || uni.fType == kHalf_GrSLType); 88 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount); 89 if (kUnusedUniform != uni.fLocation) { 90 GR_GL_CALL(fGpu->glInterface(), Uniform1f(uni.fLocation, v0)); 91 } 92 } 93 94 void GrGLProgramDataManager::set1fv(UniformHandle u, 95 int arrayCount, 96 const float v[]) const { 97 const Uniform& uni = fUniforms[u.toIndex()]; 98 SkASSERT(uni.fType == kFloat_GrSLType || uni.fType == kHalf_GrSLType); 99 SkASSERT(arrayCount > 0); 100 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount); 101 // This assert fires in some instances of the two-pt gradient for its VSParams. 102 // Once the uniform manager is responsible for inserting the duplicate uniform 103 // arrays in VS and FS driver bug workaround, this can be enabled. 104 // this->printUni(uni); 105 if (kUnusedUniform != uni.fLocation) { 106 GR_GL_CALL(fGpu->glInterface(), Uniform1fv(uni.fLocation, arrayCount, v)); 107 } 108 } 109 110 void GrGLProgramDataManager::set2i(UniformHandle u, int32_t i0, int32_t i1) const { 111 const Uniform& uni = fUniforms[u.toIndex()]; 112 SkASSERT(uni.fType == kInt2_GrSLType || uni.fType == kShort2_GrSLType); 113 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount); 114 if (kUnusedUniform != uni.fLocation) { 115 GR_GL_CALL(fGpu->glInterface(), Uniform2i(uni.fLocation, i0, i1)); 116 } 117 } 118 119 void GrGLProgramDataManager::set2iv(UniformHandle u, 120 int arrayCount, 121 const int32_t v[]) const { 122 const Uniform& uni = fUniforms[u.toIndex()]; 123 SkASSERT(uni.fType == kInt2_GrSLType || uni.fType == kShort2_GrSLType); 124 SkASSERT(arrayCount > 0); 125 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount); 126 if (kUnusedUniform != uni.fLocation) { 127 GR_GL_CALL(fGpu->glInterface(), Uniform2iv(uni.fLocation, arrayCount, v)); 128 } 129 } 130 131 void GrGLProgramDataManager::set2f(UniformHandle u, float v0, float v1) const { 132 const Uniform& uni = fUniforms[u.toIndex()]; 133 SkASSERT(uni.fType == kFloat2_GrSLType || uni.fType == kHalf2_GrSLType); 134 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount); 135 if (kUnusedUniform != uni.fLocation) { 136 GR_GL_CALL(fGpu->glInterface(), Uniform2f(uni.fLocation, v0, v1)); 137 } 138 } 139 140 void GrGLProgramDataManager::set2fv(UniformHandle u, 141 int arrayCount, 142 const float v[]) const { 143 const Uniform& uni = fUniforms[u.toIndex()]; 144 SkASSERT(uni.fType == kFloat2_GrSLType || uni.fType == kHalf2_GrSLType); 145 SkASSERT(arrayCount > 0); 146 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount); 147 if (kUnusedUniform != uni.fLocation) { 148 GR_GL_CALL(fGpu->glInterface(), Uniform2fv(uni.fLocation, arrayCount, v)); 149 } 150 } 151 152 void GrGLProgramDataManager::set3i(UniformHandle u, int32_t i0, int32_t i1, int32_t i2) const { 153 const Uniform& uni = fUniforms[u.toIndex()]; 154 SkASSERT(uni.fType == kInt3_GrSLType || uni.fType == kShort3_GrSLType); 155 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount); 156 if (kUnusedUniform != uni.fLocation) { 157 GR_GL_CALL(fGpu->glInterface(), Uniform3i(uni.fLocation, i0, i1, i2)); 158 } 159 } 160 161 void GrGLProgramDataManager::set3iv(UniformHandle u, 162 int arrayCount, 163 const int32_t v[]) const { 164 const Uniform& uni = fUniforms[u.toIndex()]; 165 SkASSERT(uni.fType == kInt3_GrSLType || uni.fType == kShort3_GrSLType); 166 SkASSERT(arrayCount > 0); 167 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount); 168 if (kUnusedUniform != uni.fLocation) { 169 GR_GL_CALL(fGpu->glInterface(), Uniform3iv(uni.fLocation, arrayCount, v)); 170 } 171 } 172 173 void GrGLProgramDataManager::set3f(UniformHandle u, float v0, float v1, float v2) const { 174 const Uniform& uni = fUniforms[u.toIndex()]; 175 SkASSERT(uni.fType == kFloat3_GrSLType || uni.fType == kHalf3_GrSLType); 176 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount); 177 if (kUnusedUniform != uni.fLocation) { 178 GR_GL_CALL(fGpu->glInterface(), Uniform3f(uni.fLocation, v0, v1, v2)); 179 } 180 } 181 182 void GrGLProgramDataManager::set3fv(UniformHandle u, 183 int arrayCount, 184 const float v[]) const { 185 const Uniform& uni = fUniforms[u.toIndex()]; 186 SkASSERT(uni.fType == kFloat3_GrSLType || uni.fType == kHalf3_GrSLType); 187 SkASSERT(arrayCount > 0); 188 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount); 189 if (kUnusedUniform != uni.fLocation) { 190 GR_GL_CALL(fGpu->glInterface(), Uniform3fv(uni.fLocation, arrayCount, v)); 191 } 192 } 193 194 void GrGLProgramDataManager::set4i(UniformHandle u, 195 int32_t i0, 196 int32_t i1, 197 int32_t i2, 198 int32_t i3) const { 199 const Uniform& uni = fUniforms[u.toIndex()]; 200 SkASSERT(uni.fType == kInt4_GrSLType || uni.fType == kShort4_GrSLType); 201 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount); 202 if (kUnusedUniform != uni.fLocation) { 203 GR_GL_CALL(fGpu->glInterface(), Uniform4i(uni.fLocation, i0, i1, i2, i3)); 204 } 205 } 206 207 void GrGLProgramDataManager::set4iv(UniformHandle u, 208 int arrayCount, 209 const int32_t v[]) const { 210 const Uniform& uni = fUniforms[u.toIndex()]; 211 SkASSERT(uni.fType == kInt4_GrSLType || uni.fType == kShort4_GrSLType); 212 SkASSERT(arrayCount > 0); 213 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount); 214 if (kUnusedUniform != uni.fLocation) { 215 GR_GL_CALL(fGpu->glInterface(), Uniform4iv(uni.fLocation, arrayCount, v)); 216 } 217 } 218 219 void GrGLProgramDataManager::set4f(UniformHandle u, 220 float v0, 221 float v1, 222 float v2, 223 float v3) const { 224 const Uniform& uni = fUniforms[u.toIndex()]; 225 SkASSERT(uni.fType == kFloat4_GrSLType || uni.fType == kHalf4_GrSLType); 226 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount); 227 if (kUnusedUniform != uni.fLocation) { 228 GR_GL_CALL(fGpu->glInterface(), Uniform4f(uni.fLocation, v0, v1, v2, v3)); 229 } 230 } 231 232 void GrGLProgramDataManager::set4fv(UniformHandle u, 233 int arrayCount, 234 const float v[]) const { 235 const Uniform& uni = fUniforms[u.toIndex()]; 236 SkASSERT(uni.fType == kFloat4_GrSLType || uni.fType == kHalf4_GrSLType); 237 SkASSERT(arrayCount > 0); 238 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount); 239 if (kUnusedUniform != uni.fLocation) { 240 GR_GL_CALL(fGpu->glInterface(), Uniform4fv(uni.fLocation, arrayCount, v)); 241 } 242 } 243 244 void GrGLProgramDataManager::setMatrix2f(UniformHandle u, const float matrix[]) const { 245 this->setMatrices<2>(u, 1, matrix); 246 } 247 248 void GrGLProgramDataManager::setMatrix3f(UniformHandle u, const float matrix[]) const { 249 this->setMatrices<3>(u, 1, matrix); 250 } 251 252 void GrGLProgramDataManager::setMatrix4f(UniformHandle u, const float matrix[]) const { 253 this->setMatrices<4>(u, 1, matrix); 254 } 255 256 void GrGLProgramDataManager::setMatrix2fv(UniformHandle u, int arrayCount, const float m[]) const { 257 this->setMatrices<2>(u, arrayCount, m); 258 } 259 260 void GrGLProgramDataManager::setMatrix3fv(UniformHandle u, int arrayCount, const float m[]) const { 261 this->setMatrices<3>(u, arrayCount, m); 262 } 263 264 void GrGLProgramDataManager::setMatrix4fv(UniformHandle u, int arrayCount, const float m[]) const { 265 this->setMatrices<4>(u, arrayCount, m); 266 } 267 268 template<int N> struct set_uniform_matrix; 269 270 template<int N> inline void GrGLProgramDataManager::setMatrices(UniformHandle u, 271 int arrayCount, 272 const float matrices[]) const { 273 const Uniform& uni = fUniforms[u.toIndex()]; 274 SkASSERT(uni.fType == kFloat2x2_GrSLType + (N - 2) || 275 uni.fType == kHalf2x2_GrSLType + (N - 2)); 276 SkASSERT(arrayCount > 0); 277 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount); 278 if (kUnusedUniform != uni.fLocation) { 279 set_uniform_matrix<N>::set(fGpu->glInterface(), uni.fLocation, arrayCount, matrices); 280 } 281 } 282 283 template<> struct set_uniform_matrix<2> { 284 inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) { 285 GR_GL_CALL(gli, UniformMatrix2fv(loc, cnt, false, m)); 286 } 287 }; 288 289 template<> struct set_uniform_matrix<3> { 290 inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) { 291 GR_GL_CALL(gli, UniformMatrix3fv(loc, cnt, false, m)); 292 } 293 }; 294 295 template<> struct set_uniform_matrix<4> { 296 inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) { 297 GR_GL_CALL(gli, UniformMatrix4fv(loc, cnt, false, m)); 298 } 299 }; 300 301 void GrGLProgramDataManager::setPathFragmentInputTransform(VaryingHandle u, 302 int components, 303 const SkMatrix& matrix) const { 304 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport()); 305 const PathProcVarying& fragmentInput = fPathProcVaryings[u.toIndex()]; 306 307 SkASSERT((components == 2 && (fragmentInput.fType == kFloat2_GrSLType || 308 fragmentInput.fType == kHalf2_GrSLType)) || 309 (components == 3 && (fragmentInput.fType == kFloat3_GrSLType || 310 fragmentInput.fType == kHalf3_GrSLType))); 311 312 fGpu->glPathRendering()->setProgramPathFragmentInputTransform(fProgramID, 313 fragmentInput.fLocation, 314 GR_GL_OBJECT_LINEAR, 315 components, 316 matrix); 317 } 318