Gawain: remove GL enum from primitive API
authorMike Erwin <significant.bit@gmail.com>
Sat, 8 Apr 2017 05:19:25 +0000 (01:19 -0400)
committerMike Erwin <significant.bit@gmail.com>
Sat, 8 Apr 2017 05:19:48 +0000 (01:19 -0400)
Callers now have to use Gawain's PRIM enum to specify geometric primitives.

This makes the API more bullet-proof (at least less vulnerable) since GLenum covers waaay more than GL_POINTS, GL_LINES, etc.

Also prepares us for Vulkan.

intern/gawain/gawain/batch.h
intern/gawain/gawain/primitive.h
intern/gawain/src/batch.c
intern/gawain/src/immediate.c
intern/gawain/src/primitive.c

index 189a586f9643a3bb7824d09b2da786c049dad802..532c45ae83262da326c271a94066ea5f9956ba16 100644 (file)
@@ -28,6 +28,7 @@ typedef struct Batch {
        VertexBuffer* verts[BATCH_MAX_VBO_CT]; // verts[0] is required, others can be NULL
        ElementList* elem; // NULL if element list not needed
        PrimitiveType prim_type;
        VertexBuffer* verts[BATCH_MAX_VBO_CT]; // verts[0] is required, others can be NULL
        ElementList* elem; // NULL if element list not needed
        PrimitiveType prim_type;
+       GLenum gl_prim_type;
 
        // book-keeping
        GLuint vao_id; // remembers all geometry state (vertex attrib bindings & element buffer)
 
        // book-keeping
        GLuint vao_id; // remembers all geometry state (vertex attrib bindings & element buffer)
index 214902e9a2b2f674e7dfaf29ad30638a3ce10d7f..c9123ec79df291042cf879613842c56e0676db65 100644 (file)
 #include "common.h"
 
 typedef enum {
 #include "common.h"
 
 typedef enum {
-       PRIM_POINTS = GL_POINTS,
-       PRIM_LINES = GL_LINES,
-       PRIM_TRIANGLES = GL_TRIANGLES,
+       PRIM_POINTS,
+       PRIM_LINES,
+       PRIM_TRIANGLES,
+       PRIM_LINE_STRIP,
+       PRIM_LINE_LOOP, // GL has this, Vulkan does not
+       PRIM_TRIANGLE_STRIP,
+       PRIM_TRIANGLE_FAN,
+
+       PRIM_LINE_STRIP_ADJACENCY,
 
 #ifdef WITH_GL_PROFILE_COMPAT
 
 #ifdef WITH_GL_PROFILE_COMPAT
-       PRIM_QUADS_XXX = GL_QUADS, // legacy GL has this, modern GL & Vulkan do not
+       PRIM_QUADS_XXX, // legacy GL has this, modern GL & Vulkan do not
 #endif
 
 #endif
 
-       PRIM_LINE_STRIP = GL_LINE_STRIP,
-       PRIM_LINE_STRIP_ADJACENCY = GL_LINE_STRIP_ADJACENCY,
-       PRIM_LINE_LOOP = GL_LINE_LOOP, // GL has this, Vulkan does not
-       PRIM_TRIANGLE_STRIP = GL_TRIANGLE_STRIP,
-       PRIM_TRIANGLE_FAN = GL_TRIANGLE_FAN,
-
-       PRIM_NONE = 0xF
+       PRIM_NONE
 } PrimitiveType;
 
 // what types of primitives does each shader expect?
 } PrimitiveType;
 
 // what types of primitives does each shader expect?
@@ -42,3 +42,5 @@ typedef enum {
 
 PrimitiveClass prim_class_of_type(PrimitiveType);
 bool prim_type_belongs_to_class(PrimitiveType, PrimitiveClass);
 
 PrimitiveClass prim_class_of_type(PrimitiveType);
 bool prim_type_belongs_to_class(PrimitiveType, PrimitiveClass);
+
+GLenum convert_prim_type_to_gl(PrimitiveType);
index 68771e593833ea5bcad997eebf052ec487b08196..7e904cbd4871d7dc66f4afabbd7b3fdf42b424ce 100644 (file)
@@ -30,8 +30,6 @@ void Batch_init(Batch* batch, PrimitiveType prim_type, VertexBuffer* verts, Elem
        {
 #if TRUST_NO_ONE
        assert(verts != NULL);
        {
 #if TRUST_NO_ONE
        assert(verts != NULL);
-       // assert(prim_type == PRIM_POINTS || prim_type == PRIM_LINES || prim_type == PRIM_TRIANGLES);
-       // we will allow other primitive types in a future update
 #endif
 
        batch->verts[0] = verts;
 #endif
 
        batch->verts[0] = verts;
@@ -39,6 +37,7 @@ void Batch_init(Batch* batch, PrimitiveType prim_type, VertexBuffer* verts, Elem
                batch->verts[v] = NULL;
        batch->elem = elem;
        batch->prim_type = prim_type;
                batch->verts[v] = NULL;
        batch->elem = elem;
        batch->prim_type = prim_type;
+       batch->gl_prim_type = convert_prim_type_to_gl(prim_type);
        batch->phase = READY_TO_DRAW;
        }
 
        batch->phase = READY_TO_DRAW;
        }
 
@@ -304,15 +303,15 @@ void Batch_draw(Batch* batch)
 
 #if TRACK_INDEX_RANGE
                if (el->base_index)
 
 #if TRACK_INDEX_RANGE
                if (el->base_index)
-                       glDrawRangeElementsBaseVertex(batch->prim_type, el->min_index, el->max_index, el->index_ct, el->index_type, 0, el->base_index);
+                       glDrawRangeElementsBaseVertex(batch->gl_prim_type, el->min_index, el->max_index, el->index_ct, el->index_type, 0, el->base_index);
                else
                else
-                       glDrawRangeElements(batch->prim_type, el->min_index, el->max_index, el->index_ct, el->index_type, 0);
+                       glDrawRangeElements(batch->gl_prim_type, el->min_index, el->max_index, el->index_ct, el->index_type, 0);
 #else
 #else
-               glDrawElements(batch->prim_type, el->index_ct, GL_UNSIGNED_INT, 0);
+               glDrawElements(batch->gl_prim_type, el->index_ct, GL_UNSIGNED_INT, 0);
 #endif
                }
        else
 #endif
                }
        else
-               glDrawArrays(batch->prim_type, 0, batch->verts[0]->vertex_ct);
+               glDrawArrays(batch->gl_prim_type, 0, batch->verts[0]->vertex_ct);
 
        Batch_done_using_program(batch);
        glBindVertexArray(0);
 
        Batch_done_using_program(batch);
        glBindVertexArray(0);
@@ -341,15 +340,15 @@ void Batch_draw_stupid(Batch* batch)
 
 #if TRACK_INDEX_RANGE
                if (el->base_index)
 
 #if TRACK_INDEX_RANGE
                if (el->base_index)
-                       glDrawRangeElementsBaseVertex(batch->prim_type, el->min_index, el->max_index, el->index_ct, el->index_type, 0, el->base_index);
+                       glDrawRangeElementsBaseVertex(batch->gl_prim_type, el->min_index, el->max_index, el->index_ct, el->index_type, 0, el->base_index);
                else
                else
-                       glDrawRangeElements(batch->prim_type, el->min_index, el->max_index, el->index_ct, el->index_type, 0);
+                       glDrawRangeElements(batch->gl_prim_type, el->min_index, el->max_index, el->index_ct, el->index_type, 0);
 #else
 #else
-               glDrawElements(batch->prim_type, el->index_ct, GL_UNSIGNED_INT, 0);
+               glDrawElements(batch->gl_prim_type, el->index_ct, GL_UNSIGNED_INT, 0);
 #endif
                }
        else
 #endif
                }
        else
