BsGLPixelBuffer.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGLPixelBuffer.h"
  4. #include "BsGLTexture.h"
  5. #include "BsGLSupport.h"
  6. #include "BsGLPixelFormat.h"
  7. #include "Error/BsException.h"
  8. #include "Utility/BsBitwise.h"
  9. #include "BsGLRenderTexture.h"
  10. #include "Profiling/BsRenderStats.h"
  11. namespace bs { namespace ct
  12. {
  13. GLPixelBuffer::GLPixelBuffer(UINT32 inWidth, UINT32 inHeight, UINT32 inDepth, PixelFormat inFormat, GpuBufferUsage usage)
  14. : mUsage(usage), mIsLocked(false), mWidth(inWidth), mHeight(inHeight), mDepth(inDepth), mFormat(inFormat)
  15. , mBuffer(inWidth, inHeight, inDepth, inFormat)
  16. {
  17. mSizeInBytes = mHeight*mWidth*PixelUtil::getNumElemBytes(mFormat);
  18. mCurrentLockOptions = (GpuLockOptions)0;
  19. }
  20. GLPixelBuffer::~GLPixelBuffer()
  21. {
  22. mBuffer.freeInternalBuffer();
  23. }
  24. void GLPixelBuffer::allocateBuffer()
  25. {
  26. if(mBuffer.getData())
  27. return;
  28. mBuffer.allocateInternalBuffer();
  29. // TODO: use PBO if we're HBU_DYNAMIC
  30. }
  31. void GLPixelBuffer::freeBuffer()
  32. {
  33. if(mUsage & GBU_STATIC)
  34. mBuffer.freeInternalBuffer();
  35. }
  36. void* GLPixelBuffer::lock(UINT32 offset, UINT32 length, GpuLockOptions options)
  37. {
  38. assert(!mIsLocked && "Cannot lock this buffer, it is already locked!");
  39. assert(offset == 0 && length == mSizeInBytes && "Cannot lock memory region, most lock box or entire buffer");
  40. PixelVolume volume(0, 0, 0, mWidth, mHeight, mDepth);
  41. const PixelData& lockedData = lock(volume, options);
  42. return lockedData.getData();
  43. }
  44. const PixelData& GLPixelBuffer::lock(const PixelVolume& lockBox, GpuLockOptions options)
  45. {
  46. allocateBuffer();
  47. if (options != GBL_WRITE_ONLY_DISCARD)
  48. {
  49. // Download the old contents of the texture
  50. download(mBuffer);
  51. }
  52. mCurrentLockOptions = options;
  53. mLockedBox = lockBox;
  54. mCurrentLock = mBuffer.getSubVolume(lockBox);
  55. mIsLocked = true;
  56. return mCurrentLock;
  57. }
  58. void GLPixelBuffer::unlock()
  59. {
  60. assert(mIsLocked && "Cannot unlock this buffer, it is not locked!");
  61. if (mCurrentLockOptions != GBL_READ_ONLY)
  62. {
  63. // From buffer to card, only upload if was locked for writing
  64. upload(mCurrentLock, mLockedBox);
  65. }
  66. freeBuffer();
  67. mIsLocked = false;
  68. }
  69. void GLPixelBuffer::upload(const PixelData& data, const PixelVolume& dest)
  70. {
  71. BS_EXCEPT(RenderingAPIException, "Upload not possible for this pixel buffer type");
  72. }
  73. void GLPixelBuffer::download(const PixelData& data)
  74. {
  75. BS_EXCEPT(RenderingAPIException, "Download not possible for this pixel buffer type");
  76. }
  77. void GLPixelBuffer::blitFromTexture(GLTextureBuffer* src)
  78. {
  79. blitFromTexture(src,
  80. PixelVolume(0, 0, 0, src->getWidth(), src->getHeight(), src->getDepth()),
  81. PixelVolume(0, 0, 0, mWidth, mHeight, mDepth)
  82. );
  83. }
  84. void GLPixelBuffer::blitFromTexture(GLTextureBuffer* src, const PixelVolume& srcBox, const PixelVolume& dstBox)
  85. {
  86. BS_EXCEPT(RenderingAPIException, "BlitFromTexture not possible for this pixel buffer type");
  87. }
  88. void GLPixelBuffer::bindToFramebuffer(GLenum attachment, UINT32 zoffset, bool allLayers)
  89. {
  90. BS_EXCEPT(RenderingAPIException, "Framebuffer bind not possible for this pixel buffer type");
  91. }
  92. GLTextureBuffer::GLTextureBuffer(GLenum target, GLuint id,
  93. GLint face, GLint level, GpuBufferUsage usage,
  94. PixelFormat format, UINT32 multisampleCount):
  95. GLPixelBuffer(0, 0, 0, format, usage),
  96. mTarget(target), mFaceTarget(0), mTextureID(id), mFace(face), mLevel(level), mMultisampleCount(multisampleCount)
  97. {
  98. GLint value = 0;
  99. glBindTexture(mTarget, mTextureID);
  100. BS_CHECK_GL_ERROR();
  101. // Get face identifier
  102. mFaceTarget = mTarget;
  103. if(mTarget == GL_TEXTURE_CUBE_MAP)
  104. mFaceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + (face % 6);
  105. // Get width
  106. glGetTexLevelParameteriv(mFaceTarget, level, GL_TEXTURE_WIDTH, &value);
  107. BS_CHECK_GL_ERROR();
  108. mWidth = value;
  109. // Get height
  110. if(target == GL_TEXTURE_1D)
  111. value = 1; // Height always 1 for 1D textures
  112. else
  113. {
  114. glGetTexLevelParameteriv(mFaceTarget, level, GL_TEXTURE_HEIGHT, &value);
  115. BS_CHECK_GL_ERROR();
  116. }
  117. mHeight = value;
  118. // Get depth
  119. if(target != GL_TEXTURE_3D)
  120. value = 1; // Depth always 1 for non-3D textures
  121. else
  122. {
  123. glGetTexLevelParameteriv(mFaceTarget, level, GL_TEXTURE_DEPTH, &value);
  124. BS_CHECK_GL_ERROR();
  125. }
  126. mDepth = value;
  127. // Default
  128. mSizeInBytes = PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  129. // Set up pixel box
  130. mBuffer = PixelData(mWidth, mHeight, mDepth, mFormat);
  131. }
  132. GLTextureBuffer::~GLTextureBuffer()
  133. { }
  134. void GLTextureBuffer::upload(const PixelData &data, const PixelVolume &dest)
  135. {
  136. if ((mUsage & TU_RENDERTARGET) != 0)
  137. {
  138. LOGERR("Writing to render texture from CPU not supported.");
  139. return;
  140. }
  141. if ((mUsage & TU_DEPTHSTENCIL) != 0)
  142. {
  143. LOGERR("Writing to depth stencil texture from CPU not supported.");
  144. return;
  145. }
  146. glBindTexture(mTarget, mTextureID);
  147. BS_CHECK_GL_ERROR();
  148. if(PixelUtil::isCompressed(data.getFormat()))
  149. {
  150. if (data.getFormat() != mFormat || !data.isConsecutive())
  151. {
  152. LOGERR("Compressed images must be consecutive, in the source format");
  153. return;
  154. }
  155. GLenum format = GLPixelUtil::getGLInternalFormat(mFormat);
  156. switch(mTarget) {
  157. case GL_TEXTURE_1D:
  158. if (dest.left == 0)
  159. {
  160. glCompressedTexImage1D(GL_TEXTURE_1D, mLevel,
  161. format,
  162. dest.getWidth(),
  163. 0,
  164. data.getConsecutiveSize(),
  165. data.getData());
  166. BS_CHECK_GL_ERROR();
  167. }
  168. else
  169. {
  170. glCompressedTexSubImage1D(GL_TEXTURE_1D, mLevel,
  171. dest.left,
  172. dest.getWidth(),
  173. format, data.getConsecutiveSize(),
  174. data.getData());
  175. BS_CHECK_GL_ERROR();
  176. }
  177. break;
  178. case GL_TEXTURE_2D:
  179. case GL_TEXTURE_CUBE_MAP:
  180. if (dest.left == 0 && dest.top == 0)
  181. {
  182. glCompressedTexImage2D(mFaceTarget, mLevel,
  183. format,
  184. dest.getWidth(),
  185. dest.getHeight(),
  186. 0,
  187. data.getConsecutiveSize(),
  188. data.getData());
  189. BS_CHECK_GL_ERROR();
  190. }
  191. else
  192. {
  193. glCompressedTexSubImage2D(mFaceTarget, mLevel,
  194. dest.left, dest.top,
  195. dest.getWidth(), dest.getHeight(),
  196. format, data.getConsecutiveSize(),
  197. data.getData());
  198. BS_CHECK_GL_ERROR();
  199. }
  200. break;
  201. case GL_TEXTURE_3D:
  202. if (dest.left == 0 && dest.top == 0 && dest.front == 0)
  203. {
  204. glCompressedTexImage3D(GL_TEXTURE_3D, mLevel,
  205. format,
  206. dest.getWidth(),
  207. dest.getHeight(),
  208. dest.getDepth(),
  209. 0,
  210. data.getConsecutiveSize(),
  211. data.getData());
  212. BS_CHECK_GL_ERROR();
  213. }
  214. else
  215. {
  216. glCompressedTexSubImage3D(GL_TEXTURE_3D, mLevel,
  217. dest.left, dest.top, dest.front,
  218. dest.getWidth(), dest.getHeight(), dest.getDepth(),
  219. format, data.getConsecutiveSize(),
  220. data.getData());
  221. BS_CHECK_GL_ERROR();
  222. }
  223. break;
  224. }
  225. }
  226. else
  227. {
  228. if (data.getWidth() != data.getRowPitch())
  229. {
  230. glPixelStorei(GL_UNPACK_ROW_LENGTH, data.getRowPitch());
  231. BS_CHECK_GL_ERROR();
  232. }
  233. if (data.getHeight()*data.getWidth() != data.getSlicePitch())
  234. {
  235. glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, (data.getSlicePitch() / data.getWidth()));
  236. BS_CHECK_GL_ERROR();
  237. }
  238. if (data.getLeft() > 0 || data.getTop() > 0 || data.getFront() > 0)
  239. {
  240. glPixelStorei(
  241. GL_UNPACK_SKIP_PIXELS,
  242. data.getLeft() + data.getRowPitch() * data.getTop() + data.getSlicePitch() * data.getFront());
  243. BS_CHECK_GL_ERROR();
  244. }
  245. if ((data.getWidth()*PixelUtil::getNumElemBytes(data.getFormat())) & 3)
  246. {
  247. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  248. BS_CHECK_GL_ERROR();
  249. }
  250. switch(mTarget) {
  251. case GL_TEXTURE_1D:
  252. glTexSubImage1D(GL_TEXTURE_1D, mLevel,
  253. dest.left,
  254. dest.getWidth(),
  255. GLPixelUtil::getGLOriginFormat(data.getFormat()), GLPixelUtil::getGLOriginDataType(data.getFormat()),
  256. data.getData());
  257. BS_CHECK_GL_ERROR();
  258. break;
  259. case GL_TEXTURE_2D:
  260. case GL_TEXTURE_CUBE_MAP:
  261. glTexSubImage2D(mFaceTarget, mLevel,
  262. dest.left, dest.top,
  263. dest.getWidth(), dest.getHeight(),
  264. GLPixelUtil::getGLOriginFormat(data.getFormat()), GLPixelUtil::getGLOriginDataType(data.getFormat()),
  265. data.getData());
  266. BS_CHECK_GL_ERROR();
  267. break;
  268. case GL_TEXTURE_3D:
  269. glTexSubImage3D(
  270. GL_TEXTURE_3D, mLevel,
  271. dest.left, dest.top, dest.front,
  272. dest.getWidth(), dest.getHeight(), dest.getDepth(),
  273. GLPixelUtil::getGLOriginFormat(data.getFormat()), GLPixelUtil::getGLOriginDataType(data.getFormat()),
  274. data.getData());
  275. BS_CHECK_GL_ERROR();
  276. break;
  277. }
  278. }
  279. // Restore defaults
  280. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  281. BS_CHECK_GL_ERROR();
  282. glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
  283. BS_CHECK_GL_ERROR();
  284. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  285. BS_CHECK_GL_ERROR();
  286. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  287. BS_CHECK_GL_ERROR();
  288. BS_INC_RENDER_STAT_CAT(ResWrite, RenderStatObject_Texture);
  289. }
  290. void GLTextureBuffer::download(const PixelData &data)
  291. {
  292. if (data.getWidth() != getWidth() || data.getHeight() != getHeight() || data.getDepth() != getDepth())
  293. {
  294. LOGERR("Only download of entire buffer is supported by OpenGL.");
  295. return;
  296. }
  297. glBindTexture(mTarget, mTextureID);
  298. BS_CHECK_GL_ERROR();
  299. if(PixelUtil::isCompressed(data.getFormat()))
  300. {
  301. if (data.getFormat() != mFormat || !data.isConsecutive())
  302. {
  303. LOGERR("Compressed images must be consecutive, in the source format");
  304. return;
  305. }
  306. // Data must be consecutive and at beginning of buffer as PixelStorei not allowed
  307. // for compressed formate
  308. glGetCompressedTexImage(mFaceTarget, mLevel, data.getData());
  309. BS_CHECK_GL_ERROR();
  310. }
  311. else
  312. {
  313. if (data.getWidth() != data.getRowPitch())
  314. {
  315. glPixelStorei(GL_PACK_ROW_LENGTH, data.getRowPitch());
  316. BS_CHECK_GL_ERROR();
  317. }
  318. if (data.getHeight()*data.getWidth() != data.getSlicePitch())
  319. {
  320. glPixelStorei(GL_PACK_IMAGE_HEIGHT, (data.getSlicePitch() / data.getWidth()));
  321. BS_CHECK_GL_ERROR();
  322. }
  323. if (data.getLeft() > 0 || data.getTop() > 0 || data.getFront() > 0)
  324. {
  325. glPixelStorei(
  326. GL_PACK_SKIP_PIXELS,
  327. data.getLeft() + data.getRowPitch() * data.getTop() + data.getSlicePitch() * data.getFront());
  328. BS_CHECK_GL_ERROR();
  329. }
  330. if ((data.getWidth()*PixelUtil::getNumElemBytes(data.getFormat())) & 3)
  331. {
  332. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  333. BS_CHECK_GL_ERROR();
  334. }
  335. // We can only get the entire texture
  336. glGetTexImage(mFaceTarget, mLevel, GLPixelUtil::getGLOriginFormat(data.getFormat()),
  337. GLPixelUtil::getGLOriginDataType(data.getFormat()), data.getData());
  338. BS_CHECK_GL_ERROR();
  339. // Restore defaults
  340. glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  341. BS_CHECK_GL_ERROR();
  342. glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
  343. BS_CHECK_GL_ERROR();
  344. glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
  345. BS_CHECK_GL_ERROR();
  346. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  347. BS_CHECK_GL_ERROR();
  348. }
  349. BS_INC_RENDER_STAT_CAT(ResRead, RenderStatObject_Texture);
  350. }
  351. void GLTextureBuffer::bindToFramebuffer(GLenum attachment, UINT32 zoffset, bool allLayers)
  352. {
  353. if(allLayers)
  354. {
  355. switch (mTarget)
  356. {
  357. case GL_TEXTURE_1D:
  358. glFramebufferTexture1D(GL_FRAMEBUFFER, attachment, mFaceTarget, mTextureID, mLevel);
  359. BS_CHECK_GL_ERROR();
  360. break;
  361. case GL_TEXTURE_2D:
  362. glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, mFaceTarget, mTextureID, mLevel);
  363. BS_CHECK_GL_ERROR();
  364. break;
  365. case GL_TEXTURE_2D_MULTISAMPLE:
  366. glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, mFaceTarget, mTextureID, 0);
  367. BS_CHECK_GL_ERROR();
  368. break;
  369. case GL_TEXTURE_CUBE_MAP:
  370. case GL_TEXTURE_3D:
  371. default: // Texture arrays (binding all layers)
  372. glFramebufferTexture(GL_FRAMEBUFFER, attachment, mTextureID, mLevel);
  373. BS_CHECK_GL_ERROR();
  374. break;
  375. }
  376. }
  377. else
  378. {
  379. switch (mTarget)
  380. {
  381. case GL_TEXTURE_3D:
  382. glFramebufferTexture3D(GL_FRAMEBUFFER, attachment, mFaceTarget, mTextureID, mLevel, zoffset);
  383. BS_CHECK_GL_ERROR();
  384. break;
  385. default: // Texture arrays and cube maps
  386. glFramebufferTextureLayer(GL_FRAMEBUFFER, attachment, mTextureID, mLevel, mFace);
  387. BS_CHECK_GL_ERROR();
  388. break;
  389. }
  390. }
  391. }
  392. void GLTextureBuffer::copyFromFramebuffer(UINT32 zoffset)
  393. {
  394. glBindTexture(mTarget, mTextureID);
  395. BS_CHECK_GL_ERROR();
  396. switch(mTarget)
  397. {
  398. case GL_TEXTURE_1D:
  399. glCopyTexSubImage1D(mFaceTarget, mLevel, 0, 0, 0, mWidth);
  400. BS_CHECK_GL_ERROR();
  401. break;
  402. case GL_TEXTURE_2D:
  403. case GL_TEXTURE_CUBE_MAP:
  404. glCopyTexSubImage2D(mFaceTarget, mLevel, 0, 0, 0, 0, mWidth, mHeight);
  405. BS_CHECK_GL_ERROR();
  406. break;
  407. case GL_TEXTURE_3D:
  408. glCopyTexSubImage3D(mFaceTarget, mLevel, 0, 0, zoffset, 0, 0, mWidth, mHeight);
  409. BS_CHECK_GL_ERROR();
  410. break;
  411. }
  412. }
  413. void GLTextureBuffer::blitFromTexture(GLTextureBuffer* src, const PixelVolume& srcBox, const PixelVolume& dstBox)
  414. {
  415. if (src->mMultisampleCount > 1 && mMultisampleCount <= 1) // Resolving MS texture
  416. {
  417. if (mTarget != GL_TEXTURE_2D || mTarget != GL_TEXTURE_2D_MULTISAMPLE)
  418. BS_EXCEPT(InvalidParametersException, "Non-2D multisampled texture not supported.");
  419. GLint currentFBO = 0;
  420. glGetIntegerv(GL_FRAMEBUFFER_BINDING, &currentFBO);
  421. BS_CHECK_GL_ERROR();
  422. GLuint readFBO = GLRTTManager::instance().getBlitReadFBO();
  423. GLuint drawFBO = GLRTTManager::instance().getBlitDrawFBO();
  424. // Attach source texture
  425. glBindFramebuffer(GL_FRAMEBUFFER, readFBO);
  426. BS_CHECK_GL_ERROR();
  427. src->bindToFramebuffer(0, 0, true);
  428. // Attach destination texture
  429. glBindFramebuffer(GL_FRAMEBUFFER, drawFBO);
  430. BS_CHECK_GL_ERROR();
  431. bindToFramebuffer(0, 0, true);
  432. // Perform blit
  433. glBindFramebuffer(GL_READ_FRAMEBUFFER, readFBO);
  434. BS_CHECK_GL_ERROR();
  435. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, drawFBO);
  436. BS_CHECK_GL_ERROR();
  437. glReadBuffer(GL_COLOR_ATTACHMENT0);
  438. BS_CHECK_GL_ERROR();
  439. glDrawBuffer(GL_COLOR_ATTACHMENT0);
  440. BS_CHECK_GL_ERROR();
  441. glBlitFramebuffer(srcBox.left, srcBox.top, srcBox.right, srcBox.bottom,
  442. dstBox.left, dstBox.top, dstBox.right, dstBox.bottom, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  443. BS_CHECK_GL_ERROR();
  444. // Restore the previously bound FBO
  445. glBindFramebuffer(GL_FRAMEBUFFER, currentFBO);
  446. BS_CHECK_GL_ERROR();
  447. }
  448. else // Just plain copy
  449. {
  450. if (mMultisampleCount != src->mMultisampleCount)
  451. BS_EXCEPT(InvalidParametersException, "When copying textures their multisample counts must match.");
  452. if (mTarget == GL_TEXTURE_3D) // 3D textures can't have arrays so their Z coordinate is handled differently
  453. {
  454. glCopyImageSubData(src->mTextureID, src->mTarget, src->mLevel, srcBox.left, srcBox.top, srcBox.front,
  455. mTextureID, mTarget, mLevel, dstBox.left, dstBox.top, dstBox.front, srcBox.getWidth(), srcBox.getHeight(), srcBox.getDepth());
  456. BS_CHECK_GL_ERROR();
  457. }
  458. else
  459. {
  460. glCopyImageSubData(src->mTextureID, src->mTarget, src->mLevel, srcBox.left, srcBox.top, src->mFace,
  461. mTextureID, mTarget, mLevel, dstBox.left, dstBox.top, mFace, srcBox.getWidth(), srcBox.getHeight(), 1);
  462. BS_CHECK_GL_ERROR();
  463. }
  464. }
  465. }
  466. }}