This commit is contained in:
Areloch 2019-04-15 23:11:18 -05:00
parent c025760422
commit 000c7b2263
21 changed files with 4102 additions and 108 deletions

View file

@ -31,7 +31,7 @@
#include "gfx/bitmap/imageUtils.h"
GLenum GFXGLCubemap::faceList[6] =
static GLenum faceList[6] =
{
GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
@ -55,6 +55,11 @@ GFXGLCubemap::~GFXGLCubemap()
GFXTextureManager::removeEventDelegate( this, &GFXGLCubemap::_onTextureEvent );
}
GLenum GFXGLCubemap::getEnumForFaceNumber(U32 face)
{
return faceList[face];
}
void GFXGLCubemap::fillCubeTextures(GFXTexHandle* faces)
{
AssertFatal( faces, "");
@ -280,3 +285,225 @@ void GFXGLCubemap::_onTextureEvent( GFXTexCallbackCode code )
else
tmResurrect();
}
U8* GFXGLCubemap::getTextureData(U32 face, U32 mip)
{
AssertFatal(mMipMapLevels, "");
mip = (mip < mMipMapLevels) ? mip : 0;
const U32 bytesPerTexel = 4; //TODO make work with more formats!!!!!
const U32 dataSize = ImageUtil::isCompressedFormat(mFaceFormat)
? getCompressedSurfaceSize(mFaceFormat, mWidth, mHeight, mip)
: (mWidth >> mip) * (mHeight >> mip) * bytesPerTexel;
U8* data = new U8[dataSize];
PRESERVE_TEXTURE(GL_TEXTURE_CUBE_MAP);
glBindTexture(GL_TEXTURE_CUBE_MAP, mCubemap);
if (ImageUtil::isCompressedFormat(mFaceFormat))
glGetCompressedTexImage(faceList[face], mip, data);
else
glGetTexImage(faceList[face], mip, GFXGLTextureFormat[mFaceFormat], GFXGLTextureType[mFaceFormat], data);
return data;
}
//-----------------------------------------------------------------------------
// Cubemap Array
//-----------------------------------------------------------------------------
GFXGLCubemapArray::GFXGLCubemapArray()
{
}
GFXGLCubemapArray::~GFXGLCubemapArray()
{
glDeleteTextures(1, &mCubemap);
}
//TODO: really need a common private 'init' function to avoid code double up with these init* functions
void GFXGLCubemapArray::init(GFXCubemapHandle *cubemaps, const U32 cubemapCount)
{
AssertFatal(cubemaps, "GFXGLCubemapArray- Got null GFXCubemapHandle!");
AssertFatal(*cubemaps, "GFXGLCubemapArray - Got empty cubemap!");
//all cubemaps must be the same size,format and number of mipmaps. Grab the details from the first cubemap
mSize = cubemaps[0]->getSize();
mFormat = cubemaps[0]->getFormat();
mMipMapLevels = cubemaps[0]->getMipMapLevels();
mNumCubemaps = cubemapCount;
const bool isCompressed = ImageUtil::isCompressedFormat(mFormat);
glGenTextures(1, &mCubemap);
PRESERVE_CUBEMAP_ARRAY_TEXTURE();
glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAX_LEVEL, mMipMapLevels - 1);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
for (U32 i = 0; i < cubemapCount; i++)
{
GFXGLCubemap* glTex = static_cast<GFXGLCubemap*>(cubemaps[i].getPointer());
for (U32 face = 0; face < 6; face++)
{
for (U32 currentMip = 0; currentMip < mMipMapLevels; currentMip++)
//U32 currentMip = 0;
{
U8 *pixelData = glTex->getTextureData(face, currentMip);
const U32 mipSize = getMax(U32(1), mSize >> currentMip);
/*if (isCompressed)
{
const U32 mipDataSize = getCompressedSurfaceSize(mFormat, mSize, mSize, currentMip);
glCompressedTexImage2D(faceList[face], currentMip, GFXGLTextureInternalFormat[mFormat], mipSize, mipSize, 0, mipDataSize, pixelData);
}
else
{*/ //TODO figure out xyzOffsets
glTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, 0, 0, 0, mipSize, mipSize, i * face, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], pixelData);
//}
delete[] pixelData;
}
}
}
}
//Just allocate the cubemap array but we don't upload any data
void GFXGLCubemapArray::init(const U32 cubemapCount, const U32 cubemapFaceSize, const GFXFormat format)
{
//all cubemaps must be the same size,format and number of mipmaps. Grab the details from the first cubemap
mSize = cubemapFaceSize;
mFormat = format;
mMipMapLevels = ImageUtil::getMaxMipCount(cubemapFaceSize, cubemapFaceSize);
mNumCubemaps = cubemapCount;
const bool isCompressed = ImageUtil::isCompressedFormat(mFormat);
glGenTextures(1, &mCubemap);
PRESERVE_CUBEMAP_ARRAY_TEXTURE();
glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAX_LEVEL, mMipMapLevels - 1);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
/*for (U32 i = 0; i < cubemapCount; i++)
{
GFXGLCubemap* glTex = static_cast<GFXGLCubemap*>(cubemaps[i].getPointer());
for (U32 face = 0; face < 6; face++)
{
for (U32 currentMip = 0; currentMip < mMipMapLevels; currentMip++)
{
U8 *pixelData = glTex->getTextureData(face, currentMip);
const U32 mipSize = getMax(U32(1), mSize >> currentMip);
if (isCompressed)
{
const U32 mipDataSize = getCompressedSurfaceSize(mFormat, mSize, mSize, currentMip);
glCompressedTexImage2D(faceList[face], currentMip, GFXGLTextureInternalFormat[mFormat], mipSize, mipSize, 0, mipDataSize, pixelData);
}
else
{
glTexImage2D(faceList[face], currentMip, GFXGLTextureInternalFormat[mFormat], mipSize, mipSize,
0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], pixelData);
}
delete[] pixelData;
}
}
}*/
}
void GFXGLCubemapArray::updateTexture(const GFXCubemapHandle &cubemap, const U32 slot)
{
AssertFatal(slot <= mNumCubemaps, "GFXD3D11CubemapArray::updateTexture - trying to update a cubemap texture that is out of bounds!");
const bool isCompressed = ImageUtil::isCompressedFormat(mFormat);
GFXGLCubemap* glTex = static_cast<GFXGLCubemap*>(cubemap.getPointer());
for (U32 face = 0; face < 6; face++)
{
for (U32 currentMip = 0; currentMip < mMipMapLevels; currentMip++)
//U32 currentMip = 0;
{
U8 *pixelData = glTex->getTextureData(face, currentMip);
const U32 mipSize = getMax(U32(1), mSize >> currentMip);
/*if (isCompressed)
{
const U32 mipDataSize = getCompressedSurfaceSize(mFormat, mSize, mSize, currentMip);
glCompressedTexImage2D(faceList[face], currentMip, GFXGLTextureInternalFormat[mFormat], mipSize, mipSize, 0, mipDataSize, pixelData);
}
else
{*/ //TODO figure out xyzOffsets
glTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, 0, 0, 0, mipSize, mipSize, slot * face, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], pixelData);
//}
delete[] pixelData;
}
}
}
void GFXGLCubemapArray::copyTo(GFXCubemapArray *pDstCubemap)
{
AssertFatal(pDstCubemap, "GFXGLCubemapArray::copyTo - Got null GFXCubemapArray");
const U32 dstCount = pDstCubemap->getNumCubemaps();
const GFXFormat dstFmt = pDstCubemap->getFormat();
const U32 dstSize = pDstCubemap->getSize();
const U32 dstMips = pDstCubemap->getMipMapLevels();
AssertFatal(dstCount > mNumCubemaps, "GFXGLCubemapArray::copyTo - Destination too small");
AssertFatal(dstFmt == mFormat, "GFXGLCubemapArray::copyTo - Destination format doesn't match");
AssertFatal(dstSize == mSize, "GFXGLCubemapArray::copyTo - Destination size doesn't match");
AssertFatal(dstMips == mMipMapLevels, "GFXGLCubemapArray::copyTo - Destination mip levels doesn't match");
GFXGLCubemapArray* pDstCube = static_cast<GFXGLCubemapArray*>(pDstCubemap);
for (U32 cubeMap = 0; cubeMap < mNumCubemaps; cubeMap++)
{
for (U32 face = 0; face < CubeFaces; face++)
{
for (U32 currentMip = 0; currentMip < mMipMapLevels; currentMip++)
//U32 currentMip = 0;
{
//U8 *pixelData = pDstCube->get->getTextureData(face, currentMip);
const U32 mipSize = getMax(U32(1), mSize >> currentMip);
/*if (isCompressed)
{
const U32 mipDataSize = getCompressedSurfaceSize(mFormat, mSize, mSize, currentMip);
glCompressedTexImage2D(faceList[face], currentMip, GFXGLTextureInternalFormat[mFormat], mipSize, mipSize, 0, mipDataSize, pixelData);
}
else
{*/ //TODO figure out xyzOffsets
glCopyImageSubData(mCubemap, GL_TEXTURE_CUBE_MAP_ARRAY, cubeMap * face, 0, 0, 0, pDstCube->mCubemap, GL_TEXTURE_CUBE_MAP_ARRAY, cubeMap * face, 0, 0, 0, mipSize, mipSize, 6);
//glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
//glTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, currentMip, 0, 0, 0, mipSize, mipSize, CubeFaces, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], pixelData);
//}
//delete[] pixelData;
}
}
}
}
void GFXGLCubemapArray::setToTexUnit(U32 tuNum)
{
static_cast<GFXGLDevice*>(getOwningDevice())->setCubemapArrayInternal(tuNum, this);
}
void GFXGLCubemapArray::bind(U32 textureUnit) const
{
glActiveTexture(GL_TEXTURE0 + textureUnit);
glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
static_cast<GFXGLDevice*>(getOwningDevice())->getOpenglCache()->setCacheBindedTex(textureUnit, GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
GFXGLStateBlockRef sb = static_cast<GFXGLDevice*>(GFX)->getCurrentStateBlock();
AssertFatal(sb, "GFXGLCubemap::bind - No active stateblock!");
if (!sb)
return;
const GFXSamplerStateDesc& ssd = sb->getDesc().samplers[textureUnit];
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, 0));
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_S, GFXGLTextureAddress[ssd.addressModeU]);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GFXGLTextureAddress[ssd.addressModeV]);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
}

View file

