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.
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.
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.
16 * The Original Code is Copyright (C) 2005 Blender Foundation.
17 * All rights reserved.
20 /** \file GPU_shader.h
24 #ifndef __GPU_SHADER_H__
25 #define __GPU_SHADER_H__
31 typedef struct GPUShader GPUShader;
33 struct GPUUniformBuffer;
36 * - only for fragment shaders now
37 * - must call texture bind before setting a texture as uniform! */
39 typedef enum eGPUShaderTFBType {
40 GPU_SHADER_TFB_NONE = 0, /* Transform feedback unsupported. */
41 GPU_SHADER_TFB_POINTS = 1,
42 GPU_SHADER_TFB_LINES = 2,
43 GPU_SHADER_TFB_TRIANGLES = 3,
46 GPUShader *GPU_shader_create(
47 const char *vertexcode,
52 const char *shader_name);
53 GPUShader *GPU_shader_create_ex(
54 const char *vertexcode,
59 const eGPUShaderTFBType tf_type,
60 const char **tf_names,
62 const char *shader_name);
63 struct GPU_ShaderCreateFromArray_Params { const char **vert, **geom, **frag, **defs; };
64 struct GPUShader *GPU_shader_create_from_arrays_impl(
65 const struct GPU_ShaderCreateFromArray_Params *params);
66 #define GPU_shader_create_from_arrays(...) \
67 GPU_shader_create_from_arrays_impl(&(const struct GPU_ShaderCreateFromArray_Params)__VA_ARGS__)
69 void GPU_shader_free(GPUShader *shader);
71 void GPU_shader_bind(GPUShader *shader);
72 void GPU_shader_unbind(void);
74 /* Returns true if transform feedback was successfully enabled. */
75 bool GPU_shader_transform_feedback_enable(GPUShader *shader, unsigned int vbo_id);
76 void GPU_shader_transform_feedback_disable(GPUShader *shader);
78 int GPU_shader_get_program(GPUShader *shader);
80 void *GPU_shader_get_interface(GPUShader *shader);
82 int GPU_shader_get_uniform(GPUShader *shader, const char *name);
83 int GPU_shader_get_uniform_ensure(GPUShader *shader, const char *name);
84 int GPU_shader_get_builtin_uniform(GPUShader *shader, int builtin);
85 int GPU_shader_get_uniform_block(GPUShader *shader, const char *name);
86 void GPU_shader_uniform_vector(
87 GPUShader *shader, int location, int length,
88 int arraysize, const float *value);
89 void GPU_shader_uniform_vector_int(
90 GPUShader *shader, int location, int length,
91 int arraysize, const int *value);
93 void GPU_shader_uniform_buffer(GPUShader *shader, int location, struct GPUUniformBuffer *ubo);
94 void GPU_shader_uniform_texture(GPUShader *shader, int location, struct GPUTexture *tex);
95 void GPU_shader_uniform_float(GPUShader *shader, int location, float value);
96 void GPU_shader_uniform_int(GPUShader *shader, int location, int value);
98 int GPU_shader_get_attribute(GPUShader *shader, const char *name);
100 /* Builtin/Non-generated shaders */
101 typedef enum eGPUBuiltinShader {
102 /* specialized drawing */
104 GPU_SHADER_TEXT_SIMPLE,
105 GPU_SHADER_EDGES_FRONT_BACK_PERSP,
106 GPU_SHADER_EDGES_FRONT_BACK_ORTHO,
107 GPU_SHADER_EDGES_OVERLAY_SIMPLE,
108 GPU_SHADER_EDGES_OVERLAY,
109 GPU_SHADER_KEYFRAME_DIAMOND,
110 GPU_SHADER_SIMPLE_LIGHTING,
111 GPU_SHADER_SIMPLE_LIGHTING_FLAT_COLOR,
112 GPU_SHADER_SIMPLE_LIGHTING_SMOOTH_COLOR,
113 GPU_SHADER_SIMPLE_LIGHTING_SMOOTH_COLOR_ALPHA,
115 /* for simple 2D drawing */
117 * Take a single color for all the vertices and a 2D position for each vertex.
119 * \param color: uniform vec4
120 * \param pos: in vec2
122 GPU_SHADER_2D_UNIFORM_COLOR,
124 * Take a 2D position and color for each vertex without color interpolation.
126 * \param color: in vec4
127 * \param pos: in vec2
129 GPU_SHADER_2D_FLAT_COLOR,
131 * Take a 2D position and color for each vertex with linear interpolation in window space.
133 * \param color: in vec4
134 * \param pos: in vec2
136 GPU_SHADER_2D_SMOOTH_COLOR,
137 GPU_SHADER_2D_SMOOTH_COLOR_DITHER,
139 GPU_SHADER_2D_IMAGE_COLOR,
140 GPU_SHADER_2D_IMAGE_DESATURATE_COLOR,
141 GPU_SHADER_2D_IMAGE_ALPHA_COLOR,
142 GPU_SHADER_2D_IMAGE_ALPHA,
143 GPU_SHADER_2D_IMAGE_RECT_COLOR,
144 GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR,
145 GPU_SHADER_2D_IMAGE_MULTISAMPLE_2,
146 GPU_SHADER_2D_IMAGE_MULTISAMPLE_4,
147 GPU_SHADER_2D_IMAGE_MULTISAMPLE_8,
148 GPU_SHADER_2D_IMAGE_MULTISAMPLE_16,
149 GPU_SHADER_2D_IMAGE_MULTISAMPLE_2_DEPTH_TEST,
150 GPU_SHADER_2D_IMAGE_MULTISAMPLE_4_DEPTH_TEST,
151 GPU_SHADER_2D_IMAGE_MULTISAMPLE_8_DEPTH_TEST,
152 GPU_SHADER_2D_IMAGE_MULTISAMPLE_16_DEPTH_TEST,
153 GPU_SHADER_2D_CHECKER,
154 GPU_SHADER_2D_DIAG_STRIPES,
155 /* for simple 3D drawing */
157 * Take a single color for all the vertices and a 3D position for each vertex.
159 * \param color: uniform vec4
160 * \param pos: in vec3
162 GPU_SHADER_3D_UNIFORM_COLOR,
163 /* Sets Z-depth to 1.0 (draw onto background). */
164 GPU_SHADER_3D_UNIFORM_COLOR_BACKGROUND,
165 GPU_SHADER_3D_UNIFORM_COLOR_INSTANCE,
167 * Take a 3D position and color for each vertex without color interpolation.
169 * \param color: in vec4
170 * \param pos: in vec3
172 GPU_SHADER_3D_FLAT_COLOR,
174 * Take a 3D position and color for each vertex with perspective correct interpolation.
176 * \param color: in vec4
177 * \param pos: in vec3
179 GPU_SHADER_3D_SMOOTH_COLOR,
181 * Take a 3D position for each vertex and output only depth.
183 * \param pos: in vec3
185 GPU_SHADER_3D_DEPTH_ONLY,
186 GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR,
187 /* basic image drawing */
188 GPU_SHADER_2D_IMAGE_LINEAR_TO_SRGB,
189 GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR,
190 GPU_SHADER_2D_IMAGE_MASK_UNIFORM_COLOR,
192 * Draw texture with alpha. Take a 3D position and a 2D texture coordinate for each vertex.
194 * \param alpha: uniform float
195 * \param image: uniform sampler2D
196 * \param texCoord: in vec2
197 * \param pos: in vec3
199 GPU_SHADER_3D_IMAGE_MODULATE_ALPHA,
201 * Draw linearized depth texture relate to near and far distances.
202 * Take a 3D position and a 2D texture coordinate for each vertex.
204 * \param znear: uniform float
205 * \param zfar: uniform float
206 * \param image: uniform sampler2D
207 * \param texCoord: in vec2
208 * \param pos: in vec3
210 GPU_SHADER_3D_IMAGE_DEPTH,
211 GPU_SHADER_3D_IMAGE_DEPTH_COPY,
213 GPU_SHADER_2D_IMAGE_INTERLACE,
216 * Draw round points with a hardcoded size.
217 * Take a single color for all the vertices and a 2D position for each vertex.
219 * \param color: uniform vec4
220 * \param pos: in vec2
222 GPU_SHADER_2D_POINT_FIXED_SIZE_UNIFORM_COLOR,
224 * Draw round points with a constant size.
225 * Take a single color for all the vertices and a 2D position for each vertex.
227 * \param size: uniform float
228 * \param color: uniform vec4
229 * \param pos: in vec2
231 GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA,
233 * Draw round points with a constant size and an outline.
234 * Take a single color for all the vertices and a 2D position for each vertex.
236 * \param size: uniform float
237 * \param outlineWidth: uniform float
238 * \param color: uniform vec4
239 * \param outlineColor: uniform vec4
240 * \param pos: in vec2
242 GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA,
244 * Draw round points with a constant size and an outline. Take a 2D position and a color for each vertex.
246 * \param size: uniform float
247 * \param outlineWidth: uniform float
248 * \param outlineColor: uniform vec4
249 * \param color: in vec4
250 * \param pos: in vec2
252 GPU_SHADER_2D_POINT_UNIFORM_SIZE_VARYING_COLOR_OUTLINE_AA,
254 * Draw round points with a constant size and an outline. Take a 2D position and a color for each vertex.
256 * \param size: in float
257 * \param color: in vec4
258 * \param pos: in vec2
260 GPU_SHADER_2D_POINT_VARYING_SIZE_VARYING_COLOR,
262 * Draw round points with a hardcoded size.
263 * Take a single color for all the vertices and a 3D position for each vertex.
265 * \param color: uniform vec4
266 * \param pos: in vec3
268 GPU_SHADER_3D_POINT_FIXED_SIZE_UNIFORM_COLOR,
270 * Draw round points with a hardcoded size.
271 * Take a single color for all the vertices and a 3D position for each vertex.
273 * \param color: uniform vec4
274 * \param pos: in vec3
276 GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR,
278 * Draw round points with a constant size.
279 * Take a single color for all the vertices and a 3D position for each vertex.
281 * \param size: uniform float
282 * \param color: uniform vec4
283 * \param pos: in vec3
285 GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA,
287 * Draw round points with a constant size and an outline.
288 * Take a single color for all the vertices and a 3D position for each vertex.
290 * \param size: uniform float
291 * \param outlineWidth: uniform float
292 * \param color: uniform vec4
293 * \param outlineColor: uniform vec4
294 * \param pos: in vec3
296 GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA,
298 * Draw round points with a constant size and an outline.
299 * Take a single color for all the vertices and a 3D position for each vertex.
301 * \param color: uniform vec4
302 * \param size: in float
303 * \param pos: in vec3
305 GPU_SHADER_3D_POINT_VARYING_SIZE_UNIFORM_COLOR,
307 * Draw round points with a constant size and an outline. Take a 3D position and a color for each vertex.
309 * \param size: in float
310 * \param color: in vec4
311 * \param pos: in vec3
313 GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR,
315 GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR,
316 GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR,
318 GPU_SHADER_3D_GROUNDPOINT,
319 GPU_SHADER_3D_GROUNDLINE,
320 GPU_SHADER_3D_SCREENSPACE_VARIYING_COLOR,
322 GPU_SHADER_3D_OBJECTSPACE_VARIYING_COLOR,
323 GPU_SHADER_3D_OBJECTSPACE_SIMPLE_LIGHTING_VARIYING_COLOR,
326 /* distance in front of objects */
327 GPU_SHADER_DISTANCE_LINES,
329 GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED_AXIS,
330 GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED,
332 GPU_SHADER_INSTANCE_UNIFORM_COLOR,
333 GPU_SHADER_INSTANCE_VARIYING_ID_VARIYING_SIZE, /* Uniformly scaled */
334 GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE, /* Uniformly scaled */
335 GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SCALE,
336 GPU_SHADER_INSTANCE_EDGES_VARIYING_COLOR,
337 /* grease pencil drawing */
338 GPU_SHADER_GPENCIL_STROKE,
339 GPU_SHADER_GPENCIL_FILL,
340 /* specialized for widget drawing */
341 GPU_SHADER_2D_AREA_EDGES,
342 GPU_SHADER_2D_WIDGET_BASE,
343 GPU_SHADER_2D_WIDGET_BASE_INST,
344 GPU_SHADER_2D_WIDGET_SHADOW,
345 GPU_SHADER_2D_NODELINK,
346 GPU_SHADER_2D_NODELINK_INST,
347 /* specialized for edituv drawing */
348 GPU_SHADER_2D_UV_UNIFORM_COLOR,
349 GPU_SHADER_2D_UV_VERTS,
350 GPU_SHADER_2D_UV_FACEDOTS,
351 GPU_SHADER_2D_UV_EDGES,
352 GPU_SHADER_2D_UV_EDGES_SMOOTH,
353 GPU_SHADER_2D_UV_FACES,
354 GPU_SHADER_2D_UV_FACES_STRETCH_AREA,
355 GPU_SHADER_2D_UV_FACES_STRETCH_ANGLE,
357 GPU_SHADER_3D_FLAT_SELECT_ID,
358 GPU_SHADER_3D_UNIFORM_SELECT_ID,
360 #define GPU_SHADER_BUILTIN_LEN (GPU_SHADER_3D_UNIFORM_SELECT_ID + 1)
362 /** Support multiple configurations. */
363 typedef enum eGPUShaderConfig {
364 GPU_SHADER_CFG_DEFAULT = 0,
365 GPU_SHADER_CFG_CLIPPED = 1,
367 #define GPU_SHADER_CFG_LEN (GPU_SHADER_CFG_CLIPPED + 1)
369 /** Keep these in sync with:
370 * - `gpu_shader_image_interlace_frag.glsl`
371 * - `gpu_shader_image_rect_interlace_frag.glsl`
373 typedef enum eGPUInterlaceShader {
374 GPU_SHADER_INTERLACE_ROW = 0,
375 GPU_SHADER_INTERLACE_COLUMN = 1,
376 GPU_SHADER_INTERLACE_CHECKER = 2,
377 } eGPUInterlaceShader;
379 GPUShader *GPU_shader_get_builtin_shader_with_config(
380 eGPUBuiltinShader shader, eGPUShaderConfig shader_cfg);
381 GPUShader *GPU_shader_get_builtin_shader(
382 eGPUBuiltinShader shader);
384 void GPU_shader_get_builtin_shader_code(
385 eGPUBuiltinShader shader,
386 const char **r_vert, const char **r_frag,
387 const char **r_geom, const char **r_defines);
389 void GPU_shader_free_builtin_shaders(void);
391 /* Vertex attributes for shaders */
393 #define GPU_MAX_ATTR 32
395 typedef struct GPUVertAttrLayers {
402 char name[64]; /* MAX_CUSTOMDATA_LAYER_NAME */
403 } layer[GPU_MAX_ATTR];
412 #endif /* __GPU_SHADER_H__ */