Implement Singlepass Terrain Render

This commit is contained in:
Lukas Aldershaab 2021-01-01 21:05:21 +01:00
parent 49a8c0ad36
commit 87dd7ffc4a
35 changed files with 1658 additions and 951 deletions

View file

@ -40,6 +40,8 @@
#include "shaderGen/shaderGen.h"
#include <d3d9.h> //d3dperf
#include "gfxD3D11TextureArray.h"
#ifdef TORQUE_DEBUG
#include "d3d11sdklayers.h"
#endif
@ -48,6 +50,8 @@
#pragma comment(lib, "d3d9.lib") //d3dperf
#pragma comment(lib, "d3d11.lib")
class GFXD3D11TextureArray;
class GFXPCD3D11RegisterDevice
{
public:
@ -1736,6 +1740,13 @@ GFXCubemapArray * GFXD3D11Device::createCubemapArray()
return cubeArray;
}
GFXTextureArray * GFXD3D11Device::createTextureArray()
{
GFXD3D11TextureArray* textureArray = new GFXD3D11TextureArray();
textureArray->registerResourceWithDevice(this);
return textureArray;
}
// Debug events
//------------------------------------------------------------------------------
void GFXD3D11Device::enterDebugEvent(ColorI color, const char *name)

View file

@ -231,6 +231,7 @@ public:
virtual GFXCubemap *createCubemap();
virtual GFXCubemapArray *createCubemapArray();
virtual GFXTextureArray* createTextureArray();
virtual F32 getPixelShaderVersion() const { return mPixVersion; }
virtual void setPixelShaderVersion( F32 version ){ mPixVersion = version;}

View file

