librw/tests/ps2/main.cpp

285 lines
6.8 KiB
C++
Raw Normal View History

2015-01-26 19:23:45 +00:00
#include <cstdio>
#include <cstring>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cassert>
2015-01-18 18:35:23 +00:00
#include <rw.h>
#include <src/gtaplg.h>
2015-01-18 18:35:23 +00:00
#include "ps2.h"
#include "dma.h"
#include "gif.h"
#include "gs.h"
#include "math.h"
using namespace std;
Matrix projMat, viewMat, worldMat;
2015-01-18 18:35:23 +00:00
extern uint32 MyDmaPacket[];
extern Matrix vuMat;
extern Matrix vuLightMat;
2015-01-18 18:35:23 +00:00
extern float vuOffset[];
extern uint64 vuGIFtag[];
extern float vuMatcolor[];
extern float vuSurfProps[];
extern uint32 vuGeometry[];
extern uint32 mpgCall[];
2015-07-11 13:45:38 +01:00
extern uint32 textureCall[];
2015-01-18 18:35:23 +00:00
extern uint32 geometryCall[];
extern uint32 defaultPipe[];
extern uint32 skinPipe[];
rw::Clump *clump;
2015-01-18 18:35:23 +00:00
2015-07-11 13:45:38 +01:00
uint64 __attribute__((aligned (16))) rasterPacket[16];
2015-07-11 13:45:38 +01:00
void
uploadRaster(rw::Raster *ras)
{
GsState *g = gsCurState;
2015-07-11 13:45:38 +01:00
uint64 *dp;
uint32 *wp;
uint32 destAddr = g->currentMemPtr;
uint32 size = ras->width*ras->height*4;
g->currentMemPtr += size;
2015-07-11 13:45:38 +01:00
// set up transfer
wp = (uint32*)rasterPacket;
*wp++ = 0x10000000 | 6; // DMAcnt; 6 qw
*wp++ = 0;
*wp++ = 0;
*wp++ = 0x50000000 | 6; // DIRECT; 6 qw
dp = (uint64*)wp;
*dp++ = MAKE_GIF_TAG(4, 1, 0, 0, 0, 1);
*dp++ = 0x0e;
*dp++ = MAKE_GS_BITBLTBUF(0,0,0,destAddr/4/64, ras->width/64, PSMCT32);
*dp++ = GS_BITBLTBUF;
*dp++ = MAKE_GS_TRXPOS(0, 0, 0, 0, 0);
*dp++ = GS_TRXPOS;
*dp++ = MAKE_GS_TRXREG(ras->width, ras->height);
*dp++ = GS_TRXREG;
*dp++ = 0;
*dp++ = GS_TRXDIR;
*dp++ = MAKE_GIF_TAG(size/0x10, 1, 0, 0, 2, 0);
*dp++ = 0;
// the data
wp = (uint32*)dp;
*wp++ = 0x30000000 | size/0x10; // DMAref
*wp++ = (uint32)ras->texels;
*wp++ = 0;
*wp++ = 0x50000000 | size/0x10;
int logw = 0, logh = 0;
int s;
for(s = 1; s < ras->width; s *= 2)
logw++;
for(s = 1; s < ras->height; s *= 2)
logh++;
2015-07-11 13:45:38 +01:00
// set texturing registers
*wp++ = 0x60000000 | 4; // DMAret; 4 qw
*wp++ = 0;
*wp++ = 0;
*wp++ = 0x50000000 | 4; // DIRECT; 4 qw
dp = (uint64*)wp;
*dp++ = MAKE_GIF_TAG(3, 1, 0, 0, 0, 1);
*dp++ = 0x0e;
*dp++ = 1;
*dp++ = GS_TEXFLUSH;
*dp++ = MAKE_GS_TEX0(destAddr/4/64, ras->width/64, PSMCT32,
logw, logh, 0, 0, 0, 0, 0, 0, 0);
*dp++ = GS_TEX0_1;
*dp++ = MAKE_GS_TEX1(0, 0, 1, 1, 0, 0, 0);
*dp++ = GS_TEX1_1;
}
void
dumpRasterPacket(int n)
{
int i;
uint32 *p = (uint32*)rasterPacket;
for(i = 0; i < n; i++){
printf("%p %p %p %p\n", p[0], p[1], p[2], p[3]);
p += 4;
}
printf("\n");
}
2015-01-18 18:35:23 +00:00
void
drawAtomic(rw::Atomic *atomic)
2015-01-18 18:35:23 +00:00
{
rw::Geometry *geo = atomic->geometry;
2015-01-18 18:35:23 +00:00
assert(geo->instData != NULL);
rw::ps2::InstanceDataHeader *instData =
(rw::ps2::InstanceDataHeader*)geo->instData;
rw::MeshHeader *meshHeader = geo->meshHeader;
rw::Mesh *mesh;
uint8 *color;
2015-01-18 18:35:23 +00:00
atomic->frame->updateLTM();
matCopy(vuLightMat, atomic->frame->ltm);
2015-01-18 18:35:23 +00:00
matMult(vuMat, atomic->frame->ltm);
rw::Skin *skin = *PLUGINOFFSET(rw::Skin*, geo, rw::skinGlobals.offset);
2015-06-18 22:05:37 +01:00
for(uint i = 0; i < instData->numMeshes; i++){
2015-01-18 18:35:23 +00:00
if(instData->instanceMeshes[i].arePointersFixed == 0)
rw::ps2::fixDmaOffsets(&instData->instanceMeshes[i]);
2015-01-18 18:35:23 +00:00
geometryCall[1] = (uint32)instData->instanceMeshes[i].data;
mesh = &meshHeader->mesh[i];
color = mesh->material->color;
2015-01-18 18:35:23 +00:00
uint32 oldPtr = gsCurState->currentMemPtr;
vuMatcolor[0] = color[0]/255.0f;
vuMatcolor[1] = color[1]/255.0f;
vuMatcolor[2] = color[2]/255.0f;
vuMatcolor[3] = color[3]/2.0f/255.0f;
uint32 tex = 0;
if(mesh->material->texture && mesh->material->texture->raster){
vuMatcolor[0] /= 2.0f;
vuMatcolor[1] /= 2.0f;
vuMatcolor[2] /= 2.0f;
tex = 0x10;
uploadRaster(mesh->material->texture->raster);
2015-07-11 13:45:38 +01:00
}else{
rasterPacket[0] = 0x60000000;
rasterPacket[1] = 0x00000000;
rasterPacket[2] = 0x00000000;
rasterPacket[3] = 0x00000000;
}
2015-07-11 13:45:38 +01:00
textureCall[1] = (uint32)rasterPacket;
vuGIFtag[0] = MAKE_GIF_TAG(0,1,1,0xC|tex,0,3);
vuGIFtag[1] = 0x412;
if(rw::skinGlobals.offset && skin){
2015-01-18 18:35:23 +00:00
geometryCall[3] = 0x020000DC;
mpgCall[1] = (uint32)skinPipe;
}else{
geometryCall[3] = 0x02000114;
mpgCall[1] = (uint32)defaultPipe;
}
SET_REG32(D1_QWC, 0x00);
SET_REG32(D1_TADR, (uint)MyDmaPacket & 0x0FFFFFFF);
FlushCache(0);
SET_REG32(D1_CHCR, MAKE_DN_CHCR(1, 1, 0, 1, 0, 1));
DMA_WAIT(D1_CHCR);
gsCurState->currentMemPtr = oldPtr;
2015-01-18 18:35:23 +00:00
}
}
void
draw(void)
{
Matrix m;
static float rot = 0.0f;
gsClear();
matMakeIdentity(viewMat);
matTranslate(viewMat, 0.0f, 0.0f, -34.0f);
// matTranslate(viewMat, 0.0f, 0.0f, -10.0f);
// matTranslate(viewMat, 0.0f, 0.0f, -8.0f);
2015-07-11 13:45:38 +01:00
// matTranslate(viewMat, 0.0f, 0.0f, -4.0f);
matRotateX(viewMat, rot);
matRotateY(viewMat, rot);
matRotateZ(viewMat, rot);
2015-01-18 18:35:23 +00:00
matCopy(vuMat, projMat);
matMult(vuMat, viewMat);
2015-01-18 18:35:23 +00:00
for(int i = 0; i < clump->numAtomics; i++){
char *name = PLUGINOFFSET(char, clump->atomicList[i]->frame,
gta::nodeNameOffset);
2015-01-18 18:35:23 +00:00
if(strstr(name, "_dam") || strstr(name, "_vlo"))
continue;
matCopy(m, vuMat);
drawAtomic(clump->atomicList[i]);
matCopy(vuMat, m);
}
rot += 0.01f;
if(rot > 2*M_PI)
rot -= 2*M_PI;
}
int
main()
{
GsState gss;
gsCurState = &gss;
dmaReset(1);
// gsInitState(NONINTERLACED, PAL, FRAME);
// gsInitState(INTERLACED, PAL, FRAME);
gsInitState(INTERLACED, PAL, FIELD);
gsCurState->clearcol = 0x80404040;
gsInit();
2015-01-26 19:23:45 +00:00
gta::registerEnvSpecPlugin();
rw::registerMatFXPlugin();
2015-01-26 19:23:45 +00:00
rw::registerMaterialRightsPlugin();
rw::registerAtomicRightsPlugin();
2015-01-26 19:23:45 +00:00
rw::registerHAnimPlugin();
gta::registerNodeNamePlugin();
gta::registerBreakableModelPlugin();
gta::registerExtraVertColorPlugin();
rw::ps2::registerADCPlugin();
rw::registerSkinPlugin();
rw::registerNativeDataPlugin();
2015-01-26 19:23:45 +00:00
// rw::ps2::registerNativeDataPlugin();
rw::registerMeshPlugin();
2015-06-18 22:05:37 +01:00
rw::uint32 len;
// rw::uint8 *data = rw::getFileContents("host:player-vc-ps2.dff", &len);
rw::uint8 *data = rw::getFileContents("host:od_newscafe_dy-ps2.dff", &len);
2015-06-18 22:05:37 +01:00
// rw::uint8 *data = rw::getFileContents("host:admiral-ps2.dff", &len);
rw::StreamMemory in;
2015-01-18 18:35:23 +00:00
in.open(data, len);
rw::findChunk(&in, rw::ID_CLUMP, NULL, NULL);
clump = rw::Clump::streamRead(&in);
2015-01-18 18:35:23 +00:00
in.close();
delete[] data;
data = new rw::uint8[256*1024];
rw::StreamMemory out;
2015-01-18 18:35:23 +00:00
out.open(data, 0, 256*1024);
clump->streamWrite(&out);
// cf = fopen("host:out-ps2.dff", "wb");
// assert(cf != NULL);
// fwrite(data, out.getLength(), 1, cf);
// fclose(cf);
out.close();
delete[] data;
vuOffset[0] = 2048.0f;
vuOffset[1] = 2048.0f;
for(int i = 0; i < clump->numAtomics; i++){
rw::Atomic *a = clump->atomicList[i];
2015-01-18 18:35:23 +00:00
char *name =
PLUGINOFFSET(char, a->frame, gta::nodeNameOffset);
2015-01-18 18:35:23 +00:00
printf("%s\n", name);
}
// gsDumpState();
// matPerspective(mathProjectionMat, 60.0f, (float)gss.width/gss.height,
// matPerspective(mathProjectionMat, 60.0f, (float)gss.width/gss.dh,
// matPerspective(mathProjectionMat, 60.0f, 4.0/3.0, 1.0f, 100.0f);
// matPerspective(mathProjectionMat, 60.0f, 16.0/9.0, 1.0f, 100.0f);
matPerspective2(projMat, 60.0f, 4.0/3.0, gss.width,gss.height,
2015-01-18 18:35:23 +00:00
1.0f, 100.0f, 16777216, 1);
for(;;){
draw();
2015-07-11 13:45:38 +01:00
gsFlip(0);
2015-01-18 18:35:23 +00:00
}
return 0;
}