CmTextureManager.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "CmTextureManager.h"
  2. #include "CmException.h"
  3. #include "CmPixelUtil.h"
  4. #include "CmMultiRenderTexture.h"
  5. #include "CmRenderSystem.h"
  6. namespace BansheeEngine
  7. {
  8. TextureManager::TextureManager()
  9. {
  10. // Subclasses should register (when this is fully constructed)
  11. }
  12. TextureManager::~TextureManager()
  13. {
  14. // subclasses should unregister with resource group manager
  15. }
  16. void TextureManager::onStartUp()
  17. {
  18. // Internally this will call our createTextureImpl virtual method. But since this is guaranteed
  19. // to be the last class in the hierarchy, we can call a virtual method from constructor.
  20. mDummyTexture = Texture::create(TEX_TYPE_2D, 2, 2, 0, PF_R8G8B8A8);
  21. UINT32 subresourceIdx = mDummyTexture->mapToSubresourceIdx(0, 0);
  22. PixelDataPtr data = mDummyTexture->allocateSubresourceBuffer(subresourceIdx);
  23. data->setColorAt(Color::Red, 0, 0);
  24. data->setColorAt(Color::Red, 0, 1);
  25. data->setColorAt(Color::Red, 1, 0);
  26. data->setColorAt(Color::Red, 1, 1);
  27. AsyncOp op;
  28. data->_lock();
  29. RenderSystem::instance().writeSubresource(mDummyTexture.getInternalPtr(), mDummyTexture->mapToSubresourceIdx(0, 0), data, false, op);
  30. }
  31. TexturePtr TextureManager::createTexture(TextureType texType, UINT32 width, UINT32 height, UINT32 depth, int numMipmaps,
  32. PixelFormat format, int usage, bool hwGamma,
  33. UINT32 fsaa, const String& fsaaHint)
  34. {
  35. TexturePtr ret = createTextureImpl();
  36. ret->_setThisPtr(ret);
  37. ret->initialize(texType, width, height, depth, static_cast<size_t>(numMipmaps), format, usage, hwGamma, fsaa, fsaaHint);
  38. return ret;
  39. }
  40. TexturePtr TextureManager::_createEmpty()
  41. {
  42. TexturePtr texture = createTextureImpl();
  43. texture->_setThisPtr(texture);
  44. return texture;
  45. }
  46. RenderTexturePtr TextureManager::createRenderTexture(TextureType textureType, UINT32 width, UINT32 height,
  47. PixelFormat format, bool hwGamma, UINT32 fsaa, const String& fsaaHint,
  48. bool createDepth, PixelFormat depthStencilFormat)
  49. {
  50. TexturePtr texture = createTexture(textureType, width, height, 0, format, TU_RENDERTARGET, hwGamma, fsaa, fsaaHint);
  51. TexturePtr depthStencil = nullptr;
  52. if(createDepth)
  53. {
  54. depthStencil = createTexture(TEX_TYPE_2D, width, height, 0, depthStencilFormat, TU_DEPTHSTENCIL, false, fsaa, fsaaHint);
  55. }
  56. RENDER_TEXTURE_DESC desc;
  57. desc.colorSurface.texture = texture;
  58. desc.colorSurface.face = 0;
  59. desc.colorSurface.mipLevel = 0;
  60. desc.colorSurface.numFaces = 1;
  61. desc.depthStencilSurface.texture = depthStencil;
  62. desc.depthStencilSurface.face = 0;
  63. desc.depthStencilSurface.mipLevel = 0;
  64. desc.depthStencilSurface.numFaces = 1;
  65. RenderTexturePtr newRT = createRenderTexture(desc);
  66. return newRT;
  67. }
  68. RenderTexturePtr TextureManager::createRenderTexture(const RENDER_TEXTURE_DESC& desc)
  69. {
  70. RenderTexturePtr newRT = createRenderTextureImpl();
  71. newRT->_setThisPtr(newRT);
  72. newRT->initialize(desc);
  73. return newRT;
  74. }
  75. MultiRenderTexturePtr TextureManager::createMultiRenderTexture(const MULTI_RENDER_TEXTURE_DESC& desc)
  76. {
  77. MultiRenderTexturePtr newRT = createMultiRenderTextureImpl();
  78. newRT->_setThisPtr(newRT);
  79. newRT->initialize(desc);
  80. return newRT;
  81. }
  82. }