@ -1385,7 +1385,8 @@ void GFXD3D11Shader::_buildSamplerShaderConstantHandles( Vector<GFXShaderConstDe
AssertFatal( desc.constType == GFXSCT_Sampler ||
desc.constType == GFXSCT_SamplerCube ||
desc.constType == GFXSCT_SamplerCubeArray,
desc.constType == GFXSCT_SamplerCubeArray ||
desc.constType == GFXSCT_SamplerTextureArray,
"GFXD3D11Shader::_buildSamplerShaderConstantHandles - Invalid samplerDescription type!" );
GFXD3D11ShaderConstHandle *handle;

View file

@ -0,0 +1,266 @@
#include "gfxD3D11TextureArray.h"
#include <d3d11.h>
#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)
{
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();
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
// Create a resource view to the texture array.
//---------------------------------------------------------------------------------------
createResourceView(texArrayDesc.Format, texArrayDesc.MipLevels, texArrayDesc.BindFlags);
//---------------------------------------------------------------------------------------
return success;
}
void GFXD3D11TextureArray::setToTexUnit(U32 tuNum)
{
D3D11DEVICECONTEXT->PSSetShaderResources(tuNum, 1, &mSRView);
}
void GFXD3D11TextureArray::createResourceView(DXGI_FORMAT format, U32 numMipLevels, U32 usageFlags)
{
HRESULT hr;
if (usageFlags & D3D11_BIND_SHADER_RESOURCE)
{
D3D11_SHADER_RESOURCE_VIEW_DESC desc;
if (usageFlags & D3D11_BIND_DEPTH_STENCIL)
desc.Format = DXGI_FORMAT_R24_UNORM_X8_TYPELESS; // reads the depth
else
desc.Format = format;
desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
desc.Texture2DArray.MostDetailedMip = 0;
desc.Texture2DArray.MipLevels = numMipLevels;
desc.Texture2DArray.FirstArraySlice = 0;
desc.Texture2DArray.ArraySize = mArraySize;
hr = D3D11DEVICE->CreateShaderResourceView(mTextureArray, &desc, &mSRView);
AssertFatal(SUCCEEDED(hr), "GFXD3D11TextureArray::CreateShaderResourceView failed to create view!");
}
if (usageFlags & D3D11_BIND_RENDER_TARGET)
{
D3D11_RENDER_TARGET_VIEW_DESC desc;
desc.Format = format;
desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
desc.Texture2DArray.MipSlice = 0;
desc.Texture2DArray.FirstArraySlice = 0;
desc.Texture2DArray.ArraySize = mArraySize;
hr = D3D11DEVICE->CreateRenderTargetView(mTextureArray, &desc, &mRTView);
AssertFatal(SUCCEEDED(hr), "GFXD3D11TextureArray::CreateRenderTargetView failed to create view!");
}
if (usageFlags & D3D11_BIND_DEPTH_STENCIL)
{
D3D11_DEPTH_STENCIL_VIEW_DESC desc;
desc.Format = format;
desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY;
desc.Texture2DArray.MipSlice = 0;
desc.Texture2DArray.FirstArraySlice = 0;
desc.Texture2DArray.ArraySize = mArraySize;
desc.Flags = 0;
hr = D3D11DEVICE->CreateDepthStencilView(mTextureArray, &desc, &mDSView);
AssertFatal(SUCCEEDED(hr), "GFXD3D11TextureArray::CreateDepthStencilView failed to create view!");
}
}
void GFXD3D11TextureArray::Release()
{
SAFE_RELEASE(mSRView)
SAFE_RELEASE(mRTView)
SAFE_RELEASE(mDSView)
SAFE_RELEASE(mTextureArray)
}
ID3D11ShaderResourceView* GFXD3D11TextureArray::getSRView()
{
return mSRView;
}
ID3D11RenderTargetView* GFXD3D11TextureArray::getRTView()
{
return mRTView;
}
ID3D11DepthStencilView* GFXD3D11TextureArray::getDSView()
{
return mDSView;
}
ID3D11ShaderResourceView** GFXD3D11TextureArray::getSRViewPtr()
{
return &mSRView;
}
ID3D11RenderTargetView** GFXD3D11TextureArray::getRTViewPtr()
{
return &mRTView;
}
ID3D11DepthStencilView** GFXD3D11TextureArray::getDSViewPtr()
{
return &mDSView;
}
void GFXD3D11TextureArray::zombify()
{
// Unsupported
}
void GFXD3D11TextureArray::resurrect()
{
// Unsupported
}

View file

@ -0,0 +1,51 @@
#ifndef _GFXD3D11TEXTUREARRAY_H_
#define _GFXD3D11TEXTUREARRAY_H_
#include <dxgiformat.h>
#include "gfx/gfxTextureArray.h"
#include "gfx/gfxTextureManager.h"
#include "core/util/safeRelease.h"
#include "gfxD3D11TextureManager.h"
class GFXD3D11TextureArray : public GFXTextureArray
{
public:
GFXD3D11TextureArray()
: mSRView( NULL ),
mRTView( NULL ),
mDSView( NULL ),
mTextureArray( NULL )
{
}
bool fromTextureArray(const Vector<GFXTexHandle> &textureArray) override;
void setToTexUnit(U32 tuNum) override;
void createResourceView(DXGI_FORMAT format, U32 numMipLevels, U32 usageFlags);
// GFXResource interface
void zombify() override;
void resurrect() override;
void Release() override;
ID3D11ShaderResourceView* getSRView();
ID3D11RenderTargetView* getRTView();
ID3D11DepthStencilView* getDSView();
ID3D11ShaderResourceView** getSRViewPtr();
ID3D11RenderTargetView** getRTViewPtr();
ID3D11DepthStencilView** getDSViewPtr();
private:
ID3D11ShaderResourceView* mSRView; // for shader resource input
ID3D11RenderTargetView* mRTView; // for render targets
ID3D11DepthStencilView* mDSView; //render target view for depth stencil
ID3D11Texture2D* mTextureArray;
};
#endif

View file

@ -210,8 +210,8 @@ bool GFXD3D11TextureObject::copyToBmp(GBitmap* bmp)
// check format limitations
// at the moment we only support RGBA for the source (other 4 byte formats should
// be easy to add though)
AssertFatal(mFormat == GFXFormatR16G16B16A16F || mFormat == GFXFormatR8G8B8A8 || mFormat == GFXFormatR8G8B8A8_LINEAR_FORCE || mFormat == GFXFormatR8G8B8A8_SRGB, "copyToBmp: invalid format");
if (mFormat != GFXFormatR16G16B16A16F && mFormat != GFXFormatR8G8B8A8 && mFormat != GFXFormatR8G8B8A8_LINEAR_FORCE && mFormat != GFXFormatR8G8B8A8_SRGB)
AssertFatal(mFormat == GFXFormatR16G16B16A16F || mFormat == GFXFormatR8G8B8A8 || mFormat == GFXFormatR8G8B8A8_LINEAR_FORCE || mFormat == GFXFormatR8G8B8A8_SRGB || mFormat == GFXFormatR8G8B8, "copyToBmp: invalid format");
if (mFormat != GFXFormatR16G16B16A16F && mFormat != GFXFormatR8G8B8A8 && mFormat != GFXFormatR8G8B8A8_LINEAR_FORCE && mFormat != GFXFormatR8G8B8A8_SRGB && mFormat != GFXFormatR8G8B8)
return false;
PROFILE_START(GFXD3D11TextureObject_copyToBmp);
@ -248,6 +248,7 @@ bool GFXD3D11TextureObject::copyToBmp(GBitmap* bmp)
desc.BindFlags = 0;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
desc.Usage = D3D11_USAGE_STAGING;
desc.MiscFlags = 0;
ID3D11Texture2D* pStagingTexture = NULL;
HRESULT hr = D3D11DEVICE->CreateTexture2D(&desc, NULL, &pStagingTexture);

View file

@ -178,6 +178,16 @@ public:
virtual void resurrect() {}
};
class GFXNullTextureArray : public GFXTextureArray
{
public:
void zombify() override {}
void resurrect() override {}
void Release() override {}
bool fromTextureArray(const Vector<GFXTexHandle> &textureArray) override { return true; }
virtual void setToTexUnit(U32 tuNum) { }
};
class GFXNullVertexBuffer : public GFXVertexBuffer
{
unsigned char* tempBuf;
@ -317,6 +327,11 @@ GFXCubemapArray* GFXNullDevice::createCubemapArray()
return new GFXNullCubemapArray();
};
GFXTextureArray* GFXNullDevice::createTextureArray()
{
return new GFXNullTextureArray();
};
void GFXNullDevice::enumerateAdapters( Vector<GFXAdapter*> &adapterList )
{
// Add the NULL renderer

View file

@ -130,6 +130,7 @@ protected:
public:
virtual GFXCubemap * createCubemap();
virtual GFXCubemapArray *createCubemapArray();
virtual GFXTextureArray *createTextureArray();
virtual F32 getFillConventionOffset() const { return 0.0f; };

View file

@ -727,7 +727,7 @@ bool GBitmap::checkForTransparency()
}
//------------------------------------------------------------------------------
LinearColorF GBitmap::sampleTexel(F32 u, F32 v) const
LinearColorF GBitmap::sampleTexel(F32 u, F32 v, bool retAlpha) const
{
LinearColorF col(0.5f, 0.5f, 0.5f);
// normally sampling wraps all the way around at 1.0,
@ -751,6 +751,13 @@ LinearColorF GBitmap::sampleTexel(F32 u, F32 v) const
col.red = F32(buffer[lexelindex + 0]) / 255.0f;
col.green = F32(buffer[lexelindex + 1]) / 255.0f;
col.blue = F32(buffer[lexelindex + 2]) / 255.0f;
if (retAlpha)
{
if (getHasTransparency())
col.alpha = F32(buffer[lexelindex + 3]) / 255.0f;
else
col.alpha = 1.0f;
}
}
return col;
@ -1352,7 +1359,7 @@ U32 GBitmap::getSurfaceSize(const U32 mipLevel) const
}
DefineEngineFunction( getBitmapInfo, String, ( const char *filename ),,
"Returns image info in the following format: width TAB height TAB bytesPerPixel. "
"Returns image info in the following format: width TAB height TAB bytesPerPixel TAB format. "
"It will return an empty string if the file is not found.\n"
"@ingroup Rendering\n" )
{
@ -1360,7 +1367,8 @@ DefineEngineFunction( getBitmapInfo, String, ( const char *filename ),,
if ( !image )
return String::EmptyString;
return String::ToString( "%d\t%d\t%d", image->getWidth(),
return String::ToString( "%d\t%d\t%d\t%d", image->getWidth(),
image->getHeight(),
image->getBytesPerPixel() );
image->getBytesPerPixel(),
image->getFormat());
}

View file

@ -205,7 +205,7 @@ public:
/// the bitmap bits and to check for alpha values less than 255
bool checkForTransparency();
LinearColorF sampleTexel(F32 u, F32 v) const;
LinearColorF sampleTexel(F32 u, F32 v, bool retAlpha = false) const;
bool getColor(const U32 x, const U32 y, ColorI& rColor) const;
bool setColor(const U32 x, const U32 y, const ColorI& rColor);
U8 getChanelValueAt(U32 x, U32 y, U32 chan);

View file

@ -132,6 +132,8 @@ GFXDevice::GFXDevice()
mCurrentCubemap[i] = NULL;
mNewCubemap[i] = NULL;
mCurrentCubemapArray[i] = NULL;
mNewTextureArray[i] = NULL;
mCurrentTextureArray[i] = NULL;
mNewCubemapArray[i] = NULL;
mTexType[i] = GFXTDT_Normal;
@ -266,6 +268,8 @@ GFXDevice::~GFXDevice()
mNewCubemap[i] = NULL;
mCurrentCubemapArray[i] = NULL;
mNewCubemapArray[i] = NULL;
mCurrentTextureArray[i] = NULL;
mNewTextureArray[i] = NULL;
}
mCurrentRT = NULL;
@ -403,14 +407,23 @@ void GFXDevice::updateStates(bool forceSetAll /*=false*/)
}
break;
case GFXTDT_CubeArray:
{
mCurrentCubemapArray[i] = mNewCubemapArray[i];
if (mCurrentCubemapArray[i])
mCurrentCubemapArray[i]->setToTexUnit(i);
else
setTextureInternal(i, NULL);
}
break;
{
mCurrentCubemapArray[i] = mNewCubemapArray[i];
if (mCurrentCubemapArray[i])
mCurrentCubemapArray[i]->setToTexUnit(i);
else
setTextureInternal(i, NULL);
}
break;
case GFXTDT_TextureArray:
{
mCurrentTextureArray[i] = mNewTextureArray[i];
if (mCurrentTextureArray[i])
mCurrentTextureArray[i]->setToTexUnit(i);
else
setTextureInternal(i, NULL);
}
break;
default:
AssertFatal(false, "Unknown texture type!");
break;
@ -557,6 +570,15 @@ void GFXDevice::updateStates(bool forceSetAll /*=false*/)
mCurrentCubemapArray[i]->setToTexUnit(i);
else
setTextureInternal(i, NULL);
}
break;
case GFXTDT_TextureArray:
{
mCurrentTextureArray[i] = mNewTextureArray[i];
if (mCurrentTextureArray[i])
mCurrentTextureArray[i]->setToTexUnit(i);
else
setTextureInternal(i, NULL);
}
break;
default:
@ -801,6 +823,8 @@ void GFXDevice::setTexture( U32 stage, GFXTextureObject *texture )
mCurrentCubemap[stage] = NULL;
mNewCubemapArray[stage] = NULL;
mCurrentCubemapArray[stage] = NULL;
mNewTextureArray[stage] = NULL;
mCurrentTextureArray[stage] = NULL;
}
//-----------------------------------------------------------------------------
@ -827,6 +851,8 @@ void GFXDevice::setCubeTexture( U32 stage, GFXCubemap *cubemap )
mCurrentTexture[stage] = NULL;
mNewCubemapArray[stage] = NULL;
mCurrentCubemapArray[stage] = NULL;
mNewTextureArray[stage] = NULL;
mCurrentTextureArray[stage] = NULL;
}
//-----------------------------------------------------------------------------
@ -853,6 +879,36 @@ void GFXDevice::setCubeArrayTexture(U32 stage, GFXCubemapArray *cubemapArray)
mCurrentTexture[stage] = NULL;
mNewCubemap[stage] = NULL;
mCurrentCubemap[stage] = NULL;
mNewTextureArray[stage] = NULL;
mCurrentTextureArray[stage] = NULL;
}
//-----------------------------------------------------------------------------
// Set texture array
//-----------------------------------------------------------------------------
void GFXDevice::setTextureArray(U32 stage, GFXTextureArray *textureArray)
{
AssertFatal(stage < getNumSamplers(), avar("GFXDevice::setTextureArray - out of range stage! %i>%i", stage, getNumSamplers()));
if (mTexType[stage] == GFXTDT_TextureArray &&
((mTextureDirty[stage] && mNewTextureArray[stage].getPointer() == textureArray) ||
(!mTextureDirty[stage] && mCurrentTextureArray[stage].getPointer() == textureArray)))
return;
mStateDirty = true;
mTexturesDirty = true;
mTextureDirty[stage] = true;
mNewTextureArray[stage] = textureArray;
mTexType[stage] = GFXTDT_TextureArray;
// Clear out textures
mNewTexture[stage] = NULL;
mCurrentTexture[stage] = NULL;
mNewCubemap[stage] = NULL;
mCurrentCubemap[stage] = NULL;
mNewCubemapArray[stage] = NULL;
mCurrentCubemapArray[stage] = NULL;
}
//------------------------------------------------------------------------------

