Texture.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /**
  2. * Copyright (c) 2006-2023 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 "Texture.h"
  21. #include "graphics/Graphics.h"
  22. #include "Graphics.h"
  23. #include "Buffer.h"
  24. #include "common/int.h"
  25. // STD
  26. #include <algorithm> // for min/max
  27. namespace love
  28. {
  29. namespace graphics
  30. {
  31. namespace opengl
  32. {
  33. static GLenum createFBO(GLuint &framebuffer, TextureType texType, PixelFormat format, GLuint texture, int mips, int layers, bool clear)
  34. {
  35. // get currently bound fbo to reset to it later
  36. GLuint current_fbo = gl.getFramebuffer(OpenGL::FRAMEBUFFER_ALL);
  37. glGenFramebuffers(1, &framebuffer);
  38. gl.bindFramebuffer(OpenGL::FRAMEBUFFER_ALL, framebuffer);
  39. if (texture != 0)
  40. {
  41. if (isPixelFormatDepthStencil(format) && (GLAD_ES_VERSION_3_0 || !GLAD_ES_VERSION_2_0))
  42. {
  43. // glDrawBuffers is an ext in GL2. glDrawBuffer doesn't exist in ES3.
  44. GLenum none = GL_NONE;
  45. if (GLAD_ES_VERSION_3_0)
  46. glDrawBuffers(1, &none);
  47. else
  48. glDrawBuffer(GL_NONE);
  49. glReadBuffer(GL_NONE);
  50. }
  51. OpenGL::TextureFormat fmt = OpenGL::convertPixelFormat(format, false);
  52. int faces = texType == TEXTURE_CUBE ? 6 : 1;
  53. // Make sure all faces and layers of the texture are initialized to
  54. // transparent black. This is unfortunately probably pretty slow for
  55. // 2D-array and 3D textures with a lot of layers...
  56. for (int mip = mips - 1; mip >= 0; mip--)
  57. {
  58. for (int layer = layers - 1; layer >= 0; layer--)
  59. {
  60. for (int face = faces - 1; face >= 0; face--)
  61. {
  62. for (GLenum attachment : fmt.framebufferAttachments)
  63. {
  64. if (attachment == GL_NONE)
  65. continue;
  66. gl.framebufferTexture(attachment, texType, texture, mip, layer, face);
  67. }
  68. if (clear && isPixelFormatInteger(format))
  69. {
  70. PixelFormatType datatype = getPixelFormatInfo(format).dataType;
  71. if (datatype == PIXELFORMATTYPE_SINT)
  72. {
  73. const GLint carray[] = { 0, 0, 0, 0 };
  74. glClearBufferiv(GL_COLOR, 0, carray);
  75. }
  76. else
  77. {
  78. const GLuint carray[] = { 0, 0, 0, 0 };
  79. glClearBufferuiv(GL_COLOR, 0, carray);
  80. }
  81. }
  82. else if (clear)
  83. {
  84. bool ds = isPixelFormatDepthStencil(format);
  85. GLbitfield clearflags = ds ? GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT : GL_COLOR_BUFFER_BIT;
  86. OpenGL::CleanClearState cleanClearState(clearflags);
  87. if (ds)
  88. {
  89. gl.clearDepth(1.0);
  90. glClearStencil(0);
  91. }
  92. else
  93. {
  94. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  95. }
  96. glClear(clearflags);
  97. }
  98. }
  99. }
  100. }
  101. }
  102. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  103. gl.bindFramebuffer(OpenGL::FRAMEBUFFER_ALL, current_fbo);
  104. return status;
  105. }
  106. static GLenum newRenderbuffer(int width, int height, int &samples, PixelFormat pixelformat, GLuint &buffer)
  107. {
  108. OpenGL::TextureFormat fmt = OpenGL::convertPixelFormat(pixelformat, true);
  109. GLuint current_fbo = gl.getFramebuffer(OpenGL::FRAMEBUFFER_ALL);
  110. // Temporary FBO used to clear the renderbuffer.
  111. GLuint fbo = 0;
  112. glGenFramebuffers(1, &fbo);
  113. gl.bindFramebuffer(OpenGL::FRAMEBUFFER_ALL, fbo);
  114. if (isPixelFormatDepthStencil(pixelformat) && (GLAD_ES_VERSION_3_0 || !GLAD_ES_VERSION_2_0))
  115. {
  116. // glDrawBuffers is an ext in GL2. glDrawBuffer doesn't exist in ES3.
  117. GLenum none = GL_NONE;
  118. if (GLAD_ES_VERSION_3_0)
  119. glDrawBuffers(1, &none);
  120. else
  121. glDrawBuffer(GL_NONE);
  122. glReadBuffer(GL_NONE);
  123. }
  124. glGenRenderbuffers(1, &buffer);
  125. glBindRenderbuffer(GL_RENDERBUFFER, buffer);
  126. if (samples > 1)
  127. glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, fmt.internalformat, width, height);
  128. else
  129. glRenderbufferStorage(GL_RENDERBUFFER, fmt.internalformat, width, height);
  130. for (GLenum attachment : fmt.framebufferAttachments)
  131. {
  132. if (attachment != GL_NONE)
  133. glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, buffer);
  134. }
  135. if (samples > 1)
  136. {
  137. glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
  138. samples = std::max(1, samples);
  139. }
  140. glBindRenderbuffer(GL_RENDERBUFFER, 0);
  141. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  142. if (status == GL_FRAMEBUFFER_COMPLETE)
  143. {
  144. if (isPixelFormatInteger(pixelformat))
  145. {
  146. PixelFormatType datatype = getPixelFormatInfo(pixelformat).dataType;
  147. if (datatype == PIXELFORMATTYPE_SINT)
  148. {
  149. const GLint carray[] = { 0, 0, 0, 0 };
  150. glClearBufferiv(GL_COLOR, 0, carray);
  151. }
  152. else
  153. {
  154. const GLuint carray[] = { 0, 0, 0, 0 };
  155. glClearBufferuiv(GL_COLOR, 0, carray);
  156. }
  157. }
  158. else
  159. {
  160. bool ds = isPixelFormatDepthStencil(pixelformat);
  161. GLbitfield clearflags = ds ? GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT : GL_COLOR_BUFFER_BIT;
  162. OpenGL::CleanClearState cleanClearState(clearflags);
  163. if (ds)
  164. {
  165. gl.clearDepth(1.0);
  166. glClearStencil(0);
  167. }
  168. else
  169. {
  170. // Initialize the buffer to transparent black.
  171. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  172. }
  173. glClear(clearflags);
  174. }
  175. }
  176. else
  177. {
  178. glDeleteRenderbuffers(1, &buffer);
  179. buffer = 0;
  180. samples = 1;
  181. }
  182. gl.bindFramebuffer(OpenGL::FRAMEBUFFER_ALL, current_fbo);
  183. gl.deleteFramebuffer(fbo);
  184. return status;
  185. }
  186. Texture::Texture(love::graphics::Graphics *gfx, const Settings &settings, const Slices *data)
  187. : love::graphics::Texture(gfx, settings, data)
  188. , slices(settings.type)
  189. , fbo(0)
  190. , texture(0)
  191. , renderbuffer(0)
  192. , framebufferStatus(GL_FRAMEBUFFER_COMPLETE)
  193. , textureGLError(GL_NO_ERROR)
  194. , actualSamples(1)
  195. {
  196. if (data != nullptr)
  197. slices = *data;
  198. if (!loadVolatile())
  199. {
  200. if (framebufferStatus != GL_FRAMEBUFFER_COMPLETE)
  201. throw love::Exception("Cannot create Texture (OpenGL framebuffer error: %s)", OpenGL::framebufferStatusString(framebufferStatus));
  202. if (textureGLError != GL_NO_ERROR)
  203. throw love::Exception("Cannot create Texture (OpenGL error: %s)", OpenGL::errorString(textureGLError));
  204. }
  205. // ImageData is referenced by the first loadVolatile call, but we don't
  206. // hang on to it after that so we can save memory.
  207. slices.clear();
  208. }
  209. Texture::~Texture()
  210. {
  211. unloadVolatile();
  212. }
  213. void Texture::createTexture()
  214. {
  215. // The base class handles some validation. For example, if ImageData is
  216. // given then it must exist for all mip levels, a render target can't use
  217. // a compressed format, etc.
  218. glGenTextures(1, &texture);
  219. gl.bindTextureToUnit(this, 0, false);
  220. GLenum gltype = OpenGL::getGLTextureType(texType);
  221. if (renderTarget && GLAD_ANGLE_texture_usage)
  222. glTexParameteri(gltype, GL_TEXTURE_USAGE_ANGLE, GL_FRAMEBUFFER_ATTACHMENT_ANGLE);
  223. setSamplerState(samplerState);
  224. int mipcount = getMipmapCount();
  225. int slicecount = 1;
  226. if (texType == TEXTURE_VOLUME)
  227. slicecount = getDepth();
  228. else if (texType == TEXTURE_2D_ARRAY)
  229. slicecount = getLayerCount();
  230. else if (texType == TEXTURE_CUBE)
  231. slicecount = 6;
  232. // For a couple flimsy reasons, we don't initialize the texture here if it's
  233. // compressed. I need to verify that getPixelFormatSliceSize will return the
  234. // correct value for all compressed texture formats, and I also vaguely
  235. // remember some driver issues on some old Android systems, maybe...
  236. // For now, the base class enforces data on init for compressed textures.
  237. if (!isCompressed())
  238. gl.rawTexStorage(texType, mipcount, format, pixelWidth, pixelHeight, texType == TEXTURE_VOLUME ? depth : layers);
  239. // rawTexStorage handles this for uncompressed textures.
  240. if (isCompressed() && (GLAD_VERSION_1_1 || GLAD_ES_VERSION_3_0))
  241. glTexParameteri(gltype, GL_TEXTURE_MAX_LEVEL, mipcount - 1);
  242. int w = pixelWidth;
  243. int h = pixelHeight;
  244. int d = depth;
  245. OpenGL::TextureFormat fmt = gl.convertPixelFormat(format, false);
  246. for (int mip = 0; mip < mipcount; mip++)
  247. {
  248. if (isCompressed() && (texType == TEXTURE_2D_ARRAY || texType == TEXTURE_VOLUME))
  249. {
  250. int slicecount = slices.getSliceCount(mip);
  251. size_t mipsize = 0;
  252. for (int slice = 0; slice < slicecount; slice++)
  253. {
  254. auto id = slices.get(slice, mip);
  255. if (id != nullptr)
  256. mipsize += id->getSize();
  257. }
  258. if (mipsize > 0)
  259. glCompressedTexImage3D(gltype, mip, fmt.internalformat, w, h, slicecount, 0, mipsize, nullptr);
  260. }
  261. for (int slice = 0; slice < slicecount; slice++)
  262. {
  263. love::image::ImageDataBase *id = slices.get(slice, mip);
  264. if (id != nullptr)
  265. uploadImageData(id, mip, slice, 0, 0);
  266. }
  267. w = std::max(w / 2, 1);
  268. h = std::max(h / 2, 1);
  269. if (texType == TEXTURE_VOLUME)
  270. d = std::max(d / 2, 1);
  271. }
  272. bool hasdata = slices.get(0, 0) != nullptr;
  273. // All mipmap levels need to be initialized - for color formats we can clear
  274. // the base mip and use glGenerateMipmap after that's done. Depth and
  275. // stencil formats don't always support glGenerateMipmap so we need to
  276. // individually clear each mip level in that case. We avoid doing that for
  277. // color formats because of an Intel driver bug:
  278. // https://github.com/love2d/love/issues/1585
  279. int clearmips = 1;
  280. if (isPixelFormatDepthStencil(format))
  281. clearmips = mipmapCount;
  282. // Create a local FBO used for glReadPixels as well as MSAA blitting.
  283. if (isRenderTarget())
  284. {
  285. bool clear = !hasdata;
  286. int slices = texType == TEXTURE_VOLUME ? depth : layers;
  287. framebufferStatus = createFBO(fbo, texType, format, texture, clearmips, slices, clear);
  288. }
  289. else if (!hasdata)
  290. {
  291. // Initialize all slices to transparent black.
  292. for (int mip = 0; mip < clearmips; mip++)
  293. {
  294. int mipw = getPixelWidth(mip);
  295. int miph = getPixelHeight(mip);
  296. std::vector<uint8> emptydata(getPixelFormatSliceSize(format, mipw, miph));
  297. Rect r = {0, 0, mipw, miph};
  298. int slices = texType == TEXTURE_VOLUME ? getDepth(mip) : layers;
  299. slices = texType == TEXTURE_CUBE ? 6 : slices;
  300. for (int i = 0; i < slices; i++)
  301. uploadByteData(format, emptydata.data(), emptydata.size(), mip, i, r);
  302. }
  303. }
  304. // Non-readable textures can't have mipmaps (enforced in the base class),
  305. // so generateMipmaps here is fine - when they aren't already initialized.
  306. if (clearmips < mipmapCount && slices.getMipmapCount() <= 1 && getMipmapsMode() != MIPMAPS_NONE)
  307. generateMipmaps();
  308. }
  309. bool Texture::loadVolatile()
  310. {
  311. if (texture != 0 || renderbuffer != 0)
  312. return true;
  313. OpenGL::TempDebugGroup debuggroup("Texture load");
  314. // NPOT textures don't support mipmapping without full NPOT support.
  315. if ((GLAD_ES_VERSION_2_0 && !(GLAD_ES_VERSION_3_0 || GLAD_OES_texture_npot))
  316. && (pixelWidth != nextP2(pixelWidth) || pixelHeight != nextP2(pixelHeight)))
  317. {
  318. mipmapCount = 1;
  319. samplerState.mipmapFilter = SamplerState::MIPMAP_FILTER_NONE;
  320. }
  321. actualSamples = std::max(1, std::min(getRequestedMSAA(), gl.getMaxSamples()));
  322. while (glGetError() != GL_NO_ERROR); // Clear errors.
  323. framebufferStatus = GL_FRAMEBUFFER_COMPLETE;
  324. textureGLError = GL_NO_ERROR;
  325. if (isReadable())
  326. createTexture();
  327. if (framebufferStatus == GL_FRAMEBUFFER_COMPLETE && (!isReadable() || actualSamples > 1))
  328. framebufferStatus = newRenderbuffer(pixelWidth, pixelHeight, actualSamples, format, renderbuffer);
  329. textureGLError = glGetError();
  330. if (framebufferStatus != GL_FRAMEBUFFER_COMPLETE || textureGLError != GL_NO_ERROR)
  331. {
  332. unloadVolatile();
  333. return false;
  334. }
  335. int64 memsize = 0;
  336. for (int mip = 0; mip < getMipmapCount(); mip++)
  337. {
  338. int w = getPixelWidth(mip);
  339. int h = getPixelHeight(mip);
  340. int slices = getDepth(mip) * layers * (texType == TEXTURE_CUBE ? 6 : 1);
  341. memsize += getPixelFormatSliceSize(format, w, h) * slices;
  342. }
  343. if (actualSamples > 1 && isReadable())
  344. {
  345. int slices = depth * layers * (texType == TEXTURE_CUBE ? 6 : 1);
  346. memsize += getPixelFormatSliceSize(format, pixelWidth, pixelHeight) * slices * actualSamples;
  347. }
  348. else if (actualSamples > 1)
  349. memsize *= actualSamples;
  350. setGraphicsMemorySize(memsize);
  351. if (!debugName.empty() && (GLAD_VERSION_4_3 || GLAD_ES_VERSION_3_2))
  352. {
  353. if (texture)
  354. glObjectLabel(GL_TEXTURE, texture, -1, debugName.c_str());
  355. if (renderbuffer)
  356. {
  357. std::string rname = debugName;
  358. if (actualSamples > 1)
  359. rname += " (MSAA buffer)";
  360. glObjectLabel(GL_RENDERBUFFER, renderbuffer, -1, rname.c_str());
  361. }
  362. }
  363. return true;
  364. }
  365. void Texture::unloadVolatile()
  366. {
  367. if (isRenderTarget() && (fbo != 0 || renderbuffer != 0 || texture != 0))
  368. {
  369. // This is a bit ugly, but we need some way to destroy the cached FBO
  370. // when this texture's GL object is destroyed.
  371. auto gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS);
  372. if (gfx != nullptr)
  373. gfx->cleanupRenderTexture(this);
  374. }
  375. if (fbo != 0)
  376. gl.deleteFramebuffer(fbo);
  377. if (renderbuffer != 0)
  378. glDeleteRenderbuffers(1, &renderbuffer);
  379. if (texture != 0)
  380. gl.deleteTexture(texture);
  381. fbo = 0;
  382. renderbuffer = 0;
  383. texture = 0;
  384. setGraphicsMemorySize(0);
  385. }
  386. void Texture::uploadByteData(PixelFormat pixelformat, const void *data, size_t size, int level, int slice, const Rect &r)
  387. {
  388. OpenGL::TempDebugGroup debuggroup("Texture data upload");
  389. gl.bindTextureToUnit(this, 0, false);
  390. OpenGL::TextureFormat fmt = OpenGL::convertPixelFormat(pixelformat, false);
  391. GLenum gltarget = OpenGL::getGLTextureType(texType);
  392. if (texType == TEXTURE_CUBE)
  393. gltarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + slice;
  394. if (isPixelFormatCompressed(pixelformat))
  395. {
  396. if (texType == TEXTURE_2D || texType == TEXTURE_CUBE)
  397. {
  398. // Possible issues on some very old drivers if TexSubImage is used.
  399. if (r.x != 0 || r.y != 0 || r.w != getPixelWidth(level) || r.h != getPixelHeight(level))
  400. glCompressedTexSubImage2D(gltarget, level, r.x, r.y, r.w, r.h, fmt.internalformat, size, data);
  401. else
  402. glCompressedTexImage2D(gltarget, level, fmt.internalformat, r.w, r.h, 0, size, data);
  403. }
  404. else if (texType == TEXTURE_2D_ARRAY || texType == TEXTURE_VOLUME)
  405. glCompressedTexSubImage3D(gltarget, level, r.x, r.y, slice, r.w, r.h, 1, fmt.internalformat, size, data);
  406. }
  407. else
  408. {
  409. if (texType == TEXTURE_2D || texType == TEXTURE_CUBE)
  410. glTexSubImage2D(gltarget, level, r.x, r.y, r.w, r.h, fmt.externalformat, fmt.type, data);
  411. else if (texType == TEXTURE_2D_ARRAY || texType == TEXTURE_VOLUME)
  412. glTexSubImage3D(gltarget, level, r.x, r.y, slice, r.w, r.h, 1, fmt.externalformat, fmt.type, data);
  413. }
  414. }
  415. void Texture::generateMipmapsInternal()
  416. {
  417. gl.bindTextureToUnit(this, 0, false);
  418. GLenum gltextype = OpenGL::getGLTextureType(texType);
  419. if (gl.bugs.generateMipmapsRequiresTexture2DEnable)
  420. glEnable(gltextype);
  421. glGenerateMipmap(gltextype);
  422. }
  423. void Texture::readbackInternal(int slice, int mipmap, const Rect &rect, int destwidth, size_t size, void *dest)
  424. {
  425. // Not supported in GL with compressed textures...
  426. if ((GLAD_VERSION_1_1 || GLAD_ES_VERSION_3_0) && !isCompressed())
  427. glPixelStorei(GL_PACK_ROW_LENGTH, destwidth);
  428. gl.bindTextureToUnit(this, 0, false);
  429. OpenGL::TextureFormat fmt = gl.convertPixelFormat(format, false);
  430. if (gl.isCopyTextureToBufferSupported())
  431. {
  432. if (isCompressed())
  433. glGetCompressedTextureSubImage(texture, mipmap, rect.x, rect.y, slice, rect.w, rect.h, 1, size, dest);
  434. else
  435. glGetTextureSubImage(texture, mipmap, rect.x, rect.y, slice, rect.w, rect.h, 1, fmt.externalformat, fmt.type, size, dest);
  436. }
  437. else if (fbo)
  438. {
  439. GLuint current_fbo = gl.getFramebuffer(OpenGL::FRAMEBUFFER_ALL);
  440. gl.bindFramebuffer(OpenGL::FRAMEBUFFER_ALL, getFBO());
  441. if (slice > 0 || mipmap > 0)
  442. {
  443. int layer = texType == TEXTURE_CUBE ? 0 : slice;
  444. int face = texType == TEXTURE_CUBE ? slice : 0;
  445. gl.framebufferTexture(GL_COLOR_ATTACHMENT0, texType, texture, mipmap, layer, face);
  446. }
  447. glReadPixels(rect.x, rect.y, rect.w, rect.h, fmt.externalformat, fmt.type, dest);
  448. if (slice > 0 || mipmap > 0)
  449. gl.framebufferTexture(GL_COLOR_ATTACHMENT0, texType, texture, 0, 0, 0);
  450. gl.bindFramebuffer(OpenGL::FRAMEBUFFER_ALL, current_fbo);
  451. }
  452. if ((GLAD_VERSION_1_1 || GLAD_ES_VERSION_3_0) && !isCompressed())
  453. glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  454. }
  455. void Texture::copyFromBuffer(love::graphics::Buffer *source, size_t sourceoffset, int sourcewidth, size_t size, int slice, int mipmap, const Rect &rect)
  456. {
  457. // Higher level code does validation.
  458. GLuint glbuffer = (GLuint) source->getHandle();
  459. glBindBuffer(GL_PIXEL_UNPACK_BUFFER, glbuffer);
  460. if (!isCompressed()) // Not supported in GL with compressed textures...
  461. glPixelStorei(GL_UNPACK_ROW_LENGTH, sourcewidth);
  462. // glTexSubImage and friends copy from the active pixel_unpack_buffer by
  463. // treating the pointer as a byte offset.
  464. const uint8 *byteoffset = (const uint8 *)(ptrdiff_t)sourceoffset;
  465. uploadByteData(format, byteoffset, size, mipmap, slice, rect);
  466. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  467. glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
  468. }
  469. void Texture::copyToBuffer(love::graphics::Buffer *dest, int slice, int mipmap, const Rect &rect, size_t destoffset, int destwidth, size_t size)
  470. {
  471. // Higher level code does validation.
  472. GLuint glbuffer = (GLuint) dest->getHandle();
  473. glBindBuffer(GL_PIXEL_PACK_BUFFER, glbuffer);
  474. // glTexSubImage and friends copy to the active PIXEL_PACK_BUFFER by
  475. // treating the pointer as a byte offset.
  476. uint8 *byteoffset = (uint8 *)(ptrdiff_t)destoffset;
  477. readbackInternal(slice, mipmap, rect, destwidth, size, byteoffset);
  478. glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
  479. }
  480. void Texture::setSamplerState(const SamplerState &s)
  481. {
  482. if (s.depthSampleMode.hasValue && !gl.isDepthCompareSampleSupported())
  483. throw love::Exception("Depth comparison sampling in shaders is not supported on this system.");
  484. // Base class does common validation and assigns samplerState.
  485. love::graphics::Texture::setSamplerState(s);
  486. auto supportedflags = OpenGL::getPixelFormatUsageFlags(getPixelFormat());
  487. if ((supportedflags & PIXELFORMATUSAGEFLAGS_LINEAR) == 0)
  488. {
  489. samplerState.magFilter = samplerState.minFilter = SamplerState::FILTER_NEAREST;
  490. if (samplerState.mipmapFilter == SamplerState::MIPMAP_FILTER_LINEAR)
  491. samplerState.mipmapFilter = SamplerState::MIPMAP_FILTER_NEAREST;
  492. }
  493. // If we only have limited NPOT support then the wrap mode must be CLAMP.
  494. if ((GLAD_ES_VERSION_2_0 && !(GLAD_ES_VERSION_3_0 || GLAD_OES_texture_npot))
  495. && (pixelWidth != nextP2(pixelWidth) || pixelHeight != nextP2(pixelHeight) || depth != nextP2(depth)))
  496. {
  497. samplerState.wrapU = samplerState.wrapV = samplerState.wrapW = SamplerState::WRAP_CLAMP;
  498. }
  499. gl.bindTextureToUnit(this, 0, false);
  500. gl.setSamplerState(texType, samplerState);
  501. }
  502. ptrdiff_t Texture::getHandle() const
  503. {
  504. return texture;
  505. }
  506. ptrdiff_t Texture::getRenderTargetHandle() const
  507. {
  508. return renderTarget ? (renderbuffer != 0 ? renderbuffer : texture) : 0;
  509. }
  510. } // opengl
  511. } // graphics
  512. } // love