CmGLTextureManager.cpp 7.6 KB

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