candlewick 0.6.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
7namespace candlewick {
8
9constexpr const char *g_default_shader_dir = CANDLEWICK_SHADER_BIN_DIR;
10
13void setShadersDirectory(const char *path);
16
17SDL_GPUShaderStage detect_shader_stage(const char *filename);
18
19const char *shader_format_name(SDL_GPUShaderFormat shader_format);
20
26struct Shader {
29 struct Config {
31 Uint32 samplers;
32 Uint32 storage_textures = 0;
33 Uint32 storage_buffers = 0;
34 };
35
40 Shader(const Device &device, const char *filename, const Config &config);
42 Shader(const Shader &) = delete;
44 Shader(Shader &&other) noexcept;
45
46 operator SDL_GPUShader *() const noexcept { return _shader; }
47 void release() noexcept;
48 ~Shader() noexcept { release(); }
49
51 static Shader fromMetadata(const Device &device, const char *filename);
52
53 SDL_GPUShaderStage stage() const noexcept { return _stage; }
54
55private:
56 SDL_GPUShader *_shader;
57 SDL_GPUDevice *_device;
58 SDL_GPUShaderStage _stage;
59};
60
61inline Shader::Shader(Shader &&other) noexcept
62 : _shader(other._shader), _device(other._device) {
63 other._shader = nullptr;
64 other._device = nullptr;
65}
66
69Shader::Config loadShaderMetadata(const char *shader_name);
70
71inline Shader Shader::fromMetadata(const Device &device,
72 const char *shader_name) {
73 auto config = loadShaderMetadata(shader_name);
74 return Shader{device, shader_name, config};
75}
76
77} // 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:9
SDL_GPUShaderStage detect_shader_stage(const char *filename)
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:29
Uint32 storage_buffers
Definition Shader.h:33
Uint32 samplers
Definition Shader.h:31
Uint32 storage_textures
Definition Shader.h:32
Uint32 uniform_buffers
Definition Shader.h:30
RAII wrapper around SDL_GPUShader, with loading utilities.
Definition Shader.h:26
Shader(const Shader &)=delete
Deleted copy constructor.
SDL_GPUShaderStage stage() const noexcept
Definition Shader.h:53
void release() noexcept
static Shader fromMetadata(const Device &device, const char *filename)
Load shader from metadata.
Definition Shader.h:71
Shader(const Device &device, const char *filename, const Config &config)
Load a shader to device provided the shader name and configuration.