Skip to content

Commit

Permalink
Fix issue with missing capability when enabling shaderc optimizations
Browse files Browse the repository at this point in the history
See gfx-rs/wgpu#4915

Also:
* Remove unused vert-out frag-in variables from shaders (naga doesn't
  like this probably because they are optimized out on the fragment
  side). This restriction from naga may be relaxed in the future
  see gfx-rs/wgpu#3748.
* Enable OptimizationLevel::Performance for shaderc by default
* Add a environment variable VOXYGEN_SHADERC_OPTS for disabling this
  (e.g. to test if it actually makes a difference on any platform).
  (TODO: testing might be easier if there was a way to do toggle it
  without restarting...)
  • Loading branch information
Imberflur committed Feb 14, 2024
1 parent 68ba2b4 commit 5434ce5
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 29 deletions.
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,14 @@ async-trait = "0.1.42"

[patch.crates-io]
shred = { git = "https://github.com/amethyst/shred.git", rev = "5d52c6fc390dd04c12158633e77591f6523d1f85" }
# This is needed because of an issue with spirv & naga in wgpu 0.18, and there's an issue
# with uint in uniforms for gl.
wgpu = { git = "https://github.com/IsseW/wgpu", rev = "5ea160164" }
# This is needed because of:
# * an issue with spirv & naga in wgpu 0.18 (I assume this is fixed upstream but not in 0.18)
# * an issue with uint in uniforms for gl. (potentially fixed in 0.19?)
# * an issue with releasing current context on Windows opengl (fixed in 0.19.1)
# * another spirv issue: unused builtins not always being removed. (upstream PR open)
wgpu = { git = "https://github.com/Imberflur/wgpu.git", tag = "0.18-with-fixes-for-veloren-v1" }
# wgpu = { path = "../wgpu/wgpu" }

# # 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" }
# naga = { path = "../naga" }
# keyboard-keynames = { git = "https://gitlab.com/Capucho/keyboard-keynames.git", rev = "7b1375ee4ea01d0e0b80c419cb27f0498e67df3a" }

# # Uncomment this to use a local fork of winit (for testing purposes)
Expand All @@ -174,6 +175,5 @@ wgpu = { git = "https://github.com/IsseW/wgpu", rev = "5ea160164" }
# wgpu-hal = { path = "../wgpu/wgpu-hal" }
# wgpu-core = { path = "../wgpu/wgpu-core" }
# wgpu-types = { path = "../wgpu/wgpu-types" }
# naga = { path = "../wgpu/naga" }

[patch."https://github.com/gfx-rs/naga"]
# naga = { path = "../naga" }
1 change: 0 additions & 1 deletion assets/voxygen/shaders/terrain-frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ layout(location = 0) in vec3 f_pos;
// in vec3 f_chunk_pos;
// #ifdef FLUID_MODE_SHINY
layout(location = 1) flat in uint f_pos_norm;
layout(location = 2) flat in float f_load_time;
// #else
// const uint f_pos_norm = 0u;
// #endif
Expand Down
3 changes: 0 additions & 3 deletions assets/voxygen/shaders/terrain-vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ uniform u_locals {
layout(location = 0) out vec3 f_pos;
// #ifdef FLUID_MODE_SHINY
layout(location = 1) flat out uint f_pos_norm;
layout(location = 2) flat out float f_load_time;

// #if (SHADOW_MODE == SHADOW_MODE_MAP)
// out vec4 sun_pos;
Expand Down Expand Up @@ -77,8 +76,6 @@ void main() {

f_pos = (model_mat * vec4(f_chunk_pos, 1.0)).xyz - focus_off.xyz;

f_load_time = load_time;

vec3 v_pos = f_pos;

// Terrain 'pop-in' effect
Expand Down
2 changes: 0 additions & 2 deletions assets/voxygen/shaders/trail-frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

#include <globals.glsl>

layout(location = 0) in vec3 f_pos;

layout(location = 0) out vec4 tgt_color;

#include <sky.glsl>
Expand Down
6 changes: 1 addition & 5 deletions assets/voxygen/shaders/trail-vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

layout(location = 0) in vec3 v_pos;

layout(location = 0) out vec3 f_pos;

void main() {
f_pos = v_pos;

gl_Position = all_mat * vec4(f_pos - focus_off.xyz, 1);
gl_Position = all_mat * vec4(v_pos - focus_off.xyz, 1);
}
22 changes: 21 additions & 1 deletion voxygen/src/render/renderer/pipeline_creation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::{
};
use common_base::{prof_span, prof_span_alloc};
use std::sync::Arc;
use tracing::info;

/// All the pipelines
pub struct Pipelines {
Expand Down Expand Up @@ -282,7 +283,26 @@ impl ShaderModules {

let mut compiler = Compiler::new().ok_or(RenderError::ErrorInitializingCompiler)?;
let mut options = CompileOptions::new().ok_or(RenderError::ErrorInitializingCompiler)?;
options.set_optimization_level(OptimizationLevel::Zero);
use std::sync::OnceLock;
static OPTS: OnceLock<bool> = OnceLock::new();
let shaderc_opts = *OPTS.get_or_init(|| {
match std::env::var("VOXYGEN_SHADERC_OPTS").as_ref().map(|s| &**s) {
Ok("0" | "no") => {
info!("Disabled optimization by shaderc.");
false
},
Ok("1" | "yes") | Ok(_) | Err(_) => {
info!("Enabled optimization by shaderc.");
true
},
}
});

if shaderc_opts {
options.set_optimization_level(OptimizationLevel::Performance);
} else {
options.set_optimization_level(OptimizationLevel::Zero);
}
options.set_forced_version_profile(430, shaderc::GlslProfile::Core);
// options.set_generate_debug_info();
options.set_include_callback(move |name, _, shader_name, _| {
Expand Down

0 comments on commit 5434ce5

Please sign in to comment.