Blender V5.0
path_trace_display.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
8
9#include "util/half.h"
10#include "util/thread.h"
11#include "util/unique_ptr.h"
12
14
15class BufferParams;
16
17/* PathTraceDisplay is used for efficient render buffer display.
18 *
19 * The host applications implements a DisplayDriver, storing a render pass in a GPU-side
20 * textures. This texture is continuously updated by the path tracer and drawn by the host
21 * application.
22 *
23 * PathTraceDisplay is a wrapper around the DisplayDriver, adding thread safety, state tracking
24 * and error checking. */
25
27 public:
29 virtual ~PathTraceDisplay() = default;
30
31 /* Reset the display for the new state of render session. Is called whenever session is reset,
32 * which happens on changes like viewport navigation or viewport dimension change.
33 *
34 * This call will configure parameters for a changed buffer and reset the texture state.
35 *
36 * When the `reset_rendering` a complete display reset happens. When it is false reset happens
37 * for a new state of the buffer parameters which is assumed to correspond to the next tile. */
38 void reset(const BufferParams &buffer_params, bool reset_rendering);
39
40 /* --------------------------------------------------------------------
41 * Update procedure.
42 *
43 * These calls indicates a desire of the caller to update content of the displayed texture. */
44
45 /* Returns true when update is ready. Update should be finished with update_end().
46 *
47 * If false is returned then no update is possible, and no update_end() call is needed.
48 *
49 * The texture width and height denotes an actual resolution of the underlying render result. */
50 bool update_begin(const int texture_width, const int texture_height);
51
52 void update_end();
53
54 /* Get currently configured texture size of the display (as configured by `update_begin()`. */
55 int2 get_texture_size() const;
56
57 /* --------------------------------------------------------------------
58 * Texture update from CPU buffer.
59 *
60 * NOTE: The PathTraceDisplay should be marked for an update being in process with
61 * `update_begin()`.
62 *
63 * Most portable implementation, which must be supported by all platforms. Might not be the most
64 * efficient one.
65 */
66
67 /* Copy buffer of rendered pixels of a given size into a given position of the texture.
68 *
69 * This function does not acquire a lock. The reason for this is to allow use of this function
70 * for partial updates from different devices. In this case the caller will acquire the lock
71 * once, update all the slices and release
72 * the lock once. This will ensure that draw() will never use partially updated texture. */
73 void copy_pixels_to_texture(const half4 *rgba_pixels,
74 const int texture_x,
75 const int texture_y,
76 const int pixels_width,
77 const int pixels_height);
78
79 /* --------------------------------------------------------------------
80 * Texture buffer mapping.
81 *
82 * This functionality is used to update GPU-side texture content without need to maintain CPU
83 * side buffer on the caller.
84 *
85 * NOTE: The PathTraceDisplay should be marked for an update being in process with
86 * `update_begin()`.
87 *
88 * NOTE: Texture buffer can not be mapped while graphics interoperability is active. This means
89 * that `map_texture_buffer()` is not allowed between `graphics_interop_begin()` and
90 * `graphics_interop_end()` calls.
91 */
92
93 /* Map pixels memory form texture to a buffer available for write from CPU. Width and height will
94 * define a requested size of the texture to write to.
95 * Upon success a non-null pointer is returned and the texture buffer is to be unmapped.
96 * If an error happens during mapping, or if mapping is not supported by this GPU display a
97 * null pointer is returned and the buffer is NOT to be unmapped.
98 *
99 * NOTE: Usually the implementation will rely on a GPU context of some sort, and the GPU context
100 * is often can not be bound to two threads simultaneously, and can not be released from a
101 * different thread. This means that the mapping API should be used from the single thread only,
102 */
105
106 /* --------------------------------------------------------------------
107 * Graphics interoperability.
108 *
109 * A special code path which allows to update texture content directly from the GPU compute
110 * device. Complementary part of DeviceGraphicsInterop.
111 *
112 * NOTE: Graphics interoperability can not be used while the texture buffer is mapped. This means
113 * that `graphics_interop_get_buffer()` is not allowed between `map_texture_buffer()` and
114 * `unmap_texture_buffer()` calls. */
115
116 /* Get PathTraceDisplay graphics interoperability information which acts as a destination for the
117 * device API. */
120
121 /* (De)activate GPU display for graphics interoperability outside of regular display update
122 * routines. */
125
126 /* --------------------------------------------------------------------
127 * Drawing.
128 */
129
130 /* Clear the texture by filling it with all zeroes.
131 *
132 * This call might happen in parallel with draw, but can never happen in parallel with the
133 * update.
134 *
135 * The actual zeroing can be deferred to a later moment. What is important is that after clear
136 * and before pixels update the drawing texture will be fully empty, and that partial update
137 * after clear will write new pixel values for an updating area, leaving everything else zeroed.
138 *
139 * If the GPU display supports graphics interoperability then the zeroing the display is to be
140 * delegated to the device via the `GraphicsInterop`. */
141 void zero();
142
143 /* Draw the current state of the texture.
144 *
145 * Returns true if this call did draw an updated state of the texture. */
146 bool draw();
147
148 /* Flush outstanding display commands before ending the render loop. */
149 void flush();
150
151 private:
152 /* Display driver implemented by the host application. */
154
155 /* Current display parameters */
156 thread_mutex mutex_;
157 DisplayDriver::Params params_;
158
159 /* Mark texture as its content has been updated.
160 * Used from places which knows that the texture content has been brought up-to-date, so that the
161 * drawing knows whether it can be performed, and whether drawing happened with an up-to-date
162 * texture state. */
163 void mark_texture_updated();
164
165 /* State of the update process. */
166 struct {
167 /* True when update is in process, indicated by `update_begin()` / `update_end()`. */
168 bool is_active = false;
169 } update_state_;
170
171 /* State of the texture, which is needed for an integration with render session and interactive
172 * updates and navigation. */
173 struct {
174 /* Texture is considered outdated after `reset()` until the next call of
175 * `copy_pixels_to_texture()`. */
176 bool is_outdated = true;
177
178 /* Texture size in pixels. */
180 } texture_state_;
181
182 /* State of the texture buffer. Is tracked to perform sanity checks. */
183 struct {
184 /* True when the texture buffer is mapped with `map_texture_buffer()`. */
185 bool is_mapped = false;
186 } texture_buffer_state_;
187};
188
void reset()
clear internal cached data and reset random seed
virtual ~PathTraceDisplay()=default
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)
Definition half.h:60
std::mutex thread_mutex
Definition thread.h:27