Skip to content

Commit

Permalink
Merge pull request #88 from fjtrujy/poc_CSM2
Browse files Browse the repository at this point in the history
Adding basic`CSM2` CLUT Storage Mode Support
  • Loading branch information
uyjulian authored Dec 20, 2024
2 parents 2bc919a + 8add3e1 commit 87a823f
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ add_object_library_macros(GS_CORE_OBJS ee/gs/src/gsCore.c
gsKit_set_clamp
gsKit_set_primalpha
gsKit_set_texfilter
gsKit_set_texclut
gsKit_set_dither_matrix
gsKit_set_dither
gsKit_set_drawfield
Expand Down Expand Up @@ -351,6 +352,7 @@ if(NOT SKIP_BUILD_EXAMPLES)
bigtex
coverflow
clut
clutcsm
cube
fb
fhdbg
Expand Down
8 changes: 8 additions & 0 deletions ee/gs/include/gsCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
/// Use bilinear filter on texture
#define GS_FILTER_LINEAR 0x01

/// Use clut storage mode CSM1
#define GS_CLUT_STORAGE_CSM1 0x00
/// Use clut storage mode CSM2
#define GS_CLUT_STORAGE_CSM2 0x01

/// Basic X/Y/Z Vertex Structure
struct gsVertex
{
Expand Down Expand Up @@ -209,6 +214,9 @@ void gsKit_set_primalpha(GSGLOBAL *gsGlobal, u64 AlphaMode, u8 PerPixel);
/// Sets the Texture Filtering Parameters
void gsKit_set_texfilter(GSGLOBAL *gsGlobal, u8 FilterMode);

/// Sets the CLUT position specfications
void gsKit_set_texclut(GSGLOBAL *gsGlobal, gs_texclut texClut);

/// Sets the Dither Matrix Setting
void gsKit_set_dither_matrix(GSGLOBAL *gsGlobal);

Expand Down
20 changes: 20 additions & 0 deletions ee/gs/include/gsInit.h
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,7 @@ struct gsTexture
u32 Vram; ///< GS VRAM Memory Pointer
u32 VramClut; ///< GS VRAM CLUT Memory Pointer
u32 Filter; ///< NEAREST or LINEAR
u8 ClutStorageMode; ///< CLUT Storage Mode
u8 Delayed; ///< Delay Texture Upload To VRAM
};
typedef struct gsTexture GSTEXTURE;
Expand Down Expand Up @@ -1039,6 +1040,25 @@ typedef union {
};
} __attribute__((packed)) gs_stq;

typedef union {
u64 position;
struct {
u32 cbw : 6; // Bits 0-5 (6 bits)
u32 cou : 6; // Bits 6-11 (6 bits)
u32 cov : 10; // Bits 12-21 (10 bits)
u32 unused_bits: 10; // Bits 22-31 (10 bits)
u32 unused; // Remaining bits (64 - 22 = 42 bits)
} __attribute__((packed)); // Pack the structure to avoid padding
} __attribute__((packed)) gs_textclut_t;

typedef union {
u128 texclut;
struct {
gs_textclut_t specification;
u64 tag;
};
} __attribute__((packed)) gs_texclut;

/// gsKit Point Primitive Structure
/// This structure holds all relevant data for any
/// given point object, regardless of original format or type.
Expand Down
12 changes: 12 additions & 0 deletions ee/gs/include/gsInline.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,16 @@ static inline gs_stq vertex_to_STQ(float s, float t)
return res;
}

static inline gs_texclut postion_to_TEXCLUT(u8 cbw, u8 cou, u8 cov)
{
gs_texclut res;

res.specification.cbw = cbw;
res.specification.cou = cou;
res.specification.cov = cov;
res.tag = GS_TEXCLUT;

return res;
}

#endif /* __GSINLINE_H__ */
16 changes: 16 additions & 0 deletions ee/gs/src/gsCore.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,22 @@ void gsKit_set_texfilter(GSGLOBAL *gsGlobal, u8 FilterMode)
}
#endif

