sKit  0.0.9
TextureDownloader.cpp
Go to the documentation of this file.
1 #include "TextureDownloader.hpp"
2 
3 #include <stdexcept>
4 
5 namespace sKit {
6 namespace SDL {
7 
9  SDL_GPUDevice* device,
10  SDL_GPUCopyPass* pass,
11  SDL_GPUTexture* sourceTexture,
12  Uint32 width,
13  Uint32 height
14 )
15  : device_(device) {
16  auto info = SDL_GPUTransferBufferCreateInfo {
17  .usage = SDL_GPU_TRANSFERBUFFERUSAGE_DOWNLOAD, .size = width * height * 4
18  };
19  buffer = SDL_CreateGPUTransferBuffer(device, &info);
20  if (!buffer) {
21  throw std::runtime_error("Could not create transfer buffer");
22  }
23  auto src = SDL_GPUTextureRegion {
24  .texture = sourceTexture, .w = width, .h = height, .d = 1
25  };
26  auto dst = SDL_GPUTextureTransferInfo {
27  .transfer_buffer = buffer,
28  };
29  SDL_DownloadFromGPUTexture(pass, &src, &dst);
30 }
31 
33  SDL_UnmapGPUTransferBuffer(device_, buffer);
34  SDL_ReleaseGPUTransferBuffer(device_, buffer);
35 }
36 
37 auto TextureDownloader::getData() -> unsigned char* {
38  return reinterpret_cast<unsigned char*>(
39  SDL_MapGPUTransferBuffer(device_, buffer, false)
40  );
41 }
42 
43 } // namespace SDL
44 } // namespace sKit
sKit::SDL::TextureDownloader::~TextureDownloader
~TextureDownloader()
Unmaps and releases the SDL_GPUTransferBuffer.
Definition: TextureDownloader.cpp:32
sKit::SDL::TextureDownloader::buffer
SDL_GPUTransferBuffer * buffer
Definition: TextureDownloader.hpp:24
TextureDownloader.hpp
sKit
Definition: camera.hpp:8
sKit::SDL::TextureDownloader::TextureDownloader
TextureDownloader()=default
sKit::SDL::TextureDownloader::getData
auto getData() -> unsigned char *
Definition: TextureDownloader.cpp:37