sKit  0.0.9
ShaderBuilder.cpp
Go to the documentation of this file.
1 #include "ShaderBuilder.hpp"
2 
3 #include <format>
4 #include <fstream>
5 #include <sstream>
6 
7 namespace sKit {
8 namespace SDL {
9 
10 auto ShaderBuilder::path(std::string path) noexcept -> ShaderBuilder& {
11  path_ = path;
12  return *this;
13 }
14 
15 auto ShaderBuilder::vertex() noexcept -> ShaderBuilder& {
16  stage_ = SDL_GPU_SHADERSTAGE_VERTEX;
17  return *this;
18 }
19 
21  stage_ = SDL_GPU_SHADERSTAGE_FRAGMENT;
22  return *this;
23 }
24 
25 auto ShaderBuilder::numUniformBuffers(uint32_t n) noexcept -> ShaderBuilder& {
26  numUniformBuffers_ = n;
27  return *this;
28 }
29 
30 auto ShaderBuilder::build(SDL_GPUDevice* device) -> SDL_GPUShader* {
31  std::ifstream f(path_);
32  if (!f) {
33  throw std::runtime_error(std::format("Could not find shader {}", path_));
34  }
35  std::stringstream buffer;
36  buffer << f.rdbuf();
37  std::string code = buffer.str();
38  SDL_GPUShaderCreateInfo info {
39  .code_size = code.size(),
40  .code = reinterpret_cast<unsigned char*>(code.data()),
41  .entrypoint = "main",
42  .format = SDL_GPU_SHADERFORMAT_SPIRV,
43  .stage = stage_,
44  .num_uniform_buffers = numUniformBuffers_
45  };
46  if (auto shader = SDL_CreateGPUShader(device, &info)) {
47  return shader;
48  } else {
49  throw std::runtime_error(SDL_GetError());
50  }
51 }
52 
53 } // namespace SDL
54 } // namespace sKit
sKit::SDL::ShaderBuilder::build
auto build(SDL_GPUDevice *device) -> SDL_GPUShader *
Definition: ShaderBuilder.cpp:30
sKit::SDL::ShaderBuilder
SDL_GPUShader builder class
Definition: ShaderBuilder.hpp:20
sKit::SDL::ShaderBuilder::fragment
auto fragment() noexcept -> ShaderBuilder &
Tells the builder that the shader built is a fragment shader.
Definition: ShaderBuilder.cpp:20
sKit::SDL::ShaderBuilder::numUniformBuffers
auto numUniformBuffers(uint32_t n) noexcept -> ShaderBuilder &
Tells the builder how many uniform bufferms the shader will receive.
Definition: ShaderBuilder.cpp:25
sKit::SDL::ShaderBuilder::vertex
auto vertex() noexcept -> ShaderBuilder &
Tells the builder that the shader built is a vertex shader.
Definition: ShaderBuilder.cpp:15
ShaderBuilder.hpp
sKit::SDL::ShaderBuilder::path
auto path(std::string path) noexcept -> ShaderBuilder &
Definition: ShaderBuilder.cpp:10
sKit
Definition: camera.hpp:8