Blender V5.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
8
9#pragma once
10
12
13#ifndef GPU_SHADER
14# include "BLI_math_vector.hh"
15# include "GPU_shader.hh"
16# include "draw_defines.hh"
17
18# if defined(__cplusplus) && !defined(GPU_SHADER)
19
20/* Defined on windows in `windows.h`. */
21# undef far
22# undef near
23
24namespace blender {
25# endif
26
27struct ViewCullingData;
28struct ViewMatrices;
29struct ObjectMatrices;
30struct ObjectInfos;
31struct ObjectBounds;
32struct VolumeInfos;
33struct GSplatInfos;
34struct CurvesInfos;
35struct ObjectAttribute;
36struct LayerAttribute;
37struct DrawCommand;
38struct DispatchCommand;
39struct DRWDebugPrintBuffer;
40struct DRWDebugVertPair;
42struct FrustumCorners;
43struct FrustumPlanes;
44
45/* __cplusplus is true when compiling with MSL. */
46# if defined(__cplusplus) && !defined(GPU_SHADER)
47/* C++ only forward declarations. */
48struct Object;
49struct Scene;
50struct ViewLayer;
51struct GPUUniformAttr;
52struct GPULayerAttr;
53
54namespace draw {
55
56class ObjectRef;
57
58} // namespace draw
59
60using namespace math;
61
62# endif
63#endif
64
65#define DRW_SHADER_SHARED_H
66
67[[maybe_unused]] static constexpr int DRW_RESOURCE_CHUNK_LEN = 512;
68
69/* Define the maximum number of grid we allow in a volume UBO. */
70[[maybe_unused]] static constexpr int DRW_GRID_PER_VOLUME_MAX = 16;
71
72/* Define the maximum number of attribute we allow in a curves UBO.
73 * This should be kept in sync with `GPU_ATTR_MAX` */
74[[maybe_unused]] static constexpr int DRW_ATTRIBUTE_PER_CURVES_MAX = 15;
75
76/* -------------------------------------------------------------------- */
79
80/* NOTE: Legacy, should be removed after we port everything to BSL. */
81
83[[maybe_unused]] static uint drw_view_id = 0;
84
85#if !defined(DRW_VIEW_LEN) && !defined(GLSL_CPP_STUBS)
86/* Single-view case (default). */
87# define DRW_VIEW_LEN 1
88# define DRW_VIEW_SHIFT 0
89# define DRW_VIEW_FROM_RESOURCE_ID
90#else
91
92/* Multi-view case. */
94// # define DRW_VIEW_LEN 64
100# define DRW_VIEW_SHIFT \
101 ((DRW_VIEW_LEN > 32) ? 6 : \
102 (DRW_VIEW_LEN > 16) ? 5 : \
103 (DRW_VIEW_LEN > 8) ? 4 : \
104 (DRW_VIEW_LEN > 4) ? 3 : \
105 (DRW_VIEW_LEN > 2) ? 2 : \
106 1)
107# define DRW_VIEW_MASK ~(0xFFFFFFFFu << DRW_VIEW_SHIFT)
108# define DRW_VIEW_FROM_RESOURCE_ID drw_view_id = (drw_resource_id_raw() & DRW_VIEW_MASK)
109#endif
110
114
116 /* [0] left
117 * [1] right
118 * [2] bottom
119 * [3] top
120 * [4] near
121 * [5] far */
123};
124
132
138
139 /* Returns true if the current view has a perspective projection matrix. */
140 bool is_perspective() const
141 {
142 return winmat[3][3] == 0.0f;
143 }
144
145 /* Returns the view forward vector, going towards the viewer. */
147 {
148 return viewinv[2].xyz();
149 }
150
151 /* Returns the view up vector. */
152 float3 up() const
153 {
154 return viewinv[1].xyz();
155 }
156
157 /* Returns the view origin. */
159 {
160 return viewinv[3].xyz();
161 }
162
163 /* Positive Z distance from the view origin. Faster than using `drw_point_world_to_view`. */
164 float z_distance(float3 P) const
165 {
166 return dot(P - position(), -forward());
167 }
168
169 /* Returns the projection matrix far clip distance. */
170 float far() const
171 {
172 if (is_perspective()) {
173 return -winmat[3][2] / (winmat[2][2] + 1.0f);
174 }
175 return -(winmat[3][2] - 1.0f) / winmat[2][2];
176 }
177
178 /* Returns the projection matrix near clip distance. */
179 float near() const
180 {
181 if (is_perspective()) {
182 return -winmat[3][2] / (winmat[2][2] - 1.0f);
183 }
184 return -(winmat[3][2] + 1.0f) / winmat[2][2];
185 }
186
192 {
193 return is_perspective() ? normalize(position() - P) : forward();
194 }
195
201 {
202 return is_perspective() ? normalize(-vP) : float3(0.0f, 0.0f, 1.0f);
203 }
204
208 template<typename T> T screen_to_ndc(T ss_P) const
209 {
210 return ss_P * 2.0f - 1.0f;
211 }
212
216 template<typename T> T ndc_to_screen(T ndc_P) const
217 {
218 return ndc_P * 0.5f + 0.5f;
219 }
220
221 /* -------------------------------------------------------------------- */
224
225#ifdef GPU_SHADER /* `to_float3x3` doesn't exist on CPU side. */
226 float3 normal_view_to_world(float3 vN) const
227 {
228 return (to_float3x3(viewinv) * vN);
229 }
230
231 float3 normal_world_to_view(float3 N) const
232 {
233 return (to_float3x3(viewmat) * N);
234 }
235#endif
236
238
239 /* -------------------------------------------------------------------- */
242
244 {
245 return hs_P.xyz() / hs_P.w;
246 }
247
249 {
250 return (viewinv * float4(vP, 1.0f)).xyz();
251 }
252
254 {
255 return (winmat * float4(vP, 1.0f));
256 }
257
259 {
260 return (wininv * hP).xyz();
261 }
262
267
269 {
270 return (viewmat * float4(P, 1.0f)).xyz();
271 }
272
274 {
275 return (winmat * (viewmat * float4(P, 1.0f)));
276 }
277
282
287
289 {
290 return perspective_divide(wininv * float4(ssP, 1.0f));
291 }
292
294 {
296 }
297
299
300 /* -------------------------------------------------------------------- */
303
305 {
307 }
308
313
315 {
316 return point_ndc_to_view(screen_to_ndc(ssP));
317 }
318
323
324 float depth_view_to_screen(float v_depth) const
325 {
326 return point_view_to_screen(float3(0.0f, 0.0f, v_depth)).z;
327 }
328
329 float depth_screen_to_view(float ss_depth) const
330 {
331 return point_screen_to_view(float3(0.0f, 0.0f, ss_depth)).z;
332 }
333
335};
336
337template float ViewMatrices::screen_to_ndc<float>(float ss_P) const;
340template float ViewMatrices::ndc_to_screen<float>(float ss_P) const;
343
345
346/* -------------------------------------------------------------------- */
349
353
354#ifndef GPU_SHADER
355 void sync(const Object &object);
356 void sync(const float4x4 &model_matrix);
357#endif
358
359#ifdef GPU_SHADER /* `to_float3x3` doesn't exist on CPU side. */
372 float3x3 normal() const
373 {
374 return transpose(to_float3x3(model_inverse));
375 }
376 float3x3 normal_inverse() const
377 {
378 return transpose(to_float3x3(model));
379 }
380#endif
381
382 /* -------------------------------------------------------------------- */
387
388#ifdef GPU_SHADER /* `to_float3x3` doesn't exist on CPU side. */
389 float3 normal_object_to_world(float3 lN) const
390 {
391 return normal() * lN;
392 }
393
394 float3 normal_world_to_object(float3 N) const
395 {
396 return normal_inverse() * N;
397 }
398
399 float3 normal_object_to_view(ViewMatrices view, float3 lN) const
400 {
401 return to_float3x3(view.viewmat) * (normal() * lN);
402 }
403
404 float3 normal_view_to_object(ViewMatrices view, float3 vN) const
405 {
406 return normal_inverse() * (to_float3x3(view.viewinv) * vN);
407 }
408#endif
409
411
412 /* -------------------------------------------------------------------- */
417
419 {
420 return (model * float4(lP, 1.0f)).xyz();
421 }
422
424 {
425 return (model_inverse * float4(P, 1.0f)).xyz();
426 }
427
429 {
430 return (view.viewmat * (model * float4(lP, 1.0f))).xyz();
431 }
432
434 {
435 return (model_inverse * (view.viewinv * float4(vP, 1.0f))).xyz();
436 }
437
439 {
440 return view.winmat * (view.viewmat * (model * float4(lP, 1.0f)));
441 }
442
444 {
445 return view.perspective_divide(point_object_to_homogenous(view, lP));
446 }
447
449};
450
452 OBJECT_SELECTED = (1u << 0u),
453 OBJECT_FROM_DUPLI = (1u << 1u),
454 OBJECT_FROM_SET = (1u << 2u),
455 OBJECT_ACTIVE = (1u << 3u),
457 OBJECT_HOLDOUT = (1u << 5u),
458 /* Implies all objects that match the current active object's mode and able to be edited
459 * simultaneously. Currently only applicable for edit mode. */
461 /* Avoid skipped info to change culling. */
463};
464
465#ifndef GPU_SHADER
467#endif
468
475
480 float random;
484 float _pad1;
485 float _pad2;
486
487#ifndef GPU_SHADER
488 void sync();
489 void sync(const draw::ObjectRef ref, bool is_active_object, bool is_active_edit_mode);
490#endif
491};
492
494{
495 return object_infos.light_and_shadow_set_membership & 0xFFu;
496}
497
499{
500 return (object_infos.light_and_shadow_set_membership >> 8u) & 0xFFu;
501}
502
513 {
514 return bounding_corners[3].w;
515 }
516 void inner_sphere_radius_set(float value)
517 {
518 bounding_corners[3].w = value;
519 }
520
521#ifndef GPU_SHADER
522 void sync();
523 void sync(const Object &ob, float inflate_bounds = 0.0f);
524 void sync(const float3 &center, const float3 &size);
525#endif
526};
527
528/* Return true if `bounding_corners` are valid. Should be checked before accessing them.
529 * Does not guarantee that `bounding_sphere` is valid.
530 * Converting these bounds to an `IsectBox` may generate invalid clip planes.
531 * For safe `IsectBox` generation check `drw_bounds_are_valid`. */
533{
534 return bounds.bounding_sphere.w != -1.0f;
535}
536
537/* Return true if bounds are ready for culling.
538 * In this case, both `bounding_corners` and `bounding_sphere` are valid.
539 * These bounds can be safely converted to an `IsectBox` with valid clip planes. */
541{
542 return bounds.bounding_sphere.w >= 0.0f;
543}
544
546
547/* -------------------------------------------------------------------- */
550
551/* Struct to scale a value from [0, 1] to a range [M, N]. Can be used for
552 * quantized packing if the value's boundaries are known. */
555 float _pad0;
557 float _pad1;
558
561 {
562 return value * range_mul + range_add;
563 }
564
565#if !defined(GPU_SHADER)
568 {
569 float3 reciprocal_denom;
570 for (int i = 0; i < 3; ++i) {
571 reciprocal_denom[i] = range_mul[i] == 0.0f ? 1.0f : (1.0f / range_mul[i]);
572 }
573 return math::clamp((value - range_add) * reciprocal_denom, float3(0.0f), float3(1.0f));
574 }
575#endif
576};
577
579
580/* -------------------------------------------------------------------- */
583
591
594 float4x4 grids_xform[16 /*DRW_GRID_PER_VOLUME_MAX*/];
600 float _pad;
601};
602
604 /* Attribute domain. One bit per attribute. */
606 /* Number of vertex in a segment (including restart vertex for cylinder). */
608 /* Edge count for the visible half cylinder. Equal to face count + 1. */
611};
612
613#pragma pack(push, 4)
615 /* Workaround the padding cost from alignment requirements.
616 * (see GL spec : 7.6.2.2 Standard Uniform Block Layout) */
617 float data_x;
618 float data_y;
619 float data_z;
620 float data_w;
622
623#ifndef GPU_SHADER
631 bool sync(const draw::ObjectRef &ref, const GPUUniformAttr &attr, int instance_index);
632 bool sync(const draw::ObjectRef &ref, const char *name, bool use_dupli, int instance_index);
633#endif
634};
635#pragma pack(pop)
640#ifndef GPU_SHADER
642#endif
643
647 uint buffer_length; /* Only in the first record. */
650
651#ifndef GPU_SHADER
652 bool sync(const Scene *scene, const ViewLayer *layer, const GPULayerAttr &attr);
653#endif
654};
655
657
658#if !defined(GPU_SHADER)
659} // namespace blender
660#endif
#define ENUM_OPERATORS(_enum_type)
#define host_shared
#define BLI_STATIC_ASSERT_ALIGN(st, align)
Definition scene.h:133
bool drw_bounds_corners_are_valid(ObjectBounds bounds)
bool drw_bounds_are_valid(ObjectBounds bounds)
uint blocker_shadow_set_get(ObjectInfos object_infos)
static uint drw_view_id
uint receiver_light_set_get(ObjectInfos object_infos)
static constexpr int DRW_RESOURCE_CHUNK_LEN
@ OBJECT_NO_INFO
@ OBJECT_SELECTED
@ OBJECT_FROM_DUPLI
@ OBJECT_ACTIVE
@ OBJECT_ACTIVE_EDIT_MODE
@ OBJECT_FROM_SET
@ OBJECT_HOLDOUT
@ OBJECT_NEGATIVE_SCALE
static constexpr int DRW_ATTRIBUTE_PER_CURVES_MAX
static constexpr int DRW_GRID_PER_VOLUME_MAX
#define instance_index
#define layer
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
MatBase< R, C > transpose(MatBase< C, R >) RET
#define N
#define T
T clamp(const T &a, const T &min, const T &max)
BMesh Iterators.
unsigned int uint
Definition BKE_text.h:55
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
unsigned int uint32_t
Definition BKE_text.h:81
VecBase< float, 2 > float2
VecBase< float, 3 > float3
struct ScalingRange splat_mean_range
struct ScalingRange splat_scale_range
struct ScalingRange radiance_base_range
struct ScalingRange radiance_sh_range
bool sync(const Scene *scene, const ViewLayer *layer, const GPULayerAttr &attr)
bool sync(const draw::ObjectRef &ref, const GPUUniformAttr &attr, int instance_index)
float4 bounding_corners[4]
float inner_sphere_radius() const
void inner_sphere_radius_set(float value)
enum 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
float3 point_view_to_object(ViewMatrices view, float3 vP) const
float3 point_object_to_view(ViewMatrices view, float3 lP) const
float3 point_world_to_object(float3 P) const
float3 point_object_to_world(float3 lP) const
float3 point_object_to_ndc(ViewMatrices view, float3 lP) const
float4 point_object_to_homogenous(ViewMatrices view, float3 lP) const
void sync(const Object &object)
packed_float3 range_mul
float3 scale_to_unit(float3 value) const
float3 scale_from_unit(float3 value) const
packed_float3 range_add
struct FrustumPlanes frustum_planes
struct FrustumCorners frustum_corners
bool is_perspective() const
float3 point_homogeneous_to_view(float4 hP) const
float3 point_world_to_view(float3 P) const
float3 forward() const
float3 perspective_divide(float4 hs_P) const
float4 point_view_to_homogenous(float3 vP) const
float depth_screen_to_view(float ss_depth) const
T ndc_to_screen(T ndc_P) const
float near() const
float4 point_world_to_homogenous(float3 P) const
float3 point_ndc_to_world(float3 ssP) const
float3 point_view_to_ndc(float3 vP) const
float3 point_screen_to_world(float3 ssP) const
T screen_to_ndc(T ss_P) const
float far() const
float3 view_incident_vector(float3 vP) const
float z_distance(float3 P) const
float depth_view_to_screen(float v_depth) const
float3 up() const
float3 point_world_to_ndc(float3 P) const
float3 position() const
float3 point_homogeneous_to_world(float4 hP) const
float3 point_view_to_world(float3 vP) const
float3 point_view_to_screen(float3 vP) const
float3 world_incident_vector(float3 P) const
float3 point_ndc_to_view(float3 ssP) const
float3 point_screen_to_view(float3 ssP) const
float3 point_world_to_screen(float3 vP) const
float4x4 grids_xform[16]
float w
Definition sky_math.h:225