Skip to content

Commit 7ed7a53

Browse files
committed
Add: GPU Buffer, Shader Copying
1 parent 156380f commit 7ed7a53

10 files changed

Lines changed: 395 additions & 43 deletions

File tree

cmake/CmakeMacros.cmake

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,16 @@ macro(CREATE_APP name)
5353
add_definitions(-D_CONSOLE)
5454
set_property(TARGET ${PROJECT_NAME} PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
5555
endif()
56+
57+
# Copy shaders
58+
foreach(SHADER ${SHADER_FILES})
59+
get_filename_component(SHADER_NAME ${SHADER} NAME)
60+
add_custom_command(
61+
TARGET ${PROJECT_NAME} POST_BUILD
62+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
63+
"${SHADER}"
64+
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/${SHADER_NAME}"
65+
COMMENT "Copying shader file: ${SHADER_NAME}"
66+
)
67+
endforeach()
5668
endmacro()

src/EOS.h

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,15 @@ namespace EOS
272272
float MaxDepth = 1.0f;
273273
};
274274

275+
struct BufferDescription final
276+
{
277+
BufferUsageFlags Usage = BufferUsageFlags::None;
278+
StorageType Storage = StorageType::HostVisible;
279+
size_t Size = 0;
280+
const void* Data = nullptr;
281+
const char* DebugName = "";
282+
};
283+
275284
#pragma region INTERFACES
276285
//TODO: instead of interfaces use concept and a forward declare. And then every API implements 1 class of that name with the concept.
277286
//CMake should handle that only 1 type of API is being used at the time.
@@ -327,12 +336,20 @@ namespace EOS
327336
virtual EOS::Holder<EOS::ShaderModuleHandle> CreateShaderModule(const EOS::ShaderInfo& shaderInfo) = 0;
328337

329338
/**
330-
* @brief Creates a RenderPipeline and returns a handle to it.
331-
* @param renderPipelineDescription The description about what type of pipeline we want to create and what it exists of.
332-
* @return A Holder Handle to a Render Pipeline.
333-
*/
339+
* @brief Creates a RenderPipeline and returns a handle to it.
340+
* @param renderPipelineDescription The description about what type of pipeline we want to create and what it exists of.
341+
* @return A Holder Handle to a Render Pipeline.
342+
*/
334343
virtual EOS::Holder<EOS::RenderPipelineHandle> CreateRenderPipeline(const RenderPipelineDescription& renderPipelineDescription) = 0;
335344

345+
346+
/**
347+
* @brief Creates a Buffer and returns a handle to it.
348+
* @param description desecribes the what sort of buffer it is and its properties.
349+
* @return A Holder Handle to the buffer.
350+
*/
351+
virtual EOS::Holder<BufferHandle> CreateBuffer(const BufferDescription& description) = 0;
352+
336353
/**
337354
* @brief Handles the destruction of a TextureHandle and what it holds.
338355
* @param handle The handle to the texture you want to destroy.
@@ -352,6 +369,12 @@ namespace EOS
352369
*/
353370
virtual void Destroy(RenderPipelineHandle handle) = 0;
354371

372+
/**
373+
* @brief Handles the destruction of a BufferHandle and what it holds.
374+
* @param handle The handle to the Buffer you want to destroy.
375+
*/
376+
virtual void Destroy(BufferHandle handle) = 0;
377+
355378
protected:
356379
IContext() = default;
357380
};

src/enums.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,24 @@ namespace EOS
306306
MsaaResolve,
307307
None,
308308
};
309+
310+
enum class StorageType : uint8_t
311+
{
312+
Device,
313+
HostVisible,
314+
Memoryless
315+
};
316+
317+
enum BufferUsageFlags : uint8_t
318+
{
319+
None = 0x00,
320+
Index = 0x01,
321+
Vertex = 0x02,
322+
Uniform = 0x04,
323+
Storage = 0x08,
324+
Indirect = 0x10,
325+
ShaderBindingTable = 0x20,
326+
AccelStructBuildInputReadOnly = 0x40,
327+
AccelStructStorage = 0x80
328+
};
309329
}

src/main.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ int main()
1515
std::unique_ptr<EOS::IContext> context = EOS::CreateContextWithSwapChain(contextDescr);
1616
std::unique_ptr<EOS::ShaderCompiler> shaderCompiler = EOS::CreateShaderCompiler("./");
1717

18+
//TODO First check if the shader is not already compiled
1819
EOS::Holder<EOS::ShaderModuleHandle> shaderHandleVert = EOS::LoadShader(context, shaderCompiler, "triangleVert");
1920
EOS::Holder<EOS::ShaderModuleHandle> shaderHandleFrag = EOS::LoadShader(context, shaderCompiler, "triangleFrag");
2021

@@ -28,6 +29,27 @@ int main()
2829

2930
EOS::Holder<EOS::RenderPipelineHandle> renderPipelineHandle = context->CreateRenderPipeline(triangleDescription);
3031

