2022-03-06 19:30:49 +00:00
|
|
|
use super::{
|
|
|
|
ambient_lighting_system::AmbientLightingSystem,
|
|
|
|
directional_lighting_system::DirectionalLightingSystem,
|
|
|
|
point_lighting_system::PointLightingSystem,
|
|
|
|
};
|
|
|
|
use cgmath::{Matrix4, SquareMatrix, Vector3};
|
2022-10-27 18:59:47 +00:00
|
|
|
use std::sync::Arc;
|
2022-03-06 19:30:49 +00:00
|
|
|
use vulkano::{
|
|
|
|
command_buffer::{
|
2023-12-09 11:24:52 +00:00
|
|
|
allocator::StandardCommandBufferAllocator, CommandBufferUsage, PrimaryAutoCommandBuffer,
|
|
|
|
RecordingCommandBuffer, RenderPassBeginInfo, SecondaryAutoCommandBuffer, SubpassBeginInfo,
|
|
|
|
SubpassContents,
|
2022-03-06 19:30:49 +00:00
|
|
|
},
|
2022-10-05 09:09:26 +00:00
|
|
|
descriptor_set::allocator::StandardDescriptorSetAllocator,
|
2022-03-06 19:30:49 +00:00
|
|
|
device::Queue,
|
|
|
|
format::Format,
|
2023-07-06 08:52:11 +00:00
|
|
|
image::{view::ImageView, Image, ImageCreateInfo, ImageType, ImageUsage},
|
2023-07-03 20:37:29 +00:00
|
|
|
memory::allocator::{AllocationCreateInfo, StandardMemoryAllocator},
|
2022-03-06 19:30:49 +00:00
|
|
|
render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass},
|
|
|
|
sync::GpuFuture,
|
2021-04-26 14:53:18 +00:00
|
|
|
};
|
2018-08-30 01:37:51 +00:00
|
|
|
|
|
|
|
/// System that contains the necessary facilities for rendering a single frame.
|
|
|
|
pub struct FrameSystem {
|
|
|
|
// Queue to use to render everything.
|
|
|
|
gfx_queue: Arc<Queue>,
|
|
|
|
|
|
|
|
// Render pass used for the drawing. See the `new` method for the actual render pass content.
|
|
|
|
// We need to keep it in `FrameSystem` because we may want to recreate the intermediate buffers
|
|
|
|
// in of a change in the dimensions.
|
2021-04-10 11:09:03 +00:00
|
|
|
render_pass: Arc<RenderPass>,
|
2018-08-30 01:37:51 +00:00
|
|
|
|
2022-10-26 14:25:01 +00:00
|
|
|
memory_allocator: Arc<StandardMemoryAllocator>,
|
2022-10-27 18:59:47 +00:00
|
|
|
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
|
2022-10-05 09:09:26 +00:00
|
|
|
|
2018-08-30 01:37:51 +00:00
|
|
|
// Intermediate render target that will contain the albedo of each pixel of the scene.
|
2023-07-03 20:37:29 +00:00
|
|
|
diffuse_buffer: Arc<ImageView>,
|
2018-08-30 01:37:51 +00:00
|
|
|
// Intermediate render target that will contain the normal vector in world coordinates of each
|
|
|
|
// pixel of the scene.
|
|
|
|
// The normal vector is the vector perpendicular to the surface of the object at this point.
|
2023-07-03 20:37:29 +00:00
|
|
|
normals_buffer: Arc<ImageView>,
|
2018-08-30 01:37:51 +00:00
|
|
|
// Intermediate render target that will contain the depth of each pixel of the scene.
|
2018-09-02 04:18:22 +00:00
|
|
|
// This is a traditional depth buffer. `0.0` means "near", and `1.0` means "far".
|
2023-07-03 20:37:29 +00:00
|
|
|
depth_buffer: Arc<ImageView>,
|
2018-08-30 01:37:51 +00:00
|
|
|
|
|
|
|
// Will allow us to add an ambient lighting to a scene during the second subpass.
|
|
|
|
ambient_lighting_system: AmbientLightingSystem,
|
|
|
|
// Will allow us to add a directional light to a scene during the second subpass.
|
|
|
|
directional_lighting_system: DirectionalLightingSystem,
|
|
|
|
// Will allow us to add a spot light source to a scene during the second subpass.
|
|
|
|
point_lighting_system: PointLightingSystem,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FrameSystem {
|
|
|
|
/// Initializes the frame system.
|
|
|
|
///
|
|
|
|
/// Should be called at initialization, as it can take some time to build.
|
|
|
|
///
|
|
|
|
/// - `gfx_queue` is the queue that will be used to perform the main rendering.
|
|
|
|
/// - `final_output_format` is the format of the image that will later be passed to the
|
|
|
|
/// `frame()` method. We need to know that in advance. If that format ever changes, we have
|
|
|
|
/// to create a new `FrameSystem`.
|
2022-10-05 09:09:26 +00:00
|
|
|
pub fn new(
|
|
|
|
gfx_queue: Arc<Queue>,
|
|
|
|
final_output_format: Format,
|
2022-10-26 14:25:01 +00:00
|
|
|
memory_allocator: Arc<StandardMemoryAllocator>,
|
2022-10-27 18:59:47 +00:00
|
|
|
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
|
2022-10-05 09:09:26 +00:00
|
|
|
) -> FrameSystem {
|
2018-08-30 01:37:51 +00:00
|
|
|
// Creating the render pass.
|
|
|
|
//
|
|
|
|
// The render pass has two subpasses. In the first subpass, we draw all the objects of the
|
|
|
|
// scene. Note that it is not the `FrameSystem` that is responsible for the drawing,
|
|
|
|
// instead it only provides an API that allows the user to do so.
|
|
|
|
//
|
|
|
|
// The drawing of the objects will write to the `diffuse`, `normals` and `depth`
|
|
|
|
// attachments.
|
|
|
|
//
|
|
|
|
// Then in the second subpass, we read these three attachments as input attachments and
|
|
|
|
// draw to `final_color`. Each draw operation performed in this second subpass has its
|
|
|
|
// value added to `final_color` and not replaced, thanks to blending.
|
|
|
|
//
|
|
|
|
// > **Warning**: If the red, green or blue component of the final image goes over `1.0`
|
|
|
|
// > then it will be clamped. For example a pixel of `[2.0, 1.0, 1.0]` (which is red) will
|
|
|
|
// > be clamped to `[1.0, 1.0, 1.0]` (which is white) instead of being converted to
|
|
|
|
// > `[1.0, 0.5, 0.5]` as desired. In a real-life application you want to use an additional
|
|
|
|
// > intermediate image with a floating-point format, then perform additional passes to
|
|
|
|
// > convert all the colors in the correct range. These techniques are known as HDR and
|
|
|
|
// > tone mapping.
|
|
|
|
//
|
|
|
|
// Input attachments are a special kind of way to read images. You can only read from them
|
|
|
|
// from a fragment shader, and you can only read the pixel corresponding to the pixel
|
|
|
|
// currently being processed by the fragment shader. If you want to read from attachments
|
|
|
|
// but can't deal with these restrictions, then you should create multiple render passes
|
|
|
|
// instead.
|
2023-03-25 09:25:25 +00:00
|
|
|
let render_pass = vulkano::ordered_passes_renderpass!(
|
|
|
|
gfx_queue.device().clone(),
|
2021-11-02 20:33:58 +00:00
|
|
|
attachments: {
|
|
|
|
// The image that will contain the final rendering (in this example the swapchain
|
|
|
|
// image, but it could be another image).
|
|
|
|
final_color: {
|
|
|
|
format: final_output_format,
|
|
|
|
samples: 1,
|
2023-06-17 19:01:50 +00:00
|
|
|
load_op: Clear,
|
|
|
|
store_op: Store,
|
2018-08-30 01:37:51 +00:00
|
|
|
},
|
2021-11-02 20:33:58 +00:00
|
|
|
// Will be bound to `self.diffuse_buffer`.
|
|
|
|
diffuse: {
|
|
|
|
format: Format::A2B10G10R10_UNORM_PACK32,
|
|
|
|
samples: 1,
|
2023-06-17 19:01:50 +00:00
|
|
|
load_op: Clear,
|
|
|
|
store_op: DontCare,
|
2021-11-02 20:33:58 +00:00
|
|
|
},
|
|
|
|
// Will be bound to `self.normals_buffer`.
|
|
|
|
normals: {
|
|
|
|
format: Format::R16G16B16A16_SFLOAT,
|
|
|
|
samples: 1,
|
2023-06-17 19:01:50 +00:00
|
|
|
load_op: Clear,
|
|
|
|
store_op: DontCare,
|
2021-11-02 20:33:58 +00:00
|
|
|
},
|
|
|
|
// Will be bound to `self.depth_buffer`.
|
2023-06-17 19:01:50 +00:00
|
|
|
depth_stencil: {
|
2021-11-02 20:33:58 +00:00
|
|
|
format: Format::D16_UNORM,
|
|
|
|
samples: 1,
|
2023-06-17 19:01:50 +00:00
|
|
|
load_op: Clear,
|
|
|
|
store_op: DontCare,
|
2023-03-25 09:25:25 +00:00
|
|
|
},
|
2021-11-02 20:33:58 +00:00
|
|
|
},
|
|
|
|
passes: [
|
|
|
|
// Write to the diffuse, normals and depth attachments.
|
|
|
|
{
|
|
|
|
color: [diffuse, normals],
|
2023-06-17 19:01:50 +00:00
|
|
|
depth_stencil: {depth_stencil},
|
2023-03-25 09:25:25 +00:00
|
|
|
input: [],
|
2021-11-02 20:33:58 +00:00
|
|
|
},
|
|
|
|
// Apply lighting by reading these three attachments and writing to `final_color`.
|
|
|
|
{
|
|
|
|
color: [final_color],
|
|
|
|
depth_stencil: {},
|
2023-06-17 19:01:50 +00:00
|
|
|
input: [diffuse, normals, depth_stencil],
|
2023-03-25 09:25:25 +00:00
|
|
|
},
|
|
|
|
],
|
2021-11-02 20:33:58 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-08-30 01:37:51 +00:00
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
// For now we create three temporary images with a dimension of 1 by 1 pixel. These images
|
|
|
|
// will be replaced the first time we call `frame()`.
|
2022-02-27 06:18:14 +00:00
|
|
|
let diffuse_buffer = ImageView::new_default(
|
2023-07-03 20:37:29 +00:00
|
|
|
Image::new(
|
2023-09-03 11:09:07 +00:00
|
|
|
memory_allocator.clone(),
|
2023-07-03 20:37:29 +00:00
|
|
|
ImageCreateInfo {
|
2023-07-06 08:52:11 +00:00
|
|
|
image_type: ImageType::Dim2d,
|
2023-07-14 14:44:34 +00:00
|
|
|
format: Format::A2B10G10R10_UNORM_PACK32,
|
2023-07-06 08:52:11 +00:00
|
|
|
extent: [1, 1, 1],
|
2023-07-03 20:37:29 +00:00
|
|
|
usage: ImageUsage::COLOR_ATTACHMENT
|
|
|
|
| ImageUsage::TRANSIENT_ATTACHMENT
|
|
|
|
| ImageUsage::INPUT_ATTACHMENT,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
AllocationCreateInfo::default(),
|
2021-03-14 12:09:08 +00:00
|
|
|
)
|
|
|
|
.unwrap(),
|
2020-05-10 00:36:20 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-02-27 06:18:14 +00:00
|
|
|
let normals_buffer = ImageView::new_default(
|
2023-07-03 20:37:29 +00:00
|
|
|
Image::new(
|
2023-09-03 11:09:07 +00:00
|
|
|
memory_allocator.clone(),
|
2023-07-03 20:37:29 +00:00
|
|
|
ImageCreateInfo {
|
2023-07-06 08:52:11 +00:00
|
|
|
image_type: ImageType::Dim2d,
|
2023-07-14 14:44:34 +00:00
|
|
|
format: Format::R16G16B16A16_SFLOAT,
|
2023-07-06 08:52:11 +00:00
|
|
|
extent: [1, 1, 1],
|
2023-07-03 20:37:29 +00:00
|
|
|
usage: ImageUsage::TRANSIENT_ATTACHMENT | ImageUsage::INPUT_ATTACHMENT,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
AllocationCreateInfo::default(),
|
2021-03-14 12:09:08 +00:00
|
|
|
)
|
|
|
|
.unwrap(),
|
2020-05-10 00:36:20 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-02-27 06:18:14 +00:00
|
|
|
let depth_buffer = ImageView::new_default(
|
2023-07-03 20:37:29 +00:00
|
|
|
Image::new(
|
2023-09-03 11:09:07 +00:00
|
|
|
memory_allocator.clone(),
|
2023-07-03 20:37:29 +00:00
|
|
|
ImageCreateInfo {
|
2023-07-06 08:52:11 +00:00
|
|
|
image_type: ImageType::Dim2d,
|
2023-07-14 14:44:34 +00:00
|
|
|
format: Format::D16_UNORM,
|
2023-07-06 08:52:11 +00:00
|
|
|
extent: [1, 1, 1],
|
2023-07-03 20:37:29 +00:00
|
|
|
usage: ImageUsage::TRANSIENT_ATTACHMENT | ImageUsage::INPUT_ATTACHMENT,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
AllocationCreateInfo::default(),
|
2021-03-14 12:09:08 +00:00
|
|
|
)
|
|
|
|
.unwrap(),
|
2020-05-10 00:36:20 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-08-30 01:37:51 +00:00
|
|
|
|
2022-10-27 18:59:47 +00:00
|
|
|
let descriptor_set_allocator = Arc::new(StandardDescriptorSetAllocator::new(
|
2022-10-05 09:09:26 +00:00
|
|
|
gfx_queue.device().clone(),
|
2023-10-24 14:23:17 +00:00
|
|
|
Default::default(),
|
2022-10-05 09:09:26 +00:00
|
|
|
));
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
// Initialize the three lighting systems. Note that we need to pass to them the subpass
|
|
|
|
// where they will be executed.
|
2018-08-30 01:37:51 +00:00
|
|
|
let lighting_subpass = Subpass::from(render_pass.clone(), 1).unwrap();
|
2022-10-05 09:09:26 +00:00
|
|
|
let ambient_lighting_system = AmbientLightingSystem::new(
|
|
|
|
gfx_queue.clone(),
|
|
|
|
lighting_subpass.clone(),
|
2023-09-03 11:09:07 +00:00
|
|
|
memory_allocator.clone(),
|
2022-10-05 09:09:26 +00:00
|
|
|
command_buffer_allocator.clone(),
|
|
|
|
descriptor_set_allocator.clone(),
|
|
|
|
);
|
|
|
|
let directional_lighting_system = DirectionalLightingSystem::new(
|
|
|
|
gfx_queue.clone(),
|
|
|
|
lighting_subpass.clone(),
|
2023-09-03 11:09:07 +00:00
|
|
|
memory_allocator.clone(),
|
2022-10-05 09:09:26 +00:00
|
|
|
command_buffer_allocator.clone(),
|
|
|
|
descriptor_set_allocator.clone(),
|
|
|
|
);
|
|
|
|
let point_lighting_system = PointLightingSystem::new(
|
|
|
|
gfx_queue.clone(),
|
|
|
|
lighting_subpass,
|
2023-09-03 11:09:07 +00:00
|
|
|
memory_allocator.clone(),
|
2022-10-05 09:09:26 +00:00
|
|
|
command_buffer_allocator.clone(),
|
|
|
|
descriptor_set_allocator,
|
|
|
|
);
|
2018-08-30 01:37:51 +00:00
|
|
|
|
|
|
|
FrameSystem {
|
|
|
|
gfx_queue,
|
2021-11-02 20:33:58 +00:00
|
|
|
render_pass,
|
2022-10-26 14:25:01 +00:00
|
|
|
memory_allocator,
|
2022-10-05 09:09:26 +00:00
|
|
|
command_buffer_allocator,
|
2018-08-30 01:37:51 +00:00
|
|
|
diffuse_buffer,
|
|
|
|
normals_buffer,
|
|
|
|
depth_buffer,
|
|
|
|
ambient_lighting_system,
|
|
|
|
directional_lighting_system,
|
|
|
|
point_lighting_system,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the subpass of the render pass where the rendering should write info to gbuffers.
|
|
|
|
///
|
|
|
|
/// Has two outputs: the diffuse color (3 components) and the normals in world coordinates
|
|
|
|
/// (3 components). Also has a depth attachment.
|
|
|
|
///
|
|
|
|
/// This method is necessary in order to initialize the pipelines that will draw the objects
|
|
|
|
/// of the scene.
|
|
|
|
#[inline]
|
2021-04-10 11:09:03 +00:00
|
|
|
pub fn deferred_subpass(&self) -> Subpass {
|
2018-08-30 01:37:51 +00:00
|
|
|
Subpass::from(self.render_pass.clone(), 0).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Starts drawing a new frame.
|
|
|
|
///
|
|
|
|
/// - `before_future` is the future after which the main rendering should be executed.
|
|
|
|
/// - `final_image` is the image we are going to draw to.
|
|
|
|
/// - `world_to_framebuffer` is the matrix that will be used to convert from 3D coordinates in
|
|
|
|
/// the world into 2D coordinates on the framebuffer.
|
2021-09-02 08:29:27 +00:00
|
|
|
pub fn frame<F>(
|
2020-05-10 00:36:20 +00:00
|
|
|
&mut self,
|
|
|
|
before_future: F,
|
2023-07-06 08:52:11 +00:00
|
|
|
final_image_view: Arc<ImageView>,
|
2020-05-10 00:36:20 +00:00
|
|
|
world_to_framebuffer: Matrix4<f32>,
|
|
|
|
) -> Frame
|
|
|
|
where
|
|
|
|
F: GpuFuture + 'static,
|
2018-08-30 01:37:51 +00:00
|
|
|
{
|
|
|
|
// First of all we recreate `self.diffuse_buffer`, `self.normals_buffer` and
|
2023-07-06 08:52:11 +00:00
|
|
|
// `self.depth_buffer` if their extent doesn't match the extent of the final image.
|
|
|
|
let extent = final_image_view.image().extent();
|
|
|
|
if self.diffuse_buffer.image().extent() != extent {
|
2018-08-30 01:37:51 +00:00
|
|
|
// Note that we create "transient" images here. This means that the content of the
|
|
|
|
// image is only defined when within a render pass. In other words you can draw to
|
|
|
|
// them in a subpass then read them in another subpass, but as soon as you leave the
|
|
|
|
// render pass their content becomes undefined.
|
2022-02-27 06:18:14 +00:00
|
|
|
self.diffuse_buffer = ImageView::new_default(
|
2023-07-03 20:37:29 +00:00
|
|
|
Image::new(
|
2023-09-03 11:09:07 +00:00
|
|
|
self.memory_allocator.clone(),
|
2023-07-03 20:37:29 +00:00
|
|
|
ImageCreateInfo {
|
2023-07-06 08:52:11 +00:00
|
|
|
extent,
|
2023-07-14 14:44:34 +00:00
|
|
|
format: Format::A2B10G10R10_UNORM_PACK32,
|
2023-07-03 20:37:29 +00:00
|
|
|
usage: ImageUsage::COLOR_ATTACHMENT
|
|
|
|
| ImageUsage::TRANSIENT_ATTACHMENT
|
|
|
|
| ImageUsage::INPUT_ATTACHMENT,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
AllocationCreateInfo::default(),
|
2021-03-14 12:09:08 +00:00
|
|
|
)
|
|
|
|
.unwrap(),
|
2020-05-10 00:36:20 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-02-27 06:18:14 +00:00
|
|
|
self.normals_buffer = ImageView::new_default(
|
2023-07-03 20:37:29 +00:00
|
|
|
Image::new(
|
2023-09-03 11:09:07 +00:00
|
|
|
self.memory_allocator.clone(),
|
2023-07-03 20:37:29 +00:00
|
|
|
ImageCreateInfo {
|
2023-07-06 08:52:11 +00:00
|
|
|
extent,
|
2023-07-14 14:44:34 +00:00
|
|
|
format: Format::R16G16B16A16_SFLOAT,
|
2023-07-03 20:37:29 +00:00
|
|
|
usage: ImageUsage::COLOR_ATTACHMENT
|
|
|
|
| ImageUsage::TRANSIENT_ATTACHMENT
|
|
|
|
| ImageUsage::INPUT_ATTACHMENT,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
AllocationCreateInfo::default(),
|
2021-03-14 12:09:08 +00:00
|
|
|
)
|
|
|
|
.unwrap(),
|
2020-05-10 00:36:20 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-02-27 06:18:14 +00:00
|
|
|
self.depth_buffer = ImageView::new_default(
|
2023-07-03 20:37:29 +00:00
|
|
|
Image::new(
|
2023-09-03 11:09:07 +00:00
|
|
|
self.memory_allocator.clone(),
|
2023-07-03 20:37:29 +00:00
|
|
|
ImageCreateInfo {
|
2023-07-06 08:52:11 +00:00
|
|
|
extent,
|
2023-07-14 14:44:34 +00:00
|
|
|
format: Format::D16_UNORM,
|
2023-07-03 20:37:29 +00:00
|
|
|
usage: ImageUsage::DEPTH_STENCIL_ATTACHMENT
|
|
|
|
| ImageUsage::TRANSIENT_ATTACHMENT
|
|
|
|
| ImageUsage::INPUT_ATTACHMENT,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
AllocationCreateInfo::default(),
|
2021-03-14 12:09:08 +00:00
|
|
|
)
|
|
|
|
.unwrap(),
|
2020-05-10 00:36:20 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-08-30 01:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the framebuffer. The image must be attached in the same order as they were defined
|
|
|
|
// with the `ordered_passes_renderpass!` macro.
|
2022-02-20 00:14:16 +00:00
|
|
|
let framebuffer = Framebuffer::new(
|
|
|
|
self.render_pass.clone(),
|
|
|
|
FramebufferCreateInfo {
|
|
|
|
attachments: vec![
|
2023-07-06 08:52:11 +00:00
|
|
|
final_image_view,
|
2022-02-20 00:14:16 +00:00
|
|
|
self.diffuse_buffer.clone(),
|
|
|
|
self.normals_buffer.clone(),
|
|
|
|
self.depth_buffer.clone(),
|
|
|
|
],
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-08-30 01:37:51 +00:00
|
|
|
|
|
|
|
// Start the command buffer builder that will be filled throughout the frame handling.
|
2023-12-09 11:24:52 +00:00
|
|
|
let mut command_buffer_builder = RecordingCommandBuffer::primary(
|
2023-11-14 16:57:43 +00:00
|
|
|
self.command_buffer_allocator.clone(),
|
2022-09-10 06:00:08 +00:00
|
|
|
self.gfx_queue.queue_family_index(),
|
2021-04-26 14:53:18 +00:00
|
|
|
CommandBufferUsage::OneTimeSubmit,
|
2020-06-01 14:41:42 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
command_buffer_builder
|
2020-05-10 00:36:20 +00:00
|
|
|
.begin_render_pass(
|
2022-04-24 01:16:19 +00:00
|
|
|
RenderPassBeginInfo {
|
|
|
|
clear_values: vec![
|
|
|
|
Some([0.0, 0.0, 0.0, 0.0].into()),
|
|
|
|
Some([0.0, 0.0, 0.0, 0.0].into()),
|
|
|
|
Some([0.0, 0.0, 0.0, 0.0].into()),
|
|
|
|
Some(1.0f32.into()),
|
|
|
|
],
|
|
|
|
..RenderPassBeginInfo::framebuffer(framebuffer.clone())
|
|
|
|
},
|
2023-08-04 18:55:16 +00:00
|
|
|
SubpassBeginInfo {
|
|
|
|
contents: SubpassContents::SecondaryCommandBuffers,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2020-05-10 00:36:20 +00:00
|
|
|
)
|
2020-06-01 14:41:42 +00:00
|
|
|
.unwrap();
|
2018-08-30 01:37:51 +00:00
|
|
|
|
|
|
|
Frame {
|
|
|
|
system: self,
|
|
|
|
before_main_cb_future: Some(Box::new(before_future)),
|
|
|
|
framebuffer,
|
|
|
|
num_pass: 0,
|
2020-06-01 14:41:42 +00:00
|
|
|
command_buffer_builder: Some(command_buffer_builder),
|
2018-08-30 01:37:51 +00:00
|
|
|
world_to_framebuffer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents the active process of rendering a frame.
|
|
|
|
///
|
|
|
|
/// This struct mutably borrows the `FrameSystem`.
|
|
|
|
pub struct Frame<'a> {
|
|
|
|
// The `FrameSystem`.
|
|
|
|
system: &'a mut FrameSystem,
|
|
|
|
|
|
|
|
// The active pass we are in. This keeps track of the step we are in.
|
|
|
|
// - If `num_pass` is 0, then we haven't start anything yet.
|
|
|
|
// - If `num_pass` is 1, then we have finished drawing all the objects of the scene.
|
|
|
|
// - If `num_pass` is 2, then we have finished applying lighting.
|
|
|
|
// - Otherwise the frame is finished.
|
|
|
|
// In a more complex application you can have dozens of passes, in which case you probably
|
|
|
|
// don't want to document them all here.
|
|
|
|
num_pass: u8,
|
|
|
|
|
|
|
|
// Future to wait upon before the main rendering.
|
2019-07-02 08:25:58 +00:00
|
|
|
before_main_cb_future: Option<Box<dyn GpuFuture>>,
|
2018-08-30 01:37:51 +00:00
|
|
|
// Framebuffer that was used when starting the render pass.
|
2021-11-02 20:33:58 +00:00
|
|
|
framebuffer: Arc<Framebuffer>,
|
2018-08-30 01:37:51 +00:00
|
|
|
// The command buffer builder that will be built during the lifetime of this object.
|
2023-12-09 11:24:52 +00:00
|
|
|
command_buffer_builder: Option<RecordingCommandBuffer<PrimaryAutoCommandBuffer>>,
|
2018-08-30 01:37:51 +00:00
|
|
|
// Matrix that was passed to `frame()`.
|
|
|
|
world_to_framebuffer: Matrix4<f32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Frame<'a> {
|
|
|
|
/// Returns an enumeration containing the next pass of the rendering.
|
|
|
|
pub fn next_pass<'f>(&'f mut self) -> Option<Pass<'f, 'a>> {
|
|
|
|
// This function reads `num_pass` increments its value, and returns a struct corresponding
|
|
|
|
// to that pass that the user will be able to manipulate in order to customize the pass.
|
2020-05-10 00:36:20 +00:00
|
|
|
match {
|
|
|
|
let current_pass = self.num_pass;
|
|
|
|
self.num_pass += 1;
|
|
|
|
current_pass
|
|
|
|
} {
|
2018-08-30 01:37:51 +00:00
|
|
|
0 => {
|
|
|
|
// If we are in the pass 0 then we haven't start anything yet.
|
|
|
|
// We already called `begin_render_pass` (in the `frame()` method), and that's the
|
|
|
|
// state we are in.
|
|
|
|
// We return an object that will allow the user to draw objects on the scene.
|
2020-05-10 00:36:20 +00:00
|
|
|
Some(Pass::Deferred(DrawPass { frame: self }))
|
|
|
|
}
|
2018-08-30 01:37:51 +00:00
|
|
|
|
|
|
|
1 => {
|
|
|
|
// If we are in pass 1 then we have finished drawing the objects on the scene.
|
|
|
|
// Going to the next subpass.
|
2020-06-01 14:41:42 +00:00
|
|
|
self.command_buffer_builder
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
2023-08-04 18:55:16 +00:00
|
|
|
.next_subpass(
|
|
|
|
Default::default(),
|
|
|
|
SubpassBeginInfo {
|
|
|
|
contents: SubpassContents::SecondaryCommandBuffers,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
2020-06-01 14:41:42 +00:00
|
|
|
.unwrap();
|
2018-08-30 01:37:51 +00:00
|
|
|
|
|
|
|
// And returning an object that will allow the user to apply lighting to the scene.
|
2020-05-10 00:36:20 +00:00
|
|
|
Some(Pass::Lighting(LightingPass { frame: self }))
|
|
|
|
}
|
2018-08-30 01:37:51 +00:00
|
|
|
|
|
|
|
2 => {
|
|
|
|
// If we are in pass 2 then we have finished applying lighting.
|
|
|
|
// We take the builder, call `end_render_pass()`, and then `build()` it to obtain
|
|
|
|
// an actual command buffer.
|
2020-06-01 14:41:42 +00:00
|
|
|
self.command_buffer_builder
|
|
|
|
.as_mut()
|
2020-05-10 00:36:20 +00:00
|
|
|
.unwrap()
|
2023-08-04 18:55:16 +00:00
|
|
|
.end_render_pass(Default::default())
|
2020-05-10 00:36:20 +00:00
|
|
|
.unwrap();
|
2023-12-09 11:24:52 +00:00
|
|
|
let command_buffer = self.command_buffer_builder.take().unwrap().end().unwrap();
|
2018-08-30 01:37:51 +00:00
|
|
|
|
|
|
|
// Extract `before_main_cb_future` and append the command buffer execution to it.
|
2020-05-10 00:36:20 +00:00
|
|
|
let after_main_cb = self
|
|
|
|
.before_main_cb_future
|
|
|
|
.take()
|
|
|
|
.unwrap()
|
2018-08-30 01:37:51 +00:00
|
|
|
.then_execute(self.system.gfx_queue.clone(), command_buffer)
|
|
|
|
.unwrap();
|
|
|
|
// We obtain `after_main_cb`, which we give to the user.
|
|
|
|
Some(Pass::Finished(Box::new(after_main_cb)))
|
2020-05-10 00:36:20 +00:00
|
|
|
}
|
2018-08-30 01:37:51 +00:00
|
|
|
|
|
|
|
// If the pass is over 2 then the frame is in the finished state and can't do anything
|
|
|
|
// more.
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Struct provided to the user that allows them to customize or handle the pass.
|
|
|
|
pub enum Pass<'f, 's: 'f> {
|
|
|
|
/// We are in the pass where we draw objects on the scene. The `DrawPass` allows the user to
|
|
|
|
/// draw the objects.
|
|
|
|
Deferred(DrawPass<'f, 's>),
|
|
|
|
|
|
|
|
/// We are in the pass where we add lighting to the scene. The `LightingPass` allows the user
|
|
|
|
/// to add light sources.
|
|
|
|
Lighting(LightingPass<'f, 's>),
|
|
|
|
|
|
|
|
/// The frame has been fully prepared, and here is the future that will perform the drawing
|
|
|
|
/// on the image.
|
2019-07-02 08:25:58 +00:00
|
|
|
Finished(Box<dyn GpuFuture>),
|
2018-08-30 01:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Allows the user to draw objects on the scene.
|
|
|
|
pub struct DrawPass<'f, 's: 'f> {
|
|
|
|
frame: &'f mut Frame<'s>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'f, 's: 'f> DrawPass<'f, 's> {
|
|
|
|
/// Appends a command that executes a secondary command buffer that performs drawing.
|
2023-12-06 16:23:24 +00:00
|
|
|
pub fn execute(&mut self, command_buffer: Arc<SecondaryAutoCommandBuffer>) {
|
2021-03-28 10:36:14 +00:00
|
|
|
self.frame
|
|
|
|
.command_buffer_builder
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
|
|
|
.execute_commands(command_buffer)
|
|
|
|
.unwrap();
|
2018-08-30 01:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the dimensions in pixels of the viewport.
|
|
|
|
pub fn viewport_dimensions(&self) -> [u32; 2] {
|
2022-02-20 00:14:16 +00:00
|
|
|
self.frame.framebuffer.extent()
|
2018-08-30 01:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the 4x4 matrix that turns world coordinates into 2D coordinates on the framebuffer.
|
2019-10-24 07:46:40 +00:00
|
|
|
#[allow(dead_code)]
|
2018-08-30 01:37:51 +00:00
|
|
|
pub fn world_to_framebuffer_matrix(&self) -> Matrix4<f32> {
|
|
|
|
self.frame.world_to_framebuffer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Allows the user to apply lighting on the scene.
|
|
|
|
pub struct LightingPass<'f, 's: 'f> {
|
|
|
|
frame: &'f mut Frame<'s>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'f, 's: 'f> LightingPass<'f, 's> {
|
|
|
|
/// Applies an ambient lighting to the scene.
|
|
|
|
///
|
|
|
|
/// All the objects will be colored with an intensity of `color`.
|
|
|
|
pub fn ambient_light(&mut self, color: [f32; 3]) {
|
2021-03-28 10:36:14 +00:00
|
|
|
let command_buffer = self.frame.system.ambient_lighting_system.draw(
|
2022-02-20 00:14:16 +00:00
|
|
|
self.frame.framebuffer.extent(),
|
2021-03-28 10:36:14 +00:00
|
|
|
self.frame.system.diffuse_buffer.clone(),
|
|
|
|
color,
|
|
|
|
);
|
|
|
|
self.frame
|
|
|
|
.command_buffer_builder
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
|
|
|
.execute_commands(command_buffer)
|
|
|
|
.unwrap();
|
2018-08-30 01:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Applies an directional lighting to the scene.
|
|
|
|
///
|
|
|
|
/// All the objects will be colored with an intensity varying between `[0, 0, 0]` and `color`,
|
|
|
|
/// depending on the dot product of their normal and `direction`.
|
|
|
|
pub fn directional_light(&mut self, direction: Vector3<f32>, color: [f32; 3]) {
|
2021-03-28 10:36:14 +00:00
|
|
|
let command_buffer = self.frame.system.directional_lighting_system.draw(
|
2022-02-20 00:14:16 +00:00
|
|
|
self.frame.framebuffer.extent(),
|
2021-03-28 10:36:14 +00:00
|
|
|
self.frame.system.diffuse_buffer.clone(),
|
|
|
|
self.frame.system.normals_buffer.clone(),
|
|
|
|
direction,
|
|
|
|
color,
|
|
|
|
);
|
|
|
|
self.frame
|
|
|
|
.command_buffer_builder
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
|
|
|
.execute_commands(command_buffer)
|
|
|
|
.unwrap();
|
2018-08-30 01:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Applies a spot lighting to the scene.
|
|
|
|
///
|
|
|
|
/// All the objects will be colored with an intensity varying between `[0, 0, 0]` and `color`,
|
|
|
|
/// depending on their distance with `position`. Objects that aren't facing `position` won't
|
|
|
|
/// receive any light.
|
|
|
|
pub fn point_light(&mut self, position: Vector3<f32>, color: [f32; 3]) {
|
2021-03-28 10:36:14 +00:00
|
|
|
let command_buffer = {
|
|
|
|
self.frame.system.point_lighting_system.draw(
|
2022-02-20 00:14:16 +00:00
|
|
|
self.frame.framebuffer.extent(),
|
2021-03-28 10:36:14 +00:00
|
|
|
self.frame.system.diffuse_buffer.clone(),
|
|
|
|
self.frame.system.normals_buffer.clone(),
|
|
|
|
self.frame.system.depth_buffer.clone(),
|
|
|
|
self.frame.world_to_framebuffer.invert().unwrap(),
|
|
|
|
position,
|
|
|
|
color,
|
|
|
|
)
|
|
|
|
};
|
2018-08-30 01:37:51 +00:00
|
|
|
|
2021-03-28 10:36:14 +00:00
|
|
|
self.frame
|
|
|
|
.command_buffer_builder
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
|
|
|
.execute_commands(command_buffer)
|
|
|
|
.unwrap();
|
2018-08-30 01:37:51 +00:00
|
|
|
}
|
|
|
|
}
|