BsGLTexture.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "BsGLTexture.h"
  5. #include "BsGLSupport.h"
  6. #include "BsGLPixelFormat.h"
  7. #include "BsGLPixelBuffer.h"
  8. #include "BsException.h"
  9. #include "BsBitwise.h"
  10. #include "BsCoreThread.h"
  11. #include "BsTextureManager.h"
  12. #include "BsGLRenderTexture.h"
  13. #include "BsRenderStats.h"
  14. namespace BansheeEngine
  15. {
  16. GLTexture::GLTexture(GLSupport& support)
  17. : Texture(), mTextureID(0), mGLSupport(support)
  18. { }
  19. GLTexture::~GLTexture()
  20. { }
  21. void GLTexture::initialize_internal()
  22. {
  23. // Check requested number of mipmaps
  24. UINT32 maxMips = PixelUtil::getMaxMipmaps(mWidth, mHeight, mDepth, mFormat);
  25. if(mNumMipmaps > maxMips)
  26. BS_EXCEPT(InvalidParametersException, "Invalid number of mipmaps. Maximum allowed is: " + toString(maxMips));
  27. if((mUsage & TU_RENDERTARGET) != 0)
  28. {
  29. if(mTextureType != TEX_TYPE_2D)
  30. BS_EXCEPT(NotImplementedException, "Only 2D render targets are supported at the moment");
  31. }
  32. if((mUsage & TU_DEPTHSTENCIL) != 0)
  33. {
  34. if(mTextureType != TEX_TYPE_2D)
  35. BS_EXCEPT(NotImplementedException, "Only 2D depth stencil targets are supported at the moment");
  36. if(!PixelUtil::isDepth(mFormat))
  37. BS_EXCEPT(NotImplementedException, "Supplied format is not a depth stencil format. Format: " + toString(mFormat));
  38. }
  39. // Generate texture handle
  40. glGenTextures(1, &mTextureID);
  41. // Set texture type
  42. glBindTexture(getGLTextureTarget(), mTextureID);
  43. // This needs to be set otherwise the texture doesn't get rendered
  44. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_MAX_LEVEL, mNumMipmaps);
  45. // Set some misc default parameters so NVidia won't complain, these can of course be changed later
  46. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  47. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  48. if (GLEW_VERSION_1_2)
  49. {
  50. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  51. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  52. }
  53. // Allocate internal buffer so that glTexSubImageXD can be used
  54. GLenum format = GLPixelUtil::getClosestGLInternalFormat(mFormat, mHwGamma);
  55. UINT32 width = mWidth;
  56. UINT32 height = mHeight;
  57. UINT32 depth = mDepth;
  58. if(PixelUtil::isCompressed(mFormat))
  59. {
  60. if((mUsage & TU_RENDERTARGET) != 0)
  61. BS_EXCEPT(InvalidParametersException, "Cannot use a compressed format for a render target.");
  62. if((mUsage & TU_DEPTHSTENCIL) != 0)
  63. BS_EXCEPT(InvalidParametersException, "Cannot use a compressed format for a depth stencil target.");
  64. }
  65. if((mUsage & TU_RENDERTARGET) != 0 && mTextureType == TEX_TYPE_2D && mMultisampleCount > 0)
  66. {
  67. glTexImage2DMultisample(GL_TEXTURE_2D, mMultisampleCount, format, width, height, GL_FALSE);
  68. }
  69. else if((mUsage & TU_DEPTHSTENCIL) != 0)
  70. {
  71. if(mMultisampleCount > 0)
  72. {
  73. glTexImage2DMultisample(GL_TEXTURE_2D, mMultisampleCount, format,
  74. width, height, GL_FALSE);
  75. }
  76. else
  77. {
  78. GLenum depthStencilFormat = GLPixelUtil::getDepthStencilTypeFromFormat(mFormat);
  79. glTexImage2D(GL_TEXTURE_2D, 0, format,
  80. width, height, 0,
  81. GL_DEPTH_STENCIL, depthStencilFormat, nullptr);
  82. }
  83. }
  84. else
  85. {
  86. GLenum baseFormat = GLPixelUtil::getGLOriginFormat(mFormat);
  87. GLenum baseDataType = GLPixelUtil::getGLOriginDataType(mFormat);
  88. // Run through this process to pre-generate mipmap pyramid
  89. for(UINT32 mip = 0; mip <= mNumMipmaps; mip++)
  90. {
  91. switch(mTextureType)
  92. {
  93. case TEX_TYPE_1D:
  94. glTexImage1D(GL_TEXTURE_1D, mip, format, width, 0,
  95. baseFormat, baseDataType, nullptr);
  96. break;
  97. case TEX_TYPE_2D:
  98. glTexImage2D(GL_TEXTURE_2D, mip, format,
  99. width, height, 0, baseFormat, baseDataType, nullptr);
  100. break;
  101. case TEX_TYPE_3D:
  102. glTexImage3D(GL_TEXTURE_3D, mip, format, width, height,
  103. depth, 0, baseFormat, baseDataType, nullptr);
  104. break;
  105. case TEX_TYPE_CUBE_MAP:
  106. for(UINT32 face = 0; face < 6; face++)
  107. {
  108. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, mip, format,
  109. width, height, 0, baseFormat, baseDataType, nullptr);
  110. }
  111. break;
  112. };
  113. if(width > 1)
  114. width = width/2;
  115. if(height > 1)
  116. height = height/2;
  117. if(depth > 1)
  118. depth = depth/2;
  119. }
  120. }
  121. createSurfaceList();
  122. PixelBufferPtr buffer = getBuffer(0, 0);
  123. #if BS_DEBUG_MODE
  124. if(buffer != nullptr)
  125. {
  126. if(mFormat != buffer->getFormat())
  127. {
  128. BS_EXCEPT(InternalErrorException, "Could not create a texture buffer with wanted format: " + toString(mFormat));
  129. }
  130. }
  131. #endif
  132. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_Texture);
  133. Texture::initialize_internal();
  134. }
  135. void GLTexture::destroy_internal()
  136. {
  137. mSurfaceList.clear();
  138. glDeleteTextures(1, &mTextureID);
  139. clearBufferViews();
  140. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_Texture);
  141. Texture::destroy_internal();
  142. }
  143. GLenum GLTexture::getGLTextureTarget(void) const
  144. {
  145. switch(mTextureType)
  146. {
  147. case TEX_TYPE_1D:
  148. return GL_TEXTURE_1D;
  149. case TEX_TYPE_2D:
  150. if(mMultisampleCount > 0)
  151. return GL_TEXTURE_2D_MULTISAMPLE;
  152. else
  153. return GL_TEXTURE_2D;
  154. case TEX_TYPE_3D:
  155. return GL_TEXTURE_3D;
  156. case TEX_TYPE_CUBE_MAP:
  157. return GL_TEXTURE_CUBE_MAP;
  158. default:
  159. return 0;
  160. };
  161. }
  162. GLuint GLTexture::getGLID() const
  163. {
  164. THROW_IF_NOT_CORE_THREAD;
  165. return mTextureID;
  166. }
  167. PixelData GLTexture::lockImpl(GpuLockOptions options, UINT32 mipLevel, UINT32 face)
  168. {
  169. if(mLockedBuffer != nullptr)
  170. BS_EXCEPT(InternalErrorException, "Trying to lock a buffer that's already locked.");
  171. UINT32 mipWidth = mWidth >> mipLevel;
  172. UINT32 mipHeight = mHeight >> mipLevel;
  173. UINT32 mipDepth = mDepth >> mipLevel;
  174. PixelData lockedArea(mipWidth, mipHeight, mipDepth, mFormat);
  175. mLockedBuffer = getBuffer(face, mipLevel);
  176. lockedArea.setExternalBuffer((UINT8*)mLockedBuffer->lock(options));
  177. return lockedArea;
  178. }
  179. void GLTexture::unlockImpl()
  180. {
  181. if(mLockedBuffer == nullptr)
  182. BS_EXCEPT(InternalErrorException, "Trying to unlock a buffer that's not locked.");
  183. mLockedBuffer->unlock();
  184. mLockedBuffer = nullptr;
  185. }
  186. void GLTexture::readData(PixelData& dest, UINT32 mipLevel, UINT32 face)
  187. {
  188. getBuffer(face, mipLevel)->download(dest);
  189. }
  190. void GLTexture::writeData(const PixelData& src, UINT32 mipLevel, UINT32 face, bool discardWholeBuffer)
  191. {
  192. getBuffer(face, mipLevel)->upload(src, src.getExtents());
  193. }
  194. void GLTexture::copyImpl(TexturePtr& target)
  195. {
  196. size_t numMips = std::min(getNumMipmaps(), target->getNumMipmaps());
  197. GLTexture* glTexture = static_cast<GLTexture*>(target.get());
  198. for (UINT32 face = 0; face < getNumFaces(); face++)
  199. {
  200. for(UINT32 mip = 0; mip <= numMips; mip++)
  201. {
  202. GLTextureBuffer *src = static_cast<GLTextureBuffer*>(getBuffer(face, mip).get());
  203. glTexture->getBuffer(face, mip)->blitFromTexture(src);
  204. }
  205. }
  206. }
  207. void GLTexture::createSurfaceList()
  208. {
  209. mSurfaceList.clear();
  210. for(UINT32 face=0; face<getNumFaces(); face++)
  211. {
  212. for(UINT32 mip=0; mip<=getNumMipmaps(); mip++)
  213. {
  214. GLPixelBuffer *buf = bs_new<GLTextureBuffer, PoolAlloc>(getGLTextureTarget(), mTextureID, face, mip,
  215. static_cast<GpuBufferUsage>(mUsage), mHwGamma, mMultisampleCount);
  216. mSurfaceList.push_back(bs_shared_ptr<GLPixelBuffer, PoolAlloc>(buf));
  217. if(buf->getWidth() == 0 || buf->getHeight() == 0 || buf->getDepth() == 0)
  218. {
  219. BS_EXCEPT(RenderingAPIException,
  220. "Zero sized texture surface on texture face "
  221. + toString(face)
  222. + " mipmap "+toString(mip)
  223. + ". Probably, the GL driver refused to create the texture.");
  224. }
  225. }
  226. }
  227. }
  228. std::shared_ptr<GLPixelBuffer> GLTexture::getBuffer(UINT32 face, UINT32 mipmap)
  229. {
  230. THROW_IF_NOT_CORE_THREAD;
  231. if(face >= getNumFaces())
  232. BS_EXCEPT(InvalidParametersException, "Face index out of range");
  233. if(mipmap > mNumMipmaps)
  234. BS_EXCEPT(InvalidParametersException, "Mipmap index out of range");
  235. unsigned int idx = face*(mNumMipmaps+1) + mipmap;
  236. assert(idx < mSurfaceList.size());
  237. return mSurfaceList[idx];
  238. }
  239. }