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