BsGLTexture.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include "BsGLTexture.h"
  2. #include "BsGLSupport.h"
  3. #include "BsGLPixelFormat.h"
  4. #include "BsGLPixelBuffer.h"
  5. #include "BsException.h"
  6. #include "BsBitwise.h"
  7. #include "BsCoreThread.h"
  8. #include "BsTextureManager.h"
  9. #include "BsGLRenderTexture.h"
  10. #include "BsRenderStats.h"
  11. namespace BansheeEngine
  12. {
  13. GLTextureCore::GLTextureCore(GLSupport& support, TextureType textureType, UINT32 width, UINT32 height,
  14. UINT32 depth, UINT32 numMipmaps, PixelFormat format, int usage, bool hwGamma, UINT32 multisampleCount)
  15. : TextureCore(textureType, width, height, depth, numMipmaps, format, usage, hwGamma, multisampleCount),
  16. mTextureID(0), mGLSupport(support), mGLFormat(0)
  17. { }
  18. GLTextureCore::~GLTextureCore()
  19. {
  20. mSurfaceList.clear();
  21. glDeleteTextures(1, &mTextureID);
  22. clearBufferViews();
  23. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_Texture);
  24. }
  25. void GLTextureCore::initialize()
  26. {
  27. UINT32 width = mProperties.getWidth();
  28. UINT32 height = mProperties.getHeight();
  29. UINT32 depth = mProperties.getDepth();
  30. TextureType texType = mProperties.getTextureType();
  31. PixelFormat pixFormat = mProperties.getFormat();
  32. int usage = mProperties.getUsage();
  33. UINT32 numMips = mProperties.getNumMipmaps();
  34. // Check requested number of mipmaps
  35. UINT32 maxMips = PixelUtil::getMaxMipmaps(width, height, depth, mProperties.getFormat());
  36. if (numMips > maxMips)
  37. BS_EXCEPT(InvalidParametersException, "Invalid number of mipmaps. Maximum allowed is: " + toString(maxMips));
  38. if ((usage & TU_RENDERTARGET) != 0)
  39. {
  40. if (texType != TEX_TYPE_2D)
  41. BS_EXCEPT(NotImplementedException, "Only 2D render targets are supported at the moment");
  42. }
  43. if ((usage & TU_DEPTHSTENCIL) != 0)
  44. {
  45. if (texType != TEX_TYPE_2D)
  46. BS_EXCEPT(NotImplementedException, "Only 2D depth stencil targets are supported at the moment");
  47. if (!PixelUtil::isDepth(pixFormat))
  48. BS_EXCEPT(NotImplementedException, "Supplied format is not a depth stencil format. Format: " + toString(pixFormat));
  49. }
  50. // Generate texture handle
  51. glGenTextures(1, &mTextureID);
  52. // Set texture type
  53. glBindTexture(getGLTextureTarget(), mTextureID);
  54. // This needs to be set otherwise the texture doesn't get rendered
  55. glTexParameteri(getGLTextureTarget(), GL_TEXTURE_MAX_LEVEL, numMips);
  56. // Allocate internal buffer so that glTexSubImageXD can be used
  57. mGLFormat = GLPixelUtil::getClosestGLInternalFormat(pixFormat, mProperties.isHardwareGammaEnabled());
  58. if (PixelUtil::isCompressed(pixFormat))
  59. {
  60. if((usage & TU_RENDERTARGET) != 0)
  61. BS_EXCEPT(InvalidParametersException, "Cannot use a compressed format for a render target.");
  62. if ((usage & TU_DEPTHSTENCIL) != 0)
  63. BS_EXCEPT(InvalidParametersException, "Cannot use a compressed format for a depth stencil target.");
  64. }
  65. UINT32 sampleCount = mProperties.getMultisampleCount();
  66. if ((usage & TU_RENDERTARGET) != 0 && mProperties.getTextureType() == TEX_TYPE_2D && sampleCount > 1)
  67. {
  68. glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, sampleCount, mGLFormat, width, height, GL_FALSE);
  69. }
  70. else if ((usage & TU_DEPTHSTENCIL) != 0)
  71. {
  72. if (sampleCount > 1)
  73. {
  74. glTexImage2DMultisample(GL_TEXTURE_2D, sampleCount, mGLFormat,
  75. width, height, GL_FALSE);
  76. }
  77. else
  78. {
  79. GLenum depthStencilFormat = GLPixelUtil::getDepthStencilTypeFromFormat(pixFormat);
  80. glTexImage2D(GL_TEXTURE_2D, 0, mGLFormat,
  81. width, height, 0,
  82. GL_DEPTH_STENCIL, depthStencilFormat, nullptr);
  83. }
  84. }
  85. else
  86. {
  87. GLenum baseFormat = GLPixelUtil::getGLOriginFormat(pixFormat);
  88. GLenum baseDataType = GLPixelUtil::getGLOriginDataType(pixFormat);
  89. // Run through this process to pre-generate mipmap pyramid
  90. for (UINT32 mip = 0; mip <= numMips; mip++)
  91. {
  92. switch (texType)
  93. {
  94. case TEX_TYPE_1D:
  95. glTexImage1D(GL_TEXTURE_1D, mip, mGLFormat, width, 0,
  96. baseFormat, baseDataType, nullptr);
  97. break;
  98. case TEX_TYPE_2D:
  99. glTexImage2D(GL_TEXTURE_2D, mip, mGLFormat,
  100. width, height, 0, baseFormat, baseDataType, nullptr);
  101. break;
  102. case TEX_TYPE_3D:
  103. glTexImage3D(GL_TEXTURE_3D, mip, mGLFormat, width, height,
  104. depth, 0, baseFormat, baseDataType, nullptr);
  105. break;
  106. case TEX_TYPE_CUBE_MAP:
  107. for(UINT32 face = 0; face < 6; face++)
  108. {
  109. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, mip, mGLFormat,
  110. width, height, 0, baseFormat, baseDataType, nullptr);
  111. }
  112. break;
  113. };
  114. if(width > 1)
  115. width = width/2;
  116. if(height > 1)
  117. height = height/2;
  118. if(depth > 1)
  119. depth = depth/2;
  120. }
  121. }
  122. createSurfaceList();
  123. PixelBufferPtr buffer = getBuffer(0, 0);
  124. #if BS_DEBUG_MODE
  125. if(buffer != nullptr)
  126. {
  127. if(pixFormat != buffer->getFormat())
  128. {
  129. BS_EXCEPT(InternalErrorException, "Could not create a texture buffer with wanted format: " + toString(pixFormat));
  130. }
  131. }
  132. #endif
  133. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_Texture);
  134. TextureCore::initialize();
  135. }
  136. GLenum GLTextureCore::getGLTextureTarget() const
  137. {
  138. switch (mProperties.getTextureType())
  139. {
  140. case TEX_TYPE_1D:
  141. return GL_TEXTURE_1D;
  142. case TEX_TYPE_2D:
  143. if (mProperties.getMultisampleCount() > 1)
  144. return GL_TEXTURE_2D_MULTISAMPLE;
  145. else
  146. return GL_TEXTURE_2D;
  147. case TEX_TYPE_3D:
  148. return GL_TEXTURE_3D;
  149. case TEX_TYPE_CUBE_MAP:
  150. return GL_TEXTURE_CUBE_MAP;
  151. default:
  152. return 0;
  153. };
  154. }
  155. GLuint GLTextureCore::getGLID() const
  156. {
  157. THROW_IF_NOT_CORE_THREAD;
  158. return mTextureID;
  159. }
  160. PixelData GLTextureCore::lockImpl(GpuLockOptions options, UINT32 mipLevel, UINT32 face)
  161. {
  162. if (mProperties.getMultisampleCount() > 0)
  163. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  164. if(mLockedBuffer != nullptr)
  165. BS_EXCEPT(InternalErrorException, "Trying to lock a buffer that's already locked.");
  166. UINT32 mipWidth = mProperties.getWidth() >> mipLevel;
  167. UINT32 mipHeight = mProperties.getHeight() >> mipLevel;
  168. UINT32 mipDepth = mProperties.getDepth() >> mipLevel;
  169. PixelData lockedArea(mipWidth, mipHeight, mipDepth, mProperties.getFormat());
  170. mLockedBuffer = getBuffer(face, mipLevel);
  171. lockedArea.setExternalBuffer((UINT8*)mLockedBuffer->lock(options));
  172. return lockedArea;
  173. }
  174. void GLTextureCore::unlockImpl()
  175. {
  176. if(mLockedBuffer == nullptr)
  177. BS_EXCEPT(InternalErrorException, "Trying to unlock a buffer that's not locked.");
  178. mLockedBuffer->unlock();
  179. mLockedBuffer = nullptr;
  180. }
  181. void GLTextureCore::readData(PixelData& dest, UINT32 mipLevel, UINT32 face)
  182. {
  183. if (mProperties.getMultisampleCount() > 0)
  184. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  185. getBuffer(face, mipLevel)->download(dest);
  186. }
  187. void GLTextureCore::writeData(const PixelData& src, UINT32 mipLevel, UINT32 face, bool discardWholeBuffer)
  188. {
  189. if (mProperties.getMultisampleCount() > 0)
  190. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  191. getBuffer(face, mipLevel)->upload(src, src.getExtents());
  192. }
  193. void GLTextureCore::copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel, const SPtr<TextureCore>& target)
  194. {
  195. size_t numMips = std::min(mProperties.getNumMipmaps(), target->getProperties().getNumMipmaps());
  196. GLTextureCore* destTex = static_cast<GLTextureCore*>(target.get());
  197. GLTextureBuffer *src = static_cast<GLTextureBuffer*>(getBuffer(srcFace, srcMipLevel).get());
  198. destTex->getBuffer(destFace, destMipLevel)->blitFromTexture(src);
  199. }
  200. void GLTextureCore::createSurfaceList()
  201. {
  202. mSurfaceList.clear();
  203. for (UINT32 face = 0; face < mProperties.getNumFaces(); face++)
  204. {
  205. for (UINT32 mip = 0; mip <= mProperties.getNumMipmaps(); mip++)
  206. {
  207. GLPixelBuffer *buf = bs_new<GLTextureBuffer, PoolAlloc>(getGLTextureTarget(), mTextureID, face, mip,
  208. static_cast<GpuBufferUsage>(mProperties.getUsage()),
  209. mProperties.isHardwareGammaEnabled(), mProperties.getMultisampleCount());
  210. mSurfaceList.push_back(bs_shared_ptr<GLPixelBuffer, PoolAlloc>(buf));
  211. if(buf->getWidth() == 0 || buf->getHeight() == 0 || buf->getDepth() == 0)
  212. {
  213. BS_EXCEPT(RenderingAPIException,
  214. "Zero sized texture surface on texture face "
  215. + toString(face)
  216. + " mipmap "+toString(mip)
  217. + ". Probably, the GL driver refused to create the texture.");
  218. }
  219. }
  220. }
  221. }
  222. std::shared_ptr<GLPixelBuffer> GLTextureCore::getBuffer(UINT32 face, UINT32 mipmap)
  223. {
  224. THROW_IF_NOT_CORE_THREAD;
  225. if(face >= mProperties.getNumFaces())
  226. BS_EXCEPT(InvalidParametersException, "Face index out of range");
  227. if (mipmap > mProperties.getNumMipmaps())
  228. BS_EXCEPT(InvalidParametersException, "Mipmap index out of range");
  229. unsigned int idx = face * (mProperties.getNumMipmaps() + 1) + mipmap;
  230. assert(idx < mSurfaceList.size());
  231. return mSurfaceList[idx];
  232. }
  233. }