Use createBuffer function from Vulkan device class

This commit is contained in:
saschawillems 2017-01-07 20:46:28 +01:00
parent 927660680d
commit e573a4c244
15 changed files with 222 additions and 233 deletions

View file

@ -65,9 +65,9 @@ public:
uint32_t queueFamilyIndex; // Family index of the graphics queue, used for barriers uint32_t queueFamilyIndex; // Family index of the graphics queue, used for barriers
} compute; } compute;
struct { vk::Buffer vertexBuffer;
vkMeshLoader::MeshBuffer quad; vk::Buffer indexBuffer;
} meshes; uint32_t indexCount;
vk::Buffer uniformBufferVS; vk::Buffer uniformBufferVS;
@ -105,8 +105,10 @@ public:
vkDestroyFence(device, compute.fence, nullptr); vkDestroyFence(device, compute.fence, nullptr);
vkDestroyCommandPool(device, compute.commandPool, nullptr); vkDestroyCommandPool(device, compute.commandPool, nullptr);
vkMeshLoader::freeMeshBufferResources(device, &meshes.quad); vertexBuffer.destroy();
indexBuffer.destroy();
uniformBufferVS.destroy(); uniformBufferVS.destroy();
textureLoader->destroyTexture(textureColorMap); textureLoader->destroyTexture(textureColorMap);
textureLoader->destroyTexture(textureComputeTarget); textureLoader->destroyTexture(textureComputeTarget);
} }
@ -262,14 +264,14 @@ public:
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor); vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
VkDeviceSize offsets[1] = { 0 }; VkDeviceSize offsets[1] = { 0 };
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &meshes.quad.vertices.buf, offsets); vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &vertexBuffer.buffer, offsets);
vkCmdBindIndexBuffer(drawCmdBuffers[i], meshes.quad.indices.buf, 0, VK_INDEX_TYPE_UINT32); vkCmdBindIndexBuffer(drawCmdBuffers[i], indexBuffer.buffer, 0, VK_INDEX_TYPE_UINT32);
// Left (pre compute) // Left (pre compute)
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphics.pipelineLayout, 0, 1, &graphics.descriptorSetPreCompute, 0, NULL); vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphics.pipelineLayout, 0, 1, &graphics.descriptorSetPreCompute, 0, NULL);
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphics.pipeline); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphics.pipeline);
vkCmdDrawIndexed(drawCmdBuffers[i], meshes.quad.indexCount, 1, 0, 0, 0); vkCmdDrawIndexed(drawCmdBuffers[i], indexCount, 1, 0, 0, 0);
// Right (post compute) // Right (post compute)
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphics.pipelineLayout, 0, 1, &graphics.descriptorSetPostCompute, 0, NULL); vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphics.pipelineLayout, 0, 1, &graphics.descriptorSetPostCompute, 0, NULL);
@ -277,7 +279,7 @@ public:
viewport.x = (float)width / 2.0f; viewport.x = (float)width / 2.0f;
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
vkCmdDrawIndexed(drawCmdBuffers[i], meshes.quad.indexCount, 1, 0, 0, 0); vkCmdDrawIndexed(drawCmdBuffers[i], indexCount, 1, 0, 0, 0);
vkCmdEndRenderPass(drawCmdBuffers[i]); vkCmdEndRenderPass(drawCmdBuffers[i]);
@ -306,33 +308,35 @@ public:
// Setup vertices for a single uv-mapped quad // Setup vertices for a single uv-mapped quad
void generateQuad() void generateQuad()
{ {
#define dim 1.0f // Setup vertices for a single uv-mapped quad made from two triangles
std::vector<Vertex> vertexBuffer = std::vector<Vertex> vertices =
{ {
{ { dim, dim, 0.0f }, { 1.0f, 1.0f } }, { { 1.0f, 1.0f, 0.0f }, { 1.0f, 1.0f } },
{ { -dim, dim, 0.0f }, { 0.0f, 1.0f } }, { { -1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f } },
{ { -dim, -dim, 0.0f }, { 0.0f, 0.0f } }, { { -1.0f, -1.0f, 0.0f }, { 0.0f, 0.0f } },
{ { dim, -dim, 0.0f }, { 1.0f, 0.0f } } { { 1.0f, -1.0f, 0.0f }, { 1.0f, 0.0f } }
}; };
#undef dim
createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
vertexBuffer.size() * sizeof(Vertex),
vertexBuffer.data(),
&meshes.quad.vertices.buf,
&meshes.quad.vertices.mem);
// Setup indices // Setup indices
std::vector<uint32_t> indexBuffer = { 0,1,2, 2,3,0 }; std::vector<uint32_t> indices = { 0,1,2, 2,3,0 };
meshes.quad.indexCount = indexBuffer.size(); indexCount = static_cast<uint32_t>(indices.size());
createBuffer( // Create buffers
// For the sake of simplicity we won't stage the vertex data to the gpu memory
// Vertex buffer
VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&vertexBuffer,
vertices.size() * sizeof(Vertex),
vertices.data()));
// Index buffer
VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
indexBuffer.size() * sizeof(uint32_t), VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
indexBuffer.data(), &indexBuffer,
&meshes.quad.indices.buf, indices.size() * sizeof(uint32_t),
&meshes.quad.indices.mem); indices.data()));
} }
void setupVertexDescriptions() void setupVertexDescriptions()
@ -354,14 +358,14 @@ public:
VERTEX_BUFFER_BIND_ID, VERTEX_BUFFER_BIND_ID,
0, 0,
VK_FORMAT_R32G32B32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT,
0); offsetof(Vertex, pos));
// Location 1 : Texture coordinates // Location 1 : Texture coordinates
vertices.attributeDescriptions[1] = vertices.attributeDescriptions[1] =
vkTools::initializers::vertexInputAttributeDescription( vkTools::initializers::vertexInputAttributeDescription(
VERTEX_BUFFER_BIND_ID, VERTEX_BUFFER_BIND_ID,
1, 1,
VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32_SFLOAT,
sizeof(float) * 3); offsetof(Vertex, uv));
// Assign to vertex buffer // Assign to vertex buffer
vertices.inputState = vkTools::initializers::pipelineVertexInputStateCreateInfo(); vertices.inputState = vkTools::initializers::pipelineVertexInputStateCreateInfo();

