From 45d304ee31db2226377e31c6fc0dd53bb76f1162 Mon Sep 17 00:00:00 2001 From: Areloch Date: Fri, 7 Aug 2020 01:10:36 -0500 Subject: [PATCH] Fixes the backend logic for setting/creating 3DTextures in D3D11 --- .../gfx/D3D11/gfxD3D11TextureManager.cpp | 7 ++- .../gfx/D3D11/gfxD3D11TextureObject.cpp | 50 +++++++++++++++---- Engine/source/gfx/gfxTextureHandle.cpp | 4 +- Engine/source/gfx/gfxTextureHandle.h | 2 +- Engine/source/gfx/gfxTextureManager.cpp | 9 ---- Engine/source/gfx/gfxTextureManager.h | 1 - 6 files changed, 49 insertions(+), 24 deletions(-) diff --git a/Engine/source/gfx/D3D11/gfxD3D11TextureManager.cpp b/Engine/source/gfx/D3D11/gfxD3D11TextureManager.cpp index 2fdfb28e7..d7d508abf 100644 --- a/Engine/source/gfx/D3D11/gfxD3D11TextureManager.cpp +++ b/Engine/source/gfx/D3D11/gfxD3D11TextureManager.cpp @@ -133,6 +133,11 @@ void GFXD3D11TextureManager::_innerCreateTexture( GFXD3D11TextureObject *retTex, AssertFatal(false, "GFXD3D11TextureManager::_createTexture - failed to create volume texture!"); } + if (!retTex->mProfile->isSystemMemory()) + { + createResourceView(height, width, depth, d3dTextureFormat, numMipLevels, bindFlags, retTex); + } + retTex->mTextureSize.set(width, height, depth); retTex->get3DTex()->GetDesc(&desc); retTex->mMipLevels = numMipLevels; @@ -588,4 +593,4 @@ void GFXD3D11TextureManager::createResourceView(U32 height, U32 width, U32 depth hr = D3D11DEVICE->CreateDepthStencilView(resource,&desc, tex->getDSViewPtr()); AssertFatal(SUCCEEDED(hr), "CreateDepthStencilView:: failed to create view!"); } -} \ No newline at end of file +} diff --git a/Engine/source/gfx/D3D11/gfxD3D11TextureObject.cpp b/Engine/source/gfx/D3D11/gfxD3D11TextureObject.cpp index 27965a7da..ea26e0042 100644 --- a/Engine/source/gfx/D3D11/gfxD3D11TextureObject.cpp +++ b/Engine/source/gfx/D3D11/gfxD3D11TextureObject.cpp @@ -70,9 +70,17 @@ GFXLockedRect *GFXD3D11TextureObject::lock(U32 mipLevel /*= 0*/, RectI *inRect / if( !mStagingTex || mStagingTex->getWidth() != getWidth() || - mStagingTex->getHeight() != getHeight() ) + mStagingTex->getHeight() != getHeight() || + mStagingTex->getDepth() != getDepth()) { - mStagingTex.set( getWidth(), getHeight(), mFormat, &GFXSystemMemTextureProfile, avar("%s() - mLockTex (line %d)", __FUNCTION__, __LINE__) ); + if (getDepth() != 0) + { + mStagingTex.set(getWidth(), getHeight(), getDepth(), mFormat, &GFXSystemMemTextureProfile, avar("%s() - mLockTex (line %d)", __FUNCTION__, __LINE__, 0)); + } + else + { + mStagingTex.set(getWidth(), getHeight(), mFormat, &GFXSystemMemTextureProfile, avar("%s() - mLockTex (line %d)", __FUNCTION__, __LINE__)); + } } ID3D11DeviceContext* pContext = D3D11DEVICECONTEXT; @@ -82,13 +90,20 @@ GFXLockedRect *GFXD3D11TextureObject::lock(U32 mipLevel /*= 0*/, RectI *inRect / GFXD3D11TextureObject* pD3DStagingTex = (GFXD3D11TextureObject*)&(*mStagingTex); //map staging texture - HRESULT hr = pContext->Map(pD3DStagingTex->get2DTex(), mLockedSubresource, D3D11_MAP_READ, 0, &mapInfo); + HRESULT hr; + + bool is3D = mStagingTex->getDepth() != 0; + if (is3D) //3d texture + hr = pContext->Map(pD3DStagingTex->get3DTex(), mLockedSubresource, D3D11_MAP_READ, 0, &mapInfo); + else + hr = pContext->Map(pD3DStagingTex->get2DTex(), mLockedSubresource, D3D11_MAP_READ, 0, &mapInfo); if (FAILED(hr)) AssertFatal(false, "GFXD3D11TextureObject:lock - failed to map render target resource!"); const U32 width = mTextureSize.x >> mipLevel; const U32 height = mTextureSize.y >> mipLevel; + const U32 depth = is3D ? mTextureSize.z >> mipLevel : 1; //calculate locked box region and offset if (inRect) @@ -100,7 +115,7 @@ GFXLockedRect *GFXD3D11TextureObject::lock(U32 mipLevel /*= 0*/, RectI *inRect / mLockBox.left = inRect->point.x; mLockBox.bottom = inRect->point.y + inRect->extent.y; mLockBox.right = inRect->point.x + inRect->extent.x; - mLockBox.back = 1; + mLockBox.back = depth; mLockBox.front = 0; //calculate offset @@ -112,7 +127,7 @@ GFXLockedRect *GFXD3D11TextureObject::lock(U32 mipLevel /*= 0*/, RectI *inRect / mLockBox.left = 0; mLockBox.bottom = height; mLockBox.right = width; - mLockBox.back = 1; + mLockBox.back = depth; mLockBox.front = 0; } @@ -132,12 +147,27 @@ void GFXD3D11TextureObject::unlock(U32 mipLevel) ID3D11DeviceContext* pContext = D3D11DEVICECONTEXT; GFXD3D11TextureObject* pD3DStagingTex = (GFXD3D11TextureObject*)&(*mStagingTex); - ID3D11Texture2D *pStagingTex = pD3DStagingTex->get2DTex(); - //unmap staging texture - pContext->Unmap(pStagingTex, mLockedSubresource); - //copy lock box region from the staging texture to our regular texture - pContext->CopySubresourceRegion(mD3DTexture, mLockedSubresource, mLockBox.left, mLockBox.top, 0, pStagingTex, mLockedSubresource, &mLockBox); + bool is3D = mStagingTex->getDepth() != 0; + + if (is3D) + { + ID3D11Texture3D* pStagingTex = pD3DStagingTex->get3DTex(); + + //unmap staging texture + pContext->Unmap(pStagingTex, mLockedSubresource); + //copy lock box region from the staging texture to our regular texture + pContext->CopySubresourceRegion(mD3DTexture, mLockedSubresource, mLockBox.left, mLockBox.top, mLockBox.front, pStagingTex, mLockedSubresource, &mLockBox); + } + else + { + ID3D11Texture2D* pStagingTex = pD3DStagingTex->get2DTex(); + + //unmap staging texture + pContext->Unmap(pStagingTex, mLockedSubresource); + //copy lock box region from the staging texture to our regular texture + pContext->CopySubresourceRegion(mD3DTexture, mLockedSubresource, mLockBox.left, mLockBox.top, 0, pStagingTex, mLockedSubresource, &mLockBox); + } PROFILE_END(); diff --git a/Engine/source/gfx/gfxTextureHandle.cpp b/Engine/source/gfx/gfxTextureHandle.cpp index 7eb5773a0..72c2ef872 100644 --- a/Engine/source/gfx/gfxTextureHandle.cpp +++ b/Engine/source/gfx/gfxTextureHandle.cpp @@ -161,14 +161,14 @@ bool GFXTexHandle::set( U32 width, U32 height, GFXFormat format, GFXTextureProfi return isValid(); } -bool GFXTexHandle::set( U32 width, U32 height, U32 depth, void *pixels, GFXFormat format, GFXTextureProfile *profile, const String &desc, U32 numMipLevels ) +bool GFXTexHandle::set(U32 width, U32 height, U32 depth, GFXFormat format, GFXTextureProfile* profile, const String& desc, U32 numMipLevels) { // Clear the existing texture first, so that // its memory is free for the new allocation. free(); // Create and set the new texture. - StrongObjectRef::set( TEXMGR->createTexture( width, height, depth, pixels, format, profile,numMipLevels ) ); + StrongObjectRef::set(TEXMGR->createTexture(width, height, depth, format, profile, numMipLevels)); #ifdef TORQUE_DEBUG if ( getPointer() ) diff --git a/Engine/source/gfx/gfxTextureHandle.h b/Engine/source/gfx/gfxTextureHandle.h index 267be2fc3..08ff40f4f 100644 --- a/Engine/source/gfx/gfxTextureHandle.h +++ b/Engine/source/gfx/gfxTextureHandle.h @@ -60,7 +60,7 @@ public: // Sized bitmap GFXTexHandle( U32 width, U32 height, GFXFormat format, GFXTextureProfile *profile, const String &desc, U32 numMipLevels = 1, S32 antialiasLevel = 0); bool set( U32 width, U32 height, GFXFormat format, GFXTextureProfile *profile, const String &desc, U32 numMipLevels = 1, S32 antialiasLevel = 0); - bool set( U32 width, U32 height, U32 depth, void *pixels, GFXFormat format, GFXTextureProfile *profile, const String &desc, U32 numMipLevels = 1 ); + bool set( U32 width, U32 height, U32 depth, GFXFormat format, GFXTextureProfile* profile, const String& desc, U32 numMipLevels = 1); /// Returns the width and height as a point. Point2I getWidthHeight() const { return getPointer() ? Point2I( getPointer()->getWidth(), getPointer()->getHeight() ) : Point2I::Zero; } diff --git a/Engine/source/gfx/gfxTextureManager.cpp b/Engine/source/gfx/gfxTextureManager.cpp index 3096f10e3..9ab48dfce 100644 --- a/Engine/source/gfx/gfxTextureManager.cpp +++ b/Engine/source/gfx/gfxTextureManager.cpp @@ -815,7 +815,6 @@ GFXTextureObject *GFXTextureManager::createTexture( U32 width, U32 height, GFXFo GFXTextureObject *GFXTextureManager::createTexture( U32 width, U32 height, U32 depth, - void *pixels, GFXFormat format, GFXTextureProfile *profile, U32 numMipLevels) @@ -831,14 +830,6 @@ GFXTextureObject *GFXTextureManager::createTexture( U32 width, return NULL; } - // Call the internal load... - if( !_loadTexture( ret, pixels ) ) - { - Con::errorf("GFXTextureManager - failed to load volume texture" ); - return NULL; - } - - // And do book-keeping... // - texture info ret->mBitmapSize.set( width, height, depth ); diff --git a/Engine/source/gfx/gfxTextureManager.h b/Engine/source/gfx/gfxTextureManager.h index f5bf7c38e..be1640351 100644 --- a/Engine/source/gfx/gfxTextureManager.h +++ b/Engine/source/gfx/gfxTextureManager.h @@ -119,7 +119,6 @@ public: virtual GFXTextureObject *createTexture( U32 width, U32 height, U32 depth, - void *pixels, GFXFormat format, GFXTextureProfile *profile, U32 numMipLevels = 1);