Compare commits

...

3 Commits

Author SHA1 Message Date
Anish Bhobe 219c3394ed Mass restructure. 2025-06-15 22:35:54 +02:00
Anish Bhobe 6da0250c81 Mipmapping implemented. 2025-06-15 22:21:00 +02:00
Anish Bhobe 98c5f28146 Load and add texture to the Rect. 2025-06-14 17:16:37 +02:00
25 changed files with 1290 additions and 707 deletions

5
.gitattributes vendored Normal file
View File

@ -0,0 +1,5 @@
*.jpg filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text
*.glb filter=lfs diff=lfs merge=lfs -text
*.hdr filter=lfs diff=lfs merge=lfs -text
*.exr filter=lfs diff=lfs merge=lfs -text

51
Assets/Shaders/Mesh.slang Normal file
View File

@ -0,0 +1,51 @@
struct VertexOut {
float4 outPosition : SV_Position;
float4 screenPosition : ScreenPosition;
float4 vertexColor : CoarseColor;
float2 texCoord0 : TexCoord0;
};
struct CameraData {
float4x4 view;
float4x4 proj;
};
struct PerFrameData {
CameraData camera;
Sampler2D texture;
}
ParameterBlock<PerFrameData> perFrameData;
struct PerInstanceData {
float4x4 transform;
}
[[vk::push_constant]]
uniform ConstantBuffer<PerInstanceData> pcb;
[shader("vertex")]
VertexOut VertexMain(
uint vertexId: SV_VertexID,
float3 position,
float3 color,
float2 texCoord0,
) {
VertexOut output;
output.outPosition = mul(perFrameData.camera.proj, mul(perFrameData.camera.view, mul(pcb.transform, float4(position, 1.0f))));
output.screenPosition = mul(perFrameData.camera.proj, mul(perFrameData.camera.view, mul(pcb.transform, float4(position, 1.0f))));
output.vertexColor = float4(color, 1.0f);
output.texCoord0 = texCoord0 * 2.0f;
return output;
}
[shader("fragment")]
float4 FragmentMain(
float4 interpolatePosition : ScreenPosition,
float4 interpolatedColors : CoarseColor,
float2 uv0 : TexCoord0,
) : SV_Target0 {
return float4(perFrameData.texture.Sample(uv0).rgb, 1.0f) * interpolatedColors;
}

