diff --git a/include/c3d/texture.h b/include/c3d/texture.h index 9db4830..bd4647a 100644 --- a/include/c3d/texture.h +++ b/include/c3d/texture.h @@ -58,6 +58,8 @@ void C3D_TexBind(int unitId, C3D_Tex* tex); void C3D_TexFlush(C3D_Tex* tex); void C3D_TexDelete(C3D_Tex* tex); +void C3D_TexShadowParams(bool perspective, float bias); + static inline int C3D_TexCalcMaxLevel(u32 width, u32 height) { return (31-__builtin_clz(width < height ? width : height)) - 3; // avoid sizes smaller than 8 @@ -113,6 +115,18 @@ static inline bool C3D_TexInitVRAM(C3D_Tex* tex, u16 width, u16 height, GPU_TEXC (C3D_TexInitParams){ width, height, 0, format, GPU_TEX_2D, true }); } +static inline bool C3D_TexInitShadow(C3D_Tex* tex, u16 width, u16 height) +{ + return C3D_TexInitWithParams(tex, NULL, + (C3D_TexInitParams){ width, height, 0, GPU_RGBA8, GPU_TEX_SHADOW_2D, true }); +} + +static inline bool C3D_TexInitShadowCube(C3D_Tex* tex, C3D_TexCube* cube, u16 width, u16 height) +{ + return C3D_TexInitWithParams(tex, cube, + (C3D_TexInitParams){ width, height, 0, GPU_RGBA8, GPU_TEX_SHADOW_CUBE, true }); +} + static inline GPU_TEXTURE_MODE_PARAM C3D_TexGetType(C3D_Tex* tex) { return (GPU_TEXTURE_MODE_PARAM)((tex->param>>28)&0x7); diff --git a/source/base.c b/source/base.c index ced26a9..5eebb9f 100644 --- a/source/base.c +++ b/source/base.c @@ -71,6 +71,7 @@ bool C3D_Init(size_t cmdBufSize) C3D_FragOpShadow(0.0, 1.0); ctx->texConfig = BIT(12); + ctx->texShadow = BIT(0); ctx->texEnvBuf = 0; ctx->texEnvBufClr = 0xFFFFFFFF; @@ -188,6 +189,7 @@ void C3Di_UpdateContext(void) { ctx->flags &= ~C3DiF_TexStatus; GPUCMD_AddWrite(GPUREG_TEXUNIT_CONFIG, ctx->texConfig); + GPUCMD_AddWrite(GPUREG_TEXUNIT0_SHADOW, ctx->texShadow); ctx->texConfig &= ~BIT(16); // Remove clear-texture-cache flag } diff --git a/source/internal.h b/source/internal.h index 475cd90..b1ffefa 100644 --- a/source/internal.h +++ b/source/internal.h @@ -39,6 +39,7 @@ typedef struct C3D_LightEnv* lightEnv; u32 texConfig; + u32 texShadow; C3D_Tex* tex[3]; C3D_TexEnv texEnv[6]; diff --git a/source/texture.c b/source/texture.c index 89af640..242df99 100644 --- a/source/texture.c +++ b/source/texture.c @@ -255,6 +255,20 @@ void C3D_TexDelete(C3D_Tex* tex) tex->data = NULL; } +void C3D_TexShadowParams(bool perspective, float bias) +{ + C3D_Context* ctx = C3Di_GetContext(); + + if (!(ctx->flags & C3DiF_Active)) + return; + + u32 iBias = (u32)(fabs(bias) * BIT(24)); + if (iBias >= BIT(24)) + iBias = BIT(24)-1; + + ctx->texShadow = (iBias &~ 1) | (perspective ? 0 : 1); +} + void C3Di_SetTex(int unit, C3D_Tex* tex) { u32 reg[10];