From 7cea137baf27c4536e7043c751ce3f180eb2bbf9 Mon Sep 17 00:00:00 2001 From: aap Date: Thu, 16 Apr 2020 18:40:11 +0200 Subject: [PATCH] fixed ztest on d3d9; texture refcounting in matfx --- src/d3d/d3ddevice.cpp | 40 ++++++++++++++++++++++++++++++++-------- src/matfx.cpp | 18 +++++++++++++++--- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/d3d/d3ddevice.cpp b/src/d3d/d3ddevice.cpp index d99a702..dc0a57a 100644 --- a/src/d3d/d3ddevice.cpp +++ b/src/d3d/d3ddevice.cpp @@ -246,6 +246,36 @@ resetD3d9Device(void) // RW render state +static void +setDepthTest(bool32 enable) +{ + if(rwStateCache.ztest != enable){ + rwStateCache.ztest = enable; + if(rwStateCache.zwrite && !enable){ + // If we still want to write, enable but set mode to always + setRenderState(D3DRS_ZENABLE, TRUE); + setRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); + }else{ + setRenderState(D3DRS_ZENABLE, rwStateCache.ztest); + setRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL); + } + } +} + +static void +setDepthWrite(bool32 enable) +{ + if(rwStateCache.zwrite != enable){ + rwStateCache.zwrite = enable; + if(enable && !rwStateCache.ztest){ + // Have to switch on ztest so writing can work + setRenderState(D3DRS_ZENABLE, TRUE); + setRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); + } + setRenderState(D3DRS_ZWRITEENABLE, rwStateCache.zwrite); + } +} + static void setVertexAlpha(bool32 enable) { @@ -415,16 +445,10 @@ setRwRenderState(int32 state, void *pvalue) } break; case ZTESTENABLE: - if(rwStateCache.ztest != bval){ - rwStateCache.ztest = bval; - setRenderState(D3DRS_ZENABLE, rwStateCache.ztest); - } + setDepthTest(bval); break; case ZWRITEENABLE: - if(rwStateCache.zwrite != bval){ - rwStateCache.zwrite = bval; - setRenderState(D3DRS_ZWRITEENABLE, rwStateCache.zwrite); - } + setDepthWrite(bval); break; case FOGENABLE: if(rwStateCache.fogenable != bval){ diff --git a/src/matfx.cpp b/src/matfx.cpp index 0035527..3f1d1b3 100644 --- a/src/matfx.cpp +++ b/src/matfx.cpp @@ -159,8 +159,12 @@ void MatFX::setBumpTexture(Texture *t) { int32 i = this->getEffectIndex(BUMPMAP); - if(i >= 0) + if(i >= 0){ + if(this->fx[i].bump.tex) + this->fx[i].bump.tex->destroy(); this->fx[i].bump.tex = t; + t->refCount++; + } } void @@ -193,8 +197,12 @@ void MatFX::setEnvTexture(Texture *t) { int32 i = this->getEffectIndex(ENVMAP); - if(i >= 0) + if(i >= 0){ + if(this->fx[i].env.tex) + this->fx[i].env.tex->destroy(); this->fx[i].env.tex = t; + t->refCount++; + } } void @@ -245,8 +253,12 @@ void MatFX::setDualTexture(Texture *t) { int32 i = this->getEffectIndex(DUAL); - if(i >= 0) + if(i >= 0){ + if(this->fx[i].dual.tex) + this->fx[i].dual.tex->destroy(); this->fx[i].dual.tex = t; + t->refCount++; + } } void