Browse Source

Vulkan: Renderer's MS and IS work with vulkan

Panagiotis Christopoulos Charitos 9 years ago
parent
commit
d9d5d9a5a5

+ 1 - 1
samples/assets/scene.lua

@@ -52,7 +52,7 @@ node:setAll(1.5708, 1.0 / getMainRenderer():getAspectRatio() * 1.5708, 0.1, 100)
 trf = Transform.new()
 trf:setOrigin(Vec4.new(5.65394, -5.98675, 3.80237, 0))
 rot = Mat3x4.new()
-rot:setAll(1, 0, 0, 0, 0, 1, -4.63724e-05, 0, 0, 4.63724e-05, 1, 0)
+rot:setAll(1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 0, 0)
 trf:setRotation(rot)
 trf:setScale(1)
 node:getSceneNodeBase():getMoveComponent():setLocalTransform(trf)

+ 5 - 1
shaders/Final.frag.glsl

@@ -15,6 +15,10 @@ layout(location = 0) out vec3 out_color;
 
 void main()
 {
-	vec3 col = textureLod(u_tex, in_texCoord, 0.0).rgb;
+	vec2 uv = in_texCoord;
+#ifdef ANKI_VK
+	uv.y = 1.0 - uv.y;
+#endif
+	vec3 col = textureLod(u_tex, uv, 0.0).rgb;
 	out_color = col;
 }

+ 1 - 1
shaders/MsCommonVert.glsl

@@ -65,7 +65,7 @@ void writePositionAndUv(in mat4 mvp)
 #if TESSELLATION
 	gl_Position = vec4(in_position, 1.0);
 #else
-	gl_Position = mvp * vec4(in_position, 1.0);
+	ANKI_WRITE_POSITION(mvp * vec4(in_position, 1.0));
 #endif
 }
 

+ 1 - 1
shaders/Quad.vert.glsl

@@ -22,5 +22,5 @@ void main()
 
 	vec2 pos = POSITIONS[gl_VertexID];
 	out_uv = pos * 0.5 + (0.5 + UV_OFFSET);
-	gl_Position = vec4(pos, 0.0, 1.0);
+	ANKI_WRITE_POSITION(vec4(pos, 0.0, 1.0));
 }

+ 3 - 1
src/anki/gr/vulkan/GrManagerImpl.cpp

@@ -335,7 +335,7 @@ Error GrManagerImpl::initSwapchain(const GrManagerInitInfo& init)
 	VkColorSpaceKHR colorspace = VK_COLOR_SPACE_MAX_ENUM_KHR;
 	while(formatCount--)
 	{
-		if(formats[formatCount].format == VK_FORMAT_B8G8R8A8_SRGB)
+		if(formats[formatCount].format == VK_FORMAT_B8G8R8A8_UNORM)
 		{
 			m_surfaceFormat = formats[formatCount].format;
 			colorspace = formats[formatCount].colorSpace;
@@ -517,6 +517,8 @@ void GrManagerImpl::endFrame()
 		ANKI_VK_CHECKF(vkQueuePresentKHR(m_queue, &present));
 	}
 
+	m_transientMem.endFrame();
+
 	// Finalize
 	++m_frame;
 }

+ 1 - 1
src/anki/gr/vulkan/PipelineImpl.cpp

@@ -212,7 +212,7 @@ VkPipelineRasterizationStateCreateInfo* PipelineImpl::initRasterizerState(
 	ci.rasterizerDiscardEnable = VK_FALSE;
 	ci.polygonMode = convertFillMode(r.m_fillMode);
 	ci.cullMode = convertCullMode(r.m_cullMode);
-	ci.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
+	ci.frontFace = VK_FRONT_FACE_CLOCKWISE; // Use CW to workaround Vulkan's y flip
 	ci.depthBiasEnable = VK_FALSE; // TODO
 	ci.lineWidth = 1.0;
 

+ 1 - 1
src/anki/gr/vulkan/ShaderImpl.cpp

@@ -170,7 +170,7 @@ static const char* SHADER_HEADER = R"(#version 450 core
 #endif
 
 #if defined(VERTEX_SHADER)
-#define ANKI_WRITE_POSITION(x_) gl_Position = x_; gl_Position.y = -gl_Position.y
+#define ANKI_WRITE_POSITION(x_) gl_Position = x_;
 #endif
 
 %s)";

+ 27 - 0
src/anki/gr/vulkan/TransientMemoryManager.cpp

@@ -8,6 +8,7 @@
 #include <anki/gr/vulkan/GrManagerImpl.h>
 #include <anki/gr/vulkan/BufferImpl.h>
 #include <anki/core/Config.h>
+#include <anki/core/Trace.h>
 
 namespace anki
 {
@@ -97,4 +98,30 @@ void TransientMemoryManager::allocate(
 	}
 }
 
