Add new Example - AMD FidelityFX-FSR (#2612)

* Add new sample

* Add FSR shaders

* Add example template

* Add Antialiasing

* Add multi resolution rendering

* Implement magnifier

* Implement FSR EASU pass

* Implement FSR RCAS pass

* Improve wording of comments and UI

* Remove use of ffx_a.h in cpp

* Remove example external files

* Perform bilinear upsampling by compute shader

* Add FSR 16 Bit support

* Improve magnifier picking

* Fix magnifier picking

* Render magnifier widget

* Renaming of stuff

* Separate magnifier functionality

* Move FSR into separate class

* Reduce scope of FSR resources

* Fix FSR for Vulkan

* Fix OpenGL support

* Update sample screenshot
This commit is contained in:
Richard Schubert 2021-10-05 18:01:50 +02:00 committed by GitHub
parent f99a6b4246
commit fc513e163b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 5270 additions and 0 deletions

768
examples/46-fsr/app.cpp Normal file
View File

@ -0,0 +1,768 @@
/*
* Copyright 2021 Richard Schubert. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*
* AMD FidelityFX Super Resolution 1.0 (FSR)
* Based on https://github.com/GPUOpen-Effects/FidelityFX-FSR/blob/master/sample/
*/
#include <common.h>
#include <camera.h>
#include <bgfx_utils.h>
#include <imgui/imgui.h>
#include <bx/rng.h>
#include <bx/os.h>
#include <cmath>
#include <algorithm>
#include "fsr.h"
namespace
{
#define FRAMEBUFFER_RT_COLOR 0
#define FRAMEBUFFER_RT_DEPTH 1
#define FRAMEBUFFER_RENDER_TARGETS 2
enum Meshes
{
MeshCube = 0,
MeshHollowCube
};
static const char *s_meshPaths[] =
{
"meshes/cube.bin",
"meshes/hollowcube.bin"};
static const float s_meshScale[] =
{
0.45f,
0.30f};
// 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;
void screenSpaceTriangle(float _textureWidth, float _textureHeight, float _texelHalf, bool _originBottomLeft, float _width = 1.0f, float _height = 1.0f, float _offsetX = 0.0f, float _offsetY = 0.0f)
{
if (3 == bgfx::getAvailTransientVertexBuffer(3, PosTexCoord0Vertex::ms_layout))
{
bgfx::TransientVertexBuffer vb;
bgfx::allocTransientVertexBuffer(&vb, 3, PosTexCoord0Vertex::ms_layout);
PosTexCoord0Vertex *vertex = (PosTexCoord0Vertex *)vb.data;
const float minx = -_width - _offsetX;
const float maxx = _width - _offsetX;
const float miny = 0.0f - _offsetY;
const float maxy = _height * 2.0f - _offsetY;
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);
}
}
struct ModelUniforms
{
enum
{
NumVec4 = 2
};
void init()
{
u_params = bgfx::createUniform("u_modelParams", 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_color[3];
float m_unused0;
};
/* 1 */ struct
{
float m_lightPosition[3];
float m_unused1;
};
};
float m_params[NumVec4 * 4];
};
bgfx::UniformHandle u_params;
};
struct AppState
{
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_forwardProgram;
bgfx::ProgramHandle m_gridProgram;
bgfx::ProgramHandle m_copyLinearToGammaProgram;
// Shader uniforms
ModelUniforms m_modelUniforms;
// Uniforms to indentify texture samplers
bgfx::UniformHandle s_albedo;
bgfx::UniformHandle s_color;
bgfx::UniformHandle s_normal;
bgfx::FrameBufferHandle m_frameBuffer;
bgfx::TextureHandle m_frameBufferTex[FRAMEBUFFER_RENDER_TARGETS];
Mesh *m_meshes[BX_COUNTOF(s_meshPaths)];
bgfx::TextureHandle m_groundTexture;
bgfx::TextureHandle m_normalTexture;
uint32_t m_currFrame{UINT32_MAX};
float m_lightRotation = 0.0f;
float m_texelHalf = 0.0f;
float m_fovY = 60.0f;
float m_animationTime = 0.0f;
float m_view[16];
float m_proj[16];
int32_t m_size[2];
// UI parameters
bool m_renderNativeResolution = false;
bool m_animateScene = false;
int32_t m_antiAliasingSetting = 2;
Fsr m_fsr;
};
struct RenderTarget
{
void init(uint32_t _width, uint32_t _height, bgfx::TextureFormat::Enum _format, uint64_t _flags)
{
m_width = _width;
m_height = _height;
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);
}
uint32_t m_width;
uint32_t m_height;
bgfx::TextureHandle m_texture;
bgfx::FrameBufferHandle m_buffer;
};
struct MagnifierWidget
{
void init(uint32_t _width, uint32_t _height)
{
m_content.init(_width, _height, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT | BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT);
createWidgetTexture(_width + 6, _height + 6);
}
void destroy()
{
bgfx::destroy(m_widgetTexture);
m_content.destroy();
}
void setPosition(float x, float y)
{
m_position.x = x;
m_position.y = y;
}
void drawToScreen(bgfx::ViewId &view, AppState const &state, const bgfx::Caps *caps)
{
float invScreenScaleX = 1.0f / static_cast<float>(state.m_width);
float invScreenScaleY = 1.0f / static_cast<float>(state.m_height);
float scaleX = m_widgetWidth * invScreenScaleX;
float scaleY = m_widgetHeight * invScreenScaleY;
float offsetX = -std::min(std::max(m_position.x - m_widgetWidth * 0.5f, -3.0f), static_cast<float>(state.m_width - m_widgetWidth + 3)) * invScreenScaleX;
float offsetY = -std::min(std::max(m_position.y - m_widgetHeight * 0.5f, -3.0f), static_cast<float>(state.m_height - m_widgetHeight + 3)) * invScreenScaleY;
bgfx::setState(0 | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_DEPTH_TEST_ALWAYS | BGFX_STATE_BLEND_ALPHA);
bgfx::setTexture(0, state.s_color, m_widgetTexture);
screenSpaceTriangle(float(m_widgetWidth), float(m_widgetHeight), state.m_texelHalf, false, scaleX, scaleY, offsetX, offsetY);
bgfx::submit(view, state.m_copyLinearToGammaProgram);
}
void updateContent(bgfx::ViewId &view, AppState const &state, const bgfx::Caps *caps, bgfx::TextureHandle srcTexture)
{
float orthoProj[16];
bx::mtxOrtho(orthoProj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, caps->homogeneousDepth);
{
// clear out transform stack
float identity[16];
bx::mtxIdentity(identity);
bgfx::setTransform(identity);
}
float const verticalPos = caps->originBottomLeft ? state.m_height - m_position.y : m_position.y;
float const invMagScaleX = 1.0f / static_cast<float>(m_content.m_width);
float const invMagScaleY = 1.0f / static_cast<float>(m_content.m_height);
float const scaleX = state.m_width * invMagScaleX;
float const scaleY = state.m_height * invMagScaleY;
float const offsetX = std::min(std::max(m_position.x - m_content.m_width * 0.5f, 0.0f), static_cast<float>(state.m_width - m_content.m_width)) * scaleX / state.m_width;
float const offsetY = std::min(std::max(verticalPos - m_content.m_height * 0.5f, 0.0f), static_cast<float>(state.m_height - m_content.m_height)) * scaleY / state.m_height;
bgfx::setViewName(view, "magnifier");
bgfx::setViewRect(view, 0, 0, uint16_t(m_content.m_width), uint16_t(m_content.m_height));
bgfx::setViewTransform(view, NULL, orthoProj);
bgfx::setViewFrameBuffer(view, m_content.m_buffer);
bgfx::setState(0 | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A);
bgfx::setTexture(0, state.s_color, srcTexture, BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT | BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP);
screenSpaceTriangle(float(state.m_width), float(state.m_height), state.m_texelHalf, false, scaleX, scaleY, offsetX, offsetY);
bgfx::submit(view, state.m_copyLinearToGammaProgram);
++view;
}
uint32_t m_widgetWidth{0};
uint32_t m_widgetHeight{0};
bgfx::TextureHandle m_widgetTexture;
RenderTarget m_content;
ImVec2 m_position;
private:
void createWidgetTexture(uint32_t _width, uint32_t _height)
{
const bgfx::Memory *mem = bgfx::alloc(_width * _height * sizeof(uint32_t));
uint32_t *pixels = const_cast<uint32_t *>((uint32_t *const)(mem->data));
memset(pixels, 0, mem->size);
uint32_t const white = 0xFFFFFFFF;
uint32_t const black = 0xFF000000;
uint32_t const y0 = 1;
uint32_t const y1 = _height - 3;
for (uint32_t x = 0; x < _width - 4; x++)
{
pixels[(y0 + 0) * _width + x + 1] = white;
pixels[(y0 + 1) * _width + x + 2] = black;
pixels[(y1 + 0) * _width + x + 1] = white;
pixels[(y1 + 1) * _width + x + 2] = black;
}
uint32_t const x0 = 1;
uint32_t const x1 = _width - 3;
for (uint32_t y = 0; y < _height - 3; y++)
{
pixels[(y + 1) * _width + x0 + 0] = white;
pixels[(y + 2) * _width + x0 + 1] = black;
pixels[(y + 1) * _width + x1 + 0] = white;
pixels[(y + 2) * _width + x1 + 1] = black;
}
pixels[(y1 + 0) * _width + 2] = white;
m_widgetWidth = _width;
m_widgetHeight = _height;
m_widgetTexture = bgfx::createTexture2D(uint16_t(_width), uint16_t(_height), false, 1, bgfx::TextureFormat::BGRA8, BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT | BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP, mem);
}
};
class ExampleFsr : public entry::AppI
{
public:
ExampleFsr(const char *_name, const char *_description)
: entry::AppI(_name, _description)
{
}
void init(int32_t _argc, const char *const *_argv, uint32_t _width, uint32_t _height) override
{
Args args(_argc, _argv);
m_state.m_width = _width;
m_state.m_height = _height;
m_state.m_debug = BGFX_DEBUG_NONE;
m_state.m_reset = BGFX_RESET_MAXANISOTROPY;
bgfx::Init init;
init.type = args.m_type;
init.vendorId = args.m_pciId;
init.resolution.width = m_state.m_width;
init.resolution.height = m_state.m_height;
init.resolution.reset = m_state.m_reset;
bgfx::init(init);
// Enable debug text.
bgfx::setDebug(m_state.m_debug);
// Create uniforms for screen passes and models
m_state.m_modelUniforms.init();
// Create texture sampler uniforms (used when we bind textures)
m_state.s_albedo = bgfx::createUniform("s_albedo", bgfx::UniformType::Sampler);
m_state.s_color = bgfx::createUniform("s_color", bgfx::UniformType::Sampler);
m_state.s_normal = bgfx::createUniform("s_normal", bgfx::UniformType::Sampler);
// Create program from shaders.
m_state.m_forwardProgram = loadProgram("vs_fsr_forward", "fs_fsr_forward");
m_state.m_gridProgram = loadProgram("vs_fsr_forward", "fs_fsr_forward_grid");
m_state.m_copyLinearToGammaProgram = loadProgram("vs_fsr_screenquad", "fs_fsr_copy_linear_to_gamma");
// Load some meshes
for (uint32_t ii = 0; ii < BX_COUNTOF(s_meshPaths); ++ii)
{
m_state.m_meshes[ii] = meshLoad(s_meshPaths[ii]);
}
m_state.m_groundTexture = loadTexture("textures/fieldstone-rgba.dds");
m_state.m_normalTexture = loadTexture("textures/fieldstone-n.dds");
createFramebuffers();
// Vertex decl
PosTexCoord0Vertex::init();
// Init camera
cameraCreate();
cameraSetPosition({-10.0f, 2.5f, -0.0f});
cameraSetVerticalAngle(-0.2f);
cameraSetHorizontalAngle(0.8f);
// Init "prev" matrices, will be same for first frame
cameraGetViewMtx(m_state.m_view);
bx::mtxProj(m_state.m_proj, m_state.m_fovY, float(m_state.m_size[0]) / float(m_state.m_size[1]), 0.01f, 100.0f, bgfx::getCaps()->homogeneousDepth);
// Get renderer capabilities info.
const bgfx::RendererType::Enum renderer = bgfx::getRendererType();
m_state.m_texelHalf = bgfx::RendererType::Direct3D9 == renderer ? 0.5f : 0.0f;
const uint32_t magnifierSize = 32;
m_magnifierWidget.init(magnifierSize, magnifierSize);
m_magnifierWidget.setPosition(m_state.m_width * 0.5f, m_state.m_height * 0.5f);
imguiCreate();
m_state.m_fsr.init(_width, _height);
}
int32_t shutdown() override
{
m_state.m_fsr.destroy();
for (uint32_t ii = 0; ii < BX_COUNTOF(s_meshPaths); ++ii)
{
meshUnload(m_state.m_meshes[ii]);
}
bgfx::destroy(m_state.m_normalTexture);
bgfx::destroy(m_state.m_groundTexture);
bgfx::destroy(m_state.m_forwardProgram);
bgfx::destroy(m_state.m_gridProgram);
bgfx::destroy(m_state.m_copyLinearToGammaProgram);
m_state.m_modelUniforms.destroy();
m_magnifierWidget.destroy();
bgfx::destroy(m_state.s_albedo);
bgfx::destroy(m_state.s_color);
bgfx::destroy(m_state.s_normal);
destroyFramebuffers();
cameraDestroy();
imguiDestroy();
bgfx::shutdown();
return 0;
}
bool update() override
{
if (!entry::processEvents(m_state.m_width, m_state.m_height, m_state.m_debug, m_state.m_reset, &m_state.m_mouseState))
{
// skip processing when minimized, otherwise crashing
if (0 == m_state.m_width || 0 == m_state.m_height)
{
return true;
}
if (m_state.m_mouseState.m_buttons[entry::MouseButton::Left] && !ImGui::MouseOverArea())
{
m_magnifierWidget.setPosition(static_cast<float>(m_state.m_mouseState.m_mx),
static_cast<float>(m_state.m_mouseState.m_my));
}
// Update frame timer
int64_t now = bx::getHPCounter();
static int64_t last = now;
const int64_t frameTime = now - last;
last = now;
const double freq = double(bx::getHPFrequency());
const float deltaTime = float(frameTime / freq);
const bgfx::Caps *caps = bgfx::getCaps();
if (m_state.m_size[0] != (int32_t)m_state.m_width || m_state.m_size[1] != (int32_t)m_state.m_height)
{
resize();
}
// update animation time
const float rotationSpeed = 0.25f;
if (m_state.m_animateScene)
{
m_state.m_animationTime += deltaTime * rotationSpeed;
if (bx::kPi2 < m_state.m_animationTime)
{
m_state.m_animationTime -= bx::kPi2;
}
}
// Update camera
cameraUpdate(deltaTime * 0.15f, m_state.m_mouseState, ImGui::MouseOverArea());
cameraGetViewMtx(m_state.m_view);
updateUniforms();
bx::mtxProj(m_state.m_proj, m_state.m_fovY, float(m_state.m_size[0]) / float(m_state.m_size[1]), 0.01f, 100.0f, caps->homogeneousDepth);
bgfx::ViewId view = 0;
// Draw models into scene
{
bgfx::setViewName(view, "forward scene");
bgfx::setViewClear(view, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x7fb8ffff, 1.0f, 0);
float const viewScale = m_state.m_renderNativeResolution ? 1.0f : 1.0f / m_state.m_fsr.m_config.m_superSamplingFactor;
uint16_t const viewRectWidth = uint16_t(ceilf(m_state.m_size[0] * viewScale));
uint16_t const viewRectHeight = uint16_t(ceilf(m_state.m_size[1] * viewScale));
uint16_t const viewRectY = caps->originBottomLeft ? m_state.m_size[1] - viewRectHeight : 0;
bgfx::setViewRect(view, 0, viewRectY, viewRectWidth, viewRectHeight);
bgfx::setViewTransform(view, m_state.m_view, m_state.m_proj);
bgfx::setViewFrameBuffer(view, m_state.m_frameBuffer);
bgfx::setState(0 | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_WRITE_Z | BGFX_STATE_DEPTH_TEST_LESS);
drawAllModels(view, m_state.m_forwardProgram, m_state.m_modelUniforms);
++view;
}
// optionally run FSR
if (!m_state.m_renderNativeResolution)
{
view = m_state.m_fsr.computeFsr(view, m_state.m_frameBufferTex[FRAMEBUFFER_RT_COLOR]);
}
// render result to screen
{
bgfx::TextureHandle srcTexture = m_state.m_frameBufferTex[FRAMEBUFFER_RT_COLOR];
if (!m_state.m_renderNativeResolution)
{
srcTexture = m_state.m_fsr.getResultTexture();
}
m_magnifierWidget.updateContent(view, m_state, caps, srcTexture);
float orthoProj[16];
bx::mtxOrtho(orthoProj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, caps->homogeneousDepth);
{
// clear out transform stack
float identity[16];
bx::mtxIdentity(identity);
bgfx::setTransform(identity);
}
bgfx::setViewName(view, "display");
bgfx::setViewClear(view, BGFX_CLEAR_NONE, 0, 1.0f, 0);
bgfx::setViewRect(view, 0, 0, uint16_t(m_state.m_width), uint16_t(m_state.m_height));
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, m_state.s_color, srcTexture, BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT | BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP);
screenSpaceTriangle(float(m_state.m_width), float(m_state.m_height), m_state.m_texelHalf, caps->originBottomLeft);
bgfx::submit(view, m_state.m_copyLinearToGammaProgram);
}
m_magnifierWidget.drawToScreen(view, m_state, caps);
++view;
// Draw UI
imguiBeginFrame(m_state.m_mouseState.m_mx, m_state.m_mouseState.m_my, (m_state.m_mouseState.m_buttons[entry::MouseButton::Left] ? IMGUI_MBUT_LEFT : 0) | (m_state.m_mouseState.m_buttons[entry::MouseButton::Right] ? IMGUI_MBUT_RIGHT : 0) | (m_state.m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0), m_state.m_mouseState.m_mz, uint16_t(m_state.m_width), uint16_t(m_state.m_height));
showExampleDialog(this);
ImGui::SetNextWindowPos(ImVec2(m_state.m_width - m_state.m_width / 4.0f - 10.0f, 10.0f), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(m_state.m_width / 4.0f, m_state.m_height / 1.2f), ImGuiCond_FirstUseEver);
ImGui::Begin("Settings", NULL, 0);
ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
ImVec2 const itemSize = ImGui::GetItemRectSize();
{
ImGui::Checkbox("Animate scene", &m_state.m_animateScene);
if (ImGui::Combo("Antialiasing", &m_state.m_antiAliasingSetting, "none\0"
"4x\0"
"16x\0"
"\0"))
{
resize();
}
ImGui::Checkbox("Render native resolution", &m_state.m_renderNativeResolution);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Disable super sampling and FSR.");
ImGui::Image(m_magnifierWidget.m_content.m_texture, ImVec2(itemSize.x * 0.94f, itemSize.x * 0.94f));
if (!m_state.m_renderNativeResolution)
{
ImGui::SliderFloat("Super sampling", &m_state.m_fsr.m_config.m_superSamplingFactor, 1.0f, 2.0f);
if (ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
ImGui::Text("2.0 means the scene is rendered at half window resolution.");
ImGui::Text("1.0 means the scene is rendered at native window resolution.");
ImGui::EndTooltip();
}
ImGui::Separator();
if (m_state.m_fsr.supports16BitPrecision())
{
ImGui::Checkbox("Use 16 Bit", &m_state.m_fsr.m_config.m_fsr16Bit);
if (ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
ImGui::Text("For better performance and less memory consumption use 16 Bit precision.");
ImGui::Text("If disabled use 32 Bit per channel precision for FSR which works better on older hardware.");
ImGui::Text("FSR in 16 Bit precision is also prone to be broken in Direct3D11, Direct3D12 works though.");
ImGui::EndTooltip();
}
}
ImGui::Checkbox("Apply FSR", &m_state.m_fsr.m_config.m_applyFsr);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Compare between FSR and bilinear interpolation of source image.");
if (m_state.m_fsr.m_config.m_applyFsr)
{
ImGui::Checkbox("Apply FSR sharpening", &m_state.m_fsr.m_config.m_applyFsrRcas);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Apply the FSR RCAS sharpening pass.");
if (m_state.m_fsr.m_config.m_applyFsrRcas)
{
ImGui::SliderFloat("Sharpening attenuation", &m_state.m_fsr.m_config.m_rcasAttenuation, 0.01f, 2.0f);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Lower value means sharper.");
}
}
}
}
ImGui::End();
imguiEndFrame();
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
m_state.m_currFrame = bgfx::frame();
return true;
}
return false;
}
void drawAllModels(bgfx::ViewId _pass, bgfx::ProgramHandle _program, ModelUniforms &_uniforms)
{
const int32_t width = 6;
const int32_t length = 20;
float c0[] = {235.0f / 255.0f, 126.0f / 255.0f, 30.0f / 255.0f}; // orange
float c1[] = {235.0f / 255.0f, 146.0f / 255.0f, 251.0f / 255.0f}; // purple
float c2[] = {199.0f / 255.0f, 0.0f / 255.0f, 57.0f / 255.0f}; // pink
for (int32_t zz = 0; zz < length; ++zz)
{
// make a color gradient, nothing special about this for example
float *ca = c0;
float *cb = c1;
float lerpVal = float(zz) / float(length);
if (0.5f <= lerpVal)
{
ca = c1;
cb = c2;
}
lerpVal = bx::fract(2.0f * lerpVal);
float r = bx::lerp(ca[0], cb[0], lerpVal);
float g = bx::lerp(ca[1], cb[1], lerpVal);
float b = bx::lerp(ca[2], cb[2], lerpVal);
for (int32_t xx = 0; xx < width; ++xx)
{
const float angle = m_state.m_animationTime + float(zz) * (bx::kPi2 / length) + float(xx) * (bx::kPiHalf / width);
const float posX = 2.0f * xx - width + 1.0f;
const float posY = bx::sin(angle);
const float posZ = 2.0f * zz - length + 1.0f;
const float scale = s_meshScale[MeshHollowCube];
float mtx[16];
bx::mtxSRT(mtx, scale, scale, scale, 0.0f, 0.0f, 0.0f, posX, posY, posZ);
bgfx::setTexture(0, m_state.s_albedo, m_state.m_groundTexture);
bgfx::setTexture(1, m_state.s_normal, m_state.m_normalTexture);
_uniforms.m_color[0] = r;
_uniforms.m_color[1] = g;
_uniforms.m_color[2] = b;
_uniforms.submit();
meshSubmit(m_state.m_meshes[MeshHollowCube], _pass, _program, mtx);
}
}
// draw box as ground plane
{
const float posY = -2.0f;
const float scale = length;
float mtx[16];
bx::mtxSRT(mtx, scale, scale, scale, 0.0f, 0.0f, 0.0f, 0.0f, -scale + posY, 0.0f);
_uniforms.m_color[0] = 0.5f;
_uniforms.m_color[1] = 0.5f;
_uniforms.m_color[2] = 0.5f;
_uniforms.submit();
meshSubmit(m_state.m_meshes[MeshCube], _pass, m_state.m_gridProgram, mtx);
}
}
void resize()
{
destroyFramebuffers();
createFramebuffers();
m_state.m_fsr.resize(m_state.m_width, m_state.m_height);
}
void createFramebuffers()
{
m_state.m_size[0] = m_state.m_width;
m_state.m_size[1] = m_state.m_height;
uint64_t constexpr msaaFlags[] = {BGFX_TEXTURE_NONE, BGFX_TEXTURE_RT_MSAA_X4, BGFX_TEXTURE_RT_MSAA_X16};
const uint64_t msaa = msaaFlags[m_state.m_antiAliasingSetting];
const uint64_t colorFlags = 0 | BGFX_TEXTURE_RT | BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP | msaa;
const uint64_t depthFlags = 0 | BGFX_TEXTURE_RT_WRITE_ONLY | msaa;
m_state.m_frameBufferTex[FRAMEBUFFER_RT_COLOR] = bgfx::createTexture2D(uint16_t(m_state.m_size[0]), uint16_t(m_state.m_size[1]), false, 1, bgfx::TextureFormat::RGBA16F, colorFlags);
m_state.m_frameBufferTex[FRAMEBUFFER_RT_DEPTH] = bgfx::createTexture2D(uint16_t(m_state.m_size[0]), uint16_t(m_state.m_size[1]), false, 1, bgfx::TextureFormat::D24S8, depthFlags);
m_state.m_frameBuffer = bgfx::createFrameBuffer(BX_COUNTOF(m_state.m_frameBufferTex), m_state.m_frameBufferTex, true);
}
// all buffers set to destroy their textures
void destroyFramebuffers()
{
bgfx::destroy(m_state.m_frameBuffer);
}
void updateUniforms()
{
m_state.m_modelUniforms.m_lightPosition[0] = 0.0f;
m_state.m_modelUniforms.m_lightPosition[1] = 6.0f;
m_state.m_modelUniforms.m_lightPosition[2] = 10.0f;
}
AppState m_state;
MagnifierWidget m_magnifierWidget;
};
} // namespace
ENTRY_IMPLEMENT_MAIN(ExampleFsr, "46-fsr", "AMD FidelityFX Super Resolution (FSR)\n\nFor an optimal FSR result high quality antialiasing for the low resolution source image and negative texture LOD bias is recommended.");

