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 \ingroup gpu
23 #ifndef __GPU_SHADER_H__
24 #define __GPU_SHADER_H__
30 typedef struct GPUShader GPUShader;
32 struct GPUUniformBuffer;
35 * - only for fragment shaders now
36 * - must call texture bind before setting a texture as uniform! */
38 typedef enum eGPUShaderTFBType {
39 GPU_SHADER_TFB_NONE = 0, /* Transform feedback unsupported. */
40 GPU_SHADER_TFB_POINTS = 1,
41 GPU_SHADER_TFB_LINES = 2,
42 GPU_SHADER_TFB_TRIANGLES = 3,
45 GPUShader *GPU_shader_create(
46 const char *vertexcode,
51 const char *shader_name);
52 GPUShader *GPU_shader_create_ex(
53 const char *vertexcode,
58 const eGPUShaderTFBType tf_type,
59 const char **tf_names,
61 const char *shader_name);
62 struct GPU_ShaderCreateFromArray_Params { const char **vert, **geom, **frag, **defs; };
63 struct GPUShader *GPU_shader_create_from_arrays_impl(
64 const struct GPU_ShaderCreateFromArray_Params *params);
65 #define GPU_shader_create_from_arrays(...) \
66 GPU_shader_create_from_arrays_impl(&(const struct GPU_ShaderCreateFromArray_Params)__VA_ARGS__)
68 void GPU_shader_free(GPUShader *shader);
70 void GPU_shader_bind(GPUShader *shader);
71 void GPU_shader_unbind(void);
73 /* Returns true if transform feedback was successfully enabled. */
74 bool GPU_shader_transform_feedback_enable(GPUShader *shader, unsigned int vbo_id);
75 void GPU_shader_transform_feedback_disable(GPUShader *shader);
77 int GPU_shader_get_program(GPUShader *shader);
79 void *GPU_shader_get_interface(GPUShader *shader);
81 int GPU_shader_get_uniform(GPUShader *shader, const char *name);
82 int GPU_shader_get_uniform_ensure(GPUShader *shader, const char *name);
83 int GPU_shader_get_builtin_uniform(GPUShader *shader, int builtin);
84 int GPU_shader_get_uniform_block(GPUShader *shader, const char *name);
85 void GPU_shader_uniform_vector(
86 GPUShader *shader, int location, int length,
87 int arraysize, const float *value);
88 void GPU_shader_uniform_vector_int(
89 GPUShader *shader, int location, int length,
90 int arraysize, const int *value);
92 void GPU_shader_uniform_buffer(GPUShader *shader, int location, struct GPUUniformBuffer *ubo);
93 void GPU_shader_uniform_texture(GPUShader *shader, int location, struct GPUTexture *tex);
94 void GPU_shader_uniform_float(GPUShader *shader, int location, float value);
95 void GPU_shader_uniform_int(GPUShader *shader, int location, int value);
97 int GPU_shader_get_attribute(GPUShader *shader, const char *name);
99 /* Builtin/Non-generated shaders */
100 typedef enum eGPUBuiltinShader {
101 /* specialized drawing */
103 GPU_SHADER_TEXT_SIMPLE,
104 GPU_SHADER_EDGES_FRONT_BACK_PERSP,
105 GPU_SHADER_EDGES_FRONT_BACK_ORTHO,
106 GPU_SHADER_EDGES_OVERLAY_SIMPLE,
107 GPU_SHADER_EDGES_OVERLAY,
108 GPU_SHADER_KEYFRAME_DIAMOND,
109 GPU_SHADER_SIMPLE_LIGHTING,
110 GPU_SHADER_SIMPLE_LIGHTING_FLAT_COLOR,
111 GPU_SHADER_SIMPLE_LIGHTING_SMOOTH_COLOR,
112 GPU_SHADER_SIMPLE_LIGHTING_SMOOTH_COLOR_ALPHA,
114 /* for simple 2D drawing */
116 * Take a single color for all the vertices and a 2D position for each vertex.
118 * \param color: uniform vec4
119 * \param pos: in vec2
121 GPU_SHADER_2D_UNIFORM_COLOR,
123 * Take a 2D position and color for each vertex without color interpolation.
125 * \param color: in vec4
126 * \param pos: in vec2
128 GPU_SHADER_2D_FLAT_COLOR,
130 * Take a 2D position and color for each vertex with linear interpolation in window space.
132 * \param color: in vec4
133 * \param pos: in vec2
135 GPU_SHADER_2D_SMOOTH_COLOR,
136 GPU_SHADER_2D_SMOOTH_COLOR_DITHER,
138 GPU_SHADER_2D_IMAGE_COLOR,
139 GPU_SHADER_2D_IMAGE_DESATURATE_COLOR,
140 GPU_SHADER_2D_IMAGE_ALPHA_COLOR,
141 GPU_SHADER_2D_IMAGE_ALPHA,
142 GPU_SHADER_2D_IMAGE_RECT_COLOR,
143 GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR,
144 GPU_SHADER_2D_IMAGE_MULTISAMPLE_2,
145 GPU_SHADER_2D_IMAGE_MULTISAMPLE_4,
146 GPU_SHADER_2D_IMAGE_MULTISAMPLE_8,
147 GPU_SHADER_2D_IMAGE_MULTISAMPLE_16,
148 GPU_SHADER_2D_IMAGE_MULTISAMPLE_2_DEPTH_TEST,
149 GPU_SHADER_2D_IMAGE_MULTISAMPLE_4_DEPTH_TEST,
150 GPU_SHADER_2D_IMAGE_MULTISAMPLE_8_DEPTH_TEST,
151 GPU_SHADER_2D_IMAGE_MULTISAMPLE_16_DEPTH_TEST,
152 GPU_SHADER_2D_CHECKER,
153 GPU_SHADER_2D_DIAG_STRIPES,
154 /* for simple 3D drawing */
156 * Take a single color for all the vertices and a 3D position for each vertex.
158 * \param color: uniform vec4
159 * \param pos: in vec3
161 GPU_SHADER_3D_UNIFORM_COLOR,
162 /* Sets Z-depth to 1.0 (draw onto background). */
163 GPU_SHADER_3D_UNIFORM_COLOR_BACKGROUND,
164 GPU_SHADER_3D_UNIFORM_COLOR_INSTANCE,
166 * Take a 3D position and color for each vertex without color interpolation.
168 * \param color: in vec4
169 * \param pos: in vec3
171 GPU_SHADER_3D_FLAT_COLOR,
173 * Take a 3D position and color for each vertex with perspective correct interpolation.
175 * \param color: in vec4
176 * \param pos: in vec3
178 GPU_SHADER_3D_SMOOTH_COLOR,
180 * Take a 3D position for each vertex and output only depth.
182 * \param pos: in vec3
184 GPU_SHADER_3D_DEPTH_ONLY,
185 GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR,
186 /* basic image drawing */
187 GPU_SHADER_2D_IMAGE_LINEAR_TO_SRGB,
188 GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR,
189 GPU_SHADER_2D_IMAGE_MASK_UNIFORM_COLOR,
191 * Draw texture with alpha. Take a 3D position and a 2D texture coordinate for each vertex.
193 * \param alpha: uniform float
194 * \param image: uniform sampler2D
195 * \param texCoord: in vec2
196 * \param pos: in vec3
198 GPU_SHADER_3D_IMAGE_MODULATE_ALPHA,
200 * Draw linearized depth texture relate to near and far distances.
201 * Take a 3D position and a 2D texture coordinate for each vertex.
203 * \param znear: uniform float
204 * \param zfar: uniform float
205 * \param image: uniform sampler2D
206 * \param texCoord: in vec2
207 * \param pos: in vec3
209 GPU_SHADER_3D_IMAGE_DEPTH,
210 GPU_SHADER_3D_IMAGE_DEPTH_COPY,
212 GPU_SHADER_2D_IMAGE_INTERLACE,
215 * Draw round points with a hardcoded size.
216 * Take a single color for all the vertices and a 2D position for each vertex.
218 * \param color: uniform vec4
219 * \param pos: in vec2
221 GPU_SHADER_2D_POINT_FIXED_SIZE_UNIFORM_COLOR,
223 * Draw round points with a constant size.
224 * Take a single color for all the vertices and a 2D position for each vertex.
226 * \param size: uniform float
227 * \param color: uniform vec4
228 * \param pos: in vec2
230 GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA,
232 * Draw round points with a constant size and an outline.
233 * Take a single color for all the vertices and a 2D position for each vertex.
235 * \param size: uniform float
236 * \param outlineWidth: uniform float
237 * \param color: uniform vec4
238 * \param outlineColor: uniform vec4
239 * \param pos: in vec2
241 GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA,
243 * Draw round points with a constant size and an outline. Take a 2D position and a color for each vertex.
245 * \param size: uniform float
246 * \param outlineWidth: uniform float
247 * \param outlineColor: uniform vec4
248 * \param color: in vec4
249 * \param pos: in vec2
251 GPU_SHADER_2D_POINT_UNIFORM_SIZE_VARYING_COLOR_OUTLINE_AA,
253 * Draw round points with a constant size and an outline. Take a 2D position and a color for each vertex.
255 * \param size: in float
256 * \param color: in vec4
257 * \param pos: in vec2
259 GPU_SHADER_2D_POINT_VARYING_SIZE_VARYING_COLOR,
261 * Draw round points with a hardcoded size.
262 * Take a single color for all the vertices and a 3D position for each vertex.
264 * \param color: uniform vec4
265 * \param pos: in vec3
267 GPU_SHADER_3D_POINT_FIXED_SIZE_UNIFORM_COLOR,
269 * Draw round points with a hardcoded size.
270 * Take a single color for all the vertices and a 3D position for each vertex.
272 * \param color: uniform vec4
273 * \param pos: in vec3
275 GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR,
277 * Draw round points with a constant size.
278 * Take a single color for all the vertices and a 3D position for each vertex.
280 * \param size: uniform float
281 * \param color: uniform vec4
282 * \param pos: in vec3
284 GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA,
286 * Draw round points with a constant size and an outline.
287 * Take a single color for all the vertices and a 3D position for each vertex.
289 * \param size: uniform float
290 * \param outlineWidth: uniform float
291 * \param color: uniform vec4
292 * \param outlineColor: uniform vec4
293 * \param pos: in vec3
295 GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA,
297 * Draw round points with a constant size and an outline.
298 * Take a single color for all the vertices and a 3D position for each vertex.
300 * \param color: uniform vec4
301 * \param size: in float
302 * \param pos: in vec3
304 GPU_SHADER_3D_POINT_VARYING_SIZE_UNIFORM_COLOR,
306 * Draw round points with a constant size and an outline. Take a 3D position and a color for each vertex.
308 * \param size: in float
309 * \param color: in vec4
310 * \param pos: in vec3
312 GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR,
314 GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR,
315 GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR,
317 GPU_SHADER_3D_GROUNDPOINT,
318 GPU_SHADER_3D_GROUNDLINE,
319 GPU_SHADER_3D_SCREENSPACE_VARIYING_COLOR,
321 GPU_SHADER_3D_OBJECTSPACE_VARIYING_COLOR,
322 GPU_SHADER_3D_OBJECTSPACE_SIMPLE_LIGHTING_VARIYING_COLOR,
325 /* distance in front of objects */
326 GPU_SHADER_DISTANCE_LINES,
328 GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED_AXIS,
329 GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED,
331 GPU_SHADER_INSTANCE_UNIFORM_COLOR,
332 GPU_SHADER_INSTANCE_VARIYING_ID_VARIYING_SIZE, /* Uniformly scaled */
333 GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE, /* Uniformly scaled */
334 GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SCALE,
335 GPU_SHADER_INSTANCE_EDGES_VARIYING_COLOR,
336 /* grease pencil drawing */
337 GPU_SHADER_GPENCIL_STROKE,
338 GPU_SHADER_GPENCIL_FILL,
339 /* specialized for widget drawing */
340 GPU_SHADER_2D_AREA_EDGES,
341 GPU_SHADER_2D_WIDGET_BASE,
342 GPU_SHADER_2D_WIDGET_BASE_INST,
343 GPU_SHADER_2D_WIDGET_SHADOW,
344 GPU_SHADER_2D_NODELINK,
345 GPU_SHADER_2D_NODELINK_INST,
346 /* specialized for edituv drawing */
347 GPU_SHADER_2D_UV_UNIFORM_COLOR,
348 GPU_SHADER_2D_UV_VERTS,
349 GPU_SHADER_2D_UV_FACEDOTS,
350 GPU_SHADER_2D_UV_EDGES,
351 GPU_SHADER_2D_UV_EDGES_SMOOTH,
352 GPU_SHADER_2D_UV_FACES,
353 GPU_SHADER_2D_UV_FACES_STRETCH_AREA,
354 GPU_SHADER_2D_UV_FACES_STRETCH_ANGLE,
356 GPU_SHADER_3D_FLAT_SELECT_ID,
357 GPU_SHADER_3D_UNIFORM_SELECT_ID,
359 #define GPU_SHADER_BUILTIN_LEN (GPU_SHADER_3D_UNIFORM_SELECT_ID + 1)
361 /** Support multiple configurations. */
362 typedef enum eGPUShaderConfig {
363 GPU_SHADER_CFG_DEFAULT = 0,
364 GPU_SHADER_CFG_CLIPPED = 1,
366 #define GPU_SHADER_CFG_LEN (GPU_SHADER_CFG_CLIPPED + 1)
368 typedef struct GPUShaderConfigData {
371 } GPUShaderConfigData;
373 extern const GPUShaderConfigData GPU_shader_cfg_data[GPU_SHADER_CFG_LEN];
375 /** Keep these in sync with:
376 * - `gpu_shader_image_interlace_frag.glsl`
377 * - `gpu_shader_image_rect_interlace_frag.glsl`
379 typedef enum eGPUInterlaceShader {
380 GPU_SHADER_INTERLACE_ROW = 0,
381 GPU_SHADER_INTERLACE_COLUMN = 1,
382 GPU_SHADER_INTERLACE_CHECKER = 2,
383 } eGPUInterlaceShader;
385 GPUShader *GPU_shader_get_builtin_shader_with_config(
386 eGPUBuiltinShader shader, eGPUShaderConfig sh_cfg);
387 GPUShader *GPU_shader_get_builtin_shader(
388 eGPUBuiltinShader shader);
390 void GPU_shader_get_builtin_shader_code(
391 eGPUBuiltinShader shader,
392 const char **r_vert, const char **r_frag,
393 const char **r_geom, const char **r_defines);
395 void GPU_shader_free_builtin_shaders(void);
397 /* Vertex attributes for shaders */
399 #define GPU_MAX_ATTR 32
401 typedef struct GPUVertAttrLayers {
408 char name[64]; /* MAX_CUSTOMDATA_LAYER_NAME */
409 } layer[GPU_MAX_ATTR];
418 #endif /* __GPU_SHADER_H__ */