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