Reformatted + Frame_Create.

This commit is contained in:
Anish Bhobe 2025-06-14 00:29:22 +02:00
parent fd9ceae67d
commit d09f725803
17 changed files with 754 additions and 669 deletions

72
.clang-format Normal file
View File

@ -0,0 +1,72 @@
---
BasedOnStyle: Microsoft
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveAssignments:
Enabled: true
AcrossEmptyLines: true
AlignFunctionPointers: true
AlignConsecutiveDeclarations:
Enabled: true
AcrossEmptyLines: true
AlignCompound: true
AlignFunctionPointers: true
PadOperators: true
AlignOperands: true
AlignTrailingComments:
OverEmptyLines: 2
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: true
BeforeWhile: true
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakConstructorInitializers: BeforeComma
ConstructorInitializerIndentWidth: 2
Cpp11BracedListStyle: false
IncludeCategories:
- Regex: ^<.*
Priority: 1
- Regex: ^".*
Priority: 2
- Regex: .*
Priority: 3
IncludeIsMainRegex: ([-_](test|unittest))?$
IndentCaseLabels: true
IndentWidth: 2
InsertNewlineAtEOF: true
MaxEmptyLinesToKeep: 2
PointerAlignment: Left
SpaceBeforeParensOptions:
AfterOverloadedOperator: true
SpacesInAngles: false
SpacesInParens: Custom
SpacesInParensOptions:
InConditionalStatements: true
InCStyleCasts: true
Other: true
TabWidth: 2
Language: Cpp
ConstructorInitializerAllOnOneLineOrOnePerLine: true
SpaceInEmptyParentheses: false
SpacesInConditionalStatement: true
SpacesInCStyleCastParentheses: false
AlignArrayOfStructures: Right

View File

@ -24,31 +24,26 @@ void AppState::destroy()
} }
AppState::AppState( SDL_Window* window, RenderDevice* renderDevice, MiscData* miscData ) AppState::AppState( SDL_Window* window, RenderDevice* renderDevice, MiscData* miscData )
: window{ window } : window{ window }, renderDevice{ renderDevice }, miscData{ miscData }, sprintfBuffer{ 0 }
, renderDevice{ renderDevice } {}
, miscData{ miscData } {}
AppState* CreateAppState( GlobalMemory* memory, uint32_t const width, uint32_t const height ) AppState* AppState_Create( GlobalMemory* memory, uint32_t const width, uint32_t const height )
{ {
SDL_Window* window = SDL_CreateWindow( SDL_Window* window =
"Blaze Test", SDL_CreateWindow( "Blaze Test", static_cast<int>( width ), static_cast<int>( height ), SDL_WINDOW_VULKAN );
static_cast<int>(width),
static_cast<int>(height),
SDL_WINDOW_VULKAN );
if ( !window ) if ( !window )
{ {
SDL_LogError( SDL_LOG_CATEGORY_APPLICATION, "%s", SDL_GetError() ); SDL_LogError( SDL_LOG_CATEGORY_APPLICATION, "%s", SDL_GetError() );
return nullptr; return nullptr;
} }
auto state = memory->getState(); RenderDevice* renderDevice = RenderDevice_Create( memory, { .window = window } );
RenderDevice* renderDevice = CreateRenderDevice( memory, { .window = window } );
if ( !renderDevice->isInit() ) if ( !renderDevice->isInit() )
{ {
SDL_LogError( SDL_LOG_CATEGORY_APPLICATION, "RenderDevice failed to init" ); SDL_LogError( SDL_LOG_CATEGORY_APPLICATION, "RenderDevice failed to init" );
SDL_DestroyWindow( window );
return nullptr; return nullptr;
} }
(void)state;
auto* miscDataAllocation = memory->allocate( sizeof( MiscData ) ); auto* miscDataAllocation = memory->allocate( sizeof( MiscData ) );

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <memory> #include <cstdint>
struct SDL_Window; struct SDL_Window;
@ -13,9 +13,9 @@ struct AppState
SDL_Window* window; SDL_Window* window;
RenderDevice* renderDevice; RenderDevice* renderDevice;
MiscData* miscData; MiscData* miscData;
char sprintfBuffer[256];
[[nodiscard]] [[nodiscard]] bool isInit() const;
bool isInit() const;
void destroy(); void destroy();
AppState( SDL_Window* window, RenderDevice* renderDevice, MiscData* miscData ); AppState( SDL_Window* window, RenderDevice* renderDevice, MiscData* miscData );
@ -29,4 +29,4 @@ struct AppState
~AppState(); ~AppState();
}; };
AppState* CreateAppState( GlobalMemory* memory, uint32_t const width, uint32_t const height ); AppState* AppState_Create( GlobalMemory* memory, uint32_t width, uint32_t height );

