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