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
use std::marker;

use crate::image::format::{InternalFormat, Multisamplable};
use crate::rendering::attachment::{AsMultisampleAttachment, AttachmentData};
use crate::rendering::load_op::LoadAction;
use crate::rendering::AsAttachment;
use crate::rendering::{
    FloatBuffer, IntegerBuffer, LoadOp, RenderingOutputBuffer, StoreOp, UnsignedIntegerBuffer,
};

/// Helper trait implemented by types that describe a color image attachment for a [RenderTarget].
pub trait EncodeColorBuffer {
    /// The type of [RenderingOutputBuffer] that is allocated in the framebuffer to buffer
    /// modifications to the attached image.
    type Buffer: RenderingOutputBuffer;

    /// Returns an encoding of the information needed by a [RenderPass] to load data from the
    /// attached image into the framebuffer before the render pass, and to store data from the
    /// framebuffer back into the attached image after the render pass.
    fn encode_color_buffer<'a, 'b>(
        &'a mut self,
        context: &'b mut ColorBufferEncodingContext,
    ) -> ColorBufferEncoding<'b, 'a, Self::Buffer>;
}

/// Helper trait implemented by types that describe a multisample color image attachment for a
/// [MultisampleRenderTarget].
pub trait EncodeMultisampleColorBuffer {
    /// The type of [RenderingOutputBuffer] that is allocated in the framebuffer to buffer
    /// modifications to the attached image.
    type Buffer: RenderingOutputBuffer;

    /// Returns an encoding of the information needed by a [RenderPass] to load data from the
    /// attached image into the framebuffer before the render pass, and to store data from the
    /// framebuffer back into the attached image after the render pass.
    fn encode_multisample_color_buffer<'a, 'b>(
        &'a mut self,
        context: &'b mut ColorBufferEncodingContext,
    ) -> ColorBufferEncoding<'b, 'a, Self::Buffer>;
}

/// Provides the context for encoding a [ColorAttachmentDescription].
///
/// See [ColorAttachmentDescription::encode].
pub struct ColorBufferEncodingContext {
    pub(crate) render_pass_id: u64,
    pub(crate) buffer_index: i32,
}

/// An encoding of the information needed by a [RenderPass] to load data from an attached image
/// into the framebuffer before the render pass, and to store data from the framebuffer back into
/// the attached image after the render pass.
pub struct ColorBufferEncoding<'a, 'b, B> {
    pub(crate) buffer: B,
    pub(crate) load_action: LoadAction,
    pub(crate) store_op: StoreOp,
    pub(crate) image: AttachmentData,
    pub(crate) _context: &'a mut ColorBufferEncodingContext,
    pub(crate) _image_ref: marker::PhantomData<&'b ()>,
}

impl<'a, 'b, F> ColorBufferEncoding<'a, 'b, FloatBuffer<F>>
where
    F: InternalFormat,
{
    pub fn float_attachment<I>(
        context: &'a mut ColorBufferEncodingContext,
        image: &'b mut I,
        load_op: LoadOp<[f32; 4]>,
        store_op: StoreOp,
    ) -> Self
    where
        I: AsAttachment<Format = F>,
    {
        let image = image.as_attachment().into_data();

        ColorBufferEncoding {
            buffer: FloatBuffer::new(
                context.render_pass_id,
                context.buffer_index,
                image.width,
                image.height,
            ),
            load_action: load_op.as_load_float_action(context.buffer_index),
            store_op,
            image,
            _context: context,
            _image_ref: marker::PhantomData,
        }
    }

    pub fn multisample_float_attachment<I>(
        context: &'a mut ColorBufferEncodingContext,
        image: &'b mut I,
        load_op: LoadOp<[f32; 4]>,
        store_op: StoreOp,
    ) -> Self
    where
        I: AsMultisampleAttachment<SampleFormat = F>,
        F: Multisamplable,
    {
        let image = image.as_multisample_attachment().into_data();

        ColorBufferEncoding {
            buffer: FloatBuffer::new(
                context.render_pass_id,
                context.buffer_index,
                image.width,
                image.height,
            ),
            load_action: load_op.as_load_float_action(context.buffer_index),
            store_op,
            image,
            _context: context,
            _image_ref: marker::PhantomData,
        }
    }
}

