more "cleanup"
This commit is contained in:
parent
ae1925511d
commit
1dbb93543e
5 changed files with 29 additions and 38 deletions
|
@ -176,9 +176,7 @@ bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const
|
|||
|
||||
const VkDeviceSize buffer_size = 65536;
|
||||
|
||||
PTK_OPTION(BufferStuff) vertex_buffer_stuff_opt = create_buffer(
|
||||
g_dev,
|
||||
g_physical_dev,
|
||||
PTK_OPTION(BufferStuff) vertex_buffer_stuff_opt = vk_create_buffer(
|
||||
buffer_size,
|
||||
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
|
||||
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
||||
|
@ -192,9 +190,7 @@ bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const
|
|||
g_vertex_buffer = vertex_buffer_stuff_opt.value.buffer;
|
||||
g_vertex_buffer_memory = vertex_buffer_stuff_opt.value.buffer_memory;
|
||||
|
||||
PTK_OPTION(BufferStuff) index_buffer_stuff_opt = create_buffer(
|
||||
g_dev,
|
||||
g_physical_dev,
|
||||
PTK_OPTION(BufferStuff) index_buffer_stuff_opt = vk_create_buffer(
|
||||
buffer_size,
|
||||
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
|
||||
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
||||
|
|
|
@ -2,12 +2,13 @@
|
|||
|
||||
#include "ptk_vk/buffer.h"
|
||||
|
||||
#include "ptk_vk/command_pool.h"
|
||||
#include "ptk_vk/device.h"
|
||||
#include "ptk_vk/physical_device.h"
|
||||
#include "ptk_vk/utils.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
PTK_OPTION_DEFINE(uint32_t);
|
||||
|
||||
PTK_OPTION(uint32_t) find_memory_type(VkPhysicalDevice physical_dev, const uint32_t type_filter, const VkMemoryPropertyFlags props) {
|
||||
VkPhysicalDeviceMemoryProperties mem_props;
|
||||
vkGetPhysicalDeviceMemoryProperties(physical_dev, &mem_props);
|
||||
|
@ -21,12 +22,12 @@ PTK_OPTION(uint32_t) find_memory_type(VkPhysicalDevice physical_dev, const uint3
|
|||
return PTK_OPTION_NONE(uint32_t);
|
||||
}
|
||||
|
||||
PTK_OPTION(BufferStuff) create_buffer(VkDevice dev, VkPhysicalDevice physical_dev, const VkDeviceSize size, const VkBufferUsageFlags usage, const VkMemoryPropertyFlags props) {
|
||||
PTK_OPTION(BufferStuff) vk_create_buffer(const VkDeviceSize size, const VkBufferUsageFlags usage, const VkMemoryPropertyFlags props) {
|
||||
BufferStuff ret;
|
||||
|
||||
VK_TRY(PTK_OPTION_NONE(BufferStuff),
|
||||
vkCreateBuffer(
|
||||
dev,
|
||||
g_dev,
|
||||
&(VkBufferCreateInfo){
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
||||
.pNext = NULL,
|
||||
|
@ -43,9 +44,9 @@ PTK_OPTION(BufferStuff) create_buffer(VkDevice dev, VkPhysicalDevice physical_de
|
|||
);
|
||||
|
||||
VkMemoryRequirements mem_reqs;
|
||||
vkGetBufferMemoryRequirements(dev, ret.buffer, &mem_reqs);
|
||||
vkGetBufferMemoryRequirements(g_dev, ret.buffer, &mem_reqs);
|
||||
|
||||
const PTK_OPTION(uint32_t) memory_type = find_memory_type(physical_dev, mem_reqs.memoryTypeBits, props);
|
||||
const PTK_OPTION(uint32_t) memory_type = find_memory_type(g_physical_dev, mem_reqs.memoryTypeBits, props);
|
||||
|
||||
if (!memory_type.exists) {
|
||||
PTK_ERR("failed to find suitable memory type");
|
||||
|
@ -54,7 +55,7 @@ PTK_OPTION(BufferStuff) create_buffer(VkDevice dev, VkPhysicalDevice physical_de
|
|||
|
||||
VK_TRY(PTK_OPTION_NONE(BufferStuff),
|
||||
vkAllocateMemory(
|
||||
dev,
|
||||
g_dev,
|
||||
&(VkMemoryAllocateInfo){
|
||||
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
|
||||
.pNext = NULL,
|
||||
|
@ -66,21 +67,21 @@ PTK_OPTION(BufferStuff) create_buffer(VkDevice dev, VkPhysicalDevice physical_de
|
|||
)
|
||||
);
|
||||
|
||||
vkBindBufferMemory(dev, ret.buffer, ret.buffer_memory, 0);
|
||||
vkBindBufferMemory(g_dev, ret.buffer, ret.buffer_memory, 0);
|
||||
|
||||
return PTK_OPTION_SOME(BufferStuff, ret);
|
||||
}
|
||||
|
||||
bool copy_buffer(VkDevice dev, VkCommandPool command_pool, const VkBuffer src, const VkBuffer dst, const VkDeviceSize size, VkQueue graphics_queue) {
|
||||
bool copy_buffer(const VkBuffer src, const VkBuffer dst, const VkDeviceSize size) {
|
||||
VkCommandBuffer command_buffer;
|
||||
|
||||
VK_TRY(false,
|
||||
vkAllocateCommandBuffers(
|
||||
dev,
|
||||
g_dev,
|
||||
&(VkCommandBufferAllocateInfo){
|
||||
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
|
||||
.pNext = NULL,
|
||||
.commandPool = command_pool,
|
||||
.commandPool = g_command_pool,
|
||||
.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
|
||||
.commandBufferCount = 1,
|
||||
},
|
||||
|
@ -108,7 +109,7 @@ bool copy_buffer(VkDevice dev, VkCommandPool command_pool, const VkBuffer src, c
|
|||
);
|
||||
|
||||
vkQueueSubmit(
|
||||
graphics_queue,
|
||||
g_graphics_queue,
|
||||
1,
|
||||
&(VkSubmitInfo){
|
||||
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
|
||||
|
@ -124,19 +125,17 @@ bool copy_buffer(VkDevice dev, VkCommandPool command_pool, const VkBuffer src, c
|
|||
VK_NULL_HANDLE
|
||||
);
|
||||
|
||||
vkQueueWaitIdle(graphics_queue);
|
||||
vkQueueWaitIdle(g_graphics_queue);
|
||||
|
||||
vkFreeCommandBuffers(dev, command_pool, 1, &command_buffer);
|
||||
vkFreeCommandBuffers(g_dev, g_command_pool, 1, &command_buffer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool transfer_to_buffer(VkDevice dev, VkPhysicalDevice physical_dev, VkCommandPool command_pool, void *src, size_t src_size, VkBuffer buffer, VkQueue graphics_queue) {
|
||||
bool vk_transfer_to_buffer(void *src, size_t src_size, VkBuffer buffer) {
|
||||
const VkDeviceSize buffer_size = src_size;
|
||||
|
||||
PTK_OPTION(BufferStuff) staging_buffer_stuff_opt = create_buffer(
|
||||
dev,
|
||||
physical_dev,
|
||||
PTK_OPTION(BufferStuff) staging_buffer_stuff_opt = vk_create_buffer(
|
||||
buffer_size,
|
||||
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
||||
|
@ -151,17 +150,17 @@ bool transfer_to_buffer(VkDevice dev, VkPhysicalDevice physical_dev, VkCommandPo
|
|||
VkDeviceMemory staging_buffer_memory = staging_buffer_stuff_opt.value.buffer_memory;
|
||||
|
||||
void *data;
|
||||
vkMapMemory(dev, staging_buffer_memory, 0, buffer_size, 0, &data);
|
||||
vkMapMemory(g_dev, staging_buffer_memory, 0, buffer_size, 0, &data);
|
||||
memcpy(data, src, (size_t)buffer_size);
|
||||
vkUnmapMemory(dev, staging_buffer_memory);
|
||||
vkUnmapMemory(g_dev, staging_buffer_memory);
|
||||
|
||||
if (!copy_buffer(dev, command_pool, staging_buffer, buffer, buffer_size, graphics_queue)) {
|
||||
if (!copy_buffer(staging_buffer, buffer, buffer_size)) {
|
||||
PTK_ERR("failed copying staging buffer to vertex buffer");
|
||||
return false;
|
||||
}
|
||||
|
||||
vkDestroyBuffer(dev, staging_buffer, NULL);
|
||||
vkFreeMemory(dev, staging_buffer_memory, NULL);
|
||||
vkDestroyBuffer(g_dev, staging_buffer, NULL);
|
||||
vkFreeMemory(g_dev, staging_buffer_memory, NULL);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -14,8 +14,8 @@ typedef struct {
|
|||
|
||||
PTK_OPTION_DEFINE(BufferStuff);
|
||||
|
||||
PTK_OPTION(BufferStuff) create_buffer(VkDevice dev, VkPhysicalDevice physical_dev, const VkDeviceSize size, const VkBufferUsageFlags usage, const VkMemoryPropertyFlags props);
|
||||
PTK_OPTION(BufferStuff) vk_create_buffer(const VkDeviceSize size, const VkBufferUsageFlags usage, const VkMemoryPropertyFlags props);
|
||||
|
||||
bool transfer_to_buffer(VkDevice dev, VkPhysicalDevice physical_dev, VkCommandPool command_pool, void *src, size_t src_size, VkBuffer buffer, VkQueue graphics_queue);
|
||||
bool vk_transfer_to_buffer(void *src, size_t src_size, VkBuffer buffer);
|
||||
|
||||
#endif // PTK_PTK_VK_BUFFER_H_
|
||||
|
|
|
@ -9,7 +9,6 @@
|
|||
#include "ptk_vk/descriptors.h"
|
||||
#include "ptk_vk/device.h"
|
||||
#include "ptk_vk/instance.h"
|
||||
#include "ptk_vk/physical_device.h"
|
||||
#include "ptk_vk/pipeline.h"
|
||||
#include "ptk_vk/render_pass.h"
|
||||
#include "ptk_vk/swapchain.h"
|
||||
|
@ -33,7 +32,7 @@ bool vk_transfer_vertex_data(void) {
|
|||
PTK_DEBUG("transferring vertices to gpu…");
|
||||
|
||||
const size_t vertices_size = sizeof(g_vertices.data[0]) * g_vertices.size;
|
||||
if (!transfer_to_buffer(g_dev, g_physical_dev, g_command_pool, g_vertices.data, vertices_size, g_vertex_buffer, g_graphics_queue)) {
|
||||
if (!vk_transfer_to_buffer(g_vertices.data, vertices_size, g_vertex_buffer)) {
|
||||
PTK_ERR("failed transferring vertices");
|
||||
return false;
|
||||
}
|
||||
|
@ -45,7 +44,7 @@ bool vk_transfer_vertex_data(void) {
|
|||
PTK_DEBUG("transferring indices to gpu…");
|
||||
|
||||
const size_t indices_size = sizeof(g_indices.data[0]) * g_indices.size;
|
||||
if (!transfer_to_buffer(g_dev, g_physical_dev, g_command_pool, g_indices.data, indices_size, g_index_buffer, g_graphics_queue)) {
|
||||
if (!vk_transfer_to_buffer(g_indices.data, indices_size, g_index_buffer)) {
|
||||
PTK_ERR("failed transferring indices");
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
|
||||
#include "ptk_vk/buffer.h"
|
||||
#include "ptk_vk/device.h"
|
||||
#include "ptk_vk/physical_device.h"
|
||||
#include "ptk_vk/sync_objects.h"
|
||||
|
||||
#include "ptk_log.h"
|
||||
|
@ -21,9 +20,7 @@ bool vk_create_uniform_buffers(void) {
|
|||
g_uniform_buffers_mapped = PTK_LIST_NEW(voidptr, g_max_frames_in_flight);
|
||||
|
||||
for (size_t i = 0; i < g_max_frames_in_flight; ++i) {
|
||||
PTK_OPTION(BufferStuff) uniform_buffer_stuff_opt = create_buffer(
|
||||
g_dev,
|
||||
g_physical_dev,
|
||||
PTK_OPTION(BufferStuff) uniform_buffer_stuff_opt = vk_create_buffer(
|
||||
buffer_size,
|
||||
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
||||
|
|
Loading…
Reference in a new issue