Gawain: remove GL enum from primitive API

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.
This commit is contained in:
Mike Erwin 2017-04-08 01:19:25 -04:00
parent 0947c97fad
commit d5883bb1ba
5 changed files with 56 additions and 25 deletions

View 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;
GLenum gl_prim_type;
// book-keeping
GLuint vao_id; // remembers all geometry state (vertex attrib bindings & element buffer)

View File

@ -14,21 +14,21 @@
#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
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
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?
@ -42,3 +42,5 @@ typedef enum {
PrimitiveClass prim_class_of_type(PrimitiveType);
bool prim_type_belongs_to_class(PrimitiveType, PrimitiveClass);
GLenum convert_prim_type_to_gl(PrimitiveType);

View File

@ -30,8 +30,6 @@ void Batch_init(Batch* batch, PrimitiveType prim_type, VertexBuffer* verts, Elem
{
#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;
@ -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->gl_prim_type = convert_prim_type_to_gl(prim_type);
batch->phase = READY_TO_DRAW;
}
@ -304,15 +303,15 @@ void Batch_draw(Batch* batch)
#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
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
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
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);
@ -341,15 +340,15 @@ void Batch_draw_stupid(Batch* batch)
#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
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
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
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);
@ -394,10 +393,10 @@ void Batch_draw_stupid_instanced(Batch* batch, unsigned int instance_vbo, int in
{
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
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);

View File

@ -409,7 +409,7 @@ void immEnd(void)
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);

View File

@ -15,7 +15,6 @@ PrimitiveClass prim_class_of_type(PrimitiveType prim_type)
{
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,
@ -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_LINE_STRIP_ADJACENCY] = PRIM_CLASS_LINE,
#ifdef WITH_GL_PROFILE_COMPAT
[PRIM_QUADS_XXX] = PRIM_CLASS_SURFACE,
#endif
[PRIM_NONE] = PRIM_CLASS_NONE
};
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);
}
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];
}