Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "texturemanager.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "toonz/preferences.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//TextureManager
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TextureManager *TextureManager::instance()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_instance)
Toshihiro Shimizu 890ddd
		m_instance = new TextureManager();
Toshihiro Shimizu 890ddd
	return m_instance;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
TDimensionI TextureManager::getMaxSize(bool isRGBM)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	GLenum fmt, type;
Toshihiro Shimizu 890ddd
	getFmtAndType(isRGBM, fmt, type);
Toshihiro Shimizu 890ddd
	if ((m_textureSize.lx == 0) || (m_textureSize.ly == 0)) {
Toshihiro Shimizu 890ddd
		glEnable(GL_TEXTURE_2D);
Toshihiro Shimizu 890ddd
		int texLx, texLy;
Toshihiro Shimizu 890ddd
		int outX, outY;
Toshihiro Shimizu 890ddd
		int shift = 0;
Toshihiro Shimizu 890ddd
		do {
Toshihiro Shimizu 890ddd
			++shift;
Toshihiro Shimizu 890ddd
			texLx = 64 << shift;
Toshihiro Shimizu 890ddd
			texLy = 64 << shift;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			glTexImage2D(GL_PROXY_TEXTURE_2D,
Toshihiro Shimizu 890ddd
						 0,		// is one level only
Toshihiro Shimizu 890ddd
						 4,		// number of component of a pixel
Toshihiro Shimizu 890ddd
						 texLx, // size width
Toshihiro Shimizu 890ddd
						 texLy, // height
Toshihiro Shimizu 890ddd
						 0,		// size of a border
Toshihiro Shimizu 890ddd
						 fmt,
Toshihiro Shimizu 890ddd
						 type,
Toshihiro Shimizu 890ddd
						 0);
Toshihiro Shimizu 890ddd
			glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &outX);
Toshihiro Shimizu 890ddd
			glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &outY);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef _DEBUG
Toshihiro Shimizu 890ddd
			int intFmt, rSize, gSize, bSize, aSize, cmpt;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &intFmt);
Toshihiro Shimizu 890ddd
			glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_RED_SIZE, &rSize);
Toshihiro Shimizu 890ddd
			glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_GREEN_SIZE, &gSize);
Toshihiro Shimizu 890ddd
			glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_BLUE_SIZE, &bSize);
Toshihiro Shimizu 890ddd
			glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE, &aSize);
Toshihiro Shimizu 890ddd
			glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_COMPONENTS, &cmpt);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (outX && outY) {
Toshihiro Shimizu 890ddd
				ostrstream os;
Toshihiro Shimizu 890ddd
				os << "texture size = " << outX << "x" << outY << " fmt " << intFmt << " cmpt# " << cmpt << " " << rSize << "," << gSize << "," << bSize << "," << aSize << '\n' << '\0';
Toshihiro Shimizu 890ddd
				TSystem::outputDebug(os.str());
Toshihiro Shimizu 890ddd
				os.freeze(false);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		} while ((outX == texLx) && (outY == texLy));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int s = Preferences::instance()->getTextureSize();
Toshihiro Shimizu 890ddd
		if (s)
Toshihiro Shimizu 890ddd
			s = tmin(s, 64 << (shift - 1));
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			s = 64 << (shift - 1);
Toshihiro Shimizu 890ddd
		m_textureSize.lx = s;
Toshihiro Shimizu 890ddd
		m_textureSize.ly = s;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		glDisable(GL_TEXTURE_2D);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return TDimension(tmin(m_textureSize.lx, 2048), tmin(m_textureSize.ly, 2048));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
TDimension TextureManager::getMaxSize(bool isRGBM)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_textureSize = TDimension(512, 512);
Toshihiro Shimizu 890ddd
	return m_textureSize;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TextureManager::getFmtAndType(bool isRGBM, GLenum &fmt, GLenum &type)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (isRGBM) {
Toshihiro Shimizu 890ddd
		fmt =
Toshihiro Shimizu 890ddd
#ifdef TNZ_MACHINE_CHANNEL_ORDER_BGRM
Toshihiro Shimizu 890ddd
			GL_BGRA_EXT
Toshihiro Shimizu 890ddd
#elif TNZ_MACHINE_CHANNEL_ORDER_MBGR
Toshihiro Shimizu 890ddd
			GL_ABGR_EXT
Toshihiro Shimizu 890ddd
#elif TNZ_MACHINE_CHANNEL_ORDER_RGBM
Toshihiro Shimizu 890ddd
			GL_RGBA
Toshihiro Shimizu 890ddd
#elif TNZ_MACHINE_CHANNEL_ORDER_MRGB
Toshihiro Shimizu 890ddd
			GL_BGRA
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
			@undefined chan order
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
			;
Toshihiro Shimizu 890ddd
		type =
Toshihiro Shimizu 890ddd
#ifdef TNZ_MACHINE_CHANNEL_ORDER_MRGB
Toshihiro Shimizu 890ddd
			GL_UNSIGNED_INT_8_8_8_8_REV
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
			GL_UNSIGNED_BYTE
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
			;
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		fmt = GL_LUMINANCE;
Toshihiro Shimizu 890ddd
		type = GL_UNSIGNED_BYTE;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TDimension TextureManager::selectTexture(TDimension reqSize, bool isRGBM)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int lx = 1;
Toshihiro Shimizu 890ddd
	int ly = 1;
Toshihiro Shimizu 890ddd
	while (lx < reqSize.lx)
Toshihiro Shimizu 890ddd
		lx = lx << 1;
Toshihiro Shimizu 890ddd
	while (ly < reqSize.ly)
Toshihiro Shimizu 890ddd
		ly = ly << 1;
Toshihiro Shimizu 890ddd
	TDimension textureSize = instance()->getMaxSize(isRGBM);
Toshihiro Shimizu 890ddd
	assert(lx <= textureSize.lx);
Toshihiro Shimizu 890ddd
	assert(ly <= textureSize.ly);
Toshihiro Shimizu 890ddd
	GLenum fmt, type;
Toshihiro Shimizu 890ddd
	getFmtAndType(isRGBM, fmt, type);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	glTexImage2D(
Toshihiro Shimizu 890ddd
		GL_TEXTURE_2D, // target (is a 2D texture)
Toshihiro Shimizu 890ddd
		0,			   // is one level only
Toshihiro Shimizu 890ddd
		4,			   //  number of component of a pixel
Toshihiro Shimizu 890ddd
		lx,			   // size width
Toshihiro Shimizu 890ddd
		ly,			   //      height
Toshihiro Shimizu 890ddd
		0,			   // size of a border
Toshihiro Shimizu 890ddd
		fmt,
Toshihiro Shimizu 890ddd
		type,
Toshihiro Shimizu 890ddd
		0);
Toshihiro Shimizu 890ddd
	return TDimension(lx, ly);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
UCHAR *m_transpRow; //comune a RGBM e GR8...
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TextureManager::TextureManager()
Toshihiro Shimizu 890ddd
	: m_textureSize(0, 0), m_isRGBM(true)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TextureManager *TextureManager::m_instance;