Blender V5.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
8
9#pragma once
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_assert.hh"
14
16
17namespace blender::gpu {
18
19class GLTexturePool;
20
21class GLTexture : public Texture {
22 friend class GLStateManager;
23 friend class GLFrameBuffer;
24 friend class GLTexturePool;
25
26 private:
33 static GLuint samplers_state_cache_[GPU_SAMPLER_EXTEND_MODES_COUNT]
35 [GPU_SAMPLER_FILTERING_TYPES_COUNT];
36 static const int samplers_state_cache_count_ = GPU_SAMPLER_EXTEND_MODES_COUNT *
38 GPU_SAMPLER_FILTERING_TYPES_COUNT;
43 static GLuint custom_samplers_state_cache_[GPU_SAMPLER_CUSTOM_TYPES_COUNT];
44
46 GLenum target_ = -1;
48 GLuint tex_id_ = 0;
50 FrameBuffer *framebuffer_ = nullptr;
51 /* Mip level currently bound to the framebuffer. */
52 int framebuffer_mip_ = -1;
54 /* TODO(fclem): How do we ensure thread safety here? */
55 bool is_bound_ = false;
57 bool is_bound_image_ = false;
59 bool has_pixels_ = false;
60
61 public:
62 GLTexture(const char *name);
63 ~GLTexture();
64
65 void update_sub(int mip,
66 int offset[3],
67 int extent[3],
68 eGPUDataFormat type,
69 const void *data,
70 const uint unpack_row_length = 0) override;
71 void update_sub(int offset[3],
72 int extent[3],
73 eGPUDataFormat format,
74 GPUPixelBuffer *pixbuf) override;
75
82 void generate_mipmap() override;
83 void copy_to(Texture *dst, IndexRange mip_levels) override;
84 void clear(const double4 data) override;
85 void swizzle_set(const char swizzle_mask[4]) override;
86 void read(int mip, eGPUDataFormat type, void *data) override;
87
89
95 static void samplers_init();
96
100 static void samplers_free();
101
106 static GLuint get_sampler(const GPUSamplerState &sampler_state);
107
108 protected:
110 bool init_internal() override;
112 bool init_internal(VertBuf *vbo) override;
114 bool init_internal(gpu::Texture *src, bool use_stencil) override;
115
116 private:
117 bool proxy_check(int mip);
118 void mip_range_set(int min, int max);
119 void stencil_texture_mode_set(bool use_stencil);
120 void update_sub_direct_state_access(
121 int mip, int offset[3], int extent[3], GLenum gl_format, GLenum gl_type, const void *data);
122 FrameBuffer *framebuffer_get(int mip);
123
124 MEM_CXX_CLASS_ALLOC_FUNCS("GLTexture")
125};
126
128 private:
129 GLuint gl_id_ = 0;
130
131 public:
132 GLPixelBuffer(size_t size);
134
135 void *map() override;
136 void unmap() override;
138 size_t get_size() override;
139
140 MEM_CXX_CLASS_ALLOC_FUNCS("GLPixelBuffer")
141};
142
144{
145#define CASE(a, b, c, blender_enum, d, e, f, gl_pixel_enum, h) \
146 case TextureFormat::blender_enum: \
147 return GL_##gl_pixel_enum;
148
149 switch (format) {
152 break;
153 }
154#undef CASE
155 BLI_assert_msg(0, "Texture format incorrect or unsupported");
156 return 0;
157}
158
159inline GLenum to_gl_target(GPUTextureType type)
160{
161 switch (type) {
162 case GPU_TEXTURE_1D:
163 return GL_TEXTURE_1D;
165 return GL_TEXTURE_1D_ARRAY;
166 case GPU_TEXTURE_2D:
167 return GL_TEXTURE_2D;
169 return GL_TEXTURE_2D_ARRAY;
170 case GPU_TEXTURE_3D:
171 return GL_TEXTURE_3D;
172 case GPU_TEXTURE_CUBE:
173 return GL_TEXTURE_CUBE_MAP;
175 return GL_TEXTURE_CUBE_MAP_ARRAY_ARB;
177 return GL_TEXTURE_BUFFER;
178 default:
179 BLI_assert(0);
180 return GL_TEXTURE_1D;
181 }
182}
183
184inline GLenum to_gl_proxy(GPUTextureType type)
185{
186 switch (type) {
187 case GPU_TEXTURE_1D:
188 return GL_PROXY_TEXTURE_1D;
190 return GL_PROXY_TEXTURE_1D_ARRAY;
191 case GPU_TEXTURE_2D:
192 return GL_PROXY_TEXTURE_2D;
194 return GL_PROXY_TEXTURE_2D_ARRAY;
195 case GPU_TEXTURE_3D:
196 return GL_PROXY_TEXTURE_3D;
197 case GPU_TEXTURE_CUBE:
198 return GL_PROXY_TEXTURE_CUBE_MAP;
200 return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY_ARB;
202 default:
203 BLI_assert(0);
204 return GL_TEXTURE_1D;
205 }
206}
207
208inline GLenum swizzle_to_gl(const char swizzle)
209{
210 switch (swizzle) {
211 default:
212 case 'x':
213 case 'r':
214 return GL_RED;
215 case 'y':
216 case 'g':
217 return GL_GREEN;
218 case 'z':
219 case 'b':
220 return GL_BLUE;
221 case 'w':
222 case 'a':
223 return GL_ALPHA;
224 case '0':
225 return GL_ZERO;
226 case '1':
227 return GL_ONE;
228 }
229}
230
231inline GLenum to_gl(eGPUDataFormat format)
232{
233 switch (format) {
234 case GPU_DATA_FLOAT:
235 return GL_FLOAT;
236 case GPU_DATA_INT:
237 return GL_INT;
238 case GPU_DATA_UINT:
239 return GL_UNSIGNED_INT;
240 case GPU_DATA_UBYTE:
241 return GL_UNSIGNED_BYTE;
243 return GL_UNSIGNED_INT_24_8;
245 return GL_UNSIGNED_INT_2_10_10_10_REV;
247 return GL_UNSIGNED_INT_10F_11F_11F_REV;
249 return GL_HALF_FLOAT;
250 default:
251 BLI_assert_msg(0, "Unhandled data format");
252 return GL_FLOAT;
253 }
254}
255
256inline GLenum to_gl_data_format(TextureFormat format)
257{
258 switch (format) {
259 /* Texture & Render-Buffer Formats. */
260 case TextureFormat::UNORM_8_8_8_8:
261 case TextureFormat::SFLOAT_32_32_32_32:
262 case TextureFormat::SFLOAT_16_16_16_16:
263 case TextureFormat::UNORM_16_16_16_16:
264 return GL_RGBA;
265 case TextureFormat::UINT_8_8_8_8:
266 case TextureFormat::SINT_8_8_8_8:
267 case TextureFormat::SINT_32_32_32_32:
268 case TextureFormat::UINT_32_32_32_32:
269 case TextureFormat::UINT_16_16_16_16:
270 case TextureFormat::SINT_16_16_16_16:
271 return GL_RGBA_INTEGER;
272 case TextureFormat::UNORM_8_8:
273 case TextureFormat::SFLOAT_32_32:
274 case TextureFormat::SFLOAT_16_16:
275 case TextureFormat::UNORM_16_16:
276 return GL_RG;
277 case TextureFormat::UINT_8_8:
278 case TextureFormat::SINT_8_8:
279 case TextureFormat::UINT_32_32:
280 case TextureFormat::SINT_32_32:
281 case TextureFormat::SINT_16_16:
282 case TextureFormat::UINT_16_16:
283 return GL_RG_INTEGER;
284 case TextureFormat::UNORM_8:
285 case TextureFormat::SFLOAT_32:
286 case TextureFormat::SFLOAT_16:
287 case TextureFormat::UNORM_16:
288 return GL_RED;
289 case TextureFormat::UINT_8:
290 case TextureFormat::SINT_8:
291 case TextureFormat::UINT_32:
292 case TextureFormat::SINT_32:
293 case TextureFormat::UINT_16:
294 case TextureFormat::SINT_16:
295 return GL_RED_INTEGER;
296 /* Special formats texture & render-buffer. */
297 case TextureFormat::UINT_10_10_10_2:
298 case TextureFormat::UNORM_10_10_10_2:
299 case TextureFormat::SRGBA_8_8_8_8:
300 return GL_RGBA;
301 case TextureFormat::UFLOAT_11_11_10:
302 return GL_RGB;
303 case TextureFormat::SFLOAT_32_DEPTH_UINT_8:
304 return GL_DEPTH_STENCIL;
305 /* Texture only formats. */
306 case TextureFormat::SNORM_16_16_16_16:
307 case TextureFormat::SNORM_8_8_8_8:
308 return GL_RGBA;
309 case TextureFormat::SFLOAT_16_16_16:
310 case TextureFormat::SFLOAT_32_32_32:
311 case TextureFormat::SINT_32_32_32:
312 case TextureFormat::UINT_32_32_32:
313 case TextureFormat::SNORM_16_16_16:
314 case TextureFormat::SINT_16_16_16:
315 case TextureFormat::UINT_16_16_16:
316 case TextureFormat::UNORM_16_16_16:
317 case TextureFormat::SNORM_8_8_8:
318 case TextureFormat::UNORM_8_8_8:
319 case TextureFormat::SINT_8_8_8:
320 case TextureFormat::UINT_8_8_8:
321 return GL_RGB;
322 case TextureFormat::SNORM_16_16:
323 case TextureFormat::SNORM_8_8:
324 return GL_RG;
325 case TextureFormat::SNORM_16:
326 case TextureFormat::SNORM_8:
327 return GL_RED;
328 /* Special formats, texture only. */
329 case TextureFormat::SRGB_DXT1:
330 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
331 case TextureFormat::SRGB_DXT3:
332 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
333 case TextureFormat::SRGB_DXT5:
334 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
335 case TextureFormat::SNORM_DXT1:
336 return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
337 case TextureFormat::SNORM_DXT3:
338 return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
339 case TextureFormat::SNORM_DXT5:
340 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
341 case TextureFormat::SRGBA_8_8_8:
342 case TextureFormat::UFLOAT_9_9_9_EXP_5:
343 return GL_RGB;
344 /* Depth Formats. */
345 case TextureFormat::SFLOAT_32_DEPTH:
346 case TextureFormat::UNORM_16_DEPTH:
347 return GL_DEPTH_COMPONENT;
349 break;
350 }
351 BLI_assert_msg(0, "Texture format incorrect or unsupported\n");
352 return 0;
353}
354
358inline GLenum channel_len_to_gl(int channel_len)
359{
360 switch (channel_len) {
361 case 1:
362 return GL_RED;
363 case 2:
364 return GL_RG;
365 case 3:
366 return GL_RGB;
367 case 4:
368 return GL_RGBA;
369 default:
370 BLI_assert_msg(0, "Wrong number of texture channels");
371 return GL_RED;
372 }
373}
374
376{
377 return static_cast<GLTexture *>(tex);
378}
379
381{
382 return static_cast<Texture *>(texture);
383}
384
385} // namespace blender::gpu
#define CASE(a, size, c, blender_enum, d, e, f, g, h)
#define GPU_TEXTURE_FORMAT_EXPAND(impl)
#define GPU_SAMPLER_CUSTOM_TYPES_COUNT
#define GPU_SAMPLER_EXTEND_MODES_COUNT
Read Blender memory allocation and freeing.
#define BLI_INLINE
#define BLI_assert_msg(a, msg)
btScalar max
GPUPixelBufferNativeHandle get_native_handle() override
size_t get_size() override
GLTexture(const char *name)
Definition gl_texture.cc:37
void update_sub(int mip, int offset[3], int extent[3], eGPUDataFormat type, const void *data, const uint unpack_row_length=0) override
friend class GLFrameBuffer
Definition gl_texture.hh:23
void read(int mip, eGPUDataFormat type, void *data) override
static void samplers_init()
static GLuint get_sampler(const GPUSamplerState &sampler_state)
friend class GLTexturePool
Definition gl_texture.hh:24
friend class GLStateManager
Definition gl_texture.hh:22
void clear(const double4 data) override
static void samplers_free()
bool init_internal() override
Definition gl_texture.cc:58
void copy_to(Texture *dst, IndexRange mip_levels) override
void generate_mipmap() override
void swizzle_set(const char swizzle_mask[4]) override
Texture(const char *name)
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
GLenum to_gl_internal_format(TextureFormat format)
GLenum to_gl_target(GPUTextureType type)
GLenum to_gl_data_format(TextureFormat format)
static GPUContext * wrap(Context *ctx)
GLenum to_gl_proxy(GPUTextureType type)
BLI_INLINE MTLTexture * unwrap(Texture *tex)
GLenum swizzle_to_gl(const char swizzle)
GLenum channel_len_to_gl(int channel_len)
GLenum to_gl(eGPUDataFormat format)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned int uint
Definition BKE_text.h:55
@ GPU_DATA_2_10_10_10_REV
@ GPU_DATA_HALF_FLOAT
@ GPU_DATA_10_11_11_REV
@ GPU_DATA_UINT_24_8_DEPRECATED
BMesh const char void * data
VecBase< double, 4 > double4
#define min(a, b)
Definition sort.cc:38