+void TransientMemoryManager::endFrame()
+{
+	for(TransientBufferType usage = TransientBufferType::UNIFORM; usage < TransientBufferType::COUNT; ++usage)
+	{
+		PerFrameBuffer& buff = m_perFrameBuffers[usage];
+
+		if(buff.m_mappedMem)
+		{
+			// Increase the counters
+			switch(usage)
+			{
+			case TransientBufferType::UNIFORM:
+				ANKI_TRACE_INC_COUNTER(GR_DYNAMIC_UNIFORMS_SIZE, buff.m_alloc.getUnallocatedMemorySize());
+				break;
+			case TransientBufferType::STORAGE:
+				ANKI_TRACE_INC_COUNTER(GR_DYNAMIC_STORAGE_SIZE, buff.m_alloc.getUnallocatedMemorySize());
+				break;
+			default:
+				break;
+			}
+
+			buff.m_alloc.endFrame();
+		}
+	}
+}
+
 } // end namespace anki

+ 2 - 2
src/anki/renderer/MainRenderer.cpp

@@ -84,14 +84,14 @@ Error MainRenderer::create(ThreadPool* threadpool,
 
 		ColorStateInfo colorState;
 		colorState.m_attachmentCount = 1;
+		colorState.m_attachments[0].m_format.m_components = ComponentFormat::DEFAULT_FRAMEBUFFER;
 		m_r->createDrawQuadPipeline(m_blitFrag->getGrShader(), colorState, m_blitPpline);
 
 		// Init RC group
 		ResourceGroupInitInfo rcinit;
 		rcinit.m_textures[0].m_texture = m_r->getPps().getRt();
 		m_rcGroup = m_r->getGrManager().newInstance<ResourceGroup>(rcinit);
-		ANKI_LOGI("The main renderer will have to blit the offscreen "
-				  "renderer's result");
+		ANKI_LOGI("The main renderer will have to blit the offscreen renderer's result");
 	}
 
 	ANKI_LOGI("Main renderer initialized. Rendering size %ux%u", m_width, m_height);

+ 10 - 15
tests/gr/Gr.cpp

@@ -57,9 +57,6 @@ void main()
 	vec2 pos = rot * POSITIONS[gl_VertexID % 3];
 
 	gl_Position = vec4(pos, 0.0, 1.0);
-#if defined(ANKI_VK)
-	gl_Position.y = -gl_Position.y;
-#endif
 })";
 
 static const char* VERT_INP_SRC = R"(
@@ -78,9 +75,6 @@ layout(location = 1) out vec3 out_color1;
 void main()
 {
 	gl_Position = vec4(in_position, 1.0);
-#if defined(ANKI_VK)
-	gl_Position.y = -gl_Position.y;
-#endif
 
 	out_color0 = in_color0;
 	out_color1 = in_color1;
@@ -100,10 +94,7 @@ void main()
 		vec2[](vec2(-1.0, 1.0), vec2(-1.0, -1.0), vec2(1.0, -1.0),
 		vec2(1.0, -1.0), vec2(1.0, 1.0), vec2(-1.0, 1.0));
 
-	gl_Position = vec4(POSITIONS[gl_VertexID], 0.0, 1.0);
-#if defined(ANKI_VK)
-	gl_Position.y = -gl_Position.y;
-#endif
+	ANKI_WRITE_POSITION(vec4(POSITIONS[gl_VertexID], 0.0, 1.0));
 	out_uv = POSITIONS[gl_VertexID] / 2.0 + 0.5;
 })";
 
@@ -228,9 +219,13 @@ layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_tex1;
 
 void main()
 {
-	float factor = in_uv.x;
-	vec3 col0 = texture(u_tex0, in_uv).rgb;
-	vec3 col1 = texture(u_tex1, in_uv).rgb;
+	vec2 uv = in_uv;
+#ifdef ANKI_VK
+	uv.y = 1.0 - uv.y;
+#endif
+	float factor = uv.x;
+	vec3 col0 = texture(u_tex0, uv).rgb;
+	vec3 col1 = texture(u_tex1, uv).rgb;
 	
 	out_color = vec4(col1 + col0, 1.0);
 })";
@@ -857,7 +852,7 @@ static void drawOffscreenDrawcalls(
 
 	TransientMemoryInfo transientInfo;
 
-	Mat4 modelMat(Vec4(0.0, 0.0, 0.0, 1.0), Mat3(Euler(ang, ang / 2.0f, ang / 3.0f)), 1.0f);
+	Mat4 modelMat(Vec4(-0.5, -0.5, 0.0, 1.0), Mat3(Euler(ang, ang / 2.0f, ang / 3.0f)), 1.0f);
 
 	Mat4* mvp = static_cast<Mat4*>(
 		gr.allocateFrameTransientMemory(sizeof(*mvp), BufferUsageBit::UNIFORM_ALL, transientInfo.m_uniformBuffers[0]));
@@ -874,7 +869,7 @@ static void drawOffscreenDrawcalls(
 	cmdb->drawElements(6 * 2 * 3);
 
 	// 2nd draw
-	modelMat = Mat4(Vec4(0.0, 0.0, 0.0, 1.0), Mat3(Euler(ang * 2.0, ang, ang / 3.0f * 2.0)), 1.0f);
+	modelMat = Mat4(Vec4(0.5, 0.5, 0.0, 1.0), Mat3(Euler(ang * 2.0, ang, ang / 3.0f * 2.0)), 1.0f);
 
 	mvp = static_cast<Mat4*>(
 		gr.allocateFrameTransientMemory(sizeof(*mvp), BufferUsageBit::UNIFORM_ALL, transientInfo.m_uniformBuffers[0]));