Browse Source

GR interface: More bug fixes and optimizations

Panagiotis Christopoulos Charitos 9 years ago
parent
commit
843d604365

+ 3 - 1
shaders/Irradiance.frag.glsl

@@ -7,6 +7,8 @@
 
 #include "shaders/Common.glsl"
 
+const float INDIRECT_BUMP = 2.5; // A sort of hack
+
 layout(location = 0) in vec2 in_uv;
 layout(location = 0) out vec3 out_color;
 
@@ -53,7 +55,7 @@ void main()
 				vec3 col = texture(u_envTex, vec4(r, texArrIdx)).rgb;
 
 				float lambert = max(0.0, dot(r, ri));
-				outCol += col * lambert;
+				outCol += col * lambert * INDIRECT_BUMP;
 				weight += lambert;
 			}
 		}

+ 2 - 3
src/anki/core/Trace.cpp

@@ -44,9 +44,8 @@ static Array<const char*, U(TraceCounterType::COUNT)> counterNames = {{"GR_DRAWC
 	"GR_DYNAMIC_UNIFORMS_SIZE",
 	"GR_DYNAMIC_STORAGE_SIZE",
 	"GR_VERTICES",
-	"GR_PIPELINES_CREATED",
-	"GR_PIPELINE_BINDS_SKIPPED",
-	"GR_PIPELINE_BINDS_HAPPENED",
+	"GL_PROGS_SKIPPED",
+	"VK_PIPELINES_CREATED",
 	"VK_PIPELINE_BARRIERS",
 	"VK_CMD_BUFFER_CREATE",
 	"VK_FENCE_CREATE",

+ 2 - 3
src/anki/core/Trace.h

@@ -62,9 +62,8 @@ enum class TraceCounterType
 	GR_DYNAMIC_UNIFORMS_SIZE,
 	GR_DYNAMIC_STORAGE_SIZE,
 	GR_VERTICES,
-	GR_PIPELINES_CREATED,
-	GR_PIPELINE_BINDS_SKIPPED,
-	GR_PIPELINE_BINDS_HAPPENED,
+	GL_PROGS_SKIPPED,
+	VK_PIPELINES_CREATED,
 	VK_PIPELINE_BARRIERS,
 	VK_CMD_BUFFER_CREATE,
 	VK_FENCE_CREATE,

+ 19 - 5
src/anki/gr/gl/CommandBuffer.cpp

@@ -621,25 +621,35 @@ void CommandBuffer::bindTexture(U32 set, U32 binding, TexturePtr tex, DepthStenc
 	public:
 		U32 m_unit;
 		TexturePtr m_tex;
+		Bool8 m_samplerChanged;
 
-		Cmd(U32 unit, TexturePtr tex)
+		Cmd(U32 unit, TexturePtr tex, Bool samplerChanged)
 			: m_unit(unit)
 			, m_tex(tex)
+			, m_samplerChanged(samplerChanged)
 		{
 		}
 
 		Error operator()(GlState&)
 		{
-			glBindTextureUnit(m_unit, m_tex->m_impl->getGlName());
-			glBindSampler(m_unit, 0);
+			if(m_tex)
+			{
+				glBindTextureUnit(m_unit, m_tex->m_impl->getGlName());
+			}
+
+			if(m_samplerChanged)
+			{
+				glBindSampler(m_unit, 0);
+			}
 			return ErrorCode::NONE;
 		}
 	};
 
-	if(m_impl->m_state.bindTexture(set, binding, tex, aspect))
+	Bool texChanged, samplerChanged;
+	if(m_impl->m_state.bindTexture(set, binding, tex, aspect, texChanged, samplerChanged))
 	{
 		U unit = binding + MAX_TEXTURE_BINDINGS * set;
-		m_impl->pushBackNewCommand<Cmd>(unit, tex);
+		m_impl->pushBackNewCommand<Cmd>(unit, (texChanged) ? tex : TexturePtr(), samplerChanged);
 	}
 }
 
@@ -878,6 +888,10 @@ void CommandBuffer::bindShaderProgram(ShaderProgramPtr prog)
 	{
 		m_impl->pushBackNewCommand<Cmd>(prog);
 	}
+	else
+	{
+		ANKI_TRACE_INC_COUNTER(GL_PROGS_SKIPPED, 1);
+	}
 }
 
 void CommandBuffer::beginRenderPass(FramebufferPtr fb)

+ 24 - 8
src/anki/gr/gl/StateTracker.h