BIN
Assets/Textures/container2.png (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Assets/Textures/wall.jpg (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -103,13 +103,13 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>EnableAllWarnings</WarningLevel>
<WarningLevel>Level4</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
<TreatWarningAsError>false</TreatWarningAsError>
<TreatWarningAsError>true</TreatWarningAsError>
<ExceptionHandling>Sync</ExceptionHandling>
<MinimalRebuild>false</MinimalRebuild>
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
@ -124,7 +124,7 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>EnableAllWarnings</WarningLevel>
<WarningLevel>Level4</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
@ -132,7 +132,7 @@
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
<TreatWarningAsError>false</TreatWarningAsError>
<TreatWarningAsError>true</TreatWarningAsError>
<ExceptionHandling>/EH-</ExceptionHandling>
<MinimalRebuild>false</MinimalRebuild>
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
@ -147,47 +147,50 @@
<AdditionalLibraryDirectories>C:\Users\Eon\source\repos\Blaze\vcpkg_installed\x64-windows\x64-windows\bin;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="AppState.cpp" />
<ClCompile Include="Blaze.cpp" />
<ClCompile Include="Frame.cpp" />
<ClCompile Include="GlobalMemory.cpp" />
<ClCompile Include="MiscData.cpp" />
<ClCompile Include="RenderDevice.cpp" />
<ClCompile Include="VmaImpl.cpp" />
</ItemGroup>
<ItemGroup>
<None Include=".clang-format" />
<None Include=".gitignore" />
<CustomBuild Include="Assets\Shaders\Mesh.slang">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
<FileType>Document</FileType>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">slangc %(FullPath) -profile sm_6_6 -target spirv -o %(Filename).spv</Command>
<Message Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Compiling %(Filename).slang</Message>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).spv</Outputs>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">slangc %(FullPath) -profile sm_6_6 -target spirv -o %(Filename).spv</Command>
<Message Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Compiling %(Filename).slang</Message>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).spv</Outputs>
</CustomBuild>
<None Include="PLAN.md">
<SubType>
</SubType>
</None>
<None Include="README.md" />
<CustomBuild Include="Mesh.slang">
<FileType>Document</FileType>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">slangc %(FullPath) -profile sm_6_6 -target spirv -o %(Filename).spv</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).spv</Outputs>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">slangc %(FullPath) -profile sm_6_6 -target spirv -o %(Filename).spv</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).spv</Outputs>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
<TreatOutputAsContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</TreatOutputAsContent>
<TreatOutputAsContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</TreatOutputAsContent>
<Message Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Compiling slang shader</Message>
<Message Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Compiling slang shader</Message>
</CustomBuild>
<None Include="vcpkg-configuration.json" />
<None Include="vcpkg.json" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="AppState.h" />
<ClInclude Include="Frame.h" />
<ClInclude Include="GlobalMemory.h" />
<ClInclude Include="MacroUtils.h" />
<ClInclude Include="MathUtil.h" />
<ClInclude Include="MiscData.h" />
<ClInclude Include="RenderDevice.h" />
<ClInclude Include="Blaze\AppState.h" />
<ClInclude Include="Blaze\Frame.h" />
<ClInclude Include="Blaze\GlobalMemory.h" />
<ClInclude Include="Blaze\MacroUtils.h" />
<ClInclude Include="Blaze\MathUtil.h" />
<ClInclude Include="Blaze\MiscData.h" />
<ClInclude Include="Blaze\RenderDevice.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Blaze\AppState.cpp" />
<ClCompile Include="Blaze\Blaze.cpp" />
<ClCompile Include="Blaze\Frame.cpp" />
<ClCompile Include="Blaze\GlobalMemory.cpp" />
<ClCompile Include="Blaze\MiscData.cpp" />
<ClCompile Include="Blaze\RenderDevice.cpp" />
<ClCompile Include="Blaze\StbImpl.cpp" />
<ClCompile Include="Blaze\VmaImpl.cpp" />
</ItemGroup>
<ItemGroup>
<Image Include="Assets\Textures\container2.png" />
<Image Include="Assets\Textures\wall.jpg" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -13,79 +13,99 @@
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
<Filter Include="Shader Files">
<Filter Include="Source Files\HeaderOnlyImpl">
<UniqueIdentifier>{f1e377e6-a7c9-4c3e-8068-1d83aa67df45}</UniqueIdentifier>
</Filter>
<Filter Include="Resource Files\Shader Files">
<UniqueIdentifier>{672b8ec5-258c-4a21-bbf7-66d3b1185fe9}</UniqueIdentifier>
</Filter>
<Filter Include="Resource Files\Textures">
<UniqueIdentifier>{644c6f90-020a-4691-8c0a-6eee17301b80}</UniqueIdentifier>
</Filter>
<Filter Include="Resource Files\Config">
<UniqueIdentifier>{1f13daa9-d8c1-4fda-b5a5-09ed652775e7}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Blaze.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Frame.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="RenderDevice.cpp">
<Filter>Source Files</Filter>
</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>
<ClCompile Include="VmaImpl.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="vcpkg.json">
<Filter>Resource Files</Filter>
</None>
<None Include="vcpkg-configuration.json">
<Filter>Resource Files</Filter>
</None>
<None Include="README.md">
<Filter>Resource Files</Filter>
</None>
<None Include=".gitignore">
<Filter>Resource Files</Filter>
</None>
<None Include="PLAN.md">
<Filter>Resource Files</Filter>
</None>
<None Include=".clang-format">
<Filter>Resource Files</Filter>
<Filter>Resource Files\Config</Filter>
</None>
<None Include=".gitignore">
<Filter>Resource Files\Config</Filter>
</None>
<None Include="vcpkg.json">
<Filter>Resource Files\Config</Filter>
</None>
<None Include="vcpkg-configuration.json">
<Filter>Resource Files\Config</Filter>
</None>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="Mesh.slang">
<Filter>Shader Files</Filter>
</CustomBuild>
<ClInclude Include="Blaze\AppState.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Blaze\Frame.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Blaze\GlobalMemory.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Blaze\MacroUtils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Blaze\MathUtil.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Blaze\MiscData.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Blaze\RenderDevice.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Frame.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="RenderDevice.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MacroUtils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MathUtil.h">
<Filter>Header Files</Filter>
</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>
<ClCompile Include="Blaze\AppState.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Blaze\Blaze.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Blaze\Frame.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Blaze\GlobalMemory.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Blaze\MiscData.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Blaze\RenderDevice.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Blaze\StbImpl.cpp">
<Filter>Source Files\HeaderOnlyImpl</Filter>
</ClCompile>
<ClCompile Include="Blaze\VmaImpl.cpp">
<Filter>Source Files\HeaderOnlyImpl</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Image Include="Assets\Textures\container2.png">
<Filter>Resource Files\Textures</Filter>
</Image>
<Image Include="Assets\Textures\wall.jpg">
<Filter>Resource Files\Textures</Filter>
</Image>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="Assets\Shaders\Mesh.slang">
<Filter>Resource Files\Shader Files</Filter>
</CustomBuild>
</ItemGroup>
</Project>

