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
use gfx_hal::device::Device;

use super::{
  gpu::Gpu,
  surface::ColorFormat,
};

// ----------------------- RENDER ATTACHMENT OPERATIONS ------------------------

#[derive(Debug)]
pub enum Operations {
  DontCare,
  Load,
  Clear,
  Store,
}

impl Operations {
  fn to_gfx_hal_load_operation(&self) -> gfx_hal::pass::AttachmentLoadOp {
    match self {
      Operations::DontCare => gfx_hal::pass::AttachmentLoadOp::DontCare,
      Operations::Load => gfx_hal::pass::AttachmentLoadOp::Load,
      Operations::Clear => gfx_hal::pass::AttachmentLoadOp::Clear,
      _ => panic!("Cannot pass in {:?} as an operation for the attachment load operation!", self)
    }
  }

  fn to_gfx_hal_store_operation(&self) -> gfx_hal::pass::AttachmentStoreOp {
    return match self {
      Operations::DontCare => gfx_hal::pass::AttachmentStoreOp::DontCare,
      Operations::Store => gfx_hal::pass::AttachmentStoreOp::Store,
      _ => panic!(
        "Cannot pass in {:?} as an operation for the attachment store operation!",
        self
      ),
    };
  }
}

// ----------------------------- RENDER ATTACHMENT -----------------------------

///
pub struct AttachmentBuilder {
  samples: u8,
  color_format: Option<ColorFormat>,
  load_operation: gfx_hal::pass::AttachmentLoadOp,
  store_operation: gfx_hal::pass::AttachmentStoreOp,
}

/// builder for a render attachment
impl AttachmentBuilder {
  pub fn new() -> Self {
    return Self {
      samples: 0,
      color_format: None,
      load_operation: gfx_hal::pass::AttachmentLoadOp::DontCare,
      store_operation: gfx_hal::pass::AttachmentStoreOp::DontCare,
    };
  }

  ///  Sets the number of samples to use for the attachment.
  pub fn with_samples(mut self, samples: u8) -> Self {
    self.samples = samples;
    return self;
  }

  /// Sets the color format to use for the attachment.
  pub fn with_color_format(mut self, color_format: ColorFormat) -> Self {
    self.color_format = Some(color_format);
    return self;
  }

  /// Sets the load operation for the attachment.
  pub fn on_load(mut self, operation: Operations) -> Self {
    self.load_operation = operation.to_gfx_hal_load_operation();
    return self;
  }

  ///
  pub fn on_store(mut self, operation: Operations) -> Self {
    self.store_operation = operation.to_gfx_hal_store_operation();
    return self;
  }

  /// Builds a render attachment that can be used within a render pass.
  pub fn build(self) -> Attachment {
    return Attachment {
      attachment: gfx_hal::pass::Attachment {
        format: self.color_format,
        samples: self.samples,
        ops: gfx_hal::pass::AttachmentOps::new(
          self.load_operation,
          self.store_operation,
        ),
        stencil_ops: gfx_hal::pass::AttachmentOps::DONT_CARE,
        layouts: gfx_hal::image::Layout::Undefined
          ..gfx_hal::image::Layout::Present,
      },
    };
  }
}

pub struct Attachment {
  attachment: gfx_hal::pass::Attachment,
}

impl Attachment {
  fn gfx_hal_attachment(&self) -> gfx_hal::pass::Attachment {
    return self.attachment.clone();
  }
}

// ------------------------------ RENDER SUBPASS -------------------------------

pub use gfx_hal::image::Layout as ImageLayoutHint;

pub struct SubpassBuilder {
  color_attachment: Option<(usize, ImageLayoutHint)>,
}

impl SubpassBuilder {
  pub fn new() -> Self {
    return Self {
      color_attachment: None,
    };
  }

  pub fn with_color_attachment(
    mut self,
    attachment_index: usize,
    layout: ImageLayoutHint,
  ) -> Self {
    self.color_attachment = Some((attachment_index, layout));
    return self;
  }
  pub fn with_inputs() {
    todo!("Implement input support for subpasses")
  }
  pub fn with_resolves() {
    todo!("Implement resolving support for subpasses")
  }
  pub fn with_preserves() {
    todo!("Implement preservation support for subpasses")
  }

  pub fn build<'a>(self) -> Subpass<'a> {
    return Subpass {
      subpass: gfx_hal::pass::SubpassDesc {
        colors: &[(0, ImageLayoutHint::ColorAttachmentOptimal)],
        depth_stencil: None,
        inputs: &[],
        resolves: &[],
        preserves: &[],
      },
    };
  }
}

pub struct Subpass<'a> {
  subpass: gfx_hal::pass::SubpassDesc<'a>,
}

impl<'a> Subpass<'a> {
  fn gfx_hal_subpass(self) -> gfx_hal::pass::SubpassDesc<'a> {
    return self.subpass;
  }
}

// -------------------------------- RENDER PASS --------------------------------

pub struct RenderPassBuilder<'builder> {
  attachments: Vec<Attachment>,
  subpasses: Vec<Subpass<'builder>>,
}

impl<'builder> RenderPassBuilder<'builder> {
  pub fn new() -> Self {
    return Self {
      attachments: vec![],
      subpasses: vec![],
    };
  }

  /// Adds an attachment to the render pass. Can add multiple.
  pub fn add_attachment(mut self, attachment: Attachment) -> Self {
    self.attachments.push(attachment);
    return self;
  }

  pub fn add_subpass(mut self, subpass: Subpass<'builder>) -> Self {
    self.subpasses.push(subpass);
    return self;
  }

  pub fn build<RenderBackend: gfx_hal::Backend>(
    self,
    gpu: &Gpu<RenderBackend>,
  ) -> RenderPass<RenderBackend> {
    // If there are no attachments, use a stub image attachment with clear and
    // store operations.
    let attachments = match self.attachments.is_empty() {
      true => vec![AttachmentBuilder::new()
        .with_samples(1)
        .on_load(Operations::Clear)
        .on_store(Operations::Store)
        .with_color_format(ColorFormat::Rgba8Srgb)
        .build()
        .gfx_hal_attachment()],
      false => self
        .attachments
        .into_iter()
        .map(|attachment| attachment.gfx_hal_attachment())
        .collect(),
    };

    // If there are no subpass descriptions, use a stub subpass attachment
    let subpasses = match self.subpasses.is_empty() {
      true => vec![SubpassBuilder::new().build().gfx_hal_subpass()],
      false => self
        .subpasses
        .into_iter()
        .map(|subpass| subpass.gfx_hal_subpass())
        .collect(),
    };

    let render_pass = unsafe {
      gpu.internal_logical_device().create_render_pass(
        attachments.into_iter(),
        subpasses.into_iter(),
        vec![].into_iter(),
      )
    }
    .expect("The GPU does not have enough memory to allocate a render pass.");

    return RenderPass { render_pass };
  }
}

#[derive(Debug)]
pub struct RenderPass<RenderBackend: gfx_hal::Backend> {
  render_pass: RenderBackend::RenderPass,
}

impl<RenderBackend: gfx_hal::Backend> RenderPass<RenderBackend> {
  pub fn destroy(self, gpu: &Gpu<RenderBackend>) {
    unsafe {
      gpu
        .internal_logical_device()
        .destroy_render_pass(self.render_pass);
    }
  }
}

impl<RenderBackend: gfx_hal::Backend> RenderPass<RenderBackend> {
  pub(super) fn internal_render_pass(&self) -> &RenderBackend::RenderPass {
    return &self.render_pass;
  }
}