Skip to content

Commit

Permalink
Fixed meshlet rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
facundo-villa committed Oct 31, 2023
1 parent e8aabb7 commit 09a88f7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/render_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
];
Expand Down Expand Up @@ -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);

Expand Down
28 changes: 22 additions & 6 deletions src/rendering/visibility_shader_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
};
Expand All @@ -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;
Expand Down Expand Up @@ -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](
Expand Down
11 changes: 9 additions & 2 deletions src/resource_manager/mesh_resource_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 });
}

Expand Down
6 changes: 3 additions & 3 deletions tests/gi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,11 +21,11 @@ fn gi() {
});

let _floor: EntityHandle<Mesh> = 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<Mesh> = 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<Mesh> = 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<Mesh> = 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<Mesh> = 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<PointLight> = orchestrator.spawn(PointLight::new(Vec3f::new(0.0, 2.5, -1.0), 4500.0));
let _sun: EntityHandle<PointLight> = orchestrator.spawn(PointLight::new(Vec3f::new(0.0, 2.5, -1.5), 4500.0));

app.do_loop();

Expand Down

0 comments on commit 09a88f7

Please sign in to comment.