2025-03-01 17:05:27 +00:00
|
|
|
mod vertex;
|
|
|
|
|
2025-02-28 14:13:19 +00:00
|
|
|
use std::{path::PathBuf, sync::Arc};
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
use ecs::*;
|
2025-03-01 17:05:27 +00:00
|
|
|
use engine::prelude::{shader_type::*, *};
|
|
|
|
use plexus::primitive::{
|
|
|
|
cube::{Bounds, Cube},
|
|
|
|
decompose::Triangulate,
|
|
|
|
generate::PolygonsWithPosition,
|
|
|
|
};
|
|
|
|
use vertex::VertexPoint;
|
2025-02-28 14:13:19 +00:00
|
|
|
|
|
|
|
pub struct SkyBoxImages {
|
|
|
|
left: PathBuf,
|
|
|
|
right: PathBuf,
|
|
|
|
front: PathBuf,
|
|
|
|
back: PathBuf,
|
|
|
|
top: PathBuf,
|
|
|
|
bottom: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: ExactSizeIterator<Item = PathBuf>> From<T> for SkyBoxImages {
|
|
|
|
fn from(mut paths: T) -> Self {
|
|
|
|
debug_assert_eq!(paths.len(), 6);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
left: paths.next().unwrap(),
|
|
|
|
right: paths.next().unwrap(),
|
|
|
|
front: paths.next().unwrap(),
|
|
|
|
back: paths.next().unwrap(),
|
|
|
|
top: paths.next().unwrap(),
|
|
|
|
bottom: paths.next().unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SkyBox {
|
2025-03-01 17:05:27 +00:00
|
|
|
_cube_map: Arc<Image>,
|
|
|
|
cube_buffer: Arc<Buffer<VertexPoint>>,
|
|
|
|
|
|
|
|
vertex_shader: Arc<ShaderModule<Vertex>>,
|
|
|
|
fragment_shader: Arc<ShaderModule<Fragment>>,
|
|
|
|
|
|
|
|
render_target: TargetMode<RenderTarget>,
|
|
|
|
pipeline: TargetMode<Arc<Pipeline>>,
|
|
|
|
descriptor_set: Arc<DescriptorSet>,
|
2025-02-28 14:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SkyBox {
|
2025-03-01 05:43:44 +00:00
|
|
|
pub fn new(world: &mut WorldBuilder, images: impl Into<SkyBoxImages>) -> Result<()> {
|
2025-03-01 17:05:27 +00:00
|
|
|
let sample_count = world
|
|
|
|
.resources
|
|
|
|
.get::<EngineSettings>()
|
|
|
|
.graphics_info()?
|
|
|
|
.sample_count;
|
|
|
|
|
|
|
|
let context = world.resources.get_mut_unchecked::<Context>();
|
2025-03-01 05:43:44 +00:00
|
|
|
context.render_core_mut().add_render_routine::<Self>(1);
|
2025-02-28 14:13:19 +00:00
|
|
|
|
2025-03-01 17:05:27 +00:00
|
|
|
let images = images.into();
|
2025-02-28 14:13:19 +00:00
|
|
|
let cube_map = Image::cube_map([
|
|
|
|
images.left.try_into()?,
|
|
|
|
images.right.try_into()?,
|
|
|
|
images.front.try_into()?,
|
|
|
|
images.back.try_into()?,
|
|
|
|
images.top.try_into()?,
|
|
|
|
images.bottom.try_into()?,
|
|
|
|
])?
|
2025-03-01 09:16:52 +00:00
|
|
|
.format(VK_FORMAT_R8G8B8A8_UNORM)
|
2025-02-28 14:13:19 +00:00
|
|
|
.max_mip_map_levels()
|
|
|
|
.attach_pretty_sampler(context.device())?
|
|
|
|
.build(context.device(), context.queue())?;
|
|
|
|
|
2025-03-01 17:05:27 +00:00
|
|
|
let descriptor_set_layout = DescriptorSetLayout::builder()
|
|
|
|
.add_layout_binding(
|
|
|
|
0,
|
|
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
|
|
VK_SHADER_STAGE_VERTEX_BIT,
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
.add_layout_binding(
|
|
|
|
1,
|
|
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
|
|
VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
.build(context.device().clone())?;
|
|
|
|
|
|
|
|
let render_target = Self::create_render_target(context, sample_count)?;
|
|
|
|
|
|
|
|
let pipeline_layout = PipelineLayout::builder()
|
|
|
|
.add_descriptor_set_layout(&descriptor_set_layout)
|
|
|
|
.build(context.device().clone())?;
|
|
|
|
|
|
|
|
let pipeline =
|
|
|
|
Self::create_pipeline(context, sample_count, &render_target, &pipeline_layout)?;
|
|
|
|
|
|
|
|
let descriptor_pool = DescriptorPool::builder()
|
|
|
|
.set_layout(descriptor_set_layout)
|
|
|
|
.build(context.device().clone())?;
|
|
|
|
let descriptor_set = descriptor_pool.prepare_set().allocate()?;
|
|
|
|
|
|
|
|
let scene = world.resources.get::<Scene>();
|
|
|
|
let view = scene.view();
|
|
|
|
|
|
|
|
descriptor_set.update(&[
|
|
|
|
DescriptorWrite::uniform_buffers(0, &[view.buffer()]),
|
|
|
|
DescriptorWrite::combined_samplers(1, &[&cube_map]),
|
|
|
|
])?;
|
|
|
|
|
|
|
|
let cube_mesh = Cube::new()
|
|
|
|
.polygons_with_position_from(Bounds::unit_radius())
|
|
|
|
.triangulate()
|
|
|
|
.map(|d| {
|
|
|
|
vec![
|
|
|
|
[d.a.0.into_inner(), d.a.1.into_inner(), d.a.2.into_inner()],
|
|
|
|
[d.b.0.into_inner(), d.b.1.into_inner(), d.b.2.into_inner()],
|
|
|
|
[d.c.0.into_inner(), d.c.1.into_inner(), d.c.2.into_inner()],
|
|
|
|
]
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.map(|t| VertexPoint::new(t[0] as f32, t[1] as f32, t[2] as f32))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let cube_buffer = Buffer::builder()
|
|
|
|
.set_memory_usage(MemoryUsage::GpuOnly)
|
|
|
|
.set_sharing_mode(VK_SHARING_MODE_EXCLUSIVE)
|
|
|
|
.set_usage(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT)
|
|
|
|
.set_data(&cube_mesh)
|
|
|
|
.build(context.device().clone())?;
|
|
|
|
|
|
|
|
let me = Self {
|
|
|
|
_cube_map: cube_map,
|
|
|
|
cube_buffer,
|
|
|
|
|
|
|
|
render_target,
|
|
|
|
pipeline,
|
|
|
|
descriptor_set,
|
|
|
|
};
|
2025-03-01 05:43:44 +00:00
|
|
|
world.resources.insert(me);
|
|
|
|
|
|
|
|
Ok(())
|
2025-02-28 14:13:19 +00:00
|
|
|
}
|
2025-03-01 17:05:27 +00:00
|
|
|
|
|
|
|
fn create_render_target(
|
|
|
|
context: &Context,
|
|
|
|
sample_count: VkSampleCountFlags,
|
|
|
|
) -> Result<TargetMode<RenderTarget>> {
|
|
|
|
context.images().execute(|images| {
|
|
|
|
let first = images.first().unwrap();
|
|
|
|
|
|
|
|
let width = first.width();
|
|
|
|
let height = first.height();
|
|
|
|
let format = first.vk_format();
|
|
|
|
|
|
|
|
if sample_count == VK_SAMPLE_COUNT_1_BIT {
|
|
|
|
RenderTarget::builder()
|
|
|
|
.add_sub_pass(
|
|
|
|
SubPass::builder(width, height)
|
|
|
|
// render directly into swapchain target if there is no multi sampling
|
|
|
|
.set_prepared_targets(images, 0, None)
|
|
|
|
.use_queue(context.queue().clone())
|
|
|
|
.build(context.device())?,
|
|
|
|
)
|
|
|
|
.build(context.device())
|
|
|
|
} else {
|
|
|
|
RenderTarget::builder()
|
|
|
|
.add_sub_pass(
|
|
|
|
SubPass::builder(width, height)
|
|
|
|
// render into multi sampled images
|
|
|
|
.add_target_info(CustomTarget {
|
|
|
|
usage: VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT.into(),
|
|
|
|
format,
|
|
|
|
clear_on_load: true,
|
|
|
|
store_on_save: true,
|
|
|
|
attach_sampler: false,
|
|
|
|
use_as_input: false,
|
|
|
|
clear_value: ClearValue::Color([0.0, 0.0, 0.0, 0.0]),
|
|
|
|
})
|
|
|
|
.set_sample_count(sample_count)
|
|
|
|
// resolve multi sampling into swapchain image
|
|
|
|
.add_resolve_targets((images, false))
|
|
|
|
.use_queue(context.queue().clone())
|
|
|
|
.build(context.device())?,
|
|
|
|
)
|
|
|
|
.build(context.device())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_pipeline(
|
|
|
|
context: &Context,
|
|
|
|
sample_count: VkSampleCountFlags,
|
|
|
|
render_target: &TargetMode<RenderTarget>,
|
|
|
|
pipeline_layout: &Arc<PipelineLayout>,
|
|
|
|
) -> Result<TargetMode<Arc<Pipeline>>> {
|
|
|
|
render_target.execute(|render_target| {
|
|
|
|
Pipeline::new_graphics()
|
|
|
|
.default_multisample(sample_count)
|
|
|
|
.default_rasterization(VK_CULL_MODE_NONE, VK_FRONT_FACE_CLOCKWISE)
|
|
|
|
.build(
|
|
|
|
context.device().clone(),
|
|
|
|
pipeline_layout,
|
|
|
|
render_target.render_pass(),
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2025-02-28 14:13:19 +00:00
|
|
|
}
|
2025-03-01 05:01:46 +00:00
|
|
|
|
|
|
|
impl TScene for SkyBox {
|
|
|
|
fn process(
|
|
|
|
&mut self,
|
|
|
|
buffer_recorder: &mut CommandBufferRecorder<'_>,
|
2025-03-01 17:05:27 +00:00
|
|
|
_images: &TargetMode<Vec<Arc<Image>>>,
|
2025-03-01 05:01:46 +00:00
|
|
|
indices: &TargetMode<usize>,
|
2025-03-01 17:05:27 +00:00
|
|
|
_world: &World,
|
2025-03-01 05:01:46 +00:00
|
|
|
) -> Result<()> {
|
2025-03-01 17:05:27 +00:00
|
|
|
self.render_target
|
|
|
|
.chain(indices)
|
|
|
|
.chain(&self.pipeline)
|
|
|
|
.unfold()
|
|
|
|
.execute(|(render_target, index, pipeline)| {
|
|
|
|
render_target.begin(buffer_recorder, VK_SUBPASS_CONTENTS_INLINE, ***index);
|
|
|
|
|
|
|
|
buffer_recorder.bind_pipeline(pipeline)?;
|
|
|
|
buffer_recorder.bind_descriptor_sets_minimal(&[&self.descriptor_set]);
|
|
|
|
|
|
|
|
buffer_recorder.bind_vertex_buffer(&self.cube_buffer);
|
|
|
|
buffer_recorder.draw_complete_single_instance(self.cube_buffer.size() as u32);
|
|
|
|
|
|
|
|
render_target.end(buffer_recorder);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(())
|
2025-03-01 05:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resize(
|
|
|
|
&mut self,
|
|
|
|
window_width: f32,
|
|
|
|
window_height: f32,
|
|
|
|
images: &TargetMode<Vec<Arc<Image>>>,
|
|
|
|
) -> Result<()> {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|