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 | |
| 11 | #include "GrGLSLProgramDataManager.h" |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 12 | #include "GrShaderVar.h" |
| 13 | #include "GrSwizzle.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; |
| 19 | |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 20 | // Handles for program uniforms (other than per-effect uniforms) |
| 21 | struct GrGLSLBuiltinUniformHandles { |
| 22 | GrGLSLProgramDataManager::UniformHandle fRTAdjustmentUni; |
| 23 | // We use the render target height to provide a y-down frag coord when specifying |
| 24 | // origin_upper_left is not supported. |
| 25 | GrGLSLProgramDataManager::UniformHandle fRTHeightUni; |
| 26 | }; |
| 27 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 28 | class GrGLSLUniformHandler { |
| 29 | public: |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 30 | virtual ~GrGLSLUniformHandler() {} |
| 31 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 32 | using UniformHandle = GrGLSLProgramDataManager::UniformHandle; |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 33 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 34 | GR_DEFINE_RESOURCE_HANDLE_CLASS(SamplerHandle); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 35 | |
| 36 | /** 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] | 37 | visibility is a bitfield of GrShaderFlag values indicating from which shaders the uniform |
| 38 | 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] | 39 | supported at this time. The actual uniform name will be mangled. If outName is not nullptr |
| 40 | then it will refer to the final uniform name after return. Use the addUniformArray variant |
| 41 | to add an array of uniforms. */ |
| 42 | UniformHandle addUniform(uint32_t visibility, |
| 43 | GrSLType type, |
| 44 | GrSLPrecision precision, |
| 45 | const char* name, |
| 46 | const char** outName = nullptr) { |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 47 | SkASSERT(!GrSLTypeIsCombinedSamplerType(type)); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 48 | return this->addUniformArray(visibility, type, precision, name, 0, outName); |
| 49 | } |
| 50 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 51 | UniformHandle addUniform(uint32_t visibility, |
| 52 | GrSLType type, |
| 53 | const char* name, |
| 54 | const char** outName = nullptr) { |
| 55 | return this->addUniform(visibility, type, kDefault_GrSLPrecision, name, outName); |
| 56 | } |
| 57 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 58 | UniformHandle addUniformArray(uint32_t visibility, |
| 59 | GrSLType type, |
| 60 | GrSLPrecision precision, |
| 61 | const char* name, |
| 62 | int arrayCount, |
| 63 | const char** outName = nullptr) { |
egdaniel | 990dbc8 | 2016-07-13 14:09:30 -0700 | [diff] [blame] | 64 | SkASSERT(!GrSLTypeIsCombinedSamplerType(type)); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 65 | bool mangle = strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX)); |
| 66 | return this->internalAddUniformArray(visibility, type, precision, name, mangle, arrayCount, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 67 | outName); |
| 68 | } |
| 69 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 70 | UniformHandle addUniformArray(uint32_t visibility, |
| 71 | GrSLType type, |
| 72 | const char* name, |
| 73 | int arrayCount, |
| 74 | const char** outName = nullptr) { |
| 75 | SkASSERT(!GrSLTypeIsCombinedSamplerType(type)); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 76 | bool mangle = strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX)); |
| 77 | return this->internalAddUniformArray(visibility, type, kDefault_GrSLPrecision, name, mangle, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 78 | arrayCount, outName); |
| 79 | } |
| 80 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 81 | virtual const GrShaderVar& getUniformVariable(UniformHandle u) const = 0; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Shortcut for getUniformVariable(u).c_str() |
| 85 | */ |
| 86 | virtual const char* getUniformCStr(UniformHandle u) const = 0; |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 87 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 88 | protected: |
| 89 | explicit GrGLSLUniformHandler(GrGLSLProgramBuilder* program) : fProgramBuilder(program) {} |
| 90 | |
| 91 | // This is not owned by the class |
| 92 | GrGLSLProgramBuilder* fProgramBuilder; |
| 93 | |
| 94 | private: |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 95 | virtual const GrShaderVar& samplerVariable(SamplerHandle) const = 0; |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 96 | virtual GrSwizzle samplerSwizzle(SamplerHandle) const = 0; |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 97 | |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 98 | virtual SamplerHandle addSampler(uint32_t visibility, GrSwizzle, GrSLType, GrSLPrecision, |
| 99 | const char* name) = 0; |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 100 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 101 | virtual UniformHandle internalAddUniformArray(uint32_t visibility, |
| 102 | GrSLType type, |
| 103 | GrSLPrecision precision, |
| 104 | const char* name, |
| 105 | bool mangleName, |
| 106 | int arrayCount, |
| 107 | const char** outName) = 0; |
| 108 | |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 109 | virtual void appendUniformDecls(GrShaderFlags visibility, SkString*) const = 0; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 110 | |
| 111 | friend class GrGLSLProgramBuilder; |
| 112 | }; |
| 113 | |
| 114 | #endif |