first commit

This commit is contained in:
sam 2024-11-04 12:32:36 +13:00
commit fd310560f9
46 changed files with 553 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

25
.clang-format Normal file
View file

@ -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

8
.gitignore vendored Normal file
View file

@ -0,0 +1,8 @@
# Xmake cache
.xmake/
build/
# MacOS Cache
.DS_Store

21
compile_commands.json Normal file
View file

@ -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"
}]

10
shader.frag Normal file
View file

@ -0,0 +1,10 @@
#version 460
layout(location = 0) in vec4 vertColor;
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = vertColor;
}

BIN
shader.frag.spv Normal file

Binary file not shown.

BIN
shader.spv Normal file

Binary file not shown.

12
shader.vert Normal file
View file

@ -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);
}

BIN
shader.vert.spv Normal file

Binary file not shown.

View file

@ -0,0 +1,24 @@
#include <SDL3/SDL_gpu.h>
#include <pch.h>
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;
}

View file

@ -0,0 +1,9 @@
#ifndef COMMAND_BUFFER_H
#define COMMAND_BUFFER_H
#include <SDL3/SDL_gpu.h>
SDL_GPUCommandBuffer* CommandBufferAcquire(SDL_GPUDevice* device);
bool CommandBufferSubmit(SDL_GPUCommandBuffer* command_buffer);
#endif

View file

@ -0,0 +1,14 @@
#include <SDL3/SDL_gpu.h>
#include <SDL3/SDL_log.h>
#include <pch.h>
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;
}

View file

@ -0,0 +1,8 @@
#ifndef DEVICE_H
#define DEVICE_H
#include <SDL3/SDL_gpu.h>
SDL_GPUDevice* DeviceCreate();
#endif

View file

@ -0,0 +1,62 @@
#include <SDL3/SDL_gpu.h>
#include <pch.h>
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;
}

View file

@ -0,0 +1,9 @@
#ifndef GRAPHICS_PIPELINE_H
#define GRAPHICS_PIPELINE_H
#include <SDL3/SDL_gpu.h>
SDL_GPUGraphicsPipeline* GraphicsPipelineCreate(SDL_GPUDevice* device, SDL_Window* window,
SDL_GPUShader* vertex_shader, SDL_GPUShader* fragment_shader);
#endif

View file

@ -0,0 +1,20 @@
#include <pch.h>
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;
}

View file

@ -0,0 +1,8 @@
#ifndef SHADERS_H
#define SHADERS_H
#include <SDL3/SDL_gpu.h>
SDL_GPUShader* ShaderLoad(SDL_GPUDevice* device, const char* filename, SDL_GPUShaderStage stage);
#endif

View file

@ -0,0 +1,53 @@
#include "brimstone/graphics/command_buffer.h"
#include <SDL3/SDL_gpu.h>
#include <pch.h>
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;
}

View file

@ -0,0 +1,8 @@
#ifndef UPLOAD_H
#define UPLOAD_H
#include <SDL3/SDL_gpu.h>
bool UploadToBuffer(SDL_GPUDevice* device, const void* data, size_t byte_size, SDL_GPUBuffer* buffer);
#endif

View file

@ -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

View file

@ -0,0 +1,8 @@
#include <pch.h>
void InitSystems() {
if(!SDL_Init(SDL_INIT_VIDEO)) {
SDL_Log("Failed to initialize SDL: %s", SDL_GetError());
exit(EXIT_FAILURE);
}
}

View file

@ -0,0 +1,6 @@
#ifndef INIT_SYSTEMS_H
#define INIT_SYSTEMS_H
void InitSystems();
#endif

View file

@ -0,0 +1,18 @@
#include <pch.h>
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);
}

View file

@ -0,0 +1,9 @@
#ifndef COPY_PASS_H
#define COPY_PASS_H
#include <SDL3/SDL_gpu.h>
SDL_GPUCopyPass* CopyPassBegin(SDL_GPUCommandBuffer* copy_command_buffer);
void CopyPassEnd(SDL_GPUCopyPass* copy_pass);
#endif

32
src/brimstone/io/file.c Normal file
View file

@ -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);
}

15
src/brimstone/io/file.h Normal file
View file

@ -0,0 +1,15 @@
#ifndef FILE_IO_H
#define FILE_IO_H
#include <stddef.h>
#include <stdint.h>
typedef struct {
uint8_t* data;
size_t length;
} FileData;
FileData FileRead(const char* filename);
void FileFree(FileData file);
#endif

119
src/brimstone/main.c Normal file
View file

@ -0,0 +1,119 @@
#include "brimstone/graphics/pipeline.h"
#include <pch.h>
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;
}

30
src/pch.h Normal file
View file

@ -0,0 +1,30 @@
#ifndef PCH_H
#define PCH_H
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_gpu.h>
#include <SDL3/SDL_init.h>
#include <SDL3/SDL_log.h>
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_pixels.h>
#include <SDL3/SDL_video.h>
#include <brimstone/graphics/command_buffer.h>
#include <brimstone/graphics/device.h>
#include <brimstone/graphics/pipeline.h>
#include <brimstone/graphics/shaders.h>
#include <brimstone/graphics/upload.h>
#include <brimstone/graphics/vertex.h>
#include <brimstone/init/systems.h>
#include <brimstone/io/copy_pass.h>
#include <brimstone/io/file.h>
#endif

12
xmake.lua Normal file
View file

@ -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")