Using custom alloctor.

This commit is contained in:
Anish Bhobe 2025-06-12 19:12:12 +02:00
parent 1e00847f90
commit 35347d28e0
13 changed files with 678 additions and 422 deletions

64
AppState.cpp Normal file
View File

@ -0,0 +1,64 @@
#include "AppState.h"
#include <SDL3/SDL_log.h>
#include "GlobalMemory.h"
#include "RenderDevice.h"
#include "MiscData.h"
bool AppState::isInit() const
{
return window and renderDevice and renderDevice->isInit();
}
void AppState::cleanup()
{
if (!isInit()) return;
renderDevice->waitIdle();
Take(miscData)->cleanup(*renderDevice);
Take(renderDevice)->cleanup();
SDL_DestroyWindow(Take(window));
}
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)
{
SDL_Window* window = SDL_CreateWindow("Blaze Test", static_cast<int>(width), static_cast<int>(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* miscDataAllocation = memory->allocate(sizeof(MiscData));
MiscData* miscData = new (miscDataAllocation) MiscData{};
miscData->init(*renderDevice);
auto* allocation = memory->allocate(sizeof(AppState));
AppState* appState = new (allocation) AppState{ window, renderDevice, miscData };
return appState;
}
AppState::~AppState()
{
cleanup();
}

30
AppState.h Normal file
View File

@ -0,0 +1,30 @@
#pragma once
#include <memory>
struct GlobalMemory;
struct SDL_Window;
struct RenderDevice;
struct MiscData;
struct AppState
{
SDL_Window* window;
RenderDevice* renderDevice;
MiscData* miscData;
[[nodiscard]] bool isInit() const;
void cleanup();
AppState(SDL_Window* window, RenderDevice* renderDevice, MiscData* miscData);
AppState(AppState const& other) = delete;
AppState(AppState&& other) noexcept = delete;
AppState& operator=(AppState const& other) = delete;
AppState& operator=(AppState&& other) noexcept = delete;
~AppState();
};
AppState* CreateAppState(GlobalMemory* memory, uint32_t const width, uint32_t const height);

371
Blaze.cpp
View File

@ -15,96 +15,50 @@
#include <SDL3/SDL_filesystem.h> #include <SDL3/SDL_filesystem.h>
#include <SDL3/SDL_vulkan.h> #include <SDL3/SDL_vulkan.h>
#include "AppState.h"
#include "Frame.h" #include "Frame.h"
#include "GlobalMemory.h"
#include "MacroUtils.h" #include "MacroUtils.h"
#include "MathUtil.h" #include "MathUtil.h"
#include "RenderDevice.h" #include "RenderDevice.h"
#include "MiscData.h"
constexpr uint32_t WIDTH = 1280; constexpr uint32_t WIDTH = 1280;
constexpr uint32_t HEIGHT = 720; constexpr uint32_t HEIGHT = 720;
constexpr uint32_t NUM_FRAMES = 3; constexpr uint32_t NUM_FRAMES = 3;
struct MiscData using Byte = uint8_t;
constexpr size_t operator ""_KiB(size_t const value)
{ {
VkPipelineLayout pipelineLayout; return value * 1024;
VkPipeline trianglePipeline;
VkImageMemoryBarrier2 acquireToRenderBarrier;
VkDependencyInfo acquireToRenderDependency;
VkImageMemoryBarrier2 renderToPresentBarrier;
VkDependencyInfo renderToPresentDependency;
void init(RenderDevice const& renderDevice);
void cleanup(RenderDevice const& renderDevice);
};
struct AppState
{
SDL_Window* window;
std::unique_ptr<RenderDevice> renderDevice;
MiscData miscData;
[[nodiscard]] bool isInit() const;
void cleanup();
AppState();
AppState(AppState const& other) = delete;
AppState(AppState&& other) noexcept = delete;
AppState& operator=(AppState const& other) = delete;
AppState& operator=(AppState&& other) noexcept = delete;
~AppState();
};
static_assert(sizeof(AppState) < 1024);
bool AppState::isInit() const
{
return window and renderDevice and renderDevice->isInit();
} }
void AppState::cleanup() constexpr size_t operator ""_MiB(size_t const value)
{ {
if (!isInit()) return; return value * 1024_KiB;
renderDevice->waitIdle();
miscData.cleanup(*renderDevice);
renderDevice->cleanup();
SDL_DestroyWindow(window);
} }
AppState::AppState() constexpr size_t operator ""_GiB(size_t const value)
{ {
window = SDL_CreateWindow("Blaze Test", WIDTH, HEIGHT, SDL_WINDOW_VULKAN); return value * 1024_MiB;
if (!window)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s", SDL_GetError());
abort();
} }
renderDevice = std::make_unique<RenderDevice>(RenderDevice::CreateInfo{ .window = window }); namespace Blaze::Global
if (!renderDevice->isInit())
{ {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "RenderDevice failed to init"); GlobalMemory g_Memory;
abort();
}
miscData.init(*renderDevice);
}
AppState::~AppState()
{
cleanup();
} }
SDL_AppResult SDL_AppInit(void** pAppState, int, char**) SDL_AppResult SDL_AppInit(void** pAppState, int, char**)
{ {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS); SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
*pAppState = new AppState{}; Blaze::Global::g_Memory.init(128_MiB);
*pAppState = CreateAppState(&Blaze::Global::g_Memory, WIDTH, HEIGHT);
if (!*pAppState)
return SDL_APP_FAILURE;
AppState& appState = *static_cast<AppState*>(*pAppState); AppState& appState = *static_cast<AppState*>(*pAppState);
if (!appState.isInit()) if (!appState.isInit())
@ -119,7 +73,7 @@ SDL_AppResult SDL_AppIterate(void* appstate)
{ {
AppState& appState = *static_cast<AppState*>(appstate); AppState& appState = *static_cast<AppState*>(appstate);
RenderDevice& renderDevice = *appState.renderDevice; RenderDevice& renderDevice = *appState.renderDevice;
MiscData& misc = appState.miscData; MiscData& misc = *appState.miscData;
Frame& currentFrame = renderDevice.frames[renderDevice.frameIndex]; Frame& currentFrame = renderDevice.frames[renderDevice.frameIndex];
VK_CHECK(vkWaitForFences(renderDevice.device, 1, &currentFrame.frameReadyToReuse, VK_TRUE, std::numeric_limits<uint32_t>::max())); VK_CHECK(vkWaitForFences(renderDevice.device, 1, &currentFrame.frameReadyToReuse, VK_TRUE, std::numeric_limits<uint32_t>::max()));
@ -251,288 +205,5 @@ void SDL_AppQuit(void* appstate, SDL_AppResult)
appState->cleanup(); appState->cleanup();
// Not necessary, this memory will be winked out. Blaze::Global::g_Memory.destroy();
delete appState;
}
void MiscData::init(RenderDevice const& renderDevice)
{
VkDevice const device = renderDevice.device;
{
size_t dataSize;
void* rawData = SDL_LoadFile("Triangle.spv", &dataSize);
ASSERT(dataSize % 4 == 0);
if (not rawData)
{
SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "%s", SDL_GetError());
abort();
}
auto data = static_cast<uint32_t const*>(rawData);
VkShaderModuleCreateInfo const shaderModuleCreateInfo = {
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.codeSize = dataSize,
.pCode = data,
};
VkShaderModule shaderModule;
VK_CHECK(vkCreateShaderModule(device, &shaderModuleCreateInfo, nullptr, &shaderModule));
VkPipelineLayoutCreateInfo constexpr pipelineLayoutCreateInfo = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.setLayoutCount = 0,
.pSetLayouts = nullptr,
.pushConstantRangeCount = 0,
.pPushConstantRanges = nullptr,
};
VK_CHECK(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &pipelineLayout));
std::array stages = {
VkPipelineShaderStageCreateInfo{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.stage = VK_SHADER_STAGE_VERTEX_BIT,
.module = shaderModule,
.pName = "VertexMain",
.pSpecializationInfo = nullptr,
},
VkPipelineShaderStageCreateInfo{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.stage = VK_SHADER_STAGE_FRAGMENT_BIT,
.module = shaderModule,
.pName = "FragmentMain",
.pSpecializationInfo = nullptr,
}
};
VkPipelineVertexInputStateCreateInfo constexpr vertexInputState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.vertexBindingDescriptionCount = 0,
.pVertexBindingDescriptions = nullptr,
.vertexAttributeDescriptionCount = 0,
.pVertexAttributeDescriptions = nullptr,
};
VkPipelineInputAssemblyStateCreateInfo constexpr inputAssembly = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
.primitiveRestartEnable = VK_FALSE,
};
VkPipelineTessellationStateCreateInfo constexpr tessellationState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.patchControlPoints = 0,
};
VkPipelineViewportStateCreateInfo constexpr viewportState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.viewportCount = 1,
.pViewports = nullptr,
.scissorCount = 1,
.pScissors = nullptr,
};
VkPipelineRasterizationStateCreateInfo constexpr rasterizationState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.depthClampEnable = VK_TRUE,
.rasterizerDiscardEnable = VK_FALSE,
.polygonMode = VK_POLYGON_MODE_FILL,
.cullMode = VK_CULL_MODE_NONE,
.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
.depthBiasEnable = VK_FALSE,
.depthBiasConstantFactor = 0.0f,
.depthBiasClamp = 0.0f,
.depthBiasSlopeFactor = 0.0f,
.lineWidth = 1.0f,
};
VkPipelineMultisampleStateCreateInfo constexpr multisampleState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT,
.sampleShadingEnable = VK_FALSE,
.minSampleShading = 0.0f,
.pSampleMask = nullptr,
.alphaToCoverageEnable = VK_FALSE,
.alphaToOneEnable = VK_FALSE,
};
VkPipelineDepthStencilStateCreateInfo constexpr depthStencilState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.depthTestEnable = VK_FALSE,
.depthWriteEnable = VK_FALSE,
.depthCompareOp = VK_COMPARE_OP_ALWAYS,
.depthBoundsTestEnable = VK_FALSE,
.stencilTestEnable = VK_FALSE,
.front = {},
.back = {},
.minDepthBounds = 0.0f,
.maxDepthBounds = 1.0f,
};
VkPipelineColorBlendAttachmentState constexpr colorBlendAttachmentState = {
.blendEnable = VK_FALSE,
.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA,
.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
.colorBlendOp = VK_BLEND_OP_ADD,
.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE,
.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO,
.alphaBlendOp = VK_BLEND_OP_ADD,
.colorWriteMask = VK_COLOR_COMPONENT_R_BIT
| VK_COLOR_COMPONENT_G_BIT
| VK_COLOR_COMPONENT_B_BIT
| VK_COLOR_COMPONENT_A_BIT,
};
VkPipelineColorBlendStateCreateInfo const colorBlendState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.logicOpEnable = VK_FALSE,
.logicOp = VK_LOGIC_OP_COPY,
.attachmentCount = 1,
.pAttachments = &colorBlendAttachmentState,
.blendConstants = {0.0f, 0.0f, 0.0f, 0.0f},
};
std::array constexpr dynamicStates = {
VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR
};
VkPipelineDynamicStateCreateInfo const dynamicStateCreateInfo = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.dynamicStateCount = static_cast<uint32_t>(dynamicStates.size()),
.pDynamicStates = dynamicStates.data()
};
VkPipelineRenderingCreateInfoKHR const renderingCreateInfo = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR,
.colorAttachmentCount = 1,
.pColorAttachmentFormats = &renderDevice.swapchainFormat,
};
VkGraphicsPipelineCreateInfo const graphicsPipelineCreateInfo = {
.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
.pNext = &renderingCreateInfo,
.flags = 0,
.stageCount = static_cast<uint32_t>(stages.size()),
.pStages = stages.data(),
.pVertexInputState = &vertexInputState,
.pInputAssemblyState = &inputAssembly,
.pTessellationState = &tessellationState,
.pViewportState = &viewportState,
.pRasterizationState = &rasterizationState,
.pMultisampleState = &multisampleState,
.pDepthStencilState = &depthStencilState,
.pColorBlendState = &colorBlendState,
.pDynamicState = &dynamicStateCreateInfo,
.layout = pipelineLayout,
.renderPass = nullptr,
.subpass = 0,
.basePipelineHandle = nullptr,
.basePipelineIndex = 0,
};
VK_CHECK(vkCreateGraphicsPipelines(device, nullptr, 1, &graphicsPipelineCreateInfo, nullptr, &trianglePipeline));
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,
};
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)
{
VkDevice const device = renderDevice.device;
vkDestroyPipeline(device, trianglePipeline, nullptr);
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
} }

