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 "DNA_customdata_types.h" /* for CustomDataType */
27 #include "DNA_listBase.h"
28 
29 #include "BLI_sys_types.h" /* for bool */
30 
31 #include "GPU_texture.h" /* for eGPUSamplerState */
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 struct GPUMaterial;
38 struct GPUNode;
39 struct GPUNodeLink;
40 struct GPUNodeStack;
41 struct GPUTexture;
42 struct GPUUniformBuf;
43 struct Image;
44 struct ImageUser;
45 struct ListBase;
46 struct Main;
47 struct Material;
48 struct Scene;
49 struct bNode;
50 struct bNodeTree;
51 
52 typedef struct GPUMaterial GPUMaterial;
53 typedef struct GPUNode GPUNode;
54 typedef struct GPUNodeLink GPUNodeLink;
55 
56 /* Functions to create GPU Materials nodes */
57 
58 typedef enum eGPUType {
59   /* Keep in sync with GPU_DATATYPE_STR */
60   /* The value indicates the number of elements in each type */
61   GPU_NONE = 0,
62   GPU_FLOAT = 1,
63   GPU_VEC2 = 2,
64   GPU_VEC3 = 3,
65   GPU_VEC4 = 4,
66   GPU_MAT3 = 9,
67   GPU_MAT4 = 16,
68   GPU_MAX_CONSTANT_DATA = GPU_MAT4,
69 
70   /* Values not in GPU_DATATYPE_STR */
71   GPU_TEX1D_ARRAY = 1001,
72   GPU_TEX2D = 1002,
73   GPU_TEX2D_ARRAY = 1003,
74   GPU_TEX3D = 1004,
75   GPU_SHADOW2D = 1005,
76   GPU_TEXCUBE = 1006,
77 
78   /* GLSL Struct types */
79   GPU_CLOSURE = 1007,
80 
81   /* Opengl Attributes */
82   GPU_ATTR = 3001,
83 } eGPUType;
84 
85 typedef enum eGPUBuiltin {
86   GPU_VIEW_MATRIX = (1 << 0),
87   GPU_OBJECT_MATRIX = (1 << 1),
88   GPU_INVERSE_VIEW_MATRIX = (1 << 2),
89   GPU_INVERSE_OBJECT_MATRIX = (1 << 3),
90   GPU_VIEW_POSITION = (1 << 4),
91   GPU_VIEW_NORMAL = (1 << 5),
92   GPU_OBJECT_COLOR = (1 << 6),
93   GPU_AUTO_BUMPSCALE = (1 << 7),
94   GPU_CAMERA_TEXCO_FACTORS = (1 << 8),
95   GPU_PARTICLE_SCALAR_PROPS = (1 << 9),
96   GPU_PARTICLE_LOCATION = (1 << 10),
97   GPU_PARTICLE_VELOCITY = (1 << 11),
98   GPU_PARTICLE_ANG_VELOCITY = (1 << 12),
99   GPU_LOC_TO_VIEW_MATRIX = (1 << 13),
100   GPU_INVERSE_LOC_TO_VIEW_MATRIX = (1 << 14),
101   GPU_OBJECT_INFO = (1 << 15),
102   GPU_BARYCENTRIC_TEXCO = (1 << 16),
103   GPU_BARYCENTRIC_DIST = (1 << 17),
104   GPU_WORLD_NORMAL = (1 << 18),
105 } eGPUBuiltin;
106 
107 typedef enum eGPUMatFlag {
108   GPU_MATFLAG_DIFFUSE = (1 << 0),
109   GPU_MATFLAG_GLOSSY = (1 << 1),
110   GPU_MATFLAG_REFRACT = (1 << 2),
111   GPU_MATFLAG_SSS = (1 << 3),
112   GPU_MATFLAG_BARYCENTRIC = (1 << 4),
113 } eGPUMatFlag;
114 
115 typedef struct GPUNodeStack {
116   eGPUType type;
117   float vec[4];
118   struct GPUNodeLink *link;
119   bool hasinput;
120   bool hasoutput;
121   short sockettype;
122   bool end;
123 } GPUNodeStack;
124 
125 typedef enum eGPUMaterialStatus {
126   GPU_MAT_FAILED = 0,
127   GPU_MAT_QUEUED,
128   GPU_MAT_SUCCESS,
129 } eGPUMaterialStatus;
130 
131 typedef enum eGPUVolumeDefaultValue {
132   GPU_VOLUME_DEFAULT_0,
133   GPU_VOLUME_DEFAULT_1,
134 } eGPUVolumeDefaultValue;
135 
136 typedef void (*GPUMaterialEvalCallbackFn)(GPUMaterial *mat,
137                                           int options,
138                                           const char **vert_code,
139                                           const char **geom_code,
140                                           const char **frag_lib,
141                                           const char **defines);
142 
143 GPUNodeLink *GPU_constant(const float *num);
144 GPUNodeLink *GPU_uniform(const float *num);
145 GPUNodeLink *GPU_attribute(GPUMaterial *mat, CustomDataType type, const char *name);
146 GPUNodeLink *GPU_image(GPUMaterial *mat,
147                        struct Image *ima,
148                        struct ImageUser *iuser,
149                        eGPUSamplerState sampler_state);
150 GPUNodeLink *GPU_image_tiled(GPUMaterial *mat,
151                              struct Image *ima,
152                              struct ImageUser *iuser,
153                              eGPUSamplerState sampler_state);
154 GPUNodeLink *GPU_image_tiled_mapping(GPUMaterial *mat, struct Image *ima, struct ImageUser *iuser);
155 GPUNodeLink *GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *row);
156 GPUNodeLink *GPU_volume_grid(GPUMaterial *mat,
157                              const char *name,
158                              eGPUVolumeDefaultValue default_value);
159 GPUNodeLink *GPU_builtin(eGPUBuiltin builtin);
160 
161 bool GPU_link(GPUMaterial *mat, const char *name, ...);
162 bool GPU_stack_link(GPUMaterial *mat,
163                     struct bNode *node,
164                     const char *name,
165                     GPUNodeStack *in,
166                     GPUNodeStack *out,
167                     ...);
168 GPUNodeLink *GPU_uniformbuf_link_out(struct GPUMaterial *mat,
169                                      struct bNode *node,
170                                      struct GPUNodeStack *stack,
171                                      const int index);
172 
173 void GPU_material_output_link(GPUMaterial *material, GPUNodeLink *link);
174 
175 void GPU_material_sss_profile_create(GPUMaterial *material,
176                                      float radii[3],
177                                      const short *falloff_type,
178                                      const float *sharpness);
179 struct GPUUniformBuf *GPU_material_sss_profile_get(GPUMaterial *material,
180                                                    int sample_len,
181                                                    struct GPUTexture **tex_profile);
182 
183 /* High level functions to create and use GPU materials */
184 GPUMaterial *GPU_material_from_nodetree_find(struct ListBase *gpumaterials,
185                                              const void *engine_type,
186                                              int options);
187 GPUMaterial *GPU_material_from_nodetree(struct Scene *scene,
188                                         struct Material *ma,
189                                         struct bNodeTree *ntree,
190                                         struct ListBase *gpumaterials,
191                                         const void *engine_type,
192                                         const int options,
193                                         const bool is_volume_shader,
194                                         const char *vert_code,
195                                         const char *geom_code,
196                                         const char *frag_lib,
197                                         const char *defines,
198                                         const char *name,
199                                         GPUMaterialEvalCallbackFn callback);
200 void GPU_material_compile(GPUMaterial *mat);
201 void GPU_material_free(struct ListBase *gpumaterial);
202 
203 void GPU_materials_free(struct Main *bmain);
204 
205 struct Scene *GPU_material_scene(GPUMaterial *material);
206 struct GPUPass *GPU_material_get_pass(GPUMaterial *material);
207 struct GPUShader *GPU_material_get_shader(GPUMaterial *material);
208 struct Material *GPU_material_get_material(GPUMaterial *material);
209 eGPUMaterialStatus GPU_material_status(GPUMaterial *mat);
210 
211 struct GPUUniformBuf *GPU_material_uniform_buffer_get(GPUMaterial *material);
212 void GPU_material_uniform_buffer_create(GPUMaterial *material, ListBase *inputs);
213 struct GPUUniformBuf *GPU_material_create_sss_profile_ubo(void);
214 
215 bool GPU_material_has_surface_output(GPUMaterial *mat);
216 bool GPU_material_has_volume_output(GPUMaterial *mat);
217 
218 bool GPU_material_is_volume_shader(GPUMaterial *mat);
219 
220 void GPU_material_flag_set(GPUMaterial *mat, eGPUMatFlag flag);
221 bool GPU_material_flag_get(GPUMaterial *mat, eGPUMatFlag flag);
222 
223 void GPU_pass_cache_init(void);
224 void GPU_pass_cache_garbage_collect(void);
225 void GPU_pass_cache_free(void);
226 
227 /* Requested Material Attributes and Textures */
228 
229 typedef struct GPUMaterialAttribute {
230   struct GPUMaterialAttribute *next, *prev;
231   int type;      /* CustomDataType */
232   char name[64]; /* MAX_CUSTOMDATA_LAYER_NAME */
233   eGPUType gputype;
234   int id;
235   int users;
236 } GPUMaterialAttribute;
237 
238 typedef struct GPUMaterialTexture {
239   struct GPUMaterialTexture *next, *prev;
240   struct Image *ima;
241   struct ImageUser *iuser;
242   struct GPUTexture **colorband;
243   char sampler_name[32];       /* Name of sampler in GLSL. */
244   char tiled_mapping_name[32]; /* Name of tile mapping sampler in GLSL. */
245   int users;
246   int sampler_state; /* eGPUSamplerState */
247 } GPUMaterialTexture;
248 
249 typedef struct GPUMaterialVolumeGrid {
250   struct GPUMaterialVolumeGrid *next, *prev;
251   char *name;
252   eGPUVolumeDefaultValue default_value;
253   char sampler_name[32];   /* Name of sampler in GLSL. */
254   char transform_name[32]; /* Name of 4x4 matrix in GLSL. */
255   int users;
256 } GPUMaterialVolumeGrid;
257 
258 ListBase GPU_material_attributes(GPUMaterial *material);
259 ListBase GPU_material_textures(GPUMaterial *material);
260 ListBase GPU_material_volume_grids(GPUMaterial *material);
261 
262 #ifdef __cplusplus
263 }
264 #endif
265