diff --git a/src/base/differentiation.cpp b/src/base/differentiation.cpp index b60fbd62..5d7c1388 100644 --- a/src/base/differentiation.cpp +++ b/src/base/differentiation.cpp @@ -464,7 +464,7 @@ void Differentiation::register_geometry_parameter(const CommandBuffer &command_b _counter_size = (_counter_size + length + 8u) & ~0b11u; _param_buffer_size = (_param_buffer_size + length + 8u) & ~0b11u; _gradient_buffer_size = (_gradient_buffer_size + length + 8u) & ~0b11u; - _geometry_params.emplace_back(param_index, instance_id, grad_offset, param_offset, counter_offset, buffer_view, length, 0u); + _geometry_params.emplace_back(param_index, instance_id, grad_offset, param_offset, counter_offset, buffer_view, length, 0u, mesh.resource); LUISA_INFO("buffer_view size is {}",buffer_view.size()); LUISA_INFO("working here, {}, {}, {}, Mesh with {} triangles.", length, _counter_size, _param_buffer_size, mesh.resource->triangle_count()); @@ -483,7 +483,7 @@ void Differentiation::update_parameter_from_external(Stream &stream, luisa::vect stream << image.copy_from(textures[textures_id[i]]); } - LUISA_INFO("working on update paramaters"); + LUISA_INFO("working on update paramaters {}",geoms_id.size()); // apply geometry parameters for (auto i=0;i(buffer_id); auto length = buffer_view.size(); LUISA_INFO("here length is {}, size is {}, as size is {}",length,geoms[geoms_id[i]].view().size(),geoms[geoms_id[i]].view().as().size()); - stream << buffer_view.copy_from(geoms[geoms_id[i]].view().as()); + stream << buffer_view.copy_from(geoms[geoms_id[i]].view().as()) << synchronize() << p.mesh()->build() << synchronize(); _is_dirty = true; } - stream << synchronize(); + + //CommandBuffer command_buffer{&stream}; + //pipeline().geometry()->build(command_buffer, _shapes, pipeline()._initial_time); + //stream << synchronize(); } std::tuple, luisa::vector> Differentiation::get_gradients(Stream &stream) { diff --git a/src/base/differentiation.h b/src/base/differentiation.h index 1a9413c9..0eba0086 100644 --- a/src/base/differentiation.h +++ b/src/base/differentiation.h @@ -68,11 +68,12 @@ class Differentiation { BufferView _buffer_view; uint _length; uint _buffer_id; + Mesh *_mesh; public: GeometryParameter(uint index, uint instance_id, uint grad_offset, uint param_offset, - uint counter_offset, BufferView buffer_view, uint length, uint buffer_id) noexcept + uint counter_offset, BufferView buffer_view, uint length, uint buffer_id, Mesh *mesh) noexcept : _index(index), _instance_id{instance_id}, _grad_offset{grad_offset}, _param_offset{param_offset}, - _counter_offset{counter_offset}, _buffer_view{buffer_view}, _length(length), _buffer_id(buffer_id) {} + _counter_offset{counter_offset}, _buffer_view{buffer_view}, _length(length), _buffer_id(buffer_id), _mesh(mesh) {} [[nodiscard]] auto index() const noexcept { return _index; } [[nodiscard]] auto buffer() const noexcept { return _buffer_view; } [[nodiscard]] auto buffer_id() const noexcept { return _buffer_id; } @@ -80,6 +81,7 @@ class Differentiation { [[nodiscard]] auto gradient_buffer_offset() const noexcept { return _grad_offset; } [[nodiscard]] auto param_offset() const noexcept { return _param_offset; } [[nodiscard]] auto counter_offset() const noexcept { return _counter_offset; } + [[nodiscard]] auto mesh() const noexcept { return _mesh; } [[nodiscard]] auto identifier() const noexcept { return luisa::format("diff_geom({})", _index); } }; @@ -142,6 +144,7 @@ class Differentiation { luisa::optional> instance2offset; + private: auto &pipeline() noexcept { return _pipeline; } @@ -184,6 +187,7 @@ class Differentiation { void set_parameter(CommandBuffer &command_buffer, const GeometryParameter ¶m, BufferView value) noexcept; std::tuple,luisa::vector> get_gradients(Stream &stream); + luisa::span _shapes; }; }// namespace luisa::render diff --git a/src/base/geometry.cpp b/src/base/geometry.cpp index aaeb8cbe..f383db67 100644 --- a/src/base/geometry.cpp +++ b/src/base/geometry.cpp @@ -48,6 +48,9 @@ void Geometry::_process_shape( } auto mesh = [&] { if (auto iter = _meshes.find(shape); iter != _meshes.end()) { + command_buffer << compute::commit() + << iter->second.resource->build() + << compute::commit(); return iter->second; } auto mesh_geom = [&] { @@ -190,14 +193,14 @@ bool Geometry::update(CommandBuffer &command_buffer, float time) noexcept { if(_pipeline.differentiable()) { if (_pipeline.differentiation()->is_dirty()) { - for (auto t : _pipeline.differentiation()->geometry_parameters()) { - _accel.set_prim_handle(t.instance_id(), (uint64_t)t.buffer().native_handle()); - } - _pipeline.differentiation()->clear_dirty(); + //for (auto t : _pipeline.differentiation()->geometry_parameters()) { + // _accel.set_prim_handle(t.instance_id(), (uint64_t)t.buffer().native_handle()); + //} + //_pipeline.differentiation()->clear_dirty(); + LUISA_INFO("start build accel"); + command_buffer << _accel.build() << synchronize(); + LUISA_INFO("end build accel"); } - LUISA_INFO("start build accel"); - command_buffer << _accel.build() << synchronize(); - LUISA_INFO("end build accel"); } return updated; } diff --git a/src/base/pipeline.cpp b/src/base/pipeline.cpp index d68ecb55..4d5d752a 100644 --- a/src/base/pipeline.cpp +++ b/src/base/pipeline.cpp @@ -96,6 +96,7 @@ luisa::unique_ptr Pipeline::create(Device &device, Stream &stream, con diff->register_optimizer(dynamic_cast(pipeline->_integrator.get())->optimizer()); LUISA_INFO_WITH_LOCATION("middle _differentiation build."); diff->materialize(command_buffer); + diff->_shapes = scene.shapes(); } LUISA_INFO_WITH_LOCATION("finish _differentiation build."); if (!pipeline->_transforms.empty()) { diff --git a/src/base/pipeline.h b/src/base/pipeline.h index 00ac3223..b2d57c17 100644 --- a/src/base/pipeline.h +++ b/src/base/pipeline.h @@ -97,10 +97,10 @@ class Pipeline { luisa::unordered_map _named_ids; // other things luisa::unique_ptr _printer; - float _initial_time{}; bool _any_dynamic_transforms{false}; public: + float _initial_time{}; // for internal use only; use Pipeline::create() instead explicit Pipeline(Device &device) noexcept; Pipeline(Pipeline &&) noexcept = delete; diff --git a/src/tests/init.exr b/src/tests/init.exr index 6f54c029..790cdfb3 100644 Binary files a/src/tests/init.exr and b/src/tests/init.exr differ