Skip to content

Commit

Permalink
debug accel update
Browse files Browse the repository at this point in the history
  • Loading branch information
jkxing committed Apr 25, 2024
1 parent d33f999 commit e5a6d7c
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 75 deletions.
13 changes: 13 additions & 0 deletions errlog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Processing d:\code\luisarender2
Installing build dependencies: started
Installing build dependencies: finished with status 'done'
Getting requirements to build wheel: started
Getting requirements to build wheel: finished with status 'done'
Preparing metadata (pyproject.toml): started
Preparing metadata (pyproject.toml): finished with status 'done'
Building wheels for collected packages: luisarender-python
Building wheel for luisarender-python (pyproject.toml): started
Building wheel for luisarender-python (pyproject.toml): finished with status 'done'
Created wheel for luisarender-python: filename=luisarender_python-0.0.4-cp310-cp310-win_amd64.whl size=142570754 sha256=0a7fa8cb4f0185fe0a1958fbcd36bb69b5d25304a9a2ae40a83272ade13d91ca
Stored in directory: C:\Users\jiankai\AppData\Local\Temp\pip-ephem-wheel-cache-b57azy5n\wheels\f6\99\5f\252c215c0d5676460f053881571f097ce199905284240b4ed3
Successfully built luisarender-python
30 changes: 18 additions & 12 deletions src/base/differentiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,13 @@ void Differentiation::materialize(CommandBuffer &command_buffer) noexcept {
command_buffer << image.copy_to(_param_buffer->subview(param_offset, length))
<< textured_params_range_shader(*_param_range_buffer, p.range(), param_offset).dispatch(length);
}

//Todo: add buffer parameters(mesh vertex or sth) to _param_buffer
for (auto &&p : _geometry_params) {
auto buffer = p.buffer();
auto param_offset = p.param_offset();
auto length = buffer.size();
command_buffer << _param_buffer->subview(param_offset, length).copy_from(buffer.as<float>());
command_buffer << _param_buffer->subview(param_offset, length*8).copy_from(buffer.as<float>());
}

command_buffer << synchronize();
Expand Down Expand Up @@ -451,20 +452,23 @@ void Differentiation::register_optimizer(Optimizer::Instance *optimizer) noexcep
}

void Differentiation::register_geometry_parameter(const CommandBuffer &command_buffer, Geometry::MeshData& mesh, Accel& accel, uint instance_id) noexcept {

auto param_offset = _param_buffer_size;
auto counter_offset = _counter_size;
auto grad_offset = _gradient_buffer_size;
auto param_index = static_cast<uint>(_geometry_params.size());
//auto channels = 3u;
uint buffer_id = mesh.geometry_buffer_id_base;
auto [buffer_view, bindlessbuffer_id] = _pipeline.bindless_arena_buffer<Vertex>(buffer_id);
auto length = buffer_view.size_bytes();
//buffer.size();
_counter_size = (_counter_size + length + 3u) & ~0b11u;
_param_buffer_size = (_param_buffer_size + length + 3u) & ~0b11u;
_gradient_buffer_size = (_gradient_buffer_size + length + 3u) & ~0b11u;
_geometry_params.emplace_back(param_index, instance_id, grad_offset, param_offset, counter_offset, buffer_view, length, buffer_id);
instance2offset.value()->write(instance_id, grad_offset+1u);
//uint buffer_id = mesh.geometry_buffer_id_base;
auto buffer_view = mesh.vertices;
auto length = buffer_view.size() * 8;
_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);

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());
//instance2offset.value()->write(instance_id, grad_offset+1u);
//command_buffer << buffer.copy_to(_param_buffer->subview(param_offset, length));
}

Expand All @@ -479,13 +483,15 @@ void Differentiation::update_parameter_from_external(Stream &stream, luisa::vect
stream << image.copy_from(textures[textures_id[i]]);
}

