From fd9ceae67d9d327d1cc3f565c08cb986cf0a095f Mon Sep 17 00:00:00 2001 From: Anish Bhobe Date: Fri, 13 Jun 2025 22:48:31 +0200 Subject: [PATCH] VertexBuffer ingestion and Camera. --- .gitignore | 1 + AppState.cpp | 73 ++++--- Blaze.cpp | 43 +++- Blaze.vcxproj | 2 +- Blaze.vcxproj.filters | 2 +- Triangle.slang => Mesh.slang | 16 +- MiscData.cpp | 373 ++++++++++++++++++++++++++++------- MiscData.h | 42 +++- RenderDevice.cpp | 13 +- Triangle.spv | Bin 1296 -> 0 bytes vcpkg.json | 1 + 11 files changed, 438 insertions(+), 128 deletions(-) rename Triangle.slang => Mesh.slang (67%) delete mode 100644 Triangle.spv diff --git a/.gitignore b/.gitignore index 3077ef9..964514f 100644 --- a/.gitignore +++ b/.gitignore @@ -613,3 +613,4 @@ $RECYCLE.BIN/ # Windows shortcuts *.lnk +*.spv diff --git a/AppState.cpp b/AppState.cpp index 448e0ae..189eb2b 100644 --- a/AppState.cpp +++ b/AppState.cpp @@ -8,61 +8,60 @@ bool AppState::isInit() const { - return window and renderDevice and renderDevice->isInit(); + return window and renderDevice and renderDevice->isInit(); } void AppState::destroy() { - if (!isInit()) return; + if ( !isInit() ) return; - renderDevice->waitIdle(); + renderDevice->waitIdle(); - Take(miscData)->cleanup(*renderDevice); + Take( miscData )->destroy( *renderDevice ); - Take(renderDevice)->destroy(); - SDL_DestroyWindow(Take(window)); + Take( renderDevice )->destroy(); + SDL_DestroyWindow( Take( window ) ); } -AppState::AppState(SDL_Window* window, RenderDevice* renderDevice, MiscData* miscData) - : window{ window } - , renderDevice{ renderDevice } - , miscData{ miscData } { -} +AppState::AppState( SDL_Window* window, RenderDevice* renderDevice, MiscData* miscData ) + : window{ window } + , renderDevice{ renderDevice } + , miscData{ miscData } {} -AppState* CreateAppState(GlobalMemory* memory, uint32_t const width, uint32_t const height) +AppState* CreateAppState( GlobalMemory* memory, uint32_t const width, uint32_t const height ) { - SDL_Window* window = SDL_CreateWindow( - "Blaze Test", - static_cast(width), - static_cast(height), - SDL_WINDOW_VULKAN); - if (!window) - { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s", SDL_GetError()); - return nullptr; - } + SDL_Window* window = SDL_CreateWindow( + "Blaze Test", + static_cast(width), + static_cast(height), + SDL_WINDOW_VULKAN ); + if ( !window ) + { + SDL_LogError( SDL_LOG_CATEGORY_APPLICATION, "%s", SDL_GetError() ); + return nullptr; + } - auto state = memory->getState(); - RenderDevice* renderDevice = CreateRenderDevice(memory, { .window = window }); - if (!renderDevice->isInit()) - { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "RenderDevice failed to init"); - return nullptr; - } - (void)state; + auto state = memory->getState(); + RenderDevice* renderDevice = CreateRenderDevice( memory, { .window = window } ); + if ( !renderDevice->isInit() ) + { + SDL_LogError( SDL_LOG_CATEGORY_APPLICATION, "RenderDevice failed to init" ); + return nullptr; + } + (void)state; - auto* miscDataAllocation = memory->allocate(sizeof(MiscData)); + auto* miscDataAllocation = memory->allocate( sizeof( MiscData ) ); - MiscData* miscData = new(miscDataAllocation) MiscData{}; - miscData->init(*renderDevice); + MiscData* miscData = new( miscDataAllocation ) MiscData{}; + miscData->init( *renderDevice ); - auto* allocation = memory->allocate(sizeof(AppState)); - AppState* appState = new(allocation) AppState{ window, renderDevice, miscData }; + auto* allocation = memory->allocate( sizeof( AppState ) ); + AppState* appState = new( allocation ) AppState{ window, renderDevice, miscData }; - return appState; + return appState; } AppState::~AppState() { - ASSERT(!isInit()); + ASSERT( !isInit() ); } diff --git a/Blaze.cpp b/Blaze.cpp index 43be370..719ae75 100644 --- a/Blaze.cpp +++ b/Blaze.cpp @@ -10,6 +10,7 @@ #define SDL_MAIN_USE_CALLBACKS 1 #include +#include #include #include #include @@ -68,6 +69,8 @@ SDL_AppResult SDL_AppInit( void** pAppState, int, char** ) return SDL_APP_CONTINUE; } +char g_buf[1000]; + SDL_AppResult SDL_AppIterate( void* appstate ) { AppState& appState = *static_cast(appstate); @@ -76,10 +79,31 @@ SDL_AppResult SDL_AppIterate( void* appstate ) Frame& currentFrame = renderDevice.frames[renderDevice.frameIndex]; VK_CHECK( - vkWaitForFences(renderDevice.device, 1, ¤tFrame.frameReadyToReuse, VK_TRUE, std::numeric_limits::max - ()) ); + vkWaitForFences(renderDevice.device, + 1, + ¤tFrame.frameReadyToReuse, + VK_TRUE, + std::numeric_limits::max()) ); // All resources of frame 'frameIndex' are free. + uint64_t const previousCounter = misc.previousCounter; + uint64_t const currentCounter = SDL_GetPerformanceCounter(); + uint64_t const deltaCount = currentCounter - previousCounter; + uint64_t const perfFreq = SDL_GetPerformanceFrequency(); + double const deltaTime = static_cast(deltaCount) / static_cast(perfFreq); + misc.previousCounter = currentCounter; + + double deltaTimeMs = deltaTime * 1000.0; + double fps = 1.0 / deltaTime; + auto _ = sprintf_s<1000>( g_buf, "%.2f fps %.5fms %llu -> %llu", fps, deltaTimeMs, previousCounter, currentCounter ); + SDL_SetWindowTitle( appState.window, g_buf ); + + misc.cameraData.modelMatrix = glm::rotate( + misc.cameraData.modelMatrix, + glm::radians( 60.0f ) * static_cast(deltaTime), + glm::vec3{ 0.0f, 1.0f, 0.0f } ); + memcpy( misc.cameraUniformBufferPtr, &misc.cameraData, sizeof misc.cameraData ); + uint32_t currentImageIndex; VK_CHECK( vkAcquireNextImageKHR(renderDevice.device, renderDevice.swapchain, std::numeric_limits::max(), @@ -151,8 +175,19 @@ SDL_AppResult SDL_AppIterate( void* appstate ) vkCmdSetScissor( cmd, 0, 1, &scissor ); // Render Something? - vkCmdBindPipeline( cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, misc.trianglePipeline ); - vkCmdDraw( cmd, 3, 1, 0, 0 ); + vkCmdBindPipeline( cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, misc.meshPipeline ); + VkDeviceSize constexpr offset = 0; + vkCmdBindVertexBuffers( cmd, 0, 1, &misc.vertexBuffer, &offset ); + vkCmdBindDescriptorSets( + cmd, + VK_PIPELINE_BIND_POINT_GRAPHICS, + misc.pipelineLayout, + 0, + 1, + &misc.descriptorSet, + 0, + nullptr ); + vkCmdDraw( cmd, static_cast(misc.vertices.size()), 1, 0, 0 ); } vkCmdEndRendering( cmd ); vkCmdPipelineBarrier2( cmd, &misc.renderToPresentDependency ); diff --git a/Blaze.vcxproj b/Blaze.vcxproj index a403489..3ffce4d 100644 --- a/Blaze.vcxproj +++ b/Blaze.vcxproj @@ -163,7 +163,7 @@ - + Document slangc %(FullPath) -profile sm_6_6 -target spirv -o %(Filename).spv %(Filename).spv diff --git a/Blaze.vcxproj.filters b/Blaze.vcxproj.filters index 5559e39..f0108cd 100644 --- a/Blaze.vcxproj.filters +++ b/Blaze.vcxproj.filters @@ -58,7 +58,7 @@ - + Shader Files diff --git a/Triangle.slang b/Mesh.slang similarity index 67% rename from Triangle.slang rename to Mesh.slang index d02bf6f..79493ef 100644 --- a/Triangle.slang +++ b/Mesh.slang @@ -15,13 +15,23 @@ struct VertexOut { float4 vertexColor : CoarseColor; }; +struct CameraData { + float4x4 model; + float4x4 view; + float4x4 proj; +}; + +ParameterBlock camera; + [shader("vertex")] VertexOut VertexMain( - uint vertexId: SV_VertexID + uint vertexId: SV_VertexID, + float4 position, + float4 color, ) { VertexOut output; - output.outPosition = vertexPos[vertexId]; - output.vertexColor = vertexColors[vertexId]; + output.outPosition = mul(camera.proj, mul(camera.view, mul(camera.model, position))); + output.vertexColor = color; return output; } diff --git a/MiscData.cpp b/MiscData.cpp index bbc3f8d..82217f4 100644 --- a/MiscData.cpp +++ b/MiscData.cpp @@ -6,13 +6,18 @@ #include "MacroUtils.h" #include "RenderDevice.h" +#include void MiscData::init( RenderDevice const& renderDevice ) { VkDevice const device = renderDevice.device; + + previousCounter = 0; + + // Pipeline Creation { size_t dataSize; - void* rawData = SDL_LoadFile( "Triangle.spv", &dataSize ); + void* rawData = SDL_LoadFile( "Mesh.spv", &dataSize ); ASSERT( dataSize % 4 == 0 ); if ( !rawData ) @@ -34,12 +39,29 @@ void MiscData::init( RenderDevice const& renderDevice ) VkShaderModule shaderModule; VK_CHECK( vkCreateShaderModule(device, &shaderModuleCreateInfo, nullptr, &shaderModule) ); - VkPipelineLayoutCreateInfo constexpr pipelineLayoutCreateInfo = { + VkDescriptorSetLayoutBinding constexpr descriptorSetLayoutBinding = { + .binding = 0, + .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .descriptorCount = 1, + .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, + .pImmutableSamplers = nullptr, + }; + + VkDescriptorSetLayoutCreateInfo const descriptorSetLayoutCreateInfo = { + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .bindingCount = 1, + .pBindings = &descriptorSetLayoutBinding, + }; + VK_CHECK( vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCreateInfo, nullptr, &descriptorSetLayout) ); + + VkPipelineLayoutCreateInfo const pipelineLayoutCreateInfo = { .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .pNext = nullptr, .flags = 0, - .setLayoutCount = 0, - .pSetLayouts = nullptr, + .setLayoutCount = 1, + .pSetLayouts = &descriptorSetLayout, .pushConstantRangeCount = 0, .pPushConstantRanges = nullptr, }; @@ -66,21 +88,42 @@ void MiscData::init( RenderDevice const& renderDevice ) } }; - VkPipelineVertexInputStateCreateInfo constexpr vertexInputState = { + VkVertexInputBindingDescription constexpr bindingDescription = { + .binding = 0, + .stride = sizeof( Vertex ), + .inputRate = VK_VERTEX_INPUT_RATE_VERTEX, + }; + + std::array attributeDescriptions = { + VkVertexInputAttributeDescription{ + .location = 0, + .binding = 0, + .format = VK_FORMAT_R32G32B32A32_SFLOAT, + .offset = offsetof( Vertex, position ), + }, + VkVertexInputAttributeDescription{ + .location = 1, + .binding = 0, + .format = VK_FORMAT_R32G32B32A32_SFLOAT, + .offset = offsetof( Vertex, color ), + }, + }; + + VkPipelineVertexInputStateCreateInfo const vertexInputState = { .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, .pNext = nullptr, .flags = 0, - .vertexBindingDescriptionCount = 0, - .pVertexBindingDescriptions = nullptr, - .vertexAttributeDescriptionCount = 0, - .pVertexAttributeDescriptions = nullptr, + .vertexBindingDescriptionCount = 1, + .pVertexBindingDescriptions = &bindingDescription, + .vertexAttributeDescriptionCount = static_cast(attributeDescriptions.size()), + .pVertexAttributeDescriptions = attributeDescriptions.data(), }; VkPipelineInputAssemblyStateCreateInfo constexpr inputAssembly = { .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, .pNext = nullptr, .flags = 0, - .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, + .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP, .primitiveRestartEnable = VK_FALSE, }; @@ -210,80 +253,264 @@ void MiscData::init( RenderDevice const& renderDevice ) .basePipelineIndex = 0, }; - VK_CHECK( vkCreateGraphicsPipelines(device, nullptr, 1, &graphicsPipelineCreateInfo, nullptr, &trianglePipeline) ); + VK_CHECK( vkCreateGraphicsPipelines(device, nullptr, 1, &graphicsPipelineCreateInfo, nullptr, &meshPipeline) ); vkDestroyShaderModule( device, shaderModule, nullptr ); SDL_free( rawData ); } - acquireToRenderBarrier = { - .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, - .pNext = nullptr, - .srcStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, - .srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT, - .dstStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, - .dstAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT, - .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, - .newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, - .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .subresourceRange = { - .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, - .baseMipLevel = 0, - .levelCount = 1, - .baseArrayLayer = 0, - .layerCount = 1, - } - }; - acquireToRenderDependency = { - .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, - .pNext = nullptr, - .dependencyFlags = 0, - .memoryBarrierCount = 0, - .pMemoryBarriers = nullptr, - .bufferMemoryBarrierCount = 0, - .pBufferMemoryBarriers = nullptr, - .imageMemoryBarrierCount = 1, - .pImageMemoryBarriers = &acquireToRenderBarrier, - }; + // Vertex Buffer Creation + { + vertexBufferSize = sizeof vertices[0] * vertices.size(); - renderToPresentBarrier = { - .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, - .pNext = nullptr, - .srcStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, - .srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT, - .dstStageMask = VK_PIPELINE_STAGE_2_BOTTOM_OF_PIPE_BIT, - .dstAccessMask = VK_ACCESS_2_NONE, - .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, - .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, - .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .subresourceRange = { - .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, - .baseMipLevel = 0, - .levelCount = 1, - .baseArrayLayer = 0, - .layerCount = 1, + // TL----TR + // | \ | + // | \ | + // | \ | + // BL----BR + // + // BL -> BR -> TL + // TL -> BR -> TR + + vertices = std::array{ + // Bottom Left + Vertex{ + .position = { -1.0f, -1.0f, 0.0f, 1.0f }, + .color = { 0.0f, 0.0f, 1.0f, 1.0f }, + }, + // Bottom Right + Vertex{ + .position = { 1.0f, -1.0f, 0.0f, 1.0f }, + .color = { 1.0f, 0.0f, 0.0f, 1.0f }, + }, + // Top Left + Vertex{ + .position = { -1.0f, 1.0f, 0.0f, 1.0f }, + .color = { 0.0f, 1.0f, 0.0f, 1.0f }, + }, + // Top Right + Vertex{ + .position = { 1.0f, 1.0f, 0.0f, 1.0f }, + .color = { 1.0f, 1.0f, 0.0f, 1.0f }, + } + }; + + VkBufferCreateInfo const bufferCreateInfo = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .size = vertexBufferSize, + .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, + .sharingMode = VK_SHARING_MODE_EXCLUSIVE, + .queueFamilyIndexCount = 0, + .pQueueFamilyIndices = nullptr, + }; + + VmaAllocationCreateInfo constexpr allocationCreateInfo = { + .flags = VMA_ALLOCATION_CREATE_MAPPED_BIT | + VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT, + .usage = VMA_MEMORY_USAGE_AUTO, + .requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, + .preferredFlags = 0, + .memoryTypeBits = 0, + .pool = nullptr, + .pUserData = nullptr, + .priority = 1.0f, + }; + + VmaAllocationInfo allocationInfo; + + VK_CHECK( + vmaCreateBuffer( + renderDevice.gpuAllocator, + &bufferCreateInfo, + &allocationCreateInfo, + &vertexBuffer, + &vertexBufferAllocation, + &allocationInfo) ); + + if ( allocationInfo.pMappedData ) + { + memcpy( allocationInfo.pMappedData, vertices.data(), vertices.size() * sizeof vertices[0] ); } - }; - renderToPresentDependency = { - .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, - .pNext = nullptr, - .dependencyFlags = 0, - .memoryBarrierCount = 0, - .pMemoryBarriers = nullptr, - .bufferMemoryBarrierCount = 0, - .pBufferMemoryBarriers = nullptr, - .imageMemoryBarrierCount = 1, - .pImageMemoryBarriers = &renderToPresentBarrier, - }; + } + + // Camera + { + cameraPosition = glm::vec3{ 0.0f, 0.0f, -5.0f }; + cameraTarget = glm::vec3{ 0.0f, 0.0f, 0.0f }; + cameraData.modelMatrix = glm::mat4{ 1.0f }; + cameraData.viewMatrix = glm::lookAt( cameraPosition, cameraTarget, glm::vec3{ 0.0f, 1.0f, 0.0f } ); + cameraData.projectionMatrix = glm::perspective( glm::radians( 70.0f ), 16.0f / 9.0f, 0.1f, 1000.0f ); + + cameraUniformBufferSize = sizeof( CameraData ); + + VkBufferCreateInfo const bufferCreateInfo = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .size = cameraUniformBufferSize, + .usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, + .sharingMode = VK_SHARING_MODE_EXCLUSIVE, + .queueFamilyIndexCount = 0, + .pQueueFamilyIndices = nullptr, + }; + + VmaAllocationCreateInfo constexpr allocationCreateInfo = { + .flags = VMA_ALLOCATION_CREATE_MAPPED_BIT | + VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT, + .usage = VMA_MEMORY_USAGE_AUTO, + .requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, + .preferredFlags = 0, + .memoryTypeBits = 0, + .pool = nullptr, + .pUserData = nullptr, + .priority = 1.0f, + }; + + VmaAllocationInfo allocationInfo; + + VK_CHECK( + vmaCreateBuffer( + renderDevice.gpuAllocator, + &bufferCreateInfo, + &allocationCreateInfo, + &cameraUniformBuffer, + &cameraUniformBufferAllocation, + &allocationInfo) ); + + if ( allocationInfo.pMappedData ) + { + memcpy( allocationInfo.pMappedData, &cameraData, sizeof cameraData ); + cameraUniformBufferPtr = static_cast(allocationInfo.pMappedData); + } + } + + // Descriptors + { + VkDescriptorPoolSize const poolSize = { + .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .descriptorCount = renderDevice.getNumFrames(), + }; + VkDescriptorPoolCreateInfo const descriptorPoolCreateInfo = { + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .maxSets = renderDevice.getNumFrames(), + .poolSizeCount = 1, + .pPoolSizes = &poolSize, + }; + + VK_CHECK( vkCreateDescriptorPool(device, &descriptorPoolCreateInfo, nullptr, &descriptorPool) ); + + VkDescriptorSetAllocateInfo const descriptorSetAllocateInfo = { + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, + .pNext = nullptr, + .descriptorPool = descriptorPool, + .descriptorSetCount = 1, + .pSetLayouts = &descriptorSetLayout, + }; + + VK_CHECK( vkAllocateDescriptorSets(device, &descriptorSetAllocateInfo, &descriptorSet) ); + + VkDescriptorBufferInfo const descriptorBufferInfo = { + .buffer = cameraUniformBuffer, + .offset = 0, + .range = sizeof CameraData, + }; + + VkWriteDescriptorSet writeDescriptorSet = { + .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, + .pNext = nullptr, + .dstSet = descriptorSet, + .dstBinding = 0, + .dstArrayElement = 0, + .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .pImageInfo = nullptr, + .pBufferInfo = &descriptorBufferInfo, + .pTexelBufferView = nullptr, + }; + + vkUpdateDescriptorSets( device, 1, &writeDescriptorSet, 0, nullptr ); + } + + // Barrier Creation + { + acquireToRenderBarrier = { + .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, + .pNext = nullptr, + .srcStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, + .srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT, + .dstStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, + .dstAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT, + .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, + .newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .subresourceRange = { + .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, + .baseMipLevel = 0, + .levelCount = 1, + .baseArrayLayer = 0, + .layerCount = 1, + } + }; + acquireToRenderDependency = { + .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, + .pNext = nullptr, + .dependencyFlags = 0, + .memoryBarrierCount = 0, + .pMemoryBarriers = nullptr, + .bufferMemoryBarrierCount = 0, + .pBufferMemoryBarriers = nullptr, + .imageMemoryBarrierCount = 1, + .pImageMemoryBarriers = &acquireToRenderBarrier, + }; + + renderToPresentBarrier = { + .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, + .pNext = nullptr, + .srcStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, + .srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT, + .dstStageMask = VK_PIPELINE_STAGE_2_BOTTOM_OF_PIPE_BIT, + .dstAccessMask = VK_ACCESS_2_NONE, + .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .subresourceRange = { + .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, + .baseMipLevel = 0, + .levelCount = 1, + .baseArrayLayer = 0, + .layerCount = 1, + } + }; + renderToPresentDependency = { + .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, + .pNext = nullptr, + .dependencyFlags = 0, + .memoryBarrierCount = 0, + .pMemoryBarriers = nullptr, + .bufferMemoryBarrierCount = 0, + .pBufferMemoryBarriers = nullptr, + .imageMemoryBarrierCount = 1, + .pImageMemoryBarriers = &renderToPresentBarrier, + }; + } } -void MiscData::cleanup( RenderDevice const& renderDevice ) +void MiscData::destroy( RenderDevice const& renderDevice ) { VkDevice const device = renderDevice.device; - vkDestroyPipeline( device, trianglePipeline, nullptr ); - vkDestroyPipelineLayout( device, pipelineLayout, nullptr ); + vkDestroyDescriptorPool( device, Take( descriptorPool ), nullptr ); + vmaDestroyBuffer( renderDevice.gpuAllocator, Take( cameraUniformBuffer ), Take( cameraUniformBufferAllocation ) ); + vmaDestroyBuffer( renderDevice.gpuAllocator, Take( vertexBuffer ), Take( vertexBufferAllocation ) ); + + vkDestroyPipeline( device, Take( meshPipeline ), nullptr ); + vkDestroyPipelineLayout( device, Take( pipelineLayout ), nullptr ); + vkDestroyDescriptorSetLayout( device, Take( descriptorSetLayout ), nullptr ); } diff --git a/MiscData.h b/MiscData.h index 0050012..f90715f 100644 --- a/MiscData.h +++ b/MiscData.h @@ -1,13 +1,49 @@ #pragma once +#include #include +#include + +#include + struct RenderDevice; +struct Vertex +{ + float position[4]; + float color[4]; +}; + struct MiscData { - VkPipelineLayout pipelineLayout; - VkPipeline trianglePipeline; + struct CameraData + { + glm::mat4x4 modelMatrix; + glm::mat4x4 viewMatrix; + glm::mat4x4 projectionMatrix; + }; + + uint64_t previousCounter; + + VkDescriptorSetLayout descriptorSetLayout; + VkPipelineLayout pipelineLayout; + VkPipeline meshPipeline; + + VkBuffer vertexBuffer; + VmaAllocation vertexBufferAllocation; + size_t vertexBufferSize; + std::array vertices; + + glm::vec3 cameraPosition; + glm::vec3 cameraTarget; + CameraData cameraData; + VkBuffer cameraUniformBuffer; + VmaAllocation cameraUniformBufferAllocation; + size_t cameraUniformBufferSize; + uint8_t* cameraUniformBufferPtr; + VkDescriptorPool descriptorPool; + VkDescriptorSet descriptorSet; VkImageMemoryBarrier2 acquireToRenderBarrier; VkDependencyInfo acquireToRenderDependency; @@ -15,5 +51,5 @@ struct MiscData VkDependencyInfo renderToPresentDependency; void init( RenderDevice const& renderDevice ); - void cleanup( RenderDevice const& renderDevice ); + void destroy( RenderDevice const& renderDevice ); }; diff --git a/RenderDevice.cpp b/RenderDevice.cpp index 9d14689..54ea3d7 100644 --- a/RenderDevice.cpp +++ b/RenderDevice.cpp @@ -59,11 +59,11 @@ RenderDevice* CreateRenderDevice( GlobalMemory* mem, RenderDevice::CreateInfo co // Create Surface ASSERT( SDL_Vulkan_CreateSurface(createInfo.window, instance, nullptr, &surface) ); - VkPhysicalDevice physicalDeviceInUse = nullptr; - VkDevice device = nullptr; - VmaAllocator gpuAllocator = nullptr; - std::optional directQueueFamilyIndex; - VkQueue directQueue = nullptr; + VkPhysicalDevice physicalDeviceInUse = nullptr; + VkDevice device = nullptr; + VmaAllocator gpuAllocator = nullptr; + std::optional directQueueFamilyIndex = std::nullopt; + VkQueue directQueue = nullptr; // Create Device and Queue { auto tempAllocStart = mem->getState(); @@ -418,6 +418,7 @@ void RenderDevice::destroy() vkDestroySwapchainKHR( device, Take( swapchain ), nullptr ); + vmaDestroyAllocator( Take( gpuAllocator ) ); vkDestroyDevice( Take( device ), nullptr ); SDL_Vulkan_DestroySurface( instance, Take( surface ), nullptr ); @@ -442,7 +443,7 @@ RenderDevice::RenderDevice( VkSurfaceKHR const surface, VkPhysicalDevice const physicalDeviceInUse, VkDevice const device, - VmaAllocator gpuAllocator, + VmaAllocator const gpuAllocator, VkQueue const directQueue, uint32_t const directQueueFamilyIndex, VkFormat const swapchainFormat, diff --git a/Triangle.spv b/Triangle.spv deleted file mode 100644 index 9ab68bc2eae0eef70e01f71c1fdb366b335d5d23..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1296 zcmZvcTTfF#6oogva&5T@UaA&NM1c@Vh*m08LV{0*KKW`+;)!X}3pqAM6CV6${w-ce zSl^u4u_X*yXZG6n*)yeBcv-d5jJ-CqPXcSj3O1v?X2rmf>oynUg1%qVY@Gi7lJtgV z2jYjqs(?>P`+MO$OMVQ};W+m)>qyrYmX#BGRVeCjK|A@&wixm#B>U4H^@ihak|l$0 z|7-343T4km{ZWR0SzS)|QdU-KC^*e7NBv}+eh=HZIKhT$2O@aCC zM!qPSTWg5ZFEQwv;!BaEhp%WSmOSKJ3wnLRUa}4C!~^nE`?0`s{v@Xz=Z(PscJ>rS z+#`V)`~Y_Mjc5PozE5RC2i(_f&!cYIRtaZ(C>wQ%$IoN7B~y?30NHK4ch~vuwl~q{ z`aCYL^-4DK5|1Bqe5;+Ab6)t7cIFD_-acw)cKEc#nLBR>A2{*69k;`IJI=}17AF09 zJJ{dpWuxb}z(RK|#v36wcU}|d#d$sQW7)ZT-ZTANiSq{5qwYl6h-JQ~s>6HO642p8 zJb7OT==8i_r%fT&$)?5=FHe`bfT8vXXF0`%g@%5?6p5gW;Pc#8|Hj0 Y869BxY`W^7UQYykxKnB}M|58E7B=@