From e4d9c516f586057f467d1e040c82f5e802eb9997 Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Tue, 6 Apr 2021 14:26:41 +0100 Subject: [PATCH] Fix kos compilation --- CMakeLists.txt | 2 +- GL/matrix.c | 75 ++++--------- GL/platforms/sh4.c | 7 +- GL/platforms/sh4.h | 43 ++++--- GL/platforms/software.c | 125 ++++++++++++++++++++- GL/platforms/software.h | 2 +- GL/platforms/software/parameter_equation.h | 2 +- GL/private.h | 1 - containers/aligned_vector.h | 8 +- 9 files changed, 178 insertions(+), 87 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 332348a..40e8725 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,7 +33,7 @@ else() find_package(SDL2 REQUIRED) include_directories(${SDL2_INCLUDE_DIRS}) link_libraries(${SDL2_LIBRARIES}) -set(SOURCES ${SOURCES} GL/platforms/software.c) +set(SOURCES ${SOURCES} GL/platforms/software.c GL/platforms/software/edge_equation.c GL/platforms/software/parameter_equation.c) endif() add_library(GLdc ${SOURCES}) diff --git a/GL/matrix.c b/GL/matrix.c index 1f40bdf..275b607 100644 --- a/GL/matrix.c +++ b/GL/matrix.c @@ -122,17 +122,13 @@ void APIENTRY glLoadIdentity() { } void APIENTRY glTranslatef(GLfloat x, GLfloat y, GLfloat z) { - static Matrix4x4 trn __attribute__((aligned(32))) = { + const Matrix4x4 trn __attribute__((aligned(32))) = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f + x, y, z, 1.0f }; - trn[M12] = x; - trn[M13] = y; - trn[M14] = z; - UploadMatrix4x4(stack_top(MATRIX_STACKS + MATRIX_IDX)); MultiplyMatrix4x4(&trn); DownloadMatrix4x4(stack_top(MATRIX_STACKS + MATRIX_IDX)); @@ -144,17 +140,13 @@ void APIENTRY glTranslatef(GLfloat x, GLfloat y, GLfloat z) { void APIENTRY glScalef(GLfloat x, GLfloat y, GLfloat z) { - static Matrix4x4 scale __attribute__((aligned(32))) = { - 1.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 1.0f, 0.0f, + const Matrix4x4 scale __attribute__((aligned(32))) = { + x, 0.0f, 0.0f, 0.0f, + 0.0f, y, 0.0f, 0.0f, + 0.0f, 0.0f, z, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; - scale[M0] = x; - scale[M5] = y; - scale[M10] = z; - UploadMatrix4x4(stack_top(MATRIX_STACKS + MATRIX_IDX)); MultiplyMatrix4x4(&scale); DownloadMatrix4x4(stack_top(MATRIX_STACKS + MATRIX_IDX)); @@ -165,7 +157,7 @@ void APIENTRY glScalef(GLfloat x, GLfloat y, GLfloat z) { } void APIENTRY glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z) { - static Matrix4x4 rotate __attribute__((aligned(32))) = { + Matrix4x4 rotate __attribute__((aligned(32))) = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, @@ -199,7 +191,7 @@ void APIENTRY glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z) { rotate[M10] = (z * z) * invc + c; UploadMatrix4x4(stack_top(MATRIX_STACKS + MATRIX_IDX)); - MultiplyMatrix4x4(&rotate); + MultiplyMatrix4x4((const Matrix4x4*) &rotate); DownloadMatrix4x4(stack_top(MATRIX_STACKS + MATRIX_IDX)); if(MATRIX_MODE == GL_MODELVIEW) { @@ -244,7 +236,7 @@ void APIENTRY glOrtho(GLfloat left, GLfloat right, GLfloat znear, GLfloat zfar) { /* Ortho Matrix */ - static Matrix4x4 OrthoMatrix __attribute__((aligned(32))) = { + Matrix4x4 OrthoMatrix __attribute__((aligned(32))) = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, @@ -259,7 +251,7 @@ void APIENTRY glOrtho(GLfloat left, GLfloat right, OrthoMatrix[M14] = -(zfar + znear) / (zfar - znear); UploadMatrix4x4(stack_top(MATRIX_STACKS + MATRIX_IDX)); - MultiplyMatrix4x4(&OrthoMatrix); + MultiplyMatrix4x4((const Matrix4x4*) &OrthoMatrix); DownloadMatrix4x4(stack_top(MATRIX_STACKS + MATRIX_IDX)); } @@ -270,7 +262,7 @@ void APIENTRY glFrustum(GLfloat left, GLfloat right, GLfloat znear, GLfloat zfar) { /* Frustum Matrix */ - static Matrix4x4 FrustumMatrix __attribute__((aligned(32))); + Matrix4x4 FrustumMatrix __attribute__((aligned(32))); MEMSET(FrustumMatrix, 0, sizeof(float) * 16); @@ -290,37 +282,18 @@ void APIENTRY glFrustum(GLfloat left, GLfloat right, FrustumMatrix[M14] = D; UploadMatrix4x4(stack_top(MATRIX_STACKS + MATRIX_IDX)); - MultiplyMatrix4x4(&FrustumMatrix); + MultiplyMatrix4x4((const Matrix4x4*) &FrustumMatrix); DownloadMatrix4x4(stack_top(MATRIX_STACKS + MATRIX_IDX)); } /* Multiply the current matrix by an arbitrary matrix */ void glMultMatrixf(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]; + Matrix4x4 TEMP; + FASTCPY4(TEMP, m, sizeof(Matrix4x4)); UploadMatrix4x4(stack_top(MATRIX_STACKS + MATRIX_IDX)); - MultiplyMatrix4x4((Matrix4x4*) &TEMP); + MultiplyMatrix4x4((const Matrix4x4*) &TEMP); DownloadMatrix4x4(stack_top(MATRIX_STACKS + MATRIX_IDX)); if(MATRIX_MODE == GL_MODELVIEW) { @@ -387,7 +360,7 @@ void glMultTransposeMatrixf(const GLfloat *m) { TEMP[M15] = m[15]; UploadMatrix4x4(stack_top(MATRIX_STACKS + MATRIX_IDX)); - MultiplyMatrix4x4(&TEMP); + MultiplyMatrix4x4((const Matrix4x4*) &TEMP); DownloadMatrix4x4(stack_top(MATRIX_STACKS + MATRIX_IDX)); if(MATRIX_MODE == GL_MODELVIEW) { @@ -489,26 +462,26 @@ void gluLookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez, GLfloat centerx, trn[M14] = -eyez; // Does not modify internal Modelview matrix - UploadMatrix4x4(&m); - MultiplyMatrix4x4(&trn); + UploadMatrix4x4((const Matrix4x4*) &m); + MultiplyMatrix4x4((const Matrix4x4*) &trn); MultiplyMatrix4x4(stack_top(MATRIX_STACKS + (GL_MODELVIEW & 0xF))); DownloadMatrix4x4(stack_top(MATRIX_STACKS + (GL_MODELVIEW & 0xF))); } void _glApplyRenderMatrix() { - UploadMatrix4x4(&SCREENVIEW_MATRIX); - MultiplyMatrix4x4(stack_top(MATRIX_STACKS + (GL_PROJECTION & 0xF))); - MultiplyMatrix4x4(stack_top(MATRIX_STACKS + (GL_MODELVIEW & 0xF))); + UploadMatrix4x4((const Matrix4x4*) &SCREENVIEW_MATRIX); + MultiplyMatrix4x4((const Matrix4x4*) stack_top(MATRIX_STACKS + (GL_PROJECTION & 0xF))); + MultiplyMatrix4x4((const Matrix4x4*) stack_top(MATRIX_STACKS + (GL_MODELVIEW & 0xF))); } void _glMatrixLoadTexture() { - UploadMatrix4x4(stack_top(MATRIX_STACKS + (GL_TEXTURE & 0xF))); + UploadMatrix4x4((const Matrix4x4*) stack_top(MATRIX_STACKS + (GL_TEXTURE & 0xF))); } void _glMatrixLoadModelView() { - UploadMatrix4x4(stack_top(MATRIX_STACKS + (GL_MODELVIEW & 0xF))); + UploadMatrix4x4((const Matrix4x4*) stack_top(MATRIX_STACKS + (GL_MODELVIEW & 0xF))); } void _glMatrixLoadNormal() { - UploadMatrix4x4(&NORMAL_MATRIX); + UploadMatrix4x4((const Matrix4x4*) &NORMAL_MATRIX); } diff --git a/GL/platforms/sh4.c b/GL/platforms/sh4.c index 76b7647..0dceeb1 100644 --- a/GL/platforms/sh4.c +++ b/GL/platforms/sh4.c @@ -6,6 +6,7 @@ #define QACRTA ((((unsigned int)0x10000000)>>26)<<2)&0x1c +#define PVR_VERTEX_BUF_SIZE 2560 * 256 void InitGPU(_Bool autosort, _Bool fsaa) { pvr_init_params_t params = { @@ -32,8 +33,8 @@ void SceneListBegin(GPUList list) { } void SceneListSubmit(void* src, int n) { - GLuint *d = TA_SQ_ADDR; - GLuint *s = src; + uint32_t *d = (uint32_t*) TA_SQ_ADDR; + uint32_t *s = src; /* fill/write queues as many times necessary */ while(n--) { @@ -51,7 +52,7 @@ void SceneListSubmit(void* src, int n) { } /* Wait for both store queues to complete */ - d = (GLuint *)0xe0000000; + d = (uint32_t *)0xe0000000; d[0] = d[8] = 0; } diff --git a/GL/platforms/sh4.h b/GL/platforms/sh4.h index 8158cc8..64a754e 100644 --- a/GL/platforms/sh4.h +++ b/GL/platforms/sh4.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -21,33 +22,33 @@ #define VEC3_LENGTH(x, y, z, l) vec3f_length((x), (y), (z), (l)) #define VEC3_DOT(x1, y1, z1, x2, y2, z2, d) vec3f_dot((x1), (y1), (z1), (x2), (y2), (z2), (d)) -inline void CompilePolyHeader(PolyHeader* out, const PolyContext* in) { +static inline void CompilePolyHeader(PolyHeader* out, const PolyContext* in) { pvr_poly_compile((pvr_poly_hdr_t*) out, (pvr_poly_cxt_t*) in); } -inline void UploadMatrix4x4(const float* mat) { +static inline void UploadMatrix4x4(const Matrix4x4* mat) { mat_load((matrix_t*) mat); } -inline void DownloadMatrix4x4(float* mat) { +static inline void DownloadMatrix4x4(Matrix4x4* mat) { mat_store((matrix_t*) mat); } -inline void MultiplyMatrix4x4(const float* mat) { +static inline void MultiplyMatrix4x4(const Matrix4x4* mat) { mat_apply((matrix_t*) mat); } -inline void TransformVec3(float* x) { +static inline void TransformVec3(float* x) { mat_trans_single4(x[0], x[1], x[2], x[3]); } /* Transform a 3-element vector using the stored matrix (w == 1) */ -inline void TransformVec3NoMod(const float* xIn, float* xOut) { +static inline void TransformVec3NoMod(const float* xIn, float* xOut) { mat_trans_single3_nodiv_nomod(xIn[0], xIn[1], xIn[2], xOut[0], xOut[1], xOut[2]); } /* Transform a 3-element normal using the stored matrix (w == 0)*/ -inline void TransformNormalNoMod(const float* in, float* out) { +static inline void TransformNormalNoMod(const float* in, float* out) { mat_trans_normal3_nomod(in[0], in[1], in[2], out[0], out[1], out[2]); } @@ -56,7 +57,7 @@ inline void TransformVec4(float* x) { } -inline void TransformVertices(const Vertex* vertices, const int count) { +static inline void TransformVertices(Vertex* vertices, const int count) { Vertex* it = vertices; for(int i = 0; i < count; ++i, ++it) { register float __x __asm__("fr12") = (it->xyz[0]); @@ -80,40 +81,48 @@ inline void TransformVertices(const Vertex* vertices, const int count) { void InitGPU(_Bool autosort, _Bool fsaa); -inline void GPUSetPaletteFormat(GPUPaletteFormat format) { +static inline size_t GPUMemoryAvailable() { + return pvr_mem_available(); +} + +static inline void* GPUMemoryAlloc(size_t size) { + return pvr_mem_malloc(size); +} + +static inline void GPUSetPaletteFormat(GPUPaletteFormat format) { pvr_set_pal_format(format); } -inline void GPUSetPaletteEntry(uint32_t idx, uint32_t value) { +static inline void GPUSetPaletteEntry(uint32_t idx, uint32_t value) { pvr_set_pal_entry(idx, value); } -inline void GPUSetBackgroundColour(float r, float g, float b) { +static inline void GPUSetBackgroundColour(float r, float g, float b) { pvr_set_bg_color(r, g, b); } #define PT_ALPHA_REF 0x011c -inline void GPUSetAlphaCutOff(uint8_t val) { +static inline void GPUSetAlphaCutOff(uint8_t val) { PVR_SET(PT_ALPHA_REF, val); } -inline void GPUSetClearDepth(float v) { +static inline void GPUSetClearDepth(float v) { pvr_set_zclip(v); } -inline void GPUSetFogLinear(float start, float end) { +static inline void GPUSetFogLinear(float start, float end) { pvr_fog_table_linear(start, end); } -inline void GPUSetFogExp(float density) { +static inline void GPUSetFogExp(float density) { pvr_fog_table_exp(density); } -inline void GPUSetFogExp2(float density) { +static inline void GPUSetFogExp2(float density) { pvr_fog_table_exp2(density); } -inline void GPUSetFogColor(float r, float g, float b, float a) { +static inline void GPUSetFogColor(float r, float g, float b, float a) { pvr_fog_table_color(r, g, b, a); } diff --git a/GL/platforms/software.c b/GL/platforms/software.c index 9620a71..e87a85b 100644 --- a/GL/platforms/software.c +++ b/GL/platforms/software.c @@ -5,18 +5,89 @@ #include "../platform.h" #include "software.h" +#include "software/edge_equation.h" +#include "software/parameter_equation.h" static size_t AVAILABLE_VRAM = 16 * 1024 * 1024; static Matrix4x4 MATRIX; +static SDL_Window* WINDOW = NULL; +static SDL_Renderer* RENDERER = NULL; + static VideoMode vid_mode = { 640, 480 }; + +typedef struct GPUVertex { + uint32_t flags; + float x; + float y; + float z; + float u; + float v; + uint8_t bgra[4]; + uint8_t obgra[4]; +} GPUVertex; + +#define MIN(x, y) ((x) < (y) ? (x) : (y)) +#define MAX(x, y) ((x) > (y) ? (x) : (y)) + +static void DrawTriangle(const GPUVertex* v0, const GPUVertex* v1, const GPUVertex* v2) { + // Compute triangle bounding box. + + int minX = MIN(MIN(v0->x, v1->x), v2->x); + int maxX = MAX(MAX(v0->x, v1->x), v2->x); + int minY = MIN(MIN(v0->y, v1->y), v2->y); + int maxY = MAX(MAX(v0->y, v1->y), v2->y); + + // Clip to scissor rect. +/* + minX = MAX(minX, m_minX); + maxX = MIN(maxX, m_maxX); + minY = MAX(minY, m_minY); + maxY = MIN(maxY, m_maxY); */ + + // Compute edge equations. + + EdgeEquation e0, e1, e2; + EdgeEquationInit(&e0, &v0->x, &v1->x); + EdgeEquationInit(&e1, &v1->x, &v2->x); + EdgeEquationInit(&e2, &v2->x, &v0->x); + + float area = 0.5 * (e0.c + e1.c + e2.c); + + ParameterEquation r, g, b; + + ParameterEquationInit(&r, v0->bgra[2], v1->bgra[2], v2->bgra[2], &e0, &e1, &e2, area); + ParameterEquationInit(&g, v0->bgra[1], v1->bgra[1], v2->bgra[1], &e0, &e1, &e2, area); + ParameterEquationInit(&b, v0->bgra[0], v1->bgra[0], v2->bgra[0], &e0, &e1, &e2, area); + + // Check if triangle is backfacing. + + if (area < 0) { + return; + } + + // Add 0.5 to sample at pixel centers. + for (float x = minX + 0.5f, xm = maxX + 0.5f; x <= xm; x += 1.0f) + for (float y = minY + 0.5f, ym = maxY + 0.5f; y <= ym; y += 1.0f) + { + if (EdgeEquationTestPoint(&e0, x, y) && EdgeEquationTestPoint(&e1, x, y) && EdgeEquationTestPoint(&e2, x, y)) { + int rint = ParameterEquationEvaluate(&r, x, y); + int gint = ParameterEquationEvaluate(&g, x, y); + int bint = ParameterEquationEvaluate(&b, x, y); + SDL_SetRenderDrawColor(RENDERER, rint, gint, bint, 255); + SDL_RenderDrawPoint(RENDERER, x, y); + } + } +} + + void InitGPU(_Bool autosort, _Bool fsaa) { SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS); - SDL_Window *window = SDL_CreateWindow( + WINDOW = SDL_CreateWindow( "GLdc", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, @@ -24,14 +95,14 @@ void InitGPU(_Bool autosort, _Bool fsaa) { SDL_WINDOW_SHOWN ); - - SDL_Renderer *renderer = SDL_CreateRenderer( - window, -1, SDL_RENDERER_ACCELERATED + RENDERER = SDL_CreateRenderer( + WINDOW, -1, SDL_RENDERER_ACCELERATED ); } void SceneBegin() { - + SDL_SetRenderDrawColor(RENDERER, 0, 0, 0, 0); + SDL_RenderClear(RENDERER); } void SceneListBegin(GPUList list) { @@ -39,7 +110,37 @@ void SceneListBegin(GPUList list) { } void SceneListSubmit(void* src, int n) { + uint32_t vertex_counter = 0; + const uint32_t* flags = (const uint32_t*) src; + uint32_t step = sizeof(GPUVertex) / sizeof(uint32_t); + for(int i = 0; i < n; ++i, flags += step) { + bool draw_triangle = false; + switch(*flags) { + case GPU_CMD_POLYHDR: + break; + case GPU_CMD_VERTEX_EOL: + draw_triangle = (++vertex_counter == 3); + vertex_counter = 0; + break; + case GPU_CMD_VERTEX: // Fallthrough + vertex_counter++; + draw_triangle = (vertex_counter == 3); + if(draw_triangle) { + vertex_counter--; + } + break; + default: + break; + } + + if(draw_triangle) { + const GPUVertex* v0 = (const GPUVertex*) (flags - step - step); + const GPUVertex* v1 = (const GPUVertex*) (flags - step); + const GPUVertex* v2 = (const GPUVertex*) (flags); + DrawTriangle(v0, v1, v2); + } + } } void SceneListFinish() { @@ -47,6 +148,7 @@ void SceneListFinish() { } void SceneFinish() { + SDL_RenderPresent(RENDERER); /* Only sensible place to hook the quit signal */ @@ -67,7 +169,18 @@ void UploadMatrix4x4(const Matrix4x4* mat) { } void MultiplyMatrix4x4(const Matrix4x4* mat) { - (void) mat; + Matrix4x4 product; + + for(int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + product[j + i * 4] = 0; + for (int k = 0; k < 4; k++) { + product[j + i * 4] += MATRIX[k + i * 4] * (*mat)[j + k * 4]; + } + } + } + + UploadMatrix4x4(&product); } void DownloadMatrix4x4(Matrix4x4* mat) { diff --git a/GL/platforms/software.h b/GL/platforms/software.h index 30698ee..4e8f0b3 100644 --- a/GL/platforms/software.h +++ b/GL/platforms/software.h @@ -61,7 +61,7 @@ static inline void TransformVec4(float* x) { } -static inline void TransformVertices(const Vertex* vertices, const int count) { +static inline void TransformVertices(Vertex* vertices, const int count) { (void) vertices; (void) count; } diff --git a/GL/platforms/software/parameter_equation.h b/GL/platforms/software/parameter_equation.h index 7aea832..6c3beb7 100644 --- a/GL/platforms/software/parameter_equation.h +++ b/GL/platforms/software/parameter_equation.h @@ -11,6 +11,6 @@ struct EdgeEquation; void ParameterEquationInit( ParameterEquation* equation, float p0, float p1, float p2, - const EdgeEquation* e0, const EdgeEquation* e1, const EdgeEquation* e2, float area); + const struct EdgeEquation* e0, const struct EdgeEquation* e1, const struct EdgeEquation* e2, float area); float ParameterEquationEvaluate(const ParameterEquation* equation, float x, float y); diff --git a/GL/private.h b/GL/private.h index 3236498..2908508 100644 --- a/GL/private.h +++ b/GL/private.h @@ -356,7 +356,6 @@ GLuint _glFreeTextureMemory(); GLuint _glUsedTextureMemory(); GLuint _glFreeContiguousTextureMemory(); -#define PVR_VERTEX_BUF_SIZE 2560 * 256 #define MAX_TEXTURE_UNITS 2 #define MAX_LIGHTS 8 diff --git a/containers/aligned_vector.h b/containers/aligned_vector.h index dbbcda4..b64d089 100644 --- a/containers/aligned_vector.h +++ b/containers/aligned_vector.h @@ -1,14 +1,12 @@ #pragma once -#ifndef ALIGNED_VECTOR_H -#define ALIGNED_VECTOR_H +#include +#include #ifdef __cplusplus extern "C" { #endif -#include - typedef struct { unsigned int size; unsigned int capacity; @@ -39,5 +37,3 @@ static inline void* aligned_vector_back(AlignedVector* vector){ #ifdef __cplusplus } #endif - -#endif // ALIGNED_VECTOR_H