f780eda62fefae2ad653b932d111ef0f43720576
[blender.git] / source / blender / gpu / GPU_material.h
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Brecht Van Lommel.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file GPU_material.h
29  *  \ingroup gpu
30  */
31
32 #ifndef __GPU_MATERIAL_H__
33 #define __GPU_MATERIAL_H__
34
35 #include "DNA_customdata_types.h" /* for CustomDataType */
36 #include "DNA_listBase.h"
37
38 #include "BLI_sys_types.h" /* for bool */
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 struct Image;
45 struct ImageUser;
46 struct ListBase;
47 struct Material;
48 struct Object;
49 struct Scene;
50 struct GPUVertexAttribs;
51 struct GPUNode;
52 struct GPUNodeLink;
53 struct GPUNodeStack;
54 struct GPUMaterial;
55 struct GPUTexture;
56 struct GPUUniformBuffer;
57 struct GPULamp;
58 struct PreviewImage;
59 struct World;
60 struct bNode;
61 struct bNodeTree;
62
63 typedef struct GPUNode GPUNode;
64 typedef struct GPUNodeLink GPUNodeLink;
65 typedef struct GPUMaterial GPUMaterial;
66
67 typedef struct GPUParticleInfo GPUParticleInfo;
68
69 /* Functions to create GPU Materials nodes */
70
71 typedef enum GPUType {
72         /* Keep in sync with GPU_DATATYPE_STR */
73         /* The value indicates the number of elements in each type */
74         GPU_NONE = 0,
75         GPU_FLOAT = 1,
76         GPU_VEC2 = 2,
77         GPU_VEC3 = 3,
78         GPU_VEC4 = 4,
79         GPU_MAT3 = 9,
80         GPU_MAT4 = 16,
81
82         /* Values not in GPU_DATATYPE_STR */
83         GPU_TEX2D = 1002,
84         GPU_TEX3D = 1003,
85         GPU_SHADOW2D = 1004,
86         GPU_TEXCUBE = 1005,
87
88         /* GLSL Struct types */
89         GPU_CLOSURE = 1006,
90
91         /* Opengl Attributes */
92         GPU_ATTRIB = 3001
93 } GPUType;
94
95 typedef enum GPUBuiltin {
96         GPU_VIEW_MATRIX =           (1 << 0),
97         GPU_OBJECT_MATRIX =         (1 << 1),
98         GPU_INVERSE_VIEW_MATRIX =   (1 << 2),
99         GPU_INVERSE_OBJECT_MATRIX = (1 << 3),
100         GPU_VIEW_POSITION =         (1 << 4),
101         GPU_VIEW_NORMAL =           (1 << 5),
102         GPU_OBCOLOR =               (1 << 6),
103         GPU_AUTO_BUMPSCALE =        (1 << 7),
104         GPU_CAMERA_TEXCO_FACTORS =  (1 << 8),
105         GPU_PARTICLE_SCALAR_PROPS = (1 << 9),
106         GPU_PARTICLE_LOCATION =     (1 << 10),
107         GPU_PARTICLE_VELOCITY =     (1 << 11),
108         GPU_PARTICLE_ANG_VELOCITY = (1 << 12),
109         GPU_LOC_TO_VIEW_MATRIX =    (1 << 13),
110         GPU_INVERSE_LOC_TO_VIEW_MATRIX = (1 << 14),
111         GPU_OBJECT_INFO =           (1 << 15),
112         GPU_VOLUME_DENSITY =        (1 << 16),
113         GPU_VOLUME_FLAME =          (1 << 17),
114         GPU_VOLUME_TEMPERATURE =    (1 << 18)
115 } GPUBuiltin;
116
117 typedef enum GPUOpenGLBuiltin {
118         GPU_MATCAP_NORMAL = 1,
119         GPU_COLOR = 2,
120 } GPUOpenGLBuiltin;
121
122 typedef enum GPUMatType {
123         GPU_MATERIAL_TYPE_MESH  = 1,
124         GPU_MATERIAL_TYPE_WORLD = 2,
125 } GPUMatType;
126
127
128 typedef enum GPUBlendMode {
129         GPU_BLEND_SOLID = 0,
130         GPU_BLEND_ADD = 1,
131         GPU_BLEND_ALPHA = 2,
132         GPU_BLEND_CLIP = 4,
133         GPU_BLEND_ALPHA_SORT = 8,
134         GPU_BLEND_ALPHA_TO_COVERAGE = 16
135 } GPUBlendMode;
136
137 typedef struct GPUNodeStack {
138         GPUType type;
139         float vec[4];
140         struct GPUNodeLink *link;
141         bool hasinput;
142         bool hasoutput;
143         short sockettype;
144         bool end;
145 } GPUNodeStack;
146
147 typedef enum GPUMaterialStatus {
148         GPU_MAT_FAILED = 0,
149         GPU_MAT_QUEUED,
150         GPU_MAT_SUCCESS,
151 } GPUMaterialStatus;
152
153 #define GPU_DYNAMIC_GROUP_FROM_TYPE(f) ((f) & 0xFFFF0000)
154
155 #define GPU_DYNAMIC_GROUP_MISC     0x00010000
156 #define GPU_DYNAMIC_GROUP_LAMP     0x00020000
157 #define GPU_DYNAMIC_GROUP_OBJECT   0x00030000
158 #define GPU_DYNAMIC_GROUP_SAMPLER  0x00040000
159 #define GPU_DYNAMIC_GROUP_MIST     0x00050000
160 #define GPU_DYNAMIC_GROUP_WORLD    0x00060000
161 #define GPU_DYNAMIC_GROUP_MAT      0x00070000
162 #define GPU_DYNAMIC_UBO            0x00080000
163
164 typedef enum GPUDynamicType {
165
166         GPU_DYNAMIC_NONE                 = 0,
167
168         GPU_DYNAMIC_OBJECT_VIEWMAT       = 1  | GPU_DYNAMIC_GROUP_OBJECT,
169         GPU_DYNAMIC_OBJECT_MAT           = 2  | GPU_DYNAMIC_GROUP_OBJECT,
170         GPU_DYNAMIC_OBJECT_VIEWIMAT      = 3  | GPU_DYNAMIC_GROUP_OBJECT,
171         GPU_DYNAMIC_OBJECT_IMAT          = 4  | GPU_DYNAMIC_GROUP_OBJECT,
172         GPU_DYNAMIC_OBJECT_COLOR         = 5  | GPU_DYNAMIC_GROUP_OBJECT,
173         GPU_DYNAMIC_OBJECT_AUTOBUMPSCALE = 6  | GPU_DYNAMIC_GROUP_OBJECT,
174         GPU_DYNAMIC_OBJECT_LOCTOVIEWMAT  = 7  | GPU_DYNAMIC_GROUP_OBJECT,
175         GPU_DYNAMIC_OBJECT_LOCTOVIEWIMAT = 8  | GPU_DYNAMIC_GROUP_OBJECT,
176
177         GPU_DYNAMIC_LAMP_DYNVEC          = 1  | GPU_DYNAMIC_GROUP_LAMP,
178         GPU_DYNAMIC_LAMP_DYNCO           = 2  | GPU_DYNAMIC_GROUP_LAMP,
179         GPU_DYNAMIC_LAMP_DYNIMAT         = 3  | GPU_DYNAMIC_GROUP_LAMP,
180         GPU_DYNAMIC_LAMP_DYNPERSMAT      = 4  | GPU_DYNAMIC_GROUP_LAMP,
181         GPU_DYNAMIC_LAMP_DYNENERGY       = 5  | GPU_DYNAMIC_GROUP_LAMP,
182         GPU_DYNAMIC_LAMP_DYNCOL          = 6  | GPU_DYNAMIC_GROUP_LAMP,
183         GPU_DYNAMIC_LAMP_DISTANCE        = 7  | GPU_DYNAMIC_GROUP_LAMP,
184         GPU_DYNAMIC_LAMP_ATT1            = 8  | GPU_DYNAMIC_GROUP_LAMP,
185         GPU_DYNAMIC_LAMP_ATT2            = 9  | GPU_DYNAMIC_GROUP_LAMP,
186         GPU_DYNAMIC_LAMP_SPOTSIZE        = 10 | GPU_DYNAMIC_GROUP_LAMP,
187         GPU_DYNAMIC_LAMP_SPOTBLEND       = 11 | GPU_DYNAMIC_GROUP_LAMP,
188         GPU_DYNAMIC_LAMP_SPOTSCALE       = 12 | GPU_DYNAMIC_GROUP_LAMP,
189         GPU_DYNAMIC_LAMP_COEFFCONST      = 13 | GPU_DYNAMIC_GROUP_LAMP,
190         GPU_DYNAMIC_LAMP_COEFFLIN        = 14 | GPU_DYNAMIC_GROUP_LAMP,
191         GPU_DYNAMIC_LAMP_COEFFQUAD       = 15 | GPU_DYNAMIC_GROUP_LAMP,
192
193         GPU_DYNAMIC_SAMPLER_2DBUFFER     = 1  | GPU_DYNAMIC_GROUP_SAMPLER,
194         GPU_DYNAMIC_SAMPLER_2DIMAGE      = 2  | GPU_DYNAMIC_GROUP_SAMPLER,
195         GPU_DYNAMIC_SAMPLER_2DSHADOW     = 3  | GPU_DYNAMIC_GROUP_SAMPLER,
196
197         GPU_DYNAMIC_MIST_ENABLE          = 1  | GPU_DYNAMIC_GROUP_MIST,
198         GPU_DYNAMIC_MIST_START           = 2  | GPU_DYNAMIC_GROUP_MIST,
199         GPU_DYNAMIC_MIST_DISTANCE        = 3  | GPU_DYNAMIC_GROUP_MIST,
200         GPU_DYNAMIC_MIST_INTENSITY       = 4  | GPU_DYNAMIC_GROUP_MIST,
201         GPU_DYNAMIC_MIST_TYPE            = 5  | GPU_DYNAMIC_GROUP_MIST,
202         GPU_DYNAMIC_MIST_COLOR           = 6  | GPU_DYNAMIC_GROUP_MIST,
203
204         GPU_DYNAMIC_HORIZON_COLOR        = 1  | GPU_DYNAMIC_GROUP_WORLD,
205         GPU_DYNAMIC_AMBIENT_COLOR        = 2  | GPU_DYNAMIC_GROUP_WORLD,
206         GPU_DYNAMIC_ZENITH_COLOR         = 3  | GPU_DYNAMIC_GROUP_WORLD,
207
208         GPU_DYNAMIC_MAT_DIFFRGB          = 1  | GPU_DYNAMIC_GROUP_MAT,
209         GPU_DYNAMIC_MAT_REF              = 2  | GPU_DYNAMIC_GROUP_MAT,
210         GPU_DYNAMIC_MAT_SPECRGB          = 3  | GPU_DYNAMIC_GROUP_MAT,
211         GPU_DYNAMIC_MAT_SPEC             = 4  | GPU_DYNAMIC_GROUP_MAT,
212         GPU_DYNAMIC_MAT_HARD             = 5  | GPU_DYNAMIC_GROUP_MAT,
213         GPU_DYNAMIC_MAT_EMIT             = 6  | GPU_DYNAMIC_GROUP_MAT,
214         GPU_DYNAMIC_MAT_AMB              = 7  | GPU_DYNAMIC_GROUP_MAT,
215         GPU_DYNAMIC_MAT_ALPHA            = 8  | GPU_DYNAMIC_GROUP_MAT,
216         GPU_DYNAMIC_MAT_MIR              = 9  | GPU_DYNAMIC_GROUP_MAT
217 } GPUDynamicType;
218
219 GPUNodeLink *GPU_attribute(CustomDataType type, const char *name);
220 GPUNodeLink *GPU_uniform(float *num);
221 GPUNodeLink *GPU_dynamic_uniform(float *num, GPUDynamicType dynamictype, void *data);
222 GPUNodeLink *GPU_uniform_buffer(float *num, GPUType gputype);
223 GPUNodeLink *GPU_image(struct Image *ima, struct ImageUser *iuser, bool is_data);
224 GPUNodeLink *GPU_cube_map(struct Image *ima, struct ImageUser *iuser, bool is_data);
225 GPUNodeLink *GPU_image_preview(struct PreviewImage *prv);
226 GPUNodeLink *GPU_texture(int size, float *pixels);
227 GPUNodeLink *GPU_dynamic_texture(struct GPUTexture *tex, GPUDynamicType dynamictype, void *data);
228 GPUNodeLink *GPU_builtin(GPUBuiltin builtin);
229 GPUNodeLink *GPU_opengl_builtin(GPUOpenGLBuiltin builtin);
230 void GPU_node_link_set_type(GPUNodeLink *link, GPUType type);
231
232 bool GPU_link(GPUMaterial *mat, const char *name, ...);
233 bool GPU_stack_link(GPUMaterial *mat, struct bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out, ...);
234 GPUNodeLink *GPU_uniformbuffer_link_out(
235         struct GPUMaterial *mat, struct bNode *node,
236         struct GPUNodeStack *stack, const int index);
237
238 void GPU_material_output_link(GPUMaterial *material, GPUNodeLink *link);
239 GPUBuiltin GPU_get_material_builtins(GPUMaterial *material);
240
241 void GPU_material_sss_profile_create(GPUMaterial *material, float *radii, short *falloff_type, float *sharpness);
242 struct GPUUniformBuffer *GPU_material_sss_profile_get(
243         GPUMaterial *material, int sample_ct, struct GPUTexture **tex_profile);
244
245 /* High level functions to create and use GPU materials */
246 GPUMaterial *GPU_material_from_nodetree_find(
247         struct ListBase *gpumaterials, const void *engine_type, int options);
248 GPUMaterial *GPU_material_from_nodetree(
249         struct Scene *scene, struct bNodeTree *ntree, struct ListBase *gpumaterials, const void *engine_type, int options,
250         const char *vert_code, const char *geom_code, const char *frag_lib, const char *defines);
251 void GPU_material_compile(GPUMaterial *mat);
252 void GPU_material_free(struct ListBase *gpumaterial);
253
254 void GPU_materials_free(void);
255
256 struct Scene *GPU_material_scene(GPUMaterial *material);
257 GPUMatType GPU_Material_get_type(GPUMaterial *material);
258 struct GPUPass *GPU_material_get_pass(GPUMaterial *material);
259 struct ListBase *GPU_material_get_inputs(GPUMaterial *material);
260 GPUMaterialStatus GPU_material_status(GPUMaterial *mat);
261
262 struct GPUUniformBuffer *GPU_material_get_uniform_buffer(GPUMaterial *material);
263 void GPU_material_create_uniform_buffer(GPUMaterial *material, struct ListBase *inputs);
264 void GPU_material_uniform_buffer_tag_dirty(struct ListBase *gpumaterials);
265
266 void GPU_material_vertex_attributes(GPUMaterial *material,
267         struct GPUVertexAttribs *attrib);
268
269 bool GPU_material_do_color_management(GPUMaterial *mat);
270 bool GPU_material_use_domain_surface(GPUMaterial *mat);
271 bool GPU_material_use_domain_volume(GPUMaterial *mat);
272
273 void GPU_pass_cache_init(void);
274 void GPU_pass_cache_garbage_collect(void);
275 void GPU_pass_cache_free(void);
276
277 #ifdef __cplusplus
278 }
279 #endif
280
281 #endif /*__GPU_MATERIAL_H__*/