bgfx/examples/21-deferred/deferred.cpp

972 lines
28 KiB
C++
Raw Normal View History

2014-05-22 07:33:12 +04:00
/*
2024-01-14 12:56:36 +03:00
* Copyright 2011-2024 Branimir Karadzic. All rights reserved.
2022-01-15 22:59:06 +03:00
* License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
2014-05-22 07:33:12 +04:00
*/
2021-10-16 20:22:47 +03:00
#include <bx/bounds.h>
2014-05-22 07:33:12 +04:00
#include "common.h"
#include "bgfx_utils.h"
#include "imgui/imgui.h"
#include "camera.h"
2017-06-26 07:44:04 +03:00
namespace
{
2019-02-19 00:40:41 +03:00
constexpr bgfx::ViewId kRenderPassGeometry = 0;
constexpr bgfx::ViewId kRenderPassClearUav = 1;
constexpr bgfx::ViewId kRenderPassLight = 2;
constexpr bgfx::ViewId kRenderPassCombine = 3;
constexpr bgfx::ViewId kRenderPassDebugLights = 4;
constexpr bgfx::ViewId kRenderPassDebugGBuffer = 5;
2014-05-22 07:33:12 +04:00
struct PosNormalTangentTexcoordVertex
{
float m_x;
float m_y;
float m_z;
uint32_t m_normal;
uint32_t m_tangent;
int16_t m_u;
int16_t m_v;
static void init()
{
2019-08-17 20:35:21 +03:00
ms_layout
2014-05-22 07:33:12 +04:00
.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
.add(bgfx::Attrib::Tangent, 4, bgfx::AttribType::Uint8, true, true)
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Int16, true, true)
.end();
}
2019-08-17 20:35:21 +03:00
static bgfx::VertexLayout ms_layout;
2014-05-22 07:33:12 +04:00
};
2019-08-17 20:35:21 +03:00
bgfx::VertexLayout PosNormalTangentTexcoordVertex::ms_layout;
2014-05-22 07:33:12 +04:00
struct PosTexCoord0Vertex
{
float m_x;
float m_y;
float m_z;
float m_u;
float m_v;
static void init()
{
2019-08-17 20:35:21 +03:00
ms_layout
2014-05-22 07:33:12 +04:00
.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
.end();
}
2019-08-17 20:35:21 +03:00
static bgfx::VertexLayout ms_layout;
2014-05-22 07:33:12 +04:00
};
2019-08-17 20:35:21 +03:00
bgfx::VertexLayout PosTexCoord0Vertex::ms_layout;
2014-05-22 07:33:12 +04:00
struct DebugVertex
{
float m_x;
float m_y;
float m_z;
uint32_t m_abgr;
static void init()
{
2019-08-17 20:35:21 +03:00
ms_layout
2014-05-22 07:33:12 +04:00
.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
.end();
}
2019-08-17 20:35:21 +03:00
static bgfx::VertexLayout ms_layout;
2014-05-22 07:33:12 +04:00
};
2019-08-17 20:35:21 +03:00
bgfx::VertexLayout DebugVertex::ms_layout;
2014-05-22 07:33:12 +04:00
static PosNormalTangentTexcoordVertex s_cubeVertices[24] =
{
2017-04-01 07:01:08 +03:00
{-1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0, 0 },
{ 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0 },
{-1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0, 0x7fff },
{ 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0x7fff },
{-1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0, 0 },
{ 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0 },
{-1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0, 0x7fff },
{ 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0x7fff },
{-1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0, 0 },
{ 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0 },
{-1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0, 0x7fff },
{ 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0x7fff },
{-1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0, 0 },
{ 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0 },
{-1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0, 0x7fff },
{ 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0x7fff },
{ 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0, 0 },
{ 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
{ 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
{ 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
{-1.0f, -1.0f, 1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0, 0 },
{-1.0f, 1.0f, 1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
{-1.0f, -1.0f, -1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
{-1.0f, 1.0f, -1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
2014-05-22 07:33:12 +04:00
};
static const uint16_t s_cubeIndices[36] =
{
0, 2, 1,
1, 2, 3,
4, 5, 6,
5, 7, 6,
8, 10, 9,
9, 10, 11,
2014-09-03 11:29:54 +04:00
12, 13, 14,
2014-05-22 07:33:12 +04:00
13, 15, 14,
16, 18, 17,
17, 18, 19,
20, 21, 22,
21, 23, 22,
};
2023-11-04 07:15:42 +03:00
void screenSpaceQuad(bool _originBottomLeft, float _width = 1.0f, float _height = 1.0f)
2014-05-22 07:33:12 +04:00
{
2019-08-17 20:35:21 +03:00
if (3 == bgfx::getAvailTransientVertexBuffer(3, PosTexCoord0Vertex::ms_layout) )
2014-05-22 07:33:12 +04:00
{
bgfx::TransientVertexBuffer vb;
2019-08-17 20:35:21 +03:00
bgfx::allocTransientVertexBuffer(&vb, 3, PosTexCoord0Vertex::ms_layout);
2014-05-22 07:33:12 +04:00
PosTexCoord0Vertex* vertex = (PosTexCoord0Vertex*)vb.data;
const float minx = -_width;
const float maxx = _width;
const float miny = 0.0f;
const float maxy = _height*2.0f;
2023-11-04 07:15:42 +03:00
const float minu = -1.0f;
const float maxu = 1.0f;
2014-05-22 07:33:12 +04:00
const float zz = 0.0f;
2023-11-04 07:15:42 +03:00
float minv = 0.0f;
float maxv = 2.0f;
2014-05-22 07:33:12 +04:00
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;
2017-05-14 21:48:59 +03:00
bgfx::setVertexBuffer(0, &vb);
2014-05-22 07:33:12 +04:00
}
}
2016-03-07 02:29:22 +03:00
class ExampleDeferred : public entry::AppI
2014-05-22 07:33:12 +04:00
{
2017-06-26 07:44:04 +03:00
public:
2019-08-18 00:40:38 +03:00
ExampleDeferred(const char* _name, const char* _description, const char* _url)
: entry::AppI(_name, _description, _url)
2017-06-26 07:44:04 +03:00
{
}
2017-07-15 10:17:29 +03:00
void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
2014-05-22 07:33:12 +04:00
{
2015-10-24 06:52:22 +03:00
Args args(_argc, _argv);
2016-04-02 08:48:57 +03:00
2017-06-30 08:23:18 +03:00
m_width = _width;
m_height = _height;
m_debug = BGFX_DEBUG_TEXT;
2015-10-20 01:09:56 +03:00
m_reset = BGFX_RESET_VSYNC;
2014-05-22 07:33:12 +04:00
bgfx::Init init;
init.type = args.m_type;
init.vendorId = args.m_pciId;
init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
init.platformData.ndt = entry::getNativeDisplayHandle();
2023-11-04 07:36:00 +03:00
init.platformData.type = entry::getNativeWindowHandleType();
init.resolution.width = m_width;
init.resolution.height = m_height;
init.resolution.reset = m_reset;
bgfx::init(init);
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
// Enable m_debug text.
bgfx::setDebug(m_debug);
2014-05-22 07:33:12 +04:00
2015-09-17 03:21:28 +03:00
// Set palette color for index 0
bgfx::setPaletteColor(0, UINT32_C(0x00000000) );
2014-05-22 07:33:12 +04:00
2015-09-17 03:21:28 +03:00
// Set palette color for index 1
bgfx::setPaletteColor(1, UINT32_C(0x303030ff) );
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
// Set geometry pass view clear state.
2019-02-19 00:40:41 +03:00
bgfx::setViewClear(kRenderPassGeometry
2015-08-22 08:52:37 +03:00
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
, 1.0f
, 0
, 1
, 0
2015-08-22 08:52:37 +03:00
);
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
// Set light pass view clear state.
2019-02-19 00:40:41 +03:00
bgfx::setViewClear(kRenderPassLight
2015-08-22 08:52:37 +03:00
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
, 1.0f
, 0
, 0
2014-05-22 07:33:12 +04:00
);
2015-08-22 08:52:37 +03:00
// Create vertex stream declaration.
PosNormalTangentTexcoordVertex::init();
PosTexCoord0Vertex::init();
DebugVertex::init();
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
calcTangents(s_cubeVertices
, BX_COUNTOF(s_cubeVertices)
2019-08-17 20:35:21 +03:00
, PosNormalTangentTexcoordVertex::ms_layout
2015-08-22 08:52:37 +03:00
, s_cubeIndices
, BX_COUNTOF(s_cubeIndices)
);
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
// Create static vertex buffer.
m_vbh = bgfx::createVertexBuffer(
bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
2019-08-17 20:35:21 +03:00
, PosNormalTangentTexcoordVertex::ms_layout
2015-08-22 08:52:37 +03:00
);
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
// Create static index buffer.
m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
// Create texture sampler uniforms.
s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
s_texNormal = bgfx::createUniform("s_texNormal", bgfx::UniformType::Sampler);
2014-05-22 07:33:12 +04:00
s_albedo = bgfx::createUniform("s_albedo", bgfx::UniformType::Sampler);
s_normal = bgfx::createUniform("s_normal", bgfx::UniformType::Sampler);
s_depth = bgfx::createUniform("s_depth", bgfx::UniformType::Sampler);
s_light = bgfx::createUniform("s_light", bgfx::UniformType::Sampler);
2015-01-23 08:01:09 +03:00
2015-08-22 08:52:37 +03:00
u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Vec4);
u_lightRgbInnerR = bgfx::createUniform("u_lightRgbInnerR", bgfx::UniformType::Vec4);
u_layer = bgfx::createUniform("u_layer", bgfx::UniformType::Vec4);
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
// Create program from shaders.
m_geomProgram = loadProgram("vs_deferred_geom", "fs_deferred_geom");
m_lightProgram = loadProgram("vs_deferred_light", "fs_deferred_light");
m_combineProgram = loadProgram("vs_deferred_combine", "fs_deferred_combine");
m_debugProgram = loadProgram("vs_deferred_debug", "fs_deferred_debug");
m_lineProgram = loadProgram("vs_deferred_debug_line", "fs_deferred_debug_line");
2014-05-22 07:33:12 +04:00
m_useTArray = false;
2019-02-17 18:50:26 +03:00
m_useUav = false;
if (0 != (BGFX_CAPS_TEXTURE_2D_ARRAY & bgfx::getCaps()->supported) )
{
m_lightTaProgram = loadProgram("vs_deferred_light", "fs_deferred_light_ta");
m_combineTaProgram = loadProgram("vs_deferred_combine", "fs_deferred_combine_ta");
m_debugTaProgram = loadProgram("vs_deferred_debug", "fs_deferred_debug_ta");
}
else
{
m_lightTaProgram = BGFX_INVALID_HANDLE;
m_combineTaProgram = BGFX_INVALID_HANDLE;
m_debugTaProgram = BGFX_INVALID_HANDLE;
}
if (0 != (BGFX_CAPS_IMAGE_RW & bgfx::getCaps()->supported)
&& 0 != (BGFX_CAPS_FORMAT_TEXTURE_IMAGE_READ & bgfx::getCaps()->formats[bgfx::TextureFormat::RGBA8])
&& 0 != (BGFX_CAPS_FORMAT_TEXTURE_IMAGE_WRITE & bgfx::getCaps()->formats[bgfx::TextureFormat::RGBA8]) )
2019-02-17 18:50:26 +03:00
{
m_clearUavProgram = loadProgram("vs_deferred_light", "fs_deferred_clear_uav");
m_lightUavProgram = loadProgram("vs_deferred_light", "fs_deferred_light_uav");
2019-02-17 18:50:26 +03:00
}
else
{
m_lightUavProgram = BGFX_INVALID_HANDLE;
m_clearUavProgram = BGFX_INVALID_HANDLE;
}
2015-08-22 08:52:37 +03:00
// Load diffuse texture.
2016-04-22 08:12:35 +03:00
m_textureColor = loadTexture("textures/fieldstone-rgba.dds");
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
// Load normal texture.
2016-04-22 08:12:35 +03:00
m_textureNormal = loadTexture("textures/fieldstone-n.dds");
2014-05-22 07:33:12 +04:00
m_lightBufferTex.idx = bgfx::kInvalidHandle;
2017-06-10 07:57:08 +03:00
m_gbufferTex[0].idx = bgfx::kInvalidHandle;
m_gbufferTex[1].idx = bgfx::kInvalidHandle;
m_gbufferTex[2].idx = bgfx::kInvalidHandle;
m_gbuffer.idx = bgfx::kInvalidHandle;
m_lightBuffer.idx = bgfx::kInvalidHandle;
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
// Imgui.
imguiCreate();
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
m_timeOffset = bx::getHPCounter();
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
// Get renderer capabilities info.
m_caps = bgfx::getCaps();
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
m_oldWidth = 0;
m_oldHeight = 0;
m_oldReset = m_reset;
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
m_scrollArea = 0;
m_numLights = 512;
m_lightAnimationSpeed = 0.3f;
m_animateMesh = true;
m_showScissorRects = false;
m_showGBuffer = true;
cameraCreate();
2018-12-22 08:05:26 +03:00
cameraSetPosition({ 0.0f, 0.0f, -15.0f });
2015-08-22 08:52:37 +03:00
cameraSetVerticalAngle(0.0f);
}
2017-07-15 10:17:29 +03:00
virtual int shutdown() override
2015-08-22 08:52:37 +03:00
{
// Cleanup.
cameraDestroy();
imguiDestroy();
if (bgfx::isValid(m_gbuffer) )
{
bgfx::destroy(m_gbuffer);
}
if (bgfx::isValid(m_lightBuffer) )
{
bgfx::destroy(m_lightBuffer);
2015-08-22 08:52:37 +03:00
}
if (bgfx::isValid(m_lightBufferTex) )
{
bgfx::destroy(m_lightBufferTex);
}
bgfx::destroy(m_ibh);
bgfx::destroy(m_vbh);
2015-08-22 08:52:37 +03:00
bgfx::destroy(m_geomProgram);
bgfx::destroy(m_lightProgram);
if (bgfx::isValid(m_lightTaProgram) )
{
bgfx::destroy(m_lightTaProgram);
}
2019-02-17 19:56:26 +03:00
if (bgfx::isValid(m_lightUavProgram) )
{
bgfx::destroy(m_lightUavProgram);
bgfx::destroy(m_clearUavProgram);
}
bgfx::destroy(m_combineProgram);
if (bgfx::isValid(m_combineTaProgram) )
{
bgfx::destroy(m_combineTaProgram);
}
bgfx::destroy(m_debugProgram);
if (bgfx::isValid(m_debugTaProgram) )
{
bgfx::destroy(m_debugTaProgram);
}
bgfx::destroy(m_lineProgram);
2014-05-22 07:33:12 +04:00
bgfx::destroy(m_textureColor);
bgfx::destroy(m_textureNormal);
bgfx::destroy(s_texColor);
bgfx::destroy(s_texNormal);
2014-05-22 07:33:12 +04:00
bgfx::destroy(s_albedo);
bgfx::destroy(s_normal);
bgfx::destroy(s_depth);
bgfx::destroy(s_light);
2015-08-22 08:52:37 +03:00
2021-10-29 04:43:09 +03:00
bgfx::destroy(u_layer);
bgfx::destroy(u_lightPosRadius);
bgfx::destroy(u_lightRgbInnerR);
bgfx::destroy(u_mtx);
2015-08-22 08:52:37 +03:00
// Shutdown bgfx.
bgfx::shutdown();
return 0;
}
2017-07-15 10:17:29 +03:00
bool update() override
2015-08-22 08:52:37 +03:00
{
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
{
2017-06-26 07:44:04 +03:00
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)
);
2017-06-26 07:44:04 +03:00
2017-06-30 09:19:20 +03:00
showExampleDialog(this);
2017-06-26 07:44:04 +03:00
2015-08-22 08:52:37 +03:00
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);
float time = (float)( (now-m_timeOffset)/freq);
ImGui::SetNextWindowPos(
ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
, ImGuiCond_FirstUseEver
);
ImGui::SetNextWindowSize(
ImVec2(m_width / 5.0f, m_height / 3.0f)
, ImGuiCond_FirstUseEver
);
ImGui::Begin("Settings", NULL, 0);
ImGui::SliderInt("Num lights", &m_numLights, 1, 2048);
ImGui::Checkbox("Show G-Buffer.", &m_showGBuffer);
ImGui::Checkbox("Show light scissor.", &m_showScissorRects);
if (bgfx::isValid(m_lightTaProgram))
{
ImGui::Checkbox("Use texture array frame buffer.", &m_useTArray);
}
else
{
ImGui::Text("Texture array frame buffer is not supported.");
}
if (bgfx::isValid(m_lightUavProgram))
{
ImGui::Checkbox("Use UAV.", &m_useUav);
}
else
{
ImGui::Text("UAV is not supported.");
}
ImGui::Checkbox("Animate mesh.", &m_animateMesh);
ImGui::SliderFloat("Anim.speed", &m_lightAnimationSpeed, 0.0f, 0.4f);
ImGui::End();
2016-09-24 10:29:59 +03:00
if (2 > m_caps->limits.maxFBAttachments)
2014-05-22 07:33:12 +04:00
{
2015-08-22 08:52:37 +03:00
// When multiple render targets (MRT) is not supported by GPU,
// implement alternative code path that doesn't use MRT.
bool blink = uint32_t(time*3.0f)&1;
bgfx::dbgTextPrintf(0, 0, blink ? 0x4f : 0x04, " MRT not supported by GPU. ");
2015-08-22 08:52:37 +03:00
// Set view 0 default viewport.
2017-03-13 04:25:23 +03:00
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
2015-08-22 08:52:37 +03:00
// This dummy draw call is here to make sure that view 0 is cleared
// if no other draw calls are submitted to view 0.
bgfx::touch(0);
}
else
{
if (m_oldWidth != m_width
|| m_oldHeight != m_height
|| m_oldReset != m_reset
|| m_oldUseTArray != m_useTArray
2019-02-17 18:50:26 +03:00
|| m_oldUseUav != m_useUav
2015-10-20 01:09:56 +03:00
|| !bgfx::isValid(m_gbuffer) )
2014-05-22 07:33:12 +04:00
{
2015-08-22 08:52:37 +03:00
// Recreate variable size render targets when resolution changes.
m_oldWidth = m_width;
m_oldHeight = m_height;
m_oldReset = m_reset;
m_oldUseTArray = m_useTArray;
2019-02-17 18:50:26 +03:00
m_oldUseUav = m_useUav;
2015-08-22 08:52:37 +03:00
if (bgfx::isValid(m_gbuffer) )
2014-05-22 07:33:12 +04:00
{
bgfx::destroy(m_gbuffer);
m_gbufferTex[0].idx = bgfx::kInvalidHandle;
m_gbufferTex[1].idx = bgfx::kInvalidHandle;
m_gbufferTex[2].idx = bgfx::kInvalidHandle;
2014-05-22 07:33:12 +04:00
}
2015-08-22 08:52:37 +03:00
const uint64_t tsFlags = 0
| BGFX_SAMPLER_MIN_POINT
| BGFX_SAMPLER_MAG_POINT
| BGFX_SAMPLER_MIP_POINT
| BGFX_SAMPLER_U_CLAMP
| BGFX_SAMPLER_V_CLAMP
2015-08-22 08:52:37 +03:00
;
2019-02-17 18:50:26 +03:00
bgfx::Attachment gbufferAt[3];
2019-02-17 20:28:02 +03:00
if (m_useTArray)
{
2019-02-17 18:50:26 +03:00
m_gbufferTex[0] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 2, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT | tsFlags);
gbufferAt[0].init(m_gbufferTex[0], bgfx::Access::Write, 0);
gbufferAt[1].init(m_gbufferTex[0], bgfx::Access::Write, 1);
}
else
{
2019-02-17 18:50:26 +03:00
m_gbufferTex[0] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT | tsFlags);
m_gbufferTex[1] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT | tsFlags);
gbufferAt[0].init(m_gbufferTex[0]);
gbufferAt[1].init(m_gbufferTex[1]);
}
bgfx::TextureFormat::Enum depthFormat =
bgfx::isTextureValid(0, false, 1, bgfx::TextureFormat::D32F, BGFX_TEXTURE_RT | tsFlags)
? bgfx::TextureFormat::D32F
: bgfx::TextureFormat::D24
;
m_gbufferTex[2] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, depthFormat, BGFX_TEXTURE_RT | tsFlags);
2019-02-17 18:50:26 +03:00
gbufferAt[2].init(m_gbufferTex[2]);
2019-02-17 18:50:26 +03:00
m_gbuffer = bgfx::createFrameBuffer(BX_COUNTOF(gbufferAt), gbufferAt, true);
2015-08-22 08:52:37 +03:00
2019-02-17 20:28:02 +03:00
if (bgfx::isValid(m_lightBuffer) )
2014-05-22 07:33:12 +04:00
{
bgfx::destroy(m_lightBuffer);
m_lightBuffer.idx = bgfx::kInvalidHandle;
2014-05-22 07:33:12 +04:00
}
if (bgfx::isValid(m_lightBufferTex))
2019-02-17 18:50:26 +03:00
{
bgfx::destroy(m_lightBufferTex);
m_lightBufferTex.idx = bgfx::kInvalidHandle;
}
2019-02-17 18:50:26 +03:00
if (m_useUav)
{
m_lightBufferTex = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::RGBA8, BGFX_TEXTURE_COMPUTE_WRITE | tsFlags);
2019-02-17 18:50:26 +03:00
}
else
{
m_lightBufferTex = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT | tsFlags);
m_lightBuffer = bgfx::createFrameBuffer(1, &m_lightBufferTex, true);
}
2015-08-22 08:52:37 +03:00
}
// Update camera.
cameraUpdate(deltaTime, m_mouseState, ImGui::MouseOverArea() );
2015-08-22 08:52:37 +03:00
float view[16];
cameraGetViewMtx(view);
// Setup views
float vp[16];
float invMvp[16];
2014-05-22 07:33:12 +04:00
{
bgfx::setViewRect(kRenderPassGeometry, 0, 0, uint16_t(m_width), uint16_t(m_height) );
bgfx::setViewRect(kRenderPassClearUav, 0, 0, uint16_t(m_width), uint16_t(m_height) );
bgfx::setViewRect(kRenderPassLight, 0, 0, uint16_t(m_width), uint16_t(m_height) );
bgfx::setViewRect(kRenderPassCombine, 0, 0, uint16_t(m_width), uint16_t(m_height) );
2019-02-19 00:40:41 +03:00
bgfx::setViewRect(kRenderPassDebugLights, 0, 0, uint16_t(m_width), uint16_t(m_height) );
bgfx::setViewRect(kRenderPassDebugGBuffer, 0, 0, uint16_t(m_width), uint16_t(m_height) );
2015-08-22 08:52:37 +03:00
if (!m_useUav)
{
bgfx::setViewFrameBuffer(kRenderPassLight, m_lightBuffer);
}
else
{
bgfx::setViewFrameBuffer(kRenderPassLight, BGFX_INVALID_HANDLE);
}
2015-08-22 08:52:37 +03:00
float proj[16];
2016-04-02 08:48:57 +03:00
bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, m_caps->homogeneousDepth);
2015-08-22 08:52:37 +03:00
2019-02-19 00:40:41 +03:00
bgfx::setViewFrameBuffer(kRenderPassGeometry, m_gbuffer);
bgfx::setViewTransform(kRenderPassGeometry, view, proj);
2015-08-22 08:52:37 +03:00
bx::mtxMul(vp, view, proj);
bx::mtxInverse(invMvp, vp);
2017-06-13 08:43:07 +03:00
const bgfx::Caps* caps = bgfx::getCaps();
bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f, 0.0f, caps->homogeneousDepth);
bgfx::setViewTransform(kRenderPassClearUav, NULL, proj);
bgfx::setViewTransform(kRenderPassLight, NULL, proj);
bgfx::setViewTransform(kRenderPassCombine, NULL, proj);
2015-08-22 08:52:37 +03:00
const float aspectRatio = float(m_height)/float(m_width);
const float size = 10.0f;
2017-06-13 08:43:07 +03:00
bx::mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth);
2019-02-19 00:40:41 +03:00
bgfx::setViewTransform(kRenderPassDebugGBuffer, NULL, proj);
2015-08-22 08:52:37 +03:00
2017-06-13 08:43:07 +03:00
bx::mtxOrtho(proj, 0.0f, (float)m_width, 0.0f, (float)m_height, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth);
2019-02-19 00:40:41 +03:00
bgfx::setViewTransform(kRenderPassDebugLights, NULL, proj);
2014-05-22 07:33:12 +04:00
}
2015-08-22 08:52:37 +03:00
const uint32_t dim = 11;
const float offset = (float(dim-1) * 3.0f) * 0.5f;
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
// Draw into geometry pass.
for (uint32_t yy = 0; yy < dim; ++yy)
{
for (uint32_t xx = 0; xx < dim; ++xx)
2014-05-22 07:33:12 +04:00
{
2015-08-22 08:52:37 +03:00
float mtx[16];
if (m_animateMesh)
2014-05-22 07:33:12 +04:00
{
2015-08-22 08:52:37 +03:00
bx::mtxRotateXY(mtx, time*1.023f + xx*0.21f, time*0.03f + yy*0.37f);
}
else
{
bx::mtxIdentity(mtx);
}
mtx[12] = -offset + float(xx)*3.0f;
mtx[13] = -offset + float(yy)*3.0f;
mtx[14] = 0.0f;
// Set transform for draw call.
bgfx::setTransform(mtx);
// Set vertex and index buffer.
2017-05-14 21:48:59 +03:00
bgfx::setVertexBuffer(0, m_vbh);
2015-08-22 08:52:37 +03:00
bgfx::setIndexBuffer(m_ibh);
// Bind textures.
bgfx::setTexture(0, s_texColor, m_textureColor);
bgfx::setTexture(1, s_texNormal, m_textureNormal);
// Set render states.
bgfx::setState(0
2019-02-14 08:43:00 +03:00
| BGFX_STATE_WRITE_RGB
| BGFX_STATE_WRITE_A
| BGFX_STATE_WRITE_Z
| BGFX_STATE_DEPTH_TEST_LESS
| BGFX_STATE_MSAA
);
2015-08-22 08:52:37 +03:00
// Submit primitive for rendering to view 0.
2019-02-19 00:40:41 +03:00
bgfx::submit(kRenderPassGeometry, m_geomProgram);
2014-05-22 07:33:12 +04:00
}
2015-08-22 08:52:37 +03:00
}
2019-02-17 18:50:26 +03:00
// Clear UAV texture
2019-02-17 20:28:02 +03:00
if (m_useUav)
2019-02-17 18:50:26 +03:00
{
2023-11-04 07:15:42 +03:00
screenSpaceQuad(m_caps->originBottomLeft);
bgfx::setViewFrameBuffer(kRenderPassClearUav, BGFX_INVALID_HANDLE);
bgfx::setState(0);
bgfx::setImage(2, m_lightBufferTex, 0, bgfx::Access::ReadWrite, bgfx::TextureFormat::RGBA8);
bgfx::submit(kRenderPassClearUav, m_clearUavProgram);
2019-02-17 18:50:26 +03:00
}
2015-08-22 08:52:37 +03:00
// Draw lights into light buffer.
for (int32_t light = 0; light < m_numLights; ++light)
{
2021-10-16 20:22:47 +03:00
bx::Sphere lightPosRadius;
2015-08-22 08:52:37 +03:00
2018-01-14 02:33:50 +03:00
float lightTime = time * m_lightAnimationSpeed * (bx::sin(light/float(m_numLights) * bx::kPiHalf ) * 0.5f + 0.5f);
2019-02-04 06:47:33 +03:00
lightPosRadius.center.x = bx::sin( ( (lightTime + light*0.47f) + bx::kPiHalf*1.37f ) )*offset;
lightPosRadius.center.y = bx::cos( ( (lightTime + light*0.69f) + bx::kPiHalf*1.49f ) )*offset;
lightPosRadius.center.z = bx::sin( ( (lightTime + light*0.37f) + bx::kPiHalf*1.57f ) )*2.0f;
lightPosRadius.radius = 2.0f;
2014-05-22 07:33:12 +04:00
2021-10-16 20:22:47 +03:00
bx::Aabb aabb;
2016-12-06 04:06:13 +03:00
toAabb(aabb, lightPosRadius);
2015-08-22 08:52:37 +03:00
2019-01-04 02:16:29 +03:00
const bx::Vec3 box[8] =
2015-01-23 08:01:09 +03:00
{
2019-02-04 06:47:33 +03:00
{ aabb.min.x, aabb.min.y, aabb.min.z },
{ aabb.min.x, aabb.min.y, aabb.max.z },
{ aabb.min.x, aabb.max.y, aabb.min.z },
{ aabb.min.x, aabb.max.y, aabb.max.z },
{ aabb.max.x, aabb.min.y, aabb.min.z },
{ aabb.max.x, aabb.min.y, aabb.max.z },
{ aabb.max.x, aabb.max.y, aabb.min.z },
{ aabb.max.x, aabb.max.y, aabb.max.z },
2014-05-22 07:33:12 +04:00
};
2019-01-04 02:16:29 +03:00
bx::Vec3 xyz = bx::mulH(box[0], vp);
bx::Vec3 min = xyz;
bx::Vec3 max = xyz;
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
for (uint32_t ii = 1; ii < 8; ++ii)
{
2019-01-04 02:16:29 +03:00
xyz = bx::mulH(box[ii], vp);
min = bx::min(min, xyz);
max = bx::max(max, xyz);
2015-08-22 08:52:37 +03:00
}
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
// Cull light if it's fully behind camera.
2019-01-04 02:16:29 +03:00
if (max.z >= 0.0f)
2015-08-22 08:52:37 +03:00
{
2019-01-04 02:16:29 +03:00
const float x0 = bx::clamp( (min.x * 0.5f + 0.5f) * m_width, 0.0f, (float)m_width);
const float y0 = bx::clamp( (min.y * 0.5f + 0.5f) * m_height, 0.0f, (float)m_height);
const float x1 = bx::clamp( (max.x * 0.5f + 0.5f) * m_width, 0.0f, (float)m_width);
const float y1 = bx::clamp( (max.y * 0.5f + 0.5f) * m_height, 0.0f, (float)m_height);
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
if (m_showScissorRects)
{
bgfx::TransientVertexBuffer tvb;
bgfx::TransientIndexBuffer tib;
2019-08-17 20:35:21 +03:00
if (bgfx::allocTransientBuffers(&tvb, DebugVertex::ms_layout, 4, &tib, 8) )
2015-08-22 08:52:37 +03:00
{
uint32_t abgr = 0x8000ff00;
DebugVertex* vertex = (DebugVertex*)tvb.data;
vertex->m_x = x0;
vertex->m_y = y0;
vertex->m_z = 0.0f;
vertex->m_abgr = abgr;
++vertex;
vertex->m_x = x1;
vertex->m_y = y0;
vertex->m_z = 0.0f;
vertex->m_abgr = abgr;
++vertex;
vertex->m_x = x1;
vertex->m_y = y1;
vertex->m_z = 0.0f;
vertex->m_abgr = abgr;
++vertex;
vertex->m_x = x0;
vertex->m_y = y1;
vertex->m_z = 0.0f;
vertex->m_abgr = abgr;
uint16_t* indices = (uint16_t*)tib.data;
*indices++ = 0;
*indices++ = 1;
*indices++ = 1;
*indices++ = 2;
*indices++ = 2;
*indices++ = 3;
*indices++ = 3;
*indices++ = 0;
2017-05-14 21:48:59 +03:00
bgfx::setVertexBuffer(0, &tvb);
2015-08-22 08:52:37 +03:00
bgfx::setIndexBuffer(&tib);
bgfx::setState(0
2019-02-14 08:43:00 +03:00
| BGFX_STATE_WRITE_RGB
| BGFX_STATE_PT_LINES
| BGFX_STATE_BLEND_ALPHA
);
2019-02-19 00:40:41 +03:00
bgfx::submit(kRenderPassDebugLights, m_lineProgram);
2015-08-22 08:52:37 +03:00
}
}
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
uint8_t val = light&7;
float lightRgbInnerR[4] =
{
val & 0x1 ? 1.0f : 0.25f,
val & 0x2 ? 1.0f : 0.25f,
val & 0x4 ? 1.0f : 0.25f,
0.8f,
};
// Draw light.
bgfx::setUniform(u_lightPosRadius, &lightPosRadius);
bgfx::setUniform(u_lightRgbInnerR, lightRgbInnerR);
bgfx::setUniform(u_mtx, invMvp);
const uint16_t scissorHeight = uint16_t(y1-y0);
2017-03-13 04:25:23 +03:00
bgfx::setScissor(uint16_t(x0), uint16_t(m_height-scissorHeight-y0), uint16_t(x1-x0), uint16_t(scissorHeight) );
bgfx::setTexture(0, s_normal, bgfx::getTexture(m_gbuffer, 1) );
bgfx::setTexture(1, s_depth, bgfx::getTexture(m_gbuffer, 2) );
2015-08-22 08:52:37 +03:00
bgfx::setState(0
2019-02-14 08:43:00 +03:00
| BGFX_STATE_WRITE_RGB
| BGFX_STATE_WRITE_A
| BGFX_STATE_BLEND_ADD
);
2023-11-04 07:15:42 +03:00
screenSpaceQuad(m_caps->originBottomLeft);
2019-02-19 00:40:41 +03:00
if (bgfx::isValid(m_lightTaProgram)
&& m_useTArray)
2019-02-17 19:56:26 +03:00
{
2019-02-19 00:40:41 +03:00
bgfx::submit(kRenderPassLight, m_lightTaProgram);
2019-02-17 19:56:26 +03:00
}
2019-02-19 00:40:41 +03:00
else if (bgfx::isValid(m_lightUavProgram)
&& m_useUav)
2019-02-17 19:56:26 +03:00
{
bgfx::setViewFrameBuffer(kRenderPassLight, BGFX_INVALID_HANDLE);
bgfx::setState(0);
bgfx::setImage(3, m_lightBufferTex, 0, bgfx::Access::ReadWrite, bgfx::TextureFormat::RGBA8);
2019-02-19 00:40:41 +03:00
bgfx::submit(kRenderPassLight, m_lightUavProgram);
2019-02-17 19:56:26 +03:00
}
2019-02-17 18:50:26 +03:00
else
2019-02-17 19:56:26 +03:00
{
2019-02-19 00:40:41 +03:00
bgfx::submit(kRenderPassLight, m_lightProgram);
2019-02-17 19:56:26 +03:00
}
2015-08-22 08:52:37 +03:00
}
2014-05-22 07:33:12 +04:00
}
2015-08-22 08:52:37 +03:00
// Combine color and light buffers.
bgfx::setTexture(0, s_albedo, bgfx::getTexture(m_gbuffer, 0) );
2019-02-17 18:50:26 +03:00
bgfx::setTexture(1, s_light, m_lightBufferTex);
2015-08-22 08:52:37 +03:00
bgfx::setState(0
2019-02-14 08:43:00 +03:00
| BGFX_STATE_WRITE_RGB
| BGFX_STATE_WRITE_A
);
2023-11-04 07:15:42 +03:00
screenSpaceQuad(m_caps->originBottomLeft);
if (bgfx::isValid(m_lightTaProgram)
&& m_useTArray)
{
bgfx::submit(kRenderPassCombine, m_combineTaProgram);
}
else
{
bgfx::submit(kRenderPassCombine, m_combineProgram);
}
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
if (m_showGBuffer)
{
const float aspectRatio = float(m_width)/float(m_height);
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
// Draw m_debug m_gbuffer.
for (uint8_t ii = 0; ii < BX_COUNTOF(m_gbufferTex); ++ii)
2015-08-22 08:52:37 +03:00
{
float mtx[16];
bx::mtxSRT(mtx
, aspectRatio, 1.0f, 1.0f
, 0.0f, 0.0f, 0.0f
, -7.9f - BX_COUNTOF(m_gbufferTex)*0.1f*0.5f + ii*2.1f*aspectRatio, 4.0f, 0.0f
);
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
bgfx::setTransform(mtx);
2017-05-14 21:48:59 +03:00
bgfx::setVertexBuffer(0, m_vbh);
2015-08-22 08:52:37 +03:00
bgfx::setIndexBuffer(m_ibh, 0, 6);
bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_gbuffer, ii) );
bgfx::setState(BGFX_STATE_WRITE_RGB);
if (ii != BX_COUNTOF(m_gbufferTex) - 1
&& bgfx::isValid(m_lightTaProgram)
&& m_useTArray)
{
const float layer[4] = { float(ii) };
bgfx::setUniform(u_layer, layer);
bgfx::submit(kRenderPassDebugGBuffer, m_debugTaProgram);
}
else
{
bgfx::submit(kRenderPassDebugGBuffer, m_debugProgram);
}
2015-08-22 08:52:37 +03:00
}
}
}
2014-05-22 07:33:12 +04:00
2017-06-26 07:44:04 +03:00
imguiEndFrame();
2015-08-22 08:52:37 +03:00
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
2014-05-22 07:33:12 +04:00
2017-06-30 09:19:20 +03:00
return true;
2015-08-22 08:52:37 +03:00
}
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
return false;
}
2014-05-22 07:33:12 +04:00
2015-08-22 08:52:37 +03:00
bgfx::VertexBufferHandle m_vbh;
bgfx::IndexBufferHandle m_ibh;
bgfx::UniformHandle s_texColor;
bgfx::UniformHandle s_texNormal;
bgfx::UniformHandle s_albedo;
bgfx::UniformHandle s_normal;
bgfx::UniformHandle s_depth;
bgfx::UniformHandle s_light;
bgfx::UniformHandle u_mtx;
bgfx::UniformHandle u_lightPosRadius;
bgfx::UniformHandle u_lightRgbInnerR;
bgfx::UniformHandle u_layer;
2015-08-22 08:52:37 +03:00
bgfx::ProgramHandle m_geomProgram;
bgfx::ProgramHandle m_lightProgram;
bgfx::ProgramHandle m_lightTaProgram;
2019-02-17 18:50:26 +03:00
bgfx::ProgramHandle m_lightUavProgram;
bgfx::ProgramHandle m_clearUavProgram;
2015-08-22 08:52:37 +03:00
bgfx::ProgramHandle m_combineProgram;
bgfx::ProgramHandle m_combineTaProgram;
2015-08-22 08:52:37 +03:00
bgfx::ProgramHandle m_debugProgram;
bgfx::ProgramHandle m_debugTaProgram;
2015-08-22 08:52:37 +03:00
bgfx::ProgramHandle m_lineProgram;
bgfx::TextureHandle m_textureColor;
bgfx::TextureHandle m_textureNormal;
bgfx::TextureHandle m_gbufferTex[3];
2019-02-17 18:50:26 +03:00
bgfx::TextureHandle m_lightBufferTex;
2015-08-22 08:52:37 +03:00
bgfx::FrameBufferHandle m_gbuffer;
bgfx::FrameBufferHandle m_lightBuffer;
uint32_t m_width;
uint32_t m_height;
uint32_t m_debug;
uint32_t m_reset;
uint32_t m_oldWidth;
uint32_t m_oldHeight;
uint32_t m_oldReset;
bool m_useTArray;
bool m_oldUseTArray;
2019-02-17 18:50:26 +03:00
bool m_useUav;
bool m_oldUseUav;
2015-08-22 08:52:37 +03:00
int32_t m_scrollArea;
int32_t m_numLights;
float m_lightAnimationSpeed;
bool m_animateMesh;
bool m_showScissorRects;
bool m_showGBuffer;
entry::MouseState m_mouseState;
const bgfx::Caps* m_caps;
int64_t m_timeOffset;
};
2014-05-22 07:33:12 +04:00
2017-06-26 07:44:04 +03:00
} // namespace
2019-08-17 23:25:39 +03:00
ENTRY_IMPLEMENT_MAIN(
2019-08-18 00:40:38 +03:00
ExampleDeferred
, "21-deferred"
, "MRT rendering and deferred shading."
, "https://bkaradzic.github.io/bgfx/examples.html#deferred"
);