blob: e1511c257836a6756d16d9fd874eda19547bbf2d [file] [log] [blame]
egdaniel7ea439b2015-12-03 09:20:44 -08001/*
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 "GrGLSLProgramDataManager.h"
Brian Salomon99938a82016-11-21 13:41:08 -050012#include "GrShaderVar.h"
13#include "GrSwizzle.h"
egdaniel7ea439b2015-12-03 09:20:44 -080014
15class GrGLSLProgramBuilder;
16
17class GrGLSLUniformHandler {
18public:
egdaniel7ea439b2015-12-03 09:20:44 -080019 virtual ~GrGLSLUniformHandler() {}
20
Brian Salomonf9f45122016-11-29 11:59:17 -050021 using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
22 GR_DEFINE_RESOURCE_HANDLE_CLASS(SamplerHandle);
Greg Danielbc5d4d72017-05-05 10:28:42 -040023 GR_DEFINE_RESOURCE_HANDLE_CLASS(TexelBufferHandle);
egdaniel7ea439b2015-12-03 09:20:44 -080024
25 /** Add a uniform variable to the current program, that has visibility in one or more shaders.
cdalton5e58cee2016-02-11 12:49:47 -080026 visibility is a bitfield of GrShaderFlag values indicating from which shaders the uniform
27 should be accessible. At least one bit must be set. Geometry shader uniforms are not
egdaniel7ea439b2015-12-03 09:20:44 -080028 supported at this time. The actual uniform name will be mangled. If outName is not nullptr
29 then it will refer to the final uniform name after return. Use the addUniformArray variant
30 to add an array of uniforms. */
31 UniformHandle addUniform(uint32_t visibility,
32 GrSLType type,
33 GrSLPrecision precision,
34 const char* name,
35 const char** outName = nullptr) {
egdaniel990dbc82016-07-13 14:09:30 -070036 SkASSERT(!GrSLTypeIsCombinedSamplerType(type));
egdaniel7ea439b2015-12-03 09:20:44 -080037 return this->addUniformArray(visibility, type, precision, name, 0, outName);
38 }
39
Ethan Nicholasf7b88202017-09-18 14:10:39 -040040 UniformHandle addUniform(uint32_t visibility,
41 GrSLType type,
42 const char* name,
43 const char** outName = nullptr) {
44 return this->addUniform(visibility, type, kDefault_GrSLPrecision, name, outName);
45 }
46
egdaniel7ea439b2015-12-03 09:20:44 -080047 UniformHandle addUniformArray(uint32_t visibility,
48 GrSLType type,
49 GrSLPrecision precision,
50 const char* name,
51 int arrayCount,
52 const char** outName = nullptr) {
egdaniel990dbc82016-07-13 14:09:30 -070053 SkASSERT(!GrSLTypeIsCombinedSamplerType(type));
Ethan Nicholas29b34342018-01-23 16:39:42 -050054 return this->internalAddUniformArray(visibility, type, precision, name, true, arrayCount,
egdaniel7ea439b2015-12-03 09:20:44 -080055 outName);
56 }
57
Ethan Nicholasf7b88202017-09-18 14:10:39 -040058 UniformHandle addUniformArray(uint32_t visibility,
59 GrSLType type,
60 const char* name,
61 int arrayCount,
62 const char** outName = nullptr) {
63 SkASSERT(!GrSLTypeIsCombinedSamplerType(type));
Ethan Nicholas29b34342018-01-23 16:39:42 -050064 return this->internalAddUniformArray(visibility, type, kDefault_GrSLPrecision, name, true,
Ethan Nicholasf7b88202017-09-18 14:10:39 -040065 arrayCount, outName);
66 }
67
Brian Salomon99938a82016-11-21 13:41:08 -050068 virtual const GrShaderVar& getUniformVariable(UniformHandle u) const = 0;
egdaniel7ea439b2015-12-03 09:20:44 -080069
70 /**
71 * Shortcut for getUniformVariable(u).c_str()
72 */
73 virtual const char* getUniformCStr(UniformHandle u) const = 0;
egdaniel09aa1fc2016-04-20 07:09:46 -070074
egdaniel7ea439b2015-12-03 09:20:44 -080075protected:
76 explicit GrGLSLUniformHandler(GrGLSLProgramBuilder* program) : fProgramBuilder(program) {}
77
78 // This is not owned by the class
79 GrGLSLProgramBuilder* fProgramBuilder;
80
81private:
Brian Salomon99938a82016-11-21 13:41:08 -050082 virtual const GrShaderVar& samplerVariable(SamplerHandle) const = 0;
Brian Salomon101b8442016-11-18 11:58:54 -050083 virtual GrSwizzle samplerSwizzle(SamplerHandle) const = 0;
egdaniel09aa1fc2016-04-20 07:09:46 -070084
Brian Salomon101b8442016-11-18 11:58:54 -050085 virtual SamplerHandle addSampler(uint32_t visibility, GrSwizzle, GrSLType, GrSLPrecision,
86 const char* name) = 0;
egdaniel09aa1fc2016-04-20 07:09:46 -070087
Greg Danielbc5d4d72017-05-05 10:28:42 -040088 virtual const GrShaderVar& texelBufferVariable(TexelBufferHandle) const = 0;
89 virtual TexelBufferHandle addTexelBuffer(uint32_t visibility, GrSLPrecision,
90 const char* name) = 0;
91
egdaniel7ea439b2015-12-03 09:20:44 -080092 virtual UniformHandle internalAddUniformArray(uint32_t visibility,
93 GrSLType type,
94 GrSLPrecision precision,
95 const char* name,
96 bool mangleName,
97 int arrayCount,
98 const char** outName) = 0;
99
cdalton5e58cee2016-02-11 12:49:47 -0800100 virtual void appendUniformDecls(GrShaderFlags visibility, SkString*) const = 0;
egdaniel7ea439b2015-12-03 09:20:44 -0800101
102 friend class GrGLSLProgramBuilder;
103};
104
105#endif