-               glDrawArrays(batch->prim_type, 0, batch->verts[0]->vertex_ct);
+               glDrawArrays(batch->gl_prim_type, 0, batch->verts[0]->vertex_ct);
 
        // Batch_done_using_program(batch);
        glBindVertexArray(0);
 
        // Batch_done_using_program(batch);
        glBindVertexArray(0);
@@ -394,10 +393,10 @@ void Batch_draw_stupid_instanced(Batch* batch, unsigned int instance_vbo, int in
                {
                const ElementList* el = batch->elem;
 
                {
                const ElementList* el = batch->elem;
 
-               glDrawElementsInstanced(batch->prim_type, el->index_ct, GL_UNSIGNED_INT, 0, instance_count);
+               glDrawElementsInstanced(batch->gl_prim_type, el->index_ct, GL_UNSIGNED_INT, 0, instance_count);
                }
        else
                }
        else
-               glDrawArraysInstanced(batch->prim_type, 0, batch->verts[0]->vertex_ct, instance_count);
+               glDrawArraysInstanced(batch->gl_prim_type, 0, batch->verts[0]->vertex_ct, instance_count);
 
        // Batch_done_using_program(batch);
        glBindVertexArray(0);
 
        // Batch_done_using_program(batch);
        glBindVertexArray(0);
index 8867c541edc3dec6821d7b7b6dd43e34166d3b7e..15cd1cc801fa969d63c33fe9b427a451fda18dcd 100644 (file)
@@ -409,7 +409,7 @@ void immEnd(void)
                if (imm.vertex_ct > 0)
                        {
                        immDrawSetup();
                if (imm.vertex_ct > 0)
                        {
                        immDrawSetup();
-                       glDrawArrays(imm.prim_type, 0, imm.vertex_ct);
+                       glDrawArrays(convert_prim_type_to_gl(imm.prim_type), 0, imm.vertex_ct);
                        }
 
                glBindBuffer(GL_ARRAY_BUFFER, 0);
                        }
 
                glBindBuffer(GL_ARRAY_BUFFER, 0);
index 7fee06021b8c50c77d13bce763940034a168e578..21cee01d4dbfd75f7ff8dc53eaedab9683bf09d9 100644 (file)
@@ -15,7 +15,6 @@ PrimitiveClass prim_class_of_type(PrimitiveType prim_type)
        {
        static const PrimitiveClass classes[] =
                {
        {
        static const PrimitiveClass classes[] =
                {
-               [PRIM_NONE] = PRIM_CLASS_NONE,
                [PRIM_POINTS] = PRIM_CLASS_POINT,
                [PRIM_LINES] = PRIM_CLASS_LINE,
                [PRIM_LINE_STRIP] = PRIM_CLASS_LINE,
                [PRIM_POINTS] = PRIM_CLASS_POINT,
                [PRIM_LINES] = PRIM_CLASS_LINE,
                [PRIM_LINE_STRIP] = PRIM_CLASS_LINE,
@@ -24,9 +23,13 @@ PrimitiveClass prim_class_of_type(PrimitiveType prim_type)
                [PRIM_TRIANGLE_STRIP] = PRIM_CLASS_SURFACE,
                [PRIM_TRIANGLE_FAN] = PRIM_CLASS_SURFACE,
 
                [PRIM_TRIANGLE_STRIP] = PRIM_CLASS_SURFACE,
                [PRIM_TRIANGLE_FAN] = PRIM_CLASS_SURFACE,
 
+               [PRIM_LINE_STRIP_ADJACENCY] = PRIM_CLASS_LINE,
+
 #ifdef WITH_GL_PROFILE_COMPAT
                [PRIM_QUADS_XXX] = PRIM_CLASS_SURFACE,
 #endif
 #ifdef WITH_GL_PROFILE_COMPAT
                [PRIM_QUADS_XXX] = PRIM_CLASS_SURFACE,
 #endif
+
+               [PRIM_NONE] = PRIM_CLASS_NONE
                };
 
        return classes[prim_type];
                };
 
        return classes[prim_type];
@@ -39,3 +42,29 @@ bool prim_type_belongs_to_class(PrimitiveType prim_type, PrimitiveClass prim_cla
 
        return prim_class & prim_class_of_type(prim_type);
        }
 
        return prim_class & prim_class_of_type(prim_type);
        }
+
+GLenum convert_prim_type_to_gl(PrimitiveType prim_type)
+       {
+#if TRUST_NO_ONE
+       assert(prim_type != PRIM_NONE);
+#endif
+
+       static const GLenum table[] =
+               {
+               [PRIM_POINTS] = GL_POINTS,
+               [PRIM_LINES] = GL_LINES,
+               [PRIM_LINE_STRIP] = GL_LINE_STRIP,
+               [PRIM_LINE_LOOP] = GL_LINE_LOOP,
+               [PRIM_TRIANGLES] = PRIM_CLASS_SURFACE,
+               [PRIM_TRIANGLE_STRIP] = GL_TRIANGLE_STRIP,
+               [PRIM_TRIANGLE_FAN] = GL_TRIANGLE_FAN,
+
+               [PRIM_LINE_STRIP_ADJACENCY] = GL_LINE_STRIP_ADJACENCY,
+
+#ifdef WITH_GL_PROFILE_COMPAT
+               [PRIM_QUADS_XXX] = GL_QUADS,
+#endif
+               };
+
+       return table[prim_type];
+       }