Blender V5.3
gpu_context_private.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2016 by Mike Erwin. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#pragma once
12
13#include "BKE_global.hh"
14
15#include "GPU_batch.hh"
16#include "GPU_context.hh"
17#include "GPU_texture_pool.hh"
18
19#include "gpu_debug_private.hh"
22#include "gpu_shader_private.hh"
23#include "gpu_state_private.hh"
24
25#include <pthread.h>
26
27class GHOST_IWindow;
28
29namespace blender {
30
31struct GPUMatrixState;
32
33namespace gpu {
34
35class Context {
36 public:
38 Shader *shader = nullptr;
42 Immediate *imm = nullptr;
43
55
57 bool debug_is_capturing = false;
59
60 /* GPUContext counter used to assign a unique ID to each GPUContext.
61 * NOTE(Metal): This is required by the Metal Backend, as a bug exists in the global OS shader
62 * cache wherein compilation of identical source from two distinct threads can result in an
63 * invalid cache collision, result in a broken shader object. Appending the unique context ID
64 * onto compiled sources ensures the source hashes are different. */
65 static inline std::atomic<int> context_counter = 0;
66 int context_id = 0;
67
68 /* Used as a stack. Each render_begin/end pair will push pop from the stack. */
70
72 VertBuf *dummy_vbo = nullptr;
74 Batch *procedural_points_batch = nullptr;
75 Batch *procedural_lines_batch = nullptr;
78
81
85
86 protected:
88 pthread_t thread_;
92
93 public:
94 Context();
95 virtual ~Context();
96
97 static Context *get();
98
99 virtual void activate() = 0;
100 virtual void deactivate() = 0;
101 virtual void begin_frame() = 0;
102 virtual void end_frame() = 0;
103
104 /* Will push all pending commands to the GPU. */
105 virtual void flush() = 0;
106 /* Will wait until the GPU has finished executing all command. */
107 virtual void finish() = 0;
108
109 virtual void memory_statistics_get(int *r_total_mem, int *r_free_mem) = 0;
110
111 virtual void debug_group_begin(const char * /*name*/, int /*index*/) {};
112 virtual void debug_group_end() {};
113
114 /* Returns true if capture successfully started. */
115 virtual bool debug_capture_begin(const char *title) = 0;
116 virtual void debug_capture_end() = 0;
117 virtual void *debug_capture_scope_create(const char *name) = 0;
118 virtual bool debug_capture_scope_begin(void *scope) = 0;
119 virtual void debug_capture_scope_end(void *scope) = 0;
120
121 /* Consider all buffers slot empty after these call for error checking.
122 * But doesn't really free them. */
123 virtual void debug_unbind_all_ubo() = 0;
124 virtual void debug_unbind_all_ssbo() = 0;
125
126 bool is_active_on_thread();
127
133
134 /* When using `--debug-gpu`, assert that the shader fragments write to all the writable
135 * attachments of the bound frame-buffer. */
137 {
138 if (!(G.debug & G_DEBUG_GPU)) {
139 return;
140 }
141
143 return;
144 }
145
146 uint16_t fragment_output_bits = sh->fragment_output_bits;
147 uint16_t fb_attachments_bits = active_fb->get_color_attachments_bitset();
148
149 if ((fb_attachments_bits & ~fragment_output_bits) != 0) {
150 std::string msg;
151 msg = msg + "Shader (" + sh->name_get() + ") does not write to all frame-buffer (" +
152 active_fb->name_get() + ") color attachments";
153 BLI_assert_msg(false, msg.c_str());
154 std::cerr << msg << std::endl;
155 }
156 }
157
158 protected:
163 void free_resources();
164};
165
166/* Syntactic sugar. */
167static inline GPUContext *wrap(Context *ctx)
168{
169 return reinterpret_cast<GPUContext *>(ctx);
170}
171static inline Context *unwrap(GPUContext *ctx)
172{
173 return reinterpret_cast<Context *>(ctx);
174}
175static inline const Context *unwrap(const GPUContext *ctx)
176{
177 return reinterpret_cast<const Context *>(ctx);
178}
179
180} // namespace gpu
181} // namespace blender
#define BLI_assert_msg(a, msg)
virtual void debug_unbind_all_ssbo()=0
Vector< StorageBuf * > printf_buf
virtual void debug_group_begin(const char *, int)
static Context * get()
virtual void debug_capture_scope_end(void *scope)=0
virtual void memory_statistics_get(int *r_total_mem, int *r_free_mem)=0
virtual void finish()=0
virtual void debug_unbind_all_ubo()=0
GPUMatrixState * matrix_state
virtual bool debug_capture_scope_begin(void *scope)=0
virtual void * debug_capture_scope_create(const char *name)=0
Batch * procedural_triangles_batch_get()
VertBuf * dummy_vbo_get()
void assert_framebuffer_shader_compatibility(Shader *sh)
virtual void debug_capture_end()=0
Batch * procedural_triangle_strips_batch_get()
virtual bool debug_capture_begin(const char *title)=0
virtual void activate()=0
static std::atomic< int > context_counter
virtual void end_frame()=0
Batch * procedural_lines_batch_get()
virtual void begin_frame()=0
Batch * procedural_points_batch_get()
virtual void deactivate()=0
virtual void flush()=0
StringRefNull name_get() const
static Context * unwrap(GPUContext *ctx)
static GPUContext * wrap(Context *ctx)
Vector< StringRef > DebugStack
BMesh Iterators.
Global G
Definition blender.cc:57
unsigned short uint16_t
Definition BKE_text.h:80
@ GPU_WRITE_COLOR
Definition GPU_state.hh:27