View file

@ -90,6 +90,13 @@
<ClInclude Include="..\base\vulkanexamplebase.h" /> <ClInclude Include="..\base\vulkanexamplebase.h" />
<ClInclude Include="..\base\vulkantools.h" /> <ClInclude Include="..\base\vulkantools.h" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="..\data\shaders\computeshader\edgedetect.comp" />
<None Include="..\data\shaders\computeshader\emboss.comp" />
<None Include="..\data\shaders\computeshader\sharpen.comp" />
<None Include="..\data\shaders\computeshader\texture.frag" />
<None Include="..\data\shaders\computeshader\texture.vert" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>

View file

@ -42,4 +42,21 @@
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="..\data\shaders\computeshader\edgedetect.comp">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\computeshader\emboss.comp">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\computeshader\sharpen.comp">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\computeshader\texture.frag">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\computeshader\texture.vert">
<Filter>Shaders</Filter>
</None>
</ItemGroup>
</Project> </Project>

View file

@ -3,15 +3,6 @@
#extension GL_ARB_separate_shader_objects : enable #extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable #extension GL_ARB_shading_language_420pack : enable
layout (location = 0) in vec3 inPos;
layout (location = 1) in vec2 inUV;
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 model;
} ubo;
layout (location = 0) out vec2 outUV; layout (location = 0) out vec2 outUV;
out gl_PerVertex out gl_PerVertex
@ -21,6 +12,6 @@ out gl_PerVertex
void main() void main()
{ {
outUV = inUV; outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2);
gl_Position = ubo.projection * ubo.model * vec4(inPos.xyz, 1.0); gl_Position = vec4(outUV * 2.0f - 1.0f, 0.0f, 1.0f);
} }

