From 542325c75c39c9d6a69572bf725a174c7c007ce3 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 25 Nov 2023 18:44:57 +1100 Subject: [PATCH] PS2: Textures sort of work --- src/Core.h | 10 ++++++ src/Graphics_PS2.c | 81 +++++++++++++++++++++++++++------------------- src/SystemFonts.c | 2 +- 3 files changed, 59 insertions(+), 34 deletions(-) diff --git a/src/Core.h b/src/Core.h index 5ea12e43f..347e8b5eb 100644 --- a/src/Core.h +++ b/src/Core.h @@ -130,6 +130,7 @@ typedef cc_uint8 cc_bool; #define CC_BUILD_BEARSSL #define CC_BUILD_LOWMEM #define CC_BUILD_CONSOLE + #undef CC_BUILD_FREETYPE #elif defined XENON /* libxenon also defines __linux__ (yes, really) */ #define CC_BUILD_XBOX360 @@ -138,6 +139,7 @@ typedef cc_uint8 cc_bool; #define CC_BUILD_HTTPCLIENT #define CC_BUILD_LOWMEM #define CC_BUILD_CONSOLE + #undef CC_BUILD_FREETYPE #elif defined _WIN32 #define CC_BUILD_WIN #define CC_BUILD_D3D9 @@ -271,6 +273,7 @@ typedef cc_uint8 cc_bool; #define CC_BUILD_BEARSSL #define CC_BUILD_LOWMEM #define CC_BUILD_CONSOLE + #undef CC_BUILD_FREETYPE #elif defined __3DS__ #define CC_BUILD_3DS #define CC_BUILD_OPENAL @@ -278,6 +281,7 @@ typedef cc_uint8 cc_bool; #define CC_BUILD_BEARSSL #define CC_BUILD_LOWMEM #define CC_BUILD_CONSOLE + #undef CC_BUILD_FREETYPE #elif defined GEKKO #define CC_BUILD_GCWII #define CC_BUILD_OPENAL @@ -286,6 +290,7 @@ typedef cc_uint8 cc_bool; #define CC_BUILD_COOPTHREADED #define CC_BUILD_LOWMEM #define CC_BUILD_CONSOLE + #undef CC_BUILD_FREETYPE #elif defined __vita__ #define CC_BUILD_PSVITA #define CC_BUILD_OPENAL @@ -293,12 +298,14 @@ typedef cc_uint8 cc_bool; #define CC_BUILD_BEARSSL #define CC_BUILD_LOWMEM #define CC_BUILD_CONSOLE + #undef CC_BUILD_FREETYPE #elif defined _arch_dreamcast #define CC_BUILD_DREAMCAST #define CC_BUILD_OPENAL #define CC_BUILD_HTTPCLIENT #define CC_BUILD_LOWMEM #define CC_BUILD_CONSOLE + #undef CC_BUILD_FREETYPE #elif defined PLAT_PS3 #define CC_BUILD_PS3 #define CC_BUILD_OPENAL @@ -306,6 +313,7 @@ typedef cc_uint8 cc_bool; #define CC_BUILD_LOWMEM #define CC_BUILD_BEARSSL #define CC_BUILD_CONSOLE + #undef CC_BUILD_FREETYPE #elif defined N64 #define CC_BIG_ENDIAN #define CC_BUILD_N64 @@ -314,6 +322,7 @@ typedef cc_uint8 cc_bool; #define CC_BUILD_COOPTHREADED #define CC_BUILD_LOWMEM #define CC_BUILD_CONSOLE + #undef CC_BUILD_FREETYPE #elif defined PLAT_PS2 #define CC_BUILD_PS2 #define CC_BUILD_OPENAL @@ -321,6 +330,7 @@ typedef cc_uint8 cc_bool; #define CC_BUILD_COOPTHREADED #define CC_BUILD_LOWMEM #define CC_BUILD_CONSOLE + #undef CC_BUILD_FREETYPE #endif #endif diff --git a/src/Graphics_PS2.c b/src/Graphics_PS2.c index 7fa142886..1cfda7013 100644 --- a/src/Graphics_PS2.c +++ b/src/Graphics_PS2.c @@ -63,14 +63,37 @@ static void InitBuffers(void) { graph_initialize(fb_color.address, fb_color.width, fb_color.height, fb_color.psm, 0, 0); } +static qword_t* SetTextureWrapping(qword_t* q, int context) { + PACK_GIFTAG(q, GIF_SET_TAG(1,0,0,0, GIF_FLG_PACKED, 1), GIF_REG_AD); + q++; + + PACK_GIFTAG(q, GS_SET_CLAMP(WRAP_REPEAT, WRAP_REPEAT, 0, 0, 0, 0), + GS_REG_CLAMP + context); + q++; + return q; +} + +static qword_t* SetTextureSampling(qword_t* q, int context) { + PACK_GIFTAG(q, GIF_SET_TAG(1,0,0,0, GIF_FLG_PACKED, 1), GIF_REG_AD); + q++; + + // TODO: should mipmapselect (first 0 after MIN_NEAREST) be 1? + PACK_GIFTAG(q, GS_SET_TEX1(LOD_USE_K, 0, LOD_MAG_NEAREST, LOD_MIN_NEAREST, 0, 0, 0), + GS_REG_TEX1 + context); + q++; + return q; +} + static void InitDrawingEnv(void) { - packet_t *packet = packet_init(20, PACKET_NORMAL); + packet_t *packet = packet_init(30, PACKET_NORMAL); // TODO: is 30 too much? qword_t *q = packet->data; q = draw_setup_environment(q, 0, &fb_color, &fb_depth); // GS can render from 0 to 4096, so set primitive origin to centre of that q = draw_primitive_xyoffset(q, 0, 2048 - vp_hwidth, 2048 - vp_hheight); + q = SetTextureWrapping(q, 0); + q = SetTextureSampling(q, 0); q = draw_finish(q); dma_channel_send_normal(DMA_CHANNEL_GIF,packet->data,q - packet->data, 0, 0); @@ -102,7 +125,7 @@ void Gfx_Create(void) { InitBuffers(); InitDrawingEnv(); InitDMABuffers(); - tex_offset = graph_vram_size(16, 16, GS_PSM_32, GRAPH_ALIGN_PAGE); + tex_offset = graph_vram_size(64, 64, GS_PSM_32, GRAPH_ALIGN_PAGE); context = 1; FlipContext(); @@ -142,39 +165,29 @@ static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, cc_uint8 flags, cc_boo return tex; } +static void UpdateTextureBuffer(int context, texbuffer_t *texture, CCTexture* tex) { + PACK_GIFTAG(q, GIF_SET_TAG(1,0,0,0, GIF_FLG_PACKED, 1), GIF_REG_AD); + q++; + + PACK_GIFTAG(q, GS_SET_TEX0(texture->address >> 6, texture->width >> 6, GS_PSM_32, + tex->log2_width, tex->log2_height, TEXTURE_COMPONENTS_RGBA, TEXTURE_FUNCTION_MODULATE, + 0, 0, CLUT_STORAGE_MODE1, 0, CLUT_NO_LOAD), GS_REG_TEX0 + context); + q++; +} + +static int BINDS; void Gfx_BindTexture(GfxResourceID texId) { if (!texId) texId = white_square; CCTexture* tex = (CCTexture*)texId; - return; Platform_Log2("BIND: %i x %i", &tex->width, &tex->height); // TODO + if (BINDS) return; + BINDS = 1; texbuffer_t texbuf; - clutbuffer_t clut; - lod_t lod; - - texbuf.width = tex->width; - texbuf.psm = GS_PSM_32; + texbuf.width = max(256, tex->width); texbuf.address = tex_offset; - lod.calculation = LOD_USE_K; - lod.max_level = 0; - lod.mag_filter = LOD_MAG_NEAREST; - lod.min_filter = LOD_MIN_NEAREST; - lod.l = 0; - lod.k = 0; - - texbuf.info.width = tex->log2_width; - texbuf.info.height = tex->log2_height; - texbuf.info.components = TEXTURE_COMPONENTS_RGBA; - texbuf.info.function = TEXTURE_FUNCTION_MODULATE; - - clut.storage_mode = CLUT_STORAGE_MODE1; - clut.start = 0; - clut.psm = 0; - clut.load_method = CLUT_NO_LOAD; - clut.address = 0; - // TODO terrible perf DMATAG_END(dma_tag, (q - current->data) - 1, 0, 0, 0); dma_wait_fast(); @@ -195,10 +208,7 @@ void Gfx_BindTexture(GfxResourceID texId) { // TODO terrible perf q = dma_tag + 1; - // - - q = draw_texture_sampling(q, 0, &lod); - q = draw_texturebuffer(q, 0, &texbuf, &clut); + UpdateTextureBuffer(0, &texbuf, tex); Platform_LogConst("====="); } @@ -226,6 +236,7 @@ void Gfx_DisableMipmaps(void) { } *------------------------------------------------------State management---------------------------------------------------* *#########################################################################################################################*/ static int clearR, clearG, clearB; +static cc_bool gfx_alphaBlend; static cc_bool gfx_alphaTest; static cc_bool gfx_depthTest; static cc_bool stateDirty; @@ -262,7 +273,8 @@ void Gfx_SetAlphaTest(cc_bool enabled) { } void Gfx_SetAlphaBlending(cc_bool enabled) { - // TODO + gfx_alphaBlend = enabled; + // TODO update primitive state } void Gfx_SetAlphaArgBlend(cc_bool enabled) { } @@ -416,6 +428,7 @@ void Gfx_CalcPerspectiveMatrix(struct Matrix* matrix, float fov, float aspect, f void Gfx_SetVertexFormat(VertexFormat fmt) { gfx_format = fmt; gfx_stride = strideSizes[fmt]; + // TODO update cached primitive state } typedef struct Vector4 { float X, Y, Z, W; } Vector4; @@ -451,6 +464,7 @@ static void DrawTriangle(Vector4 v0, Vector4 v1, Vector4 v2, struct VertexTextur Vector4 verts[3] = { v0, v1, v2 }; struct VertexTextured* v[] = { V0, V1, V2 }; + cc_bool texturing = gfx_format == VERTEX_FORMAT_TEXTURED; //Platform_Log4(" X: %f3, Y: %f3, Z: %f3, W: %f3", &in_vertices[0].x, &in_vertices[0].y, &in_vertices[0].z, &in_vertices[0].w); @@ -458,8 +472,8 @@ static void DrawTriangle(Vector4 v0, Vector4 v1, Vector4 v2, struct VertexTextur PACK_GIFTAG(q, GIF_SET_TAG(1,0,0,0, GIF_FLG_PACKED, 1), GIF_REG_AD); q++; - PACK_GIFTAG(q, GS_SET_PRIM(PRIM_TRIANGLE, PRIM_SHADE_GOURAUD, DRAW_DISABLE, DRAW_DISABLE, - DRAW_DISABLE, DRAW_DISABLE, PRIM_MAP_ST, + PACK_GIFTAG(q, GS_SET_PRIM(PRIM_TRIANGLE, PRIM_SHADE_GOURAUD, texturing, DRAW_DISABLE, + gfx_alphaBlend, DRAW_DISABLE, PRIM_MAP_ST, 0, PRIM_UNFIXED), GS_REG_PRIM); q++; @@ -572,6 +586,7 @@ void Gfx_BeginFrame(void) { } void Gfx_EndFrame(void) { + BINDS = 0; Platform_LogConst("--- EF1 ---"); q = draw_finish(q); Platform_LogConst("--- EF2 ---"); diff --git a/src/SystemFonts.c b/src/SystemFonts.c index 7d396a848..0f0ddffa8 100644 --- a/src/SystemFonts.c +++ b/src/SystemFonts.c @@ -34,7 +34,7 @@ struct IGameComponent SystemFonts_Component = { /*########################################################################################################################* *--------------------------------------------------------Freetype---------------------------------------------------------* *#########################################################################################################################*/ -#if defined CC_BUILD_FREETYPE && !defined CC_BUILD_CONSOLE +#if defined CC_BUILD_FREETYPE #include "freetype/ft2build.h" #include "freetype/freetype.h" #include "freetype/ftmodapi.h"