Skip to content

Commit

Permalink
Trace functions renaming. (#1325)
Browse files Browse the repository at this point in the history
  • Loading branch information
xelatihy authored Jan 16, 2022
1 parent 492bbc5 commit 38f8f24
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 76 deletions.
12 changes: 7 additions & 5 deletions apps/yscene/yscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ void run_render(const render_params& params_) {
}

// build bvh
auto bvh = make_bvh(scene, params);
auto bvh = make_trace_bvh(scene, params);

// init renderer
auto lights = make_lights(scene, params);
auto lights = make_trace_lights(scene, params);

// fix renderer type if no lights
if (lights.lights.empty() && is_sampler_lit(params)) {
Expand All @@ -211,7 +211,7 @@ void run_render(const render_params& params_) {
}

// state
auto state = make_state(scene, params);
auto state = make_trace_state(scene, params);

// render
timer = simple_timer{};
Expand All @@ -221,7 +221,8 @@ void run_render(const render_params& params_) {
print_info("render sample {}/{}: {}", sample, params.samples,
elapsed_formatted(sample_timer));
if (params.savebatch && state.samples % params.batch == 0) {
auto image = params.denoise ? get_denoised(state) : get_render(state);
auto image = params.denoise ? get_denoised_image(state)
: get_rendered_image(state);
auto outfilename = fs::path(params.output)
.replace_extension(
"-s" + std::to_string(sample) +
Expand All @@ -236,7 +237,8 @@ void run_render(const render_params& params_) {

// save image
timer = simple_timer{};
auto image = params.denoise ? get_denoised(state) : get_render(state);
auto image = params.denoise ? get_denoised_image(state)
: get_rendered_image(state);
if (!is_hdr_filename(params.output))
image = tonemap_image(image, params.exposure, params.filmic);
save_image(params.output, image);
Expand Down
50 changes: 26 additions & 24 deletions docs/yocto/yocto_trace.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ give feedback before the render is finished.

To render a scene, first tesselate shapes for subdivs and displacement,
with `tesselate_shapes(scene, params)`, then call
`trace_image(scene, camera, params)`, where `params` are the rendering options.
`trace_image(scene, params)`, where `params` are the rendering options.

```cpp
auto scene = scene_data{...}; // initialize scene
auto params = trace_params{}; // default params
tesselate_shapes(scene, params); // tesselate shapes if needed
trace_image(scene, params); // render image
auto params = trace_params{}; // default params
auto image = trace_image(scene, params); // render image
```
## Rendering options
Expand Down Expand Up @@ -94,55 +94,57 @@ progressive computation. Each object need to be initialized separately.

To render a scene, first tesselate shapes for subdivs and displacement,
with `tesselate_shapes(scene, params, progress)`, then initialize the scene
bvh and lights, with `make_bvh(scene, params)` and
`make_lights(scene, params)`, then the rendering state
with `make_state(state, scene)`.
bvh and lights, with `make_trace_bvh(scene, params)` and
`make_trace_lights(scene, params)`, then the rendering state
with `make_trace_state(state, scene)`.

Then, for each sample, call `trace_samples(state, scene, lights, params)`
and retrieve the computed image with `get_render(state)` or
`get_render(image, state)`. This interface can be useful to provide user
and retrieve the computed image with `get_rendered_image(state)` or
`get_rendered_image(image, state)`. This interface can be useful to provide user
feedback by either saving or displaying partial images.

```cpp
auto scene = scene_data{...}; // initialize scene
auto params = trace_params{}; // default params
tesselate_shapes(scene, params); // tesselate shapes if needed
auto bvh = make_bvh(scene, params); // init bvh
auto lights = make_lights(scene, params); // init lights
auto state = make_state(scene, params); // init state
auto bvh = make_trace_bvh(scene, params); // init bvh
auto lights = make_trace_lights(scene, params); // init lights
auto state = make_trace_state(scene, params); // init state
for(auto sample : range(params.samples)) { // for each sample
trace_samples(state, scene, camera, bvh, // render sample
lights, params);
process_image(get_render(state)); // get image computed so far
auto image = get_rendered_image(state); // get image computed so far
process_image(image);
};
```

## Denoising with Intel's Open Image Denoise

We support denoising of rendered images in the low-level interface.
Just call `get_denoised(...)` instead of `get_render(...)` to get a denoised image.
Alternatively, you can call `get_albedo(state)` or `get_normal(state)` to get
denoising buffers and either run the denoiser in a different process, or
call `denoise_render(render, albedo, normal)` to denoise the image.
Just call `get_denoised_image(...)` instead of `get_rendered_image(...)`
to get a denoised image. Alternatively, you can call `get_albedo_image(state)`
or `get_normal_image(state)` to get denoising buffers and either run the
denoiser in a different process, or call `denoise_render(render, albedo, normal)`
to denoise the image.
To denoise within Yocto/GL, the library should be compiled with OIDN support by
setting the `YOCTO_DENOISE` compile flag and linking to OIDN's libraries.

```cpp
auto scene = scene_data{...}; // initialize scene
auto params = trace_params{}; // default params
tesselate_shapes(scene, params); // tesselate shapes if needed
auto bvh = make_bvh(scene, params); // init bvh
auto lights = make_lights(scene, params); // init lights
auto state = make_state(scene, params); // init state
auto bvh = make_trace_bvh(scene, params); // init bvh
auto lights = make_trace_lights(scene, params); // init lights
auto state = make_trace_state(scene, params); // init state
for(auto sample : range(params.samples)) { // for each sample
trace_samples(state, scene, camera, bvh, // render sample
lights, params);
};
auto denoised = get_denoised(state); // get denoised final image
auto denoised = get_denoised_image(state); // get denoised final image
// alternative interface
auto render = get_render(state); // get final image
auto albedo = get_albedo(state); // get denoising buffers
auto normal = get_normal(state);
auto render = get_render_image(state); // get rendered image
auto albedo = get_albedo_image(state); // get denoising buffers
auto normal = get_normal_image(state);
// run denoiser here or save buffers and run elsewhere
auto denoised2 = denoise_render(render, albedo, normal);
auto denoised2 = denoise_rendered_image(render, albedo, normal);
```
16 changes: 8 additions & 8 deletions libs/yocto/yocto_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,18 +600,18 @@ void show_trace_gui(const string& title, const string& name, scene_data& scene,
auto params = params_;

// build bvh
auto bvh = make_bvh(scene, params);
auto bvh = make_trace_bvh(scene, params);

// init renderer
auto lights = make_lights(scene, params);
auto lights = make_trace_lights(scene, params);

// fix renderer type if no lights
if (lights.lights.empty() && is_sampler_lit(params)) {
params.sampler = trace_sampler_type::eyelight;
}

// init state
auto state = make_state(scene, params);
auto state = make_trace_state(scene, params);
auto image = make_image(state.width, state.height, true);
auto display = make_image(state.width, state.height, false);
auto render = make_image(state.width, state.height, true);
Expand Down Expand Up @@ -643,7 +643,7 @@ void show_trace_gui(const string& title, const string& name, scene_data& scene,
render_stop = true;
if (render_worker.valid()) render_worker.get();

state = make_state(scene, params);
state = make_trace_state(scene, params);
image = make_image(state.width, state.height, true);
display = make_image(state.width, state.height, false);
render = make_image(state.width, state.height, true);
Expand All @@ -655,9 +655,9 @@ void show_trace_gui(const string& title, const string& name, scene_data& scene,
auto pparams = params;
pparams.resolution /= params.pratio;
pparams.samples = 1;
auto pstate = make_state(scene, pparams);
auto pstate = make_trace_state(scene, pparams);
trace_samples(pstate, scene, bvh, lights, pparams);
auto preview = get_render(pstate);
auto preview = get_rendered_image(pstate);
for (auto idx = 0; idx < state.width * state.height; idx++) {
auto i = idx % render.width, j = idx / render.width;
auto pi = clamp(i / params.pratio, 0, preview.width - 1),
Expand Down Expand Up @@ -688,9 +688,9 @@ void show_trace_gui(const string& title, const string& name, scene_data& scene,
auto lock = std::lock_guard{render_mutex};
render_current = state.samples;
if (!params.denoise || render_stop) {
get_render(render, state);
get_rendered_image(render, state);
} else {
get_denoised(render, state);
get_denoised_image(render, state);
}
image = render;
tonemap_image_mt(display, image, params.exposure, params.filmic);
Expand Down
52 changes: 27 additions & 25 deletions libs/yocto/yocto_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ inline void parallel_for(T num1, T num2, Func&& func) {
namespace yocto {

// Build the Bvh acceleration structure.
trace_bvh make_bvh(const scene_data& scene, const trace_params& params) {
trace_bvh make_trace_bvh(const scene_data& scene, const trace_params& params) {
if (params.embreebvh) {
return {{},
make_scene_embree_bvh(scene, params.highqualitybvh, params.noparallel)};
Expand Down Expand Up @@ -1409,7 +1409,8 @@ void trace_sample(trace_state& state, const scene_data& scene,
}

// Init a sequence of random number generators.
trace_state make_state(const scene_data& scene, const trace_params& params) {
trace_state make_trace_state(
const scene_data& scene, const trace_params& params) {
auto& camera = scene.cameras[params.camera];
auto state = trace_state{};
if (camera.aspect >= 1) {
Expand Down Expand Up @@ -1438,7 +1439,8 @@ static trace_light& add_light(trace_lights& lights) {
}

// Init trace lights
trace_lights make_lights(const scene_data& scene, const trace_params& params) {
trace_lights make_trace_lights(
const scene_data& scene, const trace_params& params) {
auto lights = trace_lights{};

for (auto handle = 0; handle < (int)scene.instances.size(); handle++) {
Expand Down Expand Up @@ -1494,13 +1496,13 @@ trace_lights make_lights(const scene_data& scene, const trace_params& params) {

// Progressively computes an image.
image_data trace_image(const scene_data& scene, const trace_params& params) {
auto bvh = make_bvh(scene, params);
auto lights = make_lights(scene, params);
auto state = make_state(scene, params);
auto bvh = make_trace_bvh(scene, params);
auto lights = make_trace_lights(scene, params);
auto state = make_trace_state(scene, params);
for (auto sample = 0; sample < params.samples; sample++) {
trace_samples(state, scene, bvh, lights, params);
}
return get_render(state);
return get_rendered_image(state);
}

// Progressively compute an image by calling trace_samples multiple times.
Expand Down Expand Up @@ -1533,12 +1535,12 @@ static void check_image(
}

// Get resulting render
image_data get_render(const trace_state& state) {
image_data get_rendered_image(const trace_state& state) {
auto image = make_image(state.width, state.height, true);
get_render(image, state);
get_rendered_image(image, state);
return image;
}
void get_render(image_data& image, const trace_state& state) {
void get_rendered_image(image_data& image, const trace_state& state) {
check_image(image, state.width, state.height, true);
auto scale = 1.0f / (float)state.samples;
for (auto idx = 0; idx < state.width * state.height; idx++) {
Expand All @@ -1547,19 +1549,19 @@ void get_render(image_data& image, const trace_state& state) {
}

// Get denoised render
image_data get_denoised(const trace_state& state) {
image_data get_denoised_image(const trace_state& state) {
auto image = make_image(state.width, state.height, true);
get_denoised(image, state);
get_denoised_image(image, state);
return image;
}
void get_denoised(image_data& image, const trace_state& state) {
void get_denoised_image(image_data& image, const trace_state& state) {
#if YOCTO_DENOISE
// Create an Intel Open Image Denoise device
oidn::DeviceRef device = oidn::newDevice();
device.commit();

// get image
get_render(image, state);
get_rendered_image(image, state);

// get albedo and normal
auto albedo = vector<vec3f>(image.pixels.size()),
Expand Down Expand Up @@ -1587,30 +1589,30 @@ void get_denoised(image_data& image, const trace_state& state) {
// Filter the image
filter.execute();
#else
get_render(image, state);
get_rendered_image(image, state);
#endif
}

// Get denoising buffers
image_data get_albedo(const trace_state& state) {
image_data get_albedo_image(const trace_state& state) {
auto albedo = make_image(state.width, state.height, true);
get_albedo(albedo, state);
get_albedo_image(albedo, state);
return albedo;
}
void get_albedo(image_data& albedo, const trace_state& state) {
void get_albedo_image(image_data& albedo, const trace_state& state) {
check_image(albedo, state.width, state.height, true);
auto scale = 1.0f / (float)state.samples;
for (auto idx = 0; idx < state.width * state.height; idx++) {
albedo.pixels[idx] = {state.albedo[idx].x * scale,
state.albedo[idx].y * scale, state.albedo[idx].z * scale, 1.0f};
}
}
image_data get_normal(const trace_state& state) {
image_data get_normal_image(const trace_state& state) {
auto normal = make_image(state.width, state.height, true);
get_normal(normal, state);
get_normal_image(normal, state);
return normal;
}
void get_normal(image_data& normal, const trace_state& state) {
void get_normal_image(image_data& normal, const trace_state& state) {
check_image(normal, state.width, state.height, true);
auto scale = 1.0f / (float)state.samples;
for (auto idx = 0; idx < state.width * state.height; idx++) {
Expand All @@ -1620,13 +1622,13 @@ void get_normal(image_data& normal, const trace_state& state) {
}

// Denoise image
image_data denoise_render(const image_data& render, const image_data& albedo,
const image_data& normal) {
image_data denoise_rendered_image(const image_data& render,
const image_data& albedo, const image_data& normal) {
auto denoised = make_image(render.width, render.height, render.linear);
denoise_render(denoised, render, albedo, normal);
denoise_rendered_image(denoised, render, albedo, normal);
return denoised;
}
void denoise_render(image_data& denoised, const image_data& render,
void denoise_rendered_image(image_data& denoised, const image_data& render,
const image_data& albedo, const image_data& normal) {
check_image(denoised, render.width, render.height, render.linear);
check_image(albedo, render.width, render.height, albedo.linear);
Expand Down
Loading

0 comments on commit 38f8f24

Please sign in to comment.