View File

@ -48,7 +48,7 @@ AppState* AppState_Create( GlobalMemory* memory, uint32_t const width, uint32_t
auto* miscDataAllocation = memory->allocate( sizeof( MiscData ) );
MiscData* miscData = new ( miscDataAllocation ) MiscData{};
miscData->init( *renderDevice );
if ( !miscData->init( *renderDevice ) ) return nullptr;
auto* allocation = memory->allocate( sizeof( AppState ) );
AppState* appState = new ( allocation ) AppState{ window, renderDevice, miscData };

View File

@ -70,10 +70,14 @@ SDL_AppResult SDL_AppIterate( void* appstate )
SDL_SetWindowTitle( appState.window, appState.sprintfBuffer );
}
misc.cameraData.modelMatrix = DirectX::XMMatrixMultiply(
misc.cameraData.modelMatrix,
DirectX::XMMatrixRotationY( DirectX::XMConvertToRadians( 60.0f ) * static_cast<float>( deltaTime ) ) );
memcpy( misc.cameraUniformBufferPtr, &misc.cameraData, sizeof misc.cameraData );
for ( Transform& transform : misc.modelTransform )
{
transform.rotation = DirectX::XMQuaternionMultiply(
DirectX::XMQuaternionRotationAxis(
DirectX::XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f ),
DirectX::XMConvertToRadians( 60.0f ) * static_cast<float>( deltaTime ) ),
transform.rotation );
}
uint32_t currentImageIndex;
VK_CHECK( vkAcquireNextImageKHR(
@ -155,8 +159,21 @@ SDL_AppResult SDL_AppIterate( void* appstate )
vkCmdBindVertexBuffers( cmd, 0, 1, &misc.vertexBuffer, &offset );
vkCmdBindDescriptorSets(
cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, misc.pipelineLayout, 0, 1, &misc.descriptorSet, 0, nullptr );
for ( Transform& localTransform : misc.modelTransform )
{
DirectX::XMMATRIX worldTransform;
{
auto [x, y, z] = localTransform.position;
auto scale = localTransform.scale;
worldTransform = DirectX::XMMatrixScaling( scale, scale, scale ) *
DirectX::XMMatrixRotationQuaternion( localTransform.rotation ) *
DirectX::XMMatrixTranslation( x, y, z );
}
vkCmdPushConstants(
cmd, misc.pipelineLayout, VK_SHADER_STAGE_ALL_GRAPHICS, 0, sizeof worldTransform, &worldTransform );
vkCmdDraw( cmd, static_cast<uint32_t>( misc.vertices.size() ), 1, 0, 0 );
}
}
vkCmdEndRendering( cmd );
vkCmdPipelineBarrier2( cmd, &misc.renderToPresentDependency );
}
@ -208,7 +225,7 @@ void SDL_AppQuit( void* appstate, SDL_AppResult )
{
AppState* appState = static_cast<AppState*>( appstate );
appState->destroy();
if ( appState ) appState->destroy();
Blaze::Global::g_Memory.destroy();
}

