Blender V5.3
workbench_private.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_context.hh"
10#include "BKE_image_gpu.hh"
11
12#include "DNA_camera_types.h"
13#include "DNA_material_types.h"
14#include "DRW_render.hh"
15#include "GPU_shader.hh"
16#include "draw_manager.hh"
17#include "draw_pass.hh"
18
19#include "workbench_defines.hh"
20#include "workbench_enums.hh"
22
23#include "GPU_capabilities.hh"
24#include "GPU_ray_tracing.hh"
25#include <string>
26
27namespace blender::workbench {
28
29using namespace draw;
31
33 private:
34 StaticShader prepass_[geometry_type_len][pipeline_type_len][lighting_type_len][shader_type_len]
35 [2 /*clip*/];
36 StaticShader resolve_[lighting_type_len][2 /*cavity*/][2 /*curvature*/][2 /*shadow*/];
37
38 StaticShader shadow_[2 /*depth_pass*/][2 /*manifold*/][2 /*cap*/];
39
40 StaticShader volume_[2 /*smoke*/][3 /*interpolation*/][2 /*coba*/][2 /*slice*/];
41
42 static gpu::StaticShaderCache<ShaderCache> &get_static_cache()
43 {
44 static gpu::StaticShaderCache<ShaderCache> static_cache;
45 return static_cache;
46 }
47
48 public:
49 static ShaderCache &get()
50 {
51 return get_static_cache().get();
52 }
53 static void release()
54 {
55 get_static_cache().release();
56 }
57
59
61 ePipelineType pipeline_type,
62 eLightingType lighting_type,
63 eShaderType shader_type,
64 bool clip)
65 {
66 return prepass_[int(geometry_type)][int(pipeline_type)][int(lighting_type)][int(shader_type)]
67 [clip]
68 .get();
69 }
70
72 bool cavity = false,
73 bool curvature = false,
74 bool shadow = false)
75 {
76 return resolve_[int(lighting_type)][cavity][curvature][shadow].get();
77 }
78
79 gpu::Shader *shadow_get(bool depth_pass, bool manifold, bool cap = false)
80 {
81 return shadow_[depth_pass][manifold][cap].get();
82 }
83
84 gpu::Shader *volume_get(bool smoke, int interpolation, bool coba, bool slice)
85 {
86 return volume_[smoke][interpolation][coba][slice].get();
87 }
88
89 /* Transparency */
90 StaticShader transparent_resolve = {"workbench_transparent_resolve"};
91 StaticShader merge_depth = {"workbench_merge_depth"};
92
93 /* ShadowView */
95 "workbench_shadow_visibility_compute_dynamic_pass_type"};
96 StaticShader shadow_visibility_static = {"workbench_shadow_visibility_compute_static_pass_type"};
97
98 /* Outline */
99 StaticShader outline = {"workbench_effect_outline"};
100
101 /* Dof */
102 StaticShader dof_prepare = {"workbench_effect_dof_prepare"};
103 StaticShader dof_downsample = {"workbench_effect_dof_downsample"};
104 StaticShader dof_blur1 = {"workbench_effect_dof_blur1"};
105 StaticShader dof_blur2 = {"workbench_effect_dof_blur2"};
106 StaticShader dof_resolve = {"workbench_effect_dof_resolve"};
107
108 /* AA */
109 StaticShader taa_accumulation = {"workbench_taa"};
110 StaticShader smaa_edge_detect = {"workbench_smaa_stage_0"};
111 StaticShader smaa_aa_weight = {"workbench_smaa_stage_1"};
112 StaticShader smaa_resolve = {"workbench_smaa_stage_2"};
113 StaticShader overlay_depth = {"workbench_overlay_depth"};
114
115 StaticShader shadow_raytrace = {"workbench_shadow_rt_raytrace"};
116};
117
118struct Material {
120 /* Packed data into a int. Decoded in the shader. */
122
123 Material() = default;
125
126 Material(blender::Object &ob, bool random = false);
128 : base_color(&mat.r), packed_data(Material::pack_data(mat.metallic, mat.roughness, mat.a))
129 {
130 }
131
132 static uint32_t pack_data(float metallic, float roughness, float alpha);
133
134 bool is_transparent();
135};
136
138{
139 uint32_t full_alpha_ref = 0x00ff0000;
140 return (packed_data & full_alpha_ref) != full_alpha_ref;
141}
142
143inline uint32_t Material::pack_data(float metallic, float roughness, float alpha)
144{
145 /* Remap to Disney roughness. */
146 roughness = sqrtf(roughness);
147 uint32_t packed_roughness = unit_float_to_uchar_clamp(roughness);
148 uint32_t packed_metallic = unit_float_to_uchar_clamp(metallic);
149 uint32_t packed_alpha = unit_float_to_uchar_clamp(alpha);
150 return (packed_alpha << 16u) | (packed_roughness << 8u) | packed_metallic;
151}
152
154
156 Scene *scene = nullptr;
157
159 Camera *camera = nullptr;
163
165
168 bool xray_mode = false;
169
172
174
175 bool draw_cavity = false;
176 bool draw_curvature = false;
177 bool draw_shadows = false;
178 bool draw_outline = false;
179 bool draw_dof = false;
180 bool draw_aa = false;
181
182 bool draw_object_id = false;
183
184 bool shadows_use_rt = false;
185
186 int sample = 0;
187 int samples_len = 0;
189 bool render_finished = false;
190
191 bool updated = false;
192
193 /* Used when material_type == eMaterialType::SINGLE */
195 /* When r == -1.0 the shader uses the vertex color */
197
199
200 void init(const DRWContext *context, bool scene_updated, Object *camera_ob = nullptr);
201};
202
204 const char *name = nullptr;
207 bool premultiplied = false;
208 bool alpha_cutoff = false;
209
210 MaterialTexture() = default;
211 MaterialTexture(Manager &manager, Object *ob, int material_index);
212 MaterialTexture(Manager &manager, blender::Image *image, ImageUser *user = nullptr);
213};
214
215struct SceneResources;
216
221 bool draw_shadow = false;
223 bool sculpt_pbvh = false;
224
225 ObjectState(const DRWContext *draw_ctx,
226 const SceneState &scene_state,
227 const SceneResources &resources,
228 Object *ob,
229 Manager &manager);
230};
231
233 private:
234 /* This value must be kept in sync with the one declared at
235 * workbench_composite_info.hh (cavity_samples) */
236 static const int max_samples_ = 512;
237
239
240 int sample_ = 0;
241 int sample_count_ = 0;
242 bool curvature_enabled_ = false;
243 bool cavity_enabled_ = false;
244
245 public:
246 void init(const SceneState &scene_state, SceneResources &resources);
247 void setup_resolve_pass(PassSimple &pass, SceneResources &resources);
248
249 private:
250 void load_samples_buf(int ssao_samples);
251};
252
254 static const int jitter_tx_size = 64;
255
258 Texture matcap_tx = "matcap_tx";
259
260 TextureFromPool object_id_tx = "wb_object_id_tx";
261
265
266 Framebuffer clear_fb = {"Clear Main"};
268 Framebuffer clear_in_front_fb = {"Clear In Front"};
269
273
274 Texture jitter_tx = "wb_jitter_tx";
275
277
279
280 Texture dummy_texture_tx = {"dummy_texture"};
281 Texture dummy_tile_data_tx = {"dummy_tile_data"};
282 Texture dummy_tile_array_tx = {"dummy_tile_array"};
283
284 gpu::Batch *volume_cube_batch = nullptr;
285
287 {
288 /* TODO(fclem): Auto destruction. */
292 }
293 }
294
295 void init(const SceneState &scene_state, const DRWContext *ctx);
296 void load_jitter_tx(int total_samples);
297};
298
299class MeshPass : public PassMain {
300 private:
301 struct TextureSubPassKey {
303 GPUSamplerState sampler_state;
304 eGeometryType geom_type;
305
306 uint64_t hash() const
307 {
308 return get_default_hash(texture, sampler_state.as_uint(), geom_type);
309 }
310
311 bool operator==(TextureSubPassKey const &rhs) const
312 {
313 return this->texture == rhs.texture && this->sampler_state == rhs.sampler_state &&
314 this->geom_type == rhs.geom_type;
315 }
316 };
317
318 Map<TextureSubPassKey, PassMain::Sub *> texture_subpass_map_;
319
320 PassMain::Sub *passes_[geometry_type_len][shader_type_len] = {{nullptr}};
321
322 ePipelineType pipeline_;
323 eLightingType lighting_;
324 bool clip_;
325
326 bool is_empty_ = false;
327
328 PassMain::Sub &get_subpass(eGeometryType geometry_type, eShaderType shader_type);
329
330 public:
331 MeshPass(const char *name);
332
333 /* TODO: Move to draw::Pass */
334 bool is_empty() const;
335
336 void init_pass(SceneResources &resources, DRWState state, int clip_planes);
337 void init_subpasses(ePipelineType pipeline, eLightingType lighting, bool clip);
338
339 PassMain::Sub &get_subpass(eGeometryType geometry_type,
340 const MaterialTexture *texture = nullptr);
341};
342
343enum class StencilBits : uint8_t {
345 OBJECT = 1u << 0,
347};
348
350 public:
351 TextureFromPool gbuffer_normal_tx = {"gbuffer_normal_tx"};
352 TextureFromPool gbuffer_material_tx = {"gbuffer_material_tx"};
353
354 Texture shadow_depth_stencil_tx = {"shadow_depth_stencil_tx"};
356
357 MeshPass gbuffer_ps_ = {"Opaque.Gbuffer"};
358 MeshPass gbuffer_in_front_ps_ = {"Opaque.GbufferInFront"};
359 PassSimple deferred_ps_ = {"Opaque.Deferred"};
360
361 Framebuffer gbuffer_fb = {"Opaque.Gbuffer"};
362 Framebuffer gbuffer_in_front_fb = {"Opaque.GbufferInFront"};
363 Framebuffer deferred_fb = {"Opaque.Deferred"};
364 Framebuffer clear_fb = {"Opaque.Clear"};
365
366 void sync(const SceneState &scene_state, SceneResources &resources);
367 void draw(Manager &manager,
368 View &view,
369 SceneResources &resources,
370 int2 resolution,
371 class ShadowPass *shadow_pass);
372 bool is_empty() const;
373};
374
376 public:
377 TextureFromPool accumulation_tx = {"accumulation_accumulation_tx"};
378 TextureFromPool reveal_tx = {"accumulation_reveal_tx"};
380
381 MeshPass accumulation_ps_ = {"Transparent.Accumulation"};
382 MeshPass accumulation_in_front_ps_ = {"Transparent.AccumulationInFront"};
383 PassSimple resolve_ps_ = {"Transparent.Resolve"};
385
386 void sync(const SceneState &scene_state, SceneResources &resources);
387 void draw(Manager &manager, View &view, SceneResources &resources, int2 resolution);
388 bool is_empty() const;
389};
390
392 public:
393 MeshPass main_ps_ = {"TransparentDepth.Main"};
394 Framebuffer main_fb = {"TransparentDepth.Main"};
395 MeshPass in_front_ps_ = {"TransparentDepth.InFront"};
396 Framebuffer in_front_fb = {"TransparentDepth.InFront"};
397 PassSimple merge_ps_ = {"TransparentDepth.Merge"};
398 Framebuffer merge_fb = {"TransparentDepth.Merge"};
399
400 void sync(const SceneState &scene_state, SceneResources &resources);
401 void draw(Manager &manager, View &view, SceneResources &resources);
402 bool is_empty() const;
403};
404
405#define DEBUG_SHADOW_VOLUME 0
406
408 private:
409 enum PassType { PASS = 0, FAIL, FORCED_FAIL, MAX };
410
411 class ShadowView : public View {
412 bool force_fail_method_ = false;
413 float3 light_direction_ = float3(0);
414 UniformBuffer<ExtrudedFrustum> extruded_frustum_ = {};
415 ShadowPass::PassType current_pass_type_ = PASS;
416
417 VisibilityBuf pass_visibility_buf_ = {};
418 VisibilityBuf fail_visibility_buf_ = {};
419
420 public:
421 ShadowView() : View("ShadowPass.View") {};
422
423 void setup(View &view, float3 light_direction, bool force_fail_method);
424 bool debug_object_culling(Object *ob);
425 void set_mode(PassType type);
426
427 protected:
428 void compute_visibility(ObjectBoundsBuf &bounds,
429 ObjectInfosBuf &infos,
430 uint resource_len,
431 bool debug_freeze) override;
432 VisibilityBuf &get_visibility_buffer() override;
433 } view_ = {};
434
435 bool enabled_;
436 bool use_raytracing_;
437 bool needs_rt_update_;
438
439 UniformBuffer<ShadowPassData> pass_data_ = {};
440
441 /* Draws are added to both passes and the visibly compute shader selects one of them */
442 PassMain pass_ps_ = {"Shadow.Pass"};
443 PassMain fail_ps_ = {"Shadow.Fail"};
444
445 /* In some cases, we know beforehand that we need to use the fail technique */
446 PassMain forced_fail_ps_ = {"Shadow.ForcedFail"};
447
448 PassSimple raytrace_ps_ = {"Shadow.RayQuery"};
449 gpu::TopLevelASPtr shadow_as_;
450 gpu::Texture *gbuffer_normal_ref;
451
452 /* [PassType][Is Manifold][Is Cap] */
453 PassMain::Sub *passes_[PassType::MAX][2][2] = {{{nullptr}}};
454 PassMain::Sub *&get_pass_ptr(PassType type, bool manifold, bool cap = false);
455
456 TextureFromPool depth_tx_ = {};
457 Framebuffer fb_ = {};
458
459 public:
460 void init(const SceneState &scene_state, SceneResources &resources);
461 void update();
462 void sync(SceneResources &resources);
463 void object_sync(SceneState &scene_state,
464 ObjectRef &ob_ref,
465 ResourceHandleRange handle,
466 const bool has_transp_mat);
467 void end_sync();
468 void draw(Manager &manager,
469 View &view,
470 SceneResources &resources,
471 gpu::Texture &depth_stencil_tx,
472 gpu::Texture &normal_tx,
473 int2 resolution,
474 /* Needed when there are opaque "In Front" objects in the scene */
475 bool force_fail_method);
476
477 bool is_debug();
478};
479
481 bool active_ = true;
482
483 PassSortable ps_ = {"Volume"};
484 Framebuffer fb_ = {"Volume"};
485
486 Texture dummy_shadow_tx_ = {"Volume.Dummy Shadow Tx"};
487 Texture dummy_volume_tx_ = {"Volume.Dummy Volume Tx"};
488 Texture dummy_flag_tx_ = {"Volume.Dummy Flag Tx"};
489 Texture dummy_coba_tx_ = {"Volume.Dummy Coba Tx"};
490
491 gpu::Texture *stencil_tx_ = nullptr;
492
493 public:
494 void sync(SceneResources &resources);
495
496 void object_sync_volume(Manager &manager,
497 SceneResources &resources,
498 const SceneState &scene_state,
499 ObjectRef &ob_ref,
500 float3 color);
501
502 void object_sync_modifier(Manager &manager,
503 SceneResources &resources,
504 const SceneState &scene_state,
505 ObjectRef &ob_ref,
506 ModifierData *md);
507
508 void draw(Manager &manager, View &view, SceneResources &resources);
509
510 private:
511 void draw_slice_ps(Manager &manager,
512 SceneResources &resources,
513 PassMain::Sub &ps,
514 ObjectRef &ob_ref,
515 int slice_axis_enum,
516 float slice_depth);
517
518 void draw_volume_ps(Manager &manager,
519 SceneResources &resources,
520 PassMain::Sub &ps,
521 ObjectRef &ob_ref,
522 int taa_sample,
523 float3 slice_count,
524 float3 world_size);
525};
526
528 private:
529 bool enabled_ = false;
530
531 PassSimple ps_ = PassSimple("Workbench.Outline");
532 Framebuffer fb_ = Framebuffer("Workbench.Outline");
533
534 public:
535 void init(const SceneState &scene_state);
536 void sync(SceneResources &resources);
537 void draw(Manager &manager, SceneResources &resources);
538};
539
540class DofPass {
541 private:
542 static const int kernel_radius_ = 3;
543 static const int samples_len_ = (kernel_radius_ * 2 + 1) * (kernel_radius_ * 2 + 1);
544
545 bool enabled_ = false;
546
547 float offset_ = 0;
548
550
551 Texture source_tx_ = {};
552 Texture coc_halfres_tx_ = {};
553 TextureFromPool blur_tx_ = {};
554
555 Framebuffer downsample_fb_ = {};
556 Framebuffer blur1_fb_ = {};
557 Framebuffer blur2_fb_ = {};
558 Framebuffer resolve_fb_ = {};
559
560 PassSimple down_ps_ = {"Workbench.DoF.DownSample"};
561 PassSimple down2_ps_ = {"Workbench.DoF.DownSample2"};
562 PassSimple down3_ps_ = {"Workbench.DoF.DownSample3"};
563 PassSimple blur_ps_ = {"Workbench.DoF.Blur"};
564 PassSimple blur2_ps_ = {"Workbench.DoF.Blur2"};
565 PassSimple resolve_ps_ = {"Workbench.DoF.Resolve"};
566
567 float aperture_size_ = 0;
568 float distance_ = 0;
569 float invsensor_size_ = 0;
570 float near_ = 0;
571 float far_ = 0;
572 float blades_ = 0;
573 float rotation_ = 0;
574 float ratio_ = 0;
575
576 public:
577 void init(const SceneState &scene_state, const DRWContext *draw_ctx);
578 void sync(SceneResources &resources, const DRWContext *draw_ctx);
579 void draw(Manager &manager, View &view, SceneResources &resources, int2 resolution);
580 bool is_enabled();
581
582 private:
583 void setup_samples();
584};
585
587 private:
588 bool enabled_ = false;
589 /* Weight accumulated. */
590 float weight_accum_ = 0;
591 /* Samples weight for this iteration. */
592 float weights_[9] = {0};
593 /* Sum of weights. */
594 float weights_sum_ = 0;
595
596 Texture sample0_depth_tx_ = {"sample0_depth_tx"};
597 Texture sample0_depth_in_front_tx_ = {"sample0_depth_in_front_tx"};
598
599 Texture taa_accumulation_tx_ = {"taa_accumulation_tx"};
600 Texture smaa_search_tx_ = {"smaa_search_tx"};
601 Texture smaa_area_tx_ = {"smaa_area_tx"};
602 TextureFromPool smaa_edge_tx_ = {"smaa_edge_tx"};
603 TextureFromPool smaa_weight_tx_ = {"smaa_weight_tx"};
604
605 Framebuffer taa_accumulation_fb_ = {"taa_accumulation_fb"};
606 Framebuffer smaa_edge_fb_ = {"smaa_edge_fb"};
607 Framebuffer smaa_weight_fb_ = {"smaa_weight_fb"};
608 Framebuffer smaa_resolve_fb_ = {"smaa_resolve_fb"};
609 Framebuffer overlay_depth_fb_ = {"overlay_depth_fb"};
610
611 float4 smaa_viewport_metrics_ = float4(0);
612 float smaa_mix_factor_ = 0;
613
614 PassSimple taa_accumulation_ps_ = {"TAA.Accumulation"};
615 PassSimple smaa_edge_detect_ps_ = {"SMAA.EdgeDetect"};
616 PassSimple smaa_aa_weight_ps_ = {"SMAA.BlendWeights"};
617 PassSimple smaa_resolve_ps_ = {"SMAA.Resolve"};
618 PassSimple overlay_depth_ps_ = {"Overlay Depth"};
619
620 public:
622
623 void init(const SceneState &scene_state);
624 void sync(const SceneState &scene_state, SceneResources &resources);
625 void setup_view(View &view, const SceneState &scene_state);
631 void draw(const DRWContext *draw_ctx,
632 Manager &manager,
633 View &view,
634 const SceneState &scene_state,
635 SceneResources &resources,
636 gpu::Texture *depth_in_front_tx);
637};
638
639} // namespace blender::workbench
#define GPU_BATCH_DISCARD_SAFE(batch)
Definition GPU_batch.hh:199
detail::PassBase< command::DrawMultiBuf > Sub
Definition draw_pass.hh:509
void setup_view(View &view, const SceneState &scene_state)
void sync(const SceneState &scene_state, SceneResources &resources)
void setup_resolve_pass(PassSimple &pass, SceneResources &resources)
void init(const SceneState &scene_state, SceneResources &resources)
void sync(SceneResources &resources, const DRWContext *draw_ctx)
void init(const SceneState &scene_state, const DRWContext *draw_ctx)
void init_pass(SceneResources &resources, DRWState state, int clip_planes)
void init_subpasses(ePipelineType pipeline, eLightingType lighting, bool clip)
void sync(SceneResources &resources)
void init(const SceneState &scene_state)
gpu::Shader * resolve_get(eLightingType lighting_type, bool cavity=false, bool curvature=false, bool shadow=false)
gpu::Shader * prepass_get(eGeometryType geometry_type, ePipelineType pipeline_type, eLightingType lighting_type, eShaderType shader_type, bool clip)
gpu::Shader * volume_get(bool smoke, int interpolation, bool coba, bool slice)
gpu::Shader * shadow_get(bool depth_pass, bool manifold, bool cap=false)
void init(const SceneState &scene_state, SceneResources &resources)
void object_sync(SceneState &scene_state, ObjectRef &ob_ref, ResourceHandleRange handle, const bool has_transp_mat)
void sync(SceneResources &resources)
void sync(SceneResources &resources)
void object_sync_volume(Manager &manager, SceneResources &resources, const SceneState &scene_state, ObjectRef &ob_ref, float3 color)
void object_sync_modifier(Manager &manager, SceneResources &resources, const SceneState &scene_state, ObjectRef &ob_ref, ModifierData *md)
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
const int state
detail::Pass< command::DrawCommandBuf > PassSimple
detail::Pass< command::DrawMultiBuf > PassMain
StorageArrayBuffer< ObjectBounds, 128 > ObjectBoundsBuf
Definition draw_view.hh:33
StorageArrayBuffer< ObjectInfos, 128 > ObjectInfosBuf
Definition draw_view.hh:34
StorageArrayBuffer< uint, 4, true > VisibilityBuf
Definition draw_view.hh:35
gpu::StaticShader StaticShader
bool operator==(const CameraData &a, const CameraData &b)
std::unique_ptr< TopLevelAS, TopLevelASDeleter > TopLevelASPtr
ImageGPUTextures get_material_texture(GPUSamplerState &sampler_state)
unsigned __int64 uint64_t
Definition BKE_text.h:91
MINLINE unsigned char unit_float_to_uchar_clamp(float val)
unsigned int uint
Definition BKE_text.h:55
MatBase< float, 4, 4 > float4x4
@ CTX_MODE_OBJECT
VecBase< float, 4 > float4
unsigned int uint32_t
Definition BKE_text.h:81
VecBase< int32_t, 2 > int2
unsigned char uint8_t
Definition BKE_text.h:79
@ DRW_STATE_NO_DRAW
Definition draw_state.hh:29
@ V3D_SHADING_SINGLE_COLOR
void GPU_texture_free(gpu::Texture *texture)
VecBase< float, 3 > float3
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
#define hash
Definition noise_c.cc:156
#define sqrtf
uint32_t as_uint() const
static constexpr GPUSamplerState default_sampler()
Material(blender::Material &mat)
static uint32_t pack_data(float metallic, float roughness, float alpha)
ObjectState(const DRWContext *draw_ctx, const SceneState &scene_state, const SceneResources &resources, Object *ob, Manager &manager)
UniformBuffer< WorldData > world_buf
StorageVectorBuffer< Material > material_buf
UniformArrayBuffer< float4, 6 > clip_planes_buf
void init(const DRWContext *context, bool scene_updated, Object *camera_ob=nullptr)