kiba-engine
surface.c
1 #include <kiba/gpu/surface.h>
2 
3 b8 gpu_surface_create(gpu_surface surface, void *window_data) {
4  return gpu_backend_surface_create(&surface->bs, window_data);
5 }
6 
7 void gpu_surface_destroy(gpu_surface surface) { gpu_backend_surface_destroy(&surface->bs); }
8 
9 b8 gpu_surface_configure(gpu_surface surface, gpu_device device, struct gpu_surface_configuration config) {
10  surface->device = device;
11  surface->config = config;
12  return gpu_backend_surface_configure(&surface->bs, &device->bd, config);
13 }
14 
15 void gpu_surface_unconfigure(gpu_surface surface) {
16  gpu_backend_surface_unconfigure(&surface->bs, &surface->device->bd);
17 }
18 
19 b8 gpu_surface_get_current_texture(gpu_surface surface, gpu_texture *texture, b8 *suboptimal) {
20  // TODO this leaks when called twice without a present call in between
21  if (!gpu_device_resource_texture_create(surface->device, texture)) {
22  return false;
23  }
24  struct gpu_texture *texture_data = *texture;
25  texture_data->desc = (struct gpu_texture_descriptor){
26  .format = surface->config.format,
27  .dimension = GPU_TEXTURE_DIMENSION_D2,
28  .size = {.width = surface->config.width, .height = surface->config.height, .depth = 1},
29  .usage = GPU_TEXTURE_USAGE_RENDER_ATTACHMENT, // NOTE this is the only valid one so its not part of the config
30  .mip_level_count = 1,
31  .sample_count = 1,
32  };
33  // start is used here as the final destination state
34  texture_data->device->textures[texture_data->tracker_id].start_use = GPU_TEXTURE_USE_PRESENT;
35  surface->presentation = *texture;
36  return gpu_backend_surface_get_current_texture(&surface->bs, &surface->device->bd, &texture_data->bt, suboptimal);
37 }
38 
39 b8 gpu_surface_present(gpu_surface surface, b8 *suboptimal) {
40  b8 ret = gpu_backend_surface_present(&surface->bs, &surface->device->bd, &surface->presentation->bt, suboptimal);
41  gpu_device_resource_texture_release(surface->presentation);
42  return ret;
43 }