blob: 6e60cb0d961b7525f013a2fd8111f9b44b7c055c [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)
Jim Van Verth3d482992019-02-07 10:48:05 -050051 , fCurrentGeometryUBOMaxAlignment(0x0)
52 , fCurrentFragmentUBOOffset(0)
53 , fCurrentFragmentUBOMaxAlignment(0x0) {
Timothy Liang057c3902018-08-08 10:48:45 -040054 }
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 Daniel9a51a862018-11-30 10:18:14 -050064 SamplerHandle addSampler(const GrTexture*,
65 const GrSamplerState&,
66 const char* name,
67 const GrShaderCaps*) override;
Timothy Liang057c3902018-08-08 10:48:45 -040068
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 Liang057c3902018-08-08 10:48:45 -040085 const UniformInfo& getUniformInfo(UniformHandle u) const {
86 return fUniforms[u.toIndex()];
87 }
88
Timothy Liang057c3902018-08-08 10:48:45 -040089 UniformInfoArray fUniforms;
90 UniformInfoArray fSamplers;
91 SkTArray<GrSwizzle> fSamplerSwizzles;
92
93 uint32_t fCurrentGeometryUBOOffset;
Jim Van Verth3d482992019-02-07 10:48:05 -050094 uint32_t fCurrentGeometryUBOMaxAlignment;
Timothy Liang057c3902018-08-08 10:48:45 -040095 uint32_t fCurrentFragmentUBOOffset;
Jim Van Verth3d482992019-02-07 10:48:05 -050096 uint32_t fCurrentFragmentUBOMaxAlignment;
Timothy Liang057c3902018-08-08 10:48:45 -040097
98 friend class GrMtlPipelineStateBuilder;
99
100 typedef GrGLSLUniformHandler INHERITED;
101};
102
103#endif