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 class GrSamplerState; 20 class GrTexture; 21 22 // Handles for program uniforms (other than per-effect uniforms) 23 struct GrGLSLBuiltinUniformHandles { 24 GrGLSLProgramDataManager::UniformHandle fRTAdjustmentUni; 25 // Render target width, used to implement sk_Width 26 GrGLSLProgramDataManager::UniformHandle fRTWidthUni; 27 // Render target height, used to implement sk_Height and to calculate sk_FragCoord when 28 // origin_upper_left is not supported. 29 GrGLSLProgramDataManager::UniformHandle fRTHeightUni; 30 }; 31 32 class GrGLSLUniformHandler { 33 public: 34 virtual ~GrGLSLUniformHandler() {} 35 36 using UniformHandle = GrGLSLProgramDataManager::UniformHandle; 37 38 GR_DEFINE_RESOURCE_HANDLE_CLASS(SamplerHandle); 39 40 /** Add a uniform variable to the current program, that has visibility in one or more shaders. 41 visibility is a bitfield of GrShaderFlag values indicating from which shaders the uniform 42 should be accessible. At least one bit must be set. Geometry shader uniforms are not 43 supported at this time. The actual uniform name will be mangled. If outName is not nullptr 44 then it will refer to the final uniform name after return. Use the addUniformArray variant 45 to add an array of uniforms. */ 46 UniformHandle addUniform(uint32_t visibility, 47 GrSLType type, 48 GrSLPrecision precision, 49 const char* name, 50 const char** outName = nullptr) { 51 SkASSERT(!GrSLTypeIsCombinedSamplerType(type)); 52 return this->addUniformArray(visibility, type, precision, name, 0, outName); 53 } 54 55 UniformHandle addUniform(uint32_t visibility, 56 GrSLType type, 57 const char* name, 58 const char** outName = nullptr) { 59 return this->addUniform(visibility, type, kDefault_GrSLPrecision, name, outName); 60 } 61 62 UniformHandle addUniformArray(uint32_t visibility, 63 GrSLType type, 64 GrSLPrecision precision, 65 const char* name, 66 int arrayCount, 67 const char** outName = nullptr) { 68 SkASSERT(!GrSLTypeIsCombinedSamplerType(type)); 69 bool mangle = strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX)); 70 return this->internalAddUniformArray(visibility, type, precision, name, mangle, arrayCount, 71 outName); 72 } 73 74 UniformHandle addUniformArray(uint32_t visibility, 75 GrSLType type, 76 const char* name, 77 int arrayCount, 78 const char** outName = nullptr) { 79 SkASSERT(!GrSLTypeIsCombinedSamplerType(type)); 80 bool mangle = strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX)); 81 return this->internalAddUniformArray(visibility, type, kDefault_GrSLPrecision, name, mangle, 82 arrayCount, outName); 83 } 84 85 virtual const GrShaderVar& getUniformVariable(UniformHandle u) const = 0; 86 87 /** 88 * Shortcut for getUniformVariable(u).c_str() 89 */ 90 virtual const char* getUniformCStr(UniformHandle u) const = 0; 91 92 protected: 93 explicit GrGLSLUniformHandler(GrGLSLProgramBuilder* program) : fProgramBuilder(program) {} 94 95 // This is not owned by the class 96 GrGLSLProgramBuilder* fProgramBuilder; 97 98 private: 99 virtual const GrShaderVar& samplerVariable(SamplerHandle) const = 0; 100 virtual GrSwizzle samplerSwizzle(SamplerHandle) const = 0; 101 102 virtual SamplerHandle addSampler(const GrTexture*, const GrSamplerState&, const char* name, 103 const GrShaderCaps*) = 0; 104 105 virtual UniformHandle internalAddUniformArray(uint32_t visibility, 106 GrSLType type, 107 GrSLPrecision precision, 108 const char* name, 109 bool mangleName, 110 int arrayCount, 111 const char** outName) = 0; 112 113 virtual void appendUniformDecls(GrShaderFlags visibility, SkString*) const = 0; 114 115 friend class GrGLSLProgramBuilder; 116 }; 117 118 #endif 119