blob: 288e07a0325c64f6cf3ddbbb614b6b729786ba72 [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 /**
67 * Shortcut for getUniformVariable(u).c_str()
68 */
69 virtual const char* getUniformCStr(UniformHandle u) const = 0;
egdaniel09aa1fc2016-04-20 07:09:46 -070070
egdaniel7ea439b2015-12-03 09:20:44 -080071protected:
72 explicit GrGLSLUniformHandler(GrGLSLProgramBuilder* program) : fProgramBuilder(program) {}
73
74 // This is not owned by the class
75 GrGLSLProgramBuilder* fProgramBuilder;
76
77private:
Stephen Whited523a062019-06-19 13:12:46 -040078 virtual const char * samplerVariable(SamplerHandle) const = 0;
Brian Salomon68ba1172019-06-05 11:15:08 -040079 // Only called if GrShaderCaps(:textureSwizzleAppliedInShader() == true.
Brian Salomon101b8442016-11-18 11:58:54 -050080 virtual GrSwizzle samplerSwizzle(SamplerHandle) const = 0;
egdaniel09aa1fc2016-04-20 07:09:46 -070081
Greg Daniel9a51a862018-11-30 10:18:14 -050082 virtual SamplerHandle addSampler(const GrTexture*, const GrSamplerState&, const char* name,
83 const GrShaderCaps*) = 0;
egdaniel09aa1fc2016-04-20 07:09:46 -070084
egdaniel7ea439b2015-12-03 09:20:44 -080085 virtual UniformHandle internalAddUniformArray(uint32_t visibility,
86 GrSLType type,
egdaniel7ea439b2015-12-03 09:20:44 -080087 const char* name,
88 bool mangleName,
89 int arrayCount,
90 const char** outName) = 0;
91
cdalton5e58cee2016-02-11 12:49:47 -080092 virtual void appendUniformDecls(GrShaderFlags visibility, SkString*) const = 0;
egdaniel7ea439b2015-12-03 09:20:44 -080093
94 friend class GrGLSLProgramBuilder;
95};
96
97#endif