|
1 | 1 | #include <SDL3/SDL.h> |
2 | 2 |
|
| 3 | +#include <stddef.h> |
3 | 4 | #include <stdint.h> |
| 5 | +#include <string.h> |
4 | 6 |
|
5 | 7 | #include "buffer.h" |
6 | 8 |
|
7 | 9 | void cpu_buffer_init(cpu_buffer_t* cpu_buffer, SDL_GPUDevice* device, uint32_t stride) |
8 | 10 | { |
9 | | - |
| 11 | + cpu_buffer->buffer = NULL; |
| 12 | + cpu_buffer->data = NULL; |
| 13 | + cpu_buffer->capacity = 0; |
| 14 | + cpu_buffer->size = 0; |
| 15 | + cpu_buffer->stride = 0; |
10 | 16 | } |
11 | 17 |
|
12 | 18 | void cpu_buffer_free(cpu_buffer_t* cpu_buffer, SDL_GPUDevice* device) |
13 | 19 | { |
14 | | - |
| 20 | + SDL_ReleaseGPUTransferBuffer(device, cpu_buffer->buffer); |
| 21 | + cpu_buffer->buffer = NULL; |
| 22 | + cpu_buffer->data = NULL; |
| 23 | + cpu_buffer->capacity = 0; |
| 24 | + cpu_buffer->size = 0; |
| 25 | + cpu_buffer->stride = 0; |
15 | 26 | } |
16 | 27 |
|
17 | 28 | void cpu_buffer_add(cpu_buffer_t* cpu_buffer, SDL_GPUDevice* device, void* item) |
18 | 29 | { |
19 | | - |
| 30 | + uint32_t stride = cpu_buffer->stride; |
| 31 | + if (!cpu_buffer->data && cpu_buffer->buffer) |
| 32 | + { |
| 33 | + SDL_assert(!cpu_buffer->size); |
| 34 | + cpu_buffer->data = SDL_MapGPUTransferBuffer(device, cpu_buffer->buffer, true); |
| 35 | + if (!cpu_buffer->data) |
| 36 | + { |
| 37 | + SDL_Log("Failed to map transfer buffer: %s", SDL_GetError()); |
| 38 | + return; |
| 39 | + } |
| 40 | + } |
| 41 | + SDL_assert(cpu_buffer->size <= cpu_buffer->capacity); |
| 42 | + if (cpu_buffer->size == cpu_buffer->capacity) |
| 43 | + { |
| 44 | + int capacity = SDL_max(64, cpu_buffer->size * 2); |
| 45 | + SDL_GPUTransferBufferCreateInfo info = {0}; |
| 46 | + info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD; |
| 47 | + info.size = capacity * stride; |
| 48 | + SDL_GPUTransferBuffer* buffer = SDL_CreateGPUTransferBuffer(device, &info); |
| 49 | + if (!buffer) |
| 50 | + { |
| 51 | + SDL_Log("Failed to create transfer buffer: %s", SDL_GetError()); |
| 52 | + return; |
| 53 | + } |
| 54 | + void* data = SDL_MapGPUTransferBuffer(device, buffer, false); |
| 55 | + if (!data) |
| 56 | + { |
| 57 | + SDL_Log("Failed to map transfer buffer: %s", SDL_GetError()); |
| 58 | + SDL_ReleaseGPUTransferBuffer(device, buffer); |
| 59 | + return; |
| 60 | + } |
| 61 | + if (cpu_buffer->data) |
| 62 | + { |
| 63 | + memcpy(data, cpu_buffer->data, cpu_buffer->size * stride); |
| 64 | + SDL_UnmapGPUTransferBuffer(device, cpu_buffer->buffer); |
| 65 | + } |
| 66 | + SDL_ReleaseGPUTransferBuffer(device, cpu_buffer->buffer); |
| 67 | + cpu_buffer->capacity = capacity; |
| 68 | + cpu_buffer->buffer = buffer; |
| 69 | + cpu_buffer->data = data; |
| 70 | + } |
| 71 | + SDL_assert(cpu_buffer->data); |
| 72 | + memcpy(cpu_buffer->data + cpu_buffer->size * stride, item, stride); |
| 73 | + cpu_buffer->size++; |
20 | 74 | } |
21 | 75 |
|
22 | 76 | void gpu_buffer_init(gpu_buffer_t* gpu_buffer, SDL_GPUDevice* device, SDL_GPUBufferUsageFlags usage) |
23 | 77 | { |
24 | | - |
| 78 | + gpu_buffer->usage = usage; |
| 79 | + gpu_buffer->buffer = NULL; |
| 80 | + gpu_buffer->capacity = 0; |
| 81 | + gpu_buffer->size = 0; |
25 | 82 | } |
26 | 83 |
|
27 | 84 | void gpu_buffer_free(gpu_buffer_t* gpu_buffer, SDL_GPUDevice* device) |
28 | 85 | { |
29 | | - |
| 86 | + SDL_ReleaseGPUBuffer(device, gpu_buffer->buffer); |
| 87 | + gpu_buffer->buffer = NULL; |
| 88 | + gpu_buffer->capacity = 0; |
| 89 | + gpu_buffer->size = 0; |
30 | 90 | } |
31 | 91 |
|
32 | 92 | void gpu_buffer_upload(gpu_buffer_t* gpu_buffer, SDL_GPUDevice* device, SDL_GPUCopyPass* pass, cpu_buffer_t* cpu_buffer) |
33 | 93 | { |
34 | | - |
| 94 | + gpu_buffer->size = 0; |
| 95 | + if (cpu_buffer->data) |
| 96 | + { |
| 97 | + SDL_UnmapGPUTransferBuffer(device, cpu_buffer->buffer); |
| 98 | + cpu_buffer->data = NULL; |
| 99 | + } |
| 100 | + if (!cpu_buffer->size) |
| 101 | + { |
| 102 | + gpu_buffer->size = 0; |
| 103 | + return; |
| 104 | + } |
| 105 | + uint32_t size = cpu_buffer->size; |
| 106 | + cpu_buffer->size = 0; |
| 107 | + if (size > gpu_buffer->size) |
| 108 | + { |
| 109 | + SDL_ReleaseGPUBuffer(device, gpu_buffer->buffer); |
| 110 | + gpu_buffer->buffer = NULL; |
| 111 | + gpu_buffer->capacity = 0; |
| 112 | + SDL_GPUBufferCreateInfo info = {0}; |
| 113 | + info.usage = gpu_buffer->usage; |
| 114 | + info.size = cpu_buffer->capacity * cpu_buffer->stride; |
| 115 | + gpu_buffer->buffer = SDL_CreateGPUBuffer(device, &info); |
| 116 | + if (!gpu_buffer->buffer) |
| 117 | + { |
| 118 | + SDL_Log("Failed to create buffer: %s", SDL_GetError()); |
| 119 | + return; |
| 120 | + } |
| 121 | + gpu_buffer->capacity = gpu_buffer->capacity; |
| 122 | + } |
| 123 | + SDL_GPUTransferBufferLocation location = {0}; |
| 124 | + SDL_GPUBufferRegion region = {0}; |
| 125 | + location.transfer_buffer = cpu_buffer->buffer; |
| 126 | + region.buffer = gpu_buffer->buffer; |
| 127 | + region.size = size * cpu_buffer->stride; |
| 128 | + SDL_UploadToGPUBuffer(pass, &location, ®ion, true); |
| 129 | + gpu_buffer->size = size; |
35 | 130 | } |
0 commit comments