@@ -131,7 +131,7 @@ public:
 		ANKI_ASSERT(minx != MAX_U16 && miny != MAX_U16 && maxx != MAX_U16 && maxy != MAX_U16);
 		if(m_viewport[0] != minx || m_viewport[1] != miny || m_viewport[2] != maxx || m_viewport[3] != maxy)
 		{
-			m_viewport = {minx, miny, maxx, maxy};
+			m_viewport = {{minx, miny, maxx, maxy}};
 			return true;
 		}
 		return false;
@@ -373,13 +373,13 @@ public:
 
 	Bool maybeEnableBlend()
 	{
-		Bool8 enable = 0;
+		Bool enable = false;
 
 		for(U i = 0; i < m_fb->getColorBufferCount(); ++i)
 		{
 			if(!!(m_colorWriteMasks[i]) && (m_enableBlendMask & (1 << i)))
 			{
-				enable = enable || 1;
+				enable = true;
 			}
 		}
 
@@ -431,19 +431,35 @@ public:
 	{
 	public:
 		TextureImpl* m_tex = nullptr;
-		SamplerImpl* m_sampler = nullptr;
+		SamplerImpl* m_sampler = reinterpret_cast<SamplerImpl*>(0x1);
 		DepthStencilAspectMask m_aspect;
 	};
 
 	Array2d<TextureBinding, MAX_BOUND_RESOURCE_GROUPS, MAX_TEXTURE_BINDINGS> m_textures;
 
-	Bool bindTexture(U32 set, U32 binding, TexturePtr tex, DepthStencilAspectMask aspect)
+	Bool bindTexture(
+		U32 set, U32 binding, TexturePtr tex, DepthStencilAspectMask aspect, Bool& texChanged, Bool& samplerChanged)
 	{
 		TextureBinding& b = m_textures[set][binding];
-		b.m_tex = tex->m_impl.get();
-		b.m_sampler = nullptr;
+		TextureImpl* texi = tex->m_impl.get();
+
+		texChanged = false;
+		samplerChanged = false;
+
+		if(texi != b.m_tex)
+		{
+			b.m_tex = texi;
+			texChanged = true;
+		}
+
+		if(b.m_sampler != nullptr)
+		{
+			b.m_sampler = nullptr;
+			samplerChanged = true;
+		}
+
 		b.m_aspect = aspect;
-		return true;
+		return samplerChanged || texChanged;
 	}
 
 	Bool bindTextureAndSampler(U32 set, U32 binding, TexturePtr tex, SamplerPtr sampler, DepthStencilAspectMask aspect)

+ 10 - 7
src/anki/renderer/Clusterer.h

@@ -52,15 +52,18 @@ public:
 	}
 
 private:
+	class S
+	{
+	public:
+		U8 m_x;
+		U8 m_y;
+		U8 m_z;
+		U8 m_pad_;
+	};
+
 	union
 	{
-		struct
-		{
-			U8 m_x;
-			U8 m_y;
-			U8 m_z;
-			U8 m_pad_;
-		} m_v;
+		S m_v;
 		U32 m_u32;
 	};
 };

+ 3 - 0
src/anki/renderer/Ir.cpp

@@ -438,12 +438,14 @@ void Ir::runIs(RenderingContext& rctx, FrustumComponent& frc, U layer, U faceIdx
 	cmdb->bindTexture(0, 0, face.m_gbufferColorRts[0]);
 	cmdb->bindTexture(0, 1, face.m_gbufferColorRts[1]);
 	cmdb->bindTexture(0, 2, face.m_gbufferColorRts[2]);
+	cmdb->bindTexture(0, 3, face.m_gbufferDepthRt);
 
 	cmdb->setVertexAttribute(0, 0, PixelFormat(ComponentFormat::R32G32B32, TransformFormat::FLOAT), 0);
 
 	cmdb->setBlendMethods(0, BlendMethod::ONE, BlendMethod::ONE);
 	cmdb->setDepthCompareFunction(CompareOperation::GREATER);
 	cmdb->setDepthWrite(false);
+	cmdb->setCullMode(FaceSelectionMask::FRONT);
 
 	// Process all lights
 	const Mat4& vpMat = frc.getViewProjectionMatrix();
@@ -571,6 +573,7 @@ void Ir::runIs(RenderingContext& rctx, FrustumComponent& frc, U layer, U faceIdx
 	cmdb->setBlendMethods(0, BlendMethod::ONE, BlendMethod::ZERO);
 	cmdb->setDepthCompareFunction(CompareOperation::LESS);
 	cmdb->setDepthWrite(true);
+	cmdb->setCullMode(FaceSelectionMask::BACK);
 }
 
 void Ir::computeIrradiance(RenderingContext& rctx, U layer, U faceIdx)

+ 2 - 2
src/anki/scene/SoftwareRasterizer.cpp

@@ -17,11 +17,11 @@ void SoftwareRasterizer::prepare(const Mat4& mv, const Mat4& p, U width, U heigh
 	m_mvp = p * mv;
 
 	Array<Plane*, 6> planes = {
-		&m_planesL[0], &m_planesL[1], &m_planesL[2], &m_planesL[3], &m_planesL[4], &m_planesL[5]};
+		{&m_planesL[0], &m_planesL[1], &m_planesL[2], &m_planesL[3], &m_planesL[4], &m_planesL[5]}};
 	extractClipPlanes(p, planes);
 
 	Array<Plane*, 6> planes2 = {
-		&m_planesW[0], &m_planesW[1], &m_planesW[2], &m_planesW[3], &m_planesW[4], &m_planesW[5]};
+		{&m_planesW[0], &m_planesW[1], &m_planesW[2], &m_planesW[3], &m_planesW[4], &m_planesW[5]}};
 	extractClipPlanes(m_mvp, planes2);
 
 	// Reset z buffer

