From 8a10c06ad59c0b224084354ced09373e27f82088 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Tue, 18 Feb 2020 20:26:02 -0800 Subject: [PATCH] add texture to frag shader. fix weird macro name collision bug --- ROADMAP.md | 1 + bevy_derive/src/lib.rs | 2 +- docs/debugging.md | 6 ++++++ examples/custom_shader.rs | 2 +- examples/texture.rs | 4 ++-- src/render/color.rs | 6 ++++++ .../render_graph/pipelines/forward/forward.frag | 11 ++++++++++- 7 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 docs/debugging.md diff --git a/ROADMAP.md b/ROADMAP.md index b537e4d5d7..cd0e9296ba 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -11,6 +11,7 @@ Here is the current list of planned features. All items are sorted in approximat * Skeletal animation * Macro to produce vertex buffer attributes (and maybe descriptors) from structs * Add runtime type safety to uniform bindings (and maybe compile time) + * Inject layout set/bindings into shader source so they don't need to be defined in-shader * Error Handling * Custom error type? * Remove as many panics / unwraps as possible diff --git a/bevy_derive/src/lib.rs b/bevy_derive/src/lib.rs index 34b62ef7b4..eb80a20c16 100644 --- a/bevy_derive/src/lib.rs +++ b/bevy_derive/src/lib.rs @@ -166,7 +166,7 @@ pub fn derive_uniforms(input: TokenStream) -> TokenStream { Some(potential_shader_defs.drain(..) .filter(|(f, shader_def)| shader_def.is_some()) - .map(|(f, shader_def)| format!("{}{}", f, shader_def.unwrap())) + .map(|(f, shader_def)| format!("{}_{}{}", #struct_name_screaming_snake, f, shader_def.unwrap())) .collect::>()) } } diff --git a/docs/debugging.md b/docs/debugging.md new file mode 100644 index 0000000000..aafd3c2e35 --- /dev/null +++ b/docs/debugging.md @@ -0,0 +1,6 @@ +# Debugging + +## Macro Debugging + +* Print the final output of a macro using ```cargo rustc --profile=check -- -Zunstable-options --pretty=expanded``` +* Print output during macro compilation using ```eprintln!("hi");``` \ No newline at end of file diff --git a/examples/custom_shader.rs b/examples/custom_shader.rs index 9cfc014d2f..24768f5687 100644 --- a/examples/custom_shader.rs +++ b/examples/custom_shader.rs @@ -60,7 +60,7 @@ fn main() { void main() { o_Target = color; - # ifdef always_red + # ifdef MY_MATERIAL_always_red o_Target = vec4(0.8, 0.0, 0.0, 1.0); # endif } diff --git a/examples/texture.rs b/examples/texture.rs index b56f852e5f..8adb5a7755 100644 --- a/examples/texture.rs +++ b/examples/texture.rs @@ -10,7 +10,7 @@ fn setup(world: &mut World) { mesh_storage.add(Mesh::load(MeshType::Cube)) }; - let _texture_handle = { + let texture_handle = { let mut texture_storage = world.resources.get_mut::>().unwrap(); let texture = Texture::load(TextureType::Data(asset::create_texels(256))); texture_storage.add(texture) @@ -22,7 +22,7 @@ fn setup(world: &mut World) { .add_archetype(MeshEntity { mesh: cube_handle.clone(), material: StandardMaterial { - albedo: math::vec4(0.5, 0.3, 0.3, 1.0).into(), + albedo: texture_handle.into(), }, translation: Translation::new(0.0, 0.0, 1.0), ..Default::default() diff --git a/src/render/color.rs b/src/render/color.rs index 53cc578dc1..5b103d3c01 100644 --- a/src/render/color.rs +++ b/src/render/color.rs @@ -15,6 +15,12 @@ impl From for ColorSource { } } +impl From> for ColorSource { + fn from(texture: Handle) -> Self { + ColorSource::Texture(texture) + } +} + impl ShaderDefSuffixProvider for ColorSource { fn get_shader_def(&self) -> Option<&'static str> { match *self { diff --git a/src/render/render_graph/pipelines/forward/forward.frag b/src/render/render_graph/pipelines/forward/forward.frag index 05e2ecd7c6..73bda68c6c 100644 --- a/src/render/render_graph/pipelines/forward/forward.frag +++ b/src/render/render_graph/pipelines/forward/forward.frag @@ -23,12 +23,21 @@ layout(set = 0, binding = 1) uniform Lights { Light SceneLights[MAX_LIGHTS]; }; +# ifdef STANDARD_MATERIAL_albedo_texture +layout(set = 1, binding = 1) uniform texture2D StandardMaterial_albedo_texture; +layout(set = 1, binding = 2) uniform sampler StandardMaterial_albedo_sampler; +# else layout(set = 1, binding = 1) uniform StandardMaterial_albedo { vec4 Albedo; }; - +# endif void main() { +# ifdef STANDARD_MATERIAL_albedo_texture + vec4 Albedo = texture( + sampler2D(StandardMaterial_albedo_texture, StandardMaterial_albedo_sampler), + v_Uv); +# endif vec3 normal = normalize(v_Normal); vec3 ambient = vec3(0.05, 0.05, 0.05); // accumulate color