commit fd310560f9b153eb8a454cb65b627af7e3454d8c Author: sam Date: Mon Nov 4 12:32:36 2024 +1300 first commit diff --git a/.cache/clangd/index/command_buffer.h.80BF74E22FA9A8B1.idx b/.cache/clangd/index/command_buffer.h.80BF74E22FA9A8B1.idx new file mode 100644 index 0000000..2be85a9 Binary files /dev/null and b/.cache/clangd/index/command_buffer.h.80BF74E22FA9A8B1.idx differ diff --git a/.cache/clangd/index/copy_pass.h.75DC6BAB12A460E6.idx b/.cache/clangd/index/copy_pass.h.75DC6BAB12A460E6.idx new file mode 100644 index 0000000..b49c55a Binary files /dev/null and b/.cache/clangd/index/copy_pass.h.75DC6BAB12A460E6.idx differ diff --git a/.cache/clangd/index/device.c.6C259B36172A19C7.idx b/.cache/clangd/index/device.c.6C259B36172A19C7.idx new file mode 100644 index 0000000..4dca8e8 Binary files /dev/null and b/.cache/clangd/index/device.c.6C259B36172A19C7.idx differ diff --git a/.cache/clangd/index/device.h.17FFF3FE11DD7B5E.idx b/.cache/clangd/index/device.h.17FFF3FE11DD7B5E.idx new file mode 100644 index 0000000..ac34a87 Binary files /dev/null and b/.cache/clangd/index/device.h.17FFF3FE11DD7B5E.idx differ diff --git a/.cache/clangd/index/device.h.5489FF8BB9CB1FFB.idx b/.cache/clangd/index/device.h.5489FF8BB9CB1FFB.idx new file mode 100644 index 0000000..554c17b Binary files /dev/null and b/.cache/clangd/index/device.h.5489FF8BB9CB1FFB.idx differ diff --git a/.cache/clangd/index/file.c.CB50DC29A19B1694.idx b/.cache/clangd/index/file.c.CB50DC29A19B1694.idx new file mode 100644 index 0000000..1f79fe2 Binary files /dev/null and b/.cache/clangd/index/file.c.CB50DC29A19B1694.idx differ diff --git a/.cache/clangd/index/file.h.9B3CB7460AC25849.idx b/.cache/clangd/index/file.h.9B3CB7460AC25849.idx new file mode 100644 index 0000000..50b9020 Binary files /dev/null and b/.cache/clangd/index/file.h.9B3CB7460AC25849.idx differ diff --git a/.cache/clangd/index/init.h.5C86A0883D04E9FA.idx b/.cache/clangd/index/init.h.5C86A0883D04E9FA.idx new file mode 100644 index 0000000..a9c3854 Binary files /dev/null and b/.cache/clangd/index/init.h.5C86A0883D04E9FA.idx differ diff --git a/.cache/clangd/index/io.c.83B1619B498EFDA2.idx b/.cache/clangd/index/io.c.83B1619B498EFDA2.idx new file mode 100644 index 0000000..73cda9f Binary files /dev/null and b/.cache/clangd/index/io.c.83B1619B498EFDA2.idx differ diff --git a/.cache/clangd/index/io.h.A5F7786F8F198D59.idx b/.cache/clangd/index/io.h.A5F7786F8F198D59.idx new file mode 100644 index 0000000..1394c23 Binary files /dev/null and b/.cache/clangd/index/io.h.A5F7786F8F198D59.idx differ diff --git a/.cache/clangd/index/main.c.360458CBCE130659.idx b/.cache/clangd/index/main.c.360458CBCE130659.idx new file mode 100644 index 0000000..a327c3b Binary files /dev/null and b/.cache/clangd/index/main.c.360458CBCE130659.idx differ diff --git a/.cache/clangd/index/main.c.FC90165D9662D1FF.idx b/.cache/clangd/index/main.c.FC90165D9662D1FF.idx new file mode 100644 index 0000000..6696877 Binary files /dev/null and b/.cache/clangd/index/main.c.FC90165D9662D1FF.idx differ diff --git a/.cache/clangd/index/pch.h.FAF6765EBEF7A460.idx b/.cache/clangd/index/pch.h.FAF6765EBEF7A460.idx new file mode 100644 index 0000000..3528c3e Binary files /dev/null and b/.cache/clangd/index/pch.h.FAF6765EBEF7A460.idx differ diff --git a/.cache/clangd/index/shaders.c.F6267E88DF3F57D6.idx b/.cache/clangd/index/shaders.c.F6267E88DF3F57D6.idx new file mode 100644 index 0000000..aa4c205 Binary files /dev/null and b/.cache/clangd/index/shaders.c.F6267E88DF3F57D6.idx differ diff --git a/.cache/clangd/index/shaders.h.DC6F6E4EB912EE60.idx b/.cache/clangd/index/shaders.h.DC6F6E4EB912EE60.idx new file mode 100644 index 0000000..528d0b8 Binary files /dev/null and b/.cache/clangd/index/shaders.h.DC6F6E4EB912EE60.idx differ diff --git a/.cache/clangd/index/shaders.h.FB84043E831F692A.idx b/.cache/clangd/index/shaders.h.FB84043E831F692A.idx new file mode 100644 index 0000000..a2bdf7c Binary files /dev/null and b/.cache/clangd/index/shaders.h.FB84043E831F692A.idx differ diff --git a/.cache/clangd/index/systems.h.4B3B0C8EFC1B6118.idx b/.cache/clangd/index/systems.h.4B3B0C8EFC1B6118.idx new file mode 100644 index 0000000..73d01fb Binary files /dev/null and b/.cache/clangd/index/systems.h.4B3B0C8EFC1B6118.idx differ diff --git a/.cache/clangd/index/upload.h.CC70961FD695C6D2.idx b/.cache/clangd/index/upload.h.CC70961FD695C6D2.idx new file mode 100644 index 0000000..aabbe33 Binary files /dev/null and b/.cache/clangd/index/upload.h.CC70961FD695C6D2.idx differ diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..c17e64a --- /dev/null +++ b/.clang-format @@ -0,0 +1,25 @@ +BasedOnStyle: WebKit +IndentWidth: 4 +TabWidth: 4 +UseTab: Never +AlignConsecutiveDeclarations: false +AlignConsecutiveAssignments: false +AlignTrailingComments: true +ColumnLimit: 105 +BreakBeforeBraces: Attach +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AllowShortFunctionsOnASingleLine: false +AllowShortLambdasOnASingleLine: false +PointerAlignment: Left +SpaceBeforeParens: Never +SpacesInParentheses: false +SpacesInConditionalStatement: false +SpacesInContainerLiterals: false +SpaceAfterCStyleCast: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeSquareBrackets: false +SpacesBeforeTrailingComments: 2 +PenaltyBreakAssignment: 1000 +NamespaceIndentation: All + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1521057 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/compile_commands.json b/compile_commands.json new file mode 100644 index 0000000..f944c8f --- /dev/null +++ b/compile_commands.json @@ -0,0 +1,21 @@ +[ +{ + "directory": "/home/sam/Documents/Projects/brimstone", + "arguments": ["/usr/bin/gcc", "-c", "-g", "-O0", "-Isrc", "-o", "build/.objs/brimstone/linux/arm64/debug/src/brimstone/io/file.c.o", "src/brimstone/io/file.c"], + "file": "src/brimstone/io/file.c" +}, +{ + "directory": "/home/sam/Documents/Projects/brimstone", + "arguments": ["/usr/bin/gcc", "-c", "-g", "-O0", "-Isrc", "-o", "build/.objs/brimstone/linux/arm64/debug/src/brimstone/shaders/shaders.c.o", "src/brimstone/shaders/shaders.c"], + "file": "src/brimstone/shaders/shaders.c" +}, +{ + "directory": "/home/sam/Documents/Projects/brimstone", + "arguments": ["/usr/bin/gcc", "-c", "-g", "-O0", "-Isrc", "-o", "build/.objs/brimstone/linux/arm64/debug/src/brimstone/device/device.c.o", "src/brimstone/device/device.c"], + "file": "src/brimstone/device/device.c" +}, +{ + "directory": "/home/sam/Documents/Projects/brimstone", + "arguments": ["/usr/bin/gcc", "-c", "-g", "-O0", "-Isrc", "-o", "build/.objs/brimstone/linux/arm64/debug/src/brimstone/main.c.o", "src/brimstone/main.c"], + "file": "src/brimstone/main.c" +}] diff --git a/shader.frag b/shader.frag new file mode 100644 index 0000000..084e7df --- /dev/null +++ b/shader.frag @@ -0,0 +1,10 @@ +#version 460 + +layout(location = 0) in vec4 vertColor; + +layout(location = 0) out vec4 fragColor; + +void main() { + fragColor = vertColor; +} + diff --git a/shader.frag.spv b/shader.frag.spv new file mode 100644 index 0000000..17f5fec Binary files /dev/null and b/shader.frag.spv differ diff --git a/shader.spv b/shader.spv new file mode 100644 index 0000000..4bc0c18 Binary files /dev/null and b/shader.spv differ diff --git a/shader.vert b/shader.vert new file mode 100644 index 0000000..90a49da --- /dev/null +++ b/shader.vert @@ -0,0 +1,12 @@ +#version 460 + +layout(location = 0) in vec3 aPos; +layout(location = 1) in vec3 aColor; + +layout(location = 0) out vec4 vertColor; + +void main() { + vertColor = vec4(aColor, 1.0f); + + gl_Position = vec4(aPos, 1.0f); +} diff --git a/shader.vert.spv b/shader.vert.spv new file mode 100644 index 0000000..731fc5f Binary files /dev/null and b/shader.vert.spv differ diff --git a/src/brimstone/graphics/command_buffer.c b/src/brimstone/graphics/command_buffer.c new file mode 100644 index 0000000..3165b88 --- /dev/null +++ b/src/brimstone/graphics/command_buffer.c @@ -0,0 +1,24 @@ +#include +#include + +SDL_GPUCommandBuffer* CommandBufferAcquire(SDL_GPUDevice* device) { + assert(device != NULL); + + SDL_GPUCommandBuffer* command_buffer = SDL_AcquireGPUCommandBuffer(device); + if(command_buffer == NULL) { + SDL_Log("Failed to acquire command buffer: %s", SDL_GetError()); + } + + return command_buffer; +} + +bool CommandBufferSubmit(SDL_GPUCommandBuffer* command_buffer) { + assert(command_buffer != NULL); + + bool success = SDL_SubmitGPUCommandBuffer(command_buffer); + if(!success) { + SDL_Log("Failed to submit command buffer: %s", SDL_GetError()); + } + + return success; +} diff --git a/src/brimstone/graphics/command_buffer.h b/src/brimstone/graphics/command_buffer.h new file mode 100644 index 0000000..c0e327e --- /dev/null +++ b/src/brimstone/graphics/command_buffer.h @@ -0,0 +1,9 @@ +#ifndef COMMAND_BUFFER_H +#define COMMAND_BUFFER_H + +#include + +SDL_GPUCommandBuffer* CommandBufferAcquire(SDL_GPUDevice* device); +bool CommandBufferSubmit(SDL_GPUCommandBuffer* command_buffer); + +#endif diff --git a/src/brimstone/graphics/device.c b/src/brimstone/graphics/device.c new file mode 100644 index 0000000..33304b1 --- /dev/null +++ b/src/brimstone/graphics/device.c @@ -0,0 +1,14 @@ +#include +#include +#include + +SDL_GPUDevice* DeviceCreate() { + SDL_GPUDevice* device = SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV, true, NULL); + if(device == NULL) { + SDL_Log("Failed to create device: %s", SDL_GetError()); + } else { + SDL_Log("Created GPU device using %s", SDL_GetGPUDeviceDriver(device)); + } + + return device; +} diff --git a/src/brimstone/graphics/device.h b/src/brimstone/graphics/device.h new file mode 100644 index 0000000..df20aa8 --- /dev/null +++ b/src/brimstone/graphics/device.h @@ -0,0 +1,8 @@ +#ifndef DEVICE_H +#define DEVICE_H + +#include + +SDL_GPUDevice* DeviceCreate(); + +#endif diff --git a/src/brimstone/graphics/pipeline.c b/src/brimstone/graphics/pipeline.c new file mode 100644 index 0000000..4ec9a09 --- /dev/null +++ b/src/brimstone/graphics/pipeline.c @@ -0,0 +1,62 @@ +#include +#include + +SDL_GPUGraphicsPipeline* GraphicsPipelineCreate(SDL_GPUDevice* device, SDL_Window* window, + SDL_GPUShader* vertex_shader, SDL_GPUShader* fragment_shader) { + assert(device != NULL); + assert(window != NULL); + assert(vertex_shader != NULL); + assert(fragment_shader != NULL); + + SDL_GPUGraphicsPipeline* pipeline = SDL_CreateGPUGraphicsPipeline(device, + &(SDL_GPUGraphicsPipelineCreateInfo){ + .target_info = { + .num_color_targets = 1, + .color_target_descriptions = (SDL_GPUColorTargetDescription[]){ + { + .format = SDL_GetGPUSwapchainTextureFormat(device, window), + }, + }, + }, + .vertex_input_state = (SDL_GPUVertexInputState){ + .num_vertex_buffers = 1, + .vertex_buffer_descriptions = (SDL_GPUVertexBufferDescription[]){ + { + .slot = 0, + .input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX, + .instance_step_rate = 0, + .pitch = sizeof(float) * 3, + }, + { + .slot = 1, + .input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX, + .instance_step_rate = 0, + .pitch = sizeof(float) * 3, + } + }, + .num_vertex_attributes = 2, + .vertex_attributes = (SDL_GPUVertexAttribute[]){ + { + .buffer_slot = 0, + .format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3, + .location = 0, + .offset = 0, + }, + { + .buffer_slot = 1, + .format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3, + .location = 1, + .offset = 0, + } + } + }, + .primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST, + .vertex_shader = vertex_shader, + .fragment_shader = fragment_shader, + }); + if(pipeline == NULL) { + SDL_Log("Failed to create pipeline: %s", SDL_GetError()); + } + + return pipeline; +} diff --git a/src/brimstone/graphics/pipeline.h b/src/brimstone/graphics/pipeline.h new file mode 100644 index 0000000..c2cca64 --- /dev/null +++ b/src/brimstone/graphics/pipeline.h @@ -0,0 +1,9 @@ +#ifndef GRAPHICS_PIPELINE_H +#define GRAPHICS_PIPELINE_H + +#include + +SDL_GPUGraphicsPipeline* GraphicsPipelineCreate(SDL_GPUDevice* device, SDL_Window* window, + SDL_GPUShader* vertex_shader, SDL_GPUShader* fragment_shader); + +#endif diff --git a/src/brimstone/graphics/shaders.c b/src/brimstone/graphics/shaders.c new file mode 100644 index 0000000..6e2a22a --- /dev/null +++ b/src/brimstone/graphics/shaders.c @@ -0,0 +1,20 @@ +#include + +SDL_GPUShader* ShaderLoad(SDL_GPUDevice* device, const char* filename, SDL_GPUShaderStage stage) { + assert(device != NULL); + + FileData file = FileRead(filename); + SDL_GPUShader* shader = SDL_CreateGPUShader(device, + &(SDL_GPUShaderCreateInfo){ + .code = file.data, + .code_size = file.length, + .stage = stage, + .format = SDL_GPU_SHADERFORMAT_SPIRV, + .entrypoint = "main", + }); + if(shader == NULL) { + SDL_Log("Failed to create shader: %s", SDL_GetError()); + } + + return shader; +} diff --git a/src/brimstone/graphics/shaders.h b/src/brimstone/graphics/shaders.h new file mode 100644 index 0000000..1f24c9d --- /dev/null +++ b/src/brimstone/graphics/shaders.h @@ -0,0 +1,8 @@ +#ifndef SHADERS_H +#define SHADERS_H + +#include + +SDL_GPUShader* ShaderLoad(SDL_GPUDevice* device, const char* filename, SDL_GPUShaderStage stage); + +#endif diff --git a/src/brimstone/graphics/upload.c b/src/brimstone/graphics/upload.c new file mode 100644 index 0000000..7b20aef --- /dev/null +++ b/src/brimstone/graphics/upload.c @@ -0,0 +1,53 @@ +#include "brimstone/graphics/command_buffer.h" +#include +#include + +bool UploadToBuffer(SDL_GPUDevice* device, const void* data, size_t byte_size, SDL_GPUBuffer* buffer) { + assert(device != NULL); + assert(data != NULL); + assert(buffer != NULL); + + SDL_GPUTransferBuffer* transfer_buffer = SDL_CreateGPUTransferBuffer(device, + &(SDL_GPUTransferBufferCreateInfo){ + .usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, + .size = byte_size, + }); + if(transfer_buffer == NULL) { + SDL_Log("Failed to create transfer buffer: %s", SDL_GetError()); + return false; + } + + void* mapped_buffer = SDL_MapGPUTransferBuffer(device, transfer_buffer, false); + if(mapped_buffer == NULL) { + SDL_Log("Failed to map GPU transfer buffer: %s", SDL_GetError()); + return false; + } + + memcpy(mapped_buffer, data, byte_size); + + SDL_UnmapGPUTransferBuffer(device, transfer_buffer); + + SDL_GPUCommandBuffer* copy_command_buffer = CommandBufferAcquire(device); + assert(copy_command_buffer != NULL); + + SDL_GPUCopyPass* copy_pass = CopyPassBegin(copy_command_buffer); + assert(copy_pass != NULL); + + SDL_UploadToGPUBuffer(copy_pass, + &(SDL_GPUTransferBufferLocation){ + .transfer_buffer = transfer_buffer, + .offset = 0, + }, + &(SDL_GPUBufferRegion){ + .buffer = buffer, + .offset = 0, + .size = byte_size, + }, + false); + + CopyPassEnd(copy_pass); + bool submit_success = CommandBufferSubmit(copy_command_buffer); + SDL_ReleaseGPUTransferBuffer(device, transfer_buffer); + + return submit_success; +} diff --git a/src/brimstone/graphics/upload.h b/src/brimstone/graphics/upload.h new file mode 100644 index 0000000..6e00f22 --- /dev/null +++ b/src/brimstone/graphics/upload.h @@ -0,0 +1,8 @@ +#ifndef UPLOAD_H +#define UPLOAD_H + +#include + +bool UploadToBuffer(SDL_GPUDevice* device, const void* data, size_t byte_size, SDL_GPUBuffer* buffer); + +#endif diff --git a/src/brimstone/graphics/vertex.h b/src/brimstone/graphics/vertex.h new file mode 100644 index 0000000..d685077 --- /dev/null +++ b/src/brimstone/graphics/vertex.h @@ -0,0 +1,13 @@ +#ifndef VERTEX_H +#define VERTEX_H + +typedef struct { + struct { + float x, y, z; + } pos; + struct { + float r, g, b; + } color; +} Vertex; + +#endif diff --git a/src/brimstone/init/systems.c b/src/brimstone/init/systems.c new file mode 100644 index 0000000..2fe182f --- /dev/null +++ b/src/brimstone/init/systems.c @@ -0,0 +1,8 @@ +#include + +void InitSystems() { + if(!SDL_Init(SDL_INIT_VIDEO)) { + SDL_Log("Failed to initialize SDL: %s", SDL_GetError()); + exit(EXIT_FAILURE); + } +} diff --git a/src/brimstone/init/systems.h b/src/brimstone/init/systems.h new file mode 100644 index 0000000..ba4dcf8 --- /dev/null +++ b/src/brimstone/init/systems.h @@ -0,0 +1,6 @@ +#ifndef INIT_SYSTEMS_H +#define INIT_SYSTEMS_H + +void InitSystems(); + +#endif diff --git a/src/brimstone/io/copy_pass.c b/src/brimstone/io/copy_pass.c new file mode 100644 index 0000000..1c48296 --- /dev/null +++ b/src/brimstone/io/copy_pass.c @@ -0,0 +1,18 @@ +#include + +SDL_GPUCopyPass* CopyPassBegin(SDL_GPUCommandBuffer* copy_command_buffer) { + assert(copy_command_buffer != NULL); + + SDL_GPUCopyPass* copy_pass = SDL_BeginGPUCopyPass(copy_command_buffer); + if(copy_pass == NULL) { + SDL_Log("Failed to begin copy pass: %s", SDL_GetError()); + } + + return copy_pass; +} + +void CopyPassEnd(SDL_GPUCopyPass* copy_pass) { + assert(copy_pass != NULL); + + SDL_EndGPUCopyPass(copy_pass); +} diff --git a/src/brimstone/io/copy_pass.h b/src/brimstone/io/copy_pass.h new file mode 100644 index 0000000..3e7ad1f --- /dev/null +++ b/src/brimstone/io/copy_pass.h @@ -0,0 +1,9 @@ +#ifndef COPY_PASS_H +#define COPY_PASS_H + +#include + +SDL_GPUCopyPass* CopyPassBegin(SDL_GPUCommandBuffer* copy_command_buffer); +void CopyPassEnd(SDL_GPUCopyPass* copy_pass); + +#endif diff --git a/src/brimstone/io/file.c b/src/brimstone/io/file.c new file mode 100644 index 0000000..16ded48 --- /dev/null +++ b/src/brimstone/io/file.c @@ -0,0 +1,32 @@ +#include "pch.h" + +FileData FileRead(const char* filename) { + FILE* f = fopen(filename, "r"); + if(f == NULL) { + perror(filename); + return (FileData){ 0 }; + } + fseek(f, 0, SEEK_END); + size_t length = ftell(f); + rewind(f); + + uint8_t* buffer = malloc(length); + if(buffer == NULL) { + perror("Failed to allocate memory"); + return (FileData){ 0 }; + } + size_t read_count = fread(buffer, sizeof(char), length, f); + if(read_count != length) { + perror("Failed to read entire file"); + return (FileData){ 0 }; + } + + return (FileData){ + .data = buffer, + .length = length, + }; +} + +void FileFree(FileData file) { + free(file.data); +} diff --git a/src/brimstone/io/file.h b/src/brimstone/io/file.h new file mode 100644 index 0000000..b596f90 --- /dev/null +++ b/src/brimstone/io/file.h @@ -0,0 +1,15 @@ +#ifndef FILE_IO_H +#define FILE_IO_H + +#include +#include + +typedef struct { + uint8_t* data; + size_t length; +} FileData; + +FileData FileRead(const char* filename); +void FileFree(FileData file); + +#endif diff --git a/src/brimstone/main.c b/src/brimstone/main.c new file mode 100644 index 0000000..aec2534 --- /dev/null +++ b/src/brimstone/main.c @@ -0,0 +1,119 @@ +#include "brimstone/graphics/pipeline.h" +#include + +int main(int argc, char** argv) { + InitSystems(); + + SDL_GPUDevice* device = DeviceCreate(); + assert(device != NULL); + + SDL_Window* window = SDL_CreateWindow("Brimstone", 800, 600, 0); + if(window == NULL) { + SDL_Log("SDL_CreateWindow: %s", SDL_GetError()); + exit(EXIT_FAILURE); + } + SDL_ClaimWindowForGPUDevice(device, window); + + /*Vertex vertices[] = { + { -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f }, + { 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f }, + { 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f }, + };*/ + float vertices[3][3] = { + { -0.5f, -0.5f, 0.0f }, + { 0.5f, -0.5f, 0.0f }, + { 0.0f, 0.5f, 0.0f }, + }; + + float colors[3][3] = { + { 1.0f, 0.0f, 0.0f }, + { 0.0f, 1.0f, 0.0f }, + { 0.0f, 0.0f, 1.0f }, + }; + + SDL_GPUBuffer* vertex_buffer = SDL_CreateGPUBuffer(device, + &(SDL_GPUBufferCreateInfo){ + .usage = SDL_GPU_BUFFERUSAGE_VERTEX, + .size = sizeof(vertices), + }); + + bool upload_successful = UploadToBuffer(device, vertices, sizeof(vertices), vertex_buffer); + assert(upload_successful); + + SDL_GPUBuffer* color_buffer = SDL_CreateGPUBuffer(device, + &(SDL_GPUBufferCreateInfo){ + .usage = SDL_GPU_BUFFERUSAGE_VERTEX, + .size = sizeof(colors), + }); + + upload_successful = UploadToBuffer(device, colors, sizeof(colors), color_buffer); + assert(upload_successful); + + SDL_GPUShader* vertex_shader = ShaderLoad(device, "shader.vert.spv", SDL_GPU_SHADERSTAGE_VERTEX); + assert(vertex_shader != NULL); + + SDL_GPUShader* fragment_shader = ShaderLoad(device, "shader.frag.spv", SDL_GPU_SHADERSTAGE_FRAGMENT); + assert(fragment_shader != NULL); + + SDL_GPUGraphicsPipeline* pipeline = GraphicsPipelineCreate( + device, window, vertex_shader, fragment_shader); + assert(pipeline != NULL); + + bool running = true; + SDL_Event event; + while(running) { + while(SDL_PollEvent(&event)) { + switch(event.type) { + case SDL_EVENT_QUIT: + running = false; + break; + } + } + + SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer(device); + if(cmd == NULL) { + SDL_Log("SDL_AcquireGPUCommandBuffer: %s", SDL_GetError()); + exit(EXIT_FAILURE); + } + + SDL_GPUTexture* swapchain; + uint32_t swapchain_width, swapchain_height; + SDL_AcquireGPUSwapchainTexture(cmd, window, &swapchain, &swapchain_width, &swapchain_height); + + SDL_GPURenderPass* render_pass = SDL_BeginGPURenderPass(cmd, + &(SDL_GPUColorTargetInfo){ + .texture = swapchain, + .load_op = SDL_GPU_LOADOP_CLEAR, + .clear_color = (SDL_FColor){ .r = 0.1f, .g = 0.3f, .b = 0.4f, .a = 1.0f }, + }, + 1, NULL); + if(render_pass == NULL) { + SDL_Log("SDL_BeginGPURenderPass: %s", SDL_GetError()); + exit(EXIT_FAILURE); + } + + SDL_BindGPUGraphicsPipeline(render_pass, pipeline); + SDL_BindGPUVertexBuffers(render_pass, 0, + (SDL_GPUBufferBinding[]){ + { + .buffer = vertex_buffer, + .offset = 0, + }, + { + .buffer = color_buffer, + .offset = 0, + }, + }, + 2); + SDL_DrawGPUPrimitives(render_pass, 3, 1, 0, 0); + + SDL_EndGPURenderPass(render_pass); + + if(!SDL_SubmitGPUCommandBuffer(cmd)) { + SDL_Log("SDL_SubmitGPUCommandBuffer: %s", SDL_GetError()); + exit(EXIT_FAILURE); + } + } + + return 0; +} diff --git a/src/pch.h b/src/pch.h new file mode 100644 index 0000000..152a8b0 --- /dev/null +++ b/src/pch.h @@ -0,0 +1,30 @@ +#ifndef PCH_H +#define PCH_H + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#endif diff --git a/xmake.lua b/xmake.lua new file mode 100644 index 0000000..8608525 --- /dev/null +++ b/xmake.lua @@ -0,0 +1,12 @@ +add_rules("mode.debug", "mode.release") + +add_requires("stc") + +target("brimstone") + set_kind("binary") + set_rundir(".") + add_files("src/**.c") + add_links("SDL3") + add_includedirs("src") + set_pcheader("src/pch.h") + add_packages("stc")