Add OpenGL support.

This commit is contained in:
LuisAntonRebollo 2014-11-08 17:41:17 +01:00
parent c354f59b72
commit dd08fd2e7d
55 changed files with 2957 additions and 802 deletions

View file

@ -21,7 +21,7 @@
//-----------------------------------------------------------------------------
#include "console/console.h"
#include "gfx/gl/ggl/ggl.h"
#include "gfx/gl/tGL/tGL.h"
#include "math/mRect.h"
#include "gfx/gl/gfxGLTextureObject.h"
#include "gfx/gfxDevice.h"
@ -37,7 +37,10 @@ GFXGLTextureObject::GFXGLTextureObject(GFXDevice * aDevice, GFXTextureProfile *p
mBytesPerTexel(4),
mLockedRectRect(0, 0, 0, 0),
mGLDevice(static_cast<GFXGLDevice*>(mDevice)),
mZombieCache(NULL)
mZombieCache(NULL),
mNeedInitSamplerState(true),
mFrameAllocatorMark(0),
mFrameAllocatorPtr(NULL)
{
AssertFatal(dynamic_cast<GFXGLDevice*>(mDevice), "GFXGLTextureObject::GFXGLTextureObject - Invalid device type, expected GFXGLDevice!");
glGenTextures(1, &mHandle);
@ -46,6 +49,7 @@ GFXGLTextureObject::GFXGLTextureObject(GFXDevice * aDevice, GFXTextureProfile *p
GFXGLTextureObject::~GFXGLTextureObject()
{
glDeleteTextures(1, &mHandle);
glDeleteBuffers(1, &mBuffer);
delete[] mZombieCache;
kill();
@ -70,12 +74,16 @@ GFXLockedRect* GFXGLTextureObject::lock(U32 mipLevel, RectI *inRect)
}
mLockedRect.pitch = mLockedRectRect.extent.x * mBytesPerTexel;
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, mBuffer);
// CodeReview [ags 12/19/07] This one texel boundary is necessary to keep the clipmap code from crashing. Figure out why.
glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, (mLockedRectRect.extent.x + 1) * (mLockedRectRect.extent.y + 1) * mBytesPerTexel, NULL, GL_STREAM_DRAW);
mLockedRect.bits = (U8*)glMapBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
U32 size = (mLockedRectRect.extent.x + 1) * (mLockedRectRect.extent.y + 1) * mBytesPerTexel;
AssertFatal(!mFrameAllocatorMark && !mFrameAllocatorPtr, "");
mFrameAllocatorMark = FrameAllocator::getWaterMark();
mFrameAllocatorPtr = (U8*)FrameAllocator::alloc( size );
mLockedRect.bits = mFrameAllocatorPtr;
#if TORQUE_DEBUG
mFrameAllocatorMarkGuard = FrameAllocator::getWaterMark();
#endif
if( !mLockedRect.bits )
return NULL;
@ -88,20 +96,27 @@ void GFXGLTextureObject::unlock(U32 mipLevel)
if(!mLockedRect.bits)
return;
glActiveTexture(GL_TEXTURE0);
U32 boundTexture;
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*)&boundTexture);
glBindTexture(GL_TEXTURE_2D, mHandle);
PRESERVE_TEXTURE(mBinding);
glBindTexture(mBinding, mHandle);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, mBuffer);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER_ARB);
glTexSubImage2D(GL_TEXTURE_2D, mipLevel, mLockedRectRect.point.x, mLockedRectRect.point.y,
mLockedRectRect.extent.x, mLockedRectRect.extent.y, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, (mLockedRectRect.extent.x + 1) * (mLockedRectRect.extent.y + 1) * mBytesPerTexel, mFrameAllocatorPtr, GL_STREAM_DRAW);
if(mBinding == GL_TEXTURE_2D)
glTexSubImage2D(mBinding, mipLevel, mLockedRectRect.point.x, mLockedRectRect.point.y,
mLockedRectRect.extent.x, mLockedRectRect.extent.y, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], NULL);
else if(mBinding == GL_TEXTURE_1D)
glTexSubImage1D(mBinding, mipLevel, (mLockedRectRect.point.x > 1 ? mLockedRectRect.point.x : mLockedRectRect.point.y),
(mLockedRectRect.extent.x > 1 ? mLockedRectRect.extent.x : mLockedRectRect.extent.y), GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], NULL);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
mLockedRect.bits = NULL;
glBindTexture(GL_TEXTURE_2D, boundTexture);
#if TORQUE_DEBUG
AssertFatal(mFrameAllocatorMarkGuard == FrameAllocator::getWaterMark(), "");
#endif
FrameAllocator::setWaterMark(mFrameAllocatorMark);
mFrameAllocatorMark = 0;
mFrameAllocatorPtr = NULL;
}
void GFXGLTextureObject::release()
@ -113,28 +128,91 @@ void GFXGLTextureObject::release()
mBuffer = 0;
}
void GFXGLTextureObject::reInit()
{
AssertFatal(!mHandle && !mBuffer,"Must release before reInit");
glGenTextures(1, &mHandle);
glGenBuffers(1, &mBuffer);
}
bool GFXGLTextureObject::copyToBmp(GBitmap * bmp)
{
GLint oldTex;
glGetIntegerv(0x8069, &oldTex);
glBindTexture(GL_TEXTURE_2D, mHandle);
if (!bmp)
return false;
// 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 == GFXFormatR8G8B8A8, "GFXGLTextureObject::copyToBmp - invalid format");
AssertFatal(bmp->getFormat() == GFXFormatR8G8B8A8 || bmp->getFormat() == GFXFormatR8G8B8, "GFXGLTextureObject::copyToBmp - invalid format");
if(mFormat != GFXFormatR8G8B8A8)
return false;
if(bmp->getFormat() != GFXFormatR8G8B8A8 && bmp->getFormat() != GFXFormatR8G8B8)
return false;
AssertFatal(bmp->getWidth() == getWidth(), "GFXGLTextureObject::copyToBmp - invalid size");
AssertFatal(bmp->getHeight() == getHeight(), "GFXGLTextureObject::copyToBmp - invalid size");
PROFILE_SCOPE(GFXGLTextureObject_copyToBmp);
PRESERVE_TEXTURE(mBinding);
glBindTexture(mBinding, mHandle);
U8 dstBytesPerPixel = GFXFormat_getByteSize( bmp->getFormat() );
U8 srcBytesPerPixel = GFXFormat_getByteSize( mFormat );
if(dstBytesPerPixel == srcBytesPerPixel)
{
glGetTexImage(mBinding, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], bmp->getWritableBits());
return true;
}
FrameAllocatorMarker mem;
GLint textureFormat = GFXGLTextureFormat[bmp->getFormat()];
// Don't swizzle outgoing textures.
if(textureFormat == GL_BGRA)
textureFormat = GL_RGBA;
U32 srcPixelCount = mTextureSize.x * mTextureSize.y;
U8 *dest = bmp->getWritableBits();
U8 *orig = (U8*)mem.alloc(srcPixelCount * srcBytesPerPixel);
glGetTexImage(mBinding, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], orig);
glGetTexImage(GL_TEXTURE_2D, 0, textureFormat, GL_UNSIGNED_BYTE, bmp->getWritableBits());
glBindTexture(GL_TEXTURE_2D, oldTex);
for(int i = 0; i < srcPixelCount; ++i)
{
dest[0] = orig[0];
dest[1] = orig[1];
dest[2] = orig[2];
if(dstBytesPerPixel == 4)
dest[3] = orig[3];
orig += srcBytesPerPixel;
dest += dstBytesPerPixel;
}
return true;
}
void GFXGLTextureObject::bind(U32 textureUnit) const
void GFXGLTextureObject::initSamplerState(const GFXSamplerStateDesc &ssd)
{
glTexParameteri(mBinding, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, mMipLevels));
glTexParameteri(mBinding, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
glTexParameteri(mBinding, GL_TEXTURE_WRAP_S, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeU] : GL_CLAMP_TO_EDGE);
glTexParameteri(mBinding, GL_TEXTURE_WRAP_T, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeV] : GL_CLAMP_TO_EDGE);
if(mBinding == GL_TEXTURE_3D)
glTexParameteri(mBinding, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
if(static_cast< GFXGLDevice* >( GFX )->supportsAnisotropic() )
glTexParameterf(mBinding, GL_TEXTURE_MAX_ANISOTROPY_EXT, ssd.maxAnisotropy);
mNeedInitSamplerState = false;
mSampler = ssd;
}
void GFXGLTextureObject::bind(U32 textureUnit)
{
glActiveTexture(GL_TEXTURE0 + textureUnit);
glBindTexture(mBinding, mHandle);
glEnable(mBinding);
GFXGL->getOpenglCache()->setCacheBindedTex(textureUnit, mBinding, mHandle);
if( gglHasExtension(ARB_sampler_objects) )
return;
GFXGLStateBlockRef sb = mGLDevice->getCurrentStateBlock();
AssertFatal(sb, "GFXGLTextureObject::bind - No active stateblock!");
@ -142,26 +220,41 @@ void GFXGLTextureObject::bind(U32 textureUnit) const
return;
const GFXSamplerStateDesc ssd = sb->getDesc().samplers[textureUnit];
glTexParameteri(mBinding, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, mMipLevels));
glTexParameteri(mBinding, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
glTexParameteri(mBinding, GL_TEXTURE_WRAP_S, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeU] : GL_CLAMP_TO_EDGE);
glTexParameteri(mBinding, GL_TEXTURE_WRAP_T, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeV] : GL_CLAMP_TO_EDGE);
if(mBinding == GL_TEXTURE_3D)
glTexParameteri(mBinding, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
glTexEnvf(GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, ssd.mipLODBias);
if(mNeedInitSamplerState)
{
initSamplerState(ssd);
return;
}
if(mSampler.minFilter != ssd.minFilter || mSampler.mipFilter != ssd.mipFilter)
glTexParameteri(mBinding, GL_TEXTURE_MIN_FILTER, minificationFilter(ssd.minFilter, ssd.mipFilter, mMipLevels));
if(mSampler.magFilter != ssd.magFilter)
glTexParameteri(mBinding, GL_TEXTURE_MAG_FILTER, GFXGLTextureFilter[ssd.magFilter]);
if(mSampler.addressModeU != ssd.addressModeU)
glTexParameteri(mBinding, GL_TEXTURE_WRAP_S, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeU] : GL_CLAMP_TO_EDGE);
if(mSampler.addressModeV != ssd.addressModeV)
glTexParameteri(mBinding, GL_TEXTURE_WRAP_T, !mIsNPoT2 ? GFXGLTextureAddress[ssd.addressModeV] : GL_CLAMP_TO_EDGE);
if(mBinding == GL_TEXTURE_3D && mSampler.addressModeW != ssd.addressModeW )
glTexParameteri(mBinding, GL_TEXTURE_WRAP_R, GFXGLTextureAddress[ssd.addressModeW]);
if(mSampler.maxAnisotropy != ssd.maxAnisotropy && static_cast< GFXGLDevice* >( GFX )->supportsAnisotropic() )
glTexParameterf(mBinding, GL_TEXTURE_MAX_ANISOTROPY_EXT, ssd.maxAnisotropy);
mSampler = ssd;
}
U8* GFXGLTextureObject::getTextureData()
{
U8* data = new U8[mTextureSize.x * mTextureSize.y * mBytesPerTexel];
glBindTexture(GL_TEXTURE_2D, mHandle);
glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, data);
PRESERVE_TEXTURE(mBinding);
glBindTexture(mBinding, mHandle);
glGetTexImage(mBinding, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], data);
return data;
}
void GFXGLTextureObject::copyIntoCache()
{
PRESERVE_TEXTURE(mBinding);
glBindTexture(mBinding, mHandle);
U32 cacheSize = mTextureSize.x * mTextureSize.y;
if(mBinding == GL_TEXTURE_3D)
@ -171,7 +264,6 @@ void GFXGLTextureObject::copyIntoCache()
mZombieCache = new U8[cacheSize];
glGetTexImage(mBinding, 0, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
glBindTexture(mBinding, 0);
}
void GFXGLTextureObject::reloadFromCache()
@ -187,8 +279,13 @@ void GFXGLTextureObject::reloadFromCache()
return;
}
PRESERVE_TEXTURE(mBinding);
glBindTexture(mBinding, mHandle);
glTexSubImage2D(mBinding, 0, 0, 0, mTextureSize.x, mTextureSize.y, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
if(mBinding == GL_TEXTURE_2D)
glTexSubImage2D(mBinding, 0, 0, 0, mTextureSize.x, mTextureSize.y, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
else if(mBinding == GL_TEXTURE_1D)
glTexSubImage1D(mBinding, 0, 0, (mTextureSize.x > 1 ? mTextureSize.x : mTextureSize.y), GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], mZombieCache);
if(GFX->getCardProfiler()->queryProfile("GL::Workaround::needsExplicitGenerateMipmap") && mMipLevels != 1)
glGenerateMipmapEXT(mBinding);