fixes to d3d; implemented some allocation counters

This commit is contained in:
aap 2020-04-17 14:34:37 +02:00
parent 7bd6d4649e
commit 206547b404
18 changed files with 293 additions and 40 deletions

View File

@ -12,6 +12,8 @@
namespace rw { namespace rw {
int32 Camera::numAllocated;
PluginList Camera::s_plglist = { sizeof(Camera), sizeof(Camera), nil, nil }; PluginList Camera::s_plglist = { sizeof(Camera), sizeof(Camera), nil, nil };
void void
@ -279,6 +281,7 @@ Camera::create(void)
RWERROR((ERR_ALLOC, s_plglist.size)); RWERROR((ERR_ALLOC, s_plglist.size));
return nil; return nil;
} }
numAllocated++;
cam->object.object.init(Camera::ID, 0); cam->object.object.init(Camera::ID, 0);
cam->object.syncCB = cameraSync; cam->object.syncCB = cameraSync;
cam->beginUpdateCB = defaultBeginUpdateCB; cam->beginUpdateCB = defaultBeginUpdateCB;
@ -339,6 +342,7 @@ Camera::destroy(void)
if(this->clump) if(this->clump)
this->inClump.remove(); this->inClump.remove();
rwFree(this); rwFree(this);
numAllocated--;
} }
void void

View File

@ -13,6 +13,9 @@
namespace rw { namespace rw {
int32 Clump::numAllocated;
int32 Atomic::numAllocated;
PluginList Clump::s_plglist = { sizeof(Clump), sizeof(Clump), nil, nil }; PluginList Clump::s_plglist = { sizeof(Clump), sizeof(Clump), nil, nil };
PluginList Atomic::s_plglist = { sizeof(Atomic), sizeof(Atomic), nil, nil }; PluginList Atomic::s_plglist = { sizeof(Atomic), sizeof(Atomic), nil, nil };
@ -28,6 +31,7 @@ Clump::create(void)
RWERROR((ERR_ALLOC, s_plglist.size)); RWERROR((ERR_ALLOC, s_plglist.size));
return nil; return nil;
} }
numAllocated++;
clump->object.init(Clump::ID, 0); clump->object.init(Clump::ID, 0);
clump->atomics.init(); clump->atomics.init();
clump->lights.init(); clump->lights.init();
@ -67,6 +71,7 @@ Clump::destroy(void)
if(f = this->getFrame(), f) if(f = this->getFrame(), f)
f->destroyHierarchy(); f->destroyHierarchy();
rwFree(this); rwFree(this);
numAllocated--;
} }
Clump* Clump*
@ -341,6 +346,7 @@ Atomic::create(void)
RWERROR((ERR_ALLOC, s_plglist.size)); RWERROR((ERR_ALLOC, s_plglist.size));
return nil; return nil;
} }
numAllocated++;
atomic->object.object.init(Atomic::ID, 0); atomic->object.object.init(Atomic::ID, 0);
atomic->object.syncCB = atomicSync; atomic->object.syncCB = atomicSync;
atomic->geometry = nil; atomic->geometry = nil;
@ -391,6 +397,7 @@ Atomic::destroy(void)
this->inClump.remove(); this->inClump.remove();
this->setFrame(nil); this->setFrame(nil);
rwFree(this); rwFree(this);
numAllocated--;
} }
void void

View File

