Blender  V3.3
eevee_instance.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2021 Blender Foundation.
3  */
4 
11 #include <sstream>
12 
13 #include "BKE_global.h"
14 #include "BKE_object.h"
15 #include "BLI_rect.h"
16 #include "DEG_depsgraph_query.h"
17 #include "DNA_ID.h"
18 #include "DNA_lightprobe_types.h"
19 #include "DNA_modifier_types.h"
20 #include "RE_pipeline.h"
21 
22 #include "eevee_instance.hh"
23 
24 namespace blender::eevee {
25 
26 /* -------------------------------------------------------------------- */
36 void Instance::init(const int2 &output_res,
37  const rcti *output_rect,
38  RenderEngine *render_,
39  Depsgraph *depsgraph_,
40  const LightProbe *light_probe_,
41  Object *camera_object_,
42  const RenderLayer *render_layer_,
43  const DRWView *drw_view_,
44  const View3D *v3d_,
45  const RegionView3D *rv3d_)
46 {
47  UNUSED_VARS(light_probe_);
48  render = render_;
49  depsgraph = depsgraph_;
50  camera_orig_object = camera_object_;
51  render_layer = render_layer_;
52  drw_view = drw_view_;
53  v3d = v3d_;
54  rv3d = rv3d_;
55 
56  info = "";
57 
58  update_eval_members();
59 
61  camera.init();
62  film.init(output_res, output_rect);
63  main_view.init();
64 }
65 
66 void Instance::set_time(float time)
67 {
70  update_eval_members();
71 }
72 
73 void Instance::update_eval_members()
74 {
79  nullptr;
80 }
81 
84 /* -------------------------------------------------------------------- */
93 {
96 
97  gpencil_engine_enabled = false;
98 
100  pipelines.sync();
101  main_view.sync();
102  world.sync();
103  camera.sync();
104  film.sync();
105 }
106 
108 {
109  const bool is_renderable_type = ELEM(ob->type, OB_CURVES, OB_GPENCIL, OB_MESH);
110  const int ob_visibility = DRW_object_visibility_in_active_context(ob);
111  const bool partsys_is_visible = (ob_visibility & OB_VISIBLE_PARTICLES) != 0 &&
112  (ob->type == OB_MESH);
113  const bool object_is_visible = DRW_object_is_renderable(ob) &&
114  (ob_visibility & OB_VISIBLE_SELF) != 0;
115 
116  if (!is_renderable_type || (!partsys_is_visible && !object_is_visible)) {
117  return;
118  }
119 
120  ObjectHandle &ob_handle = sync.sync_object(ob);
121 
122  if (partsys_is_visible && ob != DRW_context_state_get()->object_edit) {
123  LISTBASE_FOREACH (ModifierData *, md, &ob->modifiers) {
124  if (md->type == eModifierType_ParticleSystem) {
125  sync.sync_curves(ob, ob_handle, md);
126  }
127  }
128  }
129 
130  if (object_is_visible) {
131  switch (ob->type) {
132  case OB_LAMP:
133  break;
134  case OB_MESH:
135  case OB_CURVES_LEGACY:
136  case OB_SURF:
137  case OB_FONT:
138  case OB_MBALL: {
139  sync.sync_mesh(ob, ob_handle);
140  break;
141  }
142  case OB_VOLUME:
143  break;
144  case OB_CURVES:
145  sync.sync_curves(ob, ob_handle);
146  break;
147  case OB_GPENCIL:
148  sync.sync_gpencil(ob, ob_handle);
149  break;
150  default:
151  break;
152  }
153  }
154 
155  ob_handle.reset_recalc_flag();
156 }
157 
158 /* Wrapper to use with DRW_render_object_iter. */
159 void Instance::object_sync_render(void *instance_,
160  Object *ob,
161  RenderEngine *engine,
163 {
164  UNUSED_VARS(engine, depsgraph);
165  Instance &inst = *reinterpret_cast<Instance *>(instance_);
166  inst.object_sync(ob);
167 }
168 
170 {
171  velocity.end_sync();
172  sampling.end_sync();
173  film.end_sync();
174 }
175 
177 {
179 
180  begin_sync();
181  DRW_render_object_iter(this, render, depsgraph, object_sync_render);
182  end_sync();
183 
185  /* Also we weed to have a correct FBO bound for #DRW_hair_update */
186  // GPU_framebuffer_bind();
187  // DRW_hair_update();
188 }
189 
192 /* -------------------------------------------------------------------- */
200 void Instance::render_sample()
201 {
202  if (sampling.finished_viewport()) {
203  film.display();
204  return;
205  }
206 
207  /* Motion blur may need to do re-sync after a certain number of sample. */
208  if (!is_viewport() && sampling.do_render_sync()) {
209  render_sync();
210  }
211 
212  sampling.step();
213 
214  main_view.render();
215 }
216 
219 /* -------------------------------------------------------------------- */
223 void Instance::render_frame(RenderLayer *render_layer, const char *view_name)
224 {
225  while (!sampling.finished()) {
226  this->render_sample();
227  /* TODO(fclem) print progression. */
228  }
229 
230  /* Read Results. */
232  for (auto i : IndexRange(EEVEE_RENDER_PASS_MAX_BIT)) {
233  eViewLayerEEVEEPassType pass_type = eViewLayerEEVEEPassType(pass_bits & (1 << i));
234  if (pass_type == 0) {
235  continue;
236  }
237 
238  const char *pass_name = Film::pass_to_render_pass_name(pass_type);
239  RenderPass *rp = RE_pass_find_by_name(render_layer, pass_name, view_name);
240  if (rp) {
241  float *result = film.read_pass(pass_type);
242  if (result) {
243  std::cout << "read " << pass_name << std::endl;
245  /* WORKAROUND: We use texture read to avoid using a framebuffer to get the render result.
246  * However, on some implementation, we need a buffer with a few extra bytes for the read to
247  * happen correctly (see GLTexture::read()). So we need a custom memory allocation. */
248  /* Avoid memcpy(), replace the pointer directly. */
249  MEM_SAFE_FREE(rp->rect);
250  rp->rect = result;
252  }
253  }
254  }
255 }
256 
258 {
259  UNUSED_VARS(dfbl);
260  render_sample();
262 
263  if (!sampling.finished_viewport()) {
265  }
266 
268  std::stringstream ss;
269  ss << "Compiling Shaders " << materials.queued_shaders_count;
270  info = ss.str();
271  }
272 }
273 
276 } // namespace blender::eevee
General operations, lookup, etc. for blender objects.
@ OB_VISIBLE_SELF
Definition: BKE_object.h:150
@ OB_VISIBLE_PARTICLES
Definition: BKE_object.h:151
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
void BLI_mutex_lock(ThreadMutex *mutex)
Definition: threads.cc:373
void BLI_mutex_unlock(ThreadMutex *mutex)
Definition: threads.cc:378
#define UNUSED_VARS(...)
#define ELEM(...)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
struct ViewLayer * DEG_get_evaluated_view_layer(const struct Depsgraph *graph)
struct Object * DEG_get_evaluated_object(const struct Depsgraph *depsgraph, struct Object *object)
struct Scene * DEG_get_evaluated_scene(const struct Depsgraph *graph)
ID and Library types, which are fundamental for sdna.
#define EEVEE_RENDER_PASS_MAX_BIT
eViewLayerEEVEEPassType
@ eModifierType_ParticleSystem
@ OB_MBALL
@ OB_SURF
@ OB_FONT
@ OB_LAMP
@ OB_MESH
@ OB_VOLUME
@ OB_CURVES_LEGACY
@ OB_CURVES
@ OB_GPENCIL
#define MEM_SAFE_FREE(v)
float * read_pass(eViewLayerEEVEEPassType pass_type)
Definition: eevee_film.cc:606
static const char * pass_to_render_pass_name(eViewLayerEEVEEPassType pass_type)
Definition: eevee_film.hh:168
void init(const int2 &full_extent, const rcti *output_rect)
Definition: eevee_film.cc:165
eViewLayerEEVEEPassType enabled_passes_get() const
Definition: eevee_film.cc:459
A running instance of the engine.
void draw_viewport(DefaultFramebufferList *dfbl)
const DRWView * drw_view
RenderBuffers render_buffers
const RenderLayer * render_layer
void object_sync(Object *ob)
void render_frame(RenderLayer *render_layer, const char *view_name)
const RegionView3D * rv3d
void init(const int2 &output_res, const rcti *output_rect, RenderEngine *render, Depsgraph *depsgraph, const LightProbe *light_probe_=nullptr, Object *camera_object=nullptr, const RenderLayer *render_layer=nullptr, const DRWView *drw_view=nullptr, const View3D *v3d=nullptr, const RegionView3D *rv3d=nullptr)
void init(const Scene *scene)
ObjectHandle & sync_object(Object *ob)
Definition: eevee_sync.cc:36
void sync_gpencil(Object *ob, ObjectHandle &ob_handle)
Definition: eevee_sync.cc:253
void sync_mesh(Object *ob, ObjectHandle &ob_handle)
Definition: eevee_sync.cc:105
void sync_curves(Object *ob, ObjectHandle &ob_handle, ModifierData *modifier_data=nullptr)
Definition: eevee_sync.cc:294
double time
const Depsgraph * depsgraph
void DRW_render_instance_buffer_finish(void)
bool DRW_object_is_renderable(const Object *ob)
Definition: draw_manager.c:180
int DRW_object_visibility_in_active_context(const Object *ob)
Definition: draw_manager.c:209
const DRWContextState * DRW_context_state_get(void)
void DRW_cache_restart(void)
void DRW_render_set_time(RenderEngine *engine, Depsgraph *depsgraph, int frame, float subframe)
void DRW_viewport_request_redraw(void)
Definition: draw_manager.c:643
void DRW_render_object_iter(void *vedata, RenderEngine *engine, struct Depsgraph *depsgraph, void(*callback)(void *vedata, Object *ob, RenderEngine *engine, struct Depsgraph *depsgraph))
MINLINE float fractf(float a)
#define floorf(x)
Definition: metal/compat.h:224
RenderPass * RE_pass_find_by_name(RenderLayer *rl, const char *name, const char *viewname)
Definition: pipeline.c:2591
struct Object * object_edit
Definition: DRW_render.h:1003
ListBase modifiers
ThreadMutex update_render_passes_mutex
Definition: RE_engine.h:156
float * rect
Definition: RE_pipeline.h:67