View file

@ -11,6 +11,8 @@ layout (location = 2) in vec3 inViewVec;
layout (location = 3) in vec3 inLightVec; layout (location = 3) in vec3 inLightVec;
layout (location = 4) in vec4 inShadowCoord; layout (location = 4) in vec4 inShadowCoord;
layout (constant_id = 0) const int enablePCF = 0;
layout (location = 0) out vec4 outFragColor; layout (location = 0) out vec4 outFragColor;
#define ambient 0.1 #define ambient 0.1
@ -55,7 +57,7 @@ float filterPCF(vec4 sc)
void main() void main()
{ {
float shadow = filterPCF(inShadowCoord / inShadowCoord.w); float shadow = (enablePCF == 1) ? filterPCF(inShadowCoord / inShadowCoord.w) : textureProj(inShadowCoord / inShadowCoord.w, vec2(0.0));
vec3 N = normalize(inNormal); vec3 N = normalize(inNormal);
vec3 L = normalize(inLightVec); vec3 L = normalize(inLightVec);

View file

@ -574,39 +574,37 @@ public:
// Create staging buffers // Create staging buffers
// Vertex data // Vertex data
createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
vertexBufferSize, vertexBufferSize,
vertexBuffer.data(),
&vertexStaging.buffer, &vertexStaging.buffer,
&vertexStaging.memory); &vertexStaging.memory,
vertexBuffer.data()));
// Index data // Index data
createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
indexBufferSize, indexBufferSize,
indexBuffer.data(),
&indexStaging.buffer, &indexStaging.buffer,
&indexStaging.memory); &indexStaging.memory,
indexBuffer.data()));
// Create device local buffers // Create device local buffers
// Vertex buffer // Vertex buffer
createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
vertexBufferSize, vertexBufferSize,
nullptr,
&scene->vertices.buf, &scene->vertices.buf,
&scene->vertices.mem); &scene->vertices.mem));
// Index buffer // Index buffer
createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
indexBufferSize, indexBufferSize,
nullptr,
&scene->indices.buf, &scene->indices.buf,
&scene->indices.mem); &scene->indices.mem));
// Copy from staging buffers // Copy from staging buffers
VkCommandBuffer copyCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); VkCommandBuffer copyCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
@ -639,21 +637,21 @@ public:
else else
{ {
// Vertex buffer // Vertex buffer
createBuffer( vulkanDevice->createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
vertexBufferSize, vertexBufferSize,
vertexBuffer.data(),
&scene->vertices.buf, &scene->vertices.buf,
&scene->vertices.mem); &scene->vertices.mem,
vertexBuffer.data());
// Index buffer // Index buffer
createBuffer( vulkanDevice->createBuffer(
VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
indexBufferSize, indexBufferSize,
indexBuffer.data(),
&scene->indices.buf, &scene->indices.buf,
&scene->indices.mem); &scene->indices.mem,
indexBuffer.data());
} }
delete(meshLoader); delete(meshLoader);

View file

@ -519,12 +519,13 @@ public:
vertexBuffer.push_back({ { 0.0f, 0.0f, 0.0f },{ 0.0f, 0.0f },{ 1.0f, 1.0f, 1.0f },{ 0.0f, 0.0f, 0.0f } }); vertexBuffer.push_back({ { 0.0f, 0.0f, 0.0f },{ 0.0f, 0.0f },{ 1.0f, 1.0f, 1.0f },{ 0.0f, 0.0f, 0.0f } });
vertexBuffer.push_back({ { 1.0f, 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f, 1.0f },{ 0.0f, 0.0f, 0.0f } }); vertexBuffer.push_back({ { 1.0f, 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f, 1.0f },{ 0.0f, 0.0f, 0.0f } });
createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
vertexBuffer.size() * sizeof(Vertex), vertexBuffer.size() * sizeof(Vertex),
vertexBuffer.data(),
&meshes.quad.vertices.buf, &meshes.quad.vertices.buf,
&meshes.quad.vertices.mem); &meshes.quad.vertices.mem,
vertexBuffer.data()));
// Setup indices // Setup indices
std::vector<uint32_t> indexBuffer = { 0,1,2, 2,3,0 }; std::vector<uint32_t> indexBuffer = { 0,1,2, 2,3,0 };
@ -538,12 +539,13 @@ public:
} }
meshes.quad.indexCount = static_cast<uint32_t>(indexBuffer.size()); meshes.quad.indexCount = static_cast<uint32_t>(indexBuffer.size());
createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
indexBuffer.size() * sizeof(uint32_t), indexBuffer.size() * sizeof(uint32_t),
indexBuffer.data(),
&meshes.quad.indices.buf, &meshes.quad.indices.buf,
&meshes.quad.indices.mem); &meshes.quad.indices.mem,
indexBuffer.data()));
} }
void setupVertexDescriptions() void setupVertexDescriptions()

