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) 2005 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup gpu
22  */
23 
24 #pragma once
25 
26 #include "BLI_utildefines.h"
27 
28 #include "GPU_state.h"
29 
30 struct GPUVertBuf;
31 struct ImBuf;
32 struct Image;
33 struct ImageUser;
34 struct MovieClip;
35 struct MovieClipUser;
36 struct PreviewImage;
37 
38 struct GPUFrameBuffer;
39 
40 /** Opaque type hiding blender::gpu::Texture. */
41 typedef struct GPUTexture GPUTexture;
42 
43 /* GPU Samplers state
44  * - Specify the sampler state to bind a texture with.
45  * - Internally used by textures.
46  * - All states are created at startup to avoid runtime costs.
47  */
48 typedef enum eGPUSamplerState {
49   GPU_SAMPLER_DEFAULT = 0,
50   GPU_SAMPLER_FILTER = (1 << 0),
51   GPU_SAMPLER_MIPMAP = (1 << 1),
52   GPU_SAMPLER_REPEAT_S = (1 << 2),
53   GPU_SAMPLER_REPEAT_T = (1 << 3),
54   GPU_SAMPLER_REPEAT_R = (1 << 4),
55   GPU_SAMPLER_CLAMP_BORDER = (1 << 5), /* Clamp to border color instead of border texel. */
56   GPU_SAMPLER_COMPARE = (1 << 6),
57   GPU_SAMPLER_ANISO = (1 << 7),
58   GPU_SAMPLER_ICON = (1 << 8),
59 
60   GPU_SAMPLER_REPEAT = (GPU_SAMPLER_REPEAT_S | GPU_SAMPLER_REPEAT_T | GPU_SAMPLER_REPEAT_R),
61 } eGPUSamplerState;
62 
63 /* `GPU_SAMPLER_MAX` is not a valid enum value, but only a limit.
64  * It also creates a bad mask for the `NOT` operator in `ENUM_OPERATORS`.
65  */
66 static const int GPU_SAMPLER_MAX = (GPU_SAMPLER_ICON + 1);
ENUM_OPERATORS(eGPUSamplerState,GPU_SAMPLER_ICON)67 ENUM_OPERATORS(eGPUSamplerState, GPU_SAMPLER_ICON)
68 
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72 
73 void GPU_samplers_update(void);
74 
75 /* GPU Texture
76  * - always returns unsigned char RGBA textures
77  * - if texture with non square dimensions is created, depending on the
78  *   graphics card capabilities the texture may actually be stored in a
79  *   larger texture with power of two dimensions.
80  * - can use reference counting:
81  *   - reference counter after GPU_texture_create is 1
82  *   - GPU_texture_ref increases by one
83  *   - GPU_texture_free decreases by one, and frees if 0
84  * - if created with from_blender, will not free the texture
85  */
86 
87 /* Wrapper to supported OpenGL/Vulkan texture internal storage
88  * If you need a type just uncomment it. Be aware that some formats
89  * are not supported by renderbuffers. All of the following formats
90  * are part of the OpenGL 3.3 core
91  * specification. */
92 typedef enum eGPUTextureFormat {
93   /* Formats texture & renderbuffer */
94   GPU_RGBA8UI,
95   GPU_RGBA8I,
96   GPU_RGBA8,
97   GPU_RGBA32UI,
98   GPU_RGBA32I,
99   GPU_RGBA32F,
100   GPU_RGBA16UI,
101   GPU_RGBA16I,
102   GPU_RGBA16F,
103   GPU_RGBA16,
104   GPU_RG8UI,
105   GPU_RG8I,
106   GPU_RG8,
107   GPU_RG32UI,
108   GPU_RG32I,
109   GPU_RG32F,
110   GPU_RG16UI,
111   GPU_RG16I,
112   GPU_RG16F,
113   GPU_RG16,
114   GPU_R8UI,
115   GPU_R8I,
116   GPU_R8,
117   GPU_R32UI,
118   GPU_R32I,
119   GPU_R32F,
120   GPU_R16UI,
121   GPU_R16I,
122   GPU_R16F,
123   GPU_R16, /* Max texture buffer format. */
124 
125 /* Special formats texture & renderbuffer */
126 #if 0
127   GPU_RGB10_A2,
128   GPU_RGB10_A2UI,
129 #endif
130   GPU_R11F_G11F_B10F,
131   GPU_DEPTH32F_STENCIL8,
132   GPU_DEPTH24_STENCIL8,
133   GPU_SRGB8_A8,
134 
135   /* Texture only format */
136   GPU_RGB16F,
137 #if 0
138   GPU_RGBA16_SNORM,
139   GPU_RGBA8_SNORM,
140   GPU_RGB32F,
141   GPU_RGB32I,
142   GPU_RGB32UI,
143   GPU_RGB16_SNORM,
144   GPU_RGB16I,
145   GPU_RGB16UI,
146   GPU_RGB16,
147   GPU_RGB8_SNORM,
148   GPU_RGB8,
149   GPU_RGB8I,
150   GPU_RGB8UI,
151   GPU_RG16_SNORM,
152   GPU_RG8_SNORM,
153   GPU_R16_SNORM,
154   GPU_R8_SNORM,
155 #endif
156 
157   /* Special formats texture only */
158   GPU_SRGB8_A8_DXT1,
159   GPU_SRGB8_A8_DXT3,
160   GPU_SRGB8_A8_DXT5,
161   GPU_RGBA8_DXT1,
162   GPU_RGBA8_DXT3,
163   GPU_RGBA8_DXT5,
164 #if 0
165   GPU_SRGB8,
166   GPU_RGB9_E5,
167   GPU_COMPRESSED_RG_RGTC2,
168   GPU_COMPRESSED_SIGNED_RG_RGTC2,
169   GPU_COMPRESSED_RED_RGTC1,
170   GPU_COMPRESSED_SIGNED_RED_RGTC1,
171 #endif
172 
173   /* Depth Formats */
174   GPU_DEPTH_COMPONENT32F,
175   GPU_DEPTH_COMPONENT24,
176   GPU_DEPTH_COMPONENT16,
177 } eGPUTextureFormat;
178 
179 typedef enum eGPUDataFormat {
180   GPU_DATA_FLOAT,
181   GPU_DATA_INT,
182   GPU_DATA_UNSIGNED_INT,
183   GPU_DATA_UNSIGNED_BYTE,
184   GPU_DATA_UNSIGNED_INT_24_8,
185   GPU_DATA_10_11_11_REV,
186 } eGPUDataFormat;
187 
188 unsigned int GPU_texture_memory_usage_get(void);
189 
190 /**
191  * \note \a data is expected to be float. If the \a format is not compatible with float data or if
192  * the data is not in float format, use GPU_texture_update to upload the data with the right data
193  * format.
194  * \a mips is the number of mip level to allocate. It must be >= 1.
195  */
196 GPUTexture *GPU_texture_create_1d(
197     const char *name, int w, int mips, eGPUTextureFormat format, const float *data);
198 GPUTexture *GPU_texture_create_1d_array(
199     const char *name, int w, int h, int mips, eGPUTextureFormat format, const float *data);
200 GPUTexture *GPU_texture_create_2d(
201     const char *name, int w, int h, int mips, eGPUTextureFormat format, const float *data);
202 GPUTexture *GPU_texture_create_2d_array(
203     const char *name, int w, int h, int d, int mips, eGPUTextureFormat format, const float *data);
204 GPUTexture *GPU_texture_create_3d(const char *name,
205                                   int w,
206                                   int h,
207                                   int d,
208                                   int mips,
209                                   eGPUTextureFormat texture_format,
210                                   eGPUDataFormat data_format,
211                                   const void *data);
212 GPUTexture *GPU_texture_create_cube(
213     const char *name, int w, int mips, eGPUTextureFormat format, const float *data);
214 GPUTexture *GPU_texture_create_cube_array(
215     const char *name, int w, int d, int mips, eGPUTextureFormat format, const float *data);
216 
217 /* Special textures. */
218 GPUTexture *GPU_texture_create_from_vertbuf(const char *name, struct GPUVertBuf *vert);
219 /**
220  * \a data should hold all the data for all mipmaps.
221  */
222 GPUTexture *GPU_texture_create_compressed_2d(
223     const char *name, int w, int h, int miplen, eGPUTextureFormat format, const void *data);
224 GPUTexture *GPU_texture_create_error(int dimension, bool array);
225 
226 void GPU_texture_update_mipmap(GPUTexture *tex,
227                                int miplvl,
228                                eGPUDataFormat gpu_data_format,
229                                const void *pixels);
230 
231 void GPU_texture_update(GPUTexture *tex, eGPUDataFormat data_format, const void *data);
232 void GPU_texture_update_sub(GPUTexture *tex,
233                             eGPUDataFormat data_format,
234                             const void *pixels,
235                             int offset_x,
236                             int offset_y,
237                             int offset_z,
238                             int width,
239                             int height,
240                             int depth);
241 void GPU_unpack_row_length_set(uint len);
242 
243 void *GPU_texture_read(GPUTexture *tex, eGPUDataFormat data_format, int miplvl);
244 void GPU_texture_clear(GPUTexture *tex, eGPUDataFormat data_format, const void *data);
245 
246 void GPU_texture_free(GPUTexture *tex);
247 
248 void GPU_texture_ref(GPUTexture *tex);
249 void GPU_texture_bind(GPUTexture *tex, int unit);
250 void GPU_texture_bind_ex(GPUTexture *tex, eGPUSamplerState state, int unit, const bool set_number);
251 void GPU_texture_unbind(GPUTexture *tex);
252 void GPU_texture_unbind_all(void);
253 
254 void GPU_texture_image_bind(GPUTexture *tex, int unit);
255 void GPU_texture_image_unbind(GPUTexture *tex);
256 void GPU_texture_image_unbind_all(void);
257 
258 void GPU_texture_copy(GPUTexture *dst, GPUTexture *src);
259 
260 void GPU_texture_generate_mipmap(GPUTexture *tex);
261 void GPU_texture_anisotropic_filter(GPUTexture *tex, bool use_aniso);
262 void GPU_texture_compare_mode(GPUTexture *tex, bool use_compare);
263 void GPU_texture_filter_mode(GPUTexture *tex, bool use_filter);
264 void GPU_texture_mipmap_mode(GPUTexture *tex, bool use_mipmap, bool use_filter);
265 void GPU_texture_wrap_mode(GPUTexture *tex, bool use_repeat, bool use_clamp);
266 void GPU_texture_swizzle_set(GPUTexture *tex, const char swizzle[4]);
267 
268 int GPU_texture_width(const GPUTexture *tex);
269 int GPU_texture_height(const GPUTexture *tex);
270 int GPU_texture_orig_width(const GPUTexture *tex);
271 int GPU_texture_orig_height(const GPUTexture *tex);
272 void GPU_texture_orig_size_set(GPUTexture *tex, int w, int h);
273 eGPUTextureFormat GPU_texture_format(const GPUTexture *tex);
274 bool GPU_texture_array(const GPUTexture *tex);
275 bool GPU_texture_cube(const GPUTexture *tex);
276 bool GPU_texture_depth(const GPUTexture *tex);
277 bool GPU_texture_stencil(const GPUTexture *tex);
278 bool GPU_texture_integer(const GPUTexture *tex);
279 int GPU_texture_opengl_bindcode(const GPUTexture *tex);
280 
281 void GPU_texture_get_mipmap_size(GPUTexture *tex, int lvl, int *size);
282 
283 #ifdef __cplusplus
284 }
285 #endif
286