diff options
author | Lei Zhang <antiagainst@google.com> | 2016-09-26 15:43:45 -0400 |
---|---|---|
committer | Lei Zhang <antiagainst@google.com> | 2016-09-27 11:37:45 -0400 |
commit | f18f4bfd14100e2ee47edf025c2e7180fb72b54c (patch) | |
tree | cb25c52a3ef9080df625a9457ce8b231eeca4a61 | |
parent | 0771152d9edc13c639f537dece2b887f0f5e6a78 (diff) | |
download | shaderc-f18f4bfd14100e2ee47edf025c2e7180fb72b54c.tar.gz |
Add cases in examples to show optimization.
-rw-r--r-- | examples/online-compile/main.cc | 80 |
1 files changed, 52 insertions, 28 deletions
diff --git a/examples/online-compile/main.cc b/examples/online-compile/main.cc index 78f02c6..92d9835 100644 --- a/examples/online-compile/main.cc +++ b/examples/online-compile/main.cc @@ -19,6 +19,7 @@ // - Preprocessing GLSL source text // - Compiling a shader to SPIR-V assembly text // - Compliing a shader to a SPIR-V binary module +// - Performing optimization with compilation // - Setting basic options: setting a preprocessor symbol. // - Checking compilation status and extracting an error message. @@ -38,86 +39,109 @@ std::string preprocess_shader(const std::string& source_name, // Like -DMY_DEFINE=1 options.AddMacroDefinition("MY_DEFINE", "1"); - shaderc::PreprocessedSourceCompilationResult result = compiler.PreprocessGlsl( - source.c_str(), source.size(), kind, source_name.c_str(), options); + shaderc::PreprocessedSourceCompilationResult result = + compiler.PreprocessGlsl(source, kind, source_name.c_str(), options); if (result.GetCompilationStatus() != shaderc_compilation_status_success) { std::cerr << result.GetErrorMessage(); return ""; } - return std::string(result.cbegin(), result.cend()); + return {result.cbegin(), result.cend()}; } // Compiles a shader to SPIR-V assembly. Returns the assembly text // as a string. std::string compile_file_to_assembly(const std::string& source_name, shaderc_shader_kind kind, - const std::string& source) { + const std::string& source, + bool optimize = false) { shaderc::Compiler compiler; shaderc::CompileOptions options; // Like -DMY_DEFINE=1 options.AddMacroDefinition("MY_DEFINE", "1"); + if (optimize) options.SetOptimizationLevel(shaderc_optimization_level_size); shaderc::AssemblyCompilationResult result = compiler.CompileGlslToSpvAssembly( - source.c_str(), source.size(), kind, source_name.c_str(), options); + source, kind, source_name.c_str(), options); if (result.GetCompilationStatus() != shaderc_compilation_status_success) { std::cerr << result.GetErrorMessage(); return ""; } - return std::string(result.cbegin(), result.cend()); + return {result.cbegin(), result.cend()}; } // Compiles a shader to a SPIR-V binary. Returns the binary as // a vector of 32-bit words. std::vector<uint32_t> compile_file(const std::string& source_name, shaderc_shader_kind kind, - const std::string& source) { + const std::string& source, + bool optimize = false) { shaderc::Compiler compiler; shaderc::CompileOptions options; // Like -DMY_DEFINE=1 options.AddMacroDefinition("MY_DEFINE", "1"); + if (optimize) options.SetOptimizationLevel(shaderc_optimization_level_size); - shaderc::SpvCompilationResult module = compiler.CompileGlslToSpv( - source.c_str(), source.size(), kind, source_name.c_str(), options); + shaderc::SpvCompilationResult module = + compiler.CompileGlslToSpv(source, kind, source_name.c_str(), options); if (module.GetCompilationStatus() != shaderc_compilation_status_success) { std::cerr << module.GetErrorMessage(); return std::vector<uint32_t>(); } - std::vector<uint32_t> result(module.cbegin(), module.cend()); - return result; + return {module.cbegin(), module.cend()}; } int main() { const char kShaderSource[] = - "#version 310 es\nvoid main() {int x = MY_DEFINE; }\n"; + "#version 310 es\n" + "void main() { int x = MY_DEFINE; }\n"; + + { // Preprocessing + auto preprocessed = preprocess_shader( + "shader_src", shaderc_glsl_vertex_shader, kShaderSource); + std::cout << "Compiled a vertex shader resulting in preprocessed text:" + << std::endl + << preprocessed << std::endl; + } - auto preprocessed = preprocess_shader( - "shader_src", shaderc_glsl_vertex_shader, kShaderSource); - std::cout << "Compiled a vertex shader resulting in preprocessed text:" - << std::endl - << preprocessed << std::endl; + { // Compiling + auto assembly = compile_file_to_assembly( + "shader_src", shaderc_glsl_vertex_shader, kShaderSource); + std::cout << "SPIR-V assembly:" << std::endl << assembly << std::endl; - auto assembly = compile_file_to_assembly( - "shader_src", shaderc_glsl_vertex_shader, kShaderSource); - std::cout << "SPIR-V assembly:" << std::endl << assembly << std::endl; + auto spirv = + compile_file("shader_src", shaderc_glsl_vertex_shader, kShaderSource); + std::cout << "Compiled to a binary module with " << spirv.size() + << " words." << std::endl; + } - auto spirv = - compile_file("shader_src", shaderc_glsl_vertex_shader, kShaderSource); - std::cout << "Compiled to a binary module with " << spirv.size() << " words." - << std::endl; + { // Compiling with optimizing + auto assembly = + compile_file_to_assembly("shader_src", shaderc_glsl_vertex_shader, + kShaderSource, /* optimize = */ true); + std::cout << "Optimized SPIR-V assembly:" << std::endl + << assembly << std::endl; + + auto spirv = compile_file("shader_src", shaderc_glsl_vertex_shader, + kShaderSource, /* optimize = */ true); + std::cout << "Compiled to an optimized binary module with " << spirv.size() + << " words." << std::endl; + } - const char kBadShaderSource[] = - "#version 310 es\nint main() { int main_should_be_void; }\n"; + { // Error case + const char kBadShaderSource[] = + "#version 310 es\nint main() { int main_should_be_void; }\n"; - std::cout << std::endl << "Compiling a bad shader:" << std::endl; - compile_file("bad_src", shaderc_glsl_vertex_shader, kBadShaderSource); + std::cout << std::endl << "Compiling a bad shader:" << std::endl; + compile_file("bad_src", shaderc_glsl_vertex_shader, kBadShaderSource); + } return 0; } |