Refactor GFXTextureArray

This commit is contained in:
Lukas Aldershaab 2021-01-01 21:05:11 +01:00
parent 87dd7ffc4a
commit e92e945912
7 changed files with 234 additions and 307 deletions

View file

@ -5,164 +5,47 @@
#include "gfxD3D11Device.h"
#include "gfxD3D11EnumTranslate.h"
#include "core/util/tVector.h"
#include "gfx/gfxDebugEvent.h"
#include "gfx/bitmap/imageUtils.h"
#include "gfx/util/screenspace.h"
#include "shaderGen/shaderFeature.h"
bool GFXD3D11TextureArray::fromTextureArray(const Vector<GFXTexHandle> &textureArray)
void GFXD3D11TextureArray::init()
{
bool success = true;
Vector<ID3D11Texture2D*> texture2Ds;
texture2Ds.setSize(textureArray.size());
Vector<GFXTexHandle> tmpHandles;
mArraySize = textureArray.size();
//---------------------------------------------------------------------------------------
// Create the texture array. Each element in the texture
// array has the same format/dimensions.
//---------------------------------------------------------------------------------------
D3D11_TEXTURE2D_DESC texElementDesc;
GFXFormat format;
bool found = false;
for (U32 idx = 0; idx < mArraySize; ++idx)
{
GFXTexHandle texObj = textureArray[idx];
if (texObj.isValid())
{
if (!found)
{
dynamic_cast<GFXD3D11TextureObject*>(texObj.getPointer())->get2DTex()->GetDesc(&texElementDesc);
found = true;
format = texObj.getFormat();
}
if (format != texObj.getFormat() || texElementDesc.Width != texObj.getWidth() || texElementDesc.Height != texObj.getHeight())
{
AssertWarn(true, "GFXGLTextureArray::fromTextureArray there was a mismatch in texture formats, defaulting to uncompressed format");
Con::warnf("GFXGLTextureArray::fromTextureArray there was a mismatch in texture formats, defaulting to uncompressed format");
success = false;
format = GFXFormatR8G8B8A8;
}
}
}
// One might think this should return false in this case, but the return value is mostly to highlight internal errors not input errors.
if (!found) return true;
for (U32 idx = 0; idx < mArraySize; ++idx)
{
texture2Ds[idx] = NULL;
if(textureArray[idx].isValid())
{
GFXTexHandle handle = textureArray[idx];
if (textureArray[idx]->getPath().isNotEmpty())
{
D3D11_TEXTURE2D_DESC desc;
ID3D11Texture2D* tex = dynamic_cast<GFXD3D11TextureObject*>(textureArray[idx].getPointer())->get2DTex();
tex->GetDesc(&desc);
if (desc.Height != texElementDesc.Height || desc.Width != texElementDesc.Width || textureArray[idx].getFormat() != format)
{
if (desc.Height != texElementDesc.Height || desc.Width != texElementDesc.Width)
{
AssertWarn(true, "GFXD3D11TextureArray::fromTextureArray all textures should be the same size");
Con::warnf("GFXD3D11TextureArray::fromTextureArray all textures should be the same size");
}
else
{
AssertWarn(true, "GFXD3D11TextureArray::fromTextureArray all textures should have the same format");
Con::warnf("GFXD3D11TextureArray::fromTextureArray all textures should have the same format");
}
GBitmap* inBitmap = TEXMGR->loadUncompressedTexture(textureArray[idx]->getPath(), &GFXTexturePersistentProfile, texElementDesc.Width, texElementDesc.Height);
if (!inBitmap->setFormat(format))
{
AssertWarn(true, "GFXD3D11TextureArray::fromTextureArray all textures must be convertible to GFXFormatR8G8B8A8");
Con::errorf("GFXD3D11TextureArray::fromTextureArray all textures must be convertible to GFXFormatR8G8B8A8");
success = false;
handle = NULL;
delete inBitmap;
}
else
{
handle = TEXMGR->createTexture(inBitmap, "", &GFXStaticTextureProfile, true);
tmpHandles.push_back(handle);
}
}
}
if (handle.isValid())
{
D3D11_TEXTURE2D_DESC desc;
ID3D11Texture2D* tex = dynamic_cast<GFXD3D11TextureObject*>(handle.getPointer())->get2DTex();
tex->GetDesc(&desc);
if (desc.Height != texElementDesc.Height || desc.Width != texElementDesc.Width || handle.getFormat() != format)
{
AssertWarn(true, "GFXD3D11TextureArray::fromTextureArray all textures must have the same size and format");
Con::errorf("GFXD3D11TextureArray::fromTextureArray all textures must have the same size and format");
success = false;
}
texture2Ds[idx] = dynamic_cast<GFXD3D11TextureObject*>(handle.getPointer())->get2DTex();
}
}
}
D3D11_TEXTURE2D_DESC texArrayDesc;
texArrayDesc.Width = texElementDesc.Width;
texArrayDesc.Height = texElementDesc.Height;
texArrayDesc.MipLevels = texElementDesc.MipLevels;
texArrayDesc.ArraySize = mArraySize;
texArrayDesc.Format = GFXD3D11TextureFormat[format];
texArrayDesc.SampleDesc.Count = 1;
texArrayDesc.SampleDesc.Quality = 0;
texArrayDesc.Usage = D3D11_USAGE_DEFAULT;
texArrayDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
texArrayDesc.CPUAccessFlags = 0;
texArrayDesc.MiscFlags = 0;
HRESULT hr = D3D11DEVICE->CreateTexture2D(&texArrayDesc, NULL, &mTextureArray);
AssertFatal(SUCCEEDED(hr), "GFXD3D11TextureArray::_createTextureArray failed to create texture array!");
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
// Copy individual texture elements into texture array.
//---------------------------------------------------------------------------------------
// for each texture element...
for (UINT i = 0; i < mArraySize; ++i)
{
if (texture2Ds[i] == NULL)
{
continue;
}
D3D11_TEXTURE2D_DESC desc;
texture2Ds[i]->GetDesc(&desc);
// for each mipmap level...
for (UINT j = 0; j < desc.MipLevels; ++j)
{
const U32 srcSubResource = D3D11CalcSubresource(j, 0, desc.MipLevels);
const U32 dstSubResource = D3D11CalcSubresource(j, i, texArrayDesc.MipLevels);
D3D11DEVICECONTEXT->CopySubresourceRegion(mTextureArray, dstSubResource, 0, 0, 0, texture2Ds[i], srcSubResource, NULL);
}
}
// Clean temporary textures
for (GFXTexHandle handle : tmpHandles)
{
handle.free();
}
//---------------------------------------------------------------------------------------
mTextureArrayDesc.Width = mWidth;
mTextureArrayDesc.Height = mHeight;
mTextureArrayDesc.MipLevels = mMipLevels;
mTextureArrayDesc.ArraySize = mArraySize;
mTextureArrayDesc.Format = GFXD3D11TextureFormat[mFormat];
mTextureArrayDesc.SampleDesc.Count = 1;
mTextureArrayDesc.SampleDesc.Quality = 0;
mTextureArrayDesc.Usage = D3D11_USAGE_DEFAULT;
mTextureArrayDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
mTextureArrayDesc.CPUAccessFlags = 0;
mTextureArrayDesc.MiscFlags = 0;
HRESULT hr = D3D11DEVICE->CreateTexture2D(&mTextureArrayDesc, NULL, &mTextureArray);
AssertFatal(SUCCEEDED(hr), "GFXD3D11TextureArray::init failed to create texture array!");
//---------------------------------------------------------------------------------------
// Create a resource view to the texture array.
//---------------------------------------------------------------------------------------
createResourceView(texArrayDesc.Format, texArrayDesc.MipLevels, texArrayDesc.BindFlags);
createResourceView(mTextureArrayDesc.Format, mTextureArrayDesc.MipLevels, mTextureArrayDesc.BindFlags);
//---------------------------------------------------------------------------------------
}
return success;
void GFXD3D11TextureArray::_setTexture(const GFXTexHandle& texture, U32 slot)
{
GFXD3D11TextureObject *texObj = dynamic_cast<GFXD3D11TextureObject*>(texture.getPointer());
ID3D11Texture2D* tex2d = texObj->get2DTex();
D3D11_TEXTURE2D_DESC desc;
tex2d->GetDesc(&desc);
// for each mipmap level...
for (UINT j = 0; j < desc.MipLevels; ++j)
{
const U32 srcSubResource = D3D11CalcSubresource(j, 0, desc.MipLevels);
const U32 dstSubResource = D3D11CalcSubresource(j, slot, mTextureArrayDesc.MipLevels);
D3D11DEVICECONTEXT->CopySubresourceRegion(mTextureArray, dstSubResource, 0, 0, 0, tex2d, srcSubResource, NULL);
}
}
void GFXD3D11TextureArray::setToTexUnit(U32 tuNum)
@ -170,7 +53,6 @@ void GFXD3D11TextureArray::setToTexUnit(U32 tuNum)
D3D11DEVICECONTEXT->PSSetShaderResources(tuNum, 1, &mSRView);
}
void GFXD3D11TextureArray::createResourceView(DXGI_FORMAT format, U32 numMipLevels, U32 usageFlags)
{
HRESULT hr;
@ -226,6 +108,8 @@ void GFXD3D11TextureArray::Release()
SAFE_RELEASE(mRTView)
SAFE_RELEASE(mDSView)
SAFE_RELEASE(mTextureArray)
GFXTextureArray::Release();
}
ID3D11ShaderResourceView* GFXD3D11TextureArray::getSRView()