BsGLTexture.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 && mProperties.getTextureType() == TEX_TYPE_2D)
  160. {
  161. GLenum depthStencilType = GLPixelUtil::getDepthStencilTypeFromPF(mInternalFormat);
  162. GLenum depthStencilFormat = GLPixelUtil::getDepthStencilFormatFromPF(mInternalFormat);
  163. if (numFaces <= 1)
  164. {
  165. glTexImage2D(GL_TEXTURE_2D, 0, mGLFormat, width, height, 0,
  166. depthStencilFormat, depthStencilType, nullptr);
  167. }
  168. else
  169. {
  170. glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, mGLFormat, width, height, numFaces, 0,
  171. depthStencilFormat, depthStencilType, nullptr);
  172. }
  173. }
  174. else
  175. {
  176. GLenum baseFormat = GLPixelUtil::getGLOriginFormat(mInternalFormat);
  177. GLenum baseDataType = GLPixelUtil::getGLOriginDataType(mInternalFormat);
  178. for (UINT32 mip = 0; mip <= numMips; mip++)
  179. {
  180. switch (texType)
  181. {
  182. case TEX_TYPE_1D:
  183. {
  184. if (numFaces <= 1)
  185. glTexImage1D(GL_TEXTURE_1D, mip, mGLFormat, width, 0, baseFormat, baseDataType, nullptr);
  186. else
  187. glTexImage2D(GL_TEXTURE_1D_ARRAY, mip, mGLFormat, width, numFaces, 0, baseFormat, baseDataType, nullptr);
  188. }
  189. break;
  190. case TEX_TYPE_2D:
  191. {
  192. if (numFaces <= 1)
  193. glTexImage2D(GL_TEXTURE_2D, mip, mGLFormat, width, height, 0, baseFormat, baseDataType, nullptr);
  194. else
  195. glTexImage3D(GL_TEXTURE_2D_ARRAY, mip, mGLFormat, width, height, numFaces, 0, baseFormat, baseDataType, nullptr);
  196. }
  197. break;
  198. case TEX_TYPE_3D:
  199. glTexImage3D(GL_TEXTURE_3D, mip, mGLFormat, width, height,
  200. depth, 0, baseFormat, baseDataType, nullptr);
  201. break;
  202. case TEX_TYPE_CUBE_MAP:
  203. {
  204. if (numFaces <= 6)
  205. {
  206. for (UINT32 face = 0; face < 6; face++)
  207. {
  208. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, mip, mGLFormat,
  209. width, height, 0, baseFormat, baseDataType, nullptr);
  210. }
  211. }
  212. else
  213. {
  214. glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, mip, mGLFormat,
  215. width, height, numFaces, 0, baseFormat, baseDataType, nullptr);
  216. }
  217. }
  218. break;
  219. }
  220. if(width > 1)
  221. width = width/2;
  222. if(height > 1)
  223. height = height/2;
  224. if(depth > 1)
  225. depth = depth/2;
  226. }
  227. }
  228. #endif
  229. }
  230. createSurfaceList();
  231. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_Texture);
  232. Texture::initialize();
  233. }
  234. GLenum GLTexture::getGLTextureTarget() const
  235. {
  236. return getGLTextureTarget(mProperties.getTextureType(), mProperties.getNumSamples(), mProperties.getNumFaces());
  237. }
  238. GLuint GLTexture::getGLID() const
  239. {
  240. THROW_IF_NOT_CORE_THREAD;
  241. return mTextureID;
  242. }
  243. GLenum GLTexture::getGLTextureTarget(TextureType type, UINT32 numSamples, UINT32 numFaces)
  244. {
  245. switch (type)
  246. {
  247. case TEX_TYPE_1D:
  248. if (numFaces <= 1)
  249. return GL_TEXTURE_1D;
  250. else
  251. return GL_TEXTURE_1D_ARRAY;
  252. case TEX_TYPE_2D:
  253. if (numSamples > 1)
  254. {
  255. if (numFaces <= 1)
  256. return GL_TEXTURE_2D_MULTISAMPLE;
  257. else
  258. return GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
  259. }
  260. else
  261. {
  262. if (numFaces <= 1)
  263. return GL_TEXTURE_2D;
  264. else
  265. return GL_TEXTURE_2D_ARRAY;
  266. }
  267. case TEX_TYPE_3D:
  268. return GL_TEXTURE_3D;
  269. case TEX_TYPE_CUBE_MAP:
  270. if (numFaces <= 6)
  271. return GL_TEXTURE_CUBE_MAP;
  272. else
  273. return GL_TEXTURE_CUBE_MAP_ARRAY;
  274. default:
  275. return 0;
  276. };
  277. }
  278. PixelData GLTexture::lockImpl(GpuLockOptions options, UINT32 mipLevel, UINT32 face, UINT32 deviceIdx,
  279. UINT32 queueIdx)
  280. {
  281. if (mProperties.getNumSamples() > 1)
  282. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  283. if(mLockedBuffer != nullptr)
  284. BS_EXCEPT(InternalErrorException, "Trying to lock a buffer that's already locked.");
  285. UINT32 mipWidth = std::max(1u, mProperties.getWidth() >> mipLevel);
  286. UINT32 mipHeight = std::max(1u, mProperties.getHeight() >> mipLevel);
  287. UINT32 mipDepth = std::max(1u, mProperties.getDepth() >> mipLevel);
  288. PixelData lockedArea(mipWidth, mipHeight, mipDepth, mProperties.getFormat());
  289. mLockedBuffer = getBuffer(face, mipLevel);
  290. lockedArea.setExternalBuffer((UINT8*)mLockedBuffer->lock(options));
  291. return lockedArea;
  292. }
  293. void GLTexture::unlockImpl()
  294. {
  295. if (mLockedBuffer == nullptr)
  296. {
  297. LOGERR("Trying to unlock a buffer that's not locked.");
  298. return;
  299. }
  300. mLockedBuffer->unlock();
  301. mLockedBuffer = nullptr;
  302. }
  303. void GLTexture::readDataImpl(PixelData& dest, UINT32 mipLevel, UINT32 face, UINT32 deviceIdx, UINT32 queueIdx)
  304. {
  305. if (mProperties.getNumSamples() > 1)
  306. {
  307. LOGERR("Multisampled textures cannot be accessed from the CPU directly.");
  308. return;
  309. }
  310. if(dest.getFormat() != mInternalFormat)
  311. {
  312. PixelData temp(dest.getExtents(), mInternalFormat);
  313. temp.allocateInternalBuffer();
  314. getBuffer(face, mipLevel)->download(temp);
  315. PixelUtil::bulkPixelConversion(temp, dest);
  316. }
  317. else
  318. getBuffer(face, mipLevel)->download(dest);
  319. }
  320. void GLTexture::writeDataImpl(const PixelData& src, UINT32 mipLevel, UINT32 face, bool discardWholeBuffer,
  321. UINT32 queueIdx)
  322. {
  323. if (mProperties.getNumSamples() > 1)
  324. {
  325. LOGERR("Multisampled textures cannot be accessed from the CPU directly.");
  326. return;
  327. }
  328. if (src.getFormat() != mInternalFormat)
  329. {
  330. PixelData temp(src.getExtents(), mInternalFormat);
  331. temp.allocateInternalBuffer();
  332. PixelUtil::bulkPixelConversion(src, temp);
  333. getBuffer(face, mipLevel)->upload(temp, temp.getExtents());
  334. }
  335. else
  336. getBuffer(face, mipLevel)->upload(src, src.getExtents());
  337. }
  338. void GLTexture::copyImpl(const SPtr<Texture>& target, const TEXTURE_COPY_DESC& desc,
  339. const SPtr<CommandBuffer>& commandBuffer)
  340. {
  341. auto executeRef = [this](const SPtr<Texture>& target, const TEXTURE_COPY_DESC& desc)
  342. {
  343. GLTexture* destTex = static_cast<GLTexture*>(target.get());
  344. GLTextureBuffer* dest = static_cast<GLTextureBuffer*>(destTex->getBuffer(desc.dstFace, desc.dstMip).get());
  345. GLTextureBuffer* src = static_cast<GLTextureBuffer*>(getBuffer(desc.srcFace, desc.srcMip).get());
  346. bool copyEntireSurface = desc.srcVolume.getWidth() == 0 ||
  347. desc.srcVolume.getHeight() == 0 ||
  348. desc.srcVolume.getDepth() == 0;
  349. if(copyEntireSurface)
  350. dest->blitFromTexture(src);
  351. else
  352. {
  353. PixelVolume dstVolume;
  354. dstVolume.left = (UINT32)desc.dstPosition.x;
  355. dstVolume.top = (UINT32)desc.dstPosition.y;
  356. dstVolume.front = (UINT32)desc.dstPosition.z;
  357. dstVolume.right = dstVolume.left + desc.srcVolume.getWidth();
  358. dstVolume.bottom = dstVolume.top + desc.srcVolume.getHeight();
  359. dstVolume.back = dstVolume.front + desc.srcVolume.getDepth();
  360. dest->blitFromTexture(src, desc.srcVolume, dstVolume);
  361. }
  362. };
  363. if (commandBuffer == nullptr)
  364. executeRef(target, desc);
  365. else
  366. {
  367. auto execute = [=]() { executeRef(target, desc); };
  368. SPtr<GLCommandBuffer> cb = std::static_pointer_cast<GLCommandBuffer>(commandBuffer);
  369. cb->queueCommand(execute);
  370. }
  371. }
  372. void GLTexture::createSurfaceList()
  373. {
  374. mSurfaceList.clear();
  375. for (UINT32 face = 0; face < mProperties.getNumFaces(); face++)
  376. {
  377. for (UINT32 mip = 0; mip <= mProperties.getNumMipmaps(); mip++)
  378. {
  379. GLPixelBuffer *buf = bs_new<GLTextureBuffer>(getGLTextureTarget(), mTextureID, face, mip,
  380. static_cast<GpuBufferUsage>(mProperties.getUsage()), mInternalFormat, mProperties.getNumSamples());
  381. mSurfaceList.push_back(bs_shared_ptr<GLPixelBuffer>(buf));
  382. if(buf->getWidth() == 0 || buf->getHeight() == 0 || buf->getDepth() == 0)
  383. {
  384. BS_EXCEPT(RenderingAPIException,
  385. "Zero sized texture surface on texture face "
  386. + toString(face)
  387. + " mipmap "+toString(mip)
  388. + ". Probably, the GL driver refused to create the texture.");
  389. }
  390. }
  391. }
  392. }
  393. SPtr<GLPixelBuffer> GLTexture::getBuffer(UINT32 face, UINT32 mipmap)
  394. {
  395. THROW_IF_NOT_CORE_THREAD;
  396. if(face >= mProperties.getNumFaces())
  397. BS_EXCEPT(InvalidParametersException, "Face index out of range");
  398. if (mipmap > mProperties.getNumMipmaps())
  399. BS_EXCEPT(InvalidParametersException, "Mipmap index out of range");
  400. unsigned int idx = face * (mProperties.getNumMipmaps() + 1) + mipmap;
  401. assert(idx < mSurfaceList.size());
  402. return mSurfaceList[idx];
  403. }
  404. SPtr<TextureView> GLTexture::createView(const TEXTURE_VIEW_DESC& desc)
  405. {
  406. return bs_shared_ptr<GLTextureView>(new (bs_alloc<GLTextureView>()) GLTextureView(this, desc));
  407. }
  408. }}