View File

@ -148,8 +148,11 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="AppState.cpp" />
<ClCompile Include="Blaze.cpp" /> <ClCompile Include="Blaze.cpp" />
<ClCompile Include="Frame.cpp" /> <ClCompile Include="Frame.cpp" />
<ClCompile Include="GlobalMemory.cpp" />
<ClCompile Include="MiscData.cpp" />
<ClCompile Include="RenderDevice.cpp" /> <ClCompile Include="RenderDevice.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -176,10 +179,13 @@
<None Include="vcpkg.json" /> <None Include="vcpkg.json" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="AppState.h" />
<ClInclude Include="Frame.h" /> <ClInclude Include="Frame.h" />
<ClInclude Include="GlobalMemory.h" />
<ClInclude Include="MemoryUtils.h" /> <ClInclude Include="MemoryUtils.h" />
<ClInclude Include="MacroUtils.h" /> <ClInclude Include="MacroUtils.h" />
<ClInclude Include="MathUtil.h" /> <ClInclude Include="MathUtil.h" />
<ClInclude Include="MiscData.h" />
<ClInclude Include="RenderDevice.h" /> <ClInclude Include="RenderDevice.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@ -27,6 +27,15 @@
<ClCompile Include="RenderDevice.cpp"> <ClCompile Include="RenderDevice.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="AppState.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MiscData.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="GlobalMemory.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="vcpkg.json"> <None Include="vcpkg.json">
@ -66,5 +75,14 @@
<ClInclude Include="MemoryUtils.h"> <ClInclude Include="MemoryUtils.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="AppState.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MiscData.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="GlobalMemory.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -10,15 +10,13 @@ bool Frame::isInit() const
return static_cast<bool>(commandPool); return static_cast<bool>(commandPool);
} }
Frame::Frame(RenderDevice const& renderDevice) Frame::Frame(VkDevice const device, uint32_t const directQueueFamilyIndex)
{ {
VkDevice const device = renderDevice.device;
VkCommandPoolCreateInfo const commandPoolCreateInfo = { VkCommandPoolCreateInfo const commandPoolCreateInfo = {
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.pNext = nullptr, .pNext = nullptr,
.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, .flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT,
.queueFamilyIndex = renderDevice.directQueueFamilyIndex, .queueFamilyIndex = directQueueFamilyIndex,
}; };
VK_CHECK(vkCreateCommandPool(device, &commandPoolCreateInfo, nullptr, &commandPool)); VK_CHECK(vkCreateCommandPool(device, &commandPoolCreateInfo, nullptr, &commandPool));

View File

@ -15,11 +15,10 @@ struct Frame
[[nodiscard]] bool isInit() const; [[nodiscard]] bool isInit() const;
explicit Frame(RenderDevice const& renderDevice); Frame(VkDevice device, uint32_t directQueueFamilyIndex);
void cleanup(RenderDevice const& renderDevice); void cleanup(RenderDevice const& renderDevice);
~Frame(); ~Frame();
}; };

