CmGLTextureManager.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #include "CmGLMultiRenderTexture.h"
  29. namespace CamelotEngine {
  30. //-----------------------------------------------------------------------------
  31. GLTextureManager::GLTextureManager(GLSupport& support)
  32. :TextureManager(), mGLSupport(support), mWarningTextureID(0)
  33. {
  34. createWarningTexture();
  35. }
  36. //-----------------------------------------------------------------------------
  37. GLTextureManager::~GLTextureManager()
  38. {
  39. // Delete warning texture
  40. glDeleteTextures(1, &mWarningTextureID);
  41. }
  42. //-----------------------------------------------------------------------------
  43. Texture* GLTextureManager::createTextureImpl()
  44. {
  45. return new GLTexture(mGLSupport);
  46. }
  47. //-----------------------------------------------------------------------------
  48. RenderTexture* GLTextureManager::createRenderTextureImpl()
  49. {
  50. return new GLRenderTexture();
  51. }
  52. //----------------------------------------------------------------------------
  53. MultiRenderTexturePtr GLTextureManager::createMultiRenderTexture()
  54. {
  55. GLMultiRenderTexture* newMRT = new GLMultiRenderTexture();
  56. newMRT->initialize();
  57. return MultiRenderTexturePtr(newMRT);
  58. }
  59. //-----------------------------------------------------------------------------
  60. void GLTextureManager::createWarningTexture()
  61. {
  62. // Generate warning texture
  63. UINT32 width = 8;
  64. UINT32 height = 8;
  65. UINT32 *data = new UINT32[width*height]; // 0xXXRRGGBB
  66. // Yellow/black stripes
  67. for(UINT32 y=0; y<height; ++y)
  68. {
  69. for(UINT32 x=0; x<width; ++x)
  70. {
  71. data[y*width+x] = (((x+y)%8)<4)?0x000000:0xFFFF00;
  72. }
  73. }
  74. // Create GL resource
  75. glGenTextures(1, &mWarningTextureID);
  76. glBindTexture(GL_TEXTURE_2D, mWarningTextureID);
  77. if (GLEW_VERSION_1_2)
  78. {
  79. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
  80. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, (void*)data);
  81. }
  82. else
  83. {
  84. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT, (void*)data);
  85. }
  86. // Free memory
  87. delete [] data;
  88. }
  89. //-----------------------------------------------------------------------------
  90. PixelFormat GLTextureManager::getNativeFormat(TextureType ttype, PixelFormat format, int usage)
  91. {
  92. // Adjust requested parameters to capabilities
  93. const RenderSystemCapabilities *caps = CamelotEngine::RenderSystem::instancePtr()->getCapabilities();
  94. // Check compressed texture support
  95. // if a compressed format not supported, revert to PF_A8R8G8B8
  96. if(PixelUtil::isCompressed(format) &&
  97. !caps->hasCapability( RSC_TEXTURE_COMPRESSION_DXT ))
  98. {
  99. return PF_A8R8G8B8;
  100. }
  101. // if floating point textures not supported, revert to PF_A8R8G8B8
  102. if(PixelUtil::isFloatingPoint(format) &&
  103. !caps->hasCapability( RSC_TEXTURE_FLOAT ))
  104. {
  105. return PF_A8R8G8B8;
  106. }
  107. // Check if this is a valid rendertarget format
  108. if( usage & TU_RENDERTARGET )
  109. {
  110. /// Get closest supported alternative
  111. /// If mFormat is supported it's returned
  112. return GLRTTManager::instance().getSupportedAlternative(format);
  113. }
  114. // Supported
  115. return format;
  116. }
  117. //-----------------------------------------------------------------------------
  118. bool GLTextureManager::isHardwareFilteringSupported(TextureType ttype, PixelFormat format, int usage,
  119. bool preciseFormatOnly)
  120. {
  121. if (format == PF_UNKNOWN)
  122. return false;
  123. // Check natively format
  124. PixelFormat nativeFormat = getNativeFormat(ttype, format, usage);
  125. if (preciseFormatOnly && format != nativeFormat)
  126. return false;
  127. // Assume non-floating point is supported always
  128. if (!PixelUtil::isFloatingPoint(nativeFormat))
  129. return true;
  130. // Hack: there are no elegant GL API to detects texture filtering supported,
  131. // just hard code for cards based on vendor specifications.
  132. // TODO: Add cards that 16 bits floating point flitering supported by
  133. // hardware below
  134. static const String sFloat16SupportedCards[] =
  135. {
  136. // GeForce 8 Series
  137. "*GeForce*8800*",
  138. // GeForce 7 Series
  139. "*GeForce*7950*",
  140. "*GeForce*7900*",
  141. "*GeForce*7800*",
  142. "*GeForce*7600*",
  143. "*GeForce*7500*",
  144. "*GeForce*7300*",
  145. // GeForce 6 Series
  146. "*GeForce*6800*",
  147. "*GeForce*6700*",
  148. "*GeForce*6600*",
  149. "*GeForce*6500*",
  150. "" // Empty string means end of list
  151. };
  152. // TODO: Add cards that 32 bits floating point flitering supported by
  153. // hardware below
  154. static const String sFloat32SupportedCards[] =
  155. {
  156. // GeForce 8 Series
  157. "*GeForce*8800*",
  158. "" // Empty string means end of list
  159. };
  160. PixelComponentType pct = PixelUtil::getComponentType(nativeFormat);
  161. const String* supportedCards;
  162. switch (pct)
  163. {
  164. case PCT_FLOAT16:
  165. supportedCards = sFloat16SupportedCards;
  166. break;
  167. case PCT_FLOAT32:
  168. supportedCards = sFloat32SupportedCards;
  169. break;
  170. default:
  171. return false;
  172. }
  173. const GLubyte* pcRenderer = glGetString(GL_RENDERER);
  174. String str = (const char*)pcRenderer;
  175. for (; !supportedCards->empty(); ++supportedCards)
  176. {
  177. if (StringUtil::match(str, *supportedCards))
  178. {
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. }