Skip to content

Commit 2fb80bc

Browse files
committed
buffer
1 parent a6d9dca commit 2fb80bc

3 files changed

Lines changed: 103 additions & 8 deletions

File tree

src/buffer.c

Lines changed: 101 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,130 @@
11
#include <SDL3/SDL.h>
22

3+
#include <stddef.h>
34
#include <stdint.h>
5+
#include <string.h>
46

57
#include "buffer.h"
68

79
void cpu_buffer_init(cpu_buffer_t* cpu_buffer, SDL_GPUDevice* device, uint32_t stride)
810
{
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;
1016
}
1117

1218
void cpu_buffer_free(cpu_buffer_t* cpu_buffer, SDL_GPUDevice* device)
1319
{
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;
1526
}
1627

1728
void cpu_buffer_add(cpu_buffer_t* cpu_buffer, SDL_GPUDevice* device, void* item)
1829
{
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++;
2074
}
2175

2276
void gpu_buffer_init(gpu_buffer_t* gpu_buffer, SDL_GPUDevice* device, SDL_GPUBufferUsageFlags usage)
2377
{
24-
78+
gpu_buffer->usage = usage;
79+
gpu_buffer->buffer = NULL;
80+
gpu_buffer->capacity = 0;
81+
gpu_buffer->size = 0;
2582
}
2683

2784
void gpu_buffer_free(gpu_buffer_t* gpu_buffer, SDL_GPUDevice* device)
2885
{
29-
86+
SDL_ReleaseGPUBuffer(device, gpu_buffer->buffer);
87+
gpu_buffer->buffer = NULL;
88+
gpu_buffer->capacity = 0;
89+
gpu_buffer->size = 0;
3090
}
3191

3292
void gpu_buffer_upload(gpu_buffer_t* gpu_buffer, SDL_GPUDevice* device, SDL_GPUCopyPass* pass, cpu_buffer_t* cpu_buffer)
3393
{
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, &region, true);
129+
gpu_buffer->size = size;
35130
}

src/buffer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
typedef struct cpu_buffer
88
{
9-
SDL_GPUTransferBufferUsage usage;
109
SDL_GPUTransferBuffer* buffer;
10+
uint8_t* data;
1111
uint32_t size;
1212
uint32_t capacity;
1313
uint32_t stride;

src/chunk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "chunk.h"
66
#include "map.h"
77

8-
static void transform(chunk_t* chunk, int* x, int* y, int* z)
8+
static void transform(const chunk_t* chunk, int* x, int* y, int* z)
99
{
1010
*x -= chunk->x;
1111
*y -= chunk->y;

0 commit comments

Comments
 (0)