Files
shadertoy/src/rendering/sdl3gpu/shader_factory.hpp

42 lines
1.3 KiB
C++

#pragma once
#include <SDL3/SDL.h>
#include <filesystem>
namespace Rendering::Sdl3Gpu {
// Loads a compiled shader binary or source from disk and creates an SDL_GPUShader.
// For SPIR-V: pass the .spv path with format = SDL_GPU_SHADERFORMAT_SPIRV.
// For MSL: pass the .msl text path with format = SDL_GPU_SHADERFORMAT_MSL.
inline auto loadShaderFromFile(SDL_GPUDevice* device,
const std::filesystem::path& path,
SDL_GPUShaderFormat format,
const char* entrypoint,
SDL_GPUShaderStage stage,
Uint32 num_samplers,
Uint32 num_uniform_buffers) -> SDL_GPUShader* {
std::size_t size = 0;
void* data = SDL_LoadFile(path.string().c_str(), &size);
if (data == nullptr) {
return nullptr;
}
SDL_GPUShaderCreateInfo info{};
info.code_size = size;
info.code = static_cast<Uint8*>(data);
info.entrypoint = entrypoint;
info.format = format;
info.stage = stage;
info.num_samplers = num_samplers;
info.num_storage_textures = 0;
info.num_storage_buffers = 0;
info.num_uniform_buffers = num_uniform_buffers;
SDL_GPUShader* shader = SDL_CreateGPUShader(device, &info);
SDL_free(data);
return shader;
}
} // namespace Rendering::Sdl3Gpu