瀏覽代碼

Move the proxy meshes to C++

Panagiotis Christopoulos Charitos 3 年之前
父節點
當前提交
428b4ad94e

+ 110 - 18
AnKi/Renderer/TraditionalDeferredShading.cpp

@@ -12,6 +12,50 @@
 
 namespace anki {
 
+// Vertices of 2 shapes
+// - Use the blend file in EngineAssets to output in .obj and then copy paste here
+// - The sphere goes from -1 to 1
+// - The cone's length is 1. Its big side goes from -1 to 1. The point of the cone is in 0,0,0. The direction is in +z
+
+inline constexpr F32 kIcosphereVertices[] = {
+	0.000000f,  -1.067367f, 0.000000f,  0.772355f,  -0.477347f, 0.561142f,  -0.295008f, -0.477348f, 0.907955f,
+	-0.954681f, -0.477343f, 0.000000f,  -0.295008f, -0.477348f, -0.907955f, 0.772355f,  -0.477347f, -0.561142f,
+	0.295008f,  0.477348f,  0.907955f,  -0.772355f, 0.477347f,  0.561142f,  -0.772355f, 0.477347f,  -0.561142f,
+	0.295008f,  0.477348f,  -0.907955f, 0.954681f,  0.477343f,  0.000000f,  0.000000f,  1.067367f,  0.000000f,
+	-0.173400f, -0.907961f, 0.533679f,  0.453976f,  -0.907960f, 0.329829f,  0.280578f,  -0.561155f, 0.863513f,
+	0.907954f,  -0.561153f, 0.000000f,  0.453976f,  -0.907960f, -0.329829f, -0.561147f, -0.907958f, 0.000000f,
+	-0.734551f, -0.561154f, 0.533680f,  -0.173400f, -0.907961f, -0.533679f, -0.734551f, -0.561154f, -0.533680f,
+	0.280578f,  -0.561155f, -0.863513f, 1.015128f,  0.000000f,  0.329830f,  1.015128f,  0.000000f,  -0.329830f,
+	0.000000f,  0.000000f,  1.067367f,  0.627383f,  0.000000f,  0.863518f,  -1.015128f, 0.000000f,  0.329830f,
+	-0.627383f, 0.000000f,  0.863518f,  -0.627383f, 0.000000f,  -0.863518f, -1.015128f, 0.000000f,  -0.329830f,
+	0.627383f,  0.000000f,  -0.863518f, 0.000000f,  0.000000f,  -1.067367f, 0.734551f,  0.561154f,  0.533680f,
+	-0.280578f, 0.561155f,  0.863513f,  -0.907954f, 0.561153f,  0.000000f,  -0.280578f, 0.561155f,  -0.863513f,
+	0.734551f,  0.561154f,  -0.533680f, 0.173400f,  0.907961f,  0.533679f,  0.561147f,  0.907958f,  0.000000f,
+	-0.453976f, 0.907960f,  0.329829f,  -0.453976f, 0.907960f,  -0.329829f, 0.173400f,  0.907961f,  -0.533679f};
+
+inline constexpr U16 kIconsphereIndices[] = {
+	0,  13, 12, 1,  13, 15, 0,  12, 17, 0,  17, 19, 0,  19, 16, 1,  15, 22, 2,  14, 24, 3,  18, 26, 4,  20, 28,
+	5,  21, 30, 1,  22, 25, 2,  24, 27, 3,  26, 29, 4,  28, 31, 5,  30, 23, 6,  32, 37, 7,  33, 39, 8,  34, 40,
+	9,  35, 41, 10, 36, 38, 38, 41, 11, 38, 36, 41, 36, 9,  41, 41, 40, 11, 41, 35, 40, 35, 8,  40, 40, 39, 11,
+	40, 34, 39, 34, 7,  39, 39, 37, 11, 39, 33, 37, 33, 6,  37, 37, 38, 11, 37, 32, 38, 32, 10, 38, 23, 36, 10,
+	23, 30, 36, 30, 9,  36, 31, 35, 9,  31, 28, 35, 28, 8,  35, 29, 34, 8,  29, 26, 34, 26, 7,  34, 27, 33, 7,
+	27, 24, 33, 24, 6,  33, 25, 32, 6,  25, 22, 32, 22, 10, 32, 30, 31, 9,  30, 21, 31, 21, 4,  31, 28, 29, 8,
+	28, 20, 29, 20, 3,  29, 26, 27, 7,  26, 18, 27, 18, 2,  27, 24, 25, 6,  24, 14, 25, 14, 1,  25, 22, 23, 10,
+	22, 15, 23, 15, 5,  23, 16, 21, 5,  16, 19, 21, 19, 4,  21, 19, 20, 4,  19, 17, 20, 17, 3,  20, 17, 18, 3,
+	17, 12, 18, 12, 2,  18, 15, 16, 5,  15, 13, 16, 13, 0,  16, 12, 14, 2,  12, 13, 14, 13, 1,  14};
+
+inline constexpr F32 kConeVertices[] = {
+	0.000000f,  1.000000f,  -1.000000f, 0.000000f,  0.000000f,  0.000000f,  0.500000f,  0.866026f,  -1.000000f,
+	0.866025f,  0.500000f,  -1.000000f, 1.000000f,  0.000000f,  -1.000000f, 0.866025f,  -0.500000f, -1.000000f,
+	0.500000f,  -0.866025f, -1.000000f, 0.000000f,  -1.000000f, -1.000000f, -0.500000f, -0.866025f, -1.000000f,
+	-0.866025f, -0.500000f, -1.000000f, -1.000000f, -0.000000f, -1.000000f, -0.866026f, 0.500000f,  -1.000000f,
+	-0.500001f, 0.866025f,  -1.000000f, 0.000000f,  0.000000f,  -1.000000f};
+
+inline constexpr U16 kConeIndices[] = {0, 1,  2, 2,  1,  3,  3,  1,  4,  4,  1,  5,  5,  1,  6,  6,  1,  7,
+									   7, 1,  8, 8,  1,  9,  9,  1,  10, 10, 1,  11, 11, 1,  12, 12, 1,  0,
+									   2, 13, 0, 0,  13, 12, 3,  13, 2,  4,  13, 3,  5,  13, 4,  6,  13, 5,
+									   7, 13, 6, 12, 13, 11, 11, 13, 10, 10, 13, 9,  9,  13, 8,  7,  8,  13};
+
 TraditionalDeferredLightShading::TraditionalDeferredLightShading(Renderer* r)
 	: RendererObject(r)
 {
@@ -48,9 +92,7 @@ Error TraditionalDeferredLightShading::init()
 		}
 	}
 
