Blender V5.3
path_trace_display.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
6
7#include "session/buffers.h"
9
10#include "util/log.h"
11
13
15{
16}
17
19{
20 driver_->reset();
21}
22void PathTraceDisplay::reset(const BufferParams &buffer_params, const bool reset_rendering)
23{
24 const thread_scoped_lock lock(mutex_);
25
26 params_.full_offset = make_int2(buffer_params.full_x + buffer_params.window_x,
27 buffer_params.full_y + buffer_params.window_y);
28 params_.full_size = make_int2(buffer_params.full_width, buffer_params.full_height);
29 params_.size = make_int2(buffer_params.window_width, buffer_params.window_height);
30
31 texture_state_.is_outdated = true;
32
33 if (!reset_rendering) {
34 driver_->next_tile_begin();
35 }
36}
37
38void PathTraceDisplay::mark_texture_updated()
39{
40 texture_state_.is_outdated = false;
41}
42
43/* --------------------------------------------------------------------
44 * Update procedure.
45 */
46
47bool PathTraceDisplay::update_begin(const int texture_width, const int texture_height)
48{
49 DCHECK(!update_state_.is_active);
50
51 if (update_state_.is_active) {
52 LOG_ERROR << "Attempt to re-activate update process.";
53 return false;
54 }
55
56 /* Get parameters within a mutex lock, to avoid reset() modifying them at the same time.
57 * The update itself is non-blocking however, for better performance and to avoid
58 * potential deadlocks due to locks held by the subclass. */
60 {
61 const thread_scoped_lock lock(mutex_);
62 params = params_;
63 texture_state_.size = make_int2(texture_width, texture_height);
64 }
65
66 if (!driver_->update_begin(params, texture_width, texture_height)) {
67 LOG_ERROR << "PathTraceDisplay implementation could not begin update.";
68 return false;
69 }
70
71 update_state_.is_active = true;
72
73 return true;
74}
75
77{
78 DCHECK(update_state_.is_active);
79
80 if (!update_state_.is_active) {
81 LOG_ERROR << "Attempt to deactivate inactive update process.";
82 return;
83 }
84
85 driver_->update_end();
86
87 update_state_.is_active = false;
88}
89
91{
92 return texture_state_.size;
93}
94
95/* --------------------------------------------------------------------
96 * Texture update from CPU buffer.
97 */
98
100 const int texture_x,
101 const int texture_y,
102 const int pixels_width,
103 const int pixels_height)
104{
105 DCHECK(update_state_.is_active);
106
107 if (!update_state_.is_active) {
108 LOG_ERROR << "Attempt to copy pixels data outside of PathTraceDisplay update.";
109 return;
110 }
111
112 mark_texture_updated();
113
114 /* This call copies pixels to a mapped texture buffer which is typically much cheaper from CPU
115 * time point of view than to copy data directly to a texture.
116 *
117 * The possible downside of this approach is that it might require a higher peak memory when
118 * doing partial updates of the texture (although, in practice even partial updates might peak
119 * with a full-frame buffer stored on the CPU if the GPU is currently occupied). */
120 half4 *mapped_rgba_pixels = map_texture_buffer();
121 if (!mapped_rgba_pixels) {
122 return;
123 }
124
125 const int texture_width = texture_state_.size.x;
126 const int texture_height = texture_state_.size.y;
127
128 if (texture_x == 0 && texture_y == 0 && pixels_width == texture_width &&
129 pixels_height == texture_height)
130 {
131 const size_t size_in_bytes = sizeof(half4) * texture_width * texture_height;
132 memcpy(mapped_rgba_pixels, rgba_pixels, size_in_bytes);
133 }
134 else {
135 const half4 *rgba_row = rgba_pixels;
136 half4 *mapped_rgba_row = mapped_rgba_pixels + texture_y * texture_width + texture_x;
137 for (int y = 0; y < pixels_height;
138 ++y, rgba_row += pixels_width, mapped_rgba_row += texture_width)
139 {
140 memcpy(mapped_rgba_row, rgba_row, sizeof(half4) * pixels_width);
141 }
142 }
143
145}
146
147/* --------------------------------------------------------------------
148 * Texture buffer mapping.
149 */
150
152{
153 DCHECK(!texture_buffer_state_.is_mapped);
154 DCHECK(update_state_.is_active);
155
156 if (texture_buffer_state_.is_mapped) {
157 LOG_ERROR << "Attempt to re-map an already mapped texture buffer.";
158 return nullptr;
159 }
160
161 if (!update_state_.is_active) {
162 LOG_ERROR << "Attempt to copy pixels data outside of PathTraceDisplay update.";
163 return nullptr;
164 }
165
166 half4 *mapped_rgba_pixels = driver_->map_texture_buffer();
167
168 if (mapped_rgba_pixels) {
169 texture_buffer_state_.is_mapped = true;
170 }
171
172 return mapped_rgba_pixels;
173}
174
176{
177 DCHECK(texture_buffer_state_.is_mapped);
178
179 if (!texture_buffer_state_.is_mapped) {
180 LOG_ERROR << "Attempt to unmap non-mapped texture buffer.";
181 return;
182 }
183
184 texture_buffer_state_.is_mapped = false;
185
186 mark_texture_updated();
187 driver_->unmap_texture_buffer();
188}
189
190/* --------------------------------------------------------------------
191 * Graphics interoperability.
192 */
193
195{
196 return driver_->graphics_interop_get_device();
197}
198
200{
201 GraphicsInteropBuffer &interop_buffer = driver_->graphics_interop_get_buffer();
202 DCHECK(!texture_buffer_state_.is_mapped);
203 DCHECK(update_state_.is_active);
204
205 if (texture_buffer_state_.is_mapped) {
207 << "Attempt to use graphics interoperability mode while the texture buffer is mapped.";
208 interop_buffer.clear();
209 return interop_buffer;
210 }
211
212 if (!update_state_.is_active) {
213 LOG_ERROR << "Attempt to use graphics interoperability outside of PathTraceDisplay update.";
214 interop_buffer.clear();
215 return interop_buffer;
216 }
217
218 /* Assume that interop will write new values to the texture. */
219 mark_texture_updated();
220
221 driver_->graphics_interop_update_buffer();
222 return interop_buffer;
223}
224
226{
227 driver_->graphics_interop_activate();
228}
229
231{
232 driver_->graphics_interop_deactivate();
233}
234
235/* --------------------------------------------------------------------
236 * Drawing.
237 */
238
240{
241 driver_->zero();
242}
243
245{
246 /* Get parameters within a mutex lock, to avoid reset() modifying them at the same time.
247 * The drawing itself is non-blocking however, for better performance and to avoid
248 * potential deadlocks due to locks held by the subclass. */
250 bool is_outdated;
251
252 {
253 const thread_scoped_lock lock(mutex_);
254 params = params_;
255 is_outdated = texture_state_.is_outdated;
256 }
257
258 driver_->draw(params);
259
260 return !is_outdated;
261}
262
264{
265 driver_->flush();
266}
267
volatile int lock
int full_width
Definition buffers.h:85
int window_y
Definition buffers.h:78
int full_height
Definition buffers.h:86
int window_height
Definition buffers.h:80
int window_width
Definition buffers.h:79
int window_x
Definition buffers.h:77
int2 get_texture_size() const
PathTraceDisplay(unique_ptr< DisplayDriver > driver)
bool update_begin(const int texture_width, const int texture_height)
void copy_pixels_to_texture(const half4 *rgba_pixels, const int texture_x, const int texture_y, const int pixels_width, const int pixels_height)
GraphicsInteropBuffer & graphics_interop_get_buffer()
GraphicsInteropDevice graphics_interop_get_device()
#define CCL_NAMESPACE_END
ccl_device_forceinline int2 make_int2(const int x, const int y)
#define DCHECK(expression)
Definition log.h:135
#define LOG_ERROR
Definition log.h:101
Definition half.h:53
std::unique_lock< std::mutex > thread_scoped_lock
Definition thread.h:28