#if F_gsKit_set_texclut
void gsKit_set_texclut(GSGLOBAL *gsGlobal, gs_texclut texClut)
{
u64 *p_data;
u64 *p_store;

p_data = p_store = gsKit_heap_alloc(gsGlobal, 1, 16, GIF_AD);

*p_data++ = GIF_TAG_AD(1);
*p_data++ = GIF_AD;

memcpy(p_data, &texClut, sizeof(gs_texclut));
p_data += 2; // Advance 2 u64, which is 16 bytes the gs_texclut struct size
}
#endif

#if F_gsKit_set_dither_matrix
void gsKit_set_dither_matrix(GSGLOBAL *gsGlobal)
{
Expand Down
28 changes: 14 additions & 14 deletions ee/gs/src/gsTexture.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ void gsKit_prim_sprite_texture_3d(GSGLOBAL *gsGlobal, const GSTEXTURE *Texture,
{
*p_data++ = GS_SETREG_TEX0(Texture->Vram/256, Texture->TBW, Texture->PSM,
tw, th, gsGlobal->PrimAlphaEnable, 0,
Texture->VramClut/256, Texture->ClutPSM, 0, 0, GS_CLUT_STOREMODE_LOAD);
Texture->VramClut/256, Texture->ClutPSM, Texture->ClutStorageMode, 0, GS_CLUT_STOREMODE_LOAD);
}

*p_data++ = GS_SETREG_PRIM( GS_PRIM_PRIM_SPRITE, 0, 1, gsGlobal->PrimFogEnable,
Expand Down Expand Up @@ -720,7 +720,7 @@ void gskit_prim_list_sprite_texture_uv_3d(GSGLOBAL *gsGlobal, const GSTEXTURE *T
{
*p_data++ = GS_SETREG_TEX0(Texture->Vram/256, Texture->TBW, Texture->PSM,
tw, th, gsGlobal->PrimAlphaEnable, 0,
Texture->VramClut/256, Texture->ClutPSM, 0, 0, GS_CLUT_STOREMODE_LOAD);
Texture->VramClut/256, Texture->ClutPSM, Texture->ClutStorageMode, 0, GS_CLUT_STOREMODE_LOAD);
}
*p_data++ = GS_TEX0_1 + gsGlobal->PrimContext;

Expand Down Expand Up @@ -765,7 +765,7 @@ void gskit_prim_list_sprite_texture_uv_flat(GSGLOBAL *gsGlobal, const GSTEXTURE
{
*p_data++ = GS_SETREG_TEX0(Texture->Vram/256, Texture->TBW, Texture->PSM,
tw, th, gsGlobal->PrimAlphaEnable, 0,
Texture->VramClut/256, Texture->ClutPSM, 0, 0, GS_CLUT_STOREMODE_LOAD);
Texture->VramClut/256, Texture->ClutPSM, Texture->ClutStorageMode, 0, GS_CLUT_STOREMODE_LOAD);
}
*p_data++ = GS_TEX0_1 + gsGlobal->PrimContext;

Expand Down Expand Up @@ -810,7 +810,7 @@ void gskit_prim_list_sprite_texture_uv_flat_color(GSGLOBAL *gsGlobal, const GSTE
{
*p_data++ = GS_SETREG_TEX0(Texture->Vram/256, Texture->TBW, Texture->PSM,
tw, th, gsGlobal->PrimAlphaEnable, 0,
Texture->VramClut/256, Texture->ClutPSM, 0, 0, GS_CLUT_STOREMODE_LOAD);
Texture->VramClut/256, Texture->ClutPSM, Texture->ClutStorageMode, 0, GS_CLUT_STOREMODE_LOAD);
}
*p_data++ = GS_TEX0_1 + gsGlobal->PrimContext;

Expand Down Expand Up @@ -891,7 +891,7 @@ void gsKit_prim_triangle_texture_3d(GSGLOBAL *gsGlobal, GSTEXTURE *Texture,
{
*p_data++ = GS_SETREG_TEX0(Texture->Vram/256, Texture->TBW, Texture->PSM,
tw, th, gsGlobal->PrimAlphaEnable, 0,
Texture->VramClut/256, Texture->ClutPSM, 0, 0, GS_CLUT_STOREMODE_LOAD);
Texture->VramClut/256, Texture->ClutPSM, Texture->ClutStorageMode, 0, GS_CLUT_STOREMODE_LOAD);
}

*p_data++ = GS_SETREG_PRIM( GS_PRIM_PRIM_TRIANGLE, 0, 1, gsGlobal->PrimFogEnable,
Expand Down Expand Up @@ -975,7 +975,7 @@ void gsKit_prim_triangle_goraud_texture_3d(GSGLOBAL *gsGlobal, GSTEXTURE *Textur
{
*p_data++ = GS_SETREG_TEX0(Texture->Vram/256, Texture->TBW, Texture->PSM,
tw, th, gsGlobal->PrimAlphaEnable, 0,
Texture->VramClut/256, Texture->ClutPSM, 0, 0, GS_CLUT_STOREMODE_LOAD);
Texture->VramClut/256, Texture->ClutPSM, Texture->ClutStorageMode, 0, GS_CLUT_STOREMODE_LOAD);
}

*p_data++ = GS_SETREG_PRIM( GS_PRIM_PRIM_TRIANGLE, 1, 1, gsGlobal->PrimFogEnable,
Expand Down Expand Up @@ -1028,7 +1028,7 @@ void gsKit_prim_list_triangle_goraud_texture_uv_3d(GSGLOBAL *gsGlobal, GSTEXTURE
{
*p_data++ = GS_SETREG_TEX0(Texture->Vram/256, Texture->TBW, Texture->PSM,
tw, th, gsGlobal->PrimAlphaEnable, 0,
Texture->VramClut/256, Texture->ClutPSM, 0, 0, GS_CLUT_STOREMODE_LOAD);
Texture->VramClut/256, Texture->ClutPSM, Texture->ClutStorageMode, 0, GS_CLUT_STOREMODE_LOAD);
}
*p_data++ = GS_TEX0_1 + gsGlobal->PrimContext;

Expand Down Expand Up @@ -1073,7 +1073,7 @@ void gsKit_prim_list_triangle_goraud_texture_stq_3d(GSGLOBAL *gsGlobal, GSTEXTUR
{
*p_data++ = GS_SETREG_TEX0(Texture->Vram/256, Texture->TBW, Texture->PSM,
tw, th, gsGlobal->PrimAlphaEnable, 0,
Texture->VramClut/256, Texture->ClutPSM, 0, 0, GS_CLUT_STOREMODE_LOAD);
Texture->VramClut/256, Texture->ClutPSM, Texture->ClutStorageMode, 0, GS_CLUT_STOREMODE_LOAD);
}
*p_data++ = GS_TEX0_1 + gsGlobal->PrimContext;

Expand Down Expand Up @@ -1127,7 +1127,7 @@ void gsKit_prim_triangle_strip_texture(GSGLOBAL *gsGlobal, GSTEXTURE *Texture,
{
*p_data++ = GS_SETREG_TEX0(Texture->Vram/256, Texture->TBW, Texture->PSM,
tw, th, gsGlobal->PrimAlphaEnable, 0,
Texture->VramClut/256, Texture->ClutPSM, 0, 0, GS_CLUT_STOREMODE_LOAD);
Texture->VramClut/256, Texture->ClutPSM, Texture->ClutStorageMode, 0, GS_CLUT_STOREMODE_LOAD);
}
*p_data++ = GS_TEX0_1+gsGlobal->PrimContext;

Expand Down Expand Up @@ -1192,7 +1192,7 @@ void gsKit_prim_triangle_strip_texture_3d(GSGLOBAL *gsGlobal, GSTEXTURE *Texture
{
*p_data++ = GS_SETREG_TEX0(Texture->Vram/256, Texture->TBW, Texture->PSM,
tw, th, gsGlobal->PrimAlphaEnable, 0,
Texture->VramClut/256, Texture->ClutPSM, 0, 0, GS_CLUT_STOREMODE_LOAD);
Texture->VramClut/256, Texture->ClutPSM, Texture->ClutStorageMode, 0, GS_CLUT_STOREMODE_LOAD);
}
*p_data++ = GS_TEX0_1+gsGlobal->PrimContext;

Expand Down Expand Up @@ -1256,7 +1256,7 @@ void gsKit_prim_triangle_fan_texture(GSGLOBAL *gsGlobal, GSTEXTURE *Texture,
{
*p_data++ = GS_SETREG_TEX0(Texture->Vram/256, Texture->TBW, Texture->PSM,
tw, th, gsGlobal->PrimAlphaEnable, 0,
Texture->VramClut/256, Texture->ClutPSM, 0, 0, GS_CLUT_STOREMODE_LOAD);
Texture->VramClut/256, Texture->ClutPSM, Texture->ClutStorageMode, 0, GS_CLUT_STOREMODE_LOAD);
}
*p_data++ = GS_TEX0_1+gsGlobal->PrimContext;

Expand Down Expand Up @@ -1320,7 +1320,7 @@ void gsKit_prim_triangle_fan_texture_3d(GSGLOBAL *gsGlobal, GSTEXTURE *Texture,
{
*p_data++ = GS_SETREG_TEX0(Texture->Vram/256, Texture->TBW, Texture->PSM,
tw, th, gsGlobal->PrimAlphaEnable, 0,
Texture->VramClut/256, Texture->ClutPSM, 0, 0, GS_CLUT_STOREMODE_LOAD);
Texture->VramClut/256, Texture->ClutPSM, Texture->ClutStorageMode, 0, GS_CLUT_STOREMODE_LOAD);
}
*p_data++ = GS_TEX0_1+gsGlobal->PrimContext;

Expand Down Expand Up @@ -1416,7 +1416,7 @@ void gsKit_prim_quad_texture_3d(GSGLOBAL *gsGlobal, GSTEXTURE *Texture,
{
*p_data++ = GS_SETREG_TEX0(Texture->Vram/256, Texture->TBW, Texture->PSM,
tw, th, gsGlobal->PrimAlphaEnable, 0,
Texture->VramClut/256, Texture->ClutPSM, 0, 0, GS_CLUT_STOREMODE_LOAD);
Texture->VramClut/256, Texture->ClutPSM, Texture->ClutStorageMode, 0, GS_CLUT_STOREMODE_LOAD);
}

*p_data++ = GS_SETREG_PRIM( GS_PRIM_PRIM_TRISTRIP, 0, 1, gsGlobal->PrimFogEnable,
Expand Down Expand Up @@ -1511,7 +1511,7 @@ void gsKit_prim_quad_goraud_texture_3d(GSGLOBAL *gsGlobal, GSTEXTURE *Texture,
{
*p_data++ = GS_SETREG_TEX0(Texture->Vram/256, Texture->TBW, Texture->PSM,
tw, th, gsGlobal->PrimAlphaEnable, 0,
Texture->VramClut/256, Texture->ClutPSM, 0, 0, GS_CLUT_STOREMODE_LOAD);
Texture->VramClut/256, Texture->ClutPSM, Texture->ClutStorageMode, 0, GS_CLUT_STOREMODE_LOAD);
}

*p_data++ = GS_SETREG_PRIM( GS_PRIM_PRIM_TRISTRIP, 1, 1, gsGlobal->PrimFogEnable,
Expand Down
Loading

0 comments on commit 87a823f

Please sign in to comment.