Home | History | Annotate | Download | only in glsl
      1 /*
      2  * Copyright 2015 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 #ifndef GrGLSLUniformHandler_DEFINED
      9 #define GrGLSLUniformHandler_DEFINED
     10 
     11 #include "GrGLSLProgramDataManager.h"
     12 #include "GrShaderVar.h"
     13 #include "GrSwizzle.h"
     14 
     15 // variable names beginning with this prefix will not be mangled
     16 #define GR_NO_MANGLE_PREFIX "sk_"
     17 
     18 class GrGLSLProgramBuilder;
     19 
     20 class GrGLSLUniformHandler {
     21 public:
     22     virtual ~GrGLSLUniformHandler() {}
     23 
     24     using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
     25     GR_DEFINE_RESOURCE_HANDLE_CLASS(SamplerHandle);
     26     GR_DEFINE_RESOURCE_HANDLE_CLASS(TexelBufferHandle);
     27 
     28     /** Add a uniform variable to the current program, that has visibility in one or more shaders.
     29         visibility is a bitfield of GrShaderFlag values indicating from which shaders the uniform
     30         should be accessible. At least one bit must be set. Geometry shader uniforms are not
     31         supported at this time. The actual uniform name will be mangled. If outName is not nullptr
     32         then it will refer to the final uniform name after return. Use the addUniformArray variant
     33         to add an array of uniforms. */
     34     UniformHandle addUniform(uint32_t visibility,
     35                              GrSLType type,
     36                              GrSLPrecision precision,
     37                              const char* name,
     38                              const char** outName = nullptr) {
     39         SkASSERT(!GrSLTypeIsCombinedSamplerType(type));
     40         return this->addUniformArray(visibility, type, precision, name, 0, outName);
     41     }
     42 
     43     UniformHandle addUniform(uint32_t visibility,
     44                              GrSLType type,
     45                              const char* name,
     46                              const char** outName = nullptr) {
     47         return this->addUniform(visibility, type, kDefault_GrSLPrecision, name, outName);
     48     }
     49 
     50     UniformHandle addUniformArray(uint32_t visibility,
     51                                   GrSLType type,
     52                                   GrSLPrecision precision,
     53                                   const char* name,
     54                                   int arrayCount,
     55                                   const char** outName = nullptr) {
     56         SkASSERT(!GrSLTypeIsCombinedSamplerType(type));
     57         bool mangle = strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX));
     58         return this->internalAddUniformArray(visibility, type, precision, name, mangle, arrayCount,
     59                                              outName);
     60     }
     61 
     62     UniformHandle addUniformArray(uint32_t visibility,
     63                                   GrSLType type,
     64                                   const char* name,
     65                                   int arrayCount,
     66                                   const char** outName = nullptr) {
     67         SkASSERT(!GrSLTypeIsCombinedSamplerType(type));
     68         bool mangle = strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX));
     69         return this->internalAddUniformArray(visibility, type, kDefault_GrSLPrecision, name, mangle,
     70                                              arrayCount, outName);
     71     }
     72 
     73     virtual const GrShaderVar& getUniformVariable(UniformHandle u) const = 0;
     74 
     75     /**
     76      * Shortcut for getUniformVariable(u).c_str()
     77      */
     78     virtual const char* getUniformCStr(UniformHandle u) const = 0;
     79 
     80 protected:
     81     explicit GrGLSLUniformHandler(GrGLSLProgramBuilder* program) : fProgramBuilder(program) {}
     82 
     83     // This is not owned by the class
     84     GrGLSLProgramBuilder* fProgramBuilder;
     85 
     86 private:
     87     virtual const GrShaderVar& samplerVariable(SamplerHandle) const = 0;
     88     virtual GrSwizzle samplerSwizzle(SamplerHandle) const = 0;
     89 
     90     virtual SamplerHandle addSampler(uint32_t visibility, GrSwizzle, GrSLType, GrSLPrecision,
     91                                      const char* name) = 0;
     92 
     93     virtual const GrShaderVar& texelBufferVariable(TexelBufferHandle) const = 0;
     94     virtual TexelBufferHandle addTexelBuffer(uint32_t visibility, GrSLPrecision,
     95                                              const char* name) = 0;
     96 
     97     virtual UniformHandle internalAddUniformArray(uint32_t visibility,
     98                                                   GrSLType type,
     99                                                   GrSLPrecision precision,
    100                                                   const char* name,
    101                                                   bool mangleName,
    102                                                   int arrayCount,
    103                                                   const char** outName) = 0;
    104 
    105     virtual void appendUniformDecls(GrShaderFlags visibility, SkString*) const = 0;
    106 
    107     friend class GrGLSLProgramBuilder;
    108 };
    109 
    110 #endif
    111