Cleanup: remove redundant doxygen \file argument
[blender.git] / source / blender / draw / engines / workbench / workbench_private.h
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2016, Blender Foundation.
17  */
18
19 /** \file \ingroup draw_engine
20  */
21
22 #ifndef __WORKBENCH_PRIVATE_H__
23 #define __WORKBENCH_PRIVATE_H__
24
25
26 #include "BKE_studiolight.h"
27
28 #include "DNA_image_types.h"
29 #include "DNA_view3d_types.h"
30 #include "DNA_world_types.h"
31 #include "DNA_userdef_types.h"
32
33 #include "DRW_render.h"
34
35 #include "workbench_engine.h"
36
37 #define WORKBENCH_ENGINE "BLENDER_WORKBENCH"
38 #define M_GOLDEN_RATION_CONJUGATE 0.618033988749895
39 #define MAX_COMPOSITE_SHADERS (1 << 6)
40 #define MAX_PREPASS_SHADERS (1 << 7)
41 #define MAX_ACCUM_SHADERS (1 << 5)
42 #define MAX_CAVITY_SHADERS (1 << 3)
43
44 #define TEXTURE_DRAWING_ENABLED(wpd) (wpd->shading.color_type == V3D_SHADING_TEXTURE_COLOR)
45 #define FLAT_ENABLED(wpd) (wpd->shading.light == V3D_LIGHTING_FLAT)
46 #define STUDIOLIGHT_ENABLED(wpd) (wpd->shading.light == V3D_LIGHTING_STUDIO)
47 #define MATCAP_ENABLED(wpd) (wpd->shading.light == V3D_LIGHTING_MATCAP)
48 #define USE_WORLD_ORIENTATION(wpd) ((wpd->shading.flag & V3D_SHADING_WORLD_ORIENTATION) != 0)
49 #define STUDIOLIGHT_TYPE_WORLD_ENABLED(wpd) (STUDIOLIGHT_ENABLED(wpd) && (wpd->studio_light->flag & STUDIOLIGHT_TYPE_WORLD))
50 #define STUDIOLIGHT_TYPE_STUDIO_ENABLED(wpd) (STUDIOLIGHT_ENABLED(wpd) && (wpd->studio_light->flag & STUDIOLIGHT_TYPE_STUDIO))
51 #define STUDIOLIGHT_TYPE_MATCAP_ENABLED(wpd) (MATCAP_ENABLED(wpd) && (wpd->studio_light->flag & STUDIOLIGHT_TYPE_MATCAP))
52 #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)))
53 #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)))
54 #define CAVITY_ENABLED(wpd) (CURVATURE_ENABLED(wpd) || SSAO_ENABLED(wpd))
55 #define SHADOW_ENABLED(wpd) (wpd->shading.flag & V3D_SHADING_SHADOW)
56 #define GHOST_ENABLED(psl) (!DRW_pass_is_empty(psl->ghost_prepass_pass) || !DRW_pass_is_empty(psl->ghost_prepass_hair_pass))
57 #define CULL_BACKFACE_ENABLED(wpd) ((wpd->shading.flag & V3D_SHADING_BACKFACE_CULLING) != 0)
58 #define OIT_ENABLED(wpd) (ELEM(wpd->shading.color_type, V3D_SHADING_MATERIAL_COLOR, V3D_SHADING_OBJECT_COLOR))
59
60 #define IS_NAVIGATING(wpd) ((DRW_context_state_get()->rv3d) && (DRW_context_state_get()->rv3d->rflag & RV3D_NAVIGATING))
61 #define FXAA_ENABLED(wpd) ((!DRW_state_is_opengl_render()) && \
62                             (IN_RANGE(wpd->preferences->gpu_viewport_quality, GPU_VIEWPORT_QUALITY_FXAA, GPU_VIEWPORT_QUALITY_TAA8) || \
63                              ((IS_NAVIGATING(wpd) || wpd->is_playback) && (wpd->preferences->gpu_viewport_quality >= GPU_VIEWPORT_QUALITY_TAA8))))
64 #define TAA_ENABLED(wpd) (DRW_state_is_image_render() || (wpd->preferences->gpu_viewport_quality >= GPU_VIEWPORT_QUALITY_TAA8 && !IS_NAVIGATING(wpd) && !wpd->is_playback))
65 #define SPECULAR_HIGHLIGHT_ENABLED(wpd) (STUDIOLIGHT_ENABLED(wpd) && (wpd->shading.flag & V3D_SHADING_SPECULAR_HIGHLIGHT) && (!STUDIOLIGHT_TYPE_MATCAP_ENABLED(wpd)))
66 #define OBJECT_OUTLINE_ENABLED(wpd) (wpd->shading.flag & V3D_SHADING_OBJECT_OUTLINE)
67 #define OBJECT_ID_PASS_ENABLED(wpd) (OBJECT_OUTLINE_ENABLED(wpd) || CURVATURE_ENABLED(wpd))
68 #define MATDATA_PASS_ENABLED(wpd) (wpd->shading.color_type != V3D_SHADING_SINGLE_COLOR || MATCAP_ENABLED(wpd))
69 #define NORMAL_VIEWPORT_COMP_PASS_ENABLED(wpd) (MATCAP_ENABLED(wpd) || STUDIOLIGHT_ENABLED(wpd) || SHADOW_ENABLED(wpd))
70 #define NORMAL_VIEWPORT_PASS_ENABLED(wpd) (NORMAL_VIEWPORT_COMP_PASS_ENABLED(wpd) || SSAO_ENABLED(wpd) || CURVATURE_ENABLED(wpd))
71 #define NORMAL_ENCODING_ENABLED() (true)
72
73
74 struct RenderEngine;
75 struct RenderLayer;
76 struct rcti;
77
78
79 typedef struct WORKBENCH_FramebufferList {
80         /* Deferred render buffers */
81         struct GPUFrameBuffer *prepass_fb;
82         struct GPUFrameBuffer *ghost_prepass_fb;
83         struct GPUFrameBuffer *cavity_fb;
84         struct GPUFrameBuffer *composite_fb;
85         struct GPUFrameBuffer *id_clear_fb;
86
87         struct GPUFrameBuffer *effect_fb;
88         struct GPUFrameBuffer *effect_taa_fb;
89         struct GPUFrameBuffer *depth_buffer_fb;
90         struct GPUFrameBuffer *color_only_fb;
91
92         struct GPUFrameBuffer *dof_downsample_fb;
93         struct GPUFrameBuffer *dof_coc_tile_h_fb;
94         struct GPUFrameBuffer *dof_coc_tile_v_fb;
95         struct GPUFrameBuffer *dof_coc_dilate_fb;
96         struct GPUFrameBuffer *dof_blur1_fb;
97         struct GPUFrameBuffer *dof_blur2_fb;
98
99         /* Forward render buffers */
100         struct GPUFrameBuffer *object_outline_fb;
101         struct GPUFrameBuffer *transparent_accum_fb;
102         struct GPUFrameBuffer *transparent_revealage_fb;
103 } WORKBENCH_FramebufferList;
104
105 typedef struct WORKBENCH_TextureList {
106         struct GPUTexture *dof_source_tx;
107         struct GPUTexture *coc_halfres_tx;
108         struct GPUTexture *history_buffer_tx;
109         struct GPUTexture *depth_buffer_tx;
110 } WORKBENCH_TextureList;
111
112 typedef struct WORKBENCH_StorageList {
113         struct WORKBENCH_PrivateData *g_data;
114         struct WORKBENCH_EffectInfo *effects;
115         float *dof_ubo_data;
116 } WORKBENCH_StorageList;
117
118 typedef struct WORKBENCH_PassList {
119         /* deferred rendering */
120         struct DRWPass *prepass_pass;
121         struct DRWPass *prepass_hair_pass;
122         struct DRWPass *ghost_prepass_pass;
123         struct DRWPass *ghost_prepass_hair_pass;
124         struct DRWPass *cavity_pass;
125         struct DRWPass *shadow_depth_pass_pass;
126         struct DRWPass *shadow_depth_pass_mani_pass;
127         struct DRWPass *shadow_depth_fail_pass;
128         struct DRWPass *shadow_depth_fail_mani_pass;
129         struct DRWPass *shadow_depth_fail_caps_pass;
130         struct DRWPass *shadow_depth_fail_caps_mani_pass;
131         struct DRWPass *composite_pass;
132         struct DRWPass *composite_shadow_pass;
133         struct DRWPass *oit_composite_pass;
134         struct DRWPass *background_pass;
135         struct DRWPass *background_pass_clip;
136         struct DRWPass *ghost_resolve_pass;
137         struct DRWPass *effect_aa_pass;
138         struct DRWPass *dof_down_ps;
139         struct DRWPass *dof_down2_ps;
140         struct DRWPass *dof_flatten_v_ps;
141         struct DRWPass *dof_flatten_h_ps;
142         struct DRWPass *dof_dilate_h_ps;
143         struct DRWPass *dof_dilate_v_ps;
144         struct DRWPass *dof_blur1_ps;
145         struct DRWPass *dof_blur2_ps;
146         struct DRWPass *dof_resolve_ps;
147         struct DRWPass *volume_pass;
148
149         /* forward rendering */
150         struct DRWPass *transparent_accum_pass;
151         struct DRWPass *object_outline_pass;
152         struct DRWPass *depth_pass;
153         struct DRWPass *checker_depth_pass;
154 } WORKBENCH_PassList;
155
156 typedef struct WORKBENCH_Data {
157         void *engine_type;
158         WORKBENCH_FramebufferList *fbl;
159         WORKBENCH_TextureList *txl;
160         WORKBENCH_PassList *psl;
161         WORKBENCH_StorageList *stl;
162 } WORKBENCH_Data;
163
164 typedef struct WORKBENCH_UBO_Light {
165         float light_direction[4];
166         float specular_color[3], pad;
167         float diffuse_color[3], wrapped;
168 } WORKBENCH_UBO_Light;
169
170 typedef struct WORKBENCH_UBO_World {
171         float background_color_low[4];
172         float background_color_high[4];
173         float object_outline_color[4];
174         float shadow_direction_vs[4];
175         WORKBENCH_UBO_Light lights[4];
176         float ambient_color[4];
177         int num_lights;
178         int matcap_orientation;
179         float background_alpha;
180         float curvature_ridge;
181         float curvature_valley;
182         int pad[3];
183 } WORKBENCH_UBO_World;
184 BLI_STATIC_ASSERT_ALIGN(WORKBENCH_UBO_World, 16)
185
186
187 typedef struct WORKBENCH_PrivateData {
188         struct GHash *material_hash;
189         struct GHash *material_transp_hash;
190         struct GPUShader *prepass_solid_sh;
191         struct GPUShader *prepass_solid_hair_sh;
192         struct GPUShader *prepass_texture_sh;
193         struct GPUShader *prepass_texture_hair_sh;
194         struct GPUShader *composite_sh;
195         struct GPUShader *background_sh;
196         struct GPUShader *transparent_accum_sh;
197         struct GPUShader *transparent_accum_hair_sh;
198         struct GPUShader *transparent_accum_texture_sh;
199         struct GPUShader *transparent_accum_texture_hair_sh;
200         View3DShading shading;
201         StudioLight *studio_light;
202         const UserDef *preferences;
203         struct GPUUniformBuffer *world_ubo;
204         struct DRWShadingGroup *shadow_shgrp;
205         struct DRWShadingGroup *depth_shgrp;
206         WORKBENCH_UBO_World world_data;
207         float shadow_multiplier;
208         float shadow_shift;
209         float shadow_focus;
210         float cached_shadow_direction[3];
211         float shadow_mat[4][4];
212         float shadow_inv[4][4];
213         /* Far plane of the view frustum. */
214         float shadow_far_plane[4];
215         /* Near plane corners in shadow space. */
216         float shadow_near_corners[4][3];
217         /* min and max of shadow_near_corners. allow fast test */
218         float shadow_near_min[3];
219         float shadow_near_max[3];
220         /* This is a parallelogram, so only 2 normal and distance to the edges. */
221         float shadow_near_sides[2][4];
222         bool shadow_changed;
223         bool is_playback;
224
225         float (*world_clip_planes)[4];
226         struct GPUBatch *world_clip_planes_batch;
227         float world_clip_planes_color[4];
228
229         /* Volumes */
230         bool volumes_do;
231         ListBase smoke_domains;
232
233         /* Ssao */
234         float winmat[4][4];
235         float viewvecs[3][4];
236         float ssao_params[4];
237         float ssao_settings[4];
238
239         /* Dof */
240         struct GPUTexture *dof_blur_tx;
241         struct GPUTexture *coc_temp_tx;
242         struct GPUTexture *coc_tiles_tx[2];
243         struct GPUUniformBuffer *dof_ubo;
244         float dof_aperturesize;
245         float dof_distance;
246         float dof_invsensorsize;
247         float dof_near_far[2];
248         float dof_blades;
249         float dof_rotation;
250         float dof_ratio;
251         bool dof_enabled;
252
253         /* Color Management */
254         bool use_color_render_settings;
255 } WORKBENCH_PrivateData; /* Transient data */
256
257 typedef struct WORKBENCH_EffectInfo {
258         float override_persmat[4][4];
259         float override_persinv[4][4];
260         float override_winmat[4][4];
261         float override_wininv[4][4];
262         float last_mat[4][4];
263         float curr_mat[4][4];
264         int jitter_index;
265         float taa_mix_factor;
266         bool view_updated;
267 } WORKBENCH_EffectInfo;
268
269 typedef struct WORKBENCH_MaterialData {
270         float base_color[3];
271         float diffuse_color[3];
272         float specular_color[3];
273         float metallic;
274         float roughness;
275         int object_id;
276         int color_type;
277         int interp;
278         Image *ima;
279
280         /* Linked shgroup for drawing */
281         DRWShadingGroup *shgrp;
282         /* forward rendering */
283         DRWShadingGroup *shgrp_object_outline;
284 } WORKBENCH_MaterialData;
285
286 typedef struct WORKBENCH_ObjectData {
287         DrawData dd;
288
289         /* Shadow direction in local object space. */
290         float shadow_dir[3], shadow_depth;
291         /* Min, max in shadow space */
292         float shadow_min[3], shadow_max[3];
293         BoundBox shadow_bbox;
294         bool shadow_bbox_dirty;
295
296         int object_id;
297 } WORKBENCH_ObjectData;
298
299 /* workbench_deferred.c */
300 void workbench_deferred_engine_init(WORKBENCH_Data *vedata);
301 void workbench_deferred_engine_free(void);
302 void workbench_deferred_draw_background(WORKBENCH_Data *vedata);
303 void workbench_deferred_draw_scene(WORKBENCH_Data *vedata);
304 void workbench_deferred_draw_finish(WORKBENCH_Data *vedata);
305 void workbench_deferred_cache_init(WORKBENCH_Data *vedata);
306 void workbench_deferred_solid_cache_populate(WORKBENCH_Data *vedata, Object *ob);
307 void workbench_deferred_cache_finish(WORKBENCH_Data *vedata);
308
309 /* workbench_forward.c */
310 void workbench_forward_engine_init(WORKBENCH_Data *vedata);
311 void workbench_forward_engine_free(void);
312 void workbench_forward_draw_background(WORKBENCH_Data *vedata);
313 void workbench_forward_draw_scene(WORKBENCH_Data *vedata);
314 void workbench_forward_draw_finish(WORKBENCH_Data *vedata);
315 void workbench_forward_cache_init(WORKBENCH_Data *vedata);
316 void workbench_forward_cache_populate(WORKBENCH_Data *vedata, Object *ob);
317 void workbench_forward_cache_finish(WORKBENCH_Data *vedata);
318
319 /* For OIT in deferred */
320 void workbench_forward_outline_shaders_ensure(WORKBENCH_PrivateData *wpd);
321 void workbench_forward_choose_shaders(WORKBENCH_PrivateData *wpd);
322 WORKBENCH_MaterialData *workbench_forward_get_or_create_material_data(
323         WORKBENCH_Data *vedata, Object *ob, Material *mat, Image *ima, int color_type, int interp);
324
325 /* workbench_effect_aa.c */
326 void workbench_aa_create_pass(WORKBENCH_Data *vedata, GPUTexture **tx);
327 void workbench_aa_draw_pass(WORKBENCH_Data *vedata, GPUTexture *tx);
328
329 /* workbench_effect_fxaa.c */
330 void workbench_fxaa_engine_init(void);
331 void workbench_fxaa_engine_free(void);
332 DRWPass *workbench_fxaa_create_pass(GPUTexture **color_buffer_tx);
333
334 /* workbench_effect_taa.c */
335 void workbench_taa_engine_init(WORKBENCH_Data *vedata);
336 void workbench_taa_engine_free(void);
337 DRWPass *workbench_taa_create_pass(WORKBENCH_Data *vedata, GPUTexture **color_buffer_tx);
338 void workbench_taa_draw_scene_start(WORKBENCH_Data *vedata);
339 void workbench_taa_draw_scene_end(WORKBENCH_Data *vedata);
340 void workbench_taa_view_updated(WORKBENCH_Data *vedata);
341 int workbench_taa_calculate_num_iterations(WORKBENCH_Data *vedata);
342
343 /* workbench_effect_dof.c */
344 void workbench_dof_engine_init(WORKBENCH_Data *vedata, Object *camera);
345 void workbench_dof_engine_free(void);
346 void workbench_dof_create_pass(WORKBENCH_Data *vedata, GPUTexture **dof_input, GPUTexture *noise_tex);
347 void workbench_dof_draw_pass(WORKBENCH_Data *vedata);
348
349 /* workbench_materials.c */
350 int workbench_material_determine_color_type(WORKBENCH_PrivateData *wpd, Image *ima, Object *ob);
351 void workbench_material_get_image_and_mat(Object *ob, int mat_nr, Image **r_image, int *r_interp, Material **r_mat);
352 char *workbench_material_build_defines(WORKBENCH_PrivateData *wpd, bool use_textures, bool is_hair);
353 void workbench_material_update_data(WORKBENCH_PrivateData *wpd, Object *ob, Material *mat, WORKBENCH_MaterialData *data);
354 uint workbench_material_get_hash(WORKBENCH_MaterialData *material_template, bool is_ghost);
355 int workbench_material_get_composite_shader_index(WORKBENCH_PrivateData *wpd);
356 int workbench_material_get_prepass_shader_index(WORKBENCH_PrivateData *wpd, bool use_textures, bool is_hair);
357 int workbench_material_get_accum_shader_index(WORKBENCH_PrivateData *wpd, bool use_textures, bool is_hair);
358 void workbench_material_shgroup_uniform(
359         WORKBENCH_PrivateData *wpd, DRWShadingGroup *grp, WORKBENCH_MaterialData *material, Object *ob,
360         const bool use_metallic, const bool deferred, const int interp);
361 void workbench_material_copy(WORKBENCH_MaterialData *dest_material, const WORKBENCH_MaterialData *source_material);
362
363 /* workbench_studiolight.c */
364 void studiolight_update_world(WORKBENCH_PrivateData *wpd, StudioLight *sl, WORKBENCH_UBO_World *wd);
365 void studiolight_update_light(WORKBENCH_PrivateData *wpd, const float light_direction[3]);
366 bool studiolight_object_cast_visible_shadow(WORKBENCH_PrivateData *wpd, Object *ob, WORKBENCH_ObjectData *oed);
367 float studiolight_object_shadow_distance(WORKBENCH_PrivateData *wpd, Object *ob, WORKBENCH_ObjectData *oed);
368 bool studiolight_camera_in_object_shadow(WORKBENCH_PrivateData *wpd, Object *ob, WORKBENCH_ObjectData *oed);
369
370 /* workbench_data.c */
371 void workbench_effect_info_init(WORKBENCH_EffectInfo *effect_info);
372 void workbench_private_data_init(WORKBENCH_PrivateData *wpd);
373 void workbench_private_data_free(WORKBENCH_PrivateData *wpd);
374 void workbench_private_data_get_light_direction(WORKBENCH_PrivateData *wpd, float r_light_direction[3]);
375
376 /* workbench_volume.c */
377 void workbench_volume_engine_init(void);
378 void workbench_volume_engine_free(void);
379 void workbench_volume_cache_init(WORKBENCH_Data *vedata);
380 void workbench_volume_cache_populate(WORKBENCH_Data *vedata, Scene *scene, Object *ob, struct ModifierData *md);
381 void workbench_volume_smoke_textures_free(WORKBENCH_PrivateData *wpd);
382
383 /* workbench_render.c */
384 void workbench_render(WORKBENCH_Data *vedata, struct RenderEngine *engine, struct RenderLayer *render_layer, const struct rcti *rect);
385 void workbench_render_update_passes(struct RenderEngine *engine, struct Scene *scene, struct ViewLayer *view_layer);
386
387 #endif