View file

@ -70,18 +70,14 @@ public:
} textures; } textures;
struct { struct {
VkBuffer buf;
VkDeviceMemory mem;
VkPipelineVertexInputStateCreateInfo inputState; VkPipelineVertexInputStateCreateInfo inputState;
std::vector<VkVertexInputBindingDescription> bindingDescriptions; std::vector<VkVertexInputBindingDescription> bindingDescriptions;
std::vector<VkVertexInputAttributeDescription> attributeDescriptions; std::vector<VkVertexInputAttributeDescription> attributeDescriptions;
} vertices; } vertices;
struct { vk::Buffer vertexBuffer;
int count; vk::Buffer indexBuffer;
VkBuffer buf; uint32_t indexCount;
VkDeviceMemory mem;
} indices;
struct { struct {
vk::Buffer vs; vk::Buffer vs;
@ -129,15 +125,13 @@ public:
textureLoader->destroyTexture(textures.fontBitmap); textureLoader->destroyTexture(textures.fontBitmap);
vkDestroyPipeline(device, pipelines.sdf, nullptr); vkDestroyPipeline(device, pipelines.sdf, nullptr);
vkDestroyPipeline(device, pipelines.bitmap, nullptr);
vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
vkDestroyBuffer(device, vertices.buf, nullptr); vertexBuffer.destroy();
vkFreeMemory(device, vertices.mem, nullptr); indexBuffer.destroy();
vkDestroyBuffer(device, indices.buf, nullptr);
vkFreeMemory(device, indices.mem, nullptr);
uniformBuffers.vs.destroy(); uniformBuffers.vs.destroy();
uniformBuffers.fs.destroy(); uniformBuffers.fs.destroy();
@ -260,9 +254,9 @@ public:
// Signed distance field font // Signed distance field font
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.sdf, 0, NULL); vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.sdf, 0, NULL);
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.sdf); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.sdf);
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &vertices.buf, offsets); vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &vertexBuffer.buffer, offsets);
vkCmdBindIndexBuffer(drawCmdBuffers[i], indices.buf, 0, VK_INDEX_TYPE_UINT32); vkCmdBindIndexBuffer(drawCmdBuffers[i], indexBuffer.buffer, 0, VK_INDEX_TYPE_UINT32);
vkCmdDrawIndexed(drawCmdBuffers[i], indices.count, 1, 0, 0, 0); vkCmdDrawIndexed(drawCmdBuffers[i], indexCount, 1, 0, 0, 0);
// Linear filtered bitmap font // Linear filtered bitmap font
if (splitScreen) if (splitScreen)
@ -271,9 +265,7 @@ public:
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.bitmap, 0, NULL); vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.bitmap, 0, NULL);
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.bitmap); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.bitmap);
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &vertices.buf, offsets); vkCmdDrawIndexed(drawCmdBuffers[i], indexCount, 1, 0, 0, 0);
vkCmdBindIndexBuffer(drawCmdBuffers[i], indices.buf, 0, VK_INDEX_TYPE_UINT32);
vkCmdDrawIndexed(drawCmdBuffers[i], indices.count, 1, 0, 0, 0);
} }
vkCmdEndRenderPass(drawCmdBuffers[i]); vkCmdEndRenderPass(drawCmdBuffers[i]);
@ -285,8 +277,8 @@ public:
// Creates a vertex buffer containing quads for the passed text // Creates a vertex buffer containing quads for the passed text
void generateText(std:: string text) void generateText(std:: string text)
{ {
std::vector<Vertex> vertexBuffer; std::vector<Vertex> vertices;
std::vector<uint32_t> indexBuffer; std::vector<uint32_t> indices;
uint32_t indexOffset = 0; uint32_t indexOffset = 0;
float w = textures.fontSDF.width; float w = textures.fontSDF.width;
@ -315,43 +307,45 @@ public:
float xo = charInfo->xoffset / 36.0f; float xo = charInfo->xoffset / 36.0f;
float yo = charInfo->yoffset / 36.0f; float yo = charInfo->yoffset / 36.0f;
vertexBuffer.push_back({ { posx + dimx + xo, posy + dimy, 0.0f }, { ue, te } }); vertices.push_back({ { posx + dimx + xo, posy + dimy, 0.0f }, { ue, te } });
vertexBuffer.push_back({ { posx + xo, posy + dimy, 0.0f }, { us, te } }); vertices.push_back({ { posx + xo, posy + dimy, 0.0f }, { us, te } });
vertexBuffer.push_back({ { posx + xo, posy, 0.0f }, { us, ts } }); vertices.push_back({ { posx + xo, posy, 0.0f }, { us, ts } });
vertexBuffer.push_back({ { posx + dimx + xo, posy, 0.0f }, { ue, ts } }); vertices.push_back({ { posx + dimx + xo, posy, 0.0f }, { ue, ts } });
std::array<uint32_t, 6> indices = { 0,1,2, 2,3,0 }; std::array<uint32_t, 6> letterIndices = { 0,1,2, 2,3,0 };
for (auto& index : indices) for (auto& index : letterIndices)
{ {
indexBuffer.push_back(indexOffset + index); indices.push_back(indexOffset + index);
} }
indexOffset += 4; indexOffset += 4;
float advance = ((float)(charInfo->xadvance) / 36.0f); float advance = ((float)(charInfo->xadvance) / 36.0f);
posx += advance; posx += advance;
} }
indices.count = indexBuffer.size(); indexCount = indices.size();
// Center // Center
for (auto& v : vertexBuffer) for (auto& v : vertices)
{ {
v.pos[0] -= posx / 2.0f; v.pos[0] -= posx / 2.0f;
v.pos[1] -= 0.5f; v.pos[1] -= 0.5f;
} }
createBuffer( // Generate device local buffers for the text vertices and indices and upload the data
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
vertexBuffer.size() * sizeof(Vertex),
vertexBuffer.data(),
&vertices.buf,
&vertices.mem);
createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&vertexBuffer,
vertices.size() * sizeof(Vertex),
vertices.data()));
VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
indexBuffer.size() * sizeof(uint32_t), VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
indexBuffer.data(), &indexBuffer,
&indices.buf, indices.size() * sizeof(uint32_t),
&indices.mem); indices.data()));
} }
void setupVertexDescriptions() void setupVertexDescriptions()

