BsTextureManager.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "BsRenderAPI.h"
  7. namespace BansheeEngine
  8. {
  9. SPtr<Texture> TextureManager::createTexture(const TEXTURE_DESC& desc)
  10. {
  11. Texture* tex = new (bs_alloc<Texture>()) Texture(desc);
  12. SPtr<Texture> ret = bs_core_ptr<Texture>(tex);
  13. ret->_setThisPtr(ret);
  14. ret->initialize();
  15. return ret;
  16. }
  17. SPtr<Texture> TextureManager::createTexture(const TEXTURE_DESC& desc, const SPtr<PixelData>& pixelData)
  18. {
  19. Texture* tex = new (bs_alloc<Texture>()) Texture(desc, pixelData);
  20. SPtr<Texture> ret = bs_core_ptr<Texture>(tex);
  21. ret->_setThisPtr(ret);
  22. ret->initialize();
  23. return ret;
  24. }
  25. SPtr<Texture> TextureManager::_createEmpty()
  26. {
  27. Texture* tex = new (bs_alloc<Texture>()) Texture();
  28. SPtr<Texture> texture = bs_core_ptr<Texture>(tex);
  29. texture->_setThisPtr(texture);
  30. return texture;
  31. }
  32. SPtr<RenderTexture> TextureManager::createRenderTexture(const TEXTURE_DESC& colorDesc, bool createDepth,
  33. PixelFormat depthStencilFormat)
  34. {
  35. TEXTURE_DESC textureDesc = colorDesc;
  36. textureDesc.usage = TU_RENDERTARGET;
  37. textureDesc.numMips = 0;
  38. HTexture texture = Texture::create(textureDesc);
  39. HTexture depthStencil;
  40. if(createDepth)
  41. {
  42. textureDesc.format = depthStencilFormat;
  43. textureDesc.hwGamma = false;
  44. textureDesc.usage = TU_DEPTHSTENCIL;
  45. depthStencil = Texture::create(textureDesc);
  46. }
  47. RENDER_TEXTURE_DESC desc;
  48. desc.colorSurfaces[0].texture = texture;
  49. desc.colorSurfaces[0].face = 0;
  50. desc.colorSurfaces[0].numFaces = 1;
  51. desc.colorSurfaces[0].mipLevel = 0;
  52. desc.depthStencilSurface.texture = depthStencil;
  53. desc.depthStencilSurface.face = 0;
  54. desc.depthStencilSurface.numFaces = 1;
  55. desc.depthStencilSurface.mipLevel = 0;
  56. SPtr<RenderTexture> newRT = createRenderTexture(desc);
  57. return newRT;
  58. }
  59. SPtr<RenderTexture> TextureManager::createRenderTexture(const RENDER_TEXTURE_DESC& desc)
  60. {
  61. SPtr<RenderTexture> newRT = createRenderTextureImpl(desc);
  62. newRT->_setThisPtr(newRT);
  63. newRT->initialize();
  64. return newRT;
  65. }
  66. void TextureCoreManager::onStartUp()
  67. {
  68. TEXTURE_DESC desc;
  69. desc.type = TEX_TYPE_2D;
  70. desc.width = 2;
  71. desc.height = 2;
  72. desc.format = PF_R8G8B8A8;
  73. desc.usage = TU_STATIC;
  74. // White built-in texture
  75. SPtr<TextureCore> whiteTexture = createTexture(desc);
  76. SPtr<PixelData> whitePixelData = PixelData::create(2, 2, 1, PF_R8G8B8A8);
  77. whitePixelData->setColorAt(Color::White, 0, 0);
  78. whitePixelData->setColorAt(Color::White, 0, 1);
  79. whitePixelData->setColorAt(Color::White, 1, 0);
  80. whitePixelData->setColorAt(Color::White, 1, 1);
  81. whiteTexture->writeData(*whitePixelData);
  82. TextureCore::WHITE = whiteTexture;
  83. // Black built-in texture
  84. SPtr<TextureCore> blackTexture = createTexture(desc);
  85. SPtr<PixelData> blackPixelData = PixelData::create(2, 2, 1, PF_R8G8B8A8);
  86. blackPixelData->setColorAt(Color::Black, 0, 0);
  87. blackPixelData->setColorAt(Color::Black, 0, 1);
  88. blackPixelData->setColorAt(Color::Black, 1, 0);
  89. blackPixelData->setColorAt(Color::Black, 1, 1);
  90. blackTexture->writeData(*blackPixelData);
  91. TextureCore::BLACK = blackTexture;
  92. // Normal (Y = Up) built-in texture
  93. SPtr<TextureCore> normalTexture = createTexture(desc);
  94. SPtr<PixelData> normalPixelData = PixelData::create(2, 2, 1, PF_R8G8B8A8);
  95. Color encodedNormal(0.5f, 0.5f, 1.0f);
  96. normalPixelData->setColorAt(encodedNormal, 0, 0);
  97. normalPixelData->setColorAt(encodedNormal, 0, 1);
  98. normalPixelData->setColorAt(encodedNormal, 1, 0);
  99. normalPixelData->setColorAt(encodedNormal, 1, 1);
  100. normalTexture->writeData(*normalPixelData);
  101. TextureCore::NORMAL = normalTexture;
  102. }
  103. void TextureCoreManager::onShutDown()
  104. {
  105. // Need to make sure these are freed while still on the core thread
  106. TextureCore::WHITE = nullptr;
  107. TextureCore::BLACK = nullptr;
  108. TextureCore::NORMAL = nullptr;
  109. }
  110. SPtr<TextureCore> TextureCoreManager::createTexture(const TEXTURE_DESC& desc, GpuDeviceFlags deviceMask)
  111. {
  112. SPtr<TextureCore> newRT = createTextureInternal(desc, nullptr, deviceMask);
  113. newRT->initialize();
  114. return newRT;
  115. }
  116. SPtr<RenderTextureCore> TextureCoreManager::createRenderTexture(const RENDER_TEXTURE_DESC_CORE& desc,
  117. GpuDeviceFlags deviceMask)
  118. {
  119. SPtr<RenderTextureCore> newRT = createRenderTextureInternal(desc, deviceMask);
  120. newRT->initialize();
  121. return newRT;
  122. }
  123. }