CmGLPixelBuffer.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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 CamelotFramework
  32. {
  33. GLPixelBuffer::GLPixelBuffer(UINT32 inWidth, UINT32 inHeight, UINT32 inDepth,
  34. PixelFormat inFormat,
  35. GpuBufferUsage usage):
  36. PixelBuffer(inWidth, inHeight, inDepth, inFormat, usage, false),
  37. mBuffer(inWidth, inHeight, inDepth, inFormat),
  38. mGLInternalFormat(GL_NONE)
  39. {
  40. mCurrentLockOptions = (GpuLockOptions)0;
  41. }
  42. GLPixelBuffer::~GLPixelBuffer()
  43. {
  44. // Force free buffer
  45. mBuffer.freeInternalBuffer();
  46. }
  47. void GLPixelBuffer::allocateBuffer()
  48. {
  49. if(mBuffer.getData())
  50. // Already allocated
  51. return;
  52. mBuffer.allocateInternalBuffer();
  53. // TODO: use PBO if we're HBU_DYNAMIC
  54. }
  55. void GLPixelBuffer::freeBuffer()
  56. {
  57. // Free buffer if we're STATIC to save memory
  58. if(mUsage & GBU_STATIC)
  59. {
  60. mBuffer.freeInternalBuffer();
  61. }
  62. }
  63. PixelData GLPixelBuffer::lockImpl(const PixelVolume lockBox, GpuLockOptions options)
  64. {
  65. allocateBuffer();
  66. if(options != GBL_WRITE_ONLY_DISCARD)
  67. {
  68. // Download the old contents of the texture
  69. download(mBuffer);
  70. }
  71. mCurrentLockOptions = options;
  72. mLockedBox = lockBox;
  73. return mBuffer.getSubVolume(lockBox);
  74. }
  75. void GLPixelBuffer::unlockImpl(void)
  76. {
  77. if (mCurrentLockOptions != GBL_READ_ONLY)
  78. {
  79. // From buffer to card, only upload if was locked for writing
  80. upload(mCurrentLock, mLockedBox);
  81. }
  82. freeBuffer();
  83. }
  84. void GLPixelBuffer::upload(const PixelData &data, const PixelVolume &dest)
  85. {
  86. CM_EXCEPT(RenderingAPIException,
  87. "Upload not possible for this pixelbuffer type");
  88. }
  89. void GLPixelBuffer::download(const PixelData &data)
  90. {
  91. CM_EXCEPT(RenderingAPIException, "Download not possible for this pixelbuffer type");
  92. }
  93. void GLPixelBuffer::blitFromTexture(GLTextureBuffer *src)
  94. {
  95. blitFromTexture(src,
  96. PixelVolume(0,0,0,src->getWidth(),src->getHeight(),src->getDepth()),
  97. PixelVolume(0,0,0,mWidth,mHeight,mDepth)
  98. );
  99. }
  100. void GLPixelBuffer::blitFromTexture(GLTextureBuffer *src, const PixelVolume &srcBox, const PixelVolume &dstBox)
  101. {
  102. CM_EXCEPT(RenderingAPIException, "BlitFromTexture not possible for this pixelbuffer type");
  103. }
  104. void GLPixelBuffer::bindToFramebuffer(GLenum attachment, UINT32 zoffset)
  105. {
  106. CM_EXCEPT(RenderingAPIException, "Framebuffer bind not possible for this pixelbuffer type");
  107. }
  108. GLTextureBuffer::GLTextureBuffer(const String &baseName, GLenum target, GLuint id,
  109. GLint face, GLint level, GpuBufferUsage usage, bool crappyCard,
  110. bool writeGamma, UINT32 fsaa):
  111. GLPixelBuffer(0, 0, 0, PF_UNKNOWN, usage),
  112. mTarget(target), mFaceTarget(0), mTextureID(id), mFace(face), mLevel(level),
  113. mSoftwareMipmap(crappyCard)
  114. {
  115. // devise mWidth, mHeight and mDepth and mFormat
  116. GLint value = 0;
  117. glBindTexture( mTarget, mTextureID );
  118. // Get face identifier
  119. mFaceTarget = mTarget;
  120. if(mTarget == GL_TEXTURE_CUBE_MAP)
  121. mFaceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + face;
  122. // Get width
  123. glGetTexLevelParameteriv(mFaceTarget, level, GL_TEXTURE_WIDTH, &value);
  124. mWidth = value;
  125. // Get height
  126. if(target == GL_TEXTURE_1D)
  127. value = 1; // Height always 1 for 1D textures
  128. else
  129. glGetTexLevelParameteriv(mFaceTarget, level, GL_TEXTURE_HEIGHT, &value);
  130. mHeight = value;
  131. // Get depth
  132. if(target != GL_TEXTURE_3D)
  133. value = 1; // Depth always 1 for non-3D textures
  134. else
  135. glGetTexLevelParameteriv(mFaceTarget, level, GL_TEXTURE_DEPTH, &value);
  136. mDepth = value;
  137. // Get format
  138. glGetTexLevelParameteriv(mFaceTarget, level, GL_TEXTURE_INTERNAL_FORMAT, &value);
  139. mGLInternalFormat = value;
  140. mFormat = GLPixelUtil::getClosestEngineFormat(value);
  141. // Default
  142. mRowPitch = mWidth;
  143. mSlicePitch = mHeight*mWidth;
  144. mSizeInBytes = PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  145. // Set up pixel box
  146. mBuffer = PixelData(mWidth, mHeight, mDepth, mFormat);
  147. if(mWidth==0 || mHeight==0 || mDepth==0)
  148. /// We are invalid, do not allocate a buffer
  149. return;
  150. }
  151. GLTextureBuffer::~GLTextureBuffer()
  152. { }
  153. void GLTextureBuffer::upload(const PixelData &data, const PixelVolume &dest)
  154. {
  155. if((mUsage & TU_RENDERTARGET) != 0)
  156. CM_EXCEPT(NotImplementedException, "Writing to render texture from CPU not supported.");
  157. glBindTexture( mTarget, mTextureID );
  158. if(PixelUtil::isCompressed(data.getFormat()))
  159. {
  160. if(data.getFormat() != mFormat || !data.isConsecutive())
  161. CM_EXCEPT(InvalidParametersException,
  162. "Compressed images must be consecutive, in the source format");
  163. GLenum format = GLPixelUtil::getClosestGLInternalFormat(mFormat);
  164. // Data must be consecutive and at beginning of buffer as PixelStorei not allowed
  165. // for compressed formats
  166. switch(mTarget) {
  167. case GL_TEXTURE_1D:
  168. // some systems (e.g. old Apple) don't like compressed subimage calls
  169. // so prefer non-sub versions
  170. if (dest.left == 0)
  171. {
  172. glCompressedTexImage1D(GL_TEXTURE_1D, mLevel,
  173. format,
  174. dest.getWidth(),
  175. 0,
  176. data.getConsecutiveSize(),
  177. data.getData());
  178. }
  179. else
  180. {
  181. glCompressedTexSubImage1D(GL_TEXTURE_1D, mLevel,
  182. dest.left,
  183. dest.getWidth(),
  184. format, data.getConsecutiveSize(),
  185. data.getData());
  186. }
  187. break;
  188. case GL_TEXTURE_2D:
  189. case GL_TEXTURE_CUBE_MAP:
  190. // some systems (e.g. old Apple) don't like compressed subimage calls
  191. // so prefer non-sub versions
  192. if (dest.left == 0 && dest.top == 0)
  193. {
  194. glCompressedTexImage2D(mFaceTarget, mLevel,
  195. format,
  196. dest.getWidth(),
  197. dest.getHeight(),
  198. 0,
  199. data.getConsecutiveSize(),
  200. data.getData());
  201. }
  202. else
  203. {
  204. glCompressedTexSubImage2D(mFaceTarget, mLevel,
  205. dest.left, dest.top,
  206. dest.getWidth(), dest.getHeight(),
  207. format, data.getConsecutiveSize(),
  208. data.getData());
  209. }
  210. break;
  211. case GL_TEXTURE_3D:
  212. // some systems (e.g. old Apple) don't like compressed subimage calls
  213. // so prefer non-sub versions
  214. if (dest.left == 0 && dest.top == 0 && dest.front == 0)
  215. {
  216. glCompressedTexImage3D(GL_TEXTURE_3D, mLevel,
  217. format,
  218. dest.getWidth(),
  219. dest.getHeight(),
  220. dest.getDepth(),
  221. 0,
  222. data.getConsecutiveSize(),
  223. data.getData());
  224. }
  225. else
  226. {
  227. glCompressedTexSubImage3D(GL_TEXTURE_3D, mLevel,
  228. dest.left, dest.top, dest.front,
  229. dest.getWidth(), dest.getHeight(), dest.getDepth(),
  230. format, data.getConsecutiveSize(),
  231. data.getData());
  232. }
  233. break;
  234. }
  235. }
  236. else
  237. {
  238. if(data.getWidth() != data.getRowPitch())
  239. glPixelStorei(GL_UNPACK_ROW_LENGTH, data.getRowPitch());
  240. if(data.getHeight()*data.getWidth() != data.getSlicePitch())
  241. glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, (data.getSlicePitch()/data.getWidth()));
  242. if(data.getLeft() > 0 || data.getTop() > 0 || data.getFront() > 0)
  243. glPixelStorei(GL_UNPACK_SKIP_PIXELS, data.getLeft() + data.getRowPitch() * data.getTop() + data.getSlicePitch() * data.getFront());
  244. if((data.getWidth()*PixelUtil::getNumElemBytes(data.getFormat())) & 3) {
  245. // Standard alignment of 4 is not right
  246. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  247. }
  248. switch(mTarget) {
  249. case GL_TEXTURE_1D:
  250. glTexSubImage1D(GL_TEXTURE_1D, mLevel,
  251. dest.left,
  252. dest.getWidth(),
  253. GLPixelUtil::getGLOriginFormat(data.getFormat()), GLPixelUtil::getGLOriginDataType(data.getFormat()),
  254. data.getData());
  255. break;
  256. case GL_TEXTURE_2D:
  257. case GL_TEXTURE_CUBE_MAP:
  258. glTexSubImage2D(mFaceTarget, mLevel,
  259. dest.left, dest.top,
  260. dest.getWidth(), dest.getHeight(),
  261. GLPixelUtil::getGLOriginFormat(data.getFormat()), GLPixelUtil::getGLOriginDataType(data.getFormat()),
  262. data.getData());
  263. break;
  264. case GL_TEXTURE_3D:
  265. glTexSubImage3D(
  266. GL_TEXTURE_3D, mLevel,
  267. dest.left, dest.top, dest.front,
  268. dest.getWidth(), dest.getHeight(), dest.getDepth(),
  269. GLPixelUtil::getGLOriginFormat(data.getFormat()), GLPixelUtil::getGLOriginDataType(data.getFormat()),
  270. data.getData());
  271. break;
  272. }
  273. }
  274. // Restore defaults
  275. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  276. if (GLEW_VERSION_1_2)
  277. glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
  278. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  279. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  280. }
  281. void GLTextureBuffer::download(const PixelData &data)
  282. {
  283. if((mUsage & TU_RENDERTARGET) != 0)
  284. CM_EXCEPT(NotImplementedException, "Reading from render texture to CPU not supported."); // TODO: This needs to be implemented
  285. if((mUsage & TU_DEPTHSTENCIL) != 0)
  286. CM_EXCEPT(NotImplementedException, "Reading from depth stencil texture to CPU not supported."); // TODO: This needs to be implemented
  287. if(data.getWidth() != getWidth() ||
  288. data.getHeight() != getHeight() ||
  289. data.getDepth() != getDepth())
  290. CM_EXCEPT(InvalidParametersException, "only download of entire buffer is supported by GL");
  291. glBindTexture( mTarget, mTextureID );
  292. if(PixelUtil::isCompressed(data.getFormat()))
  293. {
  294. if(data.getFormat() != mFormat || !data.isConsecutive())
  295. CM_EXCEPT(InvalidParametersException,
  296. "Compressed images must be consecutive, in the source format");
  297. // Data must be consecutive and at beginning of buffer as PixelStorei not allowed
  298. // for compressed formate
  299. glGetCompressedTexImage(mFaceTarget, mLevel, data.getData());
  300. }
  301. else
  302. {
  303. if(data.getWidth() != data.getRowPitch())
  304. glPixelStorei(GL_PACK_ROW_LENGTH, data.getRowPitch());
  305. if(data.getHeight()*data.getWidth() != data.getSlicePitch())
  306. glPixelStorei(GL_PACK_IMAGE_HEIGHT, (data.getSlicePitch()/data.getWidth()));
  307. if(data.getLeft() > 0 || data.getTop() > 0 || data.getFront() > 0)
  308. glPixelStorei(GL_PACK_SKIP_PIXELS, data.getLeft() + data.getRowPitch() * data.getTop() + data.getSlicePitch() * data.getFront());
  309. if((data.getWidth()*PixelUtil::getNumElemBytes(data.getFormat())) & 3) {
  310. // Standard alignment of 4 is not right
  311. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  312. }
  313. // We can only get the entire texture
  314. glGetTexImage(mFaceTarget, mLevel,
  315. GLPixelUtil::getGLOriginFormat(data.getFormat()), GLPixelUtil::getGLOriginDataType(data.getFormat()),
  316. data.getData());
  317. // Restore defaults
  318. glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  319. glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
  320. glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
  321. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  322. }
  323. }
  324. void GLTextureBuffer::bindToFramebuffer(GLenum attachment, UINT32 zoffset)
  325. {
  326. assert(zoffset < mDepth);
  327. switch(mTarget)
  328. {
  329. case GL_TEXTURE_1D:
  330. glFramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT, attachment,
  331. mFaceTarget, mTextureID, mLevel);
  332. break;
  333. case GL_TEXTURE_2D:
  334. case GL_TEXTURE_CUBE_MAP:
  335. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attachment,
  336. mFaceTarget, mTextureID, mLevel);
  337. break;
  338. case GL_TEXTURE_2D_MULTISAMPLE:
  339. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attachment,
  340. mFaceTarget, mTextureID, 0);
  341. break;
  342. case GL_TEXTURE_3D:
  343. glFramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT, attachment,
  344. mFaceTarget, mTextureID, mLevel, zoffset);
  345. break;
  346. }
  347. }
  348. void GLTextureBuffer::copyFromFramebuffer(UINT32 zoffset)
  349. {
  350. glBindTexture(mTarget, mTextureID);
  351. switch(mTarget)
  352. {
  353. case GL_TEXTURE_1D:
  354. glCopyTexSubImage1D(mFaceTarget, mLevel, 0, 0, 0, mWidth);
  355. break;
  356. case GL_TEXTURE_2D:
  357. case GL_TEXTURE_CUBE_MAP:
  358. glCopyTexSubImage2D(mFaceTarget, mLevel, 0, 0, 0, 0, mWidth, mHeight);
  359. break;
  360. case GL_TEXTURE_3D:
  361. glCopyTexSubImage3D(mFaceTarget, mLevel, 0, 0, zoffset, 0, 0, mWidth, mHeight);
  362. break;
  363. }
  364. }
  365. /// Very fast texture-to-texture blitter and hardware bi/trilinear scaling implementation using FBO
  366. /// Destination texture must be 1D, 2D, 3D, or Cube
  367. /// Source texture must be 1D, 2D or 3D
  368. /// Supports compressed formats as both source and destination format, it will use the hardware DXT compressor
  369. /// if available.
  370. /// @author W.J. van der Laan
  371. void GLTextureBuffer::blitFromTexture(GLTextureBuffer *src, const PixelVolume &srcBox, const PixelVolume &dstBox)
  372. {
  373. //std::cerr << "GLTextureBuffer::blitFromTexture " <<
  374. //src->mTextureID << ":" << srcBox.left << "," << srcBox.top << "," << srcBox.right << "," << srcBox.bottom << " " <<
  375. //mTextureID << ":" << dstBox.left << "," << dstBox.top << "," << dstBox.right << "," << dstBox.bottom << std::endl;
  376. /// Store reference to FBO manager
  377. GLRTTManager *fboMan = static_cast<GLRTTManager *>(GLRTTManager::instancePtr());
  378. /// Save and clear GL state for rendering
  379. glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT |
  380. GL_FOG_BIT | GL_LIGHTING_BIT | GL_POLYGON_BIT | GL_SCISSOR_BIT | GL_STENCIL_BUFFER_BIT |
  381. GL_TEXTURE_BIT | GL_VIEWPORT_BIT);
  382. // Important to disable all other texture units
  383. if (GLEW_VERSION_1_2)
  384. {
  385. glActiveTextureARB(GL_TEXTURE0);
  386. }
  387. /// Disable alpha, depth and scissor testing, disable blending,
  388. /// disable culling, disble lighting, disable fog and reset foreground
  389. /// colour.
  390. glDisable(GL_ALPHA_TEST);
  391. glDisable(GL_DEPTH_TEST);
  392. glDisable(GL_SCISSOR_TEST);
  393. glDisable(GL_BLEND);
  394. glDisable(GL_CULL_FACE);
  395. glDisable(GL_LIGHTING);
  396. glDisable(GL_FOG);
  397. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  398. /// Save and reset matrices
  399. glMatrixMode(GL_MODELVIEW);
  400. glPushMatrix();
  401. glLoadIdentity();
  402. glMatrixMode(GL_PROJECTION);
  403. glPushMatrix();
  404. glLoadIdentity();
  405. glMatrixMode(GL_TEXTURE);
  406. glPushMatrix();
  407. glLoadIdentity();
  408. /// Set up source texture
  409. glBindTexture(src->mTarget, src->mTextureID);
  410. /// Set filtering modes depending on the dimensions and source
  411. if(srcBox.getWidth()==dstBox.getWidth() &&
  412. srcBox.getHeight()==dstBox.getHeight() &&
  413. srcBox.getDepth()==dstBox.getDepth())
  414. {
  415. /// Dimensions match -- use nearest filtering (fastest and pixel correct)
  416. glTexParameteri(src->mTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  417. glTexParameteri(src->mTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  418. }
  419. else
  420. {
  421. /// Manual mipmaps, stay safe with bilinear filtering so that no
  422. /// intermipmap leakage occurs.
  423. glTexParameteri(src->mTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  424. glTexParameteri(src->mTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  425. }
  426. /// Clamp to edge (fastest)
  427. glTexParameteri(src->mTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  428. glTexParameteri(src->mTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  429. glTexParameteri(src->mTarget, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  430. /// Set origin base level mipmap to make sure we source from the right mip
  431. /// level.
  432. glTexParameteri(src->mTarget, GL_TEXTURE_BASE_LEVEL, src->mLevel);
  433. /// Store old binding so it can be restored later
  434. GLint oldfb;
  435. glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &oldfb);
  436. /// Set up temporary FBO
  437. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboMan->getTemporaryFBO());
  438. GLuint tempTex = 0;
  439. if(!fboMan->checkFormat(mFormat))
  440. {
  441. /// If target format not directly supported, create intermediate texture
  442. GLenum tempFormat = GLPixelUtil::getClosestGLInternalFormat(fboMan->getSupportedAlternative(mFormat));
  443. glGenTextures(1, &tempTex);
  444. glBindTexture(GL_TEXTURE_2D, tempTex);
  445. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
  446. /// Allocate temporary texture of the size of the destination area
  447. glTexImage2D(GL_TEXTURE_2D, 0, tempFormat,
  448. GLPixelUtil::optionalPO2(dstBox.getWidth()), GLPixelUtil::optionalPO2(dstBox.getHeight()),
  449. 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  450. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  451. GL_TEXTURE_2D, tempTex, 0);
  452. /// Set viewport to size of destination slice
  453. glViewport(0, 0, dstBox.getWidth(), dstBox.getHeight());
  454. }
  455. else
  456. {
  457. /// We are going to bind directly, so set viewport to size and position of destination slice
  458. glViewport(dstBox.left, dstBox.top, dstBox.getWidth(), dstBox.getHeight());
  459. }
  460. /// Process each destination slice
  461. for(UINT32 slice=dstBox.front; slice<dstBox.back; ++slice)
  462. {
  463. if(!tempTex)
  464. {
  465. /// Bind directly
  466. bindToFramebuffer(GL_COLOR_ATTACHMENT0_EXT, slice);
  467. }
  468. /// Calculate source texture coordinates
  469. float u1 = (float)srcBox.left / (float)src->mWidth;
  470. float v1 = (float)srcBox.top / (float)src->mHeight;
  471. float u2 = (float)srcBox.right / (float)src->mWidth;
  472. float v2 = (float)srcBox.bottom / (float)src->mHeight;
  473. /// Calculate source slice for this destination slice
  474. float w = (float)(slice - dstBox.front) / (float)dstBox.getDepth();
  475. /// Get slice # in source
  476. w = w * (float)(srcBox.getDepth() + srcBox.front);
  477. /// Normalise to texture coordinate in 0.0 .. 1.0
  478. w = (w+0.5f) / (float)src->mDepth;
  479. /// Finally we're ready to rumble
  480. glBindTexture(src->mTarget, src->mTextureID);
  481. glEnable(src->mTarget);
  482. glBegin(GL_QUADS);
  483. glTexCoord3f(u1, v1, w);
  484. glVertex2f(-1.0f, -1.0f);
  485. glTexCoord3f(u2, v1, w);
  486. glVertex2f(1.0f, -1.0f);
  487. glTexCoord3f(u2, v2, w);
  488. glVertex2f(1.0f, 1.0f);
  489. glTexCoord3f(u1, v2, w);
  490. glVertex2f(-1.0f, 1.0f);
  491. glEnd();
  492. glDisable(src->mTarget);
  493. if(tempTex)
  494. {
  495. /// Copy temporary texture
  496. glBindTexture(mTarget, mTextureID);
  497. switch(mTarget)
  498. {
  499. case GL_TEXTURE_1D:
  500. glCopyTexSubImage1D(mFaceTarget, mLevel,
  501. dstBox.left,
  502. 0, 0, dstBox.getWidth());
  503. break;
  504. case GL_TEXTURE_2D:
  505. case GL_TEXTURE_CUBE_MAP:
  506. glCopyTexSubImage2D(mFaceTarget, mLevel,
  507. dstBox.left, dstBox.top,
  508. 0, 0, dstBox.getWidth(), dstBox.getHeight());
  509. break;
  510. case GL_TEXTURE_3D:
  511. glCopyTexSubImage3D(mFaceTarget, mLevel,
  512. dstBox.left, dstBox.top, slice,
  513. 0, 0, dstBox.getWidth(), dstBox.getHeight());
  514. break;
  515. }
  516. }
  517. }
  518. /// Reset source texture to sane state
  519. glBindTexture(src->mTarget, src->mTextureID);
  520. glTexParameteri(src->mTarget, GL_TEXTURE_BASE_LEVEL, 0);
  521. /// Detach texture from temporary framebuffer
  522. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  523. GL_RENDERBUFFER_EXT, 0);
  524. /// Restore old framebuffer
  525. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, oldfb);
  526. /// Restore matrix stacks and render state
  527. glMatrixMode(GL_TEXTURE);
  528. glPopMatrix();
  529. glMatrixMode(GL_PROJECTION);
  530. glPopMatrix();
  531. glMatrixMode(GL_MODELVIEW);
  532. glPopMatrix();
  533. glPopAttrib();
  534. glDeleteTextures(1, &tempTex);
  535. }
  536. GLRenderBuffer::GLRenderBuffer(GLenum format, UINT32 width, UINT32 height, GLsizei numSamples):
  537. GLPixelBuffer(width, height, 1, GLPixelUtil::getClosestEngineFormat(format), GBU_DYNAMIC),
  538. mRenderbufferID(0)
  539. {
  540. mGLInternalFormat = format;
  541. /// Generate renderbuffer
  542. glGenRenderbuffersEXT(1, &mRenderbufferID);
  543. /// Bind it to FBO
  544. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mRenderbufferID);
  545. /// Allocate storage for depth buffer
  546. if (numSamples > 0)
  547. {
  548. glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT,
  549. numSamples, format, width, height);
  550. }
  551. else
  552. {
  553. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, format,
  554. width, height);
  555. }
  556. }
  557. GLRenderBuffer::~GLRenderBuffer()
  558. {
  559. /// Generate renderbuffer
  560. glDeleteRenderbuffersEXT(1, &mRenderbufferID);
  561. }
  562. void GLRenderBuffer::bindToFramebuffer(GLenum attachment, UINT32 zoffset)
  563. {
  564. assert(zoffset < mDepth);
  565. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attachment,
  566. GL_RENDERBUFFER_EXT, mRenderbufferID);
  567. }
  568. };