2f7e22fe2d1b5a0b8e7696ece71403987a370c64
[blender.git] / intern / gawain / gawain / gwn_element.h
1
2 // Gawain element list (AKA index buffer)
3 //
4 // This code is part of the Gawain library, with modifications
5 // specific to integration with Blender.
6 //
7 // Copyright 2016 Mike Erwin
8 //
9 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
10 // the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
11
12 #pragma once
13
14 #include "gwn_primitive.h"
15
16 #define GWN_TRACK_INDEX_RANGE 1
17
18 typedef enum {
19         GWN_INDEX_U8, // GL has this, Vulkan does not
20         GWN_INDEX_U16,
21         GWN_INDEX_U32
22 } Gwn_IndexBufType;
23
24 typedef struct {
25         unsigned index_ct;
26 #if GWN_TRACK_INDEX_RANGE
27         Gwn_IndexBufType index_type;
28         GLenum gl_index_type;
29         unsigned min_index;
30         unsigned max_index;
31         unsigned base_index;
32 #endif
33         void* data; // NULL indicates data in VRAM (unmapped) or not yet allocated
34         GLuint vbo_id; // 0 indicates not yet sent to VRAM
35 } Gwn_IndexBuf;
36
37 void GWN_indexbuf_use(Gwn_IndexBuf*);
38 unsigned GWN_indexbuf_size_get(const Gwn_IndexBuf*);
39
40 typedef struct {
41         unsigned max_allowed_index;
42         unsigned max_index_ct;
43         unsigned index_ct;
44         Gwn_PrimType prim_type;
45         unsigned* data;
46 } Gwn_IndexBufBuilder;
47
48 // supported primitives:
49 //  GWN_PRIM_POINTS
50 //  GWN_PRIM_LINES
51 //  GWN_PRIM_TRIS
52
53 void GWN_indexbuf_init(Gwn_IndexBufBuilder*, Gwn_PrimType, unsigned prim_ct, unsigned vertex_ct);
54 //void GWN_indexbuf_init_custom(Gwn_IndexBufBuilder*, Gwn_PrimType, unsigned index_ct, unsigned vertex_ct);
55
56 void GWN_indexbuf_add_generic_vert(Gwn_IndexBufBuilder*, unsigned v);
57
58 void GWN_indexbuf_add_point_vert(Gwn_IndexBufBuilder*, unsigned v);
59 void GWN_indexbuf_add_line_verts(Gwn_IndexBufBuilder*, unsigned v1, unsigned v2);
60 void GWN_indexbuf_add_tri_verts(Gwn_IndexBufBuilder*, unsigned v1, unsigned v2, unsigned v3);
61
62 Gwn_IndexBuf* GWN_indexbuf_build(Gwn_IndexBufBuilder*);
63 void GWN_indexbuf_build_in_place(Gwn_IndexBufBuilder*, Gwn_IndexBuf*);
64
65 void GWN_indexbuf_discard(Gwn_IndexBuf*);
66
67
68 /* Macros */
69
70 #define GWN_INDEXBUF_DISCARD_SAFE(elem) do { \
71         if (elem != NULL) { \
72                 GWN_indexbuf_discard(elem); \
73                 elem = NULL; \
74         } \
75 } while (0)