BsGLTexture.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. PixelBufferPtr buffer = getBuffer(0, 0);
  126. #if BS_DEBUG_MODE
  127. if(buffer != nullptr)
  128. {
  129. if(pixFormat != buffer->getFormat())
  130. {
  131. BS_EXCEPT(InternalErrorException, "Could not create a texture buffer with wanted format: " + toString(pixFormat));
  132. }
  133. }
  134. #endif
  135. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_Texture);
  136. TextureCore::initialize();
  137. }
  138. GLenum GLTextureCore::getGLTextureTarget() const
  139. {
  140. switch (mProperties.getTextureType())
  141. {
  142. case TEX_TYPE_1D:
  143. return GL_TEXTURE_1D;
  144. case TEX_TYPE_2D:
  145. if (mProperties.getMultisampleCount() > 1)
  146. return GL_TEXTURE_2D_MULTISAMPLE;
  147. else
  148. return GL_TEXTURE_2D;
  149. case TEX_TYPE_3D:
  150. return GL_TEXTURE_3D;
  151. case TEX_TYPE_CUBE_MAP:
  152. return GL_TEXTURE_CUBE_MAP;
  153. default:
  154. return 0;
  155. };
  156. }
  157. GLuint GLTextureCore::getGLID() const
  158. {
  159. THROW_IF_NOT_CORE_THREAD;
  160. return mTextureID;
  161. }
  162. PixelData GLTextureCore::lockImpl(GpuLockOptions options, UINT32 mipLevel, UINT32 face)
  163. {
  164. if (mProperties.getMultisampleCount() > 1)
  165. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  166. if(mLockedBuffer != nullptr)
  167. BS_EXCEPT(InternalErrorException, "Trying to lock a buffer that's already locked.");
  168. UINT32 mipWidth = std::max(1u, mProperties.getWidth() >> mipLevel);
  169. UINT32 mipHeight = std::max(1u, mProperties.getHeight() >> mipLevel);
  170. UINT32 mipDepth = std::max(1u, mProperties.getDepth() >> mipLevel);
  171. PixelData lockedArea(mipWidth, mipHeight, mipDepth, mProperties.getFormat());
  172. mLockedBuffer = getBuffer(face, mipLevel);
  173. lockedArea.setExternalBuffer((UINT8*)mLockedBuffer->lock(options));
  174. return lockedArea;
  175. }
  176. void GLTextureCore::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 GLTextureCore::readData(PixelData& dest, UINT32 mipLevel, UINT32 face)
  184. {
  185. if (mProperties.getMultisampleCount() > 1)
  186. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  187. getBuffer(face, mipLevel)->download(dest);
  188. }
  189. void GLTextureCore::writeData(const PixelData& src, UINT32 mipLevel, UINT32 face, bool discardWholeBuffer)
  190. {
  191. if (mProperties.getMultisampleCount() > 1)
  192. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  193. getBuffer(face, mipLevel)->upload(src, src.getExtents());
  194. }
  195. void GLTextureCore::copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel, const SPtr<TextureCore>& target)
  196. {
  197. size_t numMips = std::min(mProperties.getNumMipmaps(), target->getProperties().getNumMipmaps());
  198. GLTextureCore* destTex = static_cast<GLTextureCore*>(target.get());
  199. GLTextureBuffer *src = static_cast<GLTextureBuffer*>(getBuffer(srcFace, srcMipLevel).get());
  200. destTex->getBuffer(destFace, destMipLevel)->blitFromTexture(src);
  201. }
  202. void GLTextureCore::createSurfaceList()
  203. {
  204. mSurfaceList.clear();
  205. for (UINT32 face = 0; face < mProperties.getNumFaces(); face++)
  206. {
  207. for (UINT32 mip = 0; mip <= mProperties.getNumMipmaps(); mip++)
  208. {
  209. GLPixelBuffer *buf = bs_new<GLTextureBuffer>(getGLTextureTarget(), mTextureID, face, mip,
  210. static_cast<GpuBufferUsage>(mProperties.getUsage()),
  211. mProperties.isHardwareGammaEnabled(), mProperties.getMultisampleCount());
  212. mSurfaceList.push_back(bs_shared_ptr<GLPixelBuffer>(buf));
  213. if(buf->getWidth() == 0 || buf->getHeight() == 0 || buf->getDepth() == 0)
  214. {
  215. BS_EXCEPT(RenderingAPIException,
  216. "Zero sized texture surface on texture face "
  217. + toString(face)
  218. + " mipmap "+toString(mip)
  219. + ". Probably, the GL driver refused to create the texture.");
  220. }
  221. }
  222. }
  223. }
  224. std::shared_ptr<GLPixelBuffer> GLTextureCore::getBuffer(UINT32 face, UINT32 mipmap)
  225. {
  226. THROW_IF_NOT_CORE_THREAD;
  227. if(face >= mProperties.getNumFaces())
  228. BS_EXCEPT(InvalidParametersException, "Face index out of range");
  229. if (mipmap > mProperties.getNumMipmaps())
  230. BS_EXCEPT(InvalidParametersException, "Mipmap index out of range");
  231. unsigned int idx = face * (mProperties.getNumMipmaps() + 1) + mipmap;
  232. assert(idx < mSurfaceList.size());
  233. return mSurfaceList[idx];
  234. }
  235. }