impl<'a, 'b, F> ColorBufferEncoding<'a, 'b, IntegerBuffer<F>>
where
    F: InternalFormat,
{
    pub fn integer_attachment<I>(
        context: &'a mut ColorBufferEncodingContext,
        image: &'b mut I,
        load_op: LoadOp<[i32; 4]>,
        store_op: StoreOp,
    ) -> Self
    where
        I: AsAttachment<Format = F>,
    {
        let image = image.as_attachment().into_data();

        ColorBufferEncoding {
            buffer: IntegerBuffer::new(
                context.render_pass_id,
                context.buffer_index,
                image.width,
                image.height,
            ),
            load_action: load_op.as_load_integer_action(context.buffer_index),
            store_op,
            image,
            _context: context,
            _image_ref: marker::PhantomData,
        }
    }
}

impl<'a, 'b, F> ColorBufferEncoding<'a, 'b, UnsignedIntegerBuffer<F>>
where
    F: InternalFormat,
{
    pub fn unsigned_integer_attachment<I>(
        context: &'a mut ColorBufferEncodingContext,
        image: &'b mut I,
        load_op: LoadOp<[u32; 4]>,
        store_op: StoreOp,
    ) -> Self
    where
        I: AsAttachment<Format = F>,
    {
        let image = image.as_attachment().into_data();

        ColorBufferEncoding {
            buffer: UnsignedIntegerBuffer::new(
                context.render_pass_id,
                context.buffer_index,
                image.width,
                image.height,
            ),
            load_action: load_op.as_load_unsigned_integer_action(context.buffer_index),
            store_op,
            image,
            _context: context,
            _image_ref: marker::PhantomData,
        }
    }
}

pub struct FloatAttachment<I> {
    pub(crate) image: I,
    pub(crate) load_op: LoadOp<[f32; 4]>,
    pub(crate) store_op: StoreOp,
}

impl<I> EncodeColorBuffer for FloatAttachment<I>
where
    I: AsAttachment,
{
    type Buffer = FloatBuffer<I::Format>;

    fn encode_color_buffer<'a, 'b>(
        &'a mut self,
        context: &'b mut ColorBufferEncodingContext,
    ) -> ColorBufferEncoding<'b, 'a, Self::Buffer> {
        ColorBufferEncoding::float_attachment(context, &mut self.image, self.load_op, self.store_op)
    }
}

impl<I> EncodeMultisampleColorBuffer for FloatAttachment<I>
where
    I: AsMultisampleAttachment,
{
    type Buffer = FloatBuffer<I::SampleFormat>;

    fn encode_multisample_color_buffer<'a, 'b>(
        &'a mut self,
        context: &'b mut ColorBufferEncodingContext,
    ) -> ColorBufferEncoding<'b, 'a, Self::Buffer> {
        ColorBufferEncoding::multisample_float_attachment(
            context,
            &mut self.image,
            self.load_op,
            self.store_op,
        )
    }
}

pub struct IntegerAttachment<I> {
    pub(crate) image: I,
    pub(crate) load_op: LoadOp<[i32; 4]>,
    pub(crate) store_op: StoreOp,
}

impl<I> EncodeColorBuffer for IntegerAttachment<I>
where
    I: AsAttachment,
{
    type Buffer = IntegerBuffer<I::Format>;

    fn encode_color_buffer<'a, 'b>(
        &'a mut self,
        context: &'b mut ColorBufferEncodingContext,
    ) -> ColorBufferEncoding<'b, 'a, Self::Buffer> {
        ColorBufferEncoding::integer_attachment(
            context,
            &mut self.image,
            self.load_op,
            self.store_op,
        )
    }
}

pub struct UnsignedIntegerAttachment<I> {
    pub(crate) image: I,
    pub(crate) load_op: LoadOp<[u32; 4]>,
    pub(crate) store_op: StoreOp,
}

impl<I> EncodeColorBuffer for UnsignedIntegerAttachment<I>
where
    I: AsAttachment,
{
    type Buffer = UnsignedIntegerBuffer<I::Format>;

    fn encode_color_buffer<'a, 'b>(
        &'a mut self,
        context: &'b mut ColorBufferEncodingContext,
    ) -> ColorBufferEncoding<'b, 'a, Self::Buffer> {
        ColorBufferEncoding::unsigned_integer_attachment(
            context,
            &mut self.image,
            self.load_op,
            self.store_op,
        )
    }
}