BsGLTexture.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. #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)
  57. {
  58. LOGERR("Only 2D depth stencil targets are supported at the moment");
  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 if((usage & TU_DEPTHSTENCIL) != 0 && mProperties.getTextureType() == TEX_TYPE_2D)
  81. {
  82. if (numFaces <= 1)
  83. glTexStorage2D(GL_TEXTURE_2D, numMips, mGLFormat, width, height);
  84. else
  85. glTexStorage3D(GL_TEXTURE_2D_ARRAY, numMips, mGLFormat, width, height, numFaces);
  86. }
  87. else
  88. {
  89. switch (texType)
  90. {
  91. case TEX_TYPE_1D:
  92. {
  93. if (numFaces <= 1)
  94. glTexStorage1D(GL_TEXTURE_1D, numMips, mGLFormat, width);
  95. else
  96. glTexStorage2D(GL_TEXTURE_1D_ARRAY, numMips, mGLFormat, width, numFaces);
  97. }
  98. break;
  99. case TEX_TYPE_2D:
  100. {
  101. if (numFaces <= 1)
  102. glTexStorage2D(GL_TEXTURE_2D, numMips, mGLFormat, width, height);
  103. else
  104. glTexStorage3D(GL_TEXTURE_2D_ARRAY, numMips, mGLFormat, width, height, numFaces);
  105. }
  106. break;
  107. case TEX_TYPE_3D:
  108. glTexStorage3D(GL_TEXTURE_3D, numMips, mGLFormat, width, height, depth);
  109. break;
  110. case TEX_TYPE_CUBE_MAP:
  111. {
  112. if (numFaces <= 6)
  113. glTexStorage2D(GL_TEXTURE_CUBE_MAP, numMips, mGLFormat, width, height);
  114. else
  115. glTexStorage3D(GL_TEXTURE_CUBE_MAP_ARRAY, numMips, mGLFormat, width, height, numFaces);
  116. }
  117. break;
  118. }
  119. }
  120. createSurfaceList();
  121. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_Texture);
  122. Texture::initialize();
  123. }
  124. GLenum GLTexture::getGLTextureTarget() const
  125. {
  126. switch (mProperties.getTextureType())
  127. {
  128. case TEX_TYPE_1D:
  129. if(mProperties.getNumFaces() <= 1)
  130. return GL_TEXTURE_1D;
  131. else
  132. return GL_TEXTURE_1D_ARRAY;
  133. case TEX_TYPE_2D:
  134. if (mProperties.getNumSamples() > 1)
  135. {
  136. if (mProperties.getNumFaces() <= 1)
  137. return GL_TEXTURE_2D_MULTISAMPLE;
  138. else
  139. return GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
  140. }
  141. else
  142. {
  143. if (mProperties.getNumFaces() <= 1)
  144. return GL_TEXTURE_2D;
  145. else
  146. return GL_TEXTURE_2D_ARRAY;
  147. }
  148. case TEX_TYPE_3D:
  149. return GL_TEXTURE_3D;
  150. case TEX_TYPE_CUBE_MAP:
  151. if (mProperties.getNumFaces() <= 6)
  152. return GL_TEXTURE_CUBE_MAP;
  153. else
  154. return GL_TEXTURE_CUBE_MAP_ARRAY;
  155. default:
  156. return 0;
  157. };
  158. }
  159. GLuint GLTexture::getGLID() const
  160. {
  161. THROW_IF_NOT_CORE_THREAD;
  162. return mTextureID;
  163. }
  164. PixelData GLTexture::lockImpl(GpuLockOptions options, UINT32 mipLevel, UINT32 face, UINT32 deviceIdx,
  165. UINT32 queueIdx)
  166. {
  167. if (mProperties.getNumSamples() > 1)
  168. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  169. if(mLockedBuffer != nullptr)
  170. BS_EXCEPT(InternalErrorException, "Trying to lock a buffer that's already locked.");
  171. UINT32 mipWidth = std::max(1u, mProperties.getWidth() >> mipLevel);
  172. UINT32 mipHeight = std::max(1u, mProperties.getHeight() >> mipLevel);
  173. UINT32 mipDepth = std::max(1u, mProperties.getDepth() >> mipLevel);
  174. PixelData lockedArea(mipWidth, mipHeight, mipDepth, mProperties.getFormat());
  175. mLockedBuffer = getBuffer(face, mipLevel);
  176. lockedArea.setExternalBuffer((UINT8*)mLockedBuffer->lock(options));
  177. return lockedArea;
  178. }
  179. void GLTexture::unlockImpl()
  180. {
  181. if (mLockedBuffer == nullptr)
  182. {
  183. LOGERR("Trying to unlock a buffer that's not locked.");
  184. return;
  185. }
  186. mLockedBuffer->unlock();
  187. mLockedBuffer = nullptr;
  188. }
  189. void GLTexture::readDataImpl(PixelData& dest, UINT32 mipLevel, UINT32 face, UINT32 deviceIdx, UINT32 queueIdx)
  190. {
  191. if (mProperties.getNumSamples() > 1)
  192. {
  193. LOGERR("Multisampled textures cannot be accessed from the CPU directly.");
  194. return;
  195. }
  196. if(dest.getFormat() != mInternalFormat)
  197. {
  198. PixelData temp(dest.getExtents(), mInternalFormat);
  199. temp.allocateInternalBuffer();
  200. getBuffer(face, mipLevel)->download(temp);
  201. PixelUtil::bulkPixelConversion(temp, dest);
  202. }
  203. else
  204. getBuffer(face, mipLevel)->download(dest);
  205. }
  206. void GLTexture::writeDataImpl(const PixelData& src, UINT32 mipLevel, UINT32 face, bool discardWholeBuffer,
  207. UINT32 queueIdx)
  208. {
  209. if (mProperties.getNumSamples() > 1)
  210. {
  211. LOGERR("Multisampled textures cannot be accessed from the CPU directly.");
  212. return;
  213. }
  214. if (src.getFormat() != mInternalFormat)
  215. {
  216. PixelData temp(src.getExtents(), mInternalFormat);
  217. temp.allocateInternalBuffer();
  218. PixelUtil::bulkPixelConversion(src, temp);
  219. getBuffer(face, mipLevel)->upload(temp, temp.getExtents());
  220. }
  221. else
  222. getBuffer(face, mipLevel)->upload(src, src.getExtents());
  223. }
  224. void GLTexture::copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel,
  225. const SPtr<Texture>& target, const SPtr<CommandBuffer>& commandBuffer)
  226. {
  227. auto executeRef = [this](UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel,
  228. const SPtr<Texture>& target)
  229. {
  230. GLTexture* destTex = static_cast<GLTexture*>(target.get());
  231. GLTextureBuffer *src = static_cast<GLTextureBuffer*>(getBuffer(srcFace, srcMipLevel).get());
  232. destTex->getBuffer(destFace, destMipLevel)->blitFromTexture(src);
  233. };
  234. if (commandBuffer == nullptr)
  235. executeRef(srcFace, srcMipLevel, destFace, destMipLevel, target);
  236. else
  237. {
  238. auto execute = [=]() { executeRef(srcFace, srcMipLevel, destFace, destMipLevel, target); };
  239. SPtr<GLCommandBuffer> cb = std::static_pointer_cast<GLCommandBuffer>(commandBuffer);
  240. cb->queueCommand(execute);
  241. }
  242. }
  243. void GLTexture::createSurfaceList()
  244. {
  245. mSurfaceList.clear();
  246. for (UINT32 face = 0; face < mProperties.getNumFaces(); face++)
  247. {
  248. for (UINT32 mip = 0; mip <= mProperties.getNumMipmaps(); mip++)
  249. {
  250. GLPixelBuffer *buf = bs_new<GLTextureBuffer>(getGLTextureTarget(), mTextureID, face, mip,
  251. static_cast<GpuBufferUsage>(mProperties.getUsage()), mInternalFormat, mProperties.getNumSamples());
  252. mSurfaceList.push_back(bs_shared_ptr<GLPixelBuffer>(buf));
  253. if(buf->getWidth() == 0 || buf->getHeight() == 0 || buf->getDepth() == 0)
  254. {
  255. BS_EXCEPT(RenderingAPIException,
  256. "Zero sized texture surface on texture face "
  257. + toString(face)
  258. + " mipmap "+toString(mip)
  259. + ". Probably, the GL driver refused to create the texture.");
  260. }
  261. }
  262. }
  263. }
  264. SPtr<GLPixelBuffer> GLTexture::getBuffer(UINT32 face, UINT32 mipmap)
  265. {
  266. THROW_IF_NOT_CORE_THREAD;
  267. if(face >= mProperties.getNumFaces())
  268. BS_EXCEPT(InvalidParametersException, "Face index out of range");
  269. if (mipmap > mProperties.getNumMipmaps())
  270. BS_EXCEPT(InvalidParametersException, "Mipmap index out of range");
  271. unsigned int idx = face * (mProperties.getNumMipmaps() + 1) + mipmap;
  272. assert(idx < mSurfaceList.size());
  273. return mSurfaceList[idx];
  274. }
  275. SPtr<TextureView> GLTexture::createView(const TEXTURE_VIEW_DESC& desc)
  276. {
  277. return bs_shared_ptr<GLTextureView>(new (bs_alloc<GLTextureView>()) GLTextureView(this, desc));
  278. }
  279. }}