32+
33+
//EOS::Holder<EOS::BufferHandle> vertexBuffer = context->CreateBuffer(
34+
//{
35+
// .usage = EOS::BufferUsageFlags::Vertex,
36+
// .storage = EOS::StorageType::Device,
37+
// .size = sizeof(float[3]) * positions.size(),
38+
// .data = positions.data(),
39+
// .debugName = "Buffer: vertex"
40+
//});
41+
//
42+
//
43+
//EOS::Holder<EOS::BufferHandle> indexBuffer = context->CreateBuffer(
44+
//{
45+
// .usage = EOS::BufferUsageFlags::Vertex,
46+
// .storage = EOS::StorageType::Device,
47+
// .size = sizeof(uint32_t) * indices.size(),
48+
// .data = indices.data(),`
49+
// .debugName = "Buffer: vertex"
50+
//});
51+
52+
3153
while (!glfwWindowShouldClose(window))
3254
{
3355
glfwPollEvents();

src/shaders/triangleFrag.slang

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
// Fragment Shader
2-
3-
struct FragmentInput
1+
[shader("fragment")]
2+
void main(
3+
float3 inColor : COLOR0,
4+
out float4 outFragColor : SV_Target
5+
)
46
{
5-
float4 position : SV_Position; // Not typically used directly in basic fragment shaders
6-
float3 color : COLOR; // Input color from vertex shader
7-
};
8-
9-
float4 main(FragmentInput input) : SV_Target
10-
{
11-
// Output the interpolated color from the vertex shader
12-
return float4(input.color, 1.0); // r, g, b, alpha
13-
}
7+
outFragColor = float4(inColor, 1.0);
8+
}

src/shaders/triangleVert.slang

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
// Vertex Shader
1+
// Slang version
22

3-
struct VertexInput
3+
[shader("vertex")]
4+
void main(
5+
uint vertexID : SV_VertexID,
6+
out float4 outPosition : SV_Position,
7+
out float3 outColor : COLOR0
8+
)
49
{
5-
float2 position : POSITION; // Input vertex position
6-
float3 color : COLOR; // Input vertex color
7-
};
10+
static const float2 pos[3] = {
11+
float2(-0.6, -0.4),
12+
float2( 0.6, -0.4),
13+
float2( 0.0, 0.6)
14+
};
815

9-
struct VertexOutput
10-
{
11-
float4 position : SV_Position; // Output clip-space position
12-
float3 color : COLOR; // Output color to fragment shader
13-
};
16+
static const float3 col[3] = {
17+
float3(1.0, 0.0, 0.0),
18+
float3(0.0, 1.0, 0.0),
19+
float3(0.0, 0.0, 1.0)
20+
};
1421

15-
VertexOutput main(VertexInput input)
16-
{
17-
VertexOutput output;
18-
// Pass the 2D position directly, assuming it's already in clip space for simplicity.
19-
// z = 0.5 for visibility, w = 1.0 for a proper perspective divide.
20-
output.position = float4(input.position, 0.5, 1.0);
21-
output.color = input.color;
22-
return output;
23-
}
22+
outPosition = float4(pos[vertexID], 0.0, 1.0);
23+
outColor = col[vertexID];
24+
}

src/vulkan/vkTools.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,72 @@ namespace VkContext
12731273
CHECK(false, "Could not resolve StoreOp");
12741274
return VK_ATTACHMENT_STORE_OP_DONT_CARE;
12751275
}
1276+
1277+
VkBufferUsageFlags BufferUsageFlagsToVkBufferUsageFlags(EOS::BufferUsageFlags bufferUsageFlags)
1278+
{
1279+
VkBufferUsageFlags usageFlags{};
1280+
1281+
if (bufferUsageFlags & EOS::BufferUsageFlags::Index)
1282+
{
1283+
usageFlags |= VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
1284+
}
1285+
1286+
if (bufferUsageFlags & EOS::BufferUsageFlags::Vertex)
1287+
{
1288+
usageFlags |= VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
1289+
}
1290+
1291+
if (bufferUsageFlags & EOS::BufferUsageFlags::Uniform)
1292+
{
1293+
usageFlags |= VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_KHR;
1294+
}
1295+
1296+
if (bufferUsageFlags & EOS::BufferUsageFlags::Storage)
1297+
{
1298+
usageFlags |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_KHR;
1299+
}
1300+
1301+
if (bufferUsageFlags & EOS::BufferUsageFlags::Indirect)
1302+
{
1303+
usageFlags |= VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_KHR;
1304+
}
1305+
1306+
if (bufferUsageFlags & EOS::BufferUsageFlags::ShaderBindingTable)
1307+
{
1308+
usageFlags |= VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_KHR;
1309+
}
1310+
1311+
if (bufferUsageFlags & EOS::BufferUsageFlags::AccelStructBuildInputReadOnly)
1312+
{
1313+
usageFlags |= VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_KHR;
1314+
}
1315+
1316+
if (bufferUsageFlags & EOS::BufferUsageFlags::AccelStructStorage)
1317+
{
1318+
usageFlags |= VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_KHR;
1319+
}
1320+
1321+
return usageFlags;
1322+
}
1323+
1324+
VkMemoryPropertyFlags StorageTypeToVkMemoryPropertyFlags(EOS::StorageType storage)
1325+
{
1326+
VkMemoryPropertyFlags memFlags{0};
1327+
1328+
switch (storage)
1329+
{
1330+
case EOS::StorageType::Device:
1331+
memFlags |= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
1332+
break;
1333+
case EOS::StorageType::HostVisible:
1334+
memFlags |= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
1335+
break;
1336+
case EOS::StorageType::Memoryless:
1337+
memFlags |= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT;
1338+
break;
1339+
}
1340+
return memFlags;
1341+
}
12761342
};
12771343

12781344
namespace VkSynchronization

src/vulkan/vkTools.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ namespace VkContext
143143

144144
[[nodiscard]] VkAttachmentLoadOp LoadOpToVkAttachmentLoadOp(EOS::LoadOp loadOp);
145145
[[nodiscard]] VkAttachmentStoreOp StoreOpToVkAttachmentStoreOp(EOS::StoreOp storeOp);
146+
[[nodiscard]] VkBufferUsageFlags BufferUsageFlagsToVkBufferUsageFlags(EOS::BufferUsageFlags bufferUsageFlags);
147+
[[nodiscard]] VkMemoryPropertyFlags StorageTypeToVkMemoryPropertyFlags(EOS::StorageType storage);
146148
}
147149

148150
namespace VkSynchronization

0 commit comments

Comments
 (0)