@ -30,6 +30,9 @@
#include "core/resource.h"
#endif
const U32 CubeFaces = 6;
const U32 MaxMipMaps = 13; //todo this needs a proper static value somewhere to sync up with other classes like GBitmap
class GFXGLCubemap : public GFXCubemap
{
@ -55,7 +58,11 @@ public:
/// Called by texCB; this is to ensure that all textures have been resurrected before we attempt to res the cubemap.
void tmResurrect();
static GLenum getEnumForFaceNumber(U32 face) { return faceList[face]; } ///< Performs lookup to get a GLenum for the given face number
static GLenum getEnumForFaceNumber(U32 face);///< Performs lookup to get a GLenum for the given face number
/// @return An array containing the texture data
/// @note You are responsible for deleting the returned data! (Use delete[])
U8* getTextureData(U32 face, U32 mip = 0);
protected:
@ -85,7 +92,29 @@ protected:
virtual void bind(U32 textureUnit) const; ///< Notifies our owning device that we want to be set to the given texture unit (used for GL internal state tracking)
void fillCubeTextures(GFXTexHandle* faces); ///< Copies the textures in faces into the cubemap
static GLenum faceList[6]; ///< Lookup table
};
class GFXGLCubemapArray : public GFXCubemapArray
{
public:
GFXGLCubemapArray();
virtual ~GFXGLCubemapArray();
//virtual void initStatic(GFXCubemapHandle *cubemaps, const U32 cubemapCount);
virtual void init(GFXCubemapHandle *cubemaps, const U32 cubemapCount);
virtual void init(const U32 cubemapCount, const U32 cubemapFaceSize, const GFXFormat format);
virtual void updateTexture(const GFXCubemapHandle &cubemap, const U32 slot);
virtual void copyTo(GFXCubemapArray *pDstCubemap);
virtual void setToTexUnit(U32 tuNum);
// GFXResource interface
virtual void zombify() {}
virtual void resurrect() {}
protected:
friend class GFXGLDevice;
void bind(U32 textureUnit) const;
GLuint mCubemap; ///< Internal GL handle
};
#endif

View file

@ -448,6 +448,13 @@ GFXCubemap* GFXGLDevice::createCubemap()
return cube;
};
GFXCubemapArray *GFXGLDevice::createCubemapArray()
{
GFXGLCubemapArray* cubeArray = new GFXGLCubemapArray();
cubeArray->registerResourceWithDevice(this);
return cubeArray;
}
void GFXGLDevice::endSceneInternal()
{
// nothing to do for opengl
@ -675,6 +682,22 @@ void GFXGLDevice::setCubemapInternal(U32 textureUnit, const GFXGLCubemap* textur
}
}
void GFXGLDevice::setCubemapArrayInternal(U32 textureUnit, const GFXGLCubemapArray* texture)
{
if (texture)
{
mActiveTextureType[textureUnit] = GL_TEXTURE_CUBE_MAP_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

@ -39,6 +39,7 @@ class GFXGLVertexBuffer;
class GFXGLPrimitiveBuffer;
class GFXGLTextureTarget;
class GFXGLCubemap;
class GFXGLCubemapArray;
class GFXGLStateCache;
class GFXGLVertexDecl;
@ -81,7 +82,7 @@ public:
virtual U32 getTotalVideoMemory();
virtual GFXCubemap * createCubemap();
virtual GFXCubemapArray *createCubemapArray() { return NULL; } //TODO: implement
virtual GFXCubemapArray *createCubemapArray();
virtual F32 getFillConventionOffset() const { return 0.0f; }
@ -170,7 +171,8 @@ protected:
virtual void setShaderConstBufferInternal(GFXShaderConstBuffer* buffer);
virtual void setTextureInternal(U32 textureUnit, const GFXTextureObject*texture);
virtual void setCubemapInternal(U32 cubemap, const GFXGLCubemap* texture);
virtual void setCubemapInternal(U32 textureUnit, const GFXGLCubemap* texture);
virtual void setCubemapArrayInternal(U32 textureUnit, const GFXGLCubemapArray* texture);
virtual void setLightInternal(U32 lightStage, const GFXLightInfo light, bool lightEnable);
virtual void setLightMaterialInternal(const GFXLightMaterial mat);
@ -207,11 +209,12 @@ private:
friend class GFXGLTextureObject;
friend class GFXGLCubemap;
friend class GFXGLCubemapArray;
friend class GFXGLWindowTarget;
friend class GFXGLPrimitiveBuffer;
friend class GFXGLVertexBuffer;
static GFXAdapter::CreateDeviceInstanceDelegate mCreateDeviceInstance;
static GFXAdapter::CreateDeviceInstanceDelegate mCreateDeviceInstance;
U32 mAdapterIndex;

View file

@ -80,6 +80,7 @@ static U32 shaderConstTypeSize(GFXShaderConstType type)
case GFXSCT_Int:
case GFXSCT_Sampler:
case GFXSCT_SamplerCube:
case GFXSCT_SamplerCubeArray:
return 4;
case GFXSCT_Float2:
case GFXSCT_Int2:
@ -625,6 +626,9 @@ void GFXGLShader::initConstantDescs()
case GL_SAMPLER_CUBE:
desc.constType = GFXSCT_SamplerCube;
break;
case GL_SAMPLER_CUBE_MAP_ARRAY:
desc.constType = GFXSCT_SamplerCubeArray;
break;
default:
AssertFatal(false, "GFXGLShader::initConstantDescs - unrecognized uniform type");
// If we don't recognize the constant don't add its description.
@ -656,7 +660,7 @@ void GFXGLShader::initHandles()
HandleMap::Iterator handle = mHandles.find(desc.name);
S32 sampler = -1;
if(desc.constType == GFXSCT_Sampler || desc.constType == GFXSCT_SamplerCube)
if(desc.constType == GFXSCT_Sampler || desc.constType == GFXSCT_SamplerCube || desc.constType == GFXSCT_SamplerCubeArray)
{
S32 idx = mSamplerNamesOrdered.find_next(desc.name);
AssertFatal(idx != -1, "");
@ -699,7 +703,7 @@ 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))
if(handle->isValid() && (handle->getType() == GFXSCT_Sampler || handle->getType() == GFXSCT_SamplerCube || handle->getType() == GFXSCT_SamplerCubeArray))
{
// Set sampler number on our program.
glUniform1i(handle->mLocation, handle->mSamplerNum);
@ -831,6 +835,7 @@ void GFXGLShader::setConstantsFromBuffer(GFXGLShaderConstBuffer* buffer)
case GFXSCT_Int:
case GFXSCT_Sampler:
case GFXSCT_SamplerCube:
case GFXSCT_SamplerCubeArray:
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)
TextureUnit() : mTexture1D(0), mTexture2D(0), mTexture3D(0), mTextureCube(0), mTextureCubeArray(0)
{
}
GLuint mTexture1D, mTexture2D, mTexture3D, mTextureCube;
GLuint mTexture1D, mTexture2D, mTexture3D, mTextureCube, mTextureCubeArray;
};
/// after glBindTexture
@ -45,6 +45,9 @@ public:
case GL_TEXTURE_CUBE_MAP:
mTextureUnits[mActiveTexture].mTextureCube = handle;
break;
case GL_TEXTURE_CUBE_MAP_ARRAY:
mTextureUnits[mActiveTexture].mTextureCubeArray = handle;
break;
default:
AssertFatal(0, avar("GFXGLStateCache::setCacheBindedTex - binding (%x) not supported.", biding) );
return;
@ -68,6 +71,9 @@ public:
case GL_TEXTURE_CUBE_MAP:
mTextureUnits[mActiveTexture].mTextureCube = handle;
break;
case GL_TEXTURE_CUBE_MAP_ARRAY:
mTextureUnits[mActiveTexture].mTextureCubeArray = handle;
break;
case GL_FRAMEBUFFER:
mBindedFBO_W = mBindedFBO_R = handle;
break;
@ -101,6 +107,8 @@ public:
return mTextureUnits[mActiveTexture].mTexture1D;
case GL_TEXTURE_CUBE_MAP:
return mTextureUnits[mActiveTexture].mTextureCube;
case GL_TEXTURE_CUBE_MAP_ARRAY:
return mTextureUnits[mActiveTexture].mTextureCubeArray;
case GL_DRAW_FRAMEBUFFER:
return mBindedFBO_W;
case GL_READ_FRAMEBUFFER:

View file

@ -191,6 +191,9 @@ GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_3D, GL_TEXTU
#define PRESERVE_CUBEMAP_TEXTURE() \
GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP, (GFXGLPreserveInteger::BindFn)glBindTexture)
#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 _GET_TEXTURE_BINDING(binding) \
binding == GL_TEXTURE_2D ? GL_TEXTURE_BINDING_2D : (binding == GL_TEXTURE_3D ? GL_TEXTURE_BINDING_3D : GL_TEXTURE_BINDING_1D )

View file

