1/*
2 * Copyright 2015 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 GrGLSLUniformHandler_DEFINED
9#define GrGLSLUniformHandler_DEFINED
10
11#include "src/gpu/GrShaderVar.h"
12#include "src/gpu/GrSwizzle.h"
13#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
14
15// variable names beginning with this prefix will not be mangled
16#define GR_NO_MANGLE_PREFIX "sk_"
17
18class GrGLSLProgramBuilder;
19class GrSamplerState;
20class GrSurfaceProxy;
21
22// Handles for program uniforms (other than per-effect uniforms)
23struct GrGLSLBuiltinUniformHandles {
24 GrGLSLProgramDataManager::UniformHandle fRTAdjustmentUni;
25 // Render target width, used to implement sk_Width
26 GrGLSLProgramDataManager::UniformHandle fRTWidthUni;
27 // Render target height, used to implement sk_Height and to calculate sk_FragCoord when
28 // origin_upper_left is not supported.
29 GrGLSLProgramDataManager::UniformHandle fRTHeightUni;
30};
31
32class GrGLSLUniformHandler {
33public:
34 struct UniformInfo {
35 GrShaderVar fVariable;
36 uint32_t fVisibility;
37 const GrFragmentProcessor* fOwner;
38 SkString fRawName;
39 };
40
41 virtual ~GrGLSLUniformHandler() {}
42
43 using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
44
45 GR_DEFINE_RESOURCE_HANDLE_CLASS(SamplerHandle);
46
47 /** Add a uniform variable to the current program, that has visibility in one or more shaders.
48 visibility is a bitfield of GrShaderFlag values indicating from which shaders the uniform
49 should be accessible. At least one bit must be set. Geometry shader uniforms are not
50 supported at this time. The actual uniform name will be mangled. If outName is not nullptr
51 then it will refer to the final uniform name after return. Use the addUniformArray variant
52 to add an array of uniforms. */
53 UniformHandle addUniform(const GrFragmentProcessor* owner,
54 uint32_t visibility,
55 GrSLType type,
56 const char* name,
57 const char** outName = nullptr) {
58 SkASSERT(!GrSLTypeIsCombinedSamplerType(type));
59 return this->addUniformArray(owner, visibility, type, name, 0, outName);
60 }
61
62 UniformHandle addUniformArray(const GrFragmentProcessor* owner,
63 uint32_t visibility,
64 GrSLType type,
65 const char* name,
66 int arrayCount,
67 const char** outName = nullptr) {
68 SkASSERT(!GrSLTypeIsCombinedSamplerType(type));
69 bool mangle = strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX));
70 return this->internalAddUniformArray(owner, visibility, type, name, mangle, arrayCount,
71 outName);
72 }
73
74 virtual const GrShaderVar& getUniformVariable(UniformHandle u) const = 0;
75
76 /**
77 * Shortcut for getUniformVariable(u).c_str()
78 */
79 virtual const char* getUniformCStr(UniformHandle u) const = 0;
80
81protected:
82 explicit GrGLSLUniformHandler(GrGLSLProgramBuilder* program) : fProgramBuilder(program) {}
83
84 // This is not owned by the class
85 GrGLSLProgramBuilder* fProgramBuilder;
86
87private:
88 virtual const char * samplerVariable(SamplerHandle) const = 0;
89 // Only called if GrShaderCaps(:textureSwizzleAppliedInShader() == true.
90 virtual GrSwizzle samplerSwizzle(SamplerHandle) const = 0;
91
92 virtual SamplerHandle addSampler(const GrBackendFormat&, GrSamplerState, const GrSwizzle&,
93 const char* name, const GrShaderCaps*) = 0;
94
95 virtual UniformHandle internalAddUniformArray(const GrFragmentProcessor* owner,
96 uint32_t visibility,
97 GrSLType type,
98 const char* name,
99 bool mangleName,
100 int arrayCount,
101 const char** outName) = 0;
102
103 virtual void appendUniformDecls(GrShaderFlags visibility, SkString*) const = 0;
104
105 friend class GrGLSLProgramBuilder;
106};
107
108#endif
109