diff options
Diffstat (limited to '3rdparty/bgfx/examples/21-deferred')
16 files changed, 411 insertions, 189 deletions
diff --git a/3rdparty/bgfx/examples/21-deferred/common.sh b/3rdparty/bgfx/examples/21-deferred/common.sh new file mode 100644 index 00000000000..fcb93f2c013 --- /dev/null +++ b/3rdparty/bgfx/examples/21-deferred/common.sh @@ -0,0 +1,62 @@ +/* + * Copyright 2011-2019 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause + */ + +#include "../common/common.sh" + +vec2 blinn(vec3 _lightDir, vec3 _normal, vec3 _viewDir) +{ + float ndotl = dot(_normal, _lightDir); + vec3 reflected = _lightDir - 2.0*ndotl*_normal; // reflect(_lightDir, _normal); + float rdotv = dot(reflected, _viewDir); + return vec2(ndotl, rdotv); +} + +float fresnel(float _ndotl, float _bias, float _pow) +{ + float facing = (1.0 - _ndotl); + return max(_bias + (1.0 - _bias) * pow(facing, _pow), 0.0); +} + +vec4 lit(float _ndotl, float _rdotv, float _m) +{ + float diff = max(0.0, _ndotl); + float spec = step(0.0, _ndotl) * max(0.0, _rdotv * _m); + return vec4(1.0, diff, spec, 1.0); +} + +vec4 powRgba(vec4 _rgba, float _pow) +{ + vec4 result; + result.xyz = pow(_rgba.xyz, vec3_splat(_pow) ); + result.w = _rgba.w; + return result; +} + +vec3 calcLight(vec3 _wpos, vec3 _normal, vec3 _view, vec3 _lightPos, float _lightRadius, vec3 _lightRgb, float _lightInner) +{ + vec3 lp = _lightPos - _wpos; + float attn = 1.0 - smoothstep(_lightInner, 1.0, length(lp) / _lightRadius); + vec3 lightDir = normalize(lp); + vec2 bln = blinn(lightDir, _normal, _view); + vec4 lc = lit(bln.x, bln.y, 1.0); + vec3 rgb = _lightRgb * saturate(lc.y) * attn; + return rgb; +} + +float toClipSpaceDepth(float _depthTextureZ) +{ +#if BGFX_SHADER_LANGUAGE_GLSL + return _depthTextureZ * 2.0 - 1.0; +#else + return _depthTextureZ; +#endif // BGFX_SHADER_LANGUAGE_GLSL +} + +vec3 clipToWorld(mat4 _invViewProj, vec3 _clipPos) +{ + vec4 wpos = mul(_invViewProj, vec4(_clipPos, 1.0) ); + return wpos.xyz / wpos.w; +} + 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" + ); diff --git a/3rdparty/bgfx/examples/21-deferred/fs_deferred_clear_uav.sc b/3rdparty/bgfx/examples/21-deferred/fs_deferred_clear_uav.sc new file mode 100644 index 00000000000..6fd797a0338 --- /dev/null +++ b/3rdparty/bgfx/examples/21-deferred/fs_deferred_clear_uav.sc @@ -0,0 +1,17 @@ +$input v_texcoord0 + +/* + * Copyright 2011-2019 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause + */ + +#include "common.sh" +#include <bgfx_compute.sh> + +FRAMEBUFFER_IMAGE2D_RW(s_light, rgba8, 0); + +void main() +{ + ivec2 coord = ivec2(gl_FragCoord.xy); + imageStore(s_light, coord, vec4(0.0, 0.0, 0.0, 0.0)); +} diff --git a/3rdparty/bgfx/examples/21-deferred/fs_deferred_combine.sc b/3rdparty/bgfx/examples/21-deferred/fs_deferred_combine.sc index 524732cc612..ff60c910fe3 100644 --- a/3rdparty/bgfx/examples/21-deferred/fs_deferred_combine.sc +++ b/3rdparty/bgfx/examples/21-deferred/fs_deferred_combine.sc @@ -1,7 +1,7 @@ $input v_texcoord0 /* - * 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 */ diff --git a/3rdparty/bgfx/examples/21-deferred/fs_deferred_debug.sc b/3rdparty/bgfx/examples/21-deferred/fs_deferred_debug.sc index 9dc1de09e31..d9c5becf535 100644 --- a/3rdparty/bgfx/examples/21-deferred/fs_deferred_debug.sc +++ b/3rdparty/bgfx/examples/21-deferred/fs_deferred_debug.sc @@ -1,7 +1,7 @@ $input v_texcoord0 /* - * 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 */ diff --git a/3rdparty/bgfx/examples/21-deferred/fs_deferred_debug_line.sc b/3rdparty/bgfx/examples/21-deferred/fs_deferred_debug_line.sc index 99ec64685ad..9dc0deaf872 100644 --- a/3rdparty/bgfx/examples/21-deferred/fs_deferred_debug_line.sc +++ b/3rdparty/bgfx/examples/21-deferred/fs_deferred_debug_line.sc @@ -1,7 +1,7 @@ $input v_color0 /* - * 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 */ diff --git a/3rdparty/bgfx/examples/21-deferred/fs_deferred_geom.sc b/3rdparty/bgfx/examples/21-deferred/fs_deferred_geom.sc index 2026e7e8af3..804f4d1c3ab 100644 --- a/3rdparty/bgfx/examples/21-deferred/fs_deferred_geom.sc +++ b/3rdparty/bgfx/examples/21-deferred/fs_deferred_geom.sc @@ -1,7 +1,7 @@ $input v_wpos, v_view, v_normal, v_tangent, v_bitangent, v_texcoord0 /* - * 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 */ diff --git a/3rdparty/bgfx/examples/21-deferred/fs_deferred_light.sc b/3rdparty/bgfx/examples/21-deferred/fs_deferred_light.sc index dac140d8e3d..e5e34c37a5b 100644 --- a/3rdparty/bgfx/examples/21-deferred/fs_deferred_light.sc +++ b/3rdparty/bgfx/examples/21-deferred/fs_deferred_light.sc @@ -1,11 +1,11 @@ $input v_texcoord0 /* - * 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 */ -#include "../common/common.sh" +#include "common.sh" SAMPLER2D(s_normal, 0); SAMPLER2D(s_depth, 1); @@ -14,61 +14,6 @@ uniform vec4 u_lightPosRadius[1]; uniform vec4 u_lightRgbInnerR[1]; uniform mat4 u_mtx; -vec2 blinn(vec3 _lightDir, vec3 _normal, vec3 _viewDir) -{ - float ndotl = dot(_normal, _lightDir); - vec3 reflected = _lightDir - 2.0*ndotl*_normal; // reflect(_lightDir, _normal); - float rdotv = dot(reflected, _viewDir); - return vec2(ndotl, rdotv); -} - -float fresnel(float _ndotl, float _bias, float _pow) -{ - float facing = (1.0 - _ndotl); - return max(_bias + (1.0 - _bias) * pow(facing, _pow), 0.0); -} - -vec4 lit(float _ndotl, float _rdotv, float _m) -{ - float diff = max(0.0, _ndotl); - float spec = step(0.0, _ndotl) * max(0.0, _rdotv * _m); - return vec4(1.0, diff, spec, 1.0); -} - -vec4 powRgba(vec4 _rgba, float _pow) -{ - vec4 result; - result.xyz = pow(_rgba.xyz, vec3_splat(_pow) ); - result.w = _rgba.w; - return result; -} - -vec3 calcLight(int _idx, vec3 _wpos, vec3 _normal, vec3 _view) -{ - vec3 lp = u_lightPosRadius[_idx].xyz - _wpos; - float attn = 1.0 - smoothstep(u_lightRgbInnerR[_idx].w, 1.0, length(lp) / u_lightPosRadius[_idx].w); - vec3 lightDir = normalize(lp); - vec2 bln = blinn(lightDir, _normal, _view); - vec4 lc = lit(bln.x, bln.y, 1.0); - vec3 rgb = u_lightRgbInnerR[_idx].xyz * saturate(lc.y) * attn; - return rgb; -} - -float toClipSpaceDepth(float _depthTextureZ) -{ -#if BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_METAL - return _depthTextureZ; -#else - return _depthTextureZ * 2.0 - 1.0; -#endif // BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_METAL -} - -vec3 clipToWorld(mat4 _invViewProj, vec3 _clipPos) -{ - vec4 wpos = mul(_invViewProj, vec4(_clipPos, 1.0) ); - return wpos.xyz / wpos.w; -} - void main() { vec3 normal = decodeNormalUint(texture2D(s_normal, v_texcoord0).xyz); @@ -76,16 +21,15 @@ void main() float depth = toClipSpaceDepth(deviceDepth); vec3 clip = vec3(v_texcoord0 * 2.0 - 1.0, depth); -#if BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_METAL +#if !BGFX_SHADER_LANGUAGE_GLSL clip.y = -clip.y; -#endif // BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_METAL +#endif // !BGFX_SHADER_LANGUAGE_GLSL vec3 wpos = clipToWorld(u_mtx, clip); vec3 view = mul(u_view, vec4(wpos, 0.0) ).xyz; view = -normalize(view); - vec3 lightColor; - lightColor = calcLight(0, wpos, normal, view); + vec3 lightColor = calcLight(wpos, normal, view, u_lightPosRadius[0].xyz, u_lightPosRadius[0].w, u_lightRgbInnerR[0].xyz, u_lightRgbInnerR[0].w); gl_FragColor.xyz = toGamma(lightColor.xyz); gl_FragColor.w = 1.0; } diff --git a/3rdparty/bgfx/examples/21-deferred/fs_deferred_light_ta.sc b/3rdparty/bgfx/examples/21-deferred/fs_deferred_light_ta.sc new file mode 100644 index 00000000000..4fb5754015d --- /dev/null +++ b/3rdparty/bgfx/examples/21-deferred/fs_deferred_light_ta.sc @@ -0,0 +1,35 @@ +$input v_texcoord0 + +/* + * Copyright 2011-2019 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause + */ + +#include "common.sh" + +SAMPLER2DARRAY(s_normal, 0); +SAMPLER2D(s_depth, 1); + +uniform vec4 u_lightPosRadius[1]; +uniform vec4 u_lightRgbInnerR[1]; +uniform mat4 u_mtx; + +void main() +{ + vec3 normal = decodeNormalUint(texture2DArray(s_normal, vec3(v_texcoord0, 1.0) ).xyz); + float deviceDepth = texture2D(s_depth, v_texcoord0).x; + float depth = toClipSpaceDepth(deviceDepth); + + vec3 clip = vec3(v_texcoord0 * 2.0 - 1.0, depth); +#if !BGFX_SHADER_LANGUAGE_GLSL + clip.y = -clip.y; +#endif // !BGFX_SHADER_LANGUAGE_GLSL + vec3 wpos = clipToWorld(u_mtx, clip); + + vec3 view = mul(u_view, vec4(wpos, 0.0) ).xyz; + view = -normalize(view); + + vec3 lightColor = calcLight(wpos, normal, view, u_lightPosRadius[0].xyz, u_lightPosRadius[0].w, u_lightRgbInnerR[0].xyz, u_lightRgbInnerR[0].w); + gl_FragColor.xyz = toGamma(lightColor.xyz); + gl_FragColor.w = 1.0; +} diff --git a/3rdparty/bgfx/examples/21-deferred/fs_deferred_light_uav.sc b/3rdparty/bgfx/examples/21-deferred/fs_deferred_light_uav.sc new file mode 100644 index 00000000000..a12872a6a7e --- /dev/null +++ b/3rdparty/bgfx/examples/21-deferred/fs_deferred_light_uav.sc @@ -0,0 +1,40 @@ +$input v_texcoord0 + +/* + * Copyright 2011-2019 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause + */ + +#include "common.sh" +#include <bgfx_compute.sh> + +SAMPLER2D(s_normal, 0); +SAMPLER2D(s_depth, 1); + +FRAMEBUFFER_IMAGE2D_RW(s_light, rgba8, 0); + +uniform vec4 u_lightPosRadius[1]; +uniform vec4 u_lightRgbInnerR[1]; +uniform mat4 u_mtx; + +void main() +{ + vec3 normal = decodeNormalUint(texture2D(s_normal, v_texcoord0).xyz); + float deviceDepth = texture2D(s_depth, v_texcoord0).x; + float depth = toClipSpaceDepth(deviceDepth); + + vec3 clip = vec3(v_texcoord0 * 2.0 - 1.0, depth); +#if !BGFX_SHADER_LANGUAGE_GLSL + clip.y = -clip.y; +#endif // !BGFX_SHADER_LANGUAGE_GLSL + vec3 wpos = clipToWorld(u_mtx, clip); + + vec3 view = mul(u_view, vec4(wpos, 0.0) ).xyz; + view = -normalize(view); + + ivec2 coord = ivec2(gl_FragCoord.xy); + + vec3 lightColor = calcLight(wpos, normal, view, u_lightPosRadius[0].xyz, u_lightPosRadius[0].w, u_lightRgbInnerR[0].xyz, u_lightRgbInnerR[0].w); + vec4 color = imageLoad(s_light, coord); + imageStore(s_light, coord, color + vec4(toGamma(lightColor.xyz), 1.0)); +} diff --git a/3rdparty/bgfx/examples/21-deferred/makefile b/3rdparty/bgfx/examples/21-deferred/makefile index 171709170a4..ef9203197d7 100644 --- a/3rdparty/bgfx/examples/21-deferred/makefile +++ b/3rdparty/bgfx/examples/21-deferred/makefile @@ -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 # diff --git a/3rdparty/bgfx/examples/21-deferred/vs_deferred_combine.sc b/3rdparty/bgfx/examples/21-deferred/vs_deferred_combine.sc index 8c3b7a822b6..33cba4f17cc 100644 --- a/3rdparty/bgfx/examples/21-deferred/vs_deferred_combine.sc +++ b/3rdparty/bgfx/examples/21-deferred/vs_deferred_combine.sc @@ -2,7 +2,7 @@ $input a_position, a_texcoord0 $output v_texcoord0 /* - * 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 */ diff --git a/3rdparty/bgfx/examples/21-deferred/vs_deferred_debug.sc b/3rdparty/bgfx/examples/21-deferred/vs_deferred_debug.sc index 8c3b7a822b6..33cba4f17cc 100644 --- a/3rdparty/bgfx/examples/21-deferred/vs_deferred_debug.sc +++ b/3rdparty/bgfx/examples/21-deferred/vs_deferred_debug.sc @@ -2,7 +2,7 @@ $input a_position, a_texcoord0 $output v_texcoord0 /* - * 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 */ diff --git a/3rdparty/bgfx/examples/21-deferred/vs_deferred_debug_line.sc b/3rdparty/bgfx/examples/21-deferred/vs_deferred_debug_line.sc index 906372cb0f8..abe24857eec 100644 --- a/3rdparty/bgfx/examples/21-deferred/vs_deferred_debug_line.sc +++ b/3rdparty/bgfx/examples/21-deferred/vs_deferred_debug_line.sc @@ -2,7 +2,7 @@ $input a_position, a_color0 $output v_color0 /* - * 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 */ diff --git a/3rdparty/bgfx/examples/21-deferred/vs_deferred_geom.sc b/3rdparty/bgfx/examples/21-deferred/vs_deferred_geom.sc index 0b466d598ef..13df0a7716a 100644 --- a/3rdparty/bgfx/examples/21-deferred/vs_deferred_geom.sc +++ b/3rdparty/bgfx/examples/21-deferred/vs_deferred_geom.sc @@ -2,7 +2,7 @@ $input a_position, a_normal, a_tangent, a_texcoord0 $output v_wpos, v_view, v_normal, v_tangent, v_bitangent, v_texcoord0 /* - * 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 */ diff --git a/3rdparty/bgfx/examples/21-deferred/vs_deferred_light.sc b/3rdparty/bgfx/examples/21-deferred/vs_deferred_light.sc index 8c3b7a822b6..33cba4f17cc 100644 --- a/3rdparty/bgfx/examples/21-deferred/vs_deferred_light.sc +++ b/3rdparty/bgfx/examples/21-deferred/vs_deferred_light.sc @@ -2,7 +2,7 @@ $input a_position, a_texcoord0 $output v_texcoord0 /* - * 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 */ |