Просмотр исходного кода

Some additions to the mesh resource

Panagiotis Christopoulos Charitos 5 лет назад
Родитель
Сommit
35587d8c3e

+ 1 - 1
anki/resource/AsyncLoader.h

@@ -66,7 +66,7 @@ public:
 		submitTask(newTask<TTask>(std::forward<TArgs>(args)...));
 	}
 
-	/// Pause the loader. This method will block the main thread for the current async task to finish. The rest of the
+	/// Pause the loader. This method will block the caller for the current async task to finish. The rest of the
 	/// tasks in the queue will not be executed until resume is called.
 	void pause();
 

+ 6 - 6
anki/resource/MaterialResource.cpp

@@ -148,11 +148,11 @@ public:
 };
 
 static const Array<GpuMaterialFloats, 5> GPU_MATERIAL_FLOATS = {
-	{{"diffuseColor", offsetof(GpuMaterial, m_diffuseColor), 3},
-	 {"specularColor", offsetof(GpuMaterial, m_specularColor), 3},
-	 {"emissiveColor", offsetof(GpuMaterial, m_emissiveColor), 3},
-	 {"roughness", offsetof(GpuMaterial, m_roughness), 1},
-	 {"metalness", offsetof(GpuMaterial, m_metalness), 1}}};
+	{{"diffuseColor", offsetof(MaterialGpuDescriptor, m_diffuseColor), 3},
+	 {"specularColor", offsetof(MaterialGpuDescriptor, m_specularColor), 3},
+	 {"emissiveColor", offsetof(MaterialGpuDescriptor, m_emissiveColor), 3},
+	 {"roughness", offsetof(MaterialGpuDescriptor, m_roughness), 1},
+	 {"metalness", offsetof(MaterialGpuDescriptor, m_metalness), 1}}};
 
 MaterialVariable::MaterialVariable()
 {
@@ -1128,7 +1128,7 @@ Error MaterialResource::parseRtMaterial(XmlElement rtMaterialEl)
 
 	// input
 	RayTracingMaterialVariant& variant = m_rt[type].m_variant;
-	GpuMaterial& gpuMaterial = variant.m_gpuMaterialDescr;
+	MaterialGpuDescriptor& gpuMaterial = variant.m_materialGpuDescriptor;
 	XmlElement inputsEl;
 	ANKI_CHECK(rtMaterialEl.getChildElementOptional("inputs", inputsEl));
 	if(inputsEl)

+ 4 - 4
anki/resource/MaterialResource.h

@@ -278,9 +278,9 @@ public:
 		return m_shaderGroupHandle;
 	}
 
-	const GpuMaterial& getGpuMaterialDescriptor() const
+	const MaterialGpuDescriptor& getMaterialGpuDescriptor() const
 	{
-		return m_gpuMaterialDescr;
+		return m_materialGpuDescriptor;
 	}
 
 	const Array<TextureViewPtr, TEXTURE_CHANNEL_COUNT>& getTextureViews() const
@@ -290,13 +290,13 @@ public:
 
 private:
 	ConstWeakArray<U8> m_shaderGroupHandle;
-	GpuMaterial m_gpuMaterialDescr;
+	MaterialGpuDescriptor m_materialGpuDescriptor;
 	Array<TextureResourcePtr, TEXTURE_CHANNEL_COUNT> m_textureResources; ///< Keep the resources alive.
 	Array<TextureViewPtr, TEXTURE_CHANNEL_COUNT> m_textureViews; ///< Cache the GPU objects.
 
 	RayTracingMaterialVariant()
 	{
-		memset(&m_gpuMaterialDescr, 0, sizeof(m_gpuMaterialDescr));
+		memset(&m_materialGpuDescriptor, 0, sizeof(m_materialGpuDescriptor));
 	}
 
 	~RayTracingMaterialVariant() = default;

+ 35 - 3
anki/resource/MeshResource.cpp

