Blender V5.3
compositor_engine.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_bounds.hh"
6#include "BLI_listbase.hh"
8#include "BLI_string_ref.hh"
9#include "BLI_utildefines.hh"
10
11#include "DNA_layer_types.h"
12#include "DNA_node_types.h"
13#include "DNA_scene_types.h"
14#include "DNA_vec_types.h"
15#include "DNA_view3d_types.h"
16
17#include "BKE_camera.h"
18#include "BKE_compositor.hh"
20#include "BKE_node.hh"
21
23
24#include "ED_view3d.hh"
25
26#include "DRW_gpu_wrapper.hh"
27#include "DRW_render.hh"
28
29#include "COM_context.hh"
30#include "COM_domain.hh"
33#include "COM_result.hh"
35#include "COM_utilities.hh"
36
37#include "GPU_context.hh"
38#include "GPU_state.hh"
39#include "GPU_texture.hh"
40
41#include "draw_view_data.hh"
42
43#include "compositor_engine.h" /* Own include. */
44
46
48 private:
49 const Main *main_;
50 const Scene *scene_;
51 /* The compute context of the active viewer if one exists. */
52 const ComputeContext *active_compute_context_;
53
54 public:
57 const Main *main,
58 const Scene *scene)
60 main_(main),
61 scene_(scene),
62 active_compute_context_(
63 bke::compositor::compute_viewer_compute_context(*scene, compute_context_cache))
64 {
65 }
66
67 const Main &get_main() const override
68 {
69 return *main_;
70 }
71
72 const Scene &get_scene() const override
73 {
74 return *scene_;
75 }
76
77 bool use_gpu() const override
78 {
79 return true;
80 }
81
82 bool is_viewport() const override
83 {
84 return true;
85 }
86
91
92 const ComputeContext *viewer_compute_context() const override
93 {
94 return active_compute_context_;
95 }
96
97 /* In case the viewport has no camera region or is an image render, the domain covers the entire
98 * viewport. But in case the camera region is not entirely visible in the viewport, the data size
99 * of the domain will only cover the intersection of the viewport and the camera regions, while
100 * the display size will cover the virtual extension of the camera region. */
102 {
103 const DRWContext *draw_ctx = DRW_context_get();
104
105 /* No camera region or is a viewport render, the domain is the entire viewport. */
106 if (draw_ctx->rv3d->persp != RV3D_CAMOB || draw_ctx->is_viewport_image_render()) {
107 return compositor::Domain(int2(draw_ctx->viewport_size_get()));
108 }
109
110 const rctf camera_border = BKE_camera_view_border(draw_ctx->scene,
111 draw_ctx->depsgraph,
112 draw_ctx->v3d,
113 draw_ctx->rv3d,
114 draw_ctx->region->winx,
115 draw_ctx->region->winy,
116 false,
117 false,
118 true);
119 const Bounds<int2> camera_region = Bounds<int2>(
120 int2(int(camera_border.xmin), int(camera_border.ymin)),
121 int2(int(camera_border.xmax), int(camera_border.ymax)));
122
123 const Bounds<int2> render_region = Bounds<int2>(int2(0), int2(draw_ctx->viewport_size_get()));
124 const Bounds<int2> border_region = bounds::intersect(render_region, camera_region).value();
125
126 compositor::Domain domain = compositor::Domain(camera_region.size());
127 domain.data_size = border_region.size();
128 domain.data_offset = border_region.min - camera_region.min;
129 return domain;
130 }
131
132 /* Get the bounds of the camera region in pixels relative to the viewport. In case the viewport
133 * has no camera region or is an image render, return the bounds of the entire viewport. */
135 {
136 const DRWContext *draw_ctx = DRW_context_get();
137 const int2 viewport_size = int2(draw_ctx->viewport_size_get());
138 const Bounds<int2> render_region = Bounds<int2>(int2(0), viewport_size);
139
140 /* No camera region or is a viewport render, the domain is the entire viewport. */
141 if (draw_ctx->rv3d->persp != RV3D_CAMOB || draw_ctx->is_viewport_image_render()) {
142 return render_region;
143 }
144
145 const rctf camera_border = BKE_camera_view_border(draw_ctx->scene,
146 draw_ctx->depsgraph,
147 draw_ctx->v3d,
148 draw_ctx->rv3d,
149 draw_ctx->region->winx,
150 draw_ctx->region->winy,
151 false,
152 false,
153 true);
154 const Bounds<int2> camera_region = Bounds<int2>(
155 int2(int(camera_border.xmin), int(camera_border.ymin)),
156 int2(int(camera_border.xmax), int(camera_border.ymax)));
157
158 return bounds::intersect(render_region, camera_region).value_or(Bounds<int2>(int2(0)));
159 }
160
162 {
164 if (result.is_single_value()) {
166 return;
167 }
168
169 gpu::Shader *shader = this->get_shader("compositor_write_output",
172
173 const Bounds<int2> bounds = this->get_camera_region();
174 GPU_shader_uniform_2iv(shader, "lower_bound", bounds.min);
175 GPU_shader_uniform_2iv(shader, "upper_bound", bounds.max);
176
177 result.bind_as_texture(shader, "input_tx");
178
179 const int image_unit = GPU_shader_get_sampler_binding(shader, "output_img");
180 GPU_texture_image_bind(output, image_unit);
181
183
184 result.unbind_as_texture();
187 }
188
190 {
191 using namespace compositor;
192
193 /* Realize the result on the compositing domain if needed. */
194 const Domain compositing_domain = this->get_compositing_domain();
195 const InputDescriptor input_descriptor = {ResultType::Color,
196 InputRealizationMode::OperationDomain};
197 SimpleOperation *realization_operation = RealizeOnDomainOperation::construct_if_needed(
198 *this, result, input_descriptor, compositing_domain);
199
200 if (!realization_operation) {
201 this->write_output(result);
202 return;
203 }
204
205 Result realize_input = this->create_result(result.type(), result.precision());
206 realize_input.share_data(result);
207 realization_operation->map_input_to_result(&realize_input);
208 realization_operation->evaluate();
209
210 Result &realized_result = realization_operation->get_result();
211 this->write_output(realized_result);
212 realized_result.release();
213 delete realization_operation;
214 }
215
217 {
219 invalid_pass.allocate_invalid();
220 return invalid_pass;
221 }
222
223 /* Get the pass that corresponds to the given pass name. If no pass with the given name exists,
224 * returns an unallocated result instead. */
225 compositor::Result get_pass_result(const char *pass_name)
226 {
227 /* Return the pass that was written by the engine if such pass was found. */
228 if (DRW_viewport_pass_texture_exists(pass_name)) {
229 gpu::Texture *pass_texture = DRW_viewport_pass_texture_get(pass_name).gpu_texture();
230 compositor::Result pass = compositor::Result(*this, GPU_texture_format(pass_texture));
231 pass.share_data(pass_texture);
232 return pass;
233 }
234
235 /* The combined pass is a special case where we return the viewport color texture if the engine
236 * didn't provide a combined pass. */
237 if (STREQ(pass_name, RE_PASSNAME_COMBINED)) {
239 compositor::Result pass = compositor::Result(*this, GPU_texture_format(combined_texture));
240 pass.share_data(combined_texture);
241 return pass;
242 }
243
245 }
246
248 {
249 const char *shader_name = pass.type() == compositor::ResultType::Float ?
250 "compositor_image_crop_float" :
251 "compositor_image_crop_float4";
252 gpu::Shader *shader = this->get_shader(shader_name, pass.precision());
254
255 /* The compositing space is limited to a subset of the pass texture, so only read that
256 * compositing region into an appropriately sized result. */
257 const int2 lower_bound = this->get_camera_region().min;
258 GPU_shader_uniform_2iv(shader, "lower_bound", lower_bound);
259
260 pass.bind_as_texture(shader, "input_tx");
261
262 compositor::Result cropped_pass = this->create_result(pass.type(), pass.precision());
263 cropped_pass.allocate_texture(this->get_compositing_domain());
264 cropped_pass.bind_as_image(shader, "output_img");
265
267
269 pass.unbind_as_texture();
270 cropped_pass.unbind_as_image();
271
272 return cropped_pass;
273 }
274
275 compositor::Result get_pass(const Scene *scene, int view_layer_index, const char *name) override
276 {
277 /* Blender aliases the Image pass name to be the Combined pass, so we return the combined pass
278 * in that case. */
279 const char *pass_name = StringRef(name) == "Image" ? "Combined" : name;
280
281 const Scene *original_scene = DEG_get_original(scene_);
282 if (DEG_get_original(scene) != original_scene) {
283 return this->get_invalid_pass();
284 }
285
286 ViewLayer *view_layer = static_cast<ViewLayer *>(
287 BLI_findlink(&original_scene->view_layers, view_layer_index));
288 if (StringRef(view_layer->name) != DRW_context_get()->view_layer->name) {
289 return this->get_invalid_pass();
290 }
291
292 compositor::Result pass = this->get_pass_result(pass_name);
293 if (!pass.is_allocated()) {
294 return this->get_invalid_pass();
295 }
296
297 /* The pass data window matches the compositing domain data window, so update its display
298 * window and return it as is. */
299 const compositor::Domain compositing_domain = this->get_compositing_domain();
300 if (this->get_camera_region().min == int2(0) &&
301 compositing_domain.data_size == pass.domain().data_size)
302 {
303 pass.domain() = compositing_domain;
304 return pass;
305 }
306
307 return this->crop_pass(pass);
308 }
309
310 StringRef get_view_name() const override
311 {
312 const SceneRenderView *view = static_cast<SceneRenderView *>(
313 BLI_findlink(&get_render_data().views, DRW_context_get()->v3d->multiview_eye));
314 return view->name;
315 }
316
329
330 void evaluate()
331 {
334
335 const int active_view_layer_index = BLI_findstringindex(
336 &scene_->view_layers, DRW_context_get()->view_layer->name, offsetof(ViewLayer, name));
337 compositor::Result combined_pass = this->get_pass(
338 scene_, active_view_layer_index, RE_PASSNAME_COMBINED);
339 operation.map_input_to_result(&combined_pass);
340 operation.evaluate();
341
342 if (!operation.has_output()) {
343 operation.free_results();
344 return;
345 }
346
347 /* If the no viewer output exist, write the output as a viewer. */
348 compositor::Result &output_result = operation.get_result();
349 if (!this->viewer_compute_context()) {
350 this->write_viewer(output_result);
351 }
352
353 output_result.release();
354 }
355};
356
357class Instance : public DrawEngine {
358 private:
359 compositor::StaticCacheManager cache_manager_;
360
361 public:
363 {
364 return "Compositor";
365 }
366
367 void init() final {};
368 void begin_sync() final {};
369 void object_sync(draw::ObjectRef & /*ob_ref*/, draw::Manager & /*manager*/) final {};
370 void end_sync() final {};
371
372 void draw(Manager & /*manager*/) final
373 {
374 bke::ComputeContextCache compute_context_cache;
375 Context context(cache_manager_,
376 compute_context_cache,
377 DEG_get_bmain(DRW_context_get()->depsgraph),
378 DRW_context_get()->scene);
379 if (context.get_camera_region().is_empty()) {
380 return;
381 }
382
384
385#if defined(__APPLE__)
387 /* NOTE(Metal): Isolate Compositor compute work in individual command buffer to improve
388 * workload scheduling. When expensive compositor nodes are in the graph, these can stall out
389 * the GPU for extended periods of time and sub-optimally schedule work for execution. */
390 GPU_flush();
391 }
392#endif
393
394 context.evaluate();
395 context.cache_manager().reset();
396
397#if defined(__APPLE__)
398 /* NOTE(Metal): Following previous flush to break command stream, with compositor command
399 * buffers potentially being heavy, we avoid issuing subsequent commands until compositor work
400 * has completed. If subsequent work is prematurely queued up, the subsequent command buffers
401 * will be blocked behind compositor work and may trigger a command buffer time-out error. As a
402 * result, we should wait for compositor work to complete.
403 *
404 * This is not an efficient approach for peak performance, but a catch-all to prevent command
405 * buffer failure, until the offending cases can be resolved. */
407 GPU_finish();
408 }
409#endif
411 }
412};
413
415{
416 return new Instance();
417}
418
419} // namespace blender::draw::compositor_engine
Camera data-block and utility functions.
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define RE_PASSNAME_COMBINED
#define BLI_assert_unreachable()
#define STREQ(a, b)
#define output
constexpr StringRef()
Result create_result(ResultType type, ResultPrecision precision)
bke::ComputeContextCache & compute_context_cache() const
virtual const RenderData & get_render_data() const
gpu::Shader * get_shader(const char *info_name, ResultPrecision precision)
void share_data(const Result &source)
Definition result.cc:996
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
Definition result.cc:554
void unbind_as_texture() const
Definition result.cc:964
static ResultType type(gpu::TextureFormat format)
Definition result.cc:337
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:854
void unbind_as_image() const
Definition result.cc:990
static ResultPrecision precision(gpu::TextureFormat format)
Definition result.cc:307
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
Definition result.cc:951
gpu::Texture * gpu_texture()
compositor::Result get_pass(const Scene *scene, int view_layer_index, const char *name) override
void write_output(const compositor::Result &result)
compositor::ResultPrecision get_precision() const override
const ComputeContext * viewer_compute_context() const override
Context(compositor::StaticCacheManager &cache_manager, bke::ComputeContextCache &compute_context_cache, const Main *main, const Scene *scene)
void write_viewer(compositor::Result &result) override
const Scene & get_scene() const override
compositor::Result get_pass_result(const char *pass_name)
compositor::Domain get_compositing_domain() const override
compositor::Result crop_pass(const compositor::Result &pass)
compositor::SideEffectOutputTypes needed_side_effect_output_types() const override
void object_sync(draw::ObjectRef &, draw::Manager &) final
#define offsetof(t, d)
#define main()
std::optional< Bounds< T > > intersect(const Bounds< T > &a, const Bounds< T > &b)
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
void GPU_shader_uniform_2iv(gpu::Shader *sh, const char *name, const int data[2])
int GPU_shader_get_sampler_binding(gpu::Shader *shader, const char *name)
gpu::TextureFormat GPU_texture_format(const gpu::Texture *texture)
draw::TextureFromPool & DRW_viewport_pass_texture_get(const char *pass_name)
void GPU_texture_image_unbind(gpu::Texture *texture)
GPUBackendType GPU_backend_get_type()
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
int BLI_findstringindex(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:794
VecBase< int32_t, 2 > int2
Main * DEG_get_bmain(const Depsgraph *graph)
void GPU_shader_unbind()
rctf BKE_camera_view_border(const struct Scene *scene, const struct Depsgraph *depsgraph, const struct View3D *v3d, const struct RegionView3D *rv3d, int winx, int winy, bool no_shift, bool no_zoom, bool no_roll)
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void DRW_submission_end()
const DRWContext * DRW_context_get()
T * DEG_get_original(T *id)
void GPU_flush()
Definition gpu_state.cc:284
void GPU_texture_clear(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
void DRW_submission_start()
void GPU_texture_image_bind(gpu::Texture *texture, int unit)
@ SCE_COMPOSITOR_PRECISION_AUTO
@ SCE_COMPOSITOR_PRECISION_FULL
bool DRW_viewport_pass_texture_exists(const char *pass_name)
void GPU_finish()
Definition gpu_state.cc:289
#define min(a, b)
Definition sort.cc:38
Bounds()=default
DefaultTextureList * viewport_texture_list_get() const
RegionView3D * rv3d
Depsgraph * depsgraph
float2 viewport_size_get() const
bool is_viewport_image_render() const
eRegionView3D_Persp persp
ListBaseT< ViewLayer > view_layers