CmGLHardwarePixelBuffer.cpp 33 KB

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