BsGLTexture.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGLTexture.h"
  4. #include "BsGLSupport.h"
  5. #include "BsGLPixelFormat.h"
  6. #include "BsGLPixelBuffer.h"
  7. #include "Error/BsException.h"
  8. #include "Utility/BsBitwise.h"
  9. #include "CoreThread/BsCoreThread.h"
  10. #include "Managers/BsTextureManager.h"
  11. #include "BsGLRenderTexture.h"
  12. #include "BsGLTextureView.h"
  13. #include "Profiling/BsRenderStats.h"
  14. #include "BsGLCommandBuffer.h"
  15. namespace bs { namespace ct
  16. {
  17. GLTexture::GLTexture(GLSupport& support, const TEXTURE_DESC& desc, const SPtr<PixelData>& initialData,
  18. GpuDeviceFlags deviceMask)
  19. : Texture(desc, initialData, deviceMask),
  20. mTextureID(0), mGLFormat(0), mInternalFormat(PF_UNKNOWN), mGLSupport(support)
  21. {
  22. assert((deviceMask == GDF_DEFAULT || deviceMask == GDF_PRIMARY) && "Multiple GPUs not supported natively on OpenGL.");
  23. }
  24. GLTexture::~GLTexture()
  25. {
  26. mSurfaceList.clear();
  27. glDeleteTextures(1, &mTextureID);
  28. BS_CHECK_GL_ERROR();
  29. clearBufferViews();
  30. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_Texture);
  31. }
  32. void GLTexture::initialize()
  33. {
  34. UINT32 width = mProperties.getWidth();
  35. UINT32 height = mProperties.getHeight();
  36. UINT32 depth = mProperties.getDepth();
  37. TextureType texType = mProperties.getTextureType();
  38. int usage = mProperties.getUsage();
  39. UINT32 numMips = mProperties.getNumMipmaps();
  40. UINT32 numFaces = mProperties.getNumFaces();
  41. PixelFormat pixFormat = mProperties.getFormat();
  42. mInternalFormat = GLPixelUtil::getClosestSupportedPF(pixFormat, texType, usage);
  43. if (pixFormat != mInternalFormat)
  44. {
  45. LOGWRN(StringUtil::format("Provided pixel format is not supported by the driver: {0}. Falling back on: {1}.",
  46. pixFormat, mInternalFormat));
  47. }
  48. // Check requested number of mipmaps
  49. UINT32 maxMips = PixelUtil::getMaxMipmaps(width, height, depth, mProperties.getFormat());
  50. if (numMips > maxMips)
  51. {
  52. LOGERR("Invalid number of mipmaps. Maximum allowed is: " + toString(maxMips));
  53. numMips = maxMips;
  54. }
  55. if ((usage & TU_DEPTHSTENCIL) != 0)
  56. {
  57. if (texType != TEX_TYPE_2D && texType != TEX_TYPE_CUBE_MAP)
  58. {
  59. LOGERR("Only 2D and cubemap depth stencil textures are supported. Ignoring depth-stencil flag.");
  60. usage &= ~TU_DEPTHSTENCIL;
  61. }
  62. }
  63. // Include the base mip level
  64. numMips += 1;
  65. // Generate texture handle
  66. glGenTextures(1, &mTextureID);
  67. BS_CHECK_GL_ERROR();
  68. // Set texture type
  69. glBindTexture(getGLTextureTarget(), mTextureID);
  70. BS_CHECK_GL_ERROR();
  71. if(mProperties.getNumSamples() <= 1)
  72. {
  73. // This needs to be set otherwise the texture doesn't get rendered
  74. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_MAX_LEVEL, numMips - 1);
  75. BS_CHECK_GL_ERROR();
  76. }
  77. // Allocate internal buffer so that glTexSubImageXD can be used
  78. mGLFormat = GLPixelUtil::getGLInternalFormat(mInternalFormat, mProperties.isHardwareGammaEnabled());
  79. UINT32 sampleCount = mProperties.getNumSamples();
  80. if((usage & (TU_RENDERTARGET | TU_DEPTHSTENCIL)) != 0 && mProperties.getTextureType() == TEX_TYPE_2D && sampleCount > 1)
  81. {
  82. if (numFaces <= 1)
  83. {
  84. // Create immutable storage if available, fallback to mutable
  85. #if BS_OPENGL_4_3 || BS_OPENGLES_3_1
  86. glTexStorage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, sampleCount, mGLFormat, width, height, GL_TRUE);
  87. BS_CHECK_GL_ERROR();
  88. #else
  89. glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, sampleCount, mGLFormat, width, height, GL_TRUE);
  90. BS_CHECK_GL_ERROR();
  91. #endif
  92. }
  93. else
  94. {
  95. // Create immutable storage if available, fallback to mutable
  96. #if BS_OPENGL_4_3 || BS_OPENGLES_3_2
  97. glTexStorage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, sampleCount, mGLFormat, width, height, numFaces, GL_TRUE);
  98. BS_CHECK_GL_ERROR();
  99. #else
  100. glTexImage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, sampleCount, mGLFormat, width, height, numFaces, GL_TRUE);
  101. BS_CHECK_GL_ERROR();
  102. #endif
  103. }
  104. }
  105. else
  106. {
  107. // Create immutable storage if available, fallback to mutable
  108. #if BS_OPENGL_4_2 || BS_OPENGLES_3_1
  109. switch (texType)
  110. {
  111. case TEX_TYPE_1D:
  112. {
  113. if (numFaces <= 1)
  114. {
  115. glTexStorage1D(GL_TEXTURE_1D, numMips, mGLFormat, width);
  116. BS_CHECK_GL_ERROR();
  117. }
  118. else
  119. {
  120. glTexStorage2D(GL_TEXTURE_1D_ARRAY, numMips, mGLFormat, width, numFaces);
  121. BS_CHECK_GL_ERROR();
  122. }
  123. }
  124. break;
  125. case TEX_TYPE_2D:
  126. {
  127. if (numFaces <= 1)
  128. {
  129. glTexStorage2D(GL_TEXTURE_2D, numMips, mGLFormat, width, height);
  130. BS_CHECK_GL_ERROR();
  131. }
  132. else
  133. {
  134. glTexStorage3D(GL_TEXTURE_2D_ARRAY, numMips, mGLFormat, width, height, numFaces);
  135. BS_CHECK_GL_ERROR();
  136. }
  137. }
  138. break;
  139. case TEX_TYPE_3D:
  140. glTexStorage3D(GL_TEXTURE_3D, numMips, mGLFormat, width, height, depth);
  141. BS_CHECK_GL_ERROR();
  142. break;
  143. case TEX_TYPE_CUBE_MAP:
  144. {
  145. if (numFaces <= 6)
  146. {
  147. glTexStorage2D(GL_TEXTURE_CUBE_MAP, numMips, mGLFormat, width, height);
  148. BS_CHECK_GL_ERROR();
  149. }
  150. else
  151. {
  152. glTexStorage3D(GL_TEXTURE_CUBE_MAP_ARRAY, numMips, mGLFormat, width, height, numFaces);
  153. BS_CHECK_GL_ERROR();
  154. }
  155. }
  156. break;
  157. }
  158. #else
  159. if((usage & TU_DEPTHSTENCIL) != 0)
  160. {
  161. GLenum depthStencilType = GLPixelUtil::getDepthStencilTypeFromPF(mInternalFormat);
  162. GLenum depthStencilFormat = GLPixelUtil::getDepthStencilFormatFromPF(mInternalFormat);
  163. if(texType == TEX_TYPE_2D)
  164. {
  165. if (numFaces <= 1)
  166. {
  167. glTexImage2D(GL_TEXTURE_2D, 0, mGLFormat, width, height, 0,
  168. depthStencilFormat, depthStencilType, nullptr);
  169. }
  170. else
  171. {
  172. glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, mGLFormat, width, height, numFaces, 0,
  173. depthStencilFormat, depthStencilType, nullptr);
  174. }
  175. }
  176. else if(texType == TEX_TYPE_CUBE_MAP)
  177. {
  178. if (numFaces <= 6)
  179. {
  180. for (UINT32 face = 0; face < 6; face++)
  181. {
  182. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, mGLFormat,
  183. width, height, 0, depthStencilFormat, depthStencilType, nullptr);
  184. }
  185. }
  186. else
  187. {
  188. glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, mGLFormat,
  189. width, height, numFaces, 0, depthStencilFormat, depthStencilType, nullptr);
  190. }
  191. }
  192. else
  193. {
  194. LOGERR("Unsupported texture type for depth-stencil attachment usage.");
  195. }
  196. }
  197. else
  198. {
  199. GLenum baseFormat = GLPixelUtil::getGLOriginFormat(mInternalFormat);
  200. GLenum baseDataType = GLPixelUtil::getGLOriginDataType(mInternalFormat);
  201. for (UINT32 mip = 0; mip <= numMips; mip++)
  202. {
  203. switch (texType)
  204. {
  205. case TEX_TYPE_1D:
  206. {
  207. if (numFaces <= 1)
  208. glTexImage1D(GL_TEXTURE_1D, mip, mGLFormat, width, 0, baseFormat, baseDataType, nullptr);
  209. else
  210. glTexImage2D(GL_TEXTURE_1D_ARRAY, mip, mGLFormat, width, numFaces, 0, baseFormat, baseDataType, nullptr);
  211. }
  212. break;
  213. case TEX_TYPE_2D:
  214. {
  215. if (numFaces <= 1)
  216. glTexImage2D(GL_TEXTURE_2D, mip, mGLFormat, width, height, 0, baseFormat, baseDataType, nullptr);
  217. else
  218. glTexImage3D(GL_TEXTURE_2D_ARRAY, mip, mGLFormat, width, height, numFaces, 0, baseFormat, baseDataType, nullptr);
  219. }
  220. break;
  221. case TEX_TYPE_3D:
  222. glTexImage3D(GL_TEXTURE_3D, mip, mGLFormat, width, height,
  223. depth, 0, baseFormat, baseDataType, nullptr);
  224. break;
  225. case TEX_TYPE_CUBE_MAP:
  226. {
  227. if (numFaces <= 6)
  228. {
  229. for (UINT32 face = 0; face < 6; face++)
  230. {
  231. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, mip, mGLFormat,
  232. width, height, 0, baseFormat, baseDataType, nullptr);
  233. }
  234. }
  235. else
  236. {
  237. glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, mip, mGLFormat,
  238. width, height, numFaces, 0, baseFormat, baseDataType, nullptr);
  239. }
  240. }
  241. break;
  242. }
  243. if(width > 1)
  244. width = width/2;
  245. if(height > 1)
  246. height = height/2;
  247. if(depth > 1)
  248. depth = depth/2;
  249. }
  250. }
  251. #endif
  252. }
  253. createSurfaceList();
  254. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_Texture);
  255. Texture::initialize();
  256. }
  257. GLenum GLTexture::getGLTextureTarget() const
  258. {
  259. return getGLTextureTarget(mProperties.getTextureType(), mProperties.getNumSamples(), mProperties.getNumFaces());
  260. }
  261. GLuint GLTexture::getGLID() const
  262. {
  263. THROW_IF_NOT_CORE_THREAD;
  264. return mTextureID;
  265. }
  266. GLenum GLTexture::getGLTextureTarget(TextureType type, UINT32 numSamples, UINT32 numFaces)
  267. {
  268. switch (type)
  269. {
  270. case TEX_TYPE_1D:
  271. if (numFaces <= 1)
  272. return GL_TEXTURE_1D;
  273. else
  274. return GL_TEXTURE_1D_ARRAY;
  275. case TEX_TYPE_2D:
  276. if (numSamples > 1)
  277. {
  278. if (numFaces <= 1)
  279. return GL_TEXTURE_2D_MULTISAMPLE;
  280. else
  281. return GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
  282. }
  283. else
  284. {
  285. if (numFaces <= 1)
  286. return GL_TEXTURE_2D;
  287. else
  288. return GL_TEXTURE_2D_ARRAY;
  289. }
  290. case TEX_TYPE_3D:
  291. return GL_TEXTURE_3D;
  292. case TEX_TYPE_CUBE_MAP:
  293. if (numFaces <= 6)
  294. return GL_TEXTURE_CUBE_MAP;
  295. else
  296. return GL_TEXTURE_CUBE_MAP_ARRAY;
  297. default:
  298. return 0;
  299. };
  300. }
  301. PixelData GLTexture::lockImpl(GpuLockOptions options, UINT32 mipLevel, UINT32 face, UINT32 deviceIdx,
  302. UINT32 queueIdx)
  303. {
  304. if (mProperties.getNumSamples() > 1)
  305. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  306. if(mLockedBuffer != nullptr)
  307. BS_EXCEPT(InternalErrorException, "Trying to lock a buffer that's already locked.");
  308. UINT32 mipWidth = std::max(1u, mProperties.getWidth() >> mipLevel);
  309. UINT32 mipHeight = std::max(1u, mProperties.getHeight() >> mipLevel);
  310. UINT32 mipDepth = std::max(1u, mProperties.getDepth() >> mipLevel);
  311. PixelData lockedArea(mipWidth, mipHeight, mipDepth, mProperties.getFormat());
  312. mLockedBuffer = getBuffer(face, mipLevel);
  313. lockedArea.setExternalBuffer((UINT8*)mLockedBuffer->lock(options));
  314. return lockedArea;
  315. }
  316. void GLTexture::unlockImpl()
  317. {
  318. if (mLockedBuffer == nullptr)
  319. {
  320. LOGERR("Trying to unlock a buffer that's not locked.");
  321. return;
  322. }
  323. mLockedBuffer->unlock();
  324. mLockedBuffer = nullptr;
  325. }
  326. void GLTexture::readDataImpl(PixelData& dest, UINT32 mipLevel, UINT32 face, UINT32 deviceIdx, UINT32 queueIdx)
  327. {
  328. if (mProperties.getNumSamples() > 1)
  329. {
  330. LOGERR("Multisampled textures cannot be accessed from the CPU directly.");
  331. return;
  332. }
  333. if(dest.getFormat() != mInternalFormat)
  334. {
  335. PixelData temp(dest.getExtents(), mInternalFormat);
  336. temp.allocateInternalBuffer();
  337. getBuffer(face, mipLevel)->download(temp);
  338. PixelUtil::bulkPixelConversion(temp, dest);
  339. }
  340. else
  341. getBuffer(face, mipLevel)->download(dest);
  342. }
  343. void GLTexture::writeDataImpl(const PixelData& src, UINT32 mipLevel, UINT32 face, bool discardWholeBuffer,
  344. UINT32 queueIdx)
  345. {
  346. if (mProperties.getNumSamples() > 1)
  347. {
  348. LOGERR("Multisampled textures cannot be accessed from the CPU directly.");
  349. return;
  350. }
  351. if (src.getFormat() != mInternalFormat)
  352. {
  353. PixelData temp(src.getExtents(), mInternalFormat);
  354. temp.allocateInternalBuffer();
  355. PixelUtil::bulkPixelConversion(src, temp);
  356. getBuffer(face, mipLevel)->upload(temp, temp.getExtents());
  357. }
  358. else
  359. getBuffer(face, mipLevel)->upload(src, src.getExtents());
  360. }
  361. void GLTexture::copyImpl(const SPtr<Texture>& target, const TEXTURE_COPY_DESC& desc,
  362. const SPtr<CommandBuffer>& commandBuffer)
  363. {
  364. auto executeRef = [this](const SPtr<Texture>& target, const TEXTURE_COPY_DESC& desc)
  365. {
  366. GLTexture* destTex = static_cast<GLTexture*>(target.get());
  367. GLTextureBuffer* dest = static_cast<GLTextureBuffer*>(destTex->getBuffer(desc.dstFace, desc.dstMip).get());
  368. GLTextureBuffer* src = static_cast<GLTextureBuffer*>(getBuffer(desc.srcFace, desc.srcMip).get());
  369. bool copyEntireSurface = desc.srcVolume.getWidth() == 0 ||
  370. desc.srcVolume.getHeight() == 0 ||
  371. desc.srcVolume.getDepth() == 0;
  372. if(copyEntireSurface)
  373. dest->blitFromTexture(src);
  374. else
  375. {
  376. PixelVolume dstVolume;
  377. dstVolume.left = (UINT32)desc.dstPosition.x;
  378. dstVolume.top = (UINT32)desc.dstPosition.y;
  379. dstVolume.front = (UINT32)desc.dstPosition.z;
  380. dstVolume.right = dstVolume.left + desc.srcVolume.getWidth();
  381. dstVolume.bottom = dstVolume.top + desc.srcVolume.getHeight();
  382. dstVolume.back = dstVolume.front + desc.srcVolume.getDepth();
  383. dest->blitFromTexture(src, desc.srcVolume, dstVolume);
  384. }
  385. };
  386. if (commandBuffer == nullptr)
  387. executeRef(target, desc);
  388. else
  389. {
  390. auto execute = [=]() { executeRef(target, desc); };
  391. SPtr<GLCommandBuffer> cb = std::static_pointer_cast<GLCommandBuffer>(commandBuffer);
  392. cb->queueCommand(execute);
  393. }
  394. }
  395. void GLTexture::createSurfaceList()
  396. {
  397. mSurfaceList.clear();
  398. for (UINT32 face = 0; face < mProperties.getNumFaces(); face++)
  399. {
  400. for (UINT32 mip = 0; mip <= mProperties.getNumMipmaps(); mip++)
  401. {
  402. GLPixelBuffer *buf = bs_new<GLTextureBuffer>(getGLTextureTarget(), mTextureID, face, mip,
  403. static_cast<GpuBufferUsage>(mProperties.getUsage()), mInternalFormat, mProperties.getNumSamples());
  404. mSurfaceList.push_back(bs_shared_ptr<GLPixelBuffer>(buf));
  405. if(buf->getWidth() == 0 || buf->getHeight() == 0 || buf->getDepth() == 0)
  406. {
  407. BS_EXCEPT(RenderingAPIException,
  408. "Zero sized texture surface on texture face "
  409. + toString(face)
  410. + " mipmap "+toString(mip)
  411. + ". Probably, the GL driver refused to create the texture.");
  412. }
  413. }
  414. }
  415. }
  416. SPtr<GLPixelBuffer> GLTexture::getBuffer(UINT32 face, UINT32 mipmap)
  417. {
  418. THROW_IF_NOT_CORE_THREAD;
  419. if(face >= mProperties.getNumFaces())
  420. BS_EXCEPT(InvalidParametersException, "Face index out of range");
  421. if (mipmap > mProperties.getNumMipmaps())
  422. BS_EXCEPT(InvalidParametersException, "Mipmap index out of range");
  423. unsigned int idx = face * (mProperties.getNumMipmaps() + 1) + mipmap;
  424. assert(idx < mSurfaceList.size());
  425. return mSurfaceList[idx];
  426. }
  427. SPtr<TextureView> GLTexture::createView(const TEXTURE_VIEW_DESC& desc)
  428. {
  429. return bs_shared_ptr<GLTextureView>(new (bs_alloc<GLTextureView>()) GLTextureView(this, desc));
  430. }
  431. }}