From be974e96ad629c348c8720cb7f17258cfcc74900 Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Sat, 3 Apr 2021 14:11:54 +0100 Subject: [PATCH] More porting work --- CMakeLists.txt | 2 + GL/draw.c | 65 ++++++++-------------- GL/matrix.c | 20 +++---- GL/platform.h | 132 +++++++++++++++++++++++++++++++++++++++++++++ GL/platforms/sh4.h | 39 ++++++++++++++ GL/platforms/x86.h | 28 ++++++++++ GL/private.h | 113 ++------------------------------------ GL/state.c | 6 +-- GL/types.h | 16 ++++++ 9 files changed, 257 insertions(+), 164 deletions(-) create mode 100644 GL/types.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7047c41..22da63d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,8 @@ cmake_minimum_required(VERSION 3.0) project(GLdc) +include_directories(include) + set( SOURCES GL/clip.c diff --git a/GL/draw.c b/GL/draw.c index fc99dee..6c96bcb 100644 --- a/GL/draw.c +++ b/GL/draw.c @@ -4,14 +4,13 @@ #include #include #include -#include #include "../include/gl.h" #include "../include/glext.h" + #include "private.h" #include "profiler.h" -#include "sh4_math.h" - +#include "platform.h" static AttribPointer VERTEX_POINTER; static AttribPointer UV_POINTER; @@ -439,7 +438,7 @@ GL_FORCE_INLINE void transformNormalToEyeSpace(GLfloat* normal) { mat_trans_normal3(normal[0], normal[1], normal[2]); } -PVRHeader* _glSubmissionTargetHeader(SubmissionTarget* target) { +PolyHeader *_glSubmissionTargetHeader(SubmissionTarget* target) { assert(target->header_offset < target->output->vector.size); return aligned_vector_at(&target->output->vector, target->header_offset); } @@ -458,7 +457,7 @@ static inline void genTriangles(Vertex* output, GLuint count) { GLuint i; for(i = 0; i < count; i += 3) { - it->flags = PVR_CMD_VERTEX_EOL; + it->flags = GPU_CMD_VERTEX_EOL; it += 3; } } @@ -468,13 +467,13 @@ static inline void genQuads(Vertex* output, GLuint count) { GLuint i; for(i = 0; i < count; i += 4) { swapVertex((final - 1), final); - final->flags = PVR_CMD_VERTEX_EOL; + final->flags = GPU_CMD_VERTEX_EOL; final += 4; } } static void genTriangleStrip(Vertex* output, GLuint count) { - output[count - 1].flags = PVR_CMD_VERTEX_EOL; + output[count - 1].flags = GPU_CMD_VERTEX_EOL; } static void genTriangleFan(Vertex* output, GLuint count) { @@ -486,7 +485,7 @@ static void genTriangleFan(Vertex* output, GLuint count) { GLubyte i = count - 2; while(i--) { *dst = *src--; - (*dst--).flags = PVR_CMD_VERTEX_EOL; + (*dst--).flags = GPU_CMD_VERTEX_EOL; *dst-- = *src; *dst-- = *output; } @@ -765,7 +764,7 @@ static void generateElements( st_func(st, (GLubyte*) ve->st); normal_func(nxyz, (GLubyte*) ve->nxyz); - output->flags = PVR_CMD_VERTEX; + output->flags = GPU_CMD_VERTEX; ++output; ++ve; } @@ -786,7 +785,7 @@ static void generate(SubmissionTarget* target, const GLenum mode, const GLsizei const GLubyte* pos = VERTEX_POINTER.ptr; Vertex* it = start; ITERATE(count) { - it->flags = PVR_CMD_VERTEX; + it->flags = GPU_CMD_VERTEX; memcpy(it->xyz, pos, FAST_PATH_BYTE_SIZE); it++; pos += VERTEX_POINTER.stride; @@ -799,7 +798,7 @@ static void generate(SubmissionTarget* target, const GLenum mode, const GLsizei Vertex* it = _glSubmissionTargetStart(target); ITERATE(count) { - it->flags = PVR_CMD_VERTEX; + it->flags = GPU_CMD_VERTEX; ++it; } } @@ -843,25 +842,7 @@ static void transform(SubmissionTarget* target) { _glApplyRenderMatrix(); /* Apply the Render Matrix Stack */ - ITERATE(target->count) { - register float __x __asm__("fr12") = (vertex->xyz[0]); - register float __y __asm__("fr13") = (vertex->xyz[1]); - register float __z __asm__("fr14") = (vertex->xyz[2]); - register float __w __asm__("fr15") = (vertex->w); - - __asm__ __volatile__( - "fldi1 fr15\n" - "ftrv xmtrx,fv12\n" - : "=f" (__x), "=f" (__y), "=f" (__z), "=f" (__w) - : "0" (__x), "1" (__y), "2" (__z), "3" (__w) - ); - - vertex->xyz[0] = __x; - vertex->xyz[1] = __y; - vertex->xyz[2] = __z; - vertex->w = __w; - ++vertex; - } + TransformVertices(vertex, target->count); } static void clip(SubmissionTarget* target) { @@ -930,8 +911,8 @@ static void light(SubmissionTarget* target) { _glPerformLighting(vertex, ES, target->count); } -#define PVR_MIN_Z 0.2f -#define PVR_MAX_Z 1.0 + PVR_MIN_Z +#define GPU_MIN_Z 0.2f +#define GPU_MAX_Z 1.0 + GPU_MIN_Z GL_FORCE_INLINE void divide(SubmissionTarget* target) { TRACE(); @@ -949,26 +930,26 @@ GL_FORCE_INLINE void divide(SubmissionTarget* target) { } } -GL_FORCE_INLINE void push(PVRHeader* header, GLboolean multiTextureHeader, PolyList* activePolyList, GLshort textureUnit) { +GL_FORCE_INLINE void push(PolyHeader* header, GLboolean multiTextureHeader, PolyList* activePolyList, GLshort textureUnit) { TRACE(); // Compile the header - pvr_poly_cxt_t cxt = *_glGetPVRContext(); + PolyContext cxt = *_glGetPVRContext(); cxt.list_type = activePolyList->list_type; _glUpdatePVRTextureContext(&cxt, textureUnit); if(multiTextureHeader) { - assert(cxt.list_type == PVR_LIST_TR_POLY); + assert(cxt.list_type == GPU_LIST_TR_POLY); - cxt.gen.alpha = PVR_ALPHA_ENABLE; - cxt.txr.alpha = PVR_TXRALPHA_ENABLE; - cxt.blend.src = PVR_BLEND_ZERO; - cxt.blend.dst = PVR_BLEND_DESTCOLOR; - cxt.depth.comparison = PVR_DEPTHCMP_EQUAL; + cxt.gen.alpha = GPU_ALPHA_ENABLE; + cxt.txr.alpha = GPU_TXRALPHA_ENABLE; + cxt.blend.src = GPU_BLEND_ZERO; + cxt.blend.dst = GPU_BLEND_DESTCOLOR; + cxt.depth.comparison = GPU_DEPTHCMP_EQUAL; } - pvr_poly_compile(&header->hdr, &cxt); + CompilePolyHeader(header, &cxt); /* Post-process the vertex list */ /* @@ -1136,7 +1117,7 @@ GL_FORCE_INLINE void submitVertices(GLenum mode, GLsizei first, GLuint count, GL assert(vertex); - PVRHeader* mtHeader = (PVRHeader*) vertex++; + PolyHeader* mtHeader = (PolyHeader*) vertex++; /* Replace the UV coordinates with the ST ones */ VertexExtra* ve = aligned_vector_at(target->extras, 0); diff --git a/GL/matrix.c b/GL/matrix.c index b0b21d5..5f1b569 100644 --- a/GL/matrix.c +++ b/GL/matrix.c @@ -501,26 +501,26 @@ void gluLookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez, GLfloat centerx, trn[M14] = -eyez; // Does not modify internal Modelview matrix - upload_matrix(&m); - multiply_matrix(&trn); - multiply_matrix(stack_top(MATRIX_STACKS + (GL_MODELVIEW & 0xF))); - download_matrix(stack_top(MATRIX_STACKS + (GL_MODELVIEW & 0xF))); + UploadMatrix4x4(&m); + MultiplyMatrix4x4(&trn); + MultiplyMatrix4x4(stack_top(MATRIX_STACKS + (GL_MODELVIEW & 0xF))); + DownloadMatrix4x4(stack_top(MATRIX_STACKS + (GL_MODELVIEW & 0xF))); } void _glApplyRenderMatrix() { - upload_matrix(&SCREENVIEW_MATRIX); - multiply_matrix(stack_top(MATRIX_STACKS + (GL_PROJECTION & 0xF))); - multiply_matrix(stack_top(MATRIX_STACKS + (GL_MODELVIEW & 0xF))); + UploadMatrix4x4(&SCREENVIEW_MATRIX); + MultiplyMatrix4x4(stack_top(MATRIX_STACKS + (GL_PROJECTION & 0xF))); + MultiplyMatrix4x4(stack_top(MATRIX_STACKS + (GL_MODELVIEW & 0xF))); } void _glMatrixLoadTexture() { - upload_matrix(stack_top(MATRIX_STACKS + (GL_TEXTURE & 0xF))); + UploadMatrix4x4(stack_top(MATRIX_STACKS + (GL_TEXTURE & 0xF))); } void _glMatrixLoadModelView() { - upload_matrix(stack_top(MATRIX_STACKS + (GL_MODELVIEW & 0xF))); + UploadMatrix4x4(stack_top(MATRIX_STACKS + (GL_MODELVIEW & 0xF))); } void _glMatrixLoadNormal() { - upload_matrix(&NORMAL_MATRIX); + UploadMatrix4x4(&NORMAL_MATRIX); } diff --git a/GL/platform.h b/GL/platform.h index 6beb6f9..2852d33 100644 --- a/GL/platform.h +++ b/GL/platform.h @@ -1,5 +1,137 @@ #pragma once +#include + +typedef enum GPUAlpha { + GPU_ALPHA_DISABLE = 0, + GPU_ALPHA_ENABLE = 1 +} GPUAlpha; + +typedef enum GPUTextureAlpha { + GPU_TXRALPHA_DISABLE = 1, + GPU_TXRALPHA_ENABLE = 0 +} GPUTextureAlpha; + +typedef enum GPUList { + GPU_LIST_OP_POLY = 0, + GPU_LIST_OP_MOD = 1, + GPU_LIST_TR_POLY = 2, + GPU_LIST_TR_MOD = 3, + GPU_LIST_PT_POLY = 4 +} GPUList; + +typedef enum GPUBlend { + GPU_BLEND_ZERO = 0, + GPU_BLEND_ONE = 1, + GPU_BLEND_DESTCOLOR = 2, + GPU_BLEND_INVDESTCOLOR = 3, + GPU_BLEND_SRCALPHA = 4, + GPU_BLEND_INVSRCALPHA = 5, + GPU_BLEND_DESTALPHA = 6, + GPU_BLEND_INVDESTALPHA = 7 +} GPUBlend; + + +typedef enum GPUDepthCompare { + GPU_DEPTHCMP_NEVER = 0, + GPU_DEPTHCMP_LESS = 1, + GPU_DEPTHCMP_EQUAL = 2, + GPU_DEPTHCMP_LEQUAL = 3, + GPU_DEPTHCMP_GREATER = 4, + GPU_DEPTHCMP_NOTEQUAL = 5, + GPU_DEPTHCMP_GEQUAL = 6, + GPU_DEPTHCMP_ALWAYS = 7 +} GPUDepthCompare; + +/* Duplication of pvr_poly_cxt_t from KOS so that we can + * compile on non-KOS platforms for testing */ + +typedef struct { + GPUList list_type; + + struct { + int alpha; + int shading; + int fog_type; + int culling; + int color_clamp; + int clip_mode; + int modifier_mode; + int specular; + int alpha2; + int fog_type2; + int color_clamp2; + } gen; + struct { + int src; + int dst; + int src_enable; + int dst_enable; + int src2; + int dst2; + int src_enable2; + int dst_enable2; + } blend; + struct { + int color; + int uv; + int modifier; + } fmt; + struct { + int comparison; + int write; + } depth; + struct { + int enable; + int filter; + int mipmap; + int mipmap_bias; + int uv_flip; + int uv_clamp; + int alpha; + int env; + int width; + int height; + int format; + void* base; + } txr; + struct { + int enable; + int filter; + int mipmap; + int mipmap_bias; + int uv_flip; + int uv_clamp; + int alpha; + int env; + int width; + int height; + int format; + void* base; + } txr2; +} PolyContext; + +typedef struct { + uint32_t cmd; + uint32_t mode1; + uint32_t mode2; + uint32_t mode3; + uint32_t d1; + uint32_t d2; + uint32_t d3; + uint32_t d4; +} PolyHeader; + +enum GPUCommand { + GPU_CMD_POLYHDR = 0x80840000, + GPU_CMD_VERTEX = 0xe0000000, + GPU_CMD_VERTEX_EOL = 0xf0000000, + GPU_CMD_USERCLIP = 0x20000000, + GPU_CMD_MODIFIER = 0x80000000, + GPU_CMD_SPRITE = 0xA0000000 +}; + + #ifdef __DREAMCAST__ #include "platforms/sh4.h" #else diff --git a/GL/platforms/sh4.h b/GL/platforms/sh4.h index ce25f1c..9a9ef17 100644 --- a/GL/platforms/sh4.h +++ b/GL/platforms/sh4.h @@ -6,4 +6,43 @@ #include #include +#include "../types.h" #include "sh4_math.h" + +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) { + mat_load((matrix_t*) mat); +} + +inline void DownloadMatrix4x4(float* mat) { + mat_store((matrix_t*) mat); +} + +inline void MultiplyMatrix4x4(const float* mat) { + mat_apply((matrix_t*) mat); +} + +inline void TransformVertices(const Vertex* vertices, const int count) { + Vertex* it = vertices; + for(int i = 0; i < count; ++i, ++it) { + register float __x __asm__("fr12") = (it->xyz[0]); + register float __y __asm__("fr13") = (it->xyz[1]); + register float __z __asm__("fr14") = (it->xyz[2]); + register float __w __asm__("fr15") = (it->w); + + __asm__ __volatile__( + "fldi1 fr15\n" + "ftrv xmtrx,fv12\n" + : "=f" (__x), "=f" (__y), "=f" (__z), "=f" (__w) + : "0" (__x), "1" (__y), "2" (__z), "3" (__w) + ); + + it->xyz[0] = __x; + it->xyz[1] = __y; + it->xyz[2] = __z; + it->w = __w; + } +} diff --git a/GL/platforms/x86.h b/GL/platforms/x86.h index af268d9..ce6e316 100644 --- a/GL/platforms/x86.h +++ b/GL/platforms/x86.h @@ -1,4 +1,32 @@ #pragma once +#include +#include "../types.h" + #define MATH_Fast_Divide(n, d) (n / d) #define MATH_fmac(a, b, c) (a * b + c) +#define MATH_Fast_Sqrt(x) sqrt((x)) +#define MATH_fsrra(x) (1.0f / sqrt((x))) +#define MATH_Fast_Invert(x) (1.0f / (x)) + +inline void CompilePolyHeader(PolyHeader* out, const PolyContext* in) { + (void) out; + (void) in; +} + +inline void UploadMatrix4x4(const float* mat) { + (void) mat; +} + +inline void MultiplyMatrix4x4(const float* mat) { + (void) mat; +} + +inline void DownloadMatrix4x4(float* mat) { + (void) mat; +} + +inline void TransformVertices(const Vertex* vertices, const int count) { + (void) vertices; + (void) count; +} diff --git a/GL/private.h b/GL/private.h index 904686d..8042fff 100644 --- a/GL/private.h +++ b/GL/private.h @@ -4,6 +4,7 @@ #include #include "platform.h" +#include "types.h" #include "../include/gl.h" #include "../containers/aligned_vector.h" @@ -85,99 +86,6 @@ typedef float Matrix4x4[16]; #define M15 15 #endif -typedef struct { - uint32_t cmd; - uint32_t mode1; - uint32_t mode2; - uint32_t mode3; - uint32_t d1; - uint32_t d2; - uint32_t d3; - uint32_t d4; -} PVRHeader; - -typedef struct { - int list_type; /**< \brief Primitive list - \see pvr_lists */ - struct { - int alpha; /**< \brief Enable or disable alpha outside modifier - \see pvr_alpha_switch */ - int shading; /**< \brief Shading type - \see pvr_shading_types */ - int fog_type; /**< \brief Fog type outside modifier - \see pvr_fog_types */ - int culling; /**< \brief Culling mode - \see pvr_cull_modes */ - int color_clamp; /**< \brief Color clamp enable/disable outside modifier - \see pvr_colclamp_switch */ - int clip_mode; /**< \brief Clipping mode - \see pvr_clip_modes */ - int modifier_mode; /**< \brief Modifier mode */ - int specular; /**< \brief Offset color enable/disable outside modifier - \see pvr_offset_switch */ - int alpha2; /**< \brief Enable/disable alpha inside modifier - \see pvr_alpha_switch */ - int fog_type2; /**< \brief Fog type inside modifier - \see pvr_fog_types */ - int color_clamp2; /**< \brief Color clamp enable/disable inside modifier - \see pvr_colclamp_switch */ - } gen; /**< \brief General parameters */ - struct { - int src; /**< \brief Source blending mode outside modifier - \see pvr_blend_modes */ - int dst; /**< \brief Dest blending mode outside modifier - \see pvr_blend_modes */ - int src_enable; /**< \brief Source blending enable outside modifier - \see pvr_blend_switch */ - int dst_enable; /**< \brief Dest blending enable outside modifier - \see pvr_blend_switch */ - int src2; /**< \brief Source blending mode inside modifier - \see pvr_blend_modes */ - int dst2; /**< \brief Dest blending mode inside modifier - \see pvr_blend_modes */ - int src_enable2; /**< \brief Source blending mode inside modifier - \see pvr_blend_switch */ - int dst_enable2; /**< \brief Dest blending mode inside modifier - \see pvr_blend_switch */ - } blend; /**< \brief Blending parameters */ - struct { - int color; - int uv; - int modifier; - } fmt; - struct { - int comparison; - int write; - } depth; - struct { - int enable; - int filter; - int mipmap; - int mipmap_bias; - int uv_flip; - int uv_clamp; - int alpha; - int env; - int width; - int height; - int format; - void* base; - } txr; - struct { - int enable; - int filter; - int mipmap; - int mipmap_bias; - int uv_flip; - int uv_clamp; - int alpha; - int env; - int width; - int height; - int format; - void* base; - } txr2; -} PVRState; typedef struct { unsigned int flags; /* Constant PVR_CMD_USERCLIP */ @@ -278,19 +186,6 @@ typedef struct { GLfloat specularMaterial[4]; } LightSource; -typedef struct { - /* Same 32 byte layout as pvr_vertex_t */ - uint32_t flags; - float xyz[3]; - float uv[2]; - uint8_t bgra[4]; - - /* In the pvr_vertex_t structure, this next 4 bytes is oargb - * but we're not using that for now, so having W here makes the code - * simpler */ - float w; -} Vertex; - #define argbcpy(dst, src) \ *((GLuint*) dst) = *((GLuint*) src) \ @@ -349,7 +244,7 @@ typedef struct { AlignedVector* extras; } SubmissionTarget; -PVRHeader* _glSubmissionTargetHeader(SubmissionTarget* target); +PolyHeader* _glSubmissionTargetHeader(SubmissionTarget* target); Vertex* _glSubmissionTargetStart(SubmissionTarget* target); Vertex* _glSubmissionTargetEnd(SubmissionTarget* target); @@ -396,10 +291,10 @@ Matrix4x4* _glGetModelViewMatrix(); void _glWipeTextureOnFramebuffers(GLuint texture); GLubyte _glCheckImmediateModeInactive(const char* func); -pvr_poly_cxt_t* _glGetPVRContext(); +PolyContext* _glGetPVRContext(); GLubyte _glInitTextures(); -void _glUpdatePVRTextureContext(pvr_poly_cxt_t* context, GLshort textureUnit); +void _glUpdatePVRTextureContext(PolyContext* context, GLshort textureUnit); void _glAllocateSpaceForMipmaps(TextureObject* active); typedef struct { diff --git a/GL/state.c b/GL/state.c index 4f08ea5..b6a14f6 100644 --- a/GL/state.c +++ b/GL/state.c @@ -12,9 +12,9 @@ #include "private.h" -static pvr_poly_cxt_t GL_CONTEXT; +static PolyContext GL_CONTEXT; -pvr_poly_cxt_t* _glGetPVRContext() { +PolyContext *_glGetPVRContext() { return &GL_CONTEXT; } @@ -158,7 +158,7 @@ GLboolean _glCheckValidEnum(GLint param, GLint* values, const char* func) { GLboolean TEXTURES_ENABLED [] = {GL_FALSE, GL_FALSE}; -void _glUpdatePVRTextureContext(pvr_poly_cxt_t* context, GLshort textureUnit) { +void _glUpdatePVRTextureContext(PolyContext *context, GLshort textureUnit) { const TextureObject *tx1 = (textureUnit == 0) ? _glGetTexture0() : _glGetTexture1(); /* Disable all texturing to start with */ diff --git a/GL/types.h b/GL/types.h new file mode 100644 index 0000000..99758e2 --- /dev/null +++ b/GL/types.h @@ -0,0 +1,16 @@ +#pragma once + +#include + +typedef struct { + /* Same 32 byte layout as pvr_vertex_t */ + uint32_t flags; + float xyz[3]; + float uv[2]; + uint8_t bgra[4]; + + /* In the pvr_vertex_t structure, this next 4 bytes is oargb + * but we're not using that for now, so having W here makes the code + * simpler */ + float w; +} Vertex;