Skip to content

Commit

Permalink
grad map implementing: only constant matte
Browse files Browse the repository at this point in the history
  • Loading branch information
W-Solaris committed Jan 4, 2024
1 parent e12f2ba commit 6282991
Show file tree
Hide file tree
Showing 29 changed files with 479 additions and 43 deletions.
Binary file modified cbox-diff/renders/cbox-diff.exr
Binary file not shown.
Binary file modified cbox-diff/renders/cbox-ref.exr
Binary file not shown.
142 changes: 142 additions & 0 deletions cbox-diff/scenes/cbox-diff/cbox-diff-matte-test.luisa
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// mat_cbox
Surface cbox-white : Matte {
Kd : Constant {
//v { 0.725, 0.71, 0.68 }
v { 0.5, 0.5, 0.5 }
requires_grad { true }
render_grad_map { true }
range { 0.00001, 0.99999 }
}
}


Surface cbox-green : Matte {
Kd : Constant {
//v { 0.14, 0.45, 0.091 }
v { 0.5, 0.5, 0.5 }
requires_grad { true }
range { 0.00001, 0.99999 }
}
}

Surface cbox-red : Matte {
Kd : Constant {
//v { 0.63, 0.065, 0.05 }
v { 0.5, 0.5, 0.5 }
requires_grad { true }
range { 0.00001, 0.99999 }
}
}

// shape_cbox
Shape cbox-ceiling : Mesh {
file { "../../meshes/cbox-parts/ceiling.obj" }
surface { @cbox-white }
}

Shape cbox-floor : Mesh {
file { "../../meshes/cbox-parts/floor.obj" }
surface { @cbox-white }
}

Shape cbox-left-wall : Mesh {
file { "../../meshes/cbox-parts/left-wall.obj" }
surface { @cbox-red }
}

Shape cbox-right-wall : Mesh {
file { "../../meshes/cbox-parts/right-wall.obj" }
surface { @cbox-green }
}

Shape cbox-back-wall : Mesh {
file { "../../meshes/cbox-parts/back-wall.obj" }
surface { @cbox-white }
}


Shape cbox-light : Mesh {
file { "../../meshes/cbox-parts/light.obj" }
light : Diffuse {
emission : Constant {
//v { 10, 10, 10 }
v { 45.0, 45.0, 45.0 }
}
}
}

Shape cbox-tall-box : Mesh {
file { "../../meshes/cbox-parts/tall-box.obj" }
surface { @cbox-white }
}

Shape cbox-short-box : Mesh {
file { "../../meshes/cbox-parts/short-box.obj" }
surface { @cbox-white }
}


Shape cbox : Group {
shapes {
@cbox-left-wall,
@cbox-ceiling,
@cbox-floor,
@cbox-right-wall,
@cbox-back-wall,
@cbox-tall-box,
@cbox-short-box,
@cbox-light
}
}

Film film : Color {
resolution { 1024 }
exposure { 0 }
}

FIlm display : Display {
base { @film }
}

Camera camera : Pinhole {
position { 0.0, 1.0, 5.0 }
fov { 27.8 }
spp { 1024 }
film { @display }
file { "../../renders/cbox-diff.exr" }
filter : Gaussian {
radius { 1 }
}
target : Image {
file { "../../renders/cbox-ref.exr" }
}
}

//Integrator pt : MegaRadiativeDiff {
Integrator pt : MegaReplayDiff {
sampler : Independent {}
depth { 10 }
rr_depth { 5 }

display_camera_index { 0 }
save_process { true }
save_grad_map { true }
save_finite_diff { true }
loss : L2 {}
iterations { 10 }

optimizer : Adam {
learning_rate { 0.03 }
}
}

