2021-01-02 21:42:02 +03:00
|
|
|
/*
|
2021-01-02 22:12:45 +03:00
|
|
|
* Copyright 2021 elven cache. All rights reserved.
|
2022-01-15 22:59:06 +03:00
|
|
|
* License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
|
2021-01-02 22:12:45 +03:00
|
|
|
*/
|
2021-01-02 21:42:02 +03:00
|
|
|
|
|
|
|
/*
|
2021-01-02 22:12:45 +03:00
|
|
|
* Implement SVGF style denoising as bgfx example. Goal is to explore various
|
|
|
|
* options and parameters, not produce an optimized, efficient denoiser.
|
|
|
|
*
|
|
|
|
* Starts with deferred rendering scene with very basic lighting. Lighting is
|
|
|
|
* masked out with a noise pattern to provide something to denoise. There are
|
|
|
|
* two options for the noise pattern. One is a fixed 2x2 dither pattern to
|
|
|
|
* stand-in for lighting at quarter resolution. The other is the common
|
|
|
|
* shadertoy random pattern as a stand-in for some fancier lighting without
|
|
|
|
* enough samples per pixel, like ray tracing.
|
|
|
|
*
|
|
|
|
* First a temporal denoising filter is applied. The temporal filter is only
|
|
|
|
* using normals to reject previous samples. The SVGF paper also describes using
|
|
|
|
* depth comparison to reject samples but that is not implemented here.
|
|
|
|
*
|
|
|
|
* Followed by some number of spatial filters. These are implemented like in the
|
|
|
|
* SVGF paper. As an alternative to the 5x5 Edge-Avoiding A-Trous filter, can
|
|
|
|
* select a 3x3 filter instead. The 3x3 filter takes fewer samples and covers a
|
|
|
|
* smaller area, but takes less time to compute. From a loosely eyeballed
|
|
|
|
* comparison, N 5x5 passes looks similar to N+1 3x3 passes. The wider spatial
|
|
|
|
* filters take a fair chunk of time to compute. I wonder if it would be a good
|
|
|
|
* idea to interleave the input texture before computing, after the first pass
|
|
|
|
* which skips zero pixels.
|
|
|
|
*
|
|
|
|
* I have not implemetened the variance guided part.
|
|
|
|
*
|
|
|
|
* There's also an optional TXAA pass to be applied after. I am not happy with
|
|
|
|
* its implementation yet, so it defaults to off here.
|
|
|
|
*/
|
2021-01-02 21:42:02 +03:00
|
|
|
|
|
|
|
/*
|
2021-01-02 22:12:45 +03:00
|
|
|
* Reference(s):
|
|
|
|
*
|
2021-01-02 22:23:13 +03:00
|
|
|
* - Spatiotemporal Variance-Guided Filtering: Real-Time Reconstruction for Path-Traced Global Illumination.
|
|
|
|
* https://web.archive.org/web/20170720213354/https://research.nvidia.com/sites/default/files/pubs/2017-07_Spatiotemporal-Variance-Guided-Filtering%3A/svgf_preprint.pdf
|
2021-01-02 22:12:45 +03:00
|
|
|
*
|
2021-01-02 22:23:13 +03:00
|
|
|
* - Streaming G-Buffer Compression for Multi-Sample Anti-Aliasing.
|
|
|
|
* https://web.archive.org/web/20200807211002/https://software.intel.com/content/www/us/en/develop/articles/streaming-g-buffer-compression-for-multi-sample-anti-aliasing.html
|
|
|
|
*
|
|
|
|
* - Edge-Avoiding A-Trous Wavelet Transform for fast Global Illumination Filtering
|
|
|
|
* https://web.archive.org/web/20130412085423/https://www.uni-ulm.de/fileadmin/website_uni_ulm/iui.inst.100/institut/Papers/atrousGIfilter.pdf
|
2021-01-02 22:12:45 +03:00
|
|
|
*
|
|
|
|
*/
|
2021-01-02 21:42:02 +03:00
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <camera.h>
|
|
|
|
#include <bgfx_utils.h>
|
|
|
|
#include <imgui/imgui.h>
|
|
|
|
#include <bx/rng.h>
|
|
|
|
#include <bx/os.h>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
#define DENOISE_MAX_PASSES 6
|
|
|
|
|
|
|
|
// Gbuffer has multiple render targets
|
|
|
|
#define GBUFFER_RT_COLOR 0
|
|
|
|
#define GBUFFER_RT_NORMAL 1
|
|
|
|
#define GBUFFER_RT_VELOCITY 2
|
|
|
|
#define GBUFFER_RT_DEPTH 3
|
|
|
|
#define GBUFFER_RENDER_TARGETS 4
|
|
|
|
|
|
|
|
#define MODEL_COUNT 100
|
|
|
|
|
|
|
|
static const char * s_meshPaths[] =
|
|
|
|
{
|
|
|
|
"meshes/column.bin",
|
|
|
|
"meshes/tree.bin",
|
|
|
|
"meshes/hollowcube.bin",
|
|
|
|
"meshes/bunny.bin"
|
|
|
|
};
|
|
|
|
|
|
|
|
static const float s_meshScale[] =
|
|
|
|
{
|
|
|
|
0.05f,
|
|
|
|
0.15f,
|
|
|
|
0.25f,
|
|
|
|
0.25f
|
|
|
|
};
|
|
|
|
|
|
|
|
// Vertex decl for our screen space quad (used in deferred rendering)
|
|
|
|
struct PosTexCoord0Vertex
|
|
|
|
{
|
|
|
|
float m_x;
|
|
|
|
float m_y;
|
|
|
|
float m_z;
|
|
|
|
float m_u;
|
|
|
|
float m_v;
|
|
|
|
|
|
|
|
static void init()
|
|
|
|
{
|
|
|
|
ms_layout
|
|
|
|
.begin()
|
|
|
|
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
|
|
|
|
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
|
|
|
|
.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bgfx::VertexLayout ms_layout;
|
|
|
|
};
|
|
|
|
|
|
|
|
bgfx::VertexLayout PosTexCoord0Vertex::ms_layout;
|
|
|
|
|
|
|
|
struct Uniforms
|
|
|
|
{
|
|
|
|
enum { NumVec4 = 13 };
|
|
|
|
|
|
|
|
void init() {
|
|
|
|
u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, NumVec4);
|
|
|
|
};
|
|
|
|
|
|
|
|
void submit() const {
|
|
|
|
bgfx::setUniform(u_params, m_params, NumVec4);
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroy() {
|
|
|
|
bgfx::destroy(u_params);
|
|
|
|
}
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
/* 0 */ struct { float m_cameraJitterCurr[2]; float m_cameraJitterPrev[2]; };
|
|
|
|
/* 1 */ struct { float m_feedbackMin; float m_feedbackMax; float m_unused1[2]; };
|
|
|
|
/* 2 */ struct { float m_unused2; float m_applyMitchellFilter; float m_options[2]; };
|
|
|
|
/* 3-6 */ struct { float m_worldToViewPrev[16]; };
|
|
|
|
/* 7-10 */ struct { float m_viewToProjPrev[16]; };
|
|
|
|
/* 11 */ struct { float m_frameOffsetForNoise; float m_noiseType; float m_unused11[2]; };
|
|
|
|
/* 12 */ struct { float m_denoiseStep; float m_sigmaDepth; float m_sigmaNormal; float m_unused12; };
|
|
|
|
};
|
|
|
|
|
|
|
|
float m_params[NumVec4 * 4];
|
|
|
|
};
|
|
|
|
|
|
|
|
bgfx::UniformHandle u_params;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RenderTarget
|
|
|
|
{
|
|
|
|
void init(uint32_t _width, uint32_t _height, bgfx::TextureFormat::Enum _format, uint64_t _flags)
|
|
|
|
{
|
|
|
|
m_texture = bgfx::createTexture2D(uint16_t(_width), uint16_t(_height), false, 1, _format, _flags);
|
|
|
|
const bool destroyTextures = true;
|
|
|
|
m_buffer = bgfx::createFrameBuffer(1, &m_texture, destroyTextures);
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroy()
|
|
|
|
{
|
|
|
|
// also responsible for destroying texture
|
|
|
|
bgfx::destroy(m_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
bgfx::TextureHandle m_texture;
|
|
|
|
bgfx::FrameBufferHandle m_buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
void screenSpaceQuad(float _textureWidth, float _textureHeight, float _texelHalf, bool _originBottomLeft, float _width = 1.0f, float _height = 1.0f)
|
|
|
|
{
|
2021-01-02 22:12:45 +03:00
|
|
|
if (3 == bgfx::getAvailTransientVertexBuffer(3, PosTexCoord0Vertex::ms_layout) )
|
2021-01-02 21:42:02 +03:00
|
|
|
{
|
|
|
|
bgfx::TransientVertexBuffer vb;
|
|
|
|
bgfx::allocTransientVertexBuffer(&vb, 3, PosTexCoord0Vertex::ms_layout);
|
|
|
|
PosTexCoord0Vertex* vertex = (PosTexCoord0Vertex*)vb.data;
|
|
|
|
|
|
|
|
const float minx = -_width;
|
|
|
|
const float maxx = _width;
|
|
|
|
const float miny = 0.0f;
|
|
|
|
const float maxy = _height * 2.0f;
|
|
|
|
|
|
|
|
const float texelHalfW = _texelHalf / _textureWidth;
|
|
|
|
const float texelHalfH = _texelHalf / _textureHeight;
|
|
|
|
const float minu = -1.0f + texelHalfW;
|
|
|
|
const float maxu = 1.0f + texelHalfW;
|
|
|
|
|
|
|
|
const float zz = 0.0f;
|
|
|
|
|
|
|
|
float minv = texelHalfH;
|
|
|
|
float maxv = 2.0f + texelHalfH;
|
|
|
|
|
|
|
|
if (_originBottomLeft)
|
|
|
|
{
|
|
|
|
float temp = minv;
|
|
|
|
minv = maxv;
|
|
|
|
maxv = temp;
|
|
|
|
|
|
|
|
minv -= 1.0f;
|
|
|
|
maxv -= 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
vertex[0].m_x = minx;
|
|
|
|
vertex[0].m_y = miny;
|
|
|
|
vertex[0].m_z = zz;
|
|
|
|
vertex[0].m_u = minu;
|
|
|
|
vertex[0].m_v = minv;
|
|
|
|
|
|
|
|
vertex[1].m_x = maxx;
|
|
|
|
vertex[1].m_y = miny;
|
|
|
|
vertex[1].m_z = zz;
|
|
|
|
vertex[1].m_u = maxu;
|
|
|
|
vertex[1].m_v = minv;
|
|
|
|
|
|
|
|
vertex[2].m_x = maxx;
|
|
|
|
vertex[2].m_y = maxy;
|
|
|
|
vertex[2].m_z = zz;
|
|
|
|
vertex[2].m_u = maxu;
|
|
|
|
vertex[2].m_v = maxv;
|
|
|
|
|
|
|
|
bgfx::setVertexBuffer(0, &vb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void vec2Set(float* _v, float _x, float _y)
|
|
|
|
{
|
|
|
|
_v[0] = _x;
|
|
|
|
_v[1] = _y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mat4Set(float * _m, const float * _src)
|
|
|
|
{
|
|
|
|
const uint32_t MAT4_FLOATS = 16;
|
|
|
|
for (uint32_t ii = 0; ii < MAT4_FLOATS; ++ii) {
|
|
|
|
_m[ii] = _src[ii];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ExampleDenoise : public entry::AppI
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ExampleDenoise(const char* _name, const char* _description)
|
|
|
|
: entry::AppI(_name, _description)
|
|
|
|
, m_currFrame(UINT32_MAX)
|
|
|
|
, m_texelHalf(0.0f)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
|
|
|
|
{
|
|
|
|
Args args(_argc, _argv);
|
|
|
|
|
2021-01-02 22:12:45 +03:00
|
|
|
m_width = _width;
|
2021-01-02 21:42:02 +03:00
|
|
|
m_height = _height;
|
2021-01-02 22:12:45 +03:00
|
|
|
m_debug = BGFX_DEBUG_NONE;
|
|
|
|
m_reset = BGFX_RESET_VSYNC;
|
2021-01-02 21:42:02 +03:00
|
|
|
|
|
|
|
bgfx::Init init;
|
2022-08-26 06:07:03 +03:00
|
|
|
init.type = args.m_type;
|
|
|
|
init.vendorId = args.m_pciId;
|
|
|
|
init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
|
|
|
|
init.platformData.ndt = entry::getNativeDisplayHandle();
|
2021-01-02 22:12:45 +03:00
|
|
|
init.resolution.width = m_width;
|
2021-01-02 21:42:02 +03:00
|
|
|
init.resolution.height = m_height;
|
2021-01-02 22:12:45 +03:00
|
|
|
init.resolution.reset = m_reset;
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::init(init);
|
|
|
|
|
|
|
|
// Enable debug text.
|
|
|
|
bgfx::setDebug(m_debug);
|
|
|
|
|
|
|
|
// Create uniforms
|
|
|
|
m_uniforms.init();
|
|
|
|
|
|
|
|
// Create texture sampler uniforms (used when we bind textures)
|
2021-01-02 22:12:45 +03:00
|
|
|
s_albedo = bgfx::createUniform("s_albedo", bgfx::UniformType::Sampler); // Model's source albedo
|
|
|
|
s_color = bgfx::createUniform("s_color", bgfx::UniformType::Sampler); // Color (albedo) gbuffer, default color input
|
|
|
|
s_normal = bgfx::createUniform("s_normal", bgfx::UniformType::Sampler); // Normal gbuffer, Model's source normal
|
|
|
|
s_velocity = bgfx::createUniform("s_velocity", bgfx::UniformType::Sampler); // Velocity gbuffer
|
|
|
|
s_depth = bgfx::createUniform("s_depth", bgfx::UniformType::Sampler); // Depth gbuffer
|
|
|
|
s_previousColor = bgfx::createUniform("s_previousColor", bgfx::UniformType::Sampler); // Previous frame's result
|
2021-01-02 21:42:02 +03:00
|
|
|
s_previousNormal = bgfx::createUniform("s_previousNormal", bgfx::UniformType::Sampler); // Previous frame's gbuffer normal
|
|
|
|
|
|
|
|
// Create program from shaders.
|
2021-01-02 22:12:45 +03:00
|
|
|
m_gbufferProgram = loadProgram("vs_denoise_gbuffer", "fs_denoise_gbuffer"); // Fill gbuffer
|
|
|
|
m_combineProgram = loadProgram("vs_denoise_screenquad", "fs_denoise_deferred_combine"); // Compute lighting from gbuffer
|
|
|
|
m_copyProgram = loadProgram("vs_denoise_screenquad", "fs_denoise_copy");
|
|
|
|
m_denoiseTemporalProgram = loadProgram("vs_denoise_screenquad", "fs_denoise_temporal");
|
2021-01-02 21:42:02 +03:00
|
|
|
m_denoiseSpatialProgram3x3 = loadProgram("vs_denoise_screenquad", "fs_denoise_spatial_3x3");
|
|
|
|
m_denoiseSpatialProgram5x5 = loadProgram("vs_denoise_screenquad", "fs_denoise_spatial_5x5");
|
2021-01-02 22:12:45 +03:00
|
|
|
m_denoiseApplyLighting = loadProgram("vs_denoise_screenquad", "fs_denoise_apply_lighting");
|
|
|
|
m_txaaProgram = loadProgram("vs_denoise_screenquad", "fs_denoise_txaa");
|
2021-01-02 21:42:02 +03:00
|
|
|
|
|
|
|
// Load some meshes
|
|
|
|
for (uint32_t ii = 0; ii < BX_COUNTOF(s_meshPaths); ++ii)
|
|
|
|
{
|
|
|
|
m_meshes[ii] = meshLoad(s_meshPaths[ii]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Randomly create some models
|
|
|
|
bx::RngMwc mwc;
|
|
|
|
for (uint32_t ii = 0; ii < BX_COUNTOF(m_models); ++ii)
|
|
|
|
{
|
|
|
|
Model& model = m_models[ii];
|
|
|
|
|
|
|
|
model.mesh = mwc.gen() % BX_COUNTOF(s_meshPaths);
|
2021-01-02 22:12:45 +03:00
|
|
|
model.position[0] = ( ( (mwc.gen() % 256) ) - 128.0f) / 20.0f;
|
2021-01-02 21:42:02 +03:00
|
|
|
model.position[1] = 0;
|
2021-01-02 22:12:45 +03:00
|
|
|
model.position[2] = ( ( (mwc.gen() % 256) ) - 128.0f) / 20.0f;
|
2021-01-02 21:42:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load ground, just use the cube
|
|
|
|
m_ground = meshLoad("meshes/cube.bin");
|
|
|
|
|
|
|
|
m_groundTexture = loadTexture("textures/fieldstone-rgba.dds");
|
|
|
|
m_normalTexture = loadTexture("textures/fieldstone-n.dds");
|
|
|
|
|
|
|
|
m_recreateFrameBuffers = false;
|
|
|
|
createFramebuffers();
|
2021-01-02 21:46:43 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
// Vertex decl
|
|
|
|
PosTexCoord0Vertex::init();
|
|
|
|
|
|
|
|
// Init camera
|
|
|
|
cameraCreate();
|
|
|
|
cameraSetPosition({ 0.0f, 1.5f, 0.0f });
|
|
|
|
cameraSetVerticalAngle(-0.3f);
|
|
|
|
m_fovY = 60.0f;
|
|
|
|
|
|
|
|
// Init "prev" matrices, will be same for first frame
|
|
|
|
cameraGetViewMtx(m_view);
|
|
|
|
bx::mtxProj(m_proj, m_fovY, float(m_size[0]) / float(m_size[1]), 0.01f, 100.0f, bgfx::getCaps()->homogeneousDepth);
|
|
|
|
mat4Set(m_worldToViewPrev, m_view);
|
|
|
|
mat4Set(m_viewToProjPrev, m_proj);
|
|
|
|
|
|
|
|
// Track whether previous results are valid
|
|
|
|
m_havePrevious = false;
|
|
|
|
|
|
|
|
// Get renderer capabilities info.
|
|
|
|
const bgfx::RendererType::Enum renderer = bgfx::getRendererType();
|
|
|
|
m_texelHalf = bgfx::RendererType::Direct3D9 == renderer ? 0.5f : 0.0f;
|
|
|
|
|
|
|
|
imguiCreate();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t shutdown() override
|
|
|
|
{
|
|
|
|
for (uint32_t ii = 0; ii < BX_COUNTOF(s_meshPaths); ++ii)
|
|
|
|
{
|
|
|
|
meshUnload(m_meshes[ii]);
|
|
|
|
}
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
meshUnload(m_ground);
|
|
|
|
|
|
|
|
bgfx::destroy(m_normalTexture);
|
|
|
|
bgfx::destroy(m_groundTexture);
|
|
|
|
|
|
|
|
bgfx::destroy(m_gbufferProgram);
|
|
|
|
bgfx::destroy(m_combineProgram);
|
|
|
|
bgfx::destroy(m_copyProgram);
|
|
|
|
bgfx::destroy(m_denoiseTemporalProgram);
|
|
|
|
bgfx::destroy(m_denoiseSpatialProgram3x3);
|
|
|
|
bgfx::destroy(m_denoiseSpatialProgram5x5);
|
|
|
|
bgfx::destroy(m_denoiseApplyLighting);
|
|
|
|
bgfx::destroy(m_txaaProgram);
|
|
|
|
|
|
|
|
m_uniforms.destroy();
|
|
|
|
|
|
|
|
bgfx::destroy(s_albedo);
|
|
|
|
bgfx::destroy(s_color);
|
|
|
|
bgfx::destroy(s_normal);
|
|
|
|
bgfx::destroy(s_velocity);
|
|
|
|
bgfx::destroy(s_depth);
|
|
|
|
bgfx::destroy(s_previousColor);
|
|
|
|
bgfx::destroy(s_previousNormal);
|
|
|
|
|
|
|
|
destroyFramebuffers();
|
|
|
|
|
|
|
|
cameraDestroy();
|
|
|
|
|
|
|
|
imguiDestroy();
|
|
|
|
|
|
|
|
bgfx::shutdown();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool update() override
|
|
|
|
{
|
2021-01-02 22:12:45 +03:00
|
|
|
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
|
2021-01-02 21:42:02 +03:00
|
|
|
{
|
|
|
|
// skip processing when minimized, otherwise crashing
|
2021-01-02 22:12:45 +03:00
|
|
|
if (0 == m_width
|
|
|
|
|| 0 == m_height)
|
2021-01-02 21:42:02 +03:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update frame timer
|
|
|
|
int64_t now = bx::getHPCounter();
|
|
|
|
static int64_t last = now;
|
|
|
|
const int64_t frameTime = now - last;
|
|
|
|
last = now;
|
2021-01-02 22:12:45 +03:00
|
|
|
const double freq = double(bx::getHPFrequency() );
|
2021-01-02 21:42:02 +03:00
|
|
|
const float deltaTime = float(frameTime / freq);
|
|
|
|
const bgfx::Caps* caps = bgfx::getCaps();
|
|
|
|
|
|
|
|
if (m_size[0] != (int32_t)m_width
|
|
|
|
|| m_size[1] != (int32_t)m_height
|
|
|
|
|| m_recreateFrameBuffers)
|
|
|
|
{
|
|
|
|
destroyFramebuffers();
|
|
|
|
createFramebuffers();
|
|
|
|
m_recreateFrameBuffers = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update camera
|
2021-02-06 04:24:56 +03:00
|
|
|
cameraUpdate(deltaTime*0.15f, m_mouseState, ImGui::MouseOverArea() );
|
2021-01-02 21:42:02 +03:00
|
|
|
|
|
|
|
// Set up matrices for gbuffer
|
|
|
|
cameraGetViewMtx(m_view);
|
|
|
|
|
|
|
|
updateUniforms();
|
|
|
|
|
|
|
|
bx::mtxProj(m_proj, m_fovY, float(m_size[0]) / float(m_size[1]), 0.01f, 100.0f, caps->homogeneousDepth);
|
|
|
|
bx::mtxProj(m_proj2, m_fovY, float(m_size[0]) / float(m_size[1]), 0.01f, 100.0f, false);
|
|
|
|
|
|
|
|
if (m_enableTxaa)
|
|
|
|
{
|
|
|
|
m_proj[2*4+0] += m_jitter[0] * (2.0f / m_size[0]);
|
|
|
|
m_proj[2*4+1] -= m_jitter[1] * (2.0f / m_size[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
bgfx::ViewId view = 0;
|
|
|
|
|
|
|
|
// Draw everything into gbuffer
|
|
|
|
{
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewName(view, "GBuffer");
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::setViewClear(view
|
|
|
|
, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
|
|
|
|
, 0
|
|
|
|
, 1.0f
|
|
|
|
, 0
|
2021-01-02 22:12:45 +03:00
|
|
|
);
|
2021-01-02 21:42:02 +03:00
|
|
|
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewRect(view, 0, 0, uint16_t(m_size[0]), uint16_t(m_size[1]) );
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::setViewTransform(view, m_view, m_proj);
|
|
|
|
// Make sure when we draw it goes into gbuffer and not backbuffer
|
|
|
|
bgfx::setViewFrameBuffer(view, m_gbuffer);
|
|
|
|
|
|
|
|
bgfx::setState(0
|
|
|
|
| BGFX_STATE_WRITE_RGB
|
|
|
|
| BGFX_STATE_WRITE_A
|
|
|
|
| BGFX_STATE_WRITE_Z
|
|
|
|
| BGFX_STATE_DEPTH_TEST_LESS
|
|
|
|
);
|
|
|
|
|
|
|
|
drawAllModels(view, m_gbufferProgram, m_uniforms);
|
|
|
|
++view;
|
|
|
|
}
|
|
|
|
|
|
|
|
float orthoProj[16];
|
|
|
|
bx::mtxOrtho(orthoProj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, caps->homogeneousDepth);
|
|
|
|
|
|
|
|
// Shade gbuffer
|
|
|
|
{
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewName(view, "Combine");
|
2021-01-02 21:42:02 +03:00
|
|
|
|
|
|
|
// for some reason, previous draws texture lingering in transform stack
|
|
|
|
// need to clear out, otherwise this copy is garbled. this used to work
|
|
|
|
// and broke after updating, but i last updated like 2 years ago.
|
|
|
|
float identity[16];
|
|
|
|
bx::mtxIdentity(identity);
|
|
|
|
bgfx::setTransform(identity);
|
|
|
|
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::setViewTransform(view, NULL, orthoProj);
|
|
|
|
bgfx::setViewFrameBuffer(view, m_currentColor.m_buffer);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::setState(0
|
|
|
|
| BGFX_STATE_WRITE_RGB
|
|
|
|
| BGFX_STATE_WRITE_A
|
|
|
|
| BGFX_STATE_DEPTH_TEST_ALWAYS
|
|
|
|
);
|
|
|
|
bgfx::setTexture(0, s_color, m_gbufferTex[GBUFFER_RT_COLOR]);
|
|
|
|
bgfx::setTexture(1, s_normal, m_gbufferTex[GBUFFER_RT_NORMAL]);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
m_uniforms.submit();
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
screenSpaceQuad(float(m_width), float(m_height), m_texelHalf, caps->originBottomLeft);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::submit(view, m_combineProgram);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
++view;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update last texture written, to chain passes together
|
|
|
|
bgfx::TextureHandle lastTex = m_currentColor.m_texture;
|
|
|
|
|
|
|
|
// denoise temporal pass
|
|
|
|
if (m_useTemporalPass && m_havePrevious)
|
|
|
|
{
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewName(view, "Denoise Temporal");
|
2021-01-02 21:42:02 +03:00
|
|
|
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::setViewTransform(view, NULL, orthoProj);
|
|
|
|
bgfx::setViewFrameBuffer(view, m_temporaryColor.m_buffer);
|
|
|
|
bgfx::setState(BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_DEPTH_TEST_ALWAYS);
|
|
|
|
|
|
|
|
// want color, prevColor
|
|
|
|
// normal, prevNormal
|
|
|
|
// depth, prevDepth to reject previous samples from accumulating - skipping depth for now
|
|
|
|
|
|
|
|
bgfx::setTexture(0, s_color, lastTex);
|
|
|
|
bgfx::setTexture(1, s_normal, m_gbufferTex[GBUFFER_RT_NORMAL]);
|
|
|
|
bgfx::setTexture(2, s_velocity, m_gbufferTex[GBUFFER_RT_VELOCITY]);
|
|
|
|
bgfx::setTexture(3, s_previousColor, m_previousDenoise.m_texture);
|
|
|
|
bgfx::setTexture(4, s_previousNormal, m_previousNormal.m_texture);
|
|
|
|
|
|
|
|
m_uniforms.submit();
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
screenSpaceQuad(float(m_width), float(m_height), m_texelHalf, caps->originBottomLeft);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::submit(view, m_denoiseTemporalProgram);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
++view;
|
|
|
|
|
|
|
|
lastTex = m_temporaryColor.m_texture;
|
|
|
|
}
|
2021-01-02 21:46:43 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
// denoise spatial passes
|
|
|
|
if (0 < m_denoisePasses)
|
|
|
|
{
|
|
|
|
// variable number of passes for denoise, alternate between two textures/buffers
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::FrameBufferHandle destBuffer[] =
|
|
|
|
{
|
2021-01-02 21:42:02 +03:00
|
|
|
m_previousDenoise.m_buffer,
|
|
|
|
m_currentColor.m_buffer,
|
|
|
|
m_temporaryColor.m_buffer,
|
|
|
|
m_currentColor.m_buffer,
|
|
|
|
m_temporaryColor.m_buffer,
|
2021-01-02 22:12:45 +03:00
|
|
|
m_currentColor.m_buffer,
|
2021-01-02 21:42:02 +03:00
|
|
|
};
|
2021-01-02 22:12:45 +03:00
|
|
|
BX_STATIC_ASSERT(BX_COUNTOF(destBuffer) == DENOISE_MAX_PASSES);
|
2021-01-02 21:46:43 +03:00
|
|
|
|
2021-01-02 22:12:45 +03:00
|
|
|
const uint32_t denoisePasses = bx::min(DENOISE_MAX_PASSES, m_denoisePasses);
|
|
|
|
|
|
|
|
for (uint32_t ii = 0; ii < denoisePasses; ++ii)
|
2021-01-02 21:42:02 +03:00
|
|
|
{
|
2021-01-02 22:12:45 +03:00
|
|
|
char name[64];
|
|
|
|
bx::snprintf(name, BX_COUNTOF(name), "Denoise %d/%d", ii, denoisePasses-1);
|
|
|
|
bgfx::setViewName(view, name);
|
2021-01-02 21:42:02 +03:00
|
|
|
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::setViewTransform(view, NULL, orthoProj);
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewFrameBuffer(view, destBuffer[ii]);
|
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::setState(BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_DEPTH_TEST_ALWAYS);
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setTexture(0, s_color, lastTex);
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::setTexture(1, s_normal, m_gbufferTex[GBUFFER_RT_NORMAL]);
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setTexture(2, s_depth, m_gbufferTex[GBUFFER_RT_DEPTH]);
|
2021-01-02 21:42:02 +03:00
|
|
|
|
|
|
|
// need to update some denoise uniforms per draw
|
2021-01-02 22:12:45 +03:00
|
|
|
const float denoiseStepScale = bx::pow(2.0f, float(ii) );
|
2021-01-02 21:42:02 +03:00
|
|
|
m_uniforms.m_denoiseStep = denoiseStepScale;
|
|
|
|
m_uniforms.submit();
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
screenSpaceQuad(float(m_width), float(m_height), m_texelHalf, caps->originBottomLeft);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
|
|
|
const bgfx::ProgramHandle spatialProgram = (0 == m_spatialSampleType)
|
2021-01-02 21:42:02 +03:00
|
|
|
? m_denoiseSpatialProgram3x3
|
2021-01-02 22:12:45 +03:00
|
|
|
: m_denoiseSpatialProgram5x5
|
|
|
|
;
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::submit(view, spatialProgram);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
++view;
|
|
|
|
|
2021-01-02 22:12:45 +03:00
|
|
|
if (m_previousDenoise.m_buffer.idx == destBuffer[ii].idx)
|
2021-01-02 21:42:02 +03:00
|
|
|
{
|
|
|
|
lastTex = m_previousDenoise.m_texture;
|
|
|
|
}
|
2021-01-02 22:12:45 +03:00
|
|
|
else if (m_temporaryColor.m_buffer.idx == destBuffer[ii].idx)
|
2021-01-02 21:42:02 +03:00
|
|
|
{
|
|
|
|
lastTex = m_temporaryColor.m_texture;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lastTex = m_currentColor.m_texture;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// need color result for temporal denoise if not supplied by spatial pass
|
|
|
|
// (per SVGF paper, reuse previous frame's first spatial pass output as previous color
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewName(view, "Copy Color for Temporal Denoise");
|
|
|
|
bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::setViewTransform(view, NULL, orthoProj);
|
|
|
|
bgfx::setViewFrameBuffer(view, m_previousDenoise.m_buffer);
|
|
|
|
bgfx::setState(BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_DEPTH_TEST_ALWAYS);
|
|
|
|
bgfx::setTexture(0, s_color, lastTex);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
screenSpaceQuad(float(m_width), float(m_height), m_texelHalf, caps->originBottomLeft);
|
|
|
|
bgfx::submit(view, m_copyProgram);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
++view;
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply lighting
|
|
|
|
{
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewName(view, "Apply Lighting");
|
2021-01-02 21:42:02 +03:00
|
|
|
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::setViewTransform(view, NULL, orthoProj);
|
|
|
|
|
|
|
|
bgfx::FrameBufferHandle destBuffer = (lastTex.idx == m_currentColor.m_texture.idx)
|
|
|
|
? m_temporaryColor.m_buffer
|
2021-01-02 22:12:45 +03:00
|
|
|
: m_currentColor.m_buffer
|
|
|
|
;
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::setViewFrameBuffer(view, destBuffer);
|
|
|
|
bgfx::setState(0
|
|
|
|
| BGFX_STATE_WRITE_RGB
|
|
|
|
| BGFX_STATE_WRITE_A
|
|
|
|
| BGFX_STATE_DEPTH_TEST_ALWAYS
|
|
|
|
);
|
|
|
|
bgfx::setTexture(0, s_color, lastTex);
|
|
|
|
bgfx::setTexture(1, s_albedo, m_gbufferTex[GBUFFER_RT_COLOR]);
|
|
|
|
m_uniforms.submit();
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
screenSpaceQuad(float(m_width), float(m_height), m_texelHalf, caps->originBottomLeft);
|
|
|
|
bgfx::submit(view, m_denoiseApplyLighting);
|
|
|
|
++view;
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
lastTex = (m_temporaryColor.m_buffer.idx == destBuffer.idx)
|
|
|
|
? m_temporaryColor.m_texture
|
2021-01-02 22:12:45 +03:00
|
|
|
: m_currentColor.m_texture
|
|
|
|
;
|
2021-01-02 21:42:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_enableTxaa)
|
|
|
|
{
|
|
|
|
// Draw txaa to txaa buffer
|
|
|
|
{
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewName(view, "Temporal AA");
|
2021-01-02 21:42:02 +03:00
|
|
|
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::setViewTransform(view, NULL, orthoProj);
|
|
|
|
bgfx::setViewFrameBuffer(view, m_txaaColor.m_buffer);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::setState(0
|
|
|
|
| BGFX_STATE_WRITE_RGB
|
|
|
|
| BGFX_STATE_WRITE_A
|
|
|
|
| BGFX_STATE_DEPTH_TEST_ALWAYS
|
|
|
|
);
|
|
|
|
bgfx::setTexture(0, s_color, lastTex);
|
|
|
|
bgfx::setTexture(1, s_previousColor, m_previousColor.m_texture);
|
|
|
|
bgfx::setTexture(2, s_velocity, m_gbufferTex[GBUFFER_RT_VELOCITY]);
|
|
|
|
bgfx::setTexture(3, s_depth, m_gbufferTex[GBUFFER_RT_DEPTH]);
|
|
|
|
m_uniforms.submit();
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
screenSpaceQuad(float(m_width), float(m_height), m_texelHalf, caps->originBottomLeft);
|
|
|
|
bgfx::submit(view, m_txaaProgram);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
++view;
|
|
|
|
}
|
2021-01-02 21:46:43 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
// Copy txaa result to previous
|
|
|
|
{
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewName(view, "Copy to Previous");
|
2021-01-02 21:42:02 +03:00
|
|
|
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::setViewTransform(view, NULL, orthoProj);
|
|
|
|
bgfx::setViewFrameBuffer(view, m_previousColor.m_buffer);
|
|
|
|
bgfx::setState(0
|
|
|
|
| BGFX_STATE_WRITE_RGB
|
|
|
|
| BGFX_STATE_WRITE_A
|
|
|
|
| BGFX_STATE_DEPTH_TEST_ALWAYS
|
|
|
|
);
|
|
|
|
bgfx::setTexture(0, s_color, m_txaaColor.m_texture);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
screenSpaceQuad(float(m_width), float(m_height), m_texelHalf, caps->originBottomLeft);
|
|
|
|
bgfx::submit(view, m_copyProgram);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
++view;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy txaa result to swap chain
|
|
|
|
{
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewName(view, "Display");
|
2021-01-02 21:42:02 +03:00
|
|
|
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::setViewTransform(view, NULL, orthoProj);
|
|
|
|
bgfx::setViewFrameBuffer(view, BGFX_INVALID_HANDLE);
|
|
|
|
bgfx::setState(0
|
|
|
|
| BGFX_STATE_WRITE_RGB
|
|
|
|
| BGFX_STATE_WRITE_A
|
|
|
|
| BGFX_STATE_DEPTH_TEST_ALWAYS
|
|
|
|
);
|
|
|
|
bgfx::setTexture(0, s_color, m_txaaColor.m_texture);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
screenSpaceQuad(float(m_width), float(m_height), m_texelHalf, caps->originBottomLeft);
|
|
|
|
bgfx::submit(view, m_copyProgram);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
++view;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Copy color result to swap chain
|
|
|
|
{
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewName(view, "Display");
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::setViewClear(view
|
|
|
|
, BGFX_CLEAR_NONE
|
|
|
|
, 0
|
|
|
|
, 1.0f
|
|
|
|
, 0
|
2021-01-02 22:12:45 +03:00
|
|
|
);
|
2021-01-02 21:42:02 +03:00
|
|
|
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::setViewTransform(view, NULL, orthoProj);
|
|
|
|
bgfx::setViewFrameBuffer(view, BGFX_INVALID_HANDLE);
|
|
|
|
bgfx::setState(0
|
|
|
|
| BGFX_STATE_WRITE_RGB
|
|
|
|
| BGFX_STATE_WRITE_A
|
|
|
|
);
|
|
|
|
bgfx::setTexture(0, s_color, lastTex);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
screenSpaceQuad(float(m_width), float(m_height), m_texelHalf, caps->originBottomLeft);
|
|
|
|
bgfx::submit(view, m_copyProgram);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
++view;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy the normal buffer for next time
|
|
|
|
{
|
2021-01-02 22:12:45 +03:00
|
|
|
bgfx::setViewName(view, "Copy Normals");
|
|
|
|
bgfx::setViewRect(view, 0, 0, uint16_t(m_width), uint16_t(m_height) );
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::setViewTransform(view, NULL, orthoProj);
|
|
|
|
bgfx::setViewFrameBuffer(view, m_previousNormal.m_buffer);
|
|
|
|
bgfx::setState(BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_DEPTH_TEST_ALWAYS);
|
|
|
|
bgfx::setTexture(0, s_color, m_gbufferTex[GBUFFER_RT_NORMAL]);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
screenSpaceQuad(float(m_width), float(m_height), m_texelHalf, caps->originBottomLeft);
|
|
|
|
bgfx::submit(view, m_copyProgram);
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
++view;
|
|
|
|
|
|
|
|
// update previous status
|
|
|
|
m_havePrevious = true;
|
2021-01-02 21:46:43 +03:00
|
|
|
}
|
2021-01-02 21:42:02 +03:00
|
|
|
|
|
|
|
// Copy matrices for next time
|
|
|
|
mat4Set(m_worldToViewPrev, m_view);
|
|
|
|
mat4Set(m_viewToProjPrev, m_proj);
|
|
|
|
|
|
|
|
// Draw UI
|
|
|
|
imguiBeginFrame(m_mouseState.m_mx
|
|
|
|
, m_mouseState.m_my
|
|
|
|
, (m_mouseState.m_buttons[entry::MouseButton::Left] ? IMGUI_MBUT_LEFT : 0)
|
|
|
|
| (m_mouseState.m_buttons[entry::MouseButton::Right] ? IMGUI_MBUT_RIGHT : 0)
|
|
|
|
| (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
|
|
|
|
, m_mouseState.m_mz
|
|
|
|
, uint16_t(m_width)
|
|
|
|
, uint16_t(m_height)
|
|
|
|
);
|
|
|
|
|
|
|
|
showExampleDialog(this);
|
|
|
|
|
|
|
|
ImGui::SetNextWindowPos(
|
|
|
|
ImVec2(m_width - m_width / 4.0f - 10.0f, 10.0f)
|
|
|
|
, ImGuiCond_FirstUseEver
|
|
|
|
);
|
|
|
|
ImGui::SetNextWindowSize(
|
|
|
|
ImVec2(m_width / 4.0f, m_height / 1.24f)
|
|
|
|
, ImGuiCond_FirstUseEver
|
|
|
|
);
|
|
|
|
ImGui::Begin("Settings"
|
|
|
|
, NULL
|
|
|
|
, 0
|
|
|
|
);
|
|
|
|
|
|
|
|
ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
|
|
|
|
|
|
|
|
{
|
|
|
|
ImGui::TextWrapped(
|
|
|
|
"In this demo, noise is added to results of deferred lighting. Then denoise is applied "
|
|
|
|
"before multiplying the lit result with gbuffer albedo. Optionally, temporal antialiasing "
|
|
|
|
"can be applied after that. (off by default, implementation blurry)");
|
|
|
|
ImGui::Separator();
|
|
|
|
|
|
|
|
ImGui::Text("noise controls:");
|
|
|
|
ImGui::Combo("pattern", &m_noiseType, "none\0dither\0random\0\0");
|
2021-01-02 22:12:45 +03:00
|
|
|
if (ImGui::IsItemHovered() )
|
2021-01-02 21:42:02 +03:00
|
|
|
{
|
|
|
|
ImGui::BeginTooltip();
|
|
|
|
ImGui::Text("none");
|
|
|
|
ImGui::BulletText("compare denoised results to this");
|
|
|
|
ImGui::BulletText("brighter than noisy images, not losing any pixel's energy");
|
|
|
|
ImGui::Text("dither");
|
|
|
|
ImGui::BulletText("reject 3 out of 4 pixels in 2x2 pattern");
|
|
|
|
ImGui::BulletText("could represent lower resolution signal");
|
|
|
|
ImGui::Text("random");
|
|
|
|
ImGui::BulletText("reject about half pixels, using common shader random");
|
|
|
|
ImGui::BulletText("could represent monte carlo something or other");
|
|
|
|
ImGui::EndTooltip();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::Checkbox("dynamic noise", &m_dynamicNoise);
|
2021-01-02 22:12:45 +03:00
|
|
|
if (ImGui::IsItemHovered() )
|
|
|
|
{
|
2021-01-02 21:42:02 +03:00
|
|
|
ImGui::SetTooltip("update noise pattern each frame");
|
2021-01-02 22:12:45 +03:00
|
|
|
}
|
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
ImGui::Separator();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ImGui::Text("temporal denoise pass controls:");
|
|
|
|
ImGui::Checkbox("use temporal pass", &m_useTemporalPass);
|
|
|
|
ImGui::Separator();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ImGui::Text("spatial denoise pass controls:");
|
|
|
|
ImGui::SliderInt("spatial passes", &m_denoisePasses, 0, DENOISE_MAX_PASSES);
|
2021-01-02 22:12:45 +03:00
|
|
|
if (ImGui::IsItemHovered() )
|
|
|
|
{
|
2021-01-02 21:42:02 +03:00
|
|
|
ImGui::SetTooltip("set passes to 0 to turn off spatial denoise");
|
2021-01-02 22:12:45 +03:00
|
|
|
}
|
2021-01-02 21:42:02 +03:00
|
|
|
|
|
|
|
ImGui::Combo("spatial sample extent", &m_spatialSampleType, "three\0five\0\0");
|
2021-01-02 22:12:45 +03:00
|
|
|
if (ImGui::IsItemHovered() )
|
|
|
|
{
|
Fix various typos (#2719)
Found via `codespell -q 3 -S ./3rdparty,*.ttf -L attribut,ba,clude,conly,indext,inout,lod,nclude,retur,utput`
2022-02-05 22:34:15 +03:00
|
|
|
ImGui::SetTooltip("select 3x3 or 5x5 filter kernel");
|
2021-01-02 22:12:45 +03:00
|
|
|
}
|
2021-01-02 21:42:02 +03:00
|
|
|
|
|
|
|
ImGui::SliderFloat("sigma z", &m_sigmaDepth, 0.0f, 0.1f, "%.5f");
|
2021-01-02 22:12:45 +03:00
|
|
|
if (ImGui::IsItemHovered() )
|
|
|
|
{
|
2021-01-02 21:42:02 +03:00
|
|
|
ImGui::SetTooltip("lower sigma z, pickier blending across depth edges");
|
2021-01-02 22:12:45 +03:00
|
|
|
}
|
2021-01-02 21:42:02 +03:00
|
|
|
|
|
|
|
ImGui::SliderFloat("sigma n", &m_sigmaNormal, 1.0f, 256.0f);
|
2021-01-02 22:12:45 +03:00
|
|
|
if (ImGui::IsItemHovered() )
|
|
|
|
{
|
2021-01-02 21:42:02 +03:00
|
|
|
ImGui::SetTooltip("higher sigma n, pickier blending across normal edges");
|
2021-01-02 22:12:45 +03:00
|
|
|
}
|
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
ImGui::Separator();
|
|
|
|
}
|
|
|
|
|
2021-01-02 22:12:45 +03:00
|
|
|
if (ImGui::CollapsingHeader("TXAA options") )
|
2021-01-02 21:42:02 +03:00
|
|
|
{
|
|
|
|
ImGui::Checkbox("use TXAA", &m_enableTxaa);
|
|
|
|
ImGui::Checkbox("apply extra blur to current color", &m_applyMitchellFilter);
|
2021-01-02 22:12:45 +03:00
|
|
|
if (ImGui::IsItemHovered() )
|
|
|
|
{
|
2021-01-02 21:42:02 +03:00
|
|
|
ImGui::SetTooltip("reduces flicker/crawl on thin features, maybe too much!");
|
2021-01-02 22:12:45 +03:00
|
|
|
}
|
2021-01-02 21:42:02 +03:00
|
|
|
|
|
|
|
ImGui::SliderFloat("feedback min", &m_feedbackMin, 0.0f, 1.0f);
|
2021-01-02 22:12:45 +03:00
|
|
|
if (ImGui::IsItemHovered() )
|
|
|
|
{
|
2021-01-02 21:42:02 +03:00
|
|
|
ImGui::SetTooltip("minimum amount of previous frame to blend in");
|
2021-01-02 22:12:45 +03:00
|
|
|
}
|
2021-01-02 21:42:02 +03:00
|
|
|
|
|
|
|
ImGui::SliderFloat("feedback max", &m_feedbackMax, 0.0f, 1.0f);
|
2021-01-02 22:12:45 +03:00
|
|
|
if (ImGui::IsItemHovered() )
|
|
|
|
{
|
2021-01-02 21:42:02 +03:00
|
|
|
ImGui::SetTooltip("maximum amount of previous frame to blend in");
|
2021-01-02 22:12:45 +03:00
|
|
|
}
|
2021-01-02 21:42:02 +03:00
|
|
|
|
|
|
|
ImGui::Checkbox("debug TXAA with slow frame rate", &m_useTxaaSlow);
|
2021-01-02 22:12:45 +03:00
|
|
|
if (ImGui::IsItemHovered() )
|
2021-01-02 21:42:02 +03:00
|
|
|
{
|
|
|
|
ImGui::BeginTooltip();
|
|
|
|
ImGui::Text("sleep 100ms per frame to highlight temporal artifacts");
|
|
|
|
ImGui::Text("high framerate compensates for flickering, masking issues");
|
|
|
|
ImGui::EndTooltip();
|
|
|
|
}
|
2021-01-02 22:12:45 +03:00
|
|
|
|
2021-01-02 21:42:02 +03:00
|
|
|
ImGui::Separator();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::End();
|
|
|
|
|
|
|
|
imguiEndFrame();
|
|
|
|
|
|
|
|
// Advance to next frame. Rendering thread will be kicked to
|
|
|
|
// process submitted rendering primitives.
|
|
|
|
m_currFrame = bgfx::frame();
|
|
|
|
|
|
|
|
// add artificial wait to emphasize txaa behavior
|
|
|
|
if (m_useTxaaSlow)
|
|
|
|
{
|
|
|
|
bx::sleep(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawAllModels(bgfx::ViewId _pass, bgfx::ProgramHandle _program, const Uniforms & _uniforms)
|
|
|
|
{
|
|
|
|
for (uint32_t ii = 0; ii < BX_COUNTOF(m_models); ++ii)
|
|
|
|
{
|
|
|
|
const Model& model = m_models[ii];
|
|
|
|
|
|
|
|
// Set up transform matrix for each model
|
|
|
|
const float scale = s_meshScale[model.mesh];
|
|
|
|
float mtx[16];
|
|
|
|
bx::mtxSRT(mtx
|
|
|
|
, scale
|
|
|
|
, scale
|
|
|
|
, scale
|
|
|
|
, 0.0f
|
|
|
|
, 0.0f
|
|
|
|
, 0.0f
|
|
|
|
, model.position[0]
|
|
|
|
, model.position[1]
|
|
|
|
, model.position[2]
|
|
|
|
);
|
|
|
|
|
|
|
|
// Submit mesh to gbuffer
|
|
|
|
bgfx::setTexture(0, s_albedo, m_groundTexture);
|
|
|
|
bgfx::setTexture(1, s_normal, m_normalTexture);
|
|
|
|
_uniforms.submit();
|
|
|
|
|
|
|
|
meshSubmit(m_meshes[model.mesh], _pass, _program, mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw ground
|
|
|
|
float mtxScale[16];
|
|
|
|
const float scale = 10.0f;
|
|
|
|
bx::mtxScale(mtxScale, scale, scale, scale);
|
|
|
|
|
|
|
|
float mtxTranslate[16];
|
|
|
|
bx::mtxTranslate(mtxTranslate
|
|
|
|
, 0.0f
|
|
|
|
, -10.0f
|
|
|
|
, 0.0f
|
|
|
|
);
|
|
|
|
|
|
|
|
float mtx[16];
|
|
|
|
bx::mtxMul(mtx, mtxScale, mtxTranslate);
|
|
|
|
bgfx::setTexture(0, s_albedo, m_groundTexture);
|
|
|
|
bgfx::setTexture(1, s_normal, m_normalTexture);
|
|
|
|
_uniforms.submit();
|
|
|
|
|
|
|
|
meshSubmit(m_ground, _pass, _program, mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void createFramebuffers()
|
|
|
|
{
|
|
|
|
m_size[0] = m_width;
|
|
|
|
m_size[1] = m_height;
|
|
|
|
|
|
|
|
const uint64_t bilinearFlags = 0
|
|
|
|
| BGFX_TEXTURE_RT
|
|
|
|
| BGFX_SAMPLER_U_CLAMP
|
|
|
|
| BGFX_SAMPLER_V_CLAMP
|
|
|
|
;
|
|
|
|
|
|
|
|
const uint64_t pointSampleFlags = bilinearFlags
|
|
|
|
| BGFX_SAMPLER_MIN_POINT
|
|
|
|
| BGFX_SAMPLER_MAG_POINT
|
|
|
|
| BGFX_SAMPLER_MIP_POINT
|
|
|
|
;
|
|
|
|
|
|
|
|
m_gbufferTex[GBUFFER_RT_COLOR] = bgfx::createTexture2D(uint16_t(m_size[0]), uint16_t(m_size[1]), false, 1, bgfx::TextureFormat::BGRA8, pointSampleFlags);
|
|
|
|
m_gbufferTex[GBUFFER_RT_NORMAL] = bgfx::createTexture2D(uint16_t(m_size[0]), uint16_t(m_size[1]), false, 1, bgfx::TextureFormat::BGRA8, pointSampleFlags);
|
|
|
|
m_gbufferTex[GBUFFER_RT_VELOCITY] = bgfx::createTexture2D(uint16_t(m_size[0]), uint16_t(m_size[1]), false, 1, bgfx::TextureFormat::RG16F, pointSampleFlags);
|
2021-01-02 21:52:19 +03:00
|
|
|
m_gbufferTex[GBUFFER_RT_DEPTH] = bgfx::createTexture2D(uint16_t(m_size[0]), uint16_t(m_size[1]), false, 1, bgfx::TextureFormat::D32F , pointSampleFlags);
|
2021-01-02 21:42:02 +03:00
|
|
|
m_gbuffer = bgfx::createFrameBuffer(BX_COUNTOF(m_gbufferTex), m_gbufferTex, true);
|
|
|
|
|
2021-04-19 07:14:57 +03:00
|
|
|
bgfx::TextureFormat::Enum format = bgfx::TextureFormat::RG11B10F;
|
|
|
|
if (!bgfx::isTextureValid(1, false, 1, format, bilinearFlags))
|
2021-04-19 07:16:56 +03:00
|
|
|
{
|
2021-04-19 07:14:57 +03:00
|
|
|
format = bgfx::TextureFormat::RGBA16F;
|
2021-04-19 07:16:56 +03:00
|
|
|
}
|
2021-04-19 07:14:57 +03:00
|
|
|
|
2021-04-19 07:16:56 +03:00
|
|
|
m_currentColor .init(m_size[0], m_size[1], format, bilinearFlags);
|
|
|
|
m_previousColor .init(m_size[0], m_size[1], format, bilinearFlags);
|
|
|
|
m_txaaColor .init(m_size[0], m_size[1], format, bilinearFlags);
|
|
|
|
m_temporaryColor .init(m_size[0], m_size[1], format, bilinearFlags);
|
|
|
|
m_previousNormal .init(m_size[0], m_size[1], format, pointSampleFlags);
|
2021-04-19 07:14:57 +03:00
|
|
|
m_previousDenoise.init(m_size[0], m_size[1], format, bilinearFlags);
|
2021-01-02 21:42:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// all buffers set to destroy their textures
|
|
|
|
void destroyFramebuffers()
|
|
|
|
{
|
|
|
|
bgfx::destroy(m_gbuffer);
|
|
|
|
|
|
|
|
m_currentColor.destroy();
|
|
|
|
m_previousColor.destroy();
|
|
|
|
m_txaaColor.destroy();
|
|
|
|
m_temporaryColor.destroy();
|
|
|
|
m_previousNormal.destroy();
|
|
|
|
m_previousDenoise.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateUniforms()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
uint32_t idx = m_currFrame % 8;
|
2021-01-02 22:12:45 +03:00
|
|
|
const float offsets[] =
|
|
|
|
{
|
|
|
|
(1.0f/ 2.0f), (1.0f/3.0f),
|
|
|
|
(1.0f/ 4.0f), (2.0f/3.0f),
|
|
|
|
(3.0f/ 4.0f), (1.0f/9.0f),
|
|
|
|
(1.0f/ 8.0f), (4.0f/9.0f),
|
|
|
|
(5.0f/ 8.0f), (7.0f/9.0f),
|
|
|
|
(3.0f/ 8.0f), (2.0f/9.0f),
|
|
|
|
(7.0f/ 8.0f), (5.0f/9.0f),
|
|
|
|
(1.0f/16.0f), (8.0f/9.0f),
|
2021-01-02 21:42:02 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Strange constant for jitterX is because 8 values from halton2
|
|
|
|
// sequence above do not average out to 0.5, 1/16 skews it to the
|
|
|
|
// left. Subtracting a smaller value to center the range of jitter
|
|
|
|
// around 0. Not necessary for jitterY. Not confident this makes sense...
|
2021-01-02 22:12:45 +03:00
|
|
|
const float jitterX = 1.0f * (offsets[2*idx] - (7.125f/16.0f) );
|
2021-01-02 21:42:02 +03:00
|
|
|
const float jitterY = 1.0f * (offsets[2*idx+1] - 0.5f);
|
|
|
|
|
|
|
|
vec2Set(m_uniforms.m_cameraJitterCurr, jitterX, jitterY);
|
|
|
|
vec2Set(m_uniforms.m_cameraJitterPrev, m_jitter[0], m_jitter[1]);
|
|
|
|
|
|
|
|
m_jitter[0] = jitterX;
|
|
|
|
m_jitter[1] = jitterY;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_uniforms.m_feedbackMin = m_feedbackMin;
|
|
|
|
m_uniforms.m_feedbackMax = m_feedbackMax;
|
|
|
|
m_uniforms.m_applyMitchellFilter = m_applyMitchellFilter ? 1.0f : 0.0f;
|
|
|
|
|
|
|
|
mat4Set(m_uniforms.m_worldToViewPrev, m_worldToViewPrev);
|
|
|
|
mat4Set(m_uniforms.m_viewToProjPrev, m_viewToProjPrev);
|
|
|
|
|
|
|
|
m_uniforms.m_frameOffsetForNoise = m_dynamicNoise
|
|
|
|
? float(m_currFrame % 8)
|
2021-01-02 22:12:45 +03:00
|
|
|
: 0.0f
|
|
|
|
;
|
2021-01-02 21:42:02 +03:00
|
|
|
m_uniforms.m_noiseType = float(m_noiseType);
|
|
|
|
m_uniforms.m_sigmaDepth = m_sigmaDepth;
|
|
|
|
m_uniforms.m_sigmaNormal = m_sigmaNormal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t m_width;
|
|
|
|
uint32_t m_height;
|
|
|
|
uint32_t m_debug;
|
|
|
|
uint32_t m_reset;
|
|
|
|
|
|
|
|
entry::MouseState m_mouseState;
|
|
|
|
|
|
|
|
// Resource handles
|
|
|
|
bgfx::ProgramHandle m_gbufferProgram;
|
|
|
|
bgfx::ProgramHandle m_combineProgram;
|
|
|
|
bgfx::ProgramHandle m_copyProgram;
|
|
|
|
bgfx::ProgramHandle m_denoiseTemporalProgram;
|
|
|
|
bgfx::ProgramHandle m_denoiseSpatialProgram3x3;
|
|
|
|
bgfx::ProgramHandle m_denoiseSpatialProgram5x5;
|
|
|
|
bgfx::ProgramHandle m_denoiseApplyLighting;
|
|
|
|
bgfx::ProgramHandle m_txaaProgram;
|
|
|
|
|
|
|
|
// Shader uniforms
|
|
|
|
Uniforms m_uniforms;
|
|
|
|
|
Fix various typos (#2719)
Found via `codespell -q 3 -S ./3rdparty,*.ttf -L attribut,ba,clude,conly,indext,inout,lod,nclude,retur,utput`
2022-02-05 22:34:15 +03:00
|
|
|
// Uniforms to identify texture samplers
|
2021-01-02 21:42:02 +03:00
|
|
|
bgfx::UniformHandle s_albedo;
|
|
|
|
bgfx::UniformHandle s_color;
|
|
|
|
bgfx::UniformHandle s_normal;
|
|
|
|
bgfx::UniformHandle s_velocity;
|
|
|
|
bgfx::UniformHandle s_depth;
|
|
|
|
bgfx::UniformHandle s_previousColor;
|
|
|
|
bgfx::UniformHandle s_previousNormal;
|
|
|
|
|
|
|
|
bgfx::FrameBufferHandle m_gbuffer;
|
|
|
|
bgfx::TextureHandle m_gbufferTex[GBUFFER_RENDER_TARGETS];
|
|
|
|
|
|
|
|
RenderTarget m_currentColor;
|
|
|
|
RenderTarget m_previousColor;
|
|
|
|
RenderTarget m_txaaColor;
|
|
|
|
RenderTarget m_temporaryColor; // need another buffer to ping-pong results
|
|
|
|
RenderTarget m_previousNormal;
|
|
|
|
RenderTarget m_previousDenoise; // color output by first spatial denoise pass, input to next frame as previous color
|
|
|
|
|
|
|
|
struct Model
|
|
|
|
{
|
|
|
|
uint32_t mesh; // Index of mesh in m_meshes
|
|
|
|
float position[3];
|
|
|
|
};
|
|
|
|
|
|
|
|
Model m_models[MODEL_COUNT];
|
|
|
|
Mesh* m_meshes[BX_COUNTOF(s_meshPaths)];
|
|
|
|
Mesh* m_ground;
|
|
|
|
bgfx::TextureHandle m_groundTexture;
|
|
|
|
bgfx::TextureHandle m_normalTexture;
|
|
|
|
|
|
|
|
uint32_t m_currFrame;
|
2021-01-02 22:12:45 +03:00
|
|
|
float m_texelHalf = 0.0f;
|
|
|
|
float m_fovY = 60.0f;
|
|
|
|
bool m_recreateFrameBuffers = false;
|
|
|
|
bool m_havePrevious = false;
|
|
|
|
|
|
|
|
float m_view[16];
|
|
|
|
float m_proj[16];
|
|
|
|
float m_proj2[16];
|
|
|
|
float m_viewToProjPrev[16];
|
|
|
|
float m_worldToViewPrev[16];
|
|
|
|
float m_jitter[2];
|
2021-01-02 21:42:02 +03:00
|
|
|
int32_t m_size[2];
|
|
|
|
|
|
|
|
// UI parameters
|
2021-01-02 22:12:45 +03:00
|
|
|
int32_t m_noiseType = 2;
|
|
|
|
int32_t m_spatialSampleType = 1;
|
|
|
|
int32_t m_denoisePasses = 5;
|
|
|
|
float m_sigmaDepth = 0.05f;
|
|
|
|
float m_sigmaNormal = 128.0f;
|
|
|
|
float m_feedbackMin = 0.8f;
|
|
|
|
float m_feedbackMax = 0.95f;
|
|
|
|
bool m_dynamicNoise = true;
|
|
|
|
bool m_useTemporalPass = true;
|
|
|
|
bool m_enableTxaa = false;
|
|
|
|
bool m_applyMitchellFilter = true;
|
|
|
|
bool m_useTxaaSlow = false;
|
2021-01-02 21:42:02 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2021-01-02 21:46:43 +03:00
|
|
|
ENTRY_IMPLEMENT_MAIN(ExampleDenoise, "43-denoise", "Denoise.");
|