From 4ad58bea89d5921077880febe6ba0fa8fa9b9899 Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Tue, 11 Apr 2023 20:46:12 +0100 Subject: [PATCH 1/3] Optimise glLoadMatrixf --- GL/matrix.c | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/GL/matrix.c b/GL/matrix.c index 74768d0..73ca6fd 100644 --- a/GL/matrix.c +++ b/GL/matrix.c @@ -200,28 +200,9 @@ void APIENTRY glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z) { /* Load an arbitrary matrix */ void APIENTRY glLoadMatrixf(const GLfloat *m) { - static Matrix4x4 TEMP; - - TEMP[M0] = m[0]; - TEMP[M1] = m[1]; - TEMP[M2] = m[2]; - TEMP[M3] = m[3]; - - TEMP[M4] = m[4]; - TEMP[M5] = m[5]; - TEMP[M6] = m[6]; - TEMP[M7] = m[7]; - - TEMP[M8] = m[8]; - TEMP[M9] = m[9]; - TEMP[M10] = m[10]; - TEMP[M11] = m[11]; - - TEMP[M12] = m[12]; - TEMP[M13] = m[13]; - TEMP[M14] = m[14]; - TEMP[M15] = m[15]; + static Matrix4x4 __attribute__((aligned(32))) TEMP; + memcpy(TEMP, m, sizeof(float) * 16); stack_replace(MATRIX_STACKS + MATRIX_IDX, TEMP); if(MATRIX_MODE == GL_MODELVIEW) { From 307d371c555bef7ed62e3a5326f4b917048514f6 Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Tue, 11 Apr 2023 20:46:31 +0100 Subject: [PATCH 2/3] Various store queue shinanigans --- GL/platforms/sh4.c | 45 ++++++++++++++++++++++----------------------- GL/platforms/sh4.h | 2 +- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/GL/platforms/sh4.c b/GL/platforms/sh4.c index 05a8d6f..0d2a35c 100644 --- a/GL/platforms/sh4.c +++ b/GL/platforms/sh4.c @@ -9,7 +9,9 @@ #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) -#define SQ_BASE_ADDRESS 0xe0000000 +#define SQ_BASE_ADDRESS (uint32_t *)(void *) \ + (0xe0000000 | (((uint32_t)0x10000000) & 0x03ffffe0)) + static volatile uint32_t* PVR_LMMODE0 = (uint32_t*) 0xA05F6884; @@ -38,9 +40,6 @@ void InitGPU(_Bool autosort, _Bool fsaa) { void SceneBegin() { pvr_wait_ready(); pvr_scene_begin(); - - QACR0 = 0x11; /* Enable the direct texture path by setting the higher two bits */ - QACR1 = 0x11; } void SceneListBegin(GPUList list) { @@ -83,7 +82,6 @@ GL_FORCE_INLINE void _glSubmitHeaderOrVertex(uint32_t* d, const Vertex* v) { #endif uint32_t *s = (uint32_t*) v; - __asm__("pref @%0" : : "r"(s + 8)); /* prefetch 32 bytes for next loop */ d[0] = *(s++); d[1] = *(s++); d[2] = *(s++); @@ -104,7 +102,7 @@ static struct __attribute__((aligned(32))) { static int tri_count = 0; static int strip_count = 0; -GL_FORCE_INLINE void interpolateColour(const uint32_t* a, const uint32_t* b, const float t, uint32_t* out) { +static inline void interpolateColour(const uint32_t* a, const uint32_t* b, const float t, uint32_t* out) { const static uint32_t MASK1 = 0x00FF00FF; const static uint32_t MASK2 = 0xFF00FF00; @@ -140,7 +138,7 @@ GL_FORCE_INLINE void ClearTriangle() { tri_count = 0; } -GL_FORCE_INLINE void ShiftTriangle() { +static inline void ShiftTriangle() { if(!tri_count) { return; } @@ -156,7 +154,7 @@ GL_FORCE_INLINE void ShiftTriangle() { } -GL_FORCE_INLINE void ShiftRotateTriangle() { +static inline void ShiftRotateTriangle() { if(!tri_count) { return; } @@ -177,8 +175,16 @@ void SceneListSubmit(void* src, int n) { PVR_SET(SPAN_SORT_CFG, 0x0); - uint32_t *d = (uint32_t*) SQ_BASE_ADDRESS; - *PVR_LMMODE0 = 0x0; /* Enable 64bit mode */ + //Set PVR DMA registers + volatile int *pvrdmacfg = (int*)0xA05F6888; + pvrdmacfg[0] = 1; + pvrdmacfg[1] = 0; + + //Set QACR registers + volatile int *qacr = (int*)0xFF000038; + qacr[1] = qacr[0] = 0x11; + + uint32_t *d = SQ_BASE_ADDRESS; Vertex __attribute__((aligned(32))) tmp; @@ -188,17 +194,14 @@ void SceneListSubmit(void* src, int n) { if(!_glNearZClippingEnabled()) { /* Prep store queues */ - for(int i = 0; i < n; ++i, ++vertex) { - PREFETCH(vertex + 1); + while(n--) { if(glIsVertex(vertex->flags)) { _glPerspectiveDivideVertex(vertex, h); } - _glSubmitHeaderOrVertex(d, vertex); - } - /* Wait for both store queues to complete */ - d = (uint32_t *) SQ_BASE_ADDRESS; - d[0] = d[8] = 0; + _glSubmitHeaderOrVertex(d, vertex); + ++vertex; + } return; } @@ -211,8 +214,8 @@ void SceneListSubmit(void* src, int n) { #endif for(int i = 0; i < n; ++i, ++vertex) { - PREFETCH(vertex + 12); - + PREFETCH(vertex + 1); + PREFETCH(vertex + 2); /* Wait until we fill the triangle */ if(tri_count < 3) { if(glIsVertex(vertex->flags)) { @@ -422,10 +425,6 @@ void SceneListSubmit(void* src, int n) { strip_count = 2; } } - - /* Wait for both store queues to complete */ - d = (uint32_t *)0xe0000000; - d[0] = d[8] = 0; } void SceneListFinish() { diff --git a/GL/platforms/sh4.h b/GL/platforms/sh4.h index 6bc89c1..46c3513 100644 --- a/GL/platforms/sh4.h +++ b/GL/platforms/sh4.h @@ -24,7 +24,7 @@ #define GL_FORCE_INLINE static GL_INLINE_DEBUG #endif -#define PREFETCH(addr) __asm__("pref @%0" : : "r"((addr))) +#define PREFETCH(addr) __builtin_prefetch((addr)) GL_FORCE_INLINE void* memcpy_fast(void *dest, const void *src, size_t len) { if(!len) { From 25d215dad3928bfac2bbcbaa146c5adaf2b8d736 Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Tue, 11 Apr 2023 20:46:44 +0100 Subject: [PATCH 3/3] Add some compiler flags for lolz --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c1bb39..13ad28d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,7 +36,7 @@ else() set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -ffp-contract=fast -ffast-math") endif() -set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -fexpensive-optimizations") +set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -fexpensive-optimizations -fomit-frame-pointer -finline-functions") set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -g -Wall -Wextra") set(