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