Skip to content

Commit

Permalink
Implemented mipmap generation for palletized textures, but disabled b…
Browse files Browse the repository at this point in the history
…y default.
  • Loading branch information
michael-fadely committed Oct 11, 2015
1 parent c04f76e commit b29eb29
Showing 1 changed file with 32 additions and 53 deletions.
85 changes: 32 additions & 53 deletions SADXModLoader/AutoMipmap.cpp
Expand Up @@ -31,30 +31,34 @@ static HRESULT CopyTexture(IDirect3DTexture8* dest, IDirect3DTexture8* src, UINT
return result;
}

inline void SetSurface(IDirect3DTexture8* d3d_texture, NJS_TEXSURFACE* texsurface)
inline void SetSurface(IDirect3DTexture8* src, NJS_TEXSURFACE* surface)
{
D3DSURFACE_DESC info;
d3d_texture->GetLevelDesc(0, &info);
src->GetLevelDesc(0, &info);

texsurface->pSurface = (Uint32*)d3d_texture;
texsurface->TextureSize = info.Size;
surface->pSurface = (Uint32*)src;
surface->TextureSize = info.Size;
}

/// <summary>
/// Generates mipmaps if the specified texture has the levels to accommodate them.
/// </summary>
/// <param name="d3d_texture">The DirectX texture to apply mipmaps to.</param>
/// <param name="src">The DirectX texture to apply mipmaps to.</param>
/// <param name="njs_texture">The Dremcast texture to receive the DirectX texture.</param>
static void __fastcall GenerateMipmaps_c(IDirect3DTexture8* src, NJS_TEXMEMLIST* njs_texture)
{
if (src == nullptr || njs_texture == nullptr)
return;

// TODO: Figure out how Chao palettized textures are handled.
#ifndef PALLETIZED_MIPMAPS
Uint32 format = njs_texture->texinfo.texsurface.PixelFormat;

if (format == NJD_PIXELFORMAT_PALETTIZED_4BPP || format == NJD_PIXELFORMAT_PALETTIZED_8BPP || src->GetLevelCount() > 1)
goto ABORT;
#endif

if (src->GetLevelCount() > 1)
goto ABORT;

HRESULT result;

Expand Down Expand Up @@ -89,33 +93,6 @@ static void __fastcall GenerateMipmaps_c(IDirect3DTexture8* src, NJS_TEXMEMLIST*
return;
}

/*
/// <summary>
/// Disables mipmaps for palettized textures as a work around.
/// </summary>
/// <param name="format">The texture's pixel format.</param>
static void __stdcall EnableMipmaps(uint32_t format)
{
if (format == NJD_PIXELFORMAT_PALETTIZED_4BPP || format == NJD_PIXELFORMAT_PALETTIZED_8BPP)
{
if (mipmapsEnabled)
{
// Default behavior
WriteData((uint8_t*)0x0078C8B0, (uint8_t)1);
WriteData((uint8_t*)0x0078C8F6, (uint8_t)1);
mipmapsEnabled = false;
}
}
else if (!mipmapsEnabled)
{
// Forces generation of empty mipmap levels for textures that don't normally have them
WriteData((uint8_t*)0x0078C8B0, (uint8_t)0);
WriteData((uint8_t*)0x0078C8F6, (uint8_t)0);
mipmapsEnabled = true;
}
}
*/

#pragma region Assembly

static void* GenerateMipmaps_return = (void*)0x0078CD37;
Expand All @@ -135,33 +112,35 @@ static void __declspec(naked) GenerateMipmaps_asm()
}
}

/*
static void* EnableMipmaps_return = (void*)0x0078C889;
static void __declspec(naked) EnableMipmaps_asm()
#pragma endregion

#ifdef PALLETIZED_MIPMAPS
void __cdecl GeneratePalletizedMipmaps_c(IDirect3DTexture8* src)
{
if (src->GetLevelCount() >= 2)
D3DXFilterTexture(src, nullptr, 0, D3DX_DEFAULT);
}

void* loc_78CE10 = (void*)0x0078CE10;
void __declspec(naked) GeneratePalletizedMipmaps_asm()
{
__asm
{
push eax
push ecx
push [esp + 16]
call EnableMipmaps
pop ecx
pop eax
// Stuff overwritten by the jump to this code
sub esp, 14h
push ebx
push ebp
mov ebp, [esp+1Ch+0Ch]
jmp EnableMipmaps_return
push edi
call test_c
pop edi
jmp loc_78CE10
}
}
*/

#pragma endregion
#endif

void EnableAutoMipmaps()
{
WriteJump((void*)0x0078CD2A, GenerateMipmaps_asm); // Hooks the end of the function that converts PVRs to D3D textures
//WriteJump((void*)0x0078C880, EnableMipmaps_asm); // Toggles mipmaps off for palettized textures, on otherwise.
#ifdef PALLETIZED_MIPMAPS
// This happens every frame for every palletized texture in the scene. Rather inefficient where mipmap generation is concerned.
// Assuming somebody can figure out a method of keeping track of palette changes and textures who's mips have already been generated,
// we can enable this for mainstream builds. Otherwise it should stay off.
WriteJump((void*)0x0078CF06, GeneratePalletizedMipmaps_asm);
#endif
}

0 comments on commit b29eb29

Please sign in to comment.