Hi,
I am unable to run this Vulkan Triangle code. All my allocations are succeeding but couldn’t find where it is going wrong.
The shaders can be found from GitHub - SaschaWillems/Vulkan: Examples and demos for the new Vulkan API
/*
File : Source.cpp
*/
#define VK_USE_PLATFORM_WIN32_KHR 1
#include"external\vulkan\vulkan.h"
#include<iostream>
#include<string>
#include<assert.h>
#include"Triangle.h"
#include<vector>
#include<array>
#include"SwapChain.h"
#include<glm\glm.hpp>
#include<glm\gtc\matrix_transform.hpp>
struct{
glm::mat4 projectionMatrix;
glm::mat4 modelMatrix;
glm::mat4 viewMatrix;
}uboVS;
struct{
VkBuffer buffer;
VkDeviceMemory memory;
VkDescriptorBufferInfo descriptor;
} uniformDataVS;
std::wstring stemp = std::wstring(name.begin(), name.end());
std::wstring _win32_class_name;
std::wstring _window_name = L"Indexed Triangle";
HWND setupWindow(HINSTANCE hinstance, WNDPROC wndproc, int _surface_size_x, int _surface_size_y){
WNDCLASSEX win_class{};
assert(_surface_size_x > 0);
assert(_surface_size_y > 0);
uint64_t _win32_class_id_counter = 1;
hinstance = GetModuleHandle(nullptr);
_win32_class_name = _window_name + L"_" + std::to_wstring(_win32_class_id_counter);
_win32_class_id_counter++;
// Initialize the window class structure:
win_class.cbSize = sizeof(WNDCLASSEX);
win_class.style = CS_HREDRAW | CS_VREDRAW;
win_class.lpfnWndProc = wndproc;
win_class.cbClsExtra = 0;
win_class.cbWndExtra = 0;
win_class.hInstance = hinstance; // hInstance
win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
win_class.lpszMenuName = NULL;
win_class.lpszClassName = _win32_class_name.c_str();
win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
// Register window class:
if (!RegisterClassEx(&win_class)) {
// It didn't work, so try to give a useful error:
assert(0 && "Cannot create a window in which to draw!
");
fflush(stdout);
std::exit(-1);
}
DWORD ex_style = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
DWORD style = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
HWND _win32_window;
// Create window with the registered class:
RECT wr = { 0, 0, LONG(_surface_size_x), LONG(_surface_size_y) };
AdjustWindowRectEx(&wr, style, FALSE, ex_style);
_win32_window = CreateWindowEx(0,
_win32_class_name.c_str(), // class name
_window_name.c_str(), // app name
style, // window style
CW_USEDEFAULT, CW_USEDEFAULT, // x/y coords
wr.right - wr.left, // width
wr.bottom - wr.top, // height
NULL, // handle to parent
NULL, // handle to menu
hinstance, // hInstance
NULL); // no extra parameters
if (!_win32_window) {
// It didn't work, so try to give a useful error:
assert(1 && "Cannot create a window in which to draw!
");
fflush(stdout);
std::exit(-1);
}
// SetWindowLongPtr(_win32_window, GWLP_USERDATA, (LONG_PTR)this);
ShowWindow(_win32_window, SW_SHOW);
SetForegroundWindow(_win32_window);
SetFocus(_win32_window);
return _win32_window;
}
bool isopen = true;
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
if (uMsg == WM_CLOSE){
isopen = false;
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
bool Run(HWND _win32_window){
MSG msg;
if (PeekMessage(&msg, _win32_window, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return isopen;
}
static VkInstance instance;
struct{
VkSemaphore presentComplete;
VkSemaphore renderComplete;
VkSemaphore textOverlayComplete;
}semaphores;
struct {
VkImage image;
VkDeviceMemory mem;
VkImageView view;
} depthStencil;
VkPhysicalDevice physicalDevice;
VkDevice device;
VkPhysicalDeviceMemoryProperties deviceMemoryProperties;
VkQueue queue;
void CreateInstance(){
// Creating vkInstance
VkApplicationInfo appInfo = {};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = name.c_str();
appInfo.apiVersion = VK_API_VERSION_1_0;
std::vector<const char*>enabledExtensions = { VK_KHR_SURFACE_EXTENSION_NAME };
enabledExtensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
VkInstanceCreateInfo instanceCreateInfo = {};
instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instanceCreateInfo.pNext = NULL;
instanceCreateInfo.pApplicationInfo = &appInfo;
instanceCreateInfo.enabledExtensionCount = (uint32_t)enabledExtensions.size();
instanceCreateInfo.ppEnabledExtensionNames = enabledExtensions.data();
VkResult err = vkCreateInstance(&instanceCreateInfo, nullptr, &instance);
assert(err == VK_SUCCESS);
uint32_t gpuCount = 0;
vkEnumeratePhysicalDevices(instance, &gpuCount, nullptr);
assert(gpuCount > 0);
std::cout << gpuCount << std::endl;
std::vector<VkPhysicalDevice> physicalDevices(gpuCount);
err = vkEnumeratePhysicalDevices(instance, &gpuCount, physicalDevices.data());
physicalDevice = physicalDevices[0];
// Check for GPUs
uint32_t graphicsQueueIndex = 0;
uint32_t queueCount;
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueCount, NULL);
assert(queueCount >= 1);
std::vector<VkQueueFamilyProperties> queueProps;
queueProps.resize(queueCount);
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueCount, queueProps.data());
for (graphicsQueueIndex = 0; graphicsQueueIndex < queueCount; graphicsQueueIndex++){
if (queueProps[graphicsQueueIndex].queueFlags & VK_QUEUE_GRAPHICS_BIT)
break;
}
assert(graphicsQueueIndex < queueCount);
std::array<float, 1> queueProperties = { 0.0f };
VkDeviceQueueCreateInfo queueCreateInfo = {};
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueCreateInfo.queueFamilyIndex = graphicsQueueIndex;
queueCreateInfo.queueCount = 1;
queueCreateInfo.pQueuePriorities = queueProperties.data();
VkDeviceCreateInfo deviceCreateInfo = {};
deviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
deviceCreateInfo.pNext = NULL;
deviceCreateInfo.queueCreateInfoCount = 1;
deviceCreateInfo.pQueueCreateInfos = &queueCreateInfo;
VkPhysicalDeviceFeatures enabledFeatures = {};
deviceCreateInfo.pEnabledFeatures = &enabledFeatures;
VkPhysicalDeviceProperties deviceProperties;
VkPhysicalDeviceFeatures deviceFeatures;
vkCreateDevice(physicalDevice, &deviceCreateInfo, nullptr, &device);
vkGetPhysicalDeviceProperties(physicalDevice, &deviceProperties);
vkGetPhysicalDeviceFeatures(physicalDevice, &deviceFeatures);
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &deviceMemoryProperties);
vkGetDeviceQueue(device, graphicsQueueIndex, 0, &queue);
VkSemaphoreCreateInfo semaphoreCreateInfo = {};
semaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
semaphoreCreateInfo.pNext = NULL;
semaphoreCreateInfo.flags = 0;
vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &semaphores.presentComplete);
vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &semaphores.renderComplete);
vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &semaphores.textOverlayComplete);
VkSubmitInfo submitInfo = {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.pNext = NULL;
VkPipelineStageFlags submitPipelineStages = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
submitInfo.pWaitDstStageMask = &submitPipelineStages;
submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = &semaphores.presentComplete;
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = &semaphores.renderComplete;
}
std::vector<VkCommandBuffer> drawCmdBuffers;
std::vector<VkCommandBuffer> prePresentCmdBuffers;
std::vector<VkCommandBuffer> postPresentCmdBuffers;
VkFormat depthFormat;
VkBool32 getDepthFormat(){
std::vector<VkFormat> depthFormats = {
VK_FORMAT_D32_SFLOAT_S8_UINT,
VK_FORMAT_D32_SFLOAT,
VK_FORMAT_D24_UNORM_S8_UINT,
VK_FORMAT_D16_UNORM_S8_UINT,
VK_FORMAT_D16_UNORM
};
for (auto& format : depthFormats){
VkFormatProperties formatProps;
vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &formatProps);
if (formatProps.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT){
depthFormat = format;
return true;
}
}
return false;
}
VkBool32 getMemoryType(uint32_t typeBits, VkFlags properties, uint32_t *typeIndex){
for (uint32_t i = 0; i < 32; i++){
if ((typeBits & 1) == 1){
if ((deviceMemoryProperties.memoryTypes[i].propertyFlags & properties) == properties){
*typeIndex = i;
return true;
}
}
typeBits >>= 1;
}
return false;
}
uint32_t getMemoryType(uint32_t typeBits, VkFlags properties){
for (uint32_t i = 0; i < 32; i++){
if ((typeBits & 1) == 1){
if ((deviceMemoryProperties.memoryTypes[i].propertyFlags & properties) == properties){
return i;
}
}
typeBits >>= 1;
}
return 0;
}
VkCommandBuffer setupCmdBuffer;
VkCommandPool cmdPool;
void initVulkan(){
VkResult err;
// Creating command pool
VkCommandPoolCreateInfo cmdPoolInfo = {};
cmdPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
cmdPoolInfo.queueFamilyIndex = queueNodeIndex;
cmdPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
vkCreateCommandPool(device, &cmdPoolInfo, nullptr, &cmdPool);
// Create Setup command buffer
VkCommandBufferAllocateInfo cmdBufferAllocateInfo = {};
cmdBufferAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
cmdBufferAllocateInfo.commandPool = cmdPool;
cmdBufferAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
cmdBufferAllocateInfo.commandBufferCount = 1;
vkAllocateCommandBuffers(device, &cmdBufferAllocateInfo, &setupCmdBuffer);
VkCommandBufferBeginInfo cmdBufInfo = {};
cmdBufInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
vkBeginCommandBuffer(setupCmdBuffer, &cmdBufInfo);
create(physicalDevice, device, surface, setupCmdBuffer, &width, &height, true);
drawCmdBuffers.resize(imageCount);
prePresentCmdBuffers.resize(imageCount);
postPresentCmdBuffers.resize(imageCount);
VkCommandBufferAllocateInfo cmdBufAllocInfo = {};
cmdBufAllocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
cmdBufAllocInfo.commandPool = cmdPool;
cmdBufAllocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
cmdBufAllocInfo.commandBufferCount = static_cast<uint32_t>(drawCmdBuffers.size());
vkAllocateCommandBuffers(device, &cmdBufAllocInfo, drawCmdBuffers.data());
vkAllocateCommandBuffers(device, &cmdBufAllocInfo, prePresentCmdBuffers.data());
vkAllocateCommandBuffers(device, &cmdBufAllocInfo, postPresentCmdBuffers.data());
// Present Command Buffers
cmdBufInfo = {};
cmdBufInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
cmdBufInfo.pNext = NULL;
for (uint32_t i = 0; i < imageCount; i++){
vkBeginCommandBuffer(postPresentCmdBuffers[i], &cmdBufInfo);
VkImageMemoryBarrier postPresentBarrier = {};
postPresentBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
postPresentBarrier.pNext = NULL;
postPresentBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
postPresentBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
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.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
postPresentBarrier.image = buffers[i].image;
vkCmdPipelineBarrier(
postPresentCmdBuffers[i],
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
0,
0, nullptr,
0, nullptr,
1, &postPresentBarrier);
std::cout << i << std::endl;
vkEndCommandBuffer(postPresentCmdBuffers[i]);
vkBeginCommandBuffer(prePresentCmdBuffers[i], &cmdBufInfo);
VkImageMemoryBarrier prePresentBarrier = {};
prePresentBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
prePresentBarrier.pNext = NULL;
prePresentBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
prePresentBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
prePresentBarrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
prePresentBarrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
postPresentBarrier.oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
postPresentBarrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
postPresentBarrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
postPresentBarrier.image = buffers[i].image;
vkCmdPipelineBarrier(
prePresentCmdBuffers[i],
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
0,
0, nullptr,
0, nullptr,
1, &prePresentBarrier);
}
getDepthFormat();
// Setup Depth and Stencil buffer
VkImageCreateInfo imgCreateInfo = {};
imgCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imgCreateInfo.imageType = VK_IMAGE_TYPE_2D;
imgCreateInfo.format = depthFormat;
imgCreateInfo.extent = { width, height, 1 };
imgCreateInfo.mipLevels = 1;
imgCreateInfo.arrayLayers = 1;
imgCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imgCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
imgCreateInfo.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
imgCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
vkCreateImage(device, &imgCreateInfo, nullptr, &depthStencil.image);
VkMemoryAllocateInfo memAlloc = {};
memAlloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
VkMemoryRequirements memReqs;
vkGetImageMemoryRequirements(device, depthStencil.image, &memReqs);
memAlloc.allocationSize = memReqs.size;
memAlloc.memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_HEAP_DEVICE_LOCAL_BIT);
memAlloc.pNext = NULL;
vkAllocateMemory(device, &memAlloc, nullptr, &depthStencil.mem);
vkBindImageMemory(device, depthStencil.image, depthStencil.mem, 0);
VkCommandBuffer layoutCmd;
VkCommandBufferAllocateInfo cmdBufAllocateInfo = {};
cmdBufAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
cmdBufAllocateInfo.commandPool = cmdPool;
cmdBufAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
cmdBufAllocateInfo.commandBufferCount = 1;
vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, &layoutCmd);
VkCommandBufferBeginInfo cmdBufferBeginInfo = {};
cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
cmdBufferBeginInfo.pNext = NULL;
vkBeginCommandBuffer(layoutCmd, &cmdBufferBeginInfo);
// Have to do setImageLayout
VkImageMemoryBarrier imageMemoryBarrier = {};
imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
imageMemoryBarrier.pNext = NULL;
imageMemoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
imageMemoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
imageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
imageMemoryBarrier.image = depthStencil.image;
imageMemoryBarrier.srcAccessMask = 0;
imageMemoryBarrier.dstAccessMask = imageMemoryBarrier.dstAccessMask | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
VkImageSubresourceRange subresourceRange = {};
subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
subresourceRange.baseMipLevel = 0;
subresourceRange.levelCount = 1;
subresourceRange.layerCount = 1;
imageMemoryBarrier.subresourceRange = subresourceRange;
VkPipelineStageFlags srcStageFlags = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
VkPipelineStageFlags destStageFlags = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
// Put barrier inside setup command buffer
vkCmdPipelineBarrier(
layoutCmd,
srcStageFlags,
destStageFlags,
0,
0, nullptr,
0, nullptr,
1, &imageMemoryBarrier);
VkImageViewCreateInfo depthStencilView = {};
depthStencilView.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
depthStencilView.pNext = NULL;
depthStencilView.viewType = VK_IMAGE_VIEW_TYPE_2D;
depthStencilView.format = depthFormat;
depthStencilView.flags = 0;
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;
vkCreateImageView(device, &depthStencilView, nullptr, &depthStencil.view);
}
VkFormat colorformat = VK_FORMAT_B8G8R8A8_UNORM;
VkRenderPass renderPass;
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
vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass);
}
VkPipelineCache pipelineCache;
void createPipelineCache(){
VkPipelineCacheCreateInfo pipelineCacheCreateInfo = {};
pipelineCacheCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
vkCreatePipelineCache(device, &pipelineCacheCreateInfo, nullptr, &pipelineCache);
}
void initSwapChain(HINSTANCE hInstance, HWND window){
VkWin32SurfaceCreateInfoKHR surfaceCreateInfo = {};
surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
surfaceCreateInfo.hinstance = hInstance;
surfaceCreateInfo.hwnd = window;
vkCreateWin32SurfaceKHR(instance, &surfaceCreateInfo, nullptr, &surface);
}
std::vector<VkFramebuffer>frameBuffers;
void setupFrameBuffer(){
frameBuffers.resize(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] = 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ü
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
vkCreateFramebuffer(device, &frameBufferCreateInfo, nullptr, &frameBuffers[i]);
}
}
void flushSetupCommandBuffer(){
if (setupCmdBuffer == VK_NULL_HANDLE)
return;
vkEndCommandBuffer(setupCmdBuffer);
VkSubmitInfo submitInfo = {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &setupCmdBuffer;
vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE);
vkQueueWaitIdle(queue);
vkFreeCommandBuffers(device, cmdPool, 1, &setupCmdBuffer);
setupCmdBuffer = VK_NULL_HANDLE;
}
void createSetupCommandBuffer()
{
if (setupCmdBuffer != VK_NULL_HANDLE)
{
vkFreeCommandBuffers(device, cmdPool, 1, &setupCmdBuffer);
setupCmdBuffer = VK_NULL_HANDLE; // todo : check if still necessary
}
VkCommandBufferAllocateInfo cmdBufAllocateInfo = {};
cmdBufAllocateInfo.commandBufferCount = 1;
cmdBufAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
cmdBufAllocateInfo.commandPool = cmdPool;
cmdBufAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, &setupCmdBuffer);
VkCommandBufferBeginInfo cmdBufInfo = {};
cmdBufInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
vkBeginCommandBuffer(setupCmdBuffer, &cmdBufInfo);
}
void prepareSemaphores(){
VkSemaphoreCreateInfo semaphoreCreateInfo = {};
semaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
semaphoreCreateInfo.pNext = NULL;
vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &semaphores.presentComplete);
vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &semaphores.renderComplete);
}
/*
uint32_t getMemoryType(uint32_t typeBits, VkFlags properties){
for (uint32_t i = 0; i < 32; i++){
if ((typeBits & 1) == 1){
if ((deviceMemoryProperties.memoryTypes[i].propertyFlags & properties) == properties){
return i;
}
}
typeBits >>= 1;
}
return 0;
}
*/
struct{
VkBuffer buf;
VkDeviceMemory mem;
VkPipelineVertexInputStateCreateInfo inputState;
std::vector<VkVertexInputAttributeDescription> attributeDescriptions;
std::vector<VkVertexInputBindingDescription> bindingDescriptions;
} vertices;
struct{
VkBuffer buf;
VkDeviceMemory mem;
int count;
} indices;
void prepareVertices(){
struct Vertex{
float pos[3];
float col[3];
};
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 } }
};
size_t vertexBufferSize = vertexBuffer.size() * sizeof(Vertex);
std::vector<uint32_t> indexBuffer = { 0, 1, 2 };
size_t indexCount = indexBuffer.size();
size_t indexSize = sizeof(uint32_t) * indexCount;
VkMemoryAllocateInfo memAlloc = {};
memAlloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
VkMemoryRequirements memReqs;
void *data;
struct StagingBuffer{
VkDeviceMemory memory;
VkBuffer buffer;
};
struct{
StagingBuffer vertices;
StagingBuffer indices;
} stagingBuffers;
VkBufferCreateInfo vertexBufferInfo = {};
vertexBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
vertexBufferInfo.size = vertexBufferSize;
vertexBufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
vkCreateBuffer(device, &vertexBufferInfo, nullptr, &stagingBuffers.vertices.buffer);
vkGetBufferMemoryRequirements(device, stagingBuffers.vertices.buffer, &memReqs);
memAlloc.allocationSize = memReqs.size;
memAlloc.memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
vkAllocateMemory(device, &memAlloc, nullptr, &stagingBuffers.vertices.memory);
vkMapMemory(device, stagingBuffers.vertices.memory, 0, memAlloc.allocationSize, 0, &data);
memcpy(data, vertexBuffer.data(), vertexBufferSize);
vkUnmapMemory(device, stagingBuffers.vertices.memory);
vkBindBufferMemory(device, stagingBuffers.vertices.buffer, stagingBuffers.vertices.memory, 0);
vertexBufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
vkCreateBuffer(device, &vertexBufferInfo, nullptr, &vertices.buf);
vkGetBufferMemoryRequirements(device, vertices.buf, &memReqs);
memAlloc.allocationSize = memReqs.size;
memAlloc.memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
vkAllocateMemory(device, &memAlloc, nullptr, &vertices.mem);
vkBindBufferMemory(device, vertices.buf, vertices.mem, 0);
// Index buffer
VkBufferCreateInfo indexbufferInfo = {};
indexbufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
indexbufferInfo.size = indexSize;
indexbufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
vkCreateBuffer(device, &indexbufferInfo, nullptr, &stagingBuffers.indices.buffer);
vkGetBufferMemoryRequirements(device, stagingBuffers.indices.buffer, &memReqs);
memAlloc.allocationSize = memReqs.size;
memAlloc.memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
vkAllocateMemory(device, &memAlloc, nullptr, &stagingBuffers.indices.memory);
vkMapMemory(device, stagingBuffers.indices.memory, 0, indexSize, 0, &data);
memcpy(data, indexBuffer.data(), indexSize);
vkUnmapMemory(device, stagingBuffers.indices.memory);
vkBindBufferMemory(device, stagingBuffers.indices.buffer, stagingBuffers.indices.memory, 0);
indexbufferInfo.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
vkCreateBuffer(device, &indexbufferInfo, nullptr, &indices.buf);
vkGetBufferMemoryRequirements(device, indices.buf, &memReqs);
memAlloc.allocationSize = memReqs.size;
memAlloc.memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
vkAllocateMemory(device, &memAlloc, nullptr, &indices.mem);
vkBindBufferMemory(device, indices.buf, indices.mem, 0);
VkCommandBufferBeginInfo cmdBufferBeginInfo = {};
cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
cmdBufferBeginInfo.pNext = NULL;
VkCommandBuffer copyCmd;
VkCommandBufferAllocateInfo cmdBufAllocateInfo = {};
cmdBufAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
cmdBufAllocateInfo.commandPool = cmdPool;
cmdBufAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
cmdBufAllocateInfo.commandBufferCount = 1;
vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, ©Cmd);
VkCommandBufferBeginInfo cmdBufInfo = {};
cmdBufInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
cmdBufInfo.pNext = NULL;
vkBeginCommandBuffer(copyCmd, &cmdBufInfo);
VkBufferCopy copyRegion = {};
copyRegion.size = vertexBufferSize;
vkCmdCopyBuffer(
copyCmd,
stagingBuffers.vertices.buffer,
vertices.buf,
1,
©Region);
vkCmdCopyBuffer(
copyCmd,
stagingBuffers.indices.buffer,
indices.buf,
1,
©Region
);
vkDestroyBuffer(device, stagingBuffers.vertices.buffer, nullptr);
vkFreeMemory(device, stagingBuffers.vertices.memory, nullptr);
vkDestroyBuffer(device, stagingBuffers.indices.buffer, nullptr);
vkFreeMemory(device, stagingBuffers.indices.memory, nullptr);
vertices.bindingDescriptions.resize(1);
#define VERTEX_BUFFER_BIND_ID 0
vertices.bindingDescriptions[0].binding = VERTEX_BUFFER_BIND_ID;
vertices.bindingDescriptions[0].stride = sizeof(Vertex);
vertices.bindingDescriptions[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
vertices.attributeDescriptions.resize(2);
vertices.attributeDescriptions[0].binding = VERTEX_BUFFER_BIND_ID;
vertices.attributeDescriptions[0].location = 0;
vertices.attributeDescriptions[0].format = VK_FORMAT_R32G32B32A32_SFLOAT;
vertices.attributeDescriptions[0].offset = 0;
vertices.attributeDescriptions[0].binding = VERTEX_BUFFER_BIND_ID;
vertices.attributeDescriptions[0].location = 1;
vertices.attributeDescriptions[0].format = VK_FORMAT_R32G32B32A32_SFLOAT;
vertices.attributeDescriptions[0].offset = sizeof(float)*3;
vertices.inputState.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vertices.inputState.pNext = NULL;
#define VK_FLAGS_NONE 0
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 prepareUniforms(){
VkMemoryRequirements memReqs;
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;
vkCreateBuffer(device, &bufferInfo, nullptr, &uniformDataVS.buffer);
vkGetBufferMemoryRequirements(device, uniformDataVS.buffer, &memReqs);
allocInfo.allocationSize = memReqs.size;
allocInfo.memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
vkAllocateMemory(device, &allocInfo, nullptr, &(uniformDataVS.memory));
vkBindBufferMemory(device, uniformDataVS.buffer, uniformDataVS.memory, 0);
uniformDataVS.descriptor.buffer = uniformDataVS.buffer;
uniformDataVS.descriptor.offset = 0;
uniformDataVS.descriptor.range = sizeof(uboVS);
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(0.0f), glm::vec3(1.0f, 0.0f, 0.0f));
uboVS.modelMatrix = glm::rotate(uboVS.modelMatrix, glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
uboVS.modelMatrix = glm::rotate(uboVS.modelMatrix, glm::radians(0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
uint8_t *pData;
vkMapMemory(device, uniformDataVS.memory, 0, sizeof(uboVS), 0, (void**)&pData);
memcpy(pData, &uboVS, sizeof(uboVS));
vkUnmapMemory(device, uniformDataVS.memory);
}
VkDescriptorSetLayout descriptorSetLayout;
VkPipelineLayout pipelineLayout;
void setupDescriptorSetLayout(){
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;
vkCreateDescriptorSetLayout(device, &descriptorLayout, NULL, &descriptorSetLayout);
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {};
pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pPipelineLayoutCreateInfo.pNext = NULL;
pPipelineLayoutCreateInfo.setLayoutCount = 1;
pPipelineLayoutCreateInfo.pSetLayouts = &descriptorSetLayout;
vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout);
}
std::vector<VkShaderModule> shaderModules;
VkPipelineShaderStageCreateInfo loadShader(std::string path, VkDevice device, VkShaderStageFlagBits stage){
size_t size;
FILE *fp = fopen(path.c_str(), "rb");
assert(fp);
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
char *shaderCode = new char[size];
size_t retval = fread(shaderCode, size, 1, fp);
assert(retval == 1);
assert(size > 0);
fclose(fp);
VkShaderModule shaderModule;
VkShaderModuleCreateInfo moduleCreateInfo;
moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
moduleCreateInfo.pNext = NULL;
moduleCreateInfo.codeSize = size;
moduleCreateInfo.pCode = (uint32_t*)shaderCode;
moduleCreateInfo.flags = 0;
vkCreateShaderModule(device, &moduleCreateInfo, NULL, &shaderModule);
delete[] shaderCode;
VkPipelineShaderStageCreateInfo shaderStage = {};
shaderStage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shaderStage.stage = stage;
shaderStage.module = shaderModule;
shaderStage.pName = "main";
assert(shaderStage.module != NULL);
shaderModules.push_back(shaderStage.module);
return shaderStage;
}
const std::string getAssetPath(){
return "./../data/";
}
VkPipeline pipeline;
void preparePipelines(){
VkGraphicsPipelineCreateInfo pipelineCreateInfo = {};
pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipelineCreateInfo.layout = pipelineLayout;
pipelineCreateInfo.renderPass = renderPass;
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = {};
inputAssemblyState.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
inputAssemblyState.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
VkPipelineRasterizationStateCreateInfo rasterizationState = {};
rasterizationState.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
rasterizationState.polygonMode = VK_POLYGON_MODE_FILL;
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;
VkPipelineColorBlendStateCreateInfo colorBlendState = {};
colorBlendState.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
VkPipelineColorBlendAttachmentState blendAttachmentState[1] = {};
blendAttachmentState[0].colorWriteMask = 0xf;
blendAttachmentState[0].blendEnable = VK_FALSE;
colorBlendState.attachmentCount = 1;
colorBlendState.pAttachments = blendAttachmentState;
VkPipelineViewportStateCreateInfo viewportState = {};
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportState.viewportCount = 1;
viewportState.scissorCount = 1;
VkPipelineDynamicStateCreateInfo dynamicState = {};
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());
VkPipelineDepthStencilStateCreateInfo depthStencilState = {};
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;
VkPipelineMultisampleStateCreateInfo multisampleState = {};
multisampleState.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multisampleState.pSampleMask = NULL;
multisampleState.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages;
shaderStages[0] = loadShader(getAssetPath() + "triangle.vert.spv", device, VK_SHADER_STAGE_VERTEX_BIT);
shaderStages[1] = loadShader(getAssetPath() + "triangle.frag.spv", device, VK_SHADER_STAGE_FRAGMENT_BIT);
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;
assert(VK_SUCCESS == vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipeline));
}
VkDescriptorPool descriptorPool = VK_NULL_HANDLE;
void setupDescriptorPool(){
VkDescriptorPoolSize typeCounts[1];
typeCounts[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
typeCounts[0].descriptorCount = 1;
VkDescriptorPoolCreateInfo descriptorPoolInfo = {};
descriptorPoolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
descriptorPoolInfo.pNext = NULL;
descriptorPoolInfo.poolSizeCount = 1;
descriptorPoolInfo.pPoolSizes = typeCounts;
descriptorPoolInfo.maxSets = 1;
assert(VK_SUCCESS == vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
}
VkDescriptorSet descriptorSet;
void setupDescriptorSet(){
VkDescriptorSetAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = descriptorPool;
allocInfo.descriptorSetCount = 1;
allocInfo.pSetLayouts = &descriptorSetLayout;
assert(VK_SUCCESS == 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);
}
VkClearColorValue defaultClearColor = { { 0.025f, 0.025f, 0.025f, 1.0f } };
VkImageMemoryBarrier createImageMemoryBarrier(){
VkImageMemoryBarrier imageMemoryBarrier = {};
imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
imageMemoryBarrier.pNext = NULL;
imageMemoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
imageMemoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
return imageMemoryBarrier;
}
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];
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 = 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);
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 < 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 = createImageMemoryBarrier();
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 = 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;
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);
vkEndCommandBuffer(postPresentCmdBuffers[i]);
}
}
uint32_t currentBuffer = 0;
void draw(){
vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, semaphores.presentComplete, (VkFence)nullptr, ¤tBuffer);
VkSubmitInfo submitInfo = {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &postPresentCmdBuffers[currentBuffer];
vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE);
// Make sure that the image barrier command submitted to the queue
// has finished executing
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
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
VkPresentInfoKHR presentInfo = {};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.pNext = NULL;
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = &swapChain;
presentInfo.pImageIndices = ¤tBuffer;
if (semaphores.renderComplete != VK_NULL_HANDLE){
presentInfo.pWaitSemaphores = &semaphores.renderComplete;
presentInfo.waitSemaphoreCount = 1;
}
vkQueuePresentKHR(queue, &presentInfo);
// VK_CHECK_RESULT(swapChain.queuePresent(queue, currentBuffer, semaphores.renderComplete));
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow){
HWND window = setupWindow(hInstance, WndProc, width, height);
RedirectIOToConsole();
CreateInstance();
initSwapChain(hInstance, window);
startSwapChain(physicalDevice);
initVulkan();
setupRenderPass();
createPipelineCache();
setupFrameBuffer();
createSetupCommandBuffer();
prepareSemaphores();
prepareVertices();
prepareUniforms();
setupDescriptorSetLayout();
preparePipelines();
setupDescriptorPool();
setupDescriptorSet();
buildCommandBuffers();
MSG msg;
while (true){
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
draw();
}
std::cout << "HAHA" << std::endl;
while (Run(window)){}
FreeConsole();
}