Skip to content

Commit

Permalink
vulkan WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mtuncbilek95 committed Oct 20, 2024
1 parent e455bce commit e651749
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 2 deletions.
60 changes: 60 additions & 0 deletions Source/Engine/VulkanRHI/Buffer/VDstBuffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "VDstBuffer.h"

#include "../Core/VAssert.h"
#include "../Core/VLoadFuncs.h"
#include "../Device/VDevice.h"

namespace MAGE
{
VDstBuffer::VDstBuffer(const DstBufferProps& desc, VDevice* device) : VObject(device), m_props(desc)
{
VkBufferCreateInfo bufferInfo = {};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = desc.sizeInBytes;
bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | desc.usage;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;

ErrorUtils::VkAssert(vkCreateBuffer(m_rootDevice->GetDevice(), &bufferInfo, nullptr, &m_buffer), "VulkanDescBuffer");

VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(m_rootDevice->GetDevice(), m_buffer, &memRequirements);

VkMemoryAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = device->FindMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);

ErrorUtils::VkAssert(vkAllocateMemory(m_rootDevice->GetDevice(), &allocInfo, nullptr, &m_memory), "VulkanDescBuffer");
ErrorUtils::VkAssert(vkBindBufferMemory(m_rootDevice->GetDevice(), m_buffer, m_memory, 0), "VulkanDescBuffer");

m_totalSize = bufferInfo.size;
}

VDstBuffer::~VDstBuffer()
{
Destroy();
}

void VDstBuffer::MapMemory(RawBuffer buffer)
{
void* data;
ErrorUtils::VkAssert(vkMapMemory(m_rootDevice->GetDevice(), m_memory, 0, m_totalSize, 0, &data), "VulkanDescBuffer");
memcpy(data, buffer.Data(), buffer.Size());
vkUnmapMemory(m_rootDevice->GetDevice(), m_memory);
}

void VDstBuffer::Destroy()
{
if (m_buffer != VK_NULL_HANDLE)
{
vkDestroyBuffer(m_rootDevice->GetDevice(), m_buffer, nullptr);
m_buffer = VK_NULL_HANDLE;
}

if (m_memory != VK_NULL_HANDLE)
{
vkFreeMemory(m_rootDevice->GetDevice(), m_memory, nullptr);
m_memory = VK_NULL_HANDLE;
}
}
}
42 changes: 42 additions & 0 deletions Source/Engine/VulkanRHI/Buffer/VDstBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2024 Metehan Tuncbilek
*/

#pragma once

#include "../Abstraction/VObject.h"
#include "Engine/Memory/RawBuffer.h"

namespace MAGE
{
struct DstBufferProps final
{
usize sizeInBytes;
VkBufferUsageFlags usage;
};

class VDstBuffer final : public VObject
{
public:
VDstBuffer(const DstBufferProps& desc, VDevice* device);
~VDstBuffer() override final;

inline VkBuffer GetBuffer() const { return m_buffer; }
inline VkDeviceMemory GetMemory() const { return m_memory; }
inline u64 GetTotalSize() const { return m_totalSize; }

void MapMemory(RawBuffer buffer);
void Destroy() override final;

private:
VkBuffer m_buffer;
VkDeviceMemory m_memory;
VkDeviceSize m_totalSize;

DstBufferProps m_props;
};
}
59 changes: 59 additions & 0 deletions Source/Engine/VulkanRHI/Buffer/VStageBuffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "VStageBuffer.h"

#include "../Core/VAssert.h"
#include "../Core/VLoadFuncs.h"
#include "../Device/VDevice.h"

namespace MAGE
{
VStageBuffer::VStageBuffer(const StageBufferProps& desc, VDevice* device) : VObject(device), m_props(desc)
{
VkBufferCreateInfo bufferInfo = {};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = desc.sizeInBytes;
bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;

ErrorUtils::VkAssert(vkCreateBuffer(m_rootDevice->GetDevice(), &bufferInfo, nullptr, &m_buffer), "VStageBuffer");

VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(m_rootDevice->GetDevice(), m_buffer, &memRequirements);

VkMemoryAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = device->FindMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);

ErrorUtils::VkAssert(vkAllocateMemory(m_rootDevice->GetDevice(), &allocInfo, nullptr, &m_memory), "VStageBuffer");
ErrorUtils::VkAssert(vkBindBufferMemory(m_rootDevice->GetDevice(), m_buffer, m_memory, 0), "VStageBuffer");

m_totalSize = bufferInfo.size;
}

VStageBuffer::~VStageBuffer()
{
Destroy();
}

