voxels: extract Pipelines::new helpers under the 500-line limit
- Commit
ee9f4742bc9f7f12a47d85cd0637f793b1d49fef- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 20:37 UTC (4 days ago)
- Parents
3ead4cf
vulkan/pipeline.rs (605 lines) exceeded the 500-line rustFileLength limit because Pipelines::new is one large unsafe method; the established include! split cannot cut inside it. Extract two contiguous statement blocks into pub(super) helpers in a private child module, pipeline/pipeline_helpers.rs, following the map pipelines_finish precedent. create_descriptors takes the device, the UBO and the three view/sampler pairs and returns the descriptor set layout, pipeline layout, pool and set it built; create_post_pipelines takes the shared pipeline state and the post render passes and returns the post layout plus the bright, blur and composite pipelines. Both blocks, their order and their error strings move verbatim; the call sites only forward the values Pipelines::new already held. The only in-block change is qualifying shader-module creation and the SPIR-V constants through the parent module. No public API changes: the helpers are pub(super) and no pub item in pipeline.rs changes. pipeline.rs drops 605 -> 467 lines; the new helper file is 214 lines. Verified: cargo fmt -p voxels_engine -- --check clean, cargo test -p voxels_engine --locked green (252 passed, plus 5 integration tests), and nm -D on libvoxels_engine.so shows the same 55 exported JNI symbols.
Diff
games/voxels/src/main/rust/src/vulkan/pipeline.rs
diff --git a/games/voxels/src/main/rust/src/vulkan/pipeline.rs b/games/voxels/src/main/rust/src/vulkan/pipeline.rsindex a9c6cc79a6..a1fe29201b 100644--- a/games/voxels/src/main/rust/src/vulkan/pipeline.rs+++ b/games/voxels/src/main/rust/src/vulkan/pipeline.rs@@ -1,5 +1,7 @@ use ash::vk; use std::ffi::CString;++mod pipeline_helpers; const BLOCK_VERT_SPV: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../../build/spirv/block.vert.spv")); const BLOCK_FRAG_SPV: &[u8] =@@ -84,100 +86,17 @@ impl Pipelines { entity_view, entity_sampler, } = deps;- let bindings = [- vk::DescriptorSetLayoutBinding::default()- .binding(0)- .descriptor_type(vk::DescriptorType::UNIFORM_BUFFER)- .descriptor_count(1)- .stage_flags(vk::ShaderStageFlags::VERTEX | vk::ShaderStageFlags::FRAGMENT),- vk::DescriptorSetLayoutBinding::default()- .binding(1)- .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER)- .descriptor_count(1)- .stage_flags(vk::ShaderStageFlags::FRAGMENT),- vk::DescriptorSetLayoutBinding::default()- .binding(2)- .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER)- .descriptor_count(1)- .stage_flags(vk::ShaderStageFlags::FRAGMENT),- vk::DescriptorSetLayoutBinding::default()- .binding(3)- .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER)- .descriptor_count(1)- .stage_flags(vk::ShaderStageFlags::FRAGMENT),- ];- let dsl = device- .create_descriptor_set_layout(- &vk::DescriptorSetLayoutCreateInfo::default().bindings(&bindings),- None,- )- .map_err(|e| format!("dsl {e:?}"))?;- let layouts = [dsl];- let layout = device- .create_pipeline_layout(- &vk::PipelineLayoutCreateInfo::default().set_layouts(&layouts),- None,- )- .map_err(|e| format!("layout {e:?}"))?;- let pool_sizes = [- vk::DescriptorPoolSize::default()- .ty(vk::DescriptorType::UNIFORM_BUFFER)- .descriptor_count(2),- vk::DescriptorPoolSize::default()- .ty(vk::DescriptorType::COMBINED_IMAGE_SAMPLER)- .descriptor_count(6),- ];- let pool = device- .create_descriptor_pool(- &vk::DescriptorPoolCreateInfo::default().pool_sizes(&pool_sizes).max_sets(2),- None,- )- .map_err(|e| format!("pool {e:?}"))?;- let sets = device- .allocate_descriptor_sets(- &vk::DescriptorSetAllocateInfo::default()- .descriptor_pool(pool)- .set_layouts(&layouts),- )- .map_err(|e| format!("alloc {e:?}"))?;- let set = sets[0];- let buf_info =- vk::DescriptorBufferInfo::default().buffer(ubo_buffer).offset(0).range(ubo_size);- let img_info = vk::DescriptorImageInfo::default()- .image_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL)- .image_view(atlas_view)- .sampler(atlas_sampler);- let shadow_info = vk::DescriptorImageInfo::default()- .image_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL)- .image_view(shadow_view)- .sampler(shadow_sampler);- let entity_info = vk::DescriptorImageInfo::default()- .image_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL)- .image_view(entity_view)- .sampler(entity_sampler);- let writes = [- vk::WriteDescriptorSet::default()- .dst_set(set)- .dst_binding(0)- .descriptor_type(vk::DescriptorType::UNIFORM_BUFFER)- .buffer_info(std::slice::from_ref(&buf_info)),- vk::WriteDescriptorSet::default()- .dst_set(set)- .dst_binding(1)- .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER)- .image_info(std::slice::from_ref(&img_info)),- vk::WriteDescriptorSet::default()- .dst_set(set)- .dst_binding(2)- .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER)- .image_info(std::slice::from_ref(&shadow_info)),- vk::WriteDescriptorSet::default()- .dst_set(set)- .dst_binding(3)- .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER)- .image_info(std::slice::from_ref(&entity_info)),- ];- device.update_descriptor_sets(&writes, &[]);+ let (dsl, layout, pool, set) = pipeline_helpers::create_descriptors(+ device,+ ubo_buffer,+ ubo_size,+ atlas_view,+ atlas_sampler,+ shadow_view,+ shadow_sampler,+ entity_view,+ entity_sampler,+ )?; let vert_mod = Self::create_module(device, BLOCK_VERT_SPV)?; let frag_mod = Self::create_module(device, BLOCK_FRAG_SPV)?; let entry = CString::new("main").unwrap();@@ -489,76 +408,19 @@ impl Pipelines { device.destroy_shader_module(water_vert_mod, None); device.destroy_shader_module(water_frag_mod, None); - // --- Post-processing pipelines (fullscreen): bloom bright/blur + FXAA composite. ---- let push = vk::PushConstantRange::default()- .stage_flags(vk::ShaderStageFlags::FRAGMENT)- .offset(0)- .size(16);- let post_set_layouts = [post_desc_layout];- let post_layout = device- .create_pipeline_layout(- &vk::PipelineLayoutCreateInfo::default()- .set_layouts(&post_set_layouts)- .push_constant_ranges(std::slice::from_ref(&push)),- None,- )- .map_err(|e| format!("post layout {e:?}"))?;- let post_vert_mod = Self::create_module(device, POST_VERT_SPV)?;- let bright_mod = Self::create_module(device, BRIGHT_FRAG_SPV)?;- let blur_mod = Self::create_module(device, BLUR_FRAG_SPV)?;- let composite_mod = Self::create_module(device, COMPOSITE_FRAG_SPV)?;- let empty_vi = vk::PipelineVertexInputStateCreateInfo::default();- let post_ds = vk::PipelineDepthStencilStateCreateInfo::default()- .depth_test_enable(false)- .depth_write_enable(false);- let post_att = vk::PipelineColorBlendAttachmentState::default()- .color_write_mask(vk::ColorComponentFlags::RGBA)- .blend_enable(false);- let post_cb = vk::PipelineColorBlendStateCreateInfo::default()- .attachments(std::slice::from_ref(&post_att));- let mk_post = |device: &ash::Device,- frag: vk::ShaderModule,- rp: vk::RenderPass|- -> Result<vk::Pipeline, String> {- let stages = [- vk::PipelineShaderStageCreateInfo::default()- .stage(vk::ShaderStageFlags::VERTEX)- .module(post_vert_mod)- .name(&entry),- vk::PipelineShaderStageCreateInfo::default()- .stage(vk::ShaderStageFlags::FRAGMENT)- .module(frag)- .name(&entry),- ];- let pi = vk::GraphicsPipelineCreateInfo::default()- .stages(&stages)- .vertex_input_state(&empty_vi)- .input_assembly_state(&ia)- .viewport_state(&vp_state)- .rasterization_state(&raster)- .multisample_state(&ms)- .depth_stencil_state(&post_ds)- .color_blend_state(&post_cb)- .dynamic_state(&dyn_state)- .layout(post_layout)- .render_pass(rp)- .subpass(0);- let p = device- .create_graphics_pipelines(- vk::PipelineCache::null(),- std::slice::from_ref(&pi),- None,- )- .map_err(|(_, e)| format!("post pipe {e:?}"))?;- Ok(p[0])- };- let bright_pipeline = mk_post(device, bright_mod, post_rp)?;- let blur_pipeline = mk_post(device, blur_mod, post_rp)?;- let composite_pipeline = mk_post(device, composite_mod, composite_rp)?;- device.destroy_shader_module(post_vert_mod, None);- device.destroy_shader_module(bright_mod, None);- device.destroy_shader_module(blur_mod, None);- device.destroy_shader_module(composite_mod, None);+ let (post_layout, bright_pipeline, blur_pipeline, composite_pipeline) =+ pipeline_helpers::create_post_pipelines(+ device,+ &entry,+ &ia,+ &vp_state,+ &raster,+ &ms,+ &dyn_state,+ post_desc_layout,+ post_rp,+ composite_rp,+ )?; Ok(Self { layout,
games/voxels/src/main/rust/src/vulkan/pipeline/pipeline_helpers.rs
diff --git a/games/voxels/src/main/rust/src/vulkan/pipeline/pipeline_helpers.rs b/games/voxels/src/main/rust/src/vulkan/pipeline/pipeline_helpers.rsnew file mode 100644index 0000000000..f440057c61--- /dev/null+++ b/games/voxels/src/main/rust/src/vulkan/pipeline/pipeline_helpers.rs@@ -0,0 +1,214 @@+//! Helpers extracted from `pipeline.rs` (file-length limit): the descriptor+//! setup and the post-processing pipelines for [`Pipelines::new`]. Both blocks+//! move verbatim, in the same order, with the same error paths.++use ash::vk;+use std::ffi::CString;++use super::{BLUR_FRAG_SPV, BRIGHT_FRAG_SPV, COMPOSITE_FRAG_SPV, POST_VERT_SPV};++/// Creates the descriptor set layout, pipeline layout, descriptor pool and+/// the single descriptor set, writing the UBO and the three combined image+/// samplers into it.+///+/// # Safety+///+/// Same contract as [`super::Pipelines::new`]: the device and every handle+/// passed in must be valid and belong to the same device.+#[allow(clippy::too_many_arguments)]+pub(super) unsafe fn create_descriptors(+ device: &ash::Device,+ ubo_buffer: vk::Buffer,+ ubo_size: u64,+ atlas_view: vk::ImageView,+ atlas_sampler: vk::Sampler,+ shadow_view: vk::ImageView,+ shadow_sampler: vk::Sampler,+ entity_view: vk::ImageView,+ entity_sampler: vk::Sampler,+) -> Result<+ (vk::DescriptorSetLayout, vk::PipelineLayout, vk::DescriptorPool, vk::DescriptorSet),+ String,+> {+ let bindings = [+ vk::DescriptorSetLayoutBinding::default()+ .binding(0)+ .descriptor_type(vk::DescriptorType::UNIFORM_BUFFER)+ .descriptor_count(1)+ .stage_flags(vk::ShaderStageFlags::VERTEX | vk::ShaderStageFlags::FRAGMENT),+ vk::DescriptorSetLayoutBinding::default()+ .binding(1)+ .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER)+ .descriptor_count(1)+ .stage_flags(vk::ShaderStageFlags::FRAGMENT),+ vk::DescriptorSetLayoutBinding::default()+ .binding(2)+ .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER)+ .descriptor_count(1)+ .stage_flags(vk::ShaderStageFlags::FRAGMENT),+ vk::DescriptorSetLayoutBinding::default()+ .binding(3)+ .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER)+ .descriptor_count(1)+ .stage_flags(vk::ShaderStageFlags::FRAGMENT),+ ];+ let dsl = device+ .create_descriptor_set_layout(+ &vk::DescriptorSetLayoutCreateInfo::default().bindings(&bindings),+ None,+ )+ .map_err(|e| format!("dsl {e:?}"))?;+ let layouts = [dsl];+ let layout = device+ .create_pipeline_layout(+ &vk::PipelineLayoutCreateInfo::default().set_layouts(&layouts),+ None,+ )+ .map_err(|e| format!("layout {e:?}"))?;+ let pool_sizes = [+ vk::DescriptorPoolSize::default()+ .ty(vk::DescriptorType::UNIFORM_BUFFER)+ .descriptor_count(2),+ vk::DescriptorPoolSize::default()+ .ty(vk::DescriptorType::COMBINED_IMAGE_SAMPLER)+ .descriptor_count(6),+ ];+ let pool = device+ .create_descriptor_pool(+ &vk::DescriptorPoolCreateInfo::default().pool_sizes(&pool_sizes).max_sets(2),+ None,+ )+ .map_err(|e| format!("pool {e:?}"))?;+ let sets = device+ .allocate_descriptor_sets(+ &vk::DescriptorSetAllocateInfo::default().descriptor_pool(pool).set_layouts(&layouts),+ )+ .map_err(|e| format!("alloc {e:?}"))?;+ let set = sets[0];+ let buf_info = vk::DescriptorBufferInfo::default().buffer(ubo_buffer).offset(0).range(ubo_size);+ let img_info = vk::DescriptorImageInfo::default()+ .image_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL)+ .image_view(atlas_view)+ .sampler(atlas_sampler);+ let shadow_info = vk::DescriptorImageInfo::default()+ .image_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL)+ .image_view(shadow_view)+ .sampler(shadow_sampler);+ let entity_info = vk::DescriptorImageInfo::default()+ .image_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL)+ .image_view(entity_view)+ .sampler(entity_sampler);+ let writes = [+ vk::WriteDescriptorSet::default()+ .dst_set(set)+ .dst_binding(0)+ .descriptor_type(vk::DescriptorType::UNIFORM_BUFFER)+ .buffer_info(std::slice::from_ref(&buf_info)),+ vk::WriteDescriptorSet::default()+ .dst_set(set)+ .dst_binding(1)+ .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER)+ .image_info(std::slice::from_ref(&img_info)),+ vk::WriteDescriptorSet::default()+ .dst_set(set)+ .dst_binding(2)+ .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER)+ .image_info(std::slice::from_ref(&shadow_info)),+ vk::WriteDescriptorSet::default()+ .dst_set(set)+ .dst_binding(3)+ .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER)+ .image_info(std::slice::from_ref(&entity_info)),+ ];+ device.update_descriptor_sets(&writes, &[]);+ Ok((dsl, layout, pool, set))+}++/// Creates the post pipeline layout and the bright, blur and composite+/// fullscreen pipelines, destroying their shader modules afterwards.+///+/// # Safety+///+/// Same contract as [`super::Pipelines::new`]: the device and every handle+/// passed in must be valid and belong to the same device.+#[allow(clippy::too_many_arguments)]+pub(super) unsafe fn create_post_pipelines(+ device: &ash::Device,+ entry: &CString,+ ia: &vk::PipelineInputAssemblyStateCreateInfo,+ vp_state: &vk::PipelineViewportStateCreateInfo,+ raster: &vk::PipelineRasterizationStateCreateInfo,+ ms: &vk::PipelineMultisampleStateCreateInfo,+ dyn_state: &vk::PipelineDynamicStateCreateInfo,+ post_desc_layout: vk::DescriptorSetLayout,+ post_rp: vk::RenderPass,+ composite_rp: vk::RenderPass,+) -> Result<(vk::PipelineLayout, vk::Pipeline, vk::Pipeline, vk::Pipeline), String> {+ // --- Post-processing pipelines (fullscreen): bloom bright/blur + FXAA composite. ---+ let push = vk::PushConstantRange::default()+ .stage_flags(vk::ShaderStageFlags::FRAGMENT)+ .offset(0)+ .size(16);+ let post_set_layouts = [post_desc_layout];+ let post_layout = device+ .create_pipeline_layout(+ &vk::PipelineLayoutCreateInfo::default()+ .set_layouts(&post_set_layouts)+ .push_constant_ranges(std::slice::from_ref(&push)),+ None,+ )+ .map_err(|e| format!("post layout {e:?}"))?;+ let post_vert_mod = super::Pipelines::create_module(device, POST_VERT_SPV)?;+ let bright_mod = super::Pipelines::create_module(device, BRIGHT_FRAG_SPV)?;+ let blur_mod = super::Pipelines::create_module(device, BLUR_FRAG_SPV)?;+ let composite_mod = super::Pipelines::create_module(device, COMPOSITE_FRAG_SPV)?;+ let empty_vi = vk::PipelineVertexInputStateCreateInfo::default();+ let post_ds = vk::PipelineDepthStencilStateCreateInfo::default()+ .depth_test_enable(false)+ .depth_write_enable(false);+ let post_att = vk::PipelineColorBlendAttachmentState::default()+ .color_write_mask(vk::ColorComponentFlags::RGBA)+ .blend_enable(false);+ let post_cb = vk::PipelineColorBlendStateCreateInfo::default()+ .attachments(std::slice::from_ref(&post_att));+ let mk_post = |device: &ash::Device,+ frag: vk::ShaderModule,+ rp: vk::RenderPass|+ -> Result<vk::Pipeline, String> {+ let stages = [+ vk::PipelineShaderStageCreateInfo::default()+ .stage(vk::ShaderStageFlags::VERTEX)+ .module(post_vert_mod)+ .name(&entry),+ vk::PipelineShaderStageCreateInfo::default()+ .stage(vk::ShaderStageFlags::FRAGMENT)+ .module(frag)+ .name(&entry),+ ];+ let pi = vk::GraphicsPipelineCreateInfo::default()+ .stages(&stages)+ .vertex_input_state(&empty_vi)+ .input_assembly_state(&ia)+ .viewport_state(&vp_state)+ .rasterization_state(&raster)+ .multisample_state(&ms)+ .depth_stencil_state(&post_ds)+ .color_blend_state(&post_cb)+ .dynamic_state(&dyn_state)+ .layout(post_layout)+ .render_pass(rp)+ .subpass(0);+ let p = device+ .create_graphics_pipelines(vk::PipelineCache::null(), std::slice::from_ref(&pi), None)+ .map_err(|(_, e)| format!("post pipe {e:?}"))?;+ Ok(p[0])+ };+ let bright_pipeline = mk_post(device, bright_mod, post_rp)?;+ let blur_pipeline = mk_post(device, blur_mod, post_rp)?;+ let composite_pipeline = mk_post(device, composite_mod, composite_rp)?;+ device.destroy_shader_module(post_vert_mod, None);+ device.destroy_shader_module(bright_mod, None);+ device.destroy_shader_module(blur_mod, None);+ device.destroy_shader_module(composite_mod, None);+ Ok((post_layout, bright_pipeline, blur_pipeline, composite_pipeline))+}