@ -116,9 +116,6 @@ enum {
}; };
#endif #endif
void addVidmemRaster(Raster *raster);
void removeVidmemRaster(Raster *raster);
// stolen from d3d8to9 // stolen from d3d8to9
static uint32 static uint32
calculateTextureSize(uint32 width, uint32 height, uint32 depth, uint32 format) calculateTextureSize(uint32 width, uint32 height, uint32 depth, uint32 format)
@ -190,6 +187,8 @@ createIndexBuffer(uint32 length)
#ifdef RW_D3D9 #ifdef RW_D3D9
IDirect3DIndexBuffer9 *ibuf; IDirect3DIndexBuffer9 *ibuf;
d3ddevice->CreateIndexBuffer(length, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &ibuf, 0); d3ddevice->CreateIndexBuffer(length, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &ibuf, 0);
if(ibuf)
d3d9Globals.numIndexBuffers++;
return ibuf; return ibuf;
#else #else
return rwNewT(uint8, length, MEMDUR_EVENT | ID_DRIVER); return rwNewT(uint8, length, MEMDUR_EVENT | ID_DRIVER);
@ -226,11 +225,16 @@ unlockIndices(void *indexBuffer)
} }
void* void*
createVertexBuffer(uint32 length, uint32 fvf, int32 pool) createVertexBuffer(uint32 length, uint32 fvf, bool dynamic)
{ {
#ifdef RW_D3D9 #ifdef RW_D3D9
IDirect3DVertexBuffer9 *vbuf; IDirect3DVertexBuffer9 *vbuf;
d3ddevice->CreateVertexBuffer(length, D3DUSAGE_WRITEONLY, fvf, (D3DPOOL)pool, &vbuf, 0); if(dynamic)
d3ddevice->CreateVertexBuffer(length, D3DUSAGE_WRITEONLY|D3DUSAGE_DYNAMIC, fvf, D3DPOOL_DEFAULT, &vbuf, 0);
else
d3ddevice->CreateVertexBuffer(length, D3DUSAGE_WRITEONLY, fvf, D3DPOOL_MANAGED, &vbuf, 0);
if(vbuf)
d3d9Globals.numVertexBuffers++;
return vbuf; return vbuf;
#else #else
(void)fvf; (void)fvf;
@ -275,6 +279,8 @@ createTexture(int32 width, int32 height, int32 numlevels, uint32 format)
IDirect3DTexture9 *tex; IDirect3DTexture9 *tex;
d3ddevice->CreateTexture(width, height, numlevels, 0, d3ddevice->CreateTexture(width, height, numlevels, 0,
(D3DFORMAT)format, D3DPOOL_MANAGED, &tex, nil); (D3DFORMAT)format, D3DPOOL_MANAGED, &tex, nil);
if(tex)
d3d9Globals.numTextures++;
return tex; return tex;
#else #else
int32 w = width; int32 w = width;
@ -344,7 +350,8 @@ deleteObject(void *object)
return; return;
#ifdef RW_D3D9 #ifdef RW_D3D9
IUnknown *unk = (IUnknown*)object; IUnknown *unk = (IUnknown*)object;
unk->Release(); if(unk->Release() != 0)
printf("something wasn't destroyed\n");
#else #else
rwFree(object); rwFree(object);
#endif #endif
@ -489,6 +496,15 @@ rasterCreateTexture(Raster *raster)
if(natras->format == D3DFMT_P8) if(natras->format == D3DFMT_P8)
natras->palette = (uint8*)rwNew(4*256, MEMDUR_EVENT | ID_DRIVER); natras->palette = (uint8*)rwNew(4*256, MEMDUR_EVENT | ID_DRIVER);
levels = Raster::calculateNumLevels(raster->width, raster->height); levels = Raster::calculateNumLevels(raster->width, raster->height);
// HACK
// raster <- image has to be done differently to be compatible with RW
// just delete texture here for the moment
if(natras->texture){
deleteObject(natras->texture);
d3d9Globals.numTextures--;
natras->texture = nil;
}
assert(natras->texture == nil);
natras->texture = createTexture(raster->width, raster->height, natras->texture = createTexture(raster->width, raster->height,
raster->format & Raster::MIPMAP ? levels : 1, raster->format & Raster::MIPMAP ? levels : 1,
natras->format); natras->format);
@ -513,7 +529,10 @@ rasterCreateCameraTexture(Raster *raster)
raster->format & Raster::MIPMAP ? levels : 1, raster->format & Raster::MIPMAP ? levels : 1,
D3DUSAGE_RENDERTARGET, D3DUSAGE_RENDERTARGET,
(D3DFORMAT)natras->format, D3DPOOL_DEFAULT, &tex, nil); (D3DFORMAT)natras->format, D3DPOOL_DEFAULT, &tex, nil);
assert(natras->texture == nil);
natras->texture = tex; natras->texture = tex;
assert(natras->texture && "couldn't create d3d camera texture");
d3d9Globals.numTextures++;
addVidmemRaster(raster); addVidmemRaster(raster);
} }
@ -900,8 +919,10 @@ destroyNativeRaster(void *object, int32 offset, int32)
#ifdef RW_D3D9 #ifdef RW_D3D9
destroyD3D9Raster(raster); destroyD3D9Raster(raster);
#endif #endif
if(natras->texture) if(natras->texture){
deleteObject(natras->texture); deleteObject(natras->texture);
d3d9Globals.numTextures--;
}
rwFree(natras->palette); rwFree(natras->palette);
return object; return object;
} }

View File

@ -151,7 +151,7 @@ readNativeData(Stream *stream, int32, void *object, int32, int32)
unlockIndices(inst->indexBuffer); unlockIndices(inst->indexBuffer);
inst->managed = 1; inst->managed = 1;
inst->vertexBuffer = createVertexBuffer(inst->stride*inst->numVertices, 0, D3DPOOL_MANAGED); inst->vertexBuffer = createVertexBuffer(inst->stride*inst->numVertices, 0, false);
uint8 *verts = lockVertices(inst->vertexBuffer, 0, 0, D3DLOCK_NOSYSLOCK); uint8 *verts = lockVertices(inst->vertexBuffer, 0, 0, D3DLOCK_NOSYSLOCK);
stream->read(verts, inst->stride*inst->numVertices); stream->read(verts, inst->stride*inst->numVertices);
unlockVertices(inst->vertexBuffer); unlockVertices(inst->vertexBuffer);
@ -357,7 +357,7 @@ defaultInstanceCB(Geometry *geo, InstanceData *inst)
inst->stride = getStride(geo->flags, geo->numTexCoordSets); inst->stride = getStride(geo->flags, geo->numTexCoordSets);
inst->vertexBuffer = createVertexBuffer(inst->numVertices*inst->stride, inst->vertexBuffer = createVertexBuffer(inst->numVertices*inst->stride,
inst->vertexShader, D3DPOOL_MANAGED); inst->vertexShader, false);
inst->managed = 1; inst->managed = 1;
uint8 *dst = lockVertices(inst->vertexBuffer, 0, 0, D3DLOCK_NOSYSLOCK); uint8 *dst = lockVertices(inst->vertexBuffer, 0, 0, D3DLOCK_NOSYSLOCK);

View File

@ -197,7 +197,7 @@ readNativeData(Stream *stream, int32, void *object, int32, int32)
continue; continue;
// TODO: unset managed flag when using morph targets. // TODO: unset managed flag when using morph targets.
// also uses different buffer type and locks differently // also uses different buffer type and locks differently
s->vertexBuffer = createVertexBuffer(s->stride*header->totalNumVertex, 0, D3DPOOL_MANAGED); s->vertexBuffer = createVertexBuffer(s->stride*header->totalNumVertex, 0, false);
uint8 *verts = lockVertices(s->vertexBuffer, 0, 0, D3DLOCK_NOSYSLOCK); uint8 *verts = lockVertices(s->vertexBuffer, 0, 0, D3DLOCK_NOSYSLOCK);
stream->read(verts, s->stride*header->totalNumVertex); stream->read(verts, s->stride*header->totalNumVertex);
unlockVertices(s->vertexBuffer); unlockVertices(s->vertexBuffer);
@ -521,7 +521,7 @@ defaultInstanceCB(Geometry *geo, InstanceDataHeader *header, bool32 reinstance)
assert(header->vertexDeclaration == nil); assert(header->vertexDeclaration == nil);
header->vertexDeclaration = createVertexDeclaration((VertexElement*)dcl); header->vertexDeclaration = createVertexDeclaration((VertexElement*)dcl);
s->vertexBuffer = createVertexBuffer(header->totalNumVertex*s->stride, 0, D3DPOOL_MANAGED); s->vertexBuffer = createVertexBuffer(header->totalNumVertex*s->stride, 0, false);
}else }else
getDeclaration(header->vertexDeclaration, dcl); getDeclaration(header->vertexDeclaration, dcl);

