From fff521307e683da6217c7489cd6a82139316492f Mon Sep 17 00:00:00 2001 From: Till Date: Mon, 19 Aug 2019 22:26:41 +0200 Subject: [PATCH] whatever --- shaders/triangle.frag | 6 +++--- shaders/triangle.vert | 4 ++++ src/vulkan.rs | 33 +++++++++++++++++++++++---------- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/shaders/triangle.frag b/shaders/triangle.frag index 3f17d01..f020d33 100644 --- a/shaders/triangle.frag +++ b/shaders/triangle.frag @@ -1,12 +1,12 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable +layout(binding = 1) uniform sampler2D tex; + layout(location = 0) in vec2 tex_coords; layout(location = 0) out vec4 out_color; -//layout(binding = 1) uniform sampler2D tex; - void main() { - out_color = vec4(1.0, 0.0, 0.0, 1.0);// texture(tex, tex_coords); + out_color = texture(tex, tex_coords); } \ No newline at end of file diff --git a/shaders/triangle.vert b/shaders/triangle.vert index 5ebdd6c..9517f3a 100644 --- a/shaders/triangle.vert +++ b/shaders/triangle.vert @@ -16,6 +16,10 @@ layout(push_constant) uniform PushConstants { mat4 model; } push; +out gl_PerVertex { + vec4 gl_Position; +}; + void main() { gl_Position = ubo.projection * ubo.view * push.model * vec4(position, 1.0); tex_coords = uv; diff --git a/src/vulkan.rs b/src/vulkan.rs index f92b05b..4d0a40f 100644 --- a/src/vulkan.rs +++ b/src/vulkan.rs @@ -75,8 +75,9 @@ pub struct GameObject { pub(crate) type GameObjectHandle = usize; pub(crate) type MeshHandle = usize; -type FixedGraphicsDescriptorSet = Arc, ((), PersistentDescriptorSetBuf>>)>>; -//type FixedGraphicsDescriptorSet = Arc, ((((), PersistentDescriptorSetBuf>>), PersistentDescriptorSetImg>>), PersistentDescriptorSetSampler)>>; +//type FixedGraphicsDescriptorSet = Arc, ((), PersistentDescriptorSetBuf>>)>>; +//type FixedGraphicsDescriptorSet = Arc, (((), PersistentDescriptorSetBuf>>), PersistentDescriptorSetImg>>)>>; +type FixedGraphicsDescriptorSet = Arc, ((((), PersistentDescriptorSetBuf>>), PersistentDescriptorSetImg>>), PersistentDescriptorSetSampler)>>; pub struct GameData { pub start_time: SystemTime, @@ -268,20 +269,22 @@ impl VulkanRenderer { let line_pipeline: Arc = create_pipeline::(device.clone(), render_pass.clone(), "shaders/line.vert", "shaders/line.frag", true).unwrap(); - let (default_tex, default_tex_future) = { + let default_tex = { let image = image::load_from_memory_with_format(include_bytes!("../models/missing-texture.jpg"), ImageFormat::JPEG).unwrap().to_rgba(); let image_data = image.into_raw().clone(); - ImmutableImage::from_iter( + let (image_view, future) = ImmutableImage::from_iter( image_data.iter().cloned(), Dimensions::Dim2d { width: 128, height: 128 }, Format::R8G8B8A8Unorm, queue.clone(), - ).unwrap() - }; + ).unwrap(); - default_tex_future.flush().unwrap(); + future.flush().unwrap(); + + image_view + }; // Dynamic viewports allow us to recreate just the viewport when the window is resized // Otherwise we would have to recreate the whole pipeline. @@ -310,7 +313,7 @@ impl VulkanRenderer { descriptor_set_pool .lock().unwrap().next() .add_buffer(uniform_buffer.clone()).unwrap() -// .add_sampled_image(default_tex.clone(), sampler.clone()).unwrap() + .add_sampled_image(default_tex.clone(), sampler.clone()).unwrap() .build().unwrap()) }) .collect(); @@ -419,13 +422,23 @@ impl VulkanRenderer { let command_buffer = self.create_command_buffer(fb_index, new_ubo).clone(); - let future = self.previous_frame_end.take().unwrap().join(acquire_future) + let future = self.previous_frame_end.take().unwrap() + .join(acquire_future) .then_execute(self.queue.clone(), command_buffer).unwrap() .then_swapchain_present(self.queue.clone(), self.swapchain.clone(), fb_index) .then_signal_fence_and_flush(); match future { Ok(future) => { + // we're joining on the previous future but the CPU is running faster than the GPU so + // eventually it stutters, and jumps ahead to the newer frames. + // + // See vulkano issue 1135: https://github.com/vulkano-rs/vulkano/issues/1135 + // This makes sure the CPU stays in sync with the GPU in situations when the CPU is + // running "too fast" + #[cfg(target_os = "macos")] + future.wait(None).unwrap(); + self.previous_frame_end = Some(Box::new(future) as Box<_>); }, Err(FlushError::OutOfDate) => { @@ -585,8 +598,8 @@ fn create_pipeline(device: Arc, re .vertex_shader(vertex_shader_entry.clone(), ()) .triangle_list() .viewports_dynamic_scissors_irrelevant(1) - .fragment_shader(fragment_shader_entry.clone(), ()) .depth_stencil_simple_depth() + .fragment_shader(fragment_shader_entry.clone(), ()) .blend_alpha_blending() .cull_mode_back() .render_pass(sub_pass.clone())