5ae46c060dbd8f3baa93dc77eddbabde088adb99
[blender.git] / source / blender / draw / engines / workbench / solid_flat_mode.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 solid_flat_mode.c
23  *  \ingroup draw_engine
24  *
25  * Simple engine for drawing color and/or depth.
26  * When we only need simple flat shaders.
27  */
28
29 #include "DRW_render.h"
30
31 #include "GPU_shader.h"
32
33 #include "workbench_private.h"
34 /* Functions */
35
36 static void workbench_solid_flat_engine_init(void *UNUSED(vedata))
37 {
38         workbench_materials_engine_init();
39 }
40
41 static void workbench_solid_flat_cache_init(void *vedata)
42 {
43         WORKBENCH_Data * data = (WORKBENCH_Data *)vedata;
44         WORKBENCH_PassList *psl = data->psl;
45         WORKBENCH_StorageList *stl = data->stl;
46
47         if (!stl->g_data) {
48                 /* Alloc transient pointers */
49                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
50         }
51
52         /* Depth Pass */
53         {
54                 int state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS;
55                 psl->depth_pass = DRW_pass_create("Depth Pass", state);
56         }
57
58         /* Solid Pass */
59         {
60                 int state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL;
61                 psl->solid_pass = DRW_pass_create("Solid Pass", state);
62         }
63
64         workbench_materials_cache_init(data);
65 }
66
67
68 static void workbench_solid_flat_cache_populate(void *vedata, Object *ob)
69 {
70         WORKBENCH_Data * data = (WORKBENCH_Data *)vedata;
71         WORKBENCH_StorageList *stl = data->stl;
72
73         IDProperty *props = BKE_layer_collection_engine_evaluated_get(ob, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_WORKBENCH);
74         const float* color = BKE_collection_engine_property_value_get_float_array(props, "object_color");
75
76         if (!DRW_object_is_renderable(ob))
77                 return;
78
79         struct Gwn_Batch *geom = DRW_cache_object_surface_get(ob);
80         WORKBENCH_MaterialData *material;
81         if (geom) {
82                 /* Depth */
83                 DRW_shgroup_call_add(stl->g_data->depth_shgrp, geom, ob->obmat);
84
85                 /* Solid */
86                 material = workbench_get_or_create_solid_flat_material_data(data, color);
87                 DRW_shgroup_call_add(material->shgrp, geom, ob->obmat);
88         }
89 }
90
91 static void workbench_solid_flat_cache_finish(void *vedata)
92 {
93         workbench_materials_cache_finish((WORKBENCH_Data*)vedata);
94 }
95
96 static void workbench_solid_flat_draw_scene(void *vedata)
97 {
98         WORKBENCH_Data *data = (WORKBENCH_Data *)vedata;
99         WORKBENCH_PassList *psl = data->psl;
100
101         DRW_draw_pass(psl->depth_pass);
102         DRW_draw_pass(psl->solid_pass);
103 }
104
105 static void workbench_solid_flat_engine_free(void)
106 {
107         workbench_materials_engine_finish();
108 }
109
110 static const DrawEngineDataSize workbench_data_size = DRW_VIEWPORT_DATA_SIZE(WORKBENCH_Data);
111
112 DrawEngineType draw_engine_workbench_solid_flat = {
113         NULL, NULL,
114         N_("Workbench"),
115         &workbench_data_size,
116         &workbench_solid_flat_engine_init,
117         &workbench_solid_flat_engine_free,
118         &workbench_solid_flat_cache_init,
119         &workbench_solid_flat_cache_populate,
120         &workbench_solid_flat_cache_finish,
121         NULL,
122         &workbench_solid_flat_draw_scene,
123         NULL,
124         NULL,
125         NULL,
126 };