diff --git a/src/render_domain.rs b/src/render_domain.rs index 37a61b87..0d333f81 100644 --- a/src/render_domain.rs +++ b/src/render_domain.rs @@ -168,7 +168,7 @@ impl VisibilityWorldRenderDomain { binding: 6, descriptor_type: render_system::DescriptorType::StorageBuffer, descriptor_count: 1, - stages: render_system::Stages::MESH, + stages: render_system::Stages::MESH | render_system::Stages::COMPUTE, immutable_samplers: None, }, ]; @@ -321,7 +321,7 @@ void main() {{ uint triangle_indices[3] = uint[](indices[triangle_index * 3 + 0], indices[triangle_index * 3 + 1], indices[triangle_index * 3 + 2]); gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationID.x] = uvec3(triangle_indices[0], triangle_indices[1], triangle_indices[2]); out_instance_index[gl_LocalInvocationID.x] = instance_index; - out_primitive_index[gl_LocalInvocationID.x] = triangle_index; + out_primitive_index[gl_LocalInvocationID.x] = (meshlet_index << 8) | (gl_LocalInvocationID.x & 0xFF); }} }}", TRIANGLE_COUNT=TRIANGLE_COUNT, VERTEX_COUNT=VERTEX_COUNT); diff --git a/src/rendering/visibility_shader_generator.rs b/src/rendering/visibility_shader_generator.rs index 316eb293..7260b69b 100644 --- a/src/rendering/visibility_shader_generator.rs +++ b/src/rendering/visibility_shader_generator.rs @@ -104,6 +104,19 @@ impl VisibilityShaderGenerator { string.push_str("layout(set=0,binding=5) uniform sampler2D textures[1]; +struct Meshlet { + uint32_t instance_index; + uint16_t vertex_offset; + uint16_t triangle_offset; + uint8_t vertex_count; + uint8_t triangle_count; + uint8_t padding[6]; +}; + +layout(set=0,binding=6,scalar) buffer readonly MeshletsBuffer { + Meshlet meshlets[]; +}; + layout(set=1,binding=0,scalar) buffer MaterialCount { uint material_count[]; }; @@ -113,8 +126,8 @@ layout(set=1,binding=1,scalar) buffer MaterialOffset { layout(set=1,binding=4,scalar) buffer PixelMapping { u16vec2 pixel_mapping[]; }; -layout(set=1, binding=6, r8ui) uniform readonly uimage2D triangle_index; -layout(set=1, binding=7, r8ui) uniform readonly uimage2D instance_id; +layout(set=1, binding=6, r32ui) uniform readonly uimage2D triangle_index; +layout(set=1, binding=7, r32ui) uniform readonly uimage2D instance_id; layout(set=2, binding=0, rgba16) uniform image2D out_albedo; layout(set=2, binding=7, rgba16) uniform image2D out_position; layout(set=2, binding=8, rgba16) uniform image2D out_normals; @@ -317,16 +330,19 @@ void main() {{ uint offset = material_offset[pc.material_id]; u16vec2 be_pixel_xy = pixel_mapping[offset + gl_GlobalInvocationID.x]; ivec2 be_pixel_coordinate = ivec2(be_pixel_xy.x, be_pixel_xy.y); - uint BE_TRIANGLE_INDEX = imageLoad(triangle_index, be_pixel_coordinate).r; + uint BE_TRIANGLE_MESHLET_INDEX = imageLoad(triangle_index, be_pixel_coordinate).r; + uint BE_TRIANGLE_INDEX = BE_TRIANGLE_MESHLET_INDEX & 0xFF; + uint BE_MESHLET_INDEX = BE_TRIANGLE_MESHLET_INDEX >> 8; uint be_instance_id = imageLoad(instance_id, be_pixel_coordinate).r; Mesh mesh = meshes[be_instance_id]; Material material = materials[pc.material_id]; + Meshlet meshlet = meshlets[BE_MESHLET_INDEX]; uint vertex_indeces[3] = uint[3]( - uint(indeces[BE_TRIANGLE_INDEX * 3 + 0]), - uint(indeces[BE_TRIANGLE_INDEX * 3 + 1]), - uint(indeces[BE_TRIANGLE_INDEX * 3 + 2]) + uint(indeces[meshlet.triangle_offset * 3 + BE_TRIANGLE_INDEX * 3 + 0] + meshlet.vertex_offset), + uint(indeces[meshlet.triangle_offset * 3 + BE_TRIANGLE_INDEX * 3 + 1] + meshlet.vertex_offset), + uint(indeces[meshlet.triangle_offset * 3 + BE_TRIANGLE_INDEX * 3 + 2] + meshlet.vertex_offset) ); vec4 vertex_positions[3] = vec4[3]( diff --git a/src/resource_manager/mesh_resource_handler.rs b/src/resource_manager/mesh_resource_handler.rs index f391b3b7..b2390368 100644 --- a/src/resource_manager/mesh_resource_handler.rs +++ b/src/resource_manager/mesh_resource_handler.rs @@ -53,7 +53,10 @@ impl ResourceHandler for MeshResourceHandler { let vertex_count = if let Some(positions) = reader.read_positions() { let vertex_count = positions.clone().count(); - positions.for_each(|position| position.iter().for_each(|m| m.to_le_bytes().iter().for_each(|byte| buffer.push(*byte)))); + positions.for_each(|mut position| { + position[2] = -position[2]; + position.iter().for_each(|m| m.to_le_bytes().iter().for_each(|byte| buffer.push(*byte))) + }); vertex_components.push(VertexComponent { semantic: VertexSemantics::Position, format: "vec3f".to_string(), channel: 0 }); vertex_count } else { @@ -65,7 +68,11 @@ impl ResourceHandler for MeshResourceHandler { let optimized_indices = meshopt::optimize::optimize_vertex_cache(&indices, vertex_count); if let Some(normals) = reader.read_normals() { - normals.for_each(|normal| normal.iter().for_each(|m| m.to_le_bytes().iter().for_each(|byte| buffer.push(*byte)))); + normals.for_each(|mut normal| { + normal[2] = -normal[2]; + normal.iter().for_each(|m| m.to_le_bytes().iter().for_each(|byte| buffer.push(*byte))); + }); + vertex_components.push(VertexComponent { semantic: VertexSemantics::Normal, format: "vec3f".to_string(), channel: 1 }); } diff --git a/tests/gi.rs b/tests/gi.rs index 20f28c98..7d7debf9 100644 --- a/tests/gi.rs +++ b/tests/gi.rs @@ -12,7 +12,7 @@ fn gi() { let orchestrator = app.get_mut_orchestrator(); orchestrator.spawn(byte_engine::camera::Camera { - position: Vec3f::new(1.0, 0.5, -2.0), + position: Vec3f::new(0.0, 0.5, -2.0), direction: Vec3f::new(0.0, 0.0, 1.0), fov: 90.0, aspect_ratio: 1.0, @@ -21,11 +21,11 @@ fn gi() { }); let _floor: EntityHandle = orchestrator.spawn(Mesh{ resource_id: "Box", material_id: "white_solid", transform: maths_rs::Mat4f::from_translation(Vec3f::new(0.0, -0.5, 0.0)) * maths_rs::Mat4f::from_scale(Vec3f::new(5.0, 1.0, 2.5)), }); - let _a: EntityHandle = orchestrator.spawn(Mesh{ resource_id: "Suzanne", material_id: "white_solid", transform: maths_rs::Mat4f::from_translation(Vec3f::new(0.0, 0.25, 0.0)) * maths_rs::Mat4f::from_scale(Vec3f::new(0.5, 0.5, -0.5)), }); + let _a: EntityHandle = orchestrator.spawn(Mesh{ resource_id: "Suzanne", material_id: "white_solid", transform: maths_rs::Mat4f::from_translation(Vec3f::new(0.0, 0.5, 0.0)) * maths_rs::Mat4f::from_scale(Vec3f::new(0.4, 0.4, 0.4)), }); let _b: EntityHandle = orchestrator.spawn(Mesh{ resource_id: "Box", material_id: "red_solid", transform: maths_rs::Mat4f::from_translation(Vec3f::new(-0.6, 0.17, -0.1)) * maths_rs::Mat4f::from_scale(Vec3f::new(0.34, 0.34, 0.34)), }); let _c: EntityHandle = orchestrator.spawn(Mesh{ resource_id: "Box", material_id: "green_solid", transform: maths_rs::Mat4f::from_translation(Vec3f::new(0.5, 0.13, -0.3)) * maths_rs::Mat4f::from_scale(Vec3f::new(0.26, 0.26, 0.26)), }); - let _sun: EntityHandle = orchestrator.spawn(PointLight::new(Vec3f::new(0.0, 2.5, -1.0), 4500.0)); + let _sun: EntityHandle = orchestrator.spawn(PointLight::new(Vec3f::new(0.0, 2.5, -1.5), 4500.0)); app.do_loop();