Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/appleseed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,10 @@ set (renderer_kernel_rendering_sources
renderer/kernel/rendering/tilecallbackcollection.h
renderer/kernel/rendering/timedrenderercontroller.cpp
renderer/kernel/rendering/timedrenderercontroller.h
renderer/kernel/rendering/variancetrackingshadingresultframebuffer.cpp
renderer/kernel/rendering/variancetrackingshadingresultframebuffer.h
renderer/kernel/rendering/variancetrackingshadingresultframebufferfactory.cpp
renderer/kernel/rendering/variancetrackingshadingresultframebufferfactory.h
)
list (APPEND appleseed_sources
${renderer_kernel_rendering_sources}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,11 @@ void EphemeralShadingResultFrameBufferFactory::destroy(
delete framebuffer;
}

size_t EphemeralShadingResultFrameBufferFactory::get_total_channel_count(
const size_t aov_count) const
{
return ShadingResultFrameBuffer::get_total_channel_count(
aov_count);
}

} // namespace renderer
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class EphemeralShadingResultFrameBufferFactory

void destroy(
ShadingResultFrameBuffer* framebuffer) override;

size_t get_total_channel_count(
const size_t aov_count) const override;
};

} // namespace renderer
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class IShadingResultFrameBufferFactory

virtual void destroy(
ShadingResultFrameBuffer* framebuffer) = 0;

virtual size_t get_total_channel_count(
const size_t aov_count) const = 0;
};

} // namespace renderer
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,11 @@ void PermanentShadingResultFrameBufferFactory::destroy(
{
}

size_t PermanentShadingResultFrameBufferFactory::get_total_channel_count(
const size_t aov_count) const
{
return ShadingResultFrameBuffer::get_total_channel_count(
aov_count);
}

} // namespace renderer
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ class PermanentShadingResultFrameBufferFactory
void destroy(
ShadingResultFrameBuffer* framebuffer) override;

size_t get_total_channel_count(
const size_t aov_count) const override;

private:
std::vector<ShadingResultFrameBuffer*> m_framebuffers;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

// Standard headers.
#include <cassert>
#include <typeinfo>

using namespace foundation;
using namespace std;
Expand Down Expand Up @@ -107,6 +108,7 @@ void ShadingResultFrameBuffer::merge(
const size_t source_y,
const float scaling)
{
assert(typeid(this) == typeid(source));
assert(m_channel_count == source.m_channel_count);

const float* APPLESEED_RESTRICT source_ptr = source.pixel(source_x, source_y);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ class ShadingResultFrameBuffer
const size_t aov_count,
const foundation::AABB2u& crop_window);

virtual ~ShadingResultFrameBuffer() {};

static size_t get_total_channel_count(const size_t aov_count);

void add(
virtual void add(
const foundation::Vector2u& pi,
const ShadingResult& sample);

Expand All @@ -75,13 +77,15 @@ class ShadingResultFrameBuffer
const size_t source_y,
const float scaling);

void develop_to_tile(
virtual void develop_to_tile(
foundation::Tile& tile,
TileStack& aov_tiles) const;

protected:
std::vector<float> m_scratch;

private:
const size_t m_aov_count;
std::vector<float> m_scratch;
};

inline size_t ShadingResultFrameBuffer::get_total_channel_count(const size_t aov_count)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@

//
// This source file is part of appleseed.
// Visit https://appleseedhq.net/ for additional information and resources.
//
// This software is released under the MIT license.
//
// Copyright (c) 2019 Stephen Agyemang, The appleseedhq Organization
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

// Interface header.
#include "variancetrackingshadingresultframebuffer.h"

// appleseed.renderer headers.
#include "renderer/kernel/aov/tilestack.h"
#include "renderer/kernel/shading/shadingresult.h"

// appleseed.foundation headers.
#include "foundation/image/color.h"
#include "foundation/image/colorspace.h"
#include "foundation/image/tile.h"
#include "foundation/platform/compiler.h"

// Standard headers.
#include <cassert>

using namespace foundation;
using namespace std;