View File

@ -34,6 +34,18 @@ static VidmemRaster *vidmemRasters;
void addVidmemRaster(Raster *raster); void addVidmemRaster(Raster *raster);
void removeVidmemRaster(Raster *raster); void removeVidmemRaster(Raster *raster);
// Same thing for dynamic vertex buffers
struct DynamicVB
{
uint32 length;
uint32 fvf;
IDirect3DVertexBuffer9 **buf;
DynamicVB *next;
};
static DynamicVB *dynamicVBs;
void addDynamicVB(uint32 length, uint32 fvf, IDirect3DVertexBuffer9 **buf);
void removeDynamicVB(IDirect3DVertexBuffer9 **buf);
struct RwRasterStateCache { struct RwRasterStateCache {
Raster *raster; Raster *raster;
Texture::Addressing addressingU; Texture::Addressing addressingU;
@ -215,7 +227,7 @@ getSamplerState(uint32 stage, uint32 type, uint32 *value)
// Bring D3D device in accordance with saved render states (after a reset) // Bring D3D device in accordance with saved render states (after a reset)
static void static void
resetD3d9Device(void) restoreD3d9Device(void)
{ {
int32 i; int32 i;
uint32 s, t; uint32 s, t;
@ -579,6 +591,7 @@ createVertexShader(void *csosrc)
void *shdr; void *shdr;
if(d3ddevice->CreateVertexShader((DWORD*)csosrc, (IDirect3DVertexShader9**)&shdr) == D3D_OK) if(d3ddevice->CreateVertexShader((DWORD*)csosrc, (IDirect3DVertexShader9**)&shdr) == D3D_OK)
return shdr; return shdr;
d3d9Globals.numVertexShaders++;
return nil; return nil;
} }
@ -588,6 +601,7 @@ createPixelShader(void *csosrc)
void *shdr; void *shdr;
if(d3ddevice->CreatePixelShader((DWORD*)csosrc, (IDirect3DPixelShader9**)&shdr) == D3D_OK) if(d3ddevice->CreatePixelShader((DWORD*)csosrc, (IDirect3DPixelShader9**)&shdr) == D3D_OK)
return shdr; return shdr;
d3d9Globals.numPixelShaders++;
return nil; return nil;
} }
@ -680,6 +694,8 @@ endUpdate(Camera *cam)
d3ddevice->EndScene(); d3ddevice->EndScene();
} }
// Manage video memory
void void
addVidmemRaster(Raster *raster) addVidmemRaster(Raster *raster)
{ {
@ -713,11 +729,14 @@ releaseVidmemRasters(void)
raster = vmr->raster; raster = vmr->raster;
natras = PLUGINOFFSET(D3dRaster, raster, nativeRasterOffset); natras = PLUGINOFFSET(D3dRaster, raster, nativeRasterOffset);
if(raster->type == Raster::CAMERATEXTURE){ if(raster->type == Raster::CAMERATEXTURE){
if(natras->texture){
deleteObject(natras->texture); deleteObject(natras->texture);
d3d9Globals.numTextures--;
natras->texture = nil; natras->texture = nil;
} }
} }
} }
}
static void static void
recreateVidmemRasters(void) recreateVidmemRasters(void)
@ -736,10 +755,88 @@ recreateVidmemRasters(void)
D3DUSAGE_RENDERTARGET, D3DUSAGE_RENDERTARGET,
(D3DFORMAT)natras->format, D3DPOOL_DEFAULT, &tex, nil); (D3DFORMAT)natras->format, D3DPOOL_DEFAULT, &tex, nil);
natras->texture = tex; natras->texture = tex;
if(natras->texture)
d3d9Globals.numTextures++;
} }
} }
} }
void
addDynamicVB(uint32 length, uint32 fvf, IDirect3DVertexBuffer9 **buf)
{
DynamicVB *dvb = rwNewT(DynamicVB, 1, ID_DRIVER | MEMDUR_EVENT);
dvb->length = length;
dvb->fvf = fvf;
dvb->buf = buf;
dvb->next = dynamicVBs;
dynamicVBs = dvb;
}
void
removeDynamicVB(IDirect3DVertexBuffer9 **buf)
{
DynamicVB **p, *dvb;
for(p = &dynamicVBs; *p; p = &(*p)->next)
if((*p)->buf == buf)
goto found;
return;
found:
dvb = *p;
*p = dvb->next;
rwFree(dvb);
}
static void
releaseDynamicVBs(void)
{
DynamicVB *dvb;
for(dvb = dynamicVBs; dvb; dvb = dvb->next){
if(*dvb->buf){
deleteObject(*dvb->buf);
d3d9Globals.numVertexBuffers--;
*dvb->buf = nil;
}
}
}
static void
recreateDynamicVBs(void)
{
DynamicVB *dvb;
for(dvb = dynamicVBs; dvb; dvb = dvb->next){
*dvb->buf = (IDirect3DVertexBuffer9*)createVertexBuffer(dvb->length, dvb->fvf, true);
if(*dvb->buf)
d3d9Globals.numVertexBuffers++;
}
}
static void
releaseVideoMemory(void)
{
int32 i;
for(i = 0; i < MAXNUMSTAGES; i++)
d3ddevice->SetTexture(i, nil);
d3ddevice->SetVertexDeclaration(nil);
d3ddevice->SetVertexShader(nil);
d3ddevice->SetPixelShader(nil);
d3ddevice->SetIndices(nil);
for(i = 0; i < 2; i++)
d3ddevice->SetStreamSource(0, nil, 0, 0);
releaseVidmemRasters();
releaseDynamicVBs();
}
static void
restoreVideoMemory(void)
{
recreateDynamicVBs();
// important that we get all raster back before restoring state
recreateVidmemRasters();
restoreD3d9Device();
}
static void static void
clearCamera(Camera *cam, RGBA *col, uint32 mode) clearCamera(Camera *cam, RGBA *col, uint32 mode)
{ {
@ -760,11 +857,9 @@ clearCamera(Camera *cam, RGBA *col, uint32 mode)
d3d9Globals.present.BackBufferWidth = r.right; d3d9Globals.present.BackBufferWidth = r.right;
d3d9Globals.present.BackBufferHeight = r.bottom; d3d9Globals.present.BackBufferHeight = r.bottom;
releaseVidmemRasters(); releaseVideoMemory();
d3d::d3ddevice->Reset(&d3d9Globals.present); d3d::d3ddevice->Reset(&d3d9Globals.present);
// important that we get all raster back before restoring state restoreVideoMemory();
recreateVidmemRasters();
resetD3d9Device();
} }
d3ddevice->Clear(0, 0, mode, c, 1.0f, 0); d3ddevice->Clear(0, 0, mode, c, 1.0f, 0);
@ -783,11 +878,9 @@ showRaster(Raster *raster)
res = d3ddevice->TestCooperativeLevel(); res = d3ddevice->TestCooperativeLevel();
// lost while being minimized, not reset once we're back // lost while being minimized, not reset once we're back
if(res == D3DERR_DEVICENOTRESET){ if(res == D3DERR_DEVICENOTRESET){
releaseVidmemRasters(); releaseVideoMemory();
d3d::d3ddevice->Reset(&d3d9Globals.present); d3d::d3ddevice->Reset(&d3d9Globals.present);
// important that we get all raster back before restoring state restoreVideoMemory();
recreateVidmemRasters();
resetD3d9Device();
} }
} }
} }
@ -965,7 +1058,9 @@ found:
static int static int
closeD3D(void) closeD3D(void)
{ {
d3d9Globals.d3d9->Release(); ULONG ref = d3d9Globals.d3d9->Release();
if(ref != 0)
printf("IDirect3D9_Release did not destroy\n");
d3d9Globals.d3d9 = nil; d3d9Globals.d3d9 = nil;
return 1; return 1;
} }
@ -984,21 +1079,21 @@ startD3D(void)
D3DFORMAT format, zformat; D3DFORMAT format, zformat;
format = d3d9Globals.modes[d3d9Globals.currentMode].mode.Format; format = d3d9Globals.modes[d3d9Globals.currentMode].mode.Format;
bool windowed = !(d3d9Globals.modes[d3d9Globals.currentMode].flags & VIDEOMODEEXCLUSIVE);
// Use window size in windowed mode, otherwise get size from video mode // Use window size in windowed mode, otherwise get size from video mode
if(d3d9Globals.modes[d3d9Globals.currentMode].flags & VIDEOMODEEXCLUSIVE){ if(windowed){
// this will be much better for restoring after iconification
SetWindowLong(d3d9Globals.window, GWL_STYLE, WS_POPUP);
width = d3d9Globals.modes[d3d9Globals.currentMode].mode.Width;
height = d3d9Globals.modes[d3d9Globals.currentMode].mode.Height;
}else{
RECT rect; RECT rect;
GetClientRect(d3d9Globals.window, &rect); GetClientRect(d3d9Globals.window, &rect);
width = rect.right - rect.left; width = rect.right - rect.left;
height = rect.bottom - rect.top; height = rect.bottom - rect.top;
}else{
// this will be much better for restoring after iconification
SetWindowLong(d3d9Globals.window, GWL_STYLE, WS_POPUP);
width = d3d9Globals.modes[d3d9Globals.currentMode].mode.Width;
height = d3d9Globals.modes[d3d9Globals.currentMode].mode.Height;
} }
bool windowed = !(d3d9Globals.modes[d3d9Globals.currentMode].flags & VIDEOMODEEXCLUSIVE);
// See if we can get an alpha channel // See if we can get an alpha channel
if(format == D3DFMT_X8R8G8B8){ if(format == D3DFMT_X8R8G8B8){
if(d3d9Globals.d3d9->CheckDeviceType(d3d9Globals.adapter, D3DDEVTYPE_HAL, format, D3DFMT_A8R8G8B8, windowed) == D3D_OK) if(d3d9Globals.d3d9->CheckDeviceType(d3d9Globals.adapter, D3DDEVTYPE_HAL, format, D3DFMT_A8R8G8B8, windowed) == D3D_OK)
@ -1029,6 +1124,9 @@ startD3D(void)
d3d9Globals.present.PresentationInterval = D3DPRESENT_INTERVAL_ONE; d3d9Globals.present.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
// d3d9Globals.present.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // d3d9Globals.present.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
assert(d3d::d3ddevice == nil);
BOOL icon = IsIconic(d3d9Globals.window);
IDirect3DDevice9 *dev; IDirect3DDevice9 *dev;
hr = d3d9Globals.d3d9->CreateDevice(d3d9Globals.adapter, D3DDEVTYPE_HAL, hr = d3d9Globals.d3d9->CreateDevice(d3d9Globals.adapter, D3DDEVTYPE_HAL,
d3d9Globals.window, vp, &d3d9Globals.present, &dev); d3d9Globals.window, vp, &d3d9Globals.present, &dev);
@ -1047,6 +1145,13 @@ initD3D(void)
// TODO: do some real stuff here // TODO: do some real stuff here
d3d9Globals.numTextures = 0;
d3d9Globals.numVertexShaders = 0;
d3d9Globals.numPixelShaders = 0;
d3d9Globals.numVertexBuffers = 0;
d3d9Globals.numIndexBuffers = 0;
d3d9Globals.numVertexDeclarations = 0;
d3ddevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL); d3ddevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
rwStateCache.alphafunc = ALPHAGREATEREQUAL; rwStateCache.alphafunc = ALPHAGREATEREQUAL;
d3ddevice->SetRenderState(D3DRS_ALPHAREF, 10); d3ddevice->SetRenderState(D3DRS_ALPHAREF, 10);
@ -1229,7 +1334,11 @@ termD3D(void)
closeIm3D(); closeIm3D();
closeIm2D(); closeIm2D();
d3d::d3ddevice->Release(); releaseVideoMemory();
ULONG ref = d3d::d3ddevice->Release();
if(ref != 0)
printf("IDirect3D9Device_Release did not destroy\n");
d3d::d3ddevice = nil; d3d::d3ddevice = nil;
return 1; return 1;
} }

