Use vulkano-shader-derive in the examples

This commit is contained in:
Pierre Krieger 2017-06-03 12:49:50 +02:00
parent 77797d0eec
commit ad3307d252
11 changed files with 124 additions and 175 deletions

View File

@ -2,16 +2,12 @@
name = "examples" name = "examples"
version = "0.1.0" version = "0.1.0"
authors = ["Pierre Krieger <pierre.krieger1708@gmail.com>"] authors = ["Pierre Krieger <pierre.krieger1708@gmail.com>"]
build = "build.rs"
[dependencies] [dependencies]
vulkano = { path = "../vulkano" } vulkano = { path = "../vulkano" }
vulkano-shader-derive = { path = "../vulkano-shader-derive" }
vulkano-win = { path = "../vulkano-win" } vulkano-win = { path = "../vulkano-win" }
cgmath = "0.12.0" cgmath = "0.12.0"
image = "0.6.1" image = "0.6.1"
winit = "0.6.4" winit = "0.6.4"
time = "0.1.35" time = "0.1.35"
[build-dependencies]
vk-sys = { path = "../vk-sys" }
vulkano-shaders = { path = "../vulkano-shaders" }

View File

@ -1,13 +0,0 @@
extern crate vulkano_shaders;
fn main() {
// building the shaders used in the examples
vulkano_shaders::build_glsl_shaders([
("src/bin/triangle_vs.glsl", vulkano_shaders::ShaderType::Vertex),
("src/bin/triangle_fs.glsl", vulkano_shaders::ShaderType::Fragment),
("src/bin/teapot_vs.glsl", vulkano_shaders::ShaderType::Vertex),
("src/bin/teapot_fs.glsl", vulkano_shaders::ShaderType::Fragment),
("src/bin/image_vs.glsl", vulkano_shaders::ShaderType::Vertex),
("src/bin/image_fs.glsl", vulkano_shaders::ShaderType::Fragment),
].iter().cloned());
}

View File

@ -13,6 +13,8 @@ extern crate winit;
#[macro_use] #[macro_use]
extern crate vulkano; extern crate vulkano;
#[macro_use]
extern crate vulkano_shader_derive;
extern crate vulkano_win; extern crate vulkano_win;
use vulkano_win::VkSurfaceBuild; use vulkano_win::VkSurfaceBuild;
@ -77,9 +79,7 @@ fn main() {
Vertex { position: [ 0.5, 0.5 ] }, Vertex { position: [ 0.5, 0.5 ] },
].iter().cloned()).expect("failed to create buffer"); ].iter().cloned()).expect("failed to create buffer");
mod vs { include!{concat!(env!("OUT_DIR"), "/shaders/src/bin/image_vs.glsl")} }
let vs = vs::Shader::load(&device).expect("failed to create shader module"); let vs = vs::Shader::load(&device).expect("failed to create shader module");
mod fs { include!{concat!(env!("OUT_DIR"), "/shaders/src/bin/image_fs.glsl")} }
let fs = fs::Shader::load(&device).expect("failed to create shader module"); let fs = fs::Shader::load(&device).expect("failed to create shader module");
let renderpass = Arc::new( let renderpass = Arc::new(
@ -197,3 +197,38 @@ fn main() {
if done { return; } if done { return; }
} }
} }
mod vs {
#[derive(VulkanoShader)]
#[ty = "vertex"]
#[src = "
#version 450
layout(location = 0) in vec2 position;
layout(location = 0) out vec2 tex_coords;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
tex_coords = position + vec2(0.5);
}
"]
struct Dummy;
}
mod fs {
#[derive(VulkanoShader)]
#[ty = "fragment"]
#[src = "
#version 450
layout(location = 0) in vec2 tex_coords;
layout(location = 0) out vec4 f_color;
layout(set = 0, binding = 0) uniform sampler2D tex;
void main() {
f_color = texture(tex, tex_coords);
}
"]
struct Dummy;
}

View File

@ -1,22 +0,0 @@
// Copyright (c) 2016 The vulkano developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_450pack : enable
layout(location = 0) in vec2 tex_coords;
layout(location = 0) out vec4 f_color;
layout(set = 0, binding = 0) uniform sampler2D tex;
void main() {
f_color = texture(tex, tex_coords);
}

View File

@ -1,21 +0,0 @@
// Copyright (c) 2016 The vulkano developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_450pack : enable
layout(location = 0) in vec2 position;
layout(location = 0) out vec2 tex_coords;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
tex_coords = position + vec2(0.5);
}

View File