namespace renderer
{

VarianceTrackingShadingResultFrameBuffer::VarianceTrackingShadingResultFrameBuffer(
const size_t width,
const size_t height,
const size_t aov_count)
: ShadingResultFrameBuffer(
width,
height,
aov_count + 1)
, m_aov_count(aov_count)
{
}

VarianceTrackingShadingResultFrameBuffer::VarianceTrackingShadingResultFrameBuffer(
const size_t width,
const size_t height,
const size_t aov_count,
const AABB2u& crop_window)
: ShadingResultFrameBuffer(
width,
height,
aov_count + 1,
crop_window)
, m_aov_count(aov_count)
{
}

void VarianceTrackingShadingResultFrameBuffer::add(
const Vector2u& pi,
const ShadingResult& sample)
{
float* ptr = &m_scratch[0];

const float main_0 = sample.m_main[0];
const float main_1 = sample.m_main[1];
const float main_2 = sample.m_main[2];
const float main_3 = sample.m_main[3];

*ptr++ = main_0;
*ptr++ = main_1;
*ptr++ = main_2;
*ptr++ = main_3;

for (size_t i = 0, e = m_aov_count; i < e; ++i)
{
const Color4f& aov = sample.m_aovs[i];
*ptr++ = aov[0];
*ptr++ = aov[1];
*ptr++ = aov[2];
*ptr++ = aov[3];
}

// Put squared samples in the last buffer channels.
*ptr++ = main_0 * main_0;
*ptr++ = main_1 * main_1;
*ptr++ = main_2 * main_2;
*ptr++ = main_3; // do not square alpha

AccumulatorTile::add(pi, &m_scratch[0]);
}

void VarianceTrackingShadingResultFrameBuffer::develop_to_tile(
Tile& tile,
TileStack& aov_tiles) const
{
const float* ptr = pixel(0);

for (size_t y = 0, h = m_height; y < h; ++y)
{
for (size_t x = 0, w = m_width; x < w; ++x)
{
const float weight = *ptr++;
const float rcp_weight = weight == 0.0f ? 0.0f : 1.0f / weight;

const Color4f color(ptr[0], ptr[1], ptr[2], ptr[3]);
tile.set_pixel(x, y, color * rcp_weight);
ptr += 4;

for (size_t i = 0, e = m_aov_count; i < e; ++i)
{
const Color4f aov(ptr[0], ptr[1], ptr[2], ptr[3]);
aov_tiles.set_pixel(x, y, i, aov * rcp_weight);
ptr += 4;
}

ptr += 4; // Skip sum of squared samples
}
}
}

float VarianceTrackingShadingResultFrameBuffer::estimator_variance() const
{
float estimator_variance_sum = 0.0f;
const float* ptr = pixel(0);

for (size_t y = 0, h = m_height; y < h; ++y)
for (size_t x = 0, w = m_width; x < w; ++x)
{
const float weight = *ptr;
const float rcp_weight = weight == 0.0f ? 0.0f : 1.0f / weight;
const float rcp_weight_minus_one = weight - 1.0f == 0.0f ? 0.0f : 1.0f / (weight - 1.0f);

const Color3f pixel_value(
ptr[1] * rcp_weight,
ptr[2] * rcp_weight,
ptr[3] * rcp_weight);

ptr += m_channel_count - 4; // skip to beginning of summed squares

const Color3f square_sum(
ptr[0],
ptr[1],
ptr[2]);

// Estimator for the variance of the mean estimator (= pixel value): Sigma² / n.
// Sigma² is estimated as [sum_of_squares - n * pixel_value²] / (n - 1)
const Color3f sigma_2((square_sum - weight * pixel_value * pixel_value) * rcp_weight_minus_one);

// Clamp sigma² to mitigate the effect of fireflies.
const float estimator_variance = std::min(luminance(sigma_2), 5000.0f) * rcp_weight;

estimator_variance_sum += estimator_variance;

ptr += 4;
}

return estimator_variance_sum / get_pixel_count();
}

float VarianceTrackingShadingResultFrameBuffer::estimator_variance_to_tile(
Tile& tile) const
{
float estimator_variance_sum = 0.0f;
const float* ptr = pixel(0);

for (size_t y = 0, h = m_height; y < h; ++y)
for (size_t x = 0, w = m_width; x < w; ++x)
{
const float weight = *ptr;
const float rcp_weight = weight == 0.0f ? 0.0f : 1.0f / weight;
const float rcp_weight_minus_one = weight - 1.0f == 0.0f ? 0.0f : 1.0f / (weight - 1.0f);

// Clamp the values to mitigate the effect of fireflies.
const Color3f pixel_value(
ptr[1] * rcp_weight,
ptr[2] * rcp_weight,
ptr[3] * rcp_weight);

ptr += m_channel_count - 4; // skip to beginning of summed squares

const Color3f square_sum(
ptr[0],
ptr[1],
ptr[2]);

// Estimator for the variance of the mean estimator (= pixel value): Sigma² / n.
// Sigma² is estimated as [sum_of_squares - n * pixel_value²] / (n - 1)
const Color3f sigma_2((square_sum - weight * pixel_value * pixel_value) * rcp_weight_minus_one);

// Clamp sigma² to mitigate the effect of fireflies.
const float estimator_variance = std::min(luminance(sigma_2), 5000.0f) * rcp_weight;

tile.set_pixel(x, y, Color3f(estimator_variance));
estimator_variance_sum += estimator_variance;

ptr += 4;
}

return estimator_variance_sum / get_pixel_count();
}

} // namespace renderer
Loading