View file

@ -57,6 +57,7 @@
#ifndef _PLATFORM_PLATFORMTIMER_H_
#include "platform/platformTimer.h"
#endif
#include "gfxTextureArray.h"
class FontRenderBatcher;
class GFont;
@ -498,7 +499,8 @@ protected:
{
GFXTDT_Normal,
GFXTDT_Cube,
GFXTDT_CubeArray
GFXTDT_CubeArray,
GFXTDT_TextureArray
};
GFXTexHandle mCurrentTexture[TEXTURE_STAGE_COUNT];
@ -507,6 +509,8 @@ protected:
GFXCubemapHandle mNewCubemap[TEXTURE_STAGE_COUNT];
GFXCubemapArrayHandle mCurrentCubemapArray[TEXTURE_STAGE_COUNT];
GFXCubemapArrayHandle mNewCubemapArray[TEXTURE_STAGE_COUNT];
GFXTextureArrayHandle mCurrentTextureArray[TEXTURE_STAGE_COUNT];
GFXTextureArrayHandle mNewTextureArray[TEXTURE_STAGE_COUNT];
TexDirtyType mTexType[TEXTURE_STAGE_COUNT];
bool mTextureDirty[TEXTURE_STAGE_COUNT];
@ -757,6 +761,7 @@ protected:
public:
virtual GFXCubemap * createCubemap() = 0;
virtual GFXCubemapArray *createCubemapArray() = 0;
virtual GFXTextureArray *createTextureArray() = 0;
inline GFXTextureManager *getTextureManager()
{
@ -952,6 +957,7 @@ public:
void setTexture(U32 stage, GFXTextureObject *texture);
void setCubeTexture( U32 stage, GFXCubemap *cubemap );
void setCubeArrayTexture( U32 stage, GFXCubemapArray *cubemapArray);
void setTextureArray( U32 stage, GFXTextureArray *textureArray);
inline GFXTextureObject* getCurrentTexture( U32 stage ) { return mCurrentTexture[stage]; }
/// @}

View file

@ -599,7 +599,8 @@ enum GFXShaderConstType
// Samplers
GFXSCT_Sampler,
GFXSCT_SamplerCube,
GFXSCT_SamplerCubeArray
GFXSCT_SamplerCubeArray,
GFXSCT_SamplerTextureArray
};

