Workbench: studiolight
[blender.git] / source / blender / draw / engines / workbench / workbench_materials.c
1 /*
2  * Copyright 2016, Blender Foundation.
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  * Contributor(s): Blender Institute
19  *
20  */
21
22 /** \file workbench_materials.c
23  *  \ingroup draw_engine
24  */
25
26 #include "workbench_private.h"
27
28 #include "BLI_dynstr.h"
29 #include "BLI_alloca.h"
30
31 #include "BKE_particle.h"
32
33 #include "DNA_modifier_types.h"
34
35 #include "GPU_shader.h"
36
37 #include "UI_resources.h"
38
39 /* *********** STATIC *********** */
40 #define MAX_SHADERS 255
41 static struct {
42         struct GPUShader *prepass_sh_cache[MAX_SHADERS];
43         struct GPUShader *composite_sh_cache[MAX_SHADERS];
44
45         struct GPUTexture *object_id_tx; /* ref only, not alloced */
46         struct GPUTexture *color_buffer_tx; /* ref only, not alloced */
47         struct GPUTexture *normal_buffer_tx; /* ref only, not alloced */
48
49         int next_object_id;
50 } e_data = {{NULL}};
51
52 /* Shaders */
53 extern char datatoc_workbench_prepass_vert_glsl[];
54 extern char datatoc_workbench_prepass_frag_glsl[];
55 extern char datatoc_workbench_composite_frag_glsl[];
56
57 extern char datatoc_workbench_background_lib_glsl[];
58 extern char datatoc_workbench_common_lib_glsl[];
59 extern char datatoc_workbench_data_lib_glsl[];
60 extern char datatoc_workbench_object_overlap_lib_glsl[];
61 extern char datatoc_workbench_world_light_lib_glsl[];
62
63 extern DrawEngineType draw_engine_workbench_solid;
64
65 #define OBJECT_ID_PASS_ENABLED(wpd) (wpd->drawtype_options & V3D_DRAWOPTION_OBJECT_OVERLAP)
66 #define NORMAL_VIEWPORT_PASS_ENABLED(wpd) (wpd->drawtype_lighting & V3D_LIGHTING_STUDIO)
67
68 static char *workbench_build_defines(WORKBENCH_PrivateData *wpd)
69 {
70         char *str = NULL;
71
72         DynStr *ds = BLI_dynstr_new();
73
74         if (wpd->drawtype_options & V3D_DRAWOPTION_OBJECT_OVERLAP) {
75                 BLI_dynstr_appendf(ds, "#define V3D_DRAWOPTION_OBJECT_OVERLAP\n");
76         }
77         if (wpd->drawtype_lighting & V3D_LIGHTING_STUDIO) {
78                 BLI_dynstr_appendf(ds, "#define V3D_LIGHTING_STUDIO\n");
79         }
80
81 #ifdef WORKBENCH_ENCODE_NORMALS
82         BLI_dynstr_appendf(ds, "#define WORKBENCH_ENCODE_NORMALS\n");
83 #endif
84
85         str = BLI_dynstr_get_cstring(ds);
86         BLI_dynstr_free(ds);
87         return str;
88 }
89
90 static char *workbench_build_composite_frag(WORKBENCH_PrivateData *wpd)
91 {
92         char *str = NULL;
93
94         DynStr *ds = BLI_dynstr_new();
95
96         BLI_dynstr_append(ds, datatoc_workbench_data_lib_glsl);
97         BLI_dynstr_append(ds, datatoc_workbench_common_lib_glsl);
98         BLI_dynstr_append(ds, datatoc_workbench_background_lib_glsl);
99
100         if (wpd->drawtype_lighting & V3D_LIGHTING_STUDIO) {
101                 BLI_dynstr_append(ds, datatoc_workbench_world_light_lib_glsl);
102         }
103         if (wpd->drawtype_options & V3D_DRAWOPTION_OBJECT_OVERLAP) {
104                 BLI_dynstr_append(ds, datatoc_workbench_object_overlap_lib_glsl);
105         }
106
107         BLI_dynstr_append(ds, datatoc_workbench_composite_frag_glsl);
108
109         str = BLI_dynstr_get_cstring(ds);
110         BLI_dynstr_free(ds);
111         return str;
112 }
113
114 static char *workbench_build_prepass_frag(void)
115 {
116         char *str = NULL;
117
118         DynStr *ds = BLI_dynstr_new();
119
120         BLI_dynstr_append(ds, datatoc_workbench_common_lib_glsl);
121         BLI_dynstr_append(ds, datatoc_workbench_prepass_frag_glsl);
122
123         str = BLI_dynstr_get_cstring(ds);
124         BLI_dynstr_free(ds);
125         return str;
126 }
127
128 static int get_shader_index(WORKBENCH_PrivateData *wpd)
129 {
130         return (wpd->drawtype_options << 2) + wpd->drawtype_lighting;
131 }
132
133 static void select_deferred_shaders(WORKBENCH_PrivateData *wpd)
134 {
135         int index = get_shader_index(wpd);
136
137         if (e_data.prepass_sh_cache[index] == NULL) {
138                 char *defines = workbench_build_defines(wpd);
139                 char *composite_frag = workbench_build_composite_frag(wpd);
140                 char *prepass_frag = workbench_build_prepass_frag();
141                 e_data.prepass_sh_cache[index] = DRW_shader_create(datatoc_workbench_prepass_vert_glsl, NULL, prepass_frag, defines);
142                 e_data.composite_sh_cache[index] = DRW_shader_create_fullscreen(composite_frag, defines);
143                 MEM_freeN(prepass_frag);
144                 MEM_freeN(composite_frag);
145                 MEM_freeN(defines);
146         }
147
148         wpd->prepass_sh = e_data.prepass_sh_cache[index];
149         wpd->composite_sh = e_data.composite_sh_cache[index];
150 }
151
152 /* Functions */
153 static uint get_material_hash(WORKBENCH_PrivateData *wpd, WORKBENCH_MaterialData *material_template)
154 {
155         uint input[4];
156         float *color = material_template->color;
157         input[0] = (uint)(color[0] * 512);
158         input[1] = (uint)(color[1] * 512);
159         input[2] = (uint)(color[2] * 512);
160
161         /* Only hash object id when needed */
162         input[3] = (uint)0;
163         if (OBJECT_ID_PASS_ENABLED(wpd)) {
164                 input[3] = material_template->object_id;
165         }
166
167         return BLI_ghashutil_uinthash_v4_murmur(input);
168 }
169
170 static void workbench_init_object_data(ObjectEngineData *engine_data)
171 {
172         WORKBENCH_ObjectData *data = (WORKBENCH_ObjectData *)engine_data;
173         data->object_id = e_data.next_object_id++;
174 }
175
176 static void get_material_solid_color(WORKBENCH_PrivateData *wpd, WORKBENCH_ObjectData *engine_object_data, Object *ob, Material *mat, float *color, float hsv_saturation, float hsv_value)
177 {
178         static float default_color[] = {1.0f, 1.0f, 1.0f};
179         if (DRW_object_is_paint_mode(ob) || wpd->drawtype_options & V3D_DRAWOPTION_SINGLE_COLOR) {
180                 copy_v3_v3(color, default_color);
181         }
182         else if (wpd->drawtype_options & V3D_DRAWOPTION_RANDOMIZE) {
183                 float offset = fmodf(engine_object_data->object_id * M_GOLDEN_RATION_CONJUGATE, 1.0);
184                 float hsv[3] = {offset, hsv_saturation, hsv_value};
185                 hsv_to_rgb_v(hsv, color);
186         }
187         else if (wpd->drawtype_options & V3D_DRAWOPTION_OBJECT_COLOR) {
188                 copy_v3_v3(color, ob->col);
189         }
190         else {
191                 /* V3D_DRAWOPTION_MATERIAL_COLOR */
192                 if (mat) {
193                         copy_v3_v3(color, &mat->r);
194                 }
195                 else {
196                         copy_v3_v3(color, default_color);
197                 }
198         }
199 }
200
201 void workbench_materials_engine_init(WORKBENCH_Data *vedata)
202 {
203         WORKBENCH_FramebufferList *fbl = vedata->fbl;
204         WORKBENCH_StorageList *stl = vedata->stl;
205         WORKBENCH_PassList *psl = vedata->psl;
206         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
207
208         if (!e_data.next_object_id) {
209                 memset(e_data.prepass_sh_cache,   0x00, sizeof(struct GPUShader *) * MAX_SHADERS);
210                 memset(e_data.composite_sh_cache, 0x00, sizeof(struct GPUShader *) * MAX_SHADERS);
211                 e_data.next_object_id = 1;
212         }
213
214         if (!stl->g_data) {
215                 /* Alloc transient pointers */
216                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
217         }
218
219         {
220                 const float *viewport_size = DRW_viewport_size_get();
221                 const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
222                 e_data.object_id_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_R32UI, &draw_engine_workbench_solid);
223                 e_data.color_buffer_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_RGBA8, &draw_engine_workbench_solid);
224 #ifdef WORKBENCH_ENCODE_NORMALS
225                 e_data.normal_buffer_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_RG8, &draw_engine_workbench_solid);
226 #else
227                 e_data.normal_buffer_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_RGBA32F, &draw_engine_workbench_solid);
228 #endif
229
230                 GPU_framebuffer_ensure_config(&fbl->prepass_fb, {
231                         GPU_ATTACHMENT_TEXTURE(dtxl->depth),
232                         GPU_ATTACHMENT_TEXTURE(e_data.object_id_tx),
233                         GPU_ATTACHMENT_TEXTURE(e_data.color_buffer_tx),
234                         GPU_ATTACHMENT_TEXTURE(e_data.normal_buffer_tx),
235                 });
236         }
237
238         /* Prepass */
239         {
240                 int state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS;
241                 psl->prepass_pass = DRW_pass_create("Prepass", state);
242         }
243 }
244
245 void workbench_materials_engine_free()
246 {
247         for (int index = 0; index < MAX_SHADERS; index ++) {
248                 DRW_SHADER_FREE_SAFE(e_data.prepass_sh_cache[index]);
249                 DRW_SHADER_FREE_SAFE(e_data.composite_sh_cache[index]);
250         }
251 }
252
253 void workbench_materials_cache_init(WORKBENCH_Data *vedata)
254 {
255         WORKBENCH_StorageList *stl = vedata->stl;
256         WORKBENCH_PassList *psl = vedata->psl;
257         WORKBENCH_PrivateData *wpd = stl->g_data;
258         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
259         DRWShadingGroup *grp;
260         const DRWContextState *DCS = DRW_context_state_get();
261
262         wpd->material_hash = BLI_ghash_ptr_new(__func__);
263
264         View3D *v3d = DCS->v3d;
265         if (v3d) {
266                 wpd->drawtype_lighting = v3d->drawtype_lighting;
267                 wpd->drawtype_options = v3d->drawtype_options;
268                 wpd->drawtype_studiolight = v3d->drawtype_studiolight;
269         }
270         else {
271                 wpd->drawtype_lighting = V3D_LIGHTING_STUDIO;
272                 wpd->drawtype_options = 0;
273                 wpd->drawtype_studiolight = 0;
274         }
275
276         select_deferred_shaders(wpd);
277         /* Deferred Mix Pass */
278         {
279                 WORKBENCH_UBO_World *wd = &wpd->world_data;
280                 UI_GetThemeColor3fv(UI_GetThemeValue(TH_SHOW_BACK_GRAD) ? TH_LOW_GRAD:TH_HIGH_GRAD, wd->background_color_low);
281                 UI_GetThemeColor3fv(TH_HIGH_GRAD, wd->background_color_high);
282                 studiolight_update_world(wpd->drawtype_studiolight, wd);
283
284                 psl->composite_pass = DRW_pass_create("Composite", DRW_STATE_WRITE_COLOR);
285                 grp = DRW_shgroup_create(wpd->composite_sh, psl->composite_pass);
286                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
287                 DRW_shgroup_uniform_texture_ref(grp, "colorBuffer", &e_data.color_buffer_tx);
288                 if (OBJECT_ID_PASS_ENABLED(wpd)) {
289                         DRW_shgroup_uniform_texture_ref(grp, "objectId", &e_data.object_id_tx);
290                 }
291                 if (NORMAL_VIEWPORT_PASS_ENABLED(wpd)) {
292                         DRW_shgroup_uniform_texture_ref(grp, "normalBuffer", &e_data.normal_buffer_tx);
293                 }
294                 wpd->world_ubo = DRW_uniformbuffer_create(sizeof(WORKBENCH_UBO_World), NULL);
295                 DRW_shgroup_uniform_block(grp, "world_block", wpd->world_ubo);
296                 DRW_uniformbuffer_update(wpd->world_ubo, &wpd->world_data);
297                 DRW_shgroup_uniform_vec2(grp, "invertedViewportSize", DRW_viewport_invert_size_get(), 1);
298
299                 DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
300         }
301
302
303 }
304 static WORKBENCH_MaterialData *get_or_create_material_data(WORKBENCH_Data *vedata, IDProperty *props, Object *ob, Material *mat)
305 {
306         WORKBENCH_StorageList *stl = vedata->stl;
307         WORKBENCH_PassList *psl = vedata->psl;
308         WORKBENCH_PrivateData *wpd = stl->g_data;
309         WORKBENCH_MaterialData *material;
310         WORKBENCH_ObjectData *engine_object_data = (WORKBENCH_ObjectData *)DRW_object_engine_data_ensure(
311                 ob, &draw_engine_workbench_solid, sizeof(WORKBENCH_ObjectData), &workbench_init_object_data, NULL);
312         WORKBENCH_MaterialData material_template;
313         float color[3];
314         const float hsv_saturation = BKE_collection_engine_property_value_get_float(props, "random_object_color_saturation");
315         const float hsv_value = BKE_collection_engine_property_value_get_float(props, "random_object_color_value");
316
317         /* Solid */
318         get_material_solid_color(wpd, engine_object_data, ob, mat, color, hsv_saturation, hsv_value);
319         copy_v3_v3(material_template.color, color);
320         material_template.object_id = engine_object_data->object_id;
321         unsigned int hash = get_material_hash(wpd, &material_template);
322
323         material = BLI_ghash_lookup(wpd->material_hash, SET_UINT_IN_POINTER(hash));
324         if (material == NULL) {
325                 material = MEM_mallocN(sizeof(WORKBENCH_MaterialData), __func__);
326                 material->shgrp = DRW_shgroup_create(wpd->prepass_sh, psl->prepass_pass);
327                 material->object_id = engine_object_data->object_id;
328                 copy_v3_v3(material->color, material_template.color);
329                 DRW_shgroup_uniform_vec3(material->shgrp, "object_color", material->color, 1);
330                 DRW_shgroup_uniform_int(material->shgrp, "object_id", &material->object_id, 1);
331                 BLI_ghash_insert(wpd->material_hash, SET_UINT_IN_POINTER(hash), material);
332         }
333         return material;
334 }
335
336 static void workbench_cache_populate_particles(WORKBENCH_Data *vedata, IDProperty *props, Object *ob)
337 {
338         const DRWContextState *draw_ctx = DRW_context_state_get();
339
340         if (ob != draw_ctx->object_edit) {
341                 for (ModifierData *md = ob->modifiers.first; md; md = md->next) {
342                         if (md->type == eModifierType_ParticleSystem) {
343                                 ParticleSystem *psys = ((ParticleSystemModifierData *)md)->psys;
344
345                                 if (psys_check_enabled(ob, psys, false)) {
346                                         ParticleSettings *part = psys->part;
347                                         int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
348
349                                         if (draw_as == PART_DRAW_PATH && !psys->pathcache && !psys->childcache) {
350                                                 draw_as = PART_DRAW_DOT;
351                                         }
352
353                                         static float mat[4][4];
354                                         unit_m4(mat);
355
356                                         if (draw_as == PART_DRAW_PATH) {
357                                                 struct Gwn_Batch *geom = DRW_cache_particles_get_hair(psys, NULL);
358                                                 WORKBENCH_MaterialData *material = get_or_create_material_data(vedata, props, ob, NULL);
359                                                 DRW_shgroup_call_add(material->shgrp, geom, mat);
360                                         }
361                                 }
362                         }
363                 }
364         }
365 }
366
367 void workbench_materials_solid_cache_populate(WORKBENCH_Data *vedata, Object *ob)
368 {
369         if (!DRW_object_is_renderable(ob))
370                 return;
371
372         IDProperty *props = BKE_layer_collection_engine_evaluated_get(ob, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_WORKBENCH);
373         if (ob->type == OB_MESH) {
374                 workbench_cache_populate_particles(vedata, props, ob);
375         }
376
377         WORKBENCH_MaterialData *material;
378         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT)) {
379                 const DRWContextState *draw_ctx = DRW_context_state_get();
380                 const bool is_active = (ob == draw_ctx->obact);
381                 const bool is_sculpt_mode = is_active && (draw_ctx->object_mode & OB_MODE_SCULPT) != 0;
382
383                 if ((vedata->stl->g_data->drawtype_options & V3D_DRAWOPTION_SOLID_COLOR_MASK) != 0 || is_sculpt_mode) {
384                         /* No material split needed */
385                         struct Gwn_Batch *geom = DRW_cache_object_surface_get(ob);
386                         if (geom) {
387                                 material = get_or_create_material_data(vedata, props, ob, NULL);
388                                 if (is_sculpt_mode) {
389                                         DRW_shgroup_call_sculpt_add(material->shgrp, ob, ob->obmat);
390                                 }
391                                 else {
392                                         DRW_shgroup_call_object_add(material->shgrp, geom, ob);
393                                 }
394                         }
395                 }
396                 else { /* MATERIAL colors */
397                         const int materials_len = MAX2(1, (is_sculpt_mode ? 1 : ob->totcol));
398                         struct GPUMaterial **gpumat_array = BLI_array_alloca(gpumat_array, materials_len);
399                         for (int i = 0; i < materials_len; i ++) {
400                                 gpumat_array[i] = NULL;
401                         }
402
403                         struct Gwn_Batch **mat_geom = DRW_cache_object_surface_material_get(ob, gpumat_array, materials_len, NULL, NULL, NULL);
404                         if (mat_geom) {
405                                 for (int i = 0; i < materials_len; ++i) {
406                                         Material *mat = give_current_material(ob, i + 1);
407                                         material = get_or_create_material_data(vedata, props, ob, mat);
408                                         DRW_shgroup_call_object_add(material->shgrp, mat_geom[i], ob);
409                                 }
410                         }
411                 }
412         }
413 }
414
415 void workbench_materials_cache_finish(WORKBENCH_Data *UNUSED(vedata))
416 {
417 }
418
419 void workbench_materials_draw_scene(WORKBENCH_Data *vedata)
420 {
421         WORKBENCH_PassList *psl = vedata->psl;
422         WORKBENCH_StorageList *stl = vedata->stl;
423         WORKBENCH_FramebufferList *fbl = vedata->fbl;
424         WORKBENCH_PrivateData *wpd = stl->g_data;
425         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
426         const float clear_depth = 1.0f;
427
428         GPU_framebuffer_bind(fbl->prepass_fb);
429         if (OBJECT_ID_PASS_ENABLED(wpd)) {
430                 const float clear_color[4] = {0.0f, 0.0f, 0.0f, 0.0f};
431                 GPU_framebuffer_clear_color_depth(fbl->prepass_fb, clear_color, clear_depth);
432         }
433         else {
434                 GPU_framebuffer_clear_depth(fbl->prepass_fb, clear_depth);
435         }
436         DRW_draw_pass(psl->prepass_pass);
437
438         GPU_framebuffer_bind(dfbl->color_only_fb);
439         DRW_draw_pass(psl->composite_pass);
440
441         BLI_ghash_free(wpd->material_hash, NULL, MEM_freeN);
442         DRW_UBO_FREE_SAFE(wpd->world_ubo);
443
444 }