@@ -46,6 +46,7 @@ public:
 MeshResource::MeshResource(ResourceManager* manager)
 	: ResourceObject(manager)
 {
+	memset(&m_meshGpuDescriptor, 0, sizeof(m_meshGpuDescriptor));
 }
 
 MeshResource::~MeshResource()
@@ -194,12 +195,12 @@ Error MeshResource::load(const ResourceFilename& filename, Bool async)
 		PtrSize relativeOffset;
 		getVertexAttributeInfo(VertexAttributeLocation::POSITION, bufferIdx, format, relativeOffset);
 
-		BufferPtr posBuffer;
+		BufferPtr buffer;
 		PtrSize offset;
 		PtrSize stride;
-		getVertexBufferInfo(bufferIdx, posBuffer, offset, stride);
+		getVertexBufferInfo(bufferIdx, buffer, offset, stride);
 
-		inf.m_bottomLevel.m_positionBuffer = posBuffer;
+		inf.m_bottomLevel.m_positionBuffer = buffer;
 		inf.m_bottomLevel.m_positionBufferOffset = offset;
 		inf.m_bottomLevel.m_positionStride = U32(stride);
 		inf.m_bottomLevel.m_positionsFormat = format;
@@ -208,6 +209,37 @@ Error MeshResource::load(const ResourceFilename& filename, Bool async)
 		m_blas = getManager().getGrManager().newAccelerationStructure(inf);
 	}
 
+	// Fill the GPU descriptor
+	if(rayTracingEnabled)
+	{
+		U32 bufferIdx;
+		Format format;
+		PtrSize relativeOffset;
+		getVertexAttributeInfo(VertexAttributeLocation::POSITION, bufferIdx, format, relativeOffset);
+		BufferPtr buffer;
+		PtrSize offset;
+		PtrSize stride;
+		getVertexBufferInfo(bufferIdx, buffer, offset, stride);
+		m_meshGpuDescriptor.m_indexBufferPtr = m_indexBuff->getGpuAddress();
+		m_meshGpuDescriptor.m_positionBufferPtr = buffer->getGpuAddress();
+
+		getVertexAttributeInfo(VertexAttributeLocation::NORMAL, bufferIdx, format, relativeOffset);
+		getVertexBufferInfo(bufferIdx, buffer, offset, stride);
+		m_meshGpuDescriptor.m_mainVertexBufferPtr = buffer->getGpuAddress();
+
+		if(hasBoneWeights())
+		{
+			getVertexAttributeInfo(VertexAttributeLocation::BONE_WEIGHTS, bufferIdx, format, relativeOffset);
+			getVertexBufferInfo(bufferIdx, buffer, offset, stride);
+			m_meshGpuDescriptor.m_boneInfoVertexBufferPtr = buffer->getGpuAddress();
+		}
+
+		m_meshGpuDescriptor.m_indexCount = m_indexCount;
+		m_meshGpuDescriptor.m_vertexCount = m_vertCount;
+		m_meshGpuDescriptor.m_aabbMin = header.m_aabbMin;
+		m_meshGpuDescriptor.m_aabbMax = header.m_aabbMax;
+	}
+
 	// Submit the loading task
 	if(async)
 	{

+ 8 - 0
anki/resource/MeshResource.h

@@ -9,6 +9,7 @@
 #include <anki/Math.h>
 #include <anki/Gr.h>
 #include <anki/collision/Obb.h>
+#include <anki/shaders/include/ModelTypes.h>
 
 namespace anki
 {
@@ -111,6 +112,11 @@ public:
 		return m_blas;
 	}
 
+	const MeshGpuDescriptor& getMeshGpuDescriptor() const
+	{
+		return m_meshGpuDescriptor;
+	}
+
 private:
 	class LoadTask;
 	class LoadContext;
@@ -155,7 +161,9 @@ private:
 	// Other
 	Obb m_obb;
 
+	// RT
 	AccelerationStructurePtr m_blas;
+	MeshGpuDescriptor m_meshGpuDescriptor;
 
 	ANKI_USE_RESULT Error loadAsync(MeshLoader& loader) const;
 };

+ 5 - 5
anki/shaders/RtShadowsHit.ankiprog

