diff --git a/src/cube.mesh.glsl b/src/cube.mesh.glsl deleted file mode 100644 index 82b03306e1b114740dc9815882e56feed11d3712..0000000000000000000000000000000000000000 --- a/src/cube.mesh.glsl +++ /dev/null @@ -1,51 +0,0 @@ -/* Copyright (c) 2021, Sascha Willems -* -* SPDX-License-Identifier: MIT -* -*/ - -#version 450 -#extension GL_EXT_mesh_shader:require - -layout(push_constant)uniform PushConstantData{ - mat4 world; - mat4 view; - mat4 proj; -}pc; - -layout(local_size_x=1,local_size_y=1,local_size_z=1)in; -layout(triangles,max_vertices=3,max_primitives=1)out; - -layout(location=0)out VertexOutput -{ - vec4 color; -}vertexOutput[]; - -const vec4[3]positions={ - vec4(0.,-1.,0.,1.), - vec4(-1.,1.,0.,1.), - vec4(1.,1.,0.,1.) -}; - -const vec4[3]colors={ - vec4(0.,1.,0.,1.), - vec4(0.,0.,1.,1.), - vec4(1.,0.,0.,1.) -}; - -void main() -{ - uint iid=gl_LocalInvocationID.x; - - vec4 offset=vec4(0.,0.,gl_GlobalInvocationID.x,0.); - - SetMeshOutputsEXT(3,1); - mat4 mvp=pc.proj*pc.view*pc.world; - gl_MeshVerticesEXT[0].gl_Position=mvp*(positions[0]+offset); - gl_MeshVerticesEXT[1].gl_Position=mvp*(positions[1]+offset); - gl_MeshVerticesEXT[2].gl_Position=mvp*(positions[2]+offset); - vertexOutput[0].color=colors[0]; - vertexOutput[1].color=colors[1]; - vertexOutput[2].color=colors[2]; - gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex]=uvec3(0,1,2); -} \ No newline at end of file diff --git a/src/frag.glsl b/src/frag.glsl deleted file mode 100644 index e3685cd4620e76f9dcd48a6db3aebe29adaf18a0..0000000000000000000000000000000000000000 --- a/src/frag.glsl +++ /dev/null @@ -1,21 +0,0 @@ -#version 450 - -layout(location=0)in vec3 normal; -layout(location=0)out vec4 f_color; - -layout(set=0,binding=0)uniform Data{ - vec4[32]pos; - vec4[32]col; - uint light_count; -}uniforms; - -void main(){ - vec3 accum=vec3(0.,0.,0.); - - for(int i=0;i<uniforms.light_count;i++) - { - accum+=uniforms.col[i].xyz*((dot(normalize(normal),uniforms.pos[i].xyz)*.5)+.5); - } - - f_color=vec4(accum,1.); -} \ No newline at end of file diff --git a/src/gui.rs b/src/gui.rs index 841d0fa3976ffffb135f1fee95a4757b10cc61d6..c6556e3e4b07facbdd1d5ff28c2bf395f915e0dc 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -1,6 +1,6 @@ use egui::{ plot::{Line, Plot, PlotPoints}, - Color32, Frame, Id, ScrollArea, TextEdit, TextStyle, + Color32, Frame, Id, }; use egui_winit_vulkano::Gui; @@ -10,14 +10,6 @@ fn sized_text(ui: &mut egui::Ui, text: impl Into<String>, size: f32) { ui.label(egui::RichText::new(text).size(size)); } -const CODE: &str = r#" -# Some markup -``` -let mut gui = Gui::new(&event_loop, renderer.surface(), renderer.queue()); -``` -Vulkan(o) is hard, that I know... -"#; - #[derive(Debug)] pub struct GState { pub cursor_sensitivity: f32, @@ -44,7 +36,6 @@ impl Default for GState { } pub fn gui_up(gui: &mut Gui, state: &mut GState) { - let mut code = CODE.to_owned(); gui.immediate_ui(|gui| { let ctx = gui.context(); egui::SidePanel::left(Id::new("main_left")) diff --git a/src/implicit.frag.glsl b/src/implicit.frag.glsl new file mode 100644 index 0000000000000000000000000000000000000000..2b014e0f566bfc056049de46db1eb6fa04fd2d2c --- /dev/null +++ b/src/implicit.frag.glsl @@ -0,0 +1,86 @@ +// Implicit Fragment shader + +#version 450 + +layout(push_constant)uniform PushConstantData{ + mat4 world; +}pc; + +layout(set=0,binding=0)uniform Lights{ + vec4[32]pos; + vec4[32]col; + uint light_count; +}light_uniforms; + +layout(set=0,binding=1)uniform Camera{ + mat4 view; + mat4 proj; + vec3 campos; +}camera_uniforms; + +layout(constant_id=0)const uint RES_X=1920; +layout(constant_id=1)const uint RES_Y=1080; + +layout(location=0)in VertexInput +{ + vec4 position; +}vertexInput; + +layout(location=0)out vec4 f_color; + +const float EPSILON=.0001; +const uint MAX_STEPS=50; + +float scene(vec3 p) +{ + return length(p-vec3(5.))-5.; +} + +vec3 getNormal(vec3 p,float dens){ + vec3 n; + n.x=scene(vec3(p.x+EPSILON,p.y,p.z)); + n.y=scene(vec3(p.x,p.y+EPSILON,p.z)); + n.z=scene(vec3(p.x,p.y,p.z+EPSILON)); + return normalize(n-scene(p)); +} + +vec2 spheretracing(vec3 ori,vec3 dir,out vec3 p){ + vec2 td=vec2(0.); + for(int i=0;i<MAX_STEPS;i++){ + p=ori+dir*td.x; + td.y=scene(p); + if(td.y<EPSILON)break; + td.x+=(td.y)*.9; + } + return td; +} +#define frac_pi_2 1.57079632679489661923132169163975144 +void main(){ + + vec3 raypos=vertexInput.position.xyz; + vec2 iResolution=vec2(RES_X,RES_Y); + vec2 iuv=gl_FragCoord.xy/iResolution.xy*2.-1.; + vec2 uv=iuv; + uv.x*=iResolution.x/iResolution.y; + vec3 p; + vec3 raydir=normalize(raypos-camera_uniforms.campos); + //raydir=(camera_uniforms.view*vec4(raydir,1.)).xyz; + vec2 td=spheretracing(raypos,raydir,p); + vec3 n=getNormal(p,td.y); + if(td.y<EPSILON) + { + vec3 accum=vec3(0.,0.,0.); + + for(int i=0;i<light_uniforms.light_count;i++) + { + accum+=light_uniforms.col[i].xyz*((dot(normalize(n),light_uniforms.pos[i].xyz)*.5)+.5); + } + + f_color=vec4(accum,1.); + } + else + { + //f_color=vec4(raydir,0.); + discard; + } +} \ No newline at end of file diff --git a/src/implicit.mesh.glsl b/src/implicit.mesh.glsl new file mode 100644 index 0000000000000000000000000000000000000000..f7592344dba7a62e9ba9bcad7cad6f6e2016f666 --- /dev/null +++ b/src/implicit.mesh.glsl @@ -0,0 +1,94 @@ +// Implicit Mesh shader + +#version 450 +#extension GL_EXT_mesh_shader:require + +layout(push_constant)uniform PushConstantData{ + mat4 world; +}pc; + +layout(set=0,binding=0)uniform Lights{ + vec4[32]pos; + vec4[32]col; + uint light_count; +}light_uniforms; + +layout(set=0,binding=1)uniform Camera{ + mat4 view; + mat4 proj; + vec3 campos; +}camera_uniforms; + +layout(local_size_x=1,local_size_y=1,local_size_z=1)in; +layout(triangles,max_vertices=64,max_primitives=162)out; + +layout(location=0)out VertexOutput +{ + vec4 color; +}vertexOutput[]; + +const vec4[8]positions={ + vec4(0.,0.,0.,1.), + vec4(0.,0.,1.,1.), + vec4(0.,1.,0.,1.), + vec4(0.,1.,1.,1.), + vec4(1.,0.,0.,1.), + vec4(1.,0.,1.,1.), + vec4(1.,1.,0.,1.), + vec4(1.,1.,1.,1.), +}; +const mat4 scale=mat4( + 10.,0.,0.,0., + 0.,10.,0.,0., + 0.,0.,10.,0., + 0.,0.,0.,1. +); + +void main() +{ + uint iid=gl_LocalInvocationID.x; + + vec4 offset=vec4(0.,0.,gl_GlobalInvocationID.x,0.); + + SetMeshOutputsEXT(8,12); + mat4 mvp=camera_uniforms.proj*camera_uniforms.view*scale; + gl_MeshVerticesEXT[0].gl_Position=mvp*(positions[0]+offset); + gl_MeshVerticesEXT[1].gl_Position=mvp*(positions[1]+offset); + gl_MeshVerticesEXT[2].gl_Position=mvp*(positions[2]+offset); + gl_MeshVerticesEXT[3].gl_Position=mvp*(positions[3]+offset); + gl_MeshVerticesEXT[4].gl_Position=mvp*(positions[4]+offset); + gl_MeshVerticesEXT[5].gl_Position=mvp*(positions[5]+offset); + gl_MeshVerticesEXT[6].gl_Position=mvp*(positions[6]+offset); + gl_MeshVerticesEXT[7].gl_Position=mvp*(positions[7]+offset); + vertexOutput[0].color=scale*positions[0]; + vertexOutput[1].color=scale*positions[1]; + vertexOutput[2].color=scale*positions[2]; + vertexOutput[3].color=scale*positions[3]; + vertexOutput[4].color=scale*positions[4]; + vertexOutput[5].color=scale*positions[5]; + vertexOutput[6].color=scale*positions[6]; + vertexOutput[7].color=scale*positions[7]; + gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+0]=uvec3(0,1,2); + gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+1]=uvec3(1,2,3); + gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+2]=uvec3(4,5,6); + gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+3]=uvec3(5,6,7); + gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+4]=uvec3(0,2,4); + gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+5]=uvec3(2,4,6); + gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+6]=uvec3(1,3,5); + gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+7]=uvec3(3,5,7); + gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+8]=uvec3(2,3,6); + gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+9]=uvec3(3,6,7); + gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+10]=uvec3(0,1,4); + gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+11]=uvec3(1,4,5); +} +/* +0 1 2 3 0 +4 5 6 7 4 +0 1 2 +1 2 3 +4 5 6 +5 6 7 +0 1 4 +1 4 5 +1 2 +*/ \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 116d682e094829f524f8f0b67d657131a78f18e6..cf43fd21754e44f99536d1381cbb3494397171df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,55 +1,24 @@ -// Copyright (c) 2016 The vulkano developers -// Licensed under the Apache License, Version 2.0 -// <LICENSE-APACHE or -// https://www.apache.org/licenses/LICENSE-2.0> or the MIT -// license <LICENSE-MIT or https://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. - -// Welcome to the triangle example! -// -// This is the only example that is entirely detailed. All the other examples avoid code -// duplication by using helper functions. -// -// This example assumes that you are already more or less familiar with graphics programming -// and that you want to learn Vulkan. This means that for example it won't go into details about -// what a vertex or a shader is. -use bytemuck::{Pod, Zeroable}; -use cgmath::{ - AbsDiffEq, Basis3, Deg, EuclideanSpace, Euler, Matrix3, Matrix4, Point3, Quaternion, Rad, - SquareMatrix, Transform, Vector3, -}; -use obj::{LoadConfig, ObjData}; -use rodio::{source::Source, Decoder, OutputStream}; +use cgmath::{Deg, EuclideanSpace, Euler, Matrix3, Matrix4, Point3, Rad, SquareMatrix, Vector3}; use std::io::Cursor; use std::{sync::Arc, time::Instant}; use vulkano::buffer::allocator::{SubbufferAllocator, SubbufferAllocatorCreateInfo}; -use vulkano::buffer::sys::BufferCreateInfo; -use vulkano::buffer::{Buffer, BufferAllocateInfo}; -use vulkano::command_buffer::allocator::{ - CommandBufferAllocator, CommandBufferBuilderAlloc, StandardCommandBufferAllocator, -}; -use vulkano::command_buffer::synced::SyncCommandBufferBuilder; -use vulkano::command_buffer::sys::{CommandBufferBeginInfo, UnsafeCommandBufferBuilder}; -use vulkano::command_buffer::CommandBufferInheritanceInfo; +use vulkano::command_buffer::allocator::StandardCommandBufferAllocator; use vulkano::descriptor_set::allocator::StandardDescriptorSetAllocator; use vulkano::descriptor_set::{PersistentDescriptorSet, WriteDescriptorSet}; -use vulkano::device::{DeviceOwned, QueueFlags}; +use vulkano::device::{DeviceOwned, Features, QueueFlags}; use vulkano::format::Format; use vulkano::image::AttachmentImage; -use vulkano::memory::allocator::{MemoryUsage, StandardMemoryAllocator}; +use vulkano::memory::allocator::StandardMemoryAllocator; use vulkano::pipeline::graphics::depth_stencil::DepthStencilState; use vulkano::pipeline::graphics::rasterization::CullMode; use vulkano::pipeline::graphics::rasterization::FrontFace::Clockwise; use vulkano::pipeline::graphics::vertex_input::Vertex; use vulkano::pipeline::PipelineBindPoint; -use vulkano::shader::ShaderModule; +use vulkano::shader::{ShaderModule, SpecializationConstants}; use vulkano::swapchain::{PresentMode, SwapchainPresentInfo}; -use vulkano::{NonExhaustive, VulkanLibrary, VulkanObject}; -use winit::event::{DeviceEvent, DeviceId, ElementState, MouseButton, VirtualKeyCode}; +use vulkano::VulkanLibrary; +use winit::event::{DeviceEvent, ElementState, MouseButton, VirtualKeyCode}; -use ash::vk::CommandBuffer; use egui_winit_vulkano::Gui; use vulkano::pipeline::StateMode::Fixed; use vulkano::{ @@ -61,13 +30,11 @@ use vulkano::{ physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo, }, image::{view::ImageView, ImageAccess, ImageUsage, SwapchainImage}, - impl_vertex, - instance::{Instance, InstanceCreateInfo, IntanceExtensions}, + instance::{Instance, InstanceCreateInfo, InstanceExtensions}, pipeline::{ graphics::{ input_assembly::InputAssemblyState, rasterization::RasterizationState, - vertex_input::BuffersDefinition, viewport::{Viewport, ViewportState}, }, GraphicsPipeline, Pipeline, @@ -93,23 +60,14 @@ use crate::objects::*; pub type MemoryAllocator = StandardMemoryAllocator; fn main() { - // The first step of any Vulkan program is to create an instance. - // - // When we create an instance, we have to pass a list of extensions that we want to enable. - // - // All the window-drawing functionalities are part of non-core extensions that we need - // to enable manually. To do so, we ask the `vulkano_win` crate for the list of extensions - // required to draw to a window. let library = VulkanLibrary::new().expect("Vulkan is not installed???"); let required_extensions = vulkano_win::required_extensions(&library); - // Now creating the instance. let instance = Instance::new( library, InstanceCreateInfo { - enabled_extensions: IntanceExtensions { - khr_get_physical_device_properties2, - ..required_extensions, + enabled_extensions: InstanceExtensions { + ..required_extensions }, // Enable enumerating devices that use non-conformant vulkan implementations. (ex. MoltenVK) enumerate_portability: true, @@ -118,80 +76,32 @@ fn main() { ) .unwrap(); - // The objective of this example is to draw a triangle on a window. To do so, we first need to - // create the window. - // - // This is done by creating a `WindowBuilder` from the `winit` crate, then calling the - // `build_vk_surface` method provided by the `VkSurfaceBuild` trait from `vulkano_win`. If you - // ever get an error about `build_vk_surface` being undefined in one of your projects, this - // probably means that you forgot to import this trait. - // - // This returns a `vulkano::swapchain::Surface` object that contains both a cross-platform winit - // window and a cross-platform Vulkan surface that represents the surface of the window. let event_loop = EventLoop::new(); let surface = WindowBuilder::new() .with_title("horizontally spinning bunny") .build_vk_surface(&event_loop, instance.clone()) .unwrap(); - // Choose device extensions that we're going to use. - // In order to present images to a surface, we need a `Swapchain`, which is provided by the - // `khr_swapchain` extension. let device_extensions = DeviceExtensions { khr_swapchain: true, ext_mesh_shader: true, - khr_spirv_1_4: true, - khr_shader_float_controls: true, ..DeviceExtensions::empty() }; - // We then choose which physical device to use. First, we enumerate all the available physical - // devices, then apply filters to narrow them down to those that can support our needs. let (physical_device, queue_family_index) = instance .enumerate_physical_devices() .unwrap() - .filter(|p| { - // Some devices may not support the extensions or features that your application, or - // report properties and limits that are not sufficient for your application. These - // should be filtered out here. - p.supported_extensions().contains(&device_extensions) - }) + .filter(|p| p.supported_extensions().contains(&device_extensions)) .filter_map(|p| { - // For each physical device, we try to find a suitable queue family that will execute - // our draw commands. - // - // Devices can provide multiple queues to run commands in parallel (for example a draw - // queue and a compute queue), similar to CPU threads. This is something you have to - // have to manage manually in Vulkan. Queues of the same type belong to the same - // queue family. - // - // Here, we look for a single queue family that is suitable for our purposes. In a - // real-life application, you may want to use a separate dedicated transfer queue to - // handle data transfers in parallel with graphics operations. You may also need a - // separate queue for compute operations, if your application uses those. p.queue_family_properties() .iter() .enumerate() .position(|(i, q)| { - // We select a queue family that supports graphics operations. When drawing to - // a window surface, as we do in this example, we also need to check that queues - // in this queue family are capable of presenting images to the surface. q.queue_flags.intersects(QueueFlags::GRAPHICS) && p.surface_support(i as u32, &surface).unwrap_or(false) }) - // The code here searches for the first queue family that is suitable. If none is - // found, `None` is returned to `filter_map`, which disqualifies this physical - // device. .map(|i| (p, i as u32)) }) - // All the physical devices that pass the filters above are suitable for the application. - // However, not every device is equal, some are preferred over others. Now, we assign - // each physical device a score, and pick the device with the - // lowest ("best") score. - // - // In this example, we simply select the best-scoring device to use in the application. - // In a real-life setting, you may want to use the best-scoring device only as a - // "default" or "recommended" device, and let the user choose the device themselves. .min_by_key(|(p, _)| { // We assign a lower score to device types that are likely to be faster/better. match p.properties().device_type { @@ -212,48 +122,32 @@ fn main() { physical_device.properties().device_type, ); - // Now initializing the device. This is probably the most important object of Vulkan. - // - // The iterator of created queues is returned by the function alongside the device. let (device, mut queues) = Device::new( - // Which physical device to connect to. physical_device, DeviceCreateInfo { - // A list of optional features and extensions that our program needs to work correctly. - // Some parts of the Vulkan specs are optional and must be enabled manually at device - // creation. In this example the only thing we are going to need is the `khr_swapchain` - // extension that allows us to draw to a window. enabled_extensions: device_extensions, - - // The list of queues that we are going to use. Here we only use one queue, from the - // previously chosen queue family. queue_create_infos: vec![QueueCreateInfo { queue_family_index, ..Default::default() }], - + enabled_features: Features { + mesh_shader: true, + task_shader: true, + ..Features::empty() + }, ..Default::default() }, ) .expect("Unable to initialize device"); - // Since we can request multiple queues, the `queues` variable is in fact an iterator. We - // only use one queue in this example, so we just retrieve the first and only element of the - // iterator. let queue = queues.next().expect("Unable to retrieve queues"); - // Before we can draw on the surface, we have to create what is called a swapchain. Creating - // a swapchain allocates the color buffers that will contain the image that will ultimately - // be visible on the screen. These images are returned alongside the swapchain. let (mut swapchain, images) = { - // Querying the capabilities of the surface. When we create the swapchain we can only - // pass values that are allowed by the capabilities. let surface_capabilities = device .physical_device() .surface_capabilities(&surface, Default::default()) .unwrap(); - // Choosing the internal format that the images will have. let image_format = Some( device .physical_device() @@ -263,7 +157,6 @@ fn main() { ); let window = surface.object().unwrap().downcast_ref::<Window>().unwrap(); - // Please take a look at the docs for the meaning of the parameters we didn't mention. Swapchain::new( device.clone(), surface.clone(), @@ -273,26 +166,10 @@ fn main() { .min(surface_capabilities.max_image_count.unwrap_or(u32::MAX)), image_format, - // The dimensions of the window, only used to initially setup the swapchain. - // NOTE: - // On some drivers the swapchain dimensions are specified by - // `surface_capabilities.current_extent` and the swapchain size must use these - // dimensions. - // These dimensions are always the same as the window dimensions. - // - // However, other drivers don't specify a value, i.e. - // `surface_capabilities.current_extent` is `None`. These drivers will allow - // anything, but the only sensible value is the window - // dimensions. - // - // Both of these cases need the swapchain to use the window dimensions, so we just - // use that. image_extent: window.inner_size().into(), image_usage: ImageUsage::COLOR_ATTACHMENT, - // The alpha mode indicates how the alpha value of the final image will behave. For - // example, you can choose whether the window will be opaque or transparent. composite_alpha: surface_capabilities .supported_composite_alpha .into_iter() @@ -307,66 +184,31 @@ fn main() { .unwrap() }; - // The next step is to create the shaders. - // - // The raw shader creation API provided by the vulkano library is unsafe for various - // reasons, so The `shader!` macro provides a way to generate a Rust module from GLSL - // source - in the example below, the source is provided as a string input directly to - // the shader, but a path to a source file can be provided as well. Note that the user - // must specify the type of shader (e.g., "vertex," "fragment, etc.") using the `ty` - // option of the macro. - // - // The module generated by the `shader!` macro includes a `load` function which loads - // the shader using an input logical device. The module also includes type definitions - // for layout structures defined in the shader source, for example, uniforms and push - // constants. - // - // A more detailed overview of what the `shader!` macro generates can be found in the - // `vulkano-shaders` crate docs. You can view them at https://docs.rs/vulkano-shaders/ mod mesh_vs { vulkano_shaders::shader! { 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(push_constant) uniform PushConstantData { - mat4 world; - mat4 view; - mat4 proj; - } pc; - - void main() { - mat4 worldview = pc.view * pc.world; - v_normal = normal; //normalize(transpose(inverse(mat3(worldview))) * normal); - gl_Position = pc.proj * worldview * vec4(position*1000.0, 1.0); - } - ", + path: "src/triangle.vert.glsl", types_meta: { use bytemuck::{Pod, Zeroable}; #[derive(Clone, Copy, Zeroable, Pod, Debug)] }, vulkan_version: "1.2", - spirv_version: "1.4" + spirv_version: "1.6" } } mod mesh_fs { vulkano_shaders::shader! { ty: "fragment", - path: "src/frag.glsl", + path: "src/triangle.frag.glsl", types_meta: { use bytemuck::{Pod, Zeroable}; #[derive(Clone, Copy, Zeroable, Pod, Debug)] }, vulkan_version: "1.2", - spirv_version: "1.4" + spirv_version: "1.6" } } @@ -376,50 +218,43 @@ fn main() { mod implicit_ms { vulkano_shaders::shader! { ty: "mesh", - path: "src/cube.mesh.glsl", + path: "src/implicit.mesh.glsl", types_meta: { use bytemuck::{Pod, Zeroable}; #[derive(Clone, Copy, Zeroable, Pod, Debug)] }, vulkan_version: "1.2", - spirv_version: "1.4" + spirv_version: "1.6" } } - let implicit_ms = implicit_ms::load(device.clone()).unwrap(); + mod implicit_fs { + vulkano_shaders::shader! { + ty: "fragment", + path: "src/implicit.frag.glsl", + types_meta: { + use bytemuck::{Pod, Zeroable}; - /*let uniform_buffer = - CpuBufferPool::<vs::ty::PushConstantData>::uniform_buffer(memory_allocator);*/ + #[derive(Clone, Copy, Zeroable, Pod, Debug)] + }, + vulkan_version: "1.2", + spirv_version: "1.6" + } + } - let memory_allocator = Arc::new(MemoryAllocator::new_default(device.clone())); + let implicit_ms = implicit_ms::load(device.clone()).unwrap(); + let implicit_fs = implicit_fs::load(device.clone()).unwrap(); - // At this point, OpenGL initialization would be finished. However in Vulkan it is not. OpenGL - // implicitly does a lot of computation whenever you draw. In Vulkan, you have to do all this - // manually. + let memory_allocator = Arc::new(MemoryAllocator::new_default(device.clone())); - // The next step is to create a *render pass*, which is an object that describes where the - // output of the graphics pipeline will go. It describes the layout of the images - // where the colors, depth and/or stencil information will be written. let render_pass = vulkano::ordered_passes_renderpass!( device.clone(), attachments: { - // `color` is a custom name we give to the first and only attachment. color: { - // `load: Clear` means that we ask the GPU to clear the content of this - // attachment at the start of the drawing. load: Clear, - // `store: Store` means that we ask the GPU to store the output of the draw - // in the actual image. We could also ask it to discard the result. store: Store, - // `format: <ty>` indicates the type of the format of the image. This has to - // be one of the types of the `vulkano::format` module (or alternatively one - // of your structs that implements the `FormatDesc` trait). Here we use the - // same format as the swapchain. format: swapchain.image_format(), - // `samples: 1` means that we ask the GPU to use one sample to determine the value - // of each pixel in the color attachment. We could use a larger value (multisampling) - // for antialiasing. An example of this can be found in msaa-renderpass.rs. samples: 1, }, depth: { @@ -430,69 +265,41 @@ fn main() { } }, passes: [{ - // We use the attachment named `color` as the one and only color attachment. color: [color], - // No depth-stencil attachment is indicated with empty brackets. depth_stencil: {depth}, input: [] },{ - // We use the attachment named `color` as the one and only color attachment. color: [color], - // No depth-stencil attachment is indicated with empty brackets. depth_stencil: {depth}, input: [] }] ) .unwrap(); - // Dynamic viewports allow us to recreate just the viewport when the window is resized - // Otherwise we would have to recreate the whole pipeline. let mut viewport = Viewport { origin: [0.0, 0.0], dimensions: [0.0, 0.0], depth_range: 0.0..1.0, }; - // The render pass we created above only describes the layout of our framebuffers. Before we - // can draw we also need to create the actual framebuffers. - // - // Since we need to draw to multiple images, we are going to create a different framebuffer for - // each image. - let ([mut mesh_pipeline], mut framebuffers) = window_size_dependent_setup( - &memory_allocator, - &mesh_vs, - &mesh_fs, - &implicit_ms, - &images, - render_pass.clone(), - &mut viewport, - ); + let [RES_X, RES_Y] = images[0].dimensions().width_height(); + let ([mut mesh_pipeline, mut implicit_pipeline], mut framebuffers) = + window_size_dependent_setup( + &memory_allocator, + &mesh_vs, + &mesh_fs, + &implicit_ms, + &implicit_fs, + &images, + render_pass.clone(), + &mut viewport, + implicit_fs::SpecializationConstants { RES_X, RES_Y }, + ); - // Before we can start creating and recording command buffers, we need a way of allocating - // them. Vulkano provides a command buffer allocator, which manages raw Vulkan command pools - // underneath and provides a safe interface for them. let command_buffer_allocator = StandardCommandBufferAllocator::new(device.clone(), Default::default()); - // Initialization is finally finished! - - // In some situations, the swapchain will become invalid by itself. This includes for example - // when the window is resized (as the images of the swapchain will no longer match the - // window's) or, on Android, when the application went to the background and goes back to the - // foreground. - // - // In this situation, acquiring a swapchain image or presenting it will return an error. - // Rendering to an image of that swapchain will not produce any error, but may or may not work. - // To continue rendering, we need to recreate the swapchain by creating a new swapchain. - // Here, we remember that we need to do this for the next loop iteration. let mut recreate_swapchain = false; - - // In the loop below we are going to submit commands to the GPU. Submitting a command produces - // an object that implements the `GpuFuture` trait, which holds the resources for as long as - // they are in use by the GPU. - // - // Destroying the `GpuFuture` blocks until the GPU is finished executing it. In order to avoid - // that, we store the submission of the previous frame here. let mut previous_frame_end = Some(sync::now(device.clone()).boxed()); /* @@ -513,7 +320,6 @@ fn main() { let uniform_buffer = SubbufferAllocator::new( memory_allocator.clone(), SubbufferAllocatorCreateInfo { - // We want to use the allocated subbuffers as vertex buffers. buffer_usage: BufferUsage::UNIFORM_BUFFER, ..Default::default() }, @@ -578,7 +384,7 @@ fn main() { WindowEvent::ScaleFactorChanged { .. } => { recreate_swapchain = true; } - WindowEvent::DroppedFile(file) => { + WindowEvent::DroppedFile(_file) => { todo!() } WindowEvent::MouseInput { @@ -622,7 +428,6 @@ fn main() { camforward.x = camforward.x + Deg(360f32) % Deg(360f32); camforward.y = camforward.y + Deg(360f32) % Deg(360f32); } - //println!("AXISM {:?}", delta); } Event::RedrawEventsCleared => { for i in 1..gstate.fps.len() { @@ -634,55 +439,43 @@ fn main() { render_start = Instant::now(); - // Do not draw frame when screen dimensions are zero. - // On Windows, this can occur from minimizing the application. let window = surface.object().unwrap().downcast_ref::<Window>().unwrap(); let dimensions = window.inner_size(); if dimensions.width == 0 || dimensions.height == 0 { return; } - // It is important to call this function from time to time, otherwise resources will keep - // accumulating and you will eventually reach an out of memory error. - // Calling this function polls various fences in order to determine what the GPU has - // already processed, and frees the resources that are no longer needed. previous_frame_end.as_mut().unwrap().cleanup_finished(); - // Whenever the window resizes we need to recreate everything dependent on the window size. - // In this example that includes the swapchain, the framebuffers and the dynamic state viewport. if recreate_swapchain { - // Use the new dimensions of the window. - let (new_swapchain, new_images) = match swapchain.recreate(SwapchainCreateInfo { image_extent: dimensions.into(), ..swapchain.create_info() }) { Ok(r) => r, - // This error tends to happen when the user is manually resizing the window. - // Simply restarting the loop is the easiest way to fix this issue. Err(SwapchainCreationError::ImageExtentNotSupported { .. }) => return, Err(e) => panic!("Failed to recreate swapchain: {e:?}"), }; swapchain = new_swapchain; - // Because framebuffers contains an Arc on the old swapchain, we need to - // recreate framebuffers as well. - ([mesh_pipeline], framebuffers) = window_size_dependent_setup( - &memory_allocator, - &mesh_vs, - &mesh_fs, - &implicit_ms, - &new_images, - render_pass.clone(), - &mut viewport, - ); + let [RES_X, RES_Y] = images[0].dimensions().width_height(); + ([mesh_pipeline, implicit_pipeline], framebuffers) = + window_size_dependent_setup( + &memory_allocator, + &mesh_vs, + &mesh_fs, + &implicit_ms, + &implicit_fs, + &new_images, + render_pass.clone(), + &mut viewport, + implicit_fs::SpecializationConstants { RES_X, RES_Y }, + ); recreate_swapchain = false; } - //println!("{:?}", right); - - let mut push_constants = { + let (mut push_constants, cam_set) = { if looking { if keys.w { campos -= Matrix3::from_angle_y(camforward.y) @@ -719,8 +512,6 @@ fn main() { keys.d = false; } - // note: this teapot was meant for OpenGL where the origin is at the lower left - // instead the origin is at the upper left in Vulkan, so we reverse the Y axis let aspect_ratio = swapchain.image_extent()[0] as f32 / swapchain.image_extent()[1] as f32; let proj = cgmath::perspective( @@ -734,23 +525,25 @@ fn main() { * Matrix4::from_angle_z(Deg(180f32)) * Matrix4::from_translation(Point3::origin() - campos) * Matrix4::from_scale(scale); - //*Matrix4::from_angle_z(Deg(180f32)); let pc = mesh_vs::ty::PushConstantData { world: Matrix4::identity().into(), + }; + + let uniform_data = mesh_fs::ty::Camera { view: view.into(), proj: proj.into(), + campos: (campos * 100.0).into(), }; + let sub = uniform_buffer.allocate_sized().unwrap(); + *sub.write().unwrap() = uniform_data; + if looking { - /*println!( - "world: {:?} view: {:?} proj: {:?}", - pc.world, pc.view, pc.proj - );*/ println!("campos: {:?} camforward: {:?}", campos, camforward); } - pc + (pc, sub) }; let uniform_buffer_subbuffer = { @@ -766,7 +559,7 @@ fn main() { col[i][2] = light.colour.z; } - let uniform_data = mesh_fs::ty::Data { + let uniform_data = mesh_fs::ty::Lights { pos, col, light_count: gstate.lights.len() as u32, @@ -777,21 +570,28 @@ fn main() { sub }; - let layout = mesh_pipeline.layout().set_layouts().get(0).unwrap(); - let set = PersistentDescriptorSet::new( + let mesh_layout = mesh_pipeline.layout().set_layouts().get(0).unwrap(); + let mesh_set = PersistentDescriptorSet::new( + &descriptor_set_allocator, + mesh_layout.clone(), + [ + WriteDescriptorSet::buffer(0, uniform_buffer_subbuffer.clone()), + WriteDescriptorSet::buffer(1, cam_set.clone()), + ], + ) + .unwrap(); + + let implicit_layout = implicit_pipeline.layout().set_layouts().get(0).unwrap(); + let implicit_set = PersistentDescriptorSet::new( &descriptor_set_allocator, - layout.clone(), - [WriteDescriptorSet::buffer(0, uniform_buffer_subbuffer)], + implicit_layout.clone(), + [ + WriteDescriptorSet::buffer(0, uniform_buffer_subbuffer.clone()), + WriteDescriptorSet::buffer(1, cam_set.clone()), + ], ) .unwrap(); - // Before we can draw on the output, we have to *acquire* an image from the swapchain. If - // no image is available (which happens if you submit draw commands too quickly), then the - // function will block. - // This operation returns the index of the image that we are allowed to draw upon. - // - // This function can block if no image is available. The parameter is an optional timeout - // after which the function call will return an error. let (image_index, suboptimal, acquire_future) = match acquire_next_image(swapchain.clone(), None) { Ok(r) => r, @@ -802,24 +602,12 @@ fn main() { Err(e) => panic!("Failed to acquire next image: {:?}", e), }; - // acquire_next_image can be successful, but suboptimal. This means that the swapchain image - // will still work, but it may not display correctly. With some drivers this can be when - // the window resizes, but it may not cause the swapchain to become out of date. if suboptimal { recreate_swapchain = true; } gui_up(&mut gui, &mut gstate); - // In order to draw, we have to build a *command buffer*. The command buffer object holds - // the list of commands that are going to be executed. - // - // Building a command buffer is an expensive operation (usually a few hundred - // microseconds), but it is known to be a hot path in the driver and is expected to be - // optimized. - // - // Note that we have to pass a queue family when we create the command buffer. The command - // buffer will only be executable on that given queue family. let mut builder = AutoCommandBufferBuilder::primary( &command_buffer_allocator, queue.queue_family_index(), @@ -830,15 +618,8 @@ fn main() { let cb = gui.draw_on_subpass_image(dimensions.into()); builder - // Before we can draw, we have to *enter a render pass*. .begin_render_pass( RenderPassBeginInfo { - // A list of values to clear the attachments with. This list contains - // one item for each attachment in the render pass. In this case, - // there is only one attachment, and we clear it with a blue color. - // - // Only attachments that have `LoadOp::Clear` are provided with clear - // values, any others should use `ClearValue::None` as the clear value. clear_values: vec![ Some([0.12, 0.1, 0.1, 1.0].into()), Some(1.0.into()), @@ -847,23 +628,16 @@ fn main() { framebuffers[image_index as usize].clone(), ) }, - // The contents of the first (and only) subpass. This can be either - // `Inline` or `SecondaryCommandBuffers`. The latter is a bit more advanced - // and is not covered here. SubpassContents::Inline, ) .unwrap() - // We are now inside the first subpass of the render pass. We add a draw command. - // - // The last two parameters contain the list of resources to pass to the shaders. - // Since we used an `EmptyPipeline` object, the objects have to be `()`. .set_viewport(0, [viewport.clone()]) .bind_pipeline_graphics(mesh_pipeline.clone()) .bind_descriptor_sets( PipelineBindPoint::Graphics, mesh_pipeline.layout().clone(), 0, - set, + mesh_set, ); for object in &gstate.meshes { @@ -880,38 +654,20 @@ fn main() { .unwrap(); } - /*unsafe { - let secondary_builder = AutoCommandBufferBuilder::secondary( - &command_buffer_allocator, - queue_family_index, - CommandBufferUsage::OneTimeSubmit, - CommandBufferInheritanceInfo { - render_pass: Some( - Subpass::from(render_pass.clone(), 0).unwrap().into(), - ), - ..Default::default() - }, - ) - .unwrap(); - - let secondary_buffer = secondary_builder.build().unwrap(); - - (device.fns().ext_mesh_shader.cmd_draw_mesh_tasks_ext)( - secondary_buffer.handle(), - 1, - 1, - 1, - ); + push_constants.world = Matrix4::identity().into(); - /*builder - .execute_commands(secondary_buffer) - .expect("Failed to execute chicanery");*/ - }*/ + builder + .bind_pipeline_graphics(implicit_pipeline.clone()) + .bind_descriptor_sets( + PipelineBindPoint::Graphics, + implicit_pipeline.layout().clone(), + 0, + implicit_set, + ) + .push_constants(implicit_pipeline.layout().clone(), 0, push_constants); builder.draw_mesh([1, 1, 1]).unwrap(); - // We leave the render pass. Note that if we had multiple - // subpasses we could have called `next_subpass` to jump to the next subpass. builder .next_subpass(SubpassContents::SecondaryCommandBuffers) .unwrap() @@ -920,7 +676,6 @@ fn main() { .end_render_pass() .unwrap(); - // Finish building the command buffer by calling `build`. let command_buffer = builder.build().unwrap(); let future = previous_frame_end @@ -929,12 +684,6 @@ fn main() { .join(acquire_future) .then_execute(queue.clone(), command_buffer) .unwrap() - // The color output is now expected to contain our triangle. But in order to show it on - // the screen, we have to *present* the image by calling `present`. - // - // This function does not actually present the image immediately. Instead it submits a - // present command at the end of the queue. This means that it will only be presented once - // the GPU has finished executing the command buffer that draws the triangle. .then_swapchain_present( queue.clone(), SwapchainPresentInfo::swapchain_image_index(swapchain.clone(), image_index), @@ -961,15 +710,20 @@ fn main() { } /// This method is called once during initialization, then again whenever the window is resized -fn window_size_dependent_setup( +fn window_size_dependent_setup<Mms>( allocator: &StandardMemoryAllocator, mesh_vs: &ShaderModule, mesh_fs: &ShaderModule, implicit_ms: &ShaderModule, + implicit_fs: &ShaderModule, images: &[Arc<SwapchainImage>], render_pass: Arc<RenderPass>, viewport: &mut Viewport, -) -> ([Arc<GraphicsPipeline>; 1], Vec<Arc<Framebuffer>>) { + specs: Mms, +) -> ([Arc<GraphicsPipeline>; 2], Vec<Arc<Framebuffer>>) +where + Mms: SpecializationConstants, +{ let dimensions = images[0].dimensions().width_height(); viewport.dimensions = [dimensions[0] as f32, dimensions[1] as f32]; @@ -993,18 +747,10 @@ fn window_size_dependent_setup( }) .collect::<Vec<_>>(); - // Before we draw we have to create what is called a pipeline. This is similar to an OpenGL - // program, but much more specific. let mesh_pipeline = GraphicsPipeline::start() - // We have to indicate which subpass of which render pass this pipeline is going to be used - // in. The pipeline will only be usable from this particular subpass. .render_pass(Subpass::from(render_pass.clone(), 0).unwrap()) - // We need to indicate the layout of the vertices. .vertex_input_state(OVertex::per_vertex()) - // The content of the vertex buffer describes a list of triangles. .input_assembly_state(InputAssemblyState::new()) - // A Vulkan shader can in theory contain multiple entry points, so we have to specify - // which one. .vertex_shader(mesh_vs.entry_point("main").unwrap(), ()) .viewport_state(ViewportState::viewport_fixed_scissor_irrelevant([ Viewport { @@ -1013,18 +759,35 @@ fn window_size_dependent_setup( depth_range: 0.0..1.0, }, ])) - // See `vertex_shader`. .fragment_shader(mesh_fs.entry_point("main").unwrap(), ()) - .mesh_shader(implicit_ms.entry_point("main").unwrap(), ()) .depth_stencil_state(DepthStencilState::simple_depth_test()) .rasterization_state(RasterizationState { front_face: Fixed(Clockwise), cull_mode: Fixed(CullMode::Back), ..RasterizationState::default() }) - // Now that our builder is filled, we call `build()` to obtain an actual pipeline. .build(allocator.device().clone()) .unwrap(); - ([mesh_pipeline], framebuffers) + let implicit_pipeline = GraphicsPipeline::start() + .render_pass(Subpass::from(render_pass.clone(), 0).unwrap()) + .vertex_input_state(OVertex::per_vertex()) + .input_assembly_state(InputAssemblyState::new()) + .viewport_state(ViewportState::viewport_fixed_scissor_irrelevant([ + Viewport { + origin: [0.0, 0.0], + dimensions: [dimensions[0] as f32, dimensions[1] as f32], + depth_range: 0.0..1.0, + }, + ])) + .fragment_shader(implicit_fs.entry_point("main").unwrap(), specs) + .mesh_shader(implicit_ms.entry_point("main").unwrap(), ()) + .depth_stencil_state(DepthStencilState::simple_depth_test()) + .rasterization_state(RasterizationState { + ..RasterizationState::default() + }) + .build(allocator.device().clone()) + .unwrap(); + + ([mesh_pipeline, implicit_pipeline], framebuffers) } diff --git a/src/objects.rs b/src/objects.rs index 441e632a802d353b3c30558c9d8db746b99cd436..d974887d6522ed390a75bbfece658a531925d157 100644 --- a/src/objects.rs +++ b/src/objects.rs @@ -1,7 +1,7 @@ -use std::{collections::HashMap, io::Read, sync::Arc}; +use std::{collections::HashMap, io::Read}; use bytemuck::{Pod, Zeroable}; -use cgmath::{Deg, Euler, Matrix3, Point3, SquareMatrix, Vector3}; +use cgmath::{Deg, Euler, Point3, Vector3}; use obj::{LoadConfig, ObjData}; use vulkano::{ buffer::{Buffer, BufferAllocateInfo, BufferUsage, Subbuffer}, diff --git a/src/triangle.frag.glsl b/src/triangle.frag.glsl new file mode 100644 index 0000000000000000000000000000000000000000..5e3ce574bffd8cb3945f522cc4d293eddfa18068 --- /dev/null +++ b/src/triangle.frag.glsl @@ -0,0 +1,32 @@ +#version 450 + +layout(push_constant)uniform PushConstantData{ + mat4 world; +}pc; + +layout(set=0,binding=0)uniform Lights{ + vec4[32]pos; + vec4[32]col; + uint light_count; +}light_uniforms; + +layout(set=0,binding=1)uniform Camera{ + mat4 view; + mat4 proj; + vec3 campos; +}camera_uniforms; + +layout(location=0)in vec3 normal; + +layout(location=0)out vec4 f_color; + +void main(){ + vec3 accum=vec3(0.,0.,0.); + + for(int i=0;i<light_uniforms.light_count;i++) + { + accum+=light_uniforms.col[i].xyz*((dot(normalize(normal),light_uniforms.pos[i].xyz)*.5)+.5); + } + + f_color=vec4(accum,1.); +} \ No newline at end of file diff --git a/src/triangle.vert.glsl b/src/triangle.vert.glsl new file mode 100644 index 0000000000000000000000000000000000000000..9595b8f1e260777777b645eb7c903a277e37f070 --- /dev/null +++ b/src/triangle.vert.glsl @@ -0,0 +1,28 @@ +#version 450 + +layout(push_constant)uniform PushConstantData{ + mat4 world; +}pc; + +layout(set=0,binding=0)uniform Lights{ + vec4[32]pos; + vec4[32]col; + uint light_count; +}light_uniforms; + +layout(set=0,binding=1)uniform Camera{ + mat4 view; + mat4 proj; + vec3 campos; +}camera_uniforms; + +layout(location=0)in vec3 position; +layout(location=1)in vec3 normal; + +layout(location=0)out vec3 v_normal; + +void main(){ + mat4 worldview=camera_uniforms.view*pc.world; + v_normal=normal;//normalize(transpose(inverse(mat3(worldview))) * normal); + gl_Position=camera_uniforms.proj*worldview*vec4(position*1000.,1.); +} \ No newline at end of file