2
0

BsGLTexture.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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)
  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. // Set some misc default parameters so NVidia won't complain, these can of course be changed later
  43. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  44. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  45. if (GLEW_VERSION_1_2)
  46. {
  47. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  48. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  49. }
  50. // Allocate internal buffer so that glTexSubImageXD can be used
  51. GLenum format = GLPixelUtil::getClosestGLInternalFormat(mFormat, mHwGamma);
  52. UINT32 width = mWidth;
  53. UINT32 height = mHeight;
  54. UINT32 depth = mDepth;
  55. if(PixelUtil::isCompressed(mFormat))
  56. {
  57. if((mUsage & TU_RENDERTARGET) != 0)
  58. BS_EXCEPT(InvalidParametersException, "Cannot use a compressed format for a render target.");
  59. if((mUsage & TU_DEPTHSTENCIL) != 0)
  60. BS_EXCEPT(InvalidParametersException, "Cannot use a compressed format for a depth stencil target.");
  61. }
  62. if((mUsage & TU_RENDERTARGET) != 0 && mTextureType == TEX_TYPE_2D && mMultisampleCount > 0)
  63. {
  64. glTexImage2DMultisample(GL_TEXTURE_2D, mMultisampleCount, format, width, height, GL_FALSE);
  65. }
  66. else if((mUsage & TU_DEPTHSTENCIL) != 0)
  67. {
  68. if(mMultisampleCount > 0)
  69. {
  70. glTexImage2DMultisample(GL_TEXTURE_2D, mMultisampleCount, format,
  71. width, height, GL_FALSE);
  72. }
  73. else
  74. {
  75. GLenum depthStencilFormat = GLPixelUtil::getDepthStencilTypeFromFormat(mFormat);
  76. glTexImage2D(GL_TEXTURE_2D, 0, format,
  77. width, height, 0,
  78. GL_DEPTH_STENCIL, depthStencilFormat, nullptr);
  79. }
  80. }
  81. else
  82. {
  83. GLenum baseFormat = GLPixelUtil::getGLOriginFormat(mFormat);
  84. GLenum baseDataType = GLPixelUtil::getGLOriginDataType(mFormat);
  85. // Run through this process to pre-generate mipmap pyramid
  86. for(UINT32 mip = 0; mip <= mNumMipmaps; mip++)
  87. {
  88. switch(mTextureType)
  89. {
  90. case TEX_TYPE_1D:
  91. glTexImage1D(GL_TEXTURE_1D, mip, format, width, 0,
  92. baseFormat, baseDataType, nullptr);
  93. break;
  94. case TEX_TYPE_2D:
  95. glTexImage2D(GL_TEXTURE_2D, mip, format,
  96. width, height, 0, baseFormat, baseDataType, nullptr);
  97. break;
  98. case TEX_TYPE_3D:
  99. glTexImage3D(GL_TEXTURE_3D, mip, format, width, height,
  100. depth, 0, baseFormat, baseDataType, nullptr);
  101. break;
  102. case TEX_TYPE_CUBE_MAP:
  103. for(UINT32 face = 0; face < 6; face++)
  104. {
  105. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, mip, format,
  106. width, height, 0, baseFormat, baseDataType, nullptr);
  107. }
  108. break;
  109. };
  110. if(width > 1)
  111. width = width/2;
  112. if(height > 1)
  113. height = height/2;
  114. if(depth > 1)
  115. depth = depth/2;
  116. }
  117. }
  118. createSurfaceList();
  119. PixelBufferPtr buffer = getBuffer(0, 0);
  120. #if BS_DEBUG_MODE
  121. if(buffer != nullptr)
  122. {
  123. if(mFormat != buffer->getFormat())
  124. {
  125. BS_EXCEPT(InternalErrorException, "Could not create a texture buffer with wanted format: " + toString(mFormat));
  126. }
  127. }
  128. #endif
  129. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_Texture);
  130. Texture::initialize_internal();
  131. }
  132. void GLTexture::destroy_internal()
  133. {
  134. mSurfaceList.clear();
  135. glDeleteTextures(1, &mTextureID);
  136. clearBufferViews();
  137. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_Texture);
  138. Texture::destroy_internal();
  139. }
  140. GLenum GLTexture::getGLTextureTarget() const
  141. {
  142. switch(mTextureType)
  143. {
  144. case TEX_TYPE_1D:
  145. return GL_TEXTURE_1D;
  146. case TEX_TYPE_2D:
  147. if(mMultisampleCount > 0)
  148. return GL_TEXTURE_2D_MULTISAMPLE;
  149. else
  150. return GL_TEXTURE_2D;
  151. case TEX_TYPE_3D:
  152. return GL_TEXTURE_3D;
  153. case TEX_TYPE_CUBE_MAP:
  154. return GL_TEXTURE_CUBE_MAP;
  155. default:
  156. return 0;
  157. };
  158. }
  159. GLuint GLTexture::getGLID() const
  160. {
  161. THROW_IF_NOT_CORE_THREAD;
  162. return mTextureID;
  163. }
  164. PixelData GLTexture::lockImpl(GpuLockOptions options, UINT32 mipLevel, UINT32 face)
  165. {
  166. if (mMultisampleCount > 0)
  167. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  168. if(mLockedBuffer != nullptr)
  169. BS_EXCEPT(InternalErrorException, "Trying to lock a buffer that's already locked.");
  170. UINT32 mipWidth = mWidth >> mipLevel;
  171. UINT32 mipHeight = mHeight >> mipLevel;
  172. UINT32 mipDepth = mDepth >> mipLevel;
  173. PixelData lockedArea(mipWidth, mipHeight, mipDepth, mFormat);
  174. mLockedBuffer = getBuffer(face, mipLevel);
  175. lockedArea.setExternalBuffer((UINT8*)mLockedBuffer->lock(options));
  176. return lockedArea;
  177. }
  178. void GLTexture::unlockImpl()
  179. {
  180. if(mLockedBuffer == nullptr)
  181. BS_EXCEPT(InternalErrorException, "Trying to unlock a buffer that's not locked.");
  182. mLockedBuffer->unlock();
  183. mLockedBuffer = nullptr;
  184. }
  185. void GLTexture::readData(PixelData& dest, UINT32 mipLevel, UINT32 face)
  186. {
  187. if (mMultisampleCount > 0)
  188. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  189. getBuffer(face, mipLevel)->download(dest);
  190. }
  191. void GLTexture::writeData(const PixelData& src, UINT32 mipLevel, UINT32 face, bool discardWholeBuffer)
  192. {
  193. if (mMultisampleCount > 0)
  194. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  195. getBuffer(face, mipLevel)->upload(src, src.getExtents());
  196. }
  197. void GLTexture::copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel, TexturePtr& target)
  198. {
  199. size_t numMips = std::min(getNumMipmaps(), target->getNumMipmaps());
  200. GLTexture* destTex = static_cast<GLTexture*>(target.get());
  201. GLTextureBuffer *src = static_cast<GLTextureBuffer*>(getBuffer(srcFace, srcMipLevel).get());
  202. destTex->getBuffer(destFace, destMipLevel)->blitFromTexture(src);
  203. }
  204. void GLTexture::createSurfaceList()
  205. {
  206. mSurfaceList.clear();
  207. for(UINT32 face = 0; face < getNumFaces(); face++)
  208. {
  209. for(UINT32 mip = 0; mip <= getNumMipmaps(); mip++)
  210. {
  211. GLPixelBuffer *buf = bs_new<GLTextureBuffer, PoolAlloc>(getGLTextureTarget(), mTextureID, face, mip,
  212. static_cast<GpuBufferUsage>(mUsage), mHwGamma, mMultisampleCount);
  213. mSurfaceList.push_back(bs_shared_ptr<GLPixelBuffer, PoolAlloc>(buf));
  214. if(buf->getWidth() == 0 || buf->getHeight() == 0 || buf->getDepth() == 0)
  215. {
  216. BS_EXCEPT(RenderingAPIException,
  217. "Zero sized texture surface on texture face "
  218. + toString(face)
  219. + " mipmap "+toString(mip)
  220. + ". Probably, the GL driver refused to create the texture.");
  221. }
  222. }
  223. }
  224. }
  225. std::shared_ptr<GLPixelBuffer> GLTexture::getBuffer(UINT32 face, UINT32 mipmap)
  226. {
  227. THROW_IF_NOT_CORE_THREAD;
  228. if(face >= getNumFaces())
  229. BS_EXCEPT(InvalidParametersException, "Face index out of range");
  230. if(mipmap > mNumMipmaps)
  231. BS_EXCEPT(InvalidParametersException, "Mipmap index out of range");
  232. unsigned int idx = face*(mNumMipmaps+1) + mipmap;
  233. assert(idx < mSurfaceList.size());
  234. return mSurfaceList[idx];
  235. }
  236. }