- Infraestructura GPU: GpuContext, GpuPipeline, GpuSpriteBatch, GpuTexture - Engine::render() migrat a 2-pass: sprites → offscreen R8G8B8A8 → swapchain + vignette - UI/text via software renderer (SDL3_ttf) + upload com a textura overlay GPU - CMakeLists.txt actualitzat per incloure subsistema gpu/ Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
79 lines
2.9 KiB
C++
79 lines
2.9 KiB
C++
#include "gpu_context.hpp"
|
|
|
|
#include <SDL3/SDL_log.h>
|
|
#include <iostream>
|
|
|
|
bool GpuContext::init(SDL_Window* window) {
|
|
window_ = window;
|
|
|
|
// Create GPU device — prefer Metal on macOS, Vulkan elsewhere
|
|
SDL_GPUShaderFormat preferred = SDL_GPU_SHADERFORMAT_MSL
|
|
| SDL_GPU_SHADERFORMAT_METALLIB
|
|
| SDL_GPU_SHADERFORMAT_SPIRV;
|
|
device_ = SDL_CreateGPUDevice(preferred, false, nullptr);
|
|
if (!device_) {
|
|
std::cerr << "GpuContext: SDL_CreateGPUDevice failed: " << SDL_GetError() << std::endl;
|
|
return false;
|
|
}
|
|
std::cout << "GpuContext: driver = " << SDL_GetGPUDeviceDriver(device_) << std::endl;
|
|
|
|
// Claim the window so the GPU device owns its swapchain
|
|
if (!SDL_ClaimWindowForGPUDevice(device_, window_)) {
|
|
std::cerr << "GpuContext: SDL_ClaimWindowForGPUDevice failed: " << SDL_GetError() << std::endl;
|
|
SDL_DestroyGPUDevice(device_);
|
|
device_ = nullptr;
|
|
return false;
|
|
}
|
|
|
|
// Query swapchain format (Metal: typically B8G8R8A8_UNORM or R8G8B8A8_UNORM)
|
|
swapchain_format_ = SDL_GetGPUSwapchainTextureFormat(device_, window_);
|
|
std::cout << "GpuContext: swapchain format = " << static_cast<int>(swapchain_format_) << std::endl;
|
|
|
|
// Default: VSync ON
|
|
SDL_SetGPUSwapchainParameters(device_, window_,
|
|
SDL_GPU_SWAPCHAINCOMPOSITION_SDR,
|
|
SDL_GPU_PRESENTMODE_VSYNC);
|
|
return true;
|
|
}
|
|
|
|
void GpuContext::destroy() {
|
|
if (device_) {
|
|
SDL_WaitForGPUIdle(device_);
|
|
SDL_ReleaseWindowFromGPUDevice(device_, window_);
|
|
SDL_DestroyGPUDevice(device_);
|
|
device_ = nullptr;
|
|
}
|
|
window_ = nullptr;
|
|
}
|
|
|
|
SDL_GPUCommandBuffer* GpuContext::acquireCommandBuffer() {
|
|
SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer(device_);
|
|
if (!cmd) {
|
|
SDL_Log("GpuContext: SDL_AcquireGPUCommandBuffer failed: %s", SDL_GetError());
|
|
}
|
|
return cmd;
|
|
}
|
|
|
|
SDL_GPUTexture* GpuContext::acquireSwapchainTexture(SDL_GPUCommandBuffer* cmd_buf,
|
|
Uint32* out_w, Uint32* out_h) {
|
|
SDL_GPUTexture* tex = nullptr;
|
|
if (!SDL_AcquireGPUSwapchainTexture(cmd_buf, window_, &tex, out_w, out_h)) {
|
|
SDL_Log("GpuContext: SDL_AcquireGPUSwapchainTexture failed: %s", SDL_GetError());
|
|
return nullptr;
|
|
}
|
|
// tex == nullptr when window is minimized — caller should skip rendering
|
|
return tex;
|
|
}
|
|
|
|
void GpuContext::submit(SDL_GPUCommandBuffer* cmd_buf) {
|
|
SDL_SubmitGPUCommandBuffer(cmd_buf);
|
|
}
|
|
|
|
bool GpuContext::setVSync(bool enabled) {
|
|
SDL_GPUPresentMode mode = enabled ? SDL_GPU_PRESENTMODE_VSYNC
|
|
: SDL_GPU_PRESENTMODE_IMMEDIATE;
|
|
return SDL_SetGPUSwapchainParameters(device_, window_,
|
|
SDL_GPU_SWAPCHAINCOMPOSITION_SDR,
|
|
mode);
|
|
}
|