BsGLTexture.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. // Run through this process to pre-generate mipmap pyramid
  83. for(UINT32 mip = 0; mip <= mNumMipmaps; mip++)
  84. {
  85. switch(mTextureType)
  86. {
  87. case TEX_TYPE_1D:
  88. glTexImage1D(GL_TEXTURE_1D, mip, format, width, 0,
  89. GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  90. break;
  91. case TEX_TYPE_2D:
  92. glTexImage2D(GL_TEXTURE_2D, mip, format,
  93. width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  94. break;
  95. case TEX_TYPE_3D:
  96. glTexImage3D(GL_TEXTURE_3D, mip, format, width, height,
  97. depth, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  98. break;
  99. case TEX_TYPE_CUBE_MAP:
  100. for(UINT32 face = 0; face < 6; face++)
  101. {
  102. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, mip, format,
  103. width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  104. }
  105. break;
  106. };
  107. if(width > 1)
  108. width = width/2;
  109. if(height > 1)
  110. height = height/2;
  111. if(depth > 1)
  112. depth = depth/2;
  113. }
  114. }
  115. createSurfaceList();
  116. PixelBufferPtr buffer = getBuffer(0, 0);
  117. #if BS_DEBUG_MODE
  118. if(buffer != nullptr)
  119. {
  120. if(mFormat != buffer->getFormat())
  121. {
  122. BS_EXCEPT(InternalErrorException, "Could not create a texture buffer with wanted format: " + toString(mFormat));
  123. }
  124. }
  125. #endif
  126. Texture::initialize_internal();
  127. }
  128. void GLTexture::destroy_internal()
  129. {
  130. mSurfaceList.clear();
  131. glDeleteTextures(1, &mTextureID);
  132. clearBufferViews();
  133. Texture::destroy_internal();
  134. }
  135. GLenum GLTexture::getGLTextureTarget(void) const
  136. {
  137. switch(mTextureType)
  138. {
  139. case TEX_TYPE_1D:
  140. return GL_TEXTURE_1D;
  141. case TEX_TYPE_2D:
  142. if(mMultisampleCount > 0)
  143. return GL_TEXTURE_2D_MULTISAMPLE;
  144. else
  145. return GL_TEXTURE_2D;
  146. case TEX_TYPE_3D:
  147. return GL_TEXTURE_3D;
  148. case TEX_TYPE_CUBE_MAP:
  149. return GL_TEXTURE_CUBE_MAP;
  150. default:
  151. return 0;
  152. };
  153. }
  154. GLuint GLTexture::getGLID() const
  155. {
  156. THROW_IF_NOT_CORE_THREAD;
  157. return mTextureID;
  158. }
  159. PixelData GLTexture::lockImpl(GpuLockOptions options, UINT32 mipLevel, UINT32 face)
  160. {
  161. if(mLockedBuffer != nullptr)
  162. BS_EXCEPT(InternalErrorException, "Trying to lock a buffer that's already locked.");
  163. UINT32 mipWidth = mWidth >> mipLevel;
  164. UINT32 mipHeight = mHeight >> mipLevel;
  165. UINT32 mipDepth = mDepth >> mipLevel;
  166. PixelData lockedArea(mipWidth, mipHeight, mipDepth, mFormat);
  167. mLockedBuffer = getBuffer(face, mipLevel);
  168. lockedArea.setExternalBuffer((UINT8*)mLockedBuffer->lock(options));
  169. return lockedArea;
  170. }
  171. void GLTexture::unlockImpl()
  172. {
  173. if(mLockedBuffer == nullptr)
  174. BS_EXCEPT(InternalErrorException, "Trying to unlock a buffer that's not locked.");
  175. mLockedBuffer->unlock();
  176. mLockedBuffer = nullptr;
  177. }
  178. void GLTexture::readData(PixelData& dest, UINT32 mipLevel, UINT32 face)
  179. {
  180. getBuffer(face, mipLevel)->download(dest);
  181. }
  182. void GLTexture::writeData(const PixelData& src, UINT32 mipLevel, UINT32 face, bool discardWholeBuffer)
  183. {
  184. getBuffer(face, mipLevel)->upload(src, src.getExtents());
  185. }
  186. void GLTexture::copyImpl(TexturePtr& target)
  187. {
  188. size_t numMips = std::min(getNumMipmaps(), target->getNumMipmaps());
  189. GLTexture* glTexture = static_cast<GLTexture*>(target.get());
  190. for (UINT32 face = 0; face < getNumFaces(); face++)
  191. {
  192. for(UINT32 mip = 0; mip <= numMips; mip++)
  193. {
  194. GLTextureBuffer *src = static_cast<GLTextureBuffer*>(getBuffer(face, mip).get());
  195. glTexture->getBuffer(face, mip)->blitFromTexture(src);
  196. }
  197. }
  198. }
  199. void GLTexture::createSurfaceList()
  200. {
  201. mSurfaceList.clear();
  202. for(UINT32 face=0; face<getNumFaces(); face++)
  203. {
  204. for(UINT32 mip=0; mip<=getNumMipmaps(); mip++)
  205. {
  206. GLPixelBuffer *buf = bs_new<GLTextureBuffer, PoolAlloc>(getGLTextureTarget(), mTextureID, face, mip,
  207. static_cast<GpuBufferUsage>(mUsage), mHwGamma, mMultisampleCount);
  208. mSurfaceList.push_back(bs_shared_ptr<GLPixelBuffer, PoolAlloc>(buf));
  209. if(buf->getWidth() == 0 || buf->getHeight() == 0 || buf->getDepth() == 0)
  210. {
  211. BS_EXCEPT(RenderingAPIException,
  212. "Zero sized texture surface on texture face "
  213. + toString(face)
  214. + " mipmap "+toString(mip)
  215. + ". Probably, the GL driver refused to create the texture.");
  216. }
  217. }
  218. }
  219. }
  220. std::shared_ptr<GLPixelBuffer> GLTexture::getBuffer(UINT32 face, UINT32 mipmap)
  221. {
  222. THROW_IF_NOT_CORE_THREAD;
  223. if(face >= getNumFaces())
  224. BS_EXCEPT(InvalidParametersException, "Face index out of range");
  225. if(mipmap > mNumMipmaps)
  226. BS_EXCEPT(InvalidParametersException, "Mipmap index out of range");
  227. unsigned int idx = face*(mNumMipmaps+1) + mipmap;
  228. assert(idx < mSurfaceList.size());
  229. return mSurfaceList[idx];
  230. }
  231. }