Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SDL_GPUTexture* Game::LoadGPUTextureFromFile(SDL_GPUDevice* device,
- SDL_GPUCommandBuffer* cmd_buffer,
- const char* file, Uint32 layer) {
- SDL_Surface* texture_surface{IMG_Load(file)};
- SDL_ConvertSurface(texture_surface, SDL_PIXELFORMAT_RGBA32);
- if (!texture_surface) {
- SDL_Log("texture surface not loaded :( %s", SDL_GetError());
- }
- if (texture_surface == nullptr) {
- SDL_Log("Failed to Load image '%s': %s", file, SDL_GetError());
- exit_code_ = SDL_APP_FAILURE;
- is_running_ = false;
- return nullptr;
- }
- std::size_t size = texture_surface->h * texture_surface->pitch;
- SDL_GPUTransferBufferCreateInfo transfer_buffer_info{
- .usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
- .size = static_cast<Uint32>(size)};
- SDL_GPUTransferBuffer* transfer_buffer{
- SDL_CreateGPUTransferBuffer(device, &transfer_buffer_info)};
- void* destination{SDL_MapGPUTransferBuffer(device, transfer_buffer, false)};
- SDL_memcpy(destination, texture_surface->pixels, size);
- SDL_UnmapGPUTransferBuffer(device, transfer_buffer);
- SDL_GPUTextureCreateInfo texture_info = {
- .type = SDL_GPU_TEXTURETYPE_2D,
- .format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM,
- .usage = SDL_GPU_TEXTUREUSAGE_SAMPLER,
- .width = static_cast<Uint32>(texture_surface->w),
- .height = static_cast<Uint32>(texture_surface->h),
- .layer_count_or_depth = 1,
- .num_levels = 0,
- };
- SDL_GPUTexture* texture{SDL_CreateGPUTexture(device, &texture_info)};
- if (texture == nullptr) {
- SDL_Log("Failed to Load GPUTexture: %s", SDL_GetError());
- exit_code_ = SDL_APP_FAILURE;
- is_running_ = false;
- return nullptr;
- }
- SDL_GPUTextureTransferInfo texture_transfer_buffer_info = {
- .transfer_buffer = transfer_buffer};
- SDL_GPUTextureRegion texture_region{
- .texture = texture,
- .mip_level = 1,
- .layer = layer,
- .w = static_cast<Uint32>(texture_surface->w),
- .h = static_cast<Uint32>(texture_surface->h),
- .d = 1};
- SDL_GPUCopyPass* copy_pass{SDL_BeginGPUCopyPass(cmd_buffer)};
- SDL_UploadToGPUTexture(copy_pass, &texture_transfer_buffer_info,
- &texture_region, true);
- SDL_DestroySurface(texture_surface);
- return texture;
- }
Advertisement
Add Comment
Please, Sign In to add comment