DolphinCTexture: Near complete RE, usable as-is!

This commit is contained in:
Phillip Stephens 2022-03-02 23:51:11 -08:00
parent 0400388fa6
commit 7010c36056
Signed by: Antidote
GPG Key ID: F8BEE4C83DACA60D
9 changed files with 208 additions and 39 deletions

View File

@ -2490,7 +2490,12 @@ void CStateManager::ClearGraveyard() {
x854_objectGraveyard.clear();
}
void CStateManager::FrameBegin(s32 frameCount) { x8d4_inputFrameIdx = frameCount; }
void CStateManager::FrameBegin(s32 frameCount) {
x8d4_inputFrameIdx = frameCount;
CTexture::SetCurrentFrameCount(frameCount);
CGraphicsPalette::SetCurrentFrameCount(frameCount);
//SwapOutTexturesToARAM(2, 0x180000);
}
void CStateManager::InitializeState(CAssetId mlvlId, TAreaId aid, CAssetId mreaId) {
const bool hadRandom = x900_activeRandom != nullptr;

View File

@ -19,4 +19,9 @@ CGraphicsPalette::CGraphicsPalette(CInputStream& in) : x0_fmt(EPaletteFormat(in.
//DCFlushRange(xc_entries.get(), x8_entryCount * 2);
}
void CGraphicsPalette::Load() {
//GXLoadTlut(x10_tlutObj, 0);
x4_frameLoaded = sCurrentFrameCount;
}
} // namespace metaforce

View File

@ -26,6 +26,9 @@ public:
explicit CGraphicsPalette(EPaletteFormat fmt, int count);
explicit CGraphicsPalette(CInputStream& in);
void Load();
static void SetCurrentFrameCount(u32 frameCount) { sCurrentFrameCount = frameCount; }
};
} // namespace metaforce

View File

