Created
June 8, 2020 03:13
-
-
Save sortofsleepy/a7fcb8221f70cab37e82f3779e78aaa5 to your computer and use it in GitHub Desktop.
sample of how to use shaderc to compile shaders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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