Blender V5.0
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.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
88 static void samplers_init();
89
93 static void samplers_free();
94
101 static void samplers_update();
102
107 static GLuint get_sampler(const GPUSamplerState &sampler_state);
108
109 protected:
111 bool init_internal() override;
113 bool init_internal(VertBuf *vbo) override;
115 bool init_internal(gpu::Texture *src,
116 int mip_offset,
117 int layer_offset,
118 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;
141 size_t get_size() override;
142
143 MEM_CXX_CLASS_ALLOC_FUNCS("GLPixelBuffer")
144};
145
147{
148#define CASE(a, b, c, blender_enum, d, e, f, gl_pixel_enum, h) \
149 case TextureFormat::blender_enum: \
150 return GL_##gl_pixel_enum;
151
152 switch (format) {
155 break;
156 }
157#undef CASE
158 BLI_assert_msg(0, "Texture format incorrect or unsupported");
159 return 0;
160}
161
162inline GLenum to_gl_target(GPUTextureType type)
163{
164 switch (type) {
165 case GPU_TEXTURE_1D:
166 return GL_TEXTURE_1D;
168 return GL_TEXTURE_1D_ARRAY;
169 case GPU_TEXTURE_2D:
170 return GL_TEXTURE_2D;
172 return GL_TEXTURE_2D_ARRAY;
173 case GPU_TEXTURE_3D:
174 return GL_TEXTURE_3D;
175 case GPU_TEXTURE_CUBE:
176 return GL_TEXTURE_CUBE_MAP;
178 return GL_TEXTURE_CUBE_MAP_ARRAY_ARB;
180 return GL_TEXTURE_BUFFER;
181 default:
182 BLI_assert(0);
183 return GL_TEXTURE_1D;
184 }
185}
186
187inline GLenum to_gl_proxy(GPUTextureType type)
188{
189 switch (type) {
190 case GPU_TEXTURE_1D:
191 return GL_PROXY_TEXTURE_1D;
193 return GL_PROXY_TEXTURE_1D_ARRAY;
194 case GPU_TEXTURE_2D:
195 return GL_PROXY_TEXTURE_2D;
197 return GL_PROXY_TEXTURE_2D_ARRAY;
198 case GPU_TEXTURE_3D:
199 return GL_PROXY_TEXTURE_3D;
200 case GPU_TEXTURE_CUBE:
201 return GL_PROXY_TEXTURE_CUBE_MAP;
203 return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY_ARB;
205 default:
206 BLI_assert(0);
207 return GL_TEXTURE_1D;
208 }
209}
210
211inline GLenum swizzle_to_gl(const char swizzle)
212{
213 switch (swizzle) {
214 default:
215 case 'x':
216 case 'r':
217 return GL_RED;
218 case 'y':
219 case 'g':
220 return GL_GREEN;
221 case 'z':
222 case 'b':
223 return GL_BLUE;
224 case 'w':
225 case 'a':
226 return GL_ALPHA;
227 case '0':
228 return GL_ZERO;
229 case '1':
230 return GL_ONE;
231 }
232}
233
235{
236 switch (format) {
237 case GPU_DATA_FLOAT:
238 return GL_FLOAT;
239 case GPU_DATA_INT:
240 return GL_INT;
241 case GPU_DATA_UINT:
242 return GL_UNSIGNED_INT;
243 case GPU_DATA_UBYTE:
244 return GL_UNSIGNED_BYTE;
246 return GL_UNSIGNED_INT_24_8;
248 return GL_UNSIGNED_INT_2_10_10_10_REV;
250 return GL_UNSIGNED_INT_10F_11F_11F_REV;
252 return GL_HALF_FLOAT;
253 default:
254 BLI_assert_msg(0, "Unhandled data format");
255 return GL_FLOAT;
256 }
257}
258
260{
261 switch (format) {
262 /* Texture & Render-Buffer Formats. */
263 case TextureFormat::UNORM_8_8_8_8:
264 case TextureFormat::SFLOAT_32_32_32_32:
265 case TextureFormat::SFLOAT_16_16_16_16:
266 case TextureFormat::UNORM_16_16_16_16:
267 return GL_RGBA;
268 case TextureFormat::UINT_8_8_8_8:
269 case TextureFormat::SINT_8_8_8_8:
270 case TextureFormat::SINT_32_32_32_32:
271 case TextureFormat::UINT_32_32_32_32:
272 case TextureFormat::UINT_16_16_16_16:
273 case TextureFormat::SINT_16_16_16_16:
274 return GL_RGBA_INTEGER;
275 case TextureFormat::UNORM_8_8:
276 case TextureFormat::SFLOAT_32_32:
277 case TextureFormat::SFLOAT_16_16:
278 case TextureFormat::UNORM_16_16:
279 return GL_RG;
280 case TextureFormat::UINT_8_8:
281 case TextureFormat::SINT_8_8:
282 case TextureFormat::UINT_32_32:
283 case TextureFormat::SINT_32_32:
284 case TextureFormat::SINT_16_16:
285 case TextureFormat::UINT_16_16:
286 return GL_RG_INTEGER;
287 case TextureFormat::UNORM_8:
288 case TextureFormat::SFLOAT_32:
289 case TextureFormat::SFLOAT_16:
290 case TextureFormat::UNORM_16:
291 return GL_RED;
292 case TextureFormat::UINT_8:
293 case TextureFormat::SINT_8:
294 case TextureFormat::UINT_32:
295 case TextureFormat::SINT_32:
296 case TextureFormat::UINT_16:
297 case TextureFormat::SINT_16:
298 return GL_RED_INTEGER;
299 /* Special formats texture & render-buffer. */
300 case TextureFormat::UINT_10_10_10_2:
301 case TextureFormat::UNORM_10_10_10_2:
302 case TextureFormat::SRGBA_8_8_8_8:
303 return GL_RGBA;
304 case TextureFormat::UFLOAT_11_11_10:
305 return GL_RGB;
306 case TextureFormat::SFLOAT_32_DEPTH_UINT_8:
307 return GL_DEPTH_STENCIL;
308 /* Texture only formats. */
309 case TextureFormat::SNORM_16_16_16_16:
310 case TextureFormat::SNORM_8_8_8_8:
311 return GL_RGBA;
312 case TextureFormat::SFLOAT_16_16_16:
313 case TextureFormat::SFLOAT_32_32_32:
314 case TextureFormat::SINT_32_32_32:
315 case TextureFormat::UINT_32_32_32:
316 case TextureFormat::SNORM_16_16_16:
317 case TextureFormat::SINT_16_16_16:
318 case TextureFormat::UINT_16_16_16:
319 case TextureFormat::UNORM_16_16_16:
320 case TextureFormat::SNORM_8_8_8:
321 case TextureFormat::UNORM_8_8_8:
322 case TextureFormat::SINT_8_8_8:
323 case TextureFormat::UINT_8_8_8:
324 return GL_RGB;
325 case TextureFormat::SNORM_16_16:
326 case TextureFormat::SNORM_8_8:
327 return GL_RG;
328 case TextureFormat::SNORM_16:
329 case TextureFormat::SNORM_8:
330 return GL_RED;
331 /* Special formats, texture only. */
332 case TextureFormat::SRGB_DXT1:
333 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
334 case TextureFormat::SRGB_DXT3:
335 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
336 case TextureFormat::SRGB_DXT5:
337 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
338 case TextureFormat::SNORM_DXT1:
339 return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
340 case TextureFormat::SNORM_DXT3:
341 return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
342 case TextureFormat::SNORM_DXT5:
343 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
344 case TextureFormat::SRGBA_8_8_8:
345 case TextureFormat::UFLOAT_9_9_9_EXP_5:
346 return GL_RGB;
347 /* Depth Formats. */
348 case TextureFormat::SFLOAT_32_DEPTH:
349 case TextureFormat::UNORM_16_DEPTH:
350 return GL_DEPTH_COMPONENT;
352 break;
353 }
354 BLI_assert_msg(0, "Texture format incorrect or unsupported\n");
355 return 0;
356}
357
361inline GLenum channel_len_to_gl(int channel_len)
362{
363 switch (channel_len) {
364 case 1:
365 return GL_RED;
366 case 2:
367 return GL_RG;
368 case 3:
369 return GL_RGB;
370 case 4:
371 return GL_RGBA;
372 default:
373 BLI_assert_msg(0, "Wrong number of texture channels");
374 return GL_RED;
375 }
376}
377
378} // namespace gpu
379} // namespace blender
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
#define CASE(a, size, c, blender_enum, d, e, f, g, h)
#define GPU_TEXTURE_FORMAT_EXPAND(impl)
static const int GPU_SAMPLER_FILTERING_TYPES_COUNT
eGPUDataFormat
@ GPU_DATA_HALF_FLOAT
@ GPU_DATA_INT
@ GPU_DATA_10_11_11_REV
@ GPU_DATA_UBYTE
@ GPU_DATA_UINT
@ GPU_DATA_UINT_24_8_DEPRECATED
@ GPU_DATA_2_10_10_10_REV
@ GPU_DATA_FLOAT
#define GPU_SAMPLER_CUSTOM_TYPES_COUNT
#define GPU_SAMPLER_EXTEND_MODES_COUNT
Read Guarded memory(de)allocation.
BMesh const char void * data
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
GPUPixelBufferNativeHandle get_native_handle() override
size_t get_size() override
GLTexture(const char *name)
Definition gl_texture.cc:34
void copy_to(Texture *dst) override
void mip_range_set(int min, int max) override
friend class GLFrameBuffer
Definition gl_texture.hh:22
static void samplers_init()
static GLuint get_sampler(const GPUSamplerState &sampler_state)
static void samplers_update()
friend class GLStateManager
Definition gl_texture.hh:21
static void samplers_free()
bool init_internal() override
Definition gl_texture.cc:55
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
Texture(const char *name)
format
GLenum to_gl_internal_format(TextureFormat format)
GLenum to_gl_target(GPUTextureType type)
GLenum to_gl_data_format(TextureFormat format)
GLenum to_gl_proxy(GPUTextureType type)
GLenum swizzle_to_gl(const char swizzle)
GLenum channel_len_to_gl(int channel_len)
static GLenum to_gl(const GPUAttachmentType type)
const char * name
#define min(a, b)
Definition sort.cc:36
max
Definition text_draw.cc:251