2
0

BsTextureManager.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "BsTextureManager.h"
  2. #include "BsException.h"
  3. #include "BsPixelUtil.h"
  4. #include "BsMultiRenderTexture.h"
  5. #include "BsRenderSystem.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, UINT32 multisampleCount, const String& multisampleHint)
  33. {
  34. TexturePtr ret = createTextureImpl();
  35. ret->_setThisPtr(ret);
  36. ret->initialize(texType, width, height, depth, static_cast<size_t>(numMipmaps), format, usage, hwGamma, multisampleCount, multisampleHint);
  37. return ret;
  38. }
  39. TexturePtr TextureManager::_createEmpty()
  40. {
  41. TexturePtr texture = createTextureImpl();
  42. texture->_setThisPtr(texture);
  43. return texture;
  44. }
  45. RenderTexturePtr TextureManager::createRenderTexture(TextureType textureType, UINT32 width, UINT32 height,
  46. PixelFormat format, bool hwGamma, UINT32 multisampleCount, const String& multisampleHint,
  47. bool createDepth, PixelFormat depthStencilFormat)
  48. {
  49. TexturePtr texture = createTexture(textureType, width, height, 0, format, TU_RENDERTARGET, hwGamma, multisampleCount, multisampleHint);
  50. TexturePtr depthStencil = nullptr;
  51. if(createDepth)
  52. {
  53. depthStencil = createTexture(TEX_TYPE_2D, width, height, 0, depthStencilFormat, TU_DEPTHSTENCIL, false, multisampleCount, multisampleHint);
  54. }
  55. RENDER_TEXTURE_DESC desc;
  56. desc.colorSurface.texture = texture;
  57. desc.colorSurface.face = 0;
  58. desc.colorSurface.mipLevel = 0;
  59. desc.colorSurface.numFaces = 1;
  60. desc.depthStencilSurface.texture = depthStencil;
  61. desc.depthStencilSurface.face = 0;
  62. desc.depthStencilSurface.mipLevel = 0;
  63. desc.depthStencilSurface.numFaces = 1;
  64. RenderTexturePtr newRT = createRenderTexture(desc);
  65. return newRT;
  66. }
  67. RenderTexturePtr TextureManager::createRenderTexture(const RENDER_TEXTURE_DESC& desc)
  68. {
  69. RenderTexturePtr newRT = createRenderTextureImpl();
  70. newRT->_setThisPtr(newRT);
  71. newRT->initialize(desc);
  72. return newRT;
  73. }
  74. MultiRenderTexturePtr TextureManager::createMultiRenderTexture(const MULTI_RENDER_TEXTURE_DESC& desc)
  75. {
  76. MultiRenderTexturePtr newRT = createMultiRenderTextureImpl();
  77. newRT->_setThisPtr(newRT);
  78. newRT->initialize(desc);
  79. return newRT;
  80. }
  81. }