BsTextureManager.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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, UINT32 numArraySlices)
  12. {
  13. Texture* tex = new (bs_alloc<Texture>()) Texture(texType, width, height, depth, numMipmaps, format, usage, hwGamma, multisampleCount, numArraySlices);
  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.numFaces = 1;
  48. desc.colorSurface.mipLevel = 0;
  49. desc.depthStencilSurface.texture = depthStencil;
  50. desc.depthStencilSurface.face = 0;
  51. desc.depthStencilSurface.numFaces = 1;
  52. desc.depthStencilSurface.mipLevel = 0;
  53. SPtr<RenderTexture> newRT = createRenderTexture(desc);
  54. return newRT;
  55. }
  56. SPtr<RenderTexture> TextureManager::createRenderTexture(const RENDER_TEXTURE_DESC& desc)
  57. {
  58. SPtr<RenderTexture> newRT = createRenderTextureImpl(desc);
  59. newRT->_setThisPtr(newRT);
  60. newRT->initialize();
  61. return newRT;
  62. }
  63. SPtr<MultiRenderTexture> TextureManager::createMultiRenderTexture(const MULTI_RENDER_TEXTURE_DESC& desc)
  64. {
  65. SPtr<MultiRenderTexture> newRT = createMultiRenderTextureImpl(desc);
  66. newRT->_setThisPtr(newRT);
  67. newRT->initialize();
  68. return newRT;
  69. }
  70. void TextureCoreManager::onStartUp()
  71. {
  72. // White built-in texture
  73. SPtr<TextureCore> whiteTexture = createTexture(TEX_TYPE_2D, 2, 2, 1, 0, PF_R8G8B8A8, TU_STATIC);
  74. SPtr<PixelData> whitePixelData = PixelData::create(2, 2, 1, PF_R8G8B8A8);
  75. whitePixelData->setColorAt(Color::White, 0, 0);
  76. whitePixelData->setColorAt(Color::White, 0, 1);
  77. whitePixelData->setColorAt(Color::White, 1, 0);
  78. whitePixelData->setColorAt(Color::White, 1, 1);
  79. whiteTexture->writeData(*whitePixelData);
  80. TextureCore::WHITE = whiteTexture;
  81. // Black built-in texture
  82. SPtr<TextureCore> blackTexture = createTexture(TEX_TYPE_2D, 2, 2, 1, 0, PF_R8G8B8A8, TU_STATIC);
  83. SPtr<PixelData> blackPixelData = PixelData::create(2, 2, 1, PF_R8G8B8A8);
  84. blackPixelData->setColorAt(Color::Black, 0, 0);
  85. blackPixelData->setColorAt(Color::Black, 0, 1);
  86. blackPixelData->setColorAt(Color::Black, 1, 0);
  87. blackPixelData->setColorAt(Color::Black, 1, 1);
  88. blackTexture->writeData(*blackPixelData);
  89. TextureCore::BLACK = blackTexture;
  90. // Normal (Y = Up) built-in texture
  91. SPtr<TextureCore> normalTexture = createTexture(TEX_TYPE_2D, 2, 2, 1, 0, PF_R8G8B8A8, TU_STATIC);
  92. SPtr<PixelData> normalPixelData = PixelData::create(2, 2, 1, PF_R8G8B8A8);
  93. Color encodedNormal(0.5f, 0.5f, 1.0f);
  94. normalPixelData->setColorAt(encodedNormal, 0, 0);
  95. normalPixelData->setColorAt(encodedNormal, 0, 1);
  96. normalPixelData->setColorAt(encodedNormal, 1, 0);
  97. normalPixelData->setColorAt(encodedNormal, 1, 1);
  98. normalTexture->writeData(*normalPixelData);
  99. TextureCore::NORMAL = normalTexture;
  100. }
  101. void TextureCoreManager::onShutDown()
  102. {
  103. // Need to make sure these are freed while still on the core thread
  104. TextureCore::WHITE = nullptr;
  105. TextureCore::BLACK = nullptr;
  106. TextureCore::NORMAL = nullptr;
  107. }
  108. SPtr<TextureCore> TextureCoreManager::createTexture(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  109. int numMips, PixelFormat format, int usage, bool hwGammaCorrection, UINT32 multisampleCount, UINT32 numArraySlices)
  110. {
  111. SPtr<TextureCore> newRT = createTextureInternal(texType, width, height, depth, numMips, format,
  112. usage, hwGammaCorrection, multisampleCount, numArraySlices);
  113. newRT->initialize();
  114. return newRT;
  115. }
  116. SPtr<RenderTextureCore> TextureCoreManager::createRenderTexture(const RENDER_TEXTURE_CORE_DESC& desc)
  117. {
  118. SPtr<RenderTextureCore> newRT = createRenderTextureInternal(desc);
  119. newRT->initialize();
  120. return newRT;
  121. }
  122. SPtr<MultiRenderTextureCore> TextureCoreManager::createMultiRenderTexture(const MULTI_RENDER_TEXTURE_CORE_DESC& desc)
  123. {
  124. SPtr<MultiRenderTextureCore> newRT = createMultiRenderTextureInternal(desc);
  125. newRT->initialize();
  126. return newRT;
  127. }
  128. }