View file

@ -460,21 +460,20 @@ public:
VkBuffer buffer; VkBuffer buffer;
} stagingBuffer; } stagingBuffer;
VulkanExampleBase::createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
instanceBuffer.size, instanceBuffer.size,
instanceData.data(),
&stagingBuffer.buffer, &stagingBuffer.buffer,
&stagingBuffer.memory); &stagingBuffer.memory,
instanceData.data()));
VulkanExampleBase::createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
instanceBuffer.size, instanceBuffer.size,
nullptr,
&instanceBuffer.buffer, &instanceBuffer.buffer,
&instanceBuffer.memory); &instanceBuffer.memory));
// Copy to staging buffer // Copy to staging buffer
VkCommandBuffer copyCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); VkCommandBuffer copyCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);

View file

@ -234,39 +234,37 @@ public:
// Create staging buffers // Create staging buffers
// Vertex data // Vertex data
createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
vertexBufferSize, vertexBufferSize,
vertexBuffer.data(),
&vertexStaging.buffer, &vertexStaging.buffer,
&vertexStaging.memory); &vertexStaging.memory,
vertexBuffer.data()));
// Index data // Index data
createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
indexBufferSize, indexBufferSize,
indexBuffer.data(),
&indexStaging.buffer, &indexStaging.buffer,
&indexStaging.memory); &indexStaging.memory,
indexBuffer.data()));
// Create device local buffers // Create device local buffers
// Vertex buffer // Vertex buffer
createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
vertexBufferSize, vertexBufferSize,
nullptr,
&mesh.vertices.buf, &mesh.vertices.buf,
&mesh.vertices.mem); &mesh.vertices.mem));
// Index buffer // Index buffer
createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
indexBufferSize, indexBufferSize,
nullptr,
&mesh.indices.buf, &mesh.indices.buf,
&mesh.indices.mem); &mesh.indices.mem));
// Copy from staging buffers // Copy from staging buffers
VkCommandBuffer copyCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); VkCommandBuffer copyCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
@ -299,21 +297,21 @@ public:
else else
{ {
// Vertex buffer // Vertex buffer
createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
vertexBufferSize, vertexBufferSize,
vertexBuffer.data(),
&mesh.vertices.buf, &mesh.vertices.buf,
&mesh.vertices.mem); &mesh.vertices.mem,
vertexBuffer.data()));
// Index buffer // Index buffer
createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
indexBufferSize, indexBufferSize,
indexBuffer.data(),
&mesh.indices.buf, &mesh.indices.buf,
&mesh.indices.mem); &mesh.indices.mem,
indexBuffer.data()));
} }
delete(meshLoader); delete(meshLoader);

