BsTextureManager.cpp 3.6 KB

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