vulkan游戏引擎的renderer下的vulkan缓冲区实现

1.vulkan_buffer.h

#pragma once

#include "vulkan_types.inl"

b8 vulkan_buffer_create(

vulkan_context* context,

u64 size,

VkBufferUsageFlagBits usage,

u32 memory_property_flags,

b8 bind_on_create,

vulkan_buffer* out_buffer

);

void vulkan_buffer_destroy(vulkan_context* context,vulkan_buffer* buffer);

b8 vulkan_buffer_resize(

vulkan_context* context,

u64 new_size,

vulkan_buffer* buffer,

VkQueue queue,

VkCommandPool pool

);

void vulkan_buffer_bind(vulkan_context* context,vulkan_buffer* buffer,u64 offset);

void* vulkan_buffer_lock_memory(vulkan_context* context,vulkan_buffer* buffer,u64 offset,u64 size,u32 flags);

void vulkan_buffer_unlock_memory(vulkan_context* context, vulkan_buffer* buffer);

void vulkan_buffer_load_data(vulkan_context* context,vulkan_buffer* buffer,u64 offset,u64 size,u32 flags,const void* data);

void vulkan_buffer_copy_to(

vulkan_context* context,

VkCommandPool pool,

VkFence fence,

VkQueue queue,

VkBuffer source,

u64 source_offset,

VkBuffer dest,

u64 dest_offset,

u64 size

);

2.vulkan_buffer.c

#include "vulkan_buffer.h"

#include "vulkan_device.h"

#include "vulkan_command_buffer.h"

#include "vulkan_types.inl"

#include "vulkan_utils.h"

#include "core/logger.h"

#include "core/kmemory.h"

b8 vulkan_buffer_create(

vulkan_context* context,

u64 size,

VkBufferUsageFlagBits usage,

u32 memory_property_flags,

b8 bind_on_create,

vulkan_buffer* out_buffer

)

{

kzero_memory(out_buffer,sizeof(vulkan_buffer));

out_buffer->total_size = size;

out_buffer->usage = usage;

out_buffer->memory_property_flags = memory_property_flags;

VkBufferCreateInfo buffer_info = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};

buffer_info.size = size;

buffer_info.usage = usage;

buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;

VK_CHECK(vkCreateBuffer(context->device.logical_device,&buffer_info,context->allocator,&out_buffer->handle));

//Gather memory requirements

VkMemoryRequirements requirements;

vkGetBufferMemoryRequirements(context->device.logical_device,out_buffer->handle,&requirements);

out_buffer->memory_index = context->find_memory_index(requirements.memoryTypeBits,out_buffer->memory_property_flags);

if(out_buffer->memory_index == -1)

{

KERROR("Unable to create vulkan buffer because the reuqired memory type index was not found.");

return false;

}

//Allocacte memory info

VkMemoryAllocateInfo allocate_info = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};

allocate_info.allocationSize = requirements.size;

allocate_info.memoryTypeIndex = (u32)out_buffer->memory_index;

//Allocate the memory

VkResult result = vkAllocateMemory(

context->device.logical_device,

&allocate_info,

context->allocator,

&out_buffer->memory

);

if(result != VK_SUCCESS)

{

KERROR("Unable to create vulkan buffer because the required memory allocation failed.Error: %i",result);

return false;

}

if(bind_on_create)

{

vulkan_buffer_bind(context,out_buffer,0);

}

return true;

}

void vulkan_buffer_destroy(vulkan_context* context,vulkan_buffer* buffer)

{

if(buffer->memory)

{

vkFreeMemory(context->device.logical_device,buffer->memory,context->allocator);

buffer->memory = 0;

}

if(buffer->handle)

{

vkDestroyBuffer(context->device.logical_device,buffer->handle,context->allocator);

buffer->handle = 0;

}

buffer->total_size= 0;

buffer->usage = 0;

buffer->is_locked = false;

}

b8 vulkan_buffer_resize(

vulkan_context* context,

u64 new_size,

vulkan_buffer* buffer,

VkQueue queue,

VkCommandPool pool

)

{

//Create new buffer

VkBufferCreateInfo buffer_info = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};

buffer_info.size = new_size;

buffer_info.usage = buffer->usage;

buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;

VkBuffer new_buffer;

VK_CHECK(vkCreateBuffer(context->device.logical_device,&buffer_info,context->allocator,&new_buffer));

//Gather memory requirements

VkMemoryRequirements requirements;