View File

@ -46,16 +46,30 @@ openIm2D(void)
D3DDECL_END() D3DDECL_END()
}; };
d3ddevice->CreateVertexDeclaration((D3DVERTEXELEMENT9*)elements, &im2ddecl); d3ddevice->CreateVertexDeclaration((D3DVERTEXELEMENT9*)elements, &im2ddecl);
im2dvertbuf = (IDirect3DVertexBuffer9*)createVertexBuffer(NUMVERTICES*sizeof(Im2DVertex), 0, D3DPOOL_MANAGED); assert(im2ddecl);
d3d9Globals.numVertexDeclarations++;
im2dvertbuf = (IDirect3DVertexBuffer9*)createVertexBuffer(NUMVERTICES*sizeof(Im2DVertex), 0, true);
assert(im2dvertbuf);
addDynamicVB(NUMVERTICES*sizeof(Im2DVertex), 0, &im2dvertbuf);
im2dindbuf = (IDirect3DIndexBuffer9*)createIndexBuffer(NUMINDICES*sizeof(uint16)); im2dindbuf = (IDirect3DIndexBuffer9*)createIndexBuffer(NUMINDICES*sizeof(uint16));
assert(im2dindbuf);
} }
void void
closeIm2D(void) closeIm2D(void)
{ {
deleteObject(im2ddecl); deleteObject(im2ddecl);
d3d9Globals.numVertexDeclarations--;
im2ddecl = nil;
removeDynamicVB(&im2dvertbuf);
deleteObject(im2dvertbuf); deleteObject(im2dvertbuf);
d3d9Globals.numVertexBuffers--;
im2dvertbuf = nil;
deleteObject(im2dindbuf); deleteObject(im2dindbuf);
d3d9Globals.numIndexBuffers--;
im2dindbuf = nil;
} }
static Im2DVertex tmpprimbuf[3]; static Im2DVertex tmpprimbuf[3];
@ -86,7 +100,7 @@ im2DRenderPrimitive(PrimitiveType primType, void *vertices, int32 numVertices)
// TODO: error // TODO: error
return; return;
} }
uint8 *lockedvertices = lockVertices(im2dvertbuf, 0, numVertices*sizeof(Im2DVertex), D3DLOCK_NOSYSLOCK); uint8 *lockedvertices = lockVertices(im2dvertbuf, 0, numVertices*sizeof(Im2DVertex), D3DLOCK_DISCARD);
memcpy(lockedvertices, vertices, numVertices*sizeof(Im2DVertex)); memcpy(lockedvertices, vertices, numVertices*sizeof(Im2DVertex));
unlockVertices(im2dvertbuf); unlockVertices(im2dvertbuf);
@ -138,7 +152,7 @@ im2DRenderIndexedPrimitive(PrimitiveType primType,
memcpy(lockedindices, indices, numIndices*sizeof(uint16)); memcpy(lockedindices, indices, numIndices*sizeof(uint16));
unlockIndices(im2dindbuf); unlockIndices(im2dindbuf);
uint8 *lockedvertices = lockVertices(im2dvertbuf, 0, numVertices*sizeof(Im2DVertex), D3DLOCK_NOSYSLOCK); uint8 *lockedvertices = lockVertices(im2dvertbuf, 0, numVertices*sizeof(Im2DVertex), D3DLOCK_DISCARD);
memcpy(lockedvertices, vertices, numVertices*sizeof(Im2DVertex)); memcpy(lockedvertices, vertices, numVertices*sizeof(Im2DVertex));
unlockVertices(im2dvertbuf); unlockVertices(im2dvertbuf);
@ -199,16 +213,30 @@ openIm3D(void)
D3DDECL_END() D3DDECL_END()
}; };
d3ddevice->CreateVertexDeclaration((D3DVERTEXELEMENT9*)elements, &im3ddecl); d3ddevice->CreateVertexDeclaration((D3DVERTEXELEMENT9*)elements, &im3ddecl);
im3dvertbuf = (IDirect3DVertexBuffer9*)createVertexBuffer(NUMVERTICES*sizeof(Im3DVertex), 0, D3DPOOL_MANAGED); assert(im3ddecl);
d3d9Globals.numVertexDeclarations++;
im3dvertbuf = (IDirect3DVertexBuffer9*)createVertexBuffer(NUMVERTICES*sizeof(Im3DVertex), 0, true);
assert(im3dvertbuf);
addDynamicVB(NUMVERTICES*sizeof(Im3DVertex), 0, &im3dvertbuf);
im3dindbuf = (IDirect3DIndexBuffer9*)createIndexBuffer(NUMINDICES*sizeof(uint16)); im3dindbuf = (IDirect3DIndexBuffer9*)createIndexBuffer(NUMINDICES*sizeof(uint16));
assert(im3dindbuf);
} }
void void
closeIm3D(void) closeIm3D(void)
{ {
deleteObject(im3ddecl); deleteObject(im3ddecl);
d3d9Globals.numVertexDeclarations--;
im3ddecl = nil;
removeDynamicVB(&im3dvertbuf);
deleteObject(im3dvertbuf); deleteObject(im3dvertbuf);
d3d9Globals.numVertexBuffers--;
im3dvertbuf = nil;
deleteObject(im3dindbuf); deleteObject(im3dindbuf);
d3d9Globals.numIndexBuffers--;
im3dindbuf = nil;
} }
void void
@ -225,7 +253,7 @@ im3DTransform(void *vertices, int32 numVertices, Matrix *world)
convMatrix(&d3dworld, world); convMatrix(&d3dworld, world);
d3ddevice->SetTransform(D3DTS_WORLD, (D3DMATRIX*)&d3dworld); d3ddevice->SetTransform(D3DTS_WORLD, (D3DMATRIX*)&d3dworld);
uint8 *lockedvertices = lockVertices(im3dvertbuf, 0, numVertices*sizeof(Im3DVertex), D3DLOCK_NOSYSLOCK); uint8 *lockedvertices = lockVertices(im3dvertbuf, 0, numVertices*sizeof(Im3DVertex), D3DLOCK_DISCARD);
memcpy(lockedvertices, vertices, numVertices*sizeof(Im3DVertex)); memcpy(lockedvertices, vertices, numVertices*sizeof(Im3DVertex));
unlockVertices(im3dvertbuf); unlockVertices(im3dvertbuf);

