Blender V5.0
overlay_background.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#pragma once
10
11#include "DNA_world_types.h"
12
13#include "draw_cache.hh"
14
15#include "overlay_base.hh"
16
17namespace blender::draw::overlay {
18
23 private:
24 PassSimple bg_ps_ = {"Background"};
25 PassSimple bg_vignette_ps_ = {"Background Vignette"};
26
27 gpu::FrameBuffer *framebuffer_ref_ = nullptr;
28
29 public:
30 void begin_sync(Resources &res, const State &state) final
31 {
33 float4 color_override(0.0f, 0.0f, 0.0f, 0.0f);
34 int background_type;
35
36 if (state.is_viewport_image_render && !state.draw_background) {
37 background_type = BG_SOLID;
38 color_override[3] = 1.0f;
39 }
40 else if (state.is_space_image()) {
41 background_type = BG_SOLID_CHECKER;
42 }
43 else if (state.is_space_node()) {
44 background_type = BG_MASK;
46 }
47 else if (!state.draw_background) {
48 background_type = BG_CHECKER;
49 }
50 else if (state.v3d->shading.background_type == V3D_SHADING_BACKGROUND_WORLD &&
51 state.v3d->shading.type <= OB_SOLID && state.scene->world)
52 {
53 background_type = BG_SOLID;
54 /* TODO(fclem): this is a scene referred linear color. we should convert
55 * it to display linear here. */
56 color_override = float4(UNPACK3(&state.scene->world->horr), 1.0f);
57 }
58 else if (state.v3d->shading.background_type == V3D_SHADING_BACKGROUND_VIEWPORT &&
59 state.v3d->shading.type <= OB_SOLID)
60 {
61 background_type = BG_SOLID;
62 color_override = float4(UNPACK3(state.v3d->shading.background_color), 1.0f);
63 }
64 else {
67 background_type = BG_GRADIENT;
68 break;
70 background_type = BG_RADIAL;
71 break;
72 default:
74 background_type = BG_SOLID;
75 break;
76 }
77 }
78
79 bg_ps_.init();
80 bg_ps_.framebuffer_set(&framebuffer_ref_);
81
82 if ((state.clipping_plane_count != 0) && (state.rv3d) && (state.rv3d->clipbb)) {
83 Span<float3> bbox(reinterpret_cast<float3 *>(state.rv3d->clipbb->vec[0]), 8);
84
86 bg_ps_.shader_set(res.shaders->background_clip_bound.get());
87 bg_ps_.push_constant("ucolor", res.theme.colors.clipping_border);
88 bg_ps_.push_constant("boundbox", bbox.data(), 8);
89 bg_ps_.draw(res.shapes.cube_solid.get());
90 }
91
92 bg_ps_.state_set(pass_state);
93 bg_ps_.shader_set(res.shaders->background_fill.get());
94 bg_ps_.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
95 bg_ps_.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
96 bg_ps_.bind_texture("color_buffer", &res.color_render_tx);
97 bg_ps_.bind_texture("depth_buffer", &res.depth_tx);
98 bg_ps_.push_constant("color_override", color_override);
99 bg_ps_.push_constant("bg_type", background_type);
100 bg_ps_.push_constant("vignette_enabled", false);
101 bg_ps_.draw_procedural(GPU_PRIM_TRIS, 1, 3);
102
103 if (state.vignette_enabled) {
104 const float vignette_aperture = state.v3d ? state.v3d->vignette_aperture : 1.0f;
105 const float vignette_falloff = 0.15f;
106
107 bg_vignette_ps_.init();
108 bg_vignette_ps_.framebuffer_set(&framebuffer_ref_);
109
110 bg_vignette_ps_.state_set(DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_ALPHA);
111 bg_vignette_ps_.shader_set(res.shaders->background_fill.get());
112 bg_vignette_ps_.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
113 bg_vignette_ps_.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
114 bg_vignette_ps_.bind_texture("color_buffer", &res.color_render_tx);
115 bg_vignette_ps_.bind_texture("depth_buffer", &res.depth_tx);
116 bg_vignette_ps_.push_constant("color_override", color_override);
117 bg_vignette_ps_.push_constant("bg_type", background_type);
118 bg_vignette_ps_.push_constant("vignette_enabled", true);
119 bg_vignette_ps_.push_constant("vignette_aperture", vignette_aperture);
120 bg_vignette_ps_.push_constant("vignette_falloff", vignette_falloff);
121 bg_vignette_ps_.draw_procedural(GPU_PRIM_TRIS, 1, 3);
122 }
123 }
124
125 void draw_output(Framebuffer &framebuffer, Manager &manager, View &view) final
126 {
127 framebuffer_ref_ = framebuffer;
128 manager.submit(bg_ps_, view);
129 }
130
131 void draw_vignette(Framebuffer &framebuffer, Manager &manager, View &view)
132 {
133 framebuffer_ref_ = framebuffer;
134 manager.submit(bg_vignette_ps_, view);
135 }
136};
137
138} // namespace blender::draw::overlay
#define UNPACK3(a)
@ OB_SOLID
@ TH_BACKGROUND_GRADIENT_RADIAL
@ TH_BACKGROUND_SINGLE_COLOR
@ TH_BACKGROUND_GRADIENT_LINEAR
@ V3D_SHADING_BACKGROUND_VIEWPORT
@ V3D_SHADING_BACKGROUND_WORLD
static AppView * view
@ GPU_PRIM_TRIS
@ TH_BACKGROUND_TYPE
int UI_GetThemeValue(int colorid)
constexpr const T * data() const
Definition BLI_span.hh:215
void submit(PassSimple &pass, View &view)
void draw_vignette(Framebuffer &framebuffer, Manager &manager, View &view)
void draw_output(Framebuffer &framebuffer, Manager &manager, View &view) final
void begin_sync(Resources &res, const State &state) final
#define DRW_CLIPPING_UBO_SLOT
#define OVERLAY_GLOBALS_SLOT
DRWState
Definition draw_state.hh:25
@ DRW_STATE_BLEND_ALPHA
Definition draw_state.hh:55
@ DRW_STATE_BLEND_BACKGROUND
Definition draw_state.hh:58
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:30
@ DRW_STATE_CULL_BACK
Definition draw_state.hh:43
@ DRW_STATE_BLEND_MUL
Definition draw_state.hh:60
static ulong state[N]
detail::Pass< command::DrawCommandBuf > PassSimple
VecBase< float, 4 > float4
VecBase< float, 3 > float3
@ BG_SOLID_CHECKER