129
examples/46-fsr/cs_fsr.h Normal file
View File

@ -0,0 +1,129 @@
/*
* Copyright 2021 Richard Schubert. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*
* AMD FidelityFX Super Resolution 1.0 (FSR)
* Port from https://github.com/GPUOpen-Effects/FidelityFX-FSR/blob/master/sample/src/VK/FSR_Pass.glsl
*/
#include "bgfx_compute.sh"
uniform vec4 u_params[3];
#define ViewportSizeRcasAttenuation (u_params[0])
#define SrcSize (u_params[1])
#define DstSize (u_params[2])
#define A_GPU 1
#if BGFX_SHADER_LANGUAGE_GLSL > 0
#define A_GLSL 1
#define A_SKIP_EXT 1
#elif BGFX_SHADER_LANGUAGE_SPIRV > 0
#define A_HLSL 1
#elif BGFX_SHADER_LANGUAGE_HLSL > 0
#define A_HLSL 1
#endif
#if SAMPLE_SLOW_FALLBACK
#include "ffx_a.h"
SAMPLER2D(InputTexture, 0);
IMAGE2D_WR(OutputTexture, rgba32f, 1);
#if SAMPLE_EASU
#define FSR_EASU_F 1
AF4 FsrEasuRF(AF2 p) { AF4 res = textureGather(InputTexture, p, 0); return res; }
AF4 FsrEasuGF(AF2 p) { AF4 res = textureGather(InputTexture, p, 1); return res; }
AF4 FsrEasuBF(AF2 p) { AF4 res = textureGather(InputTexture, p, 2); return res; }
#endif
#if SAMPLE_RCAS
#define FSR_RCAS_F
AF4 FsrRcasLoadF(ASU2 p) { return texelFetch(InputTexture, ASU2(p), 0); }
void FsrRcasInputF(inout AF1 r, inout AF1 g, inout AF1 b) {}
#endif
#else
#define A_HALF
#include "ffx_a.h"
SAMPLER2D(InputTexture, 0);
IMAGE2D_WR(OutputTexture, rgba16f, 1);
#if SAMPLE_EASU
#define FSR_EASU_H 1
AH4 FsrEasuRH(AF2 p) { AH4 res = AH4(textureGather(InputTexture, p, 0)); return res; }
AH4 FsrEasuGH(AF2 p) { AH4 res = AH4(textureGather(InputTexture, p, 1)); return res; }
AH4 FsrEasuBH(AF2 p) { AH4 res = AH4(textureGather(InputTexture, p, 2)); return res; }
#endif
#if SAMPLE_RCAS
#define FSR_RCAS_H
AH4 FsrRcasLoadH(ASW2 p) { return AH4(texelFetch(InputTexture, ASU2(p), 0)); }
void FsrRcasInputH(inout AH1 r,inout AH1 g,inout AH1 b){}
#endif
#endif
#include "ffx_fsr1.h"
void CurrFilter(AU2 pos, AU4 Const0, AU4 Const1, AU4 Const2, AU4 Const3, AU4 Sample)
{
#if SAMPLE_BILINEAR
AF2 pp = (AF2(pos) * AF2_AU2(Const0.xy) + AF2_AU2(Const0.zw)) * AF2_AU2(Const1.xy) + AF2(0.5, -0.5) * AF2_AU2(Const1.zw);
imageStore(OutputTexture, ASU2(pos), texture2DLod(InputTexture, pp, 0.0));
#endif
#if SAMPLE_EASU
#if SAMPLE_SLOW_FALLBACK
AF3 c;
FsrEasuF(c, pos, Const0, Const1, Const2, Const3);
if( Sample.x == 1 )
c *= c;
imageStore(OutputTexture, ASU2(pos), AF4(c, 1));
#else
AH3 c;
FsrEasuH(c, pos, Const0, Const1, Const2, Const3);
if( Sample.x == 1 )
c *= c;
imageStore(OutputTexture, ASU2(pos), AH4(c, 1));
#endif
#endif
#if SAMPLE_RCAS
#if SAMPLE_SLOW_FALLBACK
AF3 c;
FsrRcasF(c.r, c.g, c.b, pos, Const0);
if( Sample.x == 1 )
c *= c;
imageStore(OutputTexture, ASU2(pos), AF4(c, 1));
#else
AH3 c;
FsrRcasH(c.r, c.g, c.b, pos, Const0);
if( Sample.x == 1 )
c *= c;
imageStore(OutputTexture, ASU2(pos), AH4(c, 1));
#endif
#endif
}
NUM_THREADS(64, 1, 1)
void main()
{
AU4 Const0, Const1, Const2, Const3, Sample;
// We compute these constants on GPU because bgfx does not support uniform type uint.
#if SAMPLE_EASU || SAMPLE_BILINEAR
FsrEasuCon(Const0, Const1, Const2, Const3,
ViewportSizeRcasAttenuation.x, ViewportSizeRcasAttenuation.y, // Viewport size (top left aligned) in the input image which is to be scaled.
SrcSize.x, SrcSize.y, // The size of the input image.
DstSize.x, DstSize.y); // The output resolution.
Sample.x = 0; // no HDR output
#endif
#if SAMPLE_RCAS
FsrRcasCon(Const0, ViewportSizeRcasAttenuation.z);
Sample.x = 0; // no HDR output
#endif
// Do remapping of local xy in workgroup for a more PS-like swizzle pattern.
AU2 gxy = ARmp8x8(gl_LocalInvocationID.x) + AU2(gl_WorkGroupID.x << 4u, gl_WorkGroupID.y << 4u);
CurrFilter(gxy, Const0, Const1, Const2, Const3, Sample);
gxy.x += 8u;
CurrFilter(gxy, Const0, Const1, Const2, Const3, Sample);
gxy.y += 8u;
CurrFilter(gxy, Const0, Const1, Const2, Const3, Sample);
gxy.x -= 8u;
CurrFilter(gxy, Const0, Const1, Const2, Const3, Sample);
}

