Blender  V2.93
gl_texture.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2020 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #pragma once
25 
26 #include "MEM_guardedalloc.h"
27 
28 #include "BLI_assert.h"
29 
30 #include "gpu_texture_private.hh"
31 
32 #include "glew-mx.h"
33 
34 struct GPUFrameBuffer;
35 
36 namespace blender {
37 namespace gpu {
38 
39 class GLTexture : public Texture {
40  friend class GLStateManager;
41  friend class GLFrameBuffer;
42 
43  private:
45  static GLuint samplers_[GPU_SAMPLER_MAX];
46 
48  GLenum target_ = -1;
50  GLuint tex_id_ = 0;
52  struct GPUFrameBuffer *framebuffer_ = NULL;
54  /* TODO(fclem): How do we ensure thread safety here? */
55  bool is_bound_ = false;
57  bool has_pixels_ = false;
58 
59  public:
60  GLTexture(const char *name);
61  ~GLTexture();
62 
63  void update_sub(
64  int mip, int offset[3], int extent[3], eGPUDataFormat type, const void *data) override;
65 
66  void generate_mipmap(void) override;
67  void copy_to(Texture *dst) override;
68  void clear(eGPUDataFormat format, const void *data) override;
69  void swizzle_set(const char swizzle_mask[4]) override;
70  void mip_range_set(int min, int max) override;
71  void *read(int mip, eGPUDataFormat type) override;
72 
73  void check_feedback_loop(void);
74 
75  /* TODO(fclem): Legacy. Should be removed at some point. */
76  uint gl_bindcode_get(void) const override;
77 
78  static void samplers_init(void);
79  static void samplers_free(void);
80  static void samplers_update(void);
81 
82  protected:
83  bool init_internal(void) override;
84  bool init_internal(GPUVertBuf *vbo) override;
85 
86  private:
87  bool proxy_check(int mip);
88  void ensure_mipmaps(int mip);
89  void update_sub_direct_state_access(
90  int mip, int offset[3], int extent[3], GLenum gl_format, GLenum gl_type, const void *data);
91  GPUFrameBuffer *framebuffer_get(void);
92 
93  MEM_CXX_CLASS_ALLOC_FUNCS("GLTexture")
94 };
95 
97 {
98  /* You can add any of the available type to this list
99  * For available types see GPU_texture.h */
100  switch (format) {
101  /* Formats texture & renderbuffer */
102  case GPU_RGBA8UI:
103  return GL_RGBA8UI;
104  case GPU_RGBA8I:
105  return GL_RGBA8I;
106  case GPU_RGBA8:
107  return GL_RGBA8;
108  case GPU_RGBA32UI:
109  return GL_RGBA32UI;
110  case GPU_RGBA32I:
111  return GL_RGBA32I;
112  case GPU_RGBA32F:
113  return GL_RGBA32F;
114  case GPU_RGBA16UI:
115  return GL_RGBA16UI;
116  case GPU_RGBA16I:
117  return GL_RGBA16I;
118  case GPU_RGBA16F:
119  return GL_RGBA16F;
120  case GPU_RGBA16:
121  return GL_RGBA16;
122  case GPU_RG8UI:
123  return GL_RG8UI;
124  case GPU_RG8I:
125  return GL_RG8I;
126  case GPU_RG8:
127  return GL_RG8;
128  case GPU_RG32UI:
129  return GL_RG32UI;
130  case GPU_RG32I:
131  return GL_RG32I;
132  case GPU_RG32F:
133  return GL_RG32F;
134  case GPU_RG16UI:
135  return GL_RG16UI;
136  case GPU_RG16I:
137  return GL_RG16I;
138  case GPU_RG16F:
139  return GL_RG16F;
140  case GPU_RG16:
141  return GL_RG16;
142  case GPU_R8UI:
143  return GL_R8UI;
144  case GPU_R8I:
145  return GL_R8I;
146  case GPU_R8:
147  return GL_R8;
148  case GPU_R32UI:
149  return GL_R32UI;
150  case GPU_R32I:
151  return GL_R32I;
152  case GPU_R32F:
153  return GL_R32F;
154  case GPU_R16UI:
155  return GL_R16UI;
156  case GPU_R16I:
157  return GL_R16I;
158  case GPU_R16F:
159  return GL_R16F;
160  case GPU_R16:
161  return GL_R16;
162  /* Special formats texture & renderbuffer */
163  case GPU_RGB10_A2:
164  return GL_RGB10_A2;
165  case GPU_R11F_G11F_B10F:
166  return GL_R11F_G11F_B10F;
168  return GL_DEPTH32F_STENCIL8;
170  return GL_DEPTH24_STENCIL8;
171  case GPU_SRGB8_A8:
172  return GL_SRGB8_ALPHA8;
173  /* Texture only format */
174  case GPU_RGB16F:
175  return GL_RGB16F;
176  /* Special formats texture only */
177  case GPU_SRGB8_A8_DXT1:
178  return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
179  case GPU_SRGB8_A8_DXT3:
180  return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
181  case GPU_SRGB8_A8_DXT5:
182  return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
183  case GPU_RGBA8_DXT1:
184  return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
185  case GPU_RGBA8_DXT3:
186  return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
187  case GPU_RGBA8_DXT5:
188  return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
189  /* Depth Formats */
191  return GL_DEPTH_COMPONENT32F;
193  return GL_DEPTH_COMPONENT24;
195  return GL_DEPTH_COMPONENT16;
196  default:
197  BLI_assert(!"Texture format incorrect or unsupported\n");
198  return 0;
199  }
200 }
201 
203 {
204  switch (type) {
205  case GPU_TEXTURE_1D:
206  return GL_TEXTURE_1D;
208  return GL_TEXTURE_1D_ARRAY;
209  case GPU_TEXTURE_2D:
210  return GL_TEXTURE_2D;
212  return GL_TEXTURE_2D_ARRAY;
213  case GPU_TEXTURE_3D:
214  return GL_TEXTURE_3D;
215  case GPU_TEXTURE_CUBE:
216  return GL_TEXTURE_CUBE_MAP;
218  return GL_TEXTURE_CUBE_MAP_ARRAY_ARB;
219  case GPU_TEXTURE_BUFFER:
220  return GL_TEXTURE_BUFFER;
221  default:
222  BLI_assert(0);
223  return GL_TEXTURE_1D;
224  }
225 }
226 
228 {
229  switch (type) {
230  case GPU_TEXTURE_1D:
231  return GL_PROXY_TEXTURE_1D;
233  return GL_PROXY_TEXTURE_1D_ARRAY;
234  case GPU_TEXTURE_2D:
235  return GL_PROXY_TEXTURE_2D;
237  return GL_PROXY_TEXTURE_2D_ARRAY;
238  case GPU_TEXTURE_3D:
239  return GL_PROXY_TEXTURE_3D;
240  case GPU_TEXTURE_CUBE:
241  return GL_PROXY_TEXTURE_CUBE_MAP;
243  return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY_ARB;
244  case GPU_TEXTURE_BUFFER:
245  default:
246  BLI_assert(0);
247  return GL_TEXTURE_1D;
248  }
249 }
250 
251 inline GLenum swizzle_to_gl(const char swizzle)
252 {
253  switch (swizzle) {
254  default:
255  case 'x':
256  case 'r':
257  return GL_RED;
258  case 'y':
259  case 'g':
260  return GL_GREEN;
261  case 'z':
262  case 'b':
263  return GL_BLUE;
264  case 'w':
265  case 'a':
266  return GL_ALPHA;
267  case '0':
268  return GL_ZERO;
269  case '1':
270  return GL_ONE;
271  }
272 }
273 
275 {
276  switch (format) {
277  case GPU_DATA_FLOAT:
278  return GL_FLOAT;
279  case GPU_DATA_INT:
280  return GL_INT;
281  case GPU_DATA_UINT:
282  return GL_UNSIGNED_INT;
283  case GPU_DATA_UBYTE:
284  return GL_UNSIGNED_BYTE;
285  case GPU_DATA_UINT_24_8:
286  return GL_UNSIGNED_INT_24_8;
288  return GL_UNSIGNED_INT_2_10_10_10_REV;
290  return GL_UNSIGNED_INT_10F_11F_11F_REV;
291  default:
292  BLI_assert(!"Unhandled data format");
293  return GL_FLOAT;
294  }
295 }
296 
297 /* Definitely not complete, edit according to the gl specification. */
299 {
300  /* You can add any of the available type to this list
301  * For available types see GPU_texture.h */
302  switch (format) {
303  case GPU_R8I:
304  case GPU_R8UI:
305  case GPU_R16I:
306  case GPU_R16UI:
307  case GPU_R32I:
308  case GPU_R32UI:
309  return GL_RED_INTEGER;
310  case GPU_RG8I:
311  case GPU_RG8UI:
312  case GPU_RG16I:
313  case GPU_RG16UI:
314  case GPU_RG32I:
315  case GPU_RG32UI:
316  return GL_RG_INTEGER;
317  case GPU_RGBA8I:
318  case GPU_RGBA8UI:
319  case GPU_RGBA16I:
320  case GPU_RGBA16UI:
321  case GPU_RGBA32I:
322  case GPU_RGBA32UI:
323  return GL_RGBA_INTEGER;
324  case GPU_R8:
325  case GPU_R16:
326  case GPU_R16F:
327  case GPU_R32F:
328  return GL_RED;
329  case GPU_RG8:
330  case GPU_RG16:
331  case GPU_RG16F:
332  case GPU_RG32F:
333  return GL_RG;
334  case GPU_R11F_G11F_B10F:
335  case GPU_RGB16F:
336  return GL_RGB;
337  case GPU_RGBA8:
338  case GPU_SRGB8_A8:
339  case GPU_RGBA16:
340  case GPU_RGBA16F:
341  case GPU_RGBA32F:
342  case GPU_RGB10_A2:
343  return GL_RGBA;
346  return GL_DEPTH_STENCIL;
350  return GL_DEPTH_COMPONENT;
351  case GPU_SRGB8_A8_DXT1:
352  return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
353  case GPU_SRGB8_A8_DXT3:
354  return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
355  case GPU_SRGB8_A8_DXT5:
356  return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
357  case GPU_RGBA8_DXT1:
358  return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
359  case GPU_RGBA8_DXT3:
360  return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
361  case GPU_RGBA8_DXT5:
362  return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
363  default:
364  BLI_assert(!"Texture format incorrect or unsupported\n");
365  return 0;
366  }
367 }
368 
369 /* Assume Unorm / Float target. Used with glReadPixels. */
370 inline GLenum channel_len_to_gl(int channel_len)
371 {
372  switch (channel_len) {
373  case 1:
374  return GL_RED;
375  case 2:
376  return GL_RG;
377  case 3:
378  return GL_RGB;
379  case 4:
380  return GL_RGBA;
381  default:
382  BLI_assert(!"Wrong number of texture channels");
383  return GL_RED;
384  }
385 }
386 
387 } // namespace gpu
388 } // namespace blender
#define BLI_assert(a)
Definition: BLI_assert.h:58
unsigned int uint
Definition: BLI_sys_types.h:83
struct GPUFrameBuffer GPUFrameBuffer
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
eGPUDataFormat
Definition: GPU_texture.h:171
@ GPU_DATA_UINT_24_8
Definition: GPU_texture.h:176
@ GPU_DATA_INT
Definition: GPU_texture.h:173
@ GPU_DATA_10_11_11_REV
Definition: GPU_texture.h:177
@ GPU_DATA_UBYTE
Definition: GPU_texture.h:175
@ GPU_DATA_UINT
Definition: GPU_texture.h:174
@ GPU_DATA_2_10_10_10_REV
Definition: GPU_texture.h:178
@ GPU_DATA_FLOAT
Definition: GPU_texture.h:172
static const int GPU_SAMPLER_MAX
Definition: GPU_texture.h:58
eGPUTextureFormat
Definition: GPU_texture.h:84
@ GPU_R16UI
Definition: GPU_texture.h:112
@ GPU_RG16F
Definition: GPU_texture.h:104
@ GPU_DEPTH32F_STENCIL8
Definition: GPU_texture.h:120
@ GPU_R32F
Definition: GPU_texture.h:111
@ GPU_R16I
Definition: GPU_texture.h:113
@ GPU_SRGB8_A8
Definition: GPU_texture.h:122
@ GPU_DEPTH24_STENCIL8
Definition: GPU_texture.h:121
@ GPU_RGB10_A2
Definition: GPU_texture.h:118
@ GPU_R32I
Definition: GPU_texture.h:110
@ GPU_RG8UI
Definition: GPU_texture.h:96
@ GPU_R16F
Definition: GPU_texture.h:114
@ GPU_SRGB8_A8_DXT5
Definition: GPU_texture.h:152
@ GPU_RG8I
Definition: GPU_texture.h:97
@ GPU_RG16I
Definition: GPU_texture.h:103
@ GPU_RG32UI
Definition: GPU_texture.h:99
@ GPU_RGBA32F
Definition: GPU_texture.h:91
@ GPU_RGBA16F
Definition: GPU_texture.h:94
@ GPU_RG8
Definition: GPU_texture.h:98
@ GPU_RG32I
Definition: GPU_texture.h:100
@ GPU_SRGB8_A8_DXT1
Definition: GPU_texture.h:150
@ GPU_RG16
Definition: GPU_texture.h:105
@ GPU_RGBA32UI
Definition: GPU_texture.h:89
@ GPU_R8I
Definition: GPU_texture.h:107
@ GPU_R16
Definition: GPU_texture.h:115
@ GPU_RG16UI
Definition: GPU_texture.h:102
@ GPU_RGBA8I
Definition: GPU_texture.h:87
@ GPU_RGBA8_DXT1
Definition: GPU_texture.h:153
@ GPU_RGBA8UI
Definition: GPU_texture.h:86
@ GPU_RGBA16UI
Definition: GPU_texture.h:92
@ GPU_RGBA16I
Definition: GPU_texture.h:93
@ GPU_R8UI
Definition: GPU_texture.h:106
@ GPU_RGBA16
Definition: GPU_texture.h:95
@ GPU_SRGB8_A8_DXT3
Definition: GPU_texture.h:151
@ GPU_RGBA8_DXT3
Definition: GPU_texture.h:154
@ GPU_RG32F
Definition: GPU_texture.h:101
@ GPU_R8
Definition: GPU_texture.h:108
@ GPU_DEPTH_COMPONENT24
Definition: GPU_texture.h:167
@ GPU_RGB16F
Definition: GPU_texture.h:128
@ GPU_R32UI
Definition: GPU_texture.h:109
@ GPU_RGBA32I
Definition: GPU_texture.h:90
@ GPU_RGBA8_DXT5
Definition: GPU_texture.h:155
@ GPU_DEPTH_COMPONENT32F
Definition: GPU_texture.h:166
@ GPU_DEPTH_COMPONENT16
Definition: GPU_texture.h:168
@ GPU_R11F_G11F_B10F
Definition: GPU_texture.h:119
@ GPU_RGBA8
Definition: GPU_texture.h:88
struct GPUVertBuf GPUVertBuf
Read Guarded memory(de)allocation.
GLTexture(const char *name)
Definition: gl_texture.cc:45
static void samplers_free(void)
Definition: gl_texture.cc:560
void copy_to(Texture *dst) override
Definition: gl_texture.cc:358
void mip_range_set(int min, int max) override
Definition: gl_texture.cc:447
void check_feedback_loop(void)
Definition: gl_texture.cc:675
uint gl_bindcode_get(void) const override
Definition: gl_texture.cc:705
void generate_mipmap(void) override
Definition: gl_texture.cc:303
void * read(int mip, eGPUDataFormat type) override
Definition: gl_texture.cc:386
static void samplers_update(void)
Definition: gl_texture.cc:541
bool init_internal(void) override
Definition: gl_texture.cc:66
static void samplers_init(void)
Definition: gl_texture.cc:485
void update_sub(int mip, int offset[3], int extent[3], eGPUDataFormat type, const void *data) override
Definition: gl_texture.cc:232
void clear(eGPUDataFormat format, const void *data) override
Definition: gl_texture.cc:334
void swizzle_set(const char swizzle_mask[4]) override
Definition: gl_texture.cc:432
format
Definition: logImageCore.h:47
GLenum to_gl_target(eGPUTextureType type)
Definition: gl_texture.hh:202
GLenum to_gl_internal_format(eGPUTextureFormat format)
Definition: gl_texture.hh:96
GLenum to_gl_proxy(eGPUTextureType type)
Definition: gl_texture.hh:227
GLenum to_gl_data_format(eGPUTextureFormat format)
Definition: gl_texture.hh:298
GLenum swizzle_to_gl(const char swizzle)
Definition: gl_texture.hh:251
GLenum channel_len_to_gl(int channel_len)
Definition: gl_texture.hh:370
static GLenum to_gl(const GPUAttachmentType type)
#define min(a, b)
Definition: sort.c:51
float max