Flint Engine
Modular C++20 2D game engine powered by SDL3
Loading...
Searching...
No Matches
gpu-texture.h
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2025-2026 Manuel Osorio
3#pragma once
4#include "flint/core/log.h"
5#include "flint/graphics/gpu/gpu-tracker.h"
6
7#include <SDL3/SDL_gpu.h>
8#include <SDL3_image/SDL_image.h>
9
10#include <cstddef>
11#include <cstdint>
12#include <cstring>
13
14namespace Flint {
21 class GPUTexture {
22 public:
23 GPUTexture() = default;
24
25 virtual ~GPUTexture() {
26 free();
27 }
28
29 GPUTexture(const GPUTexture &) = delete;
30 GPUTexture &operator=(const GPUTexture &) = delete;
31
33 GPUTexture(GPUTexture &&other) noexcept
34 : m_device(other.m_device),
35 m_texture(other.m_texture),
36 m_sampler(other.m_sampler),
37 m_width(other.m_width),
38 m_height(other.m_height) {
39 other.m_device = nullptr;
40 other.m_texture = nullptr;
41 other.m_sampler = nullptr;
42 other.m_width = 0;
43 other.m_height = 0;
44 }
45
47 GPUTexture &operator=(GPUTexture &&other) noexcept {
48 if (this != &other) {
49 free();
50 m_device = other.m_device;
51 m_texture = other.m_texture;
52 m_sampler = other.m_sampler;
53 m_width = other.m_width;
54 m_height = other.m_height;
55
56 other.m_device = nullptr;
57 other.m_texture = nullptr;
58 other.m_sampler = nullptr;
59 other.m_width = 0;
60 other.m_height = 0;
61 }
62 return *this;
63 }
64
79 bool loadFromMemory(SDL_GPUDevice *device, const void *data, size_t size) {
80 m_device = device;
81
82 SDL_IOStream *rw = SDL_IOFromConstMem(data, static_cast<int>(size));
83 if (!rw) {
84 FLINT_ENGINE_ERROR("Failed to create IO stream from memory");
85 return false;
86 }
87
88 SDL_Surface *loaded = IMG_Load_IO(rw, true);
89 if (!loaded) {
90 FLINT_ENGINE_ERROR("Failed to load image: {}", SDL_GetError());
91 return false;
92 }
93
94 SDL_Surface *surface = loaded;
95 if (loaded->format != SDL_PIXELFORMAT_RGBA32) {
96 surface = SDL_ConvertSurface(loaded, SDL_PIXELFORMAT_RGBA32);
97 SDL_DestroySurface(loaded);
98 if (!surface) {
99 FLINT_ENGINE_ERROR("Failed to convert surface to RGBA32: {}", SDL_GetError());
100 return false;
101 }
102 }
103
104 m_width = surface->w;
105 m_height = surface->h;
106
107 const Uint32 bytesPerRow = static_cast<Uint32>(m_width) * 4;
108 const Uint32 dataSize = bytesPerRow * static_cast<Uint32>(m_height);
109
110 SDL_GPUTextureCreateInfo textureInfo = {};
111 textureInfo.type = SDL_GPU_TEXTURETYPE_2D;
112 textureInfo.format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
113 textureInfo.width = m_width;
114 textureInfo.height = m_height;
115 textureInfo.layer_count_or_depth = 1;
116 textureInfo.num_levels = 1;
117 textureInfo.sample_count = SDL_GPU_SAMPLECOUNT_1;
118 textureInfo.usage = SDL_GPU_TEXTUREUSAGE_SAMPLER;
119
120 m_texture = SDL_CreateGPUTexture(m_device, &textureInfo);
121 if (!m_texture) {
122 FLINT_ENGINE_ERROR("Failed to create GPU texture: {}", SDL_GetError());
123 SDL_DestroySurface(surface);
124 return false;
125 }
126
127 SDL_GPUCommandBuffer *uploadCmd = SDL_AcquireGPUCommandBuffer(m_device);
128 if (!uploadCmd) {
129 FLINT_ENGINE_ERROR("Failed to acquire command buffer for upload");
130 SDL_DestroySurface(surface);
131 GPU_RELEASE_TEXTURE(m_device, m_texture);
132 return false;
133 }
134
135 SDL_GPUCopyPass *copyPass = SDL_BeginGPUCopyPass(uploadCmd);
136 if (!copyPass) {
137 FLINT_ENGINE_ERROR("Failed to begin GPU copy pass: {}", SDL_GetError());
138 return false;
139 }
140
141 SDL_GPUTransferBufferCreateInfo transferBufferInfo = {};
142 transferBufferInfo.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
143 transferBufferInfo.size = dataSize;
144
145 SDL_GPUTransferBuffer *transferBuffer = SDL_CreateGPUTransferBuffer(m_device,
146 &transferBufferInfo);
147 if (!transferBuffer) {
148 FLINT_ENGINE_ERROR("Failed to create transfer buffer");
149 SDL_DestroySurface(surface);
150 SDL_EndGPUCopyPass(copyPass);
151 SDL_SubmitGPUCommandBuffer(uploadCmd);
152 return false;
153 }
154
155 void *mapped = SDL_MapGPUTransferBuffer(m_device, transferBuffer, false);
156 if (!mapped) {
157 FLINT_ENGINE_ERROR("Failed to map transfer buffer: {}", SDL_GetError());
158 SDL_EndGPUCopyPass(copyPass);
159 SDL_SubmitGPUCommandBuffer(uploadCmd);
160 SDL_ReleaseGPUTransferBuffer(m_device, transferBuffer);
161 GPU_RELEASE_TEXTURE(m_device, m_texture);
162 SDL_DestroySurface(surface);
163 return false;
164 }
165 auto *dstBytes = static_cast<Uint8 *>(mapped);
166 auto *srcBytes = static_cast<const Uint8 *>(surface->pixels);
167 for (int y = 0; y < m_height; ++y) {
168 memcpy(dstBytes + y * bytesPerRow, srcBytes + y * surface->pitch, bytesPerRow);
169 }
170 SDL_UnmapGPUTransferBuffer(m_device, transferBuffer);
171
172 SDL_GPUTextureTransferInfo uploadInfo = {};
173 uploadInfo.transfer_buffer = transferBuffer;
174 uploadInfo.offset = 0;
175 uploadInfo.pixels_per_row = m_width;
176 uploadInfo.rows_per_layer = m_height;
177
178 SDL_GPUTextureRegion textureRegion = {};
179 textureRegion.texture = m_texture;
180 textureRegion.w = m_width;
181 textureRegion.h = m_height;
182 textureRegion.d = 1;
183
184 SDL_UploadToGPUTexture(copyPass, &uploadInfo, &textureRegion, false);
185
186 SDL_EndGPUCopyPass(copyPass);
187 SDL_SubmitGPUCommandBuffer(uploadCmd);
188
189 SDL_ReleaseGPUTransferBuffer(m_device, transferBuffer);
190 SDL_DestroySurface(surface);
191
192 SDL_GPUSamplerCreateInfo samplerInfo = {};
193 samplerInfo.min_filter = SDL_GPU_FILTER_NEAREST;
194 samplerInfo.mag_filter = SDL_GPU_FILTER_NEAREST;
195 samplerInfo.mipmap_mode = SDL_GPU_SAMPLERMIPMAPMODE_NEAREST;
196 samplerInfo.address_mode_u = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE;
197 samplerInfo.address_mode_v = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE;
198 samplerInfo.address_mode_w = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE;
199
200 m_sampler = SDL_CreateGPUSampler(m_device, &samplerInfo);
201 if (!m_sampler) {
202 FLINT_ENGINE_ERROR("Failed to create GPU sampler: {}", SDL_GetError());
203 GPU_RELEASE_TEXTURE(m_device, m_texture);
204 return false;
205 }
206
207 FLINT_ENGINE_INFO("GPU texture loaded successfully ({}x{})", m_width, m_height);
208 return true;
209 }
210
211 void free() {
212 FLINT_ENGINE_DEBUG("Releasing GPUTexture resources");
213 FLINT_ENGINE_DEBUG("Device pointer: {}", std::uintptr_t(m_device));
214 if (m_sampler) {
215 SDL_ReleaseGPUSampler(m_device, m_sampler);
216 m_sampler = nullptr;
217 }
218 FLINT_ENGINE_DEBUG("Sampler released");
219 FLINT_ENGINE_DEBUG("Texture pointer: {}", std::uintptr_t(m_texture));
220 if (m_texture) {
221 GPU_RELEASE_TEXTURE(m_device, m_texture);
222 m_texture = nullptr;
223 }
224 FLINT_ENGINE_DEBUG("Texture released");
225 m_width = 0;
226 m_height = 0;
227 FLINT_ENGINE_DEBUG("GPUTexture resources released");
228 }
229
231 SDL_GPUTexture *getTexture() const {
232 return m_texture;
233 }
234
236 SDL_GPUSampler *getSampler() const {
237 return m_sampler;
238 }
239
241 int getWidth() const {
242 return m_width;
243 }
244
246 int getHeight() const {
247 return m_height;
248 }
249
259 static GPUTexture fromRenderer(SDL_GPUDevice *device, const void *data, size_t size) {
260 GPUTexture gpuTexture;
261 if (gpuTexture.loadFromMemory(device, data, size)) {
262 return gpuTexture;
263 } else {
264 throw std::runtime_error("Failed to create GPUTexture from memory");
265 }
266 }
267
268 private:
269 SDL_GPUDevice *m_device = nullptr;
270 SDL_GPUTexture *m_texture = nullptr;
271 SDL_GPUSampler *m_sampler = nullptr;
272 int m_width = 0, m_height = 0;
273 };
274}
Owns an SDL GPU texture and its associated sampler.
Definition gpu-texture.h:21
GPUTexture(GPUTexture &&other) noexcept
Move constructor; transfers GPU ownership from the other.
Definition gpu-texture.h:33
int getWidth() const
Return the texture width in pixels.
bool loadFromMemory(SDL_GPUDevice *device, const void *data, size_t size)
Upload an image from memory to the GPU.
Definition gpu-texture.h:79
GPUTexture & operator=(GPUTexture &&other) noexcept
Move-assignment; releases current resources then steals from the other.
Definition gpu-texture.h:47
SDL_GPUTexture * getTexture() const
Return the underlying SDL GPU texture handle.
static GPUTexture fromRenderer(SDL_GPUDevice *device, const void *data, size_t size)
Factory: create a GPUTexture by loading from memory.
int getHeight() const
Return the texture height in pixels.
SDL_GPUSampler * getSampler() const
Return the sampler created alongside this texture.