diff --git a/imgui-gfx-renderer/Cargo.toml b/imgui-gfx-renderer/Cargo.toml index e93979a..495036b 100644 --- a/imgui-gfx-renderer/Cargo.toml +++ b/imgui-gfx-renderer/Cargo.toml @@ -21,7 +21,6 @@ travis-ci = { repository = "Gekkio/imgui-rs" } gfx = "0.18" imgui = { version = "0.0.24-pre", path = "../" } imgui-sys = { version = "0.0.24-pre", path = "../imgui-sys", features = ["gfx"] } -cfg-if = "0.1" [target.'cfg(windows)'.build-dependencies] winapi = { version = "0.3", features = ["d3dcompiler"] } diff --git a/imgui-gfx-renderer/build.rs b/imgui-gfx-renderer/build.rs index 0cef94b..6625d4a 100644 --- a/imgui-gfx-renderer/build.rs +++ b/imgui-gfx-renderer/build.rs @@ -1,34 +1,41 @@ fn main() { #[cfg(windows)] - hlsl_build::compile_hlsl_shaders(); + { + // Note: When building on Windows, this build script will automatically recompile the HLSL shaders. + + use std::env; + use std::path::PathBuf; + + let src_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("src"); + + hlsl_build::update_hlsl_shaders( + &src_dir.join("shader").join("sm_40.hlsl"), + &src_dir.join("data").join("vertex.fx"), + &src_dir.join("data").join("pixel.fx"), + ); + } } #[cfg(windows)] mod hlsl_build { - use std::env; - use std::ffi::CStr; use std::ffi::CString; use std::fs; use std::path::Path; - use std::path::PathBuf; use std::ptr; use std::slice; use std::str; - pub fn compile_hlsl_shaders() { - let source_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()) - .join("src") - .join("shader") - .join("sm_40.hlsl"); - + pub fn update_hlsl_shaders( + source_path: &Path, + vertex_destination: &Path, + pixel_destination: &Path, + ) { println!("cargo:rerun-if-changed={}", source_path.display()); let src_data = fs::read_to_string(&source_path).unwrap(); - let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); - fs::write( - out_dir.join("hlsl_vertex_shader_bytecode"), + vertex_destination, compile_shader(&src_data, &source_path, "VertexMain", "vs_4_0").unwrap_or_else( |error_message| { eprintln!("{}", error_message); @@ -39,7 +46,7 @@ mod hlsl_build { .unwrap(); fs::write( - out_dir.join("hlsl_pixel_shader_bytecode"), + pixel_destination, compile_shader(&src_data, &source_path, "PixelMain", "ps_4_0").unwrap_or_else( |error_message| { eprintln!("{}", error_message); diff --git a/imgui-gfx-renderer/src/data/pixel.fx b/imgui-gfx-renderer/src/data/pixel.fx new file mode 100644 index 0000000..697cba0 Binary files /dev/null and b/imgui-gfx-renderer/src/data/pixel.fx differ diff --git a/imgui-gfx-renderer/src/data/vertex.fx b/imgui-gfx-renderer/src/data/vertex.fx new file mode 100644 index 0000000..2fccc8a Binary files /dev/null and b/imgui-gfx-renderer/src/data/vertex.fx differ diff --git a/imgui-gfx-renderer/src/lib.rs b/imgui-gfx-renderer/src/lib.rs index dbe1bec..bfaf85a 100644 --- a/imgui-gfx-renderer/src/lib.rs +++ b/imgui-gfx-renderer/src/lib.rs @@ -167,21 +167,10 @@ impl Shaders { include_bytes!("shader/glsles_100.vert"), include_bytes!("shader/glsles_100.frag"), ), - HlslSm40 => { - cfg_if::cfg_if! { - if #[cfg(all(feature = "directx", windows))] { - const HLSL_BYTECODE: (&[u8], &[u8]) = ( - include_bytes!(concat!(env!("OUT_DIR"), "/hlsl_vertex_shader_bytecode")), - include_bytes!(concat!(env!("OUT_DIR"), "/hlsl_pixel_shader_bytecode")), - ); - } else { - // panic instead? - const HLSL_BYTECODE: (&[u8], &[u8]) = (&[0], &[0]); - } - } - - HLSL_BYTECODE - } + HlslSm40 => ( + include_bytes!("data/pixel.fx"), + include_bytes!("data/vertex.fx"), + ), } } } diff --git a/imgui-gfx-renderer/src/shader/sm_40.hlsl b/imgui-gfx-renderer/src/shader/sm_40.hlsl index 5063e04..b40ba25 100644 --- a/imgui-gfx-renderer/src/shader/sm_40.hlsl +++ b/imgui-gfx-renderer/src/shader/sm_40.hlsl @@ -1,14 +1,14 @@ cbuffer Constants : register(b0) { - float4x4 matrix_; + float4x4 matrix_; } Texture2D tex; SamplerState tex_; struct VIn { - float2 position : pos; - float2 uv : uv; - float4 color : col; + float2 position : pos; + float2 uv : uv; + float4 color : col; }; struct VOut @@ -30,5 +30,5 @@ VOut VertexMain(VIn vertex) float4 PixelMain(VOut vout) : SV_TARGET { - return vout.color * tex.Sample(tex_, vout.uv); + return vout.color * tex.Sample(tex_, vout.uv); } \ No newline at end of file