View File

@ -9,68 +9,42 @@
#include <volk.h> #include <volk.h>
#define SDL_MAIN_USE_CALLBACKS 1 #define SDL_MAIN_USE_CALLBACKS 1
#include <memory>
#include <glm/ext/matrix_transform.hpp>
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_filesystem.h> #include <SDL3/SDL_filesystem.h>
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_vulkan.h> #include <SDL3/SDL_vulkan.h>
#include <glm/ext/matrix_transform.hpp>
#include <memory>
#include "AppState.h" #include "AppState.h"
#include "Frame.h" #include "Frame.h"
#include "GlobalMemory.h" #include "GlobalMemory.h"
#include "MacroUtils.h" #include "MacroUtils.h"
#include "MathUtil.h" #include "MathUtil.h"
#include "RenderDevice.h"
#include "MiscData.h" #include "MiscData.h"
#include "RenderDevice.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;
using Byte = uint8_t;
constexpr size_t operator ""_KiB( size_t const value )
{
return value * 1024;
}
constexpr size_t operator ""_MiB( size_t const value )
{
return value * 1024_KiB;
}
constexpr size_t operator ""_GiB( size_t const value )
{
return value * 1024_MiB;
}
namespace Blaze::Global namespace Blaze::Global
{ {
GlobalMemory g_Memory; GlobalMemory g_Memory;
} }
SDL_AppResult SDL_AppInit( void** pAppState, int, char** ) SDL_AppResult SDL_AppInit( void** appstate, int, char** )
{ {
SDL_Init( SDL_INIT_VIDEO | SDL_INIT_EVENTS ); SDL_Init( SDL_INIT_VIDEO | SDL_INIT_EVENTS );
Blaze::Global::g_Memory.init( 128_MiB ); Blaze::Global::g_Memory.init( 128_MiB );
*pAppState = CreateAppState( &Blaze::Global::g_Memory, WIDTH, HEIGHT ); *appstate = AppState_Create( &Blaze::Global::g_Memory, WIDTH, HEIGHT );
if ( !*pAppState ) return SDL_APP_FAILURE; if ( !*appstate ) return SDL_APP_FAILURE;
AppState& appState = *static_cast<AppState*>(*pAppState);
if ( !appState.isInit() )
{
return SDL_APP_FAILURE;
}
return SDL_APP_CONTINUE; return SDL_APP_CONTINUE;
} }
char g_buf[1000];
SDL_AppResult SDL_AppIterate( void* appstate ) SDL_AppResult SDL_AppIterate( void* appstate )
{ {
AppState& appState = *static_cast<AppState*>( appstate ); AppState& appState = *static_cast<AppState*>( appstate );
@ -78,14 +52,11 @@ SDL_AppResult SDL_AppIterate( void* appstate )
MiscData& misc = *appState.miscData; MiscData& misc = *appState.miscData;
Frame& currentFrame = renderDevice.frames[renderDevice.frameIndex]; Frame& currentFrame = renderDevice.frames[renderDevice.frameIndex];
VK_CHECK( VK_CHECK( vkWaitForFences(
vkWaitForFences(renderDevice.device, renderDevice.device, 1, &currentFrame.frameReadyToReuse, VK_TRUE, std::numeric_limits<uint32_t>::max() ) );
1,
&currentFrame.frameReadyToReuse,
VK_TRUE,
std::numeric_limits<uint32_t>::max()) );
// All resources of frame 'frameIndex' are free. // All resources of frame 'frameIndex' are free.
// time calc
uint64_t const previousCounter = misc.previousCounter; uint64_t const previousCounter = misc.previousCounter;
uint64_t const currentCounter = SDL_GetPerformanceCounter(); uint64_t const currentCounter = SDL_GetPerformanceCounter();
uint64_t const deltaCount = currentCounter - previousCounter; uint64_t const deltaCount = currentCounter - previousCounter;
@ -93,10 +64,13 @@ SDL_AppResult SDL_AppIterate( void* appstate )
double const deltaTime = static_cast<double>( deltaCount ) / static_cast<double>( perfFreq ); double const deltaTime = static_cast<double>( deltaCount ) / static_cast<double>( perfFreq );
misc.previousCounter = currentCounter; misc.previousCounter = currentCounter;
{
double deltaTimeMs = deltaTime * 1000.0; double deltaTimeMs = deltaTime * 1000.0;
double fps = 1.0 / deltaTime; double fps = 1.0 / deltaTime;
auto _ = sprintf_s<1000>( g_buf, "%.2f fps %.5fms %llu -> %llu", fps, deltaTimeMs, previousCounter, currentCounter ); ( void )sprintf_s<256>(
SDL_SetWindowTitle( appState.window, g_buf ); appState.sprintfBuffer, "%.2f fps %.5fms %llu -> %llu", fps, deltaTimeMs, previousCounter, currentCounter );
SDL_SetWindowTitle( appState.window, appState.sprintfBuffer );
}
misc.cameraData.modelMatrix = glm::rotate( misc.cameraData.modelMatrix = glm::rotate(
misc.cameraData.modelMatrix, misc.cameraData.modelMatrix,
@ -105,9 +79,13 @@ SDL_AppResult SDL_AppIterate( void* appstate )
memcpy( misc.cameraUniformBufferPtr, &misc.cameraData, sizeof misc.cameraData ); memcpy( misc.cameraUniformBufferPtr, &misc.cameraData, sizeof misc.cameraData );
uint32_t currentImageIndex; uint32_t currentImageIndex;
VK_CHECK( VK_CHECK( vkAcquireNextImageKHR(
vkAcquireNextImageKHR(renderDevice.device, renderDevice.swapchain, std::numeric_limits<uint32_t>::max(), renderDevice.device,
currentFrame.imageAcquiredSemaphore, nullptr, &currentImageIndex) ); renderDevice.swapchain,
std::numeric_limits<uint32_t>::max(),
currentFrame.imageAcquiredSemaphore,
nullptr,
&currentImageIndex ) );
VK_CHECK( vkResetFences( renderDevice.device, 1, &currentFrame.frameReadyToReuse ) ); VK_CHECK( vkResetFences( renderDevice.device, 1, &currentFrame.frameReadyToReuse ) );
VK_CHECK( vkResetCommandPool( renderDevice.device, currentFrame.commandPool, 0 ) ); VK_CHECK( vkResetCommandPool( renderDevice.device, currentFrame.commandPool, 0 ) );
@ -179,14 +157,7 @@ SDL_AppResult SDL_AppIterate( void* appstate )
VkDeviceSize constexpr offset = 0; VkDeviceSize constexpr offset = 0;
vkCmdBindVertexBuffers( cmd, 0, 1, &misc.vertexBuffer, &offset ); vkCmdBindVertexBuffers( cmd, 0, 1, &misc.vertexBuffer, &offset );
vkCmdBindDescriptorSets( vkCmdBindDescriptorSets(
cmd, cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, misc.pipelineLayout, 0, 1, &misc.descriptorSet, 0, nullptr );
VK_PIPELINE_BIND_POINT_GRAPHICS,
misc.pipelineLayout,
0,
1,
&misc.descriptorSet,
0,
nullptr );
vkCmdDraw( cmd, static_cast<uint32_t>( misc.vertices.size() ), 1, 0, 0 ); vkCmdDraw( cmd, static_cast<uint32_t>( misc.vertices.size() ), 1, 0, 0 );
} }
vkCmdEndRendering( cmd ); vkCmdEndRendering( cmd );

View File

@ -157,6 +157,7 @@
<ClCompile Include="VmaImpl.cpp" /> <ClCompile Include="VmaImpl.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include=".clang-format" />
<None Include=".gitignore" /> <None Include=".gitignore" />
<None Include="PLAN.md"> <None Include="PLAN.md">
<SubType> <SubType>

View File

@ -56,6 +56,9 @@
<None Include="PLAN.md"> <None Include="PLAN.md">
<Filter>Resource Files</Filter> <Filter>Resource Files</Filter>
</None> </None>
<None Include=".clang-format">
<Filter>Resource Files</Filter>
</None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<CustomBuild Include="Mesh.slang"> <CustomBuild Include="Mesh.slang">

View File

@ -10,7 +10,45 @@ bool Frame::isInit() const
return static_cast<bool>( commandPool ); return static_cast<bool>( commandPool );
} }
Frame::Frame( VkDevice const device, uint32_t const directQueueFamilyIndex ) Frame::Frame(
VkCommandPool const commandPool,
VkCommandBuffer const commandBuffer,
VkSemaphore const imageAcquiredSemaphore,
VkSemaphore const renderFinishedSemaphore,
VkFence const frameReadyToReuse )
: commandPool{ commandPool }
, commandBuffer{ commandBuffer }
, imageAcquiredSemaphore{ imageAcquiredSemaphore }
, renderFinishedSemaphore{ renderFinishedSemaphore }
, frameReadyToReuse{ frameReadyToReuse }
{}
void Frame::destroy( RenderDevice const& renderDevice )
{
if ( !isInit() ) return;
VkDevice const device = renderDevice.device;
vkDestroyCommandPool( device, Take( commandPool ), nullptr );
vkDestroyFence( device, Take( frameReadyToReuse ), nullptr );
vkDestroySemaphore( device, Take( imageAcquiredSemaphore ), nullptr );
vkDestroySemaphore( device, Take( renderFinishedSemaphore ), nullptr );
}
Frame::~Frame()
{
// Manual Cleanup Required.
ASSERT( !isInit() );
}
void Frame_Create( Frame* frame, VkDevice const device, uint32_t const directQueueFamilyIndex )
{
VkCommandPool commandPool;
VkCommandBuffer commandBuffer;
VkSemaphore imageAcquiredSemaphore;
VkSemaphore renderFinishedSemaphore;
VkFence frameReadyToReuse;
{ {
VkCommandPoolCreateInfo const commandPoolCreateInfo = { VkCommandPoolCreateInfo const commandPoolCreateInfo = {
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
@ -32,7 +70,7 @@ Frame::Frame( VkDevice const device, uint32_t const directQueueFamilyIndex )
VkSemaphoreCreateInfo constexpr semaphoreCreateInfo = { VkSemaphoreCreateInfo constexpr semaphoreCreateInfo = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
.pNext = nullptr, .pNext = nullptr,
.flags = 0 .flags = 0,
}; };
VK_CHECK( vkCreateSemaphore( device, &semaphoreCreateInfo, nullptr, &imageAcquiredSemaphore ) ); VK_CHECK( vkCreateSemaphore( device, &semaphoreCreateInfo, nullptr, &imageAcquiredSemaphore ) );
VK_CHECK( vkCreateSemaphore( device, &semaphoreCreateInfo, nullptr, &renderFinishedSemaphore ) ); VK_CHECK( vkCreateSemaphore( device, &semaphoreCreateInfo, nullptr, &renderFinishedSemaphore ) );
@ -45,20 +83,9 @@ Frame::Frame( VkDevice const device, uint32_t const directQueueFamilyIndex )
VK_CHECK( vkCreateFence( device, &fenceCreateInfo, nullptr, &frameReadyToReuse ) ); VK_CHECK( vkCreateFence( device, &fenceCreateInfo, nullptr, &frameReadyToReuse ) );
} }
void Frame::destroy( RenderDevice const& renderDevice ) frame->commandPool = commandPool;
{ frame->commandBuffer = commandBuffer;
if ( !isInit() ) return; frame->imageAcquiredSemaphore = imageAcquiredSemaphore;
frame->renderFinishedSemaphore = renderFinishedSemaphore;
VkDevice const device = renderDevice.device; frame->frameReadyToReuse = frameReadyToReuse;
vkDestroyCommandPool( device, Take( commandPool ), nullptr );
vkDestroyFence( device, Take( frameReadyToReuse ), nullptr );
vkDestroySemaphore( device, Take( imageAcquiredSemaphore ), nullptr );
vkDestroySemaphore( device, Take( renderFinishedSemaphore ), nullptr );
}
Frame::~Frame()
{
// Manual Cleanup Required.
ASSERT( not isInit() );
} }

14
Frame.h
View File

@ -15,9 +15,21 @@ struct Frame
[[nodiscard]] bool isInit() const; [[nodiscard]] bool isInit() const;
Frame( VkDevice device, uint32_t directQueueFamilyIndex ); Frame(
VkCommandPool commandPool,
VkCommandBuffer commandBuffer,
VkSemaphore imageAcquiredSemaphore,
VkSemaphore renderFinishedSemaphore,
VkFence frameReadyToReuse );
void destroy( RenderDevice const& renderDevice ); void destroy( RenderDevice const& renderDevice );
Frame( Frame const& other ) = delete;
Frame( Frame&& other ) noexcept = delete;
Frame& operator=( Frame const& other ) = delete;
Frame& operator=( Frame&& other ) noexcept = delete;
~Frame(); ~Frame();
}; };
void Frame_Create( Frame* frame, VkDevice device, uint32_t directQueueFamilyIndex );

