Blender V5.3
app/opengl/display_driver.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
6#include "app/opengl/shader.h"
7
8#include "util/log.h"
9
10#include <epoxy/gl.h>
11
13
14/* --------------------------------------------------------------------
15 * OpenGLDisplayDriver.
16 */
17
18OpenGLDisplayDriver::OpenGLDisplayDriver(const std::function<bool()> &gl_context_enable,
19 const std::function<void()> &gl_context_disable)
20 : gl_context_enable_(gl_context_enable), gl_context_disable_(gl_context_disable)
21{
22}
23
25
26/* --------------------------------------------------------------------
27 * Update procedure.
28 */
29
31{
32 /* Assuming no tiles used in interactive display. */
33}
34
36 const int texture_width,
37 const int texture_height)
38{
39 /* Note that it's the responsibility of OpenGLDisplayDriver to ensure updating and drawing
40 * the texture does not happen at the same time. This is achieved indirectly.
41 *
42 * When enabling the OpenGL context, it uses an internal mutex lock DST.gl_context_lock.
43 * This same lock is also held when do_draw() is called, which together ensure mutual
44 * exclusion.
45 *
46 * This locking is not performed on the Cycles side, because that would cause lock inversion. */
47 if (!gl_context_enable_()) {
48 return false;
49 }
50
51 if (gl_render_sync_) {
52 glWaitSync((GLsync)gl_render_sync_, 0, GL_TIMEOUT_IGNORED);
53 }
54
57 return false;
58 }
59
60 /* Update texture dimensions if needed. */
61 if (texture_.width != texture_width || texture_.height != texture_height) {
62 glActiveTexture(GL_TEXTURE0);
63 glBindTexture(GL_TEXTURE_2D, texture_.gl_id);
64 glTexImage2D(GL_TEXTURE_2D,
65 0,
66 GL_RGBA16F,
67 texture_width,
68 texture_height,
69 0,
70 GL_RGBA,
71 GL_HALF_FLOAT,
72 nullptr);
73 texture_.width = texture_width;
74 texture_.height = texture_height;
75 glBindTexture(GL_TEXTURE_2D, 0);
76
77 /* Texture did change, and no pixel storage was provided. Tag for an explicit zeroing out to
78 * avoid undefined content. */
79 texture_.need_zero = true;
81 }
82
83 /* Update PBO dimensions if needed.
84 *
85 * NOTE: Allocate the PBO for the size which will fit the final render resolution (as in,
86 * at a resolution divider 1. This was we don't need to recreate graphics interoperability
87 * objects which are costly and which are tied to the specific underlying buffer size.
88 * The downside of this approach is that when graphics interoperability is not used we are
89 * sending too much data to GPU when resolution divider is not 1. */
90 const int buffer_width = params.full_size.x;
91 const int buffer_height = params.full_size.y;
92 if (texture_.buffer_width != buffer_width || texture_.buffer_height != buffer_height) {
93 const size_t size_in_bytes = sizeof(half4) * buffer_width * buffer_height;
94 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture_.gl_pbo_id);
95 glBufferData(GL_PIXEL_UNPACK_BUFFER, size_in_bytes, nullptr, GL_DYNAMIC_DRAW);
96 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
97
98 texture_.buffer_width = buffer_width;
99 texture_.buffer_height = buffer_height;
100 }
101
102 /* New content will be provided to the texture in one way or another, so mark this in a
103 * centralized place. */
104 texture_.need_update = true;
105
106 return true;
107}
108
110{
111 gl_upload_sync_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
112 glFlush();
113
115}
116
117/* --------------------------------------------------------------------
118 * Texture buffer mapping.
119 */
120
122{
123 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture_.gl_pbo_id);
124
125 half4 *mapped_rgba_pixels = reinterpret_cast<half4 *>(
126 glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY));
127 if (!mapped_rgba_pixels) {
128 LOG_ERROR << "Error mapping OpenGLDisplayDriver pixel buffer object.";
129 }
130
131 if (texture_.need_zero) {
132 const int64_t texture_width = texture_.width;
133 const int64_t texture_height = texture_.height;
134 memset(reinterpret_cast<void *>(mapped_rgba_pixels),
135 0,
136 texture_width * texture_height * sizeof(half4));
137 texture_.need_zero = false;
138 }
139
140 return mapped_rgba_pixels;
141}
142
144{
145 glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
146
147 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
148}
149
150/* --------------------------------------------------------------------
151 * Graphics interoperability.
152 */
153
155{
156 GraphicsInteropDevice interop_device;
157 interop_device.type = GraphicsInteropDevice::OPENGL;
158 return interop_device;
159}
160
162{
165 texture_.gl_pbo_id,
166 texture_.buffer_width * texture_.buffer_height *
167 sizeof(half4));
168 }
169
170 if (texture_.need_zero) {
172 texture_.need_zero = false;
173 }
174}
175
180
185
186/* --------------------------------------------------------------------
187 * Drawing.
188 */
189
191{
192 texture_.need_zero = true;
193}
194
196{
197 /* See do_update_begin() for why no locking is required here. */
198 if (texture_.need_zero) {
199 /* Texture is requested to be cleared and was not yet cleared.
200 * Do early return which should be equivalent of drawing all-zero texture. */
201 return;
202 }
203
205 return;
206 }
207
208 if (gl_upload_sync_) {
209 glWaitSync((GLsync)gl_upload_sync_, 0, GL_TIMEOUT_IGNORED);
210 }
211
212 glEnable(GL_BLEND);
213 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
214
215 display_shader_.bind(params.full_size.x, params.full_size.y);
216
217 glActiveTexture(GL_TEXTURE0);
218 glBindTexture(GL_TEXTURE_2D, texture_.gl_id);
219
220 if (texture_.width != params.size.x || texture_.height != params.size.y) {
221 /* Resolution divider is different from 1, force nearest interpolation. */
222 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
223 }
224 else {
225 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
226 }
227
228 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_);
229
231 vertex_buffer_update(params);
232
233 GLuint vertex_array_object;
234 glGenVertexArrays(1, &vertex_array_object);
235 glBindVertexArray(vertex_array_object);
236
237 const int texcoord_attribute = display_shader_.get_tex_coord_attrib_location();
238 const int position_attribute = display_shader_.get_position_attrib_location();
239
240 glEnableVertexAttribArray(texcoord_attribute);
241 glEnableVertexAttribArray(position_attribute);
242
243 glVertexAttribPointer(
244 texcoord_attribute, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (const GLvoid *)nullptr);
245 glVertexAttribPointer(position_attribute,
246 2,
247 GL_FLOAT,
248 GL_FALSE,
249 4 * sizeof(float),
250 (const GLvoid *)(sizeof(float) * 2));
251
252 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
253
254 glBindBuffer(GL_ARRAY_BUFFER, 0);
255 glBindTexture(GL_TEXTURE_2D, 0);
256
257 glDeleteVertexArrays(1, &vertex_array_object);
258
260
261 glDisable(GL_BLEND);
262
263 gl_render_sync_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
264 glFlush();
265}
266
268{
269 if (!texture_.gl_id) {
270 /* If there is no texture allocated, there is nothing to draw. Inform the draw call that it can
271 * can not continue. Note that this is not an unrecoverable error, so once the texture is known
272 * we will come back here and create all the GPU resources needed for draw. */
273 return false;
274 }
275
278 }
280
281 if (!vertex_buffer_) {
282 glGenBuffers(1, &vertex_buffer_);
283 if (!vertex_buffer_) {
284 LOG_ERROR << "Error creating vertex buffer.";
285 return false;
286 }
287 }
288
290
291 return true;
292}
293
295{
297
298 if (vertex_buffer_ != 0) {
299 glDeleteBuffers(1, &vertex_buffer_);
300 }
301
302 if (texture_.gl_pbo_id) {
303 glDeleteBuffers(1, &texture_.gl_pbo_id);
304 texture_.gl_pbo_id = 0;
305 }
306
307 if (texture_.gl_id) {
308 glDeleteTextures(1, &texture_.gl_id);
309 texture_.gl_id = 0;
310 }
311
313}
314
316{
317 if (texture_.creation_attempted) {
318 return texture_.is_created;
319 }
320 texture_.creation_attempted = true;
321
322 DCHECK(!texture_.gl_id);
323 DCHECK(!texture_.gl_pbo_id);
324
325 /* Create texture. */
326 glGenTextures(1, &texture_.gl_id);
327 if (!texture_.gl_id) {
328 LOG_ERROR << "Error creating texture.";
329 return false;
330 }
331
332 /* Configure the texture. */
333 glActiveTexture(GL_TEXTURE0);
334 glBindTexture(GL_TEXTURE_2D, texture_.gl_id);
335 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
336 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
337 glBindTexture(GL_TEXTURE_2D, 0);
338
339 /* Create PBO for the texture. */
340 glGenBuffers(1, &texture_.gl_pbo_id);
341 if (!texture_.gl_pbo_id) {
342 LOG_ERROR << "Error creating texture pixel buffer object.";
343 return false;
344 }
345
346 /* Creation finished with a success. */
347 texture_.is_created = true;
349
350 return true;
351}
352
354{
355 if (!texture_.need_update) {
356 return;
357 }
358
359 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture_.gl_pbo_id);
360 glTexSubImage2D(
361 GL_TEXTURE_2D, 0, 0, 0, texture_.width, texture_.height, GL_RGBA, GL_HALF_FLOAT, nullptr);
362 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
363
364 texture_.need_update = false;
365}
366
368{
369 /* Invalidate old contents - avoids stalling if the buffer is still waiting in queue to be
370 * rendered. */
371 glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(float), nullptr, GL_STREAM_DRAW);
372
373 float *vpointer = reinterpret_cast<float *>(glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY));
374 if (!vpointer) {
375 return;
376 }
377
378 vpointer[0] = 0.0f;
379 vpointer[1] = 0.0f;
380 vpointer[2] = params.full_offset.x;
381 vpointer[3] = params.full_offset.y;
382
383 vpointer[4] = 1.0f;
384 vpointer[5] = 0.0f;
385 vpointer[6] = (float)params.size.x + params.full_offset.x;
386 vpointer[7] = params.full_offset.y;
387
388 vpointer[8] = 1.0f;
389 vpointer[9] = 1.0f;
390 vpointer[10] = (float)params.size.x + params.full_offset.x;
391 vpointer[11] = (float)params.size.y + params.full_offset.y;
392
393 vpointer[12] = 0.0f;
394 vpointer[13] = 1.0f;
395 vpointer[14] = params.full_offset.x;
396 vpointer[15] = (float)params.size.y + params.full_offset.y;
397
398 glUnmapBuffer(GL_ARRAY_BUFFER);
399}
400
long long int int64_t
GraphicsInteropBuffer graphics_interop_buffer_
void assign(GraphicsInteropDevice::Type type, int64_t handle, size_t size)
struct OpenGLDisplayDriver::@222167003027267241006021154222206020127106043332 texture_
void vertex_buffer_update(const Params &params)
~OpenGLDisplayDriver() override
bool update_begin(const Params &params, const int texture_width, const int texture_height) override
std::function< void()> gl_context_disable_
OpenGLDisplayDriver(const std::function< bool()> &gl_context_enable, const std::function< void()> &gl_context_disable)
void graphics_interop_update_buffer() override
GraphicsInteropDevice graphics_interop_get_device() override
void graphics_interop_deactivate() override
std::function< bool()> gl_context_enable_
half4 * map_texture_buffer() override
void draw(const Params &params) override
int get_position_attrib_location()
void bind(const int width, const int height)
int get_tex_coord_attrib_location()
#define CCL_NAMESPACE_END
#define DCHECK(expression)
Definition log.h:135
#define LOG_ERROR
Definition log.h:101
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
Definition half.h:53