View file

@ -0,0 +1,7 @@
#include "gfxTextureArray.h"
const String GFXTextureArray::describeSelf() const
{
// We've got nothing
return String();
}

View file

@ -0,0 +1,72 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _GFXTEXTUREARRAY_H_
#define _GFXTEXTUREARRAY_H_
#ifndef _REFBASE_H_
#include "core/util/refBase.h"
#endif
#ifndef _GFXRESOURCE_H_
#include "gfx/gfxResource.h"
#endif
#ifndef _GFXENUMS_H_
#include "gfxEnums.h"
#endif
#ifndef _GFXTEXTUREHANDLE_H_
#include "gfxTextureHandle.h"
#endif
class GFXTextureProfile;
class GFXTextureObject;
class GFXTextureArray : public StrongRefBase, public GFXResource
{
public:
virtual bool fromTextureArray(const Vector<GFXTexHandle> &textureArray) = 0;
virtual void setToTexUnit(U32 tuNum) = 0;
// GFXResource interface
virtual void zombify() = 0;
virtual void resurrect() = 0;
virtual void Release() = 0;
virtual const String describeSelf() const;
U32 mArraySize;
};
/// A reference counted handle to a texture array resource.
class GFXTextureArrayHandle : public StrongRefPtr<GFXTextureArray>
{
public:
GFXTextureArrayHandle() {}
GFXTextureArrayHandle(GFXTextureArray* textureArray) { StrongRefPtr<GFXTextureArray>::set(textureArray); }
/// Releases the texture handle.
void free() { StrongObjectRef::set(NULL); }
};
#endif // _GFXTEXTUREARRAY_H_

