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 "Canvas.h" |
22 | #include "Graphics.h" |
23 | |
24 | namespace love |
25 | { |
26 | namespace graphics |
27 | { |
28 | |
29 | love::Type Canvas::type("Canvas" , &Texture::type); |
30 | int Canvas::canvasCount = 0; |
31 | |
32 | Canvas::Canvas(const Settings &settings) |
33 | : Texture(settings.type) |
34 | { |
35 | this->settings = settings; |
36 | |
37 | width = settings.width; |
38 | height = settings.height; |
39 | pixelWidth = (int) ((width * settings.dpiScale) + 0.5); |
40 | pixelHeight = (int) ((height * settings.dpiScale) + 0.5); |
41 | |
42 | format = settings.format; |
43 | |
44 | if (texType == TEXTURE_VOLUME) |
45 | depth = settings.layers; |
46 | else if (texType == TEXTURE_2D_ARRAY) |
47 | layers = settings.layers; |
48 | |
49 | if (width <= 0 || height <= 0 || layers <= 0) |
50 | throw love::Exception("Canvas dimensions must be greater than 0." ); |
51 | |
52 | if (texType != TEXTURE_2D && settings.msaa > 1) |
53 | throw love::Exception("MSAA is only supported for Canvases with the 2D texture type." ); |
54 | |
55 | if (settings.readable.hasValue) |
56 | readable = settings.readable.value; |
57 | else |
58 | readable = !isPixelFormatDepthStencil(format); |
59 | |
60 | if (readable && isPixelFormatDepthStencil(format) && settings.msaa > 1) |
61 | throw love::Exception("Readable depth/stencil Canvases with MSAA are not currently supported." ); |
62 | |
63 | if ((!readable || settings.msaa > 1) && settings.mipmaps != MIPMAPS_NONE) |
64 | throw love::Exception("Non-readable and MSAA textures cannot have mipmaps." ); |
65 | |
66 | if (settings.mipmaps != MIPMAPS_NONE) |
67 | { |
68 | mipmapCount = getTotalMipmapCount(pixelWidth, pixelHeight, depth); |
69 | filter.mipmap = defaultMipmapFilter; |
70 | } |
71 | |
72 | if (settings.mipmaps == MIPMAPS_AUTO && isPixelFormatDepthStencil(format)) |
73 | throw love::Exception("Automatic mipmap generation cannot be used for depth/stencil Canvases." ); |
74 | |
75 | auto gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS); |
76 | const Graphics::Capabilities &caps = gfx->getCapabilities(); |
77 | |
78 | if (!gfx->isCanvasFormatSupported(format, readable)) |
79 | { |
80 | const char *fstr = "rgba8" ; |
81 | const char *readablestr = "" ; |
82 | if (readable != !isPixelFormatDepthStencil(format)) |
83 | readablestr = readable ? " readable" : " non-readable" ; |
84 | love::getConstant(format, fstr); |
85 | throw love::Exception("The %s%s canvas format is not supported by your graphics drivers." , fstr, readablestr); |
86 | } |
87 | |
88 | if (getRequestedMSAA() > 1 && texType != TEXTURE_2D) |
89 | throw love::Exception("MSAA is only supported for 2D texture types." ); |
90 | |
91 | if (!readable && texType != TEXTURE_2D) |
92 | throw love::Exception("Non-readable pixel formats are only supported for 2D texture types." ); |
93 | |
94 | if (!caps.textureTypes[texType]) |
95 | { |
96 | const char *textypestr = "unknown" ; |
97 | Texture::getConstant(texType, textypestr); |
98 | throw love::Exception("%s textures are not supported on this system!" , textypestr); |
99 | } |
100 | |
101 | validateDimensions(true); |
102 | |
103 | canvasCount++; |
104 | } |
105 | |
106 | Canvas::~Canvas() |
107 | { |
108 | canvasCount--; |
109 | } |
110 | |
111 | Canvas::MipmapMode Canvas::getMipmapMode() const |
112 | { |
113 | return settings.mipmaps; |
114 | } |
115 | |
116 | int Canvas::getRequestedMSAA() const |
117 | { |
118 | return settings.msaa; |
119 | } |
120 | |
121 | love::image::ImageData *Canvas::newImageData(love::image::Image *module, int slice, int mipmap, const Rect &r) |
122 | { |
123 | if (!isReadable()) |
124 | throw love::Exception("Canvas:newImageData cannot be called on non-readable Canvases." ); |
125 | |
126 | if (isPixelFormatDepthStencil(getPixelFormat())) |
127 | throw love::Exception("Canvas:newImageData cannot be called on Canvases with depth/stencil pixel formats." ); |
128 | |
129 | if (r.x < 0 || r.y < 0 || r.w <= 0 || r.h <= 0 || (r.x + r.w) > getPixelWidth(mipmap) || (r.y + r.h) > getPixelHeight(mipmap)) |
130 | throw love::Exception("Invalid rectangle dimensions." ); |
131 | |
132 | if (slice < 0 || (texType == TEXTURE_VOLUME && slice >= getDepth(mipmap)) |
133 | || (texType == TEXTURE_2D_ARRAY && slice >= layers) |
134 | || (texType == TEXTURE_CUBE && slice >= 6)) |
135 | { |
136 | throw love::Exception("Invalid slice index." ); |
137 | } |
138 | |
139 | Graphics *gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS); |
140 | if (gfx != nullptr && gfx->isCanvasActive(this)) |
141 | throw love::Exception("Canvas:newImageData cannot be called while that Canvas is currently active." ); |
142 | |
143 | PixelFormat dataformat = getPixelFormat(); |
144 | if (dataformat == PIXELFORMAT_sRGBA8) |
145 | dataformat = PIXELFORMAT_RGBA8; |
146 | |
147 | if (!image::ImageData::validPixelFormat(dataformat)) |
148 | { |
149 | const char *formatname = "unknown" ; |
150 | love::getConstant(dataformat, formatname); |
151 | throw love::Exception("ImageData with the '%s' pixel format is not supported." , formatname); |
152 | } |
153 | |
154 | return module->newImageData(r.w, r.h, dataformat); |
155 | } |
156 | |
157 | void Canvas::draw(Graphics *gfx, Quad *q, const Matrix4 &t) |
158 | { |
159 | if (gfx->isCanvasActive(this)) |
160 | throw love::Exception("Cannot render a Canvas to itself!" ); |
161 | |
162 | Texture::draw(gfx, q, t); |
163 | } |
164 | |
165 | void Canvas::drawLayer(Graphics *gfx, int layer, Quad *quad, const Matrix4 &m) |
166 | { |
167 | if (gfx->isCanvasActive(this, layer)) |
168 | throw love::Exception("Cannot render a Canvas to itself!" ); |
169 | |
170 | Texture::drawLayer(gfx, layer, quad, m); |
171 | } |
172 | |
173 | bool Canvas::getConstant(const char *in, MipmapMode &out) |
174 | { |
175 | return mipmapModes.find(in, out); |
176 | } |
177 | |
178 | bool Canvas::getConstant(MipmapMode in, const char *&out) |
179 | { |
180 | return mipmapModes.find(in, out); |
181 | } |
182 | |
183 | std::vector<std::string> Canvas::getConstants(MipmapMode) |
184 | { |
185 | return mipmapModes.getNames(); |
186 | } |
187 | |
188 | bool Canvas::getConstant(const char *in, SettingType &out) |
189 | { |
190 | return settingTypes.find(in, out); |
191 | } |
192 | |
193 | bool Canvas::getConstant(SettingType in, const char *&out) |
194 | { |
195 | return settingTypes.find(in, out); |
196 | } |
197 | |
198 | const char *Canvas::getConstant(SettingType in) |
199 | { |
200 | const char *name = nullptr; |
201 | getConstant(in, name); |
202 | return name; |
203 | } |
204 | |
205 | std::vector<std::string> Canvas::getConstants(SettingType) |
206 | { |
207 | return settingTypes.getNames(); |
208 | } |
209 | |
210 | StringMap<Canvas::MipmapMode, Canvas::MIPMAPS_MAX_ENUM>::Entry Canvas::mipmapEntries[] = |
211 | { |
212 | { "none" , MIPMAPS_NONE }, |
213 | { "manual" , MIPMAPS_MANUAL }, |
214 | { "auto" , MIPMAPS_AUTO }, |
215 | }; |
216 | |
217 | StringMap<Canvas::MipmapMode, Canvas::MIPMAPS_MAX_ENUM> Canvas::mipmapModes(Canvas::mipmapEntries, sizeof(Canvas::mipmapEntries)); |
218 | |
219 | StringMap<Canvas::SettingType, Canvas::SETTING_MAX_ENUM>::Entry Canvas::settingTypeEntries[] = |
220 | { |
221 | // Width / height / layers are currently omittted because they're separate |
222 | // arguments to newCanvas in the wrapper code. |
223 | { "mipmaps" , SETTING_MIPMAPS }, |
224 | { "format" , SETTING_FORMAT }, |
225 | { "type" , SETTING_TYPE }, |
226 | { "dpiscale" , SETTING_DPI_SCALE }, |
227 | { "msaa" , SETTING_MSAA }, |
228 | { "readable" , SETTING_READABLE }, |
229 | }; |
230 | |
231 | StringMap<Canvas::SettingType, Canvas::SETTING_MAX_ENUM> Canvas::settingTypes(Canvas::settingTypeEntries, sizeof(Canvas::settingTypeEntries)); |
232 | |
233 | } // graphics |
234 | } // love |
235 | |
236 | |