1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use std::sync::Arc;
use vulkano::{command_buffer::{AutoCommandBufferBuilder, SubpassContents}, device::Queue, format::Format, framebuffer::Framebuffer, framebuffer::{FramebufferAbstract, RenderPassAbstract, Subpass}, image::AttachmentImage, image::ImageUsage, image::{ImageViewAbstract, SwapchainImage, view::ImageView}, sync::GpuFuture};
use feo_math::{linear_algebra::{matrix4::Matrix4}, utils::space::Space};
use winit::window::Window;
use super::{draw_pass_manager::DrawPassManager, lighting_pass_manager::LightingPassManager, pass_builder::PassBuilder};

/// System that contains the necessary facilities for rendering a single frame.
pub struct FrameSystem {
    pub(crate) gfx_queue: Arc<Queue>,

    render_pass: Arc<dyn RenderPassAbstract + Send + Sync>,
    framebuffers: Vec<Arc<dyn FramebufferAbstract + Send + Sync>>,

    pub(crate) diffuse_buffer: Arc<ImageView<Arc<AttachmentImage>>>,
    pub(crate) normals_buffer: Arc<ImageView<Arc<AttachmentImage>>>,
    pub(crate) ambient_buffer: Arc<ImageView<Arc<AttachmentImage>>>,
    pub(crate) specular_buffer: Arc<ImageView<Arc<AttachmentImage>>>,
    pub(crate) depth_buffer: Arc<ImageView<Arc<AttachmentImage>>>,
    
    // pub(crate) light_depth_buffer: Arc<ImageView<Arc<AttachmentImage>>>,
    
    pub(crate) lighting_pass_manager: LightingPassManager,
    pub(crate) draw_pass_manager: DrawPassManager,
}

impl FrameSystem {
    /// Creates and initializes a new frame system.
    pub fn new(gfx_queue: Arc<Queue>, final_output_format: Format, dims: [u32; 2]) -> FrameSystem {
        let render_pass = Arc::new(
            vulkano::ordered_passes_renderpass!(gfx_queue.device().clone(),
                attachments: {
                    // The final rendering
                    final_color: {
                        load: Clear,
                        store: Store,
                        format: final_output_format,
                        samples: 1,
                    },
                    // Will be bound to `self.normals_buffer`.
                    normals: {
                        load: Clear,
                        store: DontCare,
                        format: Format::R16G16B16A16Sfloat,
                        samples: 1,
                    },
                    // Will be bound to `self.diffuse_buffer`.
                    diffuse: {
                        load: Clear,
                        store: DontCare,
                        format: Format::A2B10G10R10UnormPack32,
                        samples: 1,
                    },
                    // ambient reflected
                    ambient: {
                        load: Clear,
                        store: DontCare,
                        format: Format::A2B10G10R10UnormPack32,
                        samples: 1,
                    },
                    // specular reflected
                    specular: {
                        load: Clear,
                        store: DontCare,
                        format: Format::A2B10G10R10UnormPack32,
                        samples: 1,
                    },
                    // Will be bound to `self.depth_buffer`.
                    depth: {
                        load: Clear,
                        store: DontCare,
                        format: Format::D16Unorm,
                        samples: 1,
                    }// ,
                    // // Will be bound to `self.depth_buffer`.
                    // light_depth: {
                    //     load: Clear,
                    //     store: DontCare,
                    //     format: Format::D16Unorm,
                    //     samples: 1,
                    // }
                },
                passes: [
                    // Write to the diffuse, normals and depth attachments.
                    {
                        color: [normals, diffuse, ambient, specular], // albedo
                        depth_stencil: {depth},
                        input: []
                    },
                    
                    // // TODO: Shadows
                    // {
                    //     color: [],
                    //     depth_stencil: {light_depth},
                    //     input: [todo]
                    // },

                    // Apply lighting by reading these three attachments and writing to `final_color`.
                    {
                        color: [final_color],
                        depth_stencil: {},
                        input: [normals, diffuse, ambient, specular, depth/*, light_depth*/]
                    }
                ]
            ).unwrap(),
        );

        let atch_usage = ImageUsage {
            transient_attachment: true,
            input_attachment: true,
            ..ImageUsage::none()
        };
        
        let depth_buffer = ImageView::new(
            AttachmentImage::with_usage(
                gfx_queue.device().clone(),
                dims,
                Format::D16Unorm,
                atch_usage,
            ).unwrap(),
        ).unwrap();
        // let light_depth_buffer = ImageView::new(
        //     AttachmentImage::with_usage(
        //         gfx_queue.device().clone(),
        //         dims,
        //         Format::D16Unorm,
        //         atch_usage,
        //     ).unwrap(),
        // ).unwrap();
        let normals_buffer = ImageView::new(
            AttachmentImage::with_usage(
                gfx_queue.device().clone(),
                dims,
                Format::R16G16B16A16Sfloat,
                atch_usage,
            ).unwrap(),
        ).unwrap();
        let diffuse_buffer = ImageView::new(
            AttachmentImage::with_usage(
                gfx_queue.device().clone(),
                dims,
                Format::A2B10G10R10UnormPack32,
                atch_usage,
            ).unwrap(),
        ).unwrap();
        let ambient_buffer = ImageView::new(
            AttachmentImage::with_usage(
                gfx_queue.device().clone(),
                dims,
                Format::A2B10G10R10UnormPack32,
                atch_usage,
            ).unwrap(),
        ).unwrap();
        let specular_buffer = ImageView::new(
            AttachmentImage::with_usage(
                gfx_queue.device().clone(),
                dims,
                Format::A2B10G10R10UnormPack32,
                atch_usage,
            ).unwrap(),
        ).unwrap();

        // TODO: Shadows
        // let shadow_subpass = Subpass::from(render_pass.clone(), 1).unwrap();
        // let shadow_system = AmbientLightingSystem::new(gfx_queue.clone(), lighting_subpass.clone());
        // let shadow_system = AmbientLightingSystem::new(gfx_queue.clone(), lighting_subpass.clone());
        let draw_pass_manager = DrawPassManager::new(gfx_queue.clone(), Subpass::from(render_pass.clone(), 0).unwrap(), dims);
        let lighting_pass_manager = LightingPassManager::new(gfx_queue.clone(), Subpass::from(render_pass.clone(), 1).unwrap());

        FrameSystem {
            gfx_queue,
            render_pass: render_pass as Arc<_>,
            framebuffers: Vec::new(),
            diffuse_buffer,
            ambient_buffer,
            specular_buffer,
            normals_buffer,
            depth_buffer,
            // light_depth_buffer, // TODO

            lighting_pass_manager,
            draw_pass_manager,
        }
    }

