From 82550fa8420563f75beefc54aabdd1c868cd102c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Thu, 7 Feb 2019 18:04:19 -0800 Subject: [PATCH] 21-deferred: Added texture array frame buffer option. --- examples/21-deferred/deferred.cpp | 85 +++++++++++++--- examples/21-deferred/fs_deferred_light_ta.sc | 91 ++++++++++++++++++ .../shaders/dx11/fs_deferred_light_ta.bin | Bin 0 -> 1425 bytes .../shaders/glsl/fs_deferred_light_ta.bin | Bin 0 -> 1808 bytes .../shaders/metal/fs_deferred_light_ta.bin | Bin 0 -> 1538 bytes 5 files changed, 160 insertions(+), 16 deletions(-) create mode 100644 examples/21-deferred/fs_deferred_light_ta.sc create mode 100644 examples/runtime/shaders/dx11/fs_deferred_light_ta.bin create mode 100644 examples/runtime/shaders/glsl/fs_deferred_light_ta.bin create mode 100644 examples/runtime/shaders/metal/fs_deferred_light_ta.bin diff --git a/examples/21-deferred/deferred.cpp b/examples/21-deferred/deferred.cpp index a917cbe95..f38ea146c 100644 --- a/examples/21-deferred/deferred.cpp +++ b/examples/21-deferred/deferred.cpp @@ -281,6 +281,17 @@ public: m_debugProgram = loadProgram("vs_deferred_debug", "fs_deferred_debug"); m_lineProgram = loadProgram("vs_deferred_debug_line", "fs_deferred_debug_line"); + m_useTArray = 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; + } + // Load diffuse texture. m_textureColor = loadTexture("textures/fieldstone-rgba.dds"); @@ -290,8 +301,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(); @@ -337,6 +348,12 @@ public: bgfx::destroy(m_geomProgram); bgfx::destroy(m_lightProgram); + + if (bgfx::isValid(m_lightTaProgram) ) + { + bgfx::destroy(m_lightTaProgram); + } + bgfx::destroy(m_combineProgram); bgfx::destroy(m_debugProgram); bgfx::destroy(m_lineProgram); @@ -402,19 +419,24 @@ 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 || !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; 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 @@ -425,10 +447,27 @@ public: | 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); + + bgfx::Attachment at[3]; + + if (m_useTArray) + { + m_gbufferTex[0] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 2, bgfx::TextureFormat::BGRA8, tsFlags); + at[0].init(m_gbufferTex[0], bgfx::Access::Write, 0); + at[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, tsFlags); + m_gbufferTex[1] = bgfx::createTexture2D(uint16_t(m_width), uint16_t(m_height), false, 1, bgfx::TextureFormat::BGRA8, tsFlags); + at[0].init(m_gbufferTex[0]); + at[1].init(m_gbufferTex[1]); + } + 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); + at[2].init(m_gbufferTex[2]); + + m_gbuffer = bgfx::createFrameBuffer(BX_COUNTOF(at), at, true); if (bgfx::isValid(m_lightBuffer) ) { @@ -454,6 +493,16 @@ 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."); + } + ImGui::Checkbox("Animate mesh.", &m_animateMesh); ImGui::SliderFloat("Anim.speed", &m_lightAnimationSpeed, 0.0f, 0.4f); @@ -668,7 +717,7 @@ public: | BGFX_STATE_BLEND_ADD ); screenSpaceQuad( (float)m_width, (float)m_height, s_texelHalf, m_caps->originBottomLeft); - bgfx::submit(RENDER_PASS_LIGHT_ID, m_lightProgram); + bgfx::submit(RENDER_PASS_LIGHT_ID, bgfx::isValid(m_lightTaProgram) && m_useTArray ? m_lightTaProgram : m_lightProgram); } } @@ -691,10 +740,10 @@ 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); @@ -734,6 +783,7 @@ public: bgfx::ProgramHandle m_geomProgram; bgfx::ProgramHandle m_lightProgram; + bgfx::ProgramHandle m_lightTaProgram; bgfx::ProgramHandle m_combineProgram; bgfx::ProgramHandle m_debugProgram; bgfx::ProgramHandle m_lineProgram; @@ -753,6 +803,9 @@ public: uint32_t m_oldHeight; uint32_t m_oldReset; + bool m_useTArray; + bool m_oldUseTArray; + int32_t m_scrollArea; int32_t m_numLights; float m_lightAnimationSpeed; diff --git a/examples/21-deferred/fs_deferred_light_ta.sc b/examples/21-deferred/fs_deferred_light_ta.sc new file mode 100644 index 000000000..91180c0d0 --- /dev/null +++ b/examples/21-deferred/fs_deferred_light_ta.sc @@ -0,0 +1,91 @@ +$input v_texcoord0 + +/* + * Copyright 2011-2019 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause + */ + +#include "../common/common.sh" + +SAMPLER2DARRAY(s_normal, 0); +SAMPLER2D(s_depth, 1); + +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(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_HLSL || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_METAL + clip.y = -clip.y; +#endif // BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_METAL + 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); + gl_FragColor.xyz = toGamma(lightColor.xyz); + gl_FragColor.w = 1.0; +} diff --git a/examples/runtime/shaders/dx11/fs_deferred_light_ta.bin b/examples/runtime/shaders/dx11/fs_deferred_light_ta.bin new file mode 100644 index 0000000000000000000000000000000000000000..fc51f342422cd21802d19e95bb915ef8f21ab68c GIT binary patch literal 1425 zcmZuxO=}ZT6up^b@&&d-aA7wx3s)kfWFfUkI&G7*L1;pf)+k8QlF$^|q~rs-k{DdO zaOp-|=MRV=`a}EyE?jlt+H>AVzU+lN^WM4lo_Ft=#}ulE(cZ)CbAh?yjT)C1=bd4> zH>jMpE=Ge?NF+3aE9Ym$Znxd9q(UBGchtBV-rN^oA`%}oy1o9@>E))Xhz%O8_Vuu{ z844lskt>p`@9n?2aX-EM^EiC(`>!t_)1RLqRmH-1KY_o8e}xyT2S;VR9e5nR1*b8e zxwLh7PObEVxQb&@J!zCn)nct!I+9wxzF#U;ax$GxOW@@>%pXN>zZPr@0GGK#dp zaqH{c4#wu(8H|__jTKCu6Cj{l(JO2mwHp5rp=*O)iCff?`^5Fey9T#?b5`&tnM1QU z&y8AaP1olaxNIMVw>rkTdB$B_kW6tgUI3S|`0PxB*|Tv)WKAU2x92;$=I{EfHn9+! z&ouEZH!EY4mHL9=!Ci4vh_Rn-lk)|KA+cm9eKd zGh^0PPhxup3k67$@y?R&n1Qv_U0GAVc}J*;ikTcIP#Jt$7l$i#Kj!zu^Cd<-Xw=AZ zaF71uQnW+#5LYo{9g9H*?W_Q1Xc@R<98edhMMF8t}rJMNq_sDwN7vY W-?s11btcB|Ju#ToE31A>P5B4jCy2`c literal 0 HcmV?d00001 diff --git a/examples/runtime/shaders/glsl/fs_deferred_light_ta.bin b/examples/runtime/shaders/glsl/fs_deferred_light_ta.bin new file mode 100644 index 0000000000000000000000000000000000000000..d67f0098fd0d9ad3f20caaef4901bf3ce5942606 GIT binary patch literal 1808 zcmZux+isgc5G7UXRy_3!s(pmWCM@7fl{!+@Dr(;<_c4kPF@zv-aWUA~RX?PE+u6%3 zOHG`I-DPI>oHJ+k{^z$z_2KT50e=%?(uRjDIljI!j4R{aJ}j%ch;mrN|J!{SC%fiF z?B2Iwo~19%k7~b(;;h}j;z4-4NuR!#Wm0d%@tZa*nogAcJ28wyRG+dkC5L3|kwe%d z-L|Ug*u6E|GJA$gNf9*x5or3ueN^o7r1rjkuIuPT5Z_1ueB)sIPjD40I0XGbAIBpU z{y~HU1ypX$LzTsVGAoH?o4-xMxcHrQYuU_PwiY0(~p zS{8`Zv~_~yEo47c7uimCI+JVSIj&7+>`I$e(Si9|Ss%iAWevt_kwc1OL%^kFHImCwClnqu|{z_o`&wuVCFEK+D0mty1S5ZTfI8EH}BdZ~o zP(nJm_a{_1vJjCYxzT~yT*%wzFgxHP3qlF>?pNJdan&d_R?0>z+a@=7j~ed>c8|@H zXNZP{Szt2l#GVpRXgX>=Iv5HHn8fRnofB-yN@I^q^g*C(5_Hs_LQN@l&qFP#ZgvW8 zhvdTwa2y*wK@&#az`&{w=%ecF;`u~yqQ;#4l9+&73U za5^>wU~!L$+iTeeFtkO~5sPb$=N>PNyFkn2KLT?Cc&Kl*rk`t5q|2;`D2>KKY6hfr2qf` literal 0 HcmV?d00001 diff --git a/examples/runtime/shaders/metal/fs_deferred_light_ta.bin b/examples/runtime/shaders/metal/fs_deferred_light_ta.bin new file mode 100644 index 0000000000000000000000000000000000000000..30b113de58859cf4c4529e7bcbe57995a366391b GIT binary patch literal 1538 zcmaJ>&uiN-6n0?k!KYDr9)iJ;i|g8nn!_;-p)hAC{h;zW06aJ?ZxG`?35mzH#7r=NwfeSM_Ph;KM8d0-p_`m3%1NnGp3TPY&R%AD zdZGV<<=AzrG7}3VXwJ$NO&Bt}My^|`qDmAZ-?*Gn?!LGPo;jCP#dVA<$5G1&rhTT9 zX!@`9E#-rC0}4qPxxXNKB1NgFP)$Tc`cWT!MlX7;VODdh?i9bHnIN)KdkoQRF|WyO zK^I@Cq9K}063JzOLvISC{sWSUHbxgc5yk36$|BVORmlp>Wg>PMbHL9TkUXH$p5yjf zCJ=ll6OguGBoouAkG4viOn*Yq3Y=8nHml}y2&x162)L-KVBM6^qM*+=rW9jT5+RG6 za&T`v4*hqYDO;&!%P}&*CCyiy6? zB5^kaE6zmF&h;7U2BC+}ZSA`_g4tdtk$4!vKAWe&936DlbPCp>LG7b+GkEqvm*E>& z{EXuQD?1gzv5 z(^-k}KJe~@G{B&Zmg@RC;ZP84$2qNW7=%7brNSV}cEL`hoo`i|Zs{Fl+A0OUKB_XI za3L#^;x_Y~j|@khP!INKH%L>NOR1KnVym~5uMIsP zaV8dOX%W%IuIjIg0BMGZ4gWBRFU_Yv2#3+IA76E&{?)KQxVqe{My&4^-A-m-3~>fS z-2~IH@jRgIJ`n11Gs)%*KvM3e*wZZ B<97f6 literal 0 HcmV?d00001