1010
Blaze/MiscData.cpp Normal file

File diff suppressed because it is too large Load Diff

73
Blaze/MiscData.h Normal file
View File

@ -0,0 +1,73 @@
#pragma once
#include <array>
#include <volk.h>
#include <vma/vk_mem_alloc.h>
#include <DirectXMath.h>
struct GlobalMemory;
struct RenderDevice;
struct Vertex
{
DirectX::XMFLOAT3 position;
DirectX::XMFLOAT3 color;
DirectX::XMFLOAT2 texCoord0;
};
struct Transform
{
DirectX::XMFLOAT3 position;
float scale;
DirectX::XMVECTOR rotation;
};
struct MiscData
{
struct CameraData
{
DirectX::XMMATRIX viewMatrix;
DirectX::XMMATRIX projectionMatrix;
};
uint64_t previousCounter;
VkDescriptorSetLayout descriptorSetLayout;
VkPipelineLayout pipelineLayout;
VkPipeline meshPipeline;
VkBuffer vertexBuffer;
VmaAllocation vertexBufferAllocation;
size_t vertexBufferSize;
std::array<Vertex, 4> vertices;
VkImage texture;
VmaAllocation textureAllocation;
VkImageView textureView;
VkSampler sampler;
uint64_t _padding; // TODO: Optimize out?
std::array<Transform, 2> modelTransform;
DirectX::XMVECTOR cameraPosition;
DirectX::XMVECTOR cameraTarget;
DirectX::XMVECTOR cameraUp;
CameraData cameraData;
VkBuffer cameraUniformBuffer;
VmaAllocation cameraUniformBufferAllocation;
size_t cameraUniformBufferSize;
uint8_t* cameraUniformBufferPtr;
VkDescriptorPool descriptorPool;
VkDescriptorSet descriptorSet;
VkImageMemoryBarrier2 acquireToRenderBarrier;
VkDependencyInfo acquireToRenderDependency;
VkImageMemoryBarrier2 renderToPresentBarrier;
VkDependencyInfo renderToPresentDependency;
bool init( RenderDevice const& renderDevice );
void destroy( RenderDevice const& renderDevice );
};

6
Blaze/StbImpl.cpp Normal file
View File

@ -0,0 +1,6 @@
#pragma warning( push, 1 )
#pragma warning( disable : 5045 )
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#pragma warning( pop )

View File

@ -1,44 +0,0 @@
const static float4 vertexPos[] = {
float4(0.0f, 0.5f, 0.5f, 1.0f),
float4(-0.5f, -0.5f, 0.5f, 1.0f),
float4(0.5f, -0.5f, 0.5f, 1.0f),
};
const static float4 vertexColors[] = {
float4(1.0f, 0.0f, 0.0f, 1.0f),
float4(0.0f, 1.0f, 0.0f, 1.0f),
float4(0.0f, 0.0f, 1.0f, 1.0f),
};
struct VertexOut {
float4 outPosition : SV_Position;
float4 vertexColor : CoarseColor;
};
struct CameraData {
float4x4 model;
float4x4 view;
float4x4 proj;
};
ParameterBlock<CameraData> camera;
[shader("vertex")]
VertexOut VertexMain(
uint vertexId: SV_VertexID,
float4 position,
float4 color,
) {
VertexOut output;
output.outPosition = mul(camera.proj, mul(camera.view, mul(camera.model, position)));
output.vertexColor = color;
return output;
}
[shader("fragment")]
float4 FragmentMain(
float4 interpolatedColors: CoarseColor,
) : SV_Target0 {
return float4(interpolatedColors.rgb, 1.0f);
}

