From 164bb4f87fc1b2a384c017c81f1c5dfab452ac1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Capucho?= Date: Wed, 30 Jun 2021 16:10:24 +0100 Subject: [PATCH] DOESN'T WORK I made this commit because I've accidentally broke this when rebasing and deleted the veloren folder and don't want to write this for the 4th time. Please ignore this. --- Cargo.lock | 71 ++++--- Cargo.toml | 3 +- assets/voxygen/shaders/blit-frag.glsl | 2 +- assets/voxygen/shaders/blit-vert.glsl | 2 +- assets/voxygen/shaders/clouds-frag.glsl | 2 +- assets/voxygen/shaders/clouds-vert.glsl | 2 +- assets/voxygen/shaders/debug-frag.glsl | 2 +- assets/voxygen/shaders/debug-vert.glsl | 2 +- assets/voxygen/shaders/figure-frag.glsl | 2 +- assets/voxygen/shaders/figure-vert.glsl | 2 +- assets/voxygen/shaders/fluid-frag/cheap.glsl | 2 +- assets/voxygen/shaders/fluid-frag/shiny.glsl | 2 +- assets/voxygen/shaders/fluid-vert.glsl | 2 +- assets/voxygen/shaders/include/random.glsl | 6 +- .../shaders/light-shadows-directed-vert.glsl | 2 +- .../shaders/light-shadows-figure-vert.glsl | 2 +- .../voxygen/shaders/light-shadows-frag.glsl | 2 +- assets/voxygen/shaders/lod-terrain-frag.glsl | 2 +- assets/voxygen/shaders/lod-terrain-vert.glsl | 2 +- assets/voxygen/shaders/particle-frag.glsl | 2 +- assets/voxygen/shaders/particle-vert.glsl | 2 +- .../shaders/point-light-shadows-vert.glsl | 2 +- assets/voxygen/shaders/postprocess-frag.glsl | 2 +- assets/voxygen/shaders/postprocess-vert.glsl | 2 +- assets/voxygen/shaders/skybox-frag.glsl | 2 +- assets/voxygen/shaders/skybox-vert.glsl | 2 +- assets/voxygen/shaders/sprite-frag.glsl | 2 +- assets/voxygen/shaders/sprite-vert.glsl | 2 +- assets/voxygen/shaders/terrain-frag.glsl | 2 +- assets/voxygen/shaders/terrain-vert.glsl | 2 +- assets/voxygen/shaders/ui-frag.glsl | 2 +- assets/voxygen/shaders/ui-vert.glsl | 2 +- voxygen/Cargo.toml | 4 +- voxygen/src/render/error.rs | 48 ++++- .../src/render/renderer/pipeline_creation.rs | 186 +++++++++++------- 35 files changed, 240 insertions(+), 136 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6b9630ecd3..371bbde2ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1948,7 +1948,7 @@ dependencies = [ "khronos-egl", "libloading 0.7.0", "log", - "naga", + "naga 0.4.0", "parking_lot 0.11.1", "raw-window-handle", "spirv_cross", @@ -1972,7 +1972,7 @@ dependencies = [ "gfx-hal", "log", "metal", - "naga", + "naga 0.4.0", "objc", "parking_lot 0.11.1", "profiling", @@ -1995,7 +1995,7 @@ dependencies = [ "inplace_it", "libloading 0.7.0", "log", - "naga", + "naga 0.4.0", "objc", "parking_lot 0.11.1", "raw-window-handle", @@ -2010,7 +2010,7 @@ version = "0.8.0" source = "git+https://github.com/gfx-rs/gfx?rev=27a1dae3796d33d23812f2bb8c7e3b5aea18b521#27a1dae3796d33d23812f2bb8c7e3b5aea18b521" dependencies = [ "bitflags", - "naga", + "naga 0.4.0", "raw-window-handle", "thiserror", ] @@ -2106,6 +2106,16 @@ dependencies = [ "web-sys", ] +[[package]] +name = "glsl-include" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daa2afb1631e7ab4543e0dde0e3fc68bb49c58fee89c07f30a26553b1f684ab6" +dependencies = [ + "lazy_static", + "regex", +] + [[package]] name = "glyph_brush" version = "0.7.2" @@ -3080,6 +3090,22 @@ dependencies = [ "thiserror", ] +[[package]] +name = "naga" +version = "0.5.0" +source = "git+https://github.com/gfx-rs/naga.git?rev=607206f9a5cc33a8b1e7cec84663be6390e3be7f#607206f9a5cc33a8b1e7cec84663be6390e3be7f" +dependencies = [ + "bit-set", + "bitflags", + "codespan-reporting", + "fxhash", + "log", + "num-traits", + "pp-rs", + "spirv_headers", + "thiserror", +] + [[package]] name = "native-dialog" version = "0.5.5" @@ -3845,6 +3871,15 @@ dependencies = [ "rand 0.8.3", ] +[[package]] +name = "pp-rs" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb458bb7f6e250e6eb79d5026badc10a3ebb8f9a15d1fff0f13d17c71f4d6dee" +dependencies = [ + "unicode-xid 0.2.2", +] + [[package]] name = "ppv-lite86" version = "0.2.10" @@ -4746,26 +4781,6 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" -[[package]] -name = "shaderc" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50b8aeaae10b9bda5cba66736a7e265f67698e912e1cc6a4678acba286e22be9" -dependencies = [ - "libc", - "shaderc-sys", -] - -[[package]] -name = "shaderc-sys" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b12d7c62d6732884c9dfab587503fa3a795b108df152415a89da23812d4737e" -dependencies = [ - "cmake", - "libc", -] - [[package]] name = "sharded-slab" version = "0.1.1" @@ -6043,6 +6058,7 @@ dependencies = [ "bincode", "bytemuck", "chrono", + "codespan-reporting", "comma", "conrod_core", "conrod_winit", @@ -6059,6 +6075,7 @@ dependencies = [ "euc", "futures-executor", "gilrs", + "glsl-include", "glyph_brush", "guillotiere", "hashbrown 0.11.2", @@ -6068,6 +6085,7 @@ dependencies = [ "itertools 0.10.0", "keyboard-keynames", "lazy_static", + "naga 0.5.0", "native-dialog", "num 0.4.0", "num_cpus", @@ -6078,7 +6096,6 @@ dependencies = [ "rodio", "ron", "serde", - "shaderc", "specs", "specs-idvs", "strum", @@ -6680,7 +6697,7 @@ dependencies = [ "arrayvec", "js-sys", "log", - "naga", + "naga 0.4.0", "parking_lot 0.11.1", "raw-window-handle", "serde", @@ -6712,7 +6729,7 @@ dependencies = [ "gpu-alloc", "gpu-descriptor", "log", - "naga", + "naga 0.4.0", "parking_lot 0.11.1", "profiling", "raw-window-handle", diff --git a/Cargo.toml b/Cargo.toml index 23e860d861..91e90eebaa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -133,9 +133,8 @@ nativeBuildInputs = ["pkg-config"] vek = { git = "https://gitlab.com/veloren/vek.git", branch = "fix_intrinsics2" } # patch wgpu so we can use wgpu-profiler crate wgpu = { git = "https://github.com/gfx-rs/wgpu.git", rev = "a92b8549a8e2cb9dac781bafc5ed32828f3caf46" } +naga = { git = "https://github.com/gfx-rs/naga.git", rev = "607206f9a5cc33a8b1e7cec84663be6390e3be7f" } -# # use the latest fixes in naga (remove when updates trickle down to wgpu-rs) -# naga = { git = "https://github.com/gfx-rs/naga.git", rev = "3a0f0144112ff621dd7f731bf455adf6cab19164" } # # use the latest fixes in gfx (remove when updates trickle down to wgpu-rs) # gfx-hal = { git = "https://github.com/gfx-rs/gfx.git", rev = "e305dcca3557923a6a8810162d8dd09cb45a43a6" } # gfx-backend-empty = { git = "https://github.com/gfx-rs/gfx.git", rev = "e305dcca3557923a6a8810162d8dd09cb45a43a6" } diff --git a/assets/voxygen/shaders/blit-frag.glsl b/assets/voxygen/shaders/blit-frag.glsl index d0818b374a..c54cca2a8f 100644 --- a/assets/voxygen/shaders/blit-frag.glsl +++ b/assets/voxygen/shaders/blit-frag.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core layout(set = 0, binding = 0) uniform texture2D t_src_color; diff --git a/assets/voxygen/shaders/blit-vert.glsl b/assets/voxygen/shaders/blit-vert.glsl index f6538bd743..c6f9c04eda 100644 --- a/assets/voxygen/shaders/blit-vert.glsl +++ b/assets/voxygen/shaders/blit-vert.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core layout(location = 0) out vec2 uv; diff --git a/assets/voxygen/shaders/clouds-frag.glsl b/assets/voxygen/shaders/clouds-frag.glsl index 7d02bf0f1d..9e83b8f168 100644 --- a/assets/voxygen/shaders/clouds-frag.glsl +++ b/assets/voxygen/shaders/clouds-frag.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/clouds-vert.glsl b/assets/voxygen/shaders/clouds-vert.glsl index 933d3a3dc3..2c77d689c3 100644 --- a/assets/voxygen/shaders/clouds-vert.glsl +++ b/assets/voxygen/shaders/clouds-vert.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/debug-frag.glsl b/assets/voxygen/shaders/debug-frag.glsl index c681b64bfc..da1932b1cf 100644 --- a/assets/voxygen/shaders/debug-frag.glsl +++ b/assets/voxygen/shaders/debug-frag.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/debug-vert.glsl b/assets/voxygen/shaders/debug-vert.glsl index 97d774a642..14c0221ecd 100644 --- a/assets/voxygen/shaders/debug-vert.glsl +++ b/assets/voxygen/shaders/debug-vert.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/figure-frag.glsl b/assets/voxygen/shaders/figure-frag.glsl index 3b084366da..3cb0af7d38 100644 --- a/assets/voxygen/shaders/figure-frag.glsl +++ b/assets/voxygen/shaders/figure-frag.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #define FIGURE_SHADER diff --git a/assets/voxygen/shaders/figure-vert.glsl b/assets/voxygen/shaders/figure-vert.glsl index 489c439d4b..1cbb86a54f 100644 --- a/assets/voxygen/shaders/figure-vert.glsl +++ b/assets/voxygen/shaders/figure-vert.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/fluid-frag/cheap.glsl b/assets/voxygen/shaders/fluid-frag/cheap.glsl index f405fdd039..aa6e5988d2 100644 --- a/assets/voxygen/shaders/fluid-frag/cheap.glsl +++ b/assets/voxygen/shaders/fluid-frag/cheap.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/fluid-frag/shiny.glsl b/assets/voxygen/shaders/fluid-frag/shiny.glsl index 019e2b0618..8b008c2b3a 100644 --- a/assets/voxygen/shaders/fluid-frag/shiny.glsl +++ b/assets/voxygen/shaders/fluid-frag/shiny.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/fluid-vert.glsl b/assets/voxygen/shaders/fluid-vert.glsl index 0d68559912..cfdfdd8876 100644 --- a/assets/voxygen/shaders/fluid-vert.glsl +++ b/assets/voxygen/shaders/fluid-vert.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/include/random.glsl b/assets/voxygen/shaders/include/random.glsl index 482e631911..fc4c0afb24 100644 --- a/assets/voxygen/shaders/include/random.glsl +++ b/assets/voxygen/shaders/include/random.glsl @@ -98,8 +98,8 @@ vec4 rand_perm_4(vec4 pos) { vec3 smooth_rand(vec3 pos, float lerp_axis) { return vec3(snoise(vec4(pos, lerp_axis)), snoise(vec4(pos + 400.0, lerp_axis)), snoise(vec4(pos + 1000.0, lerp_axis))); - vec3 r0 = rand_perm_3(vec3(pos.x, pos.y, pos.z) + floor(lerp_axis)); - vec3 r1 = rand_perm_3(vec3(pos.x, pos.y, pos.z) + floor(lerp_axis + 1.0)); - return r0 + (r1 - r0) * fract(lerp_axis); + // vec3 r0 = rand_perm_3(vec3(pos.x, pos.y, pos.z) + floor(lerp_axis)); + // vec3 r1 = rand_perm_3(vec3(pos.x, pos.y, pos.z) + floor(lerp_axis + 1.0)); + // return r0 + (r1 - r0) * fract(lerp_axis); } #endif diff --git a/assets/voxygen/shaders/light-shadows-directed-vert.glsl b/assets/voxygen/shaders/light-shadows-directed-vert.glsl index 99aeaa61b1..a99cae8373 100644 --- a/assets/voxygen/shaders/light-shadows-directed-vert.glsl +++ b/assets/voxygen/shaders/light-shadows-directed-vert.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core // #extension ARB_texture_storage : enable #include diff --git a/assets/voxygen/shaders/light-shadows-figure-vert.glsl b/assets/voxygen/shaders/light-shadows-figure-vert.glsl index 1df58ae055..2ba601889c 100644 --- a/assets/voxygen/shaders/light-shadows-figure-vert.glsl +++ b/assets/voxygen/shaders/light-shadows-figure-vert.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core // #extension ARB_texture_storage : enable #define FIGURE_SHADER diff --git a/assets/voxygen/shaders/light-shadows-frag.glsl b/assets/voxygen/shaders/light-shadows-frag.glsl index 3671ace978..b264f60495 100644 --- a/assets/voxygen/shaders/light-shadows-frag.glsl +++ b/assets/voxygen/shaders/light-shadows-frag.glsl @@ -2,7 +2,7 @@ // // However, in the future we might apply some depth transforms here. -#version 420 core +#version 460 core // #extension ARB_texture_storage : enable #include diff --git a/assets/voxygen/shaders/lod-terrain-frag.glsl b/assets/voxygen/shaders/lod-terrain-frag.glsl index f55ae4e98c..9e84124a35 100644 --- a/assets/voxygen/shaders/lod-terrain-frag.glsl +++ b/assets/voxygen/shaders/lod-terrain-frag.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/lod-terrain-vert.glsl b/assets/voxygen/shaders/lod-terrain-vert.glsl index 48554ea74e..c80cd4a1a8 100644 --- a/assets/voxygen/shaders/lod-terrain-vert.glsl +++ b/assets/voxygen/shaders/lod-terrain-vert.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/particle-frag.glsl b/assets/voxygen/shaders/particle-frag.glsl index bd04b3388d..d6f89694c5 100644 --- a/assets/voxygen/shaders/particle-frag.glsl +++ b/assets/voxygen/shaders/particle-frag.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/particle-vert.glsl b/assets/voxygen/shaders/particle-vert.glsl index 7a2396d69f..80962dfee1 100644 --- a/assets/voxygen/shaders/particle-vert.glsl +++ b/assets/voxygen/shaders/particle-vert.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/point-light-shadows-vert.glsl b/assets/voxygen/shaders/point-light-shadows-vert.glsl index 7670a2f368..dca09849b6 100644 --- a/assets/voxygen/shaders/point-light-shadows-vert.glsl +++ b/assets/voxygen/shaders/point-light-shadows-vert.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core // #extension ARB_texture_storage : enable #include diff --git a/assets/voxygen/shaders/postprocess-frag.glsl b/assets/voxygen/shaders/postprocess-frag.glsl index aeba45f350..2ec43b0508 100644 --- a/assets/voxygen/shaders/postprocess-frag.glsl +++ b/assets/voxygen/shaders/postprocess-frag.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/postprocess-vert.glsl b/assets/voxygen/shaders/postprocess-vert.glsl index 21a6b3d8c2..a0c31268cf 100644 --- a/assets/voxygen/shaders/postprocess-vert.glsl +++ b/assets/voxygen/shaders/postprocess-vert.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/skybox-frag.glsl b/assets/voxygen/shaders/skybox-frag.glsl index 37e1676520..97fc04f3ef 100644 --- a/assets/voxygen/shaders/skybox-frag.glsl +++ b/assets/voxygen/shaders/skybox-frag.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/skybox-vert.glsl b/assets/voxygen/shaders/skybox-vert.glsl index 9f362fedb7..0628170054 100644 --- a/assets/voxygen/shaders/skybox-vert.glsl +++ b/assets/voxygen/shaders/skybox-vert.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/sprite-frag.glsl b/assets/voxygen/shaders/sprite-frag.glsl index 119bf61e59..f8c0fc65b5 100644 --- a/assets/voxygen/shaders/sprite-frag.glsl +++ b/assets/voxygen/shaders/sprite-frag.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/sprite-vert.glsl b/assets/voxygen/shaders/sprite-vert.glsl index 097e5e25f2..c0d1966f0c 100644 --- a/assets/voxygen/shaders/sprite-vert.glsl +++ b/assets/voxygen/shaders/sprite-vert.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/terrain-frag.glsl b/assets/voxygen/shaders/terrain-frag.glsl index 6aff7daaf3..56811591b0 100644 --- a/assets/voxygen/shaders/terrain-frag.glsl +++ b/assets/voxygen/shaders/terrain-frag.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core // #extension GL_ARB_texture_storage : require #include diff --git a/assets/voxygen/shaders/terrain-vert.glsl b/assets/voxygen/shaders/terrain-vert.glsl index 36be96776d..760314c831 100644 --- a/assets/voxygen/shaders/terrain-vert.glsl +++ b/assets/voxygen/shaders/terrain-vert.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/ui-frag.glsl b/assets/voxygen/shaders/ui-frag.glsl index d9010a2c90..368ccb65b3 100644 --- a/assets/voxygen/shaders/ui-frag.glsl +++ b/assets/voxygen/shaders/ui-frag.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/assets/voxygen/shaders/ui-vert.glsl b/assets/voxygen/shaders/ui-vert.glsl index 7c25f01fd3..06fe4aacdf 100644 --- a/assets/voxygen/shaders/ui-vert.glsl +++ b/assets/voxygen/shaders/ui-vert.glsl @@ -1,4 +1,4 @@ -#version 420 core +#version 460 core #include diff --git a/voxygen/Cargo.toml b/voxygen/Cargo.toml index 30e7e380bf..cccb7c1c13 100644 --- a/voxygen/Cargo.toml +++ b/voxygen/Cargo.toml @@ -53,7 +53,9 @@ winit = {version = "0.25.0", features = ["serde"]} wgpu = { version = "=0.8.0", features = ["trace", "cross"] } wgpu-profiler = { git = "https://github.com/Imberflur/wgpu-profiler", tag = "wgpu-0.8" } bytemuck = { version="1.4", features=["derive"] } -shaderc = "0.6.2" +naga = { version = "0.5.0", features = ["glsl-in", "glsl-validate", "spv-out", "codespan-reporting"] } +glsl-include = "0.3.1" +codespan-reporting = "0.11.1" # Ui conrod_core = {git = "https://gitlab.com/veloren/conrod.git", branch="copypasta_0.7"} diff --git a/voxygen/src/render/error.rs b/voxygen/src/render/error.rs index d3736216c4..e298ee12c6 100644 --- a/voxygen/src/render/error.rs +++ b/voxygen/src/render/error.rs @@ -6,8 +6,10 @@ pub enum RenderError { SwapChainError(wgpu::SwapChainError), CustomError(String), CouldNotFindAdapter, - ErrorInitializingCompiler, - ShaderError(String, shaderc::Error), + GlslIncludeError(String, glsl_include::Error), + ParserError(String, naga::front::glsl::ParseError), + ValidationError(String, naga::valid::ValidationError), + SpirvError(String, naga::back::spv::Error), } use std::fmt; @@ -25,10 +27,24 @@ impl fmt::Debug for RenderError { .finish(), Self::CustomError(err) => f.debug_tuple("CustomError").field(err).finish(), Self::CouldNotFindAdapter => f.debug_tuple("CouldNotFindAdapter").finish(), - Self::ErrorInitializingCompiler => f.debug_tuple("ErrorInitializingCompiler").finish(), - Self::ShaderError(shader_name, err) => write!( + Self::GlslIncludeError(shader_name, err) => write!( f, - "\"{}\" shader failed to compile due to the following error: {}", + "\"{}\" shader contains invalid include directives: {}", + shader_name, err + ), + Self::ParserError(shader_name, err) => write!( + f, + "\"{}\" shader failed to parse due to the following error: {}", + shader_name, err + ), + Self::ValidationError(shader_name, err) => write!( + f, + "\"{}\" shader failed to validate due to the following error: {}", + shader_name, err + ), + Self::SpirvError(shader_name, err) => write!( + f, + "\"{}\" shader failed to emit due to the following error: {}", shader_name, err ), } @@ -47,8 +63,24 @@ impl From for RenderError { fn from(err: wgpu::SwapChainError) -> Self { Self::SwapChainError(err) } } -impl From<(&str, shaderc::Error)> for RenderError { - fn from((shader_name, err): (&str, shaderc::Error)) -> Self { - Self::ShaderError(shader_name.into(), err) +impl From<(&str, glsl_include::Error)> for RenderError { + fn from((shader_name, err): (&str, glsl_include::Error)) -> Self { + Self::GlslIncludeError(shader_name.into(), err) + } +} + +impl From<(&str, naga::front::glsl::ParseError)> for RenderError { + fn from((shader_name, err): (&str, naga::front::glsl::ParseError)) -> Self { + Self::ParserError(shader_name.into(), err) + } +} +impl From<(&str, naga::valid::ValidationError)> for RenderError { + fn from((shader_name, err): (&str, naga::valid::ValidationError)) -> Self { + Self::ValidationError(shader_name.into(), err) + } +} +impl From<(&str, naga::back::spv::Error)> for RenderError { + fn from((shader_name, err): (&str, naga::back::spv::Error)) -> Self { + Self::SpirvError(shader_name.into(), err) } } diff --git a/voxygen/src/render/renderer/pipeline_creation.rs b/voxygen/src/render/renderer/pipeline_creation.rs index 6ad5e483a9..ba903f826f 100644 --- a/voxygen/src/render/renderer/pipeline_creation.rs +++ b/voxygen/src/render/renderer/pipeline_creation.rs @@ -124,7 +124,6 @@ impl ShaderModules { has_shadow_views: bool, ) -> Result { prof_span!(_guard, "ShaderModules::new"); - use shaderc::{CompileOptions, Compiler, OptimizationLevel, ResolvedInclude, ShaderKind}; let constants = shaders.get("include.constants").unwrap(); let globals = shaders.get("include.globals").unwrap(); @@ -191,41 +190,28 @@ impl ShaderModules { }) .unwrap(); - let mut compiler = Compiler::new().ok_or(RenderError::ErrorInitializingCompiler)?; - let mut options = CompileOptions::new().ok_or(RenderError::ErrorInitializingCompiler)?; - options.set_optimization_level(OptimizationLevel::Performance); - options.set_forced_version_profile(430, shaderc::GlslProfile::Core); - options.set_include_callback(move |name, _, shader_name, _| { - Ok(ResolvedInclude { - resolved_name: name.to_string(), - content: match name { - "constants.glsl" => constants.clone(), - "globals.glsl" => globals.0.to_owned(), - "shadows.glsl" => shadows.0.to_owned(), - "sky.glsl" => sky.0.to_owned(), - "light.glsl" => light.0.to_owned(), - "srgb.glsl" => srgb.0.to_owned(), - "random.glsl" => random.0.to_owned(), - "lod.glsl" => lod.0.to_owned(), - "anti-aliasing.glsl" => anti_alias.0.to_owned(), - "cloud.glsl" => cloud.0.to_owned(), - other => { - return Err(format!( - "Include {} in {} is not defined", - other, shader_name - )); - }, - }, - }) - }); + let mut context = glsl_include::Context::new(); + context.include("constants.glsl", &constants); + context.include("globals.glsl", &globals.0); + context.include("shadows.glsl", &shadows.0); + context.include("sky.glsl", &sky.0); + context.include("light.glsl", &light.0); + context.include("srgb.glsl", &srgb.0); + context.include("random.glsl", &random.0); + context.include("lod.glsl", &lod.0); + context.include("anti-aliasing.glsl", &anti_alias.0); + context.include("cloud.glsl", &cloud.0); - let mut create_shader = |name, kind| { + let mut create_shader = |name, stage| { let glsl = &shaders .get(name) .unwrap_or_else(|| panic!("Can't retrieve shader: {}", name)) .0; let file_name = format!("{}.glsl", name); - create_shader_module(device, &mut compiler, glsl, kind, &file_name, &options) + let source = context + .expand(glsl) + .map_err(|err| (file_name.as_str(), err))?; + create_shader_module(device, &file_name, &source, stage) }; let selected_fluid_shader = ["fluid-frag.", match mode.fluid { @@ -235,41 +221,41 @@ impl ShaderModules { .concat(); Ok(Self { - skybox_vert: create_shader("skybox-vert", ShaderKind::Vertex)?, - skybox_frag: create_shader("skybox-frag", ShaderKind::Fragment)?, - debug_vert: create_shader("debug-vert", ShaderKind::Vertex)?, - debug_frag: create_shader("debug-frag", ShaderKind::Fragment)?, - figure_vert: create_shader("figure-vert", ShaderKind::Vertex)?, - figure_frag: create_shader("figure-frag", ShaderKind::Fragment)?, - terrain_vert: create_shader("terrain-vert", ShaderKind::Vertex)?, - terrain_frag: create_shader("terrain-frag", ShaderKind::Fragment)?, - fluid_vert: create_shader("fluid-vert", ShaderKind::Vertex)?, - fluid_frag: create_shader(&selected_fluid_shader, ShaderKind::Fragment)?, - sprite_vert: create_shader("sprite-vert", ShaderKind::Vertex)?, - sprite_frag: create_shader("sprite-frag", ShaderKind::Fragment)?, - particle_vert: create_shader("particle-vert", ShaderKind::Vertex)?, - particle_frag: create_shader("particle-frag", ShaderKind::Fragment)?, - ui_vert: create_shader("ui-vert", ShaderKind::Vertex)?, - ui_frag: create_shader("ui-frag", ShaderKind::Fragment)?, - lod_terrain_vert: create_shader("lod-terrain-vert", ShaderKind::Vertex)?, - lod_terrain_frag: create_shader("lod-terrain-frag", ShaderKind::Fragment)?, - clouds_vert: create_shader("clouds-vert", ShaderKind::Vertex)?, - clouds_frag: create_shader("clouds-frag", ShaderKind::Fragment)?, - postprocess_vert: create_shader("postprocess-vert", ShaderKind::Vertex)?, - postprocess_frag: create_shader("postprocess-frag", ShaderKind::Fragment)?, - blit_vert: create_shader("blit-vert", ShaderKind::Vertex)?, - blit_frag: create_shader("blit-frag", ShaderKind::Fragment)?, + skybox_vert: create_shader("skybox-vert", naga::ShaderStage::Vertex)?, + skybox_frag: create_shader("skybox-frag", naga::ShaderStage::Fragment)?, + debug_vert: create_shader("debug-vert", naga::ShaderStage::Vertex)?, + debug_frag: create_shader("debug-frag", naga::ShaderStage::Fragment)?, + figure_vert: create_shader("figure-vert", naga::ShaderStage::Vertex)?, + figure_frag: create_shader("figure-frag", naga::ShaderStage::Fragment)?, + terrain_vert: create_shader("terrain-vert", naga::ShaderStage::Vertex)?, + terrain_frag: create_shader("terrain-frag", naga::ShaderStage::Fragment)?, + fluid_vert: create_shader("fluid-vert", naga::ShaderStage::Vertex)?, + fluid_frag: create_shader(&selected_fluid_shader, naga::ShaderStage::Fragment)?, + sprite_vert: create_shader("sprite-vert", naga::ShaderStage::Vertex)?, + sprite_frag: create_shader("sprite-frag", naga::ShaderStage::Fragment)?, + particle_vert: create_shader("particle-vert", naga::ShaderStage::Vertex)?, + particle_frag: create_shader("particle-frag", naga::ShaderStage::Fragment)?, + ui_vert: create_shader("ui-vert", naga::ShaderStage::Vertex)?, + ui_frag: create_shader("ui-frag", naga::ShaderStage::Fragment)?, + lod_terrain_vert: create_shader("lod-terrain-vert", naga::ShaderStage::Vertex)?, + lod_terrain_frag: create_shader("lod-terrain-frag", naga::ShaderStage::Fragment)?, + clouds_vert: create_shader("clouds-vert", naga::ShaderStage::Vertex)?, + clouds_frag: create_shader("clouds-frag", naga::ShaderStage::Fragment)?, + postprocess_vert: create_shader("postprocess-vert", naga::ShaderStage::Vertex)?, + postprocess_frag: create_shader("postprocess-frag", naga::ShaderStage::Fragment)?, + blit_vert: create_shader("blit-vert", naga::ShaderStage::Vertex)?, + blit_frag: create_shader("blit-frag", naga::ShaderStage::Fragment)?, point_light_shadows_vert: create_shader( "point-light-shadows-vert", - ShaderKind::Vertex, + naga::ShaderStage::Vertex, )?, light_shadows_directed_vert: create_shader( "light-shadows-directed-vert", - ShaderKind::Vertex, + naga::ShaderStage::Vertex, )?, light_shadows_figure_vert: create_shader( "light-shadows-figure-vert", - ShaderKind::Vertex, + naga::ShaderStage::Vertex, )?, }) } @@ -277,23 +263,91 @@ impl ShaderModules { fn create_shader_module( device: &wgpu::Device, - compiler: &mut shaderc::Compiler, - source: &str, - kind: shaderc::ShaderKind, file_name: &str, - options: &shaderc::CompileOptions, + source: &str, + stage: naga::ShaderStage, ) -> Result { prof_span!(_guard, "create_shader_modules"); - use std::borrow::Cow; - let spv = compiler - .compile_into_spirv(source, kind, file_name, "main", Some(options)) - .map_err(|e| (file_name, e))?; + use codespan_reporting::{ + diagnostic::{Diagnostic, Label}, + files::SimpleFile, + term::{ + self, + termcolor::{ColorChoice, StandardStream}, + }, + }; + use naga::{ + back::spv, + front::glsl, + valid::{Capabilities, ValidationFlags, Validator}, + }; + use std::error::Error; + + // Parse + let mut options = glsl::Options::default(); + options.entry_points.insert("main".to_string(), stage); + let module = glsl::parse_str(source, &options).map_err(|err| { + let diagnostic = match err.kind.metadata() { + Some(metadata) => Diagnostic::error() + .with_message(err.kind.to_string()) + .with_labels(vec![Label::primary((), metadata.start..metadata.end)]), + None => Diagnostic::error().with_message(err.kind.to_string()), + }; + + let files = SimpleFile::new(file_name, source); + let config = codespan_reporting::term::Config::default(); + let writer = StandardStream::stderr(ColorChoice::Auto); + let _ = term::emit(&mut writer.lock(), &config, &files, &diagnostic); + + (file_name, err) + })?; + + // Validate + let info = Validator::new(ValidationFlags::all(), Capabilities::all()) + .validate(&module) + .map_err(|err| { + eprint!("{}", err); + + let mut e = err.source(); + if e.is_some() { + eprintln!(": "); + } else { + eprintln!(); + } + + while let Some(source) = e { + eprintln!("\t{}", source); + e = source.source(); + } + + (file_name, err) + })?; + + // Emit + let spv_options = spv::Options::default(); + let spv = spv::write_vec(&module, &info, &spv_options).map_err(|err| { + eprint!("{}", err); + + let mut e = err.source(); + if e.is_some() { + eprintln!(": "); + } else { + eprintln!(); + } + + while let Some(source) = e { + eprintln!("\t{}", source); + e = source.source(); + } + + (file_name, err) + })?; let label = [file_name, "\n\n", source].concat(); Ok(device.create_shader_module(&wgpu::ShaderModuleDescriptor { label: Some(&label), - source: wgpu::ShaderSource::SpirV(Cow::Borrowed(spv.as_binary())), + source: wgpu::ShaderSource::SpirV(spv.into()), flags: wgpu::ShaderFlags::empty(), // TODO: renable // flags: wgpu::ShaderFlags::VALIDATION, }))