Image.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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(const Settings &settings, const Slices *data)
  32. : love::graphics::Texture(settings, data)
  33. , slices(settings.type)
  34. , texture(0)
  35. {
  36. if (data != nullptr)
  37. slices = *data;
  38. loadVolatile();
  39. }
  40. Image::~Image()
  41. {
  42. unloadVolatile();
  43. }
  44. void Image::generateMipmaps()
  45. {
  46. if (getMipmapCount() > 1 && !isCompressed() &&
  47. (GLAD_ES_VERSION_2_0 || GLAD_VERSION_3_0 || GLAD_ARB_framebuffer_object || GLAD_EXT_framebuffer_object))
  48. {
  49. gl.bindTextureToUnit(this, 0, false);
  50. GLenum gltextype = OpenGL::getGLTextureType(texType);
  51. if (gl.bugs.generateMipmapsRequiresTexture2DEnable)
  52. glEnable(gltextype);
  53. glGenerateMipmap(gltextype);
  54. }
  55. }
  56. void Image::loadDefaultTexture()
  57. {
  58. usingDefaultTexture = true;
  59. gl.bindTextureToUnit(this, 0, false);
  60. setSamplerState(samplerState);
  61. bool isSRGB = false;
  62. gl.rawTexStorage(texType, 1, PIXELFORMAT_RGBA8_UNORM, isSRGB, 2, 2, 1);
  63. // A nice friendly checkerboard to signify invalid textures...
  64. GLubyte px[] = {0xFF,0xFF,0xFF,0xFF, 0xFF,0xA0,0xA0,0xFF,
  65. 0xFF,0xA0,0xA0,0xFF, 0xFF,0xFF,0xFF,0xFF};
  66. int slices = texType == TEXTURE_CUBE ? 6 : 1;
  67. Rect rect = {0, 0, 2, 2};
  68. for (int slice = 0; slice < slices; slice++)
  69. uploadByteData(PIXELFORMAT_RGBA8_UNORM, px, sizeof(px), 0, slice, rect, nullptr);
  70. }
  71. void Image::loadData()
  72. {
  73. int mipcount = getMipmapCount();
  74. int slicecount = 1;
  75. if (texType == TEXTURE_VOLUME)
  76. slicecount = getDepth();
  77. else if (texType == TEXTURE_2D_ARRAY)
  78. slicecount = getLayerCount();
  79. else if (texType == TEXTURE_CUBE)
  80. slicecount = 6;
  81. if (!isCompressed())
  82. gl.rawTexStorage(texType, mipcount, format, sRGB, pixelWidth, pixelHeight, texType == TEXTURE_VOLUME ? depth : layers);
  83. int w = pixelWidth;
  84. int h = pixelHeight;
  85. int d = depth;
  86. OpenGL::TextureFormat fmt = gl.convertPixelFormat(format, false, sRGB);
  87. for (int mip = 0; mip < mipcount; mip++)
  88. {
  89. if (isCompressed() && (texType == TEXTURE_2D_ARRAY || texType == TEXTURE_VOLUME))
  90. {
  91. size_t mipsize = 0;
  92. if (texType == TEXTURE_2D_ARRAY || texType == TEXTURE_VOLUME)
  93. {
  94. for (int slice = 0; slice < slices.getSliceCount(mip); slice++)
  95. {
  96. auto id = slices.get(slice, mip);
  97. if (id != nullptr)
  98. mipsize += id->getSize();
  99. }
  100. }
  101. if (mipsize > 0)
  102. {
  103. GLenum gltarget = OpenGL::getGLTextureType(texType);
  104. glCompressedTexImage3D(gltarget, mip, fmt.internalformat, w, h, d, 0, mipsize, nullptr);
  105. }
  106. }
  107. for (int slice = 0; slice < slicecount; slice++)
  108. {
  109. love::image::ImageDataBase *id = slices.get(slice, mip);
  110. if (id != nullptr)
  111. uploadImageData(id, mip, slice, 0, 0);
  112. }
  113. w = std::max(w / 2, 1);
  114. h = std::max(h / 2, 1);
  115. if (texType == TEXTURE_VOLUME)
  116. d = std::max(d / 2, 1);
  117. }
  118. if (getMipmapCount() > 1 && slices.getMipmapCount() <= 1)
  119. generateMipmaps();
  120. }
  121. void Image::uploadByteData(PixelFormat pixelformat, const void *data, size_t size, int level, int slice, const Rect &r, love::image::ImageDataBase *imgd)
  122. {
  123. love::image::ImageDataBase *oldd = slices.get(slice, level);
  124. // We can only replace the internal Data (used when reloading due to setMode)
  125. // if the dimensions match.
  126. if (imgd != nullptr && oldd != nullptr && oldd->getWidth() == imgd->getWidth()
  127. && oldd->getHeight() == imgd->getHeight())
  128. {
  129. slices.set(slice, level, imgd);
  130. }
  131. OpenGL::TempDebugGroup debuggroup("Image data upload");
  132. gl.bindTextureToUnit(this, 0, false);
  133. OpenGL::TextureFormat fmt = OpenGL::convertPixelFormat(pixelformat, false, sRGB);
  134. GLenum gltarget = OpenGL::getGLTextureType(texType);
  135. if (texType == TEXTURE_CUBE)
  136. gltarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + slice;
  137. if (isPixelFormatCompressed(pixelformat))
  138. {
  139. if (r.x != 0 || r.y != 0)
  140. throw love::Exception("x and y parameters must be 0 for compressed images.");
  141. if (texType == TEXTURE_2D || texType == TEXTURE_CUBE)
  142. glCompressedTexImage2D(gltarget, level, fmt.internalformat, r.w, r.h, 0, size, data);
  143. else if (texType == TEXTURE_2D_ARRAY || texType == TEXTURE_VOLUME)
  144. glCompressedTexSubImage3D(gltarget, level, 0, 0, slice, r.w, r.h, 1, fmt.internalformat, size, data);
  145. }
  146. else
  147. {
  148. if (texType == TEXTURE_2D || texType == TEXTURE_CUBE)
  149. glTexSubImage2D(gltarget, level, r.x, r.y, r.w, r.h, fmt.externalformat, fmt.type, data);
  150. else if (texType == TEXTURE_2D_ARRAY || texType == TEXTURE_VOLUME)
  151. glTexSubImage3D(gltarget, level, r.x, r.y, slice, r.w, r.h, 1, fmt.externalformat, fmt.type, data);
  152. }
  153. }
  154. bool Image::loadVolatile()
  155. {
  156. if (texture != 0)
  157. return true;
  158. OpenGL::TempDebugGroup debuggroup("Image load");
  159. // NPOT textures don't support mipmapping without full NPOT support.
  160. if ((GLAD_ES_VERSION_2_0 && !(GLAD_ES_VERSION_3_0 || GLAD_OES_texture_npot))
  161. && (pixelWidth != nextP2(pixelWidth) || pixelHeight != nextP2(pixelHeight)))
  162. {
  163. mipmapCount = 1;
  164. samplerState.mipmapFilter = SamplerState::MIPMAP_FILTER_NONE;
  165. }
  166. glGenTextures(1, &texture);
  167. gl.bindTextureToUnit(this, 0, false);
  168. // Use a default texture if the size is too big for the system.
  169. if (!validateDimensions(false))
  170. {
  171. loadDefaultTexture();
  172. return true;
  173. }
  174. setSamplerState(samplerState);
  175. while (glGetError() != GL_NO_ERROR); // Clear errors.
  176. try
  177. {
  178. loadData();
  179. GLenum glerr = glGetError();
  180. if (glerr != GL_NO_ERROR)
  181. throw love::Exception("Cannot create image (OpenGL error: %s)", OpenGL::errorString(glerr));
  182. }
  183. catch (love::Exception &)
  184. {
  185. gl.deleteTexture(texture);
  186. texture = 0;
  187. throw;
  188. }
  189. int64 memsize = 0;
  190. for (int slice = 0; slice < slices.getSliceCount(0); slice++)
  191. memsize += slices.get(slice, 0)->getSize();
  192. if (getMipmapCount() > 1)
  193. memsize *= 1.33334;
  194. setGraphicsMemorySize(memsize);
  195. usingDefaultTexture = false;
  196. return true;
  197. }
  198. void Image::unloadVolatile()
  199. {
  200. if (texture == 0)
  201. return;
  202. gl.deleteTexture(texture);
  203. texture = 0;
  204. setGraphicsMemorySize(0);
  205. }
  206. ptrdiff_t Image::getHandle() const
  207. {
  208. return texture;
  209. }
  210. void Image::setSamplerState(const SamplerState &s)
  211. {
  212. Texture::setSamplerState(s);
  213. if (!OpenGL::hasTextureFilteringSupport(getPixelFormat()))
  214. {
  215. samplerState.magFilter = samplerState.minFilter = SamplerState::FILTER_NEAREST;
  216. if (samplerState.mipmapFilter == SamplerState::MIPMAP_FILTER_LINEAR)
  217. samplerState.mipmapFilter = SamplerState::MIPMAP_FILTER_NEAREST;
  218. }
  219. // We don't want filtering or (attempted) mipmaps on the default texture.
  220. if (usingDefaultTexture)
  221. {
  222. samplerState.mipmapFilter = SamplerState::MIPMAP_FILTER_NONE;
  223. samplerState.minFilter = samplerState.magFilter = SamplerState::FILTER_NEAREST;
  224. }
  225. // If we only have limited NPOT support then the wrap mode must be CLAMP.
  226. if ((GLAD_ES_VERSION_2_0 && !(GLAD_ES_VERSION_3_0 || GLAD_OES_texture_npot))
  227. && (pixelWidth != nextP2(pixelWidth) || pixelHeight != nextP2(pixelHeight) || depth != nextP2(depth)))
  228. {
  229. samplerState.wrapU = samplerState.wrapV = samplerState.wrapW = SamplerState::WRAP_CLAMP;
  230. }
  231. gl.bindTextureToUnit(this, 0, false);
  232. gl.setSamplerState(texType, samplerState);
  233. }
  234. } // opengl
  235. } // graphics
  236. } // love