Workbench: Single Color Modes
[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 *draw_ctx = DRW_context_state_get();
261         ViewLayer *view_layer = draw_ctx->view_layer;
262         IDProperty *props = BKE_view_layer_engine_evaluated_get(view_layer, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_WORKBENCH);
263
264         const DRWContextState *DCS = DRW_context_state_get();
265
266         wpd->material_hash = BLI_ghash_ptr_new(__func__);
267
268         View3D *v3d = DCS->v3d;
269         if (v3d) {
270                 wpd->drawtype_lighting = v3d->drawtype_lighting;
271                 wpd->drawtype_options = v3d->drawtype_options;
272         }
273         else {
274                 wpd->drawtype_lighting = V3D_LIGHTING_STUDIO;
275                 wpd->drawtype_options = 0;
276         }
277
278         select_deferred_shaders(wpd);
279         /* Deferred Mix Pass */
280         {
281                 WORKBENCH_UBO_World *wd = &wpd->world_data;
282                 UI_GetThemeColor3fv(UI_GetThemeValue(TH_SHOW_BACK_GRAD) ? TH_LOW_GRAD:TH_HIGH_GRAD, wd->background_color_low);
283                 UI_GetThemeColor3fv(TH_HIGH_GRAD, wd->background_color_high);
284                 copy_v3_v3(wd->diffuse_light_x_pos, BKE_collection_engine_property_value_get_float_array(props, "diffuse_light_x_pos"));
285                 copy_v3_v3(wd->diffuse_light_x_neg, BKE_collection_engine_property_value_get_float_array(props, "diffuse_light_x_neg"));
286                 copy_v3_v3(wd->diffuse_light_y_pos, BKE_collection_engine_property_value_get_float_array(props, "diffuse_light_y_pos"));
287                 copy_v3_v3(wd->diffuse_light_y_neg, BKE_collection_engine_property_value_get_float_array(props, "diffuse_light_y_neg"));
288                 copy_v3_v3(wd->diffuse_light_z_pos, BKE_collection_engine_property_value_get_float_array(props, "diffuse_light_z_pos"));
289                 copy_v3_v3(wd->diffuse_light_z_neg, BKE_collection_engine_property_value_get_float_array(props, "diffuse_light_z_neg"));
290
291                 psl->composite_pass = DRW_pass_create("Composite", DRW_STATE_WRITE_COLOR);
292                 grp = DRW_shgroup_create(wpd->composite_sh, psl->composite_pass);
293                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
294                 DRW_shgroup_uniform_texture_ref(grp, "colorBuffer", &e_data.color_buffer_tx);
295                 if (OBJECT_ID_PASS_ENABLED(wpd)) {
296                         DRW_shgroup_uniform_texture_ref(grp, "objectId", &e_data.object_id_tx);
297                 }
298                 if (NORMAL_VIEWPORT_PASS_ENABLED(wpd)) {
299                         DRW_shgroup_uniform_texture_ref(grp, "normalBuffer", &e_data.normal_buffer_tx);
300                 }
301                 wpd->world_ubo = DRW_uniformbuffer_create(sizeof(WORKBENCH_UBO_World), NULL);
302                 DRW_shgroup_uniform_block(grp, "world_block", wpd->world_ubo);
303                 DRW_uniformbuffer_update(wpd->world_ubo, &wpd->world_data);
304                 DRW_shgroup_uniform_vec2(grp, "invertedViewportSize", DRW_viewport_invert_size_get(), 1);
305
306                 DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
307         }
308
309
310 }
311 static WORKBENCH_MaterialData *get_or_create_material_data(WORKBENCH_Data *vedata, IDProperty *props, Object *ob, Material *mat)
312 {
313         WORKBENCH_StorageList *stl = vedata->stl;
314         WORKBENCH_PassList *psl = vedata->psl;
315         WORKBENCH_PrivateData *wpd = stl->g_data;
316         WORKBENCH_MaterialData *material;
317         WORKBENCH_ObjectData *engine_object_data = (WORKBENCH_ObjectData *)DRW_object_engine_data_ensure(
318                 ob, &draw_engine_workbench_solid, sizeof(WORKBENCH_ObjectData), &workbench_init_object_data, NULL);
319         WORKBENCH_MaterialData material_template;
320         float color[3];
321         const float hsv_saturation = BKE_collection_engine_property_value_get_float(props, "random_object_color_saturation");
322         const float hsv_value = BKE_collection_engine_property_value_get_float(props, "random_object_color_value");
323
324         /* Solid */
325         get_material_solid_color(wpd, engine_object_data, ob, mat, color, hsv_saturation, hsv_value);
326         copy_v3_v3(material_template.color, color);
327         material_template.object_id = engine_object_data->object_id;
328         unsigned int hash = get_material_hash(wpd, &material_template);
329
330         material = BLI_ghash_lookup(wpd->material_hash, SET_UINT_IN_POINTER(hash));
331         if (material == NULL) {
332                 material = MEM_mallocN(sizeof(WORKBENCH_MaterialData), __func__);
333                 material->shgrp = DRW_shgroup_create(wpd->prepass_sh, psl->prepass_pass);
334                 material->object_id = engine_object_data->object_id;
335                 copy_v3_v3(material->color, material_template.color);
336                 DRW_shgroup_uniform_vec3(material->shgrp, "object_color", material->color, 1);
337                 DRW_shgroup_uniform_int(material->shgrp, "object_id", &material->object_id, 1);
338                 BLI_ghash_insert(wpd->material_hash, SET_UINT_IN_POINTER(hash), material);
339         }
340         return material;
341 }
342
343 static void workbench_cache_populate_particles(WORKBENCH_Data *vedata, IDProperty *props, Object *ob)
344 {
345         const DRWContextState *draw_ctx = DRW_context_state_get();
346
347         if (ob != draw_ctx->object_edit) {
348                 for (ModifierData *md = ob->modifiers.first; md; md = md->next) {
349                         if (md->type == eModifierType_ParticleSystem) {
350                                 ParticleSystem *psys = ((ParticleSystemModifierData *)md)->psys;
351
352                                 if (psys_check_enabled(ob, psys, false)) {
353                                         ParticleSettings *part = psys->part;
354                                         int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
355
356                                         if (draw_as == PART_DRAW_PATH && !psys->pathcache && !psys->childcache) {
357                                                 draw_as = PART_DRAW_DOT;
358                                         }
359
360                                         static float mat[4][4];
361                                         unit_m4(mat);
362
363                                         if (draw_as == PART_DRAW_PATH) {
364                                                 struct Gwn_Batch *geom = DRW_cache_particles_get_hair(psys, NULL);
365                                                 WORKBENCH_MaterialData *material = get_or_create_material_data(vedata, props, ob, NULL);
366                                                 DRW_shgroup_call_add(material->shgrp, geom, mat);
367                                         }
368                                 }
369                         }
370                 }
371         }
372 }
373
374 void workbench_materials_solid_cache_populate(WORKBENCH_Data *vedata, Object *ob)
375 {
376         if (!DRW_object_is_renderable(ob))
377                 return;
378
379         IDProperty *props = BKE_layer_collection_engine_evaluated_get(ob, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_WORKBENCH);
380         if (ob->type == OB_MESH) {
381                 workbench_cache_populate_particles(vedata, props, ob);
382         }
383         
384         WORKBENCH_MaterialData *material;
385         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT)) {
386                 const DRWContextState *draw_ctx = DRW_context_state_get();
387                 const bool is_active = (ob == draw_ctx->obact);
388                 const bool is_sculpt_mode = is_active && (draw_ctx->object_mode & OB_MODE_SCULPT) != 0;
389
390                 if ((vedata->stl->g_data->drawtype_options & V3D_DRAWOPTION_SOLID_COLOR_MASK) != 0 || is_sculpt_mode) {
391                         /* No material split needed */
392                         struct Gwn_Batch *geom = DRW_cache_object_surface_get(ob);
393                         if (geom) {
394                                 material = get_or_create_material_data(vedata, props, ob, NULL);
395                                 if (is_sculpt_mode) {
396                                         DRW_shgroup_call_sculpt_add(material->shgrp, ob, ob->obmat);
397                                 }
398                                 else {
399                                         DRW_shgroup_call_object_add(material->shgrp, geom, ob);
400                                 }
401                         }
402                 } 
403                 else { /* MATERIAL colors */
404                         const int materials_len = MAX2(1, (is_sculpt_mode ? 1 : ob->totcol));
405                         struct GPUMaterial **gpumat_array = BLI_array_alloca(gpumat_array, materials_len);
406                         for (int i = 0; i < materials_len; i ++) {
407                                 gpumat_array[i] = NULL;
408                         }
409
410                         struct Gwn_Batch **mat_geom = DRW_cache_object_surface_material_get(ob, gpumat_array, materials_len);
411                         if (mat_geom) {
412                                 for (int i = 0; i < materials_len; ++i) {
413                                         Material *mat = give_current_material(ob, i + 1);
414                                         material = get_or_create_material_data(vedata, props, ob, mat);
415                                         DRW_shgroup_call_object_add(material->shgrp, mat_geom[i], ob);
416                                 }
417                         }
418                 }
419         }
420 }
421
422 void workbench_materials_cache_finish(WORKBENCH_Data *UNUSED(vedata))
423 {
424 }
425
426 void workbench_materials_draw_scene(WORKBENCH_Data *vedata)
427 {
428         WORKBENCH_PassList *psl = vedata->psl;
429         WORKBENCH_StorageList *stl = vedata->stl;
430         WORKBENCH_FramebufferList *fbl = vedata->fbl;
431         WORKBENCH_PrivateData *wpd = stl->g_data;
432         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
433         const float clear_depth = 1.0f;
434
435         GPU_framebuffer_bind(fbl->prepass_fb);
436         if (OBJECT_ID_PASS_ENABLED(wpd)) {
437                 const float clear_color[4] = {0.0f, 0.0f, 0.0f, 0.0f};
438                 GPU_framebuffer_clear_color_depth(fbl->prepass_fb, clear_color, clear_depth);
439         }
440         else {
441                 GPU_framebuffer_clear_depth(fbl->prepass_fb, clear_depth);
442         }
443         DRW_draw_pass(psl->prepass_pass);
444
445         GPU_framebuffer_bind(dfbl->color_only_fb);
446         DRW_draw_pass(psl->composite_pass);
447
448         BLI_ghash_free(wpd->material_hash, NULL, MEM_freeN);
449         DRW_UBO_FREE_SAFE(wpd->world_ubo);
450
451 }