PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/RenderSystems/GLES/src/OgreGLESHardwarePixelBuffer.cpp

https://bitbucket.org/masterfalcon/ogre-gl3plus/
C++ | 834 lines | 601 code | 85 blank | 148 comment | 105 complexity | b8966a781647d39795024fdb45b961fd MD5 | raw file
Possible License(s): MIT, LGPL-2.1
  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) 2008 Renato Araujo Oliveira Filho <renatox@gmail.com>
  7. Copyright (c) 2000-2009 Torus Knot Software Ltd
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. -----------------------------------------------------------------------------
  24. */
  25. #include "OgreRenderSystem.h"
  26. #include "OgreGLESRenderTexture.h"
  27. #include "OgreGLESHardwarePixelBuffer.h"
  28. #include "OgreGLESPixelFormat.h"
  29. #include "OgreGLESFBORenderTexture.h"
  30. #include "OgreRoot.h"
  31. static int computeLog(GLuint value)
  32. {
  33. int i;
  34. i = 0;
  35. /* Error! */
  36. if (value == 0) return -1;
  37. for (;;)
  38. {
  39. if (value & 1)
  40. {
  41. /* Error! */
  42. if (value != 1) return -1;
  43. return i;
  44. }
  45. value = value >> 1;
  46. i++;
  47. }
  48. }
  49. namespace Ogre {
  50. GLESHardwarePixelBuffer::GLESHardwarePixelBuffer(size_t mWidth, size_t mHeight,
  51. size_t mDepth, PixelFormat mFormat,
  52. HardwareBuffer::Usage usage)
  53. : HardwarePixelBuffer(mWidth, mHeight, mDepth, mFormat, usage, false, false),
  54. mBuffer(mWidth, mHeight, mDepth, mFormat),
  55. mGLInternalFormat(0)
  56. {
  57. }
  58. GLESHardwarePixelBuffer::~GLESHardwarePixelBuffer()
  59. {
  60. // Force free buffer
  61. OGRE_DELETE [] (uint8*)mBuffer.data;
  62. }
  63. void GLESHardwarePixelBuffer::allocateBuffer()
  64. {
  65. if (mBuffer.data)
  66. // Already allocated
  67. return;
  68. mBuffer.data = OGRE_NEW_FIX_FOR_WIN32 uint8[mSizeInBytes];
  69. // TODO use PBO if we're HBU_DYNAMIC
  70. }
  71. void GLESHardwarePixelBuffer::freeBuffer()
  72. {
  73. // Free buffer if we're STATIC to save memory
  74. if (mUsage & HBU_STATIC)
  75. {
  76. OGRE_DELETE [] (uint8*)mBuffer.data;
  77. mBuffer.data = 0;
  78. }
  79. }
  80. PixelBox GLESHardwarePixelBuffer::lockImpl(const Image::Box lockBox, LockOptions options)
  81. {
  82. allocateBuffer();
  83. if (options != HardwareBuffer::HBL_DISCARD &&
  84. (mUsage & HardwareBuffer::HBU_WRITE_ONLY) == 0)
  85. {
  86. // Download the old contents of the texture
  87. download(mBuffer);
  88. }
  89. mCurrentLockOptions = options;
  90. mLockedBox = lockBox;
  91. return mBuffer.getSubVolume(lockBox);
  92. }
  93. void GLESHardwarePixelBuffer::unlockImpl(void)
  94. {
  95. if (mCurrentLockOptions != HardwareBuffer::HBL_READ_ONLY)
  96. {
  97. // From buffer to card, only upload if was locked for writing
  98. upload(mCurrentLock, mLockedBox);
  99. }
  100. freeBuffer();
  101. }
  102. void GLESHardwarePixelBuffer::blitFromMemory(const PixelBox &src, const Image::Box &dstBox)
  103. {
  104. if (!mBuffer.contains(dstBox))
  105. {
  106. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  107. "Destination box out of range",
  108. "GLESHardwarePixelBuffer::blitFromMemory");
  109. }
  110. PixelBox scaled;
  111. if (src.getWidth() != dstBox.getWidth() ||
  112. src.getHeight() != dstBox.getHeight() ||
  113. src.getDepth() != dstBox.getDepth())
  114. {
  115. // Scale to destination size. Use DevIL and not iluScale because ILU screws up for
  116. // floating point textures and cannot cope with 3D images.
  117. // This also does pixel format conversion if needed
  118. allocateBuffer();
  119. scaled = mBuffer.getSubVolume(dstBox);
  120. Image::scale(src, scaled, Image::FILTER_BILINEAR);
  121. }
  122. else if ((src.format != mFormat) ||
  123. ((GLESPixelUtil::getGLOriginFormat(src.format) == 0) && (src.format != PF_R8G8B8)))
  124. {
  125. // Extents match, but format is not accepted as valid source format for GL
  126. // do conversion in temporary buffer
  127. allocateBuffer();
  128. scaled = mBuffer.getSubVolume(dstBox);
  129. PixelUtil::bulkPixelConversion(src, scaled);
  130. if(mFormat == PF_A4R4G4B4)
  131. {
  132. // ARGB->BGRA
  133. GLESPixelUtil::convertToGLformat(scaled, scaled);
  134. }
  135. }
  136. else
  137. {
  138. allocateBuffer();
  139. scaled = src;
  140. if (src.format == PF_R8G8B8)
  141. {
  142. scaled.format = PF_B8G8R8;
  143. PixelUtil::bulkPixelConversion(src, scaled);
  144. }
  145. }
  146. upload(scaled, dstBox);
  147. freeBuffer();
  148. }
  149. void GLESHardwarePixelBuffer::blitToMemory(const Image::Box &srcBox, const PixelBox &dst)
  150. {
  151. if (!mBuffer.contains(srcBox))
  152. {
  153. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  154. "source box out of range",
  155. "GLESHardwarePixelBuffer::blitToMemory");
  156. }
  157. if (srcBox.left == 0 && srcBox.right == getWidth() &&
  158. srcBox.top == 0 && srcBox.bottom == getHeight() &&
  159. srcBox.front == 0 && srcBox.back == getDepth() &&
  160. dst.getWidth() == getWidth() &&
  161. dst.getHeight() == getHeight() &&
  162. dst.getDepth() == getDepth() &&
  163. GLESPixelUtil::getGLOriginFormat(dst.format) != 0)
  164. {
  165. // The direct case: the user wants the entire texture in a format supported by GL
  166. // so we don't need an intermediate buffer
  167. download(dst);
  168. }
  169. else
  170. {
  171. // Use buffer for intermediate copy
  172. allocateBuffer();
  173. // Download entire buffer
  174. download(mBuffer);
  175. if(srcBox.getWidth() != dst.getWidth() ||
  176. srcBox.getHeight() != dst.getHeight() ||
  177. srcBox.getDepth() != dst.getDepth())
  178. {
  179. // We need scaling
  180. Image::scale(mBuffer.getSubVolume(srcBox), dst, Image::FILTER_BILINEAR);
  181. }
  182. else
  183. {
  184. // Just copy the bit that we need
  185. PixelUtil::bulkPixelConversion(mBuffer.getSubVolume(srcBox), dst);
  186. }
  187. freeBuffer();
  188. }
  189. }
  190. void GLESHardwarePixelBuffer::upload(const PixelBox &data, const Image::Box &dest)
  191. {
  192. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  193. "Upload not possible for this pixelbuffer type",
  194. "GLESHardwarePixelBuffer::upload");
  195. }
  196. void GLESHardwarePixelBuffer::download(const PixelBox &data)
  197. {
  198. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  199. "Download not possible for this pixelbuffer type",
  200. "GLESHardwarePixelBuffer::download");
  201. }
  202. void GLESHardwarePixelBuffer::bindToFramebuffer(GLenum attachment, size_t zoffset)
  203. {
  204. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  205. "Framebuffer bind not possible for this pixelbuffer type",
  206. "GLESHardwarePixelBuffer::bindToFramebuffer");
  207. }
  208. // TextureBuffer
  209. GLESTextureBuffer::GLESTextureBuffer(const String &baseName, GLenum target, GLuint id,
  210. GLint width, GLint height, GLint internalFormat, GLenum format,
  211. GLint face, GLint level, Usage usage, bool crappyCard,
  212. bool writeGamma, uint fsaa)
  213. : GLESHardwarePixelBuffer(0, 0, 0, PF_UNKNOWN, usage),
  214. mTarget(target), mTextureID(id), mFace(face), mLevel(level), mSoftwareMipmap(crappyCard)
  215. {
  216. GL_CHECK_ERROR;
  217. glBindTexture(GL_TEXTURE_2D, mTextureID);
  218. GL_CHECK_ERROR;
  219. // Get face identifier
  220. mFaceTarget = mTarget;
  221. // Calculate the width and height of the texture at this mip level
  222. mWidth = mLevel == 0 ? width : width / pow(2, level);
  223. mHeight = mLevel == 0 ? height : height / pow(2, level);
  224. if(mWidth < 1)
  225. mWidth = 1;
  226. if(mHeight < 1)
  227. mHeight = 1;
  228. // Only 2D is supported so depth is always 1
  229. mDepth = 1;
  230. mGLInternalFormat = internalFormat;
  231. mFormat = GLESPixelUtil::getClosestOGREFormat(internalFormat, format);
  232. mRowPitch = mWidth;
  233. mSlicePitch = mHeight*mWidth;
  234. mSizeInBytes = PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  235. // Log a message
  236. // std::stringstream str;
  237. // str << "GLESHardwarePixelBuffer constructed for texture " << mTextureID
  238. // << " face " << mFace << " level " << mLevel << ": "
  239. // << "width=" << mWidth << " height="<< mHeight << " depth=" << mDepth
  240. // << "format=" << PixelUtil::getFormatName(mFormat);
  241. // LogManager::getSingleton().logMessage(LML_NORMAL, str.str());
  242. // Set up a pixel box
  243. mBuffer = PixelBox(mWidth, mHeight, mDepth, mFormat);
  244. if (mWidth==0 || mHeight==0 || mDepth==0)
  245. // We are invalid, do not allocate a buffer
  246. return;
  247. // Is this a render target?
  248. if (mUsage & TU_RENDERTARGET)
  249. {
  250. // Create render target for each slice
  251. mSliceTRT.reserve(mDepth);
  252. for(size_t zoffset=0; zoffset<mDepth; ++zoffset)
  253. {
  254. String name;
  255. name = "rtt/" + StringConverter::toString((size_t)this) + "/" + baseName;
  256. GLESSurfaceDesc target;
  257. target.buffer = this;
  258. target.zoffset = zoffset;
  259. RenderTexture *trt = GLESRTTManager::getSingleton().createRenderTexture(name, target, writeGamma, fsaa);
  260. mSliceTRT.push_back(trt);
  261. Root::getSingleton().getRenderSystem()->attachRenderTarget(*mSliceTRT[zoffset]);
  262. }
  263. }
  264. }
  265. GLESTextureBuffer::~GLESTextureBuffer()
  266. {
  267. if (mUsage & TU_RENDERTARGET)
  268. {
  269. // Delete all render targets that are not yet deleted via _clearSliceRTT because the rendertarget
  270. // was deleted by the user.
  271. for (SliceTRT::const_iterator it = mSliceTRT.begin(); it != mSliceTRT.end(); ++it)
  272. {
  273. Root::getSingleton().getRenderSystem()->destroyRenderTarget((*it)->getName());
  274. }
  275. }
  276. }
  277. void GLESTextureBuffer::upload(const PixelBox &data, const Image::Box &dest)
  278. {
  279. glBindTexture(mTarget, mTextureID);
  280. GL_CHECK_ERROR;
  281. if (PixelUtil::isCompressed(data.format))
  282. {
  283. if(data.format != mFormat || !data.isConsecutive())
  284. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  285. "Compressed images must be consecutive, in the source format",
  286. "GLESTextureBuffer::upload");
  287. GLenum format = GLESPixelUtil::getClosestGLInternalFormat(mFormat);
  288. // Data must be consecutive and at beginning of buffer as PixelStorei not allowed
  289. // for compressed formats
  290. if (dest.left == 0 && dest.top == 0)
  291. {
  292. glCompressedTexImage2D(GL_TEXTURE_2D, mLevel,
  293. format,
  294. dest.getWidth(),
  295. dest.getHeight(),
  296. 0,
  297. data.getConsecutiveSize(),
  298. data.data);
  299. GL_CHECK_ERROR;
  300. }
  301. else
  302. {
  303. glCompressedTexSubImage2D(GL_TEXTURE_2D, mLevel,
  304. dest.left, dest.top,
  305. dest.getWidth(), dest.getHeight(),
  306. format, data.getConsecutiveSize(),
  307. data.data);
  308. GL_CHECK_ERROR;
  309. }
  310. }
  311. else if (mSoftwareMipmap)
  312. {
  313. if (data.getWidth() != data.rowPitch)
  314. {
  315. // TODO
  316. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  317. "Unsupported texture format",
  318. "GLESTextureBuffer::upload");
  319. }
  320. if (data.getHeight() * data.getWidth() != data.slicePitch)
  321. {
  322. // TODO
  323. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  324. "Unsupported texture format",
  325. "GLESTextureBuffer::upload");
  326. }
  327. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  328. GL_CHECK_ERROR;
  329. buildMipmaps(data);
  330. }
  331. else
  332. {
  333. if(data.getWidth() != data.rowPitch)
  334. {
  335. // TODO
  336. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  337. "Unsupported texture format",
  338. "GLESTextureBuffer::upload");
  339. }
  340. if(data.getHeight()*data.getWidth() != data.slicePitch)
  341. {
  342. // TODO
  343. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  344. "Unsupported texture format",
  345. "GLESTextureBuffer::upload");
  346. }
  347. if ((data.getWidth() * PixelUtil::getNumElemBytes(data.format)) & 3) {
  348. // Standard alignment of 4 is not right
  349. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  350. GL_CHECK_ERROR;
  351. }
  352. glTexSubImage2D(mFaceTarget,
  353. mLevel,
  354. dest.left, dest.top,
  355. dest.getWidth(), dest.getHeight(),
  356. GLESPixelUtil::getGLOriginFormat(data.format),
  357. GLESPixelUtil::getGLOriginDataType(data.format),
  358. data.data);
  359. GL_CHECK_ERROR;
  360. }
  361. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  362. GL_CHECK_ERROR;
  363. }
  364. //-----------------------------------------------------------------------------
  365. void GLESTextureBuffer::download(const PixelBox &data)
  366. {
  367. OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED,
  368. "Downloading texture buffers is not supported by OpenGL ES",
  369. "GLESTextureBuffer::download");
  370. }
  371. //-----------------------------------------------------------------------------
  372. void GLESTextureBuffer::bindToFramebuffer(GLenum attachment, size_t zoffset)
  373. {
  374. assert(zoffset < mDepth);
  375. glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, attachment,
  376. mFaceTarget, mTextureID, mLevel);
  377. GL_CHECK_ERROR;
  378. }
  379. void GLESTextureBuffer::copyFromFramebuffer(size_t zoffset)
  380. {
  381. glBindTexture(GL_TEXTURE_2D, mTextureID);
  382. GL_CHECK_ERROR;
  383. glCopyTexSubImage2D(GL_TEXTURE_2D, mLevel, 0, 0, 0, 0, mWidth, mHeight);
  384. GL_CHECK_ERROR;
  385. }
  386. //-----------------------------------------------------------------------------
  387. void GLESTextureBuffer::blit(const HardwarePixelBufferSharedPtr &src, const Image::Box &srcBox, const Image::Box &dstBox)
  388. {
  389. GLESTextureBuffer *srct = static_cast<GLESTextureBuffer *>(src.getPointer());
  390. // TODO: Check for FBO support first
  391. // Destination texture must be 2D
  392. // Source texture must be 2D
  393. if((src->getUsage() & TU_RENDERTARGET) == 0 && (srct->mTarget == GL_TEXTURE_2D))
  394. {
  395. blitFromTexture(srct, srcBox, dstBox);
  396. }
  397. else
  398. {
  399. GLESHardwarePixelBuffer::blit(src, srcBox, dstBox);
  400. }
  401. }
  402. //-----------------------------------------------------------------------------
  403. // Very fast texture-to-texture blitter and hardware bi/trilinear scaling implementation using FBO
  404. // Destination texture must be 2D
  405. // Source texture must be 2D
  406. // Supports compressed formats as both source and destination format, it will use the hardware DXT compressor
  407. // if available.
  408. // @author W.J. van der Laan
  409. void GLESTextureBuffer::blitFromTexture(GLESTextureBuffer *src, const Image::Box &srcBox, const Image::Box &dstBox)
  410. {
  411. if(Root::getSingleton().getRenderSystem()->getCapabilities()->hasCapability(RSC_FBO) == false)
  412. {
  413. // the following code depends on FBO support, it crashes if FBO is not supported.
  414. // TODO - write PBUFFER version of this function or a version that doesn't require FBO
  415. return; // for now - do nothing.
  416. }
  417. // std::cerr << "GLESTextureBuffer::blitFromTexture " <<
  418. // src->mTextureID << ":" << srcBox.left << "," << srcBox.top << "," << srcBox.right << "," << srcBox.bottom << " " <<
  419. // mTextureID << ":" << dstBox.left << "," << dstBox.top << "," << dstBox.right << "," << dstBox.bottom << std::endl;
  420. // Store reference to FBO manager
  421. GLESFBOManager *fboMan = static_cast<GLESFBOManager *>(GLESRTTManager::getSingletonPtr());
  422. // Save and clear GL state for rendering
  423. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  424. RenderSystem* rsys = Root::getSingleton().getRenderSystem();
  425. rsys->_disableTextureUnitsFrom(0);
  426. // Disable alpha, depth and scissor testing, disable blending,
  427. // disable culling, disble lighting, disable fog and reset foreground
  428. // colour.
  429. glDisable(GL_ALPHA_TEST);
  430. glDisable(GL_DEPTH_TEST);
  431. glDisable(GL_SCISSOR_TEST);
  432. glDisable(GL_BLEND);
  433. glDisable(GL_CULL_FACE);
  434. glDisable(GL_LIGHTING);
  435. glDisable(GL_FOG);
  436. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  437. GL_CHECK_ERROR;
  438. // Save and reset matrices
  439. glMatrixMode(GL_MODELVIEW);
  440. glPushMatrix();
  441. glLoadIdentity();
  442. glMatrixMode(GL_PROJECTION);
  443. glPushMatrix();
  444. glLoadIdentity();
  445. glMatrixMode(GL_TEXTURE);
  446. glPushMatrix();
  447. glLoadIdentity();
  448. GL_CHECK_ERROR;
  449. // Set up source texture
  450. glBindTexture(src->mTarget, src->mTextureID);
  451. GL_CHECK_ERROR;
  452. // Set filtering modes depending on the dimensions and source
  453. if(srcBox.getWidth()==dstBox.getWidth() &&
  454. srcBox.getHeight()==dstBox.getHeight() &&
  455. srcBox.getDepth()==dstBox.getDepth())
  456. {
  457. // Dimensions match -- use nearest filtering (fastest and pixel correct)
  458. glTexParameteri(src->mTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
  459. GL_CHECK_ERROR;
  460. glTexParameteri(src->mTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  461. GL_CHECK_ERROR;
  462. }
  463. else
  464. {
  465. // Dimensions don't match -- use bi or trilinear filtering depending on the
  466. // source texture.
  467. if(src->mUsage & TU_AUTOMIPMAP)
  468. {
  469. // Automatic mipmaps, we can safely use trilinear filter which
  470. // brings greatly imporoved quality for minimisation.
  471. glTexParameteri(src->mTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  472. GL_CHECK_ERROR;
  473. glTexParameteri(src->mTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  474. GL_CHECK_ERROR;
  475. }
  476. else
  477. {
  478. // Manual mipmaps, stay safe with bilinear filtering so that no
  479. // intermipmap leakage occurs.
  480. glTexParameteri(src->mTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  481. GL_CHECK_ERROR;
  482. glTexParameteri(src->mTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  483. GL_CHECK_ERROR;
  484. }
  485. }
  486. // Clamp to edge (fastest)
  487. glTexParameteri(src->mTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  488. GL_CHECK_ERROR;
  489. glTexParameteri(src->mTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  490. GL_CHECK_ERROR;
  491. // Store old binding so it can be restored later
  492. GLint oldfb;
  493. glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, &oldfb);
  494. GL_CHECK_ERROR;
  495. // Set up temporary FBO
  496. glBindFramebufferOES(GL_FRAMEBUFFER_OES, fboMan->getTemporaryFBO());
  497. GL_CHECK_ERROR;
  498. GLuint tempTex = 0;
  499. if(!fboMan->checkFormat(mFormat))
  500. {
  501. // If target format not directly supported, create intermediate texture
  502. GLenum tempFormat = GLESPixelUtil::getClosestGLInternalFormat(fboMan->getSupportedAlternative(mFormat));
  503. glGenTextures(1, &tempTex);
  504. GL_CHECK_ERROR;
  505. glBindTexture(GL_TEXTURE_2D, tempTex);
  506. GL_CHECK_ERROR;
  507. // Allocate temporary texture of the size of the destination area
  508. glTexImage2D(GL_TEXTURE_2D, 0, tempFormat,
  509. GLESPixelUtil::optionalPO2(dstBox.getWidth()), GLESPixelUtil::optionalPO2(dstBox.getHeight()),
  510. 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  511. GL_CHECK_ERROR;
  512. glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES,
  513. GL_TEXTURE_2D, tempTex, 0);
  514. GL_CHECK_ERROR;
  515. // Set viewport to size of destination slice
  516. glViewport(0, 0, dstBox.getWidth(), dstBox.getHeight());
  517. GL_CHECK_ERROR;
  518. }
  519. else
  520. {
  521. // We are going to bind directly, so set viewport to size and position of destination slice
  522. glViewport(dstBox.left, dstBox.top, dstBox.getWidth(), dstBox.getHeight());
  523. GL_CHECK_ERROR;
  524. }
  525. // Process each destination slice
  526. for(size_t slice=dstBox.front; slice<dstBox.back; ++slice)
  527. {
  528. if(!tempTex)
  529. {
  530. /// Bind directly
  531. bindToFramebuffer(GL_COLOR_ATTACHMENT0_OES, slice);
  532. }
  533. if(tempTex)
  534. {
  535. // Copy temporary texture
  536. glBindTexture(mTarget, mTextureID);
  537. GL_CHECK_ERROR;
  538. switch(mTarget)
  539. {
  540. case GL_TEXTURE_2D:
  541. glCopyTexSubImage2D(mFaceTarget, mLevel,
  542. dstBox.left, dstBox.top,
  543. 0, 0, dstBox.getWidth(), dstBox.getHeight());
  544. GL_CHECK_ERROR;
  545. break;
  546. }
  547. }
  548. }
  549. // Finish up
  550. if(!tempTex)
  551. {
  552. // Generate mipmaps
  553. if(mUsage & TU_AUTOMIPMAP)
  554. {
  555. glBindTexture(mTarget, mTextureID);
  556. GL_CHECK_ERROR;
  557. glGenerateMipmapOES(mTarget);
  558. GL_CHECK_ERROR;
  559. }
  560. }
  561. // Reset source texture to sane state
  562. glBindTexture(src->mTarget, src->mTextureID);
  563. GL_CHECK_ERROR;
  564. // Detach texture from temporary framebuffer
  565. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES,
  566. GL_RENDERBUFFER_OES, 0);
  567. GL_CHECK_ERROR;
  568. // Restore old framebuffer
  569. glBindFramebufferOES(GL_FRAMEBUFFER_OES, oldfb);
  570. GL_CHECK_ERROR;
  571. // Restore matrix stacks and render state
  572. glMatrixMode(GL_TEXTURE);
  573. glPopMatrix();
  574. glMatrixMode(GL_PROJECTION);
  575. glPopMatrix();
  576. glMatrixMode(GL_MODELVIEW);
  577. glPopMatrix();
  578. GL_CHECK_ERROR;
  579. glDeleteTextures(1, &tempTex);
  580. GL_CHECK_ERROR;
  581. }
  582. //-----------------------------------------------------------------------------
  583. // blitFromMemory doing hardware trilinear scaling
  584. void GLESTextureBuffer::blitFromMemory(const PixelBox &src_orig, const Image::Box &dstBox)
  585. {
  586. // Fall back to normal GLHardwarePixelBuffer::blitFromMemory in case
  587. // - FBO is not supported
  588. // - Either source or target is luminance due doesn't looks like supported by hardware
  589. // - the source dimensions match the destination ones, in which case no scaling is needed
  590. if(!GL_OES_framebuffer_object ||
  591. PixelUtil::isLuminance(src_orig.format) ||
  592. PixelUtil::isLuminance(mFormat) ||
  593. (src_orig.getWidth() == dstBox.getWidth() &&
  594. src_orig.getHeight() == dstBox.getHeight() &&
  595. src_orig.getDepth() == dstBox.getDepth()))
  596. {
  597. GLESHardwarePixelBuffer::blitFromMemory(src_orig, dstBox);
  598. return;
  599. }
  600. if(!mBuffer.contains(dstBox))
  601. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Destination box out of range",
  602. "GLESTextureBuffer::blitFromMemory");
  603. // For scoped deletion of conversion buffer
  604. MemoryDataStreamPtr buf;
  605. PixelBox src;
  606. // First, convert the srcbox to a OpenGL compatible pixel format
  607. if(GLESPixelUtil::getGLOriginFormat(src_orig.format) == 0)
  608. {
  609. // Convert to buffer internal format
  610. buf.bind(OGRE_NEW MemoryDataStream(PixelUtil::getMemorySize(src_orig.getWidth(), src_orig.getHeight(), src_orig.getDepth(),
  611. mFormat)));
  612. src = PixelBox(src_orig.getWidth(), src_orig.getHeight(), src_orig.getDepth(), mFormat, buf->getPtr());
  613. PixelUtil::bulkPixelConversion(src_orig, src);
  614. }
  615. else
  616. {
  617. // No conversion needed
  618. src = src_orig;
  619. }
  620. // Create temporary texture to store source data
  621. GLuint id;
  622. GLenum target = GL_TEXTURE_2D;
  623. GLsizei width = GLESPixelUtil::optionalPO2(src.getWidth());
  624. GLsizei height = GLESPixelUtil::optionalPO2(src.getHeight());
  625. GLenum format = GLESPixelUtil::getClosestGLInternalFormat(src.format);
  626. GLenum datatype = GLESPixelUtil::getGLOriginDataType(src.format);
  627. // Generate texture name
  628. glGenTextures(1, &id);
  629. GL_CHECK_ERROR;
  630. // Set texture type
  631. glBindTexture(target, id);
  632. GL_CHECK_ERROR;
  633. // Set automatic mipmap generation; nice for minimisation
  634. glTexParameteri(target, GL_GENERATE_MIPMAP, GL_TRUE );
  635. GL_CHECK_ERROR;
  636. // Allocate texture memory
  637. glTexImage2D(target, 0, format, width, height, 0, format, datatype, 0);
  638. GL_CHECK_ERROR;
  639. // GL texture buffer
  640. GLESTextureBuffer tex(StringUtil::BLANK, target, id, width, height, format, src.format,
  641. 0, 0, (Usage)(TU_AUTOMIPMAP|HBU_STATIC_WRITE_ONLY), false, false, 0);
  642. // Upload data to 0,0,0 in temporary texture
  643. Image::Box tempTarget(0, 0, 0, src.getWidth(), src.getHeight(), src.getDepth());
  644. tex.upload(src, tempTarget);
  645. // Blit
  646. blitFromTexture(&tex, tempTarget, dstBox);
  647. // Delete temp texture
  648. glDeleteTextures(1, &id);
  649. GL_CHECK_ERROR;
  650. }
  651. RenderTexture *GLESTextureBuffer::getRenderTarget(size_t zoffset)
  652. {
  653. assert(mUsage & TU_RENDERTARGET);
  654. assert(zoffset < mDepth);
  655. return mSliceTRT[zoffset];
  656. }
  657. void GLESTextureBuffer::buildMipmaps(const PixelBox &data)
  658. {
  659. int width;
  660. int height;
  661. int logW;
  662. int logH;
  663. int level;
  664. PixelBox scaled = data;
  665. scaled.data = data.data;
  666. scaled.left = data.left;
  667. scaled.right = data.right;
  668. scaled.top = data.top;
  669. scaled.bottom = data.bottom;
  670. scaled.front = data.front;
  671. scaled.back = data.back;
  672. width = data.getWidth();
  673. height = data.getHeight();
  674. logW = computeLog(width);
  675. logH = computeLog(height);
  676. level = (logW > logH ? logW : logH);
  677. for (int mip = 0; mip <= level; mip++)
  678. {
  679. GLenum glFormat = GLESPixelUtil::getGLOriginFormat(scaled.format);
  680. GLenum dataType = GLESPixelUtil::getGLOriginDataType(scaled.format);
  681. glTexImage2D(GL_TEXTURE_2D,
  682. mip,
  683. glFormat,
  684. width, height,
  685. 0,
  686. glFormat,
  687. dataType,
  688. scaled.data);
  689. GL_CHECK_ERROR;
  690. if (mip != 0)
  691. {
  692. OGRE_DELETE[] (uint8*) scaled.data;
  693. scaled.data = 0;
  694. }
  695. if (width > 1)
  696. {
  697. width = width / 2;
  698. }
  699. if (height > 1)
  700. {
  701. height = height / 2;
  702. }
  703. int sizeInBytes = PixelUtil::getMemorySize(width, height, 1,
  704. data.format);
  705. scaled = PixelBox(width, height, 1, data.format);
  706. scaled.data = OGRE_NEW_FIX_FOR_WIN32 uint8[sizeInBytes];
  707. Image::scale(data, scaled, Image::FILTER_LINEAR);
  708. }
  709. }
  710. //********* GLESRenderBuffer
  711. //-----------------------------------------------------------------------------
  712. GLESRenderBuffer::GLESRenderBuffer(GLenum format, size_t width, size_t height, GLsizei numSamples):
  713. GLESHardwarePixelBuffer(width, height, 1, GLESPixelUtil::getClosestOGREFormat(format, PF_A8R8G8B8),HBU_WRITE_ONLY)
  714. {
  715. mGLInternalFormat = format;
  716. // Generate renderbuffer
  717. glGenRenderbuffersOES(1, &mRenderbufferID);
  718. GL_CHECK_ERROR;
  719. // Bind it to FBO
  720. glBindRenderbufferOES(GL_RENDERBUFFER_OES, mRenderbufferID);
  721. GL_CHECK_ERROR;
  722. // Allocate storage for depth buffer
  723. if (numSamples <= 0)
  724. {
  725. glRenderbufferStorageOES(GL_RENDERBUFFER_OES, format,
  726. width, height);
  727. GL_CHECK_ERROR;
  728. }
  729. }
  730. //-----------------------------------------------------------------------------
  731. GLESRenderBuffer::~GLESRenderBuffer()
  732. {
  733. // Generate renderbuffer
  734. glDeleteRenderbuffersOES(1, &mRenderbufferID);
  735. GL_CHECK_ERROR;
  736. }
  737. //-----------------------------------------------------------------------------
  738. void GLESRenderBuffer::bindToFramebuffer(GLenum attachment, size_t zoffset)
  739. {
  740. assert(zoffset < mDepth);
  741. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, attachment,
  742. GL_RENDERBUFFER_OES, mRenderbufferID);
  743. GL_CHECK_ERROR;
  744. }
  745. };