Image.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**
  2. * Copyright (c) 2006-2020 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "Image.h"
  21. #include "graphics/Graphics.h"
  22. #include "common/int.h"
  23. // STD
  24. #include <algorithm> // for min/max
  25. namespace love
  26. {
  27. namespace graphics
  28. {
  29. namespace opengl
  30. {
  31. Image::Image(TextureType textype, PixelFormat format, int width, int height, int slices, const Settings &settings)
  32. : love::graphics::Image(textype, format, width, height, slices, settings)
  33. , slices(textype)
  34. , texture(0)
  35. {
  36. loadVolatile();
  37. }
  38. Image::Image(const Slices &slices, const Settings &settings)
  39. : love::graphics::Image(slices, settings)
  40. , slices(slices)
  41. , texture(0)
  42. {
  43. loadVolatile();
  44. }
  45. Image::~Image()
  46. {
  47. unloadVolatile();
  48. }
  49. void Image::generateMipmaps()
  50. {
  51. if (getMipmapCount() > 1 && !isCompressed() &&
  52. (GLAD_ES_VERSION_2_0 || GLAD_VERSION_3_0 || GLAD_ARB_framebuffer_object || GLAD_EXT_framebuffer_object))
  53. {
  54. gl.bindTextureToUnit(this, 0, false);
  55. GLenum gltextype = OpenGL::getGLTextureType(texType);
  56. if (gl.bugs.generateMipmapsRequiresTexture2DEnable)
  57. glEnable(gltextype);
  58. glGenerateMipmap(gltextype);
  59. }
  60. }
  61. void Image::loadDefaultTexture()
  62. {
  63. usingDefaultTexture = true;
  64. gl.bindTextureToUnit(this, 0, false);
  65. setSamplerState(samplerState);
  66. bool isSRGB = false;
  67. gl.rawTexStorage(texType, 1, PIXELFORMAT_RGBA8_UNORM, isSRGB, 2, 2, 1);
  68. // A nice friendly checkerboard to signify invalid textures...
  69. GLubyte px[] = {0xFF,0xFF,0xFF,0xFF, 0xFF,0xA0,0xA0,0xFF,
  70. 0xFF,0xA0,0xA0,0xFF, 0xFF,0xFF,0xFF,0xFF};
  71. int slices = texType == TEXTURE_CUBE ? 6 : 1;
  72. Rect rect = {0, 0, 2, 2};
  73. for (int slice = 0; slice < slices; slice++)
  74. uploadByteData(PIXELFORMAT_RGBA8_UNORM, px, sizeof(px), 0, slice, rect, nullptr);
  75. }
  76. void Image::loadData()
  77. {
  78. int mipcount = getMipmapCount();
  79. int slicecount = 1;
  80. if (texType == TEXTURE_VOLUME)
  81. slicecount = getDepth();
  82. else if (texType == TEXTURE_2D_ARRAY)
  83. slicecount = getLayerCount();
  84. else if (texType == TEXTURE_CUBE)
  85. slicecount = 6;
  86. if (!isCompressed())
  87. gl.rawTexStorage(texType, mipcount, format, sRGB, pixelWidth, pixelHeight, texType == TEXTURE_VOLUME ? depth : layers);
  88. int w = pixelWidth;
  89. int h = pixelHeight;
  90. int d = depth;
  91. OpenGL::TextureFormat fmt = gl.convertPixelFormat(format, false, sRGB);
  92. for (int mip = 0; mip < mipcount; mip++)
  93. {
  94. if (isCompressed() && (texType == TEXTURE_2D_ARRAY || texType == TEXTURE_VOLUME))
  95. {
  96. size_t mipsize = 0;
  97. if (texType == TEXTURE_2D_ARRAY || texType == TEXTURE_VOLUME)
  98. {
  99. for (int slice = 0; slice < slices.getSliceCount(mip); slice++)
  100. {
  101. auto id = slices.get(slice, mip);
  102. if (id != nullptr)
  103. mipsize += id->getSize();
  104. }
  105. }
  106. if (mipsize > 0)
  107. {
  108. GLenum gltarget = OpenGL::getGLTextureType(texType);
  109. glCompressedTexImage3D(gltarget, mip, fmt.internalformat, w, h, d, 0, mipsize, nullptr);
  110. }
  111. }
  112. for (int slice = 0; slice < slicecount; slice++)
  113. {
  114. love::image::ImageDataBase *id = slices.get(slice, mip);
  115. if (id != nullptr)
  116. uploadImageData(id, mip, slice, 0, 0);
  117. }
  118. w = std::max(w / 2, 1);
  119. h = std::max(h / 2, 1);
  120. if (texType == TEXTURE_VOLUME)
  121. d = std::max(d / 2, 1);
  122. }
  123. if (getMipmapCount() > 1 && slices.getMipmapCount() <= 1)
  124. generateMipmaps();
  125. }
  126. void Image::uploadByteData(PixelFormat pixelformat, const void *data, size_t size, int level, int slice, const Rect &r, love::image::ImageDataBase *imgd)
  127. {
  128. love::image::ImageDataBase *oldd = slices.get(slice, level);
  129. // We can only replace the internal Data (used when reloading due to setMode)
  130. // if the dimensions match.
  131. if (imgd != nullptr && oldd != nullptr && oldd->getWidth() == imgd->getWidth()
  132. && oldd->getHeight() == imgd->getHeight())
  133. {
  134. slices.set(slice, level, imgd);
  135. }
  136. OpenGL::TempDebugGroup debuggroup("Image data upload");
  137. gl.bindTextureToUnit(this, 0, false);
  138. OpenGL::TextureFormat fmt = OpenGL::convertPixelFormat(pixelformat, false, sRGB);
  139. GLenum gltarget = OpenGL::getGLTextureType(texType);
  140. if (texType == TEXTURE_CUBE)
  141. gltarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + slice;
  142. if (isPixelFormatCompressed(pixelformat))
  143. {
  144. if (r.x != 0 || r.y != 0)
  145. throw love::Exception("x and y parameters must be 0 for compressed images.");
  146. if (texType == TEXTURE_2D || texType == TEXTURE_CUBE)
  147. glCompressedTexImage2D(gltarget, level, fmt.internalformat, r.w, r.h, 0, size, data);
  148. else if (texType == TEXTURE_2D_ARRAY || texType == TEXTURE_VOLUME)
  149. glCompressedTexSubImage3D(gltarget, level, 0, 0, slice, r.w, r.h, 1, fmt.internalformat, size, data);
  150. }
  151. else
  152. {
  153. if (texType == TEXTURE_2D || texType == TEXTURE_CUBE)
  154. glTexSubImage2D(gltarget, level, r.x, r.y, r.w, r.h, fmt.externalformat, fmt.type, data);
  155. else if (texType == TEXTURE_2D_ARRAY || texType == TEXTURE_VOLUME)
  156. glTexSubImage3D(gltarget, level, r.x, r.y, slice, r.w, r.h, 1, fmt.externalformat, fmt.type, data);
  157. }
  158. }
  159. bool Image::loadVolatile()
  160. {
  161. if (texture != 0)
  162. return true;
  163. OpenGL::TempDebugGroup debuggroup("Image load");
  164. // NPOT textures don't support mipmapping without full NPOT support.
  165. if ((GLAD_ES_VERSION_2_0 && !(GLAD_ES_VERSION_3_0 || GLAD_OES_texture_npot))
  166. && (pixelWidth != nextP2(pixelWidth) || pixelHeight != nextP2(pixelHeight)))
  167. {
  168. mipmapCount = 1;
  169. samplerState.mipmapFilter = SamplerState::MIPMAP_FILTER_NONE;
  170. }
  171. glGenTextures(1, &texture);
  172. gl.bindTextureToUnit(this, 0, false);
  173. // Use a default texture if the size is too big for the system.
  174. if (!validateDimensions(false))
  175. {
  176. loadDefaultTexture();
  177. return true;
  178. }
  179. setSamplerState(samplerState);
  180. while (glGetError() != GL_NO_ERROR); // Clear errors.
  181. try
  182. {
  183. loadData();
  184. GLenum glerr = glGetError();
  185. if (glerr != GL_NO_ERROR)
  186. throw love::Exception("Cannot create image (OpenGL error: %s)", OpenGL::errorString(glerr));
  187. }
  188. catch (love::Exception &)
  189. {
  190. gl.deleteTexture(texture);
  191. texture = 0;
  192. throw;
  193. }
  194. int64 memsize = 0;
  195. for (int slice = 0; slice < slices.getSliceCount(0); slice++)
  196. memsize += slices.get(slice, 0)->getSize();
  197. if (getMipmapCount() > 1)
  198. memsize *= 1.33334;
  199. setGraphicsMemorySize(memsize);
  200. usingDefaultTexture = false;
  201. return true;
  202. }
  203. void Image::unloadVolatile()
  204. {
  205. if (texture == 0)
  206. return;
  207. gl.deleteTexture(texture);
  208. texture = 0;
  209. setGraphicsMemorySize(0);
  210. }
  211. ptrdiff_t Image::getHandle() const
  212. {
  213. return texture;
  214. }
  215. void Image::setSamplerState(const SamplerState &s)
  216. {
  217. Texture::setSamplerState(s);
  218. if (!OpenGL::hasTextureFilteringSupport(getPixelFormat()))
  219. {
  220. samplerState.magFilter = samplerState.minFilter = SamplerState::FILTER_NEAREST;
  221. if (samplerState.mipmapFilter == SamplerState::MIPMAP_FILTER_LINEAR)
  222. samplerState.mipmapFilter = SamplerState::MIPMAP_FILTER_NEAREST;
  223. }
  224. // We don't want filtering or (attempted) mipmaps on the default texture.
  225. if (usingDefaultTexture)
  226. {
  227. samplerState.mipmapFilter = SamplerState::MIPMAP_FILTER_NONE;
  228. samplerState.minFilter = samplerState.magFilter = SamplerState::FILTER_NEAREST;
  229. }
  230. // If we only have limited NPOT support then the wrap mode must be CLAMP.
  231. if ((GLAD_ES_VERSION_2_0 && !(GLAD_ES_VERSION_3_0 || GLAD_OES_texture_npot))
  232. && (pixelWidth != nextP2(pixelWidth) || pixelHeight != nextP2(pixelHeight) || depth != nextP2(depth)))
  233. {
  234. samplerState.wrapU = samplerState.wrapV = samplerState.wrapW = SamplerState::WRAP_CLAMP;
  235. }
  236. gl.bindTextureToUnit(this, 0, false);
  237. gl.setSamplerState(texType, samplerState);
  238. }
  239. } // opengl
  240. } // graphics
  241. } // love