Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implemented automatic mipmap generation.
  • Loading branch information
michael-fadely committed Sep 5, 2015
1 parent c7c2476 commit 7f4db3f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
48 changes: 48 additions & 0 deletions SADXModLoader/AutoMipmap.cpp
@@ -0,0 +1,48 @@
#include "stdafx.h"
#include <d3dx8tex.h>
#include <SADXModLoader.h>
#include "AutoMipmap.h"

IDirect3DTexture8* __fastcall GenerateMipmaps_c(IDirect3DTexture8* d3d_texture, NJS_TEXMEMLIST* njs_texture)
{
if (d3d_texture == nullptr)
return nullptr;

D3DSURFACE_DESC info;
d3d_texture->GetLevelDesc(0, &info);

// TODO: Consider different filtering. By default, it's D3DX_FILTER_BOX | D3DX_FILTER_DITHER
D3DXFilterTexture(d3d_texture, nullptr, D3DX_DEFAULT, D3DX_DEFAULT);

d3d_texture->GetLevelDesc(0, &info);
njs_texture->texinfo.texsurface.pSurface = (Uint32*)d3d_texture;
njs_texture->texinfo.texsurface.TextureSize = info.Size;

return d3d_texture;
}

void* ret_addr = (void*)0x0078CD37;
void __declspec(naked) GenerateMipmaps_asm()
{
// This could probably use some optimizing.
__asm
{
mov edx, esi
mov ecx, eax

push esi
push eax
call GenerateMipmaps_c
pop eax
pop esi

jmp ret_addr
}
}

void ConfigureAutoMipmaps()
{
WriteJump((void*)0x0078CD2A, GenerateMipmaps_asm); // Hooks the end of the function that converts PVRs to D3D textures
WriteData((uint8_t*)0x0078C8B0, (uint8_t)0); // Forces generation of empty mipmap levels for textures that don't normally have them
WriteData((uint8_t*)0x0078C8F6, (uint8_t)0); // Ditto
}
3 changes: 3 additions & 0 deletions SADXModLoader/AutoMipmap.h
@@ -0,0 +1,3 @@
#pragma once

void ConfigureAutoMipmaps();
2 changes: 2 additions & 0 deletions SADXModLoader/SADXModLoader.vcxproj
Expand Up @@ -11,6 +11,7 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="AutoMipmap.cpp" />
<ClCompile Include="CodeParser.cpp" />
<ClCompile Include="dllmain.cpp" />
<ClCompile Include="Events.cpp" />
Expand All @@ -27,6 +28,7 @@
<ClCompile Include="TextureReplacement.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="AutoMipmap.h" />
<ClInclude Include="CodeParser.hpp" />
<ClInclude Include="Events.h" />
<ClInclude Include="FileMap.hpp" />
Expand Down
6 changes: 6 additions & 0 deletions SADXModLoader/SADXModLoader.vcxproj.filters
Expand Up @@ -57,6 +57,9 @@
<ClCompile Include="FixFOV.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="AutoMipmap.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="MediaFns.hpp">
Expand Down Expand Up @@ -113,5 +116,8 @@
<ClInclude Include="FixFOV.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="AutoMipmap.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions SADXModLoader/dllmain.cpp
Expand Up @@ -8,6 +8,7 @@
#include <unordered_map>
#include <vector>
#include <sstream>
#include "AutoMipmap.h"

using std::deque;
using std::ifstream;
Expand Down Expand Up @@ -1868,6 +1869,7 @@ static void __cdecl InitMods(void)
WriteJump((void*)0x004210A0, LoadPVM_C); // Texture packs
WriteJump((void*)0x0077FC80, LoadPVRFile); // Texture packs
WriteJump((void*)0x004228E0, LoadPVR_wrapper); // Texture packs
ConfigureAutoMipmaps();

// Unprotect the .rdata section.
// TODO: Get .rdata address and length dynamically.
Expand Down

0 comments on commit 7f4db3f

Please sign in to comment.