Skip to content

Commit

Permalink
Refactoring rendering code.
Browse files Browse the repository at this point in the history
  • Loading branch information
facundo-villa committed Jan 23, 2024
1 parent b6fee0f commit b56d007
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 192 deletions.
60 changes: 32 additions & 28 deletions src/ghi/graphics_hardware_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,19 @@ pub enum DataTypes {

#[derive(Hash)]
pub struct VertexElement {
pub name: String,
pub format: DataTypes,
pub binding: u32,
pub(crate) name: String,
pub(crate) format: DataTypes,
pub(crate) binding: u32,
}

impl VertexElement {
pub fn new(name: &str, format: DataTypes, binding: u32) -> Self {
Self {
name: name.to_string(),
format,
binding,
}
}
}

bitflags::bitflags! {
Expand Down Expand Up @@ -980,38 +990,32 @@ pub struct BufferDescriptor {
pub slot: u32,
}

pub trait SpecializationMapEntry {
fn get_constant_id(&self) -> u32;
fn get_size(&self) -> usize;
fn get_data(&self) -> &[u8];
fn get_type(&self) -> String;
}

pub struct GenericSpecializationMapEntry<T> {
pub struct SpecializationMapEntry {
pub r#type: String,
pub constant_id: u32,
pub value: T,
pub value: Box<dyn std::any::Any>,
}

impl <T> SpecializationMapEntry for GenericSpecializationMapEntry<T> {
fn get_constant_id(&self) -> u32 {
impl SpecializationMapEntry {
pub fn get_constant_id(&self) -> u32 {
self.constant_id
}

fn get_type(&self) -> String {
pub fn get_type(&self) -> String {
self.r#type.clone()
}

fn get_size(&self) -> usize {
std::mem::size_of::<T>()
pub fn get_size(&self) -> usize {
std::mem::size_of_val(&self.value)
}

fn get_data(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(&self.value as *const T as *const u8, std::mem::size_of::<T>()) }
pub fn get_data(&self) -> &[u8] {
// SAFETY: We know that the data is valid for the lifetime of the specialization map entry.
unsafe { std::slice::from_raw_parts(&self.value as *const _ as *const u8, self.get_size()) }
}
}

pub type ShaderParameter<'a> = (&'a ShaderHandle, ShaderTypes, Vec<Box<dyn SpecializationMapEntry>>);
pub type ShaderParameter<'a> = (&'a ShaderHandle, ShaderTypes, &'a [SpecializationMapEntry]);

pub enum PipelineConfigurationBlocks<'a> {
VertexInput {
Expand All @@ -1024,7 +1028,7 @@ pub enum PipelineConfigurationBlocks<'a> {
targets: &'a [AttachmentInformation],
},
Shaders {
shaders: &'a [(&'a ShaderHandle, ShaderTypes, Vec<Box<dyn SpecializationMapEntry>>)],
shaders: &'a [ShaderParameter<'a>],
},
Layout {
layout: &'a PipelineLayoutHandle,
Expand Down Expand Up @@ -1148,7 +1152,7 @@ pub(super) mod tests {

let pipeline = renderer.create_raster_pipeline(&[
PipelineConfigurationBlocks::Layout { layout: &pipeline_layout },
PipelineConfigurationBlocks::Shaders { shaders: &[(&vertex_shader, ShaderTypes::Vertex, vec![]), (&fragment_shader, ShaderTypes::Fragment, vec![])], },
PipelineConfigurationBlocks::Shaders { shaders: &[(&vertex_shader, ShaderTypes::Vertex, &[]), (&fragment_shader, ShaderTypes::Fragment, &[])], },
PipelineConfigurationBlocks::VertexInput { vertex_elements: &vertex_layout, },
PipelineConfigurationBlocks::RenderTargets { targets: &attachments },
]);
Expand Down Expand Up @@ -1284,7 +1288,7 @@ pub(super) mod tests {

let pipeline = renderer.create_raster_pipeline(&[
PipelineConfigurationBlocks::Layout { layout: &pipeline_layout },
PipelineConfigurationBlocks::Shaders { shaders: &[(&vertex_shader, ShaderTypes::Vertex, vec![]), (&fragment_shader, ShaderTypes::Fragment, vec![])], },
PipelineConfigurationBlocks::Shaders { shaders: &[(&vertex_shader, ShaderTypes::Vertex, &[]), (&fragment_shader, ShaderTypes::Fragment, &[])], },
PipelineConfigurationBlocks::VertexInput { vertex_elements: &vertex_layout, },
PipelineConfigurationBlocks::RenderTargets { targets: &attachments },
]);
Expand Down Expand Up @@ -1412,7 +1416,7 @@ pub(super) mod tests {

let pipeline = renderer.create_raster_pipeline(&[
PipelineConfigurationBlocks::Layout { layout: &pipeline_layout },
PipelineConfigurationBlocks::Shaders { shaders: &[(&vertex_shader, ShaderTypes::Vertex, vec![]), (&fragment_shader, ShaderTypes::Fragment, vec![])], },
PipelineConfigurationBlocks::Shaders { shaders: &[(&vertex_shader, ShaderTypes::Vertex, &[]), (&fragment_shader, ShaderTypes::Fragment, &[])], },
PipelineConfigurationBlocks::VertexInput { vertex_elements: &vertex_layout, },
PipelineConfigurationBlocks::RenderTargets { targets: &attachments },
]);
Expand Down Expand Up @@ -1542,7 +1546,7 @@ pub(super) mod tests {

let pipeline = renderer.create_raster_pipeline(&[
PipelineConfigurationBlocks::Layout { layout: &pipeline_layout },
PipelineConfigurationBlocks::Shaders { shaders: &[(&vertex_shader, ShaderTypes::Vertex, vec![]), (&fragment_shader, ShaderTypes::Fragment, vec![])], },
PipelineConfigurationBlocks::Shaders { shaders: &[(&vertex_shader, ShaderTypes::Vertex, &[]), (&fragment_shader, ShaderTypes::Fragment, &[])], },
PipelineConfigurationBlocks::VertexInput { vertex_elements: &vertex_layout, },
PipelineConfigurationBlocks::RenderTargets { targets: &attachments },
]);
Expand Down Expand Up @@ -1679,7 +1683,7 @@ pub(super) mod tests {

let pipeline = renderer.create_raster_pipeline(&[
PipelineConfigurationBlocks::Layout { layout: &pipeline_layout },
PipelineConfigurationBlocks::Shaders { shaders: &[(&vertex_shader, ShaderTypes::Vertex, vec![]), (&fragment_shader, ShaderTypes::Fragment, vec![])], },
PipelineConfigurationBlocks::Shaders { shaders: &[(&vertex_shader, ShaderTypes::Vertex, &[]), (&fragment_shader, ShaderTypes::Fragment, &[])], },
PipelineConfigurationBlocks::VertexInput { vertex_elements: &vertex_layout, },
PipelineConfigurationBlocks::RenderTargets { targets: &attachments },
]);
Expand Down Expand Up @@ -1878,7 +1882,7 @@ pub(super) mod tests {

let pipeline = renderer.create_raster_pipeline(&[
PipelineConfigurationBlocks::Layout { layout: &pipeline_layout },
PipelineConfigurationBlocks::Shaders { shaders: &[(&vertex_shader, ShaderTypes::Vertex, vec![]), (&fragment_shader, ShaderTypes::Fragment, vec![])], },
PipelineConfigurationBlocks::Shaders { shaders: &[(&vertex_shader, ShaderTypes::Vertex, &[]), (&fragment_shader, ShaderTypes::Fragment, &[])], },
PipelineConfigurationBlocks::VertexInput { vertex_elements: &vertex_layout, },
PipelineConfigurationBlocks::RenderTargets { targets: &attachments },
]);
Expand Down Expand Up @@ -2091,7 +2095,7 @@ void main() {

let pipeline = renderer.create_ray_tracing_pipeline(
&pipeline_layout,
&[(&raygen_shader, ShaderTypes::RayGen, vec![]), (&closest_hit_shader, ShaderTypes::ClosestHit, vec![]), (&miss_shader, ShaderTypes::Miss, vec![])],
&[(&raygen_shader, ShaderTypes::RayGen, &[]), (&closest_hit_shader, ShaderTypes::ClosestHit, &[]), (&miss_shader, ShaderTypes::Miss, &[])],
);

let building_command_buffer_handle = renderer.create_command_buffer(None);
Expand Down
2 changes: 1 addition & 1 deletion src/ghi/vulkan_ghi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2363,7 +2363,7 @@ impl VulkanGHI {
.map(move |stage| {
let entries_offset = entry_count;

for entry in &stage.2 {
for entry in stage.2.iter() {
specialization_entries_buffer.extend_from_slice(entry.get_data());

entries[entry_count] = vk::SpecializationMapEntry::default()
Expand Down
2 changes: 1 addition & 1 deletion src/rendering/aces_tonemap_render_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl AcesToneMapPass {
ghi::ShaderBindingDescriptor::new(0, 1, ghi::AccessPolicies::WRITE),
]);

let tone_mapping_pipeline = ghi.create_compute_pipeline(&pipeline_layout, (&tone_mapping_shader, ghi::ShaderTypes::Compute, vec![]));
let tone_mapping_pipeline = ghi.create_compute_pipeline(&pipeline_layout, (&tone_mapping_shader, ghi::ShaderTypes::Compute, &[]));

AcesToneMapPass {
descriptor_set_layout,
Expand Down
8 changes: 2 additions & 6 deletions src/rendering/shadow_render_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl ShadowRenderingPass {

let pipeline = ghi.create_raster_pipeline(&[
ghi::PipelineConfigurationBlocks::Layout { layout: &pipeline_layout },
ghi::PipelineConfigurationBlocks::Shaders { shaders: &[(&mesh_shader, ghi::ShaderTypes::Mesh, vec![])], },
ghi::PipelineConfigurationBlocks::Shaders { shaders: &[(&mesh_shader, ghi::ShaderTypes::Mesh, &[])], },
ghi::PipelineConfigurationBlocks::RenderTargets { targets: &[ghi::AttachmentInformation::new(shadow_map, ghi::Formats::Depth32, ghi::Layouts::RenderTarget, ghi::ClearValue::Depth(0.0f32), false, true)] },
]);

Expand All @@ -93,7 +93,7 @@ impl ShadowRenderingPass {
ghi::ShaderBindingDescriptor::new(1, 3, ghi::AccessPolicies::WRITE),
]);

let occlusion_map_build_pipeline = ghi.create_compute_pipeline(&pipeline_layout, (&occlusion_map_shader, ghi::ShaderTypes::Compute, vec![]));
let occlusion_map_build_pipeline = ghi.create_compute_pipeline(&pipeline_layout, (&occlusion_map_shader, ghi::ShaderTypes::Compute, &[]));

ShadowRenderingPass { pipeline, pipeline_layout, descriptor_set, shadow_map, occlusion_map_build_pipeline }
}
Expand All @@ -107,10 +107,6 @@ impl ShadowRenderingPass {
pipeline.dispatch_meshes(192, 1, 1);
render_pass.end_render_pass();

let occlusion_map_build_pipeline = command_buffer_recording.bind_compute_pipeline(&self.occlusion_map_build_pipeline);
occlusion_map_build_pipeline.bind_descriptor_sets(&self.pipeline_layout, &[render_domain.get_descriptor_set(), self.descriptor_set]);
occlusion_map_build_pipeline.dispatch(ghi::DispatchExtent::new(Extent::rectangle(1920, 1080), Extent::square(32)));

command_buffer_recording.end_region();
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/rendering/ssao_render_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl ScreenSpaceAmbientOcclusionPass {
ghi::ShaderBindingDescriptor::new(1, 2, ghi::AccessPolicies::WRITE),
]);

let pipeline = ghi.create_compute_pipeline(&pipeline_layout, (&shader, ghi::ShaderTypes::Compute, vec![]));
let pipeline = ghi.create_compute_pipeline(&pipeline_layout, (&shader, ghi::ShaderTypes::Compute, &[]));

let result = ghi.create_image(Some("HBAO Result"), Extent::new(1920, 1080, 1), ghi::Formats::RGBA16(ghi::Encodings::IEEE754), None, ghi::Uses::Storage | ghi::Uses::Image, ghi::DeviceAccesses::GpuWrite | ghi::DeviceAccesses::GpuRead, ghi::UseCases::DYNAMIC);
let x_blur_target = ghi.create_image(Some("X Blur"), Extent::new(1920, 1080, 1), ghi::Formats::RGBA16(ghi::Encodings::IEEE754), None, ghi::Uses::Storage | ghi::Uses::Image, ghi::DeviceAccesses::GpuWrite | ghi::DeviceAccesses::GpuRead, ghi::UseCases::DYNAMIC);
Expand Down Expand Up @@ -77,8 +77,8 @@ impl ScreenSpaceAmbientOcclusionPass {
ghi::ShaderBindingDescriptor::new(1, 2, ghi::AccessPolicies::WRITE),
]);

let blur_x_pipeline = ghi.create_compute_pipeline(&pipeline_layout, (&blur_shader, ghi::ShaderTypes::Compute, vec![Box::new(ghi::GenericSpecializationMapEntry{ constant_id: 0 as u32, r#type: "vec2f".to_string(), value: [1f32, 0f32,] })]));
let blur_y_pipeline = ghi.create_compute_pipeline(&pipeline_layout, (&blur_shader, ghi::ShaderTypes::Compute, vec![Box::new(ghi::GenericSpecializationMapEntry{ constant_id: 0 as u32, r#type: "vec2f".to_string(), value: [0f32, 1f32,] })]));
let blur_x_pipeline = ghi.create_compute_pipeline(&pipeline_layout, (&blur_shader, ghi::ShaderTypes::Compute, &[ghi::SpecializationMapEntry{ constant_id: 0 as u32, r#type: "vec2f".to_string(), value: Box::new([1f32, 0f32,]) }]));
let blur_y_pipeline = ghi.create_compute_pipeline(&pipeline_layout, (&blur_shader, ghi::ShaderTypes::Compute, &[ghi::SpecializationMapEntry{ constant_id: 0 as u32, r#type: "vec2f".to_string(), value: Box::new([0f32, 1f32,]) }]));

ScreenSpaceAmbientOcclusionPass {
pipeline_layout,
Expand Down
Loading

0 comments on commit b56d007

Please sign in to comment.