CmTextureManager.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "CmTextureManager.h"
  25. #include "CmException.h"
  26. #include "CmPixelUtil.h"
  27. #include "CmMultiRenderTexture.h"
  28. #include "CmRenderSystem.h"
  29. namespace BansheeEngine
  30. {
  31. TextureManager::TextureManager()
  32. {
  33. // Subclasses should register (when this is fully constructed)
  34. }
  35. TextureManager::~TextureManager()
  36. {
  37. // subclasses should unregister with resource group manager
  38. }
  39. void TextureManager::onStartUp()
  40. {
  41. // Internally this will call our createTextureImpl virtual method. But since this is guaranteed
  42. // to be the last class in the hierarchy, we can call a virtual method from constructor.
  43. mDummyTexture = Texture::create(TEX_TYPE_2D, 2, 2, 0, PF_R8G8B8A8);
  44. UINT32 subresourceIdx = mDummyTexture->mapToSubresourceIdx(0, 0);
  45. PixelDataPtr data = mDummyTexture->allocateSubresourceBuffer(subresourceIdx);
  46. data->setColorAt(Color::Red, 0, 0);
  47. data->setColorAt(Color::Red, 0, 1);
  48. data->setColorAt(Color::Red, 1, 0);
  49. data->setColorAt(Color::Red, 1, 1);
  50. AsyncOp op;
  51. data->_lock();
  52. RenderSystem::instance().writeSubresource(mDummyTexture.getInternalPtr(), mDummyTexture->mapToSubresourceIdx(0, 0), data, false, op);
  53. }
  54. TexturePtr TextureManager::createTexture(TextureType texType, UINT32 width, UINT32 height, UINT32 depth, int numMipmaps,
  55. PixelFormat format, int usage, bool hwGamma,
  56. UINT32 fsaa, const String& fsaaHint)
  57. {
  58. TexturePtr ret = createTextureImpl();
  59. ret->_setThisPtr(ret);
  60. ret->initialize(texType, width, height, depth, static_cast<size_t>(numMipmaps), format, usage, hwGamma, fsaa, fsaaHint);
  61. return ret;
  62. }
  63. TexturePtr TextureManager::createEmpty()
  64. {
  65. TexturePtr texture = createTextureImpl();
  66. texture->_setThisPtr(texture);
  67. return texture;
  68. }
  69. RenderTexturePtr TextureManager::createRenderTexture(TextureType textureType, UINT32 width, UINT32 height,
  70. PixelFormat format, bool hwGamma, UINT32 fsaa, const String& fsaaHint,
  71. bool createDepth, PixelFormat depthStencilFormat)
  72. {
  73. TexturePtr texture = createTexture(textureType, width, height, 0, format, TU_RENDERTARGET, hwGamma, fsaa, fsaaHint);
  74. TexturePtr depthStencil = nullptr;
  75. if(createDepth)
  76. {
  77. depthStencil = createTexture(TEX_TYPE_2D, width, height, 0, depthStencilFormat, TU_DEPTHSTENCIL, false, fsaa, fsaaHint);
  78. }
  79. RENDER_TEXTURE_DESC desc;
  80. desc.colorSurface.texture = texture;
  81. desc.colorSurface.face = 0;
  82. desc.colorSurface.mipLevel = 0;
  83. desc.colorSurface.numFaces = 1;
  84. desc.depthStencilSurface.texture = depthStencil;
  85. desc.depthStencilSurface.face = 0;
  86. desc.depthStencilSurface.mipLevel = 0;
  87. desc.depthStencilSurface.numFaces = 1;
  88. RenderTexturePtr newRT = createRenderTexture(desc);
  89. return newRT;
  90. }
  91. RenderTexturePtr TextureManager::createRenderTexture(const RENDER_TEXTURE_DESC& desc)
  92. {
  93. RenderTexturePtr newRT = createRenderTextureImpl();
  94. newRT->_setThisPtr(newRT);
  95. newRT->initialize(desc);
  96. return newRT;
  97. }
  98. MultiRenderTexturePtr TextureManager::createEmptyMultiRenderTexture()
  99. {
  100. MultiRenderTexturePtr newRT = createMultiRenderTextureImpl();
  101. newRT->_setThisPtr(newRT);
  102. return newRT;
  103. }
  104. bool TextureManager::isFormatSupported(TextureType ttype, PixelFormat format, int usage)
  105. {
  106. return getNativeFormat(ttype, format, usage) == format;
  107. }
  108. }