CmGLTextureManager.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmGLTextureManager.h"
  25. #include "CmRenderSystem.h"
  26. #include "CmGLRenderTexture.h"
  27. #include "CmGLDepthStencilBuffer.h"
  28. namespace CamelotEngine {
  29. //-----------------------------------------------------------------------------
  30. GLTextureManager::GLTextureManager(GLSupport& support)
  31. :TextureManager(), mGLSupport(support), mWarningTextureID(0)
  32. {
  33. createWarningTexture();
  34. }
  35. //-----------------------------------------------------------------------------
  36. GLTextureManager::~GLTextureManager()
  37. {
  38. // Delete warning texture
  39. glDeleteTextures(1, &mWarningTextureID);
  40. }
  41. //-----------------------------------------------------------------------------
  42. Texture* GLTextureManager::createImpl()
  43. {
  44. return new GLTexture(mGLSupport);
  45. }
  46. //----------------------------------------------------------------------------
  47. DepthStencilBufferPtr GLTextureManager::createDepthStencilBuffer(UINT32 bitDepth, UINT32 width,
  48. UINT32 height, UINT32 fsaa, const String& fsaaHint)
  49. {
  50. return DepthStencilBufferPtr(new GLDepthStencilBuffer(bitDepth, width, height, fsaa, fsaaHint));
  51. }
  52. //-----------------------------------------------------------------------------
  53. void GLTextureManager::createWarningTexture()
  54. {
  55. // Generate warning texture
  56. UINT32 width = 8;
  57. UINT32 height = 8;
  58. UINT32 *data = new UINT32[width*height]; // 0xXXRRGGBB
  59. // Yellow/black stripes
  60. for(UINT32 y=0; y<height; ++y)
  61. {
  62. for(UINT32 x=0; x<width; ++x)
  63. {
  64. data[y*width+x] = (((x+y)%8)<4)?0x000000:0xFFFF00;
  65. }
  66. }
  67. // Create GL resource
  68. glGenTextures(1, &mWarningTextureID);
  69. glBindTexture(GL_TEXTURE_2D, mWarningTextureID);
  70. if (GLEW_VERSION_1_2)
  71. {
  72. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
  73. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, (void*)data);
  74. }
  75. else
  76. {
  77. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT, (void*)data);
  78. }
  79. // Free memory
  80. delete [] data;
  81. }
  82. //-----------------------------------------------------------------------------
  83. PixelFormat GLTextureManager::getNativeFormat(TextureType ttype, PixelFormat format, int usage)
  84. {
  85. // Adjust requested parameters to capabilities
  86. const RenderSystemCapabilities *caps = CamelotEngine::RenderSystem::instancePtr()->getCapabilities();
  87. // Check compressed texture support
  88. // if a compressed format not supported, revert to PF_A8R8G8B8
  89. if(PixelUtil::isCompressed(format) &&
  90. !caps->hasCapability( RSC_TEXTURE_COMPRESSION_DXT ))
  91. {
  92. return PF_A8R8G8B8;
  93. }
  94. // if floating point textures not supported, revert to PF_A8R8G8B8
  95. if(PixelUtil::isFloatingPoint(format) &&
  96. !caps->hasCapability( RSC_TEXTURE_FLOAT ))
  97. {
  98. return PF_A8R8G8B8;
  99. }
  100. // Check if this is a valid rendertarget format
  101. if( usage & TU_RENDERTARGET )
  102. {
  103. /// Get closest supported alternative
  104. /// If mFormat is supported it's returned
  105. return GLRTTManager::instance().getSupportedAlternative(format);
  106. }
  107. // Supported
  108. return format;
  109. }
  110. //-----------------------------------------------------------------------------
  111. bool GLTextureManager::isHardwareFilteringSupported(TextureType ttype, PixelFormat format, int usage,
  112. bool preciseFormatOnly)
  113. {
  114. if (format == PF_UNKNOWN)
  115. return false;
  116. // Check natively format
  117. PixelFormat nativeFormat = getNativeFormat(ttype, format, usage);
  118. if (preciseFormatOnly && format != nativeFormat)
  119. return false;
  120. // Assume non-floating point is supported always
  121. if (!PixelUtil::isFloatingPoint(nativeFormat))
  122. return true;
  123. // Hack: there are no elegant GL API to detects texture filtering supported,
  124. // just hard code for cards based on vendor specifications.
  125. // TODO: Add cards that 16 bits floating point flitering supported by
  126. // hardware below
  127. static const String sFloat16SupportedCards[] =
  128. {
  129. // GeForce 8 Series
  130. "*GeForce*8800*",
  131. // GeForce 7 Series
  132. "*GeForce*7950*",
  133. "*GeForce*7900*",
  134. "*GeForce*7800*",
  135. "*GeForce*7600*",
  136. "*GeForce*7500*",
  137. "*GeForce*7300*",
  138. // GeForce 6 Series
  139. "*GeForce*6800*",
  140. "*GeForce*6700*",
  141. "*GeForce*6600*",
  142. "*GeForce*6500*",
  143. "" // Empty string means end of list
  144. };
  145. // TODO: Add cards that 32 bits floating point flitering supported by
  146. // hardware below
  147. static const String sFloat32SupportedCards[] =
  148. {
  149. // GeForce 8 Series
  150. "*GeForce*8800*",
  151. "" // Empty string means end of list
  152. };
  153. PixelComponentType pct = PixelUtil::getComponentType(nativeFormat);
  154. const String* supportedCards;
  155. switch (pct)
  156. {
  157. case PCT_FLOAT16:
  158. supportedCards = sFloat16SupportedCards;
  159. break;
  160. case PCT_FLOAT32:
  161. supportedCards = sFloat32SupportedCards;
  162. break;
  163. default:
  164. return false;
  165. }
  166. const GLubyte* pcRenderer = glGetString(GL_RENDERER);
  167. String str = (const char*)pcRenderer;
  168. for (; !supportedCards->empty(); ++supportedCards)
  169. {
  170. if (StringUtil::match(str, *supportedCards))
  171. {
  172. return true;
  173. }
  174. }
  175. return false;
  176. }
  177. }