View file

@ -894,6 +894,42 @@ Torque::Path GFXTextureManager::validatePath(const Torque::Path &path)
return correctPath;
}
GBitmap *GFXTextureManager::loadUncompressedTexture(const Torque::Path &path, GFXTextureProfile *profile, U32 width, U32 height)
{
GBitmap* inBitmap = loadUncompressedTexture(path, &GFXTexturePersistentProfile);
if (inBitmap == NULL)
{
Con::warnf("GFXTextureManager::loadUncompressedTexture unable to load texture: %s", path.getFullPath());
return NULL;
}
// Set the format so we don't have to handle which channels are where.
if (!inBitmap->setFormat(GFXFormatR8G8B8A8))
{
Con::warnf("GFXTextureManager::loadUncompressedTexture unable to handle texture format: %s", path.getFullPath());
return NULL;
}
GBitmap* outBmp = new GBitmap(width, height, true, GFXFormatR8G8B8A8);
U8* oBits = (U8*)outBmp->getWritableBits();
for (S32 y = 0; y < width; y++)
{
for (S32 x = 0; x < height; x++)
{
ColorI texelColor = inBitmap->sampleTexel(x / F32(width), y / F32(height), true).toColorI(true);
oBits[(y * width + x) * 4] = texelColor.red;
oBits[(y * width + x) * 4 + 1] = texelColor.green;
oBits[(y * width + x) * 4 + 2] = texelColor.blue;
oBits[(y * width + x) * 4 + 3] = texelColor.alpha;
}
}
return outBmp;
}
GBitmap *GFXTextureManager::loadUncompressedTexture(const Torque::Path &path, GFXTextureProfile *profile)
{
PROFILE_SCOPE(GFXTextureManager_loadUncompressedTexture);

View file

@ -41,6 +41,7 @@
#ifndef _TSIGNAL_H_
#include "core/util/tSignal.h"
#endif
#include "gfxTextureHandle.h"
namespace Torque
@ -131,6 +132,7 @@ public:
S32 antialiasLevel);
Torque::Path validatePath(const Torque::Path &path);
GBitmap *loadUncompressedTexture(const Torque::Path& path, GFXTextureProfile* profile, U32 width, U32 height);
GBitmap *loadUncompressedTexture(const Torque::Path &path, GFXTextureProfile *profile);
virtual GFXTextureObject *createCompositeTexture(const Torque::Path &pathR, const Torque::Path &pathG, const Torque::Path &pathB, const Torque::Path &pathA, U32 inputKey[4],
GFXTextureProfile *profile);

View file