View file

@ -502,23 +502,25 @@ public:
}; };
#undef QUAD_COLOR_NORMAL #undef QUAD_COLOR_NORMAL
createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
vertexBuffer.size() * sizeof(Vertex), vertexBuffer.size() * sizeof(Vertex),
vertexBuffer.data(),
&meshes.quad.vertices.buf, &meshes.quad.vertices.buf,
&meshes.quad.vertices.mem); &meshes.quad.vertices.mem,
vertexBuffer.data()));
// Setup indices // Setup indices
std::vector<uint32_t> indexBuffer = { 0,1,2, 2,3,0 }; std::vector<uint32_t> indexBuffer = { 0,1,2, 2,3,0 };
meshes.quad.indexCount = indexBuffer.size(); meshes.quad.indexCount = indexBuffer.size();
createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
indexBuffer.size() * sizeof(uint32_t), indexBuffer.size() * sizeof(uint32_t),
indexBuffer.data(),
&meshes.quad.indices.buf, &meshes.quad.indices.buf,
&meshes.quad.indices.mem); &meshes.quad.indices.mem,
indexBuffer.data()));
} }
void setupVertexDescriptions() void setupVertexDescriptions()

View file

@ -278,12 +278,13 @@ public:
particles.size = particleBuffer.size() * sizeof(Particle); particles.size = particleBuffer.size() * sizeof(Particle);
createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
particles.size, particles.size,
particleBuffer.data(),
&particles.buffer, &particles.buffer,
&particles.memory); &particles.memory,
particleBuffer.data()));
// Map the memory and store the pointer for reuse // Map the memory and store the pointer for reuse
VK_CHECK_RESULT(vkMapMemory(device, particles.memory, 0, particles.size, 0, &particles.mappedMemory)); VK_CHECK_RESULT(vkMapMemory(device, particles.memory, 0, particles.size, 0, &particles.mappedMemory));

View file

