1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2005 Blender Foundation.
* All rights reserved.
*/
/** \file
* \ingroup gpu
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
struct GPUTexture;
struct GPUUniformBuffer;
struct GPUVertBuf;
/* TODO(fclem) These members should be private and the
* whole struct should just be an opaque pointer. */
typedef struct GPUShader {
/** For debugging purpose. */
char name[64];
} GPUShader;
typedef enum eGPUShaderTFBType {
GPU_SHADER_TFB_NONE = 0, /* Transform feedback unsupported. */
GPU_SHADER_TFB_POINTS = 1,
GPU_SHADER_TFB_LINES = 2,
GPU_SHADER_TFB_TRIANGLES = 3,
} eGPUShaderTFBType;
GPUShader *GPU_shader_create(const char *vertexcode,
const char *fragcode,
const char *geocode,
const char *libcode,
const char *defines,
const char *shader_name);
GPUShader *GPU_shader_create_from_python(const char *vertexcode,
const char *fragcode,
const char *geocode,
const char *libcode,
const char *defines);
GPUShader *GPU_shader_create_ex(const char *vertexcode,
const char *fragcode,
const char *geocode,
const char *libcode,
const char *defines,
const eGPUShaderTFBType tf_type,
const char **tf_names,
const int tf_count,
const char *shader_name);
struct GPU_ShaderCreateFromArray_Params {
const char **vert, **geom, **frag, **defs;
};
struct GPUShader *GPU_shader_create_from_arrays_impl(
const struct GPU_ShaderCreateFromArray_Params *params, const char *func, int line);
#define GPU_shader_create_from_arrays(...) \
GPU_shader_create_from_arrays_impl( \
&(const struct GPU_ShaderCreateFromArray_Params)__VA_ARGS__, __func__, __LINE__)
void GPU_shader_free(GPUShader *shader);
void GPU_shader_bind(GPUShader *shader);
void GPU_shader_unbind(void);
/* Returns true if transform feedback was successfully enabled. */
bool GPU_shader_transform_feedback_enable(GPUShader *shader, struct GPUVertBuf *vertbuf);
void GPU_shader_transform_feedback_disable(GPUShader *shader);
int GPU_shader_get_program(GPUShader *shader);
typedef enum {
GPU_UNIFORM_MODEL = 0, /* mat4 ModelMatrix */
GPU_UNIFORM_VIEW, /* mat4 ViewMatrix */
GPU_UNIFORM_MODELVIEW, /* mat4 ModelViewMatrix */
GPU_UNIFORM_PROJECTION, /* mat4 ProjectionMatrix */
GPU_UNIFORM_VIEWPROJECTION, /* mat4 ViewProjectionMatrix */
GPU_UNIFORM_MVP, /* mat4 ModelViewProjectionMatrix */
GPU_UNIFORM_MODEL_INV, /* mat4 ModelMatrixInverse */
GPU_UNIFORM_VIEW_INV, /* mat4 ViewMatrixInverse */
GPU_UNIFORM_MODELVIEW_INV, /* mat4 ModelViewMatrixInverse */
GPU_UNIFORM_PROJECTION_INV, /* mat4 ProjectionMatrixInverse */
GPU_UNIFORM_VIEWPROJECTION_INV, /* mat4 ViewProjectionMatrixInverse */
GPU_UNIFORM_NORMAL, /* mat3 NormalMatrix */
GPU_UNIFORM_ORCO, /* vec4 OrcoTexCoFactors[] */
GPU_UNIFORM_CLIPPLANES, /* vec4 WorldClipPlanes[] */
GPU_UNIFORM_COLOR, /* vec4 color */
GPU_UNIFORM_BASE_INSTANCE, /* int baseInstance */
GPU_UNIFORM_RESOURCE_CHUNK, /* int resourceChunk */
GPU_UNIFORM_RESOURCE_ID, /* int resourceId */
GPU_UNIFORM_SRGB_TRANSFORM, /* bool srgbTarget */
GPU_NUM_UNIFORMS, /* Special value, denotes number of builtin uniforms. */
} GPUUniformBuiltin;
typedef enum {
GPU_UNIFORM_BLOCK_VIEW = 0, /* viewBlock */
GPU_UNIFORM_BLOCK_MODEL, /* modelBlock */
GPU_UNIFORM_BLOCK_INFO, /* infoBlock */
GPU_NUM_UNIFORM_BLOCKS, /* Special value, denotes number of builtin uniforms block. */
} GPUUniformBlockBuiltin;
void GPU_shader_set_srgb_uniform(GPUShader *shader);
int GPU_shader_get_uniform(GPUShader *shader, const char *name);
int GPU_shader_get_builtin_uniform(GPUShader *shader, int builtin);
int GPU_shader_get_builtin_block(GPUShader *shader, int builtin);
int GPU_shader_get_uniform_block(GPUShader *shader, const char *name);
int GPU_shader_get_uniform_block_binding(GPUShader *shader, const char *name);
int GPU_shader_get_texture_binding(GPUShader *shader, const char *name);
void GPU_shader_uniform_vector(
GPUShader *shader, int location, int length, int arraysize, const float *value);
void GPU_shader_uniform_vector_int(
GPUShader *shader, int location, int length, int arraysize, const int *value);
void GPU_shader_uniform_float(GPUShader *shader, int location, float value);
void GPU_shader_uniform_int(GPUShader *shader, int location, int value);
void GPU_shader_uniform_1i(GPUShader *sh, const char *name, int value);
void GPU_shader_uniform_1b(GPUShader *sh, const char *name, bool value);
void GPU_shader_uniform_1f(GPUShader *sh, const char *name, float value);
void GPU_shader_uniform_2f(GPUShader *sh, const char *name, float x, float y);
void GPU_shader_uniform_3f(GPUShader *sh, const char *name, float x, float y, float z);
void GPU_shader_uniform_4f(GPUShader *sh, const char *name, float x, float y, float z, float w);
void GPU_shader_uniform_2fv(GPUShader *sh, const char *name, const float data[2]);
void GPU_shader_uniform_3fv(GPUShader *sh, const char *name, const float data[3]);
void GPU_shader_uniform_4fv(GPUShader *sh, const char *name, const float data[4]);
void GPU_shader_uniform_mat4(GPUShader *sh, const char *name, const float data[4][4]);
void GPU_shader_uniform_2fv_array(GPUShader *sh, const char *name, int len, const float (*val)[2]);
void GPU_shader_uniform_4fv_array(GPUShader *sh, const char *name, int len, const float (*val)[4]);
int GPU_shader_get_attribute(GPUShader *shader, const char *name);
void GPU_shader_set_framebuffer_srgb_target(int use_srgb_to_linear);
/* Builtin/Non-generated shaders */
typedef enum eGPUBuiltinShader {
/* specialized drawing */
GPU_SHADER_TEXT,
GPU_SHADER_KEYFRAME_DIAMOND,
GPU_SHADER_SIMPLE_LIGHTING,
/* for simple 2D drawing */
/**
* Take a single color for all the vertices and a 2D position for each vertex.
*
* \param color: uniform vec4
* \param pos: in vec2
*/
GPU_SHADER_2D_UNIFORM_COLOR,
/**
* Take a 2D position and color for each vertex without color interpolation.
*
* \param color: in vec4
* \param pos: in vec2
*/
GPU_SHADER_2D_FLAT_COLOR,
/**
* Take a 2D position and color for each vertex with linear interpolation in window space.
*
* \param color: in vec4
* \param pos: in vec2
*/
GPU_SHADER_2D_SMOOTH_COLOR,
GPU_SHADER_2D_IMAGE,
GPU_SHADER_2D_IMAGE_COLOR,
GPU_SHADER_2D_IMAGE_DESATURATE_COLOR,
GPU_SHADER_2D_IMAGE_RECT_COLOR,
GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR,
GPU_SHADER_2D_CHECKER,
GPU_SHADER_2D_DIAG_STRIPES,
/* for simple 3D drawing */
/**
* Take a single color for all the vertices and a 3D position for each vertex.
*
* \param color: uniform vec4
* \param pos: in vec3
*/
GPU_SHADER_3D_UNIFORM_COLOR,
GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR,
/**
* Take a 3D position and color for each vertex without color interpolation.
*
* \param color: in vec4
* \param pos: in vec3
*/
GPU_SHADER_3D_FLAT_COLOR,
/**
* Take a 3D position and color for each vertex with perspective correct interpolation.
*
* \param color: in vec4
* \param pos: in vec3
*/
GPU_SHADER_3D_SMOOTH_COLOR,
/**
* Take a single color for all the vertices and a 3D position for each vertex.
* Used for drawing wide lines.
*
* \param color: uniform vec4
* \param pos: in vec3
*/
GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR,
GPU_SHADER_3D_POLYLINE_CLIPPED_UNIFORM_COLOR,
/**
* Take a 3D position and color for each vertex without color interpolation.
* Used for drawing wide lines.
*
* \param color: in vec4
* \param pos: in vec3
*/
GPU_SHADER_3D_POLYLINE_FLAT_COLOR,
/**
* Take a 3D position and color for each vertex with perspective correct interpolation.
* Used for drawing wide lines.
*
* \param color: in vec4
* \param pos: in vec3
*/
GPU_SHADER_3D_POLYLINE_SMOOTH_COLOR,
/**
* Take a 3D position for each vertex and output only depth.
* Used for drawing wide lines.
*
* \param pos: in vec3
*/
GPU_SHADER_3D_DEPTH_ONLY,
/* basic image drawing */
GPU_SHADER_2D_IMAGE_OVERLAYS_MERGE,
GPU_SHADER_2D_IMAGE_OVERLAYS_STEREO_MERGE,
GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR,
/* points */
/**
* Draw round points with a hardcoded size.
* Take a single color for all the vertices and a 2D position for each vertex.
*
* \param color: uniform vec4
* \param pos: in vec2
*/
GPU_SHADER_2D_POINT_FIXED_SIZE_UNIFORM_COLOR,
/**
* Draw round points with a constant size.
* Take a single color for all the vertices and a 2D position for each vertex.
*
* \param size: uniform float
* \param color: uniform vec4
* \param pos: in vec2
*/
GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA,
/**
* Draw round points with a constant size and an outline.
* Take a single color for all the vertices and a 2D position for each vertex.
*
* \param size: uniform float
* \param outlineWidth: uniform float
* \param color: uniform vec4
* \param outlineColor: uniform vec4
* \param pos: in vec2
*/
GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA,
/**
* Draw round points with a constant size and an outline.
* Take a 2D position and a color for each vertex.
*
* \param size: uniform float
* \param outlineWidth: uniform float
* \param outlineColor: uniform vec4
* \param color: in vec4
* \param pos: in vec2
*/
GPU_SHADER_2D_POINT_UNIFORM_SIZE_VARYING_COLOR_OUTLINE_AA,
/**
* Draw round points with a constant size and an outline.
* Take a 2D position and a color for each vertex.
*
* \param size: in float
* \param color: in vec4
* \param pos: in vec2
*/
GPU_SHADER_2D_POINT_VARYING_SIZE_VARYING_COLOR,
/**
* Draw round points with a hardcoded size.
* Take a single color for all the vertices and a 3D position for each vertex.
*
* \param color: uniform vec4
* \param pos: in vec3
*/
GPU_SHADER_3D_POINT_FIXED_SIZE_UNIFORM_COLOR,
/**
* Draw round points with a hardcoded size.
* Take a single color for all the vertices and a 3D position for each vertex.
*
* \param color: uniform vec4
* \param pos: in vec3
*/
GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR,
/**
* Draw round points with a constant size.
* Take a single color for all the vertices and a 3D position for each vertex.
*
* \param size: uniform float
* \param color: uniform vec4
* \param pos: in vec3
*/
GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA,
/**
* Draw round points with a constant size and an outline.
* Take a single color for all the vertices and a 3D position for each vertex.
*
* \param size: uniform float
* \param outlineWidth: uniform float
* \param color: uniform vec4
* \param outlineColor: uniform vec4
* \param pos: in vec3
*/
GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA,
/**
* Draw round points with a constant size and an outline.
* Take a single color for all the vertices and a 3D position for each vertex.
*
* \param color: uniform vec4
* \param size: in float
* \param pos: in vec3
*/
GPU_SHADER_3D_POINT_VARYING_SIZE_UNIFORM_COLOR,
/**
* Draw round points with a constant size and an outline.
* Take a 3D position and a color for each vertex.
*
* \param size: in float
* \param color: in vec4
* \param pos: in vec3
*/
GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR,
/* lines */
GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR,
GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR,
/* instance */
GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE, /* Uniformly scaled */
/* grease pencil drawing */
GPU_SHADER_GPENCIL_STROKE,
GPU_SHADER_GPENCIL_FILL,
/* specialized for widget drawing */
GPU_SHADER_2D_AREA_EDGES,
GPU_SHADER_2D_WIDGET_BASE,
GPU_SHADER_2D_WIDGET_BASE_INST,
GPU_SHADER_2D_WIDGET_SHADOW,
GPU_SHADER_2D_NODELINK,
GPU_SHADER_2D_NODELINK_INST,
/* specialized for edituv drawing */
GPU_SHADER_2D_UV_UNIFORM_COLOR,
GPU_SHADER_2D_UV_VERTS,
GPU_SHADER_2D_UV_FACEDOTS,
GPU_SHADER_2D_UV_EDGES,
GPU_SHADER_2D_UV_EDGES_SMOOTH,
GPU_SHADER_2D_UV_FACES,
GPU_SHADER_2D_UV_FACES_STRETCH_AREA,
GPU_SHADER_2D_UV_FACES_STRETCH_ANGLE,
} eGPUBuiltinShader;
#define GPU_SHADER_BUILTIN_LEN (GPU_SHADER_2D_UV_FACES_STRETCH_ANGLE + 1)
/** Support multiple configurations. */
typedef enum eGPUShaderConfig {
GPU_SHADER_CFG_DEFAULT = 0,
GPU_SHADER_CFG_CLIPPED = 1,
} eGPUShaderConfig;
#define GPU_SHADER_CFG_LEN (GPU_SHADER_CFG_CLIPPED + 1)
typedef struct GPUShaderConfigData {
const char *lib;
const char *def;
} GPUShaderConfigData;
/* gpu_shader.c */
extern const GPUShaderConfigData GPU_shader_cfg_data[GPU_SHADER_CFG_LEN];
GPUShader *GPU_shader_get_builtin_shader_with_config(eGPUBuiltinShader shader,
eGPUShaderConfig sh_cfg);
GPUShader *GPU_shader_get_builtin_shader(eGPUBuiltinShader shader);
void GPU_shader_get_builtin_shader_code(eGPUBuiltinShader shader,
const char **r_vert,
const char **r_frag,
const char **r_geom,
const char **r_defines);
void GPU_shader_free_builtin_shaders(void);
/* Vertex attributes for shaders */
/* Hardware limit is 16. Position attribute is always needed so we reduce to 15.
* This makes sure the GPUVertexFormat name buffer does not overflow. */
#define GPU_MAX_ATTR 15
#ifdef __cplusplus
}
#endif
|