LUISA_INFO("working on update paramaters");
// apply geometry parameters
for (auto i=0;i<geoms_id.size();i++) {
auto &&p = _geometry_params[geoms_id[i]];
auto param_offset = p.param_offset();
auto buffer_id = p.buffer_id();
auto [buffer_view, bindlessbuffer_id] = _pipeline.bindless_arena_buffer<Vertex>(buffer_id);
auto buffer_view = p.buffer();
//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>());
_is_dirty = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/base/differentiation.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Differentiation {
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
: _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) {}
[[nodiscard]] auto index() const noexcept { return _index; }
[[nodiscard]] auto buffer() const noexcept { return _buffer_view; }
[[nodiscard]] auto buffer_id() const noexcept { return _buffer_id; }
Expand Down
17 changes: 12 additions & 5 deletions src/base/geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ void Geometry::build(CommandBuffer &command_buffer,
_triangle_count = 0u;
for (auto shape : shapes) { _process_shape(command_buffer, shape, init_time, nullptr); }
LUISA_INFO_WITH_LOCATION("Geometry built with {} triangles.", _triangle_count);

_instance_buffer = _pipeline.device().create_buffer<uint4>(_instances.size());
command_buffer << _instance_buffer.copy_from(_instances.data())
<< _accel.build();
command_buffer << synchronize();
LUISA_INFO_WITH_LOCATION("Finish accel_build");
}

void Geometry::_process_shape(
Expand Down Expand Up @@ -85,7 +88,7 @@ void Geometry::_process_shape(
command_buffer << alias_table_buffer_view.copy_from(alias_table.data())
<< pdf_buffer_view.copy_from(pdf.data());
command_buffer << compute::commit();
auto geom = MeshGeometry{mesh, vertex_buffer_id};
auto geom = MeshGeometry{mesh, vertex_buffer_id, vertex_buffer->view()};
_mesh_cache.emplace(hash, geom);
return geom;
}();
Expand All @@ -96,22 +99,24 @@ void Geometry::_process_shape(
// assign mesh data
MeshData mesh_data{
.resource = mesh_geom.resource,
.vertices = mesh_geom.vertices,
.shadow_term = encode_fixed_point(shape->has_vertex_normal() ? shape->shadow_terminator_factor() : 0.f),
.intersection_offset = encode_fixed_point(shape->intersection_offset_factor()),
.geometry_buffer_id_base = mesh_geom.buffer_id_base,
.vertex_properties = shape->vertex_properties()};
_meshes.emplace(shape, mesh_data);
return mesh_data;
}();
//_pipeline.buffer<Vertex>(v_buffer)
auto instance_id = static_cast<uint>(_accel.size());
auto [t_node, is_static] = _transform_tree.leaf(shape->transform());
InstancedTransform inst_xform{t_node, instance_id};
if (!is_static) { _dynamic_transforms.emplace_back(inst_xform); }
auto object_to_world = inst_xform.matrix(init_time);
_accel.emplace_back(*mesh.resource, object_to_world, visible);


if (shape->requires_grad()) {
//LUISA_INFO("sdlfjdslkfjsdklf {}", instance_id);
if (shape->requires_gradients()) {
LUISA_INFO("requires_gradients {}", instance_id);
_pipeline.differentiation()->register_geometry_parameter(command_buffer, mesh, _accel, instance_id);
}

Expand Down Expand Up @@ -190,7 +195,9 @@ bool Geometry::update(CommandBuffer &command_buffer, float time) noexcept {
}
_pipeline.differentiation()->clear_dirty();
}
command_buffer << _accel.build();
LUISA_INFO("start build accel");
command_buffer << _accel.build() << synchronize();
LUISA_INFO("end build accel");
}
return updated;
}
Expand Down
5 changes: 4 additions & 1 deletion src/base/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ using compute::Expr;
using compute::Float4x4;
using compute::Mesh;
using compute::Var;
using compute::BufferView;

class Pipeline;

Expand All @@ -45,17 +46,19 @@ class Geometry {
struct MeshGeometry {
Mesh *resource;
uint buffer_id_base;
BufferView<Vertex> vertices;
};

struct MeshData {
Mesh *resource;
BufferView<Vertex> vertices;
uint16_t shadow_term;
uint16_t intersection_offset;
uint geometry_buffer_id_base : 22;
uint vertex_properties : 10;
};

static_assert(sizeof(MeshData) == 16u);
//static_assert(sizeof(MeshData) == 16u);

private:
Pipeline &_pipeline;
Expand Down
7 changes: 7 additions & 0 deletions src/base/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,18 @@ luisa::unique_ptr<Pipeline> Pipeline::create(Device &device, Stream &stream, con
"No lights or environment found in the scene.");
}
update_bindless_if_dirty();