-	// Init meshes
-	ANKI_CHECK(getResourceManager().loadResource("EngineAssets/Plight.ankimesh", m_plightMesh, false));
-	ANKI_CHECK(getResourceManager().loadResource("EngineAssets/Slight.ankimesh", m_slightMesh, false));
+	createProxyMeshes();
 
 	// Shadow sampler
 	{
@@ -80,26 +122,76 @@ Error TraditionalDeferredLightShading::init()
 	return Error::kNone;
 }
 
-void TraditionalDeferredLightShading::bindVertexIndexBuffers(MeshResourcePtr& mesh, CommandBufferPtr& cmdb,
-															 U32& indexCount)
+void TraditionalDeferredLightShading::createProxyMeshes()
 {
-	// Attrib
-	const Format fmt = kMeshRelatedVertexStreamFormats[VertexStreamId::kPosition];
-	PtrSize offset;
-	U32 vertCount;
-	mesh->getVertexStreamInfo(0, VertexStreamId::kPosition, offset, vertCount);
+	constexpr PtrSize bufferSize =
+		sizeof(kIcosphereVertices) + sizeof(kIcosphereVertices) + sizeof(kConeVertices) + sizeof(kConeIndices);
+
+	BufferInitInfo buffInit("DeferredShadingPoxies");
+	buffInit.m_size = bufferSize;
+	buffInit.m_usage = BufferUsageBit::kTransferDestination | BufferUsageBit::kIndex | BufferUsageBit::kVertex;
+
+	m_proxyVolumesBuffer = getGrManager().newBuffer(buffInit);
+
+	buffInit.setName("TempTransfer");
+	buffInit.m_usage = BufferUsageBit::kTransferSource;
+	buffInit.m_mapAccess = BufferMapAccessBit::kWrite;
+	BufferPtr transferBuffer = getGrManager().newBuffer(buffInit);
+
+	U8* mappedMem = static_cast<U8*>(transferBuffer->map(0, kMaxPtrSize, BufferMapAccessBit::kWrite));
+
+	// Write verts
+	memcpy(mappedMem, kIcosphereVertices, sizeof(kIcosphereVertices));
+	mappedMem += sizeof(kIcosphereVertices);
+	memcpy(mappedMem, kConeVertices, sizeof(kConeVertices));
+	mappedMem += sizeof(kConeVertices);
+	memcpy(mappedMem, kIconsphereIndices, sizeof(kIconsphereIndices));
+	mappedMem += sizeof(kIconsphereIndices);
+	memcpy(mappedMem, kConeIndices, sizeof(kConeIndices));
+
+	transferBuffer->unmap();
+
+	CommandBufferInitInfo cmdbInit("Temp");
+	cmdbInit.m_flags = CommandBufferFlag::kSmallBatch | CommandBufferFlag::kGeneralWork;
+
+	CommandBufferPtr cmdb = getGrManager().newCommandBuffer(cmdbInit);
+	cmdb->copyBufferToBuffer(transferBuffer, 0, m_proxyVolumesBuffer, 0, bufferSize);
+	cmdb->flush();
+
+	getGrManager().finish();
+}
 
