BsTextureManager.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsTextureManager.h"
  4. #include "BsException.h"
  5. #include "BsPixelUtil.h"
  6. #include "BsMultiRenderTexture.h"
  7. #include "BsRenderAPI.h"
  8. namespace BansheeEngine
  9. {
  10. SPtr<Texture> TextureManager::createTexture(TextureType texType, UINT32 width, UINT32 height, UINT32 depth, int numMipmaps,
  11. PixelFormat format, int usage, bool hwGamma, UINT32 multisampleCount)
  12. {
  13. Texture* tex = new (bs_alloc<Texture>()) Texture(texType, width, height, depth, numMipmaps, format, usage, hwGamma, multisampleCount);
  14. SPtr<Texture> ret = bs_core_ptr<Texture>(tex);
  15. ret->_setThisPtr(ret);
  16. ret->initialize();
  17. return ret;
  18. }
  19. SPtr<Texture> TextureManager::createTexture(const SPtr<PixelData>& pixelData, int usage , bool hwGammaCorrection)
  20. {
  21. Texture* tex = new (bs_alloc<Texture>()) Texture(pixelData, usage, hwGammaCorrection);
  22. SPtr<Texture> ret = bs_core_ptr<Texture>(tex);
  23. ret->_setThisPtr(ret);
  24. ret->initialize();
  25. return ret;
  26. }
  27. SPtr<Texture> TextureManager::_createEmpty()
  28. {
  29. Texture* tex = new (bs_alloc<Texture>()) Texture();
  30. SPtr<Texture> texture = bs_core_ptr<Texture>(tex);
  31. texture->_setThisPtr(texture);
  32. return texture;
  33. }
  34. SPtr<RenderTexture> TextureManager::createRenderTexture(TextureType textureType, UINT32 width, UINT32 height,
  35. PixelFormat format, bool hwGamma, UINT32 multisampleCount,
  36. bool createDepth, PixelFormat depthStencilFormat)
  37. {
  38. HTexture texture = Texture::create(textureType, width, height, 0, format, TU_RENDERTARGET, hwGamma, multisampleCount);
  39. HTexture depthStencil;
  40. if(createDepth)
  41. {
  42. depthStencil = Texture::create(TEX_TYPE_2D, width, height, 0, depthStencilFormat, TU_DEPTHSTENCIL, false, multisampleCount);
  43. }
  44. RENDER_TEXTURE_DESC desc;
  45. desc.colorSurface.texture = texture;
  46. desc.colorSurface.face = 0;
  47. desc.colorSurface.mipLevel = 0;
  48. desc.depthStencilSurface.texture = depthStencil;
  49. desc.depthStencilSurface.face = 0;
  50. desc.depthStencilSurface.mipLevel = 0;
  51. SPtr<RenderTexture> newRT = createRenderTexture(desc);
  52. return newRT;
  53. }
  54. SPtr<RenderTexture> TextureManager::createRenderTexture(const RENDER_TEXTURE_DESC& desc)
  55. {
  56. SPtr<RenderTexture> newRT = createRenderTextureImpl(desc);
  57. newRT->_setThisPtr(newRT);
  58. newRT->initialize();
  59. return newRT;
  60. }
  61. SPtr<MultiRenderTexture> TextureManager::createMultiRenderTexture(const MULTI_RENDER_TEXTURE_DESC& desc)
  62. {
  63. SPtr<MultiRenderTexture> newRT = createMultiRenderTextureImpl(desc);
  64. newRT->_setThisPtr(newRT);
  65. newRT->initialize();
  66. return newRT;
  67. }
  68. void TextureCoreManager::onStartUp()
  69. {
  70. // White built-in texture
  71. SPtr<TextureCore> whiteTexture = createTexture(TEX_TYPE_2D, 2, 2, 1, 0, PF_R8G8B8A8, TU_STATIC);
  72. SPtr<PixelData> whitePixelData = PixelData::create(2, 2, 1, PF_R8G8B8A8);
  73. whitePixelData->setColorAt(Color::White, 0, 0);
  74. whitePixelData->setColorAt(Color::White, 0, 1);
  75. whitePixelData->setColorAt(Color::White, 1, 0);
  76. whitePixelData->setColorAt(Color::White, 1, 1);
  77. whiteTexture->writeData(*whitePixelData);
  78. TextureCore::WHITE = whiteTexture;
  79. // Black built-in texture
  80. SPtr<TextureCore> blackTexture = createTexture(TEX_TYPE_2D, 2, 2, 1, 0, PF_R8G8B8A8, TU_STATIC);
  81. SPtr<PixelData> blackPixelData = PixelData::create(2, 2, 1, PF_R8G8B8A8);
  82. blackPixelData->setColorAt(Color::Black, 0, 0);
  83. blackPixelData->setColorAt(Color::Black, 0, 1);
  84. blackPixelData->setColorAt(Color::Black, 1, 0);
  85. blackPixelData->setColorAt(Color::Black, 1, 1);
  86. blackTexture->writeData(*blackPixelData);
  87. TextureCore::BLACK = blackTexture;
  88. // Normal (Y = Up) built-in texture
  89. SPtr<TextureCore> normalTexture = createTexture(TEX_TYPE_2D, 2, 2, 1, 0, PF_R8G8B8A8, TU_STATIC);
  90. SPtr<PixelData> normalPixelData = PixelData::create(2, 2, 1, PF_R8G8B8A8);
  91. Color encodedNormal(0.5f, 0.5f, 1.0f);
  92. normalPixelData->setColorAt(encodedNormal, 0, 0);
  93. normalPixelData->setColorAt(encodedNormal, 0, 1);
  94. normalPixelData->setColorAt(encodedNormal, 1, 0);
  95. normalPixelData->setColorAt(encodedNormal, 1, 1);
  96. normalTexture->writeData(*normalPixelData);
  97. TextureCore::NORMAL = normalTexture;
  98. }
  99. void TextureCoreManager::onShutDown()
  100. {
  101. // Need to make sure these are freed while still on the core thread
  102. TextureCore::WHITE = nullptr;
  103. TextureCore::BLACK = nullptr;
  104. TextureCore::NORMAL = nullptr;
  105. }
  106. SPtr<TextureCore> TextureCoreManager::createTexture(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  107. int numMips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount)
  108. {
  109. SPtr<TextureCore> newRT = createTextureInternal(texType, width, height, depth, numMips, format, usage, hwGammaCorrection, multisampleCount);
  110. newRT->initialize();
  111. return newRT;
  112. }
  113. SPtr<RenderTextureCore> TextureCoreManager::createRenderTexture(const RENDER_TEXTURE_CORE_DESC& desc)
  114. {
  115. SPtr<RenderTextureCore> newRT = createRenderTextureInternal(desc);
  116. newRT->initialize();
  117. return newRT;
  118. }
  119. SPtr<MultiRenderTextureCore> TextureCoreManager::createMultiRenderTexture(const MULTI_RENDER_TEXTURE_CORE_DESC& desc)
  120. {
  121. SPtr<MultiRenderTextureCore> newRT = createMultiRenderTextureInternal(desc);
  122. newRT->initialize();
  123. return newRT;
  124. }
  125. }