@ -14,6 +14,8 @@ extern crate time;
#[macro_use] #[macro_use]
extern crate vulkano; extern crate vulkano;
#[macro_use]
extern crate vulkano_shader_derive;
extern crate vulkano_win; extern crate vulkano_win;
use vulkano_win::VkSurfaceBuild; use vulkano_win::VkSurfaceBuild;
@ -23,9 +25,6 @@ use vulkano::image::ImageView;
use std::sync::Arc; use std::sync::Arc;
mod vs { include!{concat!(env!("OUT_DIR"), "/shaders/src/bin/teapot_vs.glsl")} }
mod fs { include!{concat!(env!("OUT_DIR"), "/shaders/src/bin/teapot_fs.glsl")} }
fn main() { fn main() {
// The start of this example is exactly the same as `triangle`. You should read the // The start of this example is exactly the same as `triangle`. You should read the
// `triangle` example if you haven't done so yet. // `triangle` example if you haven't done so yet.
@ -208,3 +207,51 @@ fn main() {
if done { return; } if done { return; }
} }
} }
mod vs {
#[derive(VulkanoShader)]
#[ty = "vertex"]
#[src = "
#version 450
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normal;
layout(location = 0) out vec3 v_normal;
layout(set = 0, binding = 0) uniform Data {
mat4 world;
mat4 view;
mat4 proj;
} uniforms;
void main() {
mat4 worldview = uniforms.view * uniforms.world;
v_normal = transpose(inverse(mat3(worldview))) * normal;
gl_Position = uniforms.proj * worldview * vec4(position, 1.0);
}
"]
struct Dummy;
}
mod fs {
#[derive(VulkanoShader)]
#[ty = "fragment"]
#[src = "
#version 450
layout(location = 0) in vec3 v_normal;
layout(location = 0) out vec4 f_color;
const vec3 LIGHT = vec3(0.0, 0.0, 1.0);
void main() {
float brightness = dot(normalize(v_normal), normalize(LIGHT));
vec3 dark_color = vec3(0.6, 0.0, 0.0);
vec3 regular_color = vec3(1.0, 0.0, 0.0);
f_color = vec4(mix(dark_color, regular_color, brightness), 1.0);
}
"]
struct Dummy;
}

View File

@ -1,26 +0,0 @@
// Copyright (c) 2016 The vulkano developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_450pack : enable
layout(location = 0) in vec3 v_normal;
layout(location = 0) out vec4 f_color;
const vec3 LIGHT = vec3(0.0, 0.0, 1.0);
void main() {
float brightness = dot(normalize(v_normal), normalize(LIGHT));
vec3 dark_color = vec3(0.6, 0.0, 0.0);
vec3 regular_color = vec3(1.0, 0.0, 0.0);
f_color = vec4(mix(dark_color, regular_color, brightness), 1.0);
}

View File

@ -1,30 +0,0 @@
// Copyright (c) 2016 The vulkano developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_450pack : enable
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normal;
layout(location = 0) out vec3 v_normal;
layout(set = 0, binding = 0) uniform Data {
mat4 world;
mat4 view;
mat4 proj;
} uniforms;
void main() {
mat4 worldview = uniforms.view * uniforms.world;
v_normal = transpose(inverse(mat3(worldview))) * normal;
gl_Position = uniforms.proj * worldview * vec4(position, 1.0);
}

View File

@ -21,6 +21,10 @@
// The `vulkano` crate is the main crate that you must use to use Vulkan. // The `vulkano` crate is the main crate that you must use to use Vulkan.
#[macro_use] #[macro_use]
extern crate vulkano; extern crate vulkano;
// The `vulkano_shader_derive` crate allows us to use the `VulkanoShader` custom derive that we use
// in this example.
#[macro_use]
extern crate vulkano_shader_derive;
// However the Vulkan library doesn't provide any functionality to create and handle windows, as // However the Vulkan library doesn't provide any functionality to create and handle windows, as
// this would be out of scope. In order to open a window, we are going to use the `winit` crate. // this would be out of scope. In order to open a window, we are going to use the `winit` crate.
extern crate winit; extern crate winit;
@ -205,23 +209,40 @@ fn main() {
// The next step is to create the shaders. // The next step is to create the shaders.
// //
// The shader creation API provided by the vulkano library is unsafe, for various reasons. // The raw shader creation API provided by the vulkano library is unsafe, for various reasons.
// //
// Instead, in our build script we used the `vulkano-shaders` crate to parse our shaders at // TODO: explain this in details
// compile time and provide a safe wrapper over vulkano's API. See the `build.rs` file at the mod vs {
// root of the crate. You can find the shaders' source code in the `triangle_fs.glsl` and #[derive(VulkanoShader)]
// `triangle_vs.glsl` files. #[ty = "vertex"]
// #[src = "
// The author knows that this system is crappy and that it would be far better to use a plugin. #version 450
// Unfortunately plugins aren't available in stable Rust yet.
// layout(location = 0) in vec2 position;
// The code generated by the build script created a struct named `TriangleShader`, which we
// can now use to load the shader. void main() {
// gl_Position = vec4(position, 0.0, 1.0);
// Because of some restrictions with the `include!` macro, we need to use a module. }
mod vs { include!{concat!(env!("OUT_DIR"), "/shaders/src/bin/triangle_vs.glsl")} } "]
struct Dummy;
}
mod fs {
#[derive(VulkanoShader)]
#[ty = "fragment"]
#[src = "
#version 450
layout(location = 0) out vec4 f_color;
void main() {
f_color = vec4(1.0, 0.0, 0.0, 1.0);
}
"]
struct Dummy;
}
let vs = vs::Shader::load(&device).expect("failed to create shader module"); let vs = vs::Shader::load(&device).expect("failed to create shader module");
mod fs { include!{concat!(env!("OUT_DIR"), "/shaders/src/bin/triangle_fs.glsl")} }
let fs = fs::Shader::load(&device).expect("failed to create shader module"); let fs = fs::Shader::load(&device).expect("failed to create shader module");
// At this point, OpenGL initialization would be finished. However in Vulkan it is not. OpenGL // At this point, OpenGL initialization would be finished. However in Vulkan it is not. OpenGL

View File

@ -1,19 +0,0 @@
// Copyright (c) 2016 The vulkano developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_450pack : enable
layout(location = 0) out vec4 f_color;
void main() {
f_color = vec4(1.0, 0.0, 0.0, 1.0);
}

View File

@ -1,19 +0,0 @@
// Copyright (c) 2016 The vulkano developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_450pack : enable
layout(location = 0) in vec2 position;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
}