BsGLTexture.cpp 7.8 KB

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