Skip to content

Commit

Permalink
finish accel update
Browse files Browse the repository at this point in the history
  • Loading branch information
jkxing committed Apr 26, 2024
1 parent e5a6d7c commit 09772c9
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 14 deletions.
11 changes: 7 additions & 4 deletions src/base/differentiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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<geoms_id.size();i++) {
auto &&p = _geometry_params[geoms_id[i]];
Expand All @@ -492,10 +492,13 @@ void Differentiation::update_parameter_from_external(Stream &stream, luisa::vect
//auto [buffer_view, bindlessbuffer_id] = _pipeline.bindless_arena_buffer<Vertex>(buffer_id);
auto length = buffer_view.size();
LUISA_INFO("here length is {}, size is {}, as<vertex> size is {}",length,geoms[geoms_id[i]].view().size(),geoms[geoms_id[i]].view().as<Vertex>().size());
stream << buffer_view.copy_from(geoms[geoms_id[i]].view().as<Vertex>());
stream << buffer_view.copy_from(geoms[geoms_id[i]].view().as<Vertex>()) << 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<void *>, luisa::vector<void *>> Differentiation::get_gradients(Stream &stream) {
Expand Down
8 changes: 6 additions & 2 deletions src/base/differentiation.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,20 @@ class Differentiation {
BufferView<Vertex> _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<Vertex> buffer_view, uint length, uint buffer_id) noexcept
uint counter_offset, BufferView<Vertex> 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; }
[[nodiscard]] auto instance_id() const noexcept { return _instance_id; }
[[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); }
};

Expand Down Expand Up @@ -142,6 +144,7 @@ class Differentiation {

luisa::optional<BufferView<uint>> instance2offset;


private:
auto &pipeline() noexcept { return _pipeline; }

Expand Down Expand Up @@ -184,6 +187,7 @@ class Differentiation {
void set_parameter(CommandBuffer &command_buffer, const GeometryParameter &param, BufferView<float> value) noexcept;

std::tuple<luisa::vector<void*>,luisa::vector<void*>> get_gradients(Stream &stream);
luisa::span<const Shape *const> _shapes;
};

}// namespace luisa::render
17 changes: 10 additions & 7 deletions src/base/geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [&] {
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/base/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ luisa::unique_ptr<Pipeline> Pipeline::create(Device &device, Stream &stream, con
diff->register_optimizer(dynamic_cast<DifferentiableIntegrator::Instance *>(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()) {
Expand Down
2 changes: 1 addition & 1 deletion src/base/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ class Pipeline {
luisa::unordered_map<luisa::string, uint> _named_ids;
// other things
luisa::unique_ptr<Printer> _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;
Expand Down
Binary file modified src/tests/init.exr
Binary file not shown.

0 comments on commit 09772c9

Please sign in to comment.