summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/examples/21-deferred/deferred.cpp
diff options
context:
space:
mode:
author Julian Sikorski <belegdol+github@gmail.com>2019-10-13 13:50:38 +0200
committer R. Belmont <rb6502@users.noreply.github.com>2019-10-13 07:50:38 -0400
commit0837e7451a84f95c29dbdb9bd6b8b931fee1635d (patch)
tree626bcbd250a7fbebdf5958a288d2f438d4f9fb5a /3rdparty/bgfx/examples/21-deferred/deferred.cpp
parentc913ccb59d713ce0b91135520719ac5385e54358 (diff)
WIP: sync bgfx, bx and bimg with latest upstream (#5723)
* Sync with bgfx upstream revision b91d0b6 * Sync with bx upstream revision d60912b * Sync with bimg upstream revision bd81f60 * Add astc-codec decoder * Rename VertexDecl to VertexLayout * Rename UniformType enum Int1 to Sampler. * Add NVN stub * Fix unused-const-variable error on macOS * Drop redundant explicit language parameters buildoptions_cpp are only applied to c++ files and buildoptions_objcpp are only applied to objective c++ files. As such, hardcoding -x offers no benefit while preventing overrides (such as one needed by 3rdparty/bgfx/src/renderer_vk.cpp on macOS) from working. * Re-introduce -x c++ in places where C code is compiled as C++ to prevent clang from throwing a warning * Build bgfx as Objective-C++ on macOS It is needed due to included headers * Enable Direct3D12 and Vulkan bgfx rendering backends * Enable building of spirv shaders * Properly escape /c in cmd call * Comment out dx12 bgfx renderer * Honor VERBOSE setting during shaders build * Only invert hlsl shader XYZ_TO_sRGB matrix for opengl * Add spirv shaders * OpenGL ES needs transposed matrix too * Metal needs transposed matrix as well
Diffstat (limited to '3rdparty/bgfx/examples/21-deferred/deferred.cpp')
-rw-r--r--3rdparty/bgfx/examples/21-deferred/deferred.cpp360
1 files changed, 242 insertions, 118 deletions
diff --git a/3rdparty/bgfx/examples/21-deferred/deferred.cpp b/3rdparty/bgfx/examples/21-deferred/deferred.cpp
index 00379705b05..3b93c0d0b45 100644
--- a/3rdparty/bgfx/examples/21-deferred/deferred.cpp
+++ b/3rdparty/bgfx/examples/21-deferred/deferred.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright 2011-2018 Branimir Karadzic. All rights reserved.
+ * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
@@ -12,11 +12,11 @@
namespace
{
-#define RENDER_PASS_GEOMETRY_ID 0
-#define RENDER_PASS_LIGHT_ID 1
-#define RENDER_PASS_COMBINE_ID 2
-#define RENDER_PASS_DEBUG_LIGHTS_ID 3
-#define RENDER_PASS_DEBUG_GBUFFER_ID 4
+constexpr bgfx::ViewId kRenderPassGeometry = 0;
+constexpr bgfx::ViewId kRenderPassLight = 1;
+constexpr bgfx::ViewId kRenderPassCombine = 2;
+constexpr bgfx::ViewId kRenderPassDebugLights = 3;
+constexpr bgfx::ViewId kRenderPassDebugGBuffer = 4;
static float s_texelHalf = 0.0f;
@@ -32,7 +32,7 @@ struct PosNormalTangentTexcoordVertex
static void init()
{
- ms_decl
+ ms_layout
.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
@@ -41,10 +41,10 @@ struct PosNormalTangentTexcoordVertex
.end();
}
- static bgfx::VertexDecl ms_decl;
+ static bgfx::VertexLayout ms_layout;
};
-bgfx::VertexDecl PosNormalTangentTexcoordVertex::ms_decl;
+bgfx::VertexLayout PosNormalTangentTexcoordVertex::ms_layout;
struct PosTexCoord0Vertex
{
@@ -56,17 +56,17 @@ struct PosTexCoord0Vertex
static void init()
{
- ms_decl
+ ms_layout
.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
.end();
}
- static bgfx::VertexDecl ms_decl;
+ static bgfx::VertexLayout ms_layout;
};
-bgfx::VertexDecl PosTexCoord0Vertex::ms_decl;
+bgfx::VertexLayout PosTexCoord0Vertex::ms_layout;
struct DebugVertex
{
@@ -77,17 +77,17 @@ struct DebugVertex
static void init()
{
- ms_decl
+ ms_layout
.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
.end();
}
- static bgfx::VertexDecl ms_decl;
+ static bgfx::VertexLayout ms_layout;
};
-bgfx::VertexDecl DebugVertex::ms_decl;
+bgfx::VertexLayout DebugVertex::ms_layout;
static PosNormalTangentTexcoordVertex s_cubeVertices[24] =
{
@@ -137,10 +137,10 @@ static const uint16_t s_cubeIndices[36] =
void screenSpaceQuad(float _textureWidth, float _textureHeight, float _texelHalf, bool _originBottomLeft, float _width = 1.0f, float _height = 1.0f)
{
- if (3 == bgfx::getAvailTransientVertexBuffer(3, PosTexCoord0Vertex::ms_decl) )
+ if (3 == bgfx::getAvailTransientVertexBuffer(3, PosTexCoord0Vertex::ms_layout) )
{
bgfx::TransientVertexBuffer vb;
- bgfx::allocTransientVertexBuffer(&vb, 3, PosTexCoord0Vertex::ms_decl);
+ bgfx::allocTransientVertexBuffer(&vb, 3, PosTexCoord0Vertex::ms_layout);
PosTexCoord0Vertex* vertex = (PosTexCoord0Vertex*)vb.data;
const float minx = -_width;
@@ -193,8 +193,8 @@ void screenSpaceQuad(float _textureWidth, float _textureHeight, float _texelHalf
class ExampleDeferred : public entry::AppI
{
public:
- ExampleDeferred(const char* _name, const char* _description)
- : entry::AppI(_name, _description)
+ ExampleDeferred(const char* _name, const char* _description, const char* _url)
+ : entry::AppI(_name, _description, _url)
{
}
@@ -225,7 +225,7 @@ public:
bgfx::setPaletteColor(1, UINT32_C(0x303030ff) );
// Set geometry pass view clear state.
- bgfx::setViewClear(RENDER_PASS_GEOMETRY_ID
+ bgfx::setViewClear(kRenderPassGeometry
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
, 1.0f
, 0
@@ -233,7 +233,7 @@ public:
);
// Set light pass view clear state.
- bgfx::setViewClear(RENDER_PASS_LIGHT_ID
+ bgfx::setViewClear(kRenderPassLight
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
, 1.0f
, 0
@@ -247,7 +247,7 @@ public:
calcTangents(s_cubeVertices
, BX_COUNTOF(s_cubeVertices)
- , PosNormalTangentTexcoordVertex::ms_decl
+ , PosNormalTangentTexcoordVertex::ms_layout
, s_cubeIndices
, BX_COUNTOF(s_cubeIndices)
);
@@ -255,20 +255,20 @@ public:
// Create static vertex buffer.
m_vbh = bgfx::createVertexBuffer(
bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
- , PosNormalTangentTexcoordVertex::ms_decl
+ , PosNormalTangentTexcoordVertex::ms_layout
);
// Create static index buffer.
m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
// Create texture sampler uniforms.
- s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
- s_texNormal = bgfx::createUniform("s_texNormal", bgfx::UniformType::Int1);
+ s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
+ s_texNormal = bgfx::createUniform("s_texNormal", bgfx::UniformType::Sampler);
- s_albedo = bgfx::createUniform("s_albedo", bgfx::UniformType::Int1);
- s_normal = bgfx::createUniform("s_normal", bgfx::UniformType::Int1);
- s_depth = bgfx::createUniform("s_depth", bgfx::UniformType::Int1);
- s_light = bgfx::createUniform("s_light", bgfx::UniformType::Int1);
+ 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);
u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Vec4);
@@ -281,6 +281,29 @@ public:
m_debugProgram = loadProgram("vs_deferred_debug", "fs_deferred_debug");
m_lineProgram = loadProgram("vs_deferred_debug_line", "fs_deferred_debug_line");
+ m_useTArray = false;
+ m_useUav = false;
+
+ if (0 != (BGFX_CAPS_TEXTURE_2D_ARRAY & bgfx::getCaps()->supported) )
+ {
+ m_lightTaProgram = loadProgram("vs_deferred_light", "fs_deferred_light_ta");
+ }
+ else
+ {
+ m_lightTaProgram = BGFX_INVALID_HANDLE;
+ }
+
+ if (0 != (BGFX_CAPS_FRAMEBUFFER_RW & bgfx::getCaps()->supported) )
+ {
+ m_lightUavProgram = loadProgram("vs_deferred_light", "fs_deferred_light_uav");
+ m_clearUavProgram = loadProgram("vs_deferred_light", "fs_deferred_clear_uav");
+ }
+ else
+ {
+ m_lightUavProgram = BGFX_INVALID_HANDLE;
+ m_clearUavProgram = BGFX_INVALID_HANDLE;
+ }
+
// Load diffuse texture.
m_textureColor = loadTexture("textures/fieldstone-rgba.dds");
@@ -290,8 +313,8 @@ public:
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;
+ m_gbuffer.idx = bgfx::kInvalidHandle;
+ m_lightBuffer.idx = bgfx::kInvalidHandle;
// Imgui.
imguiCreate();
@@ -316,8 +339,7 @@ public:
cameraCreate();
- const float initialPos[3] = { 0.0f, 0.0f, -15.0f };
- cameraSetPosition(initialPos);
+ cameraSetPosition({ 0.0f, 0.0f, -15.0f });
cameraSetVerticalAngle(0.0f);
}
@@ -338,6 +360,18 @@ public:
bgfx::destroy(m_geomProgram);
bgfx::destroy(m_lightProgram);
+
+ if (bgfx::isValid(m_lightTaProgram) )
+ {
+ bgfx::destroy(m_lightTaProgram);
+ }
+
+ if (bgfx::isValid(m_lightUavProgram) )
+ {
+ bgfx::destroy(m_lightUavProgram);
+ bgfx::destroy(m_clearUavProgram);
+ }
+
bgfx::destroy(m_combineProgram);
bgfx::destroy(m_debugProgram);
bgfx::destroy(m_lineProgram);
@@ -403,40 +437,78 @@ public:
}
else
{
- if (m_oldWidth != m_width
- || m_oldHeight != m_height
- || m_oldReset != m_reset
+ if (m_oldWidth != m_width
+ || m_oldHeight != m_height
+ || m_oldReset != m_reset
+ || m_oldUseTArray != m_useTArray
+ || m_oldUseUav != m_useUav
|| !bgfx::isValid(m_gbuffer) )
{
// Recreate variable size render targets when resolution changes.
- m_oldWidth = m_width;
- m_oldHeight = m_height;
- m_oldReset = m_reset;
+ m_oldWidth = m_width;
+ m_oldHeight = m_height;
+ m_oldReset = m_reset;
+ m_oldUseTArray = m_useTArray;
+ m_oldUseUav = m_useUav;
if (bgfx::isValid(m_gbuffer) )
{
bgfx::destroy(m_gbuffer);
+ m_gbufferTex[0].idx = bgfx::kInvalidHandle;
+ m_gbufferTex[1].idx = bgfx::kInvalidHandle;
+ m_gbufferTex[2].idx = bgfx::kInvalidHandle;
}
const uint64_t tsFlags = 0
- | BGFX_TEXTURE_RT
| BGFX_SAMPLER_MIN_POINT
| BGFX_SAMPLER_MAG_POINT
| BGFX_SAMPLER_MIP_POINT
| BGFX_SAMPLER_U_CLAMP
| BGFX_SAMPLER_V_CLAMP
;
- m_gbufferTex[0] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::BGRA8, tsFlags);
- m_gbufferTex[1] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::BGRA8, tsFlags);
- m_gbufferTex[2] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::D24S8, tsFlags);
- m_gbuffer = bgfx::createFrameBuffer(BX_COUNTOF(m_gbufferTex), m_gbufferTex, true);
+
+ bgfx::Attachment gbufferAt[3];
+
+ if (m_useTArray)
+ {
+ 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
+ {
+ 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]);
+ }
+
+ m_gbufferTex[2] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT | tsFlags);
+ gbufferAt[2].init(m_gbufferTex[2]);
+
+ m_gbuffer = bgfx::createFrameBuffer(BX_COUNTOF(gbufferAt), gbufferAt, true);
if (bgfx::isValid(m_lightBuffer) )
{
bgfx::destroy(m_lightBuffer);
}
- m_lightBuffer = bgfx::createFrameBuffer(uint16_t(m_width), uint16_t(m_height), bgfx::TextureFormat::BGRA8, tsFlags);
+ if (m_useUav)
+ {
+ bgfx::Attachment lightAt[2];
+
+ bgfx::TextureHandle target = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT | tsFlags);
+ m_lightBufferTex = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_COMPUTE_WRITE | tsFlags);
+ lightAt[0].init(target);
+ lightAt[1].init(m_lightBufferTex, bgfx::Access::ReadWrite);
+
+ m_lightBuffer = bgfx::createFrameBuffer(BX_COUNTOF(lightAt), lightAt, true);
+ }
+ 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);
+ }
}
ImGui::SetNextWindowPos(
@@ -455,6 +527,25 @@ public:
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 frame buffer attachment.", &m_useUav);
+ }
+ else
+ {
+ ImGui::Text("UAV frame buffer attachment is not supported.");
+ }
+
ImGui::Checkbox("Animate mesh.", &m_animateMesh);
ImGui::SliderFloat("Anim.speed", &m_lightAnimationSpeed, 0.0f, 0.4f);
@@ -470,19 +561,19 @@ public:
float vp[16];
float invMvp[16];
{
- bgfx::setViewRect(RENDER_PASS_GEOMETRY_ID, 0, 0, uint16_t(m_width), uint16_t(m_height) );
- bgfx::setViewRect(RENDER_PASS_LIGHT_ID, 0, 0, uint16_t(m_width), uint16_t(m_height) );
- bgfx::setViewRect(RENDER_PASS_COMBINE_ID, 0, 0, uint16_t(m_width), uint16_t(m_height) );
- bgfx::setViewRect(RENDER_PASS_DEBUG_LIGHTS_ID, 0, 0, uint16_t(m_width), uint16_t(m_height) );
- bgfx::setViewRect(RENDER_PASS_DEBUG_GBUFFER_ID, 0, 0, uint16_t(m_width), uint16_t(m_height) );
+ bgfx::setViewRect(kRenderPassGeometry, 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) );
+ 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) );
- bgfx::setViewFrameBuffer(RENDER_PASS_LIGHT_ID, m_lightBuffer);
+ bgfx::setViewFrameBuffer(kRenderPassLight, m_lightBuffer);
float proj[16];
bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, m_caps->homogeneousDepth);
- bgfx::setViewFrameBuffer(RENDER_PASS_GEOMETRY_ID, m_gbuffer);
- bgfx::setViewTransform(RENDER_PASS_GEOMETRY_ID, view, proj);
+ bgfx::setViewFrameBuffer(kRenderPassGeometry, m_gbuffer);
+ bgfx::setViewTransform(kRenderPassGeometry, view, proj);
bx::mtxMul(vp, view, proj);
bx::mtxInverse(invMvp, vp);
@@ -490,16 +581,16 @@ public:
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(RENDER_PASS_LIGHT_ID, NULL, proj);
- bgfx::setViewTransform(RENDER_PASS_COMBINE_ID, NULL, proj);
+ bgfx::setViewTransform(kRenderPassLight, NULL, proj);
+ bgfx::setViewTransform(kRenderPassCombine, NULL, proj);
const float aspectRatio = float(m_height)/float(m_width);
const float size = 10.0f;
bx::mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth);
- bgfx::setViewTransform(RENDER_PASS_DEBUG_GBUFFER_ID, NULL, proj);
+ bgfx::setViewTransform(kRenderPassDebugGBuffer, NULL, proj);
bx::mtxOrtho(proj, 0.0f, (float)m_width, 0.0f, (float)m_height, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth);
- bgfx::setViewTransform(RENDER_PASS_DEBUG_LIGHTS_ID, NULL, proj);
+ bgfx::setViewTransform(kRenderPassDebugLights, NULL, proj);
}
const uint32_t dim = 11;
@@ -536,75 +627,79 @@ public:
// Set render states.
bgfx::setState(0
- | BGFX_STATE_WRITE_RGB
- | BGFX_STATE_WRITE_A
- | BGFX_STATE_WRITE_Z
- | BGFX_STATE_DEPTH_TEST_LESS
- | BGFX_STATE_MSAA
- );
+ | BGFX_STATE_WRITE_RGB
+ | BGFX_STATE_WRITE_A
+ | BGFX_STATE_WRITE_Z
+ | BGFX_STATE_DEPTH_TEST_LESS
+ | BGFX_STATE_MSAA
+ );
// Submit primitive for rendering to view 0.
- bgfx::submit(RENDER_PASS_GEOMETRY_ID, m_geomProgram);
+ bgfx::submit(kRenderPassGeometry, m_geomProgram);
}
}
+ // Clear UAV texture
+ if (m_useUav)
+ {
+ screenSpaceQuad( (float)m_width, (float)m_height, s_texelHalf, m_caps->originBottomLeft);
+ bgfx::setState(0
+ | BGFX_STATE_WRITE_RGB
+ | BGFX_STATE_WRITE_A
+ );
+ bgfx::submit(kRenderPassLight, m_clearUavProgram);
+ }
+
// Draw lights into light buffer.
for (int32_t light = 0; light < m_numLights; ++light)
{
Sphere lightPosRadius;
float lightTime = time * m_lightAnimationSpeed * (bx::sin(light/float(m_numLights) * bx::kPiHalf ) * 0.5f + 0.5f);
- lightPosRadius.m_center.x = bx::sin( ( (lightTime + light*0.47f) + bx::kPiHalf*1.37f ) )*offset;
- lightPosRadius.m_center.y = bx::cos( ( (lightTime + light*0.69f) + bx::kPiHalf*1.49f ) )*offset;
- lightPosRadius.m_center.z = bx::sin( ( (lightTime + light*0.37f) + bx::kPiHalf*1.57f ) )*2.0f;
- lightPosRadius.m_radius = 2.0f;
+ 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;
Aabb aabb;
toAabb(aabb, lightPosRadius);
- float box[8][3] =
+ const bx::Vec3 box[8] =
{
- { aabb.m_min.x, aabb.m_min.y, aabb.m_min.z },
- { aabb.m_min.x, aabb.m_min.y, aabb.m_max.z },
- { aabb.m_min.x, aabb.m_max.y, aabb.m_min.z },
- { aabb.m_min.x, aabb.m_max.y, aabb.m_max.z },
- { aabb.m_max.x, aabb.m_min.y, aabb.m_min.z },
- { aabb.m_max.x, aabb.m_min.y, aabb.m_max.z },
- { aabb.m_max.x, aabb.m_max.y, aabb.m_min.z },
- { aabb.m_max.x, aabb.m_max.y, aabb.m_max.z },
+ { 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 },
};
- float xyz[3];
- bx::vec3MulMtxH(xyz, box[0], vp);
- float minx = xyz[0];
- float miny = xyz[1];
- float maxx = xyz[0];
- float maxy = xyz[1];
- float maxz = xyz[2];
+ bx::Vec3 xyz = bx::mulH(box[0], vp);
+ bx::Vec3 min = xyz;
+ bx::Vec3 max = xyz;
for (uint32_t ii = 1; ii < 8; ++ii)
{
- bx::vec3MulMtxH(xyz, box[ii], vp);
- minx = bx::min(minx, xyz[0]);
- miny = bx::min(miny, xyz[1]);
- maxx = bx::max(maxx, xyz[0]);
- maxy = bx::max(maxy, xyz[1]);
- maxz = bx::max(maxz, xyz[2]);
+ xyz = bx::mulH(box[ii], vp);
+ min = bx::min(min, xyz);
+ max = bx::max(max, xyz);
}
// Cull light if it's fully behind camera.
- if (maxz >= 0.0f)
+ if (max.z >= 0.0f)
{
- float x0 = bx::clamp( (minx * 0.5f + 0.5f) * m_width, 0.0f, (float)m_width);
- float y0 = bx::clamp( (miny * 0.5f + 0.5f) * m_height, 0.0f, (float)m_height);
- float x1 = bx::clamp( (maxx * 0.5f + 0.5f) * m_width, 0.0f, (float)m_width);
- float y1 = bx::clamp( (maxy * 0.5f + 0.5f) * m_height, 0.0f, (float)m_height);
+ 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);
if (m_showScissorRects)
{
bgfx::TransientVertexBuffer tvb;
bgfx::TransientIndexBuffer tib;
- if (bgfx::allocTransientBuffers(&tvb, DebugVertex::ms_decl, 4, &tib, 8) )
+ if (bgfx::allocTransientBuffers(&tvb, DebugVertex::ms_layout, 4, &tib, 8) )
{
uint32_t abgr = 0x8000ff00;
@@ -645,11 +740,11 @@ public:
bgfx::setVertexBuffer(0, &tvb);
bgfx::setIndexBuffer(&tib);
bgfx::setState(0
- | BGFX_STATE_WRITE_RGB
- | BGFX_STATE_PT_LINES
- | BGFX_STATE_BLEND_ALPHA
- );
- bgfx::submit(RENDER_PASS_DEBUG_LIGHTS_ID, m_lineProgram);
+ | BGFX_STATE_WRITE_RGB
+ | BGFX_STATE_PT_LINES
+ | BGFX_STATE_BLEND_ALPHA
+ );
+ bgfx::submit(kRenderPassDebugLights, m_lineProgram);
}
}
@@ -671,24 +766,38 @@ public:
bgfx::setTexture(0, s_normal, bgfx::getTexture(m_gbuffer, 1) );
bgfx::setTexture(1, s_depth, bgfx::getTexture(m_gbuffer, 2) );
bgfx::setState(0
- | BGFX_STATE_WRITE_RGB
- | BGFX_STATE_WRITE_A
- | BGFX_STATE_BLEND_ADD
- );
+ | BGFX_STATE_WRITE_RGB
+ | BGFX_STATE_WRITE_A
+ | BGFX_STATE_BLEND_ADD
+ );
screenSpaceQuad( (float)m_width, (float)m_height, s_texelHalf, m_caps->originBottomLeft);
- bgfx::submit(RENDER_PASS_LIGHT_ID, m_lightProgram);
+
+ if (bgfx::isValid(m_lightTaProgram)
+ && m_useTArray)
+ {
+ bgfx::submit(kRenderPassLight, m_lightTaProgram);
+ }
+ else if (bgfx::isValid(m_lightUavProgram)
+ && m_useUav)
+ {
+ bgfx::submit(kRenderPassLight, m_lightUavProgram);
+ }
+ else
+ {
+ bgfx::submit(kRenderPassLight, m_lightProgram);
+ }
}
}
// Combine color and light buffers.
- bgfx::setTexture(0, s_albedo, bgfx::getTexture(m_gbuffer, 0) );
- bgfx::setTexture(1, s_light, bgfx::getTexture(m_lightBuffer, 0) );
+ bgfx::setTexture(0, s_albedo, m_gbufferTex[0]);
+ bgfx::setTexture(1, s_light, m_lightBufferTex);
bgfx::setState(0
- | BGFX_STATE_WRITE_RGB
- | BGFX_STATE_WRITE_A
- );
+ | BGFX_STATE_WRITE_RGB
+ | BGFX_STATE_WRITE_A
+ );
screenSpaceQuad( (float)m_width, (float)m_height, s_texelHalf, m_caps->originBottomLeft);
- bgfx::submit(RENDER_PASS_COMBINE_ID, m_combineProgram);
+ bgfx::submit(kRenderPassCombine, m_combineProgram);
if (m_showGBuffer)
{
@@ -699,17 +808,17 @@ public:
{
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
- );
+ , 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
+ );
bgfx::setTransform(mtx);
bgfx::setVertexBuffer(0, m_vbh);
bgfx::setIndexBuffer(m_ibh, 0, 6);
bgfx::setTexture(0, s_texColor, m_gbufferTex[ii]);
bgfx::setState(BGFX_STATE_WRITE_RGB);
- bgfx::submit(RENDER_PASS_DEBUG_GBUFFER_ID, m_debugProgram);
+ bgfx::submit(kRenderPassDebugGBuffer, m_debugProgram);
}
}
}
@@ -742,6 +851,9 @@ public:
bgfx::ProgramHandle m_geomProgram;
bgfx::ProgramHandle m_lightProgram;
+ bgfx::ProgramHandle m_lightTaProgram;
+ bgfx::ProgramHandle m_lightUavProgram;
+ bgfx::ProgramHandle m_clearUavProgram;
bgfx::ProgramHandle m_combineProgram;
bgfx::ProgramHandle m_debugProgram;
bgfx::ProgramHandle m_lineProgram;
@@ -749,6 +861,7 @@ public:
bgfx::TextureHandle m_textureNormal;
bgfx::TextureHandle m_gbufferTex[3];
+ bgfx::TextureHandle m_lightBufferTex;
bgfx::FrameBufferHandle m_gbuffer;
bgfx::FrameBufferHandle m_lightBuffer;
@@ -761,6 +874,12 @@ public:
uint32_t m_oldHeight;
uint32_t m_oldReset;
+ bool m_useTArray;
+ bool m_oldUseTArray;
+
+ bool m_useUav;
+ bool m_oldUseUav;
+
int32_t m_scrollArea;
int32_t m_numLights;
float m_lightAnimationSpeed;
@@ -776,4 +895,9 @@ public:
} // namespace
-ENTRY_IMPLEMENT_MAIN(ExampleDeferred, "21-deferred", "MRT rendering and deferred shading.");
+ENTRY_IMPLEMENT_MAIN(
+ ExampleDeferred
+ , "21-deferred"
+ , "MRT rendering and deferred shading."
+ , "https://bkaradzic.github.io/bgfx/examples.html#deferred"
+ );