2023-06-14 16:52:36 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0 */
|
2019-02-06 12:57:10 +01:00
|
|
|
|
2021-10-24 14:19:19 +02:00
|
|
|
#include "session/denoising.h"
|
2024-06-03 22:41:25 +02:00
|
|
|
#include "device/cpu/device.h"
|
2019-02-06 12:57:10 +01:00
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
#include "util/map.h"
|
|
|
|
|
#include "util/task.h"
|
2019-02-06 12:57:10 +01:00
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
#include <OpenImageIO/filesystem.h>
|
2019-02-06 12:57:10 +01:00
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
|
|
/* Utility Functions */
|
|
|
|
|
|
2019-05-01 21:14:11 +10:00
|
|
|
/* Splits in at its last dot, setting suffix to the part after the dot and in to the part before
|
|
|
|
|
* it. Returns whether a dot was found. */
|
2019-02-06 12:57:10 +01:00
|
|
|
static bool split_last_dot(string &in, string &suffix)
|
|
|
|
|
{
|
2024-12-29 17:32:00 +01:00
|
|
|
const size_t pos = in.rfind(".");
|
2019-02-06 12:57:10 +01:00
|
|
|
if (pos == string::npos) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
suffix = in.substr(pos + 1);
|
|
|
|
|
in = in.substr(0, pos);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Separate channel names as generated by Blender.
|
|
|
|
|
* If views is true:
|
2019-05-01 21:14:11 +10:00
|
|
|
* Inputs are expected in the form RenderLayer.Pass.View.Channel, sets renderlayer to
|
|
|
|
|
* "RenderLayer.View" Otherwise: Inputs are expected in the form RenderLayer.Pass.Channel */
|
2019-02-06 12:57:10 +01:00
|
|
|
static bool parse_channel_name(
|
|
|
|
|
string name, string &renderlayer, string &pass, string &channel, bool multiview_channels)
|
|
|
|
|
{
|
|
|
|
|
if (!split_last_dot(name, channel)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
string view;
|
|
|
|
|
if (multiview_channels && !split_last_dot(name, view)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!split_last_dot(name, pass)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
renderlayer = name;
|
|
|
|
|
|
|
|
|
|
if (multiview_channels) {
|
|
|
|
|
renderlayer += "." + view;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Channel Mapping */
|
|
|
|
|
|
|
|
|
|
struct ChannelMapping {
|
|
|
|
|
int channel;
|
|
|
|
|
string name;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void fill_mapping(vector<ChannelMapping> &map, int pos, string name, string channels)
|
|
|
|
|
{
|
|
|
|
|
for (const char *chan = channels.c_str(); *chan; chan++) {
|
|
|
|
|
map.push_back({pos++, name + "." + *chan});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
static const int INPUT_NUM_CHANNELS = 13;
|
|
|
|
|
static const int INPUT_NOISY_IMAGE = 0;
|
|
|
|
|
static const int INPUT_DENOISING_NORMAL = 3;
|
|
|
|
|
static const int INPUT_DENOISING_ALBEDO = 6;
|
|
|
|
|
static const int INPUT_MOTION = 9;
|
2019-02-12 17:10:31 +01:00
|
|
|
static vector<ChannelMapping> input_channels()
|
2019-02-06 12:57:10 +01:00
|
|
|
{
|
|
|
|
|
vector<ChannelMapping> map;
|
2022-01-04 21:39:54 +01:00
|
|
|
fill_mapping(map, INPUT_NOISY_IMAGE, "Combined", "RGB");
|
2019-03-04 16:01:11 +01:00
|
|
|
fill_mapping(map, INPUT_DENOISING_NORMAL, "Denoising Normal", "XYZ");
|
|
|
|
|
fill_mapping(map, INPUT_DENOISING_ALBEDO, "Denoising Albedo", "RGB");
|
2022-01-04 21:39:54 +01:00
|
|
|
fill_mapping(map, INPUT_MOTION, "Vector", "XYZW");
|
2019-02-06 12:57:10 +01:00
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const int OUTPUT_NUM_CHANNELS = 3;
|
2019-02-12 17:10:31 +01:00
|
|
|
static vector<ChannelMapping> output_channels()
|
2019-02-06 12:57:10 +01:00
|
|
|
{
|
|
|
|
|
vector<ChannelMapping> map;
|
|
|
|
|
fill_mapping(map, 0, "Combined", "RGB");
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 14:53:20 +10:00
|
|
|
/* Render-layer Handling. */
|
2019-02-06 12:57:10 +01:00
|
|
|
|
|
|
|
|
bool DenoiseImageLayer::detect_denoising_channels()
|
|
|
|
|
{
|
|
|
|
|
/* Map device input to image channels. */
|
|
|
|
|
input_to_image_channel.clear();
|
|
|
|
|
input_to_image_channel.resize(INPUT_NUM_CHANNELS, -1);
|
|
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
for (const ChannelMapping &mapping : input_channels()) {
|
2024-12-29 17:32:00 +01:00
|
|
|
const vector<string>::iterator i = find(channels.begin(), channels.end(), mapping.name);
|
2019-02-06 12:57:10 +01:00
|
|
|
if (i == channels.end()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-29 17:32:00 +01:00
|
|
|
const size_t input_channel = mapping.channel;
|
|
|
|
|
const size_t layer_channel = i - channels.begin();
|
2019-02-06 12:57:10 +01:00
|
|
|
input_to_image_channel[input_channel] = layer_to_image_channel[layer_channel];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Map device output to image channels. */
|
|
|
|
|
output_to_image_channel.clear();
|
|
|
|
|
output_to_image_channel.resize(OUTPUT_NUM_CHANNELS, -1);
|
|
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
for (const ChannelMapping &mapping : output_channels()) {
|
2024-12-29 17:32:00 +01:00
|
|
|
const vector<string>::iterator i = find(channels.begin(), channels.end(), mapping.name);
|
2019-02-06 12:57:10 +01:00
|
|
|
if (i == channels.end()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-29 17:32:00 +01:00
|
|
|
const size_t output_channel = mapping.channel;
|
|
|
|
|
const size_t layer_channel = i - channels.begin();
|
2019-02-06 12:57:10 +01:00
|
|
|
output_to_image_channel[output_channel] = layer_to_image_channel[layer_channel];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check that all buffer channels are correctly set. */
|
|
|
|
|
for (int i = 0; i < INPUT_NUM_CHANNELS; i++) {
|
|
|
|
|
assert(input_to_image_channel[i] >= 0);
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < OUTPUT_NUM_CHANNELS; i++) {
|
|
|
|
|
assert(output_to_image_channel[i] >= 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
bool DenoiseImageLayer::match_channels(const std::vector<string> &channelnames,
|
2019-02-06 12:57:10 +01:00
|
|
|
const std::vector<string> &neighbor_channelnames)
|
|
|
|
|
{
|
2022-01-04 21:39:54 +01:00
|
|
|
vector<int> &mapping = previous_output_to_image_channel;
|
2019-02-06 12:57:10 +01:00
|
|
|
|
2024-12-26 17:53:59 +01:00
|
|
|
assert(mapping.empty());
|
2022-01-04 21:39:54 +01:00
|
|
|
mapping.resize(output_to_image_channel.size(), -1);
|
2019-02-06 12:57:10 +01:00
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
for (int i = 0; i < output_to_image_channel.size(); i++) {
|
|
|
|
|
const string &channel = channelnames[output_to_image_channel[i]];
|
2024-12-29 17:32:00 +01:00
|
|
|
const std::vector<string>::const_iterator frame_channel = find(
|
2019-02-06 12:57:10 +01:00
|
|
|
neighbor_channelnames.begin(), neighbor_channelnames.end(), channel);
|
|
|
|
|
|
|
|
|
|
if (frame_channel == neighbor_channelnames.end()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mapping[i] = frame_channel - neighbor_channelnames.begin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Denoise Task */
|
|
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
DenoiseTask::DenoiseTask(Device *device, DenoiserPipeline *denoiser, const int frame)
|
2022-01-04 21:39:54 +01:00
|
|
|
: denoiser(denoiser), device(device), frame(frame), current_layer(0), buffers(device)
|
2019-02-06 12:57:10 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DenoiseTask::~DenoiseTask()
|
|
|
|
|
{
|
|
|
|
|
free();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Denoiser Operations */
|
|
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
bool DenoiseTask::load_input_pixels(const int layer)
|
2019-02-06 12:57:10 +01:00
|
|
|
{
|
|
|
|
|
/* Load center image */
|
2024-12-29 17:32:00 +01:00
|
|
|
const DenoiseImageLayer &image_layer = image.layers[layer];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
float *buffer_data = buffers.buffer.data();
|
|
|
|
|
image.read_pixels(image_layer, buffers.params, buffer_data);
|
2019-05-31 22:38:50 +02:00
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
/* Load previous image */
|
|
|
|
|
if (frame > 0 && !image.read_previous_pixels(image_layer, buffers.params, buffer_data)) {
|
|
|
|
|
error = "Failed to read neighbor frame pixels";
|
|
|
|
|
return false;
|
2019-02-06 12:57:10 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
/* Copy to device */
|
2022-01-04 21:39:54 +01:00
|
|
|
buffers.buffer.copy_to_device();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Task stages */
|
|
|
|
|
|
2024-12-30 02:20:36 +01:00
|
|
|
static void add_pass(unique_ptr_vector<Pass> &passes,
|
|
|
|
|
PassType type,
|
|
|
|
|
PassMode mode = PassMode::NOISY)
|
2022-01-04 21:39:54 +01:00
|
|
|
{
|
2024-12-30 02:20:36 +01:00
|
|
|
unique_ptr<Pass> pass = make_unique<Pass>();
|
2022-01-04 21:39:54 +01:00
|
|
|
pass->set_type(type);
|
|
|
|
|
pass->set_mode(mode);
|
|
|
|
|
|
2024-12-30 02:20:36 +01:00
|
|
|
passes.push_back(std::move(pass));
|
2022-01-04 21:39:54 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
bool DenoiseTask::load()
|
|
|
|
|
{
|
2024-12-29 17:32:00 +01:00
|
|
|
const string center_filepath = denoiser->input[frame];
|
2019-02-06 12:57:10 +01:00
|
|
|
if (!image.load(center_filepath, error)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
/* Use previous frame output as input for subsequent frames. */
|
|
|
|
|
if (frame > 0 && !image.load_previous(denoiser->output[frame - 1], error)) {
|
2019-02-06 12:57:10 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (image.layers.empty()) {
|
|
|
|
|
error = "No image layers found to denoise in " + center_filepath;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
/* Enable temporal denoising for frames after the first (which will use the output from the
|
|
|
|
|
* previous frames). */
|
|
|
|
|
DenoiseParams params = denoiser->denoiser->get_params();
|
|
|
|
|
params.temporally_stable = frame > 0;
|
|
|
|
|
denoiser->denoiser->set_params(params);
|
|
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
/* Allocate device buffer. */
|
2024-12-30 02:20:36 +01:00
|
|
|
unique_ptr_vector<Pass> passes;
|
2022-01-04 21:39:54 +01:00
|
|
|
add_pass(passes, PassType::PASS_COMBINED);
|
|
|
|
|
add_pass(passes, PassType::PASS_DENOISING_ALBEDO);
|
|
|
|
|
add_pass(passes, PassType::PASS_DENOISING_NORMAL);
|
|
|
|
|
add_pass(passes, PassType::PASS_MOTION);
|
|
|
|
|
add_pass(passes, PassType::PASS_DENOISING_PREVIOUS);
|
|
|
|
|
add_pass(passes, PassType::PASS_COMBINED, PassMode::DENOISED);
|
|
|
|
|
|
|
|
|
|
BufferParams buffer_params;
|
|
|
|
|
buffer_params.width = image.width;
|
|
|
|
|
buffer_params.height = image.height;
|
|
|
|
|
buffer_params.full_x = 0;
|
|
|
|
|
buffer_params.full_y = 0;
|
|
|
|
|
buffer_params.full_width = image.width;
|
|
|
|
|
buffer_params.full_height = image.height;
|
|
|
|
|
buffer_params.update_passes(passes);
|
|
|
|
|
|
2024-12-30 02:20:36 +01:00
|
|
|
passes.clear();
|
2022-01-04 21:39:54 +01:00
|
|
|
|
|
|
|
|
buffers.reset(buffer_params);
|
2019-02-06 12:57:10 +01:00
|
|
|
|
|
|
|
|
/* Read pixels for first layer. */
|
|
|
|
|
current_layer = 0;
|
|
|
|
|
if (!load_input_pixels(current_layer)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DenoiseTask::exec()
|
|
|
|
|
{
|
|
|
|
|
for (current_layer = 0; current_layer < image.layers.size(); current_layer++) {
|
|
|
|
|
/* Read pixels for secondary layers, first was already loaded. */
|
|
|
|
|
if (current_layer > 0) {
|
|
|
|
|
if (!load_input_pixels(current_layer)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Run task on device. */
|
2022-01-04 21:39:54 +01:00
|
|
|
denoiser->denoiser->denoise_buffer(buffers.params, &buffers, 1, true);
|
|
|
|
|
|
|
|
|
|
/* Copy denoised pixels from device. */
|
|
|
|
|
buffers.buffer.copy_from_device();
|
|
|
|
|
|
2024-12-29 17:32:00 +01:00
|
|
|
float *result = buffers.buffer.data();
|
|
|
|
|
float *out = image.pixels.data();
|
2022-01-04 21:39:54 +01:00
|
|
|
|
|
|
|
|
const DenoiseImageLayer &layer = image.layers[current_layer];
|
|
|
|
|
const int *output_to_image_channel = layer.output_to_image_channel.data();
|
|
|
|
|
|
|
|
|
|
for (int y = 0; y < image.height; y++) {
|
|
|
|
|
for (int x = 0; x < image.width; x++, result += buffers.params.pass_stride) {
|
|
|
|
|
for (int j = 0; j < OUTPUT_NUM_CHANNELS; j++) {
|
2024-12-29 17:32:00 +01:00
|
|
|
const int offset = buffers.params.get_pass_offset(PASS_COMBINED, PassMode::DENOISED);
|
|
|
|
|
const int image_channel = output_to_image_channel[j];
|
2022-01-04 21:39:54 +01:00
|
|
|
out[image.num_channels * x + image_channel] = result[offset + j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
out += image.num_channels * image.width;
|
|
|
|
|
}
|
2019-02-06 12:57:10 +01:00
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DenoiseTask::save()
|
|
|
|
|
{
|
2024-12-29 17:32:00 +01:00
|
|
|
const bool ok = image.save_output(denoiser->output[frame], error);
|
2019-02-06 12:57:10 +01:00
|
|
|
free();
|
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DenoiseTask::free()
|
|
|
|
|
{
|
|
|
|
|
image.free();
|
2022-01-04 21:39:54 +01:00
|
|
|
buffers.buffer.free();
|
2019-02-06 12:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Denoise Image Storage */
|
|
|
|
|
|
|
|
|
|
DenoiseImage::DenoiseImage()
|
|
|
|
|
{
|
|
|
|
|
width = 0;
|
|
|
|
|
height = 0;
|
|
|
|
|
num_channels = 0;
|
|
|
|
|
samples = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DenoiseImage::~DenoiseImage()
|
|
|
|
|
{
|
|
|
|
|
free();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DenoiseImage::close_input()
|
|
|
|
|
{
|
2022-01-04 21:39:54 +01:00
|
|
|
in_previous.reset();
|
2019-02-06 12:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DenoiseImage::free()
|
|
|
|
|
{
|
|
|
|
|
close_input();
|
|
|
|
|
pixels.clear();
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
bool DenoiseImage::parse_channels(const ImageSpec &in_spec, string &error)
|
|
|
|
|
{
|
|
|
|
|
const std::vector<string> &channels = in_spec.channelnames;
|
|
|
|
|
const ParamValue *multiview = in_spec.find_attribute("multiView");
|
|
|
|
|
const bool multiview_channels = (multiview && multiview->type().basetype == TypeDesc::STRING &&
|
|
|
|
|
multiview->type().arraylen >= 2);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
layers.clear();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
/* Loop over all the channels in the file, parse their name and sort them
|
|
|
|
|
* by RenderLayer.
|
|
|
|
|
* Channels that can't be parsed are directly passed through to the output. */
|
|
|
|
|
map<string, DenoiseImageLayer> file_layers;
|
|
|
|
|
for (int i = 0; i < channels.size(); i++) {
|
2024-12-29 17:32:00 +01:00
|
|
|
string layer;
|
|
|
|
|
string pass;
|
|
|
|
|
string channel;
|
2019-02-06 12:57:10 +01:00
|
|
|
if (parse_channel_name(channels[i], layer, pass, channel, multiview_channels)) {
|
|
|
|
|
file_layers[layer].channels.push_back(pass + "." + channel);
|
|
|
|
|
file_layers[layer].layer_to_image_channel.push_back(i);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2019-02-06 12:57:10 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
/* Loop over all detected RenderLayers, check whether they contain a full set of input channels.
|
|
|
|
|
* Any channels that won't be processed internally are also passed through. */
|
|
|
|
|
for (map<string, DenoiseImageLayer>::iterator i = file_layers.begin(); i != file_layers.end();
|
2024-01-02 18:12:54 +01:00
|
|
|
++i)
|
|
|
|
|
{
|
2019-02-06 12:57:10 +01:00
|
|
|
const string &name = i->first;
|
|
|
|
|
DenoiseImageLayer &layer = i->second;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
/* Check for full pass set. */
|
|
|
|
|
if (!layer.detect_denoising_channels()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
layer.name = name;
|
|
|
|
|
layer.samples = samples;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-05-01 21:14:11 +10:00
|
|
|
/* If the sample value isn't set yet, check if there is a layer-specific one in the input file.
|
|
|
|
|
*/
|
2019-02-06 12:57:10 +01:00
|
|
|
if (layer.samples < 1) {
|
2024-12-29 17:32:00 +01:00
|
|
|
const string sample_string = in_spec.get_string_attribute("cycles." + name + ".samples", "");
|
2024-12-26 17:53:59 +01:00
|
|
|
if (!sample_string.empty()) {
|
2019-02-06 12:57:10 +01:00
|
|
|
if (!sscanf(sample_string.c_str(), "%d", &layer.samples)) {
|
|
|
|
|
error = "Failed to parse samples metadata: " + sample_string;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
if (layer.samples < 1) {
|
|
|
|
|
error = string_printf(
|
|
|
|
|
"No sample number specified in the file for layer %s or on the command line",
|
|
|
|
|
name.c_str());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
layers.push_back(layer);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
void DenoiseImage::read_pixels(const DenoiseImageLayer &layer,
|
|
|
|
|
const BufferParams ¶ms,
|
|
|
|
|
float *input_pixels)
|
2019-02-06 12:57:10 +01:00
|
|
|
{
|
|
|
|
|
/* Pixels from center file have already been loaded into pixels.
|
|
|
|
|
* We copy a subset into the device input buffer with channels reshuffled. */
|
|
|
|
|
const int *input_to_image_channel = layer.input_to_image_channel.data();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
for (int i = 0; i < width * height; i++) {
|
2022-01-04 21:39:54 +01:00
|
|
|
for (int j = 0; j < 3; ++j) {
|
2024-12-29 17:32:00 +01:00
|
|
|
const int offset = params.get_pass_offset(PASS_COMBINED);
|
|
|
|
|
const int image_channel = input_to_image_channel[INPUT_NOISY_IMAGE + j];
|
2022-01-04 21:39:54 +01:00
|
|
|
input_pixels[i * params.pass_stride + offset + j] =
|
|
|
|
|
pixels[((size_t)i) * num_channels + image_channel];
|
|
|
|
|
}
|
|
|
|
|
for (int j = 0; j < 3; ++j) {
|
2024-12-29 17:32:00 +01:00
|
|
|
const int offset = params.get_pass_offset(PASS_DENOISING_NORMAL);
|
|
|
|
|
const int image_channel = input_to_image_channel[INPUT_DENOISING_NORMAL + j];
|
2022-01-04 21:39:54 +01:00
|
|
|
input_pixels[i * params.pass_stride + offset + j] =
|
|
|
|
|
pixels[((size_t)i) * num_channels + image_channel];
|
|
|
|
|
}
|
|
|
|
|
for (int j = 0; j < 3; ++j) {
|
2024-12-29 17:32:00 +01:00
|
|
|
const int offset = params.get_pass_offset(PASS_DENOISING_ALBEDO);
|
|
|
|
|
const int image_channel = input_to_image_channel[INPUT_DENOISING_ALBEDO + j];
|
2022-01-04 21:39:54 +01:00
|
|
|
input_pixels[i * params.pass_stride + offset + j] =
|
|
|
|
|
pixels[((size_t)i) * num_channels + image_channel];
|
|
|
|
|
}
|
|
|
|
|
for (int j = 0; j < 4; ++j) {
|
2024-12-29 17:32:00 +01:00
|
|
|
const int offset = params.get_pass_offset(PASS_MOTION);
|
|
|
|
|
const int image_channel = input_to_image_channel[INPUT_MOTION + j];
|
2022-01-04 21:39:54 +01:00
|
|
|
input_pixels[i * params.pass_stride + offset + j] =
|
2019-02-06 12:57:10 +01:00
|
|
|
pixels[((size_t)i) * num_channels + image_channel];
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
bool DenoiseImage::read_previous_pixels(const DenoiseImageLayer &layer,
|
|
|
|
|
const BufferParams ¶ms,
|
2019-02-06 12:57:10 +01:00
|
|
|
float *input_pixels)
|
|
|
|
|
{
|
|
|
|
|
/* Load pixels from neighboring frames, and copy them into device buffer
|
|
|
|
|
* with channels reshuffled. */
|
2022-12-20 02:57:02 +01:00
|
|
|
const size_t num_pixels = (size_t)width * (size_t)height;
|
|
|
|
|
const int num_channels = in_previous->spec().nchannels;
|
|
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
array<float> neighbor_pixels(num_pixels * num_channels);
|
2022-12-20 02:57:02 +01:00
|
|
|
|
|
|
|
|
if (!in_previous->read_image(0, 0, 0, num_channels, TypeDesc::FLOAT, neighbor_pixels.data())) {
|
2019-02-06 12:57:10 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
const int *output_to_image_channel = layer.previous_output_to_image_channel.data();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
for (int i = 0; i < width * height; i++) {
|
2022-01-04 21:39:54 +01:00
|
|
|
for (int j = 0; j < 3; ++j) {
|
2024-12-29 17:32:00 +01:00
|
|
|
const int offset = params.get_pass_offset(PASS_DENOISING_PREVIOUS);
|
|
|
|
|
const int image_channel = output_to_image_channel[j];
|
2022-01-04 21:39:54 +01:00
|
|
|
input_pixels[i * params.pass_stride + offset + j] =
|
2019-02-06 12:57:10 +01:00
|
|
|
neighbor_pixels[((size_t)i) * num_channels + image_channel];
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
bool DenoiseImage::load(const string &in_filepath, string &error)
|
|
|
|
|
{
|
|
|
|
|
if (!Filesystem::is_regular(in_filepath)) {
|
|
|
|
|
error = "Couldn't find file: " + in_filepath;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-14 13:57:28 +01:00
|
|
|
unique_ptr<ImageInput> in(ImageInput::open(in_filepath));
|
2019-02-06 12:57:10 +01:00
|
|
|
if (!in) {
|
|
|
|
|
error = "Couldn't open file: " + in_filepath;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-14 13:57:28 +01:00
|
|
|
in_spec = in->spec();
|
2019-02-06 12:57:10 +01:00
|
|
|
width = in_spec.width;
|
|
|
|
|
height = in_spec.height;
|
|
|
|
|
num_channels = in_spec.nchannels;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
if (!parse_channels(in_spec, error)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
if (layers.empty()) {
|
|
|
|
|
error = "Could not find a render layer containing denoising data and motion vector passes";
|
2019-02-06 12:57:10 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-12-29 17:32:00 +01:00
|
|
|
const size_t num_pixels = (size_t)width * (size_t)height;
|
2019-02-06 12:57:10 +01:00
|
|
|
pixels.resize(num_pixels * num_channels);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
/* Read all channels into buffer. Reading all channels at once is faster
|
|
|
|
|
* than individually due to interleaved EXR channel storage. */
|
2022-12-20 02:57:02 +01:00
|
|
|
if (!in->read_image(0, 0, 0, num_channels, TypeDesc::FLOAT, pixels.data())) {
|
2019-02-06 12:57:10 +01:00
|
|
|
error = "Failed to read image: " + in_filepath;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
bool DenoiseImage::load_previous(const string &filepath, string &error)
|
2019-04-17 06:17:24 +02:00
|
|
|
{
|
2022-01-04 21:39:54 +01:00
|
|
|
if (!Filesystem::is_regular(filepath)) {
|
|
|
|
|
error = "Couldn't find neighbor frame: " + filepath;
|
2019-02-06 12:57:10 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
unique_ptr<ImageInput> in_neighbor(ImageInput::open(filepath));
|
|
|
|
|
if (!in_neighbor) {
|
|
|
|
|
error = "Couldn't open neighbor frame: " + filepath;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
const ImageSpec &neighbor_spec = in_neighbor->spec();
|
|
|
|
|
if (neighbor_spec.width != width || neighbor_spec.height != height) {
|
|
|
|
|
error = "Neighbor frame has different dimensions: " + filepath;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
for (DenoiseImageLayer &layer : layers) {
|
|
|
|
|
if (!layer.match_channels(in_spec.channelnames, neighbor_spec.channelnames)) {
|
|
|
|
|
error = "Neighbor frame misses denoising data passes: " + filepath;
|
2019-02-06 12:57:10 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-01-04 21:39:54 +01:00
|
|
|
in_previous = std::move(in_neighbor);
|
|
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
bool DenoiseImage::save_output(const string &out_filepath, string &error)
|
|
|
|
|
{
|
|
|
|
|
/* Save image with identical dimensions, channels and metadata. */
|
2019-02-14 13:57:28 +01:00
|
|
|
ImageSpec out_spec = in_spec;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
/* Ensure that the output frame contains sample information even if the input didn't. */
|
|
|
|
|
for (int i = 0; i < layers.size(); i++) {
|
2024-12-29 17:32:00 +01:00
|
|
|
const string name = "cycles." + layers[i].name + ".samples";
|
2019-02-06 12:57:10 +01:00
|
|
|
if (!out_spec.find_attribute(name, TypeDesc::STRING)) {
|
|
|
|
|
out_spec.attribute(name, TypeDesc::STRING, string_printf("%d", layers[i].samples));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We don't need input anymore at this point, and will possibly
|
|
|
|
|
* overwrite the same file. */
|
|
|
|
|
close_input();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
/* Write to temporary file path, so we denoise images in place and don't
|
|
|
|
|
* risk destroying files when something goes wrong in file saving. */
|
2024-12-29 17:32:00 +01:00
|
|
|
const string extension = OIIO::Filesystem::extension(out_filepath);
|
|
|
|
|
const string unique_name = ".denoise-tmp-" + OIIO::Filesystem::unique_path();
|
|
|
|
|
const string tmp_filepath = out_filepath + unique_name + extension;
|
2019-02-12 17:10:31 +01:00
|
|
|
unique_ptr<ImageOutput> out(ImageOutput::create(tmp_filepath));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
if (!out) {
|
|
|
|
|
error = "Failed to open temporary file " + tmp_filepath + " for writing";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
/* Open temporary file and write image buffers. */
|
|
|
|
|
if (!out->open(tmp_filepath, out_spec)) {
|
|
|
|
|
error = "Failed to open file " + tmp_filepath + " for writing: " + out->geterror();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
bool ok = true;
|
|
|
|
|
if (!out->write_image(TypeDesc::FLOAT, pixels.data())) {
|
|
|
|
|
error = "Failed to write to file " + tmp_filepath + ": " + out->geterror();
|
|
|
|
|
ok = false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
if (!out->close()) {
|
|
|
|
|
error = "Failed to save to file " + tmp_filepath + ": " + out->geterror();
|
|
|
|
|
ok = false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-12 17:10:31 +01:00
|
|
|
out.reset();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-02-05 16:23:34 +11:00
|
|
|
/* Copy temporary file to output filepath. */
|
2019-02-28 19:15:40 +01:00
|
|
|
string rename_error;
|
|
|
|
|
if (ok && !OIIO::Filesystem::rename(tmp_filepath, out_filepath, rename_error)) {
|
|
|
|
|
error = "Failed to move denoised image to " + out_filepath + ": " + rename_error;
|
2019-02-06 12:57:10 +01:00
|
|
|
ok = false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
if (!ok) {
|
|
|
|
|
OIIO::Filesystem::remove(tmp_filepath);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-28 19:15:40 +01:00
|
|
|
return ok;
|
2019-02-06 12:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* File pattern handling and outer loop over frames */
|
|
|
|
|
|
2024-06-03 22:41:25 +02:00
|
|
|
DenoiserPipeline::DenoiserPipeline(DeviceInfo &denoiser_device_info, const DenoiseParams ¶ms)
|
2019-02-06 12:57:10 +01:00
|
|
|
{
|
|
|
|
|
/* Initialize task scheduler. */
|
|
|
|
|
TaskScheduler::init();
|
|
|
|
|
|
|
|
|
|
/* Initialize device. */
|
2024-06-07 17:53:44 +02:00
|
|
|
device = Device::create(denoiser_device_info, stats, profiler, true);
|
Cycles: merge of cycles-x branch, a major update to the renderer
This includes much improved GPU rendering performance, viewport interactivity,
new shadow catcher, revamped sampling settings, subsurface scattering anisotropy,
new GPU volume sampling, improved PMJ sampling pattern, and more.
Some features have also been removed or changed, breaking backwards compatibility.
Including the removal of the OpenCL backend, for which alternatives are under
development.
Release notes and code docs:
https://wiki.blender.org/wiki/Reference/Release_Notes/3.0/Cycles
https://wiki.blender.org/wiki/Source/Render/Cycles
Credits:
* Sergey Sharybin
* Brecht Van Lommel
* Patrick Mours (OptiX backend)
* Christophe Hery (subsurface scattering anisotropy)
* William Leeson (PMJ sampling pattern)
* Alaska (various fixes and tweaks)
* Thomas Dinges (various fixes)
For the full commit history, see the cycles-x branch. This squashes together
all the changes since intermediate changes would often fail building or tests.
Ref T87839, T87837, T87836
Fixes T90734, T89353, T80267, T80267, T77185, T69800
2021-09-20 17:59:20 +02:00
|
|
|
device->load_kernels(KERNEL_FEATURE_DENOISING);
|
2022-01-04 21:39:54 +01:00
|
|
|
|
2024-06-03 22:41:25 +02:00
|
|
|
vector<DeviceInfo> cpu_devices;
|
|
|
|
|
device_cpu_info(cpu_devices);
|
2024-06-07 17:53:44 +02:00
|
|
|
cpu_device = device_cpu_create(cpu_devices[0], device->stats, device->profiler, true);
|
2024-06-03 22:41:25 +02:00
|
|
|
|
2024-12-29 23:13:45 +01:00
|
|
|
denoiser = Denoiser::create(device.get(), cpu_device.get(), params);
|
2024-12-31 15:19:31 +01:00
|
|
|
if (denoiser) {
|
|
|
|
|
denoiser->load_kernels(nullptr);
|
|
|
|
|
}
|
2019-02-06 12:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
Cycles: merge of cycles-x branch, a major update to the renderer
This includes much improved GPU rendering performance, viewport interactivity,
new shadow catcher, revamped sampling settings, subsurface scattering anisotropy,
new GPU volume sampling, improved PMJ sampling pattern, and more.
Some features have also been removed or changed, breaking backwards compatibility.
Including the removal of the OpenCL backend, for which alternatives are under
development.
Release notes and code docs:
https://wiki.blender.org/wiki/Reference/Release_Notes/3.0/Cycles
https://wiki.blender.org/wiki/Source/Render/Cycles
Credits:
* Sergey Sharybin
* Brecht Van Lommel
* Patrick Mours (OptiX backend)
* Christophe Hery (subsurface scattering anisotropy)
* William Leeson (PMJ sampling pattern)
* Alaska (various fixes and tweaks)
* Thomas Dinges (various fixes)
For the full commit history, see the cycles-x branch. This squashes together
all the changes since intermediate changes would often fail building or tests.
Ref T87839, T87837, T87836
Fixes T90734, T89353, T80267, T80267, T77185, T69800
2021-09-20 17:59:20 +02:00
|
|
|
DenoiserPipeline::~DenoiserPipeline()
|
2019-02-06 12:57:10 +01:00
|
|
|
{
|
2022-01-04 21:39:54 +01:00
|
|
|
denoiser.reset();
|
2024-12-29 23:13:45 +01:00
|
|
|
device.reset();
|
2019-02-06 12:57:10 +01:00
|
|
|
TaskScheduler::exit();
|
|
|
|
|
}
|
|
|
|
|
|
Cycles: merge of cycles-x branch, a major update to the renderer
This includes much improved GPU rendering performance, viewport interactivity,
new shadow catcher, revamped sampling settings, subsurface scattering anisotropy,
new GPU volume sampling, improved PMJ sampling pattern, and more.
Some features have also been removed or changed, breaking backwards compatibility.
Including the removal of the OpenCL backend, for which alternatives are under
development.
Release notes and code docs:
https://wiki.blender.org/wiki/Reference/Release_Notes/3.0/Cycles
https://wiki.blender.org/wiki/Source/Render/Cycles
Credits:
* Sergey Sharybin
* Brecht Van Lommel
* Patrick Mours (OptiX backend)
* Christophe Hery (subsurface scattering anisotropy)
* William Leeson (PMJ sampling pattern)
* Alaska (various fixes and tweaks)
* Thomas Dinges (various fixes)
For the full commit history, see the cycles-x branch. This squashes together
all the changes since intermediate changes would often fail building or tests.
Ref T87839, T87837, T87836
Fixes T90734, T89353, T80267, T80267, T77185, T69800
2021-09-20 17:59:20 +02:00
|
|
|
bool DenoiserPipeline::run()
|
2019-02-06 12:57:10 +01:00
|
|
|
{
|
|
|
|
|
assert(input.size() == output.size());
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-12-29 17:32:00 +01:00
|
|
|
const int num_frames = output.size();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-12-31 15:19:31 +01:00
|
|
|
if (!denoiser) {
|
|
|
|
|
error = "Failed to create denoiser";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
for (int frame = 0; frame < num_frames; frame++) {
|
|
|
|
|
/* Skip empty output paths. */
|
|
|
|
|
if (output[frame].empty()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
/* Execute task. */
|
2024-12-29 23:13:45 +01:00
|
|
|
DenoiseTask task(device.get(), this, frame);
|
2019-02-06 12:57:10 +01:00
|
|
|
if (!task.load()) {
|
|
|
|
|
error = task.error;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
if (!task.exec()) {
|
|
|
|
|
error = task.error;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
if (!task.save()) {
|
|
|
|
|
error = task.error;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
task.free();
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-06 12:57:10 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|