BsGLTexture.cpp 7.9 KB

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