Better lighting, culling

This commit is contained in:
John Hunter
2023-02-12 03:00:26 +00:00
parent 56dc49f96f
commit ea771018be
5 changed files with 113 additions and 87 deletions
+14 -9
View File
@@ -28,10 +28,12 @@ const uint MAX_STEPS=50;
vec3 shading(vec3 normal) vec3 shading(vec3 normal)
{ {
vec3 accum=vec3(0.,0.,0.); vec3 accum=vec3(0.,0.,0.);
mat3 rotation=mat3(pc.world[0].xyz,pc.world[1].xyz,pc.world[2].xyz);
vec3 position=pc.world[3].xyz;
for(int i=0;i<light_uniforms.light_count;i++) 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); accum+=light_uniforms.col[i].xyz*((dot(normalize(rotation*normal),normalize(light_uniforms.pos[i].xyz-position))*.5)+.5);
} }
return accum; return accum;
@@ -69,15 +71,19 @@ void main(){
default_mask(); default_mask();
vec3 raypos=vertexInput.position.xyz; vec3 raypos=vertexInput.position.xyz;
vec2 iResolution=vec2(RES_X,RES_Y); vec2 iResolution=vec2(RES_X,RES_Y);
vec2 iuv=(gl_FragCoord.xy+gl_SamplePosition)/iResolution.xy*2.-1.; //#ifdef ssaa
//vec2 iuv=(gl_FragCoord.xy+gl_SamplePosition)/iResolution.xy*2.-1.;
//#else
vec2 iuv=(gl_FragCoord.xy)/iResolution.xy*2.-1.;
//#endif
vec2 uv=iuv; vec2 uv=iuv;
uv.x*=iResolution.x/iResolution.y; uv.x*=iResolution.x/iResolution.y;
vec3 p; vec3 p;
vec3 raydir=normalize(raypos-camera_uniforms.campos); vec3 raydir=normalize(raypos-(inverse(pc.world)*vec4(camera_uniforms.campos,1)).xyz);
raypos-=vec3(5); //raypos-=vec3(5);
//scene(raypos,false);
//f_color=vec4(scene(raypos,false),1.); //f_color=vec4(raydir,1.);
///return; //return;
vec2 td=spheretracing(raypos,raydir,p); vec2 td=spheretracing(raypos,raydir,p);
vec3 n=getNormal(p,td.y); vec3 n=getNormal(p,td.y);
@@ -86,9 +92,8 @@ void main(){
f_color=vec4(1.); f_color=vec4(1.);
f_color=vec4(shading(n),1.); f_color=vec4(shading(n),1.);
vec4 tpoint=camera_uniforms.proj*camera_uniforms.view*vec4(p,1); vec4 tpoint=camera_uniforms.proj*camera_uniforms.view*pc.world*vec4(p,1);
gl_FragDepth=(tpoint.z/tpoint.w); gl_FragDepth=(tpoint.z/tpoint.w);
//gl_FragDepth=gl_DepthRange.diff*(((tpoint.z/tpoint.w)+1.)*.5)+gl_DepthRange.near;
} }
else else
{ {
+10
View File
@@ -63,6 +63,16 @@ pub fn gui_up(gui: &mut Gui, state: &mut GState) {
ui.add(egui::Slider::new(&mut mesh.rot.y.0, 0.0..=360.0).text("Rotation.y")); ui.add(egui::Slider::new(&mut mesh.rot.y.0, 0.0..=360.0).text("Rotation.y"));
ui.add(egui::Slider::new(&mut mesh.rot.z.0, 0.0..=360.0).text("Rotation.z")); ui.add(egui::Slider::new(&mut mesh.rot.z.0, 0.0..=360.0).text("Rotation.z"));
} }
ui.heading("Implicit Surfaces");
for csg in &mut state.csg {
ui.label(csg.name.clone());
ui.add(egui::Slider::new(&mut csg.pos.x, -100.0..=100.0).text("Position.x"));
ui.add(egui::Slider::new(&mut csg.pos.y, -100.0..=100.0).text("Position.y"));
ui.add(egui::Slider::new(&mut csg.pos.z, -100.0..=100.0).text("Position.z"));
ui.add(egui::Slider::new(&mut csg.rot.x.0, 0.0..=360.0).text("Rotation.x"));
ui.add(egui::Slider::new(&mut csg.rot.y.0, 0.0..=360.0).text("Rotation.y"));
ui.add(egui::Slider::new(&mut csg.rot.z.0, 0.0..=360.0).text("Rotation.z"));
}
ui.heading("Lights"); ui.heading("Lights");
for light in &mut state.lights { for light in &mut state.lights {
ui.label("Light"); ui.label("Light");
+47 -40
View File
@@ -14,21 +14,15 @@ layout(location=0)out VertexOutput
}vertexOutput[]; }vertexOutput[];
const vec4[8]positions={ const vec4[8]positions={
vec4(0.,0.,0.,1.), vec4(-.5,-.5,-.5,1.),
vec4(0.,0.,1.,1.), vec4(-.5,-.5,.5,1.),
vec4(0.,1.,0.,1.), vec4(-.5,.5,-.5,1.),
vec4(0.,1.,1.,1.), vec4(-.5,.5,.5,1.),
vec4(1.,0.,0.,1.), vec4(.5,-.5,-.5,1.),
vec4(1.,0.,1.,1.), vec4(.5,-.5,.5,1.),
vec4(1.,1.,0.,1.), vec4(.5,.5,-.5,1.),
vec4(1.,1.,1.,1.), vec4(.5,.5,.5,1.),
}; };
const mat4 scale=mat4(
10.,0.,0.,0.,
0.,10.,0.,0.,
0.,0.,10.,0.,
0.,0.,0.,1.
);
void main() void main()
{ {
@@ -36,34 +30,47 @@ void main()
vec4 offset=vec4(0.,0.,gl_GlobalInvocationID.x,0.); vec4 offset=vec4(0.,0.,gl_GlobalInvocationID.x,0.);
SetMeshOutputsEXT(8,12); vec3 signingvec=sign((inverse(pc.world)*vec4(camera_uniforms.campos,1)).xyz);
mat4 mvp=camera_uniforms.proj*camera_uniforms.view*scale;
gl_MeshVerticesEXT[0].gl_Position=mvp*(positions[0]+offset); SetMeshOutputsEXT(8,6);
gl_MeshVerticesEXT[1].gl_Position=mvp*(positions[1]+offset); mat4 mvp=camera_uniforms.proj*camera_uniforms.view*pc.world;
gl_MeshVerticesEXT[2].gl_Position=mvp*(positions[2]+offset); //mat4 sw=pc.world;
gl_MeshVerticesEXT[3].gl_Position=mvp*(positions[3]+offset); gl_MeshVerticesEXT[0].gl_Position=mvp*(positions[0]);
gl_MeshVerticesEXT[4].gl_Position=mvp*(positions[4]+offset); gl_MeshVerticesEXT[1].gl_Position=mvp*(positions[1]);
gl_MeshVerticesEXT[5].gl_Position=mvp*(positions[5]+offset); gl_MeshVerticesEXT[2].gl_Position=mvp*(positions[2]);
gl_MeshVerticesEXT[6].gl_Position=mvp*(positions[6]+offset); gl_MeshVerticesEXT[3].gl_Position=mvp*(positions[3]);
gl_MeshVerticesEXT[7].gl_Position=mvp*(positions[7]+offset); gl_MeshVerticesEXT[4].gl_Position=mvp*(positions[4]);
vertexOutput[0].position=scale*positions[0]; gl_MeshVerticesEXT[5].gl_Position=mvp*(positions[5]);
vertexOutput[1].position=scale*positions[1]; gl_MeshVerticesEXT[6].gl_Position=mvp*(positions[6]);
vertexOutput[2].position=scale*positions[2]; gl_MeshVerticesEXT[7].gl_Position=mvp*(positions[7]);
vertexOutput[3].position=scale*positions[3]; vertexOutput[0].position=(positions[0]);
vertexOutput[4].position=scale*positions[4]; vertexOutput[1].position=(positions[1]);
vertexOutput[5].position=scale*positions[5]; vertexOutput[2].position=(positions[2]);
vertexOutput[6].position=scale*positions[6]; vertexOutput[3].position=(positions[3]);
vertexOutput[7].position=scale*positions[7]; vertexOutput[4].position=(positions[4]);
vertexOutput[5].position=(positions[5]);
vertexOutput[6].position=(positions[6]);
vertexOutput[7].position=(positions[7]);
if(signingvec.x>0){
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+0]=uvec3(4,5,6);
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+1]=uvec3(5,6,7);
}else{
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+0]=uvec3(0,1,2); gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+0]=uvec3(0,1,2);
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+1]=uvec3(1,2,3); 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); if(signingvec.y>0){
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+2]=uvec3(2,3,6);
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+3]=uvec3(7,3,6);
}else{
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+2]=uvec3(0,1,4);
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+3]=uvec3(5,1,4);
}
if(signingvec.z>0){
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+4]=uvec3(1,3,5);
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+5]=uvec3(3,5,7);
}else{
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+4]=uvec3(0,2,4); gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+4]=uvec3(0,2,4);
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+5]=uvec3(2,4,6); 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);
} }
+37 -33
View File
@@ -89,7 +89,7 @@ fn main() {
let event_loop = EventLoop::new(); let event_loop = EventLoop::new();
let surface = WindowBuilder::new() let surface = WindowBuilder::new()
.with_title("horizontally spinning bunny") .with_title("Implicit Surfaces")
.build_vk_surface(&event_loop, instance.clone()) .build_vk_surface(&event_loop, instance.clone())
.unwrap(); .unwrap();
@@ -124,7 +124,7 @@ fn main() {
_ => 5, _ => 5,
} }
}) })
.expect("No suitable physical device found"); .expect("No suitable physical device found. Do you have a Mesh Shader capable GPU and are your drivers up to date?");
// Some little debug infos. // Some little debug infos.
println!( println!(
@@ -402,10 +402,10 @@ fn main() {
gstate gstate
.lights .lights
.push(Light::new([4., 6., 8.], [1., 1., 8.], 0.01)); .push(Light::new([4., 6., 8.], [1., 1., 8.], 0.05));
gstate gstate
.lights .lights
.push(Light::new([-4., 6., -8.], [8., 4., 1.], 0.01)); .push(Light::new([-4., 6., -8.], [8., 4., 1.], 0.05));
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
if let Event::WindowEvent { event: we, .. } = &event { if let Event::WindowEvent { event: we, .. } = &event {
@@ -517,28 +517,28 @@ fn main() {
campos -= Matrix3::from_angle_y(camforward.y) campos -= Matrix3::from_angle_y(camforward.y)
* Matrix3::from_angle_x(camforward.x) * Matrix3::from_angle_x(camforward.x)
* Vector3::unit_z() * Vector3::unit_z()
* 0.02 * 0.01
* gstate.move_speed; * gstate.move_speed;
} }
if keys.s { if keys.s {
campos += Matrix3::from_angle_y(camforward.y) campos += Matrix3::from_angle_y(camforward.y)
* Matrix3::from_angle_x(camforward.x) * Matrix3::from_angle_x(camforward.x)
* Vector3::unit_z() * Vector3::unit_z()
* 0.02 * 0.01
* gstate.move_speed; * gstate.move_speed;
} }
if keys.a { if keys.a {
campos += Matrix3::from_angle_y(camforward.y) campos += Matrix3::from_angle_y(camforward.y)
* Matrix3::from_angle_x(camforward.x) * Matrix3::from_angle_x(camforward.x)
* Vector3::unit_x() * Vector3::unit_x()
* 0.02 * 0.01
* gstate.move_speed; * gstate.move_speed;
} }
if keys.d { if keys.d {
campos -= Matrix3::from_angle_y(camforward.y) campos -= Matrix3::from_angle_y(camforward.y)
* Matrix3::from_angle_x(camforward.x) * Matrix3::from_angle_x(camforward.x)
* Vector3::unit_x() * Vector3::unit_x()
* 0.02 * 0.01
* gstate.move_speed; * gstate.move_speed;
} }
} else { } else {
@@ -559,11 +559,9 @@ fn main() {
near, near,
far, far,
); );
let scale = 0.01;
let view = Matrix4::from(camforward) let view = Matrix4::from(camforward)
* Matrix4::from_angle_z(Deg(180f32)) * Matrix4::from_angle_z(Deg(180f32))
* Matrix4::from_translation(Point3::origin() - campos) * Matrix4::from_translation(Point3::origin() - campos);
* Matrix4::from_scale(scale);
let pc = mesh_vs::ty::PushConstantData { let pc = mesh_vs::ty::PushConstantData {
world: Matrix4::identity().into(), world: Matrix4::identity().into(),
@@ -572,7 +570,7 @@ fn main() {
let uniform_data = mesh_fs::ty::Camera { let uniform_data = mesh_fs::ty::Camera {
view: view.into(), view: view.into(),
proj: proj.into(), proj: proj.into(),
campos: (campos * (far - near)).into(), campos: (campos).into(),
}; };
let sub = uniform_buffer.allocate_sized().unwrap(); let sub = uniform_buffer.allocate_sized().unwrap();
@@ -614,21 +612,21 @@ fn main() {
let parts = vec![ let parts = vec![
CSGPart::literal(0i8.into()), CSGPart::literal(0i8.into()),
CSGPart::literal(2i8.into()), CSGPart::literal(f16::from_f32(0.2)),
CSGPart::literal(0i8.into()), CSGPart::literal(0i8.into()),
CSGPart::opcode(InstructionSet::OPDupVec3), CSGPart::opcode(InstructionSet::OPDupVec3),
CSGPart::opcode(InstructionSet::OPPromoteFloatFloatFloatVec3), CSGPart::opcode(InstructionSet::OPPromoteFloatFloatFloatVec3),
CSGPart::opcode(InstructionSet::OPSubVec3Vec3), CSGPart::opcode(InstructionSet::OPSubVec3Vec3),
CSGPart::literal(3i8.into()), CSGPart::literal(f16::from_f32(0.3)),
CSGPart::opcode(InstructionSet::OPSDFSphere), CSGPart::opcode(InstructionSet::OPSDFSphere),
CSGPart::literal(0i8.into()), CSGPart::literal(0i8.into()),
CSGPart::literal(2i8.into()), CSGPart::literal(f16::from_f32(0.2)),
CSGPart::literal(0i8.into()), CSGPart::literal(0i8.into()),
CSGPart::opcode(InstructionSet::OPPromoteFloatFloatFloatVec3), CSGPart::opcode(InstructionSet::OPPromoteFloatFloatFloatVec3),
CSGPart::opcode(InstructionSet::OPAddVec3Vec3), CSGPart::opcode(InstructionSet::OPAddVec3Vec3),
CSGPart::literal(3i8.into()), CSGPart::literal(f16::from_f32(0.3)),
CSGPart::opcode(InstructionSet::OPSDFSphere), CSGPart::opcode(InstructionSet::OPSDFSphere),
CSGPart::literal(f16::from_f32(0.5)), CSGPart::literal(f16::from_f32(0.05)),
CSGPart::opcode(InstructionSet::OPSmoothMinFloat), CSGPart::opcode(InstructionSet::OPSmoothMinFloat),
CSGPart::opcode(InstructionSet::OPStop), CSGPart::opcode(InstructionSet::OPStop),
]; ];
@@ -653,14 +651,7 @@ fn main() {
} }
} }
//01111101010101000000000000000000 //println!("data: {:?}, {:?}", data[0][0], data[0][1]);
/*data[0][0] = ((CSGPart::literal(5u8.into()).code as u32) << 0)
| ((CSGPart::opcode(InstructionSet::OPSDFSphere).code as u32) << 16);
data[0][1] = ((CSGPart::opcode(InstructionSet::OPStop).code as u32) << 16)
| (CSGPart::opcode(InstructionSet::OPStop).code as u32);*/
println!("data: {:?}, {:?}", data[0][0], data[0][1]);
let uniform_data = implicit_fs::ty::SceneDescription { d: data }; let uniform_data = implicit_fs::ty::SceneDescription { d: data };
@@ -715,7 +706,7 @@ fn main() {
) )
.unwrap(); .unwrap();
let cb = gui.draw_on_subpass_image(dimensions.into()); let guicb = gui.draw_on_subpass_image(dimensions.into());
builder builder
.begin_render_pass( .begin_render_pass(
@@ -743,7 +734,7 @@ fn main() {
for object in &gstate.meshes { for object in &gstate.meshes {
push_constants.world = push_constants.world =
(Matrix4::from_translation(object.pos - Point3::origin()) (Matrix4::from_translation(object.pos * 0.01 - Point3::origin())
* Matrix4::from(object.rot) * Matrix4::from(object.rot)
* Matrix4::from_nonuniform_scale( * Matrix4::from_nonuniform_scale(
object.scale.x, object.scale.x,
@@ -759,8 +750,6 @@ fn main() {
.unwrap(); .unwrap();
} }
push_constants.world = Matrix4::identity().into();
builder builder
.bind_pipeline_graphics(implicit_pipeline.clone()) .bind_pipeline_graphics(implicit_pipeline.clone())
.bind_descriptor_sets( .bind_descriptor_sets(
@@ -768,15 +757,28 @@ fn main() {
implicit_pipeline.layout().clone(), implicit_pipeline.layout().clone(),
0, 0,
implicit_set, implicit_set,
) );
.push_constants(implicit_pipeline.layout().clone(), 0, push_constants);
builder.draw_mesh([1, 1, 1]).unwrap(); for csg in &gstate.csg {
push_constants.world =
(Matrix4::from_translation(csg.pos * 0.01 - Point3::origin())
* Matrix4::from(csg.rot)
* Matrix4::from_nonuniform_scale(
csg.scale.x,
csg.scale.y,
csg.scale.z,
))
.into();
builder
.push_constants(implicit_pipeline.layout().clone(), 0, push_constants)
.draw_mesh([1, 1, 1])
.unwrap();
}
builder builder
.next_subpass(SubpassContents::SecondaryCommandBuffers) .next_subpass(SubpassContents::SecondaryCommandBuffers)
.unwrap() .unwrap()
.execute_commands(cb) .execute_commands(guicb)
.unwrap() .unwrap()
.end_render_pass() .end_render_pass()
.unwrap(); .unwrap();
@@ -926,6 +928,8 @@ where
.mesh_shader(implicit_ms.entry_point("main").unwrap(), ()) .mesh_shader(implicit_ms.entry_point("main").unwrap(), ())
.depth_stencil_state(DepthStencilState::simple_depth_test()) .depth_stencil_state(DepthStencilState::simple_depth_test())
.rasterization_state(RasterizationState { .rasterization_state(RasterizationState {
//front_face: Fixed(Clockwise),
//cull_mode: Fixed(CullMode::Back),
..RasterizationState::default() ..RasterizationState::default()
}) })
.multisample_state(MultisampleState { .multisample_state(MultisampleState {
+1 -1
View File
@@ -9,5 +9,5 @@ layout(location=0)out vec3 v_normal;
void main(){ void main(){
mat4 worldview=camera_uniforms.view*pc.world; mat4 worldview=camera_uniforms.view*pc.world;
v_normal=normal;//normalize(transpose(inverse(mat3(worldview))) * normal); v_normal=normal;//normalize(transpose(inverse(mat3(worldview))) * normal);
gl_Position=camera_uniforms.proj*worldview*vec4(position*1000.,1.); gl_Position=camera_uniforms.proj*worldview*vec4(position,1.);
} }