BsGLTexture.cpp 8.0 KB

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