render {
spectrum : sRGB {}
cameras {
@camera
}
shapes {
@cbox
}
integrator { @pt }
}
2 changes: 2 additions & 0 deletions cbox-diff/scenes/cbox-diff/cbox-diff-matte.luisa
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Surface cbox-white-2 : Matte {
Kd : Constant {
v { 0.3, 0.3, 0.9 }
requires_grad { true }
render_grad_map { true }
range { 0.00001, 0.99999 }
}
}
Expand Down Expand Up @@ -150,6 +151,7 @@ Integrator pt : MegaReplayDiff {

display_camera_index { 0 }
save_process { true }
save_grad_map { true }
loss : L2 {}
iterations { 100 }

Expand Down
2 changes: 1 addition & 1 deletion src/apps/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ using namespace luisa::render;
int main(int argc, char *argv[]) {

log_level_info();
luisa::log_level_verbose();
// luisa::log_level_verbose();
luisa::compute::Context context{argv[0]};
auto macros = parse_cli_macros(argc, argv);
for (auto &&[k, v] : macros) {
Expand Down
2 changes: 2 additions & 0 deletions src/base/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace luisa::render {
Camera::Camera(Scene *scene, const SceneNodeDesc *desc) noexcept
: SceneNode{scene, desc, SceneNodeTag::CAMERA},
_film{scene->load_film(desc->property_node("film"))},
_film_grad{scene->load_film(desc->property_node("film"))},
_filter{scene->load_filter(desc->property_node_or_default(
"filter", SceneNodeDesc::shared_default_filter("Box")))},
_transform{scene->load_transform(desc->property_node_or_default("transform"))},
Expand Down Expand Up @@ -206,6 +207,7 @@ auto Camera::shutter_samples() const noexcept -> vector<ShutterSample> {
Camera::Instance::Instance(Pipeline &pipeline, CommandBuffer &command_buffer, const Camera *camera) noexcept
: _pipeline{&pipeline}, _camera{camera},
_film{camera->film()->build(pipeline, command_buffer)},
_film_grad{camera->film_grad()->build(pipeline, command_buffer)},
_filter{pipeline.build_filter(command_buffer, camera->filter())},
_target{pipeline.build_texture(command_buffer, camera->target())} {
pipeline.register_transform(camera->transform());
Expand Down
5 changes: 5 additions & 0 deletions src/base/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Camera : public SceneNode {
const Pipeline *_pipeline;
const Camera *_camera;
luisa::unique_ptr<Film::Instance> _film;
luisa::unique_ptr<Film::Instance> _film_grad;
const Filter::Instance *_filter;
const Texture::Instance *_target;

Expand All @@ -70,6 +71,8 @@ class Camera : public SceneNode {
[[nodiscard]] auto &pipeline() const noexcept { return *_pipeline; }
[[nodiscard]] auto film() noexcept { return _film.get(); }
[[nodiscard]] auto film() const noexcept { return _film.get(); }
[[nodiscard]] auto film_grad() noexcept { return _film_grad.get(); }
[[nodiscard]] auto film_grad() const noexcept { return _film_grad.get(); }
[[nodiscard]] auto filter() noexcept { return _filter; }
[[nodiscard]] auto filter() const noexcept { return _filter; }
[[nodiscard]] auto target() const noexcept { return _target; }
Expand All @@ -92,6 +95,7 @@ class Camera : public SceneNode {

private:
const Film *_film;
const Film *_film_grad;
const Filter *_filter;
const Transform *_transform;
float2 _shutter_span;
Expand All @@ -104,6 +108,7 @@ class Camera : public SceneNode {
public:
Camera(Scene *scene, const SceneNodeDesc *desc) noexcept;
[[nodiscard]] auto film() const noexcept { return _film; }
[[nodiscard]] auto film_grad() const noexcept { return _film_grad; }
[[nodiscard]] auto filter() const noexcept { return _filter; }
[[nodiscard]] auto transform() const noexcept { return _transform; }
[[nodiscard]] auto shutter_span() const noexcept { return _shutter_span; }
Expand Down
2 changes: 2 additions & 0 deletions src/base/integrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ DifferentiableIntegrator::DifferentiableIntegrator(Scene *scene, const SceneNode
_iterations{std::max(desc->property_uint_or_default("iterations", 100u), 1u)},
_display_camera_index{desc->property_int_or_default("display_camera_index", -1)},
_save_process{desc->property_bool_or_default("save_process", false)},
_save_grad_map{desc->property_bool_or_default("save_grad_map", false)},
_save_finite_diff{desc->property_bool_or_default("save_finite_diff", false)},
_loss{scene->load_loss(desc->property_node_or_default(
"loss", SceneNodeDesc::shared_default_loss("L2")))},
_optimizer{scene->load_optimizer(desc->property_node_or_default(
Expand Down
4 changes: 4 additions & 0 deletions src/base/integrator.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class DifferentiableIntegrator : public Integrator {
uint _iterations;
int _display_camera_index;
bool _save_process;
bool _save_grad_map;
bool _save_finite_diff;

public:
DifferentiableIntegrator(Scene *scene, const SceneNodeDesc *desc) noexcept;
Expand All @@ -127,6 +129,8 @@ class DifferentiableIntegrator : public Integrator {
[[nodiscard]] auto iterations() const noexcept { return _iterations; }
[[nodiscard]] int display_camera_index() const noexcept { return _display_camera_index; }
[[nodiscard]] bool save_process() const noexcept { return _save_process; }
[[nodiscard]] bool save_grad_map() const noexcept { return _save_grad_map; }
[[nodiscard]] bool save_finite_diff() const noexcept { return _save_finite_diff; }
};

}// namespace luisa::render
Expand Down
12 changes: 12 additions & 0 deletions src/base/surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Created by Mike on 2021/12/14.
//

#include "util/spec.h"
#include <base/surface.h>
#include <base/scene.h>
#include <base/interaction.h>
Expand Down Expand Up @@ -54,6 +55,17 @@ Surface::Evaluation Surface::Closure::evaluate(
return eval;
}

SampledSpectrum Surface::Closure::eval_grad(
Expr<float3> wo, Expr<float3> wi, TransportMode mode) const noexcept {
SampledSpectrum grad{swl().dimension()};
$outline {
grad = _eval_grad(wo, wi, mode);
auto valid = validate_surface_sides(it().ng(), it().shading().n(), wo, wi);
grad = ite(valid, grad, 0.f);
};
return grad;
}

Surface::Sample Surface::Closure::sample(Expr<float3> wo,
Expr<float> u_lobe, Expr<float2> u,
TransportMode mode) const noexcept {
Expand Down
8 changes: 8 additions & 0 deletions src/base/surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class Surface : public SceneNode {
template<typename BS, typename BSI>
friend class OpacitySurfaceWrapper;
[[nodiscard]] virtual Evaluation _evaluate(Expr<float3> wo, Expr<float3> wi, TransportMode mode) const noexcept = 0;
[[nodiscard]] virtual SampledSpectrum _eval_grad(Expr<float3> wo, Expr<float3> wi, TransportMode mode) const noexcept = 0;
[[nodiscard]] virtual Sample _sample(Expr<float3> wo, Expr<float> u_lobe, Expr<float2> u, TransportMode mode) const noexcept = 0;
virtual void _backward(Expr<float3> wo, Expr<float3> wi, const SampledSpectrum &df, TransportMode mode) const noexcept = 0;

Expand All @@ -95,6 +96,8 @@ class Surface : public SceneNode {
[[nodiscard]] auto instance() const noexcept { return static_cast<const T *>(_instance); }
[[nodiscard]] Evaluation evaluate(Expr<float3> wo, Expr<float3> wi,
TransportMode mode = TransportMode::RADIANCE) const noexcept;
[[nodiscard]] SampledSpectrum eval_grad(Expr<float3> wo, Expr<float3> wi,
TransportMode mode = TransportMode::RADIANCE) const noexcept;
[[nodiscard]] Sample sample(Expr<float3> wo,
Expr<float> u_lobe, Expr<float2> u,
TransportMode mode = TransportMode::RADIANCE) const noexcept;
Expand Down Expand Up @@ -219,6 +222,11 @@ class OpacitySurfaceWrapper : public BaseSurface {
TransportMode mode) const noexcept override {
return _base->_evaluate(wo, wi, mode);
}
[[nodiscard]] SampledSpectrum _eval_grad(Expr<float3> wo,
Expr<float3> wi,
TransportMode mode) const noexcept override {
return _base->_eval_grad(wo, wi, mode);
}
[[nodiscard]] Surface::Sample _sample(Expr<float3> wo,
Expr<float> u_lobe, Expr<float2> u,
TransportMode mode) const noexcept override {
Expand Down
13 changes: 12 additions & 1 deletion src/base/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Created by Mike Smith on 2022/1/25.
//

#include "util/spec.h"
#include <base/texture.h>
#include <base/pipeline.h>

Expand All @@ -12,9 +13,11 @@ Texture::Texture(Scene *scene, const SceneNodeDesc *desc) noexcept
_range{desc->property_float2_or_default(
"range", make_float2(std::numeric_limits<float>::min(),
std::numeric_limits<float>::max()))},
_requires_grad{desc->property_bool_or_default("requires_grad", false)} {}
_requires_grad{desc->property_bool_or_default("requires_grad", false)},
_render_grad_map{desc->property_bool_or_default("render_grad_map", false)} {}

bool Texture::requires_gradients() const noexcept { return _requires_grad; }
bool Texture::render_grad_map() const noexcept { return _render_grad_map; }
void Texture::disable_gradients() noexcept { _requires_grad = false; }

luisa::optional<float4> Texture::evaluate_static() const noexcept { return luisa::nullopt; }
Expand Down Expand Up @@ -93,6 +96,14 @@ void Texture::Instance::backward_albedo_spectrum(
backward(it, swl, time, dEnc);
}

SampledSpectrum Texture::Instance::eval_grad_albedo_spectrum(
const Interaction &it, const SampledWavelengths &swl,
Expr<float> time, const SampledSpectrum &dSpec) const noexcept {
auto dEnc = pipeline().spectrum()->backward_decode_albedo(swl, evaluate(it, swl, time), dSpec);
dEnc = make_float4(pipeline().spectrum()->backward_encode_srgb_albedo(dEnc), 1.f);
return eval_grad(it, swl, time, dEnc);
}

void Texture::Instance::backward_illuminant_spectrum(
const Interaction &it, const SampledWavelengths &swl,
Expr<float> time, const SampledSpectrum &dSpec) const noexcept {
Expand Down
8 changes: 8 additions & 0 deletions src/base/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Texture : public SceneNode {
[[nodiscard]] auto &pipeline() const noexcept { return _pipeline; }
[[nodiscard]] virtual Float4 evaluate(
const Interaction &it, const SampledWavelengths &swl, Expr<float> time) const noexcept = 0;
[[nodiscard]] virtual SampledSpectrum eval_grad(
const Interaction &it, const SampledWavelengths &swl, Expr<float> time, Expr<float4> grad) const noexcept = 0;
virtual void backward(
const Interaction &it, const SampledWavelengths &swl, Expr<float> time, Expr<float4> grad) const noexcept = 0;
[[nodiscard]] virtual Spectrum::Decode evaluate_albedo_spectrum(
Expand All @@ -59,9 +61,13 @@ class Texture : public SceneNode {
const Interaction &it, const SampledWavelengths &swl, Expr<float> time) const noexcept;
[[nodiscard]] virtual Spectrum::Decode evaluate_illuminant_spectrum(
const Interaction &it, const SampledWavelengths &swl, Expr<float> time) const noexcept;
[[nodiscard]] SampledSpectrum eval_grad_albedo_spectrum(
const Interaction &it, const SampledWavelengths &swl,
Expr<float> time, const SampledSpectrum &dSpec) const noexcept;
void backward_albedo_spectrum(
const Interaction &it, const SampledWavelengths &swl,
Expr<float> time, const SampledSpectrum &dSpec) const noexcept;

void backward_illuminant_spectrum(
const Interaction &it, const SampledWavelengths &swl,
Expr<float> time, const SampledSpectrum &dSpec) const noexcept;
Expand All @@ -82,11 +88,13 @@ class Texture : public SceneNode {
private:
float2 _range;
bool _requires_grad;
bool _render_grad_map;

public:
Texture(Scene *scene, const SceneNodeDesc *desc) noexcept;
[[nodiscard]] auto range() const noexcept { return _range; }
[[nodiscard]] virtual bool requires_gradients() const noexcept;
[[nodiscard]] virtual bool render_grad_map() const noexcept;
virtual void disable_gradients() noexcept;
[[nodiscard]] virtual bool is_black() const noexcept = 0;
[[nodiscard]] virtual bool is_constant() const noexcept = 0;
Expand Down
Loading

0 comments on commit 6282991

Please sign in to comment.