View File

@ -144,7 +144,7 @@ extern int vertFormatMap[];
void *createIndexBuffer(uint32 length); void *createIndexBuffer(uint32 length);
uint16 *lockIndices(void *indexBuffer, uint32 offset, uint32 size, uint32 flags); uint16 *lockIndices(void *indexBuffer, uint32 offset, uint32 size, uint32 flags);
void unlockIndices(void *indexBuffer); void unlockIndices(void *indexBuffer);
void *createVertexBuffer(uint32 length, uint32 fvf, int32 pool); void *createVertexBuffer(uint32 length, uint32 fvf, bool dynamic);
uint8 *lockVertices(void *vertexBuffer, uint32 offset, uint32 size, uint32 flags); uint8 *lockVertices(void *vertexBuffer, uint32 offset, uint32 size, uint32 flags);
void unlockVertices(void *vertexBuffer); void unlockVertices(void *vertexBuffer);
void *createTexture(int32 width, int32 height, int32 levels, uint32 format); void *createTexture(int32 width, int32 height, int32 levels, uint32 format);

View File

@ -39,10 +39,24 @@ struct D3d9Globals
int currentMode; int currentMode;
D3DPRESENT_PARAMETERS present; D3DPRESENT_PARAMETERS present;
int numTextures;
int numVertexShaders;
int numPixelShaders;
int numVertexBuffers;
int numIndexBuffers;
int numVertexDeclarations;
}; };
extern D3d9Globals d3d9Globals; extern D3d9Globals d3d9Globals;
void addVidmemRaster(Raster *raster);
void removeVidmemRaster(Raster *raster);
void addDynamicVB(uint32 length, uint32 fvf, IDirect3DVertexBuffer9 **buf); // NB: don't share this pointer
void removeDynamicVB(IDirect3DVertexBuffer9 **buf);
int findFormatDepth(uint32 format); int findFormatDepth(uint32 format);
void destroyD3D9Raster(Raster *raster); void destroyD3D9Raster(Raster *raster);
#endif #endif

