Browse Source

Adding downscale with blur pass

Panagiotis Christopoulos Charitos 10 years ago
parent
commit
b84075c84a

+ 1 - 0
include/anki/renderer/Common.h

@@ -26,6 +26,7 @@ class Dbg;
 class Tiler;
 class Ir;
 class Upsample;
+class DownscaleBlur;
 
 /// Computes the 'a' and 'b' numbers for linearizeDepthOptimal
 inline void computeLinearizeDepthOptimal(F32 near, F32 far, F32& a, F32& b)

+ 46 - 0
include/anki/renderer/DownscaleBlur.h

@@ -0,0 +1,46 @@
+// Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#pragma once
+
+#include <anki/renderer/RenderingPass.h>
+
+namespace anki
+{
+
+/// @addtogroup renderer
+/// @{
+
+/// Downsample the IS and blur it at the same time.
+class DownscaleBlur : public RenderingPass
+{
+anki_internal:
+	DownscaleBlur(Renderer* r)
+		: RenderingPass(r)
+	{
+	}
+
+	ANKI_USE_RESULT Error init(const ConfigSet& initializer);
+
+	void run(CommandBufferPtr& cmdb);
+
+private:
+	class Subpass
+	{
+	public:
+		ShaderResourcePtr m_vert;
+		ShaderResourcePtr m_frag;
+		PipelinePtr m_ppline;
+		ResourceGroupPtr m_rcGroup;
+		FramebufferPtr m_fb;
+	};
+
+	Array<Subpass, 3> m_passes;
+
+	Error initSubpass(U idx, const UVec2& inputTexSize);
+};
+/// @}
+
+} // end namespace anki

+ 1 - 0
include/anki/renderer/Renderer.h

@@ -333,6 +333,7 @@ private:
 	UniquePtr<Lf> m_lf; ///< Forward shading lens flares.
 	UniquePtr<Upsample> m_upsample;
 	UniquePtr<Tiler> m_tiler;
+	UniquePtr<DownscaleBlur> m_downscale;
 	UniquePtr<Tm> m_tm;
 	UniquePtr<Ssao> m_ssao;
 	UniquePtr<Bloom> m_bloom;

+ 1 - 1
shaders/Bloom.frag.glsl

@@ -7,7 +7,7 @@
 #include "shaders/Tonemapping.glsl"
 
 // Vars
-layout(binding = 0) uniform lowp sampler2D u_tex; ///< Its the IS RT
+layout(TEX_BINDING(0, 0)) uniform lowp sampler2D u_tex; ///< Its the IS RT
 
 layout(UBO_BINDING(0, 0), std140) uniform u0_
 {

+ 26 - 0
shaders/DownscaleBlur.frag.glsl

@@ -0,0 +1,26 @@
+// Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#include "shaders/Common.glsl"
+
+layout(TEX_BINDING(0, 0)) uniform sampler2D u_tex;
+
+layout(location = 0) in vec2 in_uv;
+layout(location = 0) out vec3 out_color;
+
+void main()
+{
+	const vec2 TEXEL_SIZE = 1.0 / TEXTURE_SIZE;
+
+	out_color = textureLod(u_tex, in_uv, TEXTURE_MIPMAP).rgb;
+	out_color += textureLod(u_tex, in_uv + TEXEL_SIZE, TEXTURE_MIPMAP).rgb;
+	out_color += textureLod(u_tex, in_uv - TEXEL_SIZE, TEXTURE_MIPMAP).rgb;
+	out_color += textureLod(
+		u_tex, in_uv + vec2(TEXEL_SIZE.x, -TEXEL_SIZE.y), TEXTURE_MIPMAP).rgb;
+	out_color += textureLod(
+		u_tex, in_uv + vec2(-TEXEL_SIZE.x, TEXEL_SIZE.y), TEXTURE_MIPMAP).rgb;
+
+	out_color /= 5.0;
+}

+ 1 - 2
shaders/Pps.frag.glsl

@@ -168,9 +168,8 @@ void main()
 	out_color = colorGrading(out_color);
 
 #if 0
-	if(out_color.x != 0.0000001)
 	{
-		out_color = vec3(ssao);
+		out_color = textureLod(u_isRt, in_uv, 3.0).rgb;
 	}
 #endif
 }

+ 1 - 2
src/gr/gl/CommandBuffer.cpp

@@ -609,8 +609,7 @@ void CommandBuffer::setBufferMemoryBarrier(
 			| GL_COMMAND_BARRIER_BIT;
 	}
 
-	if((c & ResourceAccessBit::UNIFORM_READ)
-		!= ResourceAccessBit::NONE)
+	if((c & ResourceAccessBit::UNIFORM_READ) != ResourceAccessBit::NONE)
 	{
 		d |= GL_UNIFORM_BARRIER_BIT;
 	}

+ 102 - 0
src/renderer/DownscaleBlur.cpp

