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/Swizzle.h" 12 #include "src/gpu/ganesh/GrShaderVar.h" 13 #include "src/gpu/ganesh/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 GrGLSLShaderBuilder; 20 class GrProcessor; 21 class GrSamplerState; 22 class GrSurfaceProxy; 23 24 // Handles for program uniforms (other than per-effect uniforms) 25 struct GrGLSLBuiltinUniformHandles { 26 GrGLSLProgramDataManager::UniformHandle fRTAdjustmentUni; 27 // Render target flip uniform (used for dFdy, sk_Clockwise, and sk_FragCoord) 28 GrGLSLProgramDataManager::UniformHandle fRTFlipUni; 29 // Destination texture origin and scale, used when dest-texture readback is enabled. 30 GrGLSLProgramDataManager::UniformHandle fDstTextureCoordsUni; 31 }; 32 33 class GrGLSLUniformHandler { 34 public: 35 struct UniformInfo { 36 GrShaderVar fVariable; 37 uint32_t fVisibility; 38 const GrProcessor* fOwner; 39 SkString fRawName; 40 }; 41 ~GrGLSLUniformHandler()42 virtual ~GrGLSLUniformHandler() {} 43 44 using UniformHandle = GrGLSLProgramDataManager::UniformHandle; 45 46 GR_DEFINE_RESOURCE_HANDLE_CLASS(SamplerHandle); 47 48 /** Add a uniform variable to the current program, that has visibility in one or more shaders. 49 visibility is a bitfield of GrShaderFlag values indicating from which shaders the uniform 50 should be accessible. At least one bit must be set. Geometry shader uniforms are not 51 supported at this time. The actual uniform name will be mangled. If outName is not nullptr 52 then it will refer to the final uniform name after return. Use the addUniformArray variant 53 to add an array of uniforms. */ 54 UniformHandle addUniform(const GrProcessor* owner, 55 uint32_t visibility, 56 SkSLType type, 57 const char* name, 58 const char** outName = nullptr) { 59 SkASSERT(!SkSLTypeIsCombinedSamplerType(type)); 60 return this->addUniformArray(owner, visibility, type, name, 0, outName); 61 } 62 63 UniformHandle addUniformArray(const GrProcessor* owner, 64 uint32_t visibility, 65 SkSLType type, 66 const char* name, 67 int arrayCount, 68 const char** outName = nullptr) { 69 SkASSERT(!SkSLTypeIsCombinedSamplerType(type)); 70 bool mangle = strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX)); 71 return this->internalAddUniformArray(owner, visibility, type, name, mangle, arrayCount, 72 outName); 73 } 74 75 virtual const GrShaderVar& getUniformVariable(UniformHandle u) const = 0; 76 77 /** 78 * Shortcut for getUniformVariable(u).c_str() 79 */ 80 virtual const char* getUniformCStr(UniformHandle u) const = 0; 81 82 virtual int numUniforms() const = 0; 83 84 virtual UniformInfo& uniform(int idx) = 0; 85 virtual const UniformInfo& uniform(int idx) const = 0; 86 87 // Looks up a uniform that was added by 'owner' with the given 'rawName' (pre-mangling). 88 // If there is no such uniform, a variable with type kVoid is returned. 89 GrShaderVar getUniformMapping(const GrProcessor& owner, SkString rawName) const; 90 91 // Like getUniformMapping(), but if the uniform is found it also marks it as accessible in 92 // the vertex shader. 93 GrShaderVar liftUniformToVertexShader(const GrProcessor& owner, SkString rawName); 94 95 protected: GrGLSLUniformHandler(GrGLSLProgramBuilder * program)96 explicit GrGLSLUniformHandler(GrGLSLProgramBuilder* program) : fProgramBuilder(program) {} 97 98 // This is not owned by the class 99 GrGLSLProgramBuilder* fProgramBuilder; 100 101 private: 102 virtual const char * samplerVariable(SamplerHandle) const = 0; 103 virtual skgpu::Swizzle samplerSwizzle(SamplerHandle) const = 0; 104 inputSamplerVariable(SamplerHandle)105 virtual const char* inputSamplerVariable(SamplerHandle) const { 106 SkDEBUGFAIL("Trying to get input sampler from unsupported backend"); 107 return nullptr; 108 } inputSamplerSwizzle(SamplerHandle)109 virtual skgpu::Swizzle inputSamplerSwizzle(SamplerHandle) const { 110 SkDEBUGFAIL("Trying to get input sampler swizzle from unsupported backend"); 111 return {}; 112 } 113 114 virtual SamplerHandle addSampler(const GrBackendFormat&, GrSamplerState, const skgpu::Swizzle&, 115 const char* name, const GrShaderCaps*) = 0; 116 addInputSampler(const skgpu::Swizzle & swizzle,const char * name)117 virtual SamplerHandle addInputSampler(const skgpu::Swizzle& swizzle, const char* name) { 118 SkDEBUGFAIL("Trying to add input sampler to unsupported backend"); 119 return {}; 120 } 121 122 virtual UniformHandle internalAddUniformArray(const GrProcessor* owner, 123 uint32_t visibility, 124 SkSLType type, 125 const char* name, 126 bool mangleName, 127 int arrayCount, 128 const char** outName) = 0; 129 130 virtual void appendUniformDecls(GrShaderFlags visibility, SkString*) const = 0; 131 132 friend class GrGLSLProgramBuilder; 133 }; 134 135 #endif 136