Blender V4.3
draw_shader_shared.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#ifndef GPU_SHADER
6# pragma once
7
8# include "GPU_shader.hh"
10# include "draw_defines.hh"
11
12struct ViewCullingData;
13struct ViewMatrices;
14struct ObjectMatrices;
15struct ObjectInfos;
16struct ObjectBounds;
17struct VolumeInfos;
18struct CurvesInfos;
19struct ObjectAttribute;
20struct LayerAttribute;
21struct DrawCommand;
22struct DispatchCommand;
24struct DRWDebugVert;
26struct FrustumCorners;
27struct FrustumPlanes;
28
29/* __cplusplus is true when compiling with MSL. */
30# if defined(__cplusplus) && !defined(GPU_SHADER)
31/* C++ only forward declarations. */
32struct Object;
33struct Scene;
34struct ViewLayer;
35struct GPUUniformAttr;
36struct GPULayerAttr;
37
38namespace blender::draw {
39
40struct ObjectRef;
41
42} // namespace blender::draw
43
44# endif
45#endif
46
47#define DRW_SHADER_SHARED_H
48
49#define DRW_RESOURCE_CHUNK_LEN 512
50
51/* Define the maximum number of grid we allow in a volume UBO. */
52#define DRW_GRID_PER_VOLUME_MAX 16
53
54/* Define the maximum number of attribute we allow in a curves UBO.
55 * This should be kept in sync with `GPU_ATTR_MAX` */
56#define DRW_ATTRIBUTE_PER_CURVES_MAX 15
57
58/* -------------------------------------------------------------------- */
62#ifndef DRW_VIEW_LEN
63/* Single-view case (default). */
64# define drw_view_id 0
65# define DRW_VIEW_LEN 1
66# define DRW_VIEW_SHIFT 0
67# define DRW_VIEW_FROM_RESOURCE_ID
68#else
69
70/* Multi-view case. */
72// # define DRW_VIEW_LEN 64
80# define DRW_VIEW_SHIFT \
81 ((DRW_VIEW_LEN > 32) ? 6 : \
82 (DRW_VIEW_LEN > 16) ? 5 : \
83 (DRW_VIEW_LEN > 8) ? 4 : \
84 (DRW_VIEW_LEN > 4) ? 3 : \
85 (DRW_VIEW_LEN > 2) ? 2 : \
86 1)
87# define DRW_VIEW_MASK ~(0xFFFFFFFFu << DRW_VIEW_SHIFT)
88# define DRW_VIEW_FROM_RESOURCE_ID drw_view_id = (uint(drw_ResourceID) & DRW_VIEW_MASK)
89#endif
90
92 float4 corners[8];
93};
95
97 /* [0] left
98 * [1] right
99 * [2] bottom
100 * [3] top
101 * [4] near
102 * [5] far */
103 float4 planes[6];
104};
106
115
117 float4x4 viewmat;
118 float4x4 viewinv;
119 float4x4 winmat;
120 float4x4 wininv;
121};
123
124/* Do not override old definitions if the shader uses this header but not shader info. */
125#ifdef USE_GPU_SHADER_CREATE_INFO
126/* TODO(@fclem): Mass rename. */
127# define ViewMatrix drw_view.viewmat
128# define ViewMatrixInverse drw_view.viewinv
129# define ProjectionMatrix drw_view.winmat
130# define ProjectionMatrixInverse drw_view.wininv
131#endif
132
135/* -------------------------------------------------------------------- */
140 float4x4 model;
142
143#if !defined(GPU_SHADER) && defined(__cplusplus)
144 void sync(const Object &object);
145 void sync(const float4x4 &model_matrix);
146#endif
147};
149
151 OBJECT_SELECTED = (1u << 0u),
152 OBJECT_FROM_DUPLI = (1u << 1u),
153 OBJECT_FROM_SET = (1u << 2u),
154 OBJECT_ACTIVE = (1u << 3u),
156 OBJECT_HOLDOUT = (1u << 5u),
157 /* Avoid skipped info to change culling. */
158 OBJECT_NO_INFO = ~OBJECT_HOLDOUT
160
162#if defined(GPU_SHADER) && !defined(DRAW_FINALIZE_SHADER)
163 /* TODO Rename to struct member for GLSL too. */
164 float4 orco_mul_bias[2];
165 float4 ob_color;
166 float4 infos;
167#else
173
174 float4 ob_color;
178 float random;
180#endif
181
182#if !defined(GPU_SHADER) && defined(__cplusplus)
183 void sync();
184 void sync(const blender::draw::ObjectRef ref, bool is_active_object);
185#endif
186};
188
190{
191#if defined(GPU_SHADER) && !defined(DRAW_FINALIZE_SHADER)
192 return floatBitsToUint(object_infos.infos.y) & 0xFFu;
193#else
194 return object_infos.light_and_shadow_set_membership & 0xFFu;
195#endif
196}
197
199{
200#if defined(GPU_SHADER) && !defined(DRAW_FINALIZE_SHADER)
201 return (floatBitsToUint(object_infos.infos.y) >> 8u) & 0xFFu;
202#else
203 return (object_infos.light_and_shadow_set_membership >> 8u) & 0xFFu;
204#endif
205}
206
216#define _inner_sphere_radius bounding_corners[3].w
217
218#if !defined(GPU_SHADER) && defined(__cplusplus)
219 void sync();
220 void sync(const Object &ob, float inflate_bounds = 0.0f);
221 void sync(const float3 &center, const float3 &size);
222#endif
223};
225
226/* Return true if `bounding_corners` are valid. Should be checked before accessing them.
227 * Does not guarantee that `bounding_sphere` is valid.
228 * Converting these bounds to an `IsectBox` may generate invalid clip planes.
229 * For safe `IsectBox` generation check `drw_bounds_are_valid`. */
231{
232 return bounds.bounding_sphere.w != -1.0f;
233}
234
235/* Return true if bounds are ready for culling.
236 * In this case, both `bounding_corners` and `bounding_sphere` are valid.
237 * These bounds can be safely converted to an `IsectBox` with valid clip planes. */
239{
240 return bounds.bounding_sphere.w >= 0.0f;
241}
242
245/* -------------------------------------------------------------------- */
260
266};
268
269#pragma pack(push, 4)
271 /* Workaround the padding cost from alignment requirements.
272 * (see GL spec : 7.6.2.2 Standard Uniform Block Layout) */
275
276#if !defined(GPU_SHADER) && defined(__cplusplus)
281 bool sync(const blender::draw::ObjectRef &ref, const GPUUniformAttr &attr);
282#endif
283};
284#pragma pack(pop)
288
289#pragma pack(push, 4)
291 float4 data;
293 uint buffer_length; /* Only in the first record. */
295
296#if !defined(GPU_SHADER) && defined(__cplusplus)
297 bool sync(const Scene *scene, const ViewLayer *layer, const GPULayerAttr &attr);
298#endif
299};
300#pragma pack(pop)
302
303
305/* -------------------------------------------------------------------- */
310 /* TODO(fclem): Rename */
314#if defined(GPU_SHADER)
315 uint base_index;
317# define _instance_first_array base_index
318#else
319 union {
321 /* Use this instead of instance_first_indexed for non indexed draw calls. */
323 };
324#endif
325
327
328 uint _pad0, _pad1, _pad2;
329};
331
339
340
342/* -------------------------------------------------------------------- */
346/* Take the header (DrawCommand) into account. */
347#define DRW_DEBUG_PRINT_MAX (8 * 1024) - 4
349#define DRW_DEBUG_PRINT_WORD_WRAP_COLUMN 120u
350
351/* The debug print buffer is laid-out as the following struct.
352 * But we use plain array in shader code instead because of driver issues. */
359
360/* Use number of char as vertex count. Equivalent to `DRWDebugPrintBuffer.command.v_count`. */
361#define drw_debug_print_cursor drw_debug_print_buf[0]
362/* Reuse first instance as row index as we don't use instancing. Equivalent to
363 * `DRWDebugPrintBuffer.command.i_first`. */
364#define drw_debug_print_row_shared drw_debug_print_buf[3]
369#define drw_debug_print_offset 8
370
373/* -------------------------------------------------------------------- */
378 /* This is a weird layout, but needed to be able to use DRWDebugVert as
379 * a DrawCommand and avoid alignment issues. See drw_debug_verts_buf[] definition. */
383 /* Named vert_color to avoid global namespace collision with uniform color. */
385};
387
388inline DRWDebugVert debug_vert_make(uint in_pos0, uint in_pos1, uint in_pos2, uint in_vert_color)
389{
390 DRWDebugVert debug_vert;
391 debug_vert.pos0 = in_pos0;
392 debug_vert.pos1 = in_pos1;
393 debug_vert.pos2 = in_pos2;
394 debug_vert.vert_color = in_vert_color;
395 return debug_vert;
396}
397
398/* Take the header (DrawCommand) into account. */
399#define DRW_DEBUG_DRAW_VERT_MAX (64 * 8192) - 1
400
401/* The debug draw buffer is laid-out as the following struct.
402 * But we use plain array in shader code instead because of driver issues. */
408
409/* Equivalent to `DRWDebugDrawBuffer.command.v_count`. */
410#define drw_debug_draw_v_count drw_debug_verts_buf[0].pos0
415#define drw_debug_draw_offset 2
416
#define BLI_STATIC_ASSERT_ALIGN(st, align)
Definition BLI_assert.h:90
unsigned int uint
static btDbvtVolume bounds(btDbvtNode **leaves, int count)
Definition btDbvt.cpp:299
DRWDebugVert debug_vert_make(uint in_pos0, uint in_pos1, uint in_pos2, uint in_vert_color)
#define drw_view_id
bool drw_bounds_corners_are_valid(ObjectBounds bounds)
bool drw_bounds_are_valid(ObjectBounds bounds)
uint blocker_shadow_set_get(ObjectInfos object_infos)
uint receiver_light_set_get(ObjectInfos object_infos)
#define DRW_GRID_PER_VOLUME_MAX
#define DRW_DEBUG_PRINT_MAX
@ OBJECT_NO_INFO
@ OBJECT_SELECTED
@ OBJECT_FROM_DUPLI
@ OBJECT_ACTIVE
@ OBJECT_FROM_SET
@ OBJECT_HOLDOUT
@ OBJECT_NEGATIVE_SCALE
#define DRW_DEBUG_DRAW_VERT_MAX
#define DRW_ATTRIBUTE_PER_CURVES_MAX
unsigned int uint32_t
Definition stdint.h:80
DRWDebugVert verts[DRW_DEBUG_DRAW_VERT_MAX]
uint char_array[DRW_DEBUG_PRINT_MAX]
float4 bounding_corners[4]
eObjectInfoFlag flag
packed_float3 orco_add
uint light_and_shadow_set_membership
packed_float3 orco_mul
FrustumPlanes frustum_planes
FrustumCorners frustum_corners
float4x4 grids_xform[DRW_GRID_PER_VOLUME_MAX]