Cleanup J3D draw buffer

This commit is contained in:
intns 2024-01-01 12:23:48 +00:00
parent c91ce7991b
commit 1435887431
4 changed files with 89 additions and 89 deletions

View File

@ -27,8 +27,7 @@ namespace Game {
struct BaseItem;
struct WayPoint;
struct _BaseItemMgrParent2 {
};
struct _BaseItemMgrParent2 { };
struct BaseItemMgr : public GenericObjectMgr, virtual public _BaseItemMgrParent2 {
BaseItemMgr(int);

View File

@ -78,7 +78,7 @@ struct J3DDrawBuffer {
inline void calcZRatio();
void setNonSort() { mSortType = J3DSORT_NonSort; }
void setZSort() { mSortType = J3DSORT_Z; }
void setZMtx(MtxP mtx) { mZMtx = mtx; }
void setZMtx(MtxP mtx) { mMtxZ = mtx; }
static sortFunc sortFuncTable[6];
static drawFunc drawFuncTable[2];
@ -88,10 +88,10 @@ struct J3DDrawBuffer {
u32 mBufferSize; // _04
EDrawType mDrawType; // _08
ESortType mSortType; // _0C
f32 mZNear; // _10
f32 mZFar; // _14
f32 mZRatio; // _18
MtxP mZMtx; // _1C
f32 mNearZ; // _10
f32 mFarZ; // _14
f32 mRatioZ; // _18
MtxP mMtxZ; // _1C
J3DPacket* mCallBackPacket; // _20
};

View File

@ -9,8 +9,6 @@
#include "sqrt.h"
// TODO: Implement the global namespace Vector3 functions into the class itself
// TODO: Create a getDirection function (dest - src) -> y=0 -> normalise, either externally or internally
template <typename T>
struct Vector3 {
T x, y, z;

View File

@ -16,7 +16,7 @@ drawFunc J3DDrawBuffer::drawFuncTable[2] = {
&J3DDrawBuffer::drawTail,
};
void J3DDrawBuffer::calcZRatio() { mZRatio = (mZFar - mZNear) / (f32)mBufferSize; }
void J3DDrawBuffer::calcZRatio() { mRatioZ = (mFarZ - mNearZ) / (f32)mBufferSize; }
int J3DDrawBuffer::entryNum;
@ -28,9 +28,9 @@ void J3DDrawBuffer::initialize()
{
mDrawType = J3DDRAW_Head;
mSortType = J3DSORT_Mat;
mZNear = 1.0f;
mZFar = 10000.0f;
mZMtx = nullptr;
mNearZ = 1.0f;
mFarZ = 10000.0f;
mMtxZ = nullptr;
mCallBackPacket = nullptr;
mBufferSize = 0x20;
calcZRatio();
@ -40,11 +40,11 @@ void J3DDrawBuffer::initialize()
* @note Address: 0x80065A0C
* @note Size: 0x84
*/
J3DErrType J3DDrawBuffer::allocBuffer(u32 bufSize)
J3DErrType J3DDrawBuffer::allocBuffer(u32 size)
{
mBuffer = new (0x20) J3DPacket*[bufSize];
mBuffer = new (0x20) J3DPacket*[size];
mBufferSize = bufSize;
mBufferSize = size;
frameInit();
calcZRatio();
return JET_Success;
@ -57,8 +57,9 @@ J3DErrType J3DDrawBuffer::allocBuffer(u32 bufSize)
void J3DDrawBuffer::frameInit()
{
u32 bufSize = mBufferSize;
for (u32 i = 0; i < bufSize; i++)
for (u32 i = 0; i < bufSize; i++) {
mBuffer[i] = nullptr;
}
mCallBackPacket = nullptr;
}
@ -67,106 +68,108 @@ void J3DDrawBuffer::frameInit()
* @note Address: 0x80065B7C
* @note Size: 0x158
*/
bool J3DDrawBuffer::entryMatSort(J3DMatPacket* packet)
bool J3DDrawBuffer::entryMatSort(J3DMatPacket* materialPacket)
{
packet->drawClear();
packet->getShapePacket()->drawClear();
materialPacket->drawClear();
materialPacket->getShapePacket()->drawClear();
if (packet->isChanged()) {
packet->setNextPacket(mBuffer[0]);
mBuffer[0] = packet;
if (materialPacket->isChanged()) {
materialPacket->setNextPacket(mBuffer[0]);
mBuffer[0] = materialPacket;
return true;
}
J3DTexture* texture = j3dSys.getTexture();
u32 hash;
u16 texNo = packet->getMaterial()->getTexNo(0);
u16 texNo = materialPacket->getMaterial()->getTexNo(0);
if (texNo == 0xFFFF) {
hash = 0;
} else {
hash = ((u32)texture->getResTIMG(texNo) + texture->getResTIMG(texNo)->mImageDataOffset) >> 5;
}
u32 slot = hash & (mBufferSize - 1);
if (mBuffer[slot] == nullptr) {
mBuffer[slot] = packet;
return true;
} else {
for (J3DMatPacket* pkt = (J3DMatPacket*)mBuffer[slot]; pkt != nullptr; pkt = (J3DMatPacket*)pkt->getNextPacket()) {
if (pkt->isSame(packet)) {
pkt->addShapePacket(packet->getShapePacket());
return false;
}
}
packet->setNextPacket(mBuffer[slot]);
mBuffer[slot] = packet;
mBuffer[slot] = materialPacket;
return true;
}
for (J3DMatPacket* pkt = (J3DMatPacket*)mBuffer[slot]; pkt != nullptr; pkt = (J3DMatPacket*)pkt->getNextPacket()) {
if (pkt->isSame(materialPacket)) {
pkt->addShapePacket(materialPacket->getShapePacket());
return false;
}
}
materialPacket->setNextPacket(mBuffer[slot]);
mBuffer[slot] = materialPacket;
return true;
}
/**
* @note Address: 0x80065CE0
* @note Size: 0x1E8
*/
bool J3DDrawBuffer::entryMatAnmSort(J3DMatPacket* packet)
bool J3DDrawBuffer::entryMatAnmSort(J3DMatPacket* materialPacket)
{
J3DMaterialAnm* pMaterialAnm = packet->mMaterialAnm;
J3DMaterialAnm* pMaterialAnm = materialPacket->mMaterialAnm;
u32 slot = (u32)pMaterialAnm & (mBufferSize - 1);
if (pMaterialAnm == nullptr) {
return entryMatSort(packet);
} else {
packet->drawClear();
packet->getShapePacket()->drawClear();
if (mBuffer[slot] == nullptr) {
mBuffer[slot] = packet;
return true;
} else {
for (J3DMatPacket* pkt = (J3DMatPacket*)mBuffer[slot]; pkt != nullptr; pkt = (J3DMatPacket*)pkt->getNextPacket()) {
if (pkt->mMaterialAnm == pMaterialAnm) {
pkt->addShapePacket(packet->getShapePacket());
return false;
}
}
return entryMatSort(materialPacket);
}
packet->setNextPacket(mBuffer[slot]);
mBuffer[slot] = packet;
return true;
materialPacket->drawClear();
materialPacket->getShapePacket()->drawClear();
if (mBuffer[slot] == nullptr) {
mBuffer[slot] = materialPacket;
return true;
}
for (J3DMatPacket* pkt = (J3DMatPacket*)mBuffer[slot]; pkt != nullptr; pkt = (J3DMatPacket*)pkt->getNextPacket()) {
if (pkt->mMaterialAnm == pMaterialAnm) {
pkt->addShapePacket(materialPacket->getShapePacket());
return false;
}
}
materialPacket->setNextPacket(mBuffer[slot]);
mBuffer[slot] = materialPacket;
return true;
}
/**
* @note Address: 0x80065EC8
* @note Size: 0x11C
*/
bool J3DDrawBuffer::entryZSort(J3DMatPacket* i_packet)
bool J3DDrawBuffer::entryZSort(J3DMatPacket* materialPacket)
{
i_packet->drawClear();
i_packet->getShapePacket()->drawClear();
materialPacket->drawClear();
materialPacket->getShapePacket()->drawClear();
Vec tmp;
tmp.x = mZMtx[0][3];
tmp.y = mZMtx[1][3];
tmp.z = mZMtx[2][3];
Vec zMatrixPosition;
zMatrixPosition.x = mMtxZ[0][3];
zMatrixPosition.y = mMtxZ[1][3];
zMatrixPosition.z = mMtxZ[2][3];
f32 value = -J3DCalcZValue(j3dSys.getViewMtx(), tmp);
f32 zValue = -J3DCalcZValue(j3dSys.getViewMtx(), zMatrixPosition);
u32 uvar4;
if (mZNear + mZRatio < value) {
if (mZFar - mZRatio > value) {
uvar4 = value / mZRatio;
u32 bufferIndex;
if (mNearZ + mRatioZ < zValue) {
if (mFarZ - mRatioZ > zValue) {
bufferIndex = zValue / mRatioZ;
} else {
uvar4 = mBufferSize - 1;
bufferIndex = mBufferSize - 1;
}
} else {
uvar4 = 0;
bufferIndex = 0;
}
u32 idx = (mBufferSize - 1) - uvar4;
i_packet->setNextPacket(mBuffer[(mBufferSize - 1) - uvar4]); // why is this necessary to match smh
mBuffer[idx] = i_packet;
u32 reversedIdx = (mBufferSize - 1) - bufferIndex;
materialPacket->setNextPacket(mBuffer[(mBufferSize - 1) - bufferIndex]);
mBuffer[reversedIdx] = materialPacket;
return true;
}
@ -175,13 +178,13 @@ bool J3DDrawBuffer::entryZSort(J3DMatPacket* i_packet)
* @note Address: 0x80065FE4
* @note Size: 0x50
*/
bool J3DDrawBuffer::entryModelSort(J3DMatPacket* i_packet)
bool J3DDrawBuffer::entryModelSort(J3DMatPacket* materialPacket)
{
i_packet->drawClear();
i_packet->getShapePacket()->drawClear();
materialPacket->drawClear();
materialPacket->getShapePacket()->drawClear();
if (mCallBackPacket != nullptr) {
mCallBackPacket->addChildPacket(i_packet);
if (mCallBackPacket) {
mCallBackPacket->addChildPacket(materialPacket);
return true;
}
@ -192,13 +195,13 @@ bool J3DDrawBuffer::entryModelSort(J3DMatPacket* i_packet)
* @note Address: 0x80066034
* @note Size: 0x54
*/
bool J3DDrawBuffer::entryInvalidSort(J3DMatPacket* i_packet)
bool J3DDrawBuffer::entryInvalidSort(J3DMatPacket* materialPacket)
{
i_packet->drawClear();
i_packet->getShapePacket()->drawClear();
materialPacket->drawClear();
materialPacket->getShapePacket()->drawClear();
if (mCallBackPacket != nullptr) {
mCallBackPacket->addChildPacket(i_packet->getShapePacket());
if (mCallBackPacket) {
mCallBackPacket->addChildPacket(materialPacket->getShapePacket());
return true;
}
@ -209,13 +212,13 @@ bool J3DDrawBuffer::entryInvalidSort(J3DMatPacket* i_packet)
* @note Address: 0x80066088
* @note Size: 0x34
*/
bool J3DDrawBuffer::entryNonSort(J3DMatPacket* i_packet)
bool J3DDrawBuffer::entryNonSort(J3DMatPacket* materialPacket)
{
i_packet->drawClear();
i_packet->mShapePacket->drawClear();
materialPacket->drawClear();
materialPacket->mShapePacket->drawClear();
i_packet->setNextPacket(mBuffer[0]);
mBuffer[0] = i_packet;
materialPacket->setNextPacket(mBuffer[0]);
mBuffer[0] = materialPacket;
return true;
}
@ -240,7 +243,7 @@ void J3DDrawBuffer::drawHead() const
J3DPacket** buf = mBuffer;
for (u32 i = 0; i < size; i++) {
for (J3DPacket* packet = buf[i]; packet != nullptr; packet = packet->getNextPacket()) {
for (J3DPacket* packet = buf[i]; packet; packet = packet->getNextPacket()) {
packet->draw();
}
}
@ -255,7 +258,7 @@ void J3DDrawBuffer::drawTail() const
int num = mBufferSize - 1;
for (int i = num; i >= 0; i--) {
for (J3DPacket* packet = mBuffer[i]; packet != nullptr; packet = packet->getNextPacket()) {
for (J3DPacket* packet = mBuffer[i]; packet; packet = packet->getNextPacket()) {
packet->draw();
}
}