View File

@ -0,0 +1,3 @@
#define SAMPLE_BILINEAR 1
#include "cs_fsr.h"

View File

@ -0,0 +1,4 @@
#define SAMPLE_BILINEAR 1
#define SAMPLE_SLOW_FALLBACK 1
#include "cs_fsr.h"

View File

@ -0,0 +1,3 @@
#define SAMPLE_EASU 1
#include "cs_fsr.h"

View File

@ -0,0 +1,4 @@
#define SAMPLE_SLOW_FALLBACK 1
#define SAMPLE_EASU 1
#include "cs_fsr.h"

View File

@ -0,0 +1,3 @@
#define SAMPLE_RCAS 1
#include "cs_fsr.h"

View File

@ -0,0 +1,4 @@
#define SAMPLE_SLOW_FALLBACK 1
#define SAMPLE_RCAS 1
#include "cs_fsr.h"

2637
examples/46-fsr/ffx_a.h Normal file

File diff suppressed because it is too large Load Diff

1199
examples/46-fsr/ffx_fsr1.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,21 @@
$input v_texcoord0
/*
* Copyright 2021 elven cache. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "../common/common.sh"
SAMPLER2D(s_color, 0);
void main()
{
vec2 texCoord = v_texcoord0;
vec4 linearColor = texture2D(s_color, texCoord);
// this pass is writing directly out to backbuffer, convert from linear to gamma
vec4 color = vec4(toGamma(linearColor.xyz), linearColor.w);
gl_FragColor = color;
}

View File

@ -0,0 +1,79 @@
$input v_normal, v_texcoord0, v_texcoord1, v_texcoord2
/*
* Copyright 2021 elven cache. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "../common/common.sh"
SAMPLER2D(s_albedo, 0);
SAMPLER2D(s_normal, 1);
// struct ModelUniforms
uniform vec4 u_modelParams[2];
#define u_color (u_modelParams[0].xyz)
#define u_lightPosition (u_modelParams[1].xyz)
// http://www.thetenthplanet.de/archives/1180
// "followup: normal mapping without precomputed tangents"
mat3 cotangentFrame(vec3 N, vec3 p, vec2 uv)
{
// get edge vectors of the pixel triangle
vec3 dp1 = dFdx(p);
vec3 dp2 = dFdy(p);
vec2 duv1 = dFdx(uv);
vec2 duv2 = dFdy(uv);
// solve the linear system
vec3 dp2perp = cross(dp2, N);
vec3 dp1perp = cross(N, dp1);
vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;
// construct a scale-invariant frame
float invMax = inversesqrt(max(dot(T,T), dot(B,B)));
return mat3(T*invMax, B*invMax, N);
}
void main()
{
vec3 albedo = toLinear(texture2D(s_albedo, v_texcoord0).xyz);
// get vertex normal
vec3 normal = normalize(v_normal);
// get normal map normal, unpack, and calculate z
vec3 normalMap;
normalMap.xy = texture2D(s_normal, v_texcoord0).xy;
normalMap.xy = normalMap.xy * 2.0 - 1.0;
normalMap.z = sqrt(1.0 - dot(normalMap.xy, normalMap.xy));
// swap x and y, because the brick texture looks flipped, don't copy this...
normalMap.xy = -normalMap.yx;
// perturb geometry normal by normal map
vec3 pos = v_texcoord1.xyz; // contains world space pos
mat3 TBN = cotangentFrame(normal, pos, v_texcoord0);
vec3 bumpedNormal = normalize(instMul(TBN, normalMap));
vec3 light = (u_lightPosition - pos);
light = normalize(light);
float NdotL = saturate(dot(bumpedNormal, light));
float diffuse = NdotL * 1.0;
vec3 V = v_texcoord2.xyz; // contains view vector
vec3 H = normalize(V+light);
float NdotH = saturate(dot(bumpedNormal, H));
float specular = 5.0 * pow(NdotH, 256);
float ambient = 0.1;
float lightAmount = ambient + diffuse;
vec3 color = u_color * albedo * lightAmount + specular;
// leave color in linear space for better dof filter result
gl_FragColor = vec4(color, 1.0);
}

View File

@ -0,0 +1,58 @@
$input v_normal, v_texcoord0, v_texcoord1, v_texcoord2
/*
* Copyright 2021 elven cache. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "../common/common.sh"
// struct ModelUniforms
uniform vec4 u_modelParams[2];
#define u_color (u_modelParams[0].xyz)
#define u_lightPosition (u_modelParams[1].xyz)
int ModHelper (float a, float b)
{
return int( a - (b*floor(a/b)));
}
vec3 GetGridColor (vec2 position, float width, vec3 color)
{
position = abs(floor( position + vec2(-width, -width) ));
int posXMod = ModHelper(position.x, 2.0);
int posYMod = ModHelper(position.y, 2.0);
float gridColorScale = (posXMod == posYMod) ? 0.75 : 1.25;
return toLinear(color) * gridColorScale;
}
void main()
{
vec3 worldSpacePosition = v_texcoord1.xyz; // contains ws pos
vec2 gridCoord = worldSpacePosition.xz; // assuming y is up
vec3 gridColor = GetGridColor(gridCoord.xy, 0.002, u_color);
// get vertex normal
vec3 normal = normalize(v_normal);
vec3 light = (u_lightPosition - worldSpacePosition);
light = normalize(light);
float NdotL = saturate(dot(normal, light));
float diffuse = NdotL * 1.0;
vec3 V = v_texcoord2.xyz; // contains view vector
vec3 H = normalize(V+light);
float NdotH = saturate(dot(normal, H));
float specular = 5.0 * pow(NdotH, 256);
float ambient = 0.1;
float lightAmount = ambient + diffuse;
vec3 color = gridColor * lightAmount + specular;
// leave color in linear space for better dof filter result
gl_FragColor = vec4(color, 1.0);
}

248
examples/46-fsr/fsr.cpp Normal file
View File

@ -0,0 +1,248 @@
/*
* Copyright 2021 Richard Schubert. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*
* AMD FidelityFX Super Resolution 1.0 (FSR)
* Based on https://github.com/GPUOpen-Effects/FidelityFX-FSR/blob/master/sample/
*/
#include "fsr.h"
#include <bgfx_utils.h>
#include <assert.h>
struct FsrResources
{
struct Uniforms
{
struct Vec4
{
float x;
float y;
float z;
float w;
};
enum
{
NumVec4 = 3
};
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
{
Vec4 ViewportSizeRcasAttenuation;
Vec4 SrcSize;
Vec4 DstSize;
};
uint32_t m_params[NumVec4 * 4];
};
bgfx::UniformHandle u_params{ BGFX_INVALID_HANDLE };
};
uint32_t m_width{ 0 };
uint32_t m_height{ 0 };
// Resource handles
bgfx::ProgramHandle m_bilinear16Program{ BGFX_INVALID_HANDLE };
bgfx::ProgramHandle m_bilinear32Program{ BGFX_INVALID_HANDLE };
bgfx::ProgramHandle m_easu16Program{ BGFX_INVALID_HANDLE };
bgfx::ProgramHandle m_easu32Program{ BGFX_INVALID_HANDLE };
bgfx::ProgramHandle m_rcas16Program{ BGFX_INVALID_HANDLE };
bgfx::ProgramHandle m_rcas32Program{ BGFX_INVALID_HANDLE };
// Shader uniforms
Uniforms m_uniforms;
// Uniforms to indentify texture samplers
bgfx::UniformHandle s_inputTexture{ BGFX_INVALID_HANDLE };
bgfx::TextureHandle m_easuTexture16F{ BGFX_INVALID_HANDLE };
bgfx::TextureHandle m_rcasTexture16F{ BGFX_INVALID_HANDLE };
bgfx::TextureHandle m_easuTexture32F{ BGFX_INVALID_HANDLE };
bgfx::TextureHandle m_rcasTexture32F{ BGFX_INVALID_HANDLE };
};
Fsr::Fsr()
{
m_resources = new FsrResources();
}
Fsr::~Fsr()
{
delete m_resources;
}
void Fsr::init(uint32_t _width, uint32_t _height)
{
resize(_width, _height);
// Create uniforms for screen passes and models
m_resources->m_uniforms.init();
// Create texture sampler uniforms (used when we bind textures)
m_resources->s_inputTexture = bgfx::createUniform("InputTexture", bgfx::UniformType::Sampler);
// Create program from shaders.
m_resources->m_bilinear32Program = bgfx::createProgram(loadShader("cs_fsr_bilinear_32"), true);
m_resources->m_easu32Program = bgfx::createProgram(loadShader("cs_fsr_easu_32"), true);
m_resources->m_rcas32Program = bgfx::createProgram(loadShader("cs_fsr_rcas_32"), true);
m_support16BitPrecision = (bgfx::getRendererType() != bgfx::RendererType::OpenGL);
if (m_support16BitPrecision)
{
m_resources->m_bilinear16Program = bgfx::createProgram(loadShader("cs_fsr_bilinear_16"), true);
m_resources->m_easu16Program = bgfx::createProgram(loadShader("cs_fsr_easu_16"), true);
m_resources->m_rcas16Program = bgfx::createProgram(loadShader("cs_fsr_rcas_16"), true);
}
}
void Fsr::destroy()
{
if(m_support16BitPrecision)
{
bgfx::destroy(m_resources->m_bilinear16Program);
bgfx::destroy(m_resources->m_easu16Program);
bgfx::destroy(m_resources->m_rcas16Program);
}
bgfx::destroy(m_resources->m_bilinear32Program);
bgfx::destroy(m_resources->m_easu32Program);
bgfx::destroy(m_resources->m_rcas32Program);
m_resources->m_uniforms.destroy();
bgfx::destroy(m_resources->s_inputTexture);
if (m_support16BitPrecision)
{
bgfx::destroy(m_resources->m_easuTexture16F);
bgfx::destroy(m_resources->m_rcasTexture16F);
}
bgfx::destroy(m_resources->m_easuTexture32F);
bgfx::destroy(m_resources->m_rcasTexture32F);
}
void Fsr::resize(uint32_t _width, uint32_t _height)
{
m_resources->m_width = _width;
m_resources->m_height = _height;
if(m_resources->m_easuTexture16F.idx != bgfx::kInvalidHandle)
{
if (m_support16BitPrecision)
{
bgfx::destroy(m_resources->m_easuTexture16F);
bgfx::destroy(m_resources->m_rcasTexture16F);
}
bgfx::destroy(m_resources->m_easuTexture32F);
bgfx::destroy(m_resources->m_rcasTexture32F);
}
if (m_support16BitPrecision)
{
m_resources->m_easuTexture16F = bgfx::createTexture2D(uint16_t(m_resources->m_width), uint16_t(m_resources->m_height), false, 1, bgfx::TextureFormat::RGBA16F, BGFX_TEXTURE_COMPUTE_WRITE | BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT | BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP);
m_resources->m_rcasTexture16F = bgfx::createTexture2D(uint16_t(m_resources->m_width), uint16_t(m_resources->m_height), false, 1, bgfx::TextureFormat::RGBA16F, BGFX_TEXTURE_COMPUTE_WRITE | BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT | BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP);
}
m_resources->m_easuTexture32F = bgfx::createTexture2D(uint16_t(m_resources->m_width), uint16_t(m_resources->m_height), false, 1, bgfx::TextureFormat::RGBA32F, BGFX_TEXTURE_COMPUTE_WRITE | BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT | BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP);
m_resources->m_rcasTexture32F = bgfx::createTexture2D(uint16_t(m_resources->m_width), uint16_t(m_resources->m_height), false, 1, bgfx::TextureFormat::RGBA32F, BGFX_TEXTURE_COMPUTE_WRITE | BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT | BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP);
}
bgfx::ViewId Fsr::computeFsr(bgfx::ViewId _pass, bgfx::TextureHandle _colorTexture)
{
assert(!m_config.m_fsr16Bit || m_support16BitPrecision);
updateUniforms();
bgfx::ViewId view = _pass;
// This value is the image region dimension that each thread group of the FSR shader operates on
static constexpr int threadGroupWorkRegionDim = 16;
int const dispatchX = (m_resources->m_width + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim;
int const dispatchY = (m_resources->m_height + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim;
bgfx::TextureFormat::Enum const format = m_config.m_fsr16Bit ? bgfx::TextureFormat::RGBA16F : bgfx::TextureFormat::RGBA32F;
bgfx::TextureHandle fsrEasuTexture = m_config.m_fsr16Bit ? m_resources->m_easuTexture16F : m_resources->m_easuTexture32F;
// EASU pass (upscale)
{
bgfx::ProgramHandle program = m_config.m_fsr16Bit ? m_resources->m_easu16Program : m_resources->m_easu32Program;
if (!m_config.m_applyFsr)
{
program = m_resources->m_bilinear32Program;
}
bgfx::setViewName(view, "fsr easu");
m_resources->m_uniforms.submit();
bgfx::setTexture(0, m_resources->s_inputTexture, _colorTexture);
bgfx::setImage(1, fsrEasuTexture, 0, bgfx::Access::Write, format);
bgfx::dispatch(view, program, dispatchX, dispatchY, 1);
++view;
}
// RCAS pass (sharpening)
if (m_config.m_applyFsrRcas)
{
bgfx::ProgramHandle program = m_config.m_fsr16Bit ? m_resources->m_rcas16Program : m_resources->m_rcas32Program;
bgfx::setViewName(view, "fsr rcas");
m_resources->m_uniforms.submit();
bgfx::setTexture(0, m_resources->s_inputTexture, fsrEasuTexture);
bgfx::setImage(1, m_config.m_fsr16Bit ? m_resources->m_rcasTexture16F : m_resources->m_rcasTexture32F, 0, bgfx::Access::Write, format);
bgfx::dispatch(view, program, dispatchX, dispatchY, 1);
++view;
}
return view;
}
bgfx::TextureHandle Fsr::getResultTexture() const
{
if (m_config.m_applyFsr && m_config.m_applyFsrRcas)
{
return m_config.m_fsr16Bit ? m_resources->m_rcasTexture16F : m_resources->m_rcasTexture32F;
}
else
{
return m_config.m_fsr16Bit ? m_resources->m_easuTexture16F : m_resources->m_easuTexture32F;
}
}
bool Fsr::supports16BitPrecision() const
{
return m_support16BitPrecision;
}
void Fsr::updateUniforms()
{
float const srcWidth = static_cast<float>(m_resources->m_width) / m_config.m_superSamplingFactor;
float const srcHeight = static_cast<float>(m_resources->m_height) / m_config.m_superSamplingFactor;
m_resources->m_uniforms.ViewportSizeRcasAttenuation.x = srcWidth;
m_resources->m_uniforms.ViewportSizeRcasAttenuation.y = srcHeight;
m_resources->m_uniforms.ViewportSizeRcasAttenuation.z = m_config.m_rcasAttenuation;
m_resources->m_uniforms.SrcSize.x = static_cast<float>(m_resources->m_width);
m_resources->m_uniforms.SrcSize.y = static_cast<float>(m_resources->m_height);
m_resources->m_uniforms.DstSize.x = static_cast<float>(m_resources->m_width);
m_resources->m_uniforms.DstSize.y = static_cast<float>(m_resources->m_height);
}

47
examples/46-fsr/fsr.h Normal file
View File

@ -0,0 +1,47 @@
/*
* Copyright 2021 Richard Schubert. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*
* AMD FidelityFX Super Resolution 1.0 (FSR)
* Based on https://github.com/GPUOpen-Effects/FidelityFX-FSR/blob/master/sample/
*/
#ifndef __FSR_H__
#define __FSR_H__
#include <bgfx/bgfx.h>
class Fsr
{
public:
struct Config
{
bool m_applyFsr{ true };
bool m_applyFsrRcas{ true };
float m_superSamplingFactor{ 2.0f };
float m_rcasAttenuation{ 0.2f };
bool m_fsr16Bit{ false };
};
Config m_config;
Fsr();
~Fsr();
void init(uint32_t _width, uint32_t _height);
void destroy();
void resize(uint32_t _width, uint32_t _height);
bgfx::ViewId computeFsr(bgfx::ViewId _pass, bgfx::TextureHandle _colorTexture);
bgfx::TextureHandle getResultTexture() const;
bool supports16BitPrecision() const;
private:
void updateUniforms();
struct FsrResources *m_resources;
bool m_support16BitPrecision{ false };
};
#endif // __FSR_H__

10
examples/46-fsr/makefile Normal file
View File

@ -0,0 +1,10 @@
#
# Copyright 2011-2021 Branimir Karadzic. All rights reserved.
# License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
#
BGFX_DIR=../..
RUNTIME_DIR=$(BGFX_DIR)/examples/runtime
BUILD_DIR=../../.build
include $(BGFX_DIR)/scripts/shader.mk

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 KiB

View File

@ -0,0 +1,9 @@
vec4 a_position : POSITION;
vec2 a_texcoord0 : TEXCOORD0;
vec3 a_normal : NORMAL;
vec2 v_texcoord0 : TEXCOORD0;
vec4 v_texcoord1 : TEXCOORD1;
vec4 v_texcoord2 : TEXCOORD2;
vec4 v_texcoord3 : TEXCOORD3;
vec3 v_normal : NORMAL = vec3(0.0, 0.0, 1.0);

View File

@ -0,0 +1,34 @@
$input a_position, a_normal
$output v_normal, v_texcoord0, v_texcoord1, v_texcoord2
/*
* Copyright 2021 elven cache. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "../common/common.sh"
void main()
{
// Calculate vertex position
vec3 pos = a_position.xyz;
gl_Position = mul(u_modelViewProj, vec4(pos, 1.0));
vec3 wsPos = mul(u_model[0], vec4(pos, 1.0)).xyz;
// Calculate normal, unpack
vec3 osNormal = a_normal.xyz * 2.0 - 1.0;
// Transform normal into world space
vec3 wsNormal = mul(u_model[0], vec4(osNormal, 0.0)).xyz;
v_normal.xyz = normalize(wsNormal);
v_texcoord0 = a_position.xy * vec2_splat(0.5); // the used mesh does not provide texture coordinates
// Store world space view vector in extra texCoord attribute
vec3 wsCamPos = mul(u_invView, vec4(0.0, 0.0, 0.0, 1.0)).xyz;
vec3 view = normalize(wsCamPos - wsPos);
v_texcoord1 = vec4(wsPos, 1.0);
v_texcoord2 = vec4(view, 1.0);
}

View File

@ -0,0 +1,10 @@
$input a_position, a_texcoord0
$output v_texcoord0
#include "../common/common.sh"
void main()
{
gl_Position = mul(u_modelViewProj, vec4(a_position.xyz, 1.0));
v_texcoord0 = a_texcoord0;
}