Home | History | Annotate | Download | only in gl
      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::setSamplers(const UniformInfoArray& samplers) const {
     54     for (int i = 0; i < samplers.count(); ++i) {
     55         const UniformInfo& sampler = samplers[i];
     56         SkASSERT(sampler.fVisibility);
     57         if (kUnusedUniform != sampler.fLocation) {
     58             GR_GL_CALL(fGpu->glInterface(), Uniform1i(sampler.fLocation, i));
     59         }
     60     }
     61 }
     62 
     63 void GrGLProgramDataManager::setImageStorages(const UniformInfoArray& images) const {
     64     for (int i = 0; i < images.count(); ++i) {
     65         const UniformInfo& image = images[i];
     66         SkASSERT(image.fVisibility);
     67         if (kUnusedUniform != image.fLocation) {
     68             GR_GL_CALL(fGpu->glInterface(), Uniform1i(image.fLocation, i));
     69         }
     70     }
     71 }
     72 
     73 void GrGLProgramDataManager::set1i(UniformHandle u, int32_t i) const {
     74     const Uniform& uni = fUniforms[u.toIndex()];
     75     SkASSERT(uni.fType == kInt_GrSLType);
     76     SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
     77     if (kUnusedUniform != uni.fLocation) {
     78         GR_GL_CALL(fGpu->glInterface(), Uniform1i(uni.fLocation, i));
     79     }
     80 }
     81 
     82 void GrGLProgramDataManager::set1iv(UniformHandle u,
     83                                     int arrayCount,
     84                                     const int v[]) const {
     85     const Uniform& uni = fUniforms[u.toIndex()];
     86     SkASSERT(uni.fType == kInt_GrSLType);
     87     SkASSERT(arrayCount > 0);
     88     ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
     89     if (kUnusedUniform != uni.fLocation) {
     90         GR_GL_CALL(fGpu->glInterface(), Uniform1iv(uni.fLocation, arrayCount, v));
     91     }
     92 }
     93 
     94 void GrGLProgramDataManager::set1f(UniformHandle u, float v0) const {
     95     const Uniform& uni = fUniforms[u.toIndex()];
     96     SkASSERT(uni.fType == kFloat_GrSLType);
     97     SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
     98     if (kUnusedUniform != uni.fLocation) {
     99         GR_GL_CALL(fGpu->glInterface(), Uniform1f(uni.fLocation, v0));
    100     }
    101 }
    102 
    103 void GrGLProgramDataManager::set1fv(UniformHandle u,
    104                                     int arrayCount,
    105                                     const float v[]) const {
    106     const Uniform& uni = fUniforms[u.toIndex()];
    107     SkASSERT(uni.fType == kFloat_GrSLType);
    108     SkASSERT(arrayCount > 0);
    109     ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
    110     // This assert fires in some instances of the two-pt gradient for its VSParams.
    111     // Once the uniform manager is responsible for inserting the duplicate uniform
    112     // arrays in VS and FS driver bug workaround, this can be enabled.
    113     // this->printUni(uni);
    114     if (kUnusedUniform != uni.fLocation) {
    115         GR_GL_CALL(fGpu->glInterface(), Uniform1fv(uni.fLocation, arrayCount, v));
    116     }
    117 }
    118 
    119 void GrGLProgramDataManager::set2f(UniformHandle u, float v0, float v1) const {
    120     const Uniform& uni = fUniforms[u.toIndex()];
    121     SkASSERT(uni.fType == kVec2f_GrSLType);
    122     SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
    123     if (kUnusedUniform != uni.fLocation) {
    124         GR_GL_CALL(fGpu->glInterface(), Uniform2f(uni.fLocation, v0, v1));
    125     }
    126 }
    127 
    128 void GrGLProgramDataManager::set2fv(UniformHandle u,
    129                                     int arrayCount,
    130                                     const float v[]) const {
    131     const Uniform& uni = fUniforms[u.toIndex()];
    132     SkASSERT(uni.fType == kVec2f_GrSLType);
    133     SkASSERT(arrayCount > 0);
    134     ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
    135     if (kUnusedUniform != uni.fLocation) {
    136         GR_GL_CALL(fGpu->glInterface(), Uniform2fv(uni.fLocation, arrayCount, v));
    137     }
    138 }
    139 
    140 void GrGLProgramDataManager::set3f(UniformHandle u, float v0, float v1, float v2) const {
    141     const Uniform& uni = fUniforms[u.toIndex()];
    142     SkASSERT(uni.fType == kVec3f_GrSLType);
    143     SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
    144     if (kUnusedUniform != uni.fLocation) {
    145         GR_GL_CALL(fGpu->glInterface(), Uniform3f(uni.fLocation, v0, v1, v2));
    146     }
    147 }
    148 
    149 void GrGLProgramDataManager::set3fv(UniformHandle u,
    150                                     int arrayCount,
    151                                     const float v[]) const {
    152     const Uniform& uni = fUniforms[u.toIndex()];
    153     SkASSERT(uni.fType == kVec3f_GrSLType);
    154     SkASSERT(arrayCount > 0);
    155     ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
    156     if (kUnusedUniform != uni.fLocation) {
    157         GR_GL_CALL(fGpu->glInterface(), Uniform3fv(uni.fLocation, arrayCount, v));
    158     }
    159 }
    160 
    161 void GrGLProgramDataManager::set4f(UniformHandle u,
    162                                    float v0,
    163                                    float v1,
    164                                    float v2,
    165                                    float v3) const {
    166     const Uniform& uni = fUniforms[u.toIndex()];
    167     SkASSERT(uni.fType == kVec4f_GrSLType);
    168     SkASSERT(GrShaderVar::kNonArray == uni.fArrayCount);
    169     if (kUnusedUniform != uni.fLocation) {
    170         GR_GL_CALL(fGpu->glInterface(), Uniform4f(uni.fLocation, v0, v1, v2, v3));
    171     }
    172 }
    173 
    174 void GrGLProgramDataManager::set4fv(UniformHandle u,
    175                                     int arrayCount,
    176                                     const float v[]) const {
    177     const Uniform& uni = fUniforms[u.toIndex()];
    178     SkASSERT(uni.fType == kVec4f_GrSLType);
    179     SkASSERT(arrayCount > 0);
    180     ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
    181     if (kUnusedUniform != uni.fLocation) {
    182         GR_GL_CALL(fGpu->glInterface(), Uniform4fv(uni.fLocation, arrayCount, v));
    183     }
    184 }
    185 
    186 void GrGLProgramDataManager::setMatrix2f(UniformHandle u, const float matrix[]) const {
    187     this->setMatrices<2>(u, 1, matrix);
    188 }
    189 
    190 void GrGLProgramDataManager::setMatrix3f(UniformHandle u, const float matrix[]) const {
    191     this->setMatrices<3>(u, 1, matrix);
    192 }
    193 
    194 void GrGLProgramDataManager::setMatrix4f(UniformHandle u, const float matrix[]) const {
    195     this->setMatrices<4>(u, 1, matrix);
    196 }
    197 
    198 void GrGLProgramDataManager::setMatrix2fv(UniformHandle u, int arrayCount, const float m[]) const {
    199     this->setMatrices<2>(u, arrayCount, m);
    200 }
    201 
    202 void GrGLProgramDataManager::setMatrix3fv(UniformHandle u, int arrayCount, const float m[]) const {
    203     this->setMatrices<3>(u, arrayCount, m);
    204 }
    205 
    206 void GrGLProgramDataManager::setMatrix4fv(UniformHandle u, int arrayCount, const float m[]) const {
    207     this->setMatrices<4>(u, arrayCount, m);
    208 }
    209 
    210 template<int N> struct set_uniform_matrix;
    211 
    212 template<int N> inline void GrGLProgramDataManager::setMatrices(UniformHandle u,
    213                                                                 int arrayCount,
    214                                                                 const float matrices[]) const {
    215     const Uniform& uni = fUniforms[u.toIndex()];
    216     SkASSERT(uni.fType == kMat22f_GrSLType + (N - 2));
    217     SkASSERT(arrayCount > 0);
    218     ASSERT_ARRAY_UPLOAD_IN_BOUNDS(uni, arrayCount);
    219     if (kUnusedUniform != uni.fLocation) {
    220         set_uniform_matrix<N>::set(fGpu->glInterface(), uni.fLocation, arrayCount, matrices);
    221     }
    222 }
    223 
    224 template<> struct set_uniform_matrix<2> {
    225     inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) {
    226         GR_GL_CALL(gli, UniformMatrix2fv(loc, cnt, false, m));
    227     }
    228 };
    229 
    230 template<> struct set_uniform_matrix<3> {
    231     inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) {
    232         GR_GL_CALL(gli, UniformMatrix3fv(loc, cnt, false, m));
    233     }
    234 };
    235 
    236 template<> struct set_uniform_matrix<4> {
    237     inline static void set(const GrGLInterface* gli, const GrGLint loc, int cnt, const float m[]) {
    238         GR_GL_CALL(gli, UniformMatrix4fv(loc, cnt, false, m));
    239     }
    240 };
    241 
    242 void GrGLProgramDataManager::setPathFragmentInputTransform(VaryingHandle u,
    243                                                            int components,
    244                                                            const SkMatrix& matrix) const {
    245     SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
    246     const PathProcVarying& fragmentInput = fPathProcVaryings[u.toIndex()];
    247 
    248     SkASSERT((components == 2 && fragmentInput.fType == kVec2f_GrSLType) ||
    249               (components == 3 && fragmentInput.fType == kVec3f_GrSLType));
    250 
    251     fGpu->glPathRendering()->setProgramPathFragmentInputTransform(fProgramID,
    252                                                                   fragmentInput.fLocation,
    253                                                                   GR_GL_OBJECT_LINEAR,
    254                                                                   components,
    255                                                                   matrix);
    256 }
    257