Something went wrong. Try again.
art software for the rest of us
drawing paint image sdl art
Something went wrong. Try again.
C++
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061#include "Shader.h"#include <stdexcept>#include "GPU.h"
Shader::Shader(SDL_GPUShaderCreateInfo* createInfo){ shader = SDL_CreateGPUShader(GPU::GetDevice(), createInfo);}
Shader::Shader(std::string path, std::string entryPoint, Uint32 samplers, Uint32 storageTextures, Uint32 storageBuffers, Uint32 uniformBuffers){ SDL_GPUShaderCreateInfo* createInfo = new SDL_GPUShaderCreateInfo(); createInfo->code = (Uint8*)SDL_LoadFile(path.c_str(), &createInfo->code_size); if (!createInfo->code) { throw std::runtime_error("Failed to load shader: " + path); } // path contains shaderpath.stage.format std::string format = path.substr(path.find_last_of(".") + 1); if (format == "spirv") { createInfo->format = SDL_GPU_SHADERFORMAT_SPIRV; } else if (format == "dxil") { createInfo->format = SDL_GPU_SHADERFORMAT_DXIL; } else if (format == "msl") { createInfo->format = SDL_GPU_SHADERFORMAT_MSL; }
std::string stage = path.substr(0, path.find_last_of(".")); stage = stage.substr(stage.find_last_of(".") + 1);
if (stage == "vert") { createInfo->stage = SDL_GPU_SHADERSTAGE_VERTEX; } else if (stage == "frag") { createInfo->stage = SDL_GPU_SHADERSTAGE_FRAGMENT; } else { throw std::runtime_error("Unknown shader stage: " + stage); }
createInfo->entrypoint = entryPoint.c_str(); createInfo->num_samplers = samplers; createInfo->num_storage_textures = storageTextures; createInfo->num_storage_buffers = storageBuffers; createInfo->num_uniform_buffers = uniformBuffers; shader = SDL_CreateGPUShader(GPU::GetDevice(), createInfo); SDL_free((void*)createInfo->code); delete createInfo;}
Shader::~Shader(){ SDL_ReleaseGPUShader(GPU::GetDevice(), shader);}
SDL_GPUShader* Shader::Get(){ return shader;}