Skip to content

Instantly share code, notes, and snippets.

@sortofsleepy
Created June 8, 2020 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sortofsleepy/a7fcb8221f70cab37e82f3779e78aaa5 to your computer and use it in GitHub Desktop.
Save sortofsleepy/a7fcb8221f70cab37e82f3779e78aaa5 to your computer and use it in GitHub Desktop.
sample of how to use shaderc to compile shaders
void Shader::compile(VkDevice * device,const std::string source_name, shaderc_shader_kind kind, const std::string source,
bool optimize) {
shaderc::Compiler compiler;
shaderc::CompileOptions options;
if(optimize){
options.SetOptimizationLevel(shaderc_optimization_level_size);
}
shaderc::SpvCompilationResult module = compiler.CompileGlslToSpv(source,kind,source_name.c_str(),options);
if(module.GetCompilationStatus() != shaderc_compilation_status_success){
LOG_E("Error compiling module - " << module.GetErrorMessage());
}
std::vector<uint32_t> spirv = {module.cbegin(),module.cend()};
// build vulkan module
VkShaderModuleCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
createInfo.codeSize = spirv.size() * sizeof(unsigned int);
createInfo.pCode = spirv.data();
switch(kind){
case shaderc_vertex_shader:
if(vkCreateShaderModule(*device,&createInfo,nullptr,&vertexModule) != VK_SUCCESS){
LOG_E("Error creating vertex module");
}
break;
case shaderc_fragment_shader:
if(vkCreateShaderModule(*device,&createInfo,nullptr,&vertexModule) != VK_SUCCESS){
LOG_E("Error creating vertex module");
}
break;
// TODO compute + geometry
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment