BsTextureManager.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Managers/BsTextureManager.h"
  4. #include "Error/BsException.h"
  5. #include "Image/BsPixelUtil.h"
  6. #include "RenderAPI/BsRenderAPI.h"
  7. namespace bs
  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. namespace ct
  67. {
  68. void TextureManager::onStartUp()
  69. {
  70. TEXTURE_DESC desc;
  71. desc.type = TEX_TYPE_2D;
  72. desc.width = 2;
  73. desc.height = 2;
  74. desc.format = PF_RGBA8;
  75. desc.usage = TU_STATIC;
  76. // White built-in texture
  77. SPtr<Texture> whiteTexture = createTexture(desc);
  78. SPtr<PixelData> whitePixelData = PixelData::create(2, 2, 1, PF_RGBA8);
  79. whitePixelData->setColorAt(Color::White, 0, 0);
  80. whitePixelData->setColorAt(Color::White, 0, 1);
  81. whitePixelData->setColorAt(Color::White, 1, 0);
  82. whitePixelData->setColorAt(Color::White, 1, 1);
  83. whiteTexture->writeData(*whitePixelData);
  84. Texture::WHITE = whiteTexture;
  85. // Black built-in texture
  86. SPtr<Texture> blackTexture = createTexture(desc);
  87. SPtr<PixelData> blackPixelData = PixelData::create(2, 2, 1, PF_RGBA8);
  88. blackPixelData->setColorAt(Color::ZERO, 0, 0);
  89. blackPixelData->setColorAt(Color::ZERO, 0, 1);
  90. blackPixelData->setColorAt(Color::ZERO, 1, 0);
  91. blackPixelData->setColorAt(Color::ZERO, 1, 1);
  92. blackTexture->writeData(*blackPixelData);
  93. Texture::BLACK = blackTexture;
  94. // Normal (Y = Up) built-in texture
  95. SPtr<Texture> normalTexture = createTexture(desc);
  96. SPtr<PixelData> normalPixelData = PixelData::create(2, 2, 1, PF_RGBA8);
  97. Color encodedNormal(0.5f, 0.5f, 1.0f);
  98. normalPixelData->setColorAt(encodedNormal, 0, 0);
  99. normalPixelData->setColorAt(encodedNormal, 0, 1);
  100. normalPixelData->setColorAt(encodedNormal, 1, 0);
  101. normalPixelData->setColorAt(encodedNormal, 1, 1);
  102. normalTexture->writeData(*normalPixelData);
  103. Texture::NORMAL = normalTexture;
  104. }
  105. void TextureManager::onShutDown()
  106. {
  107. // Need to make sure these are freed while still on the core thread
  108. Texture::WHITE = nullptr;
  109. Texture::BLACK = nullptr;
  110. Texture::NORMAL = nullptr;
  111. }
  112. SPtr<Texture> TextureManager::createTexture(const TEXTURE_DESC& desc, GpuDeviceFlags deviceMask)
  113. {
  114. SPtr<Texture> newTex = createTextureInternal(desc, nullptr, deviceMask);
  115. newTex->initialize();
  116. return newTex;
  117. }
  118. SPtr<RenderTexture> TextureManager::createRenderTexture(const RENDER_TEXTURE_DESC& desc,
  119. UINT32 deviceIdx)
  120. {
  121. SPtr<RenderTexture> newRT = createRenderTextureInternal(desc, deviceIdx);
  122. newRT->initialize();
  123. return newRT;
  124. }
  125. }
  126. }