View File

@ -87,6 +87,20 @@ Engine::init(void)
Raster::registerModule(); Raster::registerModule();
Texture::registerModule(); Texture::registerModule();
// TODO: reset all allocation counts here. or maybe do that in modules?
Frame::numAllocated = 0;
Image::numAllocated = 0;
Raster::numAllocated = 0;
Texture::numAllocated = 0;
TexDictionary::numAllocated = 0;
Geometry::numAllocated = 0;
Material::numAllocated = 0;
Atomic::numAllocated = 0;
Light::numAllocated = 0;
Camera::numAllocated = 0;
Clump::numAllocated = 0;
World::numAllocated = 0;
// driver plugin attach // driver plugin attach
ps2::registerPlatformPlugins(); ps2::registerPlatformPlugins();
xbox::registerPlatformPlugins(); xbox::registerPlatformPlugins();

View File

@ -12,6 +12,8 @@
namespace rw { namespace rw {
int32 Frame::numAllocated;
PluginList Frame::s_plglist = { sizeof(Frame), sizeof(Frame), nil, nil }; PluginList Frame::s_plglist = { sizeof(Frame), sizeof(Frame), nil, nil };
static void *frameOpen(void *object, int32 offset, int32 size) { engine->frameDirtyList.init(); return object; } static void *frameOpen(void *object, int32 offset, int32 size) { engine->frameDirtyList.init(); return object; }
static void *frameClose(void *object, int32 offset, int32 size) { return object; } static void *frameClose(void *object, int32 offset, int32 size) { return object; }
@ -30,6 +32,7 @@ Frame::create(void)
RWERROR((ERR_ALLOC, s_plglist.size)); RWERROR((ERR_ALLOC, s_plglist.size));
return nil; return nil;
} }
numAllocated++;
f->object.init(Frame::ID, 0); f->object.init(Frame::ID, 0);
f->objectList.init(); f->objectList.init();
f->child = nil; f->child = nil;
@ -60,6 +63,7 @@ Frame::destroy(void)
for(Frame *f = this->child; f; f = f->next) for(Frame *f = this->child; f; f = f->next)
f->object.parent = nil; f->object.parent = nil;
rwFree(this); rwFree(this);
numAllocated--;
} }
void void

View File

