blob: 4ee5a362b1164ea539a3bb9cf22d3e7fec0821ca [file] [log] [blame]
Timothy Liang057c3902018-08-08 10:48:45 -04001/*
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.
19class GrMtlUniformHandler : public GrGLSLUniformHandler {
20public:
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
45private:
46 explicit GrMtlUniformHandler(GrGLSLProgramBuilder* program)
47 : INHERITED(program)
48 , fUniforms(kUniformsPerBlock)
49 , fSamplers(kUniformsPerBlock)
50 , fCurrentGeometryUBOOffset(0)
51 , fCurrentFragmentUBOOffset(0) {
52 }
53
54 UniformHandle internalAddUniformArray(uint32_t visibility,
55 GrSLType type,
56 GrSLPrecision precision,
57 const char* name,
58 bool mangleName,
59 int arrayCount,
60 const char** outName) override;
61
62 SamplerHandle addSampler(uint32_t visibility,
63 GrSwizzle swizzle,
64 GrTextureType type,
65 GrSLPrecision precision,
66 const char* name) override;
67
68 int numSamplers() const { return fSamplers.count(); }
69 const GrShaderVar& samplerVariable(SamplerHandle handle) const override {
70 return fSamplers[handle.toIndex()].fVariable;
71 }
72 GrSwizzle samplerSwizzle(SamplerHandle handle) const override {
73 return fSamplerSwizzles[handle.toIndex()];
74 }
75 uint32_t samplerVisibility(SamplerHandle handle) const {
76 return fSamplers[handle.toIndex()].fVisibility;
77 }
78
79 void appendUniformDecls(GrShaderFlags, SkString*) const override;
80
81 bool hasGeometryUniforms() const { return fCurrentGeometryUBOOffset > 0; }
82 bool hasFragmentUniforms() const { return fCurrentFragmentUBOOffset > 0; }
83
84
85 const UniformInfo& getUniformInfo(UniformHandle u) const {
86 return fUniforms[u.toIndex()];
87 }
88
89
90 UniformInfoArray fUniforms;
91 UniformInfoArray fSamplers;
92 SkTArray<GrSwizzle> fSamplerSwizzles;
93
94 uint32_t fCurrentGeometryUBOOffset;
95 uint32_t fCurrentFragmentUBOOffset;
96
97 friend class GrMtlPipelineStateBuilder;
98
99 typedef GrGLSLUniformHandler INHERITED;
100};
101
102#endif