BsGLTexture.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "BsGLTextureView.h"
  13. #include "BsRenderStats.h"
  14. namespace bs { namespace ct
  15. {
  16. GLTexture::GLTexture(GLSupport& support, const TEXTURE_DESC& desc, const SPtr<PixelData>& initialData,
  17. GpuDeviceFlags deviceMask)
  18. : Texture(desc, initialData, deviceMask),
  19. mTextureID(0), mGLFormat(0), mInternalFormat(PF_UNKNOWN), mGLSupport(support)
  20. {
  21. assert((deviceMask == GDF_DEFAULT || deviceMask == GDF_PRIMARY) && "Multiple GPUs not supported natively on OpenGL.");
  22. }
  23. GLTexture::~GLTexture()
  24. {
  25. mSurfaceList.clear();
  26. glDeleteTextures(1, &mTextureID);
  27. clearBufferViews();
  28. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_Texture);
  29. }
  30. void GLTexture::initialize()
  31. {
  32. UINT32 width = mProperties.getWidth();
  33. UINT32 height = mProperties.getHeight();
  34. UINT32 depth = mProperties.getDepth();
  35. TextureType texType = mProperties.getTextureType();
  36. int usage = mProperties.getUsage();
  37. UINT32 numMips = mProperties.getNumMipmaps();
  38. UINT32 numFaces = mProperties.getNumFaces();
  39. PixelFormat pixFormat = mProperties.getFormat();
  40. mInternalFormat = GLPixelUtil::getClosestSupportedPF(pixFormat, texType, usage);
  41. if (pixFormat != mInternalFormat)
  42. {
  43. LOGWRN(StringUtil::format("Provided pixel format is not supported by the driver: {0}. Falling back on: {1}.",
  44. pixFormat, mInternalFormat));
  45. }
  46. // Check requested number of mipmaps
  47. UINT32 maxMips = PixelUtil::getMaxMipmaps(width, height, depth, mProperties.getFormat());
  48. if (numMips > maxMips)
  49. {
  50. LOGERR("Invalid number of mipmaps. Maximum allowed is: " + toString(maxMips));
  51. numMips = maxMips;
  52. }
  53. if ((usage & TU_DEPTHSTENCIL) != 0)
  54. {
  55. if (texType != TEX_TYPE_2D)
  56. {
  57. LOGERR("Only 2D depth stencil targets are supported at the moment");
  58. usage &= ~TU_DEPTHSTENCIL;
  59. }
  60. }
  61. // Include the base mip level
  62. numMips += 1;
  63. // Generate texture handle
  64. glGenTextures(1, &mTextureID);
  65. // Set texture type
  66. glBindTexture(getGLTextureTarget(), mTextureID);
  67. // This needs to be set otherwise the texture doesn't get rendered
  68. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_MAX_LEVEL, numMips - 1);
  69. // Allocate internal buffer so that glTexSubImageXD can be used
  70. mGLFormat = GLPixelUtil::getGLInternalFormat(mInternalFormat, mProperties.isHardwareGammaEnabled());
  71. UINT32 sampleCount = mProperties.getNumSamples();
  72. if((usage & (TU_RENDERTARGET | TU_DEPTHSTENCIL)) != 0 && mProperties.getTextureType() == TEX_TYPE_2D && sampleCount > 1)
  73. {
  74. if (numFaces <= 1)
  75. glTexStorage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, sampleCount, mGLFormat, width, height, GL_FALSE);
  76. else
  77. glTexStorage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, sampleCount, mGLFormat, width, height, numFaces, GL_FALSE);
  78. }
  79. else if((usage & TU_DEPTHSTENCIL) != 0 && mProperties.getTextureType() == TEX_TYPE_2D)
  80. {
  81. if (numFaces <= 1)
  82. glTexStorage2D(GL_TEXTURE_2D, numMips, mGLFormat, width, height);
  83. else
  84. glTexStorage3D(GL_TEXTURE_2D_ARRAY, numMips, mGLFormat, width, height, numFaces);
  85. }
  86. else
  87. {
  88. switch (texType)
  89. {
  90. case TEX_TYPE_1D:
  91. {
  92. if (numFaces <= 1)
  93. glTexStorage1D(GL_TEXTURE_1D, numMips, mGLFormat, width);
  94. else
  95. glTexStorage2D(GL_TEXTURE_1D_ARRAY, numMips, mGLFormat, width, numFaces);
  96. }
  97. break;
  98. case TEX_TYPE_2D:
  99. {
  100. if (numFaces <= 1)
  101. glTexStorage2D(GL_TEXTURE_2D, numMips, mGLFormat, width, height);
  102. else
  103. glTexStorage3D(GL_TEXTURE_2D_ARRAY, numMips, mGLFormat, width, height, numFaces);
  104. }
  105. break;
  106. case TEX_TYPE_3D:
  107. glTexStorage3D(GL_TEXTURE_3D, numMips, mGLFormat, width, height, depth);
  108. break;
  109. case TEX_TYPE_CUBE_MAP:
  110. {
  111. if (numFaces <= 6)
  112. glTexStorage2D(GL_TEXTURE_CUBE_MAP, numMips, mGLFormat, width, height);
  113. else
  114. glTexStorage3D(GL_TEXTURE_CUBE_MAP_ARRAY, numMips, mGLFormat, width, height, numFaces);
  115. }
  116. break;
  117. }
  118. }
  119. createSurfaceList();
  120. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_Texture);
  121. Texture::initialize();
  122. }
  123. GLenum GLTexture::getGLTextureTarget() const
  124. {
  125. switch (mProperties.getTextureType())
  126. {
  127. case TEX_TYPE_1D:
  128. if(mProperties.getNumFaces() <= 1)
  129. return GL_TEXTURE_1D;
  130. else
  131. return GL_TEXTURE_1D_ARRAY;
  132. case TEX_TYPE_2D:
  133. if (mProperties.getNumSamples() > 1)
  134. {
  135. if (mProperties.getNumFaces() <= 1)
  136. return GL_TEXTURE_2D_MULTISAMPLE;
  137. else
  138. return GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
  139. }
  140. else
  141. {
  142. if (mProperties.getNumFaces() <= 1)
  143. return GL_TEXTURE_2D;
  144. else
  145. return GL_TEXTURE_2D_ARRAY;
  146. }
  147. case TEX_TYPE_3D:
  148. return GL_TEXTURE_3D;
  149. case TEX_TYPE_CUBE_MAP:
  150. if (mProperties.getNumFaces() <= 6)
  151. return GL_TEXTURE_CUBE_MAP;
  152. else
  153. return GL_TEXTURE_CUBE_MAP_ARRAY;
  154. default:
  155. return 0;
  156. };
  157. }
  158. GLuint GLTexture::getGLID() const
  159. {
  160. THROW_IF_NOT_CORE_THREAD;
  161. return mTextureID;
  162. }
  163. PixelData GLTexture::lockImpl(GpuLockOptions options, UINT32 mipLevel, UINT32 face, UINT32 deviceIdx,
  164. UINT32 queueIdx)
  165. {
  166. if (mProperties.getNumSamples() > 1)
  167. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  168. if(mLockedBuffer != nullptr)
  169. BS_EXCEPT(InternalErrorException, "Trying to lock a buffer that's already locked.");
  170. UINT32 mipWidth = std::max(1u, mProperties.getWidth() >> mipLevel);
  171. UINT32 mipHeight = std::max(1u, mProperties.getHeight() >> mipLevel);
  172. UINT32 mipDepth = std::max(1u, mProperties.getDepth() >> mipLevel);
  173. PixelData lockedArea(mipWidth, mipHeight, mipDepth, mProperties.getFormat());
  174. mLockedBuffer = getBuffer(face, mipLevel);
  175. lockedArea.setExternalBuffer((UINT8*)mLockedBuffer->lock(options));
  176. return lockedArea;
  177. }
  178. void GLTexture::unlockImpl()
  179. {
  180. if (mLockedBuffer == nullptr)
  181. {
  182. LOGERR("Trying to unlock a buffer that's not locked.");
  183. return;
  184. }
  185. mLockedBuffer->unlock();
  186. mLockedBuffer = nullptr;
  187. }
  188. void GLTexture::readDataImpl(PixelData& dest, UINT32 mipLevel, UINT32 face, UINT32 deviceIdx, UINT32 queueIdx)
  189. {
  190. if (mProperties.getNumSamples() > 1)
  191. {
  192. LOGERR("Multisampled textures cannot be accessed from the CPU directly.");
  193. return;
  194. }
  195. if(dest.getFormat() != mInternalFormat)
  196. {
  197. PixelData temp(dest.getExtents(), mInternalFormat);
  198. temp.allocateInternalBuffer();
  199. getBuffer(face, mipLevel)->download(temp);
  200. PixelUtil::bulkPixelConversion(temp, dest);
  201. }
  202. else
  203. getBuffer(face, mipLevel)->download(dest);
  204. }
  205. void GLTexture::writeDataImpl(const PixelData& src, UINT32 mipLevel, UINT32 face, bool discardWholeBuffer,
  206. UINT32 queueIdx)
  207. {
  208. if (mProperties.getNumSamples() > 1)
  209. {
  210. LOGERR("Multisampled textures cannot be accessed from the CPU directly.");
  211. return;
  212. }
  213. if (src.getFormat() != mInternalFormat)
  214. {
  215. PixelData temp(src.getExtents(), mInternalFormat);
  216. temp.allocateInternalBuffer();
  217. PixelUtil::bulkPixelConversion(src, temp);
  218. getBuffer(face, mipLevel)->upload(temp, temp.getExtents());
  219. }
  220. else
  221. getBuffer(face, mipLevel)->upload(src, src.getExtents());
  222. }
  223. void GLTexture::copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel,
  224. const SPtr<Texture>& target, UINT32 queueIdx)
  225. {
  226. GLTexture* destTex = static_cast<GLTexture*>(target.get());
  227. GLTextureBuffer *src = static_cast<GLTextureBuffer*>(getBuffer(srcFace, srcMipLevel).get());
  228. destTex->getBuffer(destFace, destMipLevel)->blitFromTexture(src);
  229. }
  230. void GLTexture::createSurfaceList()
  231. {
  232. mSurfaceList.clear();
  233. for (UINT32 face = 0; face < mProperties.getNumFaces(); face++)
  234. {
  235. for (UINT32 mip = 0; mip <= mProperties.getNumMipmaps(); mip++)
  236. {
  237. GLPixelBuffer *buf = bs_new<GLTextureBuffer>(getGLTextureTarget(), mTextureID, face, mip,
  238. static_cast<GpuBufferUsage>(mProperties.getUsage()), mInternalFormat, mProperties.getNumSamples());
  239. mSurfaceList.push_back(bs_shared_ptr<GLPixelBuffer>(buf));
  240. if(buf->getWidth() == 0 || buf->getHeight() == 0 || buf->getDepth() == 0)
  241. {
  242. BS_EXCEPT(RenderingAPIException,
  243. "Zero sized texture surface on texture face "
  244. + toString(face)
  245. + " mipmap "+toString(mip)
  246. + ". Probably, the GL driver refused to create the texture.");
  247. }
  248. }
  249. }
  250. }
  251. SPtr<GLPixelBuffer> GLTexture::getBuffer(UINT32 face, UINT32 mipmap)
  252. {
  253. THROW_IF_NOT_CORE_THREAD;
  254. if(face >= mProperties.getNumFaces())
  255. BS_EXCEPT(InvalidParametersException, "Face index out of range");
  256. if (mipmap > mProperties.getNumMipmaps())
  257. BS_EXCEPT(InvalidParametersException, "Mipmap index out of range");
  258. unsigned int idx = face * (mProperties.getNumMipmaps() + 1) + mipmap;
  259. assert(idx < mSurfaceList.size());
  260. return mSurfaceList[idx];
  261. }
  262. SPtr<TextureView> GLTexture::createView(const TEXTURE_VIEW_DESC& desc)
  263. {
  264. return bs_shared_ptr<GLTextureView>(new (bs_alloc<GLTextureView>()) GLTextureView(this, desc));
  265. }
  266. }}