void VStageBuffer::MapMemory(RawBuffer buffer)
{
void* data;
ErrorUtils::VkAssert(vkMapMemory(m_rootDevice->GetDevice(), m_memory, 0, m_totalSize, 0, &data), "VStageBuffer");
memcpy(data, buffer.Data(), buffer.Size());
}

void VStageBuffer::Destroy()
{
if (m_buffer != VK_NULL_HANDLE)
{
vkDestroyBuffer(m_rootDevice->GetDevice(), m_buffer, nullptr);
m_buffer = VK_NULL_HANDLE;
}

if (m_memory != VK_NULL_HANDLE)
{
vkFreeMemory(m_rootDevice->GetDevice(), m_memory, nullptr);
m_memory = VK_NULL_HANDLE;
}
}
}
41 changes: 41 additions & 0 deletions Source/Engine/VulkanRHI/Buffer/VStageBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2024 Metehan Tuncbilek
*/

#pragma once

#include "../Abstraction/VObject.h"
#include "Engine/Memory/RawBuffer.h"

namespace MAGE
{
struct StageBufferProps final
{
usize sizeInBytes;
};

class VStageBuffer final : public VObject
{
public:
VStageBuffer(const StageBufferProps& desc, VDevice* device);
~VStageBuffer() override final;

inline VkBuffer GetBuffer() const { return m_buffer; }
inline VkDeviceMemory GetMemory() const { return m_memory; }
inline u64 GetTotalSize() const { return m_totalSize; }

void MapMemory(RawBuffer buffer);
void Destroy() override final;

private:
VkBuffer m_buffer;
VkDeviceMemory m_memory;
VkDeviceSize m_totalSize;

StageBufferProps m_props;
};
}
2 changes: 0 additions & 2 deletions Source/Engine/VulkanRHI/Command/VCmdBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

#include "../Abstraction/VObject.h"

#include <span>

namespace MAGE
{
class VCmdPool;
Expand Down
Empty file.
Empty file.
45 changes: 45 additions & 0 deletions Source/Engine/VulkanRHI/Sampler/VSampler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "VSampler.h"

#include "../Core/VAssert.h"
#include "../Core/VLoadFuncs.h"
#include "../Device/VDevice.h"

namespace MAGE
{
VSampler::VSampler(const SamplerProps& desc, VDevice* device) : VObject(device), m_props(desc)
{
VkSamplerCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
info.magFilter = desc.magFilter;
info.minFilter = desc.minFilter;
info.mipmapMode = desc.mipmapMode;
info.addressModeU = desc.addressModeU;
info.addressModeV = desc.addressModeV;
info.addressModeW = desc.addressModeW;
info.mipLodBias = desc.mipLodBias;
info.anisotropyEnable = desc.anisotropyEnable ? VK_TRUE : VK_FALSE;
info.maxAnisotropy = desc.maxAnisotropy;
info.compareEnable = desc.compareEnable ? VK_TRUE : VK_FALSE;
info.compareOp = desc.compareOp;
info.minLod = desc.minLod;
info.maxLod = desc.maxLod;
info.borderColor = desc.borderColor;
info.unnormalizedCoordinates = VK_FALSE;

ErrorUtils::VkAssert(vkCreateSampler(m_rootDevice->GetDevice(), &info, nullptr, &m_sampler), "VSampler");
}

VSampler::~VSampler()
{
Destroy();
}

void VSampler::Destroy()
{
if (m_sampler != VK_NULL_HANDLE)
{
vkDestroySampler(m_rootDevice->GetDevice(), m_sampler, nullptr);
m_sampler = VK_NULL_HANDLE;
}
}
}
48 changes: 48 additions & 0 deletions Source/Engine/VulkanRHI/Sampler/VSampler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2024 Metehan Tuncbilek
*/

#pragma once

#include "../Abstraction/VObject.h"

namespace MAGE
{
struct SamplerProps final
{
VkFilter magFilter;
VkFilter minFilter;
VkSamplerMipmapMode mipmapMode;
VkSamplerAddressMode addressModeU;
VkSamplerAddressMode addressModeV;
VkSamplerAddressMode addressModeW;
f32 mipLodBias;
b8 anisotropyEnable;
f32 maxAnisotropy;
b8 compareEnable;
VkCompareOp compareOp;
f32 minLod;
f32 maxLod;
VkBorderColor borderColor;
};

class VSampler final : public VObject
{
public:
VSampler(const SamplerProps& desc, VDevice* device);
~VSampler() override final;

inline VkSampler GetSampler() const { return m_sampler; }

void Destroy() override final;

private:
SamplerProps m_props;

VkSampler m_sampler;
};
}

0 comments on commit e651749

Please sign in to comment.