2
0

CmGLHardwarePixelBuffer.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmGLHardwarePixelBuffer.h"
  25. #include "CmGLTexture.h"
  26. #include "CmGLSupport.h"
  27. #include "CmGLPixelFormat.h"
  28. #include "CmException.h"
  29. #include "CmBitwise.h"
  30. #include "CmGLRenderTexture.h"
  31. namespace CamelotEngine {
  32. //-----------------------------------------------------------------------------
  33. GLHardwarePixelBuffer::GLHardwarePixelBuffer(UINT32 inWidth, UINT32 inHeight, UINT32 inDepth,
  34. PixelFormat inFormat,
  35. HardwareBuffer::Usage usage):
  36. HardwarePixelBuffer(inWidth, inHeight, inDepth, inFormat, usage, false),
  37. mBuffer(inWidth, inHeight, inDepth, inFormat),
  38. mGLInternalFormat(GL_NONE)
  39. {
  40. mCurrentLockOptions = (LockOptions)0;
  41. }
  42. //-----------------------------------------------------------------------------
  43. GLHardwarePixelBuffer::~GLHardwarePixelBuffer()
  44. {
  45. // Force free buffer
  46. delete [] (UINT8*)mBuffer.data;
  47. }
  48. //-----------------------------------------------------------------------------
  49. void GLHardwarePixelBuffer::allocateBuffer()
  50. {
  51. if(mBuffer.data)
  52. // Already allocated
  53. return;
  54. mBuffer.data = new UINT8[mSizeInBytes];
  55. // TODO: use PBO if we're HBU_DYNAMIC
  56. }
  57. //-----------------------------------------------------------------------------
  58. void GLHardwarePixelBuffer::freeBuffer()
  59. {
  60. // Free buffer if we're STATIC to save memory
  61. if(mUsage & HBU_STATIC)
  62. {
  63. delete [] (UINT8*)mBuffer.data;
  64. mBuffer.data = 0;
  65. }
  66. }
  67. //-----------------------------------------------------------------------------
  68. PixelData GLHardwarePixelBuffer::lockImpl(const Box lockBox, LockOptions options)
  69. {
  70. allocateBuffer();
  71. if(options != HBL_WRITE_ONLY_DISCARD)
  72. {
  73. // Download the old contents of the texture
  74. download(mBuffer);
  75. }
  76. mCurrentLockOptions = options;
  77. mLockedBox = lockBox;
  78. return mBuffer.getSubVolume(lockBox);
  79. }
  80. //-----------------------------------------------------------------------------
  81. void GLHardwarePixelBuffer::unlockImpl(void)
  82. {
  83. if (mCurrentLockOptions != HBL_READ_ONLY)
  84. {
  85. // From buffer to card, only upload if was locked for writing
  86. upload(mCurrentLock, mLockedBox);
  87. }
  88. freeBuffer();
  89. }
  90. //-----------------------------------------------------------------------------
  91. void GLHardwarePixelBuffer::blitFromMemory(const PixelData &src, const Box &dstBox)
  92. {
  93. if(!mBuffer.contains(dstBox))
  94. CM_EXCEPT(InvalidParametersException, "destination box out of range");
  95. PixelData scaled;
  96. if(src.getWidth() != dstBox.getWidth() ||
  97. src.getHeight() != dstBox.getHeight() ||
  98. src.getDepth() != dstBox.getDepth())
  99. {
  100. // Scale to destination size. Use DevIL and not iluScale because ILU screws up for
  101. // floating point textures and cannot cope with 3D images.
  102. // This also does pixel format conversion if needed
  103. allocateBuffer();
  104. scaled = mBuffer.getSubVolume(dstBox);
  105. PixelUtil::scale(src, scaled, PixelUtil::FILTER_BILINEAR);
  106. }
  107. else if(GLPixelUtil::getGLOriginFormat(src.format) == 0)
  108. {
  109. // Extents match, but format is not accepted as valid source format for GL
  110. // do conversion in temporary buffer
  111. allocateBuffer();
  112. scaled = mBuffer.getSubVolume(dstBox);
  113. PixelUtil::bulkPixelConversion(src, scaled);
  114. }
  115. else
  116. {
  117. allocateBuffer();
  118. // No scaling or conversion needed
  119. scaled = src;
  120. }
  121. upload(scaled, dstBox);
  122. freeBuffer();
  123. }
  124. //-----------------------------------------------------------------------------
  125. void GLHardwarePixelBuffer::blitToMemory(const Box &srcBox, const PixelData &dst)
  126. {
  127. if(!mBuffer.contains(srcBox))
  128. CM_EXCEPT(InvalidParametersException, "source box out of range");
  129. if(srcBox.left == 0 && srcBox.right == getWidth() &&
  130. srcBox.top == 0 && srcBox.bottom == getHeight() &&
  131. srcBox.front == 0 && srcBox.back == getDepth() &&
  132. dst.getWidth() == getWidth() &&
  133. dst.getHeight() == getHeight() &&
  134. dst.getDepth() == getDepth() &&
  135. GLPixelUtil::getGLOriginFormat(dst.format) != 0)
  136. {
  137. // The direct case: the user wants the entire texture in a format supported by GL
  138. // so we don't need an intermediate buffer
  139. download(dst);
  140. }
  141. else
  142. {
  143. // Use buffer for intermediate copy
  144. allocateBuffer();
  145. // Download entire buffer
  146. download(mBuffer);
  147. if(srcBox.getWidth() != dst.getWidth() ||
  148. srcBox.getHeight() != dst.getHeight() ||
  149. srcBox.getDepth() != dst.getDepth())
  150. {
  151. // We need scaling
  152. PixelUtil::scale(mBuffer.getSubVolume(srcBox), dst, PixelUtil::FILTER_BILINEAR);
  153. }
  154. else
  155. {
  156. // Just copy the bit that we need
  157. PixelUtil::bulkPixelConversion(mBuffer.getSubVolume(srcBox), dst);
  158. }
  159. freeBuffer();
  160. }
  161. }
  162. //-----------------------------------------------------------------------------
  163. void GLHardwarePixelBuffer::upload(const PixelData &data, const Box &dest)
  164. {
  165. CM_EXCEPT(RenderingAPIException,
  166. "Upload not possible for this pixelbuffer type");
  167. }
  168. //-----------------------------------------------------------------------------
  169. void GLHardwarePixelBuffer::download(const PixelData &data)
  170. {
  171. CM_EXCEPT(RenderingAPIException, "Download not possible for this pixelbuffer type");
  172. }
  173. //-----------------------------------------------------------------------------
  174. void GLHardwarePixelBuffer::bindToFramebuffer(GLenum attachment, UINT32 zoffset)
  175. {
  176. CM_EXCEPT(RenderingAPIException, "Framebuffer bind not possible for this pixelbuffer type");
  177. }
  178. //********* GLTextureBuffer
  179. GLTextureBuffer::GLTextureBuffer(const String &baseName, GLenum target, GLuint id,
  180. GLint face, GLint level, Usage usage, bool crappyCard,
  181. bool writeGamma, UINT32 fsaa):
  182. GLHardwarePixelBuffer(0, 0, 0, PF_UNKNOWN, usage),
  183. mTarget(target), mFaceTarget(0), mTextureID(id), mFace(face), mLevel(level),
  184. mSoftwareMipmap(crappyCard)
  185. {
  186. // devise mWidth, mHeight and mDepth and mFormat
  187. GLint value = 0;
  188. glBindTexture( mTarget, mTextureID );
  189. // Get face identifier
  190. mFaceTarget = mTarget;
  191. if(mTarget == GL_TEXTURE_CUBE_MAP)
  192. mFaceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + face;
  193. // Get width
  194. glGetTexLevelParameteriv(mFaceTarget, level, GL_TEXTURE_WIDTH, &value);
  195. mWidth = value;
  196. // Get height
  197. if(target == GL_TEXTURE_1D)
  198. value = 1; // Height always 1 for 1D textures
  199. else
  200. glGetTexLevelParameteriv(mFaceTarget, level, GL_TEXTURE_HEIGHT, &value);
  201. mHeight = value;
  202. // Get depth
  203. if(target != GL_TEXTURE_3D)
  204. value = 1; // Depth always 1 for non-3D textures
  205. else
  206. glGetTexLevelParameteriv(mFaceTarget, level, GL_TEXTURE_DEPTH, &value);
  207. mDepth = value;
  208. // Get format
  209. glGetTexLevelParameteriv(mFaceTarget, level, GL_TEXTURE_INTERNAL_FORMAT, &value);
  210. mGLInternalFormat = value;
  211. mFormat = GLPixelUtil::getClosestOGREFormat(value);
  212. // Default
  213. mRowPitch = mWidth;
  214. mSlicePitch = mHeight*mWidth;
  215. mSizeInBytes = PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  216. // Set up pixel box
  217. mBuffer = PixelData(mWidth, mHeight, mDepth, mFormat);
  218. if(mWidth==0 || mHeight==0 || mDepth==0)
  219. /// We are invalid, do not allocate a buffer
  220. return;
  221. }
  222. GLTextureBuffer::~GLTextureBuffer()
  223. {
  224. }
  225. //-----------------------------------------------------------------------------
  226. void GLTextureBuffer::upload(const PixelData &data, const Box &dest)
  227. {
  228. glBindTexture( mTarget, mTextureID );
  229. if(PixelUtil::isCompressed(data.format))
  230. {
  231. if(data.format != mFormat || !data.isConsecutive())
  232. CM_EXCEPT(InvalidParametersException,
  233. "Compressed images must be consecutive, in the source format");
  234. GLenum format = GLPixelUtil::getClosestGLInternalFormat(mFormat);
  235. // Data must be consecutive and at beginning of buffer as PixelStorei not allowed
  236. // for compressed formats
  237. switch(mTarget) {
  238. case GL_TEXTURE_1D:
  239. // some systems (e.g. old Apple) don't like compressed subimage calls
  240. // so prefer non-sub versions
  241. if (dest.left == 0)
  242. {
  243. glCompressedTexImage1DARB(GL_TEXTURE_1D, mLevel,
  244. format,
  245. dest.getWidth(),
  246. 0,
  247. data.getConsecutiveSize(),
  248. data.data);
  249. }
  250. else
  251. {
  252. glCompressedTexSubImage1DARB(GL_TEXTURE_1D, mLevel,
  253. dest.left,
  254. dest.getWidth(),
  255. format, data.getConsecutiveSize(),
  256. data.data);
  257. }
  258. break;
  259. case GL_TEXTURE_2D:
  260. case GL_TEXTURE_CUBE_MAP:
  261. // some systems (e.g. old Apple) don't like compressed subimage calls
  262. // so prefer non-sub versions
  263. if (dest.left == 0 && dest.top == 0)
  264. {
  265. glCompressedTexImage2DARB(mFaceTarget, mLevel,
  266. format,
  267. dest.getWidth(),
  268. dest.getHeight(),
  269. 0,
  270. data.getConsecutiveSize(),
  271. data.data);
  272. }
  273. else
  274. {
  275. glCompressedTexSubImage2DARB(mFaceTarget, mLevel,
  276. dest.left, dest.top,
  277. dest.getWidth(), dest.getHeight(),
  278. format, data.getConsecutiveSize(),
  279. data.data);
  280. }
  281. break;
  282. case GL_TEXTURE_3D:
  283. // some systems (e.g. old Apple) don't like compressed subimage calls
  284. // so prefer non-sub versions
  285. if (dest.left == 0 && dest.top == 0 && dest.front == 0)
  286. {
  287. glCompressedTexImage3DARB(GL_TEXTURE_3D, mLevel,
  288. format,
  289. dest.getWidth(),
  290. dest.getHeight(),
  291. dest.getDepth(),
  292. 0,
  293. data.getConsecutiveSize(),
  294. data.data);
  295. }
  296. else
  297. {
  298. glCompressedTexSubImage3DARB(GL_TEXTURE_3D, mLevel,
  299. dest.left, dest.top, dest.front,
  300. dest.getWidth(), dest.getHeight(), dest.getDepth(),
  301. format, data.getConsecutiveSize(),
  302. data.data);
  303. }
  304. break;
  305. }
  306. }
  307. else if(mSoftwareMipmap)
  308. {
  309. GLint components = PixelUtil::getComponentCount(mFormat);
  310. if(data.getWidth() != data.rowPitch)
  311. glPixelStorei(GL_UNPACK_ROW_LENGTH, data.rowPitch);
  312. if(data.getHeight()*data.getWidth() != data.slicePitch)
  313. glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, (data.slicePitch/data.getWidth()));
  314. if(data.left > 0 || data.top > 0 || data.front > 0)
  315. glPixelStorei(GL_UNPACK_SKIP_PIXELS, data.left + data.rowPitch * data.top + data.slicePitch * data.front);
  316. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  317. switch(mTarget)
  318. {
  319. case GL_TEXTURE_1D:
  320. gluBuild1DMipmaps(
  321. GL_TEXTURE_1D, components,
  322. dest.getWidth(),
  323. GLPixelUtil::getGLOriginFormat(data.format), GLPixelUtil::getGLOriginDataType(data.format),
  324. data.data);
  325. break;
  326. case GL_TEXTURE_2D:
  327. case GL_TEXTURE_CUBE_MAP:
  328. gluBuild2DMipmaps(
  329. mFaceTarget,
  330. components, dest.getWidth(), dest.getHeight(),
  331. GLPixelUtil::getGLOriginFormat(data.format), GLPixelUtil::getGLOriginDataType(data.format),
  332. data.data);
  333. break;
  334. case GL_TEXTURE_3D:
  335. /* Requires GLU 1.3 which is harder to come by than cards doing hardware mipmapping
  336. Most 3D textures don't need mipmaps?
  337. gluBuild3DMipmaps(
  338. GL_TEXTURE_3D, internalFormat,
  339. data.getWidth(), data.getHeight(), data.getDepth(),
  340. GLPixelUtil::getGLOriginFormat(data.format), GLPixelUtil::getGLOriginDataType(data.format),
  341. data.data);
  342. */
  343. glTexImage3D(
  344. GL_TEXTURE_3D, 0, components,
  345. dest.getWidth(), dest.getHeight(), dest.getDepth(), 0,
  346. GLPixelUtil::getGLOriginFormat(data.format), GLPixelUtil::getGLOriginDataType(data.format),
  347. data.data );
  348. break;
  349. }
  350. }
  351. else
  352. {
  353. if(data.getWidth() != data.rowPitch)
  354. glPixelStorei(GL_UNPACK_ROW_LENGTH, data.rowPitch);
  355. if(data.getHeight()*data.getWidth() != data.slicePitch)
  356. glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, (data.slicePitch/data.getWidth()));
  357. if(data.left > 0 || data.top > 0 || data.front > 0)
  358. glPixelStorei(GL_UNPACK_SKIP_PIXELS, data.left + data.rowPitch * data.top + data.slicePitch * data.front);
  359. if((data.getWidth()*PixelUtil::getNumElemBytes(data.format)) & 3) {
  360. // Standard alignment of 4 is not right
  361. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  362. }
  363. switch(mTarget) {
  364. case GL_TEXTURE_1D:
  365. glTexSubImage1D(GL_TEXTURE_1D, mLevel,
  366. dest.left,
  367. dest.getWidth(),
  368. GLPixelUtil::getGLOriginFormat(data.format), GLPixelUtil::getGLOriginDataType(data.format),
  369. data.data);
  370. break;
  371. case GL_TEXTURE_2D:
  372. case GL_TEXTURE_CUBE_MAP:
  373. glTexSubImage2D(mFaceTarget, mLevel,
  374. dest.left, dest.top,
  375. dest.getWidth(), dest.getHeight(),
  376. GLPixelUtil::getGLOriginFormat(data.format), GLPixelUtil::getGLOriginDataType(data.format),
  377. data.data);
  378. break;
  379. case GL_TEXTURE_3D:
  380. glTexSubImage3D(
  381. GL_TEXTURE_3D, mLevel,
  382. dest.left, dest.top, dest.front,
  383. dest.getWidth(), dest.getHeight(), dest.getDepth(),
  384. GLPixelUtil::getGLOriginFormat(data.format), GLPixelUtil::getGLOriginDataType(data.format),
  385. data.data);
  386. break;
  387. }
  388. }
  389. // Restore defaults
  390. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  391. if (GLEW_VERSION_1_2)
  392. glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
  393. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  394. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  395. }
  396. //-----------------------------------------------------------------------------
  397. void GLTextureBuffer::download(const PixelData &data)
  398. {
  399. if(data.getWidth() != getWidth() ||
  400. data.getHeight() != getHeight() ||
  401. data.getDepth() != getDepth())
  402. CM_EXCEPT(InvalidParametersException, "only download of entire buffer is supported by GL");
  403. glBindTexture( mTarget, mTextureID );
  404. if(PixelUtil::isCompressed(data.format))
  405. {
  406. if(data.format != mFormat || !data.isConsecutive())
  407. CM_EXCEPT(InvalidParametersException,
  408. "Compressed images must be consecutive, in the source format");
  409. // Data must be consecutive and at beginning of buffer as PixelStorei not allowed
  410. // for compressed formate
  411. glGetCompressedTexImageARB(mFaceTarget, mLevel, data.data);
  412. }
  413. else
  414. {
  415. if(data.getWidth() != data.rowPitch)
  416. glPixelStorei(GL_PACK_ROW_LENGTH, data.rowPitch);
  417. if(data.getHeight()*data.getWidth() != data.slicePitch)
  418. glPixelStorei(GL_PACK_IMAGE_HEIGHT, (data.slicePitch/data.getWidth()));
  419. if(data.left > 0 || data.top > 0 || data.front > 0)
  420. glPixelStorei(GL_PACK_SKIP_PIXELS, data.left + data.rowPitch * data.top + data.slicePitch * data.front);
  421. if((data.getWidth()*PixelUtil::getNumElemBytes(data.format)) & 3) {
  422. // Standard alignment of 4 is not right
  423. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  424. }
  425. // We can only get the entire texture
  426. glGetTexImage(mFaceTarget, mLevel,
  427. GLPixelUtil::getGLOriginFormat(data.format), GLPixelUtil::getGLOriginDataType(data.format),
  428. data.data);
  429. // Restore defaults
  430. glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  431. glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
  432. glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
  433. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  434. }
  435. }
  436. //-----------------------------------------------------------------------------
  437. void GLTextureBuffer::bindToFramebuffer(GLenum attachment, UINT32 zoffset)
  438. {
  439. assert(zoffset < mDepth);
  440. switch(mTarget)
  441. {
  442. case GL_TEXTURE_1D:
  443. glFramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT, attachment,
  444. mFaceTarget, mTextureID, mLevel);
  445. break;
  446. case GL_TEXTURE_2D:
  447. case GL_TEXTURE_CUBE_MAP:
  448. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attachment,
  449. mFaceTarget, mTextureID, mLevel);
  450. break;
  451. case GL_TEXTURE_3D:
  452. glFramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT, attachment,
  453. mFaceTarget, mTextureID, mLevel, zoffset);
  454. break;
  455. }
  456. }
  457. //-----------------------------------------------------------------------------
  458. void GLTextureBuffer::copyFromFramebuffer(UINT32 zoffset)
  459. {
  460. glBindTexture(mTarget, mTextureID);
  461. switch(mTarget)
  462. {
  463. case GL_TEXTURE_1D:
  464. glCopyTexSubImage1D(mFaceTarget, mLevel, 0, 0, 0, mWidth);
  465. break;
  466. case GL_TEXTURE_2D:
  467. case GL_TEXTURE_CUBE_MAP:
  468. glCopyTexSubImage2D(mFaceTarget, mLevel, 0, 0, 0, 0, mWidth, mHeight);
  469. break;
  470. case GL_TEXTURE_3D:
  471. glCopyTexSubImage3D(mFaceTarget, mLevel, 0, 0, zoffset, 0, 0, mWidth, mHeight);
  472. break;
  473. }
  474. }
  475. //-----------------------------------------------------------------------------
  476. void GLTextureBuffer::blit(const HardwarePixelBufferPtr &src, const Box &srcBox, const Box &dstBox)
  477. {
  478. GLTextureBuffer *srct = static_cast<GLTextureBuffer *>(src.get());
  479. /// Check for FBO support first
  480. /// Destination texture must be 1D, 2D, 3D, or Cube
  481. /// Source texture must be 1D, 2D or 3D
  482. if(GLEW_EXT_framebuffer_object && (srct->mTarget==GL_TEXTURE_1D||srct->mTarget==GL_TEXTURE_2D||srct->mTarget==GL_TEXTURE_3D))
  483. {
  484. blitFromTexture(srct, srcBox, dstBox);
  485. }
  486. else
  487. {
  488. GLHardwarePixelBuffer::blit(src, srcBox, dstBox);
  489. }
  490. }
  491. //-----------------------------------------------------------------------------
  492. /// Very fast texture-to-texture blitter and hardware bi/trilinear scaling implementation using FBO
  493. /// Destination texture must be 1D, 2D, 3D, or Cube
  494. /// Source texture must be 1D, 2D or 3D
  495. /// Supports compressed formats as both source and destination format, it will use the hardware DXT compressor
  496. /// if available.
  497. /// @author W.J. van der Laan
  498. void GLTextureBuffer::blitFromTexture(GLTextureBuffer *src, const Box &srcBox, const Box &dstBox)
  499. {
  500. //std::cerr << "GLTextureBuffer::blitFromTexture " <<
  501. //src->mTextureID << ":" << srcBox.left << "," << srcBox.top << "," << srcBox.right << "," << srcBox.bottom << " " <<
  502. //mTextureID << ":" << dstBox.left << "," << dstBox.top << "," << dstBox.right << "," << dstBox.bottom << std::endl;
  503. /// Store reference to FBO manager
  504. GLRTTManager *fboMan = static_cast<GLRTTManager *>(GLRTTManager::instancePtr());
  505. /// Save and clear GL state for rendering
  506. glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT |
  507. GL_FOG_BIT | GL_LIGHTING_BIT | GL_POLYGON_BIT | GL_SCISSOR_BIT | GL_STENCIL_BUFFER_BIT |
  508. GL_TEXTURE_BIT | GL_VIEWPORT_BIT);
  509. // Important to disable all other texture units
  510. RenderSystem* rsys = CamelotEngine::RenderSystem::instancePtr();
  511. rsys->disableTextureUnitsFrom(0);
  512. if (GLEW_VERSION_1_2)
  513. {
  514. glActiveTextureARB(GL_TEXTURE0);
  515. }
  516. /// Disable alpha, depth and scissor testing, disable blending,
  517. /// disable culling, disble lighting, disable fog and reset foreground
  518. /// colour.
  519. glDisable(GL_ALPHA_TEST);
  520. glDisable(GL_DEPTH_TEST);
  521. glDisable(GL_SCISSOR_TEST);
  522. glDisable(GL_BLEND);
  523. glDisable(GL_CULL_FACE);
  524. glDisable(GL_LIGHTING);
  525. glDisable(GL_FOG);
  526. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  527. /// Save and reset matrices
  528. glMatrixMode(GL_MODELVIEW);
  529. glPushMatrix();
  530. glLoadIdentity();
  531. glMatrixMode(GL_PROJECTION);
  532. glPushMatrix();
  533. glLoadIdentity();
  534. glMatrixMode(GL_TEXTURE);
  535. glPushMatrix();
  536. glLoadIdentity();
  537. /// Set up source texture
  538. glBindTexture(src->mTarget, src->mTextureID);
  539. /// Set filtering modes depending on the dimensions and source
  540. if(srcBox.getWidth()==dstBox.getWidth() &&
  541. srcBox.getHeight()==dstBox.getHeight() &&
  542. srcBox.getDepth()==dstBox.getDepth())
  543. {
  544. /// Dimensions match -- use nearest filtering (fastest and pixel correct)
  545. glTexParameteri(src->mTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  546. glTexParameteri(src->mTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  547. }
  548. else
  549. {
  550. /// Manual mipmaps, stay safe with bilinear filtering so that no
  551. /// intermipmap leakage occurs.
  552. glTexParameteri(src->mTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  553. glTexParameteri(src->mTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  554. }
  555. /// Clamp to edge (fastest)
  556. glTexParameteri(src->mTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  557. glTexParameteri(src->mTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  558. glTexParameteri(src->mTarget, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  559. /// Set origin base level mipmap to make sure we source from the right mip
  560. /// level.
  561. glTexParameteri(src->mTarget, GL_TEXTURE_BASE_LEVEL, src->mLevel);
  562. /// Store old binding so it can be restored later
  563. GLint oldfb;
  564. glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &oldfb);
  565. /// Set up temporary FBO
  566. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboMan->getTemporaryFBO());
  567. GLuint tempTex = 0;
  568. if(!fboMan->checkFormat(mFormat))
  569. {
  570. /// If target format not directly supported, create intermediate texture
  571. GLenum tempFormat = GLPixelUtil::getClosestGLInternalFormat(fboMan->getSupportedAlternative(mFormat));
  572. glGenTextures(1, &tempTex);
  573. glBindTexture(GL_TEXTURE_2D, tempTex);
  574. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
  575. /// Allocate temporary texture of the size of the destination area
  576. glTexImage2D(GL_TEXTURE_2D, 0, tempFormat,
  577. GLPixelUtil::optionalPO2(dstBox.getWidth()), GLPixelUtil::optionalPO2(dstBox.getHeight()),
  578. 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  579. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  580. GL_TEXTURE_2D, tempTex, 0);
  581. /// Set viewport to size of destination slice
  582. glViewport(0, 0, dstBox.getWidth(), dstBox.getHeight());
  583. }
  584. else
  585. {
  586. /// We are going to bind directly, so set viewport to size and position of destination slice
  587. glViewport(dstBox.left, dstBox.top, dstBox.getWidth(), dstBox.getHeight());
  588. }
  589. /// Process each destination slice
  590. for(UINT32 slice=dstBox.front; slice<dstBox.back; ++slice)
  591. {
  592. if(!tempTex)
  593. {
  594. /// Bind directly
  595. bindToFramebuffer(GL_COLOR_ATTACHMENT0_EXT, slice);
  596. }
  597. /// Calculate source texture coordinates
  598. float u1 = (float)srcBox.left / (float)src->mWidth;
  599. float v1 = (float)srcBox.top / (float)src->mHeight;
  600. float u2 = (float)srcBox.right / (float)src->mWidth;
  601. float v2 = (float)srcBox.bottom / (float)src->mHeight;
  602. /// Calculate source slice for this destination slice
  603. float w = (float)(slice - dstBox.front) / (float)dstBox.getDepth();
  604. /// Get slice # in source
  605. w = w * (float)(srcBox.getDepth() + srcBox.front);
  606. /// Normalise to texture coordinate in 0.0 .. 1.0
  607. w = (w+0.5f) / (float)src->mDepth;
  608. /// Finally we're ready to rumble
  609. glBindTexture(src->mTarget, src->mTextureID);
  610. glEnable(src->mTarget);
  611. glBegin(GL_QUADS);
  612. glTexCoord3f(u1, v1, w);
  613. glVertex2f(-1.0f, -1.0f);
  614. glTexCoord3f(u2, v1, w);
  615. glVertex2f(1.0f, -1.0f);
  616. glTexCoord3f(u2, v2, w);
  617. glVertex2f(1.0f, 1.0f);
  618. glTexCoord3f(u1, v2, w);
  619. glVertex2f(-1.0f, 1.0f);
  620. glEnd();
  621. glDisable(src->mTarget);
  622. if(tempTex)
  623. {
  624. /// Copy temporary texture
  625. glBindTexture(mTarget, mTextureID);
  626. switch(mTarget)
  627. {
  628. case GL_TEXTURE_1D:
  629. glCopyTexSubImage1D(mFaceTarget, mLevel,
  630. dstBox.left,
  631. 0, 0, dstBox.getWidth());
  632. break;
  633. case GL_TEXTURE_2D:
  634. case GL_TEXTURE_CUBE_MAP:
  635. glCopyTexSubImage2D(mFaceTarget, mLevel,
  636. dstBox.left, dstBox.top,
  637. 0, 0, dstBox.getWidth(), dstBox.getHeight());
  638. break;
  639. case GL_TEXTURE_3D:
  640. glCopyTexSubImage3D(mFaceTarget, mLevel,
  641. dstBox.left, dstBox.top, slice,
  642. 0, 0, dstBox.getWidth(), dstBox.getHeight());
  643. break;
  644. }
  645. }
  646. }
  647. /// Reset source texture to sane state
  648. glBindTexture(src->mTarget, src->mTextureID);
  649. glTexParameteri(src->mTarget, GL_TEXTURE_BASE_LEVEL, 0);
  650. /// Detach texture from temporary framebuffer
  651. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  652. GL_RENDERBUFFER_EXT, 0);
  653. /// Restore old framebuffer
  654. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, oldfb);
  655. /// Restore matrix stacks and render state
  656. glMatrixMode(GL_TEXTURE);
  657. glPopMatrix();
  658. glMatrixMode(GL_PROJECTION);
  659. glPopMatrix();
  660. glMatrixMode(GL_MODELVIEW);
  661. glPopMatrix();
  662. glPopAttrib();
  663. glDeleteTextures(1, &tempTex);
  664. }
  665. //-----------------------------------------------------------------------------
  666. /// blitFromMemory doing hardware trilinear scaling
  667. void GLTextureBuffer::blitFromMemory(const PixelData &src_orig, const Box &dstBox)
  668. {
  669. /// Fall back to normal GLHardwarePixelBuffer::blitFromMemory in case
  670. /// - FBO is not supported
  671. /// - Either source or target is luminance due doesn't looks like supported by hardware
  672. /// - the source dimensions match the destination ones, in which case no scaling is needed
  673. if(!GLEW_EXT_framebuffer_object ||
  674. PixelUtil::isLuminance(src_orig.format) ||
  675. PixelUtil::isLuminance(mFormat) ||
  676. (src_orig.getWidth() == dstBox.getWidth() &&
  677. src_orig.getHeight() == dstBox.getHeight() &&
  678. src_orig.getDepth() == dstBox.getDepth()))
  679. {
  680. GLHardwarePixelBuffer::blitFromMemory(src_orig, dstBox);
  681. return;
  682. }
  683. if(!mBuffer.contains(dstBox))
  684. CM_EXCEPT(InvalidParametersException, "destination box out of range");
  685. /// For scoped deletion of conversion buffer
  686. void* data = NULL;
  687. PixelData src;
  688. /// First, convert the srcbox to a OpenGL compatible pixel format
  689. if(GLPixelUtil::getGLOriginFormat(src_orig.format) == 0)
  690. {
  691. /// Convert to buffer internal format
  692. data = new void*[PixelUtil::getMemorySize(src.getWidth(), src.getHeight(), src.getDepth(), mFormat)];
  693. src = PixelData(src_orig.getWidth(), src_orig.getHeight(), src_orig.getDepth(), mFormat, data);
  694. PixelUtil::bulkPixelConversion(src_orig, src);
  695. }
  696. else
  697. {
  698. /// No conversion needed
  699. src = src_orig;
  700. }
  701. /// Create temporary texture to store source data
  702. GLuint id;
  703. GLenum target = (src.getDepth()!=1)?GL_TEXTURE_3D:GL_TEXTURE_2D;
  704. GLsizei width = GLPixelUtil::optionalPO2(src.getWidth());
  705. GLsizei height = GLPixelUtil::optionalPO2(src.getHeight());
  706. GLsizei depth = GLPixelUtil::optionalPO2(src.getDepth());
  707. GLenum format = GLPixelUtil::getClosestGLInternalFormat(src.format);
  708. /// Generate texture name
  709. glGenTextures(1, &id);
  710. /// Set texture type
  711. glBindTexture(target, id);
  712. /// Set automatic mipmap generation; nice for minimisation
  713. glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, 1000 );
  714. glTexParameteri(target, GL_GENERATE_MIPMAP, GL_TRUE );
  715. /// Allocate texture memory
  716. if(target == GL_TEXTURE_3D)
  717. glTexImage3D(target, 0, format, width, height, depth, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  718. else
  719. glTexImage2D(target, 0, format, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  720. /// GL texture buffer
  721. GLTextureBuffer tex(StringUtil::BLANK, target, id, 0, 0, (Usage)(HBU_STATIC_WRITE_ONLY), false, false, 0);
  722. /// Upload data to 0,0,0 in temporary texture
  723. Box tempTarget(0, 0, 0, src.getWidth(), src.getHeight(), src.getDepth());
  724. tex.upload(src, tempTarget);
  725. /// Blit
  726. blitFromTexture(&tex, tempTarget, dstBox);
  727. /// Delete temp texture
  728. glDeleteTextures(1, &id);
  729. if(data != NULL)
  730. delete[] data;
  731. }
  732. //********* GLRenderBuffer
  733. //-----------------------------------------------------------------------------
  734. GLRenderBuffer::GLRenderBuffer(GLenum format, UINT32 width, UINT32 height, GLsizei numSamples):
  735. GLHardwarePixelBuffer(width, height, 1, GLPixelUtil::getClosestOGREFormat(format),HBU_WRITE_ONLY),
  736. mRenderbufferID(0)
  737. {
  738. mGLInternalFormat = format;
  739. /// Generate renderbuffer
  740. glGenRenderbuffersEXT(1, &mRenderbufferID);
  741. /// Bind it to FBO
  742. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mRenderbufferID);
  743. /// Allocate storage for depth buffer
  744. if (numSamples > 0)
  745. {
  746. glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT,
  747. numSamples, format, width, height);
  748. }
  749. else
  750. {
  751. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, format,
  752. width, height);
  753. }
  754. }
  755. //-----------------------------------------------------------------------------
  756. GLRenderBuffer::~GLRenderBuffer()
  757. {
  758. /// Generate renderbuffer
  759. glDeleteRenderbuffersEXT(1, &mRenderbufferID);
  760. }
  761. //-----------------------------------------------------------------------------
  762. void GLRenderBuffer::bindToFramebuffer(GLenum attachment, UINT32 zoffset)
  763. {
  764. assert(zoffset < mDepth);
  765. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attachment,
  766. GL_RENDERBUFFER_EXT, mRenderbufferID);
  767. }
  768. };