@ -423,7 +423,7 @@ void RenderProbeMgr::_setupStaticParameters()
if (!curEntry.mIsEnabled)
continue;
if (curEntry.mIsSkylight)
if (curEntry.mProbeShapeType == ProbeRenderInst::ProbeShapeType::Skylight || curEntry.mIsSkylight)
{
skylightPos = curEntry.getPosition();
skylightPrefilterMap = curEntry.mPrefilterCubemap;
@ -680,11 +680,13 @@ void RenderProbeMgr::_update4ProbeConsts(const SceneData &sgData,
{
if (curEntry.mPrefilterCubemap.isValid() && curEntry.mPrefilterCubemap.isValid())
{
U32 specSample = probeShaderConsts->mSkylightSpecularMap->getSamplerRegister();
U32 irradSample = probeShaderConsts->mSkylightIrradMap->getSamplerRegister();
S32 specSample = probeShaderConsts->mSkylightSpecularMap->getSamplerRegister();
if (specSample != -1)
GFX->setCubeTexture(specSample, curEntry.mPrefilterCubemap);
GFX->setCubeTexture(probeShaderConsts->mSkylightSpecularMap->getSamplerRegister(), curEntry.mPrefilterCubemap);
GFX->setCubeTexture(probeShaderConsts->mSkylightIrradMap->getSamplerRegister(), curEntry.mIrradianceCubemap);
S32 irradSample = probeShaderConsts->mSkylightIrradMap->getSamplerRegister();
if (irradSample != -1)
GFX->setCubeTexture(irradSample, curEntry.mIrradianceCubemap);
shaderConsts->setSafe(probeShaderConsts->mHasSkylight, 1.0f);
hasSkylight = true;
@ -718,8 +720,13 @@ void RenderProbeMgr::_update4ProbeConsts(const SceneData &sgData,
shaderConsts->setSafe(probeShaderConsts->mProbeBoxMaxSC, probeBoxMaxArray);
shaderConsts->setSafe(probeShaderConsts->mProbeConfigDataSC, probeConfigArray);
GFX->setCubeArrayTexture(probeShaderConsts->mProbeSpecularCubemapSC->getSamplerRegister(), mPrefilterArray);
GFX->setCubeArrayTexture(probeShaderConsts->mProbeIrradianceCubemapSC->getSamplerRegister(), mIrradianceArray);
S32 specSample = probeShaderConsts->mProbeSpecularCubemapSC->getSamplerRegister();
if (specSample != -1)
GFX->setCubeArrayTexture(specSample, mPrefilterArray);
S32 irradSample = probeShaderConsts->mProbeIrradianceCubemapSC->getSamplerRegister();
if (irradSample != -1)
GFX->setCubeArrayTexture(irradSample, mIrradianceArray);
if (!hasSkylight)
shaderConsts->setSafe(probeShaderConsts->mHasSkylight, 0.0f);
@ -779,8 +786,7 @@ void RenderProbeMgr::render( SceneRenderState *state )
//updateProbes();
// Early out if nothing to draw.
if (!ProbeRenderInst::all.size() || !RenderProbeMgr::smRenderReflectionProbes || !state->isDiffusePass() || (mEffectiveProbeCount == 0
|| mCubeMapCount != 0 && !hasSkylight))
if (!RenderProbeMgr::smRenderReflectionProbes || !state->isDiffusePass() || (!ProbeRenderInst::all.size() || mEffectiveProbeCount == 0 || mCubeMapCount != 0 ) && !hasSkylight)
{
getProbeArrayEffect()->setSkip(true);
return;
@ -793,6 +799,19 @@ void RenderProbeMgr::render( SceneRenderState *state )
// Initialize and set the per-frame parameters after getting
// the vector light material as we use lazy creation.
//_setupPerFrameParameters(state);
//Visualization
String useDebugAtten = Con::getVariable("$Probes::showAttenuation", "0");
mProbeArrayEffect->setShaderMacro("DEBUGVIZ_ATTENUATION", useDebugAtten);
String useDebugSpecCubemap = Con::getVariable("$Probes::showSpecularCubemaps", "0");
mProbeArrayEffect->setShaderMacro("DEBUGVIZ_SPECCUBEMAP", useDebugSpecCubemap);
String useDebugDiffuseCubemap = Con::getVariable("$Probes::showDiffuseCubemaps", "0");
mProbeArrayEffect->setShaderMacro("DEBUGVIZ_DIFFCUBEMAP", useDebugDiffuseCubemap);
String useDebugContrib = Con::getVariable("$Probes::showProbeContrib", "0");
mProbeArrayEffect->setShaderMacro("DEBUGVIZ_CONTRIB", useDebugContrib);
//Array rendering
//U32 probeCount = ProbeRenderInst::all.size();
@ -804,23 +823,14 @@ void RenderProbeMgr::render( SceneRenderState *state )
mProbeArrayEffect->setCubemapTexture(7, skylightIrradMap);
}
mProbeArrayEffect->setShaderConst("$numProbes", (float)mEffectiveProbeCount);
mProbeArrayEffect->setShaderConst("$cubeMips", (float)mMipCount);
if (mEffectiveProbeCount != 0)
{
mProbeArrayEffect->setCubemapArrayTexture(4, mPrefilterArray);
mProbeArrayEffect->setCubemapArrayTexture(5, mIrradianceArray);
String useDebugAtten = Con::getVariable("$Probes::showAttenuation", "0");
mProbeArrayEffect->setShaderMacro("DEBUGVIZ_ATTENUATION", useDebugAtten);
String useDebugSpecCubemap = Con::getVariable("$Probes::showSpecularCubemaps", "0");
mProbeArrayEffect->setShaderMacro("DEBUGVIZ_SPECCUBEMAP", useDebugSpecCubemap);
String useDebugDiffuseCubemap = Con::getVariable("$Probes::showDiffuseCubemaps", "0");
mProbeArrayEffect->setShaderMacro("DEBUGVIZ_DIFFCUBEMAP", useDebugDiffuseCubemap);
String useDebugContrib = Con::getVariable("$Probes::showProbeContrib", "0");
mProbeArrayEffect->setShaderMacro("DEBUGVIZ_CONTRIB", useDebugContrib);
if (useDebugContrib == String("1"))
{
MRandomLCG RandomGen;
@ -841,15 +851,12 @@ void RenderProbeMgr::render( SceneRenderState *state )
else if (i == 2)
contribColors[i] = Point4F(0, 0, 1, 1);
else
contribColors[i] = Point4F(RandomGen.randF(0, 1), RandomGen.randF(0, 1), RandomGen.randF(0, 1),1);
contribColors[i] = Point4F(RandomGen.randF(0, 1), RandomGen.randF(0, 1), RandomGen.randF(0, 1), 1);
}
mProbeArrayEffect->setShaderConst("$probeContribColors", contribColors);
}
mProbeArrayEffect->setShaderConst("$cubeMips", (float)mMipCount);
mProbeArrayEffect->setShaderConst("$numProbes", (float)mEffectiveProbeCount);
mProbeArrayEffect->setShaderConst("$inProbePosArray", probePositionsData);
mProbeArrayEffect->setShaderConst("$inRefPosArray", probeRefPositionsData);
mProbeArrayEffect->setShaderConst("$worldToObjArray", probeWorldToObjData);

View file

@ -36,6 +36,11 @@ singleton Material( Grid512_Blue_Mat )
metalness[0] = "0.803922";
translucent = "1";
translucentBlendOp = "Add";
normalMap[0] = "art/pbr/floor/FloorEbony_normal.png";
invertSmoothness[0] = "1";
roughMap[0] = "art/pbr/floor/FloorEbony_rough.png";
aoMap[0] = "art/pbr/floor/FloorEbony_ao.png";
metalMap[0] = "art/pbr/floor/FloorEbony_metal.png";
};
singleton Material( Grid512_ForestGreen_Mat )

View file

@ -310,15 +310,17 @@ singleton ShaderData( PFX_ReflectionProbeArray )
DXVertexShaderFile = "shaders/common/postFx/postFxV.hlsl";
DXPixelShaderFile = "shaders/common/lighting/advanced/reflectionProbeArrayP.hlsl";
//OGLVertexShaderFile = "shaders/common/postFx/gl//postFxV.glsl";
//OGLPixelShaderFile = "shaders/common/postFx/gl/passthruP.glsl";
OGLVertexShaderFile = "shaders/common/postFx/gl/postFxV.glsl";
OGLPixelShaderFile = "shaders/common/lighting/advanced/gl/reflectionProbeArrayP.glsl";
samplerNames[0] = "$deferredBuffer";
samplerNames[1] = "$colorBuffer";
samplerNames[2] = "$matInfoBuffer";
samplerNames[3] = "$BRDFTexture";
samplerNames[4] = "$cubeMap";
samplerNames[5] = "$irradianceCubemap";
samplerNames[4] = "$specularCubemapAR";
samplerNames[5] = "$irradianceCubemapAR";
samplerNames[6] = "$skylightSpecularMap";
samplerNames[7] = "$skylightIrradMap";
pixVersion = 2.0;
};

View file

@ -99,18 +99,17 @@ new SimGroup(MissionGroup) {
};
new Skylight(theSkyLight) {
enabled = "1";
ProbeShape = "Box";
radius = "10";
posOffset = "0 0 0";
ReflectionMode = "Baked Cubemap";
reflectionPath = "levels/Timmy Test/probes/";
Bake = "0";
position = "8.74661 10.1457 2.48852";
position = "8.74661 10.1457 2.94337";
rotation = "1 0 0 0";
scale = "1 1 1";
canSave = "1";
canSaveDynamicFields = "1";
persistentId = "247d7009-db8a-11e8-87b8-ed691a78e155";
posOffset = "0 0 0";
ProbeShape = "Box";
radius = "10";
reflectionPath = "levels/Timmy Test/probes/";
scale = "1 1 1";
};
new TSStatic() {
shapeName = "art/shapes/material_ball/material_ball.dae";
@ -351,5 +350,33 @@ new SimGroup(MissionGroup) {
canSave = "1";
canSaveDynamicFields = "1";
};
new ConvexShape() {
Material = "Grid512_Blue_Mat";
position = "-6.41329 13.7734 2.12278";
rotation = "0 0 1 3.19212";
scale = "1 1 1";
canSave = "1";
canSaveDynamicFields = "1";
surface = "0 0 0 1 0 0 0.384079";
surface = "0 1 0 0 0 0 -0.384079";
surface = "0.707107 0 0 0.707107 0 0.285194 0";
surface = "0 0.707107 -0.707107 0 0 -0.285194 -1.42109e-14";
surface = "0.5 0.5 -0.5 0.5 -0.758333 0 -1.07696e-07";
surface = "0.5 -0.5 0.5 0.5 0.758333 0 -1.07696e-07";
};
new BoxEnvironmentProbe() {
enabled = "0";
refOffset = "0 0 0";
refScale = "10 10 10";
ReflectionMode = "Baked Cubemap";
position = "-11.7544 15.2634 4.67576";
rotation = "1 0 0 0";
scale = "10 10 10";
canSave = "1";
canSaveDynamicFields = "1";
persistentId = "8c7e1f23-5f1c-11e9-8089-c88cdaba85a3";
attenuation = "1";
};
};
//--- OBJECT WRITE END ---

View file

@ -44,6 +44,9 @@ uniform vec4 albedo;
#endif // !TORQUE_SHADERGEN
#define MAX_PROBES 50
#define MAX_FORWARD_PROBES 4
vec3 getDistanceVectorToPlane( vec3 origin, vec3 direction, vec4 plane )
{
float denum = dot( plane.xyz, direction.xyz );
@ -119,15 +122,15 @@ void Surface::Update()
F = F_Schlick(f0, f90, NdotV);
}
Surface createSurface(vec4 gbuffer0, sampler2D gbufferTex1, sampler2D gbufferTex2, in vec2 uv, in vec3 wsEyePos, in vec3 wsEyeRay, in mat4 invView)
Surface createSurface(vec4 normDepth, sampler2D colorBuffer, sampler2D matInfoBuffer, in vec2 uv, in vec3 wsEyePos, in vec3 wsEyeRay, in mat4 invView)
{
Surface surface;// = Surface();
vec4 gbuffer1 = texture(gbufferTex1, uv);
vec4 gbuffer2 = texture(gbufferTex2, uv);
surface.depth = gbuffer0.a;
vec4 gbuffer1 = texture(colorBuffer, uv);
vec4 gbuffer2 = texture(matInfoBuffer, uv);
surface.depth = normDepth.a;
surface.P = wsEyePos + wsEyeRay * surface.depth;
surface.N = tMul(invView, vec4(gbuffer0.xyz,0)).xyz; //TODO move t3d to use WS normals
surface.N = tMul(invView, vec4(normDepth.xyz,0)).xyz; //TODO move t3d to use WS normals
surface.V = normalize(wsEyePos - surface.P);
surface.baseColor = gbuffer1;
const float minRoughness=1e-4;
@ -266,4 +269,158 @@ vec3 directSpecular(vec3 N, vec3 V, vec3 L, float roughness, float F0)
float specular = dotNL * D * F * vis;
return vec3(specular,specular,specular);
}
}
//Probe IBL stuff
float defineSphereSpaceInfluence(vec3 wsPosition, vec3 wsProbePosition, float radius)
{
vec3 L = wsProbePosition.xyz - wsPosition;
float contribution = 1.0 - length(L) / radius;
return contribution;
}
float getDistBoxToPoint(vec3 pt, vec3 extents)
{
vec3 d = max(max(-extents - pt, 0), pt - extents);
return max(max(d.x, d.y), d.z);
}
float defineBoxSpaceInfluence(vec3 wsPosition, mat4 worldToObj, float attenuation)
{
vec3 surfPosLS = tMul(worldToObj, vec4(wsPosition, 1.0)).xyz;
float atten = 1.0 - attenuation;
float baseVal = 0.25;
float dist = getDistBoxToPoint(surfPosLS, vec3(baseVal, baseVal, baseVal));
return saturate(smoothstep(baseVal + 0.0001, atten*baseVal, dist));
}
// Box Projected IBL Lighting
// Based on: http://www.gamedev.net/topic/568829-box-projected-cubemap-environment-mapping/
// and https://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/
vec3 boxProject(vec3 wsPosition, vec3 wsReflectVec, mat4 worldToObj, vec3 bbMin, vec3 bbMax, vec3 refPosition)
{
vec3 RayLS = tMul(worldToObj, vec4(wsReflectVec, 0.0)).xyz;
vec3 PositionLS = tMul(worldToObj, vec4(wsPosition, 1.0)).xyz;
vec3 unit = bbMax.xyz - bbMin.xyz;
vec3 plane1vec = (unit / 2 - PositionLS) / RayLS;
vec3 plane2vec = (-unit / 2 - PositionLS) / RayLS;
vec3 furthestPlane = max(plane1vec, plane2vec);
float dist = min(min(furthestPlane.x, furthestPlane.y), furthestPlane.z);
vec3 posonbox = wsPosition + wsReflectVec * dist;
return posonbox - refPosition.xyz;
}
/*vec4 computeForwardProbes(Surface surface,
float cubeMips, float numProbes, mat4 worldToObjArray[MAX_FORWARD_PROBES], vec4 probeConfigData[MAX_FORWARD_PROBES],
vec4 inProbePosArray[MAX_FORWARD_PROBES], vec4 bbMinArray[MAX_FORWARD_PROBES], vec4 bbMaxArray[MAX_FORWARD_PROBES], vec4 inRefPosArray[MAX_FORWARD_PROBES],
float hasSkylight, samplerCube skylightIrradMap, samplerCube skylightSpecularMap,
sampler2D BRDFTexture, samplerCubeArray irradianceCubemapAR,
samplerCubeArray specularCubemapAR)
{
return vec4(0,0,0,1);
int i = 0;
float blendFactor[MAX_FORWARD_PROBES];
float blendSum = 0;
float blendFacSum = 0;
float invBlendSum = 0;
float probehits = 0;
//Set up our struct data
float contribution[MAX_FORWARD_PROBES];
for (i = 0; i < numProbes; ++i)
{
contribution[i] = 0;
if (probeConfigData[i].r == 0) //box
{
contribution[i] = defineBoxSpaceInfluence(surface.P, worldToObjArray[i], probeConfigData[i].b);
if (contribution[i] > 0.0)
probehits++;
}
else if (probeConfigData[i].r == 1) //sphere
{
contribution[i] = defineSphereSpaceInfluence(surface.P, inProbePosArray[i].xyz, probeConfigData[i].g);
if (contribution[i] > 0.0)
probehits++;
}
contribution[i] = max(contribution[i], 0);
blendSum += contribution[i];
invBlendSum += (1.0f - contribution[i]);
}
if (probehits > 1.0)
{
for (i = 0; i < numProbes; i++)
{
blendFactor[i] = ((contribution[i] / blendSum)) / probehits;
blendFactor[i] *= ((contribution[i]) / invBlendSum);
blendFactor[i] = saturate(blendFactor[i]);
blendFacSum += blendFactor[i];
}
// Normalize blendVal
if (blendFacSum == 0.0f) // Possible with custom weight
{
blendFacSum = 1.0f;
}
float invBlendSumWeighted = 1.0f / blendFacSum;
for (i = 0; i < numProbes; ++i)
{
blendFactor[i] *= invBlendSumWeighted;
contribution[i] *= blendFactor[i];
//alpha -= contribution[i];
}
}
//else
// alpha -= blendSum;
vec3 irradiance = vec3(0, 0, 0);
vec3 specular = vec3(0, 0, 0);
// Radiance (Specular)
float lod = surface.roughness*cubeMips;
float alpha = 1;
for (i = 0; i < numProbes; ++i)
{
float contrib = contribution[i];
if (contrib != 0)
{
int cubemapIdx = probeConfigData[i].a;
vec3 dir = boxProject(surface.P, surface.R, worldToObjArray[i], bbMinArray[i].xyz, bbMaxArray[i].xyz, inRefPosArray[i].xyz);
irradiance += textureLod(irradianceCubemapAR, vec4(dir, cubemapIdx), 0).xyz * contrib;
specular += textureLod(specularCubemapAR, vec4(dir, cubemapIdx), lod).xyz * contrib;
alpha -= contrib;
}
}
if (hasSkylight == 1 && alpha > 0.001)
{
irradiance += textureLod(skylightIrradMap, surface.R, 0).xyz * alpha;
specular += textureLod(skylightSpecularMap, surface.R, lod).xyz * alpha;
}
vec3 F = FresnelSchlickRoughness(surface.NdotV, surface.f0, surface.roughness);
//energy conservation
vec3 kD = 1.0.xxx - F;
kD *= 1.0 - surface.metalness;
//apply brdf
//Do it once to save on texture samples
vec2 brdf = texture(BRDFTexture, vec2(surface.roughness, surface.NdotV)).xy;
specular *= brdf.x * F + brdf.y;
//final diffuse color
vec3 diffuse = kD * irradiance * surface.baseColor.rgb;
vec4 finalColor = vec4(diffuse + specular * surface.ao, 1.0);
finalColor = vec4(irradiance.rgb,1);
return finalColor;
}*/

View file

@ -1,63 +1,212 @@
//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------
#include "../../../gl/hlslCompat.glsl"
#include "../../../postFx/gl/postFx.glsl"
#include "../../../gl/torque.glsl"
#include "shadergen:/autogenConditioners.h"
#include "../../../gl/lighting.glsl"
#include "../../torque.hlsl"
#line 7
struct ConnectData
uniform sampler2D deferredBuffer;
uniform sampler2D colorBuffer;
uniform sampler2D matInfoBuffer;
uniform sampler2D BRDFTexture;
uniform vec4 rtParams0;
uniform vec4 vsFarPlane;
uniform mat4 cameraToWorld;
uniform vec3 eyePosWorld;
//cubemap arrays require all the same size. so shared mips# value
uniform float cubeMips;
uniform float numProbes;
uniform samplerCubeArray specularCubemapAR;
uniform samplerCubeArray irradianceCubemapAR;
uniform vec4 inProbePosArray[MAX_PROBES];
uniform vec4 inRefPosArray[MAX_PROBES];
uniform mat4 worldToObjArray[MAX_PROBES];
uniform vec4 bbMinArray[MAX_PROBES];
uniform vec4 bbMaxArray[MAX_PROBES];
uniform vec4 probeConfigData[MAX_PROBES]; //r,g,b/mode,radius,atten
#if DEBUGVIZ_CONTRIB
uniform vec4 probeContribColors[MAX_PROBES];
#endif
uniform samplerCube skylightSpecularMap;
uniform samplerCube skylightIrradMap;
uniform float hasSkylight;
out vec4 OUT_col;
void main()
{
float4 hpos : TORQUE_POSITION;
float2 uv : TEXCOORD;
};
//unpack normal and linear depth
vec4 normDepth = deferredUncondition(deferredBuffer, IN_uv0.xy);
uniform int face;
//create surface
Surface surface = createSurface(normDepth, colorBuffer, matInfoBuffer, IN_uv0.xy, eyePosWorld, IN_wsEyeRay, cameraToWorld);
TORQUE_UNIFORM_SAMPLERCUBE(environmentMap, 0);
//early out if emissive
if (getFlag(surface.matFlag, 0))
{
discard;
}
float4 main(ConnectData IN) : TORQUE_TARGET0
{
float3 N = getCubeDir(face,IN.uv);
float3 irradiance = 0;
// tangent space calculation from origin point
float3 up = float3(0.0, 0.0, 1.0);
float3 right = cross(up, N);
up = cross(N, right);
float sampleDelta = 0.025;
int nrSamples = 0;
for(float phi = 0.0; phi < M_2PI_F; phi += sampleDelta)
{
for(float theta = 0.0; theta < M_HALFPI_F; theta += sampleDelta)
{
// spherical to cartesian (in tangent space)
float3 tangentSample = float3(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta));
// tangent space to world
float3 sampleVec = tangentSample.x * right + tangentSample.y * up + tangentSample.z * N;
float alpha = 1;
irradiance += TORQUE_TEXCUBE(environmentMap, sampleVec).rgb * cos(theta) * sin(theta);
nrSamples++;
}
}
irradiance = M_PI_F * irradiance * (1.0 / float(nrSamples));
return float4(irradiance, 1.0);
}
int i = 0;
float blendFactor[MAX_PROBES];
float blendSum = 0;
float blendFacSum = 0;
float invBlendSum = 0;
float probehits = 0;
//Set up our struct data
float contribution[MAX_PROBES];
if (alpha > 0)
{
//Process prooooobes
for (i = 0; i < numProbes; ++i)
{
contribution[i] = 0;
if (probeConfigData[i].r == 0) //box
{
contribution[i] = defineBoxSpaceInfluence(surface.P, worldToObjArray[i], probeConfigData[i].b);
if (contribution[i]>0.0)
probehits++;
}
else if (probeConfigData[i].r == 1) //sphere
{
contribution[i] = defineSphereSpaceInfluence(surface.P, inProbePosArray[i].xyz, probeConfigData[i].g);
if (contribution[i]>0.0)
probehits++;
}
contribution[i] = max(contribution[i],0);
blendSum += contribution[i];
invBlendSum += (1.0f - contribution[i]);
}
// Weight0 = normalized NDF, inverted to have 1 at center, 0 at boundary.
// And as we invert, we need to divide by Num-1 to stay normalized (else sum is > 1).
// respect constraint B.
// Weight1 = normalized inverted NDF, so we have 1 at center, 0 at boundary
// and respect constraint A.
if (probehits>1.0)
{
for (i = 0; i < numProbes; i++)
{
blendFactor[i] = ((contribution[i] / blendSum)) / probehits;
blendFactor[i] *= ((contribution[i]) / invBlendSum);
blendFactor[i] = saturate(blendFactor[i]);
blendFacSum += blendFactor[i];
}
// Normalize blendVal
#if DEBUGVIZ_ATTENUATION == 0 //this can likely be removed when we fix the above normalization behavior
if (blendFacSum == 0.0f) // Possible with custom weight
{
blendFacSum = 1.0f;
}
#endif
float invBlendSumWeighted = 1.0f / blendFacSum;
for (i = 0; i < numProbes; ++i)
{
blendFactor[i] *= invBlendSumWeighted;
contribution[i] *= blendFactor[i];
alpha -= contribution[i];
}
}
else
alpha -= blendSum;
#if DEBUGVIZ_ATTENUATION == 1
float contribAlpha = 1;
for (i = 0; i < numProbes; ++i)
{
contribAlpha -= contribution[i];
}
OUT_col = vec4(1 - contribAlpha, 1 - contribAlpha, 1 - contribAlpha, 1);
return;
#endif
#if DEBUGVIZ_CONTRIB == 1
vec3 finalContribColor = vec3(0, 0, 0);
float contribAlpha = 1;
for (i = 0; i < numProbes; ++i)
{
finalContribColor += contribution[i] *probeContribColors[i].rgb;
contribAlpha -= contribution[i];
}
//Skylight coloration for anything not covered by probes above
finalContribColor += vec3(0.3, 0.3, 0.3) * contribAlpha;
OUT_col = vec4(finalContribColor, 1);
return;
#endif
}
vec3 irradiance = vec3(0, 0, 0);
vec3 specular = vec3(0, 0, 0);
// Radiance (Specular)
#if DEBUGVIZ_SPECCUBEMAP == 0
float lod = surface.roughness*cubeMips;
#elif DEBUGVIZ_SPECCUBEMAP == 1
float lod = 0;
#endif
alpha = 1;
for (i = 0; i < numProbes; ++i)
{
float contrib = contribution[i];
if (contrib != 0)
{
float cubemapIdx = probeConfigData[i].a;
vec3 dir = boxProject(surface.P, surface.R, worldToObjArray[i], bbMinArray[i].xyz, bbMaxArray[i].xyz, inRefPosArray[i].xyz);
//irradiance += textureLod(irradianceCubemapAR, vec4(dir, cubemapIdx), 0).xyz * contrib;
//specular += textureLod(specularCubemapAR, vec4(dir, cubemapIdx), lod).xyz * contrib;
irradiance += vec3(1,1,1) * contrib;
specular += vec3(1,1,1) * contrib;
alpha -= contrib;
}
}
if (hasSkylight == 1 && alpha > 0.001)
{
irradiance += textureLod(skylightIrradMap, surface.R, 0).xyz * alpha;
specular += textureLod(skylightSpecularMap, surface.R, lod).xyz * alpha;
}
#if DEBUGVIZ_SPECCUBEMAP == 1 && DEBUGVIZ_DIFFCUBEMAP == 0
OUT_col = vec4(specular, 1);
return;
#elif DEBUGVIZ_DIFFCUBEMAP == 1
OUT_col = vec4(irradiance, 1);
return;
#endif
vec3 F = FresnelSchlickRoughness(surface.NdotV, surface.f0, surface.roughness);
//energy conservation
vec3 kD = vec3(1,1,1) - F;
kD *= 1.0 - surface.metalness;
//apply brdf
//Do it once to save on texture samples
vec2 brdf = texture(BRDFTexture, vec2(surface.roughness, surface.NdotV)).xy;
specular *= brdf.x * F + brdf.y;
//final diffuse color
vec3 diffuse = kD * irradiance * surface.baseColor.rgb;
vec4 finalColor = vec4(diffuse + specular * surface.ao, 1.0);
OUT_col = finalColor;
}

