BsVulkanTextureManager.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Managers/BsVulkanTextureManager.h"
  4. #include "BsVulkanTexture.h"
  5. #include "BsVulkanRenderTexture.h"
  6. #include "BsVulkanResource.h"
  7. #include "BsVulkanUtility.h"
  8. namespace bs
  9. {
  10. struct DummyTexFormat
  11. {
  12. TextureType type;
  13. int arraySize;
  14. int width;
  15. int height;
  16. int depth;
  17. };
  18. const static DummyTexFormat DummyTexTypes[] =
  19. {
  20. { TEX_TYPE_1D, 1, 2, 1, 1 },
  21. { TEX_TYPE_1D, 2, 2, 1, 1 },
  22. { TEX_TYPE_2D, 1, 2, 2, 1 },
  23. { TEX_TYPE_2D, 2, 2, 2, 1 },
  24. { TEX_TYPE_3D, 1, 2, 2, 2 },
  25. { TEX_TYPE_CUBE_MAP, 1, 2, 2, 1 },
  26. { TEX_TYPE_CUBE_MAP, 2, 2, 2, 1 }
  27. };
  28. SPtr<RenderTexture> VulkanTextureManager::createRenderTextureImpl(const RENDER_TEXTURE_DESC& desc)
  29. {
  30. VulkanRenderTexture* tex = new (bs_alloc<VulkanRenderTexture>()) VulkanRenderTexture(desc);
  31. return bs_core_ptr<VulkanRenderTexture>(tex);
  32. }
  33. PixelFormat VulkanTextureManager::getNativeFormat(TextureType ttype, PixelFormat format, int usage, bool hwGamma)
  34. {
  35. PixelUtil::checkFormat(format, ttype, usage);
  36. if (ct::VulkanUtility::getPixelFormat(format, hwGamma) == VK_FORMAT_UNDEFINED)
  37. return PF_RGBA8;
  38. return format;
  39. }
  40. namespace ct
  41. {
  42. void VulkanTextureManager::onStartUp()
  43. {
  44. TextureManager::onStartUp();
  45. int idx = 0;
  46. for(auto& entry : DummyTexTypes)
  47. {
  48. SPtr<PixelData> pixelData = PixelData::create(entry.width, entry.height, entry.depth, PF_RGBA8);
  49. for(int depth = 0; depth < entry.depth; depth++)
  50. for(int height = 0; height < entry.height; height++)
  51. for(int width = 0; width < entry.width; width++)
  52. pixelData->setColorAt(Color::White, width, height, depth);
  53. TEXTURE_DESC desc;
  54. desc.type = entry.type;
  55. desc.width = entry.width;
  56. desc.height = entry.height;
  57. desc.depth = entry.depth;
  58. desc.numArraySlices = entry.arraySize;
  59. desc.format = PF_RGBA8;
  60. desc.usage = TU_STATIC;
  61. mDummyReadTextures[idx] = std::static_pointer_cast<VulkanTexture>(createTexture(desc));
  62. mDummyReadTextures[idx]->writeData(*pixelData);
  63. desc.usage = TU_LOADSTORE;
  64. mDummyStorageTextures[idx] = std::static_pointer_cast<VulkanTexture>(createTexture(desc));
  65. idx++;
  66. }
  67. }
  68. VkImageView VulkanTextureManager::getDummyImageView(GpuParamObjectType type, UINT32 deviceIdx) const
  69. {
  70. SPtr<VulkanTexture> texture;
  71. switch(type)
  72. {
  73. case GPOT_TEXTURE2DMS:
  74. case GPOT_TEXTURE2D:
  75. texture = mDummyReadTextures[2];
  76. break;
  77. case GPOT_RWTEXTURE2D:
  78. case GPOT_RWTEXTURE2DMS:
  79. texture = mDummyStorageTextures[2];
  80. break;
  81. case GPOT_TEXTURECUBE:
  82. texture = mDummyReadTextures[5];
  83. break;
  84. case GPOT_TEXTURECUBEARRAY:
  85. texture = mDummyReadTextures[6];
  86. break;
  87. case GPOT_TEXTURE2DARRAY:
  88. case GPOT_TEXTURE2DMSARRAY:
  89. texture = mDummyReadTextures[3];
  90. break;
  91. case GPOT_RWTEXTURE2DARRAY:
  92. case GPOT_RWTEXTURE2DMSARRAY:
  93. texture = mDummyStorageTextures[3];
  94. break;
  95. case GPOT_TEXTURE3D:
  96. texture = mDummyReadTextures[4];
  97. break;
  98. case GPOT_RWTEXTURE3D:
  99. texture = mDummyStorageTextures[4];
  100. break;
  101. case GPOT_TEXTURE1D:
  102. texture = mDummyReadTextures[0];
  103. break;
  104. case GPOT_TEXTURE1DARRAY:
  105. texture = mDummyReadTextures[1];
  106. break;
  107. case GPOT_RWTEXTURE1D:
  108. texture = mDummyStorageTextures[0];
  109. break;
  110. case GPOT_RWTEXTURE1DARRAY:
  111. texture = mDummyStorageTextures[1];
  112. break;
  113. default:
  114. break;
  115. }
  116. return texture->getResource(deviceIdx)->getView(false);
  117. }
  118. SPtr<Texture> VulkanTextureManager::createTextureInternal(const TEXTURE_DESC& desc,
  119. const SPtr<PixelData>& initialData, GpuDeviceFlags deviceMask)
  120. {
  121. VulkanTexture* tex = new (bs_alloc<VulkanTexture>()) VulkanTexture(desc, initialData, deviceMask);
  122. SPtr<VulkanTexture> texPtr = bs_shared_ptr<VulkanTexture>(tex);
  123. texPtr->_setThisPtr(texPtr);
  124. return texPtr;
  125. }
  126. SPtr<RenderTexture> VulkanTextureManager::createRenderTextureInternal(const RENDER_TEXTURE_DESC& desc,
  127. UINT32 deviceIdx)
  128. {
  129. SPtr<VulkanRenderTexture> texPtr = bs_shared_ptr_new<VulkanRenderTexture>(desc, deviceIdx);
  130. texPtr->_setThisPtr(texPtr);
  131. return texPtr;
  132. }
  133. }
  134. }