Compare commits
No commits in common. "31823101a565b0599e171b1650a0f98a712480e2" and "1220cf6e7af3b99390d636803f1462f8df8baa16" have entirely different histories.
31823101a5
...
1220cf6e7a
379
src/main.cpp
379
src/main.cpp
@ -23,46 +23,42 @@ using namespace M;
|
|||||||
|
|
||||||
#define NEAR_PLANE (0.01f)
|
#define NEAR_PLANE (0.01f)
|
||||||
|
|
||||||
static SDL_GPUDevice *device;
|
SDL_GPUDevice *device;
|
||||||
static SDL_Window *window;
|
SDL_Window *window;
|
||||||
|
|
||||||
static SDL_GPUGraphicsPipeline *basic_graphics_pipeline;
|
SDL_GPUGraphicsPipeline *basic_graphics_pipeline;
|
||||||
static SDL_GPUGraphicsPipeline *world_graphics_pipeline;
|
SDL_GPUGraphicsPipeline *world_graphics_pipeline;
|
||||||
static SDL_GPUSampler *point_sampler;
|
SDL_GPUSampler *point_sampler;
|
||||||
|
|
||||||
static SDL_GPUBuffer *vertex_buffer;
|
SDL_GPUBuffer *vertex_buffer;
|
||||||
static SDL_GPUBuffer *index_buffer;
|
SDL_GPUBuffer *index_buffer;
|
||||||
static SDL_GPUBuffer *player_instance_buffer;
|
SDL_GPUBuffer *player_instance_buffer;
|
||||||
static SDL_GPUBuffer *world_buffer;
|
SDL_GPUBuffer *world_buffer;
|
||||||
static SDL_GPUBuffer *tile_infos_buffer;
|
SDL_GPUBuffer *tile_infos_buffer;
|
||||||
|
|
||||||
static Sint32 window_width;
|
Sint32 window_width;
|
||||||
static Sint32 window_height;
|
Sint32 window_height;
|
||||||
|
|
||||||
static Sint32 msaa_texture_width;
|
bool Running = true;
|
||||||
static Sint32 msaa_texture_height;
|
|
||||||
static SDL_GPUTexture *msaa_texture;
|
|
||||||
|
|
||||||
static bool Running = true;
|
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 M4x4 view_matrix = view (V3_(0.0f, 0.0f, 0.0f), radians(45.0f), 10.0f);
|
M4x4 projection_matrix = projection (radians(45.0f), 16.0f / 9.0f, NEAR_PLANE);
|
||||||
static M4x4 inverse_view_matrix = inverse_view(V3_(0.0f, 0.0f, 0.0f), radians(45.0f), 10.0f);
|
M4x4 inverse_projection_matrix = inverse_projection(radians(45.0f), 16.0f / 9.0f, NEAR_PLANE);
|
||||||
|
|
||||||
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 {
|
struct Vertex {
|
||||||
V3 pos;
|
V3 pos;
|
||||||
};
|
};
|
||||||
|
|
||||||
static Vertex vertices[] = {
|
Vertex vertices[] = {
|
||||||
{{ -0.5f, 0.5f, 0 }},
|
{{ -0.5f, 0.5f, 0 }},
|
||||||
{{ 0.5f, 0.5f, 0 }},
|
{{ 0.5f, 0.5f, 0 }},
|
||||||
{{ 0.5f, -0.5f, 0 }},
|
{{ 0.5f, -0.5f, 0 }},
|
||||||
{{ -0.5f, -0.5f, 0 }},
|
{{ -0.5f, -0.5f, 0 }},
|
||||||
};
|
};
|
||||||
|
|
||||||
static Uint16 indices[] = {
|
Uint16 indices[] = {
|
||||||
0, 1, 2,
|
0, 1, 2,
|
||||||
0, 2, 3,
|
0, 2, 3,
|
||||||
};
|
};
|
||||||
@ -73,19 +69,19 @@ struct Instance {
|
|||||||
V4 uv2uv3;
|
V4 uv2uv3;
|
||||||
};
|
};
|
||||||
|
|
||||||
static Instance player_instance = { { 0.0f, 0.0f }, { 0, 0, 1, 0 }, { 1, 1, 0, 1 }};
|
Instance player_instance = { { 0.0f, 0.0f }, { 0, 0, 1, 0 }, { 1, 1, 0, 1 }};
|
||||||
|
|
||||||
static Sint32 map_width;
|
Sint32 map_width;
|
||||||
static Sint32 map_height;
|
Sint32 map_height;
|
||||||
|
|
||||||
static Uint32* map_tiles;
|
Uint32* map_tiles;
|
||||||
|
|
||||||
struct Player {
|
struct Player {
|
||||||
Sint32 pos_x;
|
Sint32 pos_x;
|
||||||
Sint32 pos_y;
|
Sint32 pos_y;
|
||||||
};
|
};
|
||||||
|
|
||||||
static Player player;
|
Player player;
|
||||||
|
|
||||||
struct PerFrame {
|
struct PerFrame {
|
||||||
float aspect_ratio;
|
float aspect_ratio;
|
||||||
@ -97,7 +93,7 @@ struct PerFrame {
|
|||||||
Uint32 map_width;
|
Uint32 map_width;
|
||||||
};
|
};
|
||||||
|
|
||||||
static PerFrame per_frame = {
|
PerFrame per_frame = {
|
||||||
.aspect_ratio = 16.0f / 9.0f,
|
.aspect_ratio = 16.0f / 9.0f,
|
||||||
.fovy_degrees = 31.0f,
|
.fovy_degrees = 31.0f,
|
||||||
.camera_x = 0.0f,
|
.camera_x = 0.0f,
|
||||||
@ -114,7 +110,7 @@ typedef struct {
|
|||||||
V2 uv_max;
|
V2 uv_max;
|
||||||
} TileInfo;
|
} TileInfo;
|
||||||
|
|
||||||
static TileInfo tile_infos[] = {
|
TileInfo tile_infos[] = {
|
||||||
{ 0x0001, "../assets/tiles/error.png" },
|
{ 0x0001, "../assets/tiles/error.png" },
|
||||||
{ 0x0000, "../assets/tiles/empty.png" },
|
{ 0x0000, "../assets/tiles/empty.png" },
|
||||||
{ 0x0100, "../assets/tiles/grass_1.png" },
|
{ 0x0100, "../assets/tiles/grass_1.png" },
|
||||||
@ -126,18 +122,18 @@ static TileInfo tile_infos[] = {
|
|||||||
{ 0x0400, "../assets/tiles/grass_ground_1.png" },
|
{ 0x0400, "../assets/tiles/grass_ground_1.png" },
|
||||||
};
|
};
|
||||||
|
|
||||||
static int tile_atlas_size = 256;
|
int tile_atlas_size = 256;
|
||||||
static smol_atlas_t *tile_atlas;
|
smol_atlas_t *tile_atlas;
|
||||||
|
|
||||||
static V4 cpu_tile_infos_buffer[SDL_arraysize(tile_infos)];
|
V4 cpu_tile_infos_buffer[SDL_arraysize(tile_infos)];
|
||||||
|
|
||||||
static Sint32 selected_tile = -1;
|
Sint32 selected_tile = -1;
|
||||||
static Sint32 selected_rotation = 0;
|
Sint32 selected_rotation = 0;
|
||||||
|
|
||||||
static bool dragging_tile_change = false;
|
bool dragging_tile_change = false;
|
||||||
static Sint32 drag_start_pos[2];
|
Sint32 drag_start_pos[2];
|
||||||
|
|
||||||
static void save_map() {
|
void save_map() {
|
||||||
log("Save file is under construction.");
|
log("Save file is under construction.");
|
||||||
FILE* file = fopen("../assets/map/map.sv", "wb");
|
FILE* file = fopen("../assets/map/map.sv", "wb");
|
||||||
|
|
||||||
@ -171,7 +167,7 @@ static void save_map() {
|
|||||||
log("Saving map was successful.");
|
log("Saving map was successful.");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void load_map() {
|
void load_map() {
|
||||||
log("Load save file.");
|
log("Load save file.");
|
||||||
String file = load_entire_file("../assets/map/map.sv");
|
String file = load_entire_file("../assets/map/map.sv");
|
||||||
if (!file.length) {
|
if (!file.length) {
|
||||||
@ -210,7 +206,7 @@ static void load_map() {
|
|||||||
log("Loading map was successful.");
|
log("Loading map was successful.");
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool update_buffer(SDL_GPUBuffer *buffer, Uint32 offset, Uint32 num_bytes, void *data) {
|
bool update_buffer(SDL_GPUBuffer *buffer, Uint32 offset, Uint32 num_bytes, void *data) {
|
||||||
SDL_GPUTransferBufferCreateInfo transfer_buffer_info = {
|
SDL_GPUTransferBufferCreateInfo transfer_buffer_info = {
|
||||||
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
|
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
|
||||||
.size = num_bytes,
|
.size = num_bytes,
|
||||||
@ -264,7 +260,7 @@ static bool update_buffer(SDL_GPUBuffer *buffer, Uint32 offset, Uint32 num_bytes
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static SDL_GPUBuffer *create_buffer(SDL_GPUBufferUsageFlags usage, Uint32 num_bytes, void *data = NULL, const char *name = NULL) {
|
SDL_GPUBuffer *create_buffer(SDL_GPUBufferUsageFlags usage, Uint32 num_bytes, void *data = NULL, const char *name = NULL) {
|
||||||
SDL_PropertiesID properties = 0;
|
SDL_PropertiesID properties = 0;
|
||||||
if (name) {
|
if (name) {
|
||||||
properties = SDL_CreateProperties();
|
properties = SDL_CreateProperties();
|
||||||
@ -292,7 +288,7 @@ static SDL_GPUBuffer *create_buffer(SDL_GPUBufferUsageFlags usage, Uint32 num_by
|
|||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void change_map_size(char direction, int amount) {
|
void change_map_size(char direction, int amount) {
|
||||||
SDL_GPUBuffer *old_world_buffer = world_buffer;
|
SDL_GPUBuffer *old_world_buffer = world_buffer;
|
||||||
Uint32* old_map = map_tiles;
|
Uint32* old_map = map_tiles;
|
||||||
auto old_map_width = map_width;
|
auto old_map_width = map_width;
|
||||||
@ -369,7 +365,7 @@ static void change_map_size(char direction, int amount) {
|
|||||||
SDL_ReleaseGPUBuffer(device, old_world_buffer);
|
SDL_ReleaseGPUBuffer(device, old_world_buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
static SDL_GPUTexture *create_shader_texture(const char *path) {
|
SDL_GPUTexture *create_shader_texture(const char *path) {
|
||||||
int width = 0, height = 0, channels = 0;
|
int width = 0, height = 0, channels = 0;
|
||||||
stbi_uc *data = stbi_load(path, &width, &height, &channels, 0);
|
stbi_uc *data = stbi_load(path, &width, &height, &channels, 0);
|
||||||
if (!data) {
|
if (!data) {
|
||||||
@ -473,7 +469,7 @@ static SDL_GPUTexture *create_shader_texture(const char *path) {
|
|||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
static SDL_GPUTexture *create_shader_texture(const char *name, const char *data, int width, int height, int channels) {
|
SDL_GPUTexture *create_shader_texture(const char *name, const char *data, int width, int height, int channels) {
|
||||||
SDL_GPUTextureFormat format = SDL_GPU_TEXTUREFORMAT_INVALID;
|
SDL_GPUTextureFormat format = SDL_GPU_TEXTUREFORMAT_INVALID;
|
||||||
if (channels == 1) format = SDL_GPU_TEXTUREFORMAT_A8_UNORM;
|
if (channels == 1) format = SDL_GPU_TEXTUREFORMAT_A8_UNORM;
|
||||||
if (channels == 4) format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB;
|
if (channels == 4) format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB;
|
||||||
@ -565,12 +561,12 @@ static SDL_GPUTexture *create_shader_texture(const char *name, const char *data,
|
|||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
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++)
|
for (Sint32 y = 0; y < height; y++)
|
||||||
memmove(&dst[((dst_y + y) * dst_pitch + dst_x) * components], &src[y * src_pitch * components], width * components);
|
memmove(&dst[((dst_y + y) * dst_pitch + dst_x) * components], &src[y * src_pitch * components], width * components);
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
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 ImGuiContext *context = ImGui::GetCurrentContext();
|
||||||
const ImVec2 padding = context->Style.FramePadding;
|
const ImVec2 padding = context->Style.FramePadding;
|
||||||
|
|
||||||
@ -592,7 +588,7 @@ static bool SelectableImage(const char *label, bool selected, SDL_GPUTextureSamp
|
|||||||
return pressed;
|
return pressed;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ImVec4 linear_to_sRGB(ImVec4 linear) {
|
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 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 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;
|
float blue = linear.z <= 0.0031308f ? 12.92f * linear.z : 1.055f * powf(linear.z, 1.0f / 2.4f) - 0.055;
|
||||||
@ -600,7 +596,7 @@ static ImVec4 linear_to_sRGB(ImVec4 linear) {
|
|||||||
return ImVec4(red, green, blue, linear.w);
|
return ImVec4(red, green, blue, linear.w);
|
||||||
}
|
}
|
||||||
|
|
||||||
static ImVec4 sRGB_to_linear(ImVec4 linear) {
|
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 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 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);
|
float blue = linear.z <= 0.0031308f ? linear.z / 12.92f : powf((linear.z + 0.055) / 1.055, 2.4f);
|
||||||
@ -608,14 +604,14 @@ static ImVec4 sRGB_to_linear(ImVec4 linear) {
|
|||||||
return ImVec4(red, green, blue, linear.w);
|
return ImVec4(red, green, blue, linear.w);
|
||||||
}
|
}
|
||||||
|
|
||||||
static V3 Unproject(V3 screen_pos) {
|
V3 Unproject(V3 screen_pos) {
|
||||||
V4 result = inverse_view_matrix * inverse_projection_matrix * V4_(screen_pos, 1.0f);
|
V4 result = inverse_view_matrix * inverse_projection_matrix * V4_(screen_pos, 1.0f);
|
||||||
result.xyz /= result.w;
|
result.xyz /= result.w;
|
||||||
|
|
||||||
return result.xyz;
|
return result.xyz;
|
||||||
}
|
}
|
||||||
|
|
||||||
static V2 get_floor_intersection_of_mouse(V2 mouse_pos) {
|
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 });
|
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;
|
V3 camera_position = (inverse_view_matrix * V4_(0, 0, 0, 1)).xyz;
|
||||||
|
|
||||||
@ -631,12 +627,12 @@ static V2 get_floor_intersection_of_mouse(V2 mouse_pos) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef TRACY_ENABLE
|
#ifdef TRACY_ENABLE
|
||||||
static SDL_malloc_func sdl_malloc = NULL;
|
SDL_malloc_func sdl_malloc = NULL;
|
||||||
static SDL_calloc_func sdl_calloc = NULL;
|
SDL_calloc_func sdl_calloc = NULL;
|
||||||
static SDL_realloc_func sdl_realloc = NULL;
|
SDL_realloc_func sdl_realloc = NULL;
|
||||||
static SDL_free_func sdl_free = NULL;
|
SDL_free_func sdl_free = NULL;
|
||||||
|
|
||||||
static void setup_memory_functions() {
|
void setup_memory_functions() {
|
||||||
SDL_GetMemoryFunctions(&sdl_malloc, &sdl_calloc, &sdl_realloc, &sdl_free);
|
SDL_GetMemoryFunctions(&sdl_malloc, &sdl_calloc, &sdl_realloc, &sdl_free);
|
||||||
SDL_SetMemoryFunctions(
|
SDL_SetMemoryFunctions(
|
||||||
[](size_t size) -> void * {
|
[](size_t size) -> void * {
|
||||||
@ -662,13 +658,108 @@ static void setup_memory_functions() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
static void setup_memory_functions() {}
|
void setup_memory_functions() {}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool enable_msaa = false;
|
int main(int argc, char **argv) {
|
||||||
SDL_GPUSampleCount highest_supported_sample_count = SDL_GPU_SAMPLECOUNT_1;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
static bool recreate_graphics_pipelines() {
|
|
||||||
SDL_GPUColorTargetDescription color_target_descriptions[] = {
|
SDL_GPUColorTargetDescription color_target_descriptions[] = {
|
||||||
{
|
{
|
||||||
.format = SDL_GetGPUSwapchainTextureFormat(device, window),
|
.format = SDL_GetGPUSwapchainTextureFormat(device, window),
|
||||||
@ -686,39 +777,7 @@ static bool recreate_graphics_pipelines() {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
{ // 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[] = {
|
SDL_GPUVertexBufferDescription vertex_buffer_descriptions[] = {
|
||||||
{
|
{
|
||||||
.slot = 0,
|
.slot = 0,
|
||||||
@ -776,20 +835,12 @@ static bool recreate_graphics_pipelines() {
|
|||||||
.front_face = SDL_GPU_FRONTFACE_CLOCKWISE,
|
.front_face = SDL_GPU_FRONTFACE_CLOCKWISE,
|
||||||
.enable_depth_clip = true,
|
.enable_depth_clip = true,
|
||||||
},
|
},
|
||||||
.multisample_state = {
|
|
||||||
.sample_count = enable_msaa ? highest_supported_sample_count : SDL_GPU_SAMPLECOUNT_1,
|
|
||||||
},
|
|
||||||
.target_info = {
|
.target_info = {
|
||||||
.color_target_descriptions = color_target_descriptions,
|
.color_target_descriptions = color_target_descriptions,
|
||||||
.num_color_targets = SDL_arraysize(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);
|
basic_graphics_pipeline = SDL_CreateGPUGraphicsPipeline(device, &basic_graphics_pipeline_info);
|
||||||
if (!basic_graphics_pipeline) {
|
if (!basic_graphics_pipeline) {
|
||||||
log_error("Failed to create basic graphics pipeline. Exiting.");
|
log_error("Failed to create basic graphics pipeline. Exiting.");
|
||||||
@ -800,40 +851,7 @@ static bool recreate_graphics_pipelines() {
|
|||||||
SDL_ReleaseGPUShader(device, basic_pixel_shader);
|
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[] = {
|
SDL_GPUVertexBufferDescription vertex_buffer_descriptions[] = {
|
||||||
{
|
{
|
||||||
.slot = 0,
|
.slot = 0,
|
||||||
@ -879,20 +897,12 @@ static bool recreate_graphics_pipelines() {
|
|||||||
.front_face = SDL_GPU_FRONTFACE_CLOCKWISE,
|
.front_face = SDL_GPU_FRONTFACE_CLOCKWISE,
|
||||||
.enable_depth_clip = true,
|
.enable_depth_clip = true,
|
||||||
},
|
},
|
||||||
.multisample_state = {
|
|
||||||
.sample_count = enable_msaa ? highest_supported_sample_count : SDL_GPU_SAMPLECOUNT_1,
|
|
||||||
},
|
|
||||||
.target_info = {
|
.target_info = {
|
||||||
.color_target_descriptions = color_target_descriptions,
|
.color_target_descriptions = color_target_descriptions,
|
||||||
.num_color_targets = SDL_arraysize(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);
|
world_graphics_pipeline = SDL_CreateGPUGraphicsPipeline(device, &world_graphics_pipeline_info);
|
||||||
if (!world_graphics_pipeline) {
|
if (!world_graphics_pipeline) {
|
||||||
log_error("Failed to create world graphics pipeline. Exiting.");
|
log_error("Failed to create world graphics pipeline. Exiting.");
|
||||||
@ -903,50 +913,6 @@ static bool recreate_graphics_pipelines() {
|
|||||||
SDL_ReleaseGPUShader(device, world_pixel_shader);
|
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");
|
SDL_GPUTexture *player_texture = create_shader_texture("../assets/decorations/strawberry.png");
|
||||||
if (!player_texture) {
|
if (!player_texture) {
|
||||||
log_error("Failed to create shader texture. Exiting.");
|
log_error("Failed to create shader texture. Exiting.");
|
||||||
@ -1149,14 +1115,6 @@ int main(int argc, char **argv) {
|
|||||||
ImGui::DragFloat("fovy", &per_frame.fovy_degrees);
|
ImGui::DragFloat("fovy", &per_frame.fovy_degrees);
|
||||||
ImGui::DragFloat("camera_distance", &per_frame.camera_distance, 0.25f, 1.0f, INFINITY);
|
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);
|
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();
|
ImGui::End();
|
||||||
|
|
||||||
@ -1319,33 +1277,6 @@ int main(int argc, char **argv) {
|
|||||||
continue;
|
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");
|
ZoneScopedN("Update");
|
||||||
{
|
{
|
||||||
@ -1377,12 +1308,10 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SDL_GPUColorTargetInfo color_target_info = {
|
SDL_GPUColorTargetInfo color_target_info = {
|
||||||
.texture = enable_msaa ? msaa_texture : swapchain_texture,
|
.texture = swapchain_texture,
|
||||||
.clear_color = { .r = 1.0f, .g = 0.0f, .b = 1.0f, .a = 1.0f },
|
.clear_color = { .r = 1.0f, .g = 0.0f, .b = 1.0f, .a = 1.0f },
|
||||||
.load_op = SDL_GPU_LOADOP_CLEAR,
|
.load_op = SDL_GPU_LOADOP_CLEAR,
|
||||||
.store_op = enable_msaa ? SDL_GPU_STOREOP_RESOLVE : SDL_GPU_STOREOP_STORE,
|
.store_op = SDL_GPU_STOREOP_STORE,
|
||||||
.resolve_texture = swapchain_texture,
|
|
||||||
.cycle = true,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SDL_GPURenderPass *render_pass = SDL_BeginGPURenderPass(command_buffer, &color_target_info, 1, NULL);
|
SDL_GPURenderPass *render_pass = SDL_BeginGPURenderPass(command_buffer, &color_target_info, 1, NULL);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user