From 43163cd5c0bc1cf2b6c245130ffa9155515314d9 Mon Sep 17 00:00:00 2001 From: polymonster Date: Sun, 29 Oct 2023 15:54:41 +0000 Subject: [PATCH] - pbr sample working with some issues still to investgate --- hotline-data | 2 +- plugins/ecs_examples/src/pbr.rs | 37 ++++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/hotline-data b/hotline-data index 7e36505..dad0b39 160000 --- a/hotline-data +++ b/hotline-data @@ -1 +1 @@ -Subproject commit 7e3650579ce41145a8b5c70b384aa70225caf2ae +Subproject commit dad0b391cfe6bcd40c0504eabca3d4f286313e25 diff --git a/plugins/ecs_examples/src/pbr.rs b/plugins/ecs_examples/src/pbr.rs index bd5570a..4c77cfe 100644 --- a/plugins/ecs_examples/src/pbr.rs +++ b/plugins/ecs_examples/src/pbr.rs @@ -7,6 +7,11 @@ use crate::prelude::*; +#[derive(Resource)] +pub struct State { + lut_srv : u32 +} + /// Basic pbr example #[no_mangle] pub fn pbr(client: &mut Client) -> ScheduleInfo { @@ -43,6 +48,12 @@ pub fn setup_pbr( let cubemap_filepath = hotline_rs::get_data_path("textures/cubemap.dds"); let cubemap = image::load_texture_from_file(&mut device.0, &cubemap_filepath, Some(&mut pmfx.shader_heap)).unwrap(); + let brdf_lut_filepath = hotline_rs::get_data_path("textures/luts/ibl_brdf_lut.dds"); + let lut = image::load_texture_from_file(&mut device.0, &brdf_lut_filepath, Some(&mut pmfx.shader_heap)).unwrap(); + + let lut_srv = lut.get_srv_index().unwrap() as u32; + let cubemap_srv = cubemap.get_srv_index().unwrap() as u32; + for y in 0..irc { for x in 0..irc { let iter_pos = start_pos + vec3f(x as f32 * step, y as f32 * step, 0.0); @@ -52,16 +63,24 @@ pub fn setup_pbr( Rotation(Quatf::identity()), Scale(splat3f(size)), WorldMatrix(Mat34f::identity()), - TextureInstance(cubemap.get_srv_index().unwrap() as u32) + TextureInstance(cubemap_srv), )); } } - // spawn entity to keep hold of the texture commands.spawn( TextureComponent(cubemap) ); + commands.spawn( + TextureComponent(lut) + ); + + // ? + commands.insert_resource(State{ + lut_srv + }); + Ok(()) } @@ -71,6 +90,7 @@ pub fn setup_pbr( pub fn render_meshes_pbr( pmfx: &Res, view: &pmfx::View, + state: &Res, mesh_draw_query: Query<(&WorldMatrix, &MeshComponent, &TextureInstance)>) -> Result<(), hotline_rs::Error> { let fmt = view.pass.get_format_hash(); @@ -79,19 +99,26 @@ pub fn render_meshes_pbr( view.cmd_buf.set_render_pipeline(pipeline); view.cmd_buf.push_render_constants(0, 16, 0, gfx::as_u8_slice(&camera.view_projection_matrix)); + view.cmd_buf.push_render_constants(0, 4, 16, gfx::as_u8_slice(&camera.view_position)); view.cmd_buf.set_heap(pipeline, &pmfx.shader_heap); - let mut mip = 0; + let mut i = 0; + let min_roughness = 1.0 / 5.0; for (world_matrix, mesh, cubemap) in &mesh_draw_query { + + let roughness = min_roughness + ((i % 5).as_f32() / 6.0); + let metalness = floor((i / 5).as_f32()) / 5.0; + view.cmd_buf.push_render_constants(1, 12, 0, &world_matrix.0); - view.cmd_buf.push_render_constants(1, 2, 16, gfx::as_u8_slice(&[cubemap.0, mip, 0, 0])); + view.cmd_buf.push_render_constants(1, 4, 12, gfx::as_u8_slice(&[roughness, metalness, 0.0, 0.0])); + view.cmd_buf.push_render_constants(1, 2, 16, gfx::as_u8_slice(&[cubemap.0, state.lut_srv, 0, 0])); view.cmd_buf.set_index_buffer(&mesh.0.ib); view.cmd_buf.set_vertex_buffer(&mesh.0.vb, 0); view.cmd_buf.draw_indexed_instanced(mesh.0.num_indices, 1, 0, 0, 0); - mip += 1; + i += 1; } Ok(())