+ 18 - 0
src/anki/util/Thread.h

@@ -181,6 +181,24 @@ public:
 	/// Wait until all threads call wait().
 	Bool wait();
 
+private:
+	void* m_impl = nullptr;
+};
+
+/// Semaphore for thread synchronization.
+class Semaphore : public NonCopyable
+{
+public:
+	Semaphore(I32 initialValue);
+
+	~Semaphore();
+
+	/// Same as sem_wait().
+	void wait();
+
+	/// Same as sem_post().
+	void post();
+
 private:
 	void* m_impl = nullptr;
 };

+ 49 - 0
src/anki/util/ThreadPosix.cpp

@@ -8,6 +8,7 @@
 #include <cstring>
 #include <algorithm>
 #include <pthread.h>
+#include <semaphore.h>
 
 namespace anki
 {
@@ -300,4 +301,52 @@ Bool Barrier::wait()
 	return true;
 }
 
+Semaphore::Semaphore(I32 val)
+{
+	sem_t* sem = static_cast<sem_t*>(malloc(sizeof(sem_t)));
+	m_impl = sem;
+	if(m_impl == nullptr)
+	{
+		ANKI_LOGF("Out of memory");
+	}
+
+	if(sem_init(sem, 0, val))
+	{
+		ANKI_LOGF("sem_init() failed");
+	}
+}
+
+Semaphore::~Semaphore()
+{
+	if(m_impl)
+	{
+		sem_t* sem = static_cast<sem_t*>(m_impl);
+		if(sem_destroy(sem))
+		{
+			ANKI_LOGE("sem_destroy() failed");
+		}
+
+		free(m_impl);
+		m_impl = nullptr;
+	}
+}
+
+void Semaphore::wait()
+{
+	ANKI_ASSERT(m_impl);
+	if(ANKI_UNLIKELY(sem_wait(static_cast<sem_t*>(m_impl))))
+	{
+		ANKI_LOGF("sem_wait() failed");
+	}
+}
+
+void Semaphore::post()
+{
+	ANKI_ASSERT(m_impl);
+	if(ANKI_UNLIKELY(sem_post(static_cast<sem_t*>(m_impl))))
+	{
+		ANKI_LOGF("sem_post() failed");
+	}
+}
+
 } // end namespace anki

+ 17 - 1
tools/format_source.sh

@@ -1 +1,17 @@
-find ./src ./tests ./sandbox ./tools ./shaders ./samples -name '*.h' -o -name '*.hpp' -o -name '*.c' -o -name '*.cpp' -o -name '*.glsl' | xargs -I % ./thirdparty/bin/clang-format -sort-includes=false -i %
+#!/bin/bash
+
+files=(`find ./src ./tests ./sandbox ./tools ./shaders ./samples -name '*.h' -o -name '*.hpp' -o -name '*.c' -o -name '*.cpp' -o -name '*.glsl'`)
+
+filecount=${#files[@]}
+
+count=0
+for f in ${files[@]}
+do
+	echo -ne Formatting ${count}/${filecount}\\r
+	./thirdparty/bin/clang-format -sort-includes=false -i ${f}
+	count=$((${count}+1))
+done
+
+echo Done! Formatted ${filecount} files
+
+