@ -22,6 +22,8 @@
#include "platform/platform.h"
#include "gfx/gl/gfxGLDevice.h"
#include "gfxGLTextureArray.h"
#include "platform/platformGL.h"
#include "gfx/gfxCubemap.h"
@ -457,6 +459,13 @@ GFXCubemapArray *GFXGLDevice::createCubemapArray()
return cubeArray;
}
GFXTextureArray* GFXGLDevice::createTextureArray()
{
GFXGLTextureArray* textureArray = new GFXGLTextureArray();
textureArray->registerResourceWithDevice(this);
return textureArray;
}
void GFXGLDevice::endSceneInternal()
{
// nothing to do for opengl
@ -766,6 +775,22 @@ void GFXGLDevice::setCubemapArrayInternal(U32 textureUnit, const GFXGLCubemapArr
}
}
void GFXGLDevice::setTextureArrayInternal(U32 textureUnit, const GFXGLTextureArray* texture)
{
if (texture)
{
mActiveTextureType[textureUnit] = GL_TEXTURE_2D_ARRAY;
texture->bind(textureUnit);
}
else if (mActiveTextureType[textureUnit] != GL_ZERO)
{
glActiveTexture(GL_TEXTURE0 + textureUnit);
glBindTexture(mActiveTextureType[textureUnit], 0);
getOpenglCache()->setCacheBindedTex(textureUnit, mActiveTextureType[textureUnit], 0);
mActiveTextureType[textureUnit] = GL_ZERO;
}
}
void GFXGLDevice::setMatrix( GFXMatrixType mtype, const MatrixF &mat )
{
// ONLY NEEDED ON FFP

View file

@ -35,6 +35,7 @@
#include "gfx/gfxResource.h"
#include "gfx/gl/gfxGLStateBlock.h"
class GFXGLTextureArray;
class GFXGLVertexBuffer;
class GFXGLPrimitiveBuffer;
class GFXGLTextureTarget;
@ -83,6 +84,7 @@ public:
virtual GFXCubemap * createCubemap();
virtual GFXCubemapArray *createCubemapArray();
virtual GFXTextureArray *createTextureArray();
virtual F32 getFillConventionOffset() const { return 0.0f; }
@ -173,6 +175,7 @@ protected:
virtual void setTextureInternal(U32 textureUnit, const GFXTextureObject*texture);
virtual void setCubemapInternal(U32 textureUnit, const GFXGLCubemap* texture);
virtual void setCubemapArrayInternal(U32 textureUnit, const GFXGLCubemapArray* texture);
virtual void setTextureArrayInternal(U32 textureUnit, const GFXGLTextureArray* texture);
virtual void setLightInternal(U32 lightStage, const GFXLightInfo light, bool lightEnable);
virtual void setLightMaterialInternal(const GFXLightMaterial mat);
@ -210,6 +213,7 @@ private:
friend class GFXGLTextureObject;
friend class GFXGLCubemap;
friend class GFXGLCubemapArray;
friend class GFXGLTextureArray;
friend class GFXGLWindowTarget;
friend class GFXGLPrimitiveBuffer;
friend class GFXGLVertexBuffer;

View file

@ -82,6 +82,7 @@ static U32 shaderConstTypeSize(GFXShaderConstType type)
case GFXSCT_Sampler:
case GFXSCT_SamplerCube:
case GFXSCT_SamplerCubeArray:
case GFXSCT_SamplerTextureArray:
return 4;
case GFXSCT_Float2:
case GFXSCT_Int2:
@ -630,6 +631,9 @@ void GFXGLShader::initConstantDescs()
case GL_SAMPLER_CUBE_MAP_ARRAY_ARB:
desc.constType = GFXSCT_SamplerCubeArray;
break;
case GL_SAMPLER_2D_ARRAY:
desc.constType = GFXSCT_SamplerTextureArray;
break;
default:
AssertFatal(false, "GFXGLShader::initConstantDescs - unrecognized uniform type");
// If we don't recognize the constant don't add its description.
@ -661,7 +665,10 @@ void GFXGLShader::initHandles()
HandleMap::Iterator handle = mHandles.find(desc.name);
S32 sampler = -1;
if(desc.constType == GFXSCT_Sampler || desc.constType == GFXSCT_SamplerCube || desc.constType == GFXSCT_SamplerCubeArray)
if(desc.constType == GFXSCT_Sampler ||
desc.constType == GFXSCT_SamplerCube ||
desc.constType == GFXSCT_SamplerCubeArray ||
desc.constType == GFXSCT_SamplerTextureArray)
{
S32 idx = mSamplerNamesOrdered.find_next(desc.name);
AssertFatal(idx != -1, "");
@ -704,7 +711,11 @@ void GFXGLShader::initHandles()
for (HandleMap::Iterator iter = mHandles.begin(); iter != mHandles.end(); ++iter)
{
GFXGLShaderConstHandle* handle = iter->value;
if(handle->isValid() && (handle->getType() == GFXSCT_Sampler || handle->getType() == GFXSCT_SamplerCube || handle->getType() == GFXSCT_SamplerCubeArray))
if(handle->isValid() &&
(handle->getType() == GFXSCT_Sampler ||
handle->getType() == GFXSCT_SamplerCube ||
handle->getType() == GFXSCT_SamplerCubeArray ||
handle->getType() == GFXSCT_SamplerTextureArray))
{
// Set sampler number on our program.
glUniform1i(handle->mLocation, handle->mSamplerNum);
@ -837,6 +848,7 @@ void GFXGLShader::setConstantsFromBuffer(GFXGLShaderConstBuffer* buffer)
case GFXSCT_Sampler:
case GFXSCT_SamplerCube:
case GFXSCT_SamplerCubeArray:
case GFXSCT_SamplerTextureArray:
glUniform1iv(handle->mLocation, handle->mDesc.arraySize, (GLint*)(mConstBuffer + handle->mOffset));
break;
case GFXSCT_Int2:

View file

@ -20,11 +20,11 @@ public:
class TextureUnit
{
public:
TextureUnit() : mTexture1D(0), mTexture2D(0), mTexture3D(0), mTextureCube(0), mTextureCubeArray(0)
TextureUnit() : mTexture1D(0), mTexture2D(0), mTexture3D(0), mTextureCube(0), mTextureCubeArray(0), mTextureArray(0)
{
}
GLuint mTexture1D, mTexture2D, mTexture3D, mTextureCube, mTextureCubeArray;
GLuint mTexture1D, mTexture2D, mTexture3D, mTextureCube, mTextureCubeArray, mTextureArray;
};
/// after glBindTexture
@ -48,6 +48,9 @@ public:
case GL_TEXTURE_CUBE_MAP_ARRAY:
mTextureUnits[mActiveTexture].mTextureCubeArray = handle;
break;
case GL_TEXTURE_2D_ARRAY:
mTextureUnits[mActiveTexture].mTextureArray = handle;
break;
default:
AssertFatal(0, avar("GFXGLStateCache::setCacheBindedTex - binding (%x) not supported.", biding) );
return;
@ -74,6 +77,9 @@ public:
case GL_TEXTURE_CUBE_MAP_ARRAY:
mTextureUnits[mActiveTexture].mTextureCubeArray = handle;
break;
case GL_TEXTURE_2D_ARRAY:
mTextureUnits[mActiveTexture].mTextureArray = handle;
break;
case GL_FRAMEBUFFER:
mBindedFBO_W = mBindedFBO_R = handle;
break;
@ -109,6 +115,8 @@ public:
return mTextureUnits[mActiveTexture].mTextureCube;
case GL_TEXTURE_CUBE_MAP_ARRAY:
return mTextureUnits[mActiveTexture].mTextureCubeArray;
case GL_TEXTURE_2D_ARRAY:
return mTextureUnits[mActiveTexture].mTextureArray;
case GL_DRAW_FRAMEBUFFER:
return mBindedFBO_W;
case GL_READ_FRAMEBUFFER:
@ -138,4 +146,4 @@ protected:
};
#endif
#endif

View file

@ -0,0 +1,215 @@
#include "gfxGLTextureArray.h"
#include "gfxGLTextureObject.h"
#include "gfxGLUtils.h"
#include "core/util/tVector.h"
#include "gfx/bitmap/imageUtils.h"
GFXGLTextureArray::GFXGLTextureArray()
{
mTextureArray = NULL;
}
GFXGLTextureArray::~GFXGLTextureArray()
{
glDeleteTextures(1, &mTextureArray);
}
bool GFXGLTextureArray::fromTextureArray(const Vector<GFXTexHandle> &textureArray)
{
bool success = true;
if (textureArray.empty())
{
return true;
}
bool found = false;
U32 baseWidth = 0, baseHeight = 0;
bool isCompressed = false;
mArraySize = textureArray.size();
for (GFXTexHandle texObj : textureArray)
{
if (texObj.isValid())
{
if (!found)
{
baseWidth = texObj.getWidth();
baseHeight = texObj.getHeight();
mMipMapLevels = getMax((U32)1, texObj->mMipLevels);
found = true;
mFormat = texObj.getFormat();
isCompressed = ImageUtil::isCompressedFormat(mFormat);
}
if (mFormat != texObj.getFormat() || baseWidth != texObj.getWidth() || baseHeight != texObj.getHeight())
{
AssertWarn(true, "GFXGLTextureArray::fromTextureArray there was a mismatch in texture format, defaulting to uncompressed format");
Con::warnf("GFXGLTextureArray::fromTextureArray there was a mismatch in texture format, defaulting to uncompressed format");
success = false;
mFormat = GFXFormatR8G8B8A8;
isCompressed = false;
}
}
}
// 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;
Vector <GFXGLTextureObject*> texture2Ds;
texture2Ds.setSize(textureArray.size());
Vector<GFXTexHandle> tmpHandles;
for (U32 idx = 0; idx < mArraySize; ++idx)
{
texture2Ds[idx] = NULL;
GFXTexHandle texObj = textureArray[idx];
if (texObj.isValid())
{
GFXTexHandle handle = textureArray[idx];
if (texObj->getPath().isNotEmpty())
{
if (texObj.getHeight() != baseHeight|| texObj.getWidth() != baseWidth || texObj.getFormat() != mFormat)
{
if (texObj.getHeight() != baseHeight || texObj.getWidth() != baseWidth)
{
AssertWarn(true, "GFXGLTextureArray::fromTextureArray all textures should be the same size");
Con::warnf("GFXGLTextureArray::fromTextureArray all textures should be the same size");
}
else
{
AssertWarn(true, "GFXGLTextureArray::fromTextureArray all textures should have the same format");
Con::warnf("GFXGLTextureArray::fromTextureArray all textures should have the same format");
}
GBitmap* inBitmap = TEXMGR->loadUncompressedTexture(textureArray[idx]->getPath(), &GFXTexturePersistentProfile, baseWidth, baseHeight);
if (!inBitmap->setFormat(mFormat))
{
AssertWarn(true, "GFXGLTextureArray::fromTextureArray all textures must be convertible to GFXFormatR8G8B8A8");
Con::errorf("GFXGLTextureArray::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())
{
if (handle.getHeight() != baseHeight || handle.getWidth()!= baseWidth || handle.getFormat() != mFormat)
{
AssertWarn(true, "GFXGLTextureArray::fromTextureArray all textures must have the same size and format");
Con::errorf("GFXGLTextureArray::fromTextureArray all textures must have the same size and format");
success = false;
}
texture2Ds[idx] = dynamic_cast<GFXGLTextureObject*>(handle.getPointer());
tmpHandles.push_back(handle);
}
}
}
glGenTextures(1, &mTextureArray);
PRESERVE_2D_TEXTURE_ARRAY();
glBindTexture(GL_TEXTURE_2D_ARRAY, mTextureArray);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, mMin(mMipMapLevels - 1, 1));
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexStorage3D(GL_TEXTURE_2D_ARRAY, mMipMapLevels, GL_RGBA8, baseWidth, baseHeight, textureArray.size());
for (U32 idx = 0; idx < texture2Ds.size(); ++idx)
{
if (texture2Ds[idx] == NULL)
{
continue;
}
GFXGLTextureObject* texObj = texture2Ds[idx];
for (U32 mip = 0; mip < mMipMapLevels; ++mip)
{
U8* buf = texObj->getTextureData(mip);
const U32 mipWidth = getMax(U32(1), baseWidth >> mip);
const U32 mipHeight = getMax(U32(1), baseHeight >> mip);
glBindTexture(GL_TEXTURE_2D_ARRAY, mTextureArray);
if (isCompressed)
{
glCompressedTexSubImage3D(
GL_TEXTURE_2D_ARRAY,
mip, 0, 0,
idx, mipWidth, mipHeight, 1,
GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], buf
);
}
else
{
glTexSubImage3D(
GL_TEXTURE_2D_ARRAY,
mip, 0, 0,
idx, mipWidth, mipHeight, 1,
GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], buf
);
}
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
delete[] buf;
}
}
if (!isCompressed)
glGenerateMipmap(GL_TEXTURE_2D_ARRAY);
// Clean temporary textures
for (GFXTexHandle handle : tmpHandles)
{
handle.free();
}
return success;
}
void GFXGLTextureArray::setToTexUnit(U32 tuNum)
{
dynamic_cast<GFXGLDevice*>(getOwningDevice())->setTextureArrayInternal(tuNum, this);
}
void GFXGLTextureArray::Release()
{
glDeleteTextures(1, &mTextureArray);
mTextureArray = 0;
}
void GFXGLTextureArray::bind(U32 textureUnit) const
{
glActiveTexture(GL_TEXTURE0 + textureUnit);
glBindTexture(GL_TEXTURE_2D_ARRAY, mTextureArray);
dynamic_cast<GFXGLDevice*>(getOwningDevice())->getOpenglCache()->setCacheBindedTex(textureUnit, GL_TEXTURE_2D_ARRAY, mTextureArray);
GFXGLStateBlockRef sb = static_cast<GFXGLDevice*>(GFX)->getCurrentStateBlock();
AssertFatal(sb, "GFXGLTextureArray::bind - No active stateblock!");
if (!sb)
return;
const GFXSamplerStateDesc& ssd = sb->getDesc().samplers[textureUnit];
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, 0));
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, mMin(mMipMapLevels - 1, 1));
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GFXGLTextureAddress[ssd.addressModeU]);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GFXGLTextureAddress[ssd.addressModeV]);
}
void GFXGLTextureArray::zombify()
{
}
void GFXGLTextureArray::resurrect()
{
}