1
gl/Timmy Note.txt Normal file
View file

@ -0,0 +1 @@
idiot me used glTexImage2D for the cubemap array code, obviously should be glTexImage3D

389
gl/gfxGLCubemap.cpp Normal file
View file

@ -0,0 +1,389 @@
//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------
#include "gfx/gl/gfxGLDevice.h"
#include "gfx/gl/gfxGLTextureObject.h"
#include "gfx/gl/gfxGLEnumTranslate.h"
#include "gfx/gl/gfxGLUtils.h"
#include "gfx/gl/gfxGLCubemap.h"
#include "gfx/gfxTextureManager.h"
#include "gfx/gfxCardProfile.h"
#include "gfx/bitmap/ddsFile.h"
#include "gfx/bitmap/imageUtils.h"
static GLenum faceList[6] =
{
GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
};
GFXGLCubemap::GFXGLCubemap() :
mCubemap(0),
mDynamicTexSize(0),
mFaceFormat( GFXFormatR8G8B8A8 )
{
for(U32 i = 0; i < 6; i++)
mTextures[i] = NULL;
GFXTextureManager::addEventDelegate( this, &GFXGLCubemap::_onTextureEvent );
}
GFXGLCubemap::~GFXGLCubemap()
{
glDeleteTextures(1, &mCubemap);
GFXTextureManager::removeEventDelegate( this, &GFXGLCubemap::_onTextureEvent );
}
GLenum GFXGLCubemap::getEnumForFaceNumber(U32 face)
{
return faceList[face];
}
void GFXGLCubemap::fillCubeTextures(GFXTexHandle* faces)
{
AssertFatal( faces, "");
AssertFatal( faces[0]->mMipLevels > 0, "");
PRESERVE_CUBEMAP_TEXTURE();
glBindTexture(GL_TEXTURE_CUBE_MAP, mCubemap);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, faces[0]->mMipLevels - 1 );
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
U32 reqWidth = faces[0]->getWidth();
U32 reqHeight = faces[0]->getHeight();
GFXFormat regFaceFormat = faces[0]->getFormat();
const bool isCompressed = ImageUtil::isCompressedFormat(regFaceFormat);
mWidth = reqWidth;
mHeight = reqHeight;
mFaceFormat = regFaceFormat;
mMipMapLevels = getMax( (U32)1, faces[0]->mMipLevels);
AssertFatal(reqWidth == reqHeight, "GFXGLCubemap::fillCubeTextures - Width and height must be equal!");
for(U32 i = 0; i < 6; i++)
{
AssertFatal(faces[i], avar("GFXGLCubemap::fillCubeFaces - texture %i is NULL!", i));
AssertFatal((faces[i]->getWidth() == reqWidth) && (faces[i]->getHeight() == reqHeight), "GFXGLCubemap::fillCubeFaces - All textures must have identical dimensions!");
AssertFatal(faces[i]->getFormat() == regFaceFormat, "GFXGLCubemap::fillCubeFaces - All textures must have identical formats!");
mTextures[i] = faces[i];
GFXFormat faceFormat = faces[i]->getFormat();
GFXGLTextureObject* glTex = static_cast<GFXGLTextureObject*>(faces[i].getPointer());
if( isCompressed )
{
for( U32 mip = 0; mip < mMipMapLevels; ++mip )
{
const U32 mipWidth = getMax( U32(1), faces[i]->getWidth() >> mip );
const U32 mipHeight = getMax( U32(1), faces[i]->getHeight() >> mip );
const U32 mipDataSize = getCompressedSurfaceSize( mFaceFormat, mWidth, mHeight, mip );
U8* buf = glTex->getTextureData( mip );
glCompressedTexImage2D(faceList[i], mip, GFXGLTextureInternalFormat[mFaceFormat], mipWidth, mipHeight, 0, mipDataSize, buf);
delete[] buf;
}
}
else
{
U8* buf = glTex->getTextureData();
glTexImage2D(faceList[i], 0, GFXGLTextureInternalFormat[faceFormat], mWidth, mHeight,
0, GFXGLTextureFormat[faceFormat], GFXGLTextureType[faceFormat], buf);
delete[] buf;
}
}
if( !isCompressed )
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
}
void GFXGLCubemap::initStatic(GFXTexHandle* faces)
{
if(mCubemap)
return;
if(faces)
{
AssertFatal(faces[0], "GFXGLCubemap::initStatic - empty texture passed");
glGenTextures(1, &mCubemap);
fillCubeTextures(faces);
}
}
void GFXGLCubemap::initStatic( DDSFile *dds )
{
if(mCubemap)
return;
AssertFatal( dds, "GFXGLCubemap::initStatic - Got null DDS file!" );
AssertFatal( dds->isCubemap(), "GFXGLCubemap::initStatic - Got non-cubemap DDS file!" );
AssertFatal( dds->mSurfaces.size() == 6, "GFXGLCubemap::initStatic - DDS has less than 6 surfaces!" );
mWidth = dds->getWidth();
mHeight = dds->getHeight();
mFaceFormat = dds->getFormat();
mMipMapLevels = dds->getMipLevels();
const bool isCompressed = ImageUtil::isCompressedFormat(mFaceFormat);
glGenTextures(1, &mCubemap);
PRESERVE_CUBEMAP_TEXTURE();
glBindTexture(GL_TEXTURE_CUBE_MAP, mCubemap);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, mMipMapLevels - 1);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
AssertFatal(mWidth == mHeight, "GFXGLCubemap::initStatic - Width and height must be equal!");
for(U32 i = 0; i < 6; i++)
{
if ( !dds->mSurfaces[i] )
{
// TODO: The DDS can skip surfaces, but i'm unsure what i should
// do here when creating the cubemap. Ignore it for now.
continue;
}
// convert to Z up
const U32 faceIndex = _zUpFaceIndex(i);
// Now loop thru the mip levels!
for (U32 mip = 0; mip < mMipMapLevels; ++mip)
{
const U32 mipWidth = getMax( U32(1), mWidth >> mip );
const U32 mipHeight = getMax( U32(1), mHeight >> mip );
if (isCompressed)
glCompressedTexImage2D(faceList[faceIndex], mip, GFXGLTextureInternalFormat[mFaceFormat], mipWidth, mipHeight, 0, dds->getSurfaceSize(mip), dds->mSurfaces[i]->mMips[mip]);
else
glTexImage2D(faceList[faceIndex], mip, GFXGLTextureInternalFormat[mFaceFormat], mipWidth, mipHeight, 0,
GFXGLTextureFormat[mFaceFormat], GFXGLTextureType[mFaceFormat], dds->mSurfaces[i]->mMips[mip]);
}
}
}
void GFXGLCubemap::initDynamic(U32 texSize, GFXFormat faceFormat, U32 mipLevels)
{
mDynamicTexSize = texSize;
mFaceFormat = faceFormat;
const bool isCompressed = ImageUtil::isCompressedFormat(faceFormat);
mMipMapLevels = getMax( (U32)1, getMaxMipmaps( texSize, texSize, 1 ) );
glGenTextures(1, &mCubemap);
PRESERVE_CUBEMAP_TEXTURE();
glBindTexture(GL_TEXTURE_CUBE_MAP, mCubemap);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, mMipMapLevels - 1);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
mWidth = texSize;
mHeight = texSize;
for(U32 i = 0; i < 6; i++)
{
if( ImageUtil::isCompressedFormat(faceFormat) )
{
for( U32 mip = 0; mip < mMipMapLevels; ++mip )
{
const U32 mipSize = getMax( U32(1), texSize >> mip );
const U32 mipDataSize = getCompressedSurfaceSize( mFaceFormat, texSize, texSize, mip );
glCompressedTexImage2D(faceList[i], mip, GFXGLTextureInternalFormat[mFaceFormat], mipSize, mipSize, 0, mipDataSize, NULL);
}
}
else
{
glTexImage2D( faceList[i], 0, GFXGLTextureInternalFormat[faceFormat], texSize, texSize,
0, GFXGLTextureFormat[faceFormat], GFXGLTextureType[faceFormat], NULL);
}
}
if( !isCompressed )
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
}
void GFXGLCubemap::zombify()
{
glDeleteTextures(1, &mCubemap);
mCubemap = 0;
}
void GFXGLCubemap::resurrect()
{
// Handled in tmResurrect
}
void GFXGLCubemap::tmResurrect()
{
if(mDynamicTexSize)
initDynamic(mDynamicTexSize,mFaceFormat);
else
{
if ( mDDSFile )
initStatic( mDDSFile );
else
initStatic( mTextures );
}
}
void GFXGLCubemap::setToTexUnit(U32 tuNum)
{
static_cast<GFXGLDevice*>(getOwningDevice())->setCubemapInternal(tuNum, this);
}
void GFXGLCubemap::bind(U32 textureUnit) const
{
glActiveTexture(GL_TEXTURE0 + textureUnit);
glBindTexture(GL_TEXTURE_CUBE_MAP, mCubemap);
static_cast<GFXGLDevice*>(getOwningDevice())->getOpenglCache()->setCacheBindedTex(textureUnit, GL_TEXTURE_CUBE_MAP, mCubemap);
GFXGLStateBlockRef sb = static_cast<GFXGLDevice*>(GFX)->getCurrentStateBlock();
AssertFatal(sb, "GFXGLCubemap::bind - No active stateblock!");
if (!sb)
return;
const GFXSamplerStateDesc& ssd = sb->getDesc().samplers[textureUnit];
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, 0));
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GFXGLTextureAddress[ssd.addressModeU]);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GFXGLTextureAddress[ssd.addressModeV]);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
}
void GFXGLCubemap::_onTextureEvent( GFXTexCallbackCode code )
{
if ( code == GFXZombify )
zombify();
else
tmResurrect();
}
U8* GFXGLCubemap::getTextureData(U32 face, U32 mip)
{
AssertFatal(mMipMapLevels, "");
mip = (mip < mMipMapLevels) ? mip : 0;
const U32 bytesPerTexel = 4; //TODO make work with more formats!!!!!
const U32 dataSize = ImageUtil::isCompressedFormat(mFaceFormat)
? getCompressedSurfaceSize(mFaceFormat, mWidth, mHeight, mip)
: (mWidth >> mip) * (mHeight >> mip) * bytesPerTexel;
U8* data = new U8[dataSize];
PRESERVE_TEXTURE(GL_TEXTURE_CUBE_MAP);
glBindTexture(GL_TEXTURE_CUBE_MAP, mCubemap);
if (ImageUtil::isCompressedFormat(mFaceFormat))
glGetCompressedTexImage(faceList[face], mip, data);
else
glGetTexImage(faceList[face], mip, GFXGLTextureFormat[mFaceFormat], GFXGLTextureType[mFaceFormat], data);
return data;
}
//-----------------------------------------------------------------------------
// Cubemap Array
//-----------------------------------------------------------------------------
GFXGLCubemapArray::GFXGLCubemapArray()
{
}
GFXGLCubemapArray::~GFXGLCubemapArray()
{
glDeleteTextures(1, &mCubemap);
}
void GFXGLCubemapArray::initStatic(GFXCubemapHandle *cubemaps, const U32 cubemapCount)
{
AssertFatal(cubemaps, "GFXGLCubemapArray- Got null GFXCubemapHandle!");
AssertFatal(*cubemaps, "GFXGLCubemapArray - Got empty cubemap!");
//all cubemaps must be the same size,format and number of mipmaps. Grab the details from the first cubemap
mSize = cubemaps[0]->getSize();
mFormat = cubemaps[0]->getFormat();
mMipMapLevels = cubemaps[0]->getMipMapLevels();
mNumCubemaps = cubemapCount;
const bool isCompressed = ImageUtil::isCompressedFormat(mFormat);
glGenTextures(1, &mCubemap);
PRESERVE_CUBEMAP_ARRAY_TEXTURE();
glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAX_LEVEL, mMipMapLevels - 1);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
for (U32 i = 0; i < cubemapCount; i++)
{
GFXGLCubemap* glTex = static_cast<GFXGLCubemap*>(cubemaps[i].getPointer());
for (U32 face = 0; face < 6; face++)
{
for (U32 currentMip = 0; currentMip < mMipMapLevels; currentMip++)
{
U8 *pixelData = glTex->getTextureData(face, currentMip);
const U32 mipSize = getMax(U32(1), mSize >> currentMip);
if (isCompressed)
{
const U32 mipDataSize = getCompressedSurfaceSize(mFormat, mSize, mSize, currentMip);
glCompressedTexImage2D(faceList[face], currentMip, GFXGLTextureInternalFormat[mFormat], mipSize, mipSize, 0, mipDataSize, pixelData);
}
else
{
glTexImage2D(faceList[face], currentMip, GFXGLTextureInternalFormat[mFormat], mipSize, mipSize,
0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], pixelData);
}
delete[] pixelData;
}
}
}
}
void GFXGLCubemapArray::setToTexUnit(U32 tuNum)
{
static_cast<GFXGLDevice*>(getOwningDevice())->setCubemapArrayInternal(tuNum, this);
}
void GFXGLCubemapArray::bind(U32 textureUnit) const
{
glActiveTexture(GL_TEXTURE0 + textureUnit);
glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
static_cast<GFXGLDevice*>(getOwningDevice())->getOpenglCache()->setCacheBindedTex(textureUnit, GL_TEXTURE_CUBE_MAP_ARRAY, mCubemap);
GFXGLStateBlockRef sb = static_cast<GFXGLDevice*>(GFX)->getCurrentStateBlock();
AssertFatal(sb, "GFXGLCubemap::bind - No active stateblock!");
if (!sb)
return;
const GFXSamplerStateDesc& ssd = sb->getDesc().samplers[textureUnit];
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, 0));
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_S, GFXGLTextureAddress[ssd.addressModeU]);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GFXGLTextureAddress[ssd.addressModeV]);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
}

