82 SDL_IOStream *rw = SDL_IOFromConstMem(data,
static_cast<int>(size));
84 FLINT_ENGINE_ERROR(
"Failed to create IO stream from memory");
88 SDL_Surface *loaded = IMG_Load_IO(rw,
true);
90 FLINT_ENGINE_ERROR(
"Failed to load image: {}", SDL_GetError());
94 SDL_Surface *surface = loaded;
95 if (loaded->format != SDL_PIXELFORMAT_RGBA32) {
96 surface = SDL_ConvertSurface(loaded, SDL_PIXELFORMAT_RGBA32);
97 SDL_DestroySurface(loaded);
99 FLINT_ENGINE_ERROR(
"Failed to convert surface to RGBA32: {}", SDL_GetError());
104 m_width = surface->w;
105 m_height = surface->h;
107 const Uint32 bytesPerRow =
static_cast<Uint32
>(m_width) * 4;
108 const Uint32 dataSize = bytesPerRow *
static_cast<Uint32
>(m_height);
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;
120 m_texture = SDL_CreateGPUTexture(m_device, &textureInfo);
122 FLINT_ENGINE_ERROR(
"Failed to create GPU texture: {}", SDL_GetError());
123 SDL_DestroySurface(surface);
127 SDL_GPUCommandBuffer *uploadCmd = SDL_AcquireGPUCommandBuffer(m_device);
129 FLINT_ENGINE_ERROR(
"Failed to acquire command buffer for upload");
130 SDL_DestroySurface(surface);
131 GPU_RELEASE_TEXTURE(m_device, m_texture);
135 SDL_GPUCopyPass *copyPass = SDL_BeginGPUCopyPass(uploadCmd);
137 FLINT_ENGINE_ERROR(
"Failed to begin GPU copy pass: {}", SDL_GetError());
141 SDL_GPUTransferBufferCreateInfo transferBufferInfo = {};
142 transferBufferInfo.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
143 transferBufferInfo.size = dataSize;
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);
155 void *mapped = SDL_MapGPUTransferBuffer(m_device, transferBuffer,
false);
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);
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);
170 SDL_UnmapGPUTransferBuffer(m_device, transferBuffer);
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;
178 SDL_GPUTextureRegion textureRegion = {};
179 textureRegion.texture = m_texture;
180 textureRegion.w = m_width;
181 textureRegion.h = m_height;
184 SDL_UploadToGPUTexture(copyPass, &uploadInfo, &textureRegion,
false);
186 SDL_EndGPUCopyPass(copyPass);
187 SDL_SubmitGPUCommandBuffer(uploadCmd);
189 SDL_ReleaseGPUTransferBuffer(m_device, transferBuffer);
190 SDL_DestroySurface(surface);
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;
200 m_sampler = SDL_CreateGPUSampler(m_device, &samplerInfo);
202 FLINT_ENGINE_ERROR(
"Failed to create GPU sampler: {}", SDL_GetError());
203 GPU_RELEASE_TEXTURE(m_device, m_texture);
207 FLINT_ENGINE_INFO(
"GPU texture loaded successfully ({}x{})", m_width, m_height);