Blender V5.0
blender/display_driver.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include <atomic>
8
9#include "RNA_blender_cpp.hh"
10
12
13#include "util/unique_ptr.h"
14
15struct GPUContext;
16struct GPUFence;
17namespace blender::gpu {
18class Shader;
19} // namespace blender::gpu
20
22
23/* Base class of shader used for display driver rendering. */
25 public:
26 static constexpr const char *position_attribute_name = "pos";
27 static constexpr const char *tex_coord_attribute_name = "texCoord";
28
29 /* Create shader implementation suitable for the given render engine and scene configuration. */
30 static unique_ptr<BlenderDisplayShader> create(BL::RenderEngine &b_engine, BL::Scene &b_scene);
31
33 virtual ~BlenderDisplayShader() = default;
34
35 virtual blender::gpu::Shader *bind(const int width, const int height) = 0;
36 virtual void unbind() = 0;
37
38 /* Get attribute location for position and texture coordinate respectively.
39 * NOTE: The shader needs to be bound to have access to those. */
40 virtual int get_position_attrib_location();
42
43 protected:
44 /* Get program of this display shader.
45 * NOTE: The shader needs to be bound to have access to this. */
47
48 /* Cached values of various OpenGL resources. */
51};
52
53/* Implementation of display rendering shader used in the case when render engine does not support
54 * display space shader. */
56 public:
58
59 blender::gpu::Shader *bind(const int width, const int height) override;
60 void unbind() override;
61
62 protected:
64
66 void destroy_shader();
67
71
72 /* Shader compilation attempted. Which means, that if the shader program is 0 then compilation or
73 * linking has failed. Do not attempt to re-compile the shader. */
75};
76
78 public:
79 BlenderDisplaySpaceShader(BL::RenderEngine &b_engine, BL::Scene &b_scene);
80
81 blender::gpu::Shader *bind(const int width, const int height) override;
82 void unbind() override;
83
84 protected:
86
87 BL::RenderEngine b_engine_;
88 BL::Scene &b_scene_;
89
90 /* Cached values of various OpenGL resources. */
92};
93
94/* Display driver implementation which is specific for Blender viewport integration. */
96 public:
97 BlenderDisplayDriver(BL::RenderEngine &b_engine, BL::Scene &b_scene, const bool background);
98 ~BlenderDisplayDriver() override;
99
100 void graphics_interop_activate() override;
101 void graphics_interop_deactivate() override;
102
103 void zero() override;
104
105 void set_zoom(const float zoom_x, const float zoom_y);
106
107 protected:
108 void next_tile_begin() override;
109
110 bool update_begin(const Params &params,
111 const int texture_width,
112 const int texture_height) override;
113 void update_end() override;
114
115 half4 *map_texture_buffer() override;
116 void unmap_texture_buffer() override;
117
119 void graphics_interop_update_buffer() override;
120
121 void draw(const Params &params) override;
122
123 void flush() override;
124
125 /* Helper function which allocates new GPU context. */
126 void gpu_context_create();
127 bool gpu_context_enable();
128 void gpu_context_disable();
129 void gpu_context_destroy();
130 void gpu_context_lock();
131 void gpu_context_unlock();
132
133 /* Create GPU resources used by the display driver. */
135
136 /* Destroy all GPU resources which are being used by this object. */
138
139 BL::RenderEngine b_engine_;
141
142 /* Content of the display is to be filled with zeroes. */
143 std::atomic<bool> need_zero_ = true;
144
146
147 /* Opaque storage for an internal state and data for tiles. */
148 struct Tiles;
150
151 GPUFence *gpu_render_sync_ = nullptr;
152 GPUFence *gpu_upload_sync_ = nullptr;
153
154 float2 zoom_ = make_float2(1.0f, 1.0f);
155};
156
GraphicsInteropDevice graphics_interop_get_device() override
half4 * map_texture_buffer() override
void graphics_interop_activate() override
void draw(const Params &params) override
std::atomic< bool > need_zero_
unique_ptr< BlenderDisplayShader > display_shader_
void graphics_interop_deactivate() override
void graphics_interop_update_buffer() override
BlenderDisplayDriver(BL::RenderEngine &b_engine, BL::Scene &b_scene, const bool background)
bool update_begin(const Params &params, const int texture_width, const int texture_height) override
void set_zoom(const float zoom_x, const float zoom_y)
virtual int get_position_attrib_location()
static unique_ptr< BlenderDisplayShader > create(BL::RenderEngine &b_engine, BL::Scene &b_scene)
static constexpr const char * tex_coord_attribute_name
static constexpr const char * position_attribute_name
BlenderDisplayShader()=default
virtual void unbind()=0
virtual blender::gpu::Shader * get_shader_program()=0
virtual blender::gpu::Shader * bind(const int width, const int height)=0
virtual int get_tex_coord_attrib_location()
virtual ~BlenderDisplayShader()=default
blender::gpu::Shader * shader_program_
blender::gpu::Shader * bind(const int width, const int height) override
blender::gpu::Shader * get_shader_program() override
BlenderDisplaySpaceShader(BL::RenderEngine &b_engine, BL::Scene &b_scene)
blender::gpu::Shader * get_shader_program() override
blender::gpu::Shader * bind(const int width, const int height) override
blender::gpu::Shader * shader_program_
DisplayDriver()=default
#define CCL_NAMESPACE_END
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
#define make_float2
Definition half.h:60