Blender V5.3
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 /* Padded bbox vertices. */
31
32 public:
33 void begin_sync(Resources &res, const State &state) final
34 {
36 float4 color_override(0.0f, 0.0f, 0.0f, 0.0f);
37 int background_type;
38
39 if (state.is_viewport_image_render && !state.draw_background) {
40 background_type = BG_SOLID;
41 color_override[3] = 1.0f;
42 }
43 else if (state.is_space_image()) {
44 background_type = BG_SOLID_CHECKER;
45 }
46 else if (state.is_space_node()) {
47 background_type = BG_MASK;
49 }
50 else if (!state.draw_background) {
51 background_type = BG_CHECKER;
52 }
53 else if (state.v3d->shading.background_type == V3D_SHADING_BACKGROUND_WORLD &&
54 state.v3d->shading.type <= OB_SOLID && state.scene->world)
55 {
56 background_type = BG_SOLID;
57 /* TODO(fclem): this is a scene referred linear color. we should convert
58 * it to display linear here. */
59 color_override = float4(UNPACK3(&state.scene->world->horr), 1.0f);
60 }
61 else if (state.v3d->shading.background_type == V3D_SHADING_BACKGROUND_VIEWPORT &&
62 state.v3d->shading.type <= OB_SOLID)
63 {
64 background_type = BG_SOLID;
65 color_override = float4(UNPACK3(state.v3d->shading.background_color), 1.0f);
66 }
67 else {
70 background_type = BG_GRADIENT;
71 break;
73 background_type = BG_RADIAL;
74 break;
75 default:
77 background_type = BG_SOLID;
78 break;
79 }
80 }
81
82 bg_ps_.init();
83 bg_ps_.framebuffer_set(&framebuffer_ref_);
84
85 if ((state.clipping_plane_count != 0) && (state.rv3d) && (state.rv3d->clipbb)) {
86 for (int i : IndexRange(8)) {
87 bbox_[i] = float4(float3(state.rv3d->clipbb->vec[i]), 1.0f);
88 }
89 bbox_.push_update();
90
92 bg_ps_.shader_set(res.shaders->background_clip_bound.get());
93 bg_ps_.bind_ubo("boundbox", &bbox_);
94 bg_ps_.push_constant("ucolor", res.theme.colors.clipping_border);
95 bg_ps_.draw(res.shapes.cube_solid.get());
96 }
97
98 bg_ps_.state_set(pass_state);
99 bg_ps_.shader_set(res.shaders->background_fill.get());
100 bg_ps_.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
101 bg_ps_.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
102 bg_ps_.bind_texture("color_buffer", &res.color_render_tx);
103 bg_ps_.bind_texture("depth_buffer", &res.depth_tx);
104 bg_ps_.push_constant("color_override", color_override);
105 bg_ps_.push_constant("bg_type", background_type);
106 bg_ps_.push_constant("vignette_enabled", false);
107 bg_ps_.draw_procedural(GPU_PRIM_TRIS, 1, 3);
108
109 if (state.vignette_enabled) {
110 const float vignette_aperture = state.v3d ? state.v3d->xr_vignette_aperture : 1.0f;
111
112 bg_vignette_ps_.init();
113 bg_vignette_ps_.framebuffer_set(&framebuffer_ref_);
114
115 bg_vignette_ps_.state_set(DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_ALPHA);
116 bg_vignette_ps_.shader_set(res.shaders->background_fill.get());
117 bg_vignette_ps_.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
118 bg_vignette_ps_.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
119 bg_vignette_ps_.bind_texture("color_buffer", &res.color_render_tx);
120 bg_vignette_ps_.bind_texture("depth_buffer", &res.depth_tx);
121 bg_vignette_ps_.push_constant("color_override", color_override);
122 bg_vignette_ps_.push_constant("bg_type", background_type);
123 bg_vignette_ps_.push_constant("vignette_enabled", true);
124 bg_vignette_ps_.push_constant("vignette_aperture", vignette_aperture);
125 bg_vignette_ps_.draw_procedural(GPU_PRIM_TRIS, 1, 3);
126 }
127 }
128
129 void draw_output(Framebuffer &framebuffer, Manager &manager, View &view) final
130 {
131 framebuffer_ref_ = framebuffer;
132 manager.submit(bg_ps_, view);
133 }
134
135 void draw_vignette(Framebuffer &framebuffer, Manager &manager, View &view)
136 {
137 framebuffer_ref_ = framebuffer;
138 manager.submit(bg_vignette_ps_, view);
139 }
140};
141
142} // namespace blender::draw::overlay
@ TH_BACKGROUND_GRADIENT_RADIAL
@ TH_BACKGROUND_SINGLE_COLOR
@ TH_BACKGROUND_GRADIENT_LINEAR
#define UNPACK3(a)
constexpr IndexRange()=default
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
const int state
detail::Pass< command::DrawCommandBuf > PassSimple
int get_value(int colorid)
@ TH_BACKGROUND_TYPE
@ DRW_STATE_CULL_BACK
Definition draw_state.hh:45
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_BLEND_ALPHA
Definition draw_state.hh:57
@ DRW_STATE_BLEND_MUL
Definition draw_state.hh:62
@ DRW_STATE_BLEND_BACKGROUND
Definition draw_state.hh:60
@ V3D_SHADING_BACKGROUND_VIEWPORT
@ V3D_SHADING_BACKGROUND_WORLD
@ BG_SOLID_CHECKER