pipeline->_integrator = scene.integrator()->build(*pipeline, command_buffer);


LUISA_INFO_WITH_LOCATION("start _differentiation build.");
if (auto &&diff = pipeline->_differentiation) {
diff->register_optimizer(dynamic_cast<DifferentiableIntegrator::Instance *>(pipeline->_integrator.get())->optimizer());
LUISA_INFO_WITH_LOCATION("middle _differentiation build.");
diff->materialize(command_buffer);
}
LUISA_INFO_WITH_LOCATION("finish _differentiation build.");
if (!pipeline->_transforms.empty()) {
command_buffer << pipeline->_transform_matrix_buffer.view(0u, pipeline->_transforms.size())
.copy_from(pipeline->_transform_matrices.data());
Expand Down
9 changes: 0 additions & 9 deletions src/base/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,6 @@ luisa::unique_ptr<Scene> Scene::create(const Context &ctx, const SceneDesc *desc
}
}
}
for (auto &&[_, node] : scene->_config->nodes) {
if (node->tag() == SceneNodeTag::TEXTURE) {
auto texture = static_cast<Texture *>(node.get());
if (texture->requires_gradients()) {
disabled++;
texture->disable_gradients();
}
}
}
if (disabled != 0u) {
LUISA_WARNING_WITH_LOCATION(
"Disabled gradient computation in {} "
Expand Down
2 changes: 1 addition & 1 deletion src/base/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Shape : public SceneNode {
[[nodiscard]] virtual float shadow_terminator_factor() const noexcept;
[[nodiscard]] virtual float intersection_offset_factor() const noexcept;
[[nodiscard]] virtual bool is_mesh() const noexcept;
[[nodiscard]] virtual bool requires_grad() const noexcept { return _requires_grad; }
[[nodiscard]] virtual bool requires_gradients() const noexcept { return _requires_grad; }
[[nodiscard]] virtual uint vertex_properties() const noexcept;
[[nodiscard]] bool has_vertex_normal() const noexcept;
[[nodiscard]] bool has_vertex_uv() const noexcept;
Expand Down
15 changes: 8 additions & 7 deletions src/integrators/megappm_diff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,11 +656,11 @@ class MegakernelPhotonMappingDiffInstance : public DifferentiableIntegrator::Ins
};
};

Kernel2D emit_photons_kernel = [&](UInt frame_index, Float time) noexcept {
Kernel2D emit_photons_kernel = [&](UInt frame_index, Float time, BufferFloat grad_in) noexcept {
auto pixel_id = dispatch_id().xy();
auto sampler_id = UInt2(pixel_id.x + resolution.x, pixel_id.y);
$if(pixel_id.x * resolution.y + pixel_id.y < photon_per_iter) {
photon_tracing_bp(camera, frame_index, sampler_id, time, pixel_id.x * resolution.y + pixel_id.y);
photon_tracing_bp(camera, frame_index, sampler_id, time, pixel_id.x * resolution.y + pixel_id.y, grad_in);
};
};

Expand Down Expand Up @@ -718,7 +718,7 @@ class MegakernelPhotonMappingDiffInstance : public DifferentiableIntegrator::Ins
command_buffer << viewpoint_reset().dispatch(viewpoints->size());
command_buffer << viewpath_construct(sample_id++, s.point.time, s.point.weight).dispatch(resolution);
command_buffer << build_grid().dispatch(viewpoints->size());
command_buffer << emit_photon(sample_id++, s.point.time).dispatch(make_uint2(add_x, resolution.y));
command_buffer << emit_photon(sample_id++, s.point.time, grad_in).dispatch(make_uint2(add_x, resolution.y));
command_buffer << indirect_update().dispatch(viewpoints_per_iter);
if (node<MegakernelPhotonMappingDiff>()->shared_radius()) {
command_buffer << shared_update().dispatch(1u);
Expand Down Expand Up @@ -887,10 +887,11 @@ class MegakernelPhotonMappingDiffInstance : public DifferentiableIntegrator::Ins
}
}

command_buffer << synchronize();
command_buffer << indirect_draw(node<MegakernelPhotonMappingDiff>()->photon_per_iter(), runtime_spp).dispatch(resolution);
LUISA_INFO("Finishi indirect_draw");
command_buffer << synchronize();
command_buffer << pipeline().printer().retrieve();
LUISA_INFO("Finishi indirect_draw");
progress.done();
auto render_time = clock.toc();
LUISA_INFO("Rendering finished in {} ms.", render_time);
Expand Down Expand Up @@ -1103,7 +1104,7 @@ class MegakernelPhotonMappingDiffInstance : public DifferentiableIntegrator::Ins
auto wi_local = it->shading().world_to_local(wi);
Float3 Phi;
auto rel_dis = dis / indirect->radius(pixel_id);
auto weight = 3.0f*(1.0f-rel_dis);//- 6 * pow(rel_dis, 5.) + 15 * pow(rel_dis, 4.) - 10 * pow(rel_dis, 3.);
auto weight = 3.5f*(1.0f- 6 * pow(rel_dis, 5.) + 15 * pow(rel_dis, 4.) - 10 * pow(rel_dis, 3.));
if (!spectrum->node()->is_fixed()) {
auto viewpoint_swl = viewpoints->swl(pixel_id);
Phi = spectrum->wavelength_mul(swl, beta * (eval_viewpoint / abs_cos_theta(wi_local)), viewpoint_swl, viewpoint_beta);
Expand Down Expand Up @@ -1409,11 +1410,11 @@ class MegakernelPhotonMappingDiffInstance : public DifferentiableIntegrator::Ins
requires_grad(bary, beta_diff);
Float3 photon_pos = point_0 * bary[0] + point_1 * bary[1] + point_2 * (1 - bary[0] - bary[1]);
auto rel_dis_diff = distance(position, photon_pos) / rad;
auto weight = (1-rel_dis_diff)*3;// 1- 6*pow(rel_dis_diff, 5.) + 15*pow(rel_dis_diff, 4.) - 10*pow(rel_dis_diff, 3.);
auto weight = 3.5f*(1- 6*pow(rel_dis_diff, 5.) + 15*pow(rel_dis_diff, 4.) - 10*pow(rel_dis_diff, 3.));
auto wi_local = it->shading().world_to_local(wi);
auto Phi = spectrum->srgb(swl, viewpoint_beta * eval_viewpoint / abs_cos_theta(wi_local));
auto Phi_beta = Phi * beta_diff * weight;
auto _grad_dimension = 5u;
auto _grad_dimension = 3u;
auto grad_pixel_0 = grad_in->read(pixel_id*_grad_dimension+0);
auto grad_pixel_1 = grad_in->read(pixel_id * _grad_dimension + 1);
auto grad_pixel_2 = grad_in->read(pixel_id * _grad_dimension + 2);
Expand Down
1 change: 1 addition & 0 deletions src/python/lrapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ PYBIND11_MODULE(_lrapi, m) {
geoms.push_back(std::move(buffer));
}
}
LUISA_INFO("geom_id_size is {}", geoms_id.size());
scene_python._pipeline->differentiation()->update_parameter_from_external(*scene_python._stream, constants_id, constants, textures_id, textures, geoms_id, geoms);
});

Expand Down
2 changes: 2 additions & 0 deletions src/shapes/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class MeshLoader {
aiProcess_ValidateDataStructure | aiProcess_ImproveCacheLocality |
aiProcess_PreTransformVertices | aiProcess_FindInvalidData |
aiProcess_JoinIdenticalVertices;

auto remove_flags = aiComponent_ANIMATIONS | aiComponent_BONEWEIGHTS |
aiComponent_CAMERAS | aiComponent_LIGHTS |
aiComponent_MATERIALS | aiComponent_TEXTURES |
Expand Down Expand Up @@ -101,6 +102,7 @@ class MeshLoader {
mesh->mNumUVComponents[0]);
}
auto vertex_count = mesh->mNumVertices;
LUISA_INFO("vertex_count in loading mesh is {}", vertex_count);
auto ai_positions = mesh->mVertices;
auto ai_normals = mesh->mNormals;
auto ai_uvs = mesh->mTextureCoords[0];
Expand Down
Binary file added src/tests/gt.exr
Binary file not shown.
Binary file added src/tests/init.exr
Binary file not shown.
Loading

0 comments on commit e5a6d7c

Please sign in to comment.