-	cmdb->setVertexAttribute(0, 0, fmt, 0);
+void TraditionalDeferredLightShading::bindVertexIndexBuffers(ProxyType proxyType, CommandBufferPtr& cmdb,
+															 U32& indexCount) const
+{
+	// Attrib
+	cmdb->setVertexAttribute(0, 0, Format::kR32G32B32_Sfloat, 0);
 
 	// Vert buff
-	BufferPtr buff = mesh->getManager().getUnifiedGeometryMemoryPool().getVertexBuffer();
-	cmdb->bindVertexBuffer(0, buff, offset, sizeof(Vec3));
+	PtrSize offset;
+	if(proxyType == ProxyType::kProxySphere)
+	{
+		offset = 0;
+	}
+	else
+	{
+		offset = sizeof(kIcosphereVertices);
+	}
+	cmdb->bindVertexBuffer(0, m_proxyVolumesBuffer, offset, sizeof(Vec3));
 
 	// Idx buff
-	IndexType idxType;
-	mesh->getIndexBufferInfo(0, offset, indexCount, idxType);
+	if(proxyType == ProxyType::kProxySphere)
+	{
+		offset = sizeof(kIcosphereVertices) + sizeof(kConeVertices);
+		indexCount = sizeof(kIconsphereIndices) / sizeof(U16);
+	}
+	else
+	{
+		offset = sizeof(kIcosphereVertices) + sizeof(kConeVertices) + sizeof(kIconsphereIndices);
+		indexCount = sizeof(kConeIndices) / sizeof(U16);
+	}
 
-	cmdb->bindIndexBuffer(buff, offset, idxType);
+	cmdb->bindIndexBuffer(m_proxyVolumesBuffer, offset, IndexType::kU16);
 }
 
 void TraditionalDeferredLightShading::drawLights(TraditionalDeferredLightShadingDrawInfo& info)
@@ -210,7 +302,7 @@ void TraditionalDeferredLightShading::drawLights(TraditionalDeferredLightShading
 
 	// Do point lights
 	U32 indexCount;
-	bindVertexIndexBuffers(m_plightMesh, cmdb, indexCount);
+	bindVertexIndexBuffers(ProxyType::kProxySphere, cmdb, indexCount);
 	cmdb->bindShaderProgram(m_plightGrProg[info.m_computeSpecular]);
 
 	for(const PointLightQueueElement& plightEl : info.m_pointLights)
@@ -241,7 +333,7 @@ void TraditionalDeferredLightShading::drawLights(TraditionalDeferredLightShading
 	}
 
 	// Do spot lights
-	bindVertexIndexBuffers(m_slightMesh, cmdb, indexCount);
+	bindVertexIndexBuffers(ProxyType::kProxyCone, cmdb, indexCount);
 	cmdb->bindShaderProgram(m_slightGrProg[info.m_computeSpecular]);
 
 	for(const SpotLightQueueElement& splightEl : info.m_spotLights)

+ 10 - 3
AnKi/Renderer/TraditionalDeferredShading.h

@@ -57,6 +57,12 @@ public:
 	void drawLights(TraditionalDeferredLightShadingDrawInfo& info);
 
 private:
+	enum class ProxyType
+	{
+		kProxySphere,
+		kProxyCone
+	};
+
 	ShaderProgramResourcePtr m_lightProg;
 	Array<ShaderProgramPtr, 2> m_plightGrProg;
 	Array<ShaderProgramPtr, 2> m_slightGrProg;
@@ -67,13 +73,14 @@ private:
 
 	/// @name Meshes of light volumes.
 	/// @{
-	MeshResourcePtr m_plightMesh;
-	MeshResourcePtr m_slightMesh;
+	BufferPtr m_proxyVolumesBuffer;
 	/// @}
 
 	SamplerPtr m_shadowSampler;
 
-	static void bindVertexIndexBuffers(MeshResourcePtr& mesh, CommandBufferPtr& cmdb, U32& indexCount);
+	void bindVertexIndexBuffers(ProxyType proxyType, CommandBufferPtr& cmdb, U32& indexCount) const;
+
+	void createProxyMeshes();
 };
 /// @}
 } // end namespace anki

二進制
EngineAssets/LightMeshes.blend


二進制
EngineAssets/Plight.ankimesh


二進制
EngineAssets/Slight.ankimesh