2018-05-12 20:20:26 +00:00
|
|
|
/*
|
|
|
|
* This implements immediate mode over the top of glDrawArrays
|
|
|
|
* current problems:
|
|
|
|
*
|
|
|
|
* 1. Calling glNormal(); glVertex(); glVertex(); glVertex(); will break.
|
|
|
|
* 2. Mixing with glXPointer stuff will break badly
|
|
|
|
* 3. This is entirely untested.
|
|
|
|
*/
|
|
|
|
|
2019-09-30 08:14:43 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-05-12 13:39:20 +00:00
|
|
|
#include "private.h"
|
|
|
|
|
2021-09-13 11:57:52 +00:00
|
|
|
extern inline GLuint _glRecalcFastPath();
|
2021-09-12 14:04:52 +00:00
|
|
|
|
|
|
|
GLboolean IMMEDIATE_MODE_ACTIVE = GL_FALSE;
|
2018-05-12 13:39:20 +00:00
|
|
|
static GLenum ACTIVE_POLYGON_MODE = GL_TRIANGLES;
|
|
|
|
|
2023-05-16 12:31:44 +00:00
|
|
|
static GLfloat __attribute__((aligned(32))) NORMAL[3] = {0.0f, 0.0f, 1.0f};
|
|
|
|
static GLubyte __attribute__((aligned(32))) COLOR[4] = {255, 255, 255, 255}; /* ARGB order for speed */
|
|
|
|
static GLfloat __attribute__((aligned(32))) UV_COORD[2] = {0.0f, 0.0f};
|
|
|
|
static GLfloat __attribute__((aligned(32))) ST_COORD[2] = {0.0f, 0.0f};
|
2018-05-14 15:58:58 +00:00
|
|
|
|
2021-09-13 09:29:04 +00:00
|
|
|
static AlignedVector VERTICES;
|
|
|
|
static AttribPointerList IM_ATTRIBS;
|
2021-05-25 19:40:43 +00:00
|
|
|
|
2021-04-19 19:21:46 +00:00
|
|
|
/* We store the list of attributes that have been "enabled" by a call to
|
|
|
|
glColor, glNormal, glTexCoord etc. otherwise we already have defaults that
|
|
|
|
can be applied faster */
|
2021-09-12 14:04:52 +00:00
|
|
|
static GLuint IM_ENABLED_VERTEX_ATTRIBUTES = 0;
|
2021-04-19 09:53:08 +00:00
|
|
|
|
2023-05-16 12:31:44 +00:00
|
|
|
typedef struct __attribute__((aligned(32))) {
|
2021-09-13 09:29:04 +00:00
|
|
|
GLfloat x;
|
|
|
|
GLfloat y;
|
|
|
|
GLfloat z;
|
|
|
|
GLfloat u;
|
|
|
|
GLfloat v;
|
|
|
|
GLfloat s;
|
|
|
|
GLfloat t;
|
|
|
|
GLubyte bgra[4];
|
|
|
|
GLfloat nx;
|
|
|
|
GLfloat ny;
|
|
|
|
GLfloat nz;
|
|
|
|
GLuint padding[5];
|
|
|
|
} IMVertex;
|
2021-04-18 20:33:23 +00:00
|
|
|
|
|
|
|
|
2019-03-13 15:43:50 +00:00
|
|
|
void _glInitImmediateMode(GLuint initial_size) {
|
2021-09-13 09:29:04 +00:00
|
|
|
aligned_vector_init(&VERTICES, sizeof(IMVertex));
|
2019-03-13 15:43:50 +00:00
|
|
|
aligned_vector_reserve(&VERTICES, initial_size);
|
2021-09-13 09:29:04 +00:00
|
|
|
|
2023-05-12 19:51:36 +00:00
|
|
|
IM_ATTRIBS.vertex.ptr = aligned_vector_front(&VERTICES);
|
2021-09-13 09:29:04 +00:00
|
|
|
IM_ATTRIBS.vertex.size = 3;
|
|
|
|
IM_ATTRIBS.vertex.type = GL_FLOAT;
|
|
|
|
IM_ATTRIBS.vertex.stride = sizeof(IMVertex);
|
|
|
|
|
|
|
|
IM_ATTRIBS.uv.ptr = IM_ATTRIBS.vertex.ptr + (sizeof(GLfloat) * 3);
|
|
|
|
IM_ATTRIBS.uv.stride = sizeof(IMVertex);
|
|
|
|
IM_ATTRIBS.uv.type = GL_FLOAT;
|
|
|
|
IM_ATTRIBS.uv.size = 2;
|
|
|
|
|
|
|
|
IM_ATTRIBS.st.ptr = IM_ATTRIBS.vertex.ptr + (sizeof(GLfloat) * 5);
|
|
|
|
IM_ATTRIBS.st.stride = sizeof(IMVertex);
|
|
|
|
IM_ATTRIBS.st.type = GL_FLOAT;
|
|
|
|
IM_ATTRIBS.st.size = 2;
|
|
|
|
|
|
|
|
IM_ATTRIBS.colour.ptr = IM_ATTRIBS.vertex.ptr + (sizeof(GLfloat) * 7);
|
|
|
|
IM_ATTRIBS.colour.size = GL_BGRA; /* Flipped color order */
|
|
|
|
IM_ATTRIBS.colour.type = GL_UNSIGNED_BYTE;
|
|
|
|
IM_ATTRIBS.colour.stride = sizeof(IMVertex);
|
|
|
|
|
|
|
|
IM_ATTRIBS.normal.ptr = IM_ATTRIBS.vertex.ptr + (sizeof(GLfloat) * 7) + sizeof(uint32_t);
|
|
|
|
IM_ATTRIBS.normal.stride = sizeof(IMVertex);
|
|
|
|
IM_ATTRIBS.normal.type = GL_FLOAT;
|
|
|
|
IM_ATTRIBS.normal.size = 3;
|
2018-05-12 13:54:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 13:39:20 +00:00
|
|
|
void APIENTRY glBegin(GLenum mode) {
|
|
|
|
if(IMMEDIATE_MODE_ACTIVE) {
|
|
|
|
_glKosThrowError(GL_INVALID_OPERATION, __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IMMEDIATE_MODE_ACTIVE = GL_TRUE;
|
|
|
|
ACTIVE_POLYGON_MODE = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void APIENTRY glColor4f(GLfloat r, GLfloat g, GLfloat b, GLfloat a) {
|
2021-09-12 14:04:52 +00:00
|
|
|
IM_ENABLED_VERTEX_ATTRIBUTES |= DIFFUSE_ENABLED_FLAG;
|
2021-04-19 09:53:08 +00:00
|
|
|
|
2021-05-25 19:40:43 +00:00
|
|
|
COLOR[A8IDX] = (GLubyte)(a * 255.0f);
|
|
|
|
COLOR[R8IDX] = (GLubyte)(r * 255.0f);
|
|
|
|
COLOR[G8IDX] = (GLubyte)(g * 255.0f);
|
|
|
|
COLOR[B8IDX] = (GLubyte)(b * 255.0f);
|
2019-03-25 12:56:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void APIENTRY glColor4ub(GLubyte r, GLubyte g, GLubyte b, GLubyte a) {
|
2021-09-12 14:04:52 +00:00
|
|
|
IM_ENABLED_VERTEX_ATTRIBUTES |= DIFFUSE_ENABLED_FLAG;
|
2021-04-19 09:53:08 +00:00
|
|
|
|
2021-04-25 13:21:09 +00:00
|
|
|
COLOR[A8IDX] = a;
|
|
|
|
COLOR[R8IDX] = r;
|
|
|
|
COLOR[G8IDX] = g;
|
|
|
|
COLOR[B8IDX] = b;
|
2018-05-12 13:39:20 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 17:06:26 +00:00
|
|
|
void APIENTRY glColor4ubv(const GLubyte *v) {
|
|
|
|
IM_ENABLED_VERTEX_ATTRIBUTES |= DIFFUSE_ENABLED_FLAG;
|
|
|
|
|
|
|
|
COLOR[A8IDX] = v[3];
|
|
|
|
COLOR[R8IDX] = v[0];
|
|
|
|
COLOR[G8IDX] = v[1];
|
|
|
|
COLOR[B8IDX] = v[2];
|
|
|
|
}
|
|
|
|
|
2018-05-12 13:54:06 +00:00
|
|
|
void APIENTRY glColor4fv(const GLfloat* v) {
|
2021-09-12 14:04:52 +00:00
|
|
|
IM_ENABLED_VERTEX_ATTRIBUTES |= DIFFUSE_ENABLED_FLAG;
|
2021-04-19 09:53:08 +00:00
|
|
|
|
2021-04-25 13:21:09 +00:00
|
|
|
COLOR[B8IDX] = (GLubyte)(v[2] * 255);
|
|
|
|
COLOR[G8IDX] = (GLubyte)(v[1] * 255);
|
|
|
|
COLOR[R8IDX] = (GLubyte)(v[0] * 255);
|
|
|
|
COLOR[A8IDX] = (GLubyte)(v[3] * 255);
|
2018-05-12 13:54:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 13:39:20 +00:00
|
|
|
void APIENTRY glColor3f(GLfloat r, GLfloat g, GLfloat b) {
|
2021-09-12 14:04:52 +00:00
|
|
|
IM_ENABLED_VERTEX_ATTRIBUTES |= DIFFUSE_ENABLED_FLAG;
|
2021-04-19 09:53:08 +00:00
|
|
|
|
2021-09-13 09:29:04 +00:00
|
|
|
COLOR[B8IDX] = (GLubyte)(b * 255.0f);
|
|
|
|
COLOR[G8IDX] = (GLubyte)(g * 255.0f);
|
|
|
|
COLOR[R8IDX] = (GLubyte)(r * 255.0f);
|
2021-04-25 13:21:09 +00:00
|
|
|
COLOR[A8IDX] = 255;
|
2018-05-12 13:39:20 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 12:19:41 +00:00
|
|
|
void APIENTRY glColor3ub(GLubyte red, GLubyte green, GLubyte blue) {
|
2021-09-12 14:04:52 +00:00
|
|
|
IM_ENABLED_VERTEX_ATTRIBUTES |= DIFFUSE_ENABLED_FLAG;
|
2021-04-19 09:53:08 +00:00
|
|
|
|
2021-04-25 13:21:09 +00:00
|
|
|
COLOR[A8IDX] = 255;
|
|
|
|
COLOR[R8IDX] = red;
|
|
|
|
COLOR[G8IDX] = green;
|
|
|
|
COLOR[B8IDX] = blue;
|
2019-03-10 12:19:41 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 19:42:29 +00:00
|
|
|
void APIENTRY glColor3ubv(const GLubyte *v) {
|
2021-09-12 14:04:52 +00:00
|
|
|
IM_ENABLED_VERTEX_ATTRIBUTES |= DIFFUSE_ENABLED_FLAG;
|
2021-04-19 09:53:08 +00:00
|
|
|
|
2021-04-25 13:21:09 +00:00
|
|
|
COLOR[A8IDX] = 255;
|
|
|
|
COLOR[R8IDX] = v[0];
|
|
|
|
COLOR[G8IDX] = v[1];
|
|
|
|
COLOR[B8IDX] = v[2];
|
2019-08-20 19:42:29 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 13:54:06 +00:00
|
|
|
void APIENTRY glColor3fv(const GLfloat* v) {
|
2021-09-12 14:04:52 +00:00
|
|
|
IM_ENABLED_VERTEX_ATTRIBUTES |= DIFFUSE_ENABLED_FLAG;
|
2021-04-19 09:53:08 +00:00
|
|
|
|
2021-04-25 13:21:09 +00:00
|
|
|
COLOR[A8IDX] = 255;
|
|
|
|
COLOR[R8IDX] = (GLubyte)(v[0] * 255);
|
|
|
|
COLOR[G8IDX] = (GLubyte)(v[1] * 255);
|
|
|
|
COLOR[B8IDX] = (GLubyte)(v[2] * 255);
|
2018-05-12 13:54:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 13:39:20 +00:00
|
|
|
void APIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z) {
|
2021-09-12 14:04:52 +00:00
|
|
|
IM_ENABLED_VERTEX_ATTRIBUTES |= VERTEX_ENABLED_FLAG;
|
2021-04-19 09:53:08 +00:00
|
|
|
|
2021-09-13 09:29:04 +00:00
|
|
|
IMVertex* vert = aligned_vector_extend(&VERTICES, 1);
|
2019-09-30 08:14:43 +00:00
|
|
|
|
2023-05-16 12:31:44 +00:00
|
|
|
/* Resizing could've invalidated the pointers */
|
|
|
|
IM_ATTRIBS.vertex.ptr = VERTICES.data;
|
|
|
|
IM_ATTRIBS.uv.ptr = IM_ATTRIBS.vertex.ptr + 12;
|
|
|
|
IM_ATTRIBS.st.ptr = IM_ATTRIBS.uv.ptr + 8;
|
|
|
|
IM_ATTRIBS.colour.ptr = IM_ATTRIBS.st.ptr + 8;
|
|
|
|
IM_ATTRIBS.normal.ptr = IM_ATTRIBS.colour.ptr + 4;
|
|
|
|
|
|
|
|
uint32_t* dest = (uint32_t*) &vert->x;
|
|
|
|
*(dest++) = *((uint32_t*) &x);
|
|
|
|
*(dest++) = *((uint32_t*) &y);
|
|
|
|
*(dest++) = *((uint32_t*) &z);
|
|
|
|
*(dest++) = *((uint32_t*) &UV_COORD[0]);
|
|
|
|
*(dest++) = *((uint32_t*) &UV_COORD[1]);
|
|
|
|
*(dest++) = *((uint32_t*) &ST_COORD[0]);
|
|
|
|
*(dest++) = *((uint32_t*) &ST_COORD[1]);
|
|
|
|
*(dest++) = *((uint32_t*) COLOR);
|
|
|
|
*(dest++) = *((uint32_t*) &NORMAL[0]);
|
|
|
|
*(dest++) = *((uint32_t*) &NORMAL[1]);
|
|
|
|
*(dest++) = *((uint32_t*) &NORMAL[2]);
|
2018-05-12 13:39:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 13:54:06 +00:00
|
|
|
void APIENTRY glVertex3fv(const GLfloat* v) {
|
|
|
|
glVertex3f(v[0], v[1], v[2]);
|
|
|
|
}
|
|
|
|
|
2018-05-19 08:17:24 +00:00
|
|
|
void APIENTRY glVertex2f(GLfloat x, GLfloat y) {
|
|
|
|
glVertex3f(x, y, 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void APIENTRY glVertex2fv(const GLfloat* v) {
|
|
|
|
glVertex2f(v[0], v[1]);
|
|
|
|
}
|
|
|
|
|
2018-05-12 20:20:26 +00:00
|
|
|
void APIENTRY glVertex4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w) {
|
2020-03-05 19:54:07 +00:00
|
|
|
_GL_UNUSED(w);
|
2018-05-14 15:58:58 +00:00
|
|
|
glVertex3f(x, y, z);
|
2018-05-12 20:20:26 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 13:54:06 +00:00
|
|
|
void APIENTRY glVertex4fv(const GLfloat* v) {
|
|
|
|
glVertex4f(v[0], v[1], v[2], v[3]);
|
|
|
|
}
|
|
|
|
|
2018-08-05 08:09:27 +00:00
|
|
|
void APIENTRY glMultiTexCoord2fARB(GLenum target, GLfloat s, GLfloat t) {
|
|
|
|
if(target == GL_TEXTURE0) {
|
2021-09-12 14:04:52 +00:00
|
|
|
IM_ENABLED_VERTEX_ATTRIBUTES |= UV_ENABLED_FLAG;
|
2018-08-05 08:09:27 +00:00
|
|
|
UV_COORD[0] = s;
|
|
|
|
UV_COORD[1] = t;
|
|
|
|
} else if(target == GL_TEXTURE1) {
|
2021-09-12 14:04:52 +00:00
|
|
|
IM_ENABLED_VERTEX_ATTRIBUTES |= ST_ENABLED_FLAG;
|
2018-08-05 08:09:27 +00:00
|
|
|
ST_COORD[0] = s;
|
|
|
|
ST_COORD[1] = t;
|
|
|
|
} else {
|
|
|
|
_glKosThrowError(GL_INVALID_ENUM, __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-18 21:40:31 +00:00
|
|
|
void APIENTRY glTexCoord1f(GLfloat u) {
|
|
|
|
IM_ENABLED_VERTEX_ATTRIBUTES |= UV_ENABLED_FLAG;
|
|
|
|
UV_COORD[0] = u;
|
|
|
|
UV_COORD[1] = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void APIENTRY glTexCoord1fv(const GLfloat* v) {
|
|
|
|
glTexCoord1f(v[0]);
|
|
|
|
}
|
|
|
|
|
2018-05-12 13:54:06 +00:00
|
|
|
void APIENTRY glTexCoord2f(GLfloat u, GLfloat v) {
|
2021-09-12 14:04:52 +00:00
|
|
|
IM_ENABLED_VERTEX_ATTRIBUTES |= UV_ENABLED_FLAG;
|
2018-08-05 08:09:27 +00:00
|
|
|
UV_COORD[0] = u;
|
|
|
|
UV_COORD[1] = v;
|
2018-05-12 13:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void APIENTRY glTexCoord2fv(const GLfloat* v) {
|
|
|
|
glTexCoord2f(v[0], v[1]);
|
|
|
|
}
|
|
|
|
|
2018-05-12 20:20:26 +00:00
|
|
|
void APIENTRY glNormal3f(GLfloat x, GLfloat y, GLfloat z) {
|
2021-09-12 14:04:52 +00:00
|
|
|
IM_ENABLED_VERTEX_ATTRIBUTES |= NORMAL_ENABLED_FLAG;
|
2021-09-13 09:29:04 +00:00
|
|
|
NORMAL[0] = x;
|
|
|
|
NORMAL[1] = y;
|
|
|
|
NORMAL[2] = z;
|
2018-05-12 20:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void APIENTRY glNormal3fv(const GLfloat* v) {
|
|
|
|
glNormal3f(v[0], v[1], v[2]);
|
|
|
|
}
|
|
|
|
|
2018-05-12 13:39:20 +00:00
|
|
|
void APIENTRY glEnd() {
|
|
|
|
IMMEDIATE_MODE_ACTIVE = GL_FALSE;
|
|
|
|
|
2021-09-12 14:04:52 +00:00
|
|
|
GLuint* attrs = &ENABLED_VERTEX_ATTRIBUTES;
|
2018-05-29 12:12:38 +00:00
|
|
|
|
2021-09-13 09:29:04 +00:00
|
|
|
/* Redirect attrib pointers */
|
|
|
|
AttribPointerList stashed_attrib_pointers = ATTRIB_POINTERS;
|
|
|
|
ATTRIB_POINTERS = IM_ATTRIBS;
|
2018-08-21 14:50:59 +00:00
|
|
|
|
2019-03-29 08:47:55 +00:00
|
|
|
GLuint prevAttrs = *attrs;
|
2018-08-05 08:09:27 +00:00
|
|
|
|
2021-09-12 14:04:52 +00:00
|
|
|
*attrs = IM_ENABLED_VERTEX_ATTRIBUTES;
|
2019-03-25 16:06:08 +00:00
|
|
|
|
2021-10-01 20:02:13 +00:00
|
|
|
/* Store the fast path enabled setting so we can restore it
|
|
|
|
* after drawing */
|
|
|
|
const GLboolean fp_was_enabled = FAST_PATH_ENABLED;
|
|
|
|
|
2019-09-30 08:14:43 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// Immediate mode should always activate the fast path
|
2021-09-13 11:57:52 +00:00
|
|
|
GLuint fastPathEnabled = _glRecalcFastPath();
|
2022-08-25 20:24:08 +00:00
|
|
|
gl_assert(fastPathEnabled);
|
2021-09-13 09:29:04 +00:00
|
|
|
#else
|
|
|
|
/* If we're not debugging, set to true - we assume we haven't broken it! */
|
|
|
|
FAST_PATH_ENABLED = GL_TRUE;
|
2019-09-30 08:14:43 +00:00
|
|
|
#endif
|
|
|
|
|
2023-05-12 19:51:36 +00:00
|
|
|
glDrawArrays(ACTIVE_POLYGON_MODE, 0, aligned_vector_header(&VERTICES)->size);
|
2018-05-12 13:39:20 +00:00
|
|
|
|
2021-09-13 09:29:04 +00:00
|
|
|
ATTRIB_POINTERS = stashed_attrib_pointers;
|
2019-03-25 16:06:08 +00:00
|
|
|
|
2019-03-29 08:47:55 +00:00
|
|
|
*attrs = prevAttrs;
|
|
|
|
|
2018-05-12 13:39:20 +00:00
|
|
|
aligned_vector_clear(&VERTICES);
|
2021-10-01 20:02:13 +00:00
|
|
|
|
|
|
|
FAST_PATH_ENABLED = fp_was_enabled;
|
2018-05-12 13:39:20 +00:00
|
|
|
}
|
2018-05-20 15:16:53 +00:00
|
|
|
|
|
|
|
void APIENTRY glRectf(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2) {
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
glVertex2f(x1, y1);
|
|
|
|
glVertex2f(x2, y1);
|
|
|
|
glVertex2f(x2, y2);
|
|
|
|
glVertex2f(x1, y2);
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
void APIENTRY glRectfv(const GLfloat *v1, const GLfloat *v2) {
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
glVertex2f(v1[0], v1[1]);
|
|
|
|
glVertex2f(v2[0], v1[1]);
|
|
|
|
glVertex2f(v2[0], v2[1]);
|
|
|
|
glVertex2f(v1[0], v2[1]);
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
void APIENTRY glRecti(GLint x1, GLint y1, GLint x2, GLint y2) {
|
|
|
|
return glRectf((GLfloat)x1, (GLfloat)y1, (GLfloat)x2, (GLfloat)y2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void APIENTRY glRectiv(const GLint *v1, const GLint *v2) {
|
|
|
|
return glRectfv((const GLfloat *)v1, (const GLfloat *)v2);
|
|
|
|
}
|