113
gl/gfxGLCubemap.h Normal file
View file

@ -0,0 +1,113 @@
//-----------------------------------------------------------------------------
// 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 _GFXGLCUBEMAP_H_
#define _GFXGLCUBEMAP_H_
#ifndef _GFXCUBEMAP_H_
#include "gfx/gfxCubemap.h"
#endif
#ifndef __RESOURCE_H__
#include "core/resource.h"
#endif
class GFXGLCubemap : public GFXCubemap
{
public:
GFXGLCubemap();
virtual ~GFXGLCubemap();
virtual void initStatic( GFXTexHandle *faces );
virtual void initStatic( DDSFile *dds );
virtual void initDynamic( U32 texSize, GFXFormat faceFormat = GFXFormatR8G8B8A8, U32 mipLevels = 0);
virtual U32 getSize() const { return mWidth; }
virtual GFXFormat getFormat() const { return mFaceFormat; }
// Convenience methods for GFXGLTextureTarget
U32 getWidth() { return mWidth; }
U32 getHeight() { return mHeight; }
U32 getHandle() { return mCubemap; }
// GFXResource interface
virtual void zombify();
virtual void resurrect();
/// Called by texCB; this is to ensure that all textures have been resurrected before we attempt to res the cubemap.
void tmResurrect();
static GLenum getEnumForFaceNumber(U32 face);///< Performs lookup to get a GLenum for the given face number
/// @return An array containing the texture data
/// @note You are responsible for deleting the returned data! (Use delete[])
U8* getTextureData(U32 face, U32 mip = 0);
protected:
friend class GFXDevice;
friend class GFXGLDevice;
/// The callback used to get texture events.
/// @see GFXTextureManager::addEventDelegate
void _onTextureEvent( GFXTexCallbackCode code );
GLuint mCubemap; ///< Internal GL handle
U32 mDynamicTexSize; ///< Size of faces for a dynamic texture (used in resurrect)
// Self explanatory
U32 mWidth;
U32 mHeight;
GFXFormat mFaceFormat;
GFXTexHandle mTextures[6]; ///< Keep refs to our textures for resurrection of static cubemaps
/// The backing DDSFile uses to restore the faces
/// when the surface is lost.
Resource<DDSFile> mDDSFile;
// should only be called by GFXDevice
virtual void setToTexUnit( U32 tuNum ); ///< Binds the cubemap to the given texture unit
virtual void bind(U32 textureUnit) const; ///< Notifies our owning device that we want to be set to the given texture unit (used for GL internal state tracking)
void fillCubeTextures(GFXTexHandle* faces); ///< Copies the textures in faces into the cubemap
};
class GFXGLCubemapArray : public GFXCubemapArray
{
public:
GFXGLCubemapArray();
virtual ~GFXGLCubemapArray();
virtual void initStatic(GFXCubemapHandle *cubemaps, const U32 cubemapCount);
virtual void setToTexUnit(U32 tuNum);
// GFXResource interface
virtual void zombify() {}
virtual void resurrect() {}
protected:
friend class GFXGLDevice;
void bind(U32 textureUnit) const;
GLuint mCubemap; ///< Internal GL handle
};
#endif