@ -552,98 +552,72 @@ public:
VkDeviceSize indexBufferSize = indexBuffer.size() * sizeof(uint32_t); VkDeviceSize indexBufferSize = indexBuffer.size() * sizeof(uint32_t);
skinnedMesh->meshBuffer.indexCount = indexBuffer.size(); skinnedMesh->meshBuffer.indexCount = indexBuffer.size();
bool useStaging = true; struct {
VkBuffer buffer;
VkDeviceMemory memory;
} vertexStaging, indexStaging;
if (useStaging) // Create staging buffers
{ // Vertex data
struct { VK_CHECK_RESULT(vulkanDevice->createBuffer(
VkBuffer buffer; VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VkDeviceMemory memory; VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
} vertexStaging, indexStaging; vertexBufferSize,
&vertexStaging.buffer,
&vertexStaging.memory,
vertexBuffer.data()));
// Index data
VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
indexBufferSize,
&indexStaging.buffer,
&indexStaging.memory,
indexBuffer.data()));
// Create staging buffers // Create device local buffers
// Vertex data // Vertex buffer
createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
vertexBufferSize, vertexBufferSize,
vertexBuffer.data(), &skinnedMesh->meshBuffer.vertices.buf,
&vertexStaging.buffer, &skinnedMesh->meshBuffer.vertices.mem));
&vertexStaging.memory); // Index buffer
// Index data VK_CHECK_RESULT(vulkanDevice->createBuffer(
createBuffer( VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, indexBufferSize,
indexBufferSize, &skinnedMesh->meshBuffer.indices.buf,
indexBuffer.data(), &skinnedMesh->meshBuffer.indices.mem));
&indexStaging.buffer,
&indexStaging.memory);
// Create device local buffers // Copy from staging buffers
// Vertex buffer VkCommandBuffer copyCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
vertexBufferSize,
nullptr,
&skinnedMesh->meshBuffer.vertices.buf,
&skinnedMesh->meshBuffer.vertices.mem);
// Index buffer
createBuffer(
VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
indexBufferSize,
nullptr,
&skinnedMesh->meshBuffer.indices.buf,
&skinnedMesh->meshBuffer.indices.mem);
// Copy from staging buffers VkBufferCopy copyRegion = {};
VkCommandBuffer copyCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
VkBufferCopy copyRegion = {}; copyRegion.size = vertexBufferSize;
vkCmdCopyBuffer(
copyCmd,
vertexStaging.buffer,
skinnedMesh->meshBuffer.vertices.buf,
1,
&copyRegion);
copyRegion.size = vertexBufferSize; copyRegion.size = indexBufferSize;
vkCmdCopyBuffer( vkCmdCopyBuffer(
copyCmd, copyCmd,
vertexStaging.buffer, indexStaging.buffer,
skinnedMesh->meshBuffer.vertices.buf, skinnedMesh->meshBuffer.indices.buf,
1, 1,
&copyRegion); &copyRegion);
copyRegion.size = indexBufferSize; VulkanExampleBase::flushCommandBuffer(copyCmd, queue, true);
vkCmdCopyBuffer(
copyCmd,
indexStaging.buffer,
skinnedMesh->meshBuffer.indices.buf,
1,
&copyRegion);
VulkanExampleBase::flushCommandBuffer(copyCmd, queue, true); vkDestroyBuffer(device, vertexStaging.buffer, nullptr);
vkFreeMemory(device, vertexStaging.memory, nullptr);
vkDestroyBuffer(device, vertexStaging.buffer, nullptr); vkDestroyBuffer(device, indexStaging.buffer, nullptr);
vkFreeMemory(device, vertexStaging.memory, nullptr); vkFreeMemory(device, indexStaging.memory, nullptr);
vkDestroyBuffer(device, indexStaging.buffer, nullptr);
vkFreeMemory(device, indexStaging.memory, nullptr);
}
else
{
// Vertex buffer
createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
vertexBufferSize,
vertexBuffer.data(),
&skinnedMesh->meshBuffer.vertices.buf,
&skinnedMesh->meshBuffer.vertices.mem);
// Index buffer
createBuffer(
VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
indexBufferSize,
indexBuffer.data(),
&skinnedMesh->meshBuffer.indices.buf,
&skinnedMesh->meshBuffer.indices.mem);
}
} }
void loadAssets() void loadAssets()