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 "src/gpu/GrShaderVar.h" 12 #include "src/gpu/GrSwizzle.h" 13 #include "src/gpu/glsl/GrGLSLProgramDataManager.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: ~GrGLSLUniformHandler()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 const char* name, 49 const char** outName = nullptr) { 50 SkASSERT(!GrSLTypeIsCombinedSamplerType(type)); 51 return this->addUniformArray(visibility, type, name, 0, outName); 52 } 53 54 UniformHandle addUniformArray(uint32_t visibility, 55 GrSLType type, 56 const char* name, 57 int arrayCount, 58 const char** outName = nullptr) { 59 SkASSERT(!GrSLTypeIsCombinedSamplerType(type)); 60 bool mangle = strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX)); 61 return this->internalAddUniformArray(visibility, type, name, mangle, arrayCount, outName); 62 } 63 64 virtual const GrShaderVar& getUniformVariable(UniformHandle u) const = 0; 65 66 /** 67 * Shortcut for getUniformVariable(u).c_str() 68 */ 69 virtual const char* getUniformCStr(UniformHandle u) const = 0; 70 71 protected: GrGLSLUniformHandler(GrGLSLProgramBuilder * program)72 explicit GrGLSLUniformHandler(GrGLSLProgramBuilder* program) : fProgramBuilder(program) {} 73 74 // This is not owned by the class 75 GrGLSLProgramBuilder* fProgramBuilder; 76 77 private: 78 virtual const char * samplerVariable(SamplerHandle) const = 0; 79 // Only called if GrShaderCaps(:textureSwizzleAppliedInShader() == true. 80 virtual GrSwizzle samplerSwizzle(SamplerHandle) const = 0; 81 82 virtual SamplerHandle addSampler(const GrTexture*, const GrSamplerState&, const GrSwizzle&, 83 const char* name, const GrShaderCaps*) = 0; 84 85 virtual UniformHandle internalAddUniformArray(uint32_t visibility, 86 GrSLType type, 87 const char* name, 88 bool mangleName, 89 int arrayCount, 90 const char** outName) = 0; 91 92 virtual void appendUniformDecls(GrShaderFlags visibility, SkString*) const = 0; 93 94 friend class GrGLSLProgramBuilder; 95 }; 96 97 #endif 98