Implementing basic asset serialization
This commit is contained in:
@@ -182,7 +182,7 @@ void Allocation::markFree(SubAllocation *allocation)
|
||||
|
||||
if (allocHandle == nullptr)
|
||||
{
|
||||
allocHandle = new SubAllocation(this, allocation->alignedOffset, allocation->size, allocation->alignedOffset, allocation->allocatedSize);
|
||||
allocHandle = new SubAllocation(this, allocation->allocatedOffset, allocation->size, allocation->alignedOffset, allocation->allocatedSize);
|
||||
freeRanges[allocation->allocatedOffset] = allocHandle;
|
||||
}
|
||||
activeAllocations.erase(allocation->allocatedOffset);
|
||||
@@ -226,8 +226,7 @@ PSubAllocation Allocator::allocate(const VkMemoryRequirements2 &memRequirements2
|
||||
{
|
||||
std::scoped_lock lck(lock);
|
||||
const VkMemoryRequirements &requirements = memRequirements2.memoryRequirements;
|
||||
uint8 memoryTypeIndex;
|
||||
VK_CHECK(findMemoryType(requirements.memoryTypeBits, properties, &memoryTypeIndex));
|
||||
uint32 memoryTypeIndex = findMemoryType(requirements.memoryTypeBits, properties);
|
||||
uint32 heapIndex = memProperties.memoryTypes[memoryTypeIndex].heapIndex;
|
||||
|
||||
if (memRequirements2.pNext != nullptr)
|
||||
@@ -243,10 +242,13 @@ PSubAllocation Allocator::allocate(const VkMemoryRequirements2 &memRequirements2
|
||||
}
|
||||
for (auto alloc : heaps[heapIndex].allocations)
|
||||
{
|
||||
PSubAllocation suballoc = alloc->getSuballocation(requirements.size, requirements.alignment);
|
||||
if (suballoc != nullptr)
|
||||
if(alloc->memoryTypeIndex == memoryTypeIndex)
|
||||
{
|
||||
return suballoc;
|
||||
PSubAllocation suballoc = alloc->getSuballocation(requirements.size, requirements.alignment);
|
||||
if (suballoc != nullptr)
|
||||
{
|
||||
return suballoc;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -273,22 +275,17 @@ void Allocator::free(Allocation *allocation)
|
||||
}
|
||||
}
|
||||
}
|
||||
VkResult Allocator::findMemoryType(uint32 typeBits, VkMemoryPropertyFlags properties, uint8 *typeIndex)
|
||||
uint32 Allocator::findMemoryType(uint32 typeFilter, VkMemoryPropertyFlags properties)
|
||||
{
|
||||
for (uint8 memoryIndex = 0; memoryIndex < memProperties.memoryTypeCount && typeBits; ++memoryIndex)
|
||||
for (uint32 i = 0; i < memProperties.memoryTypeCount; i++)
|
||||
{
|
||||
if ((typeBits & 1) == 1)
|
||||
if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties)
|
||||
{
|
||||
if ((memProperties.memoryTypes[memoryIndex].propertyFlags & properties) == properties)
|
||||
{
|
||||
*typeIndex = memoryIndex;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
typeBits >>= 1;
|
||||
}
|
||||
|
||||
return VK_ERROR_FORMAT_NOT_SUPPORTED;
|
||||
throw std::runtime_error("error finding memory");
|
||||
}
|
||||
|
||||
StagingBuffer::StagingBuffer()
|
||||
|
||||
@@ -155,7 +155,7 @@ private:
|
||||
Array<PAllocation> allocations;
|
||||
};
|
||||
Array<HeapInfo> heaps;
|
||||
VkResult findMemoryType(uint32 typeBits, VkMemoryPropertyFlags properties, uint8 *typeIndex);
|
||||
uint32 findMemoryType(uint32 typeBits, VkMemoryPropertyFlags properties);
|
||||
std::mutex lock;
|
||||
PGraphics graphics;
|
||||
VkPhysicalDeviceMemoryProperties memProperties;
|
||||
|
||||
@@ -208,7 +208,6 @@ void *ShaderBuffer::lockRegion(uint64 regionOffset, uint64 regionSize, bool bWri
|
||||
else
|
||||
{
|
||||
PCmdBuffer current = graphics->getQueueCommands(owner)->getCommands();
|
||||
graphics->getQueueCommands(owner)->submitCommands();
|
||||
current->waitForCommand();
|
||||
|
||||
requestOwnershipTransfer(Gfx::QueueType::DEDICATED_TRANSFER);
|
||||
@@ -466,6 +465,13 @@ void VertexBuffer::updateRegion(BulkResourceData update)
|
||||
unlock();
|
||||
}
|
||||
|
||||
void VertexBuffer::download(Array<uint8>& buffer)
|
||||
{
|
||||
void* data = lock(false);
|
||||
buffer.resize(size);
|
||||
std::memcpy(buffer.data(), data, size);
|
||||
unlock();
|
||||
}
|
||||
|
||||
void VertexBuffer::requestOwnershipTransfer(Gfx::QueueType newOwner)
|
||||
{
|
||||
@@ -509,6 +515,14 @@ IndexBuffer::~IndexBuffer()
|
||||
{
|
||||
}
|
||||
|
||||
void IndexBuffer::download(Array<uint8>& buffer)
|
||||
{
|
||||
void* data = lock(false);
|
||||
buffer.resize(size);
|
||||
std::memcpy(buffer.data(), data, size);
|
||||
unlock();
|
||||
}
|
||||
|
||||
void IndexBuffer::requestOwnershipTransfer(Gfx::QueueType newOwner)
|
||||
{
|
||||
Gfx::QueueOwnedResource::transferOwnership(newOwner);
|
||||
|
||||
@@ -166,7 +166,12 @@ void CmdBuffer::refreshFence()
|
||||
|
||||
void CmdBuffer::waitForCommand(uint32 timeout)
|
||||
{
|
||||
std::scoped_lock lock(handleLock);
|
||||
manager->submitCommands();
|
||||
if (state == State::InsideBegin)
|
||||
{
|
||||
// is already done
|
||||
return;
|
||||
}
|
||||
fence->wait(timeout);
|
||||
refreshFence();
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ private:
|
||||
PGraphics graphics;
|
||||
Array<VkDescriptorSetLayoutBinding> bindings;
|
||||
VkDescriptorSetLayout layoutHandle;
|
||||
std::string name;
|
||||
friend class DescriptorAllocator;
|
||||
};
|
||||
DEFINE_REF(DescriptorLayout)
|
||||
|
||||
@@ -501,7 +501,7 @@ void Graphics::createDevice(GraphicsInitializer initializer)
|
||||
numQueuesForFamily++;
|
||||
}
|
||||
}
|
||||
if ((currProps.queueFlags & VK_QUEUE_COMPUTE_BIT) == VK_QUEUE_COMPUTE_BIT)
|
||||
if (currProps.queueFlags & VK_QUEUE_COMPUTE_BIT)
|
||||
{
|
||||
if (computeQueueInfo.familyIndex == -1)
|
||||
{
|
||||
@@ -526,7 +526,7 @@ void Graphics::createDevice(GraphicsInitializer initializer)
|
||||
}
|
||||
}
|
||||
}
|
||||
if ((currProps.queueFlags & VK_QUEUE_TRANSFER_BIT) == VK_QUEUE_TRANSFER_BIT)
|
||||
if (currProps.queueFlags & VK_QUEUE_TRANSFER_BIT)
|
||||
{
|
||||
if (transferQueueInfo.familyIndex == -1)
|
||||
{
|
||||
@@ -587,6 +587,7 @@ void Graphics::createDevice(GraphicsInitializer initializer)
|
||||
deviceInfo.ppEnabledExtensionNames = initializer.deviceExtensions.data();
|
||||
deviceInfo.enabledLayerCount = (uint32_t)initializer.layers.size();
|
||||
deviceInfo.ppEnabledLayerNames = initializer.layers.data();
|
||||
deviceInfo.pEnabledFeatures = &features;
|
||||
|
||||
VK_CHECK(vkCreateDevice(physicalDevice, &deviceInfo, nullptr, &handle));
|
||||
std::cout << "Vulkan handle: " << handle << std::endl;
|
||||
|
||||
@@ -68,7 +68,6 @@ public:
|
||||
virtual Gfx::PPipelineLayout createPipelineLayout(Gfx::PPipelineLayout baseLayout = nullptr) override;
|
||||
|
||||
virtual void copyTexture(Gfx::PTexture srcTexture, Gfx::PTexture dstTexture) override;
|
||||
|
||||
protected:
|
||||
Array<const char *> getRequiredExtensions();
|
||||
void initInstance(GraphicsInitializer initInfo);
|
||||
|
||||
@@ -170,7 +170,7 @@ public:
|
||||
virtual ~VertexBuffer();
|
||||
|
||||
virtual void updateRegion(BulkResourceData update) override;
|
||||
|
||||
virtual void download(Array<uint8>& buffer) override;
|
||||
protected:
|
||||
// Inherited via Vulkan::Buffer
|
||||
virtual VkAccessFlags getSourceAccessMask();
|
||||
@@ -189,6 +189,7 @@ public:
|
||||
IndexBuffer(PGraphics graphics, const IndexBufferCreateInfo &resourceData);
|
||||
virtual ~IndexBuffer();
|
||||
|
||||
virtual void download(Array<uint8>& buffer) override;
|
||||
protected:
|
||||
// Inherited via Vulkan::Buffer
|
||||
virtual VkAccessFlags getSourceAccessMask();
|
||||
@@ -248,6 +249,7 @@ public:
|
||||
void executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage,
|
||||
VkAccessFlags dstAccess, VkPipelineStageFlags dstStage);
|
||||
void changeLayout(Gfx::SeImageLayout newLayout);
|
||||
void download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Array<uint8>& buffer);
|
||||
|
||||
private:
|
||||
//Updates via reference
|
||||
@@ -314,6 +316,7 @@ public:
|
||||
return textureHandle->getMipLevels();
|
||||
}
|
||||
virtual void changeLayout(Gfx::SeImageLayout newLayout) override;
|
||||
virtual void download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Array<uint8>& buffer) override;
|
||||
virtual void* getNativeHandle() override
|
||||
{
|
||||
return textureHandle;
|
||||
@@ -369,6 +372,7 @@ public:
|
||||
return textureHandle->getMipLevels();
|
||||
}
|
||||
virtual void changeLayout(Gfx::SeImageLayout newLayout) override;
|
||||
virtual void download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Array<uint8>& buffer) override;
|
||||
virtual void* getNativeHandle() override
|
||||
{
|
||||
return textureHandle;
|
||||
@@ -424,6 +428,7 @@ public:
|
||||
return textureHandle->getMipLevels();
|
||||
}
|
||||
virtual void changeLayout(Gfx::SeImageLayout newLayout) override;
|
||||
virtual void download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Array<uint8>& buffer) override;
|
||||
virtual void* getNativeHandle() override
|
||||
{
|
||||
return textureHandle;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "VulkanGraphicsEnums.h"
|
||||
#include "VulkanAllocator.h"
|
||||
#include "VulkanCommandBuffer.h"
|
||||
#include "Graphics/GraphicsEnums.h"
|
||||
#include <math.h>
|
||||
|
||||
using namespace Seele;
|
||||
@@ -127,7 +128,7 @@ TextureHandle::TextureHandle(PGraphics graphics, VkImageViewType viewType,
|
||||
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
region.imageSubresource.mipLevel = 0;
|
||||
region.imageSubresource.baseArrayLayer = 0;
|
||||
region.imageSubresource.layerCount = layerCount;
|
||||
region.imageSubresource.layerCount = arrayCount * layerCount;
|
||||
|
||||
region.imageOffset = {0, 0, 0};
|
||||
region.imageExtent = {sizeX, sizeY, sizeZ};
|
||||
@@ -212,6 +213,35 @@ void TextureHandle::changeLayout(Gfx::SeImageLayout newLayout)
|
||||
layout = newLayout;
|
||||
}
|
||||
|
||||
void TextureHandle::download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Array<uint8>& buffer)
|
||||
{
|
||||
uint64 imageSize = sizeX * sizeY * sizeZ * Gfx::getFormatInfo(format).blockSize;
|
||||
|
||||
PStagingBuffer stagingbuffer = graphics->getStagingManager()->allocateStagingBuffer(imageSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true);
|
||||
auto prevlayout = layout;
|
||||
changeLayout(Gfx::SE_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
|
||||
PCmdBuffer cmdBuffer = graphics->getQueueCommands(currentOwner)->getCommands();
|
||||
Gfx::FormatCompatibilityInfo formatInfo = Gfx::getFormatInfo(format);
|
||||
VkBufferImageCopy region = {
|
||||
.bufferOffset = 0,
|
||||
.bufferRowLength = 0,
|
||||
.bufferImageHeight = 0,
|
||||
.imageSubresource = {
|
||||
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
|
||||
.mipLevel = mipLevel,
|
||||
.baseArrayLayer = arrayLayer * layerCount + face,
|
||||
.layerCount = 1,
|
||||
},
|
||||
.imageOffset = { 0, 0, 0 },
|
||||
.imageExtent = { sizeX, sizeY, sizeZ },
|
||||
};
|
||||
vkCmdCopyImageToBuffer(cmdBuffer->getHandle(), image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, stagingbuffer->getHandle(), 1, ®ion);
|
||||
changeLayout(prevlayout);
|
||||
buffer.resize(stagingbuffer->getSize());
|
||||
void* data = stagingbuffer->getMappedPointer();
|
||||
std::memcpy(buffer.data(), data, buffer.size());
|
||||
}
|
||||
|
||||
void TextureHandle::executeOwnershipBarrier(Gfx::QueueType newOwner)
|
||||
{
|
||||
VkImageMemoryBarrier imageBarrier =
|
||||
@@ -307,6 +337,11 @@ void Texture2D::changeLayout(Gfx::SeImageLayout newLayout)
|
||||
textureHandle->changeLayout(newLayout);
|
||||
}
|
||||
|
||||
void Texture2D::download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Array<uint8>& buffer)
|
||||
{
|
||||
textureHandle->download(mipLevel, arrayLayer, face, buffer);
|
||||
}
|
||||
|
||||
void Texture2D::executeOwnershipBarrier(Gfx::QueueType newOwner)
|
||||
{
|
||||
textureHandle->executeOwnershipBarrier(newOwner);
|
||||
@@ -334,6 +369,10 @@ void Texture3D::changeLayout(Gfx::SeImageLayout newLayout)
|
||||
textureHandle->changeLayout(newLayout);
|
||||
}
|
||||
|
||||
void Texture3D::download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Array<uint8>& buffer)
|
||||
{
|
||||
textureHandle->download(mipLevel, arrayLayer, face, buffer);
|
||||
}
|
||||
void Texture3D::executeOwnershipBarrier(Gfx::QueueType newOwner)
|
||||
{
|
||||
textureHandle->executeOwnershipBarrier(newOwner);
|
||||
@@ -361,6 +400,10 @@ void TextureCube::changeLayout(Gfx::SeImageLayout newLayout)
|
||||
textureHandle->changeLayout(newLayout);
|
||||
}
|
||||
|
||||
void TextureCube::download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Array<uint8>& buffer)
|
||||
{
|
||||
textureHandle->download(mipLevel, arrayLayer, face, buffer);
|
||||
}
|
||||
void TextureCube::executeOwnershipBarrier(Gfx::QueueType newOwner)
|
||||
{
|
||||
textureHandle->executeOwnershipBarrier(newOwner);
|
||||
|
||||
Reference in New Issue
Block a user