From e9f638db05851f7ca586b5e44fff27a9b245dfce Mon Sep 17 00:00:00 2001 From: aap Date: Sat, 11 Jul 2015 23:48:11 +0200 Subject: [PATCH] started work on (ps2) pipelines --- Makefile | 2 +- Makefile.mingw | 2 +- Makefile.ps2 | 2 +- rw.h | 1 + src/clump.cpp | 1 + src/geometry.cpp | 1 + src/gtaplg.cpp | 1 + src/image.cpp | 1 + src/ogl.cpp | 1 + src/plugins.cpp | 25 ++++++---- src/ps2.cpp | 125 ++++++++++++++++++++++++++++++++++++++++++++++ src/rwbase.cpp | 13 +++-- src/rwbase.h | 13 ++++- src/rwobjects.h | 11 +--- src/rwps2.h | 25 ++++++++++ tests/gl/Makefile | 8 +-- tests/gl/main.cpp | 5 +- 17 files changed, 205 insertions(+), 32 deletions(-) diff --git a/Makefile b/Makefile index 193c83e..c163b5d 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ BUILDDEF:=$(shell echo $(BUILD) | tr a-z A-Z | sed 's/^/-DRW_/') BUILDDIR=build-$(BUILD) SRCDIR=src SRC := $(patsubst %.cpp,$(SRCDIR)/%.cpp, rwbase.cpp clump.cpp\ - geometry.cpp plugins.cpp\ + geometry.cpp plugins.cpp pipeline.cpp\ ps2.cpp ogl.cpp\ image.cpp gtaplg.cpp) OBJ := $(patsubst $(SRCDIR)/%.cpp,$(BUILDDIR)/%.o,$(SRC)) diff --git a/Makefile.mingw b/Makefile.mingw index 834049f..e6bd3ff 100644 --- a/Makefile.mingw +++ b/Makefile.mingw @@ -6,7 +6,7 @@ BUILDDEF:=$(shell echo $(BUILD) | tr a-z A-Z | sed 's/^/-DRW_/') BUILDDIR=build-$(BUILD) SRCDIR=src SRC := $(patsubst %.cpp,$(SRCDIR)/%.cpp, rwbase.cpp clump.cpp\ - geometry.cpp plugins.cpp\ + geometry.cpp plugins.cpp pipeline.cpp\ ps2.cpp ogl.cpp\ image.cpp gtaplg.cpp) OBJ := $(patsubst $(SRCDIR)/%.cpp,$(BUILDDIR)/%.o,$(SRC)) diff --git a/Makefile.ps2 b/Makefile.ps2 index 7ebfe81..4684c71 100644 --- a/Makefile.ps2 +++ b/Makefile.ps2 @@ -7,7 +7,7 @@ BUILDDEF:=$(shell echo $(BUILD) | tr a-z A-Z | sed 's/^/-DRW_/') BUILDDIR=build-$(BUILD) SRCDIR=src SRC := $(patsubst %.cpp,$(SRCDIR)/%.cpp, rwbase.cpp clump.cpp\ - geometry.cpp plugins.cpp\ + geometry.cpp plugins.cpp pipeline.cpp\ ps2.cpp ogl.cpp\ image.cpp gtaplg.cpp) OBJ := $(patsubst $(SRCDIR)/%.cpp,$(BUILDDIR)/%.o,$(SRC)) diff --git a/rw.h b/rw.h index 69f96b9..8d5382c 100644 --- a/rw.h +++ b/rw.h @@ -1,5 +1,6 @@ #include "src/rwbase.h" #include "src/rwplugin.h" +#include "src/rwpipeline.h" #include "src/rwobjects.h" #include "src/rwps2.h" #include "src/rwogl.h" diff --git a/src/clump.cpp b/src/clump.cpp index d4c84d5..621d36c 100644 --- a/src/clump.cpp +++ b/src/clump.cpp @@ -7,6 +7,7 @@ #include "rwbase.h" #include "rwplugin.h" +#include "rwpipeline.h" #include "rwobjects.h" using namespace std; diff --git a/src/geometry.cpp b/src/geometry.cpp index 31917cc..cbeab77 100644 --- a/src/geometry.cpp +++ b/src/geometry.cpp @@ -7,6 +7,7 @@ #include "rwbase.h" #include "rwplugin.h" +#include "rwpipeline.h" #include "rwobjects.h" using namespace std; diff --git a/src/gtaplg.cpp b/src/gtaplg.cpp index 4941ec5..64596b0 100644 --- a/src/gtaplg.cpp +++ b/src/gtaplg.cpp @@ -7,6 +7,7 @@ #include "rwbase.h" #include "rwplugin.h" +#include "rwpipeline.h" #include "rwobjects.h" #include "gtaplg.h" diff --git a/src/image.cpp b/src/image.cpp index f8e1de4..ed32458 100755 --- a/src/image.cpp +++ b/src/image.cpp @@ -7,6 +7,7 @@ #include "rwbase.h" #include "rwplugin.h" +#include "rwpipeline.h" #include "rwobjects.h" using namespace std; diff --git a/src/ogl.cpp b/src/ogl.cpp index 9f391e9..8103445 100644 --- a/src/ogl.cpp +++ b/src/ogl.cpp @@ -7,6 +7,7 @@ #include "rwbase.h" #include "rwplugin.h" +#include "rwpipeline.h" #include "rwobjects.h" #include "rwogl.h" diff --git a/src/plugins.cpp b/src/plugins.cpp index 23cb0fd..f3cad01 100644 --- a/src/plugins.cpp +++ b/src/plugins.cpp @@ -7,6 +7,7 @@ #include "rwbase.h" #include "rwplugin.h" +#include "rwpipeline.h" #include "rwobjects.h" #include "rwps2.h" #include "rwogl.h" @@ -540,17 +541,20 @@ getSizeSkin(void *object, int32 offset, int32) } static void -skinRights(void *object, int32, int32, uint32 data) +skinRights(void *object, int32, int32, uint32) { - ((Atomic*)object)->pipeline = skinGlobals.pipeline; + ((Atomic*)object)->pipeline = skinGlobals.pipelines[platformIdx[platform]]; } void registerSkinPlugin(void) { - skinGlobals.pipeline = new Pipeline; - skinGlobals.pipeline->pluginID = ID_SKIN; - skinGlobals.pipeline->pluginData = 1; + Pipeline *defpipe = new Pipeline(PLATFORM_NULL); + defpipe->pluginID = ID_SKIN; + defpipe->pluginData = 1; + for(uint i = 0; i < nelem(matFXGlobals.pipelines); i++) + skinGlobals.pipelines[i] = defpipe; + skinGlobals.offset = Geometry::registerPlugin(sizeof(Skin*), ID_SKIN, createSkin, @@ -589,7 +593,8 @@ readAtomicMatFX(Stream *stream, int32, void *object, int32 offset, int32) stream->read(&flag, 4); *PLUGINOFFSET(int32, object, offset) = flag; if(flag) - ((Atomic*)object)->pipeline = matFXGlobals.pipeline; + ((Atomic*)object)->pipeline = + matFXGlobals.pipelines[platformIdx[rw::platform]]; } static void @@ -873,9 +878,11 @@ getSizeMaterialMatFX(void *object, int32 offset, int32) void registerMatFXPlugin(void) { - matFXGlobals.pipeline = new Pipeline; - matFXGlobals.pipeline->pluginID = ID_MATFX; - matFXGlobals.pipeline->pluginData = 0; + Pipeline *defpipe = new Pipeline(PLATFORM_NULL); + defpipe->pluginID = ID_MATFX; + defpipe->pluginData = 0; + for(uint i = 0; i < nelem(matFXGlobals.pipelines); i++) + matFXGlobals.pipelines[i] = defpipe; matFXGlobals.atomicOffset = Atomic::registerPlugin(sizeof(int32), ID_MATFX, diff --git a/src/ps2.cpp b/src/ps2.cpp index 793865e..2ef1ee0 100644 --- a/src/ps2.cpp +++ b/src/ps2.cpp @@ -7,6 +7,7 @@ #include "rwbase.h" #include "rwplugin.h" +#include "rwpipeline.h" #include "rwobjects.h" #include "rwps2.h" @@ -146,6 +147,7 @@ fixDmaOffsets(InstanceData *inst) void unfixDmaOffsets(InstanceData *inst) { + (void)inst; #ifdef RW_PS2 if(inst->arePointersFixed != 2) return; @@ -181,6 +183,129 @@ unfixDmaOffsets(InstanceData *inst) #endif } +// Pipeline + +enum PS2Attribs { + AT_V2_32 = 0x64000000, + AT_V2_16 = 0x65000000, + AT_V2_8 = 0x66000000, + AT_V3_32 = 0x68000000, + AT_V3_16 = 0x69000000, + AT_V3_8 = 0x6A000000, + AT_V4_32 = 0x6C000000, + AT_V4_16 = 0x6D000000, + AT_V4_8 = 0x6E000000, + AT_UNSGN = 0x00004000, + + AT_RW = 0x6 +}; + +enum PS2AttibTypes { + AT_XYZ = 0, + AT_UV = 1, + AT_UV2 = 2, + AT_RGBA = 3, + AT_NORMAL = 4 +}; + +PipeAttribute attribXYZ = { + "XYZ", + AT_V3_32 +}; + +PipeAttribute attribUV = { + "UV", + AT_V2_32 +}; + +PipeAttribute attribUV2 = { + "UV2", + AT_V4_32 +}; + +PipeAttribute attribRGBA = { + "RGBA", + AT_V4_8 | AT_UNSGN +}; + +PipeAttribute attribNormal = { + "Normal", + AT_V3_8 // RW has V4_8 but uses V3_8, wtf? +}; + +PipeAttribute attribWeights = { + "Weights", + AT_V4_32 | AT_RW +}; + +Pipeline::Pipeline(uint32 platform) + : rw::Pipeline(platform) { } + +void +Pipeline::setTriBufferSizes(uint32 inputStride, + uint32 stripCount, uint32 listCount) +{ + this->inputStride = inputStride; + this->triListCount = listCount/12*12; + PipeAttribute *a; + for(uint i = 0; i < nelem(this->attribs); i++){ + a = this->attribs[i]; + if(a && a->attrib & AT_RW) + goto brokenout; + } + this->triStripCount = stripCount/4*4; + return; +brokenout: + this->triStripCount = (stripCount-2)/4*4+2; +} + +Pipeline* +makeDefaultPipeline(void) +{ + Pipeline *pipe = new Pipeline(PLATFORM_PS2); + pipe->attribs[AT_XYZ] = &attribXYZ; + pipe->attribs[AT_UV] = &attribUV; + pipe->attribs[AT_RGBA] = &attribRGBA; + pipe->attribs[AT_NORMAL] = &attribNormal; + uint32 vertCount = Pipeline::getVertCount(VU_Lights, 4, 3, 2); + pipe->setTriBufferSizes(4, vertCount, vertCount/3); + pipe->vifOffset = pipe->inputStride*vertCount; + return pipe; +} + +Pipeline* +makeSkinPipeline(void) +{ + Pipeline *pipe = new Pipeline(PLATFORM_PS2); + pipe->attribs[AT_XYZ] = &attribXYZ; + pipe->attribs[AT_UV] = &attribUV; + pipe->attribs[AT_RGBA] = &attribRGBA; + pipe->attribs[AT_NORMAL] = &attribNormal; + pipe->attribs[AT_NORMAL+1] = &attribWeights; + uint32 vertCount = Pipeline::getVertCount(VU_Lights-0x100, 5, 3, 2); + pipe->setTriBufferSizes(5, vertCount, vertCount/3); + pipe->vifOffset = pipe->inputStride*vertCount; + return pipe; +} + +void +dumpPipeline(rw::Pipeline *rwpipe) +{ + if(rwpipe->platform != PLATFORM_PS2) + return; + Pipeline *pipe = (Pipeline*)rwpipe; + PipeAttribute *a; + for(uint i = 0; i < nelem(pipe->attribs); i++){ + a = pipe->attribs[i]; + if(a) + printf("%d %s: %x\n", i, a->name, a->attrib); + } + printf("stride: %x\n", pipe->inputStride); + printf("triSCount: %x\n", pipe->triStripCount); + printf("triLCount: %x\n", pipe->triListCount); + printf("vifOffset: %x\n", pipe->vifOffset); +} + // Skin void diff --git a/src/rwbase.cpp b/src/rwbase.cpp index 630f8d6..17cc742 100644 --- a/src/rwbase.cpp +++ b/src/rwbase.cpp @@ -12,8 +12,17 @@ using namespace std; namespace rw { +int platformIdx[10] = { 0, -1, 1, -1, 2, 3, -1, -1, 4, 5 }; + int version = 0x36003; int build = 0xFFFF; +#ifdef RW_PS2 + int platform = PLATFORM_PS2; +#elseif RW_OPENGL + int platform = PLATFORM_OPENGL; +#else + int platform = PLATFORM_NULL; +#endif char *debugFile = NULL; int32 @@ -219,14 +228,12 @@ StreamFile::close(void) uint32 StreamFile::write(const void *data, uint32 length) { -//printf("write %d bytes @ %x\n", length, this->tell()); return fwrite(data, length, 1, this->file); } uint32 StreamFile::read(void *data, uint32 length) { -//printf("read %d bytes @ %x\n", length, this->tell()); return fread(data, length, 1, this->file); } @@ -255,7 +262,6 @@ writeChunkHeader(Stream *s, int32 type, int32 size) int32 type, size; uint32 id; } buf = { type, size, libraryIDPack(version, build) }; -//printf("- write chunk %x @ %x\n", buf.type, s->tell()); s->write(&buf, 12); return true; } @@ -290,7 +296,6 @@ findChunk(Stream *s, uint32 type, uint32 *length, uint32 *version) *length = header.length; if(version) *version = header.version; -//printf("- chunk %x @ %x\n", header.type, s->tell()-12); return true; } s->seek(header.length); diff --git a/src/rwbase.h b/src/rwbase.h index 43ee9fd..e0ae935 100644 --- a/src/rwbase.h +++ b/src/rwbase.h @@ -32,6 +32,8 @@ typedef int32 bool32; typedef uint8 byte; typedef uint32 uint; +#define nelem(A) (sizeof(A) / sizeof A[0]) + class Stream { public: @@ -93,13 +95,21 @@ public: enum Platform { + PLATFORM_NULL = 0, + // D3D7 PLATFORM_OGL = 2, + // MAC PLATFORM_PS2 = 4, PLATFORM_XBOX = 5, + // GAMECUBE + // SOFTRAS PLATFORM_D3D8 = 8, - PLATFORM_D3D9 = 9 + PLATFORM_D3D9 = 9, + NUM_PLATFORMS = 6 }; +extern int platformIdx[10]; + enum PluginID { // Core @@ -134,6 +144,7 @@ enum PluginID extern int version; extern int build; +extern int platform; extern char *debugFile; inline uint32 diff --git a/src/rwobjects.h b/src/rwobjects.h index f427a2d..25ca75f 100644 --- a/src/rwobjects.h +++ b/src/rwobjects.h @@ -1,12 +1,5 @@ namespace rw { -// TODO: mostly a stub right now -struct Pipeline -{ - uint32 pluginID; - uint32 pluginData; -}; - struct Object { uint8 type; @@ -233,7 +226,7 @@ struct MatFXGlobals { int32 atomicOffset; int32 materialOffset; - Pipeline *pipeline; + Pipeline *pipelines[NUM_PLATFORMS]; }; extern MatFXGlobals matFXGlobals; void registerMatFXPlugin(void); @@ -330,7 +323,7 @@ struct Skin struct SkinGlobals { int32 offset; - Pipeline *pipeline; + Pipeline *pipelines[NUM_PLATFORMS]; }; extern SkinGlobals skinGlobals; void registerSkinPlugin(void); diff --git a/src/rwps2.h b/src/rwps2.h index 79c200f..3a633b7 100644 --- a/src/rwps2.h +++ b/src/rwps2.h @@ -19,6 +19,10 @@ struct InstanceDataHeader : rw::InstanceDataHeader InstanceData *instanceMeshes; }; +enum { + VU_Lights = 0x3d0 +}; + void *destroyNativeData(void *object, int32, int32); void readNativeData(Stream *stream, int32 len, void *object, int32, int32); void writeNativeData(Stream *stream, int32 len, void *object, int32, int32); @@ -31,6 +35,27 @@ void sizedebug(InstanceData *inst); // only RW_PS2 void fixDmaOffsets(InstanceData *inst); void unfixDmaOffsets(InstanceData *inst); +// + +struct Pipeline : rw::Pipeline +{ + uint32 vifOffset; + uint32 inputStride; + uint32 triStripCount, triListCount; + + static uint32 getVertCount(uint32 top, uint32 inAttribs, + uint32 outAttribs, uint32 outBufs) { + return (top-outBufs)/(inAttribs*2+outAttribs*outBufs); + } + + Pipeline(uint32 platform); + void setTriBufferSizes(uint32 inputStride, + uint32 stripCount, uint32 listCount); +}; + +Pipeline *makeDefaultPipeline(void); +Pipeline *makeSkinPipeline(void); +void dumpPipeline(rw::Pipeline *pipe); // Skin plugin diff --git a/tests/gl/Makefile b/tests/gl/Makefile index d89aa76..7569e38 100755 --- a/tests/gl/Makefile +++ b/tests/gl/Makefile @@ -4,11 +4,11 @@ OBJ = $(patsubst %.cpp, $(BUILDDIR)/%.o, $(SRC)) DEP = $(patsubst %.cpp, $(BUILDDIR)/%.d, $(SRC)) RWDIR=$(HOME)/src/librw -#LDLIBS=-pthread -lX11 -lXrandr -lXi -lXxf86vm -lGL -lGLEW -lm -#STATICLIBS= $(RWDIR)/librw-opengl.a /usr/local/lib/libglfw3.a +LDLIBS=-pthread -lX11 -lXrandr -lXi -lXxf86vm -lGL -lGLEW -lm +STATICLIBS= $(RWDIR)/librw-opengl.a /usr/local/lib/libglfw3.a -LDLIBS=-pthread -lGL -lGLEW -lglfw -STATICLIBS=$(RWDIR)/librw-opengl.a +#LDLIBS=-pthread -lGL -lGLEW -lglfw +#STATICLIBS=$(RWDIR)/librw-opengl.a CFLAGS=-g -I$(RWDIR) -Wall -Wextra diff --git a/tests/gl/main.cpp b/tests/gl/main.cpp index 15adfef..98a575a 100755 --- a/tests/gl/main.cpp +++ b/tests/gl/main.cpp @@ -42,15 +42,16 @@ renderAtomic(rw::Atomic *atomic) uint64 offset = 0; for(uint32 i = 0; i < meshHeader->numMeshes; i++){ Mesh *mesh = &meshHeader->mesh[i]; + Material *mat = mesh->material; float color[4]; - uint8 *col = mesh->material->color; + uint8 *col = mat->color; color[0] = col[0] / 255.0f; color[1] = col[1] / 255.0f; color[2] = col[2] / 255.0f; color[3] = col[3] / 255.0f; glUniform4fv(glGetUniformLocation(program, "matColor"), 1, color); - rw::gl::Texture *tex =(rw::gl::Texture*)mesh->material->texture; + rw::gl::Texture *tex =(rw::gl::Texture*)mat->texture; if(tex) tex->bind(0); else