1 /* 2 * Copyright 2018 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 GrMtlUniformHandler_DEFINED 9 #define GrMtlUniformHandler_DEFINED 10 11 #include "src/base/SkTBlockList.h" 12 #include "src/gpu/ganesh/GrShaderVar.h" 13 #include "src/gpu/ganesh/glsl/GrGLSLUniformHandler.h" 14 15 #include <vector> 16 17 // TODO: this class is basically copy and pasted from GrVkUniformHandler so that we can have 18 // some shaders working. The SkSL Metal code generator was written to work with GLSL generated for 19 // the Ganesh Vulkan backend, so it should all work. There might be better ways to do things in 20 // Metal and/or some Vulkan GLSLisms left in. 21 class GrMtlUniformHandler : public GrGLSLUniformHandler { 22 public: 23 static const int kUniformsPerBlock = 8; 24 25 enum { 26 kUniformBinding = 0, 27 kLastUniformBinding = kUniformBinding, 28 }; 29 static constexpr int kUniformBindingCount = kLastUniformBinding + 1; 30 31 // fUBOffset is only valid if the SkSLType of the fVariable is not a sampler 32 struct MtlUniformInfo : public UniformInfo { 33 uint32_t fUBOffset; 34 }; 35 typedef SkTBlockList<MtlUniformInfo> UniformInfoArray; 36 getUniformVariable(UniformHandle u)37 const GrShaderVar& getUniformVariable(UniformHandle u) const override { 38 return fUniforms.item(u.toIndex()).fVariable; 39 } 40 getUniformCStr(UniformHandle u)41 const char* getUniformCStr(UniformHandle u) const override { 42 return this->getUniformVariable(u).c_str(); 43 } 44 numUniforms()45 int numUniforms() const override { 46 return fUniforms.count(); 47 } 48 uniform(int idx)49 UniformInfo& uniform(int idx) override { 50 return fUniforms.item(idx); 51 } uniform(int idx)52 const UniformInfo& uniform(int idx) const override { 53 return fUniforms.item(idx); 54 } 55 56 private: GrMtlUniformHandler(GrGLSLProgramBuilder * program)57 explicit GrMtlUniformHandler(GrGLSLProgramBuilder* program) 58 : INHERITED(program) 59 , fUniforms(kUniformsPerBlock) 60 , fSamplers(kUniformsPerBlock) 61 , fCurrentUBOOffset(0) 62 , fCurrentUBOMaxAlignment(0x0) { 63 } 64 65 UniformHandle internalAddUniformArray(const GrProcessor* owner, 66 uint32_t visibility, 67 SkSLType type, 68 const char* name, 69 bool mangleName, 70 int arrayCount, 71 const char** outName) override; 72 73 SamplerHandle addSampler(const GrBackendFormat&, 74 GrSamplerState, 75 const skgpu::Swizzle&, 76 const char* name, 77 const GrShaderCaps*) override; 78 numSamplers()79 int numSamplers() const { return fSamplers.count(); } samplerVariable(SamplerHandle handle)80 const char* samplerVariable(SamplerHandle handle) const override { 81 return fSamplers.item(handle.toIndex()).fVariable.c_str(); 82 } samplerSwizzle(SamplerHandle handle)83 skgpu::Swizzle samplerSwizzle(SamplerHandle handle) const override { 84 return fSamplerSwizzles[handle.toIndex()]; 85 } samplerVisibility(SamplerHandle handle)86 uint32_t samplerVisibility(SamplerHandle handle) const { 87 return fSamplers.item(handle.toIndex()).fVisibility; 88 } 89 90 void appendUniformDecls(GrShaderFlags, SkString*) const override; 91 getUniformInfo(UniformHandle u)92 const UniformInfo& getUniformInfo(UniformHandle u) const { 93 return fUniforms.item(u.toIndex()); 94 } 95 96 UniformInfoArray fUniforms; 97 UniformInfoArray fSamplers; 98 SkTArray<skgpu::Swizzle> fSamplerSwizzles; 99 100 uint32_t fCurrentUBOOffset; 101 uint32_t fCurrentUBOMaxAlignment; 102 103 friend class GrMtlPipelineStateBuilder; 104 105 using INHERITED = GrGLSLUniformHandler; 106 }; 107 108 #endif 109