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 int 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::set2f(UniformHandle u, float v0, float v1) const { 111 const Uniform& uni = fUniforms[u.toIndex()]; 112 SkASSERT(uni.fType == kFloat2_GrSLType || uni.fType == kHalf2_GrSLType); 113 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount); 114 if (kUnusedUniform != uni.fLocation) { 115 GR_GL_CALL(fGpu->glInterface(), Uniform2f(uni.fLocation, v0, v1)); 116 } 117 } 118 119 void GrGLProgramDataManager::set2fv(UniformHandle u, 120 int arrayCount, 121 const float v[]) const { 122 const Uniform& uni = fUniforms[u.toIndex()]; 123 SkASSERT(uni.fType == kFloat2_GrSLType || uni.fType == kHalf2_GrSLType); 124 SkASSERT(arrayCount > 0); 125 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount); 126 if (kUnusedUniform != uni.fLocation) { 127 GR_GL_CALL(fGpu->glInterface(), Uniform2fv(uni.fLocation, arrayCount, v)); 128 } 129 } 130 131 void GrGLProgramDataManager::set3f(UniformHandle u, float v0, float v1, float v2) const { 132 const Uniform& uni = fUniforms[u.toIndex()]; 133 SkASSERT(uni.fType == kFloat3_GrSLType || uni.fType == kHalf3_GrSLType); 134 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount); 135 if (kUnusedUniform != uni.fLocation) { 136 GR_GL_CALL(fGpu->glInterface(), Uniform3f(uni.fLocation, v0, v1, v2)); 137 } 138 } 139 140 void GrGLProgramDataManager::set3fv(UniformHandle u, 141 int arrayCount, 142 const float v[]) const { 143 const Uniform& uni = fUniforms[u.toIndex()]; 144 SkASSERT(uni.fType == kFloat3_GrSLType || uni.fType == kHalf3_GrSLType); 145 SkASSERT(arrayCount > 0); 146 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount); 147 if (kUnusedUniform != uni.fLocation) { 148 GR_GL_CALL(fGpu->glInterface(), Uniform3fv(uni.fLocation, arrayCount, v)); 149 } 150 } 151 152 void GrGLProgramDataManager::set4f(UniformHandle u, 153 float v0, 154 float v1, 155 float v2, 156 float v3) const { 157 const Uniform& uni = fUniforms[u.toIndex()]; 158 SkASSERT(uni.fType == kFloat4_GrSLType || uni.fType == kHalf4_GrSLType); 159 SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount); 160 if (kUnusedUniform != uni.fLocation) { 161 GR_GL_CALL(fGpu->glInterface(), Uniform4f(uni.fLocation, v0, v1, v2, v3)); 162 } 163 } 164 165 void GrGLProgramDataManager::set4fv(UniformHandle u, 166 int arrayCount, 167 const float v[]) const { 168 const Uniform& uni = fUniforms[u.toIndex()]; 169 SkASSERT(uni.fType == kFloat4_GrSLType || uni.fType == kHalf4_GrSLType); 170 SkASSERT(arrayCount > 0); 171 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount); 172 if (kUnusedUniform != uni.fLocation) { 173 GR_GL_CALL(fGpu->glInterface(), Uniform4fv(uni.fLocation, arrayCount, v)); 174 } 175 } 176 177 void GrGLProgramDataManager::setMatrix2f(UniformHandle u, const float matrix[]) const { 178 this->setMatrices<2>(u, 1, matrix); 179 } 180 181 void GrGLProgramDataManager::setMatrix3f(UniformHandle u, const float matrix[]) const { 182 this->setMatrices<3>(u, 1, matrix); 183 } 184 185 void GrGLProgramDataManager::setMatrix4f(UniformHandle u, const float matrix[]) const { 186 this->setMatrices<4>(u, 1, matrix); 187 } 188 189 void GrGLProgramDataManager::setMatrix2fv(UniformHandle u, int arrayCount, const float m[]) const { 190 this->setMatrices<2>(u, arrayCount, m); 191 } 192 193 void GrGLProgramDataManager::setMatrix3fv(UniformHandle u, int arrayCount, const float m[]) const { 194 this->setMatrices<3>(u, arrayCount, m); 195 } 196 197 void GrGLProgramDataManager::setMatrix4fv(UniformHandle u, int arrayCount, const float m[]) const { 198 this->setMatrices<4>(u, arrayCount, m); 199 } 200 201 template<int N> struct set_uniform_matrix; 202 203 template<int N> inline void GrGLProgramDataManager::setMatrices(UniformHandle u, 204 int arrayCount, 205 const float matrices[]) const { 206 const Uniform& uni = fUniforms[u.toIndex()]; 207 SkASSERT(uni.fType == kFloat2x2_GrSLType + (N - 2) || 208 uni.fType == kHalf2x2_GrSLType + (N - 2)); 209 SkASSERT(arrayCount > 0); 210 ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount); 211 if (kUnusedUniform != uni.fLocation) { 212 set_uniform_matrix<N>::set(fGpu->glInterface(), uni.fLocation, arrayCount, matrices); 213 } 214 } 215 216 template<> struct set_uniform_matrix<2> { 217 inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) { 218 GR_GL_CALL(gli, UniformMatrix2fv(loc, cnt, false, m)); 219 } 220 }; 221 222 template<> struct set_uniform_matrix<3> { 223 inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) { 224 GR_GL_CALL(gli, UniformMatrix3fv(loc, cnt, false, m)); 225 } 226 }; 227 228 template<> struct set_uniform_matrix<4> { 229 inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) { 230 GR_GL_CALL(gli, UniformMatrix4fv(loc, cnt, false, m)); 231 } 232 }; 233 234 void GrGLProgramDataManager::setPathFragmentInputTransform(VaryingHandle u, 235 int components, 236 const SkMatrix& matrix) const { 237 SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport()); 238 const PathProcVarying& fragmentInput = fPathProcVaryings[u.toIndex()]; 239 240 SkASSERT((components == 2 && (fragmentInput.fType == kFloat2_GrSLType || 241 fragmentInput.fType == kHalf2_GrSLType)) || 242 (components == 3 && (fragmentInput.fType == kFloat3_GrSLType || 243 fragmentInput.fType == kHalf3_GrSLType))); 244 245 fGpu->glPathRendering()->setProgramPathFragmentInputTransform(fProgramID, 246 fragmentInput.fLocation, 247 GR_GL_OBJECT_LINEAR, 248 components, 249 matrix); 250 } 251