Blender V4.5
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#pragma once
6
7#if !defined(GPU_SHADER) && !defined(GLSL_CPP_STUBS)
8# include "BLI_math_vector.hh"
9# include "GPU_shader.hh"
11# include "draw_defines.hh"
12
13struct ViewCullingData;
14struct ViewMatrices;
15struct ObjectMatrices;
16struct ObjectInfos;
17struct ObjectBounds;
18struct VolumeInfos;
19struct CurvesInfos;
20struct ObjectAttribute;
21struct LayerAttribute;
22struct DrawCommand;
23struct DispatchCommand;
24struct DRWDebugPrintBuffer;
25struct DRWDebugVertPair;
27struct FrustumCorners;
28struct FrustumPlanes;
29
30/* __cplusplus is true when compiling with MSL. */
31# if defined(__cplusplus) && !defined(GPU_SHADER)
32/* C++ only forward declarations. */
33struct Object;
34struct Scene;
35struct ViewLayer;
36struct GPUUniformAttr;
37struct GPULayerAttr;
38
39namespace blender::draw {
40
41struct ObjectRef;
42
43} // namespace blender::draw
44
45using namespace blender::math;
46
47# endif
48#endif
49
50#define DRW_SHADER_SHARED_H
51
52#define DRW_RESOURCE_CHUNK_LEN 512
53
54/* Define the maximum number of grid we allow in a volume UBO. */
55#define DRW_GRID_PER_VOLUME_MAX 16
56
57/* Define the maximum number of attribute we allow in a curves UBO.
58 * This should be kept in sync with `GPU_ATTR_MAX` */
59#define DRW_ATTRIBUTE_PER_CURVES_MAX 15
60
61/* -------------------------------------------------------------------- */
64
65#if !defined(DRW_VIEW_LEN) && !defined(GLSL_CPP_STUBS)
66/* Single-view case (default). */
67# define drw_view_id 0
68# define DRW_VIEW_LEN 1
69# define DRW_VIEW_SHIFT 0
70# define DRW_VIEW_FROM_RESOURCE_ID
71#else
72
73/* Multi-view case. */
75// # define DRW_VIEW_LEN 64
83# define DRW_VIEW_SHIFT \
84 ((DRW_VIEW_LEN > 32) ? 6 : \
85 (DRW_VIEW_LEN > 16) ? 5 : \
86 (DRW_VIEW_LEN > 8) ? 4 : \
87 (DRW_VIEW_LEN > 4) ? 3 : \
88 (DRW_VIEW_LEN > 2) ? 2 : \
89 1)
90# define DRW_VIEW_MASK ~(0xFFFFFFFFu << DRW_VIEW_SHIFT)
91# define DRW_VIEW_FROM_RESOURCE_ID drw_view_id = (drw_resource_id_raw() & DRW_VIEW_MASK)
92#endif
93
98
100 /* [0] left
101 * [1] right
102 * [2] bottom
103 * [3] top
104 * [4] near
105 * [5] far */
107};
109
118
126
127
128
129/* -------------------------------------------------------------------- */
132
136
137#if !defined(GPU_SHADER) && defined(__cplusplus)
138 void sync(const Object &object);
139 void sync(const float4x4 &model_matrix);
140#endif
141};
143
144enum eObjectInfoFlag : uint32_t {
145 OBJECT_SELECTED = (1u << 0u),
146 OBJECT_FROM_DUPLI = (1u << 1u),
147 OBJECT_FROM_SET = (1u << 2u),
148 OBJECT_ACTIVE = (1u << 3u),
150 OBJECT_HOLDOUT = (1u << 5u),
151 /* Implies all objects that match the current active object's mode and able to be edited
152 * simultaneously. Currently only applicable for edit mode. */
154 /* Avoid skipped info to change culling. */
156};
157
158#if !defined(GPU_SHADER) && defined(__cplusplus)
160#endif
161
168
173 float random;
177 float _pad1;
178 float _pad2;
179
180#if !defined(GPU_SHADER) && defined(__cplusplus)
181 void sync();
182 void sync(const blender::draw::ObjectRef ref, bool is_active_object, bool is_active_edit_mode);
183#endif
184};
186
188{
189 return object_infos.light_and_shadow_set_membership & 0xFFu;
190}
191
193{
194 return (object_infos.light_and_shadow_set_membership >> 8u) & 0xFFu;
195}
196
206#define _inner_sphere_radius bounding_corners[3].w
207
208#if !defined(GPU_SHADER) && defined(__cplusplus)
209 void sync();
210 void sync(const Object &ob, float inflate_bounds = 0.0f);
211 void sync(const float3 &center, const float3 &size);
212#endif
213};
215
216/* Return true if `bounding_corners` are valid. Should be checked before accessing them.
217 * Does not guarantee that `bounding_sphere` is valid.
218 * Converting these bounds to an `IsectBox` may generate invalid clip planes.
219 * For safe `IsectBox` generation check `drw_bounds_are_valid`. */
221{
222 return bounds.bounding_sphere.w != -1.0f;
223}
224
225/* Return true if bounds are ready for culling.
226 * In this case, both `bounding_corners` and `bounding_sphere` are valid.
227 * These bounds can be safely converted to an `IsectBox` with valid clip planes. */
229{
230 return bounds.bounding_sphere.w >= 0.0f;
231}
232
234
235/* -------------------------------------------------------------------- */
238
250
258
259#pragma pack(push, 4)
261 /* Workaround the padding cost from alignment requirements.
262 * (see GL spec : 7.6.2.2 Standard Uniform Block Layout) */
265
266#if !defined(GPU_SHADER) && defined(__cplusplus)
271 bool sync(const blender::draw::ObjectRef &ref, const GPUUniformAttr &attr);
272#endif
273};
274#pragma pack(pop)
278
279#pragma pack(push, 4)
283 uint buffer_length; /* Only in the first record. */
285
286#if !defined(GPU_SHADER) && defined(__cplusplus)
287 bool sync(const Scene *scene, const ViewLayer *layer, const GPULayerAttr &attr);
288#endif
289};
290#pragma pack(pop)
292
293
294
295/* -------------------------------------------------------------------- */
298
300 /* TODO(fclem): Rename */
304#if defined(GPU_SHADER)
307# define _instance_first_array base_index
308#else
309 union {
311 /* Use this instead of instance_first_indexed for non indexed draw calls. */
313 };
314#endif
315
317
319};
321
329
330
331
332/* -------------------------------------------------------------------- */
335
337 /* This is a weird layout, but needed to be able to use DRWDebugVertPair as
338 * a DrawCommand and avoid alignment issues. See drw_debug_lines_buf[] definition. */
342 /* Named vert_color to avoid global namespace collision with uniform color. */
344
348 /* Number of time this line is supposed to be displayed. Decremented by one on display. */
350};
352
354 uint in_pos1_y,
355 uint in_pos1_z,
356 uint in_pos2_x,
357 uint in_pos2_y,
358 uint in_pos2_z,
359 uint in_vert_color,
360 uint in_lifetime)
361{
362 DRWDebugVertPair debug_vert;
363 debug_vert.pos1_x = in_pos1_x;
364 debug_vert.pos1_y = in_pos1_y;
365 debug_vert.pos1_z = in_pos1_z;
366 debug_vert.pos2_x = in_pos2_x;
367 debug_vert.pos2_y = in_pos2_y;
368 debug_vert.pos2_z = in_pos2_z;
369 debug_vert.vert_color = in_vert_color;
370 debug_vert.lifetime = in_lifetime;
371 return debug_vert;
372}
373
375{
376 v_color = clamp(v_color, 0.0f, 1.0f);
377 uint result = 0;
378 result |= uint(v_color.x * 255.0) << 0u;
379 result |= uint(v_color.y * 255.0) << 8u;
380 result |= uint(v_color.z * 255.0) << 16u;
381 result |= uint(v_color.w * 255.0) << 24u;
382 return result;
383}
384
385/* Take the header (DrawCommand) into account. */
386#define DRW_DEBUG_DRAW_VERT_MAX (2 * 1024) - 1
387
388/* The debug draw buffer is laid-out as the following struct.
389 * But we use plain array in shader code instead because of driver issues. */
395
396/* Equivalent to `DRWDebugDrawBuffer.command.v_count`. */
397#define drw_debug_draw_v_count(buf) buf[0].pos1_x
402#define drw_debug_draw_offset 1
403
#define BLI_STATIC_ASSERT_ALIGN(st, align)
Definition BLI_assert.h:86
unsigned int uint
#define ENUM_OPERATORS(_type, _max)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
static btDbvtVolume bounds(btDbvtNode **leaves, int count)
Definition btDbvt.cpp:299
#define drw_view_id
DRWDebugVertPair debug_line_make(uint in_pos1_x, uint in_pos1_y, uint in_pos1_z, uint in_pos2_x, uint in_pos2_y, uint in_pos2_z, uint in_vert_color, uint in_lifetime)
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)
uint debug_color_pack(float4 v_color)
#define DRW_GRID_PER_VOLUME_MAX
@ OBJECT_NO_INFO
@ OBJECT_SELECTED
@ OBJECT_FROM_DUPLI
@ OBJECT_ACTIVE
@ OBJECT_ACTIVE_EDIT_MODE
@ OBJECT_FROM_SET
@ OBJECT_HOLDOUT
@ OBJECT_NEGATIVE_SCALE
#define DRW_DEBUG_DRAW_VERT_MAX
#define DRW_ATTRIBUTE_PER_CURVES_MAX
constexpr T clamp(T, U, U) RET
VecBase< uint32_t, 4 > uint4
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
uint4 is_point_attribute[DRW_ATTRIBUTE_PER_CURVES_MAX]
DRWDebugVertPair verts[DRW_DEBUG_DRAW_VERT_MAX]
float4 bounding_corners[4]
eObjectInfoFlag flag
packed_float3 orco_add
float shadow_terminator_normal_offset
uint light_and_shadow_set_membership
float shadow_terminator_geometry_offset
packed_float3 orco_mul
FrustumPlanes frustum_planes
FrustumCorners frustum_corners
float4x4 grids_xform[DRW_GRID_PER_VOLUME_MAX]