1029
gl/gfxGLDevice.cpp Normal file

File diff suppressed because it is too large Load diff

281
gl/gfxGLDevice.h Normal file
View file

@ -0,0 +1,281 @@
//-----------------------------------------------------------------------------
// 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 _GFXGLDEVICE_H_
#define _GFXGLDEVICE_H_
#include "platform/platform.h"
#include "gfx/gfxDevice.h"
#include "gfx/gfxInit.h"
#include "gfx/gl/tGL/tGL.h"
#include "windowManager/platformWindow.h"
#include "gfx/gfxFence.h"
#include "gfx/gfxResource.h"
#include "gfx/gl/gfxGLStateBlock.h"
class GFXGLVertexBuffer;
class GFXGLPrimitiveBuffer;
class GFXGLTextureTarget;
class GFXGLCubemap;
class GFXGLCubemapArray;
class GFXGLStateCache;
class GFXGLVertexDecl;
class GFXGLDevice : public GFXDevice
{
public:
struct GLCapabilities
{
bool anisotropicFiltering;
bool bufferStorage;
bool shaderModel5;
bool textureStorage;
bool samplerObjects;
bool copyImage;
bool vertexAttributeBinding;
};
GLCapabilities mCapabilities;
void zombify();
void resurrect();
GFXGLDevice(U32 adapterIndex);
virtual ~GFXGLDevice();
static void enumerateAdapters( Vector<GFXAdapter*> &adapterList );
static GFXDevice *createInstance( U32 adapterIndex );
virtual void init( const GFXVideoMode &mode, PlatformWindow *window = NULL );
virtual void activate() { }
virtual void deactivate() { }
virtual GFXAdapterType getAdapterType() { return OpenGL; }
virtual void enterDebugEvent(ColorI color, const char *name);
virtual void leaveDebugEvent();
virtual void setDebugMarker(ColorI color, const char *name);
virtual void enumerateVideoModes();
virtual U32 getTotalVideoMemory_GL_EXT();
virtual U32 getTotalVideoMemory();
virtual GFXCubemap * createCubemap();
virtual GFXCubemapArray *createCubemapArray();
virtual F32 getFillConventionOffset() const { return 0.0f; }
///@}
/// @name Render Target functions
/// @{
///
virtual GFXTextureTarget *allocRenderToTextureTarget(bool genMips = true);
virtual GFXWindowTarget *allocWindowTarget(PlatformWindow *window);
virtual void _updateRenderTargets();
///@}
/// @name Shader functions
/// @{
virtual F32 getPixelShaderVersion() const { return mPixelShaderVersion; }
virtual void setPixelShaderVersion( F32 version ) { mPixelShaderVersion = version; }
virtual void setShader(GFXShader *shader, bool force = false);
/// @attention GL cannot check if the given format supports blending or filtering!
virtual GFXFormat selectSupportedFormat(GFXTextureProfile *profile,
const Vector<GFXFormat> &formats, bool texture, bool mustblend, bool mustfilter);
/// Returns the number of texture samplers that can be used in a shader rendering pass
virtual U32 getNumSamplers() const;
/// Returns the number of simultaneous render targets supported by the device.
virtual U32 getNumRenderTargets() const;
virtual GFXShader* createShader();
virtual void clear( U32 flags, const LinearColorF& color, F32 z, U32 stencil );
virtual void clearColorAttachment(const U32 attachment, const LinearColorF& color);
virtual bool beginSceneInternal();
virtual void endSceneInternal();
virtual void drawPrimitive( GFXPrimitiveType primType, U32 vertexStart, U32 primitiveCount );
virtual void drawIndexedPrimitive( GFXPrimitiveType primType,
U32 startVertex,
U32 minIndex,
U32 numVerts,
U32 startIndex,
U32 primitiveCount );
virtual void setClipRect( const RectI &rect );
virtual const RectI &getClipRect() const { return mClip; }
virtual void preDestroy() { Parent::preDestroy(); }
virtual U32 getMaxDynamicVerts() { return MAX_DYNAMIC_VERTS; }
virtual U32 getMaxDynamicIndices() { return MAX_DYNAMIC_INDICES; }
GFXFence *createFence();
GFXOcclusionQuery* createOcclusionQuery();
GFXGLStateBlockRef getCurrentStateBlock() { return mCurrentGLStateBlock; }
virtual void setupGenericShaders( GenericShaderType type = GSColor );
///
bool supportsAnisotropic() const { return mSupportsAnisotropic; }
GFXGLStateCache* getOpenglCache() { return mOpenglStateCache; }
GFXTextureObject* getDefaultDepthTex() const;
/// Returns the number of vertex streams supported by the device.
const U32 getNumVertexStreams() const { return mNumVertexStream; }
bool glUseMap() const { return mUseGlMap; }
protected:
/// Called by GFXDevice to create a device specific stateblock
virtual GFXStateBlockRef createStateBlockInternal(const GFXStateBlockDesc& desc);
/// Called by GFXDevice to actually set a stateblock.
virtual void setStateBlockInternal(GFXStateBlock* block, bool force);
/// Called by base GFXDevice to actually set a const buffer
virtual void setShaderConstBufferInternal(GFXShaderConstBuffer* buffer);
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 setLightInternal(U32 lightStage, const GFXLightInfo light, bool lightEnable);
virtual void setLightMaterialInternal(const GFXLightMaterial mat);
virtual void setGlobalAmbientInternal(LinearColorF color);
/// @name State Initalization.
/// @{
/// State initalization. This MUST BE CALLED in setVideoMode after the device
/// is created.
virtual void initStates() { }
virtual void setMatrix( GFXMatrixType mtype, const MatrixF &mat );
virtual GFXVertexBuffer *allocVertexBuffer( U32 numVerts,
const GFXVertexFormat *vertexFormat,
U32 vertSize,
GFXBufferType bufferType,
void* data = NULL);
virtual GFXPrimitiveBuffer *allocPrimitiveBuffer( U32 numIndices, U32 numPrimitives, GFXBufferType bufferType, void* data = NULL );
// NOTE: The GL device doesn't need a vertex declaration at
// this time, but we need to return something to keep the system
// from retrying to allocate one on every call.
virtual GFXVertexDecl* allocVertexDecl( const GFXVertexFormat *vertexFormat );
virtual void setVertexDecl( const GFXVertexDecl *decl );
virtual void setVertexStream( U32 stream, GFXVertexBuffer *buffer );
virtual void setVertexStreamFrequency( U32 stream, U32 frequency );
private:
typedef GFXDevice Parent;
friend class GFXGLTextureObject;
friend class GFXGLCubemap;
friend class GFXGLCubemapArray;
friend class GFXGLWindowTarget;
friend class GFXGLPrimitiveBuffer;
friend class GFXGLVertexBuffer;
static GFXAdapter::CreateDeviceInstanceDelegate mCreateDeviceInstance;
U32 mAdapterIndex;
StrongRefPtr<GFXGLVertexBuffer> mCurrentVB[VERTEX_STREAM_COUNT];
U32 mCurrentVB_Divisor[VERTEX_STREAM_COUNT];
bool mNeedUpdateVertexAttrib;
StrongRefPtr<GFXGLPrimitiveBuffer> mCurrentPB;
U32 mDrawInstancesCount;
GFXShader* mCurrentShader;
GFXShaderRef mGenericShader[GS_COUNT];
GFXShaderConstBufferRef mGenericShaderBuffer[GS_COUNT];
GFXShaderConstHandle *mModelViewProjSC[GS_COUNT];
/// Since GL does not have separate world and view matrices we need to track them
MatrixF m_mCurrentWorld;
MatrixF m_mCurrentView;
void* mContext;
void* mPixelFormat;
F32 mPixelShaderVersion;
bool mSupportsAnisotropic;
U32 mNumVertexStream;
U32 mMaxShaderTextures;
U32 mMaxFFTextures;
U32 mMaxTRColors;
RectI mClip;
GFXGLStateBlockRef mCurrentGLStateBlock;
GLenum mActiveTextureType[TEXTURE_STAGE_COUNT];
Vector< StrongRefPtr<GFXGLVertexBuffer> > mVolatileVBs; ///< Pool of existing volatile VBs so we can reuse previously created ones
Vector< StrongRefPtr<GFXGLPrimitiveBuffer> > mVolatilePBs; ///< Pool of existing volatile PBs so we can reuse previously created ones
GLsizei primCountToIndexCount(GFXPrimitiveType primType, U32 primitiveCount);
void preDrawPrimitive();
void postDrawPrimitive(U32 primitiveCount);
GFXVertexBuffer* findVolatileVBO(U32 numVerts, const GFXVertexFormat *vertexFormat, U32 vertSize); ///< Returns an existing volatile VB which has >= numVerts and the same vert flags/size, or creates a new VB if necessary
GFXPrimitiveBuffer* findVolatilePBO(U32 numIndices, U32 numPrimitives); ///< Returns an existing volatile PB which has >= numIndices, or creates a new PB if necessary
void vsyncCallback(); ///< Vsync callback
void initGLState(); ///< Guaranteed to be called after all extensions have been loaded, use to init card profiler, shader version, max samplers, etc.
GFXFence* _createPlatformSpecificFence(); ///< If our platform (e.g. OS X) supports a fence extenstion (e.g. GL_APPLE_fence) this will create one, otherwise returns NULL
void setPB(GFXGLPrimitiveBuffer* pb); ///< Sets mCurrentPB
GFXGLStateCache *mOpenglStateCache;
GFXWindowTargetRef *mWindowRT;
bool mUseGlMap;
};
#define GFXGL static_cast<GFXGLDevice*>(GFXDevice::get())
#endif

