Blender V5.3
eevee_instance.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "CLG_log.h"
12
13#include "BKE_camera.h"
14#include "BKE_global.hh"
15#include "BKE_object.hh"
16#include "BKE_scene.hh"
17
18#include "BLI_rect.hh"
19#include "BLI_time.hh"
20#include "BLI_timecode.hh"
21
22#include "BLT_translation.hh"
23
25
26#include "DNA_camera_types.h"
28#include "DNA_modifier_types.h"
29
30#include "ED_screen.hh"
31#include "ED_view3d.hh"
32#include "GPU_context.hh"
33#include "GPU_pass.hh"
34#include "GPU_work_in_flight.hh"
35#include "IMB_imbuf_types.hh"
36
37#include "RE_pipeline.h"
38
39#include "eevee_instance.hh"
40
41#include "DNA_particle_types.h"
42
44#include "draw_debug.hh"
45#include "draw_view_data.hh"
46
47namespace blender::eevee {
48
49CLG_LogRef Instance::log = {"eevee"};
50
51void *Instance::debug_scope_render_frame = nullptr;
52void *Instance::debug_scope_render_sample = nullptr;
53void *Instance::debug_scope_irradiance_setup = nullptr;
54void *Instance::debug_scope_irradiance_sample = nullptr;
55
56/* -------------------------------------------------------------------- */
65
67{
68 this->draw_ctx = DRW_context_get();
69
70 Depsgraph *depsgraph = draw_ctx->depsgraph;
73 ARegion *region = draw_ctx->region;
75
78
79 draw::View &default_view = draw::View::default_get();
80
81 Object *camera = nullptr;
82 /* Get render borders. */
83 rcti rect;
84 BLI_rcti_init(&rect, 0, size[0], 0, size[1]);
85 rcti visible_rect = rect;
86 if (v3d) {
87 if (rv3d && (rv3d->persp == RV3D_CAMOB)) {
88 camera = v3d->camera;
89 }
90 /* Panoramic camera need the full film, none of the border resize below apply.
91 * TODO: passepartout crop is unsupported for panoramic camera.
92 * Could reuse the uv_scale/uv_bias remap already used for camera shift (see Camera::sync). */
93 const bool is_panoramic_camera =
94 camera && camera->type == OB_CAMERA &&
95 reinterpret_cast<const blender::Camera *>(camera->data)->type == CAM_PANO;
96
98 if (camera) {
99 if (!is_panoramic_camera && (scene->r.mode & R_BORDER)) {
100 rect.xmin = scene->r.border.xmin * size[0];
101 rect.ymin = scene->r.border.ymin * size[1];
102 rect.xmax = scene->r.border.xmax * size[0];
103 rect.ymax = scene->r.border.ymax * size[1];
104 }
105 }
106 else if (v3d->flag2 & V3D_RENDER_BORDER) {
107 rect.xmin = v3d->render_border.xmin * size[0];
108 rect.ymin = v3d->render_border.ymin * size[1];
109 rect.xmax = v3d->render_border.xmax * size[0];
110 rect.ymax = v3d->render_border.ymax * size[1];
111 }
112 }
113 else {
114 rctf border;
115 if (rv3d && !is_panoramic_camera &&
117 scene, depsgraph, v3d, rv3d, size[0], size[1], &border, nullptr))
118 {
119 BLI_rcti_rctf_copy_floor(&rect, &border);
120 }
121 }
122 /* Clamp it to the viewport area. */
123 rect.xmin = max(rect.xmin, 0);
124 rect.ymin = max(rect.ymin, 0);
125 rect.xmax = min(rect.xmax, size.x);
126 rect.ymax = min(rect.ymax, size.y);
127
129 const float2 vp_size = draw_ctx->viewport_size_get();
130 visible_rect.xmax = vp_size[0];
131 visible_rect.ymax = vp_size[1];
132 visible_rect.xmin = visible_rect.ymin = 0;
133 }
134 else {
135 visible_rect = *ED_region_visible_rect(region);
136 }
137 }
138
139 init(size, &rect, &visible_rect, nullptr, depsgraph, camera, nullptr, &default_view, v3d, rv3d);
140}
141
142void Instance::init(const int2 &output_res,
143 const rcti *output_rect,
144 const rcti *visible_rect,
145 RenderEngine *render_,
146 Depsgraph *depsgraph_,
147 Object *camera_object_,
148 const RenderLayer *render_layer_,
149 View *drw_view_,
150 const View3D *v3d_,
151 const RegionView3D *rv3d_)
152{
153 this->draw_ctx = DRW_context_get();
154
155 render = render_;
156 depsgraph = depsgraph_;
157 camera_orig_object = camera_object_;
158 render_layer = render_layer_;
159 drw_view = drw_view_;
160 v3d = v3d_;
161 rv3d = rv3d_;
163 update_eval_members();
164
165 info_ = "";
166
167 if (is_viewport()) {
175
176 /* Note: Do not update the value here as we use it during sync for checking ID updates. */
177 if (depsgraph_last_update_ != DEG_get_update_count(depsgraph)) {
178 sampling.reset();
179 }
181 {
182 sampling.reset();
183 }
184 if (assign_if_different(debug_mode, eDebugMode(G.debug_value))) {
185 sampling.reset();
186 }
187 if (output_res != film.display_extent_get()) {
188 sampling.reset();
189 }
190 if (output_rect) {
191 int2 offset = int2(output_rect->xmin, output_rect->ymin);
192 int2 extent = int2(BLI_rcti_size_x(output_rect), BLI_rcti_size_y(output_rect));
193 if (offset != film.get_data().offset || extent != film.get_data().extent) {
194 sampling.reset();
195 }
196 }
197 if (assign_if_different(overlays_enabled_, v3d && !(v3d->flag2 & V3D_HIDE_OVERLAYS))) {
198 sampling.reset();
199 }
200 if (is_painting) {
201 sampling.reset();
202 }
204 sampling.reset();
205 }
206 if (is_playback) {
207 sampling.reset();
208 }
209 }
210 else {
211 is_image_render = true;
212 }
213
214 rcti lookdev_rect = *visible_rect;
215 if (is_viewport() && v3d && rv3d && rv3d->persp == RV3D_CAMOB && v3d->camera &&
217 {
218 /* Anchor reference spheres to camera border. */
219 const rctf camera_border = BKE_camera_view_border(scene,
220 depsgraph,
221 v3d,
222 rv3d,
225 false,
226 false,
227 false);
228 BLI_rcti_rctf_copy(&lookdev_rect, &camera_border);
229 }
230
231 anisotropic_filtering = GPU_anisotropic_filtering_flags(scene->r.anisotropic_filter);
232
234 camera.init();
235 film.init(output_res, output_rect);
238 velocity.init();
241 shadows.init();
243 main_view.init();
246 /* Irradiance Cache needs reflection probes to be initialized. */
249 volume.init();
250 lookdev.init(&lookdev_rect);
251
252 /* Request static shaders */
262
266
267 if (is_image_render) {
268 /* Ensure all deferred shaders have been compiled to kick-start asynchronous specialization. */
270 }
271
273 bool ready = shaders.request_specializations(
281 }
282
283 if (is_image_render) {
286 }
287
288 /* Needed bits to be able to display something to the screen. */
289 needed_shaders = shader_request | DEFAULT_MATERIALS;
290
292
293 if (!samples_in_flight) {
296 }
297}
298
300{
301 this->depsgraph = depsgraph;
302 this->manager = manager;
303 camera_orig_object = nullptr;
304 render = nullptr;
305 render_layer = nullptr;
306 drw_view = nullptr;
307 v3d = nullptr;
308 rv3d = nullptr;
309 update_eval_members();
310
311 is_light_bake = true;
312 debug_mode = eDebugMode(G.debug_value);
313 info_ = "";
314
316 camera.init();
317 /* Film isn't used but init to avoid side effects in other module. */
318 rcti empty_rect{0, 0, 0, 0};
319 film.init(int2(1), &empty_rect);
322 velocity.init();
325 shadows.init();
327 main_view.init();
330 /* Irradiance Cache needs reflection probes to be initialized. */
333 volume.init();
334 lookdev.init(&empty_rect);
335
339}
340
341void Instance::set_time(float time)
342{
345 update_eval_members();
346}
347
348void Instance::update_eval_members()
349{
353 nullptr;
354}
355
357
358/* -------------------------------------------------------------------- */
365
367{
368 /* Needs to be first for sun light parameters.
369 * Also not skipped to be able to request world shader.
370 * If engine shaders are not ready, will skip the pipeline sync. */
371 world.sync();
372
373 if (skip_render_) {
374 return;
375 }
376
378 velocity.begin_sync(); /* NOTE: Also syncs camera. */
386
391 main_view.sync();
392 film.sync();
395 lookdev.sync();
396
400
401 if (is_light_bake) {
402 /* Do not use render layer visibility during bake.
403 * NOTE: This is arbitrary and could be changed if needed. */
405 }
406
408 sampling.reset();
409 }
410}
411
412void Instance::object_sync(ObjectRef &ob_ref, Manager & /*manager*/)
413{
414 if (skip_render_) {
415 return;
416 }
417
418 Object *ob = ob_ref.object;
419 const bool is_renderable_type = ELEM(ob->type,
420 OB_CURVES,
422 OB_MESH,
424 OB_VOLUME,
425 OB_LAMP,
427 const int ob_visibility = DRW_object_visibility_in_active_context(ob);
428 const bool partsys_is_visible = (ob_visibility & OB_VISIBLE_PARTICLES) != 0 &&
429 (ob->type == OB_MESH);
430 const bool object_is_visible = DRW_object_is_renderable(ob) &&
431 (ob_visibility & OB_VISIBLE_SELF) != 0;
432
433 if (!is_renderable_type || (!partsys_is_visible && !object_is_visible)) {
434 return;
435 }
436
437 if (partsys_is_visible && ob != draw_ctx->object_edit) {
438 auto sync_hair = [&](const HairParticleInfo &info) { sync.sync_curves(ob_ref, &info); };
439 foreach_hair_particle(*this, ob_ref, sync_hair);
440 }
441
442 if (object_is_visible) {
443 switch (ob->type) {
444 case OB_LAMP:
445 lights.sync_light(ob_ref);
446 break;
447 case OB_MESH:
448 if (!sync.sync_sculpt(ob_ref)) {
449 sync.sync_mesh(ob_ref);
450 }
451 break;
452 case OB_POINTCLOUD:
453 sync.sync_pointcloud(ob_ref);
454 break;
455 case OB_VOLUME:
456 sync.sync_volume(ob_ref);
457 break;
458 case OB_CURVES:
459 sync.sync_curves(ob_ref);
460 break;
461 case OB_LIGHTPROBE:
462 light_probes.sync_probe(ob_ref);
463 break;
464 default:
465 break;
466 }
467 }
468}
469
471{
472 if (skip_render_) {
473 /* We might run in the case where the next check sets skip_render_ to false after the
474 * begin_sync was skipped, which would call `end_sync` function with invalid data. */
475 return;
476 }
477
479 bool use_volume = volume.will_enable();
480
481 ShaderGroups request_bits = NONE;
482 SET_FLAG_FROM_TEST(request_bits, use_sss, SUBSURFACE_SHADERS);
483 SET_FLAG_FROM_TEST(request_bits, use_volume, VOLUME_EVAL_SHADERS);
485 needed_shaders |= request_bits;
486
487 if (is_image_render) {
489 }
490
491 /* Reset temporal accumulation if new textures will be loaded this frame to avoid ghosting. */
493 sampling.reset();
494 }
495
497 volume.end_sync(); /* Needs to be before shadows. */
498 shadows.end_sync(); /* Needs to be before lights. */
502 film.end_sync();
508
509 depsgraph_last_update_ = DEG_get_update_count(depsgraph);
510}
511
513{
515
516 begin_sync();
517
519 render, depsgraph, [this](draw::ObjectRef &ob_ref, RenderEngine *, Depsgraph *) {
520 this->object_sync(ob_ref, *this->manager);
521 });
522
524
525 end_sync();
526
527 manager->end_sync();
528}
529
531{
532 return sphere_probes.update_probes_this_sample_;
533}
534
540
542{
543 return planar_probes.update_probes_;
544}
545
551
553
554/* -------------------------------------------------------------------- */
557
558void Instance::render_sample()
559{
563 film.display();
566 return;
567 }
568
569 /* Motion blur may need to do re-sync after a certain number of sample. */
570 if (!is_viewport() && sampling.do_render_sync()) {
571 render_sync();
575 /* This may need to happen more than once, since actual materials may require more passes
576 * (eg. volume ones) than the fallback material used for queued passes. */
577 /* TODO(@pragma37): There seems to be an issue where multiple `step_object_sync` calls on the
578 * same step can cause mismatching `has_motion` values between sync. */
579 render_sync();
580 }
581 }
582
583 DebugScope debug_scope(debug_scope_render_sample, "EEVEE.render_sample");
584
585 {
586 /* Critical section. Potential gpu::Shader concurrent usage. */
588
589 sampling.step();
592
596
598
600
602 }
604}
605
606void Instance::render_read_result(RenderLayer *render_layer, const char *view_name)
607{
609
610 for (auto i : IndexRange(EEVEE_RENDER_PASS_MAX_BIT + 1)) {
611 eViewLayerEEVEEPassType pass_type = eViewLayerEEVEEPassType(pass_bits & (1 << i));
612 if (pass_type == 0) {
613 continue;
614 }
615
616 Vector<std::string> pass_names = Film::pass_to_render_pass_names(pass_type, view_layer);
617 for (int64_t pass_offset : IndexRange(pass_names.size())) {
618 RenderPass *rp = RE_pass_find_by_name(
619 render_layer, pass_names[pass_offset].c_str(), view_name);
620 if (!rp) {
621 continue;
622 }
623 float *result = film.read_pass(pass_type, pass_offset);
624
625 if (result) {
627 /* WORKAROUND: We use texture read to avoid using a frame-buffer to get the render result.
628 * However, on some implementation, we need a buffer with a few extra bytes for the read to
629 * happen correctly (see #GLTexture::read()). So we need a custom memory allocation. */
630 /* Avoid `memcpy()`, replace the pointer directly. */
633 }
634 }
635 }
636
637 /* AOVs. */
638 for (ViewLayerAOV &aov : view_layer->aovs) {
639 if ((aov.flag & AOV_CONFLICT) != 0) {
640 continue;
641 }
642 RenderPass *rp = RE_pass_find_by_name(render_layer, aov.name, view_name);
643 if (!rp) {
644 continue;
645 }
646 float *result = film.read_aov(&aov);
647
648 if (result) {
650 /* WORKAROUND: We use texture read to avoid using a frame-buffer to get the render result.
651 * However, on some implementation, we need a buffer with a few extra bytes for the read to
652 * happen correctly (see #GLTexture::read()). So we need a custom memory allocation. */
653 /* Avoid #memcpy(), replace the pointer directly. */
656 }
657 }
658
659 /* The vector pass is initialized to weird values. Set it to neutral value if not rendered. */
660 if ((pass_bits & EEVEE_RENDER_PASS_VECTOR) == 0) {
661 for (const std::string &vector_pass_name :
663 {
664 RenderPass *vector_rp = RE_pass_find_by_name(
665 render_layer, vector_pass_name.c_str(), view_name);
666 if (vector_rp) {
667 memset(vector_rp->ibuf->float_data_for_write(),
668 0,
669 sizeof(float) * 4 * vector_rp->rectx * vector_rp->recty);
670 }
671 }
672 }
673}
674
676
677/* -------------------------------------------------------------------- */
680
681void Instance::render_frame(RenderEngine *engine, RenderLayer *render_layer, const char *view_name)
682{
683 skip_render_ = skip_render_ || !is_loaded(needed_shaders);
684
685 if (skip_render_) {
686 if (!info_.empty()) {
687 RE_engine_set_error_message(engine, info_.c_str());
688 info_ = "";
689 }
690 return;
691 }
692
693 DebugScope debug_scope(debug_scope_render_frame, "EEVEE.render_frame");
694
695 /* TODO: Break on RE_engine_test_break(engine) */
696 double start_time = BLI_time_now_seconds();
697 while (!sampling.finished()) {
698 samples_in_flight->begin_work();
699 this->render_sample();
700
701 if ((sampling.sample_index() == 1) || ((sampling.sample_index() % 25) == 0) ||
703 {
704 /* TODO: Use `fmt`. */
705 std::string re_info = "Rendering " + std::to_string(sampling.sample_index()) + " / " +
706 std::to_string(sampling.sample_count()) + " samples";
707 RE_engine_update_stats(engine, nullptr, re_info.c_str());
708 }
709
710 samples_in_flight->end_work();
713
714#if 0
715 /* TODO(fclem) print progression. */
717 /* TODO(fclem): Does not currently work. But would be better to just display to 2D view like
718 * cycles does. */
719 if (G.background == false && first_read) {
720 /* Allow to preview the first sample. */
721 /* TODO(fclem): Might want to not do this during animation render to avoid too much stall. */
722 this->render_read_result(render_layer, view_name);
723 first_read = false;
725 /* Allow the 2D viewport to grab the ticket mutex to display the render. */
727 }
728#endif
729 }
730
731 this->film.cryptomatte_sort();
732
733 this->render_read_result(render_layer, view_name);
734
735 if (!is_viewport()) {
736 double time_elapsed = BLI_time_now_seconds() - start_time;
737 std::string message = fmt::format(
738 "Rendered {} samples in {:.6f} seconds", sampling.sample_index(), time_elapsed);
739 CLOG_INFO(&Instance::log, "%s", message.c_str());
740 }
741
742 if (!info_.empty()) {
744 engine, RPT_("Errors during render. See the System Console for more info."));
745 printf("%s", info_.c_str());
746 info_ = "";
747 }
748}
749
751{
752 if (skip_render_ || !is_loaded(needed_shaders)) {
756 info_append_i18n("Compiling EEVEE engine shaders");
758 }
759 /* Do not swap if the velocity module didn't go through a full sync cycle. */
761 /* The velocity module can reference some gpu::Batch. Calling this function
762 * make sure we release these references and don't de-reference them later as
763 * they might have been freed. */
765 }
766 return;
767 }
768
769 render_sample();
771
774 }
775
776 /* Do not request redraw during viewport animation to lock the frame-rate to the animation
777 * playback rate. This is in order to preserve motion blur aspect and also to avoid TAA reset
778 * that can show flickering. */
781 }
782
785 info_append_i18n("Loading textures ({} remaining)", materials.queued_textures_count);
786 }
788 info_append_i18n("Compiling shaders ({} remaining)", materials.queued_shaders_count);
790 /* Only recommend subprocesses when there is known gain. */
794 {
796 "Setting Preferences > System > Shader Compilation Method to Subprocess might improve "
797 "compilation time.");
798 }
799 }
801 }
803 info_append_i18n("Optimizing shaders ({} remaining)", materials.queued_optimize_shaders_count);
804 }
805}
806
808{
809 if (skip_render_) {
810 return;
811 }
812
813 do {
814 /* Render at least once to blit the finished image. */
815 samples_in_flight->begin_work();
816 this->render_sample();
817 samples_in_flight->end_work();
819 } while (!sampling.finished_viewport());
821
824 }
825}
826
839
841{
842 if (skip_render_) {
843 return;
844 }
845 cryptomatte.store_metadata(render_result);
846}
847
849{
851
852#define CHECK_PASS_LEGACY(name, type, channels, chanid) \
853 if (view_layer->passflag & (SCE_PASS_##name)) { \
854 RE_engine_register_pass( \
855 engine, scene, view_layer, RE_PASSNAME_##name, channels, chanid, type); \
856 } \
857 ((void)0)
858#define CHECK_PASS_EEVEE(name, type, channels, chanid) \
859 if (view_layer->eevee.render_passes & (EEVEE_RENDER_PASS_##name)) { \
860 RE_engine_register_pass( \
861 engine, scene, view_layer, RE_PASSNAME_##name, channels, chanid, type); \
862 } \
863 ((void)0)
864#define CHECK_PASS_DENOISING(name, type, channels, chanid) \
865 if (view_layer->eevee.denoising_pass_flags & (EEVEE_DENOISING_PASS_STORE)) { \
866 RE_engine_register_pass( \
867 engine, scene, view_layer, RE_PASSNAME_##name, channels, chanid, type); \
868 } \
869 ((void)0)
870
871 CHECK_PASS_LEGACY(DEPTH, SOCK_FLOAT, 1, "Z");
872 CHECK_PASS_LEGACY(MIST, SOCK_FLOAT, 1, "Z");
874 CHECK_PASS_LEGACY(POSITION, SOCK_VECTOR, 3, "XYZ");
876 CHECK_PASS_LEGACY(DIFFUSE_DIRECT, SOCK_RGBA, 3, "RGB");
877 CHECK_PASS_LEGACY(DIFFUSE_COLOR, SOCK_RGBA, 3, "RGB");
878 CHECK_PASS_LEGACY(GLOSSY_DIRECT, SOCK_RGBA, 3, "RGB");
879 CHECK_PASS_LEGACY(GLOSSY_COLOR, SOCK_RGBA, 3, "RGB");
880 CHECK_PASS_EEVEE(VOLUME_LIGHT, SOCK_RGBA, 3, "RGB");
881 CHECK_PASS_LEGACY(EMIT, SOCK_RGBA, 3, "RGB");
882 CHECK_PASS_LEGACY(ENVIRONMENT, SOCK_RGBA, 3, "RGB");
883 CHECK_PASS_LEGACY(SHADOW, SOCK_RGBA, 3, "RGB");
884 CHECK_PASS_LEGACY(AO, SOCK_RGBA, 3, "RGB");
885 CHECK_PASS_EEVEE(TRANSPARENT, SOCK_RGBA, 4, "RGBA");
886 CHECK_PASS_DENOISING(DENOISING_DEPTH, SOCK_FLOAT, 1, "X");
887 CHECK_PASS_DENOISING(DENOISING_NORMAL, SOCK_VECTOR, 3, "XYZ");
888 CHECK_PASS_DENOISING(DENOISING_ROUGHNESS, SOCK_FLOAT, 1, "X");
889 CHECK_PASS_DENOISING(DENOISING_DIFFUSE_ALBEDO, SOCK_RGBA, 3, "RGB");
890 CHECK_PASS_DENOISING(DENOISING_SPECULAR_ALBEDO, SOCK_RGBA, 3, "RGB");
891
892 for (ViewLayerAOV &aov : view_layer->aovs) {
893 if ((aov.flag & AOV_CONFLICT) != 0) {
894 continue;
895 }
896 switch (aov.type) {
897 case AOV_TYPE_COLOR:
898 RE_engine_register_pass(engine, scene, view_layer, aov.name, 4, "RGBA", SOCK_RGBA);
899 break;
900 case AOV_TYPE_VALUE:
902 break;
903 default:
904 break;
905 }
906 }
907
908 /* NOTE: Name channels lowercase `rgba` so that compression rules check in OpenEXR DWA code uses
909 * lossless compression. Reportedly this naming is the only one which works good from the
910 * interoperability point of view. Using `xyzw` naming is not portable. */
911 auto register_cryptomatte_passes = [&](eViewLayerCryptomatteFlags cryptomatte_layer,
912 eViewLayerEEVEEPassType eevee_pass) {
913 if (view_layer->cryptomatte_flag & cryptomatte_layer) {
914 for (const std::string &pass_name : Film::pass_to_render_pass_names(eevee_pass, view_layer))
915 {
917 engine, scene, view_layer, pass_name.c_str(), 4, "rgba", SOCK_RGBA);
918 }
919 }
920 };
923 register_cryptomatte_passes(VIEW_LAYER_CRYPTOMATTE_MATERIAL,
925}
926
928 Object &probe,
929 FunctionRef<void()> context_enable,
930 FunctionRef<void()> context_disable,
931 FunctionRef<bool()> stop,
932 FunctionRef<void(LightProbeGridCacheFrame *, float progress)> result_update)
933{
935
937 this->draw_ctx = &draw_ctx;
938
939 auto custom_pipeline_wrapper = [&](FunctionRef<void()> callback) {
940 context_enable();
942 callback();
944 context_disable();
945 };
946
947 auto context_wrapper = [&](FunctionRef<void()> callback) {
948 context_enable();
949 callback();
950 context_disable();
951 };
952
953 volume_probes.bake.init(probe);
954
955 custom_pipeline_wrapper([&]() {
957 this->render_sync();
961 /* This may need to happen more than once, since actual materials may require more passes
962 * (eg. volume ones) than the fallback material used for queued passes. */
963 /* TODO(@pragma37): There seems to be an issue where multiple `step_object_sync` calls on the
964 * same step can cause mismatching `has_motion` values between sync. */
965 render_sync();
966 }
967 /* Sampling module needs to be initialized to computing lighting. */
968 sampling.init(probe);
969 sampling.step();
971
972 {
973 /* Critical section. Potential gpu::Shader concurrent usage. */
975
976 DebugScope debug_scope(debug_scope_irradiance_setup, "EEVEE.irradiance_setup");
977
979
981
984 return;
985 }
986
988
991
993 }
994
995 /* Avoid big setup job to be queued with the sampling commands. */
996 GPU_flush();
997 });
998
1000 return;
1001 }
1002
1003 sampling.init(probe);
1004
1005 /* Start with 1 sample and progressively ramp up. */
1006 float time_per_sample_ms_smooth = 16.0f;
1007 double last_update_timestamp = BLI_time_now_seconds();
1008 while (!sampling.finished()) {
1009 context_wrapper([&]() {
1010 DebugScope debug_scope(debug_scope_irradiance_sample, "EEVEE.irradiance_sample");
1011
1012 int remaining_samples = sampling.sample_count() - sampling.sample_index();
1013 /* In background mode, assume we don't need as much interactivity. */
1014 int time_budget_ms = G.background ? 32 : 16;
1015 /* Batch ray cast. Avoids too much overhead of the context switch. */
1016 int sample_count_in_batch = ceilf(time_budget_ms / max(0.1f, time_per_sample_ms_smooth));
1017 /* Avoid batching too many rays, keep system responsive in case of bad values. */
1018 sample_count_in_batch = std::min({32, sample_count_in_batch, remaining_samples});
1019
1020 CLOG_INFO(&Instance::log, "IrradianceBake: Casting %d rays.", sample_count_in_batch);
1021
1022 double time_it_begin_ms = BLI_time_now_seconds() * 1000.0;
1023 for (int i = 0; i < sample_count_in_batch && !sampling.finished(); i++) {
1024 sampling.step();
1025 {
1026 /* Critical section. Potential gpu::Shader concurrent usage. */
1028
1032
1034 }
1035 };
1036 /* We use GPU_finish to take into account the GPU processing time. */
1037 /* TODO(fclem): Could use timer queries to keep pipelining of GPU commands if that become a
1038 * real bottleneck. */
1039 GPU_finish();
1040 double time_it_end_ms = BLI_time_now_seconds() * 1000.0;
1041
1042 float time_per_sample_ms = float(time_it_end_ms - time_it_begin_ms) / sample_count_in_batch;
1043 /* Exponential average. */
1044 time_per_sample_ms_smooth = interpolate(time_per_sample_ms_smooth, time_per_sample_ms, 0.7f);
1045
1046 if (sampling.finished()) {
1047 result_update(volume_probes.bake.read_result_packed(), 1.0f);
1048 }
1049 else {
1050 double time_since_last_update_ms = BLI_time_now_seconds() - last_update_timestamp;
1051 /* Only readback every 1 second. This readback is relatively expensive. */
1052 if (time_since_last_update_ms > 1.0) {
1053 float progress = sampling.sample_index() / float(sampling.sample_count());
1054 result_update(volume_probes.bake.read_result_unpacked(), progress);
1055 last_update_timestamp = BLI_time_now_seconds();
1056 }
1057 }
1058 });
1059
1060 if (stop()) {
1061 return;
1062 }
1063 }
1064}
1065
1067
1068} // namespace blender::eevee
Camera data-block and utility functions.
General operations, lookup, etc. for blender objects.
Platform independent time functions.
#define RPT_(msgid)
#define CLOG_INFO(clg_ref,...)
Definition CLG_log.h:190
#define EEVEE_RENDER_PASS_MAX_BIT
#define RE_PASSNAME_COMBINED
#define SET_FLAG_FROM_TEST(value, test, flag)
#define ELEM(...)
btScalar max
constexpr IndexRange()=default
void begin_sync(Object *object_active=nullptr)
bool has_deferred_textures() const
static View & default_get()
Definition draw_view.cc:317
void store_metadata(RenderResult *render_result)
static bool do_merge_direct_indirect_eval(const Instance &inst)
static bool do_split_direct_indirect_radiance(const Instance &inst)
eClosureBits closure_bits_get() const
int2 display_extent_get() const
static const Vector< std::string > pass_to_render_pass_names(eViewLayerEEVEEPassType pass_type, const ViewLayer *view_layer)
float * read_pass(eViewLayerEEVEEPassType pass_type, int layer_offset)
bool is_valid_render_extent() const
void write_viewport_compositor_passes()
const FilmData & get_data()
void init(const int2 &full_extent, const rcti *output_rect)
float * read_aov(ViewLayerAOV *aov)
eViewLayerEEVEEPassType enabled_passes_get() const
VolumeProbeModule volume_probes
void render_frame(RenderEngine *engine, RenderLayer *render_layer, const char *view_name)
void draw(Manager &manager) final
SphereProbeModule sphere_probes
AmbientOcclusion ambient_occlusion
void info_append_i18n(const char *msg, Args &&...args)
SubsurfaceModule subsurface
void light_bake_irradiance(Object &probe, FunctionRef< void()> context_enable, FunctionRef< void()> context_disable, FunctionRef< bool()> stop, FunctionRef< void(LightProbeGridCacheFrame *, float progress)> result_update)
bool needs_lightprobe_sphere_passes() const
bool needs_planar_probe_passes() const
gpu::WorkInFlightPtr samples_in_flight
void object_sync(ObjectRef &ob_ref, Manager &manager) final
const RenderLayer * render_layer
void init(const int2 &output_res, const rcti *output_rect, const rcti *visible_rect, RenderEngine *render, Depsgraph *depsgraph, Object *camera_object=nullptr, const RenderLayer *render_layer=nullptr, View *drw_view_=nullptr, const View3D *v3d=nullptr, const RegionView3D *rv3d=nullptr)
const DRWContext * draw_ctx
GPUSamplerFiltering anisotropic_filtering
const RegionView3D * rv3d
void store_metadata(RenderResult *render_result)
PlanarProbeModule planar_probes
static void update_passes(RenderEngine *engine, Scene *scene, ViewLayer *view_layer)
bool is_loaded(ShaderGroups groups) const
bool do_lightprobe_sphere_sync() const
UniformDataModule uniform_data
LightProbeModule light_probes
MotionBlurModule motion_blur
void init_light_bake(Depsgraph *depsgraph, draw::Manager *manager)
void init(const Object &probe_object)
LightProbeGridCacheFrame * read_result_packed()
LightProbeGridCacheFrame * read_result_unpacked()
void surfels_create(const Object &probe_object)
void sync_light(const ObjectRef &ob_ref)
void sync_probe(const ObjectRef &ob_ref)
void init(const rcti *visible_rect)
ShaderGroups default_materials_load_async()
ShaderGroups default_materials_wait_ready()
uint64_t sample_index() const
uint64_t sample_count() const
void init(const Scene *scene)
ShaderGroups static_shaders_wait_ready(ShaderGroups request_bits)
bool request_specializations(bool block_until_ready, int render_buffers_shadow_id, int shadow_ray_count, int shadow_ray_step_count, bool use_split_indirect, bool use_lightprobe_eval)
ShaderGroups static_shaders_load_async(ShaderGroups request_bits)
const ShadowSceneData & get_data()
void sync_curves(const ObjectRef &ob_ref, struct HairParticleInfo const *hair_particle=nullptr)
void sync_mesh(const ObjectRef &ob_ref)
void sync_pointcloud(const ObjectRef &ob_ref)
void sync_volume(const ObjectRef &ob_ref)
bool sync_sculpt(const ObjectRef &ob_ref)
static WorkInFlightPtr create(unsigned int max_in_flight)
Simple API to draw debug shapes and log in the viewport.
#define CHECK_PASS_EEVEE(name, type, channels, chanid)
#define CHECK_PASS_LEGACY(name, type, channels, chanid)
#define CHECK_PASS_DENOISING(name, type, channels, chanid)
Quaternion interpolate(Quaternion a, Quaternion b, float t)
void drw_debug_clear()
Definition draw_debug.cc:80
void foreach_hair_particle(Instance &inst, ObjectRef &ob_ref, HairHandleCallback callback)
void RE_engine_update_stats(RenderEngine *engine, const char *stats, const char *info)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void DRW_custom_pipeline_end(DRWContext &draw_ctx)
__int64 int64_t
Definition BKE_text.h:90
void DRW_render_context_enable(Render *render)
char * STRNCPY(char(&dst)[N], const char *src)
void DRW_render_context_disable(Render *render)
bool assign_if_different(T &old_value, T &&new_value)
uint64_t DEG_get_update_count(const Depsgraph *depsgraph)
Definition depsgraph.cc:356
bool GPU_use_subprocess_compilation()
bool DRW_object_is_renderable(const Object *ob)
bool BKE_camera_view_render_border(const struct Scene *scene, const struct Depsgraph *depsgraph, const struct View3D *v3d, const struct RegionView3D *rv3d, int winx, int winy, rctf *r_border, rctf *r_unrolled_border)
void BLI_mutex_unlock(ThreadMutex *mutex)
Definition threads.cc:353
GPUBackendType GPU_backend_get_type()
void BLI_rcti_rctf_copy(struct rcti *dst, const struct rctf *src)
void GPU_render_step(bool force_resource_release=false)
MINLINE float fractf(float a)
const rcti * ED_region_visible_rect(ARegion *region)
Definition area.cc:4791
@ GPU_DRIVER_OFFICIAL
int DRW_object_visibility_in_active_context(const Object *ob)
void DRW_render_object_iter(RenderEngine *engine, Depsgraph *depsgraph, std::function< void(draw::ObjectRef &, RenderEngine *, Depsgraph *)> callback)
void DRW_render_set_time(RenderEngine *engine, Depsgraph *depsgraph, int frame, float subframe)
Global G
Definition blender.cc:57
void RE_engine_update_progress(RenderEngine *engine, float progress)
@ SCE_EEVEE_SHADOW_JITTERED_VIEWPORT
bool GPU_type_matches(GPUDeviceType device, GPUOSType os, GPUDriverType driver)
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
void BLI_mutex_lock(ThreadMutex *mutex)
Definition threads.cc:348
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
void RE_engine_register_pass(RenderEngine *engine, Scene *scene, ViewLayer *view_layer, const char *name, int channels, const char *chanid, eNodeSocketDatatype type)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
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)
int GPU_texture_width(const gpu::Texture *texture)
void GPU_framebuffer_viewport_reset(gpu::FrameBuffer *fb)
void RE_pass_set_buffer_data(RenderPass *pass, float *data)
ViewLayer * DEG_get_evaluated_view_layer(const Depsgraph *graph)
void DRW_custom_pipeline_begin(DRWContext &draw_ctx, Depsgraph *)
void DRW_submission_end()
const DRWContext * DRW_context_get()
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
void BLI_rcti_init(struct rcti *rect, int xmin, int xmax, int ymin, int ymax)
Definition rct.cc:428
RenderPass * RE_pass_find_by_name(RenderLayer *rl, const char *name, const char *viewname)
void RE_engine_set_error_message(RenderEngine *engine, const char *msg)
void GPU_pass_cache_wait_for_all()
Definition gpu_pass.cc:320
void GPU_flush()
Definition gpu_state.cc:284
@ EEVEE_RENDER_PASS_CRYPTOMATTE_ASSET
@ EEVEE_RENDER_PASS_CRYPTOMATTE_MATERIAL
@ EEVEE_RENDER_PASS_VECTOR
@ EEVEE_RENDER_PASS_CRYPTOMATTE_OBJECT
int GPU_texture_height(const gpu::Texture *texture)
@ VIEW_LAYER_CRYPTOMATTE_ASSET
@ VIEW_LAYER_CRYPTOMATTE_MATERIAL
@ VIEW_LAYER_CRYPTOMATTE_OBJECT
void DRW_submission_start()
draw::Manager * DRW_manager_get()
double BLI_time_now_seconds()
Definition time.cc:30
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
void GPU_framebuffer_clear_color_depth(gpu::FrameBuffer *fb, const double4 clear_col, float clear_depth)
void GPU_finish()
Definition gpu_state.cc:289
@ GPU_DEVICE_INTEL
@ GPU_DEVICE_NVIDIA
VecBase< double, 4 > double4
@ OB_VISIBLE_PARTICLES
@ OB_VISIBLE_SELF
void DRW_viewport_request_redraw()
void BLI_rcti_rctf_copy_floor(struct rcti *dst, const struct rctf *src)
#define floorf
#define ceilf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
bool is_transforming() const
bool is_viewport_xr() const
DefaultTextureList * viewport_texture_list_get() const
RegionView3D * rv3d
bool is_navigating() const
bool is_painting() const
Depsgraph * depsgraph
bool is_playback() const
bool is_image_render() const
float2 viewport_size_get() const
DefaultFramebufferList * viewport_framebuffer_list_get() const
bool is_viewport_compositor_used() const
bool is_viewport_image_render() const
char info[GPU_INFO_SIZE]
Definition DRW_render.hh:74
eRegionView3D_Persp persp
ThreadMutex update_render_passes_mutex
Definition RE_engine.h:169
struct Render * re
Definition RE_engine.h:148
eSceneEEVEE_Flag flag
struct RenderData r
struct SceneEEVEE eevee
eView3D_Flag2 flag2
struct Object * camera
eViewLayerAOVFlag flag
eViewLayerAOVType type
ListBaseT< ViewLayerAOV > aovs
eViewLayerCryptomatteFlags cryptomatte_flag