Blender V5.3
workbench_mesh_passes.cc
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
10
11namespace blender::workbench {
12
13/* -------------------------------------------------------------------- */
16
17MeshPass::MeshPass(const char *name) : PassMain(name) {};
18
20{
21 /* TODO: Move to #draw::Pass. */
22
23 return is_empty_;
24}
25
40
41void MeshPass::init_subpasses(ePipelineType pipeline, eLightingType lighting, bool clip)
42{
43 texture_subpass_map_.clear();
44 pipeline_ = pipeline;
45 lighting_ = lighting;
46 clip_ = clip;
47
48 for (auto geom : IndexRange(geometry_type_len)) {
49 for (auto shader : IndexRange(shader_type_len)) {
50 passes_[geom][shader] = nullptr;
51 }
52 }
53}
54
55PassMain::Sub &MeshPass::get_subpass(eGeometryType geometry_type, eShaderType shader_type)
56{
57 static std::string pass_names[geometry_type_len][shader_type_len] = {};
58
59 PassMain::Sub *&sub_pass = passes_[int(geometry_type)][int(shader_type)];
60 if (!sub_pass) {
61 std::string &pass_name = pass_names[int(geometry_type)][int(shader_type)];
62 if (pass_name.empty()) {
63 pass_name = std::string(get_name(geometry_type)) + std::string(get_name(shader_type));
64 }
65 sub_pass = &sub(pass_name.c_str());
66 sub_pass->shader_set(
67 ShaderCache::get().prepass_get(geometry_type, pipeline_, lighting_, shader_type, clip_));
68 }
69
70 return *sub_pass;
71}
72
73PassMain::Sub &MeshPass::get_subpass(eGeometryType geometry_type,
74 const MaterialTexture *texture /* = nullptr */)
75{
76 is_empty_ = false;
77
78 if (texture && texture->gpu.texture) {
79 auto add_cb = [&] {
80 PassMain::Sub *sub_pass = &get_subpass(geometry_type, eShaderType::TEXTURE);
81 sub_pass = &sub_pass->sub(texture->name);
82 if (texture->gpu.tile_mapping) {
83 sub_pass->bind_texture(WB_TILE_ARRAY_SLOT, texture->gpu.texture, texture->sampler_state);
84 sub_pass->bind_texture(WB_TILE_DATA_SLOT, texture->gpu.tile_mapping);
85 }
86 else {
87 sub_pass->bind_texture(WB_TEXTURE_SLOT, texture->gpu.texture, texture->sampler_state);
88 }
89 sub_pass->push_constant("is_image_tile", texture->gpu.tile_mapping != nullptr);
90 sub_pass->push_constant("image_premult", texture->premultiplied);
91 /* TODO(@pragma37): This setting should be exposed on the user side,
92 * either as a global parameter (and set it here)
93 * or by reading the Material Clipping Threshold (and set it per material) */
94 float alpha_cutoff = texture->alpha_cutoff ? 0.1f : -FLT_MAX;
95 sub_pass->push_constant("image_transparency_cutoff", alpha_cutoff);
96 return sub_pass;
97 };
98
99 return *texture_subpass_map_.lookup_or_add_cb(
100 {texture->gpu.texture, texture->sampler_state, geometry_type}, add_cb);
101 }
102
103 return get_subpass(geometry_type, eShaderType::MATERIAL);
104}
105
107
108/* -------------------------------------------------------------------- */
111
112void OpaquePass::sync(const SceneState &scene_state, SceneResources &resources)
113{
116
117 bool clip = scene_state.clip_planes.size() > 0;
118
119 DRWState in_front_state = state | DRW_STATE_STENCIL_ALWAYS;
120 gbuffer_in_front_ps_.init_pass(resources, in_front_state, scene_state.clip_planes.size());
123
125 gbuffer_ps_.init_pass(resources, state, scene_state.clip_planes.size());
129
132 deferred_ps_.shader_set(ShaderCache::get().resolve_get(scene_state.lighting_type,
133 scene_state.draw_cavity,
134 scene_state.draw_curvature,
135 scene_state.draw_shadows));
136 deferred_ps_.push_constant("force_shadowing", false);
141 deferred_ps_.bind_texture("depth_tx", &resources.depth_tx);
143 resources.cavity.setup_resolve_pass(deferred_ps_, resources);
145}
146
148 View &view,
149 SceneResources &resources,
150 int2 resolution,
151 ShadowPass *shadow_pass)
152{
153 if (is_empty()) {
154 return;
155 }
157 gpu::TextureFormat::SFLOAT_16_16_16_16,
159 gbuffer_normal_tx.acquire_2d(resolution,
160 gpu::TextureFormat::SFLOAT_16_16,
162
163 GPUAttachment object_id_attachment = GPU_ATTACHMENT_NONE;
164 if (resources.object_id_tx.is_valid()) {
165 object_id_attachment = GPU_ATTACHMENT_TEXTURE(resources.object_id_tx);
166 }
167
172 object_id_attachment);
174
175 manager.submit(gbuffer_in_front_ps_, view);
176
177 if (resources.depth_in_front_tx.is_valid()) {
178 GPU_texture_copy(resources.depth_in_front_tx, resources.depth_tx);
179 }
180 }
181
182 if (!gbuffer_ps_.is_empty()) {
186 object_id_attachment);
188
189 manager.submit(gbuffer_ps_, view);
190 }
191
192 if (shadow_pass) {
193 shadow_depth_stencil_tx.ensure_2d(gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8,
194 resolution,
198
201 clear_fb.bind();
203
204 shadow_pass->draw(manager,
205 view,
206 resources,
209 resolution,
212 }
213 else {
215 deferred_ps_stencil_tx = nullptr;
216 }
217
218 if (!shadow_pass || !shadow_pass->is_debug()) {
222 manager.submit(deferred_ps_, view);
223 }
224
227}
228
233
235
236/* -------------------------------------------------------------------- */
239
266
268 View &view,
269 SceneResources &resources,
270 int2 resolution)
271{
272 if (is_empty()) {
273 return;
274 }
275 accumulation_tx.acquire_2d(resolution,
276 gpu::TextureFormat::SFLOAT_16_16_16_16,
278 reveal_tx.acquire_2d(resolution,
279 gpu::TextureFormat::SFLOAT_16,
281
283
284 if (!accumulation_ps_.is_empty()) {
289 manager.submit(accumulation_ps_, view);
291 manager.submit(resolve_ps_, view);
292 }
298 manager.submit(accumulation_in_front_ps_, view);
300 manager.submit(resolve_ps_, view);
301 }
302
305}
306
311
313
314/* -------------------------------------------------------------------- */
317
345
346void TransparentDepthPass::draw(Manager &manager, View &view, SceneResources &resources)
347{
348 if (is_empty()) {
349 return;
350 }
351
352 GPUAttachment object_id_attachment = GPU_ATTACHMENT_NONE;
353 if (resources.object_id_tx.is_valid()) {
354 object_id_attachment = GPU_ATTACHMENT_TEXTURE(resources.object_id_tx);
355 }
356
357 if (!in_front_ps_.is_empty()) {
361 object_id_attachment);
363 manager.submit(in_front_ps_, view);
364
366 merge_fb.bind();
367 manager.submit(merge_ps_, view);
368 }
369
370 if (!main_ps_.is_empty()) {
374 object_id_attachment);
375 main_fb.bind();
376 manager.submit(main_ps_, view);
377 }
378}
379
381{
383}
384
386
387} // namespace blender::workbench
#define GPU_ATTACHMENT_TEXTURE(_texture)
#define GPU_ATTACHMENT_NONE
constexpr IndexRange()=default
void ensure(GPUAttachment depth=GPU_ATTACHMENT_NONE, GPUAttachment color1=GPU_ATTACHMENT_NONE, GPUAttachment color2=GPU_ATTACHMENT_NONE, GPUAttachment color3=GPU_ATTACHMENT_NONE, GPUAttachment color4=GPU_ATTACHMENT_NONE, GPUAttachment color5=GPU_ATTACHMENT_NONE, GPUAttachment color6=GPU_ATTACHMENT_NONE, GPUAttachment color7=GPU_ATTACHMENT_NONE, GPUAttachment color8=GPU_ATTACHMENT_NONE)
void submit(PassSimple &pass, View &view)
bool acquire_2d(int2 extent, gpu::TextureFormat format, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, int mip_len=1)
bool ensure_2d(gpu::TextureFormat format, int2 extent, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const float *data=nullptr, int mip_len=1)
gpu::Texture * gpu_texture()
gpu::Texture * stencil_view(bool cube_as_array=false)
void draw_procedural(GPUPrimType primitive, uint instance_len, uint vertex_len, uint vertex_first=-1, ResourceIDRange res_id={}, uint custom_id=0)
Definition draw_pass.hh:976
void shader_set(gpu::Shader *shader)
void bind_texture(const char *name, gpu::Texture *texture, GPUSamplerState state=sampler_auto)
void clear_color(float4 color)
PassBase< DrawCommandBufType > & sub(const char *name)
Definition draw_pass.hh:701
void state_set(DRWState state, int clip_plane_count=0)
void state_stencil(uint8_t write_mask, uint8_t reference, uint8_t compare_mask)
void bind_ubo(const char *name, gpu::UniformBuf *buffer)
void push_constant(const char *name, const float &data)
void bind_ssbo(const char *name, gpu::StorageBuf *buffer)
detail::PassBase< command::DrawMultiBuf > Sub
Definition draw_pass.hh:509
void setup_resolve_pass(PassSimple &pass, SceneResources &resources)
void init_pass(SceneResources &resources, DRWState state, int clip_planes)
void init_subpasses(ePipelineType pipeline, eLightingType lighting, bool clip)
void sync(const SceneState &scene_state, SceneResources &resources)
void draw(Manager &manager, View &view, SceneResources &resources, int2 resolution, class ShadowPass *shadow_pass)
void draw(Manager &manager, View &view, SceneResources &resources, gpu::Texture &depth_stencil_tx, gpu::Texture &normal_tx, int2 resolution, bool force_fail_method)
void draw(Manager &manager, View &view, SceneResources &resources)
void sync(const SceneState &scene_state, SceneResources &resources)
void sync(const SceneState &scene_state, SceneResources &resources)
void draw(Manager &manager, View &view, SceneResources &resources, int2 resolution)
#define DRW_CLIPPING_UBO_SLOT
#define FLT_MAX
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
const int state
detail::Pass< command::DrawMultiBuf > PassMain
void GPU_texture_copy(gpu::Texture *dst, gpu::Texture *src)
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
@ GPU_TEXTURE_USAGE_FORMAT_VIEW
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_ATTACHMENT
unsigned char uint8_t
Definition BKE_text.h:79
@ DRW_STATE_BLEND_OIT
Definition draw_state.hh:61
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_BLEND_ALPHA
Definition draw_state.hh:57
@ DRW_STATE_STENCIL_ALWAYS
Definition draw_state.hh:48
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
@ DRW_STATE_DEPTH_LESS
Definition draw_state.hh:39
@ DRW_STATE_WRITE_STENCIL
Definition draw_state.hh:34
@ DRW_STATE_STENCIL_EQUAL
Definition draw_state.hh:49
@ DRW_STATE_STENCIL_NEQUAL
Definition draw_state.hh:50
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition draw_state.hh:40
void GPU_framebuffer_clear_stencil(gpu::FrameBuffer *fb, uint clear_stencil)
UniformBuffer< WorldData > world_buf
StorageVectorBuffer< Material > material_buf
UniformArrayBuffer< float4, 6 > clip_planes_buf
#define WB_TEXTURE_SLOT
#define WB_MATCAP_SLOT
#define WB_TILE_DATA_SLOT
#define WB_MATERIAL_SLOT
#define WB_WORLD_SLOT
#define WB_TILE_ARRAY_SLOT