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
use {
    super::{
        driver::{Device, Driver},
        op::{ClearOp, CopyOp, DrawOp, EncodeOp, FontOp, GradientOp, Op, WriteOp},
        pool::{Lease, Pool},
        Texture2d,
    },
    crate::{
        color::AlphaColor,
        math::{Coord, CoordF, Extent},
    },
    gfx_hal::{
        format::{Format, ImageFeature},
        image::{Layout, Usage},
    },
};

/// A powerful structure which allows you to combine various operations and other render
/// instances to create just about any creative effect.
pub struct Render {
    driver: Driver,
    pool: Option<Lease<Pool>>,
    target: Lease<Texture2d>,
    target_dirty: bool,
    ops: Vec<Box<dyn Op>>,
}

impl Render {
    pub(super) fn new(
        #[cfg(feature = "debug-names")] name: &str,
        driver: &Driver,
        dims: Extent,
        mut pool: Lease<Pool>,
        ops: Vec<Box<dyn Op>>,
    ) -> Self {
        let fmt = Device::best_fmt(
            &driver.borrow(),
            &[Format::Rgba8Unorm, Format::Bgra8Unorm],
            ImageFeature::COLOR_ATTACHMENT | ImageFeature::SAMPLED,
        )
        .unwrap();
        let target = pool.texture(
            #[cfg(feature = "debug-names")]
            name,
            driver,
            dims,
            fmt,
            Layout::Undefined,
            Usage::SAMPLED,
            1,
            1,
            1,
        );

        Self {
            driver: Driver::clone(driver),
            pool: Some(pool),
            target,
            target_dirty: false,
            ops,
        }
    }

    /// Clears the screen of all text and graphics.
    pub fn clear(&mut self, #[cfg(feature = "debug-names")] name: &str) -> &mut ClearOp {
        let pool = self.take_pool();
        let op = ClearOp::new(
            #[cfg(feature = "debug-names")]
            name,
            &self.driver,
            pool,
            &self.target,
        );

        self.target_dirty = true;

        self.ops.push(Box::new(op));
        self.ops
            .last_mut()
            .unwrap()
            .as_any_mut()
            .downcast_mut::<ClearOp>()
            .unwrap()
    }

    /// Copies the given texture onto this Render. The implementation uses a copy operation
    /// and is more efficient than `write` when there is no blending or fractional pixels.
    pub fn copy(
        &mut self,
        #[cfg(feature = "debug-names")] name: &str,
        src: &Texture2d,
    ) -> &mut CopyOp {
        let pool = self.take_pool();
        let op = CopyOp::new(
            #[cfg(feature = "debug-names")]
            name,
            &self.driver,
            pool,
            &src,
            &self.target,
        );

        self.target_dirty = true;

        self.ops.push(Box::new(op));
        self.ops
            .last_mut()
            .unwrap()
            .as_any_mut()
            .downcast_mut::<CopyOp>()
            .unwrap()
    }

    /// Gets the dimensions, in pixels, of this `Render`.
    pub fn dims(&self) -> Extent {
        self.target.borrow().dims()
    }

    /// Draws a batch of 3D elements. There is no need to give any particular order to the individual commands and the
    /// implementation may sort and re-order them, so do not count on indices remaining the same after this call completes.
    pub fn draw(&mut self, #[cfg(feature = "debug-names")] name: &str) -> &mut DrawOp {
        let pool = self.take_pool();
        let mut op = DrawOp::new(
            #[cfg(feature = "debug-names")]
            name,
            &self.driver,
            pool,
            &self.target,
        );

        if self.target_dirty {
            let _ = op.with_preserve(true);
        }

        self.target_dirty = true;
        self.ops.push(Box::new(op));
        self.ops
            .last_mut()
            .unwrap()
            .as_any_mut()
            .downcast_mut::<DrawOp>()
            .unwrap()
    }

    /// Saves this Render as a JPEG file at the given path.
    pub fn encode(&mut self, #[cfg(feature = "debug-names")] name: &str) -> &mut EncodeOp {
        let pool = self.take_pool();
        let op = EncodeOp::new(
            #[cfg(feature = "debug-names")]
            name,
            &self.driver,
            pool,
            &self.target,
        );

        self.ops.push(Box::new(op));
        self.ops
            .last_mut()
            .unwrap()
            .as_any_mut()
            .downcast_mut::<EncodeOp>()
            .unwrap()
    }

    /// Draws a linear gradient on this Render using the given path.
    /// TODO: Specialize for radial too?
    pub fn gradient<C>(
        &mut self,
        #[cfg(feature = "debug-names")] name: &str,
        path: [(Coord, C); 2],
    ) -> &mut EncodeOp
    where
        C: Copy + Into<AlphaColor>,
    {
        let pool = self.take_pool();
        let op = GradientOp::new(
            #[cfg(feature = "debug-names")]
            name,
            &self.driver,
            pool,
            &self.target,
            [(path[0].0, path[0].1.into()), (path[1].0, path[1].1.into())],
        );

        self.target_dirty = true;

        self.ops.push(Box::new(op));
        self.ops
            .last_mut()
            .unwrap()
            .as_any_mut()
            .downcast_mut::<EncodeOp>()
            .unwrap()
    }

    pub(crate) fn resolve(self) -> (Lease<Texture2d>, Vec<Box<dyn Op>>) {
        (self.target, self.ops)
    }

    fn take_pool(&mut self) -> Lease<Pool> {
        self.pool
            .take()
            .unwrap_or_else(|| self.ops.last_mut().unwrap().take_pool().unwrap())
    }

    /// Draws bitmapped text on this Render using the given details.
    /// TODO: Accept a list of font/color/text/pos combos so we can batch many at once?
    pub fn text<C, P>(
        &mut self,
        #[cfg(feature = "debug-names")] name: &str,
        pos: P,
        color: C,
    ) -> &mut FontOp
    where
        C: Into<AlphaColor>,
        P: Into<CoordF>,
    {
        let pool = self.take_pool();
        let op = FontOp::new(
            #[cfg(feature = "debug-names")]
            name,
            &self.driver,
            pool,
            &self.target,
            pos,
            color,
        );

        self.target_dirty = true;

        self.ops.push(Box::new(op));
        self.ops
            .last_mut()
            .unwrap()
            .as_any_mut()
            .downcast_mut::<FontOp>()
            .unwrap()
    }

    /// Draws the given texture writes onto this Render. Note that the given texture writes will all be applied at once and there
    /// is no 'layering' of the individual writes going on - so if you need blending between writes you must submit a new batch.
    pub fn write(&mut self, #[cfg(feature = "debug-names")] name: &str) -> &mut WriteOp {
        let pool = self.take_pool();
        let mut op = WriteOp::new(
            #[cfg(feature = "debug-names")]
            name,
            &self.driver,
            pool,
            &self.target,
        );

        if self.target_dirty {
            let _ = op.with_preserve();
        }

        self.target_dirty = true;

        self.ops.push(Box::new(op));
        self.ops
            .last_mut()
            .unwrap()
            .as_any_mut()
            .downcast_mut::<WriteOp>()
            .unwrap()
    }
}