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 | |
21 | #include "Image.h" |
22 | |
23 | #include "graphics/Graphics.h" |
24 | #include "common/int.h" |
25 | |
26 | // STD |
27 | #include <algorithm> // for min/max |
28 | |
29 | namespace love |
30 | { |
31 | namespace graphics |
32 | { |
33 | namespace opengl |
34 | { |
35 | |
36 | Image::Image(TextureType textype, PixelFormat format, int width, int height, int slices, const Settings &settings) |
37 | : love::graphics::Image(textype, format, width, height, slices, settings) |
38 | , texture(0) |
39 | { |
40 | loadVolatile(); |
41 | } |
42 | |
43 | Image::Image(const Slices &slices, const Settings &settings) |
44 | : love::graphics::Image(slices, settings) |
45 | , texture(0) |
46 | { |
47 | loadVolatile(); |
48 | } |
49 | |
50 | Image::~Image() |
51 | { |
52 | unloadVolatile(); |
53 | } |
54 | |
55 | void Image::generateMipmaps() |
56 | { |
57 | if (getMipmapCount() > 1 && !isCompressed() && |
58 | (GLAD_ES_VERSION_2_0 || GLAD_VERSION_3_0 || GLAD_ARB_framebuffer_object || GLAD_EXT_framebuffer_object)) |
59 | { |
60 | gl.bindTextureToUnit(this, 0, false); |
61 | |
62 | GLenum gltextype = OpenGL::getGLTextureType(texType); |
63 | |
64 | if (gl.bugs.generateMipmapsRequiresTexture2DEnable) |
65 | glEnable(gltextype); |
66 | |
67 | glGenerateMipmap(gltextype); |
68 | } |
69 | } |
70 | |
71 | void Image::loadDefaultTexture() |
72 | { |
73 | usingDefaultTexture = true; |
74 | |
75 | gl.bindTextureToUnit(this, 0, false); |
76 | setFilter(filter); |
77 | |
78 | bool isSRGB = false; |
79 | gl.rawTexStorage(texType, 1, PIXELFORMAT_RGBA8, isSRGB, 2, 2, 1); |
80 | |
81 | // A nice friendly checkerboard to signify invalid textures... |
82 | GLubyte px[] = {0xFF,0xFF,0xFF,0xFF, 0xFF,0xA0,0xA0,0xFF, |
83 | 0xFF,0xA0,0xA0,0xFF, 0xFF,0xFF,0xFF,0xFF}; |
84 | |
85 | int slices = texType == TEXTURE_CUBE ? 6 : 1; |
86 | Rect rect = {0, 0, 2, 2}; |
87 | for (int slice = 0; slice < slices; slice++) |
88 | uploadByteData(PIXELFORMAT_RGBA8, px, sizeof(px), 0, slice, rect); |
89 | } |
90 | |
91 | void Image::loadData() |
92 | { |
93 | int mipcount = getMipmapCount(); |
94 | int slicecount = 1; |
95 | |
96 | if (texType == TEXTURE_VOLUME) |
97 | slicecount = getDepth(); |
98 | else if (texType == TEXTURE_2D_ARRAY) |
99 | slicecount = getLayerCount(); |
100 | else if (texType == TEXTURE_CUBE) |
101 | slicecount = 6; |
102 | |
103 | if (!isCompressed()) |
104 | gl.rawTexStorage(texType, mipcount, format, sRGB, pixelWidth, pixelHeight, texType == TEXTURE_VOLUME ? depth : layers); |
105 | |
106 | if (mipmapsType == MIPMAPS_GENERATED) |
107 | mipcount = 1; |
108 | |
109 | int w = pixelWidth; |
110 | int h = pixelHeight; |
111 | int d = depth; |
112 | |
113 | OpenGL::TextureFormat fmt = gl.convertPixelFormat(format, false, sRGB); |
114 | |
115 | for (int mip = 0; mip < mipcount; mip++) |
116 | { |
117 | if (isCompressed() && (texType == TEXTURE_2D_ARRAY || texType == TEXTURE_VOLUME)) |
118 | { |
119 | int mipslices = data.getSliceCount(mip); |
120 | size_t mipsize = 0; |
121 | |
122 | for (int slice = 0; slice < mipslices; slice++) |
123 | mipsize += data.get(slice, mip)->getSize(); |
124 | |
125 | GLenum gltarget = OpenGL::getGLTextureType(texType); |
126 | glCompressedTexImage3D(gltarget, mip, fmt.internalformat, w, h, mipslices, 0, mipsize, nullptr); |
127 | } |
128 | |
129 | for (int slice = 0; slice < slicecount; slice++) |
130 | { |
131 | love::image::ImageDataBase *id = data.get(slice, mip); |
132 | |
133 | if (id != nullptr) |
134 | uploadImageData(id, mip, slice, 0, 0); |
135 | } |
136 | |
137 | w = std::max(w / 2, 1); |
138 | h = std::max(h / 2, 1); |
139 | |
140 | if (texType == TEXTURE_VOLUME) |
141 | d = std::max(d / 2, 1); |
142 | } |
143 | |
144 | if (mipmapsType == MIPMAPS_GENERATED) |
145 | generateMipmaps(); |
146 | } |
147 | |
148 | void Image::uploadByteData(PixelFormat pixelformat, const void *data, size_t size, int level, int slice, const Rect &r) |
149 | { |
150 | OpenGL::TempDebugGroup debuggroup("Image data upload" ); |
151 | |
152 | gl.bindTextureToUnit(this, 0, false); |
153 | |
154 | OpenGL::TextureFormat fmt = OpenGL::convertPixelFormat(pixelformat, false, sRGB); |
155 | GLenum gltarget = OpenGL::getGLTextureType(texType); |
156 | |
157 | if (texType == TEXTURE_CUBE) |
158 | gltarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + slice; |
159 | |
160 | if (isPixelFormatCompressed(pixelformat)) |
161 | { |
162 | if (r.x != 0 || r.y != 0) |
163 | throw love::Exception("x and y parameters must be 0 for compressed images." ); |
164 | |
165 | if (texType == TEXTURE_2D || texType == TEXTURE_CUBE) |
166 | glCompressedTexImage2D(gltarget, level, fmt.internalformat, r.w, r.h, 0, size, data); |
167 | else if (texType == TEXTURE_2D_ARRAY || texType == TEXTURE_VOLUME) |
168 | glCompressedTexSubImage3D(gltarget, level, 0, 0, slice, r.w, r.h, 1, fmt.internalformat, size, data); |
169 | } |
170 | else |
171 | { |
172 | if (texType == TEXTURE_2D || texType == TEXTURE_CUBE) |
173 | glTexSubImage2D(gltarget, level, r.x, r.y, r.w, r.h, fmt.externalformat, fmt.type, data); |
174 | else if (texType == TEXTURE_2D_ARRAY || texType == TEXTURE_VOLUME) |
175 | glTexSubImage3D(gltarget, level, r.x, r.y, slice, r.w, r.h, 1, fmt.externalformat, fmt.type, data); |
176 | } |
177 | } |
178 | |
179 | bool Image::loadVolatile() |
180 | { |
181 | if (texture != 0) |
182 | return true; |
183 | |
184 | OpenGL::TempDebugGroup debuggroup("Image load" ); |
185 | |
186 | if (!isCompressed()) |
187 | { |
188 | // GL_EXT_sRGB doesn't support glGenerateMipmap for sRGB textures. |
189 | if (sRGB && (GLAD_ES_VERSION_2_0 && GLAD_EXT_sRGB && !GLAD_ES_VERSION_3_0) |
190 | && mipmapsType != MIPMAPS_DATA) |
191 | { |
192 | mipmapsType = MIPMAPS_NONE; |
193 | filter.mipmap = FILTER_NONE; |
194 | } |
195 | } |
196 | |
197 | // NPOT textures don't support mipmapping without full NPOT support. |
198 | if ((GLAD_ES_VERSION_2_0 && !(GLAD_ES_VERSION_3_0 || GLAD_OES_texture_npot)) |
199 | && (pixelWidth != nextP2(pixelWidth) || pixelHeight != nextP2(pixelHeight))) |
200 | { |
201 | mipmapsType = MIPMAPS_NONE; |
202 | filter.mipmap = FILTER_NONE; |
203 | } |
204 | |
205 | glGenTextures(1, &texture); |
206 | gl.bindTextureToUnit(this, 0, false); |
207 | |
208 | // Use a default texture if the size is too big for the system. |
209 | if (!validateDimensions(false)) |
210 | { |
211 | loadDefaultTexture(); |
212 | return true; |
213 | } |
214 | |
215 | setFilter(filter); |
216 | setWrap(wrap); |
217 | setMipmapSharpness(mipmapSharpness); |
218 | |
219 | GLenum gltextype = OpenGL::getGLTextureType(texType); |
220 | |
221 | if (mipmapsType == MIPMAPS_NONE && (GLAD_ES_VERSION_3_0 || GLAD_VERSION_1_0)) |
222 | glTexParameteri(gltextype, GL_TEXTURE_MAX_LEVEL, 0); |
223 | |
224 | while (glGetError() != GL_NO_ERROR); // Clear errors. |
225 | |
226 | try |
227 | { |
228 | loadData(); |
229 | |
230 | GLenum glerr = glGetError(); |
231 | if (glerr != GL_NO_ERROR) |
232 | throw love::Exception("Cannot create image (OpenGL error: %s)" , OpenGL::errorString(glerr)); |
233 | } |
234 | catch (love::Exception &) |
235 | { |
236 | gl.deleteTexture(texture); |
237 | texture = 0; |
238 | throw; |
239 | } |
240 | |
241 | int64 memsize = 0; |
242 | |
243 | for (int slice = 0; slice < data.getSliceCount(0); slice++) |
244 | memsize += data.get(slice, 0)->getSize(); |
245 | |
246 | if (getMipmapCount() > 1) |
247 | memsize *= 1.33334; |
248 | |
249 | setGraphicsMemorySize(memsize); |
250 | |
251 | usingDefaultTexture = false; |
252 | return true; |
253 | } |
254 | |
255 | void Image::unloadVolatile() |
256 | { |
257 | if (texture == 0) |
258 | return; |
259 | |
260 | gl.deleteTexture(texture); |
261 | texture = 0; |
262 | |
263 | setGraphicsMemorySize(0); |
264 | } |
265 | |
266 | ptrdiff_t Image::getHandle() const |
267 | { |
268 | return texture; |
269 | } |
270 | |
271 | void Image::setFilter(const Texture::Filter &f) |
272 | { |
273 | Texture::setFilter(f); |
274 | |
275 | if (!OpenGL::hasTextureFilteringSupport(getPixelFormat())) |
276 | { |
277 | filter.mag = filter.min = FILTER_NEAREST; |
278 | |
279 | if (filter.mipmap == FILTER_LINEAR) |
280 | filter.mipmap = FILTER_NEAREST; |
281 | } |
282 | |
283 | // We don't want filtering or (attempted) mipmaps on the default texture. |
284 | if (usingDefaultTexture) |
285 | { |
286 | filter.mipmap = FILTER_NONE; |
287 | filter.min = filter.mag = FILTER_NEAREST; |
288 | } |
289 | |
290 | gl.bindTextureToUnit(this, 0, false); |
291 | gl.setTextureFilter(texType, filter); |
292 | } |
293 | |
294 | bool Image::setWrap(const Texture::Wrap &w) |
295 | { |
296 | Graphics::flushStreamDrawsGlobal(); |
297 | |
298 | bool success = true; |
299 | bool forceclamp = texType == TEXTURE_CUBE; |
300 | wrap = w; |
301 | |
302 | // If we only have limited NPOT support then the wrap mode must be CLAMP. |
303 | if ((GLAD_ES_VERSION_2_0 && !(GLAD_ES_VERSION_3_0 || GLAD_OES_texture_npot)) |
304 | && (pixelWidth != nextP2(pixelWidth) || pixelHeight != nextP2(pixelHeight) || depth != nextP2(depth))) |
305 | { |
306 | forceclamp = true; |
307 | } |
308 | |
309 | if (forceclamp) |
310 | { |
311 | if (wrap.s != WRAP_CLAMP || wrap.t != WRAP_CLAMP || wrap.r != WRAP_CLAMP) |
312 | success = false; |
313 | |
314 | wrap.s = wrap.t = wrap.r = WRAP_CLAMP; |
315 | } |
316 | |
317 | if (!gl.isClampZeroTextureWrapSupported()) |
318 | { |
319 | if (wrap.s == WRAP_CLAMP_ZERO) wrap.s = WRAP_CLAMP; |
320 | if (wrap.t == WRAP_CLAMP_ZERO) wrap.t = WRAP_CLAMP; |
321 | if (wrap.r == WRAP_CLAMP_ZERO) wrap.r = WRAP_CLAMP; |
322 | } |
323 | |
324 | gl.bindTextureToUnit(this, 0, false); |
325 | gl.setTextureWrap(texType, wrap); |
326 | |
327 | return success; |
328 | } |
329 | |
330 | bool Image::setMipmapSharpness(float sharpness) |
331 | { |
332 | if (!gl.isSamplerLODBiasSupported()) |
333 | return false; |
334 | |
335 | Graphics::flushStreamDrawsGlobal(); |
336 | |
337 | float maxbias = gl.getMaxLODBias(); |
338 | |
339 | if (maxbias > 0.01f) |
340 | maxbias -= 0.01f; |
341 | |
342 | mipmapSharpness = std::min(std::max(sharpness, -maxbias), maxbias); |
343 | |
344 | gl.bindTextureToUnit(this, 0, false); |
345 | |
346 | // negative bias is sharper |
347 | glTexParameterf(gl.getGLTextureType(texType), GL_TEXTURE_LOD_BIAS, -mipmapSharpness); |
348 | |
349 | return true; |
350 | } |
351 | |
352 | bool Image::isFormatSupported(PixelFormat pixelformat, bool sRGB) |
353 | { |
354 | return OpenGL::isPixelFormatSupported(pixelformat, false, true, sRGB); |
355 | } |
356 | |
357 | } // opengl |
358 | } // graphics |
359 | } // love |
360 | |