Welcome to mirror list, hosted at ThFree Co, Russian Federation.

workbench_state.cc « workbench « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a22599f4016e43537ea5c70b5c10566cf3740822 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2020 Blender Foundation. */

#include "workbench_private.hh"

#include "BKE_camera.h"
#include "BKE_editmesh.h"
#include "BKE_modifier.h"
#include "BKE_object.h"
#include "BKE_paint.h"
#include "BKE_particle.h"
#include "BKE_pbvh.h"
#include "DEG_depsgraph_query.h"
#include "DNA_fluid_types.h"
#include "ED_paint.h"
#include "ED_view3d.h"
#include "GPU_capabilities.h"

namespace blender::workbench {

void SceneState::init()
{
  bool reset_taa = reset_taa_next_sample;
  reset_taa_next_sample = false;

  const DRWContextState *context = DRW_context_state_get();
  View3D *v3d = context->v3d;
  RegionView3D *rv3d = context->rv3d;

  scene = DEG_get_evaluated_scene(context->depsgraph);

  GPUTexture *viewport_tx = DRW_viewport_texture_list_get()->color;
  resolution = int2(GPU_texture_width(viewport_tx), GPU_texture_height(viewport_tx));

  camera_object = nullptr;
  if (v3d && rv3d) {
    camera_object = (rv3d->persp == RV3D_CAMOB) ? v3d->camera : nullptr;
  }
  else {
    /*TODO(Miguel Pozo)*/
    // camera = wpd->cam_original_ob;
  }
  camera = camera_object && camera_object->type == OB_CAMERA ?
               static_cast<Camera *>(camera_object->data) :
               nullptr;

  object_mode = CTX_data_mode_enum_ex(context->object_edit, context->obact, context->object_mode);

  /* TODO(Miguel Pozo):
   * Check why Workbench Next exposes OB_MATERIAL, and Workbench exposes OB_RENDER */
  bool is_render_mode = !v3d || ELEM(v3d->shading.type, OB_RENDER, OB_MATERIAL);

  const View3DShading previous_shading = shading;
  shading = is_render_mode ? scene->display.shading : v3d->shading;

  cull_state = shading.flag & V3D_SHADING_BACKFACE_CULLING ? DRW_STATE_CULL_BACK :
                                                             DRW_STATE_NO_DRAW;

  /* FIXME: This reproduce old behavior when workbench was separated in 2 engines.
   * But this is a workaround for a missing update tagging. */
  DRWState new_clip_state = RV3D_CLIPPING_ENABLED(v3d, rv3d) ? DRW_STATE_CLIP_PLANES :
                                                               DRW_STATE_NO_DRAW;
  DRWState old_clip_state = clip_planes.size() > 0 ? DRW_STATE_CLIP_PLANES : DRW_STATE_NO_DRAW;
  if (new_clip_state != old_clip_state) {
    reset_taa = true;
  }
  clip_planes.clear();
  if (new_clip_state & DRW_STATE_CLIP_PLANES) {
    int plane_len = (RV3D_LOCK_FLAGS(rv3d) & RV3D_BOXCLIP) ? 4 : 6;
    for (auto i : IndexRange(plane_len)) {
      clip_planes.append(rv3d->clip[i]);
    }
  }

  if (!is_render_mode) {
    if (shading.type < OB_SOLID) {
      shading.light = V3D_LIGHTING_FLAT;
      shading.color_type = V3D_SHADING_OBJECT_COLOR;
      shading.xray_alpha = 0.0f;
    }
    else if (SHADING_XRAY_ENABLED(shading)) {
      shading.xray_alpha = SHADING_XRAY_ALPHA(shading);
    }
    else {
      shading.xray_alpha = 1.0f;
    }
  }
  xray_mode = !is_render_mode && shading.xray_alpha != 1.0f;

  if (SHADING_XRAY_FLAG_ENABLED(shading)) {
    /* Disable shading options that aren't supported in transparency mode. */
    shading.flag &= ~(V3D_SHADING_SHADOW | V3D_SHADING_CAVITY | V3D_SHADING_DEPTH_OF_FIELD);
  }
  if (SHADING_XRAY_ENABLED(shading) != SHADING_XRAY_ENABLED(previous_shading) ||
      shading.flag != previous_shading.flag) {
    reset_taa = true;
  }

  lighting_type = lighting_type_from_v3d_lighting(shading.light);
  material_override = Material(shading.single_color);

  background_color = float4(0.0f);
  if (is_render_mode && scene->r.alphamode != R_ALPHAPREMUL) {
    if (World *w = scene->world) {
      background_color = float4(w->horr, w->horg, w->horb, 1.0f);
    }
  }

  if (rv3d && rv3d->rflag & RV3D_GPULIGHT_UPDATE) {
    reset_taa = true;
    /* FIXME: This reproduce old behavior when workbench was separated in 2 engines.
     * But this is a workaround for a missing update tagging. */
    rv3d->rflag &= ~RV3D_GPULIGHT_UPDATE;
  }

  float4x4 matrix;
  /*TODO(Miguel Pozo): New API ?*/
  DRW_view_persmat_get(nullptr, matrix.ptr(), false);
  if (matrix != view_projection_matrix) {
    view_projection_matrix = matrix;
    reset_taa = true;
  }

  bool is_playback = DRW_state_is_playback();
  bool is_navigating = DRW_state_is_navigating();

  /* Reset complete drawing when navigating or during viewport playback or when
   * leaving one of those states. In case of multires modifier the navigation
   * mesh differs from the viewport mesh, so we need to be sure to restart. */
  if (is_playback || is_navigating) {
    reset_taa = true;
    reset_taa_next_sample = true;
  }

  int _samples_len = U.viewport_aa;
  if (v3d && ELEM(v3d->shading.type, OB_RENDER, OB_MATERIAL)) {
    _samples_len = scene->display.viewport_aa;
  }
  else if (DRW_state_is_image_render()) {
    _samples_len = scene->display.render_aa;
  }
  if (is_navigating || is_playback) {
    /* Only draw using SMAA or no AA when navigating. */
    _samples_len = min_ii(_samples_len, 1);
  }
  /* 0 samples means no AA */
  draw_aa = _samples_len > 0;
  _samples_len = max_ii(_samples_len, 1);

  /* Reset the TAA when we have already draw a sample, but the sample count differs from previous
   * time. This removes render artifacts when the viewport anti-aliasing in the user preferences
   * is set to a lower value. */
  if (samples_len != _samples_len) {
    samples_len = _samples_len;
    reset_taa = true;
  }

  if (reset_taa || samples_len <= 1) {
    sample = 0;
  }
  else {
    sample++;
  }
  render_finished = sample >= samples_len && samples_len > 1;

  /* TODO(Miguel Pozo) volumes_do */

  draw_cavity = shading.flag & V3D_SHADING_CAVITY &&
                ELEM(shading.cavity_type, V3D_SHADING_CAVITY_SSAO, V3D_SHADING_CAVITY_BOTH);
  draw_curvature = shading.flag & V3D_SHADING_CAVITY && ELEM(shading.cavity_type,
                                                             V3D_SHADING_CAVITY_CURVATURE,
                                                             V3D_SHADING_CAVITY_BOTH);
  draw_outline = shading.flag & V3D_SHADING_OBJECT_OUTLINE;
  draw_dof = camera && camera->dof.flag & CAM_DOF_ENABLED &&
             shading.flag & V3D_SHADING_DEPTH_OF_FIELD;

  draw_transparent_depth = draw_outline || draw_dof;
  draw_object_id = draw_outline || draw_curvature;
};

static const CustomData *get_loop_custom_data(const Mesh *mesh)
{
  if (mesh->runtime->wrapper_type == ME_WRAPPER_TYPE_BMESH) {
    BLI_assert(mesh->edit_mesh != nullptr);
    BLI_assert(mesh->edit_mesh->bm != nullptr);
    return &mesh->edit_mesh->bm->ldata;
  }
  return &mesh->ldata;
}

static const CustomData *get_vert_custom_data(const Mesh *mesh)
{
  if (mesh->runtime->wrapper_type == ME_WRAPPER_TYPE_BMESH) {
    BLI_assert(mesh->edit_mesh != nullptr);
    BLI_assert(mesh->edit_mesh->bm != nullptr);
    return &mesh->edit_mesh->bm->vdata;
  }
  return &mesh->vdata;
}

ObjectState::ObjectState(const SceneState &scene_state, Object *ob)
{
  sculpt_pbvh = false;
  texture_paint_mode = false;
  image_paint_override = nullptr;
  override_sampler_state = GPU_SAMPLER_DEFAULT;
  draw_shadow = false;

  const DRWContextState *draw_ctx = DRW_context_state_get();
  const Mesh *me = (ob->type == OB_MESH) ? static_cast<Mesh *>(ob->data) : nullptr;
  const bool is_active = (ob == draw_ctx->obact);
  /* TODO(Miguel Pozo) Is the double check needed?
   * If it is, wouldn't be needed for sculpt_pbvh too?
   */
  const bool is_render = DRW_state_is_image_render() && (draw_ctx->v3d == nullptr);

  color_type = (eV3DShadingColorType)scene_state.shading.color_type;
  if (!(is_active && DRW_object_use_hide_faces(ob))) {
    draw_shadow = (ob->dtx & OB_DRAW_NO_SHADOW_CAST) == 0 &&
                  scene_state.shading.flag & V3D_SHADING_SHADOW;
  }
  if (me == nullptr) {
    if (color_type == V3D_SHADING_TEXTURE_COLOR) {
      color_type = V3D_SHADING_MATERIAL_COLOR;
    }
    else if (color_type == V3D_SHADING_VERTEX_COLOR) {
      color_type = V3D_SHADING_OBJECT_COLOR;
    }
    use_per_material_batches = color_type == V3D_SHADING_MATERIAL_COLOR;
    /* Early return */
    return;
  }

  sculpt_pbvh = BKE_sculptsession_use_pbvh_draw(ob, draw_ctx->rv3d) &&
                !DRW_state_is_image_render();

  if (sculpt_pbvh) {
    /* Shadows are unsupported in sculpt mode. We could revert to the slow
     * method in this case but I'm not sure if it's a good idea given that
     * sculpted meshes are heavy to begin with. */
    draw_shadow = false;

    if (color_type == V3D_SHADING_TEXTURE_COLOR && BKE_pbvh_type(ob->sculpt->pbvh) != PBVH_FACES) {
      /* Force use of material color for sculpt. */
      color_type = V3D_SHADING_MATERIAL_COLOR;
    }

    /* Bad call C is required to access the tool system that is context aware. Cast to non-const
     * due to current API. */
    bContext *C = (bContext *)DRW_context_state_get()->evil_C;
    if (C != NULL) {
      color_type = ED_paint_shading_color_override(
          C, &scene_state.scene->toolsettings->paint_mode, ob, color_type);
    }
  }
  else {
    const CustomData *cd_vdata = get_vert_custom_data(me);
    const CustomData *cd_ldata = get_loop_custom_data(me);

    bool has_color = (CustomData_has_layer(cd_vdata, CD_PROP_COLOR) ||
                      CustomData_has_layer(cd_vdata, CD_PROP_BYTE_COLOR) ||
                      CustomData_has_layer(cd_ldata, CD_PROP_COLOR) ||
                      CustomData_has_layer(cd_ldata, CD_PROP_BYTE_COLOR));

    if (color_type == V3D_SHADING_TEXTURE_COLOR) {
      if (ob->dt < OB_TEXTURE || !CustomData_has_layer(cd_ldata, CD_MLOOPUV)) {
        color_type = V3D_SHADING_MATERIAL_COLOR;
      }
    }
    else if (color_type == V3D_SHADING_VERTEX_COLOR && !has_color) {
      color_type = V3D_SHADING_OBJECT_COLOR;
    }

    if (!is_render) {
      /* Force texture or vertex mode if object is in paint mode. */
      const bool is_vertpaint_mode = is_active &&
                                     (scene_state.object_mode == CTX_MODE_PAINT_VERTEX);
      const bool is_texpaint_mode = is_active &&
                                    (scene_state.object_mode == CTX_MODE_PAINT_TEXTURE);
      if (is_vertpaint_mode && has_color) {
        color_type = V3D_SHADING_VERTEX_COLOR;
      }
      else if (is_texpaint_mode && CustomData_has_layer(cd_ldata, CD_MLOOPUV)) {
        color_type = V3D_SHADING_TEXTURE_COLOR;
        texture_paint_mode = true;

        const ImagePaintSettings *imapaint = &scene_state.scene->toolsettings->imapaint;
        if (imapaint->mode == IMAGEPAINT_MODE_IMAGE) {
          image_paint_override = imapaint->canvas;
          override_sampler_state = GPU_SAMPLER_REPEAT;
          SET_FLAG_FROM_TEST(override_sampler_state,
                             imapaint->interp == IMAGEPAINT_INTERP_LINEAR,
                             GPU_SAMPLER_FILTER);
        }
      }
    }
  }

  use_per_material_batches = image_paint_override == nullptr && ELEM(color_type,
                                                                     V3D_SHADING_TEXTURE_COLOR,
                                                                     V3D_SHADING_MATERIAL_COLOR);
}

}  // namespace blender::workbench