@ -107,6 +107,7 @@ public:
[[nodiscard]] const CTextureInfo* GetTextureInfo() const { return m_textureInfo; }
static u32 TexelFormatBitsPerPixel(ETexelFormat fmt);
static void SetCurrentFrameCount(u32 frameCount) { sCurrentFrameCount = frameCount; }
};
CFactoryFnReturn FTextureFactory(const metaforce::SObjectTag& tag, std::unique_ptr<u8[]>&& in, u32 len,

View File

@ -1,4 +1,7 @@
#include "DolphinCTexture.hpp"
#include "Runtime/Graphics/DolphinCTexture.hpp"
#include "Runtime/CToken.hpp"
#include "zeus/Math.hpp"
u32 GXGetTexBufferSize(u16 width, u16 height, u32 format, bool mipmap, u8 max_lod) {
s32 shiftX = 0;
@ -69,6 +72,10 @@ u32 GXGetTexBufferSize(u16 width, u16 height, u32 format, bool mipmap, u8 max_lo
}
namespace metaforce::WIP {
namespace {
static std::array<CTexture*, GX::MAX_TEXMAP> sLoadedTextures{};
}
CTexture::CTexture(ETexelFormat fmt, s16 w, s16 h, s32 mips)
: x0_fmt(fmt)
, x4_w(w)
@ -90,14 +97,14 @@ CTexture::CTexture(CInputStream& in, EAutoMipmap automip, EBlackKey blackKey) {
bool hasPalette = (x0_fmt == ETexelFormat::C4 || x0_fmt == ETexelFormat::C8 || x0_fmt == ETexelFormat::C14X2);
if (hasPalette) {
x10_graphicsPalette = std::make_unique<CGraphicsPalette>(in);
xa_25_hasPalette = true;
xa_25_canLoadPalette = true;
}
x9_bitsPerPixel = TexelFormatBitsPerPixel(x0_fmt);
InitBitmapBuffers(x0_fmt, x4_w, x6_h, x8_mips);
u32 bufLen = 0;
if (x8_mips > 0) {
for (u32 i = 0; i < x8_mips; ++i) {
u32 curMip = i & 3;
u32 curMip = i & 63;
const u32 width = ROUND_UP_4(x4_w >> curMip);
const u32 height = ROUND_UP_4(x6_h >> curMip);
bufLen += (width * height * x9_bitsPerPixel) / 8;
@ -110,8 +117,9 @@ CTexture::CTexture(CInputStream& in, EAutoMipmap automip, EBlackKey blackKey) {
len = 256;
}
in.Get(x44_aramToken_x4_buff.get() + i, len);
//DCFlushRangeNoSync(x44_aramToken_x4_buff.get() + i, ROUND_UP_32(len));
auto image_ptr = /*x44_aramToken.GetMRAMSafe() */ x44_aramToken_x4_buff.get();
in.Get(image_ptr + i, len);
// DCFlushRangeNoSync(x44_aramToken_x4_buff.get() + i, ROUND_UP_32(len));
}
if (sMangleMips) {
@ -123,6 +131,83 @@ CTexture::CTexture(CInputStream& in, EAutoMipmap automip, EBlackKey blackKey) {
InitTextureObjs();
}
void* CTexture::Lock() {
xa_24_locked = true;
return GetBitMapData(0);
}
void CTexture::UnLock() {
xa_24_locked = false;
CountMemory();
// DCFlushRange(x44_aramToken.GetMRAMSafe(), ROUND_UP_32(xc_memoryAllocated));
}
void CTexture::Load(GX::TexMapID id, CTexture::EClampMode clamp) {
if (sLoadedTextures[id] != this || xa_29_canLoadObj) {
auto* image_ptr = /*x44_aramToken.GetMRAMSafe() */ x44_aramToken_x4_buff.get();
CountMemory();
if (HasPalette()) {
x10_graphicsPalette->Load();
xa_25_canLoadPalette = false;
}
xa_29_canLoadObj = false;
if (x40_clampMode != clamp) {
x40_clampMode = !xa_26_isPowerOfTwo ? EClampMode::Clamp : clamp;
// GXInitTexObjWrapMode(x20_texObj, static_cast<u32>(x40_clampMode), static_cast<u32>(x40_clampMode));
}
// GXInitObjectData(x20_texObj, image_ptr);
// GXLoadObj(x20_texObj, id);
sLoadedTextures[id] = this;
x64_frameAllocated = sCurrentFrameCount;
}
}
void CTexture::LoadMipLevel(s32 mip, GX::TexMapID id, CTexture::EClampMode clamp) {
auto image_ptr = /*x44_aramToken.GetMRAMSafe() */ x44_aramToken_x4_buff.get();
u32 width = x4_w;
u32 height = x6_h;
u32 iVar15 = 0;
u32 offset = 0;
if (mip > 0) {
for (u32 i = 0; i < mip; ++i) {
offset += ROUND_UP_32(x9_bitsPerPixel * (ROUND_UP_4(width) * ROUND_UP_4(height)));
width /= 2;
height /= 2;
}
}
// GXTexObj texObj;
// GXInitTexObj(&texObj, image_ptr + offset, width, height, x18_gxFormat);
// GXInitTexObjLod(&texObj, GX_LINEAR, GX_LINEAR, 0.f, 1.f, 0.f, false, false, GX_ANISO_1);
if (HasPalette()) {
x10_graphicsPalette->Load();
xa_25_canLoadPalette = false;
}
// GXLoadTexObj(&texObj, mapId);
x64_frameAllocated = sCurrentFrameCount;
sLoadedTextures[id] = nullptr;
}
void CTexture::MakeSwappable() {
if (!xa_27_noSwap) {
return;
}
xa_27_noSwap = false;
}
const void* CTexture::GetConstBitMapData(s32 mip) const {
u32 buffOffset = 0;
if (x8_mips > 0) {
for (u32 i = 0; i < x8_mips; ++i) {
buffOffset += (x9_bitsPerPixel >> 3) * (x4_w >> (i & 0x3f)) * (x6_h >> (i & 0x3f));
}
}
return x44_aramToken_x4_buff.get() + buffOffset; /* x44_aramToken.GetMRAMSafe() + buffOffset*/
}
void* CTexture::GetBitMapData(s32 mip) const { return const_cast<void*>(GetConstBitMapData(mip)); }
void CTexture::InitBitmapBuffers(ETexelFormat fmt, s16 width, s16 height, s32 mips) {
switch (fmt) {
case ETexelFormat::I4:
@ -166,13 +251,31 @@ void CTexture::InitBitmapBuffers(ETexelFormat fmt, s16 width, s16 height, s32 mi
? x1c_gxCIFormat
: x18_gxFormat;
/* I have no idea what they're doing with that last argument... */
xc_memoryAllocated = GXGetTexBufferSize(width, height, format, mips > 1, static_cast<u8>(mips > 1) & 11);
xc_memoryAllocated = GXGetTexBufferSize(width, height, format, mips > 1, mips > 1 ? 11 : 0);
x44_aramToken_x4_buff.reset(new u8[xc_memoryAllocated]);
/*x44_aramToken.PostConstruct(buf, xc_memoryAllocated, 1);*/
CountMemory();
}
void CTexture::InitTextureObjs() {}
void CTexture::InitTextureObjs() {
xa_26_isPowerOfTwo = zeus::floorPowerOfTwo(x4_w) == x4_w && zeus::floorPowerOfTwo(x6_h) == x6_h;
if (!xa_26_isPowerOfTwo) {
x40_clampMode = EClampMode::Clamp;
}
CountMemory();
if (IsCITexture()) {
// GXInitTexObjCI(x20_texObj, x44_aramToken_x4_buff.get(), x4_w, x6_h, x1c_gxCIFormat, u32(x40_clampMode),
// u32(x40_clampMode), x8_mips > 1, 0);
} else {
// GXInitTexObj(x20_texObj, x44_aramToken_x4_buff.get(), x4_w, x6_h, x1c_gxCIFormat, u32(x40_clampMode),
// u32(x40_clampMode), x8_mips > 1);
// GXInitTexObjLOD(x20_texObj, x8_mips > 1 ? GX_LIN_MIP_LIN : GX_LINEAR, 0.f, static_cast<float>(x8_mips) - 1.f,
// 0.f,
// false, false, x8_mips > 1 ? GX_ANISO_4 : GX_ANISO_1);
}
xa_29_canLoadObj = true;
}
void CTexture::CountMemory() {
if (xa_28_counted) {
@ -183,6 +286,15 @@ void CTexture::CountMemory() {
sTotalAllocatedMemory += xc_memoryAllocated;
}
void CTexture::UncountMemory() {
if (!xa_28_counted) {
return;
}
xa_28_counted = false;
sTotalAllocatedMemory -= xc_memoryAllocated;
}
void CTexture::MangleMipmap(u32 mip) {
// TODO(phil): Mangle mipmap
}
@ -209,6 +321,12 @@ u32 CTexture::TexelFormatBitsPerPixel(ETexelFormat fmt) {
}
}
bool CTexture::sMangleMips = false;
u32 CTexture::sCurrentFrameCount = 0;
u32 CTexture::sTotalAllocatedMemory = 0;
CFactoryFnReturn FTextureFactory(const SObjectTag& tag, CInputStream& in, const CVParamTransfer& vparms,
CObjectReference* selfRef) {
return TToken<CTexture>::GetIObjObjectFor(std::make_unique<CTexture>(in));
}
} // namespace metaforce::WIP

View File

@ -1,9 +1,11 @@
#pragma once
#include "Runtime/CFactoryMgr.hpp"
#include "Runtime/Graphics/CGraphics.hpp"
#include "Runtime/Graphics/CGraphicsPalette.hpp"
#include "Runtime/IObj.hpp"
#include "Runtime/Streams/CInputStream.hpp"
#include "Runtime/Graphics/GX.hpp"
namespace metaforce::WIP {
class CTexture {
@ -16,6 +18,7 @@ class CTexture {
int x14_;
void* x18_;
};
public:
enum class EClampMode {
Clamp,
@ -28,13 +31,10 @@ public:
One,
};
enum class EBlackKey {
Zero,
One
};
enum class EBlackKey { Zero, One };
private:
static constexpr bool sMangleMips = false;
static bool sMangleMips;
static u32 sCurrentFrameCount;
static u32 sTotalAllocatedMemory;
ETexelFormat x0_fmt = ETexelFormat::Invalid;
@ -42,12 +42,12 @@ private:
u16 x6_h = 0;
u8 x8_mips = 0;
u8 x9_bitsPerPixel = 0;
bool xa_24_ : 1 = false;
bool xa_25_hasPalette : 1 = false;
bool xa_26_ : 1 = false;
bool xa_27_ : 1 = true;
bool xa_24_locked : 1 = false;
bool xa_25_canLoadPalette : 1 = false;
bool xa_26_isPowerOfTwo : 1 = false;
bool xa_27_noSwap : 1 = true;
bool xa_28_counted : 1 = false;
bool xa_29_ : 1 = false;
bool xa_29_canLoadObj : 1 = false;
u32 xc_memoryAllocated = 0;
std::unique_ptr<CGraphicsPalette> x10_graphicsPalette;
std::unique_ptr<CDumpedBitmapDataReloader> x14_bitmapReloader;
@ -62,14 +62,43 @@ private:
void InitBitmapBuffers(ETexelFormat fmt, s16 width, s16 height, s32 mips);
void InitTextureObjs();
void CountMemory();
void UncountMemory();
void MangleMipmap(u32 mip);
static u32 TexelFormatBitsPerPixel(ETexelFormat fmt);
public:
CTexture(ETexelFormat, s16, s16, s32);
CTexture(CInputStream& in, EAutoMipmap automip = EAutoMipmap::Zero, EBlackKey blackKey = EBlackKey::Zero);
const void* GetConstBitMapData(s32 mip) const { /* TODO: get bitmap data for specified mipmap */ return nullptr; }
void* GetBitMapData(s32 mip) const { return const_cast<void*>(GetConstBitMapData(mip)); }
};
} // namespace metaforce::WIP
[[nodiscard]] ETexelFormat GetTextureFormat() const { return x0_fmt; }
[[nodiscard]] s16 GetWidth(s32 mip) const { return x4_w; }
[[nodiscard]] s16 GetHeight(s32 mip) const { return x6_h; }
[[nodiscard]] u8 GetNumberOfMipMaps() const { return x8_mips; }
[[nodiscard]] u32 GetBitDepth() const { return x9_bitsPerPixel; }
[[nodiscard]] u32 GetMemoryAllocated() const { return xc_memoryAllocated; }
[[nodiscard]] const std::unique_ptr<CGraphicsPalette>& GetPalette() const { return x10_graphicsPalette; }
[[nodiscard]] bool HasPalette() const { return x10_graphicsPalette != nullptr; }
[[nodiscard]] void* Lock();
void UnLock();
void Load(GX::TexMapID id, EClampMode clamp);
void LoadMipLevel(s32 mip, GX::TexMapID id, EClampMode clamp);
// void UnloadBitmapData(u32) const;
// void TryReloadBitmapData(CResFactory&) const;
// void LoadToMRAM() const;
// void LoadToARAM() const;
// bool IsARAMTransferInProgress() const { return false; }
void MakeSwappable();
[[nodiscard]] const void* GetConstBitMapData(s32 mip) const;
[[nodiscard]] void* GetBitMapData(s32 mip) const;
[[nodiscard]] bool IsCITexture() const {
return x0_fmt == ETexelFormat::C4 || x0_fmt == ETexelFormat::C8 || x0_fmt == ETexelFormat::C14X2;
}
static void SetMangleMips(bool b) { sMangleMips = b; }
static void SetCurrentFrameCount(u32 frameCount) { sCurrentFrameCount = frameCount; }
};
CFactoryFnReturn FTextureFactory(const SObjectTag& tag, CInputStream& in, const CVParamTransfer& vparms,
CObjectReference* selfRef);
} // namespace metaforce::WIP

View File

@ -275,6 +275,17 @@ enum TextureFormat : uint32_t {
CTF_Z16L = 0x3c,
};
enum TexMapID {
TEXMAP0,
TEXMAP2,
TEXMAP3,
TEXMAP4,
TEXMAP5,
TEXMAP6,
TEXMAP7,
MAX_TEXMAP,
};
struct Color {
union {
uint8_t color[4];

View File

@ -31,29 +31,27 @@ bool CInputStream::InternalReadNext() {
bool CInputStream::GrabAnotherBlock() { return InternalReadNext(); }
void CInputStream::Get(u8* dest, u32 len) {
s32 readCount = 0;
x20_bitOffset = 0;
u32 readCount = 0;
while (len != 0) {
u32 blockLen = x8_blockLen - x4_blockOffset;
s32 blockLen = x8_blockLen - x4_blockOffset;
if (len < blockLen) {
blockLen = len;
}
if (blockLen == 0) {
if (len <= 256) {
GrabAnotherBlock();
} else {
u32 readLen = Read(dest + readCount, len);
len -= readLen;
readCount += readLen;
}
} else {
if (blockLen != 0) {
memcpy(dest + readCount, x10_ptr + x4_blockOffset, blockLen);
len -= blockLen;
readCount += blockLen;
x4_blockOffset += blockLen;
} else if (len > 256) {
u32 readLen = Read(dest + readCount, len);
len -= readLen;
readCount += readLen;
} else {
GrabAnotherBlock();
};
}
}
x18_readPosition += readCount;
}
@ -89,7 +87,6 @@ u32 CInputStream::ReadBytes(void* dest, u32 len) {
return curReadLen;
}
u32 CInputStream::ReadBits(u32 bitCount) {
u32 ret = 0;
const s32 shiftAmt = x20_bitOffset - s32(bitCount);

2
extern/zeus vendored

@ -1 +1 @@
Subproject commit e53b380f4298364c8f389c562415e2c093423b56
Subproject commit 82a3a0def9cd31efe9b28ee9332cb2fcf1d27173