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 "../include/gl.h"
|
2018-08-27 20:35:15 +00:00
|
|
|
#include "../include/glext.h"
|
2019-09-30 08:14:43 +00:00
|
|
|
#include "../include/glkos.h"
|
2019-03-25 16:06:08 +00:00
|
|
|
#include "profiler.h"
|
2018-08-27 20:35:15 +00:00
|
|
|
|
2018-05-12 13:39:20 +00:00
|
|
|
#include "private.h"
|
|
|
|
|
|
|
|
static GLboolean IMMEDIATE_MODE_ACTIVE = GL_FALSE;
|
|
|
|
static GLenum ACTIVE_POLYGON_MODE = GL_TRIANGLES;
|
|
|
|
|
|
|
|
static AlignedVector VERTICES;
|
2018-08-05 08:09:27 +00:00
|
|
|
static AlignedVector ST_COORDS;
|
2018-05-12 20:20:26 +00:00
|
|
|
static AlignedVector NORMALS;
|
|
|
|
|
2018-05-14 15:58:58 +00:00
|
|
|
static GLfloat NORMAL[3] = {0.0f, 0.0f, 1.0f};
|
2019-03-25 12:56:48 +00:00
|
|
|
static GLubyte COLOR[4] = {255, 255, 255, 255};
|
2018-08-05 08:09:27 +00:00
|
|
|
static GLfloat UV_COORD[2] = {0.0f, 0.0f};
|
|
|
|
static GLfloat ST_COORD[2] = {0.0f, 0.0f};
|
2018-05-14 15:58:58 +00:00
|
|
|
|
2019-03-29 08:47:55 +00:00
|
|
|
static AttribPointer VERTEX_ATTRIB;
|
|
|
|
static AttribPointer DIFFUSE_ATTRIB;
|
|
|
|
static AttribPointer UV_ATTRIB;
|
|
|
|
static AttribPointer ST_ATTRIB;
|
|
|
|
static AttribPointer NORMAL_ATTRIB;
|
|
|
|
|
2019-03-13 15:43:50 +00:00
|
|
|
void _glInitImmediateMode(GLuint initial_size) {
|
2019-09-30 08:14:43 +00:00
|
|
|
aligned_vector_init(&VERTICES, sizeof(GLVertexKOS));
|
2018-08-05 08:09:27 +00:00
|
|
|
aligned_vector_init(&ST_COORDS, sizeof(GLfloat));
|
2019-11-18 17:39:09 +00:00
|
|
|
aligned_vector_init(&NORMALS, sizeof(GLuint));
|
2019-03-13 15:43:50 +00:00
|
|
|
|
|
|
|
aligned_vector_reserve(&VERTICES, initial_size);
|
2019-09-30 08:14:43 +00:00
|
|
|
aligned_vector_reserve(&ST_COORDS, initial_size * 2);
|
2019-11-18 17:39:09 +00:00
|
|
|
aligned_vector_reserve(&NORMALS, initial_size);
|
2019-03-29 08:47:55 +00:00
|
|
|
|
2019-09-30 08:14:43 +00:00
|
|
|
VERTEX_ATTRIB.ptr = VERTICES.data + sizeof(uint32_t);
|
2019-03-29 08:47:55 +00:00
|
|
|
VERTEX_ATTRIB.size = 3;
|
|
|
|
VERTEX_ATTRIB.type = GL_FLOAT;
|
2019-09-30 08:14:43 +00:00
|
|
|
VERTEX_ATTRIB.stride = 32;
|
2019-03-29 08:47:55 +00:00
|
|
|
|
2019-09-30 08:14:43 +00:00
|
|
|
UV_ATTRIB.ptr = VERTEX_ATTRIB.ptr + (sizeof(GLfloat) * 3);
|
|
|
|
UV_ATTRIB.stride = 32;
|
2019-03-29 08:47:55 +00:00
|
|
|
UV_ATTRIB.type = GL_FLOAT;
|
|
|
|
UV_ATTRIB.size = 2;
|
|
|
|
|
2019-09-30 08:14:43 +00:00
|
|
|
DIFFUSE_ATTRIB.ptr = VERTEX_ATTRIB.ptr + (sizeof(GLfloat) * 5);
|
2019-09-30 20:59:11 +00:00
|
|
|
DIFFUSE_ATTRIB.size = GL_BGRA; /* Flipped color order */
|
2019-09-30 08:14:43 +00:00
|
|
|
DIFFUSE_ATTRIB.type = GL_UNSIGNED_BYTE;
|
|
|
|
DIFFUSE_ATTRIB.stride = 32;
|
2019-03-29 08:47:55 +00:00
|
|
|
|
|
|
|
NORMAL_ATTRIB.ptr = NORMALS.data;
|
|
|
|
NORMAL_ATTRIB.stride = 0;
|
2020-02-29 09:47:04 +00:00
|
|
|
NORMAL_ATTRIB.type = GL_UNSIGNED_INT_2_10_10_10_REV;
|
2019-11-18 17:39:09 +00:00
|
|
|
NORMAL_ATTRIB.size = 1;
|
2019-09-30 08:14:43 +00:00
|
|
|
|
|
|
|
ST_ATTRIB.ptr = ST_COORDS.data;
|
|
|
|
ST_ATTRIB.stride = 0;
|
|
|
|
ST_ATTRIB.type = GL_FLOAT;
|
|
|
|
ST_ATTRIB.size = 2;
|
2018-05-12 13:54:06 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 19:06:01 +00:00
|
|
|
GLubyte _glCheckImmediateModeInactive(const char* func) {
|
2018-05-12 13:54:06 +00:00
|
|
|
/* Returns 1 on error */
|
|
|
|
if(IMMEDIATE_MODE_ACTIVE) {
|
|
|
|
_glKosThrowError(GL_INVALID_OPERATION, func);
|
|
|
|
_glKosPrintError();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-05-12 13:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void APIENTRY glBegin(GLenum mode) {
|
|
|
|
if(IMMEDIATE_MODE_ACTIVE) {
|
|
|
|
_glKosThrowError(GL_INVALID_OPERATION, __func__);
|
|
|
|
_glKosPrintError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IMMEDIATE_MODE_ACTIVE = GL_TRUE;
|
|
|
|
ACTIVE_POLYGON_MODE = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void APIENTRY glColor4f(GLfloat r, GLfloat g, GLfloat b, GLfloat a) {
|
2019-03-25 12:56:48 +00:00
|
|
|
COLOR[0] = (GLubyte)(r * 255);
|
|
|
|
COLOR[1] = (GLubyte)(g * 255);
|
|
|
|
COLOR[2] = (GLubyte)(b * 255);
|
|
|
|
COLOR[3] = (GLubyte)(a * 255);
|
|
|
|
}
|
|
|
|
|
|
|
|
void APIENTRY glColor4ub(GLubyte r, GLubyte g, GLubyte b, GLubyte a) {
|
2018-05-14 15:58:58 +00:00
|
|
|
COLOR[0] = r;
|
|
|
|
COLOR[1] = g;
|
|
|
|
COLOR[2] = b;
|
|
|
|
COLOR[3] = a;
|
2018-05-12 13:39:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 13:54:06 +00:00
|
|
|
void APIENTRY glColor4fv(const GLfloat* v) {
|
2019-03-25 16:05:36 +00:00
|
|
|
COLOR[0] = (GLubyte)(v[0] * 255);
|
|
|
|
COLOR[1] = (GLubyte)(v[1] * 255);
|
|
|
|
COLOR[2] = (GLubyte)(v[2] * 255);
|
|
|
|
COLOR[3] = (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) {
|
2019-03-25 16:05:36 +00:00
|
|
|
COLOR[0] = (GLubyte)(r * 255);
|
|
|
|
COLOR[1] = (GLubyte)(g * 255);
|
|
|
|
COLOR[2] = (GLubyte)(b * 255);
|
|
|
|
COLOR[3] = 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) {
|
2019-03-25 12:56:48 +00:00
|
|
|
COLOR[0] = red;
|
|
|
|
COLOR[1] = green;
|
|
|
|
COLOR[2] = blue;
|
|
|
|
COLOR[3] = 255;
|
2019-03-10 12:19:41 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 19:42:29 +00:00
|
|
|
void APIENTRY glColor3ubv(const GLubyte *v) {
|
|
|
|
COLOR[0] = v[0];
|
|
|
|
COLOR[1] = v[1];
|
|
|
|
COLOR[2] = v[2];
|
|
|
|
COLOR[3] = 255;
|
|
|
|
}
|
|
|
|
|
2018-05-12 13:54:06 +00:00
|
|
|
void APIENTRY glColor3fv(const GLfloat* v) {
|
2019-03-25 16:05:36 +00:00
|
|
|
COLOR[0] = (GLubyte)(v[0] * 255);
|
|
|
|
COLOR[1] = (GLubyte)(v[1] * 255);
|
|
|
|
COLOR[2] = (GLubyte)(v[2] * 255);
|
|
|
|
COLOR[3] = 255;
|
2018-05-12 13:54:06 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 09:10:10 +00:00
|
|
|
static inline uint32_t pack_vertex_attribute_vec3_1i(float x, float y, float z) {
|
|
|
|
const float w = 0.0f;
|
2019-11-18 17:39:09 +00:00
|
|
|
|
2019-11-27 09:10:10 +00:00
|
|
|
const uint32_t xs = x < 0;
|
|
|
|
const uint32_t ys = y < 0;
|
|
|
|
const uint32_t zs = z < 0;
|
|
|
|
const uint32_t ws = w < 0;
|
2019-11-18 17:39:09 +00:00
|
|
|
|
2019-11-27 09:10:10 +00:00
|
|
|
uint32_t vi =
|
|
|
|
ws << 31 | ((uint32_t)(w + (ws << 1)) & 1) << 30 |
|
|
|
|
zs << 29 | ((uint32_t)(z * 511 + (zs << 9)) & 511) << 20 |
|
|
|
|
ys << 19 | ((uint32_t)(y * 511 + (ys << 9)) & 511) << 10 |
|
|
|
|
xs << 9 | ((uint32_t)(x * 511 + (xs << 9)) & 511);
|
2019-11-18 17:39:09 +00:00
|
|
|
|
2019-11-27 09:10:10 +00:00
|
|
|
return vi;
|
2019-11-18 17:39:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 13:39:20 +00:00
|
|
|
void APIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z) {
|
2019-09-30 08:14:43 +00:00
|
|
|
GLVertexKOS* vert = aligned_vector_extend(&VERTICES, 1);
|
|
|
|
GLfloat* st = aligned_vector_extend(&ST_COORDS, 2);
|
2019-11-18 17:39:09 +00:00
|
|
|
GLuint* n = aligned_vector_extend(&NORMALS, 1);
|
2019-09-30 08:14:43 +00:00
|
|
|
|
|
|
|
vert->x = x;
|
|
|
|
vert->y = y;
|
|
|
|
vert->z = z;
|
|
|
|
vert->u = UV_COORD[0];
|
|
|
|
vert->v = UV_COORD[1];
|
|
|
|
|
2019-09-30 20:59:11 +00:00
|
|
|
vert->bgra[R8IDX] = COLOR[0];
|
|
|
|
vert->bgra[G8IDX] = COLOR[1];
|
|
|
|
vert->bgra[B8IDX] = COLOR[2];
|
|
|
|
vert->bgra[A8IDX] = COLOR[3];
|
2019-09-30 08:14:43 +00:00
|
|
|
|
2019-11-27 09:10:10 +00:00
|
|
|
*n = pack_vertex_attribute_vec3_1i(NORMAL[0], NORMAL[1], NORMAL[2]);
|
2019-11-18 17:39:09 +00:00
|
|
|
|
2019-09-30 08:14:43 +00:00
|
|
|
memcpy(st, ST_COORD, sizeof(GLfloat) * 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) {
|
|
|
|
UV_COORD[0] = s;
|
|
|
|
UV_COORD[1] = t;
|
|
|
|
} else if(target == GL_TEXTURE1) {
|
|
|
|
ST_COORD[0] = s;
|
|
|
|
ST_COORD[1] = t;
|
|
|
|
} else {
|
|
|
|
_glKosThrowError(GL_INVALID_ENUM, __func__);
|
|
|
|
_glKosPrintError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-12 13:54:06 +00:00
|
|
|
void APIENTRY glTexCoord2f(GLfloat u, GLfloat v) {
|
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) {
|
2018-05-14 15:58:58 +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() {
|
2019-03-25 16:06:08 +00:00
|
|
|
profiler_push(__func__);
|
|
|
|
|
2018-05-12 13:39:20 +00:00
|
|
|
IMMEDIATE_MODE_ACTIVE = GL_FALSE;
|
|
|
|
|
2019-03-29 08:47:55 +00:00
|
|
|
/* Resizing could have invalidated these pointers */
|
2019-09-30 08:14:43 +00:00
|
|
|
VERTEX_ATTRIB.ptr = VERTICES.data + sizeof(uint32_t);
|
|
|
|
UV_ATTRIB.ptr = VERTEX_ATTRIB.ptr + (sizeof(GLfloat) * 3);
|
|
|
|
DIFFUSE_ATTRIB.ptr = VERTEX_ATTRIB.ptr + (sizeof(GLfloat) * 5);
|
|
|
|
|
2019-03-29 08:47:55 +00:00
|
|
|
NORMAL_ATTRIB.ptr = NORMALS.data;
|
2019-09-30 08:14:43 +00:00
|
|
|
ST_ATTRIB.ptr = ST_COORDS.data;
|
2019-03-25 16:06:08 +00:00
|
|
|
|
2019-03-29 08:47:55 +00:00
|
|
|
GLuint* attrs = _glGetEnabledAttributes();
|
2018-05-29 12:12:38 +00:00
|
|
|
|
2019-03-29 08:47:55 +00:00
|
|
|
AttribPointer* vattr = _glGetVertexAttribPointer();
|
|
|
|
AttribPointer* dattr = _glGetDiffuseAttribPointer();
|
|
|
|
AttribPointer* nattr = _glGetNormalAttribPointer();
|
|
|
|
AttribPointer* uattr = _glGetUVAttribPointer();
|
|
|
|
AttribPointer* sattr = _glGetSTAttribPointer();
|
2018-05-12 13:39:20 +00:00
|
|
|
|
2019-03-29 08:47:55 +00:00
|
|
|
/* Stash existing values */
|
|
|
|
AttribPointer vptr = *vattr;
|
|
|
|
AttribPointer dptr = *dattr;
|
|
|
|
AttribPointer nptr = *nattr;
|
|
|
|
AttribPointer uvptr = *uattr;
|
|
|
|
AttribPointer stptr = *sattr;
|
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
|
|
|
|
2019-03-29 08:47:55 +00:00
|
|
|
/* Switch to our immediate mode arrays */
|
|
|
|
*vattr = VERTEX_ATTRIB;
|
|
|
|
*dattr = DIFFUSE_ATTRIB;
|
|
|
|
*nattr = NORMAL_ATTRIB;
|
|
|
|
*uattr = UV_ATTRIB;
|
|
|
|
*sattr = ST_ATTRIB;
|
2018-05-12 13:54:06 +00:00
|
|
|
|
2019-03-29 08:47:55 +00:00
|
|
|
*attrs = ~0; // Enable everything
|
2019-03-25 16:06:08 +00:00
|
|
|
|
2019-09-30 08:14:43 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
_glRecalcFastPath();
|
|
|
|
#else
|
|
|
|
// Immediate mode should always activate the fast path
|
|
|
|
GLboolean fastPathEnabled = _glRecalcFastPath();
|
|
|
|
assert(fastPathEnabled);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
glDrawArrays(ACTIVE_POLYGON_MODE, 0, VERTICES.size);
|
2018-05-12 13:39:20 +00:00
|
|
|
|
2019-03-29 08:47:55 +00:00
|
|
|
/* Restore everything */
|
|
|
|
*vattr = vptr;
|
|
|
|
*dattr = dptr;
|
|
|
|
*nattr = nptr;
|
|
|
|
*uattr = uvptr;
|
|
|
|
*sattr = stptr;
|
2019-03-25 16:06:08 +00:00
|
|
|
|
2019-03-29 08:47:55 +00:00
|
|
|
*attrs = prevAttrs;
|
|
|
|
|
|
|
|
/* Clear arrays for next polys */
|
2018-05-12 13:39:20 +00:00
|
|
|
aligned_vector_clear(&VERTICES);
|
2018-08-05 08:09:27 +00:00
|
|
|
aligned_vector_clear(&ST_COORDS);
|
2018-05-12 20:20:26 +00:00
|
|
|
aligned_vector_clear(&NORMALS);
|
2018-05-12 13:39:20 +00:00
|
|
|
|
2019-03-29 08:47:55 +00:00
|
|
|
*vattr = vptr;
|
|
|
|
*dattr = dptr;
|
|
|
|
*nattr = nptr;
|
|
|
|
*uattr = uvptr;
|
|
|
|
*sattr = stptr;
|
2018-08-21 15:08:06 +00:00
|
|
|
|
2019-03-25 16:06:08 +00:00
|
|
|
profiler_checkpoint("restore");
|
|
|
|
profiler_pop();
|
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);
|
|
|
|
}
|