refactor webgpu initialization into init_webgpu()

This commit is contained in:
Sven Balzer 2025-10-04 13:57:16 +02:00
parent 0d3bfaf12d
commit 5a1ea421f6

View File

@ -38,12 +38,15 @@ static WGPUTexture framebuffer;
static WGPURenderPipeline basic_render_pipeline;
static WGPURenderPipeline world_render_pipeline;
static WGPURenderPipeline grid_render_pipeline;
static WGPUSampler pixel_sampler;
static WGPUSampler pixel_sampler;
static WGPUBindGroup per_frame_bind_group;
static WGPUBindGroup world_bind_group;
static WGPUBindGroup basic_bind_group;
static WGPUTexture player_texture;
static WGPUTextureView player_texture_view;
static WGPUTexture tile_textures_array;
static WGPUTextureView tile_textures_array_view;
static WGPUTextureView *tile_textures_array_view_individual;
@ -59,8 +62,11 @@ static WGPUBuffer grid_index_buffer;
static WGPUBuffer player_instance_buffer;
static WGPUBuffer tile_infos_buffer;
static Sint32 window_width;
static Sint32 window_height;
static WGPUSurfaceConfiguration surface_configuration;
static Sint32 window_width = 1280;
static Sint32 window_height = 720;
static Mix_Music *music_setting_off_piano;
@ -1366,55 +1372,17 @@ WGPUSurface create_wgpu_surface_for_SDL_window(SDL_Window *window) {
return NULL;
}
void setup_working_directory() {
if (SDL_GetPathInfo(ASSETS_PATH, NULL)) return;
const char *current_directory = SDL_GetCurrentDirectory();
change_directory(SDL_GetBasePath());
if (SDL_GetPathInfo(ASSETS_PATH, NULL)) return;
change_directory("..");
if (SDL_GetPathInfo(ASSETS_PATH, NULL)) return;
change_directory(current_directory);
}
int main(int argc, char **argv) {
setup_memory_functions();
setup_working_directory();
#ifdef SDL_PLATFORM_LINUX
if (getenv("ENABLE_VULKAN_RENDERDOC_CAPTURE"))
SDL_SetHint(SDL_HINT_VIDEO_DRIVER, "x11,wayland");
#endif
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_EVENTS | SDL_INIT_GAMEPAD)) {
log_error("Failed to initialize SDL (%s). Exiting.", SDL_GetError());
return 1;
}
if (!Mix_Init(MIX_INIT_OPUS)) {
log_error("Failed to init SDL_mixer. Exiting.");
return 1;
}
bool init_webgpu() {
instance = wgpuCreateInstance(NULL);
if (!instance) {
log_error("Failed to create webgpu instance. Exiting.");
return 1;
}
window = SDL_CreateWindow("Mikemon", 1280, 720, SDL_WINDOW_RESIZABLE);
if (!window) {
log_error("Failed to create window (%s). Exiting.", SDL_GetError());
return 1;
log_error("Failed to create webgpu instance.");
return false;
}
surface = create_wgpu_surface_for_SDL_window(window);
if (!surface) {
log_error("Failed to create webgpu surface for SDL window. Exiting.");
return 1;
log_error("Failed to create webgpu surface for SDL window.");
return false;
}
WGPURequestAdapterCallbackInfo request_adapter_callback_info = {
@ -1437,11 +1405,11 @@ int main(int argc, char **argv) {
}
if (!device) {
log_error("Failed to initialize webgpu. Exiting.");
return 1;
log_error("Failed to get webgpu device.");
return false;
}
WGPUSurfaceConfiguration surface_configuration = {
surface_configuration = {
.device = device,
.format = WGPUTextureFormat_BGRA8UnormSrgb,
.usage = WGPUTextureUsage_RenderAttachment,
@ -1456,26 +1424,8 @@ int main(int argc, char **argv) {
wgpuSurfaceConfigure(surface, &surface_configuration);
if (!recreate_graphics_pipelines()) {
log_error("Failed to create graphics pipelines. Exiting.");
return 1;
}
if (!load_map("map.sv", &current_map)) {
log_error("Failed to load initial map. Exiting.");
return 1;
}
WGPUTexture player_texture = create_shader_texture("decorations/strawberry.png");
if (!player_texture) {
log_error("Failed to create shader texture. Exiting.");
return 1;
}
WGPUTextureView player_texture_view = wgpuTextureCreateView(player_texture, NULL);
if (!recreate_tile_textures()) {
log_error("Failed to create tile textures. Exiting.");
return 1;
log_error("Failed to create graphics pipelines.");
return false;
}
WGPUSamplerDescriptor pixel_sampler_descriptor = {
@ -1496,50 +1446,63 @@ int main(int argc, char **argv) {
view_projection_matrix_buffer = create_buffer(WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst, sizeof(M4x4), NULL, "view_projection_matrix_buffer");
if (!view_projection_matrix_buffer) {
log_error("Failed to create buffer. Exiting.");
return 1;
log_error("Failed to create buffer.");
return false;
}
per_frame_buffer = create_buffer(WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst, sizeof(per_frame), NULL, "per_frame_buffer");
if (!per_frame_buffer) {
log_error("Failed to create buffer. Exiting.");
return 1;
log_error("Failed to create buffer.");
return false;
}
tint_color_buffer = create_buffer(WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst, sizeof(V3), NULL, "tint_color_buffer");
if (!tint_color_buffer) {
log_error("Failed to create buffer. Exiting.");
return 1;
log_error("Failed to create buffer.");
return false;
}
vertex_buffer = create_buffer(WGPUBufferUsage_Vertex, sizeof(vertices), vertices, "vertex_buffer");
if (!vertex_buffer) {
log_error("Failed to create buffer. Exiting.");
return 1;
log_error("Failed to create buffer.");
return false;
}
index_buffer = create_buffer(WGPUBufferUsage_Index, sizeof(indices), indices, "index_buffer");
if (!index_buffer) {
log_error("Failed to create buffer. Exiting.");
return 1;
log_error("Failed to create buffer.");
return false;
}
grid_vertex_buffer = create_buffer(WGPUBufferUsage_Vertex, sizeof(grid_vertices), grid_vertices, "grid_vertex_buffer");
if (!grid_vertex_buffer) {
log_error("Failed to create buffer. Exiting.");
return 1;
log_error("Failed to create buffer.");
return false;
}
grid_index_buffer = create_buffer(WGPUBufferUsage_Index, sizeof(grid_indices), grid_indices, "grid_index_buffer");
if (!grid_index_buffer) {
log_error("Failed to create buffer. Exiting.");
return 1;
log_error("Failed to create buffer.");
return false;
}
player_instance_buffer = create_buffer(WGPUBufferUsage_Vertex | WGPUBufferUsage_CopyDst, sizeof(player_instance), &player_instance, "player_instance_buffer");
if (!player_instance_buffer) {
log_error("Failed to create buffer. Exiting.");
return 1;
log_error("Failed to create buffer.");
return false;
}
player_texture = create_shader_texture("decorations/strawberry.png");
if (!player_texture) {
log_error("Failed to create shader texture.");
return false;
}
player_texture_view = wgpuTextureCreateView(player_texture, NULL);
if (!recreate_tile_textures()) {
log_error("Failed to create tile textures.");
return false;
}
WGPUBindGroupEntry per_frame_bind_group_entries[] = {
@ -1583,6 +1546,58 @@ int main(int argc, char **argv) {
};
basic_bind_group = wgpuDeviceCreateBindGroup(device, &basic_bind_group_descriptor);
return true;
}
void setup_working_directory() {
if (SDL_GetPathInfo(ASSETS_PATH, NULL)) return;
const char *current_directory = SDL_GetCurrentDirectory();
change_directory(SDL_GetBasePath());
if (SDL_GetPathInfo(ASSETS_PATH, NULL)) return;
change_directory("..");
if (SDL_GetPathInfo(ASSETS_PATH, NULL)) return;
change_directory(current_directory);
}
int main(int argc, char **argv) {
setup_memory_functions();
setup_working_directory();
#ifdef SDL_PLATFORM_LINUX
if (getenv("ENABLE_VULKAN_RENDERDOC_CAPTURE"))
SDL_SetHint(SDL_HINT_VIDEO_DRIVER, "x11,wayland");
#endif
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_EVENTS | SDL_INIT_GAMEPAD)) {
log_error("Failed to initialize SDL (%s). Exiting.", SDL_GetError());
return 1;
}
if (!Mix_Init(MIX_INIT_OPUS)) {
log_error("Failed to init SDL_mixer. Exiting.");
return 1;
}
window = SDL_CreateWindow("Mikemon", window_width, window_height, SDL_WINDOW_RESIZABLE);
if (!window) {
log_error("Failed to create window (%s). Exiting.", SDL_GetError());
return 1;
}
if (!init_webgpu()) {
log_error("Failed to initialize webgpu. Exiting.");
return 1;
}
if (!load_map("map.sv", &current_map)) {
log_error("Failed to load initial map. Exiting.");
return 1;
}
SDL_AudioSpec audio_spec = {
.format = SDL_AUDIO_F32,
.channels = 2,