Debian Codegraph @

Sources/nCine/Graphics/RHI/GL/GLTexture.cpp

raw
1 #include "GLTexture.h"
2 #include "GLTextureFormat.h"
3 #include "GLDebug.h"
4 #include "../../../ServiceLocator.h"
5 #include "../../../tracy_opengl.h"
6
7 #ifndef GL_TEXTURE_SWIZZLE_R
8 # define GL_TEXTURE_SWIZZLE_R 0x8E42
9 #endif
10 #ifndef GL_TEXTURE_SWIZZLE_G
11 # define GL_TEXTURE_SWIZZLE_G 0x8E43
12 #endif
13 #ifndef GL_TEXTURE_SWIZZLE_B
14 # define GL_TEXTURE_SWIZZLE_B 0x8E44
15 #endif
16 #ifndef GL_TEXTURE_SWIZZLE_A
17 # define GL_TEXTURE_SWIZZLE_A 0x8E45
18 #endif
19
20 namespace nCine::RHI::GL
21 {
22 namespace
23 {
24 GLenum TextureTargetToGL(TextureTarget target)
25 {
26 switch (target) {
27 default:
28 case TextureTarget::Texture2D:
29 return GL_TEXTURE_2D;
30 }
31 }
32
33 GLenum SamplerFilterToGLMin(SamplerFilter filter)
34 {
35 // clang-format off
36 switch (filter) {
37 case SamplerFilter::Nearest: return GL_NEAREST;
38 case SamplerFilter::Linear: return GL_LINEAR;
39 case SamplerFilter::NearestMipmapNearest: return GL_NEAREST_MIPMAP_NEAREST;
40 case SamplerFilter::LinearMipmapNearest: return GL_LINEAR_MIPMAP_NEAREST;
41 case SamplerFilter::NearestMipmapLinear: return GL_NEAREST_MIPMAP_LINEAR;
42 case SamplerFilter::LinearMipmapLinear: return GL_LINEAR_MIPMAP_LINEAR;
43 default: return GL_NEAREST;
44 }
45 // clang-format on
46 }
47
48 GLenum SamplerFilterToGLMag(SamplerFilter filter)
49 {
50 // clang-format off
51 switch (filter) {
52 case SamplerFilter::Nearest: return GL_NEAREST;
53 case SamplerFilter::Linear: return GL_LINEAR;
54 default: return GL_NEAREST;
55 }
56 // clang-format on
57 }
58
59 GLenum SamplerWrappingToGL(SamplerWrapping wrap)
60 {
61 // clang-format off
62 switch (wrap) {
63 default:
64 case SamplerWrapping::ClampToEdge: return GL_CLAMP_TO_EDGE;
65 case SamplerWrapping::MirroredRepeat: return GL_MIRRORED_REPEAT;
66 case SamplerWrapping::Repeat: return GL_REPEAT;
67 }
68 // clang-format on
69 }
70
71 #if !defined(RHI_GL_PROFILE_ES2)
72 GLint SwizzleChannelToGL(SwizzleChannel channel)
73 {
74 switch (channel) {
75 default:
76 case SwizzleChannel::Red: return GL_RED;
77 case SwizzleChannel::Green: return GL_GREEN;
78 case SwizzleChannel::Blue: return GL_BLUE;
79 case SwizzleChannel::Alpha: return GL_ALPHA;
80 case SwizzleChannel::Zero: return GL_ZERO;
81 case SwizzleChannel::One: return GL_ONE;
82 }
83 }
84 #endif
85 }
86
87 GLHashMap<GLTextureMappingFunc::Size, GLTextureMappingFunc> GLTexture::_boundTextures[MaxTextureUnits];
88 std::uint32_t GLTexture::_boundUnit = 0;
89
90 void GLTexture::InvalidateCachedBindings()
91 {
92 for (std::uint32_t i = 0; i < MaxTextureUnits; i++) {
93 _boundTextures[i].SetAll(~0u);
94 }
95 _boundUnit = ~0u;
96 }
97
98 GLTexture::GLTexture(TextureTarget target)
99 : _glHandle(0), _target(TextureTargetToGL(target)), _textureUnit(0)
100 {
101 glGenTextures(1, &_glHandle);
102 GL_LOG_ERRORS();
103 }
104
105 GLTexture::~GLTexture()
106 {
107 // Remove this texture from the bound-texture tracking on every unit it may be bound to, not just the
108 // currently active one - otherwise the freed handle lingers in the cache and a later texture that reuses
109 // the same GL handle would be considered already bound (its bind skipped -> wrong texture sampled).
110 for (std::uint32_t textureUnit = 0; textureUnit < MaxTextureUnits; textureUnit++) {
111 if (_boundTextures[textureUnit][_target] == _glHandle) {
112 BindHandle(_target, 0, textureUnit);
113 }
114 }
115
116 glDeleteTextures(1, &_glHandle);
117 GL_LOG_ERRORS();
118 }
119
120 bool GLTexture::Bind(std::uint32_t textureUnit) const
121 {
122 const bool hasBound = BindHandle(_target, _glHandle, textureUnit);
123 if (hasBound) {
124 _textureUnit = textureUnit;
125 }
126 return hasBound;
127 }
128
129 bool GLTexture::Unbind() const
130 {
131 return BindHandle(_target, 0, _textureUnit);
132 }
133
134 bool GLTexture::Unbind(GLenum target, std::uint32_t textureUnit)
135 {
136 return BindHandle(target, 0, textureUnit);
137 }
138
139 bool GLTexture::Unbind(std::uint32_t textureUnit)
140 {
141 return BindHandle(GL_TEXTURE_2D, 0, textureUnit);
142 }
143
144 void GLTexture::TexImage2D(std::int32_t level, PixelFormat format, bool bgr, std::int32_t width, std::int32_t height, const void* data)
145 {
146 TracyGpuZone("glTexImage2D");
147 GLTextureFormat::CheckSupport(format);
148 Bind();
149 GLint internalFormat;
150 GLenum externalFormat, dataType;
151 GLTextureFormat::Resolve(format, bgr, internalFormat, externalFormat, dataType);
152 glTexImage2D(_target, level, internalFormat, width, height, 0, externalFormat, dataType, data);
153 GL_LOG_ERRORS();
154 }
155
156 void GLTexture::TexSubImage2D(std::int32_t level, std::int32_t xoffset, std::int32_t yoffset, std::int32_t width, std::int32_t height, PixelFormat format, bool bgr, const void* data)
157 {
158 TracyGpuZone("glTexSubImage2D");
159 Bind();
160 GLint internalFormat;
161 GLenum externalFormat, dataType;
162 GLTextureFormat::Resolve(format, bgr, internalFormat, externalFormat, dataType);
163 glTexSubImage2D(_target, level, xoffset, yoffset, width, height, externalFormat, dataType, data);
164 GL_LOG_ERRORS();
165 }
166
167 void GLTexture::CompressedTexImage2D(std::int32_t level, PixelFormat format, std::int32_t width, std::int32_t height, std::int32_t imageSize, const void* data)
168 {
169 TracyGpuZone("glCompressedTexImage2D");
170 GLTextureFormat::CheckSupport(format);
171 Bind();
172 GLint internalFormat;
173 GLenum externalFormat, dataType;
174 GLTextureFormat::Resolve(format, false, internalFormat, externalFormat, dataType);
175 glCompressedTexImage2D(_target, level, internalFormat, width, height, 0, imageSize, data);
176 GL_LOG_ERRORS();
177 }
178
179 void GLTexture::CompressedTexSubImage2D(std::int32_t level, std::int32_t xoffset, std::int32_t yoffset, std::int32_t width, std::int32_t height, PixelFormat format, std::int32_t imageSize, const void* data)
180 {
181 TracyGpuZone("glCompressedTexSubImage2D");
182 GLTextureFormat::CheckSupport(format);
183 Bind();
184 #if !defined(DEATH_TARGET_VITA)
185 // vitaGL provides no glCompressedTexSubImage2D(), compressed-texture sub-uploads are unsupported there
186 GLint internalFormat;
187 GLenum externalFormat, dataType;
188 GLTextureFormat::Resolve(format, false, internalFormat, externalFormat, dataType);
189 glCompressedTexSubImage2D(_target, level, xoffset, yoffset, width, height, internalFormat, imageSize, data);
190 #endif
191 GL_LOG_ERRORS();
192 }
193
194 void GLTexture::TexStorage2D(std::int32_t levels, PixelFormat format, std::int32_t width, std::int32_t height)
195 {
196 TracyGpuZone("glTexStorage2D");
197 GLTextureFormat::CheckSupport(format);
198 Bind();
199 #if !defined(RHI_GL_PROFILE_ES2)
200 // glTexStorage2D() is ES 3.0 (and strict ES 2.0 headers such as vitaGL's declare none of it); the ES2
201 // profile reports SupportsImmutableStorage()==false and allocates textures via glTexImage2D instead,
202 // so this immutable-storage path is never reached here
203 GLint internalFormat;
204 GLenum externalFormat, dataType;
205 GLTextureFormat::Resolve(format, false, internalFormat, externalFormat, dataType);
206 if (GLTextureFormat::IsUnsizedInternalFormat(internalFormat)) {
207 // Immutable storage is defined for sized internal formats only, so glTexStorage2D() answers an unsized
208 // one with INVALID_ENUM - which RG8 resolves to where the profile has no texture swizzle. Allocate
209 // those per level instead: the texture stays mutable, which callers cannot tell apart, as the upload
210 // that follows goes through glTexSubImage2D() either way. Every other format keeps immutable storage.
211 for (std::int32_t level = 0; level < levels; level++) {
212 glTexImage2D(_target, level, internalFormat, width, height, 0, externalFormat, dataType, nullptr);
213 width = (width > 1 ? width / 2 : 1);
214 height = (height > 1 ? height / 2 : 1);
215 }
216 } else {
217 glTexStorage2D(_target, levels, internalFormat, width, height);
218 }
219 #else
220 static_cast<void>(levels);
221 static_cast<void>(width);
222 static_cast<void>(height);
223 #endif
224 GL_LOG_ERRORS();
225 }
226
227 void GLTexture::GetTexImage(std::int32_t level, PixelFormat format, bool bgr, void* pixels)
228 {
229 #if defined(RHI_GL_PROFILE_CORE) && !defined(DEATH_TARGET_EMSCRIPTEN)
230 TracyGpuZone("glGetTexImage");
231 Bind();
232 GLint internalFormat;
233 GLenum externalFormat, dataType;
234 GLTextureFormat::Resolve(format, bgr, internalFormat, externalFormat, dataType);
235 glGetTexImage(_target, level, externalFormat, dataType, pixels);
236 GL_LOG_ERRORS();
237 #endif
238 }
239
240 void GLTexture::SetMinFiltering(SamplerFilter filter)
241 {
242 TexParameteri(GL_TEXTURE_MIN_FILTER, SamplerFilterToGLMin(filter));
243 }
244
245 void GLTexture::SetMagFiltering(SamplerFilter filter)
246 {
247 TexParameteri(GL_TEXTURE_MAG_FILTER, SamplerFilterToGLMag(filter));
248 }
249
250 void GLTexture::SetWrap(SamplerWrapping wrap)
251 {
252 const GLenum glWrap = SamplerWrappingToGL(wrap);
253 TexParameteri(GL_TEXTURE_WRAP_S, glWrap);
254 TexParameteri(GL_TEXTURE_WRAP_T, glWrap);
255 }
256
257 void GLTexture::SetSwizzle(SwizzleChannel r, SwizzleChannel g, SwizzleChannel b, SwizzleChannel a)
258 {
259 #if defined(RHI_GL_PROFILE_ES2) || defined(DEATH_TARGET_EMSCRIPTEN)
260 // ES2 has no GL_TEXTURE_SWIZZLE_* (ES 3.0 feature), and WebGL 2.0 leaves them out as well even though the
261 // ES 3.0 it is based on has them - passing one to glTexParameteri() there is an INVALID_ENUM. The only
262 // non-identity swizzle the engine uses is the palette pipeline's (R,G,B,G) on RG8 index textures, and
263 // GLTextureFormat maps RG8 to LUMINANCE_ALPHA on both profiles, which natively samples (L,L,L,A) -
264 // identical for the channels the shaders read (.r/.a)
265 static_cast<void>(r); static_cast<void>(g); static_cast<void>(b); static_cast<void>(a);
266 #else
267 // Channels are set individually because GL_TEXTURE_SWIZZLE_RGBA (a single glTexParameteriv) is desktop-only
268 // and absent on GLES/WebGL. Requires GL 3.3+ / GLES 3.0+.
269 TexParameteri(GL_TEXTURE_SWIZZLE_R, SwizzleChannelToGL(r));
270 TexParameteri(GL_TEXTURE_SWIZZLE_G, SwizzleChannelToGL(g));
271 TexParameteri(GL_TEXTURE_SWIZZLE_B, SwizzleChannelToGL(b));
272 TexParameteri(GL_TEXTURE_SWIZZLE_A, SwizzleChannelToGL(a));
273 #endif
274 }
275
276 void GLTexture::SetMaxLevel(std::int32_t maxLevel)
277 {
278 #if defined(RHI_GL_PROFILE_ES2)
279 // ES2 has no GL_TEXTURE_MAX_LEVEL (ES 3.0 feature); an incomplete MIP chain sampled with a mipmap filter
280 // would still be incomplete, but the engine only sets this for multi-level textures it fully uploads
281 static_cast<void>(maxLevel);
282 #else
283 TexParameteri(GL_TEXTURE_MAX_LEVEL, maxLevel);
284 #endif
285 }
286
287 void GLTexture::SetUnpackAlignment(std::int32_t alignment)
288 {
289 glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
290 }
291
292 void GLTexture::TexParameterf(GLenum pname, GLfloat param)
293 {
294 Bind();
295 glTexParameterf(_target, pname, param);
296 GL_LOG_ERRORS();
297 }
298
299 void GLTexture::TexParameteri(GLenum pname, GLint param)
300 {
301 Bind();
302 glTexParameteri(_target, pname, param);
303 GL_LOG_ERRORS();
304 }
305
306 void GLTexture::SetObjectLabel(StringView label)
307 {
308 GLDebug::SetObjectLabel(GLDebug::LabelTypes::Texture, _glHandle, label);
309 }
310
311 bool GLTexture::SupportsImmutableStorage()
312 {
313 #if defined(RHI_GL_PROFILE_ES2)
314 // glTexStorage2D() is ES 3.0 (the GL_ES_VERSION_3_0 check below matches the GLES3 *headers* this build
315 // compiles against, not the runtime context); EXT_texture_storage is not assumed - use the mutable
316 // glTexImage2D() per-level fallback instead
317 return false;
318 #elif (defined(RHI_GL_PROFILE_ES) && GL_ES_VERSION_3_0) || defined(DEATH_TARGET_EMSCRIPTEN)
319 return true;
320 #else
321 const IRhiCapabilities& caps = theServiceLocator().GetRhiCapabilities();
322 return caps.HasExtension(IRhiCapabilities::Extensions::ArbTextureStorage);
323 #endif
324 }
325
326 bool GLTexture::SupportsTextureReadback()
327 {
328 #if defined(RHI_GL_PROFILE_CORE) && !defined(DEATH_TARGET_EMSCRIPTEN)
329 return true;
330 #else
331 return false;
332 #endif
333 }
334
335 void GLTexture::ClearErrors()
336 {
337 glGetError();
338 }
339
340 bool GLTexture::CheckErrors()
341 {
342 return (glGetError() != GL_NO_ERROR);
343 }
344
345 void GLTexture::CheckFormatSupport(PixelFormat format)
346 {
347 GLTextureFormat::CheckSupport(format);
348 }
349
350 bool GLTexture::BindHandle(GLenum target, GLuint glHandle, std::uint32_t textureUnit)
351 {
352 FATAL_ASSERT(textureUnit < MaxTextureUnits);
353
354 if (_boundTextures[textureUnit][target] != glHandle) {
355 if (_boundUnit != textureUnit) {
356 glActiveTexture(GL_TEXTURE0 + textureUnit);
357 _boundUnit = textureUnit;
358 }
359
360 glBindTexture(target, glHandle);
361 GL_LOG_ERRORS();
362 _boundTextures[textureUnit][target] = glHandle;
363 return true;
364 }
365 return false;
366 }
367
368 GLuint GLTexture::GetBoundHandle(GLenum target, unsigned int textureUnit)
369 {
370 FATAL_ASSERT(textureUnit < MaxTextureUnits);
371 return _boundTextures[textureUnit][target];
372 }
373 }