BsGLTexture.cpp 10.0 KB

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