Add buffer code, doesn't work

This commit is contained in:
Bailey Harrison 2022-10-21 17:03:36 +01:00
parent f697d37791
commit eb1884ee8e
4 changed files with 43 additions and 22 deletions

View File

@ -10,23 +10,11 @@ namespace engine {
FRAGMENT, FRAGMENT,
}; };
struct Shader {
ShaderType type;
uint32_t handle;
};
typedef uint32_t Program;
enum class BufferType { enum class BufferType {
VERTEX, VERTEX,
INDEX, INDEX,
}; };
struct Buffer {
BufferType type;
uint32_t handle;
};
enum class Primitive { enum class Primitive {
POINTS, POINTS,
LINES, LINES,

View File

@ -49,7 +49,8 @@ namespace engine {
void createPipeline(const char* vertShaderPath, const char* fragShaderPath); void createPipeline(const char* vertShaderPath, const char* fragShaderPath);
// creates a vertex array for holding mesh data // creates a vertex array for holding mesh data
bool createBuffer(const gfx::BufferDesc& desc, const void* data, gfx::BufferHandle* out); gfx::BufferHandle* createVertexBuffer(const gfx::BufferDesc& desc, const void* vertices, const void* indices);
void destroyBuffer(const gfx::BufferHandle* buffer);
// wait until all the active GPU queues have finished working // wait until all the active GPU queues have finished working
void waitIdle(); void waitIdle();

View File

@ -3,6 +3,7 @@
#include "window.hpp" #include "window.hpp"
#include "gfx_device.hpp" #include "gfx_device.hpp"
#include "resource_manager.hpp" #include "resource_manager.hpp"
#include "log.hpp"
namespace engine { namespace engine {
@ -14,11 +15,17 @@ namespace engine {
engine::ResourceManager resMan{}; engine::ResourceManager resMan{};
m_gfx->createPipeline(resMan.getFilePath("shader.vert.spv").string().c_str(), resMan.getFilePath("shader.frag.spv").string().c_str()); m_gfx->createPipeline(resMan.getFilePath("shader.vert.spv").string().c_str(), resMan.getFilePath("shader.frag.spv").string().c_str());
gfx::BufferDesc bufferDesc {
.size = 65536,
};
gfx::BufferHandle* buffer = m_gfx->createBuffer(bufferDesc, nullptr);
m_gfx->destroyBuffer(buffer);
} }
Application::~Application() Application::~Application()
{ {
} }
void Application::gameLoop() void Application::gameLoop()

View File

@ -56,6 +56,8 @@ namespace engine {
std::vector<VkImageView> imageViews{}; std::vector<VkImageView> imageViews{};
std::vector<VkFramebuffer> framebuffers{}; std::vector<VkFramebuffer> framebuffers{};
VkQueue activeQueue{};
VkRenderPass renderpass; VkRenderPass renderpass;
VkSemaphore acquireSemaphore = VK_NULL_HANDLE; // waits until the image is available VkSemaphore acquireSemaphore = VK_NULL_HANDLE; // waits until the image is available
@ -67,17 +69,19 @@ namespace engine {
VkPipeline handle = VK_NULL_HANDLE; VkPipeline handle = VK_NULL_HANDLE;
}; };
struct AllocatedBuffer {
VkBuffer buffer = VK_NULL_HANDLE;
VmaAllocation allocation = nullptr;
};
enum class QueueFlags : uint32_t { enum class QueueFlags : uint32_t {
GRAPHICS = (1 << 0), GRAPHICS = (1 << 0),
TRANSFER = (1 << 1), TRANSFER = (1 << 1),
COMPUTE = (1 << 2), COMPUTE = (1 << 2),
}; };
// handles
struct gfx::BufferHandle {
VkBuffer buffer = VK_NULL_HANDLE;
VmaAllocation allocation = nullptr;
};
// functions // functions
@ -516,6 +520,7 @@ namespace engine {
std::vector<Queue> queues{}; std::vector<Queue> queues{};
Queue gfxQueue; Queue gfxQueue;
Queue presentQueue;
VkCommandPool commandPool = VK_NULL_HANDLE; VkCommandPool commandPool = VK_NULL_HANDLE;
VkCommandBuffer commandBuffer = VK_NULL_HANDLE; VkCommandBuffer commandBuffer = VK_NULL_HANDLE;
@ -825,6 +830,8 @@ namespace engine {
vkGetDeviceQueue(pimpl->device, q.familyIndex, q.queueIndex, &q.handle); vkGetDeviceQueue(pimpl->device, q.familyIndex, q.queueIndex, &q.handle);
} }
pimpl->presentQueue = getQueueSupporting(pimpl->queues, QueueFlags::TRANSFER);
pimpl->gfxQueue = getQueueSupporting(pimpl->queues, QueueFlags::GRAPHICS); pimpl->gfxQueue = getQueueSupporting(pimpl->queues, QueueFlags::GRAPHICS);
VkCommandPoolCreateInfo gfxCmdPoolInfo{ VkCommandPoolCreateInfo gfxCmdPoolInfo{
@ -1036,7 +1043,7 @@ namespace engine {
presentInfo.pImageIndices = &imageIndex; presentInfo.pImageIndices = &imageIndex;
presentInfo.pResults = nullptr; presentInfo.pResults = nullptr;
res = vkQueuePresentKHR(pimpl->gfxQueue.handle, &presentInfo); res = vkQueuePresentKHR(pimpl->presentQueue.handle, &presentInfo);
if (res == VK_SUBOPTIMAL_KHR || res == VK_ERROR_OUT_OF_DATE_KHR) { if (res == VK_SUBOPTIMAL_KHR || res == VK_ERROR_OUT_OF_DATE_KHR) {
// recreate swapchain // recreate swapchain
waitIdle(); waitIdle();
@ -1198,9 +1205,27 @@ namespace engine {
} }
bool GFXDevice::createBuffer(const gfx::BufferDesc& desc, const void* data, gfx::BufferHandle* out) gfx::BufferHandle* GFXDevice::createBuffer(const gfx::BufferDesc& desc, const void* data)
{ {
return false; auto out = new gfx::BufferHandle{};
VkBufferCreateInfo createInfo{ VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
createInfo.size = desc.size;
createInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
VmaAllocationCreateInfo allocInfo{};
allocInfo.usage = VMA_MEMORY_USAGE_AUTO;
VkResult res = vmaCreateBuffer(pimpl->allocator, &createInfo, &allocInfo, &out->buffer, &out->allocation, nullptr);
assert(res == VK_SUCCESS);
return out;
}
void GFXDevice::destroyBuffer(const gfx::BufferHandle* buffer)
{
vmaDestroyBuffer(pimpl->allocator, buffer->buffer, buffer->allocation);
delete buffer;
} }
void GFXDevice::waitIdle() void GFXDevice::waitIdle()