2
0

BsGLTexture.cpp 9.6 KB

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