    // Builds a passbuilder for a pass
    pub fn pass_builder<F>(
        &mut self,
        before_future: F,
        img_num: usize,
        screen_to_camera: Matrix4<f32>,
        to_camera_space: Space) -> PassBuilder
    where F: GpuFuture + 'static {
        let framebuffer = self.framebuffers[img_num].clone();

        // Start the command buffer builder that will be filled throughout the frame handling.
        let mut command_buffer_builder = AutoCommandBufferBuilder::primary_one_time_submit(
            self.gfx_queue.device().clone(),
            self.gfx_queue.family(),
        ).unwrap();

        command_buffer_builder.begin_render_pass(
            framebuffer.clone(),
            SubpassContents::SecondaryCommandBuffers,
            vec![
                [0.0, 0.0, 0.0, 0.0].into(),
                [0.0, 0.0, 0.0, 0.0].into(),
                [0.0, 0.0, 0.0, 0.0].into(),
                [0.0, 0.0, 0.0, 0.0].into(),
                [0.0, 0.0, 0.0, 0.0].into(),
                1.0_f32.into(),
            ],
        ).unwrap();

        PassBuilder {
            system: self,
            before_main_cb_future: Some(Box::new(before_future)),
            framebuffer,
            command_buffer_builder: Some(command_buffer_builder),
            screen_to_camera,
            to_camera_space
        }
    }
    
    // Rebuilds the FrameSystem with the required dimentions
    pub fn rebuild_dims(&mut self, images: &[Arc<ImageView<Arc<SwapchainImage<Window>>>>]){
        let dimensions = &images[0].clone().image().dimensions().width_height();
        self.draw_pass_manager.rebuild(dimensions, self.render_pass.clone());
        
        let atch_usage = ImageUsage {
            transient_attachment: true,
            input_attachment: true,
            ..ImageUsage::none()
        };
        
        self.depth_buffer = ImageView::new(
            AttachmentImage::with_usage(
                self.gfx_queue.device().clone(),
                *dimensions,
                Format::D16Unorm,
                atch_usage,
            ).unwrap(),
        ).unwrap();
        self.normals_buffer = ImageView::new(
            AttachmentImage::with_usage(
                self.gfx_queue.device().clone(),
                *dimensions,
                Format::R16G16B16A16Sfloat,
                atch_usage,
            ).unwrap(),
        ).unwrap();
        self.diffuse_buffer = ImageView::new(
            AttachmentImage::with_usage(
                self.gfx_queue.device().clone(),
                *dimensions,
                Format::A2B10G10R10UnormPack32,
                atch_usage,
            ).unwrap(),
        ).unwrap();
        self.ambient_buffer = ImageView::new(
            AttachmentImage::with_usage(
                self.gfx_queue.device().clone(),
                *dimensions,
                Format::A2B10G10R10UnormPack32,
                atch_usage,
            ).unwrap(),
        ).unwrap();
        self.specular_buffer = ImageView::new(
            AttachmentImage::with_usage(
                self.gfx_queue.device().clone(),
                *dimensions,
                Format::A2B10G10R10UnormPack32,
                atch_usage,
            ).unwrap(),
        ).unwrap();

        self.framebuffers = images.iter()
            .map(|image| 
                Arc::new(
                    Framebuffer::start(self.render_pass.clone())
                        .add(image.clone()).unwrap()
                        .add(self.normals_buffer.clone()).unwrap()
                        .add(self.diffuse_buffer.clone()).unwrap()
                        .add(self.ambient_buffer.clone()).unwrap()
                        .add(self.specular_buffer.clone()).unwrap()
                        .add(self.depth_buffer.clone()).unwrap()
                        .build().unwrap(),
                ) as Arc<dyn FramebufferAbstract + Send + Sync>
            ).collect::<Vec<_>>();
    }
}