1151
gl/gfxGLShader.cpp Normal file

File diff suppressed because it is too large Load diff

141
gl/gfxGLStateCache.h Normal file
View file

@ -0,0 +1,141 @@
#ifndef GFX_GL_STATE_CACHE
#define GFX_GL_STATE_CACHE
/// GFXGLStateCache store OpenGL state to avoid performance penalities of glGet* calls
/// GL_TEXTURE_1D/2D/3D, GL_FRAMEBUFFER, GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER
class GFXGLStateCache
{
public:
GFXGLStateCache()
{
mActiveTexture = 0;
mBindedVBO = 0;
mBindedIBO = 0;
mBindedFBO_W = 0;
mBindedFBO_R = 0;
mVertexAttribActive = 0;
}
class TextureUnit
{
public:
TextureUnit() : mTexture1D(0), mTexture2D(0), mTexture3D(0), mTextureCube(0), mTextureCubeArray(0)
{
}
GLuint mTexture1D, mTexture2D, mTexture3D, mTextureCube, mTextureCubeArray;
};
/// after glBindTexture
void setCacheBindedTex(U32 texUnit, GLenum biding, GLuint handle)
{
mActiveTexture = texUnit;
switch (biding)
{
case GL_TEXTURE_2D:
mTextureUnits[mActiveTexture].mTexture2D = handle;
break;
case GL_TEXTURE_3D:
mTextureUnits[mActiveTexture].mTexture3D = handle;
break;
case GL_TEXTURE_1D:
mTextureUnits[mActiveTexture].mTexture1D = handle;
break;
case GL_TEXTURE_CUBE_MAP:
mTextureUnits[mActiveTexture].mTextureCube = handle;
break;
case GL_TEXTURE_CUBE_MAP_ARRAY:
mTextureUnits[mActiveTexture].mTextureCubeArray = handle;
break;
default:
AssertFatal(0, avar("GFXGLStateCache::setCacheBindedTex - binding (%x) not supported.", biding) );
return;
}
}
/// after opengl object binded
void setCacheBinded(GLenum biding, GLuint handle)
{
switch (biding)
{
case GL_TEXTURE_2D:
mTextureUnits[mActiveTexture].mTexture2D = handle;
break;
case GL_TEXTURE_3D:
mTextureUnits[mActiveTexture].mTexture3D = handle;
break;
case GL_TEXTURE_1D:
mTextureUnits[mActiveTexture].mTexture1D = handle;
break;
case GL_TEXTURE_CUBE_MAP:
mTextureUnits[mActiveTexture].mTextureCube = handle;
break;
case GL_TEXTURE_CUBE_MAP_ARRAY:
mTextureUnits[mActiveTexture].mTextureCubeArray = handle;
break;
case GL_FRAMEBUFFER:
mBindedFBO_W = mBindedFBO_R = handle;
break;
case GL_DRAW_FRAMEBUFFER:
mBindedFBO_W = handle;
break;
case GL_READ_FRAMEBUFFER:
mBindedFBO_R = handle;
break;
case GL_ARRAY_BUFFER:
mBindedVBO = handle;
break;
case GL_ELEMENT_ARRAY_BUFFER:
mBindedIBO = handle;
break;
default:
AssertFatal(0, avar("GFXGLStateCache::setCacheBinded - binding (%x) not supported.", biding) );
break;
}
}
GLuint getCacheBinded(GLenum biding) const
{
switch (biding)
{
case GL_TEXTURE_2D:
return mTextureUnits[mActiveTexture].mTexture2D;
case GL_TEXTURE_3D:
return mTextureUnits[mActiveTexture].mTexture3D;
case GL_TEXTURE_1D:
return mTextureUnits[mActiveTexture].mTexture1D;
case GL_TEXTURE_CUBE_MAP:
return mTextureUnits[mActiveTexture].mTextureCube;
case GL_TEXTURE_CUBE_MAP_ARRAY:
return mTextureUnits[mActiveTexture].mTextureCubeArray;
case GL_DRAW_FRAMEBUFFER:
return mBindedFBO_W;
case GL_READ_FRAMEBUFFER:
return mBindedFBO_R;
case GL_ARRAY_BUFFER:
return mBindedVBO;
case GL_ELEMENT_ARRAY_BUFFER:
return mBindedIBO;
default:
AssertFatal(0, avar("GFXGLStateCache::getCacheBinded - binding (%x) not supported.", biding) );
return 0;
}
}
/// after glActiveTexture
void setCacheActiveTexture(U32 unit) { mActiveTexture = unit; }
U32 getCacheActiveTexture() const { return mActiveTexture; }
/// for cache glEnableVertexAttribArray / glDisableVertexAttribArray
void setCacheVertexAttribActive(U32 activeMask) { mVertexAttribActive = activeMask; }
U32 getCacheVertexAttribActive() const { return mVertexAttribActive; }
protected:
GLuint mActiveTexture, mBindedVBO, mBindedIBO, mBindedFBO_W, mBindedFBO_R;
TextureUnit mTextureUnits[TEXTURE_STAGE_COUNT];
U32 mVertexAttribActive;
};
#endif

