Selaa lähdekoodia

Make the cornell box work with rasterization

Panagiotis Christopoulos Charitos 5 vuotta sitten
vanhempi
sitoutus
a0a1db1ca1
5 muutettua tiedostoa jossa 62 lisäystä ja 34 poistoa
  1. 2 2
      src/anki/gr/Enums.h
  2. 1 1
      src/anki/gr/GrObject.h
  3. 3 2
      src/anki/gr/RenderGraph.h
  4. 6 6
      src/anki/gr/vulkan/Common.cpp
  5. 50 23
      tests/gr/Gr.cpp

+ 2 - 2
src/anki/gr/Enums.h

@@ -14,7 +14,7 @@ namespace anki
 
 /// @addtogroup graphics
 /// @{
-enum ColorBit : U8
+enum class ColorBit : U8
 {
 	NONE = 0,
 	RED = 1 << 0,
@@ -25,7 +25,7 @@ enum ColorBit : U8
 };
 ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(ColorBit, inline)
 
-enum PrimitiveTopology : U8
+enum class PrimitiveTopology : U8
 {
 	POINTS,
 	LINES,

+ 1 - 1
src/anki/gr/GrObject.h

@@ -16,7 +16,7 @@ namespace anki
 /// @{
 
 /// Graphics object type.
-enum GrObjectType : U8
+enum class GrObjectType : U8
 {
 	BUFFER,
 	COMMAND_BUFFER,

+ 3 - 2
src/anki/gr/RenderGraph.h

@@ -526,8 +526,9 @@ private:
 		TextureInitInfo m_initInfo;
 		U64 m_hash = 0;
 		TexturePtr m_importedTex;
-		TextureUsageBit m_importedLastKnownUsage;
-		TextureUsageBit m_usageDerivedByDeps; ///< Derived by the deps of this RT and will be used to set its usage.
+		TextureUsageBit m_importedLastKnownUsage = TextureUsageBit::NONE;
+		/// Derived by the deps of this RT and will be used to set its usage.
+		TextureUsageBit m_usageDerivedByDeps = TextureUsageBit::NONE;
 		Bool m_importedAndUndefinedUsage = false;
 	};
 

+ 6 - 6
src/anki/gr/vulkan/Common.cpp

@@ -49,22 +49,22 @@ VkPrimitiveTopology convertTopology(PrimitiveTopology ak)
 	VkPrimitiveTopology out = VK_PRIMITIVE_TOPOLOGY_MAX_ENUM;
 	switch(ak)
 	{
-	case POINTS:
+	case PrimitiveTopology::POINTS:
 		out = VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
 		break;
-	case LINES:
+	case PrimitiveTopology::LINES:
 		out = VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
 		break;
-	case LINE_STRIP:
+	case PrimitiveTopology::LINE_STRIP:
 		out = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
 		break;
-	case TRIANGLES:
+	case PrimitiveTopology::TRIANGLES:
 		out = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
 		break;
-	case TRIANGLE_STRIP:
+	case PrimitiveTopology::TRIANGLE_STRIP:
 		out = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
 		break;
-	case PATCHES:
+	case PrimitiveTopology::PATCHES:
 		out = VK_PRIMITIVE_TOPOLOGY_PATCH_LIST;
 		break;
 	default:

+ 50 - 23
tests/gr/Gr.cpp

@@ -2630,7 +2630,8 @@ void main()
 	COMMON_END();
 }
 
-static void createCubeBuffers(GrManager& gr, Vec3 min, Vec3 max, BufferPtr& indexBuffer, BufferPtr& vertBuffer)
+static void createCubeBuffers(GrManager& gr, Vec3 min, Vec3 max, BufferPtr& indexBuffer, BufferPtr& vertBuffer,
+							  Bool turnInsideOut = false)
 {
 	BufferInitInfo inf;
 	inf.m_access = BufferMapAccessBit::WRITE;
@@ -2678,6 +2679,22 @@ static void createCubeBuffers(GrManager& gr, Vec3 min, Vec3 max, BufferPtr& inde
 	indices[t++] = 4;
 	indices[t++] = 5;
 
+	// Left
+	indices[t++] = 0;
+	indices[t++] = 3;
+	indices[t++] = 4;
+	indices[t++] = 3;
+	indices[t++] = 7;
+	indices[t++] = 4;
+
+	// Right
+	indices[t++] = 1;
+	indices[t++] = 5;
+	indices[t++] = 2;
+	indices[t++] = 2;
+	indices[t++] = 5;
+	indices[t++] = 6;
+
 	// Back
 	indices[t++] = 4;
 	indices[t++] = 7;
@@ -2694,23 +2711,16 @@ static void createCubeBuffers(GrManager& gr, Vec3 min, Vec3 max, BufferPtr& inde
 	indices[t++] = 1;
 	indices[t++] = 2;
 
-	// Left
-	indices[t++] = 0;
-	indices[t++] = 3;
-	indices[t++] = 4;
-	indices[t++] = 3;
-	indices[t++] = 7;
-	indices[t++] = 4;
+	ANKI_ASSERT(t == indices.getSize());
 
-	// Right
-	indices[t++] = 1;
-	indices[t++] = 5;
-	indices[t++] = 2;
-	indices[t++] = 2;
-	indices[t++] = 5;
-	indices[t++] = 6;
+	if(turnInsideOut)
+	{
+		for(U32 i = 0; i < t; i += 3)
+		{
+			std::swap(indices[i + 1], indices[i + 2]);
+		}
+	}
 
-	ANKI_ASSERT(t == indices.getSize());
 	indexBuffer->unmap();
 }
 
@@ -2763,8 +2773,10 @@ void main()
 	// Create geometry
 	BufferPtr smallBoxVertBuffer, smallBoxIndexBuffer;
 	BufferPtr bigBoxVertBuffer, bigBoxIndexBuffer;
+	BufferPtr roomVertBuffer, roomIndexBuffer;
 	const Aabb smallBox(Vec3(130.0f, 0.0f, 65.0f), Vec3(295.0f, 160.0f, 230.0f));
 	const Aabb bigBox(Vec3(265.0f, 0.0f, 295.0f), Vec3(430.0f, 330.0f, 460.0f));
+	const Aabb roomBox(Vec3(0.0f), Vec3(555.0f));
 	{
 		createCubeBuffers(*gr, -(smallBox.getMax().xyz() - smallBox.getMin().xyz()) / 2.0f,
 						  (smallBox.getMax().xyz() - smallBox.getMin().xyz()) / 2.0f, smallBoxIndexBuffer,
@@ -2772,6 +2784,10 @@ void main()
 
 		createCubeBuffers(*gr, -(bigBox.getMax().xyz() - bigBox.getMin().xyz()) / 2.0f,
 						  (bigBox.getMax().xyz() - bigBox.getMin().xyz()) / 2.0f, bigBoxIndexBuffer, bigBoxVertBuffer);
+
+		createCubeBuffers(*gr, -(roomBox.getMax().xyz() - roomBox.getMin().xyz()) / 2.0f,
+						  (roomBox.getMax().xyz() - roomBox.getMin().xyz()) / 2.0f, roomIndexBuffer, roomVertBuffer,
+						  true);
 	}
 
 	// Draw
@@ -2802,35 +2818,46 @@ void main()
 
 		cmdb->beginRenderPass(fb, {TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE}, {});
 
+		cmdb->setVertexAttribute(0, 0, Format::R32G32B32_SFLOAT, 0);
+
 		struct PC
 		{
 			Mat4 m_mvp;
 			Vec4 m_color;
 		} pc;
 
+		// Room
+		pc.m_mvp = projMat * viewMat
+				   * Mat4(Vec4((roomBox.getMin() + roomBox.getMax()).xyz() / 2.0f, 1.0f), Mat3::getIdentity());
+		pc.m_color = Vec4(1.0f, 0.0f, 0.0f, 1.0f);
+		cmdb->setPushConstants(&pc, sizeof(pc));
+		cmdb->bindVertexBuffer(0, roomVertBuffer, 0, sizeof(Vec3));
+		cmdb->bindIndexBuffer(roomIndexBuffer, 0, IndexType::U16);
+		cmdb->drawElements(PrimitiveTopology::TRIANGLES, 36);
+
+		// Small box
 		pc.m_mvp = projMat * viewMat
 				   * Mat4(Vec4((smallBox.getMin() + smallBox.getMax()).xyz() / 2.0f, 1.0f),
 						  Mat3(Axisang(toRad(-18.0f), Vec3(0.0f, 1.0f, 0.0f))));
 		pc.m_color = Vec4(0.75f);
 		cmdb->setPushConstants(&pc, sizeof(pc));
-
-		cmdb->setVertexAttribute(0, 0, Format::R32G32B32_SFLOAT, 0);
 		cmdb->bindVertexBuffer(0, smallBoxVertBuffer, 0, sizeof(Vec3));
 		cmdb->bindIndexBuffer(smallBoxIndexBuffer, 0, IndexType::U16);
+		cmdb->drawElements(PrimitiveTopology::TRIANGLES, 36);
 
-		cmdb->drawElements(PrimitiveTopology::TRIANGLE_STRIP, 36);
-
-		cmdb->bindVertexBuffer(0, bigBoxVertBuffer, 0, sizeof(Vec3));
+		// Big box
 		pc.m_mvp = projMat * viewMat
 				   * Mat4(Vec4((bigBox.getMin() + bigBox.getMax()).xyz() / 2.0f, 1.0f),
 						  Mat3(Axisang(toRad(15.0f), Vec3(0.0f, 1.0f, 0.0f))));
 		cmdb->setPushConstants(&pc, sizeof(pc));
-		cmdb->drawElements(PrimitiveTopology::TRIANGLE_STRIP, 36);
+		cmdb->bindVertexBuffer(0, bigBoxVertBuffer, 0, sizeof(Vec3));
+		cmdb->bindIndexBuffer(bigBoxIndexBuffer, 0, IndexType::U16);
+		cmdb->drawElements(PrimitiveTopology::TRIANGLES, 36);
 
 		cmdb->endRenderPass();
 
 		cmdb->setTextureBarrier(presentTex, TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE, TextureUsageBit::PRESENT,
-								TextureSubresourceInfo{});
+								TextureSubresourceInfo());
 
 		cmdb->flush();