BsVulkanTextureManager.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.format = PF_RGBA8;
  59. desc.usage = TU_STATIC;
  60. mDummyReadTextures[idx] = std::static_pointer_cast<VulkanTexture>(createTexture(desc));
  61. mDummyReadTextures[idx]->writeData(*pixelData);
  62. desc.usage = TU_LOADSTORE;
  63. mDummyStorageTextures[idx] = std::static_pointer_cast<VulkanTexture>(createTexture(desc));
  64. idx++;
  65. }
  66. }
  67. VkImageView VulkanTextureManager::getDummyImageView(GpuParamObjectType type, UINT32 deviceIdx) const
  68. {
  69. SPtr<VulkanTexture> texture;
  70. switch(type)
  71. {
  72. case GPOT_TEXTURE2DMS:
  73. case GPOT_TEXTURE2D:
  74. texture = mDummyReadTextures[2];
  75. break;
  76. case GPOT_RWTEXTURE2D:
  77. case GPOT_RWTEXTURE2DMS:
  78. texture = mDummyStorageTextures[2];
  79. break;
  80. case GPOT_TEXTURECUBE:
  81. texture = mDummyReadTextures[5];
  82. break;
  83. case GPOT_TEXTURECUBEARRAY:
  84. texture = mDummyReadTextures[6];
  85. break;
  86. case GPOT_TEXTURE2DARRAY:
  87. case GPOT_TEXTURE2DMSARRAY:
  88. texture = mDummyReadTextures[3];
  89. break;
  90. case GPOT_RWTEXTURE2DARRAY:
  91. case GPOT_RWTEXTURE2DMSARRAY:
  92. texture = mDummyStorageTextures[3];
  93. break;
  94. case GPOT_TEXTURE3D:
  95. texture = mDummyReadTextures[4];
  96. break;
  97. case GPOT_RWTEXTURE3D:
  98. texture = mDummyStorageTextures[4];
  99. break;
  100. case GPOT_TEXTURE1D:
  101. texture = mDummyReadTextures[0];
  102. break;
  103. case GPOT_TEXTURE1DARRAY:
  104. texture = mDummyReadTextures[1];
  105. break;
  106. case GPOT_RWTEXTURE1D:
  107. texture = mDummyStorageTextures[0];
  108. break;
  109. case GPOT_RWTEXTURE1DARRAY:
  110. texture = mDummyStorageTextures[1];
  111. break;
  112. }
  113. return texture->getResource(deviceIdx)->getView(false);
  114. }
  115. SPtr<Texture> VulkanTextureManager::createTextureInternal(const TEXTURE_DESC& desc,
  116. const SPtr<PixelData>& initialData, GpuDeviceFlags deviceMask)
  117. {
  118. VulkanTexture* tex = new (bs_alloc<VulkanTexture>()) VulkanTexture(desc, initialData, deviceMask);
  119. SPtr<VulkanTexture> texPtr = bs_shared_ptr<VulkanTexture>(tex);
  120. texPtr->_setThisPtr(texPtr);
  121. return texPtr;
  122. }
  123. SPtr<RenderTexture> VulkanTextureManager::createRenderTextureInternal(const RENDER_TEXTURE_DESC& desc,
  124. UINT32 deviceIdx)
  125. {
  126. SPtr<VulkanRenderTexture> texPtr = bs_shared_ptr_new<VulkanRenderTexture>(desc, deviceIdx);
  127. texPtr->_setThisPtr(texPtr);
  128. return texPtr;
  129. }
  130. }
  131. }