Browse Source

Vulkan: Adding missing support for index buffers

Panagiotis Christopoulos Charitos 9 years ago
parent
commit
59f4419181

+ 21 - 0
include/anki/gr/vulkan/ResourceGroupImpl.h

@@ -50,6 +50,22 @@ public:
 	void setupDynamicOffsets(
 	void setupDynamicOffsets(
 		const TransientMemoryInfo* dynInfo, U32 dynOffsets[]) const;
 		const TransientMemoryInfo* dynInfo, U32 dynOffsets[]) const;
 
 
+	/// Get index buffer info.
+	/// @return false if there is no index buffer.
+	Bool getIndexBufferInfo(
+		VkBuffer& buff, VkDeviceSize& offset, VkIndexType& idxType) const
+	{
+		if(m_indexBuffHandle)
+		{
+			buff = m_indexBuffHandle;
+			offset = m_indexBufferOffset;
+			idxType = m_indexType;
+			return true;
+		}
+
+		return false;
+	}
+
 private:
 private:
 	VkDescriptorSet m_handle = VK_NULL_HANDLE;
 	VkDescriptorSet m_handle = VK_NULL_HANDLE;
 	Array<VkBuffer, MAX_VERTEX_ATTRIBUTES> m_vertBuffs = {{}};
 	Array<VkBuffer, MAX_VERTEX_ATTRIBUTES> m_vertBuffs = {{}};
@@ -61,6 +77,11 @@ private:
 	BitSet<MAX_UNIFORM_BUFFER_BINDINGS + MAX_STORAGE_BUFFER_BINDINGS>
 	BitSet<MAX_UNIFORM_BUFFER_BINDINGS + MAX_STORAGE_BUFFER_BINDINGS>
 		m_dynamicBuffersMask = {false};
 		m_dynamicBuffersMask = {false};
 
 
+	// Index info
+	VkBuffer m_indexBuffHandle = VK_NULL_HANDLE;
+	U32 m_indexBufferOffset = MAX_U32;
+	VkIndexType m_indexType = VK_INDEX_TYPE_MAX_ENUM;
+
 	/// Holds the references to the resources. Used to release the references
 	/// Holds the references to the resources. Used to release the references
 	/// gracefully
 	/// gracefully
 	DynamicArray<GrObjectPtr<GrObject>> m_refs;
 	DynamicArray<GrObjectPtr<GrObject>> m_refs;

+ 9 - 1
src/gr/vulkan/CommandBufferImpl.cpp

@@ -263,7 +263,7 @@ void CommandBufferImpl::bindResourceGroup(
 			&dynOffsets[0]);
 			&dynOffsets[0]);
 	}
 	}
 
 
-	// Bind vertex buffers only in the first set
+	// Bind vertex and index buffer only in the first set
 	if(slot == 0)
 	if(slot == 0)
 	{
 	{
 		const VkBuffer* buffers = nullptr;
 		const VkBuffer* buffers = nullptr;
@@ -274,6 +274,14 @@ void CommandBufferImpl::bindResourceGroup(
 		{
 		{
 			vkCmdBindVertexBuffers(m_handle, 0, bindingCount, buffers, offsets);
 			vkCmdBindVertexBuffers(m_handle, 0, bindingCount, buffers, offsets);
 		}
 		}
+
+		VkBuffer idxBuff;
+		VkDeviceSize idxBuffOffset;
+		VkIndexType idxType;
+		if(impl.getIndexBufferInfo(idxBuff, idxBuffOffset, idxType))
+		{
+			vkCmdBindIndexBuffer(m_handle, idxBuff, idxBuffOffset, idxType);
+		}
 	}
 	}
 
 
 	// Hold the reference
 	// Hold the reference

+ 26 - 0
src/gr/vulkan/ResourceGroupImpl.cpp

@@ -68,6 +68,11 @@ U ResourceGroupImpl::calcRefCount(
 		}
 		}
 	}
 	}
 
 
+	if(init.m_indexBuffer.m_buffer)
+	{
+		++count;
+	}
+
 	// TODO: The rest of the resources
 	// TODO: The rest of the resources
 
 
 	return count;
 	return count;
@@ -229,6 +234,27 @@ Error ResourceGroupImpl::init(const ResourceGroupInitInfo& init)
 		}
 		}
 	}
 	}
 
 
+	if(init.m_indexBuffer.m_buffer)
+	{
+		m_indexBuffHandle =
+			init.m_indexBuffer.m_buffer->getImplementation().getHandle();
+		m_indexBufferOffset = init.m_indexBuffer.m_offset;
+
+		if(init.m_indexSize == 2)
+		{
+			m_indexType = VK_INDEX_TYPE_UINT16;
+		}
+		else
+		{
+			ANKI_ASSERT(init.m_indexSize == 4);
+			m_indexType = VK_INDEX_TYPE_UINT32;
+		}
+
+		m_refs[refCount++] = init.m_indexBuffer.m_buffer;
+	}
+
+	ANKI_ASSERT(refCount == m_refs.getSize());
+
 	return ErrorCode::NONE;
 	return ErrorCode::NONE;
 }
 }