Blender V5.3
workbench_state.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
11#include "DNA_userdef_types.h"
12
13#include "BKE_camera.h"
14#include "BKE_customdata.hh"
15#include "BKE_editmesh.hh"
16#include "BKE_mesh.hh"
17#include "BKE_mesh_types.hh"
18#include "BKE_paint.hh"
19#include "BKE_paint_bvh.hh"
20
22
24#include "DNA_world_types.h"
25
26#include "ED_paint.hh"
27#include "ED_view3d.hh"
28
29#include "GPU_capabilities.hh"
30
31namespace blender::workbench {
32
33/* Used for update detection on the render settings. */
34static bool operator!=(const View3DShading &a, const View3DShading &b)
35{
36 /* Only checks the properties that are actually used by workbench. */
37 if (a.type != b.type) {
38 return true;
39 }
40 if (a.color_type != b.color_type) {
41 return true;
42 }
43 if (a.flag != b.flag) {
44 return true;
45 }
46 if (a.light != b.light) {
47 return true;
48 }
49 if (a.background_type != b.background_type) {
50 return true;
51 }
52 if (a.cavity_type != b.cavity_type) {
53 return true;
54 }
55 if (a.wire_color_type != b.wire_color_type) {
56 return true;
57 }
58 if (StringRefNull(a.studio_light) != StringRefNull(b.studio_light)) {
59 return true;
60 }
61 if (StringRefNull(a.matcap) != StringRefNull(b.matcap)) {
62 return true;
63 }
64 if (a.shadow_intensity != b.shadow_intensity) {
65 return true;
66 }
67 if (float3(a.single_color) != float3(b.single_color)) {
68 return true;
69 }
70 if (a.studiolight_rot_z != b.studiolight_rot_z) {
71 return true;
72 }
73 if (float3(a.object_outline_color) != float3(b.object_outline_color)) {
74 return true;
75 }
76 if (a.xray_alpha != b.xray_alpha) {
77 return true;
78 }
79 if (a.xray_alpha_wire != b.xray_alpha_wire) {
80 return true;
81 }
82 if (a.cavity_valley_factor != b.cavity_valley_factor) {
83 return true;
84 }
85 if (a.cavity_ridge_factor != b.cavity_ridge_factor) {
86 return true;
87 }
88 if (float3(a.background_color) != float3(b.background_color)) {
89 return true;
90 }
91 if (a.curvature_ridge_factor != b.curvature_ridge_factor) {
92 return true;
93 }
94 if (a.curvature_valley_factor != b.curvature_valley_factor) {
95 return true;
96 }
97 return false;
98}
99
100void SceneState::init(const DRWContext *context,
101 bool scene_updated,
102 Object *camera_ob /*=nullptr*/)
103{
104 updated = scene_updated;
105 bool reset_taa = reset_taa_next_sample || scene_updated;
106 reset_taa_next_sample = false;
107
108 View3D *v3d = context->v3d;
109 RegionView3D *rv3d = context->rv3d;
110
111 scene = DEG_get_evaluated_scene(context->depsgraph);
112
113 if (assign_if_different(resolution, int2(context->viewport_size_get()))) {
114 /* In some cases, the viewport can change resolution without a call to `workbench_view_update`.
115 * This is the case when dragging a window between two screen with different DPI settings.
116 * (See #128712) */
117 reset_taa = true;
118 }
119
120 camera_object = camera_ob;
121 if (camera_object == nullptr && v3d && rv3d) {
122 camera_object = (rv3d->persp == RV3D_CAMOB) ? v3d->camera : nullptr;
123 }
126 nullptr;
127
128 object_mode = CTX_data_mode_enum_ex(context->object_edit, context->obact, context->object_mode);
129
130 /* TODO(@pragma37):
131 * Check why Workbench Next exposes OB_MATERIAL, and Workbench exposes OB_RENDER */
132 bool is_render_mode = !v3d || ELEM(v3d->shading.type, OB_RENDER, OB_MATERIAL);
133
134 const View3DShading previous_shading = shading;
135 shading = is_render_mode ? scene->display.shading : v3d->shading;
136
139
140 /* FIXME: This reproduce old behavior when workbench was separated in 2 engines.
141 * But this is a workaround for a missing update tagging. */
142 DRWState new_clip_state = RV3D_CLIPPING_ENABLED(v3d, rv3d) ? DRW_STATE_CLIP_PLANES :
144 DRWState old_clip_state = clip_planes.size() > 0 ? DRW_STATE_CLIP_PLANES : DRW_STATE_NO_DRAW;
145 if (new_clip_state != old_clip_state) {
146 reset_taa = true;
147 }
148 clip_planes.clear();
149 if (new_clip_state & DRW_STATE_CLIP_PLANES) {
150 int plane_len = (RV3D_LOCK_FLAGS(rv3d) & RV3D_BOXCLIP) ? 4 : 6;
151 for (auto i : IndexRange(plane_len)) {
152 clip_planes.append(rv3d->clip[i]);
153 }
154 }
155
156 if (shading.type < OB_SOLID) {
159 shading.xray_alpha = 0.0f;
160 }
161 else if (SHADING_XRAY_ENABLED(shading)) {
163 }
164 else {
165 shading.xray_alpha = 1.0f;
166 }
167 xray_mode = shading.xray_alpha != 1.0f;
168
169 if (xray_mode) {
170 /* Disable shading options that aren't supported in transparency mode. */
172 }
173
174 if (shading != previous_shading) {
175 reset_taa = true;
176 }
177
178 lighting_type = lighting_type_from_v3d_lighting(shading.light);
180
181 background_color = float4(0.0f);
182 if (is_render_mode && scene->r.alphamode != R_ALPHAPREMUL) {
184 /* NOTE: Only there to support world color override from Sequencer Edit Scene override.
185 * (see #149656) */
187 }
188 else if (World *w = scene->world) {
189 background_color = float4(w->horr, w->horg, w->horb, 1.0f);
190 }
191 }
192
193 if (rv3d && rv3d->rflag & RV3D_GPULIGHT_UPDATE) {
194 reset_taa = true;
195 /* FIXME: This reproduce old behavior when workbench was separated in 2 engines.
196 * But this is a workaround for a missing update tagging. */
198 }
199
201 if (matrix != view_projection_matrix) {
202 view_projection_matrix = matrix;
203 reset_taa = true;
204 }
205
207
208 bool is_playback = context->is_playback();
209 bool is_navigating = context->is_navigating();
210
211 /* Reset complete drawing when navigating or during viewport playback or when
212 * leaving one of those states. In case of multires modifier the navigation
213 * mesh differs from the viewport mesh, so we need to be sure to restart. */
214 if (is_playback || is_navigating) {
215 reset_taa = true;
217 }
218
219 int _samples_len = U.viewport_aa;
220 if (v3d && ELEM(v3d->shading.type, OB_RENDER, OB_MATERIAL)) {
221 _samples_len = scene->display.viewport_aa;
222 }
223 else if (context->is_scene_render()) {
224 _samples_len = scene->display.render_aa;
225 }
226 if (is_navigating || is_playback) {
227 /* Only draw using SMAA or no AA when navigating. */
228 _samples_len = min_ii(_samples_len, 1);
229 }
230 /* 0 samples means no AA */
231 draw_aa = _samples_len > 0;
232 _samples_len = max_ii(_samples_len, 1);
233
234 /* Reset the TAA when we have already draw a sample, but the sample count differs from previous
235 * time. This removes render artifacts when the viewport anti-aliasing in the user preferences
236 * is set to a lower value. */
237 if (samples_len != _samples_len) {
238 samples_len = _samples_len;
239 reset_taa = true;
240 }
241
242 /* TODO(@pragma37): volumes_do */
243
251 draw_dof = camera && camera->dof.flag & CAM_DOF_ENABLED &&
253
255
256 const bool shadows_use_rt_new = draw_shadows &&
259 if (assign_if_different(shadows_use_rt, shadows_use_rt_new)) {
260 reset_taa = true;
261 }
262
266 false;
267
268 if (reset_taa || samples_len <= 1) {
269 sample = 0;
270 }
271 else {
272 sample++;
273 }
275};
276
278{
279 const StringRef name = mesh.active_color_attribute ? mesh.active_color_attribute :
280 mesh.default_color_attribute;
281 if (name.is_empty()) {
282 return false;
283 }
284 if (mesh.runtime->wrapper_type == ME_WRAPPER_TYPE_BMESH) {
286 const BMDataLayerLookup attr = BM_data_layer_lookup(bm, name);
288 }
289 const bke::AttributeAccessor attributes = mesh.attributes();
290 return bke::mesh::is_color_attribute(attributes.lookup_meta_data(name));
291}
292
294{
295 StringRef active_uv_map = mesh.active_or_default_uv_map_name();
296 if (mesh.runtime->wrapper_type == ME_WRAPPER_TYPE_BMESH) {
298 const BMDataLayerLookup attr = BM_data_layer_lookup(bm, active_uv_map);
299 return attr && bke::mesh::is_uv_map(bke::AttributeMetaData{attr.domain, attr.type});
300 }
301 const bke::AttributeAccessor attributes = mesh.attributes();
302 return bke::mesh::is_uv_map(attributes.lookup_meta_data(active_uv_map));
303}
304
306 const SceneState &scene_state,
307 const SceneResources &resources,
308 Object *ob,
309 Manager &manager)
310{
311 const bool is_active = (ob == draw_ctx->obact);
312
313 const auto is_gsplat = [&]() {
314 if (ob->type == OB_POINTCLOUD) {
316 return pointcloud.type == PointCloudType::GSplat;
317 }
318 return false;
319 };
320
322 !draw_ctx->is_image_render();
323 draw_shadow = scene_state.draw_shadows && (ob->dtx & OB_DRAW_NO_SHADOW_CAST) == 0 &&
324 !sculpt_pbvh && !is_gsplat() && !(is_active && DRW_object_use_hide_faces(ob));
325
327
328 /* Don't perform CustomData lookup unless it's really necessary, since it's quite expensive. */
329 const auto has_color = [&]() {
330 if (ob->type != OB_MESH) {
331 return false;
332 }
335 };
336
337 const auto has_uv = [&]() {
338 if (ob->type != OB_MESH) {
339 return false;
340 }
343 };
344
345 if (color_type == V3D_SHADING_TEXTURE_COLOR && (!has_uv() || ob->dt < OB_TEXTURE)) {
347 }
348 else if (color_type == V3D_SHADING_VERTEX_COLOR && !has_color()) {
350 }
351
352 if (sculpt_pbvh) {
355 {
356 /* Force use of material color for sculpt. */
358 }
359 }
360 else if (ob->type == OB_MESH && !draw_ctx->is_scene_render()) {
361 /* Force texture or vertex mode if object is in paint mode. */
362 const bool is_vertpaint_mode = is_active && (scene_state.object_mode == CTX_MODE_PAINT_VERTEX);
363 const bool is_texpaint_mode = is_active && (scene_state.object_mode == CTX_MODE_PAINT_TEXTURE);
364 if (is_vertpaint_mode && has_color()) {
366 }
367 else if (is_texpaint_mode && has_uv()) {
370 const ImagePaintSettings *imapaint = &scene_state.scene->toolsettings->imapaint;
371 if (imapaint->mode == IMAGEPAINT_MODE_IMAGE) {
372 if (imapaint->canvas) {
373 image_paint_override = MaterialTexture(manager, imapaint->canvas);
376 const bool use_linear_filter = imapaint->interp == IMAGEPAINT_INTERP_LINEAR;
378 GPU_SAMPLER_FILTERING_LINEAR, use_linear_filter);
379 }
380 else {
382 }
383 }
384 }
385 }
386
391}
392
393} // namespace blender::workbench
Camera data-block and utility functions.
CustomData interface, see also DNA_customdata_types.h.
A BVH for high poly meshes.
#define IMAGEPAINT_MODE_IMAGE
#define RV3D_LOCK_FLAGS(rv3d)
#define RV3D_CLIPPING_ENABLED(v3d, rv3d)
#define SHADING_XRAY_ALPHA(shading)
#define SHADING_XRAY_ENABLED(shading)
#define UNPACK3(a)
#define ELEM(...)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
constexpr IndexRange()=default
constexpr StringRefNull()
std::optional< AttributeMetaData > lookup_meta_data(StringRef name) const
constexpr bool is_empty() const
static View & default_get()
Definition draw_view.cc:317
const float4x4 persmat(int view_id=0) const
Definition draw_view.hh:161
const float3 & forward(int view_id=0) const
Definition draw_view.hh:130
bool is_uv_map(const AttributeMetaData &meta_data)
bool is_color_attribute(const AttributeMetaData &meta_data)
pbvh::Tree * pbvh_get(Object &object)
bool operator!=(const CameraData &a, const CameraData &b)
static bool mesh_has_color_attribute(const Mesh &mesh)
static bool mesh_has_uv_map_attribute(const Mesh &mesh)
UserDef U
Definition blender.cc:58
bool assign_if_different(T &old_value, T &&new_value)
@ IMAGEPAINT_INTERP_LINEAR
@ GPU_SAMPLER_EXTEND_MODE_REPEAT
@ GPU_SAMPLER_FILTERING_LINEAR
MINLINE int max_ii(int a, int b)
bool GPU_ray_query_support()
MatBase< float, 4, 4 > float4x4
@ V3D_SHADING_BACKFACE_CULLING
@ V3D_SHADING_OBJECT_OUTLINE
@ V3D_SHADING_DEPTH_OF_FIELD
@ CTX_MODE_PAINT_VERTEX
@ CTX_MODE_PAINT_TEXTURE
VecBase< float, 4 > float4
@ V3D_SHADING_CAVITY_SSAO
@ V3D_SHADING_CAVITY_CURVATURE
@ V3D_SHADING_CAVITY_BOTH
VecBase< int32_t, 2 > int2
T & DRW_object_get_data_for_drawing(const Object &object)
@ USER_GPU_FLAG_WORKBENCH_RT_SHADOWS
bool DRW_object_use_hide_faces(const Object *ob)
BMDataLayerLookup BM_data_layer_lookup(const BMesh &bm, const StringRef name)
@ DRW_STATE_CULL_BACK
Definition draw_state.hh:45
@ DRW_STATE_NO_DRAW
Definition draw_state.hh:29
@ DRW_STATE_CLIP_PLANES
Definition draw_state.hh:72
@ V3D_SHADING_MATERIAL_COLOR
@ V3D_SHADING_VERTEX_COLOR
@ V3D_SHADING_TEXTURE_COLOR
@ V3D_SHADING_OBJECT_COLOR
@ V3D_SHADING_BACKGROUND_VIEWPORT
bool BKE_sculptsession_use_pbvh_draw(const Object *ob, const RegionView3D *rv3d)
const BMesh * BKE_editmesh_bmesh_get(const Object *ob)
Definition editmesh.cc:65
VecBase< float, 3 > float3
@ ME_WRAPPER_TYPE_BMESH
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
MINLINE int min_ii(int a, int b)
@ PAINT_DEBUG_SHOW_BVH_NODES
enum eContextObjectMode CTX_data_mode_enum_ex(const Object *obedit, const Object *ob, eObjectMode object_mode)
RegionView3D * rv3d
bool is_image_render() const
bool is_scene_render() const
GPUSamplerExtendMode extend_yz
void set_filtering_flag_from_test(GPUSamplerFiltering filtering_flags, bool test)
GPUSamplerExtendMode extend_x
eImagePaint_Interpolation interp
eObject_DrawExtraFlag dtx
ePaintDebugFlags debug_flags
eRegionView3D_Flag rflag
eRegionView3D_Persp persp
eSceneDisplay_AA render_aa
eSceneDisplay_AA viewport_aa
struct World * world
struct SceneDisplay display
struct RenderData r
struct ToolSettings * toolsettings
struct ImagePaintSettings imapaint
eV3DShadingColorType color_type
eView3DShading_Flag flag
eV3DShadingColorType wire_color_type
eV3DShadingBackgroundType background_type
eV3DShadingLightingMode light
eView3DShading_CavityType cavity_type
struct Object * camera
View3DShading shading
ObjectState(const DRWContext *draw_ctx, const SceneState &scene_state, const SceneResources &resources, Object *ob, Manager &manager)
void init(const DRWContext *context, bool scene_updated, Object *camera_ob=nullptr)