Blender V5.0
DRW_render.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
8
9/* This is the Render Functions used by Realtime engines to draw with OpenGL */
10
11#pragma once
12
13#include <functional>
14
15#include "BKE_mesh_wrapper.hh"
18#include "DNA_object_enums.h"
19#include "DNA_object_types.h"
20
21#include "GPU_material.hh"
22
23namespace blender::gpu {
24class Batch;
25class Shader;
26class Texture;
27class UniformBuf;
28class FrameBuffer;
29} // namespace blender::gpu
30struct ARegion;
31struct bContext;
32struct Depsgraph;
35struct DupliObject;
36struct GPUMaterial;
37struct Mesh;
38struct Object;
39struct ParticleSystem;
40struct rcti;
41struct RegionView3D;
42struct RenderEngine;
43struct RenderEngineType;
44struct RenderLayer;
45struct RenderResult;
46struct SpaceLink;
47struct TaskGraph;
48struct View3D;
49struct ViewLayer;
50struct DRWContext;
51struct World;
52struct DRWData;
53struct DRWViewData;
54struct GPUViewport;
55struct DRWTextStore;
56struct GSet;
57struct GPUViewport;
58namespace blender::draw {
59class TextureFromPool;
60class ObjectRef;
61class Manager;
62} // namespace blender::draw
63
64/* TODO: Put it somewhere else? */
66 float center[3], radius;
67};
68
69struct DrawEngine {
70 static constexpr int GPU_INFO_SIZE = 512; /* IMA_MAX_RENDER_TEXT_SIZE */
71
72 char info[GPU_INFO_SIZE] = {'\0'};
73
74 bool used = false;
75
76 virtual ~DrawEngine() = default;
77
79
80 /* Functions called for viewport. */
81
83 virtual void init() = 0;
85 virtual void begin_sync() = 0;
86 virtual void object_sync(blender::draw::ObjectRef &ob_ref, blender::draw::Manager &manager) = 0;
87 virtual void end_sync() = 0;
89 virtual void draw(blender::draw::Manager &manager) = 0;
90
91 /* Called when closing blender.
92 * Cleanup all lazily initialized static members that have GPU resources.
93 * Implemented on a case by case basis and called directly. */
94 // static void exit(){};
95
96 struct Pointer {
97 DrawEngine *instance = nullptr;
98
100 {
102 }
103
105 {
106 delete instance;
107 instance = nullptr;
108 }
109
110 void set_used(bool used)
111 {
112 if (used) {
113 if (instance == nullptr) {
115 }
116 instance->used = true;
117 }
118 else if (instance) {
119 instance->used = false;
120 }
121 }
122
124 };
125};
126
127/* Viewport. */
128
136
138
140 RenderEngine *engine,
141 Depsgraph *depsgraph,
142 std::function<void(RenderEngine *, RenderLayer *, const rcti)> render_view_cb,
143 std::function<void(RenderResult *)> store_metadata_cb);
144
146 RenderEngine *engine,
147 Depsgraph *depsgraph,
148 std::function<void(blender::draw::ObjectRef &, RenderEngine *, Depsgraph *)>);
149
150void DRW_render_set_time(RenderEngine *engine, Depsgraph *depsgraph, int frame, float subframe);
151
157void DRW_custom_pipeline_begin(DRWContext &draw_ctx, Depsgraph *depsgraph);
158void DRW_custom_pipeline_end(DRWContext &draw_ctx);
159
164void DRW_cache_restart();
165
166/* Settings. */
167
168bool DRW_object_is_renderable(const Object *ob);
176bool DRW_object_is_in_edit_mode(const Object *ob);
182bool DRW_object_use_hide_faces(const Object *ob);
183
185 const ParticleSystem *psys);
186
191template<typename T> T &DRW_object_get_data_for_drawing(const Object &object)
192{
193 return *static_cast<T *>(object.data);
194}
195
197{
198 /* For drawing we want either the base mesh if GPU subdivision is enabled, or the
199 * tessellated mesh if GPU subdivision is disabled. */
201 return mesh;
202 }
204}
205
206template<> inline Mesh &DRW_object_get_data_for_drawing(const Object &object)
207{
208 BLI_assert(object.type == OB_MESH);
209 return DRW_mesh_get_for_drawing(*static_cast<Mesh *>(object.data));
210}
211
217
218/* Draw State. */
219
220/* -------------------------------------------------------------------- */
223
225 private:
227 static thread_local DRWContext *g_context;
228
229 /* TODO(fclem): Private? */
230 public:
231 /* TODO: clean up this struct a bit. */
233 DRWData *data = nullptr;
236
242
249
250 const enum Mode {
253
254 /* These viewport modes will render without some overlays (i.e. no text). */
255
262
269
273
279
280 struct {
281 bool draw_background = false;
283
286
289
290 /* TODO(fclem): Public. */
291
293
295 Depsgraph *depsgraph = nullptr;
297 Scene *scene = nullptr;
300
303 const bContext *evil_C = nullptr;
305 ARegion *region = nullptr;
309 RegionView3D *rv3d = nullptr;
311 View3D *v3d = nullptr;
313 Object *obact = nullptr;
314 Object *object_pose = nullptr;
315 Object *object_edit = nullptr;
316
318
319 public:
327 Depsgraph *depsgraph,
329 const bContext *C = nullptr,
330 ARegion *region = nullptr,
331 View3D *v3d = nullptr);
333 Depsgraph *depsgraph,
334 const blender::int2 size = {1, 1},
335 const bContext *C = nullptr,
336 ARegion *region = nullptr,
337 View3D *v3d = nullptr);
338
339 ~DRWContext();
340
347 void acquire_data();
348
354 void release_data();
355
362 void enable_engines(bool gpencil_engine_needed = false,
363 RenderEngineType *render_engine_type = nullptr);
364
367
369 std::function<void(struct DupliCacheManager &, struct ExtractionGraph &)>;
370
372 void sync(iter_callback_t iter_callback);
374 void engines_init_and_sync(iter_callback_t iter_callback);
376 void engines_draw_scene();
377
379 {
380 return *g_context;
381 }
382
384 {
385 return size;
386 }
387
389 static bool is_active()
390 {
391 return g_context != nullptr;
392 }
393
394 bool is_select() const
395 {
397 }
399 {
401 }
402 bool is_depth() const
403 {
405 }
406 bool is_image_render() const
407 {
409 }
410 bool is_scene_render() const
411 {
412 return ELEM(mode, RENDER);
413 }
415 {
416 return ELEM(mode, VIEWPORT_RENDER);
417 }
418
420 bool is_playback() const;
422 bool is_navigating() const;
424 bool is_painting() const;
426 bool is_transforming() const;
429};
430
432
Mesh * BKE_mesh_wrapper_ensure_subdivision(Mesh *mesh)
bool BKE_subsurf_modifier_has_gpu_subdiv(const Mesh *mesh)
#define BLI_assert(a)
Definition BLI_assert.h:46
struct GSet GSet
Definition BLI_ghash.h:337
#define ELEM(...)
eObjectMode
@ OB_MODE_OBJECT
Object is a sort of wrapper for general info.
@ OB_MESH
const DRWContext * DRW_context_get()
void DRW_cache_restart()
bool DRW_object_is_in_edit_mode(const Object *ob)
blender::draw::TextureFromPool & DRW_viewport_pass_texture_get(const char *pass_name)
void DRW_render_to_image(RenderEngine *engine, Depsgraph *depsgraph, std::function< void(RenderEngine *, RenderLayer *, const rcti)> render_view_cb, std::function< void(RenderResult *)> store_metadata_cb)
bool DRW_object_use_hide_faces(const Object *ob)
bool DRW_object_is_renderable(const Object *ob)
T & DRW_object_get_data_for_drawing(const Object &object)
int DRW_object_visibility_in_active_context(const Object *ob)
void DRW_custom_pipeline_begin(DRWContext &draw_ctx, Depsgraph *depsgraph)
bool DRW_object_is_visible_psys_in_active_context(const Object *object, const ParticleSystem *psys)
const Mesh * DRW_object_get_editmesh_cage_for_drawing(const Object &object)
Mesh & DRW_mesh_get_for_drawing(Mesh &mesh)
void DRW_render_set_time(RenderEngine *engine, Depsgraph *depsgraph, int frame, float subframe)
void DRW_custom_pipeline_end(DRWContext &draw_ctx)
void DRW_viewport_request_redraw()
void DRW_render_object_iter(RenderEngine *engine, Depsgraph *depsgraph, std::function< void(blender::draw::ObjectRef &, RenderEngine *, Depsgraph *)>)
#define C
Definition RandGen.cpp:29
BMesh const char void * data
BPy_StructRNA * depsgraph
CCL_NAMESPACE_BEGIN struct Options options
#define T
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
float center[3]
Definition DRW_render.hh:66
bool is_image_render() const
DRWContext(Mode mode, Depsgraph *depsgraph, GPUViewport *viewport, const bContext *C=nullptr, ARegion *region=nullptr, View3D *v3d=nullptr)
bool is_playback() const
DRWViewData * view_data_active
static bool is_active()
Object * object_edit
std::function< void(struct DupliCacheManager &, struct ExtractionGraph &)> iter_callback_t
GSet * delayed_extraction
bool draw_background
void sync(iter_callback_t iter_callback)
DefaultFramebufferList * viewport_framebuffer_list_get() const
DRWData * data
View3D * v3d
Depsgraph * depsgraph
bool is_scene_render() const
DRWTextStore ** text_store_p
void enable_engines(bool gpencil_engine_needed=false, RenderEngineType *render_engine_type=nullptr)
RegionView3D * rv3d
blender::float2 inv_size
@ DEPTH_ACTIVE_OBJECT
@ SELECT_OBJECT_MATERIAL
blender::float2 size
static DRWContext & get_active()
Object * obact
enum DRWContext::Mode mode
blender::float2 viewport_size_get() const
void engines_draw_scene()
Scene * scene
Object * object_pose
void acquire_data()
const bContext * evil_C
bool is_depth() const
bool is_painting() const
bool is_viewport_image_render() const
eObjectMode object_mode
void release_data()
ARegion * region
DRWContext(Mode mode, Depsgraph *depsgraph, const blender::int2 size={1, 1}, const bContext *C=nullptr, ARegion *region=nullptr, View3D *v3d=nullptr)
void engines_init_and_sync(iter_callback_t iter_callback)
SpaceLink * space_data
bool is_transforming() const
blender::gpu::FrameBuffer * default_framebuffer()
DefaultTextureList * viewport_texture_list_get() const
GPUViewport * viewport
bool is_select() const
ViewLayer * view_layer
void engines_data_validate()
bool is_navigating() const
bool is_viewport_compositor_enabled() const
bool is_material_select() const
void set_used(bool used)
DrawEngine * instance
Definition DRW_render.hh:97
virtual DrawEngine * create_instance()=0
virtual void begin_sync()=0
virtual void object_sync(blender::draw::ObjectRef &ob_ref, blender::draw::Manager &manager)=0
virtual void draw(blender::draw::Manager &manager)=0
virtual void init()=0
static constexpr int GPU_INFO_SIZE
Definition DRW_render.hh:70
char info[GPU_INFO_SIZE]
Definition DRW_render.hh:72
virtual blender::StringRefNull name_get()=0
virtual ~DrawEngine()=default
virtual void end_sync()=0