From 94f50f18bd25971ea123adb8b5782ad65a8f085c Mon Sep 17 00:00:00 2001 From: marc0246 <40955683+marc0246@users.noreply.github.com> Date: Tue, 16 Jan 2024 19:30:14 +0100 Subject: [PATCH] Use hard-coded vertices in eligible examples (#2454) * Use hard-coded vertices in eligible examples * Remove outdated todos --- .../deferred/frame/ambient_lighting_system.rs | 2 - .../frame/directional_lighting_system.rs | 2 - .../deferred/frame/point_lighting_system.rs | 2 - examples/interactive-fractal/app.rs | 1 - .../pixels_draw_pipeline.rs | 110 ++++-------------- .../interactive-fractal/place_over_frame.rs | 3 - .../multi-window-game-of-life/pixels_draw.rs | 110 ++++-------------- 7 files changed, 46 insertions(+), 184 deletions(-) diff --git a/examples/deferred/frame/ambient_lighting_system.rs b/examples/deferred/frame/ambient_lighting_system.rs index 83866fe9..4973dce4 100644 --- a/examples/deferred/frame/ambient_lighting_system.rs +++ b/examples/deferred/frame/ambient_lighting_system.rs @@ -51,8 +51,6 @@ impl AmbientLightingSystem { command_buffer_allocator: Arc, descriptor_set_allocator: Arc, ) -> AmbientLightingSystem { - // TODO: vulkano doesn't allow us to draw without a vertex buffer, otherwise we could - // hard-code these values in the shader let vertices = [ LightingVertex { position: [-1.0, -1.0], diff --git a/examples/deferred/frame/directional_lighting_system.rs b/examples/deferred/frame/directional_lighting_system.rs index 9a248d67..a93f9147 100644 --- a/examples/deferred/frame/directional_lighting_system.rs +++ b/examples/deferred/frame/directional_lighting_system.rs @@ -52,8 +52,6 @@ impl DirectionalLightingSystem { command_buffer_allocator: Arc, descriptor_set_allocator: Arc, ) -> DirectionalLightingSystem { - // TODO: vulkano doesn't allow us to draw without a vertex buffer, otherwise we could - // hard-code these values in the shader let vertices = [ LightingVertex { position: [-1.0, -1.0], diff --git a/examples/deferred/frame/point_lighting_system.rs b/examples/deferred/frame/point_lighting_system.rs index aacf74fb..7fac88ad 100644 --- a/examples/deferred/frame/point_lighting_system.rs +++ b/examples/deferred/frame/point_lighting_system.rs @@ -51,8 +51,6 @@ impl PointLightingSystem { command_buffer_allocator: Arc, descriptor_set_allocator: Arc, ) -> PointLightingSystem { - // TODO: vulkano doesn't allow us to draw without a vertex buffer, otherwise we could - // hard-code these values in the shader let vertices = [ LightingVertex { position: [-1.0, -1.0], diff --git a/examples/interactive-fractal/app.rs b/examples/interactive-fractal/app.rs index 3a04d5c6..9c546807 100644 --- a/examples/interactive-fractal/app.rs +++ b/examples/interactive-fractal/app.rs @@ -79,7 +79,6 @@ impl FractalApp { ), place_over_frame: RenderPassPlaceOverFrame::new( gfx_queue, - memory_allocator.clone(), command_buffer_allocator, descriptor_set_allocator, image_format, diff --git a/examples/interactive-fractal/pixels_draw_pipeline.rs b/examples/interactive-fractal/pixels_draw_pipeline.rs index 77b11439..452f9599 100644 --- a/examples/interactive-fractal/pixels_draw_pipeline.rs +++ b/examples/interactive-fractal/pixels_draw_pipeline.rs @@ -1,6 +1,5 @@ use std::sync::Arc; use vulkano::{ - buffer::{Buffer, BufferContents, BufferCreateInfo, BufferUsage, Subbuffer}, command_buffer::{ allocator::StandardCommandBufferAllocator, CommandBuffer, CommandBufferBeginInfo, CommandBufferInheritanceInfo, CommandBufferLevel, CommandBufferUsage, @@ -14,14 +13,13 @@ use vulkano::{ sampler::{Filter, Sampler, SamplerAddressMode, SamplerCreateInfo, SamplerMipmapMode}, view::ImageView, }, - memory::allocator::{AllocationCreateInfo, MemoryTypeFilter, StandardMemoryAllocator}, pipeline::{ graphics::{ color_blend::{ColorBlendAttachmentState, ColorBlendState}, input_assembly::InputAssemblyState, multisample::MultisampleState, rasterization::RasterizationState, - vertex_input::{Vertex, VertexDefinition}, + vertex_input::VertexInputState, viewport::{Viewport, ViewportState}, GraphicsPipelineCreateInfo, }, @@ -32,40 +30,6 @@ use vulkano::{ render_pass::Subpass, }; -/// Vertex for textured quads. -#[derive(BufferContents, Vertex)] -#[repr(C)] -pub struct TexturedVertex { - #[format(R32G32_SFLOAT)] - pub position: [f32; 2], - #[format(R32G32_SFLOAT)] - pub tex_coords: [f32; 2], -} - -pub fn textured_quad(width: f32, height: f32) -> (Vec, Vec) { - ( - vec![ - TexturedVertex { - position: [-(width / 2.0), -(height / 2.0)], - tex_coords: [0.0, 1.0], - }, - TexturedVertex { - position: [-(width / 2.0), height / 2.0], - tex_coords: [0.0, 0.0], - }, - TexturedVertex { - position: [width / 2.0, height / 2.0], - tex_coords: [1.0, 0.0], - }, - TexturedVertex { - position: [width / 2.0, -(height / 2.0)], - tex_coords: [1.0, 1.0], - }, - ], - vec![0, 2, 1, 0, 3, 2], - ) -} - /// A subpass pipeline that fills a quad over frame. pub struct PixelsDrawPipeline { gfx_queue: Arc, @@ -73,48 +37,15 @@ pub struct PixelsDrawPipeline { pipeline: Arc, command_buffer_allocator: Arc, descriptor_set_allocator: Arc, - vertices: Subbuffer<[TexturedVertex]>, - indices: Subbuffer<[u32]>, } impl PixelsDrawPipeline { pub fn new( gfx_queue: Arc, subpass: Subpass, - memory_allocator: Arc, command_buffer_allocator: Arc, descriptor_set_allocator: Arc, ) -> PixelsDrawPipeline { - let (vertices, indices) = textured_quad(2.0, 2.0); - let vertex_buffer = Buffer::from_iter( - memory_allocator.clone(), - BufferCreateInfo { - usage: BufferUsage::VERTEX_BUFFER, - ..Default::default() - }, - AllocationCreateInfo { - memory_type_filter: MemoryTypeFilter::PREFER_DEVICE - | MemoryTypeFilter::HOST_SEQUENTIAL_WRITE, - ..Default::default() - }, - vertices, - ) - .unwrap(); - let index_buffer = Buffer::from_iter( - memory_allocator, - BufferCreateInfo { - usage: BufferUsage::INDEX_BUFFER, - ..Default::default() - }, - AllocationCreateInfo { - memory_type_filter: MemoryTypeFilter::PREFER_DEVICE - | MemoryTypeFilter::HOST_SEQUENTIAL_WRITE, - ..Default::default() - }, - indices, - ) - .unwrap(); - let pipeline = { let device = gfx_queue.device(); let vs = vs::load(device.clone()) @@ -125,9 +56,6 @@ impl PixelsDrawPipeline { .expect("failed to create shader module") .entry_point("main") .expect("shader entry point not found"); - let vertex_input_state = TexturedVertex::per_vertex() - .definition(&vs.info().input_interface) - .unwrap(); let stages = [ PipelineShaderStageCreateInfo::new(vs), PipelineShaderStageCreateInfo::new(fs), @@ -145,7 +73,7 @@ impl PixelsDrawPipeline { None, GraphicsPipelineCreateInfo { stages: stages.into_iter().collect(), - vertex_input_state: Some(vertex_input_state), + vertex_input_state: Some(VertexInputState::default()), input_assembly_state: Some(InputAssemblyState::default()), viewport_state: Some(ViewportState::default()), rasterization_state: Some(RasterizationState::default()), @@ -168,8 +96,6 @@ impl PixelsDrawPipeline { pipeline, command_buffer_allocator, descriptor_set_allocator, - vertices: vertex_buffer, - indices: index_buffer, } } @@ -236,16 +162,10 @@ impl PixelsDrawPipeline { 0, self.create_descriptor_set(image), ) - .unwrap() - .bind_vertex_buffers(0, self.vertices.clone()) - .unwrap() - .bind_index_buffer(self.indices.clone()) .unwrap(); unsafe { - builder - .draw_indexed(self.indices.len() as u32, 1, 0, 0, 0) - .unwrap(); + builder.draw(6, 1, 0, 0).unwrap(); } builder.end().unwrap() @@ -257,14 +177,30 @@ mod vs { ty: "vertex", src: r" #version 450 - layout(location=0) in vec2 position; - layout(location=1) in vec2 tex_coords; + + const vec2[6] POSITIONS = { + vec2(-1.0, -1.0), + vec2( 1.0, 1.0), + vec2(-1.0, 1.0), + vec2(-1.0, -1.0), + vec2( 1.0, -1.0), + vec2( 1.0, 1.0), + }; + + const vec2[6] TEX_COORDS = { + vec2(0.0, 1.0), + vec2(1.0, 0.0), + vec2(0.0, 0.0), + vec2(0.0, 1.0), + vec2(1.0, 1.0), + vec2(1.0, 0.0), + }; layout(location = 0) out vec2 f_tex_coords; void main() { - gl_Position = vec4(position, 0.0, 1.0); - f_tex_coords = tex_coords; + gl_Position = vec4(POSITIONS[gl_VertexIndex], 0.0, 1.0); + f_tex_coords = TEX_COORDS[gl_VertexIndex]; } ", } diff --git a/examples/interactive-fractal/place_over_frame.rs b/examples/interactive-fractal/place_over_frame.rs index 6633036b..798a88a8 100644 --- a/examples/interactive-fractal/place_over_frame.rs +++ b/examples/interactive-fractal/place_over_frame.rs @@ -10,7 +10,6 @@ use vulkano::{ device::Queue, format::Format, image::view::ImageView, - memory::allocator::StandardMemoryAllocator, render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass}, sync::GpuFuture, }; @@ -26,7 +25,6 @@ pub struct RenderPassPlaceOverFrame { impl RenderPassPlaceOverFrame { pub fn new( gfx_queue: Arc, - memory_allocator: Arc, command_buffer_allocator: Arc, descriptor_set_allocator: Arc, output_format: Format, @@ -51,7 +49,6 @@ impl RenderPassPlaceOverFrame { let pixels_draw_pipeline = PixelsDrawPipeline::new( gfx_queue.clone(), subpass, - memory_allocator, command_buffer_allocator.clone(), descriptor_set_allocator, ); diff --git a/examples/multi-window-game-of-life/pixels_draw.rs b/examples/multi-window-game-of-life/pixels_draw.rs index cbd655c9..7186d17c 100644 --- a/examples/multi-window-game-of-life/pixels_draw.rs +++ b/examples/multi-window-game-of-life/pixels_draw.rs @@ -1,7 +1,6 @@ use crate::app::App; use std::sync::Arc; use vulkano::{ - buffer::{Buffer, BufferContents, BufferCreateInfo, BufferUsage, Subbuffer}, command_buffer::{ allocator::StandardCommandBufferAllocator, CommandBuffer, CommandBufferBeginInfo, CommandBufferInheritanceInfo, CommandBufferLevel, CommandBufferUsage, @@ -15,14 +14,13 @@ use vulkano::{ sampler::{Filter, Sampler, SamplerAddressMode, SamplerCreateInfo, SamplerMipmapMode}, view::ImageView, }, - memory::allocator::{AllocationCreateInfo, MemoryTypeFilter}, pipeline::{ graphics::{ color_blend::{ColorBlendAttachmentState, ColorBlendState}, input_assembly::InputAssemblyState, multisample::MultisampleState, rasterization::RasterizationState, - vertex_input::{Vertex, VertexDefinition}, + vertex_input::VertexInputState, viewport::{Viewport, ViewportState}, GraphicsPipelineCreateInfo, }, @@ -33,40 +31,6 @@ use vulkano::{ render_pass::Subpass, }; -/// Vertex for textured quads. -#[derive(BufferContents, Vertex)] -#[repr(C)] -pub struct TexturedVertex { - #[format(R32G32_SFLOAT)] - pub position: [f32; 2], - #[format(R32G32_SFLOAT)] - pub tex_coords: [f32; 2], -} - -pub fn textured_quad(width: f32, height: f32) -> (Vec, Vec) { - ( - vec![ - TexturedVertex { - position: [-(width / 2.0), -(height / 2.0)], - tex_coords: [0.0, 1.0], - }, - TexturedVertex { - position: [-(width / 2.0), height / 2.0], - tex_coords: [0.0, 0.0], - }, - TexturedVertex { - position: [width / 2.0, height / 2.0], - tex_coords: [1.0, 0.0], - }, - TexturedVertex { - position: [width / 2.0, -(height / 2.0)], - tex_coords: [1.0, 1.0], - }, - ], - vec![0, 2, 1, 0, 3, 2], - ) -} - /// A subpass pipeline that fills a quad over the frame. pub struct PixelsDrawPipeline { gfx_queue: Arc, @@ -74,43 +38,10 @@ pub struct PixelsDrawPipeline { pipeline: Arc, command_buffer_allocator: Arc, descriptor_set_allocator: Arc, - vertices: Subbuffer<[TexturedVertex]>, - indices: Subbuffer<[u32]>, } impl PixelsDrawPipeline { pub fn new(app: &App, gfx_queue: Arc, subpass: Subpass) -> PixelsDrawPipeline { - let (vertices, indices) = textured_quad(2.0, 2.0); - let memory_allocator = app.context.memory_allocator(); - let vertex_buffer = Buffer::from_iter( - memory_allocator.clone(), - BufferCreateInfo { - usage: BufferUsage::VERTEX_BUFFER, - ..Default::default() - }, - AllocationCreateInfo { - memory_type_filter: MemoryTypeFilter::PREFER_DEVICE - | MemoryTypeFilter::HOST_SEQUENTIAL_WRITE, - ..Default::default() - }, - vertices, - ) - .unwrap(); - let index_buffer = Buffer::from_iter( - memory_allocator.clone(), - BufferCreateInfo { - usage: BufferUsage::INDEX_BUFFER, - ..Default::default() - }, - AllocationCreateInfo { - memory_type_filter: MemoryTypeFilter::PREFER_DEVICE - | MemoryTypeFilter::HOST_SEQUENTIAL_WRITE, - ..Default::default() - }, - indices, - ) - .unwrap(); - let pipeline = { let device = gfx_queue.device(); let vs = vs::load(device.clone()) @@ -121,9 +52,6 @@ impl PixelsDrawPipeline { .expect("failed to create shader module") .entry_point("main") .expect("shader entry point not found"); - let vertex_input_state = TexturedVertex::per_vertex() - .definition(&vs.info().input_interface) - .unwrap(); let stages = [ PipelineShaderStageCreateInfo::new(vs), PipelineShaderStageCreateInfo::new(fs), @@ -141,7 +69,7 @@ impl PixelsDrawPipeline { None, GraphicsPipelineCreateInfo { stages: stages.into_iter().collect(), - vertex_input_state: Some(vertex_input_state), + vertex_input_state: Some(VertexInputState::default()), input_assembly_state: Some(InputAssemblyState::default()), viewport_state: Some(ViewportState::default()), rasterization_state: Some(RasterizationState::default()), @@ -164,8 +92,6 @@ impl PixelsDrawPipeline { pipeline, command_buffer_allocator: app.command_buffer_allocator.clone(), descriptor_set_allocator: app.descriptor_set_allocator.clone(), - vertices: vertex_buffer, - indices: index_buffer, } } @@ -232,16 +158,10 @@ impl PixelsDrawPipeline { 0, self.create_image_sampler_nearest(image), ) - .unwrap() - .bind_vertex_buffers(0, self.vertices.clone()) - .unwrap() - .bind_index_buffer(self.indices.clone()) .unwrap(); unsafe { - builder - .draw_indexed(self.indices.len() as u32, 1, 0, 0, 0) - .unwrap(); + builder.draw(6, 1, 0, 0).unwrap(); } builder.end().unwrap() @@ -253,14 +173,30 @@ mod vs { ty: "vertex", src: r" #version 450 - layout(location=0) in vec2 position; - layout(location=1) in vec2 tex_coords; + + const vec2[6] POSITIONS = { + vec2(-1.0, -1.0), + vec2( 1.0, 1.0), + vec2(-1.0, 1.0), + vec2(-1.0, -1.0), + vec2( 1.0, -1.0), + vec2( 1.0, 1.0), + }; + + const vec2[6] TEX_COORDS = { + vec2(0.0, 1.0), + vec2(1.0, 0.0), + vec2(0.0, 0.0), + vec2(0.0, 1.0), + vec2(1.0, 1.0), + vec2(1.0, 0.0), + }; layout(location = 0) out vec2 f_tex_coords; void main() { - gl_Position = vec4(position, 0.0, 1.0); - f_tex_coords = tex_coords; + gl_Position = vec4(POSITIONS[gl_VertexIndex], 0.0, 1.0); + f_tex_coords = TEX_COORDS[gl_VertexIndex]; } ", }