Commit shaders so they don't need to be missing on non-Windows platforms

This commit is contained in:
Michael Tang 2019-04-23 17:49:32 -07:00
parent b97bb58a56
commit fdc8a6a4e2
6 changed files with 30 additions and 35 deletions

View File

@ -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"] }

View File

@ -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);

Binary file not shown.

Binary file not shown.

View File

@ -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"),
),
}
}
}

View File

@ -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);
}