Refactoring, use Vulkan result check macro
This commit is contained in:
parent
29d726482e
commit
92c04aad10
1 changed files with 60 additions and 80 deletions
|
|
@ -113,8 +113,8 @@ public:
|
||||||
|
|
||||||
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
|
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
|
||||||
{
|
{
|
||||||
zoom = -1.5f;
|
zoom = -2.0f;
|
||||||
rotation = { 0.0f, 0.0f, 0.0f };
|
enableTextOverlay = true;
|
||||||
title = "Vulkan Example - Distance field fonts";
|
title = "Vulkan Example - Distance field fonts";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -240,30 +240,18 @@ public:
|
||||||
renderPassBeginInfo.clearValueCount = 2;
|
renderPassBeginInfo.clearValueCount = 2;
|
||||||
renderPassBeginInfo.pClearValues = clearValues;
|
renderPassBeginInfo.pClearValues = clearValues;
|
||||||
|
|
||||||
VkResult err;
|
|
||||||
|
|
||||||
for (int32_t i = 0; i < drawCmdBuffers.size(); ++i)
|
for (int32_t i = 0; i < drawCmdBuffers.size(); ++i)
|
||||||
{
|
{
|
||||||
// Set target frame buffer
|
|
||||||
renderPassBeginInfo.framebuffer = frameBuffers[i];
|
renderPassBeginInfo.framebuffer = frameBuffers[i];
|
||||||
|
|
||||||
err = vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo);
|
VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));
|
||||||
assert(!err);
|
|
||||||
|
|
||||||
vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
||||||
|
|
||||||
VkViewport viewport = vkTools::initializers::viewport(
|
VkViewport viewport = vkTools::initializers::viewport((float)width, (splitScreen) ? (float)height / 2.0f : (float)height, 0.0f, 1.0f);
|
||||||
(float)width,
|
|
||||||
(splitScreen) ? (float)height / 2.0f : (float)height,
|
|
||||||
0.0f,
|
|
||||||
1.0f);
|
|
||||||
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
|
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
|
||||||
|
|
||||||
VkRect2D scissor = vkTools::initializers::rect2D(
|
VkRect2D scissor = vkTools::initializers::rect2D(width, height, 0, 0);
|
||||||
width,
|
|
||||||
height,
|
|
||||||
0,
|
|
||||||
0);
|
|
||||||
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
|
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
|
||||||
|
|
||||||
VkDeviceSize offsets[1] = { 0 };
|
VkDeviceSize offsets[1] = { 0 };
|
||||||
|
|
@ -289,40 +277,10 @@ public:
|
||||||
|
|
||||||
vkCmdEndRenderPass(drawCmdBuffers[i]);
|
vkCmdEndRenderPass(drawCmdBuffers[i]);
|
||||||
|
|
||||||
err = vkEndCommandBuffer(drawCmdBuffers[i]);
|
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
|
||||||
assert(!err);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw()
|
|
||||||
{
|
|
||||||
VkResult err;
|
|
||||||
|
|
||||||
// Get next image in the swap chain (back/front buffer)
|
|
||||||
err = swapChain.acquireNextImage(semaphores.presentComplete, ¤tBuffer);
|
|
||||||
assert(!err);
|
|
||||||
|
|
||||||
submitPostPresentBarrier(swapChain.buffers[currentBuffer].image);
|
|
||||||
|
|
||||||
// Command buffer to be sumitted to the queue
|
|
||||||
submitInfo.commandBufferCount = 1;
|
|
||||||
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
|
||||||
|
|
||||||
// Submit to queue
|
|
||||||
err = vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE);
|
|
||||||
assert(!err);
|
|
||||||
|
|
||||||
submitPrePresentBarrier(swapChain.buffers[currentBuffer].image);
|
|
||||||
|
|
||||||
err = swapChain.queuePresent(queue, currentBuffer, semaphores.renderComplete);
|
|
||||||
assert(!err);
|
|
||||||
|
|
||||||
err = vkQueueWaitIdle(queue);
|
|
||||||
assert(!err);
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo : function fill buffer with quads from font
|
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
|
|
@ -474,16 +432,14 @@ public:
|
||||||
setLayoutBindings.data(),
|
setLayoutBindings.data(),
|
||||||
setLayoutBindings.size());
|
setLayoutBindings.size());
|
||||||
|
|
||||||
VkResult err = vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout);
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
|
||||||
assert(!err);
|
|
||||||
|
|
||||||
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
|
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
|
||||||
vkTools::initializers::pipelineLayoutCreateInfo(
|
vkTools::initializers::pipelineLayoutCreateInfo(
|
||||||
&descriptorSetLayout,
|
&descriptorSetLayout,
|
||||||
1);
|
1);
|
||||||
|
|
||||||
err = vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout);
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
|
||||||
assert(!err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupDescriptorSet()
|
void setupDescriptorSet()
|
||||||
|
|
@ -495,8 +451,7 @@ public:
|
||||||
1);
|
1);
|
||||||
|
|
||||||
// Signed distance front descriptor set
|
// Signed distance front descriptor set
|
||||||
VkResult vkRes = vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.sdf);
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.sdf));
|
||||||
assert(!vkRes);
|
|
||||||
|
|
||||||
// Image descriptor for the color map texture
|
// Image descriptor for the color map texture
|
||||||
VkDescriptorImageInfo texDescriptor =
|
VkDescriptorImageInfo texDescriptor =
|
||||||
|
|
@ -530,8 +485,7 @@ public:
|
||||||
vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL);
|
vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL);
|
||||||
|
|
||||||
// Default font rendering descriptor set
|
// Default font rendering descriptor set
|
||||||
vkRes = vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.bitmap);
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.bitmap));
|
||||||
assert(!vkRes);
|
|
||||||
|
|
||||||
// Image descriptor for the color map texture
|
// Image descriptor for the color map texture
|
||||||
texDescriptor.sampler = textures.fontBitmap.sampler;
|
texDescriptor.sampler = textures.fontBitmap.sampler;
|
||||||
|
|
@ -637,14 +591,12 @@ public:
|
||||||
pipelineCreateInfo.stageCount = shaderStages.size();
|
pipelineCreateInfo.stageCount = shaderStages.size();
|
||||||
pipelineCreateInfo.pStages = shaderStages.data();
|
pipelineCreateInfo.pStages = shaderStages.data();
|
||||||
|
|
||||||
VkResult err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.sdf);
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.sdf));
|
||||||
assert(!err);
|
|
||||||
|
|
||||||
// Default bitmap font rendering pipeline
|
// Default bitmap font rendering pipeline
|
||||||
shaderStages[0] = loadShader(getAssetPath() + "shaders/distancefieldfonts/bitmap.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
shaderStages[0] = loadShader(getAssetPath() + "shaders/distancefieldfonts/bitmap.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
||||||
shaderStages[1] = loadShader(getAssetPath() + "shaders/distancefieldfonts/bitmap.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
shaderStages[1] = loadShader(getAssetPath() + "shaders/distancefieldfonts/bitmap.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
||||||
err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.bitmap);
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.bitmap));
|
||||||
assert(!err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare and initialize uniform buffer containing shader uniforms
|
// Prepare and initialize uniform buffer containing shader uniforms
|
||||||
|
|
@ -653,8 +605,9 @@ public:
|
||||||
// Vertex shader uniform buffer block
|
// Vertex shader uniform buffer block
|
||||||
createBuffer(
|
createBuffer(
|
||||||
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
||||||
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||||
sizeof(uboVS),
|
sizeof(uboVS),
|
||||||
&uboVS,
|
nullptr,
|
||||||
&uniformData.vs.buffer,
|
&uniformData.vs.buffer,
|
||||||
&uniformData.vs.memory,
|
&uniformData.vs.memory,
|
||||||
&uniformData.vs.descriptor);
|
&uniformData.vs.descriptor);
|
||||||
|
|
@ -663,8 +616,9 @@ public:
|
||||||
// Contains font rendering parameters
|
// Contains font rendering parameters
|
||||||
createBuffer(
|
createBuffer(
|
||||||
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
||||||
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||||
sizeof(uboFS),
|
sizeof(uboFS),
|
||||||
&uboFS,
|
nullptr,
|
||||||
&uniformData.fs.buffer,
|
&uniformData.fs.buffer,
|
||||||
&uniformData.fs.memory,
|
&uniformData.fs.memory,
|
||||||
&uniformData.fs.descriptor);
|
&uniformData.fs.descriptor);
|
||||||
|
|
@ -677,18 +631,17 @@ public:
|
||||||
{
|
{
|
||||||
// Vertex shader
|
// Vertex shader
|
||||||
glm::mat4 viewMatrix = glm::mat4();
|
glm::mat4 viewMatrix = glm::mat4();
|
||||||
uboVS.projection = glm::perspective(glm::radians(splitScreen ? 45.0f : 45.0f), (float)width / (float)(height * ((splitScreen) ? 0.5f : 1.0f)), 0.001f, 256.0f);
|
uboVS.projection = glm::perspective(glm::radians(splitScreen ? 30.0f : 45.0f), (float)width / (float)(height * ((splitScreen) ? 0.5f : 1.0f)), 0.001f, 256.0f);
|
||||||
viewMatrix = glm::translate(viewMatrix, glm::vec3(0.0f, 0.0f, splitScreen ? zoom : zoom - 2.0f));
|
viewMatrix = glm::translate(viewMatrix, glm::vec3(0.0f, 0.0f, splitScreen ? zoom : zoom - 2.0f));
|
||||||
|
|
||||||
uboVS.model = glm::mat4();
|
uboVS.model = glm::mat4();
|
||||||
uboVS.model = viewMatrix * glm::translate(uboVS.model, glm::vec3(0, 0, 0));
|
uboVS.model = viewMatrix * glm::translate(uboVS.model, cameraPos);
|
||||||
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
|
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
|
||||||
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
|
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||||
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
|
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
|
||||||
|
|
||||||
uint8_t *pData;
|
uint8_t *pData;
|
||||||
VkResult err = vkMapMemory(device, uniformData.vs.memory, 0, sizeof(uboVS), 0, (void **)&pData);
|
VK_CHECK_RESULT(vkMapMemory(device, uniformData.vs.memory, 0, sizeof(uboVS), 0, (void **)&pData));
|
||||||
assert(!err);
|
|
||||||
memcpy(pData, &uboVS, sizeof(uboVS));
|
memcpy(pData, &uboVS, sizeof(uboVS));
|
||||||
vkUnmapMemory(device, uniformData.vs.memory);
|
vkUnmapMemory(device, uniformData.vs.memory);
|
||||||
}
|
}
|
||||||
|
|
@ -697,12 +650,25 @@ public:
|
||||||
{
|
{
|
||||||
// Fragment shader
|
// Fragment shader
|
||||||
uint8_t *pData;
|
uint8_t *pData;
|
||||||
VkResult err = vkMapMemory(device, uniformData.fs.memory, 0, sizeof(uboFS), 0, (void **)&pData);
|
VK_CHECK_RESULT(vkMapMemory(device, uniformData.fs.memory, 0, sizeof(uboFS), 0, (void **)&pData));
|
||||||
assert(!err);
|
|
||||||
memcpy(pData, &uboFS, sizeof(uboFS));
|
memcpy(pData, &uboFS, sizeof(uboFS));
|
||||||
vkUnmapMemory(device, uniformData.fs.memory);
|
vkUnmapMemory(device, uniformData.fs.memory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void draw()
|
||||||
|
{
|
||||||
|
VulkanExampleBase::prepareFrame();
|
||||||
|
|
||||||
|
// Command buffer to be sumitted to the queue
|
||||||
|
submitInfo.commandBufferCount = 1;
|
||||||
|
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
||||||
|
|
||||||
|
// Submit to queue
|
||||||
|
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
||||||
|
|
||||||
|
VulkanExampleBase::submitFrame();
|
||||||
|
}
|
||||||
|
|
||||||
void prepare()
|
void prepare()
|
||||||
{
|
{
|
||||||
VulkanExampleBase::prepare();
|
VulkanExampleBase::prepare();
|
||||||
|
|
@ -746,6 +712,32 @@ public:
|
||||||
updateFontSettings();
|
updateFontSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual void keyPressed(uint32_t keyCode)
|
||||||
|
{
|
||||||
|
switch (keyCode)
|
||||||
|
{
|
||||||
|
case 0x53:
|
||||||
|
case GAMEPAD_BUTTON_X:
|
||||||
|
toggleSplitScreen();
|
||||||
|
break;
|
||||||
|
case 0x4F:
|
||||||
|
case GAMEPAD_BUTTON_A:
|
||||||
|
toggleFontOutline();
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void getOverlayText(VulkanTextOverlay *textOverlay)
|
||||||
|
{
|
||||||
|
#if defined(__ANDROID__)
|
||||||
|
textOverlay->addText("Press \"Button A\" to toggle outline", 5.0f, 85.0f, VulkanTextOverlay::alignLeft);
|
||||||
|
textOverlay->addText("Press \"Button A\" to toggle splitscreen", 5.0f, 100.0f, VulkanTextOverlay::alignLeft);
|
||||||
|
#else
|
||||||
|
textOverlay->addText("Press \"o\" to toggle outline", 5.0f, 85.0f, VulkanTextOverlay::alignLeft);
|
||||||
|
textOverlay->addText("Press \"s\" to toggle splitscreen", 5.0f, 100.0f, VulkanTextOverlay::alignLeft);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
VulkanExample *vulkanExample;
|
VulkanExample *vulkanExample;
|
||||||
|
|
@ -756,18 +748,6 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||||
if (vulkanExample != NULL)
|
if (vulkanExample != NULL)
|
||||||
{
|
{
|
||||||
vulkanExample->handleMessages(hWnd, uMsg, wParam, lParam);
|
vulkanExample->handleMessages(hWnd, uMsg, wParam, lParam);
|
||||||
if (uMsg == WM_KEYDOWN)
|
|
||||||
{
|
|
||||||
switch (wParam)
|
|
||||||
{
|
|
||||||
case 0x53:
|
|
||||||
vulkanExample->toggleSplitScreen();
|
|
||||||
break;
|
|
||||||
case 0x4F:
|
|
||||||
vulkanExample->toggleFontOutline();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return (DefWindowProc(hWnd, uMsg, wParam, lParam));
|
return (DefWindowProc(hWnd, uMsg, wParam, lParam));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue