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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Drawing and Dispatching related commands.

use crate::__gl;
use crate::{Device, Filter, Framebuffer, Pipeline, Region};
use std::{mem, ops::Range};

/// Primitve topology.
///
/// Specifies how the input assembler (fixed-function) of the graphics pipeline will
/// assemble primitives based on the incoming vertex data.
#[repr(u32)]
#[derive(Copy, Clone, Debug)]
pub enum Primitive {
    /// Independent vertex points.
    ///
    /// One vertex corresponds to one point. The size of a point can be changed
    /// dynamically in the vertex, geometry or tessellation evaluation stage.
    /// A point is rendered as square.
    ///
    /// GLSL: `gl_PointSize`.
    Points = __gl::POINTS,

    /// Lines segment list.
    ///
    /// Every two consecutive vertices will form a line segment.
    Lines = __gl::LINES,

    /// Lines segment strip.
    ///
    /// The vertices will build a connected list of line segments.
    LineStrip = __gl::LINE_STRIP,

    /// Triangle list.
    ///
    /// Three consecutive vertices will form triangle.
    Triangles = __gl::TRIANGLES,
    TriangleStrip = __gl::TRIANGLE_STRIP,
    LinesAdjacency = __gl::LINES_ADJACENCY,
    LinesStripAdjacency = __gl::LINE_STRIP_ADJACENCY,
    TrianglesAdjacency = __gl::TRIANGLES_ADJACENCY,
    TrianglesStripAdjacency = __gl::TRIANGLE_STRIP_ADJACENCY,
    Patches = __gl::PATCHES,
}

/// Index size.
///
/// Specifies the size of indices during indexed draw calls.
#[repr(u32)]
#[derive(Copy, Clone, Debug)]
pub enum IndexTy {
    /// 8-bit unsigned integer.
    U8 = __gl::UNSIGNED_BYTE,
    // 16-bit unsigned integer.
    U16 = __gl::UNSIGNED_SHORT,
    // 32-bit unsigned integer.
    U32 = __gl::UNSIGNED_INT,
}

impl IndexTy {
    fn size(self) -> u32 {
        match self {
            IndexTy::U8 => 1,
            IndexTy::U16 => 2,
            IndexTy::U32 => 4,
        }
    }
}

/// Viewport transformation.
///
/// Viewport transformation is part of the fixed-function Vertex Post-Processing pipeline.
/// The returning vertex positions (GLSL: `gl_Position`) in clip space are transformed to
/// normalized device coordinates (NDC) by perspective division. Viewport transformation
/// further converts the NDC into framebuffer coordinates.
///
/// During the geometry a primitive will be assigned a viewport index (GLSL: `gl_ViewportIndex`)
/// either automatically or manually. This index controls which viewport from the bounded
/// viewports will be selected for applying the transformation for the current **primitive**.
pub struct Viewport {
    /// Offset (x).
    pub x: f32,
    /// Offset (y).
    pub y: f32,
    /// Width.
    pub w: f32,
    /// Height.
    pub h: f32,
    // Near.
    pub n: f64,
    // Far.
    pub f: f64,
}

/// Uniform constant.
///
/// Small values which can be written directly by the API.
/// No additional buffers and binding calls are required.
///
/// ## Example
///
/// GLSL: `layout (location = 0) uniform mat4 u_perspective;`
pub enum Constant {
    /// 32-bit unsigned integer.
    U32(u32),
    /// 32-bit single precision floating point.
    F32(f32),
    /// 2 elements single precision floating point vector.
    Vec2([f32; 2]),
    /// 3 elements single precision floating point vector.
    Vec3([f32; 3]),
    /// 4 elements single precision floating point vector.
    Vec4([f32; 4]),
    /// 3x3 elements single precision floating point matrix.
    Mat3x3([[f32; 3]; 3]),
    /// 4x4 elements single precision floating point matrix.
    Mat4x4([[f32; 4]; 4]),
}

/// Indirect draw command structure.
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct DrawIndirectCmd {
    ///
    pub vertex_count: u32,
    ///
    pub instance_count: u32,
    ///
    pub first_vertex: u32,
    ///
    pub first_instance: u32,
}

/// Indirect (indexed) draw command structure.
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct DrawIndexedIndirectCmd {
    ///
    pub index_count: u32,
    ///
    pub instance_count: u32,
    ///
    pub first_index: u32,
    ///
    pub base_vertex: i32,
    ///
    pub first_instance: u32,
}

/// Indirect dispatch command structure.
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct DispatchIndirectCmd {
    /// Number of local workgroups in x dimension.
    pub x: u32,
    /// Number of local workgroups in y dimension.
    pub y: u32,
    /// Number of local workgroups in z dimension.
    pub z: u32,
}

impl Device {
    /// Set uniform constants for a pipeline.
    pub unsafe fn bind_uniform_constants(
        &self,
        pipeline: Pipeline,
        first: u32,
        constants: &[Constant],
    ) {
        for (i, constant) in constants.iter().enumerate() {
            let location = first as i32 + i as i32;
            match constant {
                Constant::U32(val) => {
                    self.0.ProgramUniform1ui(pipeline.0, location, *val as _);
                }
                Constant::F32(val) => {
                    self.0.ProgramUniform1f(pipeline.0, location, *val as _);
                }
                Constant::Vec2(v) => {
                    self.0.ProgramUniform2f(pipeline.0, location, v[0], v[1]);
                }
                Constant::Vec3(v) => {
                    self.0
                        .ProgramUniform3f(pipeline.0, location, v[0], v[1], v[2]);
                }
                Constant::Vec4(v) => {
                    self.0
                        .ProgramUniform4f(pipeline.0, location, v[0], v[1], v[2], v[3]);
                }
                Constant::Mat3x3(mat) => {
                    self.0.ProgramUniformMatrix3fv(
                        pipeline.0,
                        location,
                        1,
                        __gl::FALSE,
                        mat.as_ptr() as *const _,
                    );
                }
                Constant::Mat4x4(mat) => {
                    self.0.ProgramUniformMatrix4fv(
                        pipeline.0,
                        location,
                        1,
                        __gl::FALSE,
                        mat.as_ptr() as *const _,
                    );
                }
            }
        }
    }

    /// Set viewport transformation parameters.
    ///
    /// The viewport determines the mapping from NDC (normalized device coordinates)
    /// into framebuffer coordinates.
    ///
    /// See [Viewport](../command/struct.Viewport.html) for more information
    /// about the viewport transformation.
    pub unsafe fn set_viewport(&self, first: u32, viewports: &[Viewport]) {
        let rects = viewports
            .iter()
            .flat_map(|viewport| vec![viewport.x, viewport.y, viewport.w, viewport.h])
            .collect::<Vec<_>>();

        self.0
            .ViewportArrayv(first, viewports.len() as _, rects.as_ptr());

        let depth_ranges = viewports
            .iter()
            .flat_map(|viewport| vec![viewport.n, viewport.f])
            .collect::<Vec<_>>();

        self.0
            .DepthRangeArrayv(first, viewports.len() as _, depth_ranges.as_ptr());
    }

    /// Set scissor rectangles for viewports.
    ///
    /// # Valid usage
    ///
    /// - Every active viewport needs an associated scissor.
    pub unsafe fn set_scissor(&self, first: u32, scissors: &[Region]) {
        let scissors_raw = scissors
            .iter()
            .flat_map(|scissor| vec![scissor.x, scissor.y, scissor.w, scissor.h])
            .collect::<Vec<_>>();

        self.0
            .ScissorArrayv(first, scissors.len() as _, scissors_raw.as_ptr());
    }

    /// Set depth bias factors.
    pub unsafe fn set_depth_bias(&self, constant_factor: f32, slope_factor: f32) {
        self.0.PolygonOffset(slope_factor, constant_factor);
    }

    /// Submit a (non-indexed) draw call.
    ///
    /// # Valid usage
    ///
    /// - There must be a valid graphics pipeline currently bound.
    /// - There must be a valid vertex array currently bound.
    /// - For each attribute in the bound vertex array there must be a vertex buffer bound
    ///   at the specified binding slot.
    /// - For each attribute in the bound vertex array there must be a vertex attribute
    ///   specified in the shader with matching format and location.
    /// - The access vertices must be in bound of the vertex buffers bound.
    /// - `vertices.end` must be larger than `vertices.start`.
    /// - `vertices.end - vertices.start` must be allow assembling complete primitives.
    /// - `instances.end` must be larger than `instances.start`.
    pub unsafe fn draw(&self, primitive: Primitive, vertices: Range<u32>, instance: Range<u32>) {
        self.0.DrawArraysInstancedBaseInstance(
            primitive as _,
            vertices.start as _,
            (vertices.end - vertices.start) as _,
            (instance.end - instance.start) as _,
            instance.start as _,
        );
    }

    /// Submit an indexed draw call.
    ///
    /// # Valid usage
    ///
    /// - There must be a valid graphics pipeline currently bound.
    /// - There must be a valid vertex array currently bound.
    /// - For each attribute in the bound vertex array there must be a vertex buffer bound
    ///   at the specified binding slot.
    /// - For each attribute in the bound vertex array there must be a vertex attribute
    ///   specified in the shader with matching format and location.
    /// - The access vertices must be in bound of the vertex buffers bound.
    /// - `indices.end` must be larger than `indices.start`.
    /// - `indices.end - indices.start` must allow to assemble complete primitives.
    /// - `instances.end` must be larger than `instances.start`.
    pub unsafe fn draw_indexed(
        &self,
        primitive: Primitive,
        index_ty: IndexTy,
        indices: Range<u32>,
        instance: Range<u32>,
        base_vertex: i32,
    ) {
        self.0.DrawElementsInstancedBaseVertexBaseInstance(
            primitive as _,
            (indices.end - indices.start) as _,
            index_ty as _,
            (index_ty.size() * indices.start) as _,
            (instance.end - instance.start) as _,
            base_vertex,
            instance.start as _,
        );
    }

    /// Submit an indirect draw call.
    ///
    /// # Valid Usage
    ///
    /// - There must be a valid graphics pipeline currently bound.
    /// - There must be a valid draw indirect buffer currently bound.
    pub unsafe fn draw_indirect(&self, primitive: Primitive, offset: u64, count: u32, stride: u32) {
        self.0
            .MultiDrawArraysIndirect(primitive as _, offset as _, count as _, stride as _);
    }

    /// Submit an indirect draw call.
    pub unsafe fn draw_indirect_from_host(&self, primitive: Primitive, data: &[DrawIndirectCmd]) {
        self.0.MultiDrawArraysIndirect(
            primitive as _,
            data.as_ptr() as *const _,
            data.len() as _,
            mem::size_of::<DrawIndirectCmd>() as _,
        );
    }

    /// Indirect draw call.
    pub unsafe fn draw_indexed_indirect(
        &self,
        primitive: Primitive,
        index_ty: IndexTy,
        offset: u64,
        count: u32,
        stride: u32,
    ) {
        self.0.MultiDrawElementsIndirect(
            primitive as _,
            index_ty as _,
            offset as _,
            count as _,
            stride as _,
        );
    }

    /// Indirect (indexed) draw call.
    pub unsafe fn draw_indexed_indirect_from_host(
        &self,
        primitive: Primitive,
        index_ty: IndexTy,
        data: &[DrawIndexedIndirectCmd],
    ) {
        self.0.MultiDrawElementsIndirect(
            primitive as _,
            index_ty as _,
            data.as_ptr() as *const _,
            data.len() as _,
            mem::size_of::<DrawIndexedIndirectCmd>() as _,
        );
    }

    /// Dispatch a workgroup for computation.
    ///
    /// # Valid usage
    ///
    /// - There must be a valid compute shader currently bound.
    pub unsafe fn dispatch(&self, x: u32, y: u32, z: u32) {
        self.0.DispatchCompute(x, y, z);
    }

    ///
    pub unsafe fn dispatch_indirect(&self, offset: u64) {
        self.0.DispatchComputeIndirect(offset as _);
    }

    ///
    pub unsafe fn blit(
        &self,
        src: Framebuffer,
        src_region: Region,
        dst: Framebuffer,
        dst_region: Region,
        filter: Filter,
    ) {
        self.0.BlitNamedFramebuffer(
            src.0,
            dst.0,
            src_region.x,
            src_region.x,
            src_region.w,
            src_region.h,
            dst_region.x,
            dst_region.x,
            dst_region.w,
            dst_region.h,
            __gl::COLOR_BUFFER_BIT,
            filter as _,
        );
    }

    ///
    pub unsafe fn draw_mesh_tasks_nv(&self, task_count: u32, first_task: u32) {
        self.0.DrawMeshTasksNV(first_task, task_count);
    }

    ///
    pub unsafe fn draw_mesh_tasks_indirect_nv(&self, offset: u64, draw_count: u32, stride: u32) {
        self.0
            .MultiDrawMeshTasksIndirectNV(offset as _, draw_count as _, stride as _);
    }

    ///
    pub unsafe fn draw_mesh_tasks_indirect_count_nv(
        &self,
        offset: u64,
        count_buffer_offset: u64,
        max_draw_count: u32,
        stride: u32,
    ) {
        self.0.MultiDrawMeshTasksIndirectCountNV(
            offset as _,
            count_buffer_offset as _,
            max_draw_count as _,
            stride as _,
        );
    }
}