Browse Source

Fix 21-deferred with texture array frame buffer (#2646)

pezcode 4 years ago
parent
commit
d32d3b19b6

+ 48 - 7
examples/21-deferred/deferred.cpp

@@ -275,6 +275,7 @@ public:
 		u_mtx            = bgfx::createUniform("u_mtx",            bgfx::UniformType::Mat4);
 		u_mtx            = bgfx::createUniform("u_mtx",            bgfx::UniformType::Mat4);
 		u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Vec4);
 		u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Vec4);
 		u_lightRgbInnerR = bgfx::createUniform("u_lightRgbInnerR", bgfx::UniformType::Vec4);
 		u_lightRgbInnerR = bgfx::createUniform("u_lightRgbInnerR", bgfx::UniformType::Vec4);
+		u_layer          = bgfx::createUniform("u_layer",          bgfx::UniformType::Vec4);
 
 
 		// Create program from shaders.
 		// Create program from shaders.
 		m_geomProgram    = loadProgram("vs_deferred_geom",       "fs_deferred_geom");
 		m_geomProgram    = loadProgram("vs_deferred_geom",       "fs_deferred_geom");
@@ -288,11 +289,15 @@ public:
 
 
 		if (0 != (BGFX_CAPS_TEXTURE_2D_ARRAY & bgfx::getCaps()->supported) )
 		if (0 != (BGFX_CAPS_TEXTURE_2D_ARRAY & bgfx::getCaps()->supported) )
 		{
 		{
-			m_lightTaProgram = loadProgram("vs_deferred_light", "fs_deferred_light_ta");
+			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
 		else
 		{
 		{
-			m_lightTaProgram = BGFX_INVALID_HANDLE;
+			m_lightTaProgram   = BGFX_INVALID_HANDLE;
+			m_combineTaProgram = BGFX_INVALID_HANDLE;
+			m_debugTaProgram   = BGFX_INVALID_HANDLE;
 		}
 		}
 
 
 		if (0 != (BGFX_CAPS_IMAGE_RW & bgfx::getCaps()->supported)
 		if (0 != (BGFX_CAPS_IMAGE_RW & bgfx::getCaps()->supported)
@@ -388,7 +393,19 @@ public:
 		}
 		}
 
 
 		bgfx::destroy(m_combineProgram);
 		bgfx::destroy(m_combineProgram);
+
+		if (bgfx::isValid(m_combineTaProgram) )
+		{
+			bgfx::destroy(m_combineTaProgram);
+		}
+
 		bgfx::destroy(m_debugProgram);
 		bgfx::destroy(m_debugProgram);
+
+		if (bgfx::isValid(m_debugTaProgram) )
+		{
+			bgfx::destroy(m_debugTaProgram);
+		}
+
 		bgfx::destroy(m_lineProgram);
 		bgfx::destroy(m_lineProgram);
 
 
 		bgfx::destroy(m_textureColor);
 		bgfx::destroy(m_textureColor);
@@ -819,21 +836,30 @@ public:
 				}
 				}
 
 
 				// Combine color and light buffers.
 				// Combine color and light buffers.
-				bgfx::setTexture(0, s_albedo, m_gbufferTex[0]);
+				bgfx::setTexture(0, s_albedo, bgfx::getTexture(m_gbuffer, 0) );
 				bgfx::setTexture(1, s_light,  m_lightBufferTex);
 				bgfx::setTexture(1, s_light,  m_lightBufferTex);
 				bgfx::setState(0
 				bgfx::setState(0
 					| BGFX_STATE_WRITE_RGB
 					| BGFX_STATE_WRITE_RGB
 					| BGFX_STATE_WRITE_A
 					| BGFX_STATE_WRITE_A
 					);
 					);
 				screenSpaceQuad( (float)m_width, (float)m_height, s_texelHalf, m_caps->originBottomLeft);
 				screenSpaceQuad( (float)m_width, (float)m_height, s_texelHalf, m_caps->originBottomLeft);