@@ -14,10 +14,10 @@
 #if ALPHA_TEXTURE == 1
 layout(set = 0, binding = 0, scalar) buffer b_ankiModels
 {
-	GpuModel u_ankiModels[];
+	ModelGpuDescriptor u_modelDescriptors[];
 };
 
-layout(set = 0, binding = 1) uniform sampler u_ankiGlobalSampler;
+layout(set = 0, binding = 1) uniform sampler u_sampler;
 
 ANKI_BINDLESS_SET(1);
 #endif
@@ -34,8 +34,8 @@ ANKI_REF(MainVertex, ANKI_ALIGNOF(MainVertex));
 void main()
 {
 #if ALPHA_TEXTURE == 1
-	const GpuModel model = u_ankiModels[nonuniformEXT(gl_InstanceID)];
-	const GpuMesh mesh = model.m_mesh;
+	const ModelGpuDescriptor model = u_modelDescriptors[nonuniformEXT(gl_InstanceID)];
+	const MeshGpuDescriptor mesh = model.m_mesh;
 
 	const U32 offset = gl_PrimitiveID * ANKI_SIZEOF(U16Vec3);
 	const U16Vec3 indices = U16Vec3Ref(nonuniformEXT(mesh.m_indexBufferPtr + offset)).m_value;
@@ -50,7 +50,7 @@ void main()
 					+ vert2.m_uvs[UV_CHANNEL_0] * barycentrics.z;
 
 	const U32 texIdx = model.m_material.m_bindlessTextureIndices[TEXTURE_CHANNEL_DIFFUSE];
-	const F32 alpha = textureLod(u_bindlessTextures2dF32[nonuniformEXT(texIdx)], u_ankiGlobalSampler, uv, 3).a;
+	const F32 alpha = textureLod(u_bindlessTextures2dF32[nonuniformEXT(texIdx)], u_sampler, uv, 3).a;
 
 	g_payload += alpha;
 

+ 5 - 5
anki/shaders/include/ModelTypes.h

@@ -36,7 +36,7 @@ const U32 _ANKI_ALIGNOF_BoneInfoVertex = 4;
 ANKI_SHADER_STATIC_ASSERT(_ANKI_SIZEOF_BoneInfoVertex == sizeof(BoneInfoVertex));
 
 /// A structure that contains all the info of a geometry.
-struct GpuMesh
+struct MeshGpuDescriptor
 {
 	U64 m_indexBufferPtr; ///< Points to a buffer of U16 indices.
 	U64 m_positionBufferPtr; ///< Points to a buffer of Vec3 positions.
@@ -59,7 +59,7 @@ const U32 TEXTURE_CHANNEL_AUX_2 = 7;
 
 const U32 TEXTURE_CHANNEL_COUNT = 8;
 
-struct GpuMaterial
+struct MaterialGpuDescriptor
 {
 	U16 m_bindlessTextureIndices[TEXTURE_CHANNEL_COUNT];
 	Vec3 m_diffuseColor;
@@ -69,10 +69,10 @@ struct GpuMaterial
 	F32 m_metalness;
 };
 
-struct GpuModel
+struct ModelGpuDescriptor
 {
-	GpuMesh m_mesh;
-	GpuMaterial m_material;
+	MeshGpuDescriptor m_mesh;
+	MaterialGpuDescriptor m_material;
 #if defined(__cplusplus)
 	F32 m_worldTransform[12];
 #else

+ 8 - 0
anki/util/Thread.h

@@ -481,6 +481,10 @@ public:
 	Semaphore& operator=(const Semaphore&) = delete;
 
 	/// Same as sem_wait().
+	/// @code
+	/// if(value == 0) wait();
+	/// --value;
+	/// @endcode
 	void wait()
 	{
 #if ANKI_POSIX
@@ -491,6 +495,10 @@ public:
 	}
 
 	/// Same as sem_post().
+	/// @code
+	/// ++value;
+	/// wakeupWaiters();
+	/// @endcode
 	void post()
 	{
 #if ANKI_POSIX