Workbench: Fix wrong TEXTURE_DRAWING_ENABLED check and rework drawing logic
[blender.git] / source / blender / draw / engines / workbench / workbench_private.h
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_private.h
23  *  \ingroup draw_engine
24  */
25
26 #ifndef __WORKBENCH_PRIVATE_H__
27 #define __WORKBENCH_PRIVATE_H__
28
29
30 #include "BKE_studiolight.h"
31
32 #include "DNA_image_types.h"
33 #include "DNA_view3d_types.h"
34 #include "DNA_world_types.h"
35 #include "DNA_userdef_types.h"
36
37 #include "DRW_render.h"
38
39 #include "workbench_engine.h"
40
41 #define WORKBENCH_ENGINE "BLENDER_WORKBENCH"
42 #define M_GOLDEN_RATION_CONJUGATE 0.618033988749895
43 #define MAX_COMPOSITE_SHADERS (1 << 6)
44 #define MAX_PREPASS_SHADERS (1 << 6)
45 #define MAX_ACCUM_SHADERS (1 << 4)
46 #define MAX_CAVITY_SHADERS (1 << 3)
47
48 #define TEXTURE_DRAWING_ENABLED(wpd) (wpd->shading.color_type == V3D_SHADING_TEXTURE_COLOR)
49 #define FLAT_ENABLED(wpd) (wpd->shading.light == V3D_LIGHTING_FLAT)
50 #define STUDIOLIGHT_ENABLED(wpd) (wpd->shading.light == V3D_LIGHTING_STUDIO)
51 #define MATCAP_ENABLED(wpd) (wpd->shading.light == V3D_LIGHTING_MATCAP)
52 #define USE_WORLD_ORIENTATION(wpd) ((wpd->shading.flag & V3D_SHADING_WORLD_ORIENTATION) != 0)
53 #define STUDIOLIGHT_TYPE_WORLD_ENABLED(wpd) (STUDIOLIGHT_ENABLED(wpd) && (wpd->studio_light->flag & STUDIOLIGHT_TYPE_WORLD))
54 #define STUDIOLIGHT_TYPE_STUDIO_ENABLED(wpd) (STUDIOLIGHT_ENABLED(wpd) && (wpd->studio_light->flag & STUDIOLIGHT_TYPE_STUDIO))
55 #define STUDIOLIGHT_TYPE_MATCAP_ENABLED(wpd) (MATCAP_ENABLED(wpd) && (wpd->studio_light->flag & STUDIOLIGHT_TYPE_MATCAP))
56 #define SSAO_ENABLED(wpd) ((wpd->shading.flag & V3D_SHADING_CAVITY) && ((wpd->shading.cavity_type == V3D_SHADING_CAVITY_SSAO) || (wpd->shading.cavity_type == V3D_SHADING_CAVITY_BOTH)))
57 #define CURVATURE_ENABLED(wpd) ((wpd->shading.flag & V3D_SHADING_CAVITY) && ((wpd->shading.cavity_type == V3D_SHADING_CAVITY_CURVATURE) || (wpd->shading.cavity_type == V3D_SHADING_CAVITY_BOTH)))
58 #define CAVITY_ENABLED(wpd) (CURVATURE_ENABLED(wpd) || SSAO_ENABLED(wpd))
59 #define SHADOW_ENABLED(wpd) (wpd->shading.flag & V3D_SHADING_SHADOW)
60 #define GHOST_ENABLED(psl) (!DRW_pass_is_empty(psl->ghost_prepass_pass) || !DRW_pass_is_empty(psl->ghost_prepass_hair_pass))
61
62 #define IS_NAVIGATING(wpd) ((DRW_context_state_get()->rv3d) && (DRW_context_state_get()->rv3d->rflag & RV3D_NAVIGATING))
63 #define FXAA_ENABLED(wpd) ((!DRW_state_is_opengl_render()) && \
64                             (IN_RANGE(wpd->user_preferences->gpu_viewport_quality, GPU_VIEWPORT_QUALITY_FXAA, GPU_VIEWPORT_QUALITY_TAA8) || \
65                              ((IS_NAVIGATING(wpd) || wpd->is_playback) && (wpd->user_preferences->gpu_viewport_quality >= GPU_VIEWPORT_QUALITY_TAA8))))
66 #define TAA_ENABLED(wpd) (DRW_state_is_image_render() || (wpd->user_preferences->gpu_viewport_quality >= GPU_VIEWPORT_QUALITY_TAA8 && !IS_NAVIGATING(wpd) && !wpd->is_playback))
67 #define SPECULAR_HIGHLIGHT_ENABLED(wpd) (STUDIOLIGHT_ENABLED(wpd) && (wpd->shading.flag & V3D_SHADING_SPECULAR_HIGHLIGHT) && (!STUDIOLIGHT_TYPE_MATCAP_ENABLED(wpd)))
68 #define OBJECT_OUTLINE_ENABLED(wpd) (wpd->shading.flag & V3D_SHADING_OBJECT_OUTLINE)
69 #define OBJECT_ID_PASS_ENABLED(wpd) (OBJECT_OUTLINE_ENABLED(wpd) || CURVATURE_ENABLED(wpd))
70 #define MATDATA_PASS_ENABLED(wpd) (wpd->shading.color_type != V3D_SHADING_SINGLE_COLOR || MATCAP_ENABLED(wpd))
71 #define NORMAL_VIEWPORT_COMP_PASS_ENABLED(wpd) (MATCAP_ENABLED(wpd) || STUDIOLIGHT_ENABLED(wpd) || SHADOW_ENABLED(wpd))
72 #define NORMAL_VIEWPORT_PASS_ENABLED(wpd) (NORMAL_VIEWPORT_COMP_PASS_ENABLED(wpd) || SSAO_ENABLED(wpd) || CURVATURE_ENABLED(wpd))
73 #define NORMAL_ENCODING_ENABLED() (true)
74
75
76 struct RenderEngine;
77 struct RenderLayer;
78 struct rcti;
79
80
81 typedef struct WORKBENCH_FramebufferList {
82         /* Deferred render buffers */
83         struct GPUFrameBuffer *prepass_fb;
84         struct GPUFrameBuffer *ghost_prepass_fb;
85         struct GPUFrameBuffer *cavity_fb;
86         struct GPUFrameBuffer *composite_fb;
87         struct GPUFrameBuffer *id_clear_fb;
88
89         struct GPUFrameBuffer *effect_fb;
90         struct GPUFrameBuffer *effect_taa_fb;
91         struct GPUFrameBuffer *depth_buffer_fb;
92         struct GPUFrameBuffer *volume_fb;
93
94         /* Forward render buffers */
95         struct GPUFrameBuffer *object_outline_fb;
96         struct GPUFrameBuffer *transparent_accum_fb;
97         struct GPUFrameBuffer *transparent_revealage_fb;
98 } WORKBENCH_FramebufferList;
99
100 typedef struct WORKBENCH_TextureList {
101         struct GPUTexture *history_buffer_tx;
102         struct GPUTexture *depth_buffer_tx;
103 } WORKBENCH_TextureList;
104
105 typedef struct WORKBENCH_StorageList {
106         struct WORKBENCH_PrivateData *g_data;
107         struct WORKBENCH_EffectInfo *effects;
108 } WORKBENCH_StorageList;
109
110 typedef struct WORKBENCH_PassList {
111         /* deferred rendering */
112         struct DRWPass *prepass_pass;
113         struct DRWPass *prepass_hair_pass;
114         struct DRWPass *ghost_prepass_pass;
115         struct DRWPass *ghost_prepass_hair_pass;
116         struct DRWPass *cavity_pass;
117         struct DRWPass *shadow_depth_pass_pass;
118         struct DRWPass *shadow_depth_pass_mani_pass;
119         struct DRWPass *shadow_depth_fail_pass;
120         struct DRWPass *shadow_depth_fail_mani_pass;
121         struct DRWPass *shadow_depth_fail_caps_pass;
122         struct DRWPass *shadow_depth_fail_caps_mani_pass;
123         struct DRWPass *composite_pass;
124         struct DRWPass *composite_shadow_pass;
125         struct DRWPass *background_pass;
126         struct DRWPass *ghost_resolve_pass;
127         struct DRWPass *effect_aa_pass;
128         struct DRWPass *volume_pass;
129
130         /* forward rendering */
131         struct DRWPass *transparent_accum_pass;
132         struct DRWPass *object_outline_pass;
133         struct DRWPass *depth_pass;
134         struct DRWPass *checker_depth_pass;
135 } WORKBENCH_PassList;
136
137 typedef struct WORKBENCH_Data {
138         void *engine_type;
139         WORKBENCH_FramebufferList *fbl;
140         WORKBENCH_TextureList *txl;
141         WORKBENCH_PassList *psl;
142         WORKBENCH_StorageList *stl;
143 } WORKBENCH_Data;
144
145 typedef struct WORKBENCH_UBO_Light {
146         float light_direction[4];
147         float specular_color[3], pad;
148         float diffuse_color[3], wrapped;
149 } WORKBENCH_UBO_Light;
150
151 typedef struct WORKBENCH_UBO_World {
152         float background_color_low[4];
153         float background_color_high[4];
154         float object_outline_color[4];
155         float shadow_direction_vs[4];
156         WORKBENCH_UBO_Light lights[4];
157         float ambient_color[4];
158         int num_lights;
159         int matcap_orientation;
160         float background_alpha;
161         float curvature_ridge;
162         float curvature_valley;
163         int pad[3];
164 } WORKBENCH_UBO_World;
165 BLI_STATIC_ASSERT_ALIGN(WORKBENCH_UBO_World, 16)
166
167
168 typedef struct WORKBENCH_PrivateData {
169         struct GHash *material_hash;
170         struct GPUShader *prepass_solid_sh;
171         struct GPUShader *prepass_solid_hair_sh;
172         struct GPUShader *prepass_texture_sh;
173         struct GPUShader *prepass_texture_hair_sh;
174         struct GPUShader *composite_sh;
175         struct GPUShader *background_sh;
176         struct GPUShader *transparent_accum_sh;
177         struct GPUShader *transparent_accum_hair_sh;
178         struct GPUShader *transparent_accum_texture_sh;
179         struct GPUShader *transparent_accum_texture_hair_sh;
180         View3DShading shading;
181         StudioLight *studio_light;
182         UserDef *user_preferences;
183         struct GPUUniformBuffer *world_ubo;
184         struct DRWShadingGroup *shadow_shgrp;
185         struct DRWShadingGroup *depth_shgrp;
186         WORKBENCH_UBO_World world_data;
187         float shadow_multiplier;
188         float cached_shadow_direction[3];
189         float shadow_mat[4][4];
190         float shadow_inv[4][4];
191         float shadow_far_plane[4]; /* Far plane of the view frustum. */
192         float shadow_near_corners[4][3]; /* Near plane corners in shadow space. */
193         float shadow_near_min[3]; /* min and max of shadow_near_corners. allow fast test */
194         float shadow_near_max[3];
195         float shadow_near_sides[2][4]; /* This is a parallelogram, so only 2 normal and distance to the edges. */
196         bool shadow_changed;
197         bool is_playback;
198
199         /* Volumes */
200         bool volumes_do;
201         ListBase smoke_domains;
202
203         /* Ssao */
204         float winmat[4][4];
205         float viewvecs[3][4];
206         float ssao_params[4];
207         float ssao_settings[4];
208
209         /* Color Management */
210         bool use_color_view_settings;
211 } WORKBENCH_PrivateData; /* Transient data */
212
213 typedef struct WORKBENCH_EffectInfo {
214         float override_persmat[4][4];
215         float override_persinv[4][4];
216         float override_winmat[4][4];
217         float override_wininv[4][4];
218         float last_mat[4][4];
219         float curr_mat[4][4];
220         int jitter_index;
221         float taa_mix_factor;
222         bool view_updated;
223 } WORKBENCH_EffectInfo;
224
225 typedef struct WORKBENCH_MaterialData {
226         float base_color[3];
227         float diffuse_color[3];
228         float specular_color[3];
229         float metallic;
230         float roughness;
231         int object_id;
232         int color_type;
233         Image *ima;
234
235         /* Linked shgroup for drawing */
236         DRWShadingGroup *shgrp;
237         /* forward rendering */
238         DRWShadingGroup *shgrp_object_outline;
239 } WORKBENCH_MaterialData;
240
241 typedef struct WORKBENCH_ObjectData {
242         DrawData dd;
243
244         /* Shadow direction in local object space. */
245         float shadow_dir[3], shadow_depth;
246         float shadow_min[3], shadow_max[3]; /* Min, max in shadow space */
247         BoundBox shadow_bbox;
248         bool shadow_bbox_dirty;
249
250         int object_id;
251 } WORKBENCH_ObjectData;
252
253 /* workbench_engine.c */
254 void workbench_solid_materials_init(WORKBENCH_Data *vedata);
255 void workbench_solid_materials_cache_init(WORKBENCH_Data *vedata);
256 void workbench_solid_materials_cache_populate(WORKBENCH_Data *vedata, Object *ob);
257 void workbench_solid_materials_cache_finish(WORKBENCH_Data *vedata);
258 void workbench_solid_materials_draw_scene(WORKBENCH_Data *vedata);
259 void workbench_solid_materials_free(void);
260
261 /* workbench_deferred.c */
262 void workbench_deferred_engine_init(WORKBENCH_Data *vedata);
263 void workbench_deferred_engine_free(void);
264 void workbench_deferred_draw_background(WORKBENCH_Data *vedata);
265 void workbench_deferred_draw_scene(WORKBENCH_Data *vedata);
266 void workbench_deferred_draw_finish(WORKBENCH_Data *vedata);
267 void workbench_deferred_cache_init(WORKBENCH_Data *vedata);
268 void workbench_deferred_solid_cache_populate(WORKBENCH_Data *vedata, Object *ob);
269 void workbench_deferred_cache_finish(WORKBENCH_Data *vedata);
270
271 /* workbench_forward.c */
272 void workbench_forward_engine_init(WORKBENCH_Data *vedata);
273 void workbench_forward_engine_free(void);
274 void workbench_forward_draw_background(WORKBENCH_Data *vedata);
275 void workbench_forward_draw_scene(WORKBENCH_Data *vedata);
276 void workbench_forward_draw_finish(WORKBENCH_Data *vedata);
277 void workbench_forward_cache_init(WORKBENCH_Data *vedata);
278 void workbench_forward_cache_populate(WORKBENCH_Data *vedata, Object *ob);
279 void workbench_forward_cache_finish(WORKBENCH_Data *vedata);
280
281 /* workbench_effect_aa.c */
282 void workbench_aa_create_pass(WORKBENCH_Data *vedata, GPUTexture **tx);
283 void workbench_aa_draw_pass(WORKBENCH_Data *vedata, GPUTexture *tx);
284
285 /* workbench_effect_fxaa.c */
286 void workbench_fxaa_engine_init(void);
287 void workbench_fxaa_engine_free(void);
288 DRWPass *workbench_fxaa_create_pass(GPUTexture **color_buffer_tx);
289
290 /* workbench_effect_taa.c */
291 void workbench_taa_engine_init(WORKBENCH_Data *vedata);
292 void workbench_taa_engine_free(void);
293 DRWPass *workbench_taa_create_pass(WORKBENCH_Data *vedata, GPUTexture **color_buffer_tx);
294 void workbench_taa_draw_scene_start(WORKBENCH_Data *vedata);
295 void workbench_taa_draw_scene_end(WORKBENCH_Data *vedata);
296 void workbench_taa_view_updated(WORKBENCH_Data *vedata);
297 int workbench_taa_calculate_num_iterations(WORKBENCH_Data *vedata);
298
299 /* workbench_materials.c */
300 int workbench_material_determine_color_type(WORKBENCH_PrivateData *wpd, Image *ima, Object *ob);
301 char *workbench_material_build_defines(WORKBENCH_PrivateData *wpd, bool use_textures, bool is_hair);
302 void workbench_material_update_data(WORKBENCH_PrivateData *wpd, Object *ob, Material *mat, WORKBENCH_MaterialData *data);
303 uint workbench_material_get_hash(WORKBENCH_MaterialData *material_template, bool is_ghost);
304 int workbench_material_get_composite_shader_index(WORKBENCH_PrivateData *wpd);
305 int workbench_material_get_prepass_shader_index(WORKBENCH_PrivateData *wpd, bool use_textures, bool is_hair);
306 int workbench_material_get_accum_shader_index(WORKBENCH_PrivateData *wpd, bool use_textures, bool is_hair);
307 void workbench_material_shgroup_uniform(
308         WORKBENCH_PrivateData *wpd, DRWShadingGroup *grp, WORKBENCH_MaterialData *material, Object *ob,
309         const bool use_metallic, const bool deferred);
310 void workbench_material_copy(WORKBENCH_MaterialData *dest_material, const WORKBENCH_MaterialData *source_material);
311
312 /* workbench_studiolight.c */
313 void studiolight_update_world(WORKBENCH_PrivateData *wpd, StudioLight *sl, WORKBENCH_UBO_World *wd);
314 void studiolight_update_light(WORKBENCH_PrivateData *wpd, const float light_direction[3]);
315 bool studiolight_object_cast_visible_shadow(WORKBENCH_PrivateData *wpd, Object *ob, WORKBENCH_ObjectData *oed);
316 float studiolight_object_shadow_distance(WORKBENCH_PrivateData *wpd, Object *ob, WORKBENCH_ObjectData *oed);
317 bool studiolight_camera_in_object_shadow(WORKBENCH_PrivateData *wpd, Object *ob, WORKBENCH_ObjectData *oed);
318
319 /* workbench_data.c */
320 void workbench_effect_info_init(WORKBENCH_EffectInfo *effect_info);
321 void workbench_private_data_init(WORKBENCH_PrivateData *wpd);
322 void workbench_private_data_free(WORKBENCH_PrivateData *wpd);
323 void workbench_private_data_get_light_direction(WORKBENCH_PrivateData *wpd, float r_light_direction[3]);
324
325 /* workbench_volume.c */
326 void workbench_volume_engine_init(void);
327 void workbench_volume_engine_free(void);
328 void workbench_volume_cache_init(WORKBENCH_Data *vedata);
329 void workbench_volume_cache_populate(WORKBENCH_Data *vedata, Scene *scene, Object *ob, struct ModifierData *md);
330 void workbench_volume_smoke_textures_free(WORKBENCH_PrivateData *wpd);
331
332 /* workbench_render.c */
333 void workbench_render(WORKBENCH_Data *vedata, struct RenderEngine *engine, struct RenderLayer *render_layer, const struct rcti *rect);
334 void workbench_render_update_passes(struct RenderEngine *engine, struct Scene *scene, struct ViewLayer *view_layer);
335
336 #endif