BsGLTexture.cpp 8.9 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. GLTextureCore::GLTextureCore(GLSupport& support, TextureType textureType, UINT32 width, UINT32 height,
  14. UINT32 depth, UINT32 numMipmaps, PixelFormat format, int usage, bool hwGamma, UINT32 multisampleCount)
  15. : TextureCore(textureType, width, height, depth, numMipmaps, format, usage, hwGamma, multisampleCount),
  16. mTextureID(0), mGLSupport(support), mGLFormat(0)
  17. { }
  18. GLTextureCore::~GLTextureCore()
  19. { }
  20. void GLTextureCore::initialize()
  21. {
  22. UINT32 width = mProperties.getWidth();
  23. UINT32 height = mProperties.getHeight();
  24. UINT32 depth = mProperties.getDepth();
  25. TextureType texType = mProperties.getTextureType();
  26. PixelFormat pixFormat = mProperties.getFormat();
  27. int usage = mProperties.getUsage();
  28. UINT32 numMips = mProperties.getNumMipmaps();
  29. // Check requested number of mipmaps
  30. UINT32 maxMips = PixelUtil::getMaxMipmaps(width, height, depth, mProperties.getFormat());
  31. if (numMips > maxMips)
  32. BS_EXCEPT(InvalidParametersException, "Invalid number of mipmaps. Maximum allowed is: " + toString(maxMips));
  33. if ((usage & TU_RENDERTARGET) != 0)
  34. {
  35. if (texType != TEX_TYPE_2D)
  36. BS_EXCEPT(NotImplementedException, "Only 2D render targets are supported at the moment");
  37. }
  38. if ((usage & TU_DEPTHSTENCIL) != 0)
  39. {
  40. if (texType != TEX_TYPE_2D)
  41. BS_EXCEPT(NotImplementedException, "Only 2D depth stencil targets are supported at the moment");
  42. if (!PixelUtil::isDepth(pixFormat))
  43. BS_EXCEPT(NotImplementedException, "Supplied format is not a depth stencil format. Format: " + toString(pixFormat));
  44. }
  45. // Generate texture handle
  46. glGenTextures(1, &mTextureID);
  47. // Set texture type
  48. glBindTexture(getGLTextureTarget(), mTextureID);
  49. // This needs to be set otherwise the texture doesn't get rendered
  50. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_MAX_LEVEL, numMips);
  51. // Allocate internal buffer so that glTexSubImageXD can be used
  52. mGLFormat = GLPixelUtil::getClosestGLInternalFormat(pixFormat, mProperties.isHardwareGammaEnabled());
  53. if (PixelUtil::isCompressed(pixFormat))
  54. {
  55. if((usage & TU_RENDERTARGET) != 0)
  56. BS_EXCEPT(InvalidParametersException, "Cannot use a compressed format for a render target.");
  57. if ((usage & TU_DEPTHSTENCIL) != 0)
  58. BS_EXCEPT(InvalidParametersException, "Cannot use a compressed format for a depth stencil target.");
  59. }
  60. UINT32 sampleCount = mProperties.getMultisampleCount();
  61. if ((usage & TU_RENDERTARGET) != 0 && mProperties.getTextureType() == TEX_TYPE_2D && sampleCount > 0)
  62. {
  63. glTexImage2DMultisample(GL_TEXTURE_2D, sampleCount, mGLFormat, width, height, GL_FALSE);
  64. }
  65. else if ((usage & TU_DEPTHSTENCIL) != 0)
  66. {
  67. if (sampleCount > 0)
  68. {
  69. glTexImage2DMultisample(GL_TEXTURE_2D, sampleCount, mGLFormat,
  70. width, height, GL_FALSE);
  71. }
  72. else
  73. {
  74. GLenum depthStencilFormat = GLPixelUtil::getDepthStencilTypeFromFormat(pixFormat);
  75. glTexImage2D(GL_TEXTURE_2D, 0, mGLFormat,
  76. width, height, 0,
  77. GL_DEPTH_STENCIL, depthStencilFormat, nullptr);
  78. }
  79. }
  80. else
  81. {
  82. GLenum baseFormat = GLPixelUtil::getGLOriginFormat(pixFormat);
  83. GLenum baseDataType = GLPixelUtil::getGLOriginDataType(pixFormat);
  84. // Run through this process to pre-generate mipmap pyramid
  85. for (UINT32 mip = 0; mip <= numMips; mip++)
  86. {
  87. switch (texType)
  88. {
  89. case TEX_TYPE_1D:
  90. glTexImage1D(GL_TEXTURE_1D, mip, mGLFormat, width, 0,
  91. baseFormat, baseDataType, nullptr);
  92. break;
  93. case TEX_TYPE_2D:
  94. glTexImage2D(GL_TEXTURE_2D, mip, mGLFormat,
  95. width, height, 0, baseFormat, baseDataType, nullptr);
  96. break;
  97. case TEX_TYPE_3D:
  98. glTexImage3D(GL_TEXTURE_3D, mip, mGLFormat, 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, mGLFormat,
  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(pixFormat != buffer->getFormat())
  123. {
  124. BS_EXCEPT(InternalErrorException, "Could not create a texture buffer with wanted format: " + toString(pixFormat));
  125. }
  126. }
  127. #endif
  128. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_Texture);
  129. TextureCore::initialize();
  130. }
  131. void GLTextureCore::destroy()
  132. {
  133. mSurfaceList.clear();
  134. glDeleteTextures(1, &mTextureID);
  135. clearBufferViews();
  136. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_Texture);
  137. TextureCore::destroy();
  138. }
  139. GLenum GLTextureCore::getGLTextureTarget() const
  140. {
  141. switch (mProperties.getTextureType())
  142. {
  143. case TEX_TYPE_1D:
  144. return GL_TEXTURE_1D;
  145. case TEX_TYPE_2D:
  146. if (mProperties.getMultisampleCount() > 0)
  147. return GL_TEXTURE_2D_MULTISAMPLE;
  148. else
  149. return GL_TEXTURE_2D;
  150. case TEX_TYPE_3D:
  151. return GL_TEXTURE_3D;
  152. case TEX_TYPE_CUBE_MAP:
  153. return GL_TEXTURE_CUBE_MAP;
  154. default:
  155. return 0;
  156. };
  157. }
  158. GLuint GLTextureCore::getGLID() const
  159. {
  160. THROW_IF_NOT_CORE_THREAD;
  161. return mTextureID;
  162. }
  163. PixelData GLTextureCore::lockImpl(GpuLockOptions options, UINT32 mipLevel, UINT32 face)
  164. {
  165. if (mProperties.getMultisampleCount() > 0)
  166. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  167. if(mLockedBuffer != nullptr)
  168. BS_EXCEPT(InternalErrorException, "Trying to lock a buffer that's already locked.");
  169. UINT32 mipWidth = mProperties.getWidth() >> mipLevel;
  170. UINT32 mipHeight = mProperties.getHeight() >> mipLevel;
  171. UINT32 mipDepth = mProperties.getDepth() >> mipLevel;
  172. PixelData lockedArea(mipWidth, mipHeight, mipDepth, mProperties.getFormat());
  173. mLockedBuffer = getBuffer(face, mipLevel);
  174. lockedArea.setExternalBuffer((UINT8*)mLockedBuffer->lock(options));
  175. return lockedArea;
  176. }
  177. void GLTextureCore::unlockImpl()
  178. {
  179. if(mLockedBuffer == nullptr)
  180. BS_EXCEPT(InternalErrorException, "Trying to unlock a buffer that's not locked.");
  181. mLockedBuffer->unlock();
  182. mLockedBuffer = nullptr;
  183. }
  184. void GLTextureCore::readData(PixelData& dest, UINT32 mipLevel, UINT32 face)
  185. {
  186. if (mProperties.getMultisampleCount() > 0)
  187. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  188. getBuffer(face, mipLevel)->download(dest);
  189. }
  190. void GLTextureCore::writeData(const PixelData& src, UINT32 mipLevel, UINT32 face, bool discardWholeBuffer)
  191. {
  192. if (mProperties.getMultisampleCount() > 0)
  193. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  194. getBuffer(face, mipLevel)->upload(src, src.getExtents());
  195. }
  196. void GLTextureCore::copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel, const SPtr<TextureCore>& target)
  197. {
  198. size_t numMips = std::min(mProperties.getNumMipmaps(), target->getProperties().getNumMipmaps());
  199. GLTextureCore* destTex = static_cast<GLTextureCore*>(target.get());
  200. GLTextureBuffer *src = static_cast<GLTextureBuffer*>(getBuffer(srcFace, srcMipLevel).get());
  201. destTex->getBuffer(destFace, destMipLevel)->blitFromTexture(src);
  202. }
  203. void GLTextureCore::createSurfaceList()
  204. {
  205. mSurfaceList.clear();
  206. for (UINT32 face = 0; face < mProperties.getNumFaces(); face++)
  207. {
  208. for (UINT32 mip = 0; mip <= mProperties.getNumMipmaps(); mip++)
  209. {
  210. GLPixelBuffer *buf = bs_new<GLTextureBuffer, PoolAlloc>(getGLTextureTarget(), mTextureID, face, mip,
  211. static_cast<GpuBufferUsage>(mProperties.getUsage()),
  212. mProperties.isHardwareGammaEnabled(), mProperties.getMultisampleCount());
  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> GLTextureCore::getBuffer(UINT32 face, UINT32 mipmap)
  226. {
  227. THROW_IF_NOT_CORE_THREAD;
  228. if(face >= mProperties.getNumFaces())
  229. BS_EXCEPT(InvalidParametersException, "Face index out of range");
  230. if (mipmap > mProperties.getNumMipmaps())
  231. BS_EXCEPT(InvalidParametersException, "Mipmap index out of range");
  232. unsigned int idx = face * (mProperties.getNumMipmaps() + 1) + mipmap;
  233. assert(idx < mSurfaceList.size());
  234. return mSurfaceList[idx];
  235. }
  236. }