Blender V5.0
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#ifndef GPU_SHADER
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
41class 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#ifndef GPU_SHADER
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#ifndef GPU_SHADER
160#endif
161
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#ifndef GPU_SHADER
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
252 /* TODO(fclem): Make it a single uint. */
257
258 /* Number of vertex in a segment (including restart vertex for cylinder). */
260 /* Edge count for the visible half cylinder. Equal to face count + 1. */
264};
266
267#pragma pack(push, 4)
269 /* Workaround the padding cost from alignment requirements.
270 * (see GL spec : 7.6.2.2 Standard Uniform Block Layout) */
273
274#ifndef GPU_SHADER
279 bool sync(const blender::draw::ObjectRef &ref, const GPUUniformAttr &attr);
280#endif
281};
282#pragma pack(pop)
286
287#pragma pack(push, 4)
291 uint buffer_length; /* Only in the first record. */
293
294#ifndef GPU_SHADER
295 bool sync(const Scene *scene, const ViewLayer *layer, const GPULayerAttr &attr);
296#endif
297};
298#pragma pack(pop)
300
301
302
303/* -------------------------------------------------------------------- */
306
308 /* TODO(fclem): Rename */
312#if defined(GPU_SHADER)
315# define _instance_first_array base_index
316#else
317 union {
319 /* Use this instead of instance_first_indexed for non indexed draw calls. */
321 };
322#endif
323
325
327};
329
337
338
339
340/* -------------------------------------------------------------------- */
343
345 /* This is a weird layout, but needed to be able to use DRWDebugVertPair as
346 * a DrawCommand and avoid alignment issues. See drw_debug_lines_buf[] definition. */
350 /* Named vert_color to avoid global namespace collision with uniform color. */
352
356 /* Number of time this line is supposed to be displayed. Decremented by one on display. */
358};
360
362 uint in_pos1_y,
363 uint in_pos1_z,
364 uint in_pos2_x,
365 uint in_pos2_y,
366 uint in_pos2_z,
367 uint in_vert_color,
368 uint in_lifetime)
369{
370 DRWDebugVertPair debug_vert;
371 debug_vert.pos1_x = in_pos1_x;
372 debug_vert.pos1_y = in_pos1_y;
373 debug_vert.pos1_z = in_pos1_z;
374 debug_vert.pos2_x = in_pos2_x;
375 debug_vert.pos2_y = in_pos2_y;
376 debug_vert.pos2_z = in_pos2_z;
377 debug_vert.vert_color = in_vert_color;
378 debug_vert.lifetime = in_lifetime;
379 return debug_vert;
380}
381
383{
384 v_color = clamp(v_color, 0.0f, 1.0f);
385 uint result = 0;
386 result |= uint(v_color.x * 255.0) << 0u;
387 result |= uint(v_color.y * 255.0) << 8u;
388 result |= uint(v_color.z * 255.0) << 16u;
389 result |= uint(v_color.w * 255.0) << 24u;
390 return result;
391}
392
393/* Take the header (DrawCommand) into account. */
394#define DRW_DEBUG_DRAW_VERT_MAX (2 * 1024) - 1
395
396/* The debug draw buffer is laid-out as the following struct.
397 * But we use plain array in shader code instead because of driver issues. */
403
404/* Equivalent to `DRWDebugDrawBuffer.command.v_count`. */
405#define drw_debug_draw_v_count(buf) buf[0].pos1_x
410#define drw_debug_draw_offset 1
411
#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]
bool sync(const Scene *scene, const ViewLayer *layer, const GPULayerAttr &attr)
bool sync(const blender::draw::ObjectRef &ref, const GPUUniformAttr &attr)
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
void sync(const Object &object)
FrustumPlanes frustum_planes
FrustumCorners frustum_corners
float4x4 grids_xform[DRW_GRID_PER_VOLUME_MAX]
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225