vkGetBufferMemoryRequirements(context->device.logical_device,new_buffer,&requirements);

//Allocaate memory info

VkMemoryAllocateInfo allocate_info = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};

allocate_info.allocationSize = requirements.size;

allocate_info.memoryTypeIndex = (u32)buffer->memory_index;

//Alocate the memory

VkDeviceMemory new_memory;

VkResult result = vkAllocateMemory(context->device.logical_device,&allocate_info,context->allocator,&new_memory);

if(result!=VK_SUCCESS)

{

KERROR("Unable to resize vulkan buffer because the required memory allocation failed. Error: %i",result);

return false;

}

//Bind

VK_CHECK(vkBindBufferMemory(context->device.logical_device,new_buffer,new_memory,0));

//copy over the data

vulkan_buffer_copy_to(context,pool,0,queue,buffer->handle,0,new_buffer,0,buffer->total_size);

vkDeviceWaitIdle(context->device.logical_device);

//Destroy the old

if(buffer->memory)

{

vkFreeMemory(context->device.logical_device,buffer->memory,context->allocator);

buffer->memory = 0;

}

if(buffer->handle)

{

vkDestroyBuffer(context->device.logical_device,buffer->handle,context->allocator);

buffer->handle = 0;

}

//Set new properties

buffer->total_size = new_size;

buffer->memory = new_memory;

buffer->handle = new_buffer;

return true;

}

void vulkan_buffer_bind(vulkan_context* context,vulkan_buffer* buffer,u64 offset)

{

VK_CHECK(vkBindBufferMemory(context->device.logical_device,buffer->handle,buffer->memory,offset));

}

void* vulkan_buffer_lock_memory(vulkan_context* context,vulkan_buffer* buffer,u64 offset,u64 size,u32 flags)

{

void* data;

VK_CHECK(vkMapMemory(context->device.logical_device,buffer->memory,offset,size,flags,&data));

return data;

}

void vulkan_buffer_unlock_memory(vulkan_context* context,vulkan_buffer* buffer)

{

vkUnmapMemory(context->device.logical_device,buffer->memory);

}

void vulkan_buffer_load_data(vulkan_context* context,vulkan_buffer* buffer,u64 offset,u64 size,u32 flags,const void* data)

{

void* data_ptr;

VK_CHECK(vkMapMemory(context->device.logical_device,buffer->memory,offset,size,flags,&data_ptr));

kcopy_memory(data_ptr,data,size);

vkUnmapMemory(context->device.logical_device,buffer->memory);

}

void vulkan_buffer_copy_to(

vulkan_context* context,

VkCommandPool pool,

VkFence fence,

VkQueue queue,

VkBuffer source,

u64 source_offset,

VkBuffer dest,

u64 dest_offset,

u64 size

)

{

vkQueueWaitIdle(queue);

vulkan_command_buffer temp_command_buffer;

vulkan_command_buffer_allocate_and_begin_single_use(context,pool,&temp_command_buffer);

VkBufferCopy copy_region;

copy_region.srcOffset = source_offset;

copy_region.dstOffset = dest_offset;

copy_region.size = size;

vkCmdCopyBuffer(temp_command_buffer.handle,source,dest,1,&copy_region);

vulkan_command_buffer_end_single_use(context,pool,&temp_command_buffer,queue);

}

相关推荐
嘉琪0011 天前
vue3+ts面试题(一)JSX,SFC
前端·javascript·react.js
952361 天前
数据结构-顺序表
java·数据结构·学习
tealcwu1 天前
【Unity踩坑】Unity测试用例命名空间错误解决方案
unity·游戏引擎·测试用例
chxii1 天前
Apache Tomcat 介绍
java·tomcat·apache
何贤1 天前
🪐 行星科技概念官网!Hero Section 回归!(Three.js ✨)
前端·javascript·three.js
前端拿破轮1 天前
ReactNative从入门到性能优化(一)
前端·react native·客户端
码界奇点1 天前
Java Web学习 第1篇前端基石HTML 入门与核心概念解析
java·前端·学习·xhtml
.ZGR.1 天前
蓝桥杯高校新生编程赛第二场题解——Java
java·算法·蓝桥杯
云枫晖1 天前
Webpack系列-开发环境
前端·webpack
陈果然DeepVersion1 天前
Java大厂面试真题:Spring Boot+Kafka+AI智能客服场景全流程解析(四)
java·spring boot·微服务·kafka·面试题·rag·ai智能客服