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#ifndef LOVE_GRAPHICS_TEXTURE_H
22#define LOVE_GRAPHICS_TEXTURE_H
23
24// LOVE
25#include "common/StringMap.h"
26#include "common/math.h"
27#include "common/pixelformat.h"
28#include "common/Exception.h"
29#include "common/Optional.h"
30#include "common/int.h"
31#include "Drawable.h"
32#include "Quad.h"
33#include "vertex.h"
34#include "depthstencil.h"
35#include "Resource.h"
36
37// C
38#include <stddef.h>
39
40namespace love
41{
42namespace graphics
43{
44
45class Graphics;
46
47enum TextureType
48{
49 TEXTURE_2D,
50 TEXTURE_VOLUME,
51 TEXTURE_2D_ARRAY,
52 TEXTURE_CUBE,
53 TEXTURE_MAX_ENUM
54};
55
56/**
57 * Base class for 2D textures. All textures can be drawn with Quads, have a
58 * width and height, and have filter and wrap modes.
59 **/
60class Texture : public Drawable, public Resource
61{
62public:
63
64 static love::Type type;
65
66 enum WrapMode
67 {
68 WRAP_CLAMP,
69 WRAP_CLAMP_ZERO,
70 WRAP_REPEAT,
71 WRAP_MIRRORED_REPEAT,
72 WRAP_MAX_ENUM
73 };
74
75 enum FilterMode
76 {
77 FILTER_NONE,
78 FILTER_LINEAR,
79 FILTER_NEAREST,
80 FILTER_MAX_ENUM
81 };
82
83 struct Filter
84 {
85 FilterMode min = FILTER_LINEAR;
86 FilterMode mag = FILTER_LINEAR;
87 FilterMode mipmap = FILTER_NONE;
88 float anisotropy = 1.0f;
89 };
90
91 struct Wrap
92 {
93 WrapMode s = WRAP_CLAMP;
94 WrapMode t = WRAP_CLAMP;
95 WrapMode r = WRAP_CLAMP;
96 };
97
98 static Filter defaultFilter;
99 static FilterMode defaultMipmapFilter;
100 static float defaultMipmapSharpness;
101
102 static int64 totalGraphicsMemory;
103
104 Texture(TextureType texType);
105 virtual ~Texture();
106
107 // Drawable.
108 void draw(Graphics *gfx, const Matrix4 &m) override;
109
110 /**
111 * Draws the texture using the specified transformation with a Quad applied.
112 **/
113 virtual void draw(Graphics *gfx, Quad *quad, const Matrix4 &m);
114
115 void drawLayer(Graphics *gfx, int layer, const Matrix4 &m);
116 virtual void drawLayer(Graphics *gfx, int layer, Quad *quad, const Matrix4 &m);
117
118 TextureType getTextureType() const;
119 PixelFormat getPixelFormat() const;
120
121 bool isReadable() const;
122
123 bool isValidSlice(int slice) const;
124
125 int getWidth(int mip = 0) const;
126 int getHeight(int mip = 0) const;
127 int getDepth(int mip = 0) const;
128 int getLayerCount() const;
129 int getMipmapCount() const;
130
131 int getPixelWidth(int mip = 0) const;
132 int getPixelHeight(int mip = 0) const;
133
134 float getDPIScale() const;
135
136 virtual void setFilter(const Filter &f);
137 virtual const Filter &getFilter() const;
138
139 virtual bool setWrap(const Wrap &w) = 0;
140 virtual const Wrap &getWrap() const;
141
142 // Sets the mipmap texture LOD bias (sharpness) value.
143 virtual bool setMipmapSharpness(float sharpness) = 0;
144 float getMipmapSharpness() const;
145
146 virtual void setDepthSampleMode(Optional<CompareMode> mode = Optional<CompareMode>());
147 Optional<CompareMode> getDepthSampleMode() const;
148
149 Quad *getQuad() const;
150
151 static bool validateFilter(const Filter &f, bool mipmapsAllowed);
152
153 static int getTotalMipmapCount(int w, int h);
154 static int getTotalMipmapCount(int w, int h, int d);
155
156 static bool getConstant(const char *in, TextureType &out);
157 static bool getConstant(TextureType in, const char *&out);
158 static std::vector<std::string> getConstants(TextureType);
159
160 static bool getConstant(const char *in, FilterMode &out);
161 static bool getConstant(FilterMode in, const char *&out);
162 static std::vector<std::string> getConstants(FilterMode);
163
164 static bool getConstant(const char *in, WrapMode &out);
165 static bool getConstant(WrapMode in, const char *&out);
166 static std::vector<std::string> getConstants(WrapMode);
167
168protected:
169
170 void initQuad();
171 void setGraphicsMemorySize(int64 size);
172
173 bool validateDimensions(bool throwException) const;
174
175 TextureType texType;
176
177 PixelFormat format;
178 bool readable;
179
180 int width;
181 int height;
182
183 int depth;
184 int layers;
185 int mipmapCount;
186
187 int pixelWidth;
188 int pixelHeight;
189
190 Filter filter;
191 Wrap wrap;
192
193 float mipmapSharpness;
194
195 Optional<CompareMode> depthCompareMode;
196
197 StrongRef<Quad> quad;
198
199 int64 graphicsMemorySize;
200
201private:
202
203 static StringMap<TextureType, TEXTURE_MAX_ENUM>::Entry texTypeEntries[];
204 static StringMap<TextureType, TEXTURE_MAX_ENUM> texTypes;
205
206 static StringMap<FilterMode, FILTER_MAX_ENUM>::Entry filterModeEntries[];
207 static StringMap<FilterMode, FILTER_MAX_ENUM> filterModes;
208
209 static StringMap<WrapMode, WRAP_MAX_ENUM>::Entry wrapModeEntries[];
210 static StringMap<WrapMode, WRAP_MAX_ENUM> wrapModes;
211
212}; // Texture
213
214} // graphics
215} // love
216
217#endif // LOVE_GRAPHICS_TEXTURE_H
218