1228 lines
No EOL
51 KiB
C++
1228 lines
No EOL
51 KiB
C++
/*
|
||
* Vulkan Example - Basic indexed triangle rendering
|
||
*
|
||
* Note :
|
||
* This is a "pedal to the metal" example to show off how to get Vulkan up an displaying something
|
||
* Contrary to the other examples, this one won't make use of helper functions or initializers
|
||
* Except in a few cases (swap chain setup e.g.)
|
||
*
|
||
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
|
||
*
|
||
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
|
||
*/
|
||
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <string.h>
|
||
#include <assert.h>
|
||
#include <vector>
|
||
#include <exception>
|
||
|
||
#define GLM_FORCE_RADIANS
|
||
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
||
#include <glm/glm.hpp>
|
||
#include <glm/gtc/matrix_transform.hpp>
|
||
|
||
#include <vulkan/vulkan.h>
|
||
#include "vulkanexamplebase.h"
|
||
|
||
#define VERTEX_BUFFER_BIND_ID 0
|
||
// Set to "true" to enable Vulkan's validation layers
|
||
// See vulkandebug.cpp for details
|
||
#define ENABLE_VALIDATION false
|
||
// Set to "true" to use staging buffers for uploading
|
||
// vertex and index data to device local memory
|
||
// See "prepareVertices" for details on what's staging
|
||
// and on why to use it
|
||
#define USE_STAGING true
|
||
|
||
class VulkanExample : public VulkanExampleBase
|
||
{
|
||
public:
|
||
struct {
|
||
VkBuffer buf;
|
||
VkDeviceMemory mem;
|
||
VkPipelineVertexInputStateCreateInfo inputState;
|
||
std::vector<VkVertexInputBindingDescription> bindingDescriptions;
|
||
std::vector<VkVertexInputAttributeDescription> attributeDescriptions;
|
||
} vertices;
|
||
|
||
struct {
|
||
int count;
|
||
VkBuffer buf;
|
||
VkDeviceMemory mem;
|
||
} indices;
|
||
|
||
struct {
|
||
VkBuffer buffer;
|
||
VkDeviceMemory memory;
|
||
VkDescriptorBufferInfo descriptor;
|
||
} uniformDataVS;
|
||
|
||
// For simplicity we use the same uniform block layout as in the shader:
|
||
//
|
||
// layout(set = 0, binding = 0) uniform UBO
|
||
// {
|
||
// mat4 projectionMatrix;
|
||
// mat4 modelMatrix;
|
||
// mat4 viewMatrix;
|
||
// } ubo;
|
||
//
|
||
// This way we can just memcopy the ubo data to the ubo
|
||
// Note that you should be using data types that align with the GPU
|
||
// in order to avoid manual padding
|
||
struct {
|
||
glm::mat4 projectionMatrix;
|
||
glm::mat4 modelMatrix;
|
||
glm::mat4 viewMatrix;
|
||
} uboVS;
|
||
|
||
// The pipeline (state objects) is a static store for the 3D pipeline states (including shaders)
|
||
// Other than OpenGL this makes you setup the render states up-front
|
||
// If different render states are required you need to setup multiple pipelines
|
||
// and switch between them
|
||
// Note that there are a few dynamic states (scissor, viewport, line width) that
|
||
// can be set from a command buffer and does not have to be part of the pipeline
|
||
// This basic example only uses one pipeline
|
||
VkPipeline pipeline;
|
||
|
||
// The pipeline layout defines the resource binding slots to be used with a pipeline
|
||
// This includes bindings for buffes (ubos, ssbos), images and sampler
|
||
// A pipeline layout can be used for multiple pipeline (state objects) as long as
|
||
// their shaders use the same binding layout
|
||
VkPipelineLayout pipelineLayout;
|
||
|
||
// The descriptor set stores the resources bound to the binding points in a shader
|
||
// It connects the binding points of the different shaders with the buffers and images
|
||
// used for those bindings
|
||
VkDescriptorSet descriptorSet;
|
||
|
||
// The descriptor set layout describes the shader binding points without referencing
|
||
// the actual buffers.
|
||
// Like the pipeline layout it's pretty much a blueprint and can be used with
|
||
// different descriptor sets as long as the binding points (and shaders) match
|
||
VkDescriptorSetLayout descriptorSetLayout;
|
||
|
||
// Synchronization semaphores
|
||
// Semaphores are used to synchronize dependencies between command buffers
|
||
// We use them to ensure that we e.g. don't present to the swap chain
|
||
// until all rendering has completed
|
||
struct {
|
||
VkSemaphore presentComplete;
|
||
VkSemaphore renderComplete;
|
||
} semaphores;
|
||
|
||
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
|
||
{
|
||
width = 1280;
|
||
height = 720;
|
||
zoom = -2.5f;
|
||
title = "Vulkan Example - Basic indexed triangle";
|
||
// Values not set here are initialized in the base class constructor
|
||
}
|
||
|
||
~VulkanExample()
|
||
{
|
||
// Clean up used Vulkan resources
|
||
// Note : Inherited destructor cleans up resources stored in base class
|
||
vkDestroyPipeline(device, pipeline, nullptr);
|
||
|
||
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
||
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
||
|
||
vkDestroyBuffer(device, vertices.buf, nullptr);
|
||
vkFreeMemory(device, vertices.mem, nullptr);
|
||
|
||
vkDestroyBuffer(device, indices.buf, nullptr);
|
||
vkFreeMemory(device, indices.mem, nullptr);
|
||
|
||
vkDestroySemaphore(device, semaphores.presentComplete, nullptr);
|
||
vkDestroySemaphore(device, semaphores.renderComplete, nullptr);
|
||
|
||
vkDestroyBuffer(device, uniformDataVS.buffer, nullptr);
|
||
vkFreeMemory(device, uniformDataVS.memory, nullptr);
|
||
}
|
||
|
||
// This function is used to request a device memory type that supports all the property flags we request (e.g. device local, host visibile)
|
||
// Upon success it will return the index of the memory type that fits our requestes memory properties
|
||
// This is necessary as implementations can offer an arbitrary number of memory types with different
|
||
// memory properties.
|
||
// You can check http://vulkan.gpuinfo.org/ for details on different memory configurations
|
||
uint32_t getMemoryTypeIndex(uint32_t typeBits, VkMemoryPropertyFlags properties)
|
||
{
|
||
// Iterate over all memory types available for the device used in this example
|
||
for (uint32_t i = 0; i < deviceMemoryProperties.memoryTypeCount; i++)
|
||
{
|
||
if ((typeBits & 1) == 1)
|
||
{
|
||
if ((deviceMemoryProperties.memoryTypes[i].propertyFlags & properties) == properties)
|
||
{
|
||
return i;
|
||
}
|
||
}
|
||
typeBits >>= 1;
|
||
}
|
||
|
||
throw "Could not find a suitable memory type!";
|
||
}
|
||
|
||
// We are going to use several temporary command buffers for setup stuff
|
||
// like submitting barriers for layout translations of buffer copies
|
||
// This function will allocate a single (primary) command buffer from the
|
||
// examples' command buffer pool and if begin is set to true it will
|
||
// also start the buffer so we can directly put commands into it
|
||
VkCommandBuffer getCommandBuffer(bool begin)
|
||
{
|
||
VkCommandBuffer cmdBuffer;
|
||
|
||
VkCommandBufferAllocateInfo cmdBufAllocateInfo = {};
|
||
cmdBufAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||
cmdBufAllocateInfo.commandPool = cmdPool;
|
||
cmdBufAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
||
cmdBufAllocateInfo.commandBufferCount = 1;
|
||
|
||
VK_CHECK_RESULT(vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, &cmdBuffer));
|
||
|
||
// If requested, also start the new command buffer
|
||
if (begin)
|
||
{
|
||
VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo();
|
||
VK_CHECK_RESULT(vkBeginCommandBuffer(cmdBuffer, &cmdBufInfo));
|
||
}
|
||
|
||
return cmdBuffer;
|
||
}
|
||
|
||
// This will end the command buffer and submit it to the examples' queue
|
||
// Then waits for the queue to become idle so our submission is finished
|
||
// and then deletes the command buffer
|
||
// For use with setup command buffers created by getCommandBuffer
|
||
void flushCommandBuffer(VkCommandBuffer commandBuffer)
|
||
{
|
||
assert(commandBuffer != VK_NULL_HANDLE);
|
||
|
||
VK_CHECK_RESULT(vkEndCommandBuffer(commandBuffer));
|
||
|
||
VkSubmitInfo submitInfo = {};
|
||
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||
submitInfo.commandBufferCount = 1;
|
||
submitInfo.pCommandBuffers = &commandBuffer;
|
||
|
||
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
||
VK_CHECK_RESULT(vkQueueWaitIdle(queue));
|
||
|
||
vkFreeCommandBuffers(device, cmdPool, 1, &commandBuffer);
|
||
}
|
||
|
||
// Build separate command buffers for every framebuffer image
|
||
// Unlike in OpenGL all rendering commands are recorded once
|
||
// into command buffers that are then resubmitted to the queue
|
||
void buildCommandBuffers()
|
||
{
|
||
VkCommandBufferBeginInfo cmdBufInfo = {};
|
||
cmdBufInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||
cmdBufInfo.pNext = NULL;
|
||
|
||
// Set clear values for all framebuffer attachments with loadOp set to clear
|
||
// We use two attachments (color and depth) that are cleared at the
|
||
// start of the subpass and as such we need to set clear values for both
|
||
VkClearValue clearValues[2];
|
||
clearValues[0].color = defaultClearColor;
|
||
clearValues[1].depthStencil = { 1.0f, 0 };
|
||
|
||
VkRenderPassBeginInfo renderPassBeginInfo = {};
|
||
renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
||
renderPassBeginInfo.pNext = NULL;
|
||
renderPassBeginInfo.renderPass = renderPass;
|
||
renderPassBeginInfo.renderArea.offset.x = 0;
|
||
renderPassBeginInfo.renderArea.offset.y = 0;
|
||
renderPassBeginInfo.renderArea.extent.width = width;
|
||
renderPassBeginInfo.renderArea.extent.height = height;
|
||
renderPassBeginInfo.clearValueCount = 2;
|
||
renderPassBeginInfo.pClearValues = clearValues;
|
||
|
||
for (int32_t i = 0; i < drawCmdBuffers.size(); ++i)
|
||
{
|
||
// Set target frame buffer
|
||
renderPassBeginInfo.framebuffer = frameBuffers[i];
|
||
|
||
VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));
|
||
|
||
// Start the first sub pass specified in our default render pass setup by the base class
|
||
// This will clear the color and depth attachment
|
||
vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
||
|
||
// Update dynamic viewport state
|
||
VkViewport viewport = {};
|
||
viewport.height = (float)height;
|
||
viewport.width = (float)width;
|
||
viewport.minDepth = (float) 0.0f;
|
||
viewport.maxDepth = (float) 1.0f;
|
||
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
|
||
|
||
// Update dynamic scissor state
|
||
VkRect2D scissor = {};
|
||
scissor.extent.width = width;
|
||
scissor.extent.height = height;
|
||
scissor.offset.x = 0;
|
||
scissor.offset.y = 0;
|
||
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
|
||
|
||
// Bind descriptor sets describing shader binding points
|
||
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL);
|
||
|
||
// Bind the rendering pipeline
|
||
// The pipeline (state object) contains all states of the rendering pipeline
|
||
// So once we bind a pipeline all states that were set upon creation of that
|
||
// pipeline will be set
|
||
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
||
|
||
// Bind triangle vertex buffer (contains position and colors)
|
||
VkDeviceSize offsets[1] = { 0 };
|
||
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &vertices.buf, offsets);
|
||
|
||
// Bind triangle index buffer
|
||
vkCmdBindIndexBuffer(drawCmdBuffers[i], indices.buf, 0, VK_INDEX_TYPE_UINT32);
|
||
|
||
// Draw indexed triangle
|
||
vkCmdDrawIndexed(drawCmdBuffers[i], indices.count, 1, 0, 0, 1);
|
||
|
||
vkCmdEndRenderPass(drawCmdBuffers[i]);
|
||
|
||
// Add a present memory barrier to the end of the command buffer
|
||
// This will transform the frame buffer color attachment to a
|
||
// new layout for presenting it to the windowing system integration
|
||
VkImageMemoryBarrier prePresentBarrier = {};
|
||
prePresentBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
||
prePresentBarrier.pNext = NULL;
|
||
prePresentBarrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
||
prePresentBarrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
|
||
prePresentBarrier.oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||
prePresentBarrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
||
prePresentBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||
prePresentBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||
prePresentBarrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
|
||
prePresentBarrier.image = swapChain.buffers[i].image;
|
||
|
||
VkImageMemoryBarrier *pMemoryBarrier = &prePresentBarrier;
|
||
vkCmdPipelineBarrier(
|
||
drawCmdBuffers[i],
|
||
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
|
||
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
|
||
VK_FLAGS_NONE,
|
||
0, nullptr,
|
||
0, nullptr,
|
||
1, &prePresentBarrier);
|
||
|
||
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
|
||
}
|
||
|
||
// Build command buffers for the post present image barrier for each swap chain image
|
||
// Note: The command Buffers are allocated in the base class
|
||
|
||
for (uint32_t i = 0; i < swapChain.imageCount; i++)
|
||
{
|
||
// Insert a post present image barrier to transform the image back to a
|
||
// color attachment that our render pass can write to
|
||
// We always use undefined image layout as the source as it doesn't actually matter
|
||
// what is done with the previous image contents
|
||
VkImageMemoryBarrier postPresentBarrier = vkTools::initializers::imageMemoryBarrier();
|
||
postPresentBarrier.srcAccessMask = 0;
|
||
postPresentBarrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
||
postPresentBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||
postPresentBarrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||
postPresentBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||
postPresentBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||
postPresentBarrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
|
||
postPresentBarrier.image = swapChain.buffers[i].image;
|
||
|
||
// Use dedicated command buffer from example base class for submitting the post present barrier
|
||
VkCommandBufferBeginInfo cmdBufInfo = {};
|
||
cmdBufInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||
|
||
VK_CHECK_RESULT(vkBeginCommandBuffer(postPresentCmdBuffers[i], &cmdBufInfo));
|
||
|
||
// Put post present barrier into command buffer
|
||
vkCmdPipelineBarrier(
|
||
postPresentCmdBuffers[i],
|
||
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
|
||
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
|
||
VK_FLAGS_NONE,
|
||
0, nullptr,
|
||
0, nullptr,
|
||
1, &postPresentBarrier);
|
||
|
||
VK_CHECK_RESULT(vkEndCommandBuffer(postPresentCmdBuffers[i]));
|
||
}
|
||
}
|
||
|
||
void draw()
|
||
{
|
||
// Get next image in the swap chain (back/front buffer)
|
||
VK_CHECK_RESULT(swapChain.acquireNextImage(semaphores.presentComplete, ¤tBuffer));
|
||
|
||
// Submit the post present image barrier to transform the image back to a color attachment
|
||
// that can be used to write to by our render pass
|
||
VkSubmitInfo submitInfo = {};
|
||
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||
submitInfo.commandBufferCount = 1;
|
||
submitInfo.pCommandBuffers = &postPresentCmdBuffers[currentBuffer];
|
||
|
||
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
||
|
||
// Make sure that the image barrier command submitted to the queue
|
||
// has finished executing
|
||
VK_CHECK_RESULT(vkQueueWaitIdle(queue));
|
||
|
||
// The submit infor strcuture contains a list of
|
||
// command buffers and semaphores to be submitted to a queue
|
||
// If you want to submit multiple command buffers, pass an array
|
||
VkPipelineStageFlags pipelineStages = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
|
||
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||
submitInfo.pWaitDstStageMask = &pipelineStages;
|
||
// The wait semaphore ensures that the image is presented
|
||
// before we start submitting command buffers agein
|
||
submitInfo.waitSemaphoreCount = 1;
|
||
submitInfo.pWaitSemaphores = &semaphores.presentComplete;
|
||
// Submit the currently active command buffer
|
||
submitInfo.commandBufferCount = 1;
|
||
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
||
// The signal semaphore is used during queue presentation
|
||
// to ensure that the image is not rendered before all
|
||
// commands have been submitted
|
||
submitInfo.signalSemaphoreCount = 1;
|
||
submitInfo.pSignalSemaphores = &semaphores.renderComplete;
|
||
|
||
// Submit to the graphics queue
|
||
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
||
|
||
// Present the current buffer to the swap chain
|
||
// We pass the signal semaphore from the submit info
|
||
// to ensure that the image is not rendered until
|
||
// all commands have been submitted
|
||
VK_CHECK_RESULT(swapChain.queuePresent(queue, currentBuffer, semaphores.renderComplete));
|
||
}
|
||
|
||
// Create synchronzation semaphores
|
||
void prepareSemaphore()
|
||
{
|
||
VkSemaphoreCreateInfo semaphoreCreateInfo = {};
|
||
semaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
||
semaphoreCreateInfo.pNext = NULL;
|
||
|
||
// This semaphore ensures that the image is complete
|
||
// before starting to submit again
|
||
VK_CHECK_RESULT(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &semaphores.presentComplete));
|
||
|
||
// This semaphore ensures that all commands submitted
|
||
// have been finished before submitting the image to the queue
|
||
VK_CHECK_RESULT(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &semaphores.renderComplete));
|
||
}
|
||
|
||
// Setups vertex and index buffers for an indexed triangle,
|
||
// uploads them to the VRAM and sets binding points and attribute
|
||
// descriptions to match locations inside the shaders
|
||
void prepareVertices(bool useStagingBuffers)
|
||
{
|
||
struct Vertex {
|
||
float pos[3];
|
||
float col[3];
|
||
};
|
||
|
||
// Setup vertices
|
||
std::vector<Vertex> vertexBuffer = {
|
||
{ { 1.0f, 1.0f, 0.0f }, { 1.0f, 0.0f, 0.0f } },
|
||
{ { -1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f } },
|
||
{ { 0.0f, -1.0f, 0.0f }, { 0.0f, 0.0f, 1.0f } }
|
||
};
|
||
uint32_t vertexBufferSize = static_cast<uint32_t>(vertexBuffer.size()) * sizeof(Vertex);
|
||
|
||
// Setup indices
|
||
std::vector<uint32_t> indexBuffer = { 0, 1, 2 };
|
||
indices.count = static_cast<uint32_t>(indexBuffer.size());
|
||
uint32_t indexBufferSize = indices.count * sizeof(uint32_t);
|
||
|
||
VkMemoryAllocateInfo memAlloc = {};
|
||
memAlloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||
VkMemoryRequirements memReqs;
|
||
|
||
void *data;
|
||
|
||
if (useStagingBuffers)
|
||
{
|
||
// Static data like vertex and index buffer should be stored on the device memory
|
||
// for optimal (and fastest) access by the GPU
|
||
//
|
||
// To achieve this we use so-called "staging buffers" :
|
||
// - Create a buffer that's visible to the host (and can be mapped)
|
||
// - Copy the data to this buffer
|
||
// - Create another buffer that's local on the device (VRAM) with the same size
|
||
// - Copy the data from the host to the device using a command buffer
|
||
// - Delete the host visible (staging) buffer
|
||
// - Use the device local buffers for rendering
|
||
|
||
struct StagingBuffer {
|
||
VkDeviceMemory memory;
|
||
VkBuffer buffer;
|
||
};
|
||
|
||
struct {
|
||
StagingBuffer vertices;
|
||
StagingBuffer indices;
|
||
} stagingBuffers;
|
||
|
||
// Vertex buffer
|
||
VkBufferCreateInfo vertexBufferInfo = {};
|
||
vertexBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||
vertexBufferInfo.size = vertexBufferSize;
|
||
// Buffer is used as the copy source
|
||
vertexBufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
||
// Create a host-visible buffer to copy the vertex data to (staging buffer)
|
||
VK_CHECK_RESULT(vkCreateBuffer(device, &vertexBufferInfo, nullptr, &stagingBuffers.vertices.buffer));
|
||
vkGetBufferMemoryRequirements(device, stagingBuffers.vertices.buffer, &memReqs);
|
||
memAlloc.allocationSize = memReqs.size;
|
||
// Request a host visible memory type that can be used to copy our data do
|
||
// Also request it to be coherent, so that writes are visible to the GPU right after unmapping the buffer
|
||
memAlloc.memoryTypeIndex = getMemoryTypeIndex(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
||
VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &stagingBuffers.vertices.memory));
|
||
// Map and copy
|
||
VK_CHECK_RESULT(vkMapMemory(device, stagingBuffers.vertices.memory, 0, memAlloc.allocationSize, 0, &data));
|
||
memcpy(data, vertexBuffer.data(), vertexBufferSize);
|
||
vkUnmapMemory(device, stagingBuffers.vertices.memory);
|
||
VK_CHECK_RESULT(vkBindBufferMemory(device, stagingBuffers.vertices.buffer, stagingBuffers.vertices.memory, 0));
|
||
|
||
// Create the destination buffer with device only visibility
|
||
// Buffer will be used as a vertex buffer and is the copy destination
|
||
vertexBufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
||
VK_CHECK_RESULT(vkCreateBuffer(device, &vertexBufferInfo, nullptr, &vertices.buf));
|
||
vkGetBufferMemoryRequirements(device, vertices.buf, &memReqs);
|
||
memAlloc.allocationSize = memReqs.size;
|
||
memAlloc.memoryTypeIndex = getMemoryTypeIndex(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||
VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &vertices.mem));
|
||
VK_CHECK_RESULT(vkBindBufferMemory(device, vertices.buf, vertices.mem, 0));
|
||
|
||
// Index buffer
|
||
VkBufferCreateInfo indexbufferInfo = {};
|
||
indexbufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||
indexbufferInfo.size = indexBufferSize;
|
||
indexbufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
||
// Copy index data to a buffer visible to the host (staging buffer)
|
||
VK_CHECK_RESULT(vkCreateBuffer(device, &indexbufferInfo, nullptr, &stagingBuffers.indices.buffer));
|
||
vkGetBufferMemoryRequirements(device, stagingBuffers.indices.buffer, &memReqs);
|
||
memAlloc.allocationSize = memReqs.size;
|
||
memAlloc.memoryTypeIndex = getMemoryTypeIndex(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
||
VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &stagingBuffers.indices.memory));
|
||
VK_CHECK_RESULT(vkMapMemory(device, stagingBuffers.indices.memory, 0, indexBufferSize, 0, &data));
|
||
memcpy(data, indexBuffer.data(), indexBufferSize);
|
||
vkUnmapMemory(device, stagingBuffers.indices.memory);
|
||
VK_CHECK_RESULT(vkBindBufferMemory(device, stagingBuffers.indices.buffer, stagingBuffers.indices.memory, 0));
|
||
|
||
// Create destination buffer with device only visibility
|
||
indexbufferInfo.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
||
VK_CHECK_RESULT(vkCreateBuffer(device, &indexbufferInfo, nullptr, &indices.buf));
|
||
vkGetBufferMemoryRequirements(device, indices.buf, &memReqs);
|
||
memAlloc.allocationSize = memReqs.size;
|
||
memAlloc.memoryTypeIndex = getMemoryTypeIndex(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||
VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &indices.mem));
|
||
VK_CHECK_RESULT(vkBindBufferMemory(device, indices.buf, indices.mem, 0));
|
||
|
||
VkCommandBufferBeginInfo cmdBufferBeginInfo = {};
|
||
cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||
cmdBufferBeginInfo.pNext = NULL;
|
||
|
||
// Buffer copies have to be submitted to a queue, so we need a command buffer for them
|
||
// Note that some devices offer a dedicated transfer queue (with only the transfer bit set)
|
||
// If you do lots of copies (especially at runtime) it's advised to use such a queu instead
|
||
// of a generalized graphics queue (that also supports transfers)
|
||
VkCommandBuffer copyCmd = getCommandBuffer(true);
|
||
|
||
// Put buffer region copies into command buffer
|
||
// Note that the staging buffer must not be deleted before the copies have been submitted and executed
|
||
|
||
VkBufferCopy copyRegion = {};
|
||
|
||
// Vertex buffer
|
||
copyRegion.size = vertexBufferSize;
|
||
vkCmdCopyBuffer(
|
||
copyCmd,
|
||
stagingBuffers.vertices.buffer,
|
||
vertices.buf,
|
||
1,
|
||
©Region);
|
||
// Index buffer
|
||
copyRegion.size = indexBufferSize;
|
||
vkCmdCopyBuffer(
|
||
copyCmd,
|
||
stagingBuffers.indices.buffer,
|
||
indices.buf,
|
||
1,
|
||
©Region);
|
||
|
||
flushCommandBuffer(copyCmd);
|
||
|
||
// Destroy staging buffers
|
||
vkDestroyBuffer(device, stagingBuffers.vertices.buffer, nullptr);
|
||
vkFreeMemory(device, stagingBuffers.vertices.memory, nullptr);
|
||
vkDestroyBuffer(device, stagingBuffers.indices.buffer, nullptr);
|
||
vkFreeMemory(device, stagingBuffers.indices.memory, nullptr);
|
||
}
|
||
else
|
||
{
|
||
// Don't use staging
|
||
// Create host-visible buffers only and use these for rendering
|
||
// This is not advised for real world applications and will
|
||
// result in lower performances at least on devices that
|
||
// separate between host visible and device local memory
|
||
|
||
// Vertex buffer
|
||
VkBufferCreateInfo vertexBufferInfo = {};
|
||
vertexBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||
vertexBufferInfo.size = vertexBufferSize;
|
||
vertexBufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
|
||
|
||
// Copy vertex data to a buffer visible to the host
|
||
VK_CHECK_RESULT(vkCreateBuffer(device, &vertexBufferInfo, nullptr, &vertices.buf));
|
||
vkGetBufferMemoryRequirements(device, vertices.buf, &memReqs);
|
||
memAlloc.allocationSize = memReqs.size;
|
||
memAlloc.memoryTypeIndex = getMemoryTypeIndex(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
|
||
VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &vertices.mem));
|
||
VK_CHECK_RESULT(vkMapMemory(device, vertices.mem, 0, memAlloc.allocationSize, 0, &data));
|
||
memcpy(data, vertexBuffer.data(), vertexBufferSize);
|
||
vkUnmapMemory(device, vertices.mem);
|
||
VK_CHECK_RESULT(vkBindBufferMemory(device, vertices.buf, vertices.mem, 0));
|
||
|
||
// Index buffer
|
||
VkBufferCreateInfo indexbufferInfo = {};
|
||
indexbufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||
indexbufferInfo.size = indexBufferSize;
|
||
indexbufferInfo.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
|
||
|
||
// Copy index data to a buffer visible to the host
|
||
VK_CHECK_RESULT(vkCreateBuffer(device, &indexbufferInfo, nullptr, &indices.buf));
|
||
vkGetBufferMemoryRequirements(device, indices.buf, &memReqs);
|
||
memAlloc.allocationSize = memReqs.size;
|
||
memAlloc.memoryTypeIndex = getMemoryTypeIndex(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
|
||
VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &indices.mem));
|
||
VK_CHECK_RESULT(vkMapMemory(device, indices.mem, 0, indexBufferSize, 0, &data));
|
||
memcpy(data, indexBuffer.data(), indexBufferSize);
|
||
vkUnmapMemory(device, indices.mem);
|
||
VK_CHECK_RESULT(vkBindBufferMemory(device, indices.buf, indices.mem, 0));
|
||
}
|
||
|
||
// Binding description
|
||
vertices.bindingDescriptions.resize(1);
|
||
vertices.bindingDescriptions[0].binding = VERTEX_BUFFER_BIND_ID;
|
||
vertices.bindingDescriptions[0].stride = sizeof(Vertex);
|
||
vertices.bindingDescriptions[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
|
||
|
||
// Attribute descriptions
|
||
// Describes memory layout and shader attribute locations
|
||
vertices.attributeDescriptions.resize(2);
|
||
// Location 0 : Position
|
||
vertices.attributeDescriptions[0].binding = VERTEX_BUFFER_BIND_ID;
|
||
vertices.attributeDescriptions[0].location = 0;
|
||
vertices.attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
|
||
vertices.attributeDescriptions[0].offset = 0;
|
||
// Location 1 : Color
|
||
vertices.attributeDescriptions[1].binding = VERTEX_BUFFER_BIND_ID;
|
||
vertices.attributeDescriptions[1].location = 1;
|
||
vertices.attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
|
||
vertices.attributeDescriptions[1].offset = sizeof(float) * 3;
|
||
|
||
// Assign to vertex input state
|
||
vertices.inputState.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
||
vertices.inputState.pNext = NULL;
|
||
vertices.inputState.flags = VK_FLAGS_NONE;
|
||
vertices.inputState.vertexBindingDescriptionCount = static_cast<uint32_t>(vertices.bindingDescriptions.size());
|
||
vertices.inputState.pVertexBindingDescriptions = vertices.bindingDescriptions.data();
|
||
vertices.inputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertices.attributeDescriptions.size());
|
||
vertices.inputState.pVertexAttributeDescriptions = vertices.attributeDescriptions.data();
|
||
}
|
||
|
||
void setupDescriptorPool()
|
||
{
|
||
// We need to tell the API the number of max. requested descriptors per type
|
||
VkDescriptorPoolSize typeCounts[1];
|
||
// This example only uses one descriptor type (uniform buffer) and only
|
||
// requests one descriptor of this type
|
||
typeCounts[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||
typeCounts[0].descriptorCount = 1;
|
||
// For additional types you need to add new entries in the type count list
|
||
// E.g. for two combined image samplers :
|
||
// typeCounts[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||
// typeCounts[1].descriptorCount = 2;
|
||
|
||
// Create the global descriptor pool
|
||
// All descriptors used in this example are allocated from this pool
|
||
VkDescriptorPoolCreateInfo descriptorPoolInfo = {};
|
||
descriptorPoolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
||
descriptorPoolInfo.pNext = NULL;
|
||
descriptorPoolInfo.poolSizeCount = 1;
|
||
descriptorPoolInfo.pPoolSizes = typeCounts;
|
||
// Set the max. number of sets that can be requested
|
||
// Requesting descriptors beyond maxSets will result in an error
|
||
descriptorPoolInfo.maxSets = 1;
|
||
|
||
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
||
}
|
||
|
||
void setupDescriptorSetLayout()
|
||
{
|
||
// Setup layout of descriptors used in this example
|
||
// Basically connects the different shader stages to descriptors
|
||
// for binding uniform buffers, image samplers, etc.
|
||
// So every shader binding should map to one descriptor set layout
|
||
// binding
|
||
|
||
// Binding 0 : Uniform buffer (Vertex shader)
|
||
VkDescriptorSetLayoutBinding layoutBinding = {};
|
||
layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||
layoutBinding.descriptorCount = 1;
|
||
layoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
|
||
layoutBinding.pImmutableSamplers = NULL;
|
||
|
||
VkDescriptorSetLayoutCreateInfo descriptorLayout = {};
|
||
descriptorLayout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
|
||
descriptorLayout.pNext = NULL;
|
||
descriptorLayout.bindingCount = 1;
|
||
descriptorLayout.pBindings = &layoutBinding;
|
||
|
||
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, NULL, &descriptorSetLayout));
|
||
|
||
// Create the pipeline layout that is used to generate the rendering pipelines that
|
||
// are based on this descriptor set layout
|
||
// In a more complex scenario you would have different pipeline layouts for different
|
||
// descriptor set layouts that could be reused
|
||
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {};
|
||
pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
||
pPipelineLayoutCreateInfo.pNext = NULL;
|
||
pPipelineLayoutCreateInfo.setLayoutCount = 1;
|
||
pPipelineLayoutCreateInfo.pSetLayouts = &descriptorSetLayout;
|
||
|
||
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
|
||
}
|
||
|
||
void setupDescriptorSet()
|
||
{
|
||
// Allocate a new descriptor set from the global descriptor pool
|
||
VkDescriptorSetAllocateInfo allocInfo = {};
|
||
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
||
allocInfo.descriptorPool = descriptorPool;
|
||
allocInfo.descriptorSetCount = 1;
|
||
allocInfo.pSetLayouts = &descriptorSetLayout;
|
||
|
||
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet));
|
||
|
||
// Update the descriptor set determining the shader binding points
|
||
// For every binding point used in a shader there needs to be one
|
||
// descriptor set matching that binding point
|
||
|
||
VkWriteDescriptorSet writeDescriptorSet = {};
|
||
|
||
// Binding 0 : Uniform buffer
|
||
writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||
writeDescriptorSet.dstSet = descriptorSet;
|
||
writeDescriptorSet.descriptorCount = 1;
|
||
writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||
writeDescriptorSet.pBufferInfo = &uniformDataVS.descriptor;
|
||
// Binds this uniform buffer to binding point 0
|
||
writeDescriptorSet.dstBinding = 0;
|
||
|
||
vkUpdateDescriptorSets(device, 1, &writeDescriptorSet, 0, NULL);
|
||
}
|
||
|
||
// Create the depth (and stencil) buffer attachments used by our framebuffers
|
||
// Note : Override of virtual function in the base class and called from within VulkanExampleBase::prepare
|
||
void setupDepthStencil()
|
||
{
|
||
// Create an optimal image used as the depth stencil attachment
|
||
VkImageCreateInfo image = {};
|
||
image.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
||
image.imageType = VK_IMAGE_TYPE_2D;
|
||
image.format = depthFormat;
|
||
// Use example's height and width
|
||
image.extent = { width, height, 1 };
|
||
image.mipLevels = 1;
|
||
image.arrayLayers = 1;
|
||
image.samples = VK_SAMPLE_COUNT_1_BIT;
|
||
image.tiling = VK_IMAGE_TILING_OPTIMAL;
|
||
image.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
|
||
image.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||
VK_CHECK_RESULT(vkCreateImage(device, &image, nullptr, &depthStencil.image));
|
||
|
||
// Allocate memory for the image (device local) and bind it to our image
|
||
VkMemoryAllocateInfo memAlloc = {};
|
||
memAlloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||
VkMemoryRequirements memReqs;
|
||
vkGetImageMemoryRequirements(device, depthStencil.image, &memReqs);
|
||
memAlloc.allocationSize = memReqs.size;
|
||
memAlloc.memoryTypeIndex = getMemoryTypeIndex(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||
VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &depthStencil.mem));
|
||
VK_CHECK_RESULT(vkBindImageMemory(device, depthStencil.image, depthStencil.mem, 0));
|
||
|
||
// We need to do an initial layout transition before we can use this image
|
||
// as the depth (and stencil) attachment
|
||
// Note that this may be ignored by implementations that don't care about image layout
|
||
// transitions, but it's crucial for those that do
|
||
|
||
VkCommandBuffer layoutCmd = getCommandBuffer(true);
|
||
|
||
vkTools::setImageLayout(
|
||
setupCmdBuffer,
|
||
depthStencil.image,
|
||
VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT,
|
||
VK_IMAGE_LAYOUT_UNDEFINED,
|
||
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
|
||
|
||
// Add a present memory barrier to the end of the command buffer
|
||
// This will transform the frame buffer color attachment to a
|
||
// new layout for presenting it to the windowing system integration
|
||
VkImageMemoryBarrier imageMemoryBarrier = {};
|
||
imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
||
imageMemoryBarrier.srcAccessMask = VK_FLAGS_NONE;
|
||
imageMemoryBarrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
|
||
// Transform layout from undefined (initial) to depth/stencil attachment (usage)
|
||
imageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||
imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
||
imageMemoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||
imageMemoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||
imageMemoryBarrier.subresourceRange = { VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT, 0, 1, 0, 1 };
|
||
imageMemoryBarrier.image = depthStencil.image;
|
||
|
||
vkCmdPipelineBarrier(
|
||
layoutCmd,
|
||
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
|
||
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
|
||
VK_FLAGS_NONE,
|
||
0, nullptr,
|
||
0, nullptr,
|
||
1, &imageMemoryBarrier);
|
||
|
||
flushCommandBuffer(layoutCmd);
|
||
|
||
// Create a view for ourt depth stencil image
|
||
// In Vulkan we can't access the images directly, but rather through views
|
||
// that describe a sub resource range
|
||
// So you can actually have multiple views for a single image if required
|
||
VkImageViewCreateInfo depthStencilView = {};
|
||
depthStencilView.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
||
depthStencilView.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||
depthStencilView.format = depthFormat;
|
||
depthStencilView.subresourceRange = {};
|
||
depthStencilView.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
|
||
depthStencilView.subresourceRange.baseMipLevel = 0;
|
||
depthStencilView.subresourceRange.levelCount = 1;
|
||
depthStencilView.subresourceRange.baseArrayLayer = 0;
|
||
depthStencilView.subresourceRange.layerCount = 1;
|
||
depthStencilView.image = depthStencil.image;
|
||
VK_CHECK_RESULT(vkCreateImageView(device, &depthStencilView, nullptr, &depthStencil.view));
|
||
}
|
||
|
||
// Create a frame buffer for each swap chain image
|
||
// Note : Override of virtual function in the base class and called from within VulkanExampleBase::prepare
|
||
void setupFrameBuffer()
|
||
{
|
||
// Create a frame buffers for every image in our swapchain
|
||
frameBuffers.resize(swapChain.imageCount);
|
||
for (size_t i = 0; i < frameBuffers.size(); i++)
|
||
{
|
||
std::array<VkImageView, 2> attachments;
|
||
// Color attachment is the view of the swapchain image
|
||
attachments[0] = swapChain.buffers[i].view;
|
||
// Depth/Stencil attachment is the same for all frame buffers
|
||
attachments[1] = depthStencil.view;
|
||
|
||
VkFramebufferCreateInfo frameBufferCreateInfo = {};
|
||
frameBufferCreateInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
|
||
// All frame buffers use the same renderpass setu<74>
|
||
frameBufferCreateInfo.renderPass = renderPass;
|
||
frameBufferCreateInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
|
||
frameBufferCreateInfo.pAttachments = attachments.data();
|
||
frameBufferCreateInfo.width = width;
|
||
frameBufferCreateInfo.height = height;
|
||
frameBufferCreateInfo.layers = 1;
|
||
// Create the framebuffer
|
||
VK_CHECK_RESULT(vkCreateFramebuffer(device, &frameBufferCreateInfo, nullptr, &frameBuffers[i]));
|
||
}
|
||
}
|
||
|
||
// Render pass setup for this example
|
||
// Note : Override of virtual function in the base class and called from within VulkanExampleBase::prepare
|
||
void setupRenderPass()
|
||
{
|
||
// Render passes are a new concept in Vulkan
|
||
// They describe the attachments used during rendering
|
||
// and may contain multiple subpasses with attachment
|
||
// dependencies
|
||
// This allows the driver to know up-front how the
|
||
// rendering will look like and is a good opportunity to
|
||
// optimize, especially on tile-based renderers (with multiple subpasses)
|
||
|
||
// This example will use a single render pass with
|
||
// one subpass, which is the minimum setup
|
||
|
||
// Two attachments
|
||
// Basic setup with a color and a depth attachments
|
||
std::array<VkAttachmentDescription, 2> attachments = {};
|
||
|
||
// Color attachment
|
||
attachments[0].format = colorformat;
|
||
// We don't use multi sampling
|
||
// Multi sampling requires a setup with resolve attachments
|
||
// See the multisampling example for more details
|
||
attachments[0].samples = VK_SAMPLE_COUNT_1_BIT;
|
||
// loadOp describes what happens with the attachment content at the beginning of the subpass
|
||
// We want the color buffer to be cleared
|
||
attachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
||
// storeOp describes what happens with the attachment content after the subpass is finished
|
||
// As we want to display the color attachment after rendering is done we have to store it
|
||
attachments[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
||
// We don't use stencil and DONT_CARE allows the driver to discard the result
|
||
attachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
||
attachments[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
||
// Set the initial image layout for the color atttachment
|
||
attachments[0].initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||
attachments[0].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||
|
||
// Depth attachment
|
||
attachments[1].format = depthFormat;
|
||
attachments[1].samples = VK_SAMPLE_COUNT_1_BIT;
|
||
// Clear depth at the beginnging of the subpass
|
||
attachments[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
||
// We don't need the contents of the depth buffer after the sub pass
|
||
// anymore, so let the driver discard it
|
||
attachments[1].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
||
// Don't care for stencil
|
||
attachments[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
||
attachments[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
||
// Set the initial image layout for the depth attachment
|
||
attachments[1].initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
||
attachments[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
||
|
||
// Setup references to our attachments for the sub pass
|
||
VkAttachmentReference colorReference = {};
|
||
colorReference.attachment = 0;
|
||
colorReference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||
|
||
VkAttachmentReference depthReference = {};
|
||
depthReference.attachment = 1;
|
||
depthReference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
||
|
||
// Setup a single subpass that references our color and depth attachments
|
||
VkSubpassDescription subpass = {};
|
||
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
||
// Input attachments can be used to sample from contents of attachments
|
||
// written to by previous sub passes in a setup with multiple sub passes
|
||
// This example doesn't make use of this
|
||
subpass.inputAttachmentCount = 0;
|
||
subpass.pInputAttachments = nullptr;
|
||
// Preserved attachments can be used to loop (and preserve) attachments
|
||
// through a sub pass that does not actually use them
|
||
// This example doesn't make use of this
|
||
subpass.preserveAttachmentCount = 0;
|
||
subpass.pPreserveAttachments = nullptr;
|
||
// Resoluve attachments are resolved at the end of a sub pass and can be
|
||
// used for e.g. multi sampling
|
||
// This example doesn't make use of this
|
||
subpass.pResolveAttachments = nullptr;
|
||
// Reference to the color attachment
|
||
subpass.colorAttachmentCount = 1;
|
||
subpass.pColorAttachments = &colorReference;
|
||
// Reference to the depth attachment
|
||
subpass.pDepthStencilAttachment = &depthReference;
|
||
|
||
// Setup the render pass
|
||
VkRenderPassCreateInfo renderPassInfo = {};
|
||
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
||
// Set attachments used in our renderpass
|
||
renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
|
||
renderPassInfo.pAttachments = attachments.data();
|
||
// We only use one subpass
|
||
renderPassInfo.subpassCount = 1;
|
||
renderPassInfo.pSubpasses = &subpass;
|
||
// As we only use one subpass we don't have any subpass dependencies
|
||
renderPassInfo.dependencyCount = 0;
|
||
renderPassInfo.pDependencies = nullptr;
|
||
|
||
// Create the renderpass
|
||
VK_CHECK_RESULT(vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass));
|
||
}
|
||
|
||
void preparePipelines()
|
||
{
|
||
// Create our rendering pipeline used in this example
|
||
// Vulkan uses the concept of rendering pipelines to encapsulate
|
||
// fixed states
|
||
// This replaces OpenGL's huge (and cumbersome) state machine
|
||
// A pipeline is then stored and hashed on the GPU making
|
||
// pipeline changes much faster than having to set dozens of
|
||
// states
|
||
// In a real world application you'd have dozens of pipelines
|
||
// for every shader set used in a scene
|
||
// Note that there are a few states that are not stored with
|
||
// the pipeline. These are called dynamic states and the
|
||
// pipeline only stores that they are used with this pipeline,
|
||
// but not their states
|
||
|
||
VkGraphicsPipelineCreateInfo pipelineCreateInfo = {};
|
||
|
||
pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
|
||
// The layout used for this pipeline
|
||
pipelineCreateInfo.layout = pipelineLayout;
|
||
// Renderpass this pipeline is attached to
|
||
pipelineCreateInfo.renderPass = renderPass;
|
||
|
||
// Vertex input state
|
||
// Describes the topoloy used with this pipeline
|
||
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = {};
|
||
inputAssemblyState.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
|
||
// This pipeline renders vertex data as triangle lists
|
||
inputAssemblyState.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
||
|
||
// Rasterization state
|
||
VkPipelineRasterizationStateCreateInfo rasterizationState = {};
|
||
rasterizationState.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
|
||
// Solid polygon mode
|
||
rasterizationState.polygonMode = VK_POLYGON_MODE_FILL;
|
||
// No culling
|
||
rasterizationState.cullMode = VK_CULL_MODE_NONE;
|
||
rasterizationState.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
|
||
rasterizationState.depthClampEnable = VK_FALSE;
|
||
rasterizationState.rasterizerDiscardEnable = VK_FALSE;
|
||
rasterizationState.depthBiasEnable = VK_FALSE;
|
||
rasterizationState.lineWidth = 1.0f;
|
||
|
||
// Color blend state
|
||
// Describes blend modes and color masks
|
||
VkPipelineColorBlendStateCreateInfo colorBlendState = {};
|
||
colorBlendState.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
|
||
// One blend attachment state
|
||
// Blending is not used in this example
|
||
VkPipelineColorBlendAttachmentState blendAttachmentState[1] = {};
|
||
blendAttachmentState[0].colorWriteMask = 0xf;
|
||
blendAttachmentState[0].blendEnable = VK_FALSE;
|
||
colorBlendState.attachmentCount = 1;
|
||
colorBlendState.pAttachments = blendAttachmentState;
|
||
|
||
// Viewport state
|
||
VkPipelineViewportStateCreateInfo viewportState = {};
|
||
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
|
||
// One viewport
|
||
viewportState.viewportCount = 1;
|
||
// One scissor rectangle
|
||
viewportState.scissorCount = 1;
|
||
|
||
// Enable dynamic states
|
||
// Describes the dynamic states to be used with this pipeline
|
||
// Dynamic states can be set even after the pipeline has been created
|
||
// So there is no need to create new pipelines just for changing
|
||
// a viewport's dimensions or a scissor box
|
||
VkPipelineDynamicStateCreateInfo dynamicState = {};
|
||
// The dynamic state properties themselves are stored in the command buffer
|
||
std::vector<VkDynamicState> dynamicStateEnables;
|
||
dynamicStateEnables.push_back(VK_DYNAMIC_STATE_VIEWPORT);
|
||
dynamicStateEnables.push_back(VK_DYNAMIC_STATE_SCISSOR);
|
||
dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
|
||
dynamicState.pDynamicStates = dynamicStateEnables.data();
|
||
dynamicState.dynamicStateCount = static_cast<uint32_t>(dynamicStateEnables.size());
|
||
|
||
// Depth and stencil state
|
||
// Describes depth and stenctil test and compare ops
|
||
VkPipelineDepthStencilStateCreateInfo depthStencilState = {};
|
||
// Basic depth compare setup with depth writes and depth test enabled
|
||
// No stencil used
|
||
depthStencilState.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
|
||
depthStencilState.depthTestEnable = VK_TRUE;
|
||
depthStencilState.depthWriteEnable = VK_TRUE;
|
||
depthStencilState.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
|
||
depthStencilState.depthBoundsTestEnable = VK_FALSE;
|
||
depthStencilState.back.failOp = VK_STENCIL_OP_KEEP;
|
||
depthStencilState.back.passOp = VK_STENCIL_OP_KEEP;
|
||
depthStencilState.back.compareOp = VK_COMPARE_OP_ALWAYS;
|
||
depthStencilState.stencilTestEnable = VK_FALSE;
|
||
depthStencilState.front = depthStencilState.back;
|
||
|
||
// Multi sampling state
|
||
VkPipelineMultisampleStateCreateInfo multisampleState = {};
|
||
multisampleState.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
|
||
multisampleState.pSampleMask = NULL;
|
||
// No multi sampling used in this example
|
||
multisampleState.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
||
|
||
// Load shaders
|
||
// Shaders are loaded from the SPIR-V format, which can be generated from glsl
|
||
std::array<VkPipelineShaderStageCreateInfo,2> shaderStages;
|
||
shaderStages[0] = loadShader(getAssetPath() + "shaders/triangle.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
||
shaderStages[1] = loadShader(getAssetPath() + "shaders/triangle.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
||
|
||
// Assign states
|
||
// Assign pipeline state create information
|
||
pipelineCreateInfo.stageCount = static_cast<uint32_t>(shaderStages.size());
|
||
pipelineCreateInfo.pStages = shaderStages.data();
|
||
pipelineCreateInfo.pVertexInputState = &vertices.inputState;
|
||
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
|
||
pipelineCreateInfo.pRasterizationState = &rasterizationState;
|
||
pipelineCreateInfo.pColorBlendState = &colorBlendState;
|
||
pipelineCreateInfo.pMultisampleState = &multisampleState;
|
||
pipelineCreateInfo.pViewportState = &viewportState;
|
||
pipelineCreateInfo.pDepthStencilState = &depthStencilState;
|
||
pipelineCreateInfo.renderPass = renderPass;
|
||
pipelineCreateInfo.pDynamicState = &dynamicState;
|
||
|
||
// Create rendering pipeline
|
||
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipeline));
|
||
}
|
||
|
||
void prepareUniformBuffers()
|
||
{
|
||
// Prepare and initialize a uniform buffer block containing shader uniforms
|
||
// In Vulkan there are no more single uniforms like in GL
|
||
// All shader uniforms are passed as uniform buffer blocks
|
||
VkMemoryRequirements memReqs;
|
||
|
||
// Vertex shader uniform buffer block
|
||
VkBufferCreateInfo bufferInfo = {};
|
||
VkMemoryAllocateInfo allocInfo = {};
|
||
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||
allocInfo.pNext = NULL;
|
||
allocInfo.allocationSize = 0;
|
||
allocInfo.memoryTypeIndex = 0;
|
||
|
||
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||
bufferInfo.size = sizeof(uboVS);
|
||
bufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
|
||
|
||
// Create a new buffer
|
||
VK_CHECK_RESULT(vkCreateBuffer(device, &bufferInfo, nullptr, &uniformDataVS.buffer));
|
||
// Get memory requirements including size, alignment and memory type
|
||
vkGetBufferMemoryRequirements(device, uniformDataVS.buffer, &memReqs);
|
||
allocInfo.allocationSize = memReqs.size;
|
||
// Get the memory type index that supports host visibile memory access
|
||
// Most implementations offer multiple memory tpyes and selecting the
|
||
// correct one to allocate memory from is important
|
||
// We also want the buffer to be host coherent so we don't have to flush
|
||
// after every update.
|
||
// Note that this may affect performance so you might not want to do this
|
||
// in a real world application that updates buffers on a regular base
|
||
allocInfo.memoryTypeIndex = getMemoryTypeIndex(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
||
// Allocate memory for the uniform buffer
|
||
VK_CHECK_RESULT(vkAllocateMemory(device, &allocInfo, nullptr, &(uniformDataVS.memory)));
|
||
// Bind memory to buffer
|
||
VK_CHECK_RESULT(vkBindBufferMemory(device, uniformDataVS.buffer, uniformDataVS.memory, 0));
|
||
|
||
// Store information in the uniform's descriptor
|
||
uniformDataVS.descriptor.buffer = uniformDataVS.buffer;
|
||
uniformDataVS.descriptor.offset = 0;
|
||
uniformDataVS.descriptor.range = sizeof(uboVS);
|
||
|
||
updateUniformBuffers();
|
||
}
|
||
|
||
void updateUniformBuffers()
|
||
{
|
||
// Update matrices
|
||
uboVS.projectionMatrix = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.1f, 256.0f);
|
||
|
||
uboVS.viewMatrix = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, zoom));
|
||
|
||
uboVS.modelMatrix = glm::mat4();
|
||
uboVS.modelMatrix = glm::rotate(uboVS.modelMatrix, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
|
||
uboVS.modelMatrix = glm::rotate(uboVS.modelMatrix, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
|
||
uboVS.modelMatrix = glm::rotate(uboVS.modelMatrix, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
|
||
|
||
// Map uniform buffer and update it
|
||
uint8_t *pData;
|
||
VK_CHECK_RESULT(vkMapMemory(device, uniformDataVS.memory, 0, sizeof(uboVS), 0, (void **)&pData));
|
||
memcpy(pData, &uboVS, sizeof(uboVS));
|
||
vkUnmapMemory(device, uniformDataVS.memory);
|
||
}
|
||
|
||
void prepare()
|
||
{
|
||
VulkanExampleBase::prepare();
|
||
prepareSemaphore();
|
||
prepareVertices(USE_STAGING);
|
||
prepareUniformBuffers();
|
||
setupDescriptorSetLayout();
|
||
preparePipelines();
|
||
setupDescriptorPool();
|
||
setupDescriptorSet();
|
||
buildCommandBuffers();
|
||
prepared = true;
|
||
}
|
||
|
||
virtual void render()
|
||
{
|
||
if (!prepared)
|
||
return;
|
||
draw();
|
||
}
|
||
|
||
virtual void viewChanged()
|
||
{
|
||
// This function is called by the base example class
|
||
// each time the view is changed by user input
|
||
updateUniformBuffers();
|
||
}
|
||
};
|
||
|
||
VulkanExample *vulkanExample;
|
||
|
||
#if defined(_WIN32)
|
||
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||
{
|
||
if (vulkanExample != NULL)
|
||
{
|
||
vulkanExample->handleMessages(hWnd, uMsg, wParam, lParam);
|
||
}
|
||
return (DefWindowProc(hWnd, uMsg, wParam, lParam));
|
||
}
|
||
#elif defined(__linux__) && !defined(__ANDROID__)
|
||
static void handleEvent(const xcb_generic_event_t *event)
|
||
{
|
||
if (vulkanExample != NULL)
|
||
{
|
||
vulkanExample->handleEvent(event);
|
||
}
|
||
}
|
||
#endif
|
||
|
||
// Main entry point
|
||
#if defined(_WIN32)
|
||
// Windows entry point
|
||
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow)
|
||
#elif defined(__ANDROID__)
|
||
// Android entry point
|
||
void android_main(android_app* state)
|
||
#elif defined(__linux__)
|
||
// Linux entry point
|
||
int main(const int argc, const char *argv[])
|
||
#endif
|
||
{
|
||
#if defined(__ANDROID__)
|
||
// Removing this may cause the compiler to omit the main entry point
|
||
// which would make the application crash at start
|
||
app_dummy();
|
||
#endif
|
||
vulkanExample = new VulkanExample();
|
||
#if defined(_WIN32)
|
||
vulkanExample->setupWindow(hInstance, WndProc);
|
||
#elif defined(__ANDROID__)
|
||
// Attach vulkan example to global android application state
|
||
state->userData = vulkanExample;
|
||
state->onAppCmd = VulkanExample::handleAppCommand;
|
||
state->onInputEvent = VulkanExample::handleAppInput;
|
||
vulkanExample->androidApp = state;
|
||
#elif defined(__linux__)
|
||
vulkanExample->setupWindow();
|
||
#endif
|
||
#if !defined(__ANDROID__)
|
||
vulkanExample->initSwapchain();
|
||
vulkanExample->prepare();
|
||
#endif
|
||
vulkanExample->renderLoop();
|
||
delete(vulkanExample);
|
||
#if !defined(__ANDROID__)
|
||
return 0;
|
||
#endif
|
||
} |