Blender V4.3
gpu_shader_interface.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2016 by Mike Erwin. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
14#pragma once
15
16#include <cstring> /* required for STREQ later on. */
17
18#include "BLI_hash.h"
19#include "BLI_utildefines.h"
20
21#include "GPU_shader.hh"
22#include "GPU_vertex_format.hh" /* GPU_VERT_ATTR_MAX_LEN */
24
25namespace blender::gpu {
26
42
49 /* TODO(fclem): should be protected. */
50 public:
52 ShaderInput *inputs_ = nullptr;
54 char *name_buffer_ = nullptr;
67 /* Bitmask to apply to enabled_ssbo_mask_ to get attributes that are sourced from SSBOs. */
72
79
80 public:
82 virtual ~ShaderInterface();
83
84 void debug_print() const;
85
86 inline const ShaderInput *attr_get(const char *name) const
87 {
88 return input_lookup(inputs_, attr_len_, name);
89 }
90 inline const ShaderInput *attr_get(const int binding) const
91 {
92 return input_lookup(inputs_, attr_len_, binding);
93 }
94
95 inline const ShaderInput *ubo_get(const char *name) const
96 {
97 return input_lookup(inputs_ + attr_len_, ubo_len_, name);
98 }
99 inline const ShaderInput *ubo_get(const int binding) const
100 {
101 return input_lookup(inputs_ + attr_len_, ubo_len_, binding);
102 }
103
104 inline const ShaderInput *uniform_get(const char *name) const
105 {
106 return input_lookup(inputs_ + attr_len_ + ubo_len_, uniform_len_, name);
107 }
108
109 inline const ShaderInput *texture_get(const int binding) const
110 {
111 return input_lookup(inputs_ + attr_len_ + ubo_len_, uniform_len_, binding);
112 }
113
114 inline const ShaderInput *ssbo_get(const char *name) const
115 {
116 return input_lookup(inputs_ + attr_len_ + ubo_len_ + uniform_len_, ssbo_len_, name);
117 }
118 inline const ShaderInput *ssbo_get(const int binding) const
119 {
120 return input_lookup(inputs_ + attr_len_ + ubo_len_ + uniform_len_, ssbo_len_, binding);
121 }
122
123 inline const ShaderInput *constant_get(const char *name) const
124 {
125 return input_lookup(
127 }
128
129 inline const char *input_name_get(const ShaderInput *input) const
130 {
131 return name_buffer_ + input->name_offset;
132 }
133
134 /* Returns uniform location. */
135 inline int32_t uniform_builtin(const GPUUniformBuiltin builtin) const
136 {
137 BLI_assert(builtin >= 0 && builtin < GPU_NUM_UNIFORMS);
138 return builtins_[builtin];
139 }
140
141 /* Returns binding position. */
142 inline int32_t ubo_builtin(const GPUUniformBlockBuiltin builtin) const
143 {
144 BLI_assert(builtin >= 0 && builtin < GPU_NUM_UNIFORM_BLOCKS);
145 return builtin_blocks_[builtin];
146 }
147
148 protected:
149 static inline const char *builtin_uniform_name(GPUUniformBuiltin u);
150 static inline const char *builtin_uniform_block_name(GPUUniformBlockBuiltin u);
151
152 inline uint32_t set_input_name(ShaderInput *input, char *name, uint32_t name_len) const;
153 inline void copy_input_name(ShaderInput *input,
154 const StringRefNull &name,
155 char *name_buffer,
156 uint32_t &name_buffer_offset) const;
157
161 void sort_inputs();
162
163 private:
164 inline const ShaderInput *input_lookup(const ShaderInput *const inputs,
165 uint inputs_len,
166 const char *name) const;
167
168 inline const ShaderInput *input_lookup(const ShaderInput *const inputs,
169 uint inputs_len,
170 int binding) const;
171};
172
174{
175 switch (u) {
177 return "ModelMatrix";
178 case GPU_UNIFORM_VIEW:
179 return "ViewMatrix";
181 return "ModelViewMatrix";
183 return "ProjectionMatrix";
185 return "ViewProjectionMatrix";
186 case GPU_UNIFORM_MVP:
187 return "ModelViewProjectionMatrix";
188
190 return "ModelMatrixInverse";
192 return "ViewMatrixInverse";
194 return "ModelViewMatrixInverse";
196 return "ProjectionMatrixInverse";
198 return "ViewProjectionMatrixInverse";
199
201 return "NormalMatrix";
202 case GPU_UNIFORM_ORCO:
203 return "OrcoTexCoFactors";
205 return "WorldClipPlanes";
206
208 return "color";
210 return "gpu_BaseInstance";
212 return "drw_resourceChunk";
214 return "drw_ResourceID";
216 return "srgbTarget";
217
218 default:
219 return nullptr;
220 }
221}
222
224{
225 switch (u) {
227 return "viewBlock";
229 return "modelBlock";
231 return "infoBlock";
232
234 return "drw_view_";
236 return "drw_matrices";
238 return "drw_infos";
240 return "drw_clipping_";
241 default:
242 return nullptr;
243 }
244}
245
246/* Returns string length including '\0' terminator. */
248 char *name,
249 uint32_t name_len) const
250{
251 /* remove "[0]" from array name */
252 if (name[name_len - 1] == ']') {
253 for (; name_len > 1; name_len--) {
254 if (name[name_len] == '[') {
255 name[name_len] = '\0';
256 break;
257 }
258 }
259 }
260
261 input->name_offset = (uint32_t)(name - name_buffer_);
262 input->name_hash = BLI_hash_string(name);
263 return name_len + 1; /* include NULL terminator */
264}
265
267 const StringRefNull &name,
268 char *name_buffer,
269 uint32_t &name_buffer_offset) const
270{
271 uint32_t name_len = name.size();
272 /* Copy include NULL terminator. */
273 memcpy(name_buffer + name_buffer_offset, name.c_str(), name_len + 1);
274 name_buffer_offset += set_input_name(input, name_buffer + name_buffer_offset, name_len);
275}
276
277inline const ShaderInput *ShaderInterface::input_lookup(const ShaderInput *const inputs,
278 const uint inputs_len,
279 const char *name) const
280{
281 const uint name_hash = BLI_hash_string(name);
282 /* Simple linear search for now. */
283 for (int i = inputs_len - 1; i >= 0; i--) {
284 if (inputs[i].name_hash == name_hash) {
285 if ((i > 0) && UNLIKELY(inputs[i - 1].name_hash == name_hash)) {
286 /* Hash collision resolve. */
287 for (; i >= 0 && inputs[i].name_hash == name_hash; i--) {
288 if (STREQ(name, name_buffer_ + inputs[i].name_offset)) {
289 return inputs + i; /* not found */
290 }
291 }
292 return nullptr; /* not found */
293 }
294
295 /* This is a bit dangerous since we could have a hash collision.
296 * where the asked uniform that does not exist has the same hash
297 * as a real uniform. */
298 BLI_assert(STREQ(name, name_buffer_ + inputs[i].name_offset));
299 return inputs + i;
300 }
301 }
302 return nullptr; /* not found */
303}
304
305inline const ShaderInput *ShaderInterface::input_lookup(const ShaderInput *const inputs,
306 const uint inputs_len,
307 const int binding) const
308{
309 /* Simple linear search for now. */
310 for (int i = inputs_len - 1; i >= 0; i--) {
311 if (inputs[i].binding == binding) {
312 return inputs + i;
313 }
314 }
315 return nullptr; /* not found */
316}
317
318} // namespace blender::gpu
#define BLI_assert(a)
Definition BLI_assert.h:50
BLI_INLINE unsigned int BLI_hash_string(const char *str)
Definition BLI_hash.h:71
unsigned int uint
#define UNLIKELY(x)
#define STREQ(a, b)
#define GPU_NUM_UNIFORMS
GPUUniformBuiltin
@ GPU_UNIFORM_VIEWPROJECTION_INV
@ GPU_UNIFORM_PROJECTION
@ GPU_UNIFORM_RESOURCE_ID
@ GPU_UNIFORM_ORCO
@ GPU_UNIFORM_VIEWPROJECTION
@ GPU_UNIFORM_SRGB_TRANSFORM
@ GPU_UNIFORM_VIEW
@ GPU_UNIFORM_MODEL
@ GPU_UNIFORM_MODELVIEW
@ GPU_UNIFORM_BASE_INSTANCE
@ GPU_UNIFORM_VIEW_INV
@ GPU_UNIFORM_MODEL_INV
@ GPU_UNIFORM_PROJECTION_INV
@ GPU_UNIFORM_CLIPPLANES
@ GPU_UNIFORM_COLOR
@ GPU_UNIFORM_MODELVIEW_INV
@ GPU_UNIFORM_NORMAL
@ GPU_UNIFORM_RESOURCE_CHUNK
@ GPU_UNIFORM_MVP
GPUUniformBlockBuiltin
@ GPU_UNIFORM_BLOCK_DRW_VIEW
@ GPU_UNIFORM_BLOCK_DRW_MODEL
@ GPU_NUM_UNIFORM_BLOCKS
@ GPU_UNIFORM_BLOCK_MODEL
@ GPU_UNIFORM_BLOCK_VIEW
@ GPU_UNIFORM_BLOCK_DRW_CLIPPING
@ GPU_UNIFORM_BLOCK_DRW_INFOS
@ GPU_UNIFORM_BLOCK_INFO
#define GPU_VERT_ATTR_MAX_LEN
void copy_input_name(ShaderInput *input, const StringRefNull &name, char *name_buffer, uint32_t &name_buffer_offset) const
int32_t uniform_builtin(const GPUUniformBuiltin builtin) const
const ShaderInput * ubo_get(const char *name) const
const ShaderInput * attr_get(const char *name) const
uint32_t set_input_name(ShaderInput *input, char *name, uint32_t name_len) const
const ShaderInput * texture_get(const int binding) const
const ShaderInput * ubo_get(const int binding) const
const char * input_name_get(const ShaderInput *input) const
static const char * builtin_uniform_block_name(GPUUniformBlockBuiltin u)
const ShaderInput * ssbo_get(const char *name) const
int32_t ubo_builtin(const GPUUniformBlockBuiltin builtin) const
int32_t builtin_blocks_[GPU_NUM_UNIFORM_BLOCKS]
const ShaderInput * attr_get(const int binding) const
int32_t builtins_[GPU_NUM_UNIFORMS]
const ShaderInput * uniform_get(const char *name) const
const ShaderInput * constant_get(const char *name) const
uint8_t attr_types_[GPU_VERT_ATTR_MAX_LEN]
const ShaderInput * ssbo_get(const int binding) const
static const char * builtin_uniform_name(GPUUniformBuiltin u)
unsigned short uint16_t
Definition stdint.h:79
unsigned int uint32_t
Definition stdint.h:80
signed int int32_t
Definition stdint.h:77
unsigned char uint8_t
Definition stdint.h:78
unsigned __int64 uint64_t
Definition stdint.h:90
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...