65
GlobalMemory.cpp Normal file
View File

@ -0,0 +1,65 @@
#include "GlobalMemory.h"
#include <SDL3/SDL_log.h>
void GlobalMemory::init(size_t const size)
{
memory = new Byte[size];
capacity = size;
available = size;
}
void GlobalMemory::destroy()
{
Byte const* originalMemory = memory - (capacity - available);
delete[] originalMemory;
memory = nullptr;
available = 0;
capacity = 0;
}
Byte* GlobalMemory::allocate(size_t const size)
{
assert(size <= available && "No enough space available");
Byte* retVal = memory;
memory += size;
available -= size;
SDL_LogInfo(SDL_LOG_CATEGORY_SYSTEM, "ALLOC: %p -> %p (%llu) (avail: %llu)", reinterpret_cast<void*>(retVal), reinterpret_cast<void*>(memory), size, available);
return retVal;
}
Byte* GlobalMemory::allocate(size_t const size, size_t const alignment)
{
uintptr_t const addr = reinterpret_cast<uintptr_t>(memory);
uintptr_t const foundOffset = addr % alignment;
if (foundOffset == 0)
{
return allocate(size);
}
uintptr_t const offset = alignment - foundOffset;
size_t const allocationSize = size + offset;
return offset + allocate(allocationSize);
}
GlobalMemory::State GlobalMemory::getState() const
{
SDL_LogInfo(SDL_LOG_CATEGORY_SYSTEM, "TEMP: %p %llu", reinterpret_cast<void*>(memory), available);
return {
.memory = memory,
.available = available,
};
}
void GlobalMemory::restoreState(State const& state)
{
assert(memory >= state.memory); //< Behind top of allocator
assert(memory - (capacity - available) <= state.memory); //< Ahead of start of allocator
SDL_LogInfo(SDL_LOG_CATEGORY_SYSTEM, "RESTORE: %p %llu", reinterpret_cast<void*>(memory), available);
memory = state.memory;
available = state.available;
}

