diff options
author | Alexander Gavrilov <angavrilov@gmail.com> | 2022-09-12 00:30:58 +0300 |
---|---|---|
committer | Alexander Gavrilov <angavrilov@gmail.com> | 2022-10-08 16:43:18 +0300 |
commit | f61ff22967c5f3be2d5f661ce2d455e3e9d39f0a (patch) | |
tree | cd604a87c4f9538dc2e0656b26eb1586ed497a5f /source/blender/draw/engines | |
parent | a716e696584e90fc8ccb6b2af09fd15190af7f05 (diff) |
Attribute Node: support accessing attributes of View Layer and Scene.
The attribute node already allows accessing attributes associated
with objects and meshes, which allows changing the behavior of the
same material between different objects or instances. The same idea
can be extended to an even more global level of layers and scenes.
Currently view layers provide an option to replace all materials
with a different one. However, since the same material will be applied
to all objects in the layer, varying the behavior between layers while
preserving distinct materials requires duplicating objects.
Providing access to properties of layers and scenes via the attribute
node enables making materials with built-in switches or settings that
can be controlled globally at the view layer level. This is probably
most useful for complex NPR shading and compositing. Like with objects,
the node can also access built-in scene properties, like render resolution
or FOV of the active camera. Lookup is also attempted in World, similar
to how the Object mode checks the Mesh datablock.
In Cycles this mode is implemented by replacing the attribute node with
the attribute value during sync, allowing constant folding to take the
values into account. This means however that materials that use this
feature have to be re-synced upon any changes to scene, world or camera.
The Eevee version uses a new uniform buffer containing a sorted array
mapping name hashes to values, with binary search lookup. The array
is limited to 512 entries, which is effectively limitless even
considering it is shared by all materials in the scene; it is also
just 16KB of memory so no point trying to optimize further.
The buffer has to be rebuilt when new attributes are detected in a
material, so the draw engine keeps a table of recently seen attribute
names to minimize the chance of extra rebuilds mid-draw.
Differential Revision: https://developer.blender.org/D15941
Diffstat (limited to 'source/blender/draw/engines')
6 files changed, 50 insertions, 0 deletions
diff --git a/source/blender/draw/engines/eevee_next/eevee_instance.cc b/source/blender/draw/engines/eevee_next/eevee_instance.cc index 8005b27c30e..a0bdf70e254 100644 --- a/source/blender/draw/engines/eevee_next/eevee_instance.cc +++ b/source/blender/draw/engines/eevee_next/eevee_instance.cc @@ -106,6 +106,8 @@ void Instance::begin_sync() gpencil_engine_enabled = false; + scene_sync(); + depth_of_field.sync(); motion_blur.sync(); hiz_buffer.sync(); @@ -115,6 +117,21 @@ void Instance::begin_sync() film.sync(); } +void Instance::scene_sync() +{ + SceneHandle &sc_handle = sync.sync_scene(scene); + + sc_handle.reset_recalc_flag(); + + /* This refers specifically to the Scene camera that can be accessed + * via View Layer Attribute nodes, rather than the actual render camera. */ + if (scene->camera != nullptr) { + ObjectHandle &ob_handle = sync.sync_object(scene->camera); + + ob_handle.reset_recalc_flag(); + } +} + void Instance::object_sync(Object *ob) { const bool is_renderable_type = ELEM(ob->type, OB_CURVES, OB_GPENCIL, OB_MESH, OB_LAMP); diff --git a/source/blender/draw/engines/eevee_next/eevee_instance.hh b/source/blender/draw/engines/eevee_next/eevee_instance.hh index c8eecbd812d..bc610cd0642 100644 --- a/source/blender/draw/engines/eevee_next/eevee_instance.hh +++ b/source/blender/draw/engines/eevee_next/eevee_instance.hh @@ -162,6 +162,7 @@ class Instance { void render_sample(); void render_read_result(RenderLayer *render_layer, const char *view_name); + void scene_sync(); void mesh_sync(Object *ob, ObjectHandle &ob_handle); void update_eval_members(); diff --git a/source/blender/draw/engines/eevee_next/eevee_material.cc b/source/blender/draw/engines/eevee_next/eevee_material.cc index b63002df2e3..d190a1f17a3 100644 --- a/source/blender/draw/engines/eevee_next/eevee_material.cc +++ b/source/blender/draw/engines/eevee_next/eevee_material.cc @@ -187,6 +187,8 @@ MaterialPass MaterialModule::material_pass_get(Object *ob, /* Returned material should be ready to be drawn. */ BLI_assert(GPU_material_status(matpass.gpumat) == GPU_MAT_SUCCESS); + inst_.manager->register_layer_attributes(matpass.gpumat); + if (GPU_material_recalc_flag_get(matpass.gpumat)) { inst_.sampling.reset(); } @@ -217,6 +219,9 @@ MaterialPass MaterialModule::material_pass_get(Object *ob, matpass.sub_pass = &shader_sub->sub(GPU_material_get_name(matpass.gpumat)); matpass.sub_pass->material_set(*inst_.manager, matpass.gpumat); } + else { + matpass.sub_pass = nullptr; + } } return matpass; diff --git a/source/blender/draw/engines/eevee_next/eevee_sync.cc b/source/blender/draw/engines/eevee_next/eevee_sync.cc index f2daba48d7a..08cda6f47cf 100644 --- a/source/blender/draw/engines/eevee_next/eevee_sync.cc +++ b/source/blender/draw/engines/eevee_next/eevee_sync.cc @@ -68,6 +68,20 @@ WorldHandle &SyncModule::sync_world(::World *world) return eevee_dd; } +SceneHandle &SyncModule::sync_scene(::Scene *scene) +{ + DrawEngineType *owner = (DrawEngineType *)&DRW_engine_viewport_eevee_next_type; + struct DrawData *dd = DRW_drawdata_ensure( + (ID *)scene, owner, sizeof(eevee::SceneHandle), draw_data_init_cb, nullptr); + SceneHandle &eevee_dd = *reinterpret_cast<SceneHandle *>(dd); + + const int recalc_flags = ID_RECALC_ALL; + if ((eevee_dd.recalc & recalc_flags) != 0) { + inst_.sampling.reset(); + } + return eevee_dd; +} + /** \} */ /* -------------------------------------------------------------------- */ diff --git a/source/blender/draw/engines/eevee_next/eevee_sync.hh b/source/blender/draw/engines/eevee_next/eevee_sync.hh index ab883ce44c2..eda0342c4b6 100644 --- a/source/blender/draw/engines/eevee_next/eevee_sync.hh +++ b/source/blender/draw/engines/eevee_next/eevee_sync.hh @@ -139,6 +139,15 @@ struct WorldHandle : public DrawData { } }; +struct SceneHandle : public DrawData { + void reset_recalc_flag() + { + if (recalc != 0) { + recalc = 0; + } + } +}; + class SyncModule { private: Instance &inst_; @@ -149,6 +158,7 @@ class SyncModule { ObjectHandle &sync_object(Object *ob); WorldHandle &sync_world(::World *world); + SceneHandle &sync_scene(::Scene *scene); void sync_mesh(Object *ob, ObjectHandle &ob_handle, diff --git a/source/blender/draw/engines/eevee_next/eevee_world.cc b/source/blender/draw/engines/eevee_next/eevee_world.cc index 313c0bda42e..37f90570028 100644 --- a/source/blender/draw/engines/eevee_next/eevee_world.cc +++ b/source/blender/draw/engines/eevee_next/eevee_world.cc @@ -87,6 +87,9 @@ void World::sync() default_tree.nodetree_get(bl_world); GPUMaterial *gpumat = inst_.shaders.world_shader_get(bl_world, ntree); + + inst_.manager->register_layer_attributes(gpumat); + inst_.pipelines.world.sync(gpumat); } |