-				bgfx::submit(kRenderPassCombine, m_combineProgram);
+
+				if (bgfx::isValid(m_lightTaProgram)
+				&&  m_useTArray)
+				{
+					bgfx::submit(kRenderPassCombine, m_combineTaProgram);
+				}
+				else
+				{
+					bgfx::submit(kRenderPassCombine, m_combineProgram);
+				}
 
 
 				if (m_showGBuffer)
 				if (m_showGBuffer)
 				{
 				{
 					const float aspectRatio = float(m_width)/float(m_height);
 					const float aspectRatio = float(m_width)/float(m_height);
 
 
 					// Draw m_debug m_gbuffer.
 					// Draw m_debug m_gbuffer.
-					for (uint32_t ii = 0; ii < BX_COUNTOF(m_gbufferTex); ++ii)
+					for (uint8_t ii = 0; ii < BX_COUNTOF(m_gbufferTex); ++ii)
 					{
 					{
 						float mtx[16];
 						float mtx[16];
 						bx::mtxSRT(mtx
 						bx::mtxSRT(mtx
@@ -845,9 +871,21 @@ public:
 						bgfx::setTransform(mtx);
 						bgfx::setTransform(mtx);
 						bgfx::setVertexBuffer(0, m_vbh);
 						bgfx::setVertexBuffer(0, m_vbh);
 						bgfx::setIndexBuffer(m_ibh, 0, 6);
 						bgfx::setIndexBuffer(m_ibh, 0, 6);
-						bgfx::setTexture(0, s_texColor, m_gbufferTex[ii]);
+						bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_gbuffer, ii) );
 						bgfx::setState(BGFX_STATE_WRITE_RGB);
 						bgfx::setState(BGFX_STATE_WRITE_RGB);
-						bgfx::submit(kRenderPassDebugGBuffer, m_debugProgram);
+
+						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);
+						}
 					}
 					}
 				}
 				}
 			}
 			}
@@ -877,6 +915,7 @@ public:
 	bgfx::UniformHandle u_mtx;
 	bgfx::UniformHandle u_mtx;
 	bgfx::UniformHandle u_lightPosRadius;
 	bgfx::UniformHandle u_lightPosRadius;
 	bgfx::UniformHandle u_lightRgbInnerR;
 	bgfx::UniformHandle u_lightRgbInnerR;
+	bgfx::UniformHandle u_layer;
 
 
 	bgfx::ProgramHandle m_geomProgram;
 	bgfx::ProgramHandle m_geomProgram;
 	bgfx::ProgramHandle m_lightProgram;
 	bgfx::ProgramHandle m_lightProgram;
@@ -884,7 +923,9 @@ public:
 	bgfx::ProgramHandle m_lightUavProgram;
 	bgfx::ProgramHandle m_lightUavProgram;
 	bgfx::ProgramHandle m_clearUavProgram;
 	bgfx::ProgramHandle m_clearUavProgram;
 	bgfx::ProgramHandle m_combineProgram;
 	bgfx::ProgramHandle m_combineProgram;
+	bgfx::ProgramHandle m_combineTaProgram;
 	bgfx::ProgramHandle m_debugProgram;
 	bgfx::ProgramHandle m_debugProgram;
+	bgfx::ProgramHandle m_debugTaProgram;
 	bgfx::ProgramHandle m_lineProgram;
 	bgfx::ProgramHandle m_lineProgram;
 	bgfx::TextureHandle m_textureColor;
 	bgfx::TextureHandle m_textureColor;
 	bgfx::TextureHandle m_textureNormal;
 	bgfx::TextureHandle m_textureNormal;

+ 1 - 1
examples/21-deferred/fs_deferred_clear_uav.sc

@@ -12,5 +12,5 @@ IMAGE2D_RW(i_light, rgba8, 2);
 void main()
 void main()
 {
 {
     ivec2 coord = ivec2(gl_FragCoord.xy);
     ivec2 coord = ivec2(gl_FragCoord.xy);
-    imageStore(i_light, coord, vec4(0.0, 0.0, 0.0, 0.0));
+    imageStore(i_light, coord, vec4(0.0, 0.0, 0.0, 0.0) );
 }
 }

+ 18 - 0
examples/21-deferred/fs_deferred_combine_ta.sc

@@ -0,0 +1,18 @@
+$input v_texcoord0
+
+/*
+ * Copyright 2011-2021 Branimir Karadzic. All rights reserved.
+ * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
+ */
+
+#include "../common/common.sh"
+
+SAMPLER2DARRAY(s_albedo, 0);
+SAMPLER2D(s_light,  1);
+
+void main()
+{
+	vec4 albedo  = toLinear(texture2DArray(s_albedo, vec3(v_texcoord0, 0.0) ) );
+	vec4 light   = toLinear(texture2D(s_light,  v_texcoord0) );
+	gl_FragColor = toGamma(albedo*light);
+}

+ 17 - 0
examples/21-deferred/fs_deferred_debug_ta.sc

@@ -0,0 +1,17 @@
+$input v_texcoord0
+
+/*
+ * Copyright 2011-2021 Branimir Karadzic. All rights reserved.
+ * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
+ */
+
+#include "../common/common.sh"
+
+SAMPLER2DARRAY(s_texColor, 0);
+
+uniform vec4 u_layer;
+
+void main()
+{
+	gl_FragColor = texture2DArray(s_texColor, vec3(v_texcoord0, u_layer.x) );
+}