@ -15,6 +15,9 @@
namespace rw { namespace rw {
int32 Geometry::numAllocated;
int32 Material::numAllocated;
PluginList Geometry::s_plglist = { sizeof(Geometry), sizeof(Geometry), nil, nil }; PluginList Geometry::s_plglist = { sizeof(Geometry), sizeof(Geometry), nil, nil };
PluginList Material::s_plglist = { sizeof(Material), sizeof(Material), nil, nil }; PluginList Material::s_plglist = { sizeof(Material), sizeof(Material), nil, nil };
@ -29,6 +32,7 @@ Geometry::create(int32 numVerts, int32 numTris, uint32 flags)
RWERROR((ERR_ALLOC, s_plglist.size)); RWERROR((ERR_ALLOC, s_plglist.size));
return nil; return nil;
} }
numAllocated++;
geo->object.init(Geometry::ID, 0); geo->object.init(Geometry::ID, 0);
geo->flags = flags & 0xFF00FFFF; geo->flags = flags & 0xFF00FFFF;
geo->numTexCoordSets = (flags & 0xFF0000) >> 16; geo->numTexCoordSets = (flags & 0xFF0000) >> 16;
@ -96,6 +100,7 @@ Geometry::destroy(void)
rwFree(this->meshHeader); rwFree(this->meshHeader);
this->matList.deinit(); this->matList.deinit();
rwFree(this); rwFree(this);
numAllocated--;
} }
} }
@ -856,6 +861,7 @@ Material::create(void)
RWERROR((ERR_ALLOC, s_plglist.size)); RWERROR((ERR_ALLOC, s_plglist.size));
return nil; return nil;
} }
numAllocated++;
mat->texture = nil; mat->texture = nil;
memset(&mat->color, 0xFF, 4); memset(&mat->color, 0xFF, 4);
mat->surfaceProps = defaultSurfaceProps; mat->surfaceProps = defaultSurfaceProps;
@ -891,6 +897,7 @@ Material::destroy(void)
if(this->texture) if(this->texture)
this->texture->destroy(); this->texture->destroy();
rwFree(this); rwFree(this);
numAllocated--;
} }
} }

View File

@ -19,6 +19,8 @@
namespace rw { namespace rw {
int32 Image::numAllocated;
// TODO: full 16 bit support // TODO: full 16 bit support
Image* Image*
@ -29,6 +31,7 @@ Image::create(int32 width, int32 height, int32 depth)
RWERROR((ERR_ALLOC, sizeof(Image))); RWERROR((ERR_ALLOC, sizeof(Image)));
return nil; return nil;
} }
numAllocated++;
img->flags = 0; img->flags = 0;
img->width = width; img->width = width;
img->height = height; img->height = height;
@ -45,6 +48,7 @@ Image::destroy(void)
{ {
this->free(); this->free();
rwFree(this); rwFree(this);
numAllocated--;
} }
void void

View File

@ -12,6 +12,8 @@
namespace rw { namespace rw {
int32 Light::numAllocated;
PluginList Light::s_plglist = { sizeof(Light), sizeof(Light), nil, nil }; PluginList Light::s_plglist = { sizeof(Light), sizeof(Light), nil, nil };
static void static void
@ -34,6 +36,7 @@ Light::create(int32 type)
RWERROR((ERR_ALLOC, s_plglist.size)); RWERROR((ERR_ALLOC, s_plglist.size));
return nil; return nil;
} }
numAllocated++;
light->object.object.init(Light::ID, type); light->object.object.init(Light::ID, type);
light->object.syncCB = lightSync; light->object.syncCB = lightSync;
light->radius = 0.0f; light->radius = 0.0f;
@ -67,6 +70,7 @@ Light::destroy(void)
this->inClump.remove(); this->inClump.remove();
// we do not remove from world, be careful // we do not remove from world, be careful
rwFree(this); rwFree(this);
numAllocated--;
} }
void void

View File

