Blender  V3.3
blender/display_driver.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2021-2022 Blender Foundation */
3 
4 #pragma once
5 
6 #include <atomic>
7 
8 #include "MEM_guardedalloc.h"
9 
10 #include "RNA_blender_cpp.h"
11 
12 #include "session/display_driver.h"
13 
14 #include "util/thread.h"
15 #include "util/unique_ptr.h"
16 #include "util/vector.h"
17 
19 
20 /* Base class of shader used for display driver rendering. */
22  public:
23  static constexpr const char *position_attribute_name = "pos";
24  static constexpr const char *tex_coord_attribute_name = "texCoord";
25 
26  /* Create shader implementation suitable for the given render engine and scene configuration. */
27  static unique_ptr<BlenderDisplayShader> create(BL::RenderEngine &b_engine, BL::Scene &b_scene);
28 
29  BlenderDisplayShader() = default;
30  virtual ~BlenderDisplayShader() = default;
31 
32  virtual void bind(int width, int height) = 0;
33  virtual void unbind() = 0;
34 
35  /* Get attribute location for position and texture coordinate respectively.
36  * NOTE: The shader needs to be bound to have access to those. */
37  virtual int get_position_attrib_location();
38  virtual int get_tex_coord_attrib_location();
39 
40  protected:
41  /* Get program of this display shader.
42  * NOTE: The shader needs to be bound to have access to this. */
43  virtual uint get_shader_program() = 0;
44 
45  /* Cached values of various OpenGL resources. */
48 };
49 
50 /* Implementation of display rendering shader used in the case when render engine does not support
51  * display space shader. */
53  public:
54  virtual void bind(int width, int height) override;
55  virtual void unbind() override;
56 
57  protected:
58  virtual uint get_shader_program() override;
59 
61  void destroy_shader();
62 
66 
67  /* Shader compilation attempted. Which means, that if the shader program is 0 then compilation or
68  * linking has failed. Do not attempt to re-compile the shader. */
70 };
71 
73  public:
75 
76  virtual void bind(int width, int height) override;
77  virtual void unbind() override;
78 
79  protected:
80  virtual uint get_shader_program() override;
81 
84 
85  /* Cached values of various OpenGL resources. */
87 };
88 
89 /* Display driver implementation which is specific for Blender viewport integration. */
91  public:
92  BlenderDisplayDriver(BL::RenderEngine &b_engine, BL::Scene &b_scene);
94 
95  virtual void graphics_interop_activate() override;
96  virtual void graphics_interop_deactivate() override;
97 
98  virtual void clear() override;
99 
100  void set_zoom(float zoom_x, float zoom_y);
101 
102  protected:
103  virtual void next_tile_begin() override;
104 
105  virtual bool update_begin(const Params &params, int texture_width, int texture_height) override;
106  virtual void update_end() override;
107 
108  virtual half4 *map_texture_buffer() override;
109  virtual void unmap_texture_buffer() override;
110 
111  virtual GraphicsInterop graphics_interop_get() override;
112 
113  virtual void draw(const Params &params) override;
114 
115  virtual void flush() override;
116 
117  /* Helper function which allocates new GPU context. */
118  void gl_context_create();
119  bool gl_context_enable();
120  void gl_context_disable();
121  void gl_context_dispose();
122 
123  /* Destroy all GPU resources which are being used by this object. */
124  void gl_resources_destroy();
125 
127 
128  /* OpenGL context which is used the render engine doesn't have its own. */
129  void *gl_context_ = nullptr;
130  /* The when Blender RenderEngine side context is not available and the DisplayDriver is to create
131  * its own context. */
132  bool use_gl_context_ = false;
133  /* Mutex used to guard the `gl_context_`. */
135 
136  /* Content of the display is to be filled with zeroes. */
137  std::atomic<bool> need_clear_ = true;
138 
139  unique_ptr<BlenderDisplayShader> display_shader_;
140 
141  /* Opaque storage for an internal state and data for tiles. */
142  struct Tiles;
143  unique_ptr<Tiles> tiles_;
144 
145  void *gl_render_sync_ = nullptr;
146  void *gl_upload_sync_ = nullptr;
147 
148  float2 zoom_ = make_float2(1.0f, 1.0f);
149 };
150 
unsigned int uint
Definition: BLI_sys_types.h:67
struct Scene Scene
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei height
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei width
Read Guarded memory(de)allocation.
struct RenderEngine RenderEngine
BlenderDisplayDriver(BL::RenderEngine &b_engine, BL::Scene &b_scene)
virtual half4 * map_texture_buffer() override
virtual void unmap_texture_buffer() override
virtual void clear() override
virtual bool update_begin(const Params &params, int texture_width, int texture_height) override
std::atomic< bool > need_clear_
virtual void graphics_interop_activate() override
virtual GraphicsInterop graphics_interop_get() override
virtual void draw(const Params &params) override
void set_zoom(float zoom_x, float zoom_y)
virtual void flush() override
virtual void update_end() override
unique_ptr< Tiles > tiles_
unique_ptr< BlenderDisplayShader > display_shader_
virtual void graphics_interop_deactivate() override
virtual void next_tile_begin() override
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
virtual void bind(int width, int height)=0
BlenderDisplayShader()=default
virtual void unbind()=0
virtual uint get_shader_program()=0
virtual int get_tex_coord_attrib_location()
virtual ~BlenderDisplayShader()=default
virtual void unbind() override
virtual void bind(int width, int height) override
BlenderDisplaySpaceShader(BL::RenderEngine &b_engine, BL::Scene &b_scene)
virtual uint get_shader_program() override
virtual void bind(int width, int height) override
virtual uint get_shader_program() override
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
#define make_float2(x, y)
Definition: metal/compat.h:203
Definition: half.h:64
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition: thread.h:27