Blender V5.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
13
14#pragma once
15
16#include <cstring> /* required for STREQ later on. */
17
18#include "BLI_hash_c.hh"
19#include "BLI_sys_types.hh"
20
21#include "GPU_format.hh"
22#include "GPU_shader.hh"
23#include "GPU_vertex_format.hh" /* GPU_VERT_ATTR_MAX_LEN */
26
27namespace blender::gpu {
28
44
51
52 public:
53 using Key = uint64_t;
54
59 Key uid = 0;
60
61 /* TODO(fclem): should be protected. */
62 static std::atomic<Key> next_uid_;
63
65 ShaderInput *inputs_ = nullptr;
67 char *name_buffer_ = nullptr;
81 /* Bitmask to apply to enabled_ssbo_mask_ to get attributes that are sourced from SSBOs. */
86
92 uint8_t attr_types_[GPU_VERT_ATTR_MAX_LEN];
93
94 /* Formats of all image units. */
95 std::array<TextureWriteFormat, GPU_MAX_IMAGE> image_formats_;
96
98 virtual ~ShaderInterface();
99
100 void debug_print() const;
101
102 const ShaderInput *attr_get(const StringRefNull name) const
103 {
104 return input_lookup(inputs_, attr_len_, name);
105 }
106 const ShaderInput *attr_get(const int binding) const
107 {
108 return input_lookup(inputs_, attr_len_, binding);
109 }
110
111 const ShaderInput *ubo_get(const StringRefNull name) const
112 {
113 return input_lookup(inputs_ + attr_len_, ubo_len_, name);
114 }
115 const ShaderInput *ubo_get(const int binding) const
116 {
117 return input_lookup(inputs_ + attr_len_, ubo_len_, binding);
118 }
119
120 const ShaderInput *uniform_get(const StringRefNull name) const
121 {
122 return input_lookup(inputs_ + attr_len_ + ubo_len_, uniform_len_, name);
123 }
124
125 const ShaderInput *texture_get(const int binding) const
126 {
127 return input_lookup(inputs_ + attr_len_ + ubo_len_, uniform_len_, binding);
128 }
129
130 const ShaderInput *ssbo_get(const StringRefNull name) const
131 {
132 return input_lookup(inputs_ + attr_len_ + ubo_len_ + uniform_len_, ssbo_len_, name);
133 }
134 const ShaderInput *ssbo_get(const int binding) const
135 {
136 return input_lookup(inputs_ + attr_len_ + ubo_len_ + uniform_len_, ssbo_len_, binding);
137 }
138
139 const ShaderInput *tlas_get(const StringRefNull name) const
140 {
141 return input_lookup(
143 }
144 const ShaderInput *tlas_get(const int binding) const
145 {
146 return input_lookup(
148 }
149
150 const ShaderInput *constant_get(const StringRefNull name) const
151 {
152 return input_lookup(inputs_ + attr_len_ + ubo_len_ + uniform_len_ + ssbo_len_ + tlas_len_,
154 name);
155 }
156
157 const char *input_name_get(const ShaderInput *input) const
158 {
159 return name_buffer_ + input->name_offset;
160 }
161
162 /* Returns uniform location. */
168
169 /* Returns binding position. */
175
176 inline uint valid_bindings_get(const ShaderInput *const inputs, const uint inputs_len) const;
177
178 bool attr_len_get() const
179 {
180 return attr_len_;
181 }
182
183 bool ubo_len_get() const
184 {
185 return ubo_len_;
186 }
187
188 bool uniform_len_get() const
189 {
190 return uniform_len_;
191 }
192
193 bool ssbo_len_get() const
194 {
195 return ssbo_len_;
196 }
197
198 bool constant_len_get() const
199 {
200 return constant_len_;
201 }
202
203 protected:
204 static inline const char *builtin_uniform_name(GPUUniformBuiltin u);
205 static inline const char *builtin_uniform_block_name(GPUUniformBlockBuiltin u);
206
207 inline uint32_t set_input_name(ShaderInput *input, char *name, uint32_t name_len) const;
208 inline void copy_input_name(ShaderInput *input,
209 const StringRefNull &name,
210 char *name_buffer,
211 uint32_t &name_buffer_offset) const;
212
216 void sort_inputs();
217
219
220 private:
221 inline const ShaderInput *input_lookup(const ShaderInput *const inputs,
222 uint inputs_len,
223 StringRefNull name) const;
224
225 inline const ShaderInput *input_lookup(const ShaderInput *const inputs,
226 uint inputs_len,
227 int binding) const;
228};
229
231{
232 switch (u) {
234 return "ModelMatrix";
235 case GPU_UNIFORM_VIEW:
236 return "ViewMatrix";
238 return "ModelViewMatrix";
240 return "ProjectionMatrix";
242 return "ViewProjectionMatrix";
243 case GPU_UNIFORM_MVP:
244 return "ModelViewProjectionMatrix";
245
247 return "ModelMatrixInverse";
249 return "ViewMatrixInverse";
251 return "ModelViewMatrixInverse";
253 return "ProjectionMatrixInverse";
255 return "ViewProjectionMatrixInverse";
256
258 return "NormalMatrix";
260 return "WorldClipPlanes";
261
263 return "color";
265 return "gpu_BaseInstance";
267 return "srgbTarget";
269 return "gpu_scene_linear_to_rec709";
270
271 default:
272 return nullptr;
273 }
274}
275
277{
278 switch (u) {
280 return "viewBlock";
282 return "modelBlock";
284 return "infoBlock";
285
287 return "drw_view_";
289 return "drw_matrices";
291 return "drw_infos";
293 return "drw_clipping_";
294 default:
295 return nullptr;
296 }
297}
298
299/* Returns string length including '\0' terminator. */
301 char *name,
302 uint32_t name_len) const
303{
304 /* remove "[0]" from array name */
305 if (name[name_len - 1] == ']') {
306 for (; name_len > 1; name_len--) {
307 if (name[name_len] == '[') {
308 name[name_len] = '\0';
309 break;
310 }
311 }
312 }
313
314 input->name_offset = uint32_t(name - name_buffer_);
315 input->name_hash = BLI_hash_string(name);
316 return name_len + 1; /* include NULL terminator */
317}
318
320 const StringRefNull &name,
321 char *name_buffer,
322 uint32_t &name_buffer_offset) const
323{
324 uint32_t name_len = name.size();
325 /* Copy include NULL terminator. */
326 memcpy(name_buffer + name_buffer_offset, name.c_str(), name_len + 1);
327 name_buffer_offset += set_input_name(input, name_buffer + name_buffer_offset, name_len);
328}
329
330inline const ShaderInput *ShaderInterface::input_lookup(const ShaderInput *const inputs,
331 const uint inputs_len,
332 const StringRefNull name) const
333{
334 const uint name_hash = BLI_hash_string(name.c_str());
335 /* Simple linear search for now. */
336 for (int i = inputs_len - 1; i >= 0; i--) {
337 if (inputs[i].name_hash == name_hash) {
338 if ((i > 0) && UNLIKELY(inputs[i - 1].name_hash == name_hash)) {
339 /* Hash collision resolve. */
340 for (; i >= 0 && inputs[i].name_hash == name_hash; i--) {
341 if (name == (name_buffer_ + inputs[i].name_offset)) {
342 return inputs + i; /* not found */
343 }
344 }
345 return nullptr; /* not found */
346 }
347
348 /* This is a bit dangerous since we could have a hash collision.
349 * where the asked uniform that does not exist has the same hash
350 * as a real uniform. */
351 BLI_assert(name == (name_buffer_ + inputs[i].name_offset));
352 return inputs + i;
353 }
354 }
355 return nullptr; /* not found */
356}
357
358inline const ShaderInput *ShaderInterface::input_lookup(const ShaderInput *const inputs,
359 const uint inputs_len,
360 const int binding) const
361{
362 /* Simple linear search for now. */
363 for (int i = inputs_len - 1; i >= 0; i--) {
364 if (inputs[i].binding == binding) {
365 return inputs + i;
366 }
367 }
368 return nullptr; /* not found */
369}
370
372 const uint inputs_len) const
373{
374 /* Simple linear search for now. */
375 int valid_bindings = 0;
376 for (int i = inputs_len - 1; i >= 0; i--) {
377 if (inputs[i].binding > -1) {
378 valid_bindings++;
379 }
380 }
381 return valid_bindings;
382}
383
384} // namespace blender::gpu
#define GPU_NUM_UNIFORMS
#define BLI_assert(a)
#define UNLIKELY(x)
constexpr const char * c_str() const
constexpr int64_t size() const
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 * tlas_get(const int binding) const
void set_image_formats_from_info(const shader::ShaderCreateInfo &info)
const ShaderInput * attr_get(const StringRefNull name) const
const ShaderInput * uniform_get(const StringRefNull 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)
static std::atomic< Key > next_uid_
int32_t ubo_builtin(const GPUUniformBlockBuiltin builtin) const
const ShaderInput * ubo_get(const StringRefNull name) const
const ShaderInput * ssbo_get(const StringRefNull name) const
uint valid_bindings_get(const ShaderInput *const inputs, const uint inputs_len) const
int32_t builtin_blocks_[GPU_NUM_UNIFORM_BLOCKS]
const ShaderInput * attr_get(const int binding) const
int32_t builtins_[GPU_NUM_UNIFORMS]
std::array< TextureWriteFormat, GPU_MAX_IMAGE > image_formats_
const ShaderInput * tlas_get(const StringRefNull name) const
uint8_t attr_types_[GPU_VERT_ATTR_MAX_LEN]
const ShaderInput * constant_get(const StringRefNull name) const
const ShaderInput * ssbo_get(const int binding) const
static const char * builtin_uniform_name(GPUUniformBuiltin u)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
unsigned int uint
Definition BKE_text.h:55
@ GPU_UNIFORM_VIEWPROJECTION
@ GPU_UNIFORM_BASE_INSTANCE
@ GPU_UNIFORM_SCENE_LINEAR_XFORM
@ GPU_UNIFORM_VIEWPROJECTION_INV
@ GPU_UNIFORM_PROJECTION_INV
@ GPU_UNIFORM_MODELVIEW
@ GPU_UNIFORM_MODEL_INV
@ GPU_UNIFORM_SRGB_TRANSFORM
@ GPU_UNIFORM_MVP
@ GPU_UNIFORM_CLIPPLANES
@ GPU_UNIFORM_COLOR
@ GPU_UNIFORM_NORMAL
@ GPU_UNIFORM_MODELVIEW_INV
@ GPU_UNIFORM_MODEL
@ GPU_UNIFORM_VIEW
@ GPU_UNIFORM_VIEW_INV
@ GPU_UNIFORM_PROJECTION
unsigned short uint16_t
Definition BKE_text.h:80
unsigned int uint32_t
Definition BKE_text.h:81
unsigned char uint8_t
Definition BKE_text.h:79
BLI_INLINE unsigned int BLI_hash_string(const char *str)
Definition BLI_hash_c.hh:69
signed int int32_t
Definition BKE_text.h:78
GPUUniformBlockBuiltin
@ GPU_UNIFORM_BLOCK_MODEL
@ GPU_UNIFORM_BLOCK_DRW_VIEW
@ GPU_UNIFORM_BLOCK_DRW_CLIPPING
@ GPU_UNIFORM_BLOCK_VIEW
@ GPU_UNIFORM_BLOCK_DRW_MODEL
@ GPU_UNIFORM_BLOCK_INFO
@ GPU_UNIFORM_BLOCK_DRW_INFOS
@ GPU_NUM_UNIFORM_BLOCKS
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...