@ -19,6 +19,8 @@
namespace rw { namespace rw {
int32 Raster::numAllocated;
struct RasterGlobals struct RasterGlobals
{ {
int32 sp; int32 sp;
@ -57,6 +59,7 @@ Raster::create(int32 width, int32 height, int32 depth, int32 format, int32 platf
// TODO: pass arguments through to the driver and create the raster there // TODO: pass arguments through to the driver and create the raster there
Raster *raster = (Raster*)rwMalloc(s_plglist.size, MEMDUR_EVENT); // TODO Raster *raster = (Raster*)rwMalloc(s_plglist.size, MEMDUR_EVENT); // TODO
assert(raster != nil); assert(raster != nil);
numAllocated++;
raster->parent = raster; raster->parent = raster;
raster->offsetX = 0; raster->offsetX = 0;
raster->offsetY = 0; raster->offsetY = 0;
@ -92,9 +95,8 @@ void
Raster::destroy(void) Raster::destroy(void)
{ {
s_plglist.destruct(this); s_plglist.destruct(this);
// delete[] this->texels;
// delete[] this->palette;
rwFree(this); rwFree(this);
numAllocated--;
} }
uint8* uint8*

View File

@ -55,6 +55,8 @@ struct Frame
Frame *next; Frame *next;
Frame *root; Frame *root;
static int32 numAllocated;
static Frame *create(void); static Frame *create(void);
Frame *cloneHierarchy(void); Frame *cloneHierarchy(void);
void destroy(void); void destroy(void);
@ -130,6 +132,8 @@ struct Image
uint8 *pixels; uint8 *pixels;
uint8 *palette; uint8 *palette;
static int32 numAllocated;
static Image *create(int32 width, int32 height, int32 depth); static Image *create(int32 width, int32 height, int32 depth);
void destroy(void); void destroy(void);
void allocate(void); void allocate(void);
@ -186,6 +190,8 @@ struct Raster
Raster *parent; Raster *parent;
int32 offsetX, offsetY; int32 offsetX, offsetY;
static int32 numAllocated;
static Raster *create(int32 width, int32 height, int32 depth, static Raster *create(int32 width, int32 height, int32 depth,
int32 format, int32 platform = 0); int32 format, int32 platform = 0);
void subRaster(Raster *parent, Rect *r); void subRaster(Raster *parent, Rect *r);
@ -273,6 +279,8 @@ struct Texture
uint32 filterAddressing; // VVVVUUUU FFFFFFFF uint32 filterAddressing; // VVVVUUUU FFFFFFFF
int32 refCount; int32 refCount;
static int32 numAllocated;
static Texture *create(Raster *raster); static Texture *create(Raster *raster);
void destroy(void); void destroy(void);
static Texture *fromDict(LLLink *lnk){ static Texture *fromDict(LLLink *lnk){
@ -318,6 +326,8 @@ struct Material
Pipeline *pipeline; Pipeline *pipeline;
int32 refCount; int32 refCount;
static int32 numAllocated;
static Material *create(void); static Material *create(void);
Material *clone(void); Material *clone(void);
void destroy(void); void destroy(void);
@ -415,6 +425,8 @@ struct Geometry
int32 refCount; int32 refCount;
static int32 numAllocated;
static Geometry *create(int32 numVerts, int32 numTris, uint32 flags); static Geometry *create(int32 numVerts, int32 numTris, uint32 flags);
void destroy(void); void destroy(void);
void lock(int32 lockFlags); void lock(int32 lockFlags);
@ -510,6 +522,8 @@ struct Atomic
World *world; World *world;
ObjectWithFrame::Sync originalSync; ObjectWithFrame::Sync originalSync;
static int32 numAllocated;
static Atomic *create(void); static Atomic *create(void);
Atomic *clone(void); Atomic *clone(void);
void destroy(void); void destroy(void);
@ -562,6 +576,8 @@ struct Light
World *world; World *world;
ObjectWithFrame::Sync originalSync; ObjectWithFrame::Sync originalSync;
static int32 numAllocated;
static Light *create(int32 type); static Light *create(int32 type);
void destroy(void); void destroy(void);
void setFrame(Frame *f) { this->object.setFrame(f); } void setFrame(Frame *f) { this->object.setFrame(f); }
@ -648,6 +664,8 @@ struct Camera
void (*originalBeginUpdate)(Camera*); void (*originalBeginUpdate)(Camera*);
void (*originalEndUpdate)(Camera*); void (*originalEndUpdate)(Camera*);
static int32 numAllocated;
static Camera *create(void); static Camera *create(void);
Camera *clone(void); Camera *clone(void);
void destroy(void); void destroy(void);
@ -684,6 +702,8 @@ struct Clump
World *world; World *world;
static int32 numAllocated;
static Clump *create(void); static Clump *create(void);
Clump *clone(void); Clump *clone(void);
void destroy(void); void destroy(void);
@ -721,6 +741,8 @@ struct World
LinkList lights; // these have positions (type >= 0x80) LinkList lights; // these have positions (type >= 0x80)
LinkList directionalLights; // these do not (type < 0x80) LinkList directionalLights; // these do not (type < 0x80)
static int32 numAllocated;
static World *create(void); static World *create(void);
void destroy(void); void destroy(void);
void addLight(Light *light); void addLight(Light *light);
@ -736,6 +758,8 @@ struct TexDictionary
Object object; Object object;
LinkList textures; LinkList textures;
static int32 numAllocated;
static TexDictionary *create(void); static TexDictionary *create(void);
void destroy(void); void destroy(void);
int32 count(void) { return this->textures.count(); } int32 count(void) { return this->textures.count(); }

View File

@ -21,6 +21,9 @@
namespace rw { namespace rw {
int32 Texture::numAllocated;
int32 TexDictionary::numAllocated;
PluginList TexDictionary::s_plglist = { sizeof(TexDictionary), sizeof(TexDictionary), nil, nil }; PluginList TexDictionary::s_plglist = { sizeof(TexDictionary), sizeof(TexDictionary), nil, nil };
PluginList Texture::s_plglist = { sizeof(Texture), sizeof(Texture), nil, nil }; PluginList Texture::s_plglist = { sizeof(Texture), sizeof(Texture), nil, nil };
PluginList Raster::s_plglist = { sizeof(Raster), sizeof(Raster), nil, nil }; PluginList Raster::s_plglist = { sizeof(Raster), sizeof(Raster), nil, nil };
@ -87,6 +90,7 @@ TexDictionary::create(void)
RWERROR((ERR_ALLOC, s_plglist.size)); RWERROR((ERR_ALLOC, s_plglist.size));
return nil; return nil;
} }
numAllocated++;
dict->object.init(TexDictionary::ID, 0); dict->object.init(TexDictionary::ID, 0);
dict->textures.init(); dict->textures.init();
s_plglist.construct(dict); s_plglist.construct(dict);
@ -102,6 +106,7 @@ TexDictionary::destroy(void)
Texture::fromDict(lnk)->destroy(); Texture::fromDict(lnk)->destroy();
s_plglist.destruct(this); s_plglist.destruct(this);
rwFree(this); rwFree(this);
numAllocated--;
} }
void void
@ -228,6 +233,7 @@ Texture::create(Raster *raster)
RWERROR((ERR_ALLOC, s_plglist.size)); RWERROR((ERR_ALLOC, s_plglist.size));
return nil; return nil;
} }
numAllocated++;
tex->dict = nil; tex->dict = nil;
tex->inDict.init(); tex->inDict.init();
memset(tex->name, 0, 32); memset(tex->name, 0, 32);
@ -250,6 +256,7 @@ Texture::destroy(void)
if(this->raster) if(this->raster)
this->raster->destroy(); this->raster->destroy();
rwFree(this); rwFree(this);
numAllocated--;
} }
} }

View File

@ -13,6 +13,8 @@
namespace rw { namespace rw {
int32 World::numAllocated = 0;
PluginList World::s_plglist = { sizeof(World), sizeof(World), nil, nil }; PluginList World::s_plglist = { sizeof(World), sizeof(World), nil, nil };
World* World*
@ -23,6 +25,7 @@ World::create(void)
RWERROR((ERR_ALLOC, s_plglist.size)); RWERROR((ERR_ALLOC, s_plglist.size));
return nil; return nil;
} }
numAllocated++;
world->object.init(World::ID, 0); world->object.init(World::ID, 0);
world->lights.init(); world->lights.init();
world->directionalLights.init(); world->directionalLights.init();
@ -35,6 +38,7 @@ World::destroy(void)
{ {
s_plglist.destruct(this); s_plglist.destruct(this);
rwFree(this); rwFree(this);
numAllocated--;
} }
void void