View File

@ -4,25 +4,26 @@
void GlobalMemory::init( size_t const size ) void GlobalMemory::init( size_t const size )
{ {
memory = new Byte[size]; memory = new std::byte[size];
capacity = size; capacity = size;
available = size; available = size;
} }
void GlobalMemory::destroy() void GlobalMemory::destroy()
{ {
Byte const* originalMemory = memory - (capacity - available); std::byte const* originalMemory = memory - ( capacity - available );
delete[] originalMemory; delete[] originalMemory;
memory = nullptr; memory = nullptr;
available = 0; available = 0;
capacity = 0; capacity = 0;
} }
Byte* GlobalMemory::allocate( size_t const size ) std::byte* GlobalMemory::allocate( size_t const size )
{ {
assert( size <= available && "No enough space available" ); assert( size <= available && "No enough space available" );
Byte* retVal = memory; std::byte* retVal = memory;
memory += size; memory += size;
available -= size; available -= size;
SDL_LogInfo( SDL_LogInfo(
@ -36,7 +37,7 @@ Byte* GlobalMemory::allocate( size_t const size )
return retVal; return retVal;
} }
Byte* GlobalMemory::allocate( size_t const size, size_t const alignment ) std::byte* GlobalMemory::allocate( size_t const size, size_t const alignment )
{ {
uintptr_t const addr = reinterpret_cast<uintptr_t>( memory ); uintptr_t const addr = reinterpret_cast<uintptr_t>( memory );
uintptr_t const foundOffset = addr % alignment; uintptr_t const foundOffset = addr % alignment;

View File

@ -4,31 +4,42 @@
#include "MacroUtils.h" #include "MacroUtils.h"
using Byte = uint8_t; consteval size_t operator""_KiB( size_t const value )
{
return value * 1024;
}
consteval size_t operator""_MiB( size_t const value )
{
return value * 1024_KiB;
}
consteval size_t operator""_GiB( size_t const value )
{
return value * 1024_MiB;
}
struct GlobalMemory struct GlobalMemory
{ {
struct State struct State
{ {
Byte* memory; std::byte* memory;
size_t available; size_t available;
}; };
Byte* memory; std::byte* memory;
size_t available; size_t available;
size_t capacity; size_t capacity;
void init( size_t size ); void init( size_t size );
void destroy(); void destroy();
Byte* allocate( size_t size ); [[nodiscard]]
std::byte* allocate( size_t size );
[[nodiscard]]
std::byte* allocate( size_t size, size_t alignment );
Byte* allocate( size_t size, size_t alignment ); [[nodiscard]]
State getState() const; //< Do not do any permanent allocations after calling this.
// Do not do any permanent allocations after calling this. void restoreState( State const& state ); //< Call this before permanent allocations.
[[nodiscard]] State getState() const;
// Call this before permanent allocations.
void restoreState( State const& state );
}; };

View File

@ -1,29 +1,34 @@
#pragma once #pragma once
#include <cassert> #include <cassert>
#include <cstdlib>
#include <utility> #include <utility>
#define G_ASSERT( COND ) \ #define G_ASSERT( COND ) \
do { \ do \
{ \
auto _result = ( COND ); \ auto _result = ( COND ); \
if (not _result) { \ if ( not _result ) \
{ \
__debugbreak(); \ __debugbreak(); \
assert( _result&& #COND ); \ assert( _result&& #COND ); \
} \ } \
} while(false) } \
while ( false )
#define ASSERT( COND ) G_ASSERT( COND ) #define ASSERT( COND ) G_ASSERT( COND )
#define VK_CHECK( RESULT ) \ #define VK_CHECK( RESULT ) \
do { \ do \
{ \
auto _result = ( RESULT ); \ auto _result = ( RESULT ); \
if (_result != VK_SUCCESS) { \ if ( _result != VK_SUCCESS ) \
SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, \ { \
"" #RESULT " failed with %d at %s:%d", \ SDL_LogError( SDL_LOG_CATEGORY_SYSTEM, "" #RESULT " failed with %d at %s:%d", _result, __FILE__, __LINE__ ); \
_result, __FILE__, __LINE__); \
__debugbreak(); \ __debugbreak(); \
assert(_result == VK_SUCCESS); \ exit( _result ); \
} \ } \
} while(false) } \
while ( false )
#define Take( OBJ ) std::exchange( OBJ, {} ) #define Take( OBJ ) std::exchange( OBJ, {} )

View File

@ -1,7 +1,7 @@
#include "MiscData.h" #include "MiscData.h"
#include <array>
#include <SDL3/SDL_log.h> #include <SDL3/SDL_log.h>
#include <array>
#include "MacroUtils.h" #include "MacroUtils.h"
#include "RenderDevice.h" #include "RenderDevice.h"
@ -25,9 +25,9 @@ void MiscData::init( RenderDevice const& renderDevice )
SDL_LogError( SDL_LOG_CATEGORY_SYSTEM, "%s", SDL_GetError() ); SDL_LogError( SDL_LOG_CATEGORY_SYSTEM, "%s", SDL_GetError() );
abort(); abort();
} }
uint32_t const* data = static_cast<uint32_t const*>( rawData );
auto data = static_cast<uint32_t const*>(rawData); // Create Shader Module
VkShaderModuleCreateInfo const shaderModuleCreateInfo = { VkShaderModuleCreateInfo const shaderModuleCreateInfo = {
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.pNext = nullptr, .pNext = nullptr,
@ -88,6 +88,7 @@ void MiscData::init( RenderDevice const& renderDevice )
} }
}; };
// Bindings
VkVertexInputBindingDescription constexpr bindingDescription = { VkVertexInputBindingDescription constexpr bindingDescription = {
.binding = 0, .binding = 0,
.stride = sizeof( Vertex ), .stride = sizeof( Vertex ),
@ -195,10 +196,8 @@ void MiscData::init( RenderDevice const& renderDevice )
.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE, .srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE,
.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO, .dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO,
.alphaBlendOp = VK_BLEND_OP_ADD, .alphaBlendOp = VK_BLEND_OP_ADD,
.colorWriteMask = VK_COLOR_COMPONENT_R_BIT .colorWriteMask =
| VK_COLOR_COMPONENT_G_BIT VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT,
| VK_COLOR_COMPONENT_B_BIT
| VK_COLOR_COMPONENT_A_BIT,
}; };
VkPipelineColorBlendStateCreateInfo const colorBlendState = { VkPipelineColorBlendStateCreateInfo const colorBlendState = {
@ -212,10 +211,7 @@ void MiscData::init( RenderDevice const& renderDevice )
.blendConstants = { 0.0f, 0.0f, 0.0f, 0.0f }, .blendConstants = { 0.0f, 0.0f, 0.0f, 0.0f },
}; };
std::array constexpr dynamicStates = { std::array constexpr dynamicStates = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR
};
VkPipelineDynamicStateCreateInfo const dynamicStateCreateInfo = { VkPipelineDynamicStateCreateInfo const dynamicStateCreateInfo = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
@ -308,8 +304,7 @@ void MiscData::init( RenderDevice const& renderDevice )
}; };
VmaAllocationCreateInfo constexpr allocationCreateInfo = { VmaAllocationCreateInfo constexpr allocationCreateInfo = {
.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT | .flags = VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
.usage = VMA_MEMORY_USAGE_AUTO, .usage = VMA_MEMORY_USAGE_AUTO,
.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, .requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
.preferredFlags = 0, .preferredFlags = 0,
@ -321,8 +316,7 @@ void MiscData::init( RenderDevice const& renderDevice )
VmaAllocationInfo allocationInfo; VmaAllocationInfo allocationInfo;
VK_CHECK( VK_CHECK( vmaCreateBuffer(
vmaCreateBuffer(
renderDevice.gpuAllocator, renderDevice.gpuAllocator,
&bufferCreateInfo, &bufferCreateInfo,
&allocationCreateInfo, &allocationCreateInfo,
@ -358,8 +352,7 @@ void MiscData::init( RenderDevice const& renderDevice )
}; };
VmaAllocationCreateInfo constexpr allocationCreateInfo = { VmaAllocationCreateInfo constexpr allocationCreateInfo = {
.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT | .flags = VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
.usage = VMA_MEMORY_USAGE_AUTO, .usage = VMA_MEMORY_USAGE_AUTO,
.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, .requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
.preferredFlags = 0, .preferredFlags = 0,
@ -371,8 +364,7 @@ void MiscData::init( RenderDevice const& renderDevice )
VmaAllocationInfo allocationInfo; VmaAllocationInfo allocationInfo;
VK_CHECK( VK_CHECK( vmaCreateBuffer(
vmaCreateBuffer(
renderDevice.gpuAllocator, renderDevice.gpuAllocator,
&bufferCreateInfo, &bufferCreateInfo,
&allocationCreateInfo, &allocationCreateInfo,
@ -438,6 +430,14 @@ void MiscData::init( RenderDevice const& renderDevice )
// Barrier Creation // Barrier Creation
{ {
VkImageSubresourceRange subresourceRange = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
};
acquireToRenderBarrier = { acquireToRenderBarrier = {
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
.pNext = nullptr, .pNext = nullptr,
@ -449,14 +449,9 @@ void MiscData::init( RenderDevice const& renderDevice )
.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, .newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.subresourceRange = { .subresourceRange = subresourceRange,
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
}
}; };
acquireToRenderDependency = { acquireToRenderDependency = {
.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
.pNext = nullptr, .pNext = nullptr,
@ -480,14 +475,9 @@ void MiscData::init( RenderDevice const& renderDevice )
.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.subresourceRange = { .subresourceRange = subresourceRange,
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
}
}; };
renderToPresentDependency = { renderToPresentDependency = {
.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
.pNext = nullptr, .pNext = nullptr,

View File

@ -18,8 +18,9 @@ RenderDevice::~RenderDevice()
} }
// TODO: Failure Handling // TODO: Failure Handling
RenderDevice* CreateRenderDevice( GlobalMemory* mem, RenderDevice::CreateInfo const& createInfo ) RenderDevice* RenderDevice_Create( GlobalMemory* mem, RenderDevice::CreateInfo const& createInfo )
{ {
ASSERT( mem );
ASSERT( createInfo.window ); ASSERT( createInfo.window );
volkInitialize(); volkInitialize();
@ -72,8 +73,8 @@ RenderDevice* CreateRenderDevice( GlobalMemory* mem, RenderDevice::CreateInfo co
VK_CHECK( vkEnumeratePhysicalDevices( instance, &physicalDeviceCount, nullptr ) ); VK_CHECK( vkEnumeratePhysicalDevices( instance, &physicalDeviceCount, nullptr ) );
SDL_LogInfo( SDL_LOG_CATEGORY_GPU, "Found %u GPUs", physicalDeviceCount ); SDL_LogInfo( SDL_LOG_CATEGORY_GPU, "Found %u GPUs", physicalDeviceCount );
VkPhysicalDevice* physicalDevices = reinterpret_cast<VkPhysicalDevice*>(mem->allocate( VkPhysicalDevice* physicalDevices =
sizeof( VkPhysicalDevice ) * physicalDeviceCount )); reinterpret_cast<VkPhysicalDevice*>( mem->allocate( sizeof( VkPhysicalDevice ) * physicalDeviceCount ) );
VK_CHECK( vkEnumeratePhysicalDevices( instance, &physicalDeviceCount, physicalDevices ) ); VK_CHECK( vkEnumeratePhysicalDevices( instance, &physicalDeviceCount, physicalDevices ) );
for ( VkPhysicalDevice const physicalDevice : std::span{ physicalDevices, physicalDeviceCount } ) for ( VkPhysicalDevice const physicalDevice : std::span{ physicalDevices, physicalDeviceCount } )
@ -105,12 +106,11 @@ RenderDevice* CreateRenderDevice( GlobalMemory* mem, RenderDevice::CreateInfo co
uint32_t queueFamilyCount; uint32_t queueFamilyCount;
vkGetPhysicalDeviceQueueFamilyProperties( physicalDevice, &queueFamilyCount, nullptr ); vkGetPhysicalDeviceQueueFamilyProperties( physicalDevice, &queueFamilyCount, nullptr );
VkQueueFamilyProperties* queueFamilyProperties = reinterpret_cast<VkQueueFamilyProperties*>(mem->allocate( VkQueueFamilyProperties* queueFamilyProperties = reinterpret_cast<VkQueueFamilyProperties*>(
sizeof( VkQueueFamilyProperties ) * queueFamilyCount )); mem->allocate( sizeof( VkQueueFamilyProperties ) * queueFamilyCount ) );
vkGetPhysicalDeviceQueueFamilyProperties( physicalDevice, &queueFamilyCount, queueFamilyProperties ); vkGetPhysicalDeviceQueueFamilyProperties( physicalDevice, &queueFamilyCount, queueFamilyProperties );
for ( uint32_t queueFamilyIndex = 0; queueFamilyIndex != queueFamilyCount; for ( uint32_t queueFamilyIndex = 0; queueFamilyIndex != queueFamilyCount; ++queueFamilyIndex )
++queueFamilyIndex )
{ {
VkQueueFamilyProperties const& qfp = queueFamilyProperties[queueFamilyIndex]; VkQueueFamilyProperties const& qfp = queueFamilyProperties[queueFamilyIndex];
@ -182,9 +182,7 @@ RenderDevice* CreateRenderDevice( GlobalMemory* mem, RenderDevice::CreateInfo co
.samplerAnisotropy = true, .samplerAnisotropy = true,
}; };
std::array enabledDeviceExtensions = { std::array enabledDeviceExtensions = { VK_KHR_SWAPCHAIN_EXTENSION_NAME };
VK_KHR_SWAPCHAIN_EXTENSION_NAME
};
VkDeviceCreateInfo const deviceCreateInfo = { VkDeviceCreateInfo const deviceCreateInfo = {
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
@ -258,8 +256,8 @@ RenderDevice* CreateRenderDevice( GlobalMemory* mem, RenderDevice::CreateInfo co
uint32_t surfaceFormatCount; uint32_t surfaceFormatCount;
vkGetPhysicalDeviceSurfaceFormatsKHR( physicalDeviceInUse, surface, &surfaceFormatCount, nullptr ); vkGetPhysicalDeviceSurfaceFormatsKHR( physicalDeviceInUse, surface, &surfaceFormatCount, nullptr );
VkSurfaceFormatKHR* surfaceFormats = reinterpret_cast<VkSurfaceFormatKHR*>(mem->allocate( VkSurfaceFormatKHR* surfaceFormats =
sizeof( VkSurfaceFormatKHR ) * surfaceFormatCount )); reinterpret_cast<VkSurfaceFormatKHR*>( mem->allocate( sizeof( VkSurfaceFormatKHR ) * surfaceFormatCount ) );
vkGetPhysicalDeviceSurfaceFormatsKHR( physicalDeviceInUse, surface, &surfaceFormatCount, surfaceFormats ); vkGetPhysicalDeviceSurfaceFormatsKHR( physicalDeviceInUse, surface, &surfaceFormatCount, surfaceFormats );
VkSurfaceFormatKHR format = { VkSurfaceFormatKHR format = {
@ -292,8 +290,8 @@ RenderDevice* CreateRenderDevice( GlobalMemory* mem, RenderDevice::CreateInfo co
uint32_t presentModeCount; uint32_t presentModeCount;
vkGetPhysicalDeviceSurfacePresentModesKHR( physicalDeviceInUse, surface, &presentModeCount, nullptr ); vkGetPhysicalDeviceSurfacePresentModesKHR( physicalDeviceInUse, surface, &presentModeCount, nullptr );
VkPresentModeKHR* presentModes = reinterpret_cast<VkPresentModeKHR*>(mem->allocate( VkPresentModeKHR* presentModes =
sizeof( VkPresentModeKHR ) * presentModeCount )); reinterpret_cast<VkPresentModeKHR*>( mem->allocate( sizeof( VkPresentModeKHR ) * presentModeCount ) );
vkGetPhysicalDeviceSurfacePresentModesKHR( physicalDeviceInUse, surface, &presentModeCount, presentModes ); vkGetPhysicalDeviceSurfacePresentModesKHR( physicalDeviceInUse, surface, &presentModeCount, presentModes );
VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR; VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR;
@ -375,10 +373,10 @@ RenderDevice* CreateRenderDevice( GlobalMemory* mem, RenderDevice::CreateInfo co
Frame* frames = reinterpret_cast<Frame*>( mem->allocate( sizeof( Frame ) * swapchainImageCount ) ); Frame* frames = reinterpret_cast<Frame*>( mem->allocate( sizeof( Frame ) * swapchainImageCount ) );
for ( uint32_t i = 0; i != swapchainImageCount; ++i ) for ( uint32_t i = 0; i != swapchainImageCount; ++i )
{ {
new( frames + i ) Frame( device, directQueueFamilyIndex.value() ); Frame_Create( frames + i, device, directQueueFamilyIndex.value() );
} }
Byte* allocation = mem->allocate( sizeof( RenderDevice ), alignof( RenderDevice ) ); std::byte* allocation = mem->allocate( sizeof( RenderDevice ), alignof( RenderDevice ) );
return new ( allocation ) RenderDevice{ return new ( allocation ) RenderDevice{
instance, instance,
surface, surface,
@ -466,4 +464,5 @@ RenderDevice::RenderDevice(
, swapchainImages{ swapchainImages } , swapchainImages{ swapchainImages }
, swapchainViews{ swapchainViews } , swapchainViews{ swapchainViews }
, frames{ frames } , frames{ frames }
, swapchainImageCount{ swapchainImageCount } {} , swapchainImageCount{ swapchainImageCount }
{}

View File

@ -56,7 +56,6 @@ struct RenderDevice
VmaAllocator gpuAllocator, VmaAllocator gpuAllocator,
VkQueue directQueue, VkQueue directQueue,
uint32_t directQueueFamilyIndex, uint32_t directQueueFamilyIndex,
// TODO: Pack?
VkFormat swapchainFormat, VkFormat swapchainFormat,
VkExtent2D swapchainExtent, VkExtent2D swapchainExtent,
@ -65,8 +64,7 @@ struct RenderDevice
VkImage* swapchainImages, VkImage* swapchainImages,
VkImageView* swapchainViews, VkImageView* swapchainViews,
Frame* frames, Frame* frames,
uint32_t swapchainImageCount uint32_t swapchainImageCount );
);
RenderDevice( RenderDevice const& ) = delete; RenderDevice( RenderDevice const& ) = delete;
RenderDevice& operator=( RenderDevice const& ) = delete; RenderDevice& operator=( RenderDevice const& ) = delete;
@ -77,4 +75,4 @@ struct RenderDevice
~RenderDevice(); ~RenderDevice();
}; };
RenderDevice* CreateRenderDevice( GlobalMemory* mem, RenderDevice::CreateInfo const& createInfo ); RenderDevice* RenderDevice_Create( GlobalMemory* mem, RenderDevice::CreateInfo const& createInfo );