34
GlobalMemory.h Normal file
View File

@ -0,0 +1,34 @@
#pragma once
#include <cstdint>
#include "MacroUtils.h"
using Byte = uint8_t;
struct GlobalMemory
{
struct State
{
Byte* memory;
size_t available;
};
Byte* memory;
size_t available;
size_t capacity;
void init(size_t const size);
void destroy();
Byte* allocate(size_t const size);
Byte* allocate(size_t const size, size_t const alignment);
// Do not do any permanent allocations after calling this.
[[nodiscard]] State getState() const;
// Call this before permanent allocations.
void restoreState(State const& state);
};

290
MiscData.cpp Normal file
View File

@ -0,0 +1,290 @@
#include "MiscData.h"
#include <array>
#include <SDL3/SDL_log.h>
#include "MacroUtils.h"
#include "RenderDevice.h"
void MiscData::init(RenderDevice const& renderDevice)
{
VkDevice const device = renderDevice.device;
{
size_t dataSize;
void* rawData = SDL_LoadFile("Triangle.spv", &dataSize);
ASSERT(dataSize % 4 == 0);
if (not rawData)
{
SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "%s", SDL_GetError());
abort();
}
auto data = static_cast<uint32_t const*>(rawData);
VkShaderModuleCreateInfo const shaderModuleCreateInfo = {
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.codeSize = dataSize,
.pCode = data,
};
VkShaderModule shaderModule;
VK_CHECK(vkCreateShaderModule(device, &shaderModuleCreateInfo, nullptr, &shaderModule));
VkPipelineLayoutCreateInfo constexpr pipelineLayoutCreateInfo = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.setLayoutCount = 0,
.pSetLayouts = nullptr,
.pushConstantRangeCount = 0,
.pPushConstantRanges = nullptr,
};
VK_CHECK(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &pipelineLayout));
std::array stages = {
VkPipelineShaderStageCreateInfo{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.stage = VK_SHADER_STAGE_VERTEX_BIT,
.module = shaderModule,
.pName = "VertexMain",
.pSpecializationInfo = nullptr,
},
VkPipelineShaderStageCreateInfo{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.stage = VK_SHADER_STAGE_FRAGMENT_BIT,
.module = shaderModule,
.pName = "FragmentMain",
.pSpecializationInfo = nullptr,
}
};
VkPipelineVertexInputStateCreateInfo constexpr vertexInputState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.vertexBindingDescriptionCount = 0,
.pVertexBindingDescriptions = nullptr,
.vertexAttributeDescriptionCount = 0,
.pVertexAttributeDescriptions = nullptr,
};
VkPipelineInputAssemblyStateCreateInfo constexpr inputAssembly = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
.primitiveRestartEnable = VK_FALSE,
};
VkPipelineTessellationStateCreateInfo constexpr tessellationState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.patchControlPoints = 0,
};
VkPipelineViewportStateCreateInfo constexpr viewportState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.viewportCount = 1,
.pViewports = nullptr,
.scissorCount = 1,
.pScissors = nullptr,
};
VkPipelineRasterizationStateCreateInfo constexpr rasterizationState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.depthClampEnable = VK_TRUE,
.rasterizerDiscardEnable = VK_FALSE,
.polygonMode = VK_POLYGON_MODE_FILL,
.cullMode = VK_CULL_MODE_NONE,
.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
.depthBiasEnable = VK_FALSE,
.depthBiasConstantFactor = 0.0f,
.depthBiasClamp = 0.0f,
.depthBiasSlopeFactor = 0.0f,
.lineWidth = 1.0f,
};
VkPipelineMultisampleStateCreateInfo constexpr multisampleState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT,
.sampleShadingEnable = VK_FALSE,
.minSampleShading = 0.0f,
.pSampleMask = nullptr,
.alphaToCoverageEnable = VK_FALSE,
.alphaToOneEnable = VK_FALSE,
};
VkPipelineDepthStencilStateCreateInfo constexpr depthStencilState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.depthTestEnable = VK_FALSE,
.depthWriteEnable = VK_FALSE,
.depthCompareOp = VK_COMPARE_OP_ALWAYS,
.depthBoundsTestEnable = VK_FALSE,
.stencilTestEnable = VK_FALSE,
.front = {},
.back = {},
.minDepthBounds = 0.0f,
.maxDepthBounds = 1.0f,
};
VkPipelineColorBlendAttachmentState constexpr colorBlendAttachmentState = {
.blendEnable = VK_FALSE,
.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA,
.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
.colorBlendOp = VK_BLEND_OP_ADD,
.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE,
.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO,
.alphaBlendOp = VK_BLEND_OP_ADD,
.colorWriteMask = VK_COLOR_COMPONENT_R_BIT
| VK_COLOR_COMPONENT_G_BIT
| VK_COLOR_COMPONENT_B_BIT
| VK_COLOR_COMPONENT_A_BIT,
};
VkPipelineColorBlendStateCreateInfo const colorBlendState = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.logicOpEnable = VK_FALSE,
.logicOp = VK_LOGIC_OP_COPY,
.attachmentCount = 1,
.pAttachments = &colorBlendAttachmentState,
.blendConstants = {0.0f, 0.0f, 0.0f, 0.0f},
};
std::array constexpr dynamicStates = {
VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR
};
VkPipelineDynamicStateCreateInfo const dynamicStateCreateInfo = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.dynamicStateCount = static_cast<uint32_t>(dynamicStates.size()),
.pDynamicStates = dynamicStates.data()
};
VkPipelineRenderingCreateInfoKHR const renderingCreateInfo = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR,
.colorAttachmentCount = 1,
.pColorAttachmentFormats = &renderDevice.swapchainFormat,
};
VkGraphicsPipelineCreateInfo const graphicsPipelineCreateInfo = {
.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
.pNext = &renderingCreateInfo,
.flags = 0,
.stageCount = static_cast<uint32_t>(stages.size()),
.pStages = stages.data(),
.pVertexInputState = &vertexInputState,
.pInputAssemblyState = &inputAssembly,
.pTessellationState = &tessellationState,
.pViewportState = &viewportState,
.pRasterizationState = &rasterizationState,
.pMultisampleState = &multisampleState,
.pDepthStencilState = &depthStencilState,
.pColorBlendState = &colorBlendState,
.pDynamicState = &dynamicStateCreateInfo,
.layout = pipelineLayout,
.renderPass = nullptr,
.subpass = 0,
.basePipelineHandle = nullptr,
.basePipelineIndex = 0,
};
VK_CHECK(vkCreateGraphicsPipelines(device, nullptr, 1, &graphicsPipelineCreateInfo, nullptr, &trianglePipeline));
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,
};
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)
{
VkDevice const device = renderDevice.device;
vkDestroyPipeline(device, trianglePipeline, nullptr);
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
}

