BsGLTexture.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGLTexture.h"
  4. #include "BsGLSupport.h"
  5. #include "BsGLPixelFormat.h"
  6. #include "BsGLPixelBuffer.h"
  7. #include "BsException.h"
  8. #include "BsBitwise.h"
  9. #include "BsCoreThread.h"
  10. #include "BsTextureManager.h"
  11. #include "BsGLRenderTexture.h"
  12. #include "BsRenderStats.h"
  13. namespace BansheeEngine
  14. {
  15. GLTextureCore::GLTextureCore(GLSupport& support, TextureType textureType, UINT32 width, UINT32 height, UINT32 depth,
  16. UINT32 numMipmaps, PixelFormat format, int usage, bool hwGamma, UINT32 multisampleCount, const PixelDataPtr& initialData)
  17. : TextureCore(textureType, width, height, depth, numMipmaps, format, usage, hwGamma, multisampleCount, initialData),
  18. mTextureID(0), mGLSupport(support), mGLFormat(0)
  19. { }
  20. GLTextureCore::~GLTextureCore()
  21. {
  22. mSurfaceList.clear();
  23. glDeleteTextures(1, &mTextureID);
  24. clearBufferViews();
  25. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_Texture);
  26. }
  27. void GLTextureCore::initialize()
  28. {
  29. UINT32 width = mProperties.getWidth();
  30. UINT32 height = mProperties.getHeight();
  31. UINT32 depth = mProperties.getDepth();
  32. TextureType texType = mProperties.getTextureType();
  33. PixelFormat pixFormat = mProperties.getFormat();
  34. int usage = mProperties.getUsage();
  35. UINT32 numMips = mProperties.getNumMipmaps();
  36. // Check requested number of mipmaps
  37. UINT32 maxMips = PixelUtil::getMaxMipmaps(width, height, depth, mProperties.getFormat());
  38. if (numMips > maxMips)
  39. BS_EXCEPT(InvalidParametersException, "Invalid number of mipmaps. Maximum allowed is: " + toString(maxMips));
  40. if ((usage & TU_RENDERTARGET) != 0)
  41. {
  42. if (texType != TEX_TYPE_2D)
  43. BS_EXCEPT(NotImplementedException, "Only 2D render targets are supported at the moment");
  44. }
  45. if ((usage & TU_DEPTHSTENCIL) != 0)
  46. {
  47. if (texType != TEX_TYPE_2D)
  48. BS_EXCEPT(NotImplementedException, "Only 2D depth stencil targets are supported at the moment");
  49. if (!PixelUtil::isDepth(pixFormat))
  50. BS_EXCEPT(NotImplementedException, "Supplied format is not a depth stencil format. Format: " + toString(pixFormat));
  51. }
  52. // Generate texture handle
  53. glGenTextures(1, &mTextureID);
  54. // Set texture type
  55. glBindTexture(getGLTextureTarget(), mTextureID);
  56. // This needs to be set otherwise the texture doesn't get rendered
  57. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_MAX_LEVEL, numMips);
  58. // Allocate internal buffer so that glTexSubImageXD can be used
  59. mGLFormat = GLPixelUtil::getClosestGLInternalFormat(pixFormat, mProperties.isHardwareGammaEnabled());
  60. if (PixelUtil::isCompressed(pixFormat))
  61. {
  62. if((usage & TU_RENDERTARGET) != 0)
  63. BS_EXCEPT(InvalidParametersException, "Cannot use a compressed format for a render target.");
  64. if ((usage & TU_DEPTHSTENCIL) != 0)
  65. BS_EXCEPT(InvalidParametersException, "Cannot use a compressed format for a depth stencil target.");
  66. }
  67. UINT32 sampleCount = mProperties.getMultisampleCount();
  68. if ((usage & TU_RENDERTARGET) != 0 && mProperties.getTextureType() == TEX_TYPE_2D && sampleCount > 1)
  69. {
  70. glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, sampleCount, mGLFormat, width, height, GL_FALSE);
  71. }
  72. else if ((usage & TU_DEPTHSTENCIL) != 0)
  73. {
  74. if (sampleCount > 1)
  75. {
  76. glTexImage2DMultisample(GL_TEXTURE_2D, sampleCount, mGLFormat,
  77. width, height, GL_FALSE);
  78. }
  79. else
  80. {
  81. GLenum depthStencilFormat = GLPixelUtil::getDepthStencilTypeFromFormat(pixFormat);
  82. glTexImage2D(GL_TEXTURE_2D, 0, mGLFormat,
  83. width, height, 0,
  84. GL_DEPTH_STENCIL, depthStencilFormat, nullptr);
  85. }
  86. }
  87. else
  88. {
  89. GLenum baseFormat = GLPixelUtil::getGLOriginFormat(pixFormat);
  90. GLenum baseDataType = GLPixelUtil::getGLOriginDataType(pixFormat);
  91. // Run through this process to pre-generate mipmap pyramid
  92. for (UINT32 mip = 0; mip <= numMips; mip++)
  93. {
  94. switch (texType)
  95. {
  96. case TEX_TYPE_1D:
  97. glTexImage1D(GL_TEXTURE_1D, mip, mGLFormat, width, 0,
  98. baseFormat, baseDataType, nullptr);
  99. break;
  100. case TEX_TYPE_2D:
  101. glTexImage2D(GL_TEXTURE_2D, mip, mGLFormat,
  102. width, height, 0, baseFormat, baseDataType, nullptr);
  103. break;
  104. case TEX_TYPE_3D:
  105. glTexImage3D(GL_TEXTURE_3D, mip, mGLFormat, width, height,
  106. depth, 0, baseFormat, baseDataType, nullptr);
  107. break;
  108. case TEX_TYPE_CUBE_MAP:
  109. for(UINT32 face = 0; face < 6; face++)
  110. {
  111. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, mip, mGLFormat,
  112. width, height, 0, baseFormat, baseDataType, nullptr);
  113. }
  114. break;
  115. };
  116. if(width > 1)
  117. width = width/2;
  118. if(height > 1)
  119. height = height/2;
  120. if(depth > 1)
  121. depth = depth/2;
  122. }
  123. }
  124. createSurfaceList();
  125. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_Texture);
  126. TextureCore::initialize();
  127. }
  128. GLenum GLTextureCore::getGLTextureTarget() const
  129. {
  130. switch (mProperties.getTextureType())
  131. {
  132. case TEX_TYPE_1D:
  133. return GL_TEXTURE_1D;
  134. case TEX_TYPE_2D:
  135. if (mProperties.getMultisampleCount() > 1)
  136. return GL_TEXTURE_2D_MULTISAMPLE;
  137. else
  138. return GL_TEXTURE_2D;
  139. case TEX_TYPE_3D:
  140. return GL_TEXTURE_3D;
  141. case TEX_TYPE_CUBE_MAP:
  142. return GL_TEXTURE_CUBE_MAP;
  143. default:
  144. return 0;
  145. };
  146. }
  147. GLuint GLTextureCore::getGLID() const
  148. {
  149. THROW_IF_NOT_CORE_THREAD;
  150. return mTextureID;
  151. }
  152. PixelData GLTextureCore::lockImpl(GpuLockOptions options, UINT32 mipLevel, UINT32 face)
  153. {
  154. if (mProperties.getMultisampleCount() > 1)
  155. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  156. if(mLockedBuffer != nullptr)
  157. BS_EXCEPT(InternalErrorException, "Trying to lock a buffer that's already locked.");
  158. UINT32 mipWidth = std::max(1u, mProperties.getWidth() >> mipLevel);
  159. UINT32 mipHeight = std::max(1u, mProperties.getHeight() >> mipLevel);
  160. UINT32 mipDepth = std::max(1u, mProperties.getDepth() >> mipLevel);
  161. PixelData lockedArea(mipWidth, mipHeight, mipDepth, mProperties.getFormat());
  162. mLockedBuffer = getBuffer(face, mipLevel);
  163. lockedArea.setExternalBuffer((UINT8*)mLockedBuffer->lock(options));
  164. return lockedArea;
  165. }
  166. void GLTextureCore::unlockImpl()
  167. {
  168. if(mLockedBuffer == nullptr)
  169. BS_EXCEPT(InternalErrorException, "Trying to unlock a buffer that's not locked.");
  170. mLockedBuffer->unlock();
  171. mLockedBuffer = nullptr;
  172. }
  173. void GLTextureCore::readData(PixelData& dest, UINT32 mipLevel, UINT32 face)
  174. {
  175. if (mProperties.getMultisampleCount() > 1)
  176. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  177. getBuffer(face, mipLevel)->download(dest);
  178. }
  179. void GLTextureCore::writeData(const PixelData& src, UINT32 mipLevel, UINT32 face, bool discardWholeBuffer)
  180. {
  181. if (mProperties.getMultisampleCount() > 1)
  182. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  183. getBuffer(face, mipLevel)->upload(src, src.getExtents());
  184. }
  185. void GLTextureCore::copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel, const SPtr<TextureCore>& target)
  186. {
  187. size_t numMips = std::min(mProperties.getNumMipmaps(), target->getProperties().getNumMipmaps());
  188. GLTextureCore* destTex = static_cast<GLTextureCore*>(target.get());
  189. GLTextureBuffer *src = static_cast<GLTextureBuffer*>(getBuffer(srcFace, srcMipLevel).get());
  190. destTex->getBuffer(destFace, destMipLevel)->blitFromTexture(src);
  191. }
  192. void GLTextureCore::createSurfaceList()
  193. {
  194. mSurfaceList.clear();
  195. for (UINT32 face = 0; face < mProperties.getNumFaces(); face++)
  196. {
  197. for (UINT32 mip = 0; mip <= mProperties.getNumMipmaps(); mip++)
  198. {
  199. GLPixelBuffer *buf = bs_new<GLTextureBuffer>(getGLTextureTarget(), mTextureID, face, mip,
  200. static_cast<GpuBufferUsage>(mProperties.getUsage()),
  201. mProperties.isHardwareGammaEnabled(), mProperties.getMultisampleCount());
  202. mSurfaceList.push_back(bs_shared_ptr<GLPixelBuffer>(buf));
  203. if(buf->getWidth() == 0 || buf->getHeight() == 0 || buf->getDepth() == 0)
  204. {
  205. BS_EXCEPT(RenderingAPIException,
  206. "Zero sized texture surface on texture face "
  207. + toString(face)
  208. + " mipmap "+toString(mip)
  209. + ". Probably, the GL driver refused to create the texture.");
  210. }
  211. }
  212. }
  213. }
  214. std::shared_ptr<GLPixelBuffer> GLTextureCore::getBuffer(UINT32 face, UINT32 mipmap)
  215. {
  216. THROW_IF_NOT_CORE_THREAD;
  217. if(face >= mProperties.getNumFaces())
  218. BS_EXCEPT(InvalidParametersException, "Face index out of range");
  219. if (mipmap > mProperties.getNumMipmaps())
  220. BS_EXCEPT(InvalidParametersException, "Mipmap index out of range");
  221. unsigned int idx = face * (mProperties.getNumMipmaps() + 1) + mipmap;
  222. assert(idx < mSurfaceList.size());
  223. return mSurfaceList[idx];
  224. }
  225. }