diff options
Diffstat (limited to 'adapters/fparkan-render-vulkan/src/ffi/smoke.rs')
| -rw-r--r-- | adapters/fparkan-render-vulkan/src/ffi/smoke.rs | 127 |
1 files changed, 56 insertions, 71 deletions
diff --git a/adapters/fparkan-render-vulkan/src/ffi/smoke.rs b/adapters/fparkan-render-vulkan/src/ffi/smoke.rs index 9c6b9f11..2111e6ff 100644 --- a/adapters/fparkan-render-vulkan/src/ffi/smoke.rs +++ b/adapters/fparkan-render-vulkan/src/ffi/smoke.rs @@ -23,6 +23,10 @@ use super::{ use crate::policy::KHR_PORTABILITY_SUBSET_EXTENSION; use crate::shader::validate_triangle_shaders; +#[path = "preview_mesh.rs"] +mod preview_mesh; +use preview_mesh::preview_rect; + #[cfg(test)] fn take_runtime_owners_in_dependency_order<Instance, Validation, Surface, Device, Swapchain>( instance: &mut Option<Instance>, @@ -434,6 +438,7 @@ impl VulkanSmokeRenderer { frame_uniforms, cockpit_camera: None, model_preview: None, + preview_mesh: None, point_lights: Vec::new(), point_lights_explicit: false, frame_sync: Vec::new(), @@ -612,40 +617,6 @@ impl VulkanSmokeRenderer { Ok(()) } - /// Selects the model to draw over the interface with its own camera and - /// depth buffer region. Only the main pass draws these ranges. - pub fn set_model_preview( - &mut self, - preview: Option<super::VulkanModelPreview>, - ) -> Result<(), VulkanSmokeRendererError> { - if let Some(model) = &preview { - let [left, top, right, bottom] = model.viewport; - if !model.camera.is_finite() || !model.eye.iter().all(|value| value.is_finite()) { - return Err(VulkanSmokeRendererError::InvalidStaticCamera { - context: "non-finite model preview camera", - }); - } - if model.ranges.is_empty() || model.ranges.end > self.draw_ranges.len() { - return Err(VulkanSmokeRendererError::InvalidStaticMesh { - context: "invalid model preview draw ranges", - }); - } - if !(left >= 0.0 - && top >= 0.0 - && right <= 1.0 - && bottom <= 1.0 - && left < right - && top < bottom) - { - return Err(VulkanSmokeRendererError::InvalidStaticCamera { - context: "invalid model preview viewport", - }); - } - } - self.model_preview = preview; - Ok(()) - } - /// Replaces lighting, fog, camera-position and matrix state for /// subsequent frames without recreating GPU resources. /// @@ -758,7 +729,10 @@ impl VulkanSmokeRenderer { validate_geometry_upload( vertices, indices, - &self.draw_ranges, + &self.draw_ranges[..self + .preview_mesh + .as_ref() + .map_or(self.draw_ranges.len(), |mesh| mesh.range_start)], geometry_ranges, self.vertex_count, self.index_capacity, @@ -1445,16 +1419,6 @@ impl VulkanSmokeRenderer { }) } - fn textures_ref(&self) -> Result<&[super::VulkanAllocatedImage], VulkanSmokeRendererError> { - if self.textures.is_empty() { - Err(VulkanSmokeRendererError::InvariantViolation { - context: "static material textures", - }) - } else { - Ok(&self.textures) - } - } - fn surface_ref(&self) -> Result<&VulkanSurfaceProbe, VulkanSmokeRendererError> { self.surface .as_ref() @@ -1844,6 +1808,7 @@ impl VulkanSmokeRenderer { ) -> Result<(), VulkanSmokeRendererError> { let device = self.device_ref()?; let swapchain = self.swapchain_ref()?; + let (views, slots) = self.material_image_views(self.preview_mesh.as_ref()); let resources = RollbackOnDrop::new( create_swapchain_resources( self.instance_ref()?, @@ -1853,8 +1818,8 @@ impl VulkanSmokeRenderer { self.pipeline_cache, self.vertex_buffer_ref()?, self.index_buffer_ref()?, - self.textures_ref()?, - &self.texture_slot_indices, + &views, + &slots, &self.draw_range_base_pipeline_states, self.draw_ranges .iter() @@ -2034,13 +1999,28 @@ impl VulkanSmokeRenderer { .as_ref() .map(|preview| preview_rect(extent, preview.viewport)); let mut preview_depth_cleared = false; + let mut bound_uploaded_mesh = false; + let uploaded = self.preview_mesh.as_ref(); + let inserted = self.model_preview.as_ref().filter(|_| !reflected); + let ordered = self + .draw_order + .iter() + .copied() + .filter(|&index| uploaded.is_none_or(|mesh| index < mesh.range_start)) + .flat_map(|index| { + inserted + .filter(|preview| preview.draw_before == Some(index)) + .map(|preview| preview.ranges.clone()) + .into_iter() + .flatten() + .chain(std::iter::once(index)) + }); // Native reflection queues the translucent water after the reflected world. let draw_order = - self.draw_order - .iter() - .copied() + ordered + .clone() .filter(|&index| !reflected || !self.draw_ranges[index].terrain_reflection) - .chain(self.draw_order.iter().copied().filter(|&index| { + .chain(ordered.filter(|&index| { reflected && self.draw_ranges[index].terrain_reflection })); for range_index in draw_order { @@ -2072,6 +2052,28 @@ impl VulkanSmokeRenderer { if reflected && mode & 8 != 0 { continue; } + let use_uploaded = uploaded.is_some_and(|mesh| range_index >= mesh.range_start); + if use_uploaded != bound_uploaded_mesh { + let (vertex, index) = if let Some(mesh) = uploaded.filter(|_| use_uploaded) + { + (&mesh.vertex, &mesh.index) + } else { + (self.vertex_buffer_ref()?, self.index_buffer_ref()?) + }; + device.device().cmd_bind_vertex_buffers( + command_buffer, + 0, + &[vertex.buffer], + &offsets, + ); + device.device().cmd_bind_index_buffer( + command_buffer, + index.buffer, + 0, + vk::IndexType::UINT32, + ); + bound_uploaded_mesh = use_uploaded; + } let state = if reflected { super::reflection::reflected_draw_pipeline_state(range, shifted_reflection) } else { @@ -2428,6 +2430,9 @@ impl VulkanSmokeRenderer { fn destroy_device_owned_resources(&mut self) { self.destroy_swapchain_resources(); if let Some(device) = self.device.as_ref() { + if let Some(mesh) = self.preview_mesh.take() { + mesh.destroy(device); + } if self.pipeline_cache != vk::PipelineCache::null() { // SAFETY: All pipeline creation has finished on this device. unsafe { @@ -2827,26 +2832,6 @@ fn world_material_pipeline_states( /// compacted into world positions and sorted transparent ranges are inserted /// after the last active background; fixed passes keep their relative order but /// can shift slots. Zero-index-count ranges are inactive and are omitted. -fn preview_rect((width, height): (u32, u32), viewport: [f32; 4]) -> vk::Rect2D { - let [left, top, right, bottom] = viewport; - let x = (left * width as f32).round() as u32; - let y = (top * height as f32).round() as u32; - let x = x.min(width - 1); - let y = y.min(height - 1); - let right = ((right * width as f32).round() as u32).clamp(x + 1, width); - let bottom = ((bottom * height as f32).round() as u32).clamp(y + 1, height); - vk::Rect2D { - offset: vk::Offset2D { - x: x as i32, - y: y as i32, - }, - extent: vk::Extent2D { - width: right - x, - height: bottom - y, - }, - } -} - fn sorted_world_draw_order( range_count: usize, ranges: &[super::VulkanStaticDrawRange], |