19
MiscData.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <volk.h>
struct RenderDevice;
struct MiscData
{
VkPipelineLayout pipelineLayout;
VkPipeline trianglePipeline;
VkImageMemoryBarrier2 acquireToRenderBarrier;
VkDependencyInfo acquireToRenderDependency;
VkImageMemoryBarrier2 renderToPresentBarrier;
VkDependencyInfo renderToPresentDependency;
void init(RenderDevice const& renderDevice);
void cleanup(RenderDevice const& renderDevice);
};

View File

@ -5,16 +5,25 @@
#include <SDL3/SDL_log.h> #include <SDL3/SDL_log.h>
#include <array> #include <array>
#include <span>
#include "Frame.h" #include "Frame.h"
#include "GlobalMemory.h"
#include "MathUtil.h" #include "MathUtil.h"
RenderDevice::RenderDevice(CreateInfo const& createInfo) RenderDevice::~RenderDevice()
{
ASSERT(!isInit());
}
// TODO: Failure Handling
RenderDevice* CreateRenderDevice(GlobalMemory* mem, RenderDevice::CreateInfo const& createInfo)
{ {
ASSERT(createInfo.window); ASSERT(createInfo.window);
volkInitialize(); volkInitialize();
VkInstance instance;
// Create Instance // Create Instance
{ {
VkApplicationInfo constexpr applicationInfo = { VkApplicationInfo constexpr applicationInfo = {
@ -45,28 +54,35 @@ RenderDevice::RenderDevice(CreateInfo const& createInfo)
volkLoadInstance(instance); volkLoadInstance(instance);
} }
VkSurfaceKHR surface;
// Create Surface // Create Surface
ASSERT(SDL_Vulkan_CreateSurface(createInfo.window, instance, nullptr, &surface)); ASSERT(SDL_Vulkan_CreateSurface(createInfo.window, instance, nullptr, &surface));
VkPhysicalDevice physicalDeviceInUse = nullptr;
VkDevice device = nullptr;
uint32_t directQueueFamilyIndex = -1;
VkQueue directQueue = nullptr;
// Create Device and Queue // Create Device and Queue
{ {
auto tempAllocStart = mem->getState();
uint32_t physicalDeviceCount; uint32_t physicalDeviceCount;
VK_CHECK(vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, nullptr)); VK_CHECK(vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, nullptr));
SDL_Log("Found %u GPUs", physicalDeviceCount); SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "Found %u GPUs", physicalDeviceCount);
std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount); VkPhysicalDevice* physicalDevices = reinterpret_cast<VkPhysicalDevice*>(mem->allocate(sizeof(VkPhysicalDevice) * physicalDeviceCount));
VK_CHECK(vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, physicalDevices.data())); VK_CHECK(vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, physicalDevices));
for (VkPhysicalDevice const physicalDevice : std::span{ physicalDevices, physicalDeviceCount })
std::vector<VkQueueFamilyProperties> queueFamilyProperties;
for (VkPhysicalDevice const physicalDevice : physicalDevices)
{ {
auto tempAllocQueueProperties = mem->getState();
VkPhysicalDeviceProperties properties; VkPhysicalDeviceProperties properties;
vkGetPhysicalDeviceProperties(physicalDevice, &properties); vkGetPhysicalDeviceProperties(physicalDevice, &properties);
SDL_Log("GPU: %s", properties.deviceName); SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "GPU: %s", properties.deviceName);
SDL_Log("- API Version %d.%d.%d", SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "- API Version %d.%d.%d",
VK_API_VERSION_MAJOR(properties.apiVersion), VK_API_VERSION_MAJOR(properties.apiVersion),
VK_API_VERSION_MINOR(properties.apiVersion), VK_API_VERSION_MINOR(properties.apiVersion),
VK_API_VERSION_PATCH(properties.apiVersion)); VK_API_VERSION_PATCH(properties.apiVersion));
@ -84,31 +100,34 @@ RenderDevice::RenderDevice(CreateInfo const& createInfo)
uint32_t queueFamilyCount; uint32_t queueFamilyCount;
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, nullptr); vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, nullptr);
queueFamilyProperties.resize(queueFamilyCount); VkQueueFamilyProperties* queueFamilyProperties = reinterpret_cast<VkQueueFamilyProperties*>(mem->allocate(sizeof(VkQueueFamilyProperties) * queueFamilyCount));
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, queueFamilyProperties.data()); vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, queueFamilyProperties);
for (uint32_t queueFamilyIndex = 0; VkQueueFamilyProperties const qfp : queueFamilyProperties) for (uint32_t queueFamilyIndex = 0; queueFamilyIndex != queueFamilyCount;
++queueFamilyIndex)
{ {
VkQueueFamilyProperties const& qfp = queueFamilyProperties[queueFamilyIndex];
bool hasGraphicsSupport = false; bool hasGraphicsSupport = false;
bool hasComputeSupport = false; bool hasComputeSupport = false;
bool hasTransferSupport = false; bool hasTransferSupport = false;
bool hasPresentSupport = false; bool hasPresentSupport = false;
SDL_Log("- Queue [%d]", queueFamilyIndex); SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "- Queue [%d]", queueFamilyIndex);
if (qfp.queueFlags & VK_QUEUE_GRAPHICS_BIT) if (qfp.queueFlags & VK_QUEUE_GRAPHICS_BIT)
{ {
hasGraphicsSupport = true; hasGraphicsSupport = true;
SDL_Log("-- Graphic"); SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "-- Graphic");
} }
if (qfp.queueFlags & VK_QUEUE_COMPUTE_BIT) if (qfp.queueFlags & VK_QUEUE_COMPUTE_BIT)
{ {
hasComputeSupport = true; hasComputeSupport = true;
SDL_Log("-- Compute"); SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "-- Compute");
} }
if (qfp.queueFlags & VK_QUEUE_TRANSFER_BIT) if (qfp.queueFlags & VK_QUEUE_TRANSFER_BIT)
{ {
hasTransferSupport = true; hasTransferSupport = true;
SDL_Log("-- Transfer"); SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "-- Transfer");
} }
VkBool32 isSurfaceSupported; VkBool32 isSurfaceSupported;
@ -117,7 +136,7 @@ RenderDevice::RenderDevice(CreateInfo const& createInfo)
if (isSurfaceSupported) if (isSurfaceSupported)
{ {
hasPresentSupport = true; hasPresentSupport = true;
SDL_Log("-- Present"); SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "-- Present");
} }
if (hasGraphicsSupport and hasComputeSupport and hasTransferSupport and hasPresentSupport) if (hasGraphicsSupport and hasComputeSupport and hasTransferSupport and hasPresentSupport)
@ -127,8 +146,9 @@ RenderDevice::RenderDevice(CreateInfo const& createInfo)
break; break;
} }
++queueFamilyIndex;
} }
mem->restoreState(tempAllocQueueProperties);
} }
ASSERT(physicalDeviceInUse); ASSERT(physicalDeviceInUse);
@ -174,21 +194,30 @@ RenderDevice::RenderDevice(CreateInfo const& createInfo)
VK_CHECK(vkCreateDevice(physicalDeviceInUse, &deviceCreateInfo, nullptr, &device)); VK_CHECK(vkCreateDevice(physicalDeviceInUse, &deviceCreateInfo, nullptr, &device));
vkGetDeviceQueue(device, directQueueFamilyIndex, 0, &directQueue); vkGetDeviceQueue(device, directQueueFamilyIndex, 0, &directQueue);
mem->restoreState(tempAllocStart);
} }
// Swapchain creation // Swapchain creation
swapchainExtent = { createInfo.width, createInfo.height }; VkExtent2D swapchainExtent = { createInfo.width, createInfo.height };
VkFormat swapchainFormat = VK_FORMAT_UNDEFINED;
VkSwapchainKHR swapchain;
VkImage* swapchainImages;
VkImageView* swapchainViews;
uint32_t swapchainImageCount;
{ {
auto tempAllocStart = mem->getState();
VkSurfaceCapabilitiesKHR capabilities; VkSurfaceCapabilitiesKHR capabilities;
VK_CHECK(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDeviceInUse, surface, &capabilities)); VK_CHECK(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDeviceInUse, surface, &capabilities));
// Image Count Calculation // Image Count Calculation
uint32_t swapchainImageCount = 3; swapchainImageCount = 3;
if (capabilities.maxImageCount > 0) if (capabilities.maxImageCount > 0)
{ {
swapchainImageCount = std::min(swapchainImageCount, capabilities.maxImageCount); swapchainImageCount = std::min(swapchainImageCount, capabilities.maxImageCount);
} }
swapchainImageCount = std::max(swapchainImageCount, capabilities.minImageCount); swapchainImageCount = std::max(swapchainImageCount, capabilities.minImageCount + 1);
// Image Size calculation // Image Size calculation
{ {
@ -200,18 +229,18 @@ RenderDevice::RenderDevice(CreateInfo const& createInfo)
uint32_t surfaceFormatCount; uint32_t surfaceFormatCount;
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDeviceInUse, surface, &surfaceFormatCount, nullptr); vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDeviceInUse, surface, &surfaceFormatCount, nullptr);
std::vector<VkSurfaceFormatKHR> surfaceFormats(surfaceFormatCount); VkSurfaceFormatKHR* surfaceFormats = reinterpret_cast<VkSurfaceFormatKHR*>(mem->allocate(sizeof(VkSurfaceFormatKHR*) * surfaceFormatCount));
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDeviceInUse, surface, &surfaceFormatCount, surfaceFormats.data()); vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDeviceInUse, surface, &surfaceFormatCount, surfaceFormats);
VkSurfaceFormatKHR format = { VkSurfaceFormatKHR format = {
.format = VK_FORMAT_UNDEFINED, .format = VK_FORMAT_UNDEFINED,
.colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR, .colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR,
}; };
for (auto& surfaceFormat : surfaceFormats) for (auto& surfaceFormat : std::span{ surfaceFormats, surfaceFormatCount })
{ {
if (surfaceFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) if (surfaceFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
{ {
SDL_Log("Color Space SRGB Found %d", surfaceFormat.format); SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "Color Space SRGB Found %d", surfaceFormat.format);
if (surfaceFormat.format == VK_FORMAT_R8G8B8A8_SRGB) { if (surfaceFormat.format == VK_FORMAT_R8G8B8A8_SRGB) {
format = surfaceFormat; format = surfaceFormat;
break; break;
@ -230,11 +259,11 @@ RenderDevice::RenderDevice(CreateInfo const& createInfo)
uint32_t presentModeCount; uint32_t presentModeCount;
vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDeviceInUse, surface, &presentModeCount, nullptr); vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDeviceInUse, surface, &presentModeCount, nullptr);
std::vector<VkPresentModeKHR> presentModes(presentModeCount); VkPresentModeKHR* presentModes = reinterpret_cast<VkPresentModeKHR*>(mem->allocate(sizeof(VkPresentModeKHR*) * presentModeCount));
vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDeviceInUse, surface, &presentModeCount, presentModes.data()); vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDeviceInUse, surface, &presentModeCount, presentModes);
VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR; VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR;
for (VkPresentModeKHR presentModeIter : presentModes) for (VkPresentModeKHR presentModeIter : std::span{ presentModes, presentModeCount })
{ {
if (presentModeIter == VK_PRESENT_MODE_FIFO_RELAXED_KHR) if (presentModeIter == VK_PRESENT_MODE_FIFO_RELAXED_KHR)
{ {
@ -248,6 +277,8 @@ RenderDevice::RenderDevice(CreateInfo const& createInfo)
} }
} }
mem->restoreState(tempAllocStart);
VkSwapchainCreateInfoKHR const swapchainCreateInfo = { VkSwapchainCreateInfoKHR const swapchainCreateInfo = {
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
.pNext = nullptr, .pNext = nullptr,
@ -273,18 +304,16 @@ RenderDevice::RenderDevice(CreateInfo const& createInfo)
swapchainImageCount = 0; swapchainImageCount = 0;
vkGetSwapchainImagesKHR(device, swapchain, &swapchainImageCount, nullptr); vkGetSwapchainImagesKHR(device, swapchain, &swapchainImageCount, nullptr);
SDL_Log("%llu", swapchainImages.size()); swapchainImages = reinterpret_cast<VkImage*>(mem->allocate(sizeof(VkImage) * swapchainImageCount));
swapchainImages.resize(swapchainImageCount); vkGetSwapchainImagesKHR(device, swapchain, &swapchainImageCount, swapchainImages);
vkGetSwapchainImagesKHR(device, swapchain, &swapchainImageCount, swapchainImages.data());
swapchainViews.reserve(swapchainImageCount);
for (auto& image : swapchainImages) {
swapchainViews = reinterpret_cast<VkImageView*>(mem->allocate(sizeof(VkImageView) * swapchainImageCount));
for (uint32_t i = 0; i != swapchainImageCount; ++i) {
VkImageViewCreateInfo const viewCreateInfo = { VkImageViewCreateInfo const viewCreateInfo = {
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.pNext = nullptr, .pNext = nullptr,
.flags = 0, .flags = 0,
.image = image, .image = swapchainImages[i],
.viewType = VK_IMAGE_VIEW_TYPE_2D, .viewType = VK_IMAGE_VIEW_TYPE_2D,
.format = format.format, .format = format.format,
.components = { .components = {
@ -302,26 +331,24 @@ RenderDevice::RenderDevice(CreateInfo const& createInfo)
} }
}; };
VkImageView view; VK_CHECK(vkCreateImageView(device, &viewCreateInfo, nullptr, &swapchainViews[i]));
VK_CHECK(vkCreateImageView(device, &viewCreateInfo, nullptr, &view));
swapchainViews.push_back(view);
} }
} }
// Init frames. // Init frames.
Frame* frames = reinterpret_cast<Frame*>(mem->allocate(sizeof(Frame) * swapchainImageCount));
for (uint32_t i = 0; i != swapchainImageCount; ++i)
{ {
auto count = static_cast<uint32_t>(swapchainImages.size()); new (frames + i) Frame(device, directQueueFamilyIndex);
frames.reserve(count);
for (uint32_t i = 0; i != count; ++i)
{
frames.emplace_back(*this);
}
}
} }
RenderDevice::~RenderDevice() Byte* allocation = mem->allocate(sizeof(RenderDevice), alignof(RenderDevice));
{ return new (allocation) RenderDevice{
cleanup(); instance, surface, physicalDeviceInUse,
device, directQueue, directQueueFamilyIndex,
swapchainFormat, swapchainExtent, swapchain, swapchainImages, swapchainViews, frames, swapchainImageCount,
};
} }
inline bool RenderDevice::isInit() const inline bool RenderDevice::isInit() const
@ -334,18 +361,15 @@ void RenderDevice::cleanup()
if (not isInit()) if (not isInit())
return; return;
for (Frame& frame : frames) for (Frame& frame : std::span{ frames, swapchainImageCount })
{ {
frame.cleanup(*this); frame.cleanup(*this);
} }
frames.clear();
for (auto const& view : swapchainViews) for (auto const& view : std::span{ swapchainViews, swapchainImageCount })
{ {
vkDestroyImageView(device, view, nullptr); vkDestroyImageView(device, view, nullptr);
} }
swapchainViews.clear();
swapchainImages.clear();
vkDestroySwapchainKHR(device, Take(swapchain), nullptr); vkDestroySwapchainKHR(device, Take(swapchain), nullptr);
@ -365,5 +389,25 @@ void RenderDevice::waitIdle() const
uint32_t RenderDevice::getNumFrames() const uint32_t RenderDevice::getNumFrames() const
{ {
return static_cast<uint32_t>(frames.size()); return swapchainImageCount;
}
RenderDevice::RenderDevice(VkInstance const instance, VkSurfaceKHR const surface, VkPhysicalDevice const physicalDeviceInUse,
VkDevice const device, VkQueue const directQueue, uint32_t const directQueueFamilyIndex, VkFormat const swapchainFormat,
VkExtent2D const swapchainExtent, VkSwapchainKHR const swapchain, VkImage* swapchainImages, VkImageView* swapchainViews,
Frame* frames, uint32_t const swapchainImageCount)
: instance{ instance }
, surface{ surface }
, physicalDeviceInUse{ physicalDeviceInUse }
, device{ device }
, directQueue{ directQueue }
, directQueueFamilyIndex{ directQueueFamilyIndex }
, swapchainFormat{ swapchainFormat }
, swapchainExtent{ swapchainExtent }
, swapchain{ swapchain }
, swapchainImages{ swapchainImages }
, swapchainViews{ swapchainViews }
, frames{ frames }
, swapchainImageCount{ swapchainImageCount }
{
} }

