Blender V4.3
draw_manager_c.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2016 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9/* Private functions / structs of the draw manager */
10
11#pragma once
12
13#include "DRW_engine.hh"
14#include "DRW_render.hh"
15
16#include "BLI_assert.h"
17#include "BLI_linklist.h"
18#include "BLI_memblock.h"
19#include "BLI_task.h"
20#include "BLI_threads.h"
21
22#include "GPU_batch.hh"
23#include "GPU_context.hh"
24#include "GPU_drawlist.hh"
25#include "GPU_framebuffer.hh"
26#include "GPU_shader.hh"
27#include "GPU_uniform_buffer.hh"
28#include "GPU_viewport.hh"
29
30#include "draw_instance_data.hh"
31#include "draw_shader_shared.hh"
32
33struct DRWDebugModule;
34struct DRWTexturePool;
35struct DRWUniformChunk;
36struct DupliObject;
37struct Object;
38namespace blender::draw {
39struct CurvesUniformBufPool;
40struct DRW_Attributes;
41struct DRW_MeshCDMask;
42} // namespace blender::draw
43struct GPUMaterial;
44
46#define USE_GPU_SELECT
47
49#define USE_BATCHING 1
50
51// #define DRW_DEBUG_CULLING
52#define DRW_DEBUG_USE_UNIFORM_NAME 0
53#define DRW_UNIFORM_BUFFER_NAME 64
54
55/* -------------------------------------------------------------------- */
59#define USE_PROFILE
60
61#ifdef USE_PROFILE
62# include "BLI_time.h"
63
64# define PROFILE_TIMER_FALLOFF 0.04
65
66# define PROFILE_START(time_start) \
67 double time_start = BLI_time_now_seconds(); \
68 ((void)0)
69
70# define PROFILE_END_ACCUM(time_accum, time_start) \
71 { \
72 time_accum += (BLI_time_now_seconds() - time_start) * 1e3; \
73 } \
74 ((void)0)
75
76/* exp average */
77# define PROFILE_END_UPDATE(time_update, time_start) \
78 { \
79 double _time_delta = (BLI_time_now_seconds() - time_start) * 1e3; \
80 time_update = (time_update * (1.0 - PROFILE_TIMER_FALLOFF)) + \
81 (_time_delta * PROFILE_TIMER_FALLOFF); \
82 } \
83 ((void)0)
84
85#else /* USE_PROFILE */
86
87# define PROFILE_START(time_start) (() 0)
88# define PROFILE_END_ACCUM(time_accum, time_start) (() 0)
89# define PROFILE_END_UPDATE(time_update, time_start) (() 0)
90
91#endif /* USE_PROFILE */
92
95/* -------------------------------------------------------------------- */
103 void /*DRWRegisteredDrawEngine*/ *next, *prev;
106 int index;
108
117 /* Culling: Using Bounding Sphere for now for faster culling.
118 * Not ideal for planes. Could be extended. */
120 /* Grrr only used by EEVEE. */
122};
123
124/* Minimum max UBO size is 64KiB. We take the largest
125 * UBO struct and alloc the max number.
126 * `((1 << 16) / sizeof(DRWObjectMatrix)) = 512`
127 * Keep in sync with `common_view_lib.glsl`. */
128#define DRW_RESOURCE_CHUNK_LEN 512
129
145
147{
148 return (*handle & 0x80000000) != 0;
149}
150
152{
153 return (*handle & 0x7FFFFFFF) >> 9;
154}
155
157{
158 return (*handle & 0x000001FF);
159}
160
162{
163 *handle += 1;
164}
165
167{
168 *handle |= 0x80000000;
169}
170
172 const DRWResourceHandle *handle)
173{
174 int elem = DRW_handle_id_get(handle);
175 int chunk = DRW_handle_chunk_get(handle);
176 return BLI_memblock_elem_get(memblock, chunk, elem);
177}
178
180 float model[4][4];
181 float modelinverse[4][4];
182};
183
185 float orcotexfac[2][4];
186 float ob_color[4];
187 float ob_index;
188 float pad; /*UNUSED*/
190 float ob_flag; /* Sign is negative scaling. */
191};
192
195
196typedef enum {
197 /* Draw Commands */
198 DRW_CMD_DRAW = 0, /* Only sortable type. Must be 0. */
204
205 /* Compute Commands. */
209
210 /* Other Commands */
216 /* Needs to fit in 4bits */
218
219#define DRW_MAX_DRAW_CMD_TYPE DRW_CMD_DRAW_INDIRECT
220
222 blender::gpu::Batch *batch;
224};
225
226/* Assume DRWResourceHandle to be 0. */
233
240
247
249 blender::gpu::Batch *batch;
251 GPUStorageBuf *indirect_buf;
252};
253
259
263
265 GPUStorageBuf *indirect_buf;
266};
267
271
277
283
289
294
297 uchar r, g, b, a; /* [0..1] for each channels. Normalized. */
298 float depth; /* [0..1] for depth. Normalized. */
299 uchar stencil; /* Stencil value [0..255] */
300};
301
318
325
327/* TODO(@jbakker): rename to DRW_RESOURCE/DRWResourceType. */
360
362 union {
363 /* For reference or array/vector types. */
364 const void *pvalue;
365 /* DRW_UNIFORM_TEXTURE */
366 struct {
367 union {
368 GPUTexture *texture;
369 GPUTexture **texture_ref;
370 };
372 };
373 /* DRW_UNIFORM_BLOCK */
374 union {
375 GPUUniformBuf *block;
376 GPUUniformBuf **block_ref;
377 };
378 /* DRW_UNIFORM_STORAGE_BLOCK */
379 union {
380 GPUStorageBuf *ssbo;
381 GPUStorageBuf **ssbo_ref;
382 };
383 /* DRW_UNIFORM_VERTEX_BUFFER_AS_STORAGE */
384 union {
387 };
388 /* DRW_UNIFORM_FLOAT_COPY */
389 float fvalue[4];
390 /* DRW_UNIFORM_INT_COPY */
391 int ivalue[4];
392 /* DRW_UNIFORM_BLOCK_OBATTRS */
394 };
395 int location; /* Uniform location or binding point for textures and UBO's. */
396 uint8_t type; /* #DRWUniformType */
397 uint8_t length; /* Length of vector types. */
398 uint8_t arraysize; /* Array size of scalar/vector types. */
399};
400
403
404 GPUShader *shader; /* Shader to bind */
405 DRWUniformChunk *uniforms; /* Uniforms pointers */
406
407 struct {
408 /* Chunks of draw calls. */
411
412 union {
413 /* This struct is used during cache populate. */
414 struct {
415 int objectinfo; /* Equal to 1 if the shader needs obinfos. */
416 DRWResourceHandle pass_handle; /* Memblock key to parent pass. */
417
418 /* Set of uniform attributes used by this shader. */
420 };
421 /* This struct is used after cache populate if using the Z sorting.
422 * It will not conflict with the above struct. */
423 struct {
424 float distance; /* Distance from camera. */
425 uint original_index; /* Original position inside the shgroup list. */
426 } z_sorting;
427 };
428};
429
430#define MAX_PASS_NAME 32
431
432struct DRWPass {
433 /* Linked list */
434 struct {
438
439 /* Draw the shgroups of this pass instead.
440 * This avoid duplicating drawcalls/shgroups
441 * for similar passes. */
443 /* Link list of additional passes to render. */
445
448 char name[MAX_PASS_NAME];
449};
450
451#define MAX_CULLED_VIEWS 32
452
485/* Needed to assert that alignment is the same in C++ and C. */
487
490/* -------------------------------------------------------------------- */
507
512 /* 4bits for each command. */
514 /* -- 64 bytes aligned -- */
516 /* -- 64 bytes aligned -- */
517};
518
523 /* 4bits for each command. */
524 /* TODO: reduce size of command_type. */
527};
528
529/* Only true for 64-bit platforms. */
530#ifdef __LP64__
532#endif
533
536/* -------------------------------------------------------------------- */
579
582/* -------------------------------------------------------------------- */
590
591#define DST_MAX_SLOTS 64 /* Cannot be changed without modifying RST.bound_tex_slots */
592#define MAX_CLIP_PLANES 6 /* GL_MAX_CLIP_PLANES is at least 6 */
593#define STENCIL_UNDEFINED 256
594#define DRW_DRAWLIST_LEN 256
596 /* TODO: clean up this struct a bit. */
597 /* Cache generation */
598 /* TODO(@fclem): Rename to data. */
602 /* State of the object being evaluated if already allocated. */
610
623 /* Dupli data for the current dupli for each enabled engine. */
625
626 /* Rendering state */
628 blender::gpu::Batch *batch;
629
630 /* Managed by `DRW_state_set`, `DRW_state_reset` */
633
634 /* Per viewport */
636 GPUFrameBuffer *default_framebuffer;
637 float size[2];
638 float inv_size[2];
639 float pixsize;
640
641 struct {
650
651 /* Current rendering context */
653
654 /* Convenience pointer to text_store owned by the viewport */
656
657 bool buffer_finish_called; /* Avoid bad usage of DRW_render_instance_buffer_finish */
658
661
666
667#ifdef USE_GPU_SELECT
669#endif
670
672 /* Contains list of objects that needs to be extracted from other objects. */
674
675 /* ---------- Nothing after this point is cleared after use ----------- */
676
677 /* system_gpu_context serves as the offset for clearing only
678 * the top portion of the struct so DO NOT MOVE IT! */
684
685 GPUDrawList *draw_list;
686
687 DRWDebugModule *debug;
688};
689
690extern DRWManager DST; /* TODO: get rid of this and allow multi-threaded rendering. */
691
694/* -------------------------------------------------------------------- */
698void drw_texture_set_parameters(GPUTexture *tex, DRWTextureFlag flags);
699
700void *drw_viewport_engine_data_ensure(void *engine_type);
701
703
704void drw_debug_draw();
705void drw_debug_init();
706void drw_debug_module_free(DRWDebugModule *module);
707GPUStorageBuf *drw_debug_gpu_draw_buf_get();
708GPUStorageBuf *drw_debug_gpu_print_buf_get();
709
710eDRWCommandType command_type_get(const uint64_t *command_type_bits, int index);
711
714
720
721void drw_resource_buffer_finish(DRWData *vmempool);
722
723/* Procedural Drawing */
724blender::gpu::Batch *drw_cache_procedural_points_get();
725blender::gpu::Batch *drw_cache_procedural_lines_get();
726blender::gpu::Batch *drw_cache_procedural_triangles_get();
727blender::gpu::Batch *drw_cache_procedural_triangle_strips_get();
728
730 const GPUUniformAttrList *key,
731 DRWResourceHandle *handle,
732 const Object *ob,
733 const Object *dupli_parent,
734 const DupliObject *dupli_source);
735
736GPUUniformBuf *drw_ensure_layer_attribute_buffer();
737
739void *drw_engine_data_engine_data_create(GPUViewport *viewport, void *engine_type);
740void *drw_engine_data_engine_data_get(GPUViewport *viewport, void *engine_handle);
741bool drw_engine_data_engines_data_validate(GPUViewport *viewport, void **engine_handle_array);
744
745namespace blender::draw {
746
747void DRW_mesh_get_attributes(const Object &object,
748 const Mesh &mesh,
749 const GPUMaterial *const *gpumat_array,
750 int gpumat_array_len,
751 DRW_Attributes *r_attrs,
752 DRW_MeshCDMask *r_cd_needed);
753
754} // namespace blender::draw
755
758
#define BLI_STATIC_ASSERT_ALIGN(st, align)
Definition BLI_assert.h:90
#define BLI_INLINE
struct GSet GSet
Definition BLI_ghash.h:341
void * BLI_memblock_elem_get(BLI_memblock *mblk, int chunk, int elem) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
unsigned char uchar
unsigned int uint
Platform independent time functions.
DRWTextureFlag
bool DRWCallVisibilityFn(bool vis_in, void *user_data)
eGPUFrameBufferBits
eGPUBarrier
Definition GPU_state.hh:29
struct GPUContext GPUContext
struct GPUShader GPUShader
#define MAX_INSTANCE_DATA_SIZE
void drw_debug_init()
BLI_INLINE void * DRW_memblock_elem_from_handle(BLI_memblock *memblock, const DRWResourceHandle *handle)
GPUStorageBuf * drw_debug_gpu_print_buf_get()
void drw_texture_set_parameters(GPUTexture *tex, DRWTextureFlag flags)
void drw_resource_buffer_finish(DRWData *vmempool)
DRWUniformType
@ DRW_UNIFORM_VERTEX_BUFFER_AS_TEXTURE_REF
@ DRW_UNIFORM_BLOCK_OBINFOS
@ DRW_UNIFORM_TFEEDBACK_TARGET
@ DRW_UNIFORM_TEXTURE_REF
@ DRW_UNIFORM_MODEL_MATRIX
@ DRW_UNIFORM_VERTEX_BUFFER_AS_STORAGE
@ DRW_UNIFORM_FLOAT_COPY
@ DRW_UNIFORM_MODEL_MATRIX_INVERSE
@ DRW_UNIFORM_FLOAT
@ DRW_UNIFORM_VERTEX_BUFFER_AS_STORAGE_REF
@ DRW_UNIFORM_BASE_INSTANCE
@ DRW_UNIFORM_BLOCK_OBMATS
@ DRW_UNIFORM_IMAGE_REF
@ DRW_UNIFORM_RESOURCE_ID
@ DRW_UNIFORM_BLOCK
@ DRW_UNIFORM_TEXTURE
@ DRW_UNIFORM_STORAGE_BLOCK_REF
@ DRW_UNIFORM_VERTEX_BUFFER_AS_TEXTURE
@ DRW_UNIFORM_RESOURCE_CHUNK
@ DRW_UNIFORM_IMAGE
@ DRW_UNIFORM_BLOCK_OBATTRS
@ DRW_UNIFORM_STORAGE_BLOCK
@ DRW_UNIFORM_BLOCK_VLATTRS
@ DRW_UNIFORM_INT
@ DRW_UNIFORM_BLOCK_REF
@ DRW_UNIFORM_INT_COPY
void DRW_manager_end_sync()
BLI_INLINE uint32_t DRW_handle_chunk_get(const DRWResourceHandle *handle)
void drw_batch_cache_generate_requested_evaluated_mesh_or_curve(Object *ob)
eDRWCommandType command_type_get(const uint64_t *command_type_bits, int index)
double * drw_engine_data_cache_time_get(GPUViewport *viewport)
struct DRWRegisteredDrawEngine DRWRegisteredDrawEngine
void * drw_viewport_engine_data_ensure(void *engine_type)
bool drw_engine_data_engines_data_validate(GPUViewport *viewport, void **engine_handle_array)
void drw_state_set(DRWState state)
void drw_debug_module_free(DRWDebugModule *module)
void * drw_engine_data_engine_data_create(GPUViewport *viewport, void *engine_type)
blender::gpu::Batch * drw_cache_procedural_lines_get()
void drw_batch_cache_generate_requested(Object *ob)
DRWManager DST
blender::gpu::Batch * drw_cache_procedural_triangles_get()
GPUUniformBuf * drw_ensure_layer_attribute_buffer()
eDRWCommandType
@ DRW_CMD_COMPUTE_INDIRECT
@ DRW_CMD_COMPUTE
@ DRW_CMD_COMPUTE_REF
@ DRW_CMD_DRAW
@ DRW_CMD_DRWSTATE
@ DRW_CMD_DRAW_RANGE
@ DRW_CMD_CLEAR
@ DRW_CMD_BARRIER
@ DRW_CMD_STENCIL
@ DRW_CMD_DRAW_INSTANCE_RANGE
@ DRW_CMD_DRAW_PROCEDURAL
@ DRW_CMD_DRAW_INDIRECT
@ DRW_CMD_SELECTID
@ DRW_CMD_DRAW_INSTANCE
void drw_batch_cache_validate(Object *ob)
void drw_debug_draw()
BLI_INLINE uint32_t DRW_handle_negative_scale_get(const DRWResourceHandle *handle)
blender::gpu::Batch * drw_cache_procedural_triangle_strips_get()
void drw_batch_cache_generate_requested_delayed(Object *ob)
BLI_INLINE void DRW_handle_increment(DRWResourceHandle *handle)
BLI_INLINE uint32_t DRW_handle_id_get(const DRWResourceHandle *handle)
GPUStorageBuf * drw_debug_gpu_draw_buf_get()
void drw_engine_data_cache_release(GPUViewport *viewport)
blender::gpu::Batch * drw_cache_procedural_points_get()
uint32_t DRWResourceHandle
#define MAX_PASS_NAME
BLI_INLINE void DRW_handle_negative_scale_enable(DRWResourceHandle *handle)
void * drw_engine_data_engine_data_get(GPUViewport *viewport, void *engine_handle)
void drw_engine_data_free(GPUViewport *viewport)
void drw_uniform_attrs_pool_update(GHash *table, const GPUUniformAttrList *key, DRWResourceHandle *handle, const Object *ob, const Object *dupli_parent, const DupliObject *dupli_source)
void DRW_manager_begin_sync()
DRWState
Definition draw_state.hh:25
static ulong state[N]
void DRW_mesh_get_attributes(const Object &object, const Mesh &mesh, const GPUMaterial *const *gpumat_array, int gpumat_array_len, DRW_Attributes *r_attrs, DRW_MeshCDMask *r_cd_needed)
static struct PyModuleDef module
Definition python.cpp:991
unsigned int uint32_t
Definition stdint.h:80
unsigned char uint8_t
Definition stdint.h:78
unsigned __int64 uint64_t
Definition stdint.h:90
blender::gpu::VertBuf * buf
blender::gpu::VertBuf * buf_select
DRWCommandChunk * next
DRWCommand commands[96]
uint64_t command_type[6]
eGPUFrameBufferBits clear_channels
blender::gpu::Batch * batch
DRWResourceHandle handle
GPUStorageBuf * indirect_buf
blender::gpu::Batch * batch
DRWResourceHandle handle
blender::gpu::Batch * batch
blender::gpu::Batch * batch
blender::gpu::Batch * batch
DRWResourceHandle handle
DRWResourceHandle handle
blender::gpu::Batch * batch
blender::gpu::VertBuf * select_buf
DRWCommandChunk * next
BoundSphere bsphere
ListBase smoke_textures
BLI_memblock * views
BLI_memblock * uniforms
void * volume_grids_ubos
GPUUniformBuf ** obinfos_ubo
BLI_memblock * cullstates
BLI_memblock * passes
GHash * vlattrs_name_cache
DRWInstanceDataList * idatalist
ListBase vlattrs_name_list
BLI_memblock * commands
GPUUniformBuf * vlattrs_ubo
GHash * obattrs_ubo_pool
BLI_memblock * images
BLI_memblock * callbuffers
BLI_memblock * obmats
DRWTexturePool * texture_pool
BLI_memblock * commands_small
LayerAttribute * vlattrs_buf
bool vlattrs_ubo_ready
blender::draw::CurvesUniformBufPool * curves_ubos
GPUUniformBuf ** matrices_ubo
DRWViewData * view_data[2]
BLI_memblock * obinfos
BLI_memblock * shgroups
DRWResourceHandle ob_handle
TaskGraph * task_graph
DRWResourceHandle pass_handle
DRWView * view_active
GSet * delayed_extraction
DRWContextState draw_ctx
DRWView * view_previous
bool buffer_finish_called
DRWResourceHandle resource_handle
DRWTextStore ** text_store_p
float inv_size[2]
DRWDebugModule * debug
void ** dupli_datas
GPUViewport * viewport
GPUShader * shader
Object * dupli_origin
GPUContext * blender_gpu_context
DupliObject * dupli_source
DRWInstanceData * object_instance_data[MAX_INSTANCE_DATA_SIZE]
GPUFrameBuffer * default_framebuffer
bool ob_state_obinfo_init
uint is_material_select
struct DRWManager::@295 options
GPUDrawList * draw_list
TicketMutex * system_gpu_context_mutex
DRWData * vmempool
GHash * dupli_ghash
DRWState state_lock
DRWView * view_default
blender::gpu::Batch * batch
Object * dupli_parent
void * system_gpu_context
DRWViewData * view_data_active
float orcotexfac[2][4]
float modelinverse[4][4]
DRWResourceHandle handle
DRWState state
DRWShadingGroup * last
DRWShadingGroup * first
DRWPass * original
struct DRWPass::@294 shgroups
DRWPass * next
DrawEngineType * draw_engine
struct DRWCommandChunk * first
const struct GPUUniformAttrList * uniform_attrs
DRWResourceHandle pass_handle
DRWShadingGroup * next
struct DRWShadingGroup::@288 cmd
struct DRWCommandChunk * last
DRWUniformChunk * uniforms
DRWUniform uniforms[10]
DRWUniformChunk * next
blender::gpu::VertBuf * vertbuf
GPUStorageBuf ** ssbo_ref
uint8_t arraysize
GPUStorageBuf * ssbo
GPUTexture ** texture_ref
GPUUniformBuf ** block_ref
GPUUniformBuf * block
GPUSamplerState sampler_state
GPUTexture * texture
float fvalue[4]
const struct GPUUniformAttrList * uniform_attrs
const void * pvalue
blender::gpu::VertBuf ** vertbuf_ref
float4x4 persinv
float4x4 persmat
float4 clip_planes[6]
ViewMatrices storage
int clip_planes_len
DRWCallVisibilityFn * visibility_fn
uint32_t culling_mask
BoundBox frustum_corners
BoundSphere frustum_bsphere
void * user_data
float frustum_planes[6][4]
DRWView * parent
Object * ob
Definition DNA_ID.h:413
DRWCommandComputeIndirect compute_indirect
DRWCommandSetStencil stencil
DRWCommandComputeRef compute_ref
DRWCommandDraw draw
DRWCommandDrawInstance instance
DRWCommandDrawRange range
DRWCommandSetMutableState state
DRWCommandCompute compute
DRWCommandBarrier barrier
DRWCommandDrawInstanceRange instance_range
DRWCommandClear clear
DRWCommandDrawIndirect draw_indirect
DRWCommandDrawProcedural procedural
DRWCommandSetSelectID select_id