View File

@ -1,507 +0,0 @@
#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;
previousCounter = 0;
// Pipeline Creation
{
size_t dataSize;
void* rawData = SDL_LoadFile( "Mesh.spv", &dataSize );
ASSERT( dataSize % 4 == 0 );
if ( !rawData )
{
SDL_LogError( SDL_LOG_CATEGORY_SYSTEM, "%s", SDL_GetError() );
abort();
}
uint32_t const* data = static_cast<uint32_t const*>( rawData );
// Create Shader Module
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 ) );
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 = 1,
.pSetLayouts = &descriptorSetLayout,
.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,
}
};
// Bindings
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 = 1,
.pVertexBindingDescriptions = &bindingDescription,
.vertexAttributeDescriptionCount = static_cast<uint32_t>( 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_STRIP,
.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, &meshPipeline ) );
vkDestroyShaderModule( device, shaderModule, nullptr );
SDL_free( rawData );
}
// Vertex Buffer Creation
{
vertexBufferSize = sizeof vertices[0] * vertices.size();
// 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] );
}
}
// Camera
{
cameraPosition = DirectX::XMVectorSet( 0.0f, 0.0f, -5.0f, 1.0f );
cameraTarget = DirectX::XMVectorSet( 0.0f, 0.0f, 0.0f, 1.0f );
cameraUp = DirectX::XMVectorSet( 0.0f, 1.0f, 0.0f, 1.0f );
cameraData.modelMatrix = DirectX::XMMatrixIdentity();
cameraData.viewMatrix = DirectX::XMMatrixLookAtLH( cameraPosition, cameraTarget, cameraUp );
cameraData.projectionMatrix =
DirectX::XMMatrixPerspectiveFovLH( DirectX::XMConvertToRadians( 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<uint8_t*>( 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
{
VkImageSubresourceRange subresourceRange = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
};
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 = subresourceRange,
};
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 = subresourceRange,
};
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::destroy( RenderDevice const& renderDevice )
{
VkDevice const device = renderDevice.device;
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 );
}

View File

@ -1,58 +0,0 @@
#pragma once
#include <array>
#include <volk.h>
#include <vma/vk_mem_alloc.h>
#include <DirectXMath.h>
struct RenderDevice;
struct Vertex
{
float position[4];
float color[4];
};
struct MiscData
{
struct CameraData
{
DirectX::XMMATRIX modelMatrix;
DirectX::XMMATRIX viewMatrix;
DirectX::XMMATRIX projectionMatrix;
};
uint64_t previousCounter;
VkDescriptorSetLayout descriptorSetLayout;
VkPipelineLayout pipelineLayout;
VkPipeline meshPipeline;
VkBuffer vertexBuffer;
VmaAllocation vertexBufferAllocation;
size_t vertexBufferSize;
std::array<Vertex, 4> vertices;
uint64_t _padding; // TODO: Optimize out?
DirectX::XMVECTOR cameraPosition;
DirectX::XMVECTOR cameraTarget;
DirectX::XMVECTOR cameraUp;
CameraData cameraData;
VkBuffer cameraUniformBuffer;
VmaAllocation cameraUniformBufferAllocation;
size_t cameraUniformBufferSize;
uint8_t* cameraUniformBufferPtr;
VkDescriptorPool descriptorPool;
VkDescriptorSet descriptorSet;
VkImageMemoryBarrier2 acquireToRenderBarrier;
VkDependencyInfo acquireToRenderDependency;
VkImageMemoryBarrier2 renderToPresentBarrier;
VkDependencyInfo renderToPresentDependency;
void init( RenderDevice const& renderDevice );
void destroy( RenderDevice const& renderDevice );
};

View File

@ -4,6 +4,7 @@
"shader-slang",
"vulkan-memory-allocator",
"directxmath",
"stb",
{
"name": "sdl3",
"features": [ "vulkan" ]