View File

@ -2,11 +2,10 @@
#include <volk.h> #include <volk.h>
#include <vector>
#include <SDL3/SDL_video.h> #include <SDL3/SDL_video.h>
#include <SDL3/SDL_vulkan.h> #include <SDL3/SDL_vulkan.h>
struct GlobalMemory;
struct Frame; struct Frame;
/// The Rendering backend abstraction /// The Rendering backend abstraction
@ -33,10 +32,10 @@ struct RenderDevice
VkFormat swapchainFormat; VkFormat swapchainFormat;
VkExtent2D swapchainExtent; VkExtent2D swapchainExtent;
VkSwapchainKHR swapchain; VkSwapchainKHR swapchain;
std::vector<VkImage> swapchainImages; VkImage* swapchainImages;
std::vector<VkImageView> swapchainViews; VkImageView* swapchainViews;
Frame* frames;
std::vector<Frame> frames; uint32_t swapchainImageCount;
uint32_t frameIndex = 0; uint32_t frameIndex = 0;
[[nodiscard]] bool isInit() const; [[nodiscard]] bool isInit() const;
@ -44,7 +43,24 @@ struct RenderDevice
void waitIdle() const; void waitIdle() const;
[[nodiscard]] uint32_t getNumFrames() const; [[nodiscard]] uint32_t getNumFrames() const;
explicit RenderDevice(CreateInfo const& createInfo); RenderDevice(
VkInstance instance,
VkSurfaceKHR surface,
VkPhysicalDevice physicalDeviceInUse,
VkDevice device,
VkQueue directQueue,
uint32_t directQueueFamilyIndex,
// TODO: Pack?
VkFormat swapchainFormat,
VkExtent2D swapchainExtent,
VkSwapchainKHR swapchain,
VkImage* swapchainImages,
VkImageView* swapchainViews,
Frame* frames,
uint32_t swapchainImageCount
);
RenderDevice(RenderDevice const&) = delete; RenderDevice(RenderDevice const&) = delete;
RenderDevice& operator=(RenderDevice const&) = delete; RenderDevice& operator=(RenderDevice const&) = delete;
@ -54,3 +70,5 @@ struct RenderDevice
~RenderDevice(); ~RenderDevice();
}; };
RenderDevice* CreateRenderDevice(GlobalMemory* mem, RenderDevice::CreateInfo const& createInfo);