@@ -0,0 +1,102 @@
+// Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#include <anki/renderer/DownscaleBlur.h>
+#include <anki/renderer/Is.h>
+#include <anki/renderer/Renderer.h>
+
+namespace anki
+{
+
+//==============================================================================
+Error DownscaleBlur::initSubpass(U idx, const UVec2& inputTexSize)
+{
+	Subpass& pass = m_passes[idx];
+
+	PipelineInitializer ppinit;
+	ppinit.m_color.m_attachmentCount = 1;
+	ppinit.m_color.m_attachments[0].m_format = Is::RT_PIXEL_FORMAT;
+	ppinit.m_depthStencil.m_depthWriteEnabled = false;
+	ppinit.m_depthStencil.m_depthCompareFunction = CompareOperation::ALWAYS;
+
+	StringAuto pps(getAllocator());
+
+	// vert shader
+	pps.sprintf("#define UV_OFFSET vec2(%f, %f)\n",
+		1.0 / inputTexSize.x(),
+		1.0 / inputTexSize.y());
+
+	ANKI_CHECK(getResourceManager().loadResourceToCache(
+		pass.m_vert, "shaders/Quad.vert.glsl", pps.toCString(), "r_"));
+
+	ppinit.m_shaders[ShaderType::VERTEX] = pass.m_vert->getGrShader();
+
+	// frag shader
+	pps.destroy();
+	pps.sprintf("#define TEXTURE_SIZE vec2(%f, %f)\n"
+				"#define TEXTURE_MIPMAP float(%u)\n",
+		F32(inputTexSize.x()),
+		F32(inputTexSize.y()),
+		idx);
+
+	ANKI_CHECK(getResourceManager().loadResourceToCache(pass.m_frag,
+		"shaders/DownscaleBlur.frag.glsl",
+		pps.toCString(),
+		"r_"));
+
+	ppinit.m_shaders[ShaderType::FRAGMENT] = pass.m_frag->getGrShader();
+
+	// ppline
+	pass.m_ppline = getGrManager().newInstance<Pipeline>(ppinit);
+
+	// FB
+	FramebufferInitializer fbInit;
+	fbInit.m_colorAttachmentsCount = 1;
+	fbInit.m_colorAttachments[0].m_texture = m_r->getIs().getRt();
+	fbInit.m_colorAttachments[0].m_loadOperation =
+		AttachmentLoadOperation::DONT_CARE;
+	fbInit.m_colorAttachments[0].m_mipmap = idx + 1;
+	pass.m_fb = getGrManager().newInstance<Framebuffer>(fbInit);
+
+	// Resources
+	ResourceGroupInitializer rcinit;
+	rcinit.m_textures[0].m_texture = m_r->getIs().getRt();
+	pass.m_rcGroup = getGrManager().newInstance<ResourceGroup>(rcinit);
+
+	return ErrorCode::NONE;
+}
+
+//==============================================================================
+Error DownscaleBlur::init(const ConfigSet& initializer)
+{
+	UVec2 size(m_r->getWidth(), m_r->getHeight());
+	for(U i = 0; i < m_passes.getSize(); ++i)
+	{
+		ANKI_CHECK(initSubpass(i, size));
+		size /= 2;
+	}
+
+	return ErrorCode::NONE;
+}
+
+//==============================================================================
+void DownscaleBlur::run(CommandBufferPtr& cmdb)
+{
+	UVec2 size(m_r->getWidth(), m_r->getHeight());
+	for(U i = 0; i < m_passes.getSize(); ++i)
+	{
+		size /= 2;
+		Subpass& pass = m_passes[i];
+
+		cmdb->bindFramebuffer(pass.m_fb);
+		cmdb->setViewport(0, 0, size.x(), size.y());
+		cmdb->bindPipeline(pass.m_ppline);
+		cmdb->bindResourceGroup(pass.m_rcGroup, 0, nullptr);
+
+		m_r->drawQuad(cmdb);
+	}
+}
+
+} // end namespace anki

+ 12 - 0
src/renderer/Renderer.cpp

@@ -22,6 +22,7 @@
 #include <anki/renderer/Dbg.h>
 #include <anki/renderer/Tiler.h>
 #include <anki/renderer/Upsample.h>
+#include <anki/renderer/DownscaleBlur.h>
 
 namespace anki
 {
@@ -164,6 +165,12 @@ Error Renderer::initInternal(const ConfigSet& config)
 		ANKI_CHECK(m_tm->create(config));
 	}
 
+	if(config.getNumber("tm.enabled") && config.getNumber("pps.enabled"))
+	{
+		m_downscale.reset(getAllocator().newInstance<DownscaleBlur>(this));
+		ANKI_CHECK(m_downscale->init(config));
+	}
+
 	if(config.getNumber("ssao.enabled") && config.getNumber("pps.enabled"))
 	{
 		m_ssao.reset(m_alloc.newInstance<Ssao>(this));
@@ -259,6 +266,11 @@ Error Renderer::render(
 
 	cmdb->generateMipmaps(m_is->getRt());
 
+	if(m_downscale)
+	{
+		m_downscale->run(cmdb);
+	}
+
 	if(m_tm)
 	{
 		m_tm->run(cmdb);