egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrShaderVar.h" |
Brian Salomon | 3ec1f54 | 2019-06-17 17:54:57 +0000 | [diff] [blame] | 12 | #include "src/gpu/GrSwizzle.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 14 | |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 15 | // variable names beginning with this prefix will not be mangled |
| 16 | #define GR_NO_MANGLE_PREFIX "sk_" |
| 17 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 18 | class GrGLSLProgramBuilder; |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 19 | class GrSamplerState; |
| 20 | class GrTexture; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 21 | |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 22 | // Handles for program uniforms (other than per-effect uniforms) |
| 23 | struct GrGLSLBuiltinUniformHandles { |
| 24 | GrGLSLProgramDataManager::UniformHandle fRTAdjustmentUni; |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 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 |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 28 | // origin_upper_left is not supported. |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 29 | GrGLSLProgramDataManager::UniformHandle fRTHeightUni; |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 30 | }; |
| 31 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 32 | class GrGLSLUniformHandler { |
| 33 | public: |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 34 | virtual ~GrGLSLUniformHandler() {} |
| 35 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 36 | using UniformHandle = GrGLSLProgramDataManager::UniformHandle; |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 37 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 38 | GR_DEFINE_RESOURCE_HANDLE_CLASS(SamplerHandle); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 39 | |
| 40 | /** Add a uniform variable to the current program, that has visibility in one or more shaders. |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 41 | 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 |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 43 | 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, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 48 | const char* name, |
| 49 | const char** outName = nullptr) { |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 50 | SkASSERT(!GrSLTypeIsCombinedSamplerType(type)); |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 51 | return this->addUniformArray(visibility, type, name, 0, outName); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 54 | 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 Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 60 | bool mangle = strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX)); |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 61 | return this->internalAddUniformArray(visibility, type, name, mangle, arrayCount, outName); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 62 | } |
| 63 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 64 | virtual const GrShaderVar& getUniformVariable(UniformHandle u) const = 0; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 67 | * 'Or's the visibility parameter with the current uniform visibililty. |
| 68 | */ |
| 69 | virtual void updateUniformVisibility(UniformHandle u, uint32_t visibility) = 0; |
| 70 | |
| 71 | /** |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 72 | * Shortcut for getUniformVariable(u).c_str() |
| 73 | */ |
| 74 | virtual const char* getUniformCStr(UniformHandle u) const = 0; |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 75 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 76 | protected: |
| 77 | explicit GrGLSLUniformHandler(GrGLSLProgramBuilder* program) : fProgramBuilder(program) {} |
| 78 | |
| 79 | // This is not owned by the class |
| 80 | GrGLSLProgramBuilder* fProgramBuilder; |
| 81 | |
| 82 | private: |
Stephen White | d523a06 | 2019-06-19 13:12:46 -0400 | [diff] [blame] | 83 | virtual const char * samplerVariable(SamplerHandle) const = 0; |
Brian Salomon | 68ba117 | 2019-06-05 11:15:08 -0400 | [diff] [blame] | 84 | // Only called if GrShaderCaps(:textureSwizzleAppliedInShader() == true. |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 85 | virtual GrSwizzle samplerSwizzle(SamplerHandle) const = 0; |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 86 | |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 87 | virtual SamplerHandle addSampler(const GrTexture*, const GrSamplerState&, const GrSwizzle&, |
| 88 | const char* name, const GrShaderCaps*) = 0; |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 89 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 90 | virtual UniformHandle internalAddUniformArray(uint32_t visibility, |
| 91 | GrSLType type, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 92 | const char* name, |
| 93 | bool mangleName, |
| 94 | int arrayCount, |
| 95 | const char** outName) = 0; |
| 96 | |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 97 | virtual void appendUniformDecls(GrShaderFlags visibility, SkString*) const = 0; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 98 | |
| 99 | friend class GrGLSLProgramBuilder; |
| 100 | }; |
| 101 | |
| 102 | #endif |