Compare commits
2 Commits
1220cf6e7a
...
31823101a5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
31823101a5 | ||
|
|
45ca42df99 |
379
src/main.cpp
379
src/main.cpp
@ -23,42 +23,46 @@ using namespace M;
|
||||
|
||||
#define NEAR_PLANE (0.01f)
|
||||
|
||||
SDL_GPUDevice *device;
|
||||
SDL_Window *window;
|
||||
static SDL_GPUDevice *device;
|
||||
static SDL_Window *window;
|
||||
|
||||
SDL_GPUGraphicsPipeline *basic_graphics_pipeline;
|
||||
SDL_GPUGraphicsPipeline *world_graphics_pipeline;
|
||||
SDL_GPUSampler *point_sampler;
|
||||
static SDL_GPUGraphicsPipeline *basic_graphics_pipeline;
|
||||
static SDL_GPUGraphicsPipeline *world_graphics_pipeline;
|
||||
static SDL_GPUSampler *point_sampler;
|
||||
|
||||
SDL_GPUBuffer *vertex_buffer;
|
||||
SDL_GPUBuffer *index_buffer;
|
||||
SDL_GPUBuffer *player_instance_buffer;
|
||||
SDL_GPUBuffer *world_buffer;
|
||||
SDL_GPUBuffer *tile_infos_buffer;
|
||||
static SDL_GPUBuffer *vertex_buffer;
|
||||
static SDL_GPUBuffer *index_buffer;
|
||||
static SDL_GPUBuffer *player_instance_buffer;
|
||||
static SDL_GPUBuffer *world_buffer;
|
||||
static SDL_GPUBuffer *tile_infos_buffer;
|
||||
|
||||
Sint32 window_width;
|
||||
Sint32 window_height;
|
||||
static Sint32 window_width;
|
||||
static Sint32 window_height;
|
||||
|
||||
bool Running = true;
|
||||
static Sint32 msaa_texture_width;
|
||||
static Sint32 msaa_texture_height;
|
||||
static SDL_GPUTexture *msaa_texture;
|
||||
|
||||
M4x4 view_matrix = view (V3_(0.0f, 0.0f, 0.0f), radians(45.0f), 10.0f);
|
||||
M4x4 inverse_view_matrix = inverse_view(V3_(0.0f, 0.0f, 0.0f), radians(45.0f), 10.0f);
|
||||
static bool Running = true;
|
||||
|
||||
M4x4 projection_matrix = projection (radians(45.0f), 16.0f / 9.0f, NEAR_PLANE);
|
||||
M4x4 inverse_projection_matrix = inverse_projection(radians(45.0f), 16.0f / 9.0f, NEAR_PLANE);
|
||||
static M4x4 view_matrix = view (V3_(0.0f, 0.0f, 0.0f), radians(45.0f), 10.0f);
|
||||
static M4x4 inverse_view_matrix = inverse_view(V3_(0.0f, 0.0f, 0.0f), radians(45.0f), 10.0f);
|
||||
|
||||
static M4x4 projection_matrix = projection (radians(45.0f), 16.0f / 9.0f, NEAR_PLANE);
|
||||
static M4x4 inverse_projection_matrix = inverse_projection(radians(45.0f), 16.0f / 9.0f, NEAR_PLANE);
|
||||
|
||||
struct Vertex {
|
||||
V3 pos;
|
||||
};
|
||||
|
||||
Vertex vertices[] = {
|
||||
static Vertex vertices[] = {
|
||||
{{ -0.5f, 0.5f, 0 }},
|
||||
{{ 0.5f, 0.5f, 0 }},
|
||||
{{ 0.5f, -0.5f, 0 }},
|
||||
{{ -0.5f, -0.5f, 0 }},
|
||||
};
|
||||
|
||||
Uint16 indices[] = {
|
||||
static Uint16 indices[] = {
|
||||
0, 1, 2,
|
||||
0, 2, 3,
|
||||
};
|
||||
@ -69,19 +73,19 @@ struct Instance {
|
||||
V4 uv2uv3;
|
||||
};
|
||||
|
||||
Instance player_instance = { { 0.0f, 0.0f }, { 0, 0, 1, 0 }, { 1, 1, 0, 1 }};
|
||||
static Instance player_instance = { { 0.0f, 0.0f }, { 0, 0, 1, 0 }, { 1, 1, 0, 1 }};
|
||||
|
||||
Sint32 map_width;
|
||||
Sint32 map_height;
|
||||
static Sint32 map_width;
|
||||
static Sint32 map_height;
|
||||
|
||||
Uint32* map_tiles;
|
||||
static Uint32* map_tiles;
|
||||
|
||||
struct Player {
|
||||
Sint32 pos_x;
|
||||
Sint32 pos_y;
|
||||
};
|
||||
|
||||
Player player;
|
||||
static Player player;
|
||||
|
||||
struct PerFrame {
|
||||
float aspect_ratio;
|
||||
@ -93,7 +97,7 @@ struct PerFrame {
|
||||
Uint32 map_width;
|
||||
};
|
||||
|
||||
PerFrame per_frame = {
|
||||
static PerFrame per_frame = {
|
||||
.aspect_ratio = 16.0f / 9.0f,
|
||||
.fovy_degrees = 31.0f,
|
||||
.camera_x = 0.0f,
|
||||
@ -110,7 +114,7 @@ typedef struct {
|
||||
V2 uv_max;
|
||||
} TileInfo;
|
||||
|
||||
TileInfo tile_infos[] = {
|
||||
static TileInfo tile_infos[] = {
|
||||
{ 0x0001, "../assets/tiles/error.png" },
|
||||
{ 0x0000, "../assets/tiles/empty.png" },
|
||||
{ 0x0100, "../assets/tiles/grass_1.png" },
|
||||
@ -122,18 +126,18 @@ TileInfo tile_infos[] = {
|
||||
{ 0x0400, "../assets/tiles/grass_ground_1.png" },
|
||||
};
|
||||
|
||||
int tile_atlas_size = 256;
|
||||
smol_atlas_t *tile_atlas;
|
||||
static int tile_atlas_size = 256;
|
||||
static smol_atlas_t *tile_atlas;
|
||||
|
||||
V4 cpu_tile_infos_buffer[SDL_arraysize(tile_infos)];
|
||||
static V4 cpu_tile_infos_buffer[SDL_arraysize(tile_infos)];
|
||||
|
||||
Sint32 selected_tile = -1;
|
||||
Sint32 selected_rotation = 0;
|
||||
static Sint32 selected_tile = -1;
|
||||
static Sint32 selected_rotation = 0;
|
||||
|
||||
bool dragging_tile_change = false;
|
||||
Sint32 drag_start_pos[2];
|
||||
static bool dragging_tile_change = false;
|
||||
static Sint32 drag_start_pos[2];
|
||||
|
||||
void save_map() {
|
||||
static void save_map() {
|
||||
log("Save file is under construction.");
|
||||
FILE* file = fopen("../assets/map/map.sv", "wb");
|
||||
|
||||
@ -167,7 +171,7 @@ void save_map() {
|
||||
log("Saving map was successful.");
|
||||
}
|
||||
|
||||
void load_map() {
|
||||
static void load_map() {
|
||||
log("Load save file.");
|
||||
String file = load_entire_file("../assets/map/map.sv");
|
||||
if (!file.length) {
|
||||
@ -206,7 +210,7 @@ void load_map() {
|
||||
log("Loading map was successful.");
|
||||
}
|
||||
|
||||
bool update_buffer(SDL_GPUBuffer *buffer, Uint32 offset, Uint32 num_bytes, void *data) {
|
||||
static bool update_buffer(SDL_GPUBuffer *buffer, Uint32 offset, Uint32 num_bytes, void *data) {
|
||||
SDL_GPUTransferBufferCreateInfo transfer_buffer_info = {
|
||||
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
|
||||
.size = num_bytes,
|
||||
@ -260,7 +264,7 @@ bool update_buffer(SDL_GPUBuffer *buffer, Uint32 offset, Uint32 num_bytes, void
|
||||
return true;
|
||||
}
|
||||
|
||||
SDL_GPUBuffer *create_buffer(SDL_GPUBufferUsageFlags usage, Uint32 num_bytes, void *data = NULL, const char *name = NULL) {
|
||||
static SDL_GPUBuffer *create_buffer(SDL_GPUBufferUsageFlags usage, Uint32 num_bytes, void *data = NULL, const char *name = NULL) {
|
||||
SDL_PropertiesID properties = 0;
|
||||
if (name) {
|
||||
properties = SDL_CreateProperties();
|
||||
@ -288,7 +292,7 @@ SDL_GPUBuffer *create_buffer(SDL_GPUBufferUsageFlags usage, Uint32 num_bytes, vo
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void change_map_size(char direction, int amount) {
|
||||
static void change_map_size(char direction, int amount) {
|
||||
SDL_GPUBuffer *old_world_buffer = world_buffer;
|
||||
Uint32* old_map = map_tiles;
|
||||
auto old_map_width = map_width;
|
||||
@ -365,7 +369,7 @@ void change_map_size(char direction, int amount) {
|
||||
SDL_ReleaseGPUBuffer(device, old_world_buffer);
|
||||
}
|
||||
|
||||
SDL_GPUTexture *create_shader_texture(const char *path) {
|
||||
static SDL_GPUTexture *create_shader_texture(const char *path) {
|
||||
int width = 0, height = 0, channels = 0;
|
||||
stbi_uc *data = stbi_load(path, &width, &height, &channels, 0);
|
||||
if (!data) {
|
||||
@ -469,7 +473,7 @@ SDL_GPUTexture *create_shader_texture(const char *path) {
|
||||
return texture;
|
||||
}
|
||||
|
||||
SDL_GPUTexture *create_shader_texture(const char *name, const char *data, int width, int height, int channels) {
|
||||
static SDL_GPUTexture *create_shader_texture(const char *name, const char *data, int width, int height, int channels) {
|
||||
SDL_GPUTextureFormat format = SDL_GPU_TEXTUREFORMAT_INVALID;
|
||||
if (channels == 1) format = SDL_GPU_TEXTUREFORMAT_A8_UNORM;
|
||||
if (channels == 4) format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB;
|
||||
@ -561,12 +565,12 @@ SDL_GPUTexture *create_shader_texture(const char *name, const char *data, int wi
|
||||
return texture;
|
||||
}
|
||||
|
||||
void blit(char *dst, Sint32 dst_pitch, Sint32 dst_x, Sint32 dst_y, char *src, Sint32 src_pitch, Sint32 width, Sint32 height, int components = 4) {
|
||||
static void blit(char *dst, Sint32 dst_pitch, Sint32 dst_x, Sint32 dst_y, char *src, Sint32 src_pitch, Sint32 width, Sint32 height, int components = 4) {
|
||||
for (Sint32 y = 0; y < height; y++)
|
||||
memmove(&dst[((dst_y + y) * dst_pitch + dst_x) * components], &src[y * src_pitch * components], width * components);
|
||||
}
|
||||
|
||||
bool SelectableImage(const char *label, bool selected, SDL_GPUTextureSamplerBinding *image, const ImVec2& image_size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), Uint8 orientation = 0) {
|
||||
static bool SelectableImage(const char *label, bool selected, SDL_GPUTextureSamplerBinding *image, const ImVec2& image_size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), Uint8 orientation = 0) {
|
||||
const ImGuiContext *context = ImGui::GetCurrentContext();
|
||||
const ImVec2 padding = context->Style.FramePadding;
|
||||
|
||||
@ -588,7 +592,7 @@ bool SelectableImage(const char *label, bool selected, SDL_GPUTextureSamplerBind
|
||||
return pressed;
|
||||
}
|
||||
|
||||
ImVec4 linear_to_sRGB(ImVec4 linear) {
|
||||
static ImVec4 linear_to_sRGB(ImVec4 linear) {
|
||||
float red = linear.x <= 0.0031308f ? 12.92f * linear.x : 1.055f * powf(linear.x, 1.0f / 2.4f) - 0.055;
|
||||
float green = linear.y <= 0.0031308f ? 12.92f * linear.y : 1.055f * powf(linear.y, 1.0f / 2.4f) - 0.055;
|
||||
float blue = linear.z <= 0.0031308f ? 12.92f * linear.z : 1.055f * powf(linear.z, 1.0f / 2.4f) - 0.055;
|
||||
@ -596,7 +600,7 @@ ImVec4 linear_to_sRGB(ImVec4 linear) {
|
||||
return ImVec4(red, green, blue, linear.w);
|
||||
}
|
||||
|
||||
ImVec4 sRGB_to_linear(ImVec4 linear) {
|
||||
static ImVec4 sRGB_to_linear(ImVec4 linear) {
|
||||
float red = linear.x <= 0.0031308f ? linear.x / 12.92f : powf((linear.x + 0.055) / 1.055, 2.4f);
|
||||
float green = linear.y <= 0.0031308f ? linear.y / 12.92f : powf((linear.y + 0.055) / 1.055, 2.4f);
|
||||
float blue = linear.z <= 0.0031308f ? linear.z / 12.92f : powf((linear.z + 0.055) / 1.055, 2.4f);
|
||||
@ -604,14 +608,14 @@ ImVec4 sRGB_to_linear(ImVec4 linear) {
|
||||
return ImVec4(red, green, blue, linear.w);
|
||||
}
|
||||
|
||||
V3 Unproject(V3 screen_pos) {
|
||||
static V3 Unproject(V3 screen_pos) {
|
||||
V4 result = inverse_view_matrix * inverse_projection_matrix * V4_(screen_pos, 1.0f);
|
||||
result.xyz /= result.w;
|
||||
|
||||
return result.xyz;
|
||||
}
|
||||
|
||||
V2 get_floor_intersection_of_mouse(V2 mouse_pos) {
|
||||
static V2 get_floor_intersection_of_mouse(V2 mouse_pos) {
|
||||
V2 mouse = remap(V2{ 0, 0 }, V2{ (float)window_width, (float)window_height }, V2{ -1, 1 }, V2{ 1, -1 }, V2{ mouse_pos.x, mouse_pos.y });
|
||||
V3 camera_position = (inverse_view_matrix * V4_(0, 0, 0, 1)).xyz;
|
||||
|
||||
@ -627,12 +631,12 @@ V2 get_floor_intersection_of_mouse(V2 mouse_pos) {
|
||||
}
|
||||
|
||||
#ifdef TRACY_ENABLE
|
||||
SDL_malloc_func sdl_malloc = NULL;
|
||||
SDL_calloc_func sdl_calloc = NULL;
|
||||
SDL_realloc_func sdl_realloc = NULL;
|
||||
SDL_free_func sdl_free = NULL;
|
||||
static SDL_malloc_func sdl_malloc = NULL;
|
||||
static SDL_calloc_func sdl_calloc = NULL;
|
||||
static SDL_realloc_func sdl_realloc = NULL;
|
||||
static SDL_free_func sdl_free = NULL;
|
||||
|
||||
void setup_memory_functions() {
|
||||
static void setup_memory_functions() {
|
||||
SDL_GetMemoryFunctions(&sdl_malloc, &sdl_calloc, &sdl_realloc, &sdl_free);
|
||||
SDL_SetMemoryFunctions(
|
||||
[](size_t size) -> void * {
|
||||
@ -658,108 +662,13 @@ void setup_memory_functions() {
|
||||
);
|
||||
}
|
||||
#else
|
||||
void setup_memory_functions() {}
|
||||
static void setup_memory_functions() {}
|
||||
#endif
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
setup_memory_functions();
|
||||
|
||||
load_map();
|
||||
|
||||
#ifdef SDL_PLATFORM_LINUX
|
||||
if (!getenv("ENABLE_VULKAN_RENDERDOC_CAPTURE"))
|
||||
SDL_SetHint(SDL_HINT_VIDEO_DRIVER, "wayland,x11");
|
||||
#endif
|
||||
|
||||
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
|
||||
log_error("Failed to initialize SDL (%s). Exiting.", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
device = SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV, true, NULL);
|
||||
if (!device) {
|
||||
log_error("Failed to create gpu device (%s). Exiting.", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
SDL_SetGPUAllowedFramesInFlight(device, 1);
|
||||
|
||||
window = SDL_CreateWindow("Mikemon", 1280, 720, SDL_WINDOW_RESIZABLE);
|
||||
if (!window) {
|
||||
log_error("Failed to create window (%s). Exiting.", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!SDL_ClaimWindowForGPUDevice(device, window)) {
|
||||
log_error("Failed to claim window for gpu device (%s). Exiting.", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
SDL_SetGPUSwapchainParameters(device, window, SDL_GPU_SWAPCHAINCOMPOSITION_SDR_LINEAR, SDL_GPU_PRESENTMODE_VSYNC);
|
||||
|
||||
SDL_GPUShaderCreateInfo basic_vertex_shader_info = {
|
||||
.code_size = SDL_arraysize(SPIRV_basic),
|
||||
.code = SPIRV_basic,
|
||||
.entrypoint = "main_vertex",
|
||||
.format = SDL_GPU_SHADERFORMAT_SPIRV,
|
||||
.stage = SDL_GPU_SHADERSTAGE_VERTEX,
|
||||
|
||||
.num_uniform_buffers = 1,
|
||||
};
|
||||
|
||||
SDL_GPUShader *basic_vertex_shader = SDL_CreateGPUShader(device, &basic_vertex_shader_info);
|
||||
if (!basic_vertex_shader) {
|
||||
log_error("Failed to create basic vertex shader. Exiting.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_GPUShaderCreateInfo basic_pixel_shader_info = {
|
||||
.code_size = SDL_arraysize(SPIRV_basic),
|
||||
.code = SPIRV_basic,
|
||||
.entrypoint = "main_fragment",
|
||||
.format = SDL_GPU_SHADERFORMAT_SPIRV,
|
||||
.stage = SDL_GPU_SHADERSTAGE_FRAGMENT,
|
||||
|
||||
.num_samplers = 1,
|
||||
};
|
||||
|
||||
SDL_GPUShader *basic_pixel_shader = SDL_CreateGPUShader(device, &basic_pixel_shader_info);
|
||||
if (!basic_pixel_shader) {
|
||||
log_error("Failed to create basic pixel shader. Exiting.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_GPUShaderCreateInfo world_vertex_shader_info = {
|
||||
.code_size = SDL_arraysize(SPIRV_world),
|
||||
.code = SPIRV_world,
|
||||
.entrypoint = "main_vertex",
|
||||
.format = SDL_GPU_SHADERFORMAT_SPIRV,
|
||||
.stage = SDL_GPU_SHADERSTAGE_VERTEX,
|
||||
|
||||
.num_storage_buffers = 1,
|
||||
.num_uniform_buffers = 1,
|
||||
};
|
||||
|
||||
SDL_GPUShader *world_vertex_shader = SDL_CreateGPUShader(device, &world_vertex_shader_info);
|
||||
if (!world_vertex_shader) {
|
||||
log_error("Failed to create world vertex shader. Exiting.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_GPUShaderCreateInfo world_pixel_shader_info = {
|
||||
.code_size = SDL_arraysize(SPIRV_world),
|
||||
.code = SPIRV_world,
|
||||
.entrypoint = "main_fragment",
|
||||
.format = SDL_GPU_SHADERFORMAT_SPIRV,
|
||||
.stage = SDL_GPU_SHADERSTAGE_FRAGMENT,
|
||||
|
||||
.num_samplers = 1,
|
||||
};
|
||||
|
||||
SDL_GPUShader *world_pixel_shader = SDL_CreateGPUShader(device, &world_pixel_shader_info);
|
||||
if (!world_pixel_shader) {
|
||||
log_error("Failed to create world pixel shader. Exiting.");
|
||||
return 1;
|
||||
}
|
||||
bool enable_msaa = false;
|
||||
SDL_GPUSampleCount highest_supported_sample_count = SDL_GPU_SAMPLECOUNT_1;
|
||||
|
||||
static bool recreate_graphics_pipelines() {
|
||||
SDL_GPUColorTargetDescription color_target_descriptions[] = {
|
||||
{
|
||||
.format = SDL_GetGPUSwapchainTextureFormat(device, window),
|
||||
@ -777,7 +686,39 @@ int main(int argc, char **argv) {
|
||||
},
|
||||
};
|
||||
|
||||
{
|
||||
{ // basic_graphics_pipeline
|
||||
SDL_GPUShaderCreateInfo basic_vertex_shader_info = {
|
||||
.code_size = SDL_arraysize(SPIRV_basic),
|
||||
.code = SPIRV_basic,
|
||||
.entrypoint = "main_vertex",
|
||||
.format = SDL_GPU_SHADERFORMAT_SPIRV,
|
||||
.stage = SDL_GPU_SHADERSTAGE_VERTEX,
|
||||
|
||||
.num_uniform_buffers = 1,
|
||||
};
|
||||
|
||||
SDL_GPUShader *basic_vertex_shader = SDL_CreateGPUShader(device, &basic_vertex_shader_info);
|
||||
if (!basic_vertex_shader) {
|
||||
log_error("Failed to create basic vertex shader. Exiting.");
|
||||
return false;
|
||||
}
|
||||
|
||||
SDL_GPUShaderCreateInfo basic_pixel_shader_info = {
|
||||
.code_size = SDL_arraysize(SPIRV_basic),
|
||||
.code = SPIRV_basic,
|
||||
.entrypoint = "main_fragment",
|
||||
.format = SDL_GPU_SHADERFORMAT_SPIRV,
|
||||
.stage = SDL_GPU_SHADERSTAGE_FRAGMENT,
|
||||
|
||||
.num_samplers = 1,
|
||||
};
|
||||
|
||||
SDL_GPUShader *basic_pixel_shader = SDL_CreateGPUShader(device, &basic_pixel_shader_info);
|
||||
if (!basic_pixel_shader) {
|
||||
log_error("Failed to create basic pixel shader. Exiting.");
|
||||
return false;
|
||||
}
|
||||
|
||||
SDL_GPUVertexBufferDescription vertex_buffer_descriptions[] = {
|
||||
{
|
||||
.slot = 0,
|
||||
@ -835,12 +776,20 @@ int main(int argc, char **argv) {
|
||||
.front_face = SDL_GPU_FRONTFACE_CLOCKWISE,
|
||||
.enable_depth_clip = true,
|
||||
},
|
||||
.multisample_state = {
|
||||
.sample_count = enable_msaa ? highest_supported_sample_count : SDL_GPU_SAMPLECOUNT_1,
|
||||
},
|
||||
.target_info = {
|
||||
.color_target_descriptions = color_target_descriptions,
|
||||
.num_color_targets = SDL_arraysize(color_target_descriptions),
|
||||
},
|
||||
};
|
||||
|
||||
if (basic_graphics_pipeline) {
|
||||
SDL_ReleaseGPUGraphicsPipeline(device, basic_graphics_pipeline);
|
||||
basic_graphics_pipeline = NULL;
|
||||
}
|
||||
|
||||
basic_graphics_pipeline = SDL_CreateGPUGraphicsPipeline(device, &basic_graphics_pipeline_info);
|
||||
if (!basic_graphics_pipeline) {
|
||||
log_error("Failed to create basic graphics pipeline. Exiting.");
|
||||
@ -851,7 +800,40 @@ int main(int argc, char **argv) {
|
||||
SDL_ReleaseGPUShader(device, basic_pixel_shader);
|
||||
}
|
||||
|
||||
{
|
||||
{ // world_graphics_pipeline
|
||||
SDL_GPUShaderCreateInfo world_vertex_shader_info = {
|
||||
.code_size = SDL_arraysize(SPIRV_world),
|
||||
.code = SPIRV_world,
|
||||
.entrypoint = "main_vertex",
|
||||
.format = SDL_GPU_SHADERFORMAT_SPIRV,
|
||||
.stage = SDL_GPU_SHADERSTAGE_VERTEX,
|
||||
|
||||
.num_storage_buffers = 1,
|
||||
.num_uniform_buffers = 1,
|
||||
};
|
||||
|
||||
SDL_GPUShader *world_vertex_shader = SDL_CreateGPUShader(device, &world_vertex_shader_info);
|
||||
if (!world_vertex_shader) {
|
||||
log_error("Failed to create world vertex shader. Exiting.");
|
||||
return false;
|
||||
}
|
||||
|
||||
SDL_GPUShaderCreateInfo world_pixel_shader_info = {
|
||||
.code_size = SDL_arraysize(SPIRV_world),
|
||||
.code = SPIRV_world,
|
||||
.entrypoint = "main_fragment",
|
||||
.format = SDL_GPU_SHADERFORMAT_SPIRV,
|
||||
.stage = SDL_GPU_SHADERSTAGE_FRAGMENT,
|
||||
|
||||
.num_samplers = 1,
|
||||
};
|
||||
|
||||
SDL_GPUShader *world_pixel_shader = SDL_CreateGPUShader(device, &world_pixel_shader_info);
|
||||
if (!world_pixel_shader) {
|
||||
log_error("Failed to create world pixel shader. Exiting.");
|
||||
return false;
|
||||
}
|
||||
|
||||
SDL_GPUVertexBufferDescription vertex_buffer_descriptions[] = {
|
||||
{
|
||||
.slot = 0,
|
||||
@ -897,12 +879,20 @@ int main(int argc, char **argv) {
|
||||
.front_face = SDL_GPU_FRONTFACE_CLOCKWISE,
|
||||
.enable_depth_clip = true,
|
||||
},
|
||||
.multisample_state = {
|
||||
.sample_count = enable_msaa ? highest_supported_sample_count : SDL_GPU_SAMPLECOUNT_1,
|
||||
},
|
||||
.target_info = {
|
||||
.color_target_descriptions = color_target_descriptions,
|
||||
.num_color_targets = SDL_arraysize(color_target_descriptions),
|
||||
},
|
||||
};
|
||||
|
||||
if (world_graphics_pipeline) {
|
||||
SDL_ReleaseGPUGraphicsPipeline(device, world_graphics_pipeline);
|
||||
world_graphics_pipeline = NULL;
|
||||
}
|
||||
|
||||
world_graphics_pipeline = SDL_CreateGPUGraphicsPipeline(device, &world_graphics_pipeline_info);
|
||||
if (!world_graphics_pipeline) {
|
||||
log_error("Failed to create world graphics pipeline. Exiting.");
|
||||
@ -913,6 +903,50 @@ int main(int argc, char **argv) {
|
||||
SDL_ReleaseGPUShader(device, world_pixel_shader);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
setup_memory_functions();
|
||||
|
||||
load_map();
|
||||
|
||||
#ifdef SDL_PLATFORM_LINUX
|
||||
if (!getenv("ENABLE_VULKAN_RENDERDOC_CAPTURE"))
|
||||
SDL_SetHint(SDL_HINT_VIDEO_DRIVER, "wayland,x11");
|
||||
#endif
|
||||
|
||||
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
|
||||
log_error("Failed to initialize SDL (%s). Exiting.", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
device = SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV, true, NULL);
|
||||
if (!device) {
|
||||
log_error("Failed to create gpu device (%s). Exiting.", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
SDL_SetGPUAllowedFramesInFlight(device, 1);
|
||||
|
||||
window = SDL_CreateWindow("Mikemon", 1280, 720, SDL_WINDOW_RESIZABLE);
|
||||
if (!window) {
|
||||
log_error("Failed to create window (%s). Exiting.", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!SDL_ClaimWindowForGPUDevice(device, window)) {
|
||||
log_error("Failed to claim window for gpu device (%s). Exiting.", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
SDL_SetGPUSwapchainParameters(device, window, SDL_GPU_SWAPCHAINCOMPOSITION_SDR_LINEAR, SDL_GPU_PRESENTMODE_VSYNC);
|
||||
|
||||
SDL_GPUTextureFormat swapchain_format = SDL_GetGPUSwapchainTextureFormat(device, window);
|
||||
highest_supported_sample_count = SDL_GPUTextureSupportsSampleCount(device, swapchain_format, SDL_GPU_SAMPLECOUNT_2) ? SDL_GPU_SAMPLECOUNT_2 : highest_supported_sample_count;
|
||||
highest_supported_sample_count = SDL_GPUTextureSupportsSampleCount(device, swapchain_format, SDL_GPU_SAMPLECOUNT_4) ? SDL_GPU_SAMPLECOUNT_4 : highest_supported_sample_count;
|
||||
highest_supported_sample_count = SDL_GPUTextureSupportsSampleCount(device, swapchain_format, SDL_GPU_SAMPLECOUNT_8) ? SDL_GPU_SAMPLECOUNT_8 : highest_supported_sample_count;
|
||||
|
||||
recreate_graphics_pipelines();
|
||||
|
||||
SDL_GPUTexture *player_texture = create_shader_texture("../assets/decorations/strawberry.png");
|
||||
if (!player_texture) {
|
||||
log_error("Failed to create shader texture. Exiting.");
|
||||
@ -1115,6 +1149,14 @@ int main(int argc, char **argv) {
|
||||
ImGui::DragFloat("fovy", &per_frame.fovy_degrees);
|
||||
ImGui::DragFloat("camera_distance", &per_frame.camera_distance, 0.25f, 1.0f, INFINITY);
|
||||
ImGui::DragFloat("camera_tilt", &per_frame.camera_tilt, 0.25f, 0.0f, 89.0f);
|
||||
|
||||
if (ImGui::Checkbox("MSAA", &enable_msaa)) {
|
||||
recreate_graphics_pipelines();
|
||||
if (msaa_texture) {
|
||||
SDL_ReleaseGPUTexture(device, msaa_texture);
|
||||
msaa_texture = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
ImGui::End();
|
||||
|
||||
@ -1277,6 +1319,33 @@ int main(int argc, char **argv) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (enable_msaa && (!msaa_texture || msaa_texture_width != width || msaa_texture_height != height)) {
|
||||
if (msaa_texture) {
|
||||
SDL_ReleaseGPUTexture(device, msaa_texture);
|
||||
msaa_texture = NULL;
|
||||
}
|
||||
|
||||
SDL_GPUTextureCreateInfo texture_info = {
|
||||
.type = SDL_GPU_TEXTURETYPE_2D,
|
||||
.format = swapchain_format,
|
||||
.usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET,
|
||||
.width = (Uint32)width,
|
||||
.height = (Uint32)height,
|
||||
|
||||
.layer_count_or_depth = 1,
|
||||
.num_levels = 1,
|
||||
.sample_count = highest_supported_sample_count,
|
||||
};
|
||||
|
||||
msaa_texture = SDL_CreateGPUTexture(device, &texture_info);
|
||||
if (!msaa_texture) {
|
||||
log_error("Failed to create gpu texture (%s).", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
msaa_texture_width = width;
|
||||
msaa_texture_height = height;
|
||||
}
|
||||
|
||||
{
|
||||
ZoneScopedN("Update");
|
||||
{
|
||||
@ -1308,10 +1377,12 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
|
||||
SDL_GPUColorTargetInfo color_target_info = {
|
||||
.texture = swapchain_texture,
|
||||
.texture = enable_msaa ? msaa_texture : swapchain_texture,
|
||||
.clear_color = { .r = 1.0f, .g = 0.0f, .b = 1.0f, .a = 1.0f },
|
||||
.load_op = SDL_GPU_LOADOP_CLEAR,
|
||||
.store_op = SDL_GPU_STOREOP_STORE,
|
||||
.store_op = enable_msaa ? SDL_GPU_STOREOP_RESOLVE : SDL_GPU_STOREOP_STORE,
|
||||
.resolve_texture = swapchain_texture,
|
||||
.cycle = true,
|
||||
};
|
||||
|
||||
SDL_GPURenderPass *render_pass = SDL_BeginGPURenderPass(command_buffer, &color_target_info, 1, NULL);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user