Immediate mode optimisation

This commit is contained in:
Luke Benstead 2021-04-18 21:33:23 +01:00
parent 7bcbb86e80
commit ce9f3677f6
4 changed files with 36 additions and 28 deletions

View File

@ -140,9 +140,9 @@ gen_sample(zclip samples/zclip/main.c)
gen_sample(zclip_triangle samples/zclip_triangle/main.c) gen_sample(zclip_triangle samples/zclip_triangle/main.c)
gen_sample(zclip_trianglestrip samples/zclip_trianglestrip/main.c) gen_sample(zclip_trianglestrip samples/zclip_trianglestrip/main.c)
gen_sample(scissor samples/scissor/main.c) gen_sample(scissor samples/scissor/main.c)
gen_sample(polymark samples/polymark/main.c)
if(PLATFORM_DREAMCAST) if(PLATFORM_DREAMCAST)
gen_sample(polymark samples/polymark/main.c)
gen_sample(quadmark samples/quadmark/main.c) gen_sample(quadmark samples/quadmark/main.c)
gen_sample(trimark samples/trimark/main.c) gen_sample(trimark samples/trimark/main.c)
endif() endif()

View File

@ -20,7 +20,7 @@ static AlignedVector VERTICES;
static AlignedVector ST_COORDS; static AlignedVector ST_COORDS;
static AlignedVector NORMALS; static AlignedVector NORMALS;
static GLfloat NORMAL[3] = {0.0f, 0.0f, 1.0f}; static uint32_t NORMAL; /* Packed normal */
static GLubyte COLOR[4] = {255, 255, 255, 255}; static GLubyte COLOR[4] = {255, 255, 255, 255};
static GLfloat UV_COORD[2] = {0.0f, 0.0f}; static GLfloat UV_COORD[2] = {0.0f, 0.0f};
static GLfloat ST_COORD[2] = {0.0f, 0.0f}; static GLfloat ST_COORD[2] = {0.0f, 0.0f};
@ -31,6 +31,23 @@ static AttribPointer UV_ATTRIB;
static AttribPointer ST_ATTRIB; static AttribPointer ST_ATTRIB;
static AttribPointer NORMAL_ATTRIB; static AttribPointer NORMAL_ATTRIB;
static inline uint32_t pack_vertex_attribute_vec3_1i(float x, float y, float z) {
const float w = 0.0f;
const uint32_t xs = x < 0;
const uint32_t ys = y < 0;
const uint32_t zs = z < 0;
const uint32_t ws = w < 0;
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);
return vi;
}
void _glInitImmediateMode(GLuint initial_size) { void _glInitImmediateMode(GLuint initial_size) {
aligned_vector_init(&VERTICES, sizeof(GLVertexKOS)); aligned_vector_init(&VERTICES, sizeof(GLVertexKOS));
aligned_vector_init(&ST_COORDS, sizeof(GLfloat)); aligned_vector_init(&ST_COORDS, sizeof(GLfloat));
@ -64,6 +81,8 @@ void _glInitImmediateMode(GLuint initial_size) {
ST_ATTRIB.stride = 0; ST_ATTRIB.stride = 0;
ST_ATTRIB.type = GL_FLOAT; ST_ATTRIB.type = GL_FLOAT;
ST_ATTRIB.size = 2; ST_ATTRIB.size = 2;
NORMAL = pack_vertex_attribute_vec3_1i(0.0f, 0.0f, 1.0f);
} }
GLubyte _glCheckImmediateModeInactive(const char* func) { GLubyte _glCheckImmediateModeInactive(const char* func) {
@ -137,23 +156,6 @@ void APIENTRY glColor3fv(const GLfloat* v) {
COLOR[3] = 255; COLOR[3] = 255;
} }
static inline uint32_t pack_vertex_attribute_vec3_1i(float x, float y, float z) {
const float w = 0.0f;
const uint32_t xs = x < 0;
const uint32_t ys = y < 0;
const uint32_t zs = z < 0;
const uint32_t ws = w < 0;
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);
return vi;
}
void APIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z) { void APIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z) {
GLVertexKOS* vert = aligned_vector_extend(&VERTICES, 1); GLVertexKOS* vert = aligned_vector_extend(&VERTICES, 1);
GLfloat* st = aligned_vector_extend(&ST_COORDS, 2); GLfloat* st = aligned_vector_extend(&ST_COORDS, 2);
@ -170,9 +172,9 @@ void APIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z) {
vert->bgra[B8IDX] = COLOR[2]; vert->bgra[B8IDX] = COLOR[2];
vert->bgra[A8IDX] = COLOR[3]; vert->bgra[A8IDX] = COLOR[3];
*n = pack_vertex_attribute_vec3_1i(NORMAL[0], NORMAL[1], NORMAL[2]); *n = NORMAL;
st[0] = ST_COORD[0];
memcpy(st, ST_COORD, sizeof(GLfloat) * 2); st[1] = ST_COORD[1];
} }
void APIENTRY glVertex3fv(const GLfloat* v) { void APIENTRY glVertex3fv(const GLfloat* v) {
@ -220,9 +222,7 @@ void APIENTRY glTexCoord2fv(const GLfloat* v) {
} }
void APIENTRY glNormal3f(GLfloat x, GLfloat y, GLfloat z) { void APIENTRY glNormal3f(GLfloat x, GLfloat y, GLfloat z) {
NORMAL[0] = x; NORMAL = pack_vertex_attribute_vec3_1i(x, y, z);
NORMAL[1] = y;
NORMAL[2] = z;
} }
void APIENTRY glNormal3fv(const GLfloat* v) { void APIENTRY glNormal3fv(const GLfloat* v) {

View File

@ -382,8 +382,10 @@ GLubyte _glInitTextures() {
YALLOC_SIZE = vram_free - PVR_MEM_BUFFER_SIZE; /* Take all but 64kb VRAM */ YALLOC_SIZE = vram_free - PVR_MEM_BUFFER_SIZE; /* Take all but 64kb VRAM */
YALLOC_BASE = GPUMemoryAlloc(YALLOC_SIZE); YALLOC_BASE = GPUMemoryAlloc(YALLOC_SIZE);
#ifdef __DREAMCAST__
/* Ensure memory is aligned */ /* Ensure memory is aligned */
assert((uintptr_t) YALLOC_BASE % 32 == 0); assert((uintptr_t) YALLOC_BASE % 32 == 0);
#endif
yalloc_init(YALLOC_BASE, YALLOC_SIZE); yalloc_init(YALLOC_BASE, YALLOC_SIZE);
return 1; return 1;

View File

@ -7,11 +7,13 @@
(c)2002 Dan Potter, Paul Boese (c)2002 Dan Potter, Paul Boese
*/ */
#ifdef __DREAMCAST__
#include <kos.h> #include <kos.h>
#endif
#include <GL/gl.h> #include <GL/gl.h>
#include <GL/glkos.h> #include <GL/glkos.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
@ -22,6 +24,7 @@ int phase = PHASE_HALVE;
float avgfps = -1; float avgfps = -1;
void running_stats() { void running_stats() {
#ifdef __DREAMCAST__
pvr_stats_t stats; pvr_stats_t stats;
pvr_get_stats(&stats); pvr_get_stats(&stats);
@ -29,18 +32,22 @@ void running_stats() {
avgfps = stats.frame_rate; avgfps = stats.frame_rate;
else else
avgfps = (avgfps + stats.frame_rate) / 2.0f; avgfps = (avgfps + stats.frame_rate) / 2.0f;
#endif
} }
void stats() { void stats() {
#ifdef __DREAMCAST__
pvr_stats_t stats; pvr_stats_t stats;
pvr_get_stats(&stats); pvr_get_stats(&stats);
dbglog(DBG_DEBUG, "3D Stats: %d VBLs, frame rate ~%f fps\n", dbglog(DBG_DEBUG, "3D Stats: %d VBLs, frame rate ~%f fps\n",
stats.vbl_count, stats.frame_rate); stats.vbl_count, stats.frame_rate);
#endif
} }
int check_start() { int check_start() {
#ifdef __DREAMCAST__
maple_device_t *cont; maple_device_t *cont;
cont_state_t *state; cont_state_t *state;
@ -52,12 +59,11 @@ int check_start() {
if(state) if(state)
return state->buttons & CONT_START; return state->buttons & CONT_START;
} }
#endif
return 0; return 0;
} }
pvr_poly_hdr_t hdr;
void setup() { void setup() {
glKosInit(); glKosInit();
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);