View file

@ -0,0 +1,34 @@
#ifndef _GFXGLTEXTUREARRAY_H_
#define _GFXGLTEXTUREARRAY_H_
#include <glad/glad.h>
#include "gfx/gfxTextureArray.h"
#include "gfx/gfxTextureManager.h"
class GFXGLTextureArray : public GFXTextureArray
{
public:
GFXGLTextureArray();
~GFXGLTextureArray();
bool fromTextureArray(const Vector<GFXTexHandle>& textureArray) override;
void setToTexUnit(U32 tuNum) override;
void bind(U32 textureUnit) const;
// GFXResource interface
void zombify() override;
void resurrect() override;
void Release() override;
private:
GLuint mTextureArray;
U32 mMipMapLevels;
GFXFormat mFormat;
};
#endif

View file

@ -194,6 +194,9 @@ GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_CUBE_MAP, GL
#define PRESERVE_CUBEMAP_ARRAY_TEXTURE() \
GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_BINDING_CUBE_MAP_ARRAY, (GFXGLPreserveInteger::BindFn)glBindTexture)
#define PRESERVE_2D_TEXTURE_ARRAY() \
GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BINDING_2D_ARRAY, (GFXGLPreserveInteger::BindFn)glBindTexture)
#define _GET_TEXTURE_BINDING(binding) \
binding == GL_TEXTURE_2D ? GL_TEXTURE_BINDING_2D : (binding == GL_TEXTURE_3D ? GL_TEXTURE_BINDING_3D : GL_TEXTURE_BINDING_1D )