BsGLPixelBuffer.cpp 20 KB

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