blob: c3232cdee8718956a5f2c4e31de53ae7eaa28478 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrShaderVar.h"
Brian Salomon3ec1f542019-06-17 17:54:57 +000012#include "src/gpu/GrSwizzle.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080014
Robert Phillipsfe8da172018-01-24 14:52:02 +000015// variable names beginning with this prefix will not be mangled
16#define GR_NO_MANGLE_PREFIX "sk_"
17
egdaniel7ea439b2015-12-03 09:20:44 -080018class GrGLSLProgramBuilder;
Greg Daniel9a51a862018-11-30 10:18:14 -050019class GrSamplerState;
20class GrTexture;
egdaniel7ea439b2015-12-03 09:20:44 -080021
Brian Salomon1471df92018-06-08 10:49:00 -040022// Handles for program uniforms (other than per-effect uniforms)
23struct GrGLSLBuiltinUniformHandles {
24 GrGLSLProgramDataManager::UniformHandle fRTAdjustmentUni;
Ethan Nicholascd700e92018-08-24 16:43:57 -040025 // 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
Brian Salomon1471df92018-06-08 10:49:00 -040028 // origin_upper_left is not supported.
Greg Daniele6ab9982018-08-22 13:56:32 +000029 GrGLSLProgramDataManager::UniformHandle fRTHeightUni;
Brian Salomon1471df92018-06-08 10:49:00 -040030};
31
egdaniel7ea439b2015-12-03 09:20:44 -080032class GrGLSLUniformHandler {
33public:
egdaniel7ea439b2015-12-03 09:20:44 -080034 virtual ~GrGLSLUniformHandler() {}
35
Brian Salomonf9f45122016-11-29 11:59:17 -050036 using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
Brian Salomon802cb312018-06-08 18:05:20 -040037
Brian Salomonf9f45122016-11-29 11:59:17 -050038 GR_DEFINE_RESOURCE_HANDLE_CLASS(SamplerHandle);
egdaniel7ea439b2015-12-03 09:20:44 -080039
40 /** Add a uniform variable to the current program, that has visibility in one or more shaders.
cdalton5e58cee2016-02-11 12:49:47 -080041 visibility is a bitfield of GrShaderFlag values indicating from which shaders the uniform
42 should be accessible. At least one bit must be set. Geometry shader uniforms are not
egdaniel7ea439b2015-12-03 09:20:44 -080043 supported at this time. The actual uniform name will be mangled. If outName is not nullptr
44 then it will refer to the final uniform name after return. Use the addUniformArray variant
45 to add an array of uniforms. */
46 UniformHandle addUniform(uint32_t visibility,
47 GrSLType type,
egdaniel7ea439b2015-12-03 09:20:44 -080048 const char* name,
49 const char** outName = nullptr) {
egdaniel990dbc82016-07-13 14:09:30 -070050 SkASSERT(!GrSLTypeIsCombinedSamplerType(type));
Ethan Nicholas858fecc2019-03-07 13:19:18 -050051 return this->addUniformArray(visibility, type, name, 0, outName);
egdaniel7ea439b2015-12-03 09:20:44 -080052 }
53
Ethan Nicholasf7b88202017-09-18 14:10:39 -040054 UniformHandle addUniformArray(uint32_t visibility,
55 GrSLType type,
56 const char* name,
57 int arrayCount,
58 const char** outName = nullptr) {
59 SkASSERT(!GrSLTypeIsCombinedSamplerType(type));
Robert Phillipsfe8da172018-01-24 14:52:02 +000060 bool mangle = strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX));
Ethan Nicholas858fecc2019-03-07 13:19:18 -050061 return this->internalAddUniformArray(visibility, type, name, mangle, arrayCount, outName);
Ethan Nicholasf7b88202017-09-18 14:10:39 -040062 }
63
Brian Salomon99938a82016-11-21 13:41:08 -050064 virtual const GrShaderVar& getUniformVariable(UniformHandle u) const = 0;
egdaniel7ea439b2015-12-03 09:20:44 -080065
66 /**
Ethan Nicholasd4efe682019-08-29 16:10:13 -040067 * 'Or's the visibility parameter with the current uniform visibililty.
68 */
69 virtual void updateUniformVisibility(UniformHandle u, uint32_t visibility) = 0;
70
71 /**
egdaniel7ea439b2015-12-03 09:20:44 -080072 * Shortcut for getUniformVariable(u).c_str()
73 */
74 virtual const char* getUniformCStr(UniformHandle u) const = 0;
egdaniel09aa1fc2016-04-20 07:09:46 -070075
egdaniel7ea439b2015-12-03 09:20:44 -080076protected:
77 explicit GrGLSLUniformHandler(GrGLSLProgramBuilder* program) : fProgramBuilder(program) {}
78
79 // This is not owned by the class
80 GrGLSLProgramBuilder* fProgramBuilder;
81
82private:
Stephen Whited523a062019-06-19 13:12:46 -040083 virtual const char * samplerVariable(SamplerHandle) const = 0;
Brian Salomon68ba1172019-06-05 11:15:08 -040084 // Only called if GrShaderCaps(:textureSwizzleAppliedInShader() == true.
Brian Salomon101b8442016-11-18 11:58:54 -050085 virtual GrSwizzle samplerSwizzle(SamplerHandle) const = 0;
egdaniel09aa1fc2016-04-20 07:09:46 -070086
Greg Daniel2c3398d2019-06-19 11:58:01 -040087 virtual SamplerHandle addSampler(const GrTexture*, const GrSamplerState&, const GrSwizzle&,
88 const char* name, const GrShaderCaps*) = 0;
egdaniel09aa1fc2016-04-20 07:09:46 -070089
egdaniel7ea439b2015-12-03 09:20:44 -080090 virtual UniformHandle internalAddUniformArray(uint32_t visibility,
91 GrSLType type,
egdaniel7ea439b2015-12-03 09:20:44 -080092 const char* name,
93 bool mangleName,
94 int arrayCount,
95 const char** outName) = 0;
96
cdalton5e58cee2016-02-11 12:49:47 -080097 virtual void appendUniformDecls(GrShaderFlags visibility, SkString*) const = 0;
egdaniel7ea439b2015-12-03 09:20:44 -080098
99 friend class GrGLSLProgramBuilder;
100};
101
102#endif