diff --git a/src/frag.glsl b/src/frag.glsl new file mode 100644 index 0000000000000000000000000000000000000000..fcb43d7c23c572d4a0a808a3b0aad222d042d286 --- /dev/null +++ b/src/frag.glsl @@ -0,0 +1,104 @@ +// global fragment shader + +#version 450 +#include "include.glsl" + +layout(constant_id=0)const uint RES_X=1920; +layout(constant_id=1)const uint RES_Y=1080; + +#ifdef implicit + +layout(location=0)in VertexInput +{ + vec4 position; +}vertexInput; + +#else + +layout(location=0)in vec3 tri_normal; + +#endif + +layout(location=0)out vec4 f_color; + +const float EPSILON=.0001; +const uint MAX_STEPS=50; + +/// SHARED CODE /// +vec3 shading(vec3 normal) +{ + 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); + } + + return accum; +} + +/// IMPLICIT CODE /// +#ifdef implicit + +#define NEARPLANE 0. +#define FARPLANE length(vec3(10)) + +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(NEARPLANE,1.); + p=ori; + for(int i=0;i<MAX_STEPS&&td.y>EPSILON&&td.x<FARPLANE;i++){ + td.y=scene(p); + td.x+=(td.y)*.9; + p=ori+dir*td.x; + } + return td; +} + +//Implicit Surface Entrypoint +void main(){ + + vec3 raypos=vertexInput.position.xyz; + vec2 iResolution=vec2(RES_X,RES_Y); + vec2 iuv=(gl_FragCoord.xy+gl_SamplePosition)/iResolution.xy*2.-1.; + vec2 uv=iuv; + uv.x*=iResolution.x/iResolution.y; + vec3 p; + vec3 raydir=normalize(raypos-camera_uniforms.campos); + vec2 td=spheretracing(raypos,raydir,p); + vec3 n=getNormal(p,td.y); + if(td.y<EPSILON) + { + f_color=vec4(shading(n),1.); + + vec4 tpoint=camera_uniforms.proj*camera_uniforms.view*vec4(p,1); + gl_FragDepth=(tpoint.z/tpoint.w); + //gl_FragDepth=gl_DepthRange.diff*(((tpoint.z/tpoint.w)+1.)*.5)+gl_DepthRange.near; + } + else + { + discard; + } +} + +#else +/// TRIANGLE CODE /// + +//Mesh Surface Entrypoint +void main(){ + f_color=vec4(shading(tri_normal),1.); +} + +#endif \ No newline at end of file diff --git a/src/implicit.frag.glsl b/src/implicit.frag.glsl deleted file mode 100644 index 2b014e0f566bfc056049de46db1eb6fa04fd2d2c..0000000000000000000000000000000000000000 --- a/src/implicit.frag.glsl +++ /dev/null @@ -1,86 +0,0 @@ -// 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 index f7592344dba7a62e9ba9bcad7cad6f6e2016f666..13bcc97cf0ffe6e17060b137f91cefddf487e676 100644 --- a/src/implicit.mesh.glsl +++ b/src/implicit.mesh.glsl @@ -3,28 +3,14 @@ #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; +#include "include.glsl" 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; + vec4 position; }vertexOutput[]; const vec4[8]positions={ @@ -60,14 +46,14 @@ void main() 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]; + vertexOutput[0].position=scale*positions[0]; + vertexOutput[1].position=scale*positions[1]; + vertexOutput[2].position=scale*positions[2]; + vertexOutput[3].position=scale*positions[3]; + vertexOutput[4].position=scale*positions[4]; + vertexOutput[5].position=scale*positions[5]; + vertexOutput[6].position=scale*positions[6]; + vertexOutput[7].position=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); @@ -80,15 +66,4 @@ void main() 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 +} \ No newline at end of file diff --git a/src/include.glsl b/src/include.glsl new file mode 100644 index 0000000000000000000000000000000000000000..a1a82a66db54f2fd66598778f8efda6d4de3e844 --- /dev/null +++ b/src/include.glsl @@ -0,0 +1,15 @@ +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; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index cf43fd21754e44f99536d1381cbb3494397171df..cb480fa4da95cb79f58ffac0da86f667fca77add 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,16 +7,18 @@ use vulkano::descriptor_set::allocator::StandardDescriptorSetAllocator; use vulkano::descriptor_set::{PersistentDescriptorSet, WriteDescriptorSet}; use vulkano::device::{DeviceOwned, Features, QueueFlags}; use vulkano::format::Format; -use vulkano::image::AttachmentImage; +use vulkano::image::view::ImageViewCreateInfo; +use vulkano::image::{AttachmentImage, SampleCount}; use vulkano::memory::allocator::StandardMemoryAllocator; use vulkano::pipeline::graphics::depth_stencil::DepthStencilState; +use vulkano::pipeline::graphics::multisample::MultisampleState; 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, SpecializationConstants}; use vulkano::swapchain::{PresentMode, SwapchainPresentInfo}; -use vulkano::VulkanLibrary; +use vulkano::{Version, VulkanLibrary}; use winit::event::{DeviceEvent, ElementState, MouseButton, VirtualKeyCode}; use egui_winit_vulkano::Gui; @@ -133,6 +135,7 @@ fn main() { enabled_features: Features { mesh_shader: true, task_shader: true, + sample_rate_shading: true, ..Features::empty() }, ..Default::default() @@ -201,14 +204,15 @@ fn main() { mod mesh_fs { vulkano_shaders::shader! { ty: "fragment", - path: "src/triangle.frag.glsl", + path: "src/frag.glsl", types_meta: { use bytemuck::{Pod, Zeroable}; #[derive(Clone, Copy, Zeroable, Pod, Debug)] }, vulkan_version: "1.2", - spirv_version: "1.6" + spirv_version: "1.6", + define: [("triangle","1")] } } @@ -232,14 +236,15 @@ fn main() { mod implicit_fs { vulkano_shaders::shader! { ty: "fragment", - path: "src/implicit.frag.glsl", + path: "src/frag.glsl", types_meta: { use bytemuck::{Pod, Zeroable}; #[derive(Clone, Copy, Zeroable, Pod, Debug)] }, vulkan_version: "1.2", - spirv_version: "1.6" + spirv_version: "1.6", + define: [("implicit","1")] } } @@ -251,26 +256,33 @@ fn main() { let render_pass = vulkano::ordered_passes_renderpass!( device.clone(), attachments: { - color: { + intermediary: { load: Clear, + store: DontCare, + format: swapchain.image_format(), + samples: 4, + }, + color: { + load: DontCare, store: Store, format: swapchain.image_format(), samples: 1, }, - depth: { + multi_depth: { load: Clear, store: DontCare, format: Format::D16_UNORM, - samples: 1, + samples: 4, } }, passes: [{ - color: [color], - depth_stencil: {depth}, - input: [] + color: [intermediary], + depth_stencil: {multi_depth}, + input: [], + resolve: [color] },{ color: [color], - depth_stencil: {depth}, + depth_stencil: {}, input: [] }] ) @@ -512,13 +524,16 @@ fn main() { keys.d = false; } + let near = 0.01; + let far = 100.0; + let aspect_ratio = swapchain.image_extent()[0] as f32 / swapchain.image_extent()[1] as f32; let proj = cgmath::perspective( Rad(std::f32::consts::FRAC_PI_2), aspect_ratio, - 0.01, - 100.0, + near, + far, ); let scale = 0.01; let view = Matrix4::from(camforward) @@ -533,7 +548,7 @@ fn main() { let uniform_data = mesh_fs::ty::Camera { view: view.into(), proj: proj.into(), - campos: (campos * 100.0).into(), + campos: (campos * (far - near)).into(), }; let sub = uniform_buffer.allocate_sized().unwrap(); @@ -622,6 +637,7 @@ fn main() { RenderPassBeginInfo { clear_values: vec![ Some([0.12, 0.1, 0.1, 1.0].into()), + None, Some(1.0.into()), ], ..RenderPassBeginInfo::framebuffer( @@ -722,24 +738,54 @@ fn window_size_dependent_setup<Mms>( specs: Mms, ) -> ([Arc<GraphicsPipeline>; 2], Vec<Arc<Framebuffer>>) where - Mms: SpecializationConstants, + Mms: SpecializationConstants + Clone, { let dimensions = images[0].dimensions().width_height(); viewport.dimensions = [dimensions[0] as f32, dimensions[1] as f32]; let depth_buffer = ImageView::new_default( - AttachmentImage::transient(allocator, dimensions, Format::D16_UNORM).unwrap(), + AttachmentImage::transient_multisampled( + allocator, + dimensions, + SampleCount::Sample4, + Format::D16_UNORM, + ) + .unwrap(), + ) + .unwrap(); + + let intermediary_image = AttachmentImage::transient_multisampled( + allocator, + dimensions, + SampleCount::Sample4, + images[0].format(), + ) + .unwrap(); + + let intermediary = ImageView::new( + intermediary_image.clone(), + ImageViewCreateInfo { + usage: ImageUsage::COLOR_ATTACHMENT | ImageUsage::TRANSIENT_ATTACHMENT, + ..ImageViewCreateInfo::from_image(&intermediary_image) + }, ) .unwrap(); let framebuffers = images .iter() .map(|image| { - let view = ImageView::new_default(image.clone()).unwrap(); + let view = ImageView::new( + image.clone(), + ImageViewCreateInfo { + usage: ImageUsage::COLOR_ATTACHMENT | ImageUsage::TRANSFER_DST, + ..ImageViewCreateInfo::from_image(&intermediary_image) + }, + ) + .unwrap(); Framebuffer::new( render_pass.clone(), FramebufferCreateInfo { - attachments: vec![view, depth_buffer.clone()], + attachments: vec![intermediary.clone(), view, depth_buffer.clone()], ..Default::default() }, ) @@ -759,13 +805,20 @@ where depth_range: 0.0..1.0, }, ])) - .fragment_shader(mesh_fs.entry_point("main").unwrap(), ()) + .fragment_shader(mesh_fs.entry_point("main").unwrap(), specs.clone()) .depth_stencil_state(DepthStencilState::simple_depth_test()) .rasterization_state(RasterizationState { front_face: Fixed(Clockwise), cull_mode: Fixed(CullMode::Back), ..RasterizationState::default() }) + .multisample_state(MultisampleState { + rasterization_samples: Subpass::from(render_pass.clone(), 0) + .unwrap() + .num_samples() + .unwrap(), + ..Default::default() + }) .build(allocator.device().clone()) .unwrap(); @@ -786,6 +839,14 @@ where .rasterization_state(RasterizationState { ..RasterizationState::default() }) + .multisample_state(MultisampleState { + rasterization_samples: Subpass::from(render_pass.clone(), 0) + .unwrap() + .num_samples() + .unwrap(), + sample_shading: Some(0.5), + ..Default::default() + }) .build(allocator.device().clone()) .unwrap(); diff --git a/src/triangle.frag.glsl b/src/triangle.frag.glsl deleted file mode 100644 index 5e3ce574bffd8cb3945f522cc4d293eddfa18068..0000000000000000000000000000000000000000 --- a/src/triangle.frag.glsl +++ /dev/null @@ -1,32 +0,0 @@ -#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 index 9595b8f1e260777777b645eb7c903a277e37f070..e8ea796807d4bc4421796820ff1d45bb831e981f 100644 --- a/src/triangle.vert.glsl +++ b/src/triangle.vert.glsl @@ -1,20 +1,5 @@ #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; +#include "include.glsl" layout(location=0)in vec3 position; layout(location=1)in vec3 normal;