candlewick 0.11.0
A tiny cross-platform renderer based on SDL3
Loading...
Searching...
No Matches
Shader.h
Go to the documentation of this file.
1
2#pragma once
3
4#include "Core.h"
5#include <SDL3/SDL_gpu.h>
6#include <string>
7
8namespace candlewick {
9
10constexpr const char *g_default_shader_dir = CANDLEWICK_SHADER_BIN_DIR;
11
14void setShadersDirectory(const char *path);
17
18const char *shader_format_name(SDL_GPUShaderFormat shader_format);
19
25struct Shader {
28 struct Config {
29 SDL_GPUShaderStage stage;
30 std::string entry_point;
32 Uint32 samplers;
33 Uint32 storage_textures = 0;
34 Uint32 storage_buffers = 0;
35 };
36
41 Shader(const Device &device, const char *filename, const Config &config);
43 Shader(const Shader &) = delete;
45 Shader(Shader &&other) noexcept;
46
47 operator SDL_GPUShader *() const noexcept { return _shader; }
48 void release() noexcept;
49 ~Shader() noexcept { release(); }
50
52 static Shader fromMetadata(const Device &device, const char *filename);
53
54 SDL_GPUShaderStage stage() const noexcept { return _stage; }
55
56private:
57 SDL_GPUShader *_shader;
58 SDL_GPUDevice *_device;
59 SDL_GPUShaderStage _stage;
60};
61
62inline Shader::Shader(Shader &&other) noexcept
63 : _shader(other._shader), _device(other._device) {
64 other._shader = nullptr;
65 other._device = nullptr;
66}
67
70Shader::Config loadShaderMetadata(const char *shader_name);
71
72inline Shader Shader::fromMetadata(const Device &device,
73 const char *shader_name) {
74 auto config = loadShaderMetadata(shader_name);
75 return Shader{device, shader_name, config};
76}
77
78} // namespace candlewick
Definition Camera.h:8
void setShadersDirectory(const char *path)
Set the current (global) directory where shaders are to be found.
const char * currentShaderDirectory()
Get the current (global) directory where shaders are found.
Shader::Config loadShaderMetadata(const char *shader_name)
Load shader config from metadata. Metadata filename (in JSON format) is inferred from the shader name...
constexpr const char * g_default_shader_dir
Definition Shader.h:10
const char * shader_format_name(SDL_GPUShaderFormat shader_format)
RAII wrapper for SDL_GPUDevice.
Definition Device.h:17
Shader configuration: number of uniforms, texture samplers, storage textures and storage buffers.
Definition Shader.h:28
SDL_GPUShaderStage stage
Definition Shader.h:29
Uint32 storage_buffers
Definition Shader.h:34
std::string entry_point
Definition Shader.h:30
Uint32 samplers
Definition Shader.h:32
Uint32 storage_textures
Definition Shader.h:33
Uint32 uniform_buffers
Definition Shader.h:31
RAII wrapper around SDL_GPUShader, with loading utilities.
Definition Shader.h:25
Shader(const Shader &)=delete
Deleted copy constructor.
SDL_GPUShaderStage stage() const noexcept
Definition Shader.h:54
void release() noexcept
static Shader fromMetadata(const Device &device, const char *filename)
Load shader from metadata.
Definition Shader.h:72
Shader(const Device &device, const char *filename, const Config &config)
Load a shader to device provided the shader name and configuration.