Blender V4.3
gl_texture.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#pragma once
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_assert.h"
14
16
17namespace blender {
18namespace gpu {
19
20class GLTexture : public Texture {
21 friend class GLStateManager;
22 friend class GLFrameBuffer;
23
24 private:
31 static GLuint samplers_state_cache_[GPU_SAMPLER_EXTEND_MODES_COUNT]
34 static const int samplers_state_cache_count_ = GPU_SAMPLER_EXTEND_MODES_COUNT *
41 static GLuint custom_samplers_state_cache_[GPU_SAMPLER_CUSTOM_TYPES_COUNT];
42
44 GLenum target_ = -1;
46 GLuint tex_id_ = 0;
48 FrameBuffer *framebuffer_ = nullptr;
50 /* TODO(fclem): How do we ensure thread safety here? */
51 bool is_bound_ = false;
53 bool is_bound_image_ = false;
55 bool has_pixels_ = false;
56
57 public:
58 GLTexture(const char *name);
59 ~GLTexture();
60
61 void update_sub(
62 int mip, int offset[3], int extent[3], eGPUDataFormat type, const void *data) override;
63 void update_sub(int offset[3],
64 int extent[3],
66 GPUPixelBuffer *pixbuf) override;
67
74 void generate_mipmap() override;
75 void copy_to(Texture *dst) override;
76 void clear(eGPUDataFormat format, const void *data) override;
77 void swizzle_set(const char swizzle_mask[4]) override;
78 void mip_range_set(int min, int max) override;
79 void *read(int mip, eGPUDataFormat type) override;
80
82
83 /* TODO(fclem): Legacy. Should be removed at some point. */
84 uint gl_bindcode_get() const override;
85
91 static void samplers_init();
92
96 static void samplers_free();
97
104 static void samplers_update();
105
110 static GLuint get_sampler(const GPUSamplerState &sampler_state);
111
112 protected:
114 bool init_internal() override;
116 bool init_internal(VertBuf *vbo) override;
118 bool init_internal(GPUTexture *src, int mip_offset, int layer_offset, bool use_stencil) override;
119
120 private:
121 bool proxy_check(int mip);
122 void stencil_texture_mode_set(bool use_stencil);
123 void update_sub_direct_state_access(
124 int mip, int offset[3], int extent[3], GLenum gl_format, GLenum gl_type, const void *data);
125 FrameBuffer *framebuffer_get();
126
127 MEM_CXX_CLASS_ALLOC_FUNCS("GLTexture")
128};
129
131 private:
132 GLuint gl_id_ = 0;
133
134 public:
135 GLPixelBuffer(size_t size);
137
138 void *map() override;
139 void unmap() override;
140 int64_t get_native_handle() override;
141 size_t get_size() override;
142
143 MEM_CXX_CLASS_ALLOC_FUNCS("GLPixelBuffer")
144};
145
147{
148 switch (format) {
149 /* Texture & Render-Buffer Formats. */
150 case GPU_RGBA8UI:
151 return GL_RGBA8UI;
152 case GPU_RGBA8I:
153 return GL_RGBA8I;
154 case GPU_RGBA8:
155 return GL_RGBA8;
156 case GPU_RGBA32UI:
157 return GL_RGBA32UI;
158 case GPU_RGBA32I:
159 return GL_RGBA32I;
160 case GPU_RGBA32F:
161 return GL_RGBA32F;
162 case GPU_RGBA16UI:
163 return GL_RGBA16UI;
164 case GPU_RGBA16I:
165 return GL_RGBA16I;
166 case GPU_RGBA16F:
167 return GL_RGBA16F;
168 case GPU_RGBA16:
169 return GL_RGBA16;
170 case GPU_RG8UI:
171 return GL_RG8UI;
172 case GPU_RG8I:
173 return GL_RG8I;
174 case GPU_RG8:
175 return GL_RG8;
176 case GPU_RG32UI:
177 return GL_RG32UI;
178 case GPU_RG32I:
179 return GL_RG32I;
180 case GPU_RG32F:
181 return GL_RG32F;
182 case GPU_RG16UI:
183 return GL_RG16UI;
184 case GPU_RG16I:
185 return GL_RG16I;
186 case GPU_RG16F:
187 return GL_RG16F;
188 case GPU_RG16:
189 return GL_RG16;
190 case GPU_R8UI:
191 return GL_R8UI;
192 case GPU_R8I:
193 return GL_R8I;
194 case GPU_R8:
195 return GL_R8;
196 case GPU_R32UI:
197 return GL_R32UI;
198 case GPU_R32I:
199 return GL_R32I;
200 case GPU_R32F:
201 return GL_R32F;
202 case GPU_R16UI:
203 return GL_R16UI;
204 case GPU_R16I:
205 return GL_R16I;
206 case GPU_R16F:
207 return GL_R16F;
208 case GPU_R16:
209 return GL_R16;
210 /* Special formats texture & render-buffer. */
211 case GPU_RGB10_A2:
212 return GL_RGB10_A2;
213 case GPU_RGB10_A2UI:
214 return GL_RGB10_A2UI;
216 return GL_R11F_G11F_B10F;
218 return GL_DEPTH32F_STENCIL8;
220 return GL_DEPTH24_STENCIL8;
221 case GPU_SRGB8_A8:
222 return GL_SRGB8_ALPHA8;
223 /* Texture only formats. */
224 case GPU_RGB16F:
225 return GL_RGB16F;
226 case GPU_RGBA16_SNORM:
227 return GL_RGBA16_SNORM;
228 case GPU_RGBA8_SNORM:
229 return GL_RGBA8_SNORM;
230 case GPU_RGB32F:
231 return GL_RGB32F;
232 case GPU_RGB32I:
233 return GL_RGB32I;
234 case GPU_RGB32UI:
235 return GL_RGB32UI;
236 case GPU_RGB16_SNORM:
237 return GL_RGB16_SNORM;
238 case GPU_RGB16I:
239 return GL_RGB16I;
240 case GPU_RGB16UI:
241 return GL_RGB16UI;
242 case GPU_RGB16:
243 return GL_RGB16;
244 case GPU_RGB8_SNORM:
245 return GL_RGB8_SNORM;
246 case GPU_RGB8:
247 return GL_RGB8;
248 case GPU_RGB8I:
249 return GL_RGB8I;
250 case GPU_RGB8UI:
251 return GL_RGB8UI;
252 case GPU_RG16_SNORM:
253 return GL_RG16_SNORM;
254 case GPU_RG8_SNORM:
255 return GL_RG8_SNORM;
256 case GPU_R16_SNORM:
257 return GL_R16_SNORM;
258 case GPU_R8_SNORM:
259 return GL_R8_SNORM;
260 /* Special formats, texture only. */
262 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
264 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
266 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
267 case GPU_RGBA8_DXT1:
268 return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
269 case GPU_RGBA8_DXT3:
270 return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
271 case GPU_RGBA8_DXT5:
272 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
273 case GPU_SRGB8:
274 return GL_SRGB8;
275 case GPU_RGB9_E5:
276 return GL_RGB9_E5;
277 /* Depth Formats. */
279 return GL_DEPTH_COMPONENT32F;
281 return GL_DEPTH_COMPONENT24;
283 return GL_DEPTH_COMPONENT16;
284 }
285 BLI_assert_msg(0, "Texture format incorrect or unsupported");
286 return 0;
287}
288
289inline GLenum to_gl_target(eGPUTextureType type)
290{
291 switch (type) {
292 case GPU_TEXTURE_1D:
293 return GL_TEXTURE_1D;
295 return GL_TEXTURE_1D_ARRAY;
296 case GPU_TEXTURE_2D:
297 return GL_TEXTURE_2D;
299 return GL_TEXTURE_2D_ARRAY;
300 case GPU_TEXTURE_3D:
301 return GL_TEXTURE_3D;
302 case GPU_TEXTURE_CUBE:
303 return GL_TEXTURE_CUBE_MAP;
305 return GL_TEXTURE_CUBE_MAP_ARRAY_ARB;
307 return GL_TEXTURE_BUFFER;
308 default:
309 BLI_assert(0);
310 return GL_TEXTURE_1D;
311 }
312}
313
314inline GLenum to_gl_proxy(eGPUTextureType type)
315{
316 switch (type) {
317 case GPU_TEXTURE_1D:
318 return GL_PROXY_TEXTURE_1D;
320 return GL_PROXY_TEXTURE_1D_ARRAY;
321 case GPU_TEXTURE_2D:
322 return GL_PROXY_TEXTURE_2D;
324 return GL_PROXY_TEXTURE_2D_ARRAY;
325 case GPU_TEXTURE_3D:
326 return GL_PROXY_TEXTURE_3D;
327 case GPU_TEXTURE_CUBE:
328 return GL_PROXY_TEXTURE_CUBE_MAP;
330 return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY_ARB;
332 default:
333 BLI_assert(0);
334 return GL_TEXTURE_1D;
335 }
336}
337
338inline GLenum swizzle_to_gl(const char swizzle)
339{
340 switch (swizzle) {
341 default:
342 case 'x':
343 case 'r':
344 return GL_RED;
345 case 'y':
346 case 'g':
347 return GL_GREEN;
348 case 'z':
349 case 'b':
350 return GL_BLUE;
351 case 'w':
352 case 'a':
353 return GL_ALPHA;
354 case '0':
355 return GL_ZERO;
356 case '1':
357 return GL_ONE;
358 }
359}
360
362{
363 switch (format) {
364 case GPU_DATA_FLOAT:
365 return GL_FLOAT;
366 case GPU_DATA_INT:
367 return GL_INT;
368 case GPU_DATA_UINT:
369 return GL_UNSIGNED_INT;
370 case GPU_DATA_UBYTE:
371 return GL_UNSIGNED_BYTE;
373 return GL_UNSIGNED_INT_24_8;
375 return GL_UNSIGNED_INT_2_10_10_10_REV;
377 return GL_UNSIGNED_INT_10F_11F_11F_REV;
379 return GL_HALF_FLOAT;
380 default:
381 BLI_assert_msg(0, "Unhandled data format");
382 return GL_FLOAT;
383 }
384}
385
387{
388 switch (format) {
389 /* Texture & Render-Buffer Formats. */
390 case GPU_RGBA8:
391 case GPU_RGBA32F:
392 case GPU_RGBA16F:
393 case GPU_RGBA16:
394 return GL_RGBA;
395 case GPU_RGBA8UI:
396 case GPU_RGBA8I:
397 case GPU_RGBA32I:
398 case GPU_RGBA32UI:
399 case GPU_RGBA16UI:
400 case GPU_RGBA16I:
401 return GL_RGBA_INTEGER;
402 case GPU_RG8:
403 case GPU_RG32F:
404 case GPU_RG16I:
405 case GPU_RG16F:
406 case GPU_RG16:
407 return GL_RG;
408 case GPU_RG8UI:
409 case GPU_RG8I:
410 case GPU_RG32UI:
411 case GPU_RG32I:
412 case GPU_RG16UI:
413 return GL_RG_INTEGER;
414 case GPU_R8:
415 case GPU_R32F:
416 case GPU_R16F:
417 case GPU_R16:
418 return GL_RED;
419 case GPU_R8UI:
420 case GPU_R8I:
421 case GPU_R32UI:
422 case GPU_R32I:
423 case GPU_R16UI:
424 case GPU_R16I:
425 return GL_RED_INTEGER;
426 /* Special formats texture & render-buffer. */
427 case GPU_RGB10_A2UI:
428 case GPU_RGB10_A2:
429 case GPU_SRGB8_A8:
430 return GL_RGBA;
432 return GL_RGB;
435 return GL_DEPTH_STENCIL;
436 /* Texture only formats. */
437 case GPU_RGBA16_SNORM:
438 case GPU_RGBA8_SNORM:
439 return GL_RGBA;
440 case GPU_RGB16F:
441 case GPU_RGB32F:
442 case GPU_RGB32I:
443 case GPU_RGB32UI:
444 case GPU_RGB16_SNORM:
445 case GPU_RGB16I:
446 case GPU_RGB16UI:
447 case GPU_RGB16:
448 case GPU_RGB8_SNORM:
449 case GPU_RGB8:
450 case GPU_RGB8I:
451 case GPU_RGB8UI:
452 return GL_RGB;
453 case GPU_RG16_SNORM:
454 case GPU_RG8_SNORM:
455 return GL_RG;
456 case GPU_R16_SNORM:
457 case GPU_R8_SNORM:
458 return GL_RED;
459 /* Special formats, texture only. */
461 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
463 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
465 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
466 case GPU_RGBA8_DXT1:
467 return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
468 case GPU_RGBA8_DXT3:
469 return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
470 case GPU_RGBA8_DXT5:
471 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
472 case GPU_SRGB8:
473 case GPU_RGB9_E5:
474 return GL_RGB;
475 /* Depth Formats. */
479 return GL_DEPTH_COMPONENT;
480 }
481 BLI_assert_msg(0, "Texture format incorrect or unsupported\n");
482 return 0;
483}
484
488inline GLenum channel_len_to_gl(int channel_len)
489{
490 switch (channel_len) {
491 case 1:
492 return GL_RED;
493 case 2:
494 return GL_RG;
495 case 3:
496 return GL_RGB;
497 case 4:
498 return GL_RGBA;
499 default:
500 BLI_assert_msg(0, "Wrong number of texture channels");
501 return GL_RED;
502 }
503}
504
505} // namespace gpu
506} // namespace blender
#define BLI_assert(a)
Definition BLI_assert.h:50
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:57
unsigned int uint
static const int GPU_SAMPLER_FILTERING_TYPES_COUNT
eGPUDataFormat
@ GPU_DATA_HALF_FLOAT
@ GPU_DATA_UINT_24_8
@ GPU_DATA_INT
@ GPU_DATA_10_11_11_REV
@ GPU_DATA_UBYTE
@ GPU_DATA_UINT
@ GPU_DATA_2_10_10_10_REV
@ GPU_DATA_FLOAT
#define GPU_SAMPLER_CUSTOM_TYPES_COUNT
eGPUTextureFormat
@ GPU_RGB16
@ GPU_R16UI
@ GPU_RGB8
@ GPU_DEPTH32F_STENCIL8
@ GPU_SRGB8
@ GPU_R16I
@ GPU_SRGB8_A8
@ GPU_RG8_SNORM
@ GPU_DEPTH24_STENCIL8
@ GPU_RGB10_A2
@ GPU_RGB8I
@ GPU_R32I
@ GPU_RGBA8_SNORM
@ GPU_RGB10_A2UI
@ GPU_RG8UI
@ GPU_RGB16I
@ GPU_RGBA16_SNORM
@ GPU_RGB9_E5
@ GPU_SRGB8_A8_DXT5
@ GPU_RG8I
@ GPU_RG16I
@ GPU_RG32UI
@ GPU_RGB32I
@ GPU_RG8
@ GPU_RG32I
@ GPU_SRGB8_A8_DXT1
@ GPU_RGBA32UI
@ GPU_R8I
@ GPU_R16
@ GPU_RG16UI
@ GPU_RGBA8I
@ GPU_RGBA8_DXT1
@ GPU_RGBA8UI
@ GPU_RGB32F
@ GPU_RGBA16UI
@ GPU_RGBA16I
@ GPU_R8UI
@ GPU_RGBA16
@ GPU_SRGB8_A8_DXT3
@ GPU_RGB8_SNORM
@ GPU_RGBA8_DXT3
@ GPU_RGB32UI
@ GPU_R8_SNORM
@ GPU_RG32F
@ GPU_R8
@ GPU_RGB16_SNORM
@ GPU_DEPTH_COMPONENT24
@ GPU_RG16_SNORM
@ GPU_RGB8UI
@ GPU_RGB16F
@ GPU_RGB16UI
@ GPU_RGBA32I
@ GPU_RGBA8_DXT5
@ GPU_DEPTH_COMPONENT32F
@ GPU_R16_SNORM
@ GPU_DEPTH_COMPONENT16
#define GPU_SAMPLER_EXTEND_MODES_COUNT
Read Guarded memory(de)allocation.
size_t get_size() override
int64_t get_native_handle() override
GLTexture(const char *name)
Definition gl_texture.cc:33
void copy_to(Texture *dst) override
void mip_range_set(int min, int max) override
static void samplers_init()
static GLuint get_sampler(const GPUSamplerState &sampler_state)
static void samplers_update()
uint gl_bindcode_get() const override
void * read(int mip, eGPUDataFormat type) override
static void samplers_free()
bool init_internal() override
Definition gl_texture.cc:54
void generate_mipmap() override
void update_sub(int mip, int offset[3], int extent[3], eGPUDataFormat type, const void *data) override
void clear(eGPUDataFormat format, const void *data) override
void swizzle_set(const char swizzle_mask[4]) override
additional_info("compositor_sum_float_shared") .push_constant(Type additional_info("compositor_sum_float_shared") .push_constant(Type GPU_RGBA32F
DOF_TILES_FLATTEN_GROUP_SIZE coc_tx GPU_R11F_G11F_B10F
out_radiance out_gbuf_normal out_gbuf_closure2 GPU_RG16
SHADOW_TILEMAP_RES tiles_buf[] statistics_buf render_view_buf[SHADOW_VIEW_MAX] GPU_R32UI
RAYTRACE_GROUP_SIZE additional_info("eevee_shared", "eevee_gbuffer_data", "eevee_global_ubo", "eevee_sampling_data", "eevee_utility_texture", "eevee_hiz_data", "draw_view") .specialization_constant(Type RAYTRACE_GROUP_SIZE in_sh_0_tx in_sh_2_tx screen_normal_tx GPU_RGBA8
format
GLenum to_gl_target(eGPUTextureType type)
GLenum to_gl_internal_format(eGPUTextureFormat format)
GLenum to_gl_proxy(eGPUTextureType type)
GLenum to_gl_data_format(eGPUTextureFormat format)
GLenum swizzle_to_gl(const char swizzle)
GLenum channel_len_to_gl(int channel_len)
static GLenum to_gl(const GPUAttachmentType type)
#define min(a, b)
Definition sort.c:32
__int64 int64_t
Definition stdint.h:89