我一直在开发一个基于Vulkan的图形引擎,在尝试创建缓冲区时,我遇到了vkMapMemory()
函数的问题。在下面所示的代码中,程序返回一个"0";访问被拒绝";(-103741819或C0000005(错误。vkMapMemory()
调用内部发生错误,我无法检查其返回的VkResult
。
void createVertexBuffer(size_t sizeOfBuffer, void * _vertices , VkBuffer * _vertexBuffer, VkDeviceMemory * _vertexBufferMemory)
{
VkDeviceSize bufferSize = sizeOfBuffer;
VkBuffer stagingBuffer;
VkDeviceMemory stagingBufferMemory;
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &stagingBuffer, &stagingBufferMemory);
void* data;
if(vkMapMemory(_device, *_vertexBufferMemory, 0, bufferSize, 0, &data) != VK_SUCCESS)
printf("This is not printed");
memcpy(data, _vertices, sizeOfBuffer);
vkUnmapMemory(_device, *_vertexBufferMemory);
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, _vertexBuffer, _vertexBufferMemory);
copyBuffer(stagingBuffer, *_vertexBuffer, bufferSize);
vkDestroyBuffer(_device, stagingBuffer, NULL);
vkFreeMemory(_device, stagingBufferMemory, NULL);
return;
}
更多信息:
sizeOfBuffer
在这种情况下等于96
_vertices
数组大小实际上也等于96
,因为它看起来是这样的:
_vertices[0][0] -1.000000 0.000000
_vertices[0][1] -1.000000 0.000000
_vertices[0][2] 0.000000 0.000000
_vertices[1][0] 1.000000 0.000000
_vertices[1][1] -1.000000 0.000000
_vertices[1][2] 0.000000 0.000000
_vertices[2][0] 1.000000 0.000000
_vertices[2][1] 1.000000 0.000000
_vertices[2][2] 0.000000 0.000000
_vertices[3][0] -1.000000 0.000000
_vertices[3][1] 1.000000 0.000000
_vertices[3][2] 0.000000 0.000000
- 三行的大小等于24,因为
(sizeof(glm::vec3)=12)
和三行包含两个glm::vec3
结构:
typedef struct MKTAGA2 {
glm::vec3 position;
glm::vec3 color;
} MKTAGA;
我本来应该映射stagingBufferMemory
的时候却在映射_vertexBufferMemory
这是固定代码:
void createVertexBuffer(size_t sizeOfBuffer, void * _vertices , VkBuffer * _vertexBuffer, VkDeviceMemory * _vertexBufferMemory)
{
VkDeviceSize bufferSize = sizeOfBuffer;
VkBuffer stagingBuffer;
VkDeviceMemory stagingBufferMemory;
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &stagingBuffer, &stagingBufferMemory);
void* data;
vkMapMemory(_device, stagingBufferMemory, 0, bufferSize, 0, &data);
memcpy(data, _vertices, sizeOfBuffer);
vkUnmapMemory(_device, stagingBufferMemory);
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, _vertexBuffer, _vertexBufferMemory);
copyBuffer(stagingBuffer, *_vertexBuffer, bufferSize);
vkDestroyBuffer(_device, stagingBuffer, NULL);
vkFreeMemory(_device, stagingBufferMemory, NULL);
return;
}