Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 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 "GrAllocator.h" |
| 12 | #include "GrShaderVar.h" |
| 13 | #include "glsl/GrGLSLUniformHandler.h" |
| 14 | |
| 15 | // TODO: this class is basically copy and pasted from GrVkUniformHandler so that we can have |
| 16 | // some shaders working. The SkSL Metal code generator was written to work with GLSL generated for |
| 17 | // the Ganesh Vulkan backend, so it should all work. There might be better ways to do things in |
| 18 | // Metal and/or some Vulkan GLSLisms left in. |
| 19 | class GrMtlUniformHandler : public GrGLSLUniformHandler { |
| 20 | public: |
| 21 | static const int kUniformsPerBlock = 8; |
| 22 | |
| 23 | enum { |
| 24 | kGeometryBinding = 0, |
| 25 | kFragBinding = 1, |
| 26 | kLastUniformBinding = kFragBinding, |
| 27 | }; |
| 28 | |
| 29 | // fUBOffset is only valid if the GrSLType of the fVariable is not a sampler |
| 30 | struct UniformInfo { |
| 31 | GrShaderVar fVariable; |
| 32 | uint32_t fVisibility; |
| 33 | uint32_t fUBOffset; |
| 34 | }; |
| 35 | typedef GrTAllocator<UniformInfo> UniformInfoArray; |
| 36 | |
| 37 | const GrShaderVar& getUniformVariable(UniformHandle u) const override { |
| 38 | return fUniforms[u.toIndex()].fVariable; |
| 39 | } |
| 40 | |
| 41 | const char* getUniformCStr(UniformHandle u) const override { |
| 42 | return this->getUniformVariable(u).c_str(); |
| 43 | } |
| 44 | |
| 45 | private: |
| 46 | explicit GrMtlUniformHandler(GrGLSLProgramBuilder* program) |
| 47 | : INHERITED(program) |
| 48 | , fUniforms(kUniformsPerBlock) |
| 49 | , fSamplers(kUniformsPerBlock) |
| 50 | , fCurrentGeometryUBOOffset(0) |
Jim Van Verth | 3d48299 | 2019-02-07 10:48:05 -0500 | [diff] [blame] | 51 | , fCurrentGeometryUBOMaxAlignment(0x0) |
| 52 | , fCurrentFragmentUBOOffset(0) |
| 53 | , fCurrentFragmentUBOMaxAlignment(0x0) { |
Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | UniformHandle internalAddUniformArray(uint32_t visibility, |
| 57 | GrSLType type, |
| 58 | GrSLPrecision precision, |
| 59 | const char* name, |
| 60 | bool mangleName, |
| 61 | int arrayCount, |
| 62 | const char** outName) override; |
| 63 | |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 64 | SamplerHandle addSampler(const GrTexture*, |
| 65 | const GrSamplerState&, |
| 66 | const char* name, |
| 67 | const GrShaderCaps*) override; |
Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 68 | |
| 69 | int numSamplers() const { return fSamplers.count(); } |
| 70 | const GrShaderVar& samplerVariable(SamplerHandle handle) const override { |
| 71 | return fSamplers[handle.toIndex()].fVariable; |
| 72 | } |
| 73 | GrSwizzle samplerSwizzle(SamplerHandle handle) const override { |
| 74 | return fSamplerSwizzles[handle.toIndex()]; |
| 75 | } |
| 76 | uint32_t samplerVisibility(SamplerHandle handle) const { |
| 77 | return fSamplers[handle.toIndex()].fVisibility; |
| 78 | } |
| 79 | |
| 80 | void appendUniformDecls(GrShaderFlags, SkString*) const override; |
| 81 | |
| 82 | bool hasGeometryUniforms() const { return fCurrentGeometryUBOOffset > 0; } |
| 83 | bool hasFragmentUniforms() const { return fCurrentFragmentUBOOffset > 0; } |
| 84 | |
Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 85 | const UniformInfo& getUniformInfo(UniformHandle u) const { |
| 86 | return fUniforms[u.toIndex()]; |
| 87 | } |
| 88 | |
Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 89 | UniformInfoArray fUniforms; |
| 90 | UniformInfoArray fSamplers; |
| 91 | SkTArray<GrSwizzle> fSamplerSwizzles; |
| 92 | |
| 93 | uint32_t fCurrentGeometryUBOOffset; |
Jim Van Verth | 3d48299 | 2019-02-07 10:48:05 -0500 | [diff] [blame] | 94 | uint32_t fCurrentGeometryUBOMaxAlignment; |
Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 95 | uint32_t fCurrentFragmentUBOOffset; |
Jim Van Verth | 3d48299 | 2019-02-07 10:48:05 -0500 | [diff] [blame] | 96 | uint32_t fCurrentFragmentUBOMaxAlignment; |
Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 97 | |
| 98 | friend class GrMtlPipelineStateBuilder; |
| 99 | |
| 100 | typedef GrGLSLUniformHandler INHERITED; |
| 101 | }; |
| 102 | |
| 103 | #endif |