CmGLPixelBuffer.cpp 28 KB

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