244
gl/gfxGLUtils.h Normal file
View file

@ -0,0 +1,244 @@
//-----------------------------------------------------------------------------
// 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 TORQUE_GFX_GL_GFXGLUTILS_H_
#define TORQUE_GFX_GL_GFXGLUTILS_H_
#include "core/util/preprocessorHelpers.h"
#include "gfx/gl/gfxGLEnumTranslate.h"
#include "gfx/gl/gfxGLStateCache.h"
#include "gfx/bitmap/imageUtils.h"
inline U32 getMaxMipmaps(U32 width, U32 height, U32 depth)
{
return getMax( getBinLog2(depth), getMax(getBinLog2(width), getBinLog2(height)));
}
inline GLenum minificationFilter(U32 minFilter, U32 mipFilter, U32 /*mipLevels*/)
{
// the compiler should interpret this as array lookups
switch( minFilter )
{
case GFXTextureFilterLinear:
switch( mipFilter )
{
case GFXTextureFilterLinear:
return GL_LINEAR_MIPMAP_LINEAR;
case GFXTextureFilterPoint:
return GL_LINEAR_MIPMAP_NEAREST;
default:
return GL_LINEAR;
}
default:
switch( mipFilter ) {
case GFXTextureFilterLinear:
return GL_NEAREST_MIPMAP_LINEAR;
case GFXTextureFilterPoint:
return GL_NEAREST_MIPMAP_NEAREST;
default:
return GL_NEAREST;
}
}
}
//Get the surface size of a compressed mip map level - see ddsLoader.cpp
inline U32 getCompressedSurfaceSize(GFXFormat format,U32 width, U32 height, U32 mipLevel=0 )
{
if(!ImageUtil::isCompressedFormat(format))
return 0;
// Bump by the mip level.
height = getMax(U32(1), height >> mipLevel);
width = getMax(U32(1), width >> mipLevel);
U32 sizeMultiple = 0;
if(format == GFXFormatBC1 || format == GFXFormatBC1_SRGB)
sizeMultiple = 8;
else
sizeMultiple = 16;
return getMax(U32(1), width/4) * getMax(U32(1), height/4) * sizeMultiple;
}
/// Simple class which preserves a given GL integer.
/// This class determines the integer to preserve on construction and restores
/// it on destruction.
class GFXGLPreserveInteger
{
public:
typedef void(STDCALL *BindFn)(GLenum, GLuint);
/// Preserve the integer.
/// @param binding The binding which should be set on destruction.
/// @param getBinding The parameter to be passed to glGetIntegerv to determine
/// the integer to be preserved.
/// @param binder The gl function to call to restore the integer.
GFXGLPreserveInteger(GLenum binding, GLint getBinding, BindFn binder) :
mBinding(binding), mPreserved(0), mBinder(binder)
{
AssertFatal(mBinder, "GFXGLPreserveInteger - Need a valid binder function");
mPreserved = GFXGL->getOpenglCache()->getCacheBinded(mBinding);
#if defined(TORQUE_DEBUG) && defined(TORQUE_DEBUG_GFX)
GLint bindedOnOpenglDriver;
glGetIntegerv(getBinding, &bindedOnOpenglDriver);
AssertFatal( mPreserved == bindedOnOpenglDriver, "GFXGLPreserveInteger - GFXGLDevice/OpenGL mismatch on cache binded resource.");
#endif
}
/// Restores the integer.
~GFXGLPreserveInteger()
{
mBinder(mBinding, mPreserved);
}
private:
GLenum mBinding;
GLint mPreserved;
BindFn mBinder;
};
class GFXGLPreserveTexture
{
public:
typedef void(STDCALL *BindFn)(GLenum, GLuint);
GFXGLPreserveTexture(GLenum binding, GLint getBinding, BindFn binder) :
mBinding(binding), mPreserved(0), mBinder(binder)
{
AssertFatal(mBinder, "GFXGLPreserveTexture - Need a valid binder function");
GFXGLDevice *gfx = GFXGL;
mPreserved = gfx->getOpenglCache()->getCacheBinded(mBinding);
mActiveTexture = gfx->getOpenglCache()->getCacheActiveTexture();
#if defined(TORQUE_DEBUG) && defined(TORQUE_DEBUG_GFX)
GLint activeTextureOnOpenglDriver, bindedTextureOnOpenglDriver;
glGetIntegerv(getBinding, &bindedTextureOnOpenglDriver);
glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureOnOpenglDriver);
activeTextureOnOpenglDriver -= GL_TEXTURE0;
AssertFatal( mPreserved == bindedTextureOnOpenglDriver, "GFXGLPreserveTexture - GFXGLDevice/OpenGL mismatch on cache binded resource.");
AssertFatal( activeTextureOnOpenglDriver == mActiveTexture, "GFXGLPreserveTexture - GFXGLDevice/OpenGL mismatch on cache binded resource.");
#endif
}
/// Restores the texture.
~GFXGLPreserveTexture()
{
#if defined(TORQUE_DEBUG) && defined(TORQUE_DEBUG_GFX)
GLint activeTextureOnOpenglDriver;
glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureOnOpenglDriver);
activeTextureOnOpenglDriver -= GL_TEXTURE0;
GLint cacheActiveTexture = GFXGL->getOpenglCache()->getCacheActiveTexture();
AssertFatal( cacheActiveTexture == activeTextureOnOpenglDriver, "GFXGLPreserveTexture - GFXGLDevice/OpenGL mismatch on cache ActiveTexture.");
#endif
mBinder(mBinding, mPreserved);
}
private:
GLenum mBinding;
GLint mPreserved;
BindFn mBinder;
S16 mActiveTexture;
};
/// Helper macro to preserve the current VBO binding.
#define PRESERVE_VERTEX_BUFFER() \
GFXGLPreserveInteger TORQUE_CONCAT(preserve_, __LINE__) (GL_ARRAY_BUFFER, GL_ARRAY_BUFFER_BINDING, (GFXGLPreserveInteger::BindFn)glBindBuffer)
/// Helper macro to preserve the current element array binding.
#define PRESERVE_INDEX_BUFFER() \
GFXGLPreserveInteger TORQUE_CONCAT(preserve_, __LINE__) (GL_ELEMENT_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER_BINDING, (GFXGLPreserveInteger::BindFn)glBindBuffer)
#define _GET_BUFFER_BINDING( BINDING ) \
BINDING == GL_ARRAY_BUFFER ? GL_ARRAY_BUFFER_BINDING : ( BINDING == GL_ELEMENT_ARRAY_BUFFER ? GL_ELEMENT_ARRAY_BUFFER_BINDING : 0 )
/// Helper macro to preserve the current element array binding.
#define PRESERVE_BUFFER( BINDING ) \
GFXGLPreserveInteger TORQUE_CONCAT(preserve_, __LINE__) (BINDING, _GET_BUFFER_BINDING(BINDING), (GFXGLPreserveInteger::BindFn)glBindBuffer)
/// ASSERT: Never call glActiveTexture for a "bind to modify" or in a PRESERVER_TEXTURE MACRO scope.
/// Helper macro to preserve the current 1D texture binding.
#define PRESERVE_1D_TEXTURE() \
GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D, (GFXGLPreserveInteger::BindFn)glBindTexture)
/// Helper macro to preserve the current 2D texture binding.
#define PRESERVE_2D_TEXTURE() \
GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D, (GFXGLPreserveInteger::BindFn)glBindTexture)
/// Helper macro to preserve the current 3D texture binding.
#define PRESERVE_3D_TEXTURE() \
GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D, (GFXGLPreserveInteger::BindFn)glBindTexture)
/// Helper macro to preserve the current 3D texture binding.
#define PRESERVE_CUBEMAP_TEXTURE() \
GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP, (GFXGLPreserveInteger::BindFn)glBindTexture)
#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 _GET_TEXTURE_BINDING(binding) \
binding == GL_TEXTURE_2D ? GL_TEXTURE_BINDING_2D : (binding == GL_TEXTURE_3D ? GL_TEXTURE_BINDING_3D : GL_TEXTURE_BINDING_1D )
#define PRESERVE_TEXTURE(binding) \
GFXGLPreserveTexture TORQUE_CONCAT(preserve_, __LINE__) (binding, _GET_TEXTURE_BINDING(binding), (GFXGLPreserveInteger::BindFn)glBindTexture)
#define PRESERVE_FRAMEBUFFER() \
GFXGLPreserveInteger TORQUE_CONCAT(preserve_, __LINE__) (GL_READ_FRAMEBUFFER, GL_READ_FRAMEBUFFER_BINDING, (GFXGLPreserveInteger::BindFn)glBindFramebuffer);\
GFXGLPreserveInteger TORQUE_CONCAT(preserve2_, __LINE__) (GL_DRAW_FRAMEBUFFER, GL_DRAW_FRAMEBUFFER_BINDING, (GFXGLPreserveInteger::BindFn)glBindFramebuffer)
#if TORQUE_DEBUG
// Handy macro for checking the status of a framebuffer. Framebuffers can fail in
// all sorts of interesting ways, these are just the most common. Further, no existing GL profiling
// tool catches framebuffer errors when the framebuffer is created, so we actually need this.
#define CHECK_FRAMEBUFFER_STATUS()\
{\
GLenum status;\
status = glCheckFramebufferStatus(GL_FRAMEBUFFER);\
switch(status) {\
case GL_FRAMEBUFFER_COMPLETE:\
break;\
case GL_FRAMEBUFFER_UNSUPPORTED:\
AssertFatal(false, "Unsupported FBO");\
break;\
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:\
AssertFatal(false, "Incomplete FBO Attachment");\
break;\
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:\
AssertFatal(false, "Incomplete FBO Missing Attachment");\
break;\
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:\
AssertFatal(false, "Incomplete FBO Draw buffer");\
break;\
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:\
AssertFatal(false, "Incomplete FBO Read buffer");\
break;\
default:\
/* programming error; will fail on all hardware */\
AssertFatal(false, "Something really bad happened with an FBO");\
}\
}
#else
#define CHECK_FRAMEBUFFER_STATUS()
#endif //TORQUE_DEBUG
#endif