Skip to content

Commit

Permalink
- pbr sample working with some issues still to investgate
Browse files Browse the repository at this point in the history
  • Loading branch information
polymonster committed Oct 29, 2023
1 parent d4d7b2b commit 43163cd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
37 changes: 32 additions & 5 deletions plugins/ecs_examples/src/pbr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<gfx_platform::Device, os_platform::App>) -> ScheduleInfo {
Expand Down Expand Up @@ -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);
Expand All @@ -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(())
}

Expand All @@ -71,6 +90,7 @@ pub fn setup_pbr(
pub fn render_meshes_pbr(
pmfx: &Res<PmfxRes>,
view: &pmfx::View<gfx_platform::Device>,
state: &Res<State>,
mesh_draw_query: Query<(&WorldMatrix, &MeshComponent, &TextureInstance)>) -> Result<(), hotline_rs::Error> {

let fmt = view.pass.get_format_hash();
Expand All @@ -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(())
Expand Down

0 comments on commit 43163cd

Please sign in to comment.