1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __BLENDER_SYNC_H__
18 #define __BLENDER_SYNC_H__
19 
20 #include "MEM_guardedalloc.h"
21 #include "RNA_access.h"
22 #include "RNA_blender_cpp.h"
23 #include "RNA_types.h"
24 
25 #include "blender/blender_id_map.h"
26 #include "blender/blender_viewport.h"
27 
28 #include "render/scene.h"
29 #include "render/session.h"
30 
31 #include "util/util_map.h"
32 #include "util/util_set.h"
33 #include "util/util_transform.h"
34 #include "util/util_vector.h"
35 
36 CCL_NAMESPACE_BEGIN
37 
38 class Background;
39 class BlenderObjectCulling;
40 class BlenderViewportParameters;
41 class Camera;
42 class Film;
43 class Hair;
44 class Light;
45 class Mesh;
46 class Object;
47 class ParticleSystem;
48 class Scene;
49 class ViewLayer;
50 class Shader;
51 class ShaderGraph;
52 class ShaderNode;
53 
54 class BlenderSync {
55  public:
56   BlenderSync(BL::RenderEngine &b_engine,
57               BL::BlendData &b_data,
58               BL::Scene &b_scene,
59               Scene *scene,
60               bool preview,
61               Progress &progress);
62   ~BlenderSync();
63 
64   void reset(BL::BlendData &b_data, BL::Scene &b_scene);
65 
66   /* sync */
67   void sync_recalc(BL::Depsgraph &b_depsgraph, BL::SpaceView3D &b_v3d);
68   void sync_data(BL::RenderSettings &b_render,
69                  BL::Depsgraph &b_depsgraph,
70                  BL::SpaceView3D &b_v3d,
71                  BL::Object &b_override,
72                  int width,
73                  int height,
74                  void **python_thread_state);
75   void sync_view_layer(BL::SpaceView3D &b_v3d, BL::ViewLayer &b_view_layer);
76   vector<Pass> sync_render_passes(BL::RenderLayer &b_render_layer,
77                                   BL::ViewLayer &b_view_layer,
78                                   bool adaptive_sampling,
79                                   const DenoiseParams &denoising);
80   void sync_integrator();
81   void sync_camera(BL::RenderSettings &b_render,
82                    BL::Object &b_override,
83                    int width,
84                    int height,
85                    const char *viewname);
86   void sync_view(BL::SpaceView3D &b_v3d, BL::RegionView3D &b_rv3d, int width, int height);
get_layer_samples()87   inline int get_layer_samples()
88   {
89     return view_layer.samples;
90   }
get_layer_bound_samples()91   inline int get_layer_bound_samples()
92   {
93     return view_layer.bound_samples;
94   }
95 
96   /* get parameters */
97   static SceneParams get_scene_params(BL::Scene &b_scene, bool background);
98   static SessionParams get_session_params(
99       BL::RenderEngine &b_engine,
100       BL::Preferences &b_userpref,
101       BL::Scene &b_scene,
102       bool background,
103       BL::ViewLayer b_view_layer = BL::ViewLayer(PointerRNA_NULL));
104   static bool get_session_pause(BL::Scene &b_scene, bool background);
105   static BufferParams get_buffer_params(BL::RenderSettings &b_render,
106                                         BL::SpaceView3D &b_v3d,
107                                         BL::RegionView3D &b_rv3d,
108                                         Camera *cam,
109                                         int width,
110                                         int height,
111                                         const bool use_denoiser);
112 
113   static PassType get_pass_type(BL::RenderPass &b_pass);
114   static int get_denoising_pass(BL::RenderPass &b_pass);
115 
116  private:
117   static DenoiseParams get_denoise_params(BL::Scene &b_scene,
118                                           BL::ViewLayer &b_view_layer,
119                                           bool background);
120 
121   /* sync */
122   void sync_lights(BL::Depsgraph &b_depsgraph, bool update_all);
123   void sync_materials(BL::Depsgraph &b_depsgraph, bool update_all);
124   void sync_objects(BL::Depsgraph &b_depsgraph, BL::SpaceView3D &b_v3d, float motion_time = 0.0f);
125   void sync_motion(BL::RenderSettings &b_render,
126                    BL::Depsgraph &b_depsgraph,
127                    BL::SpaceView3D &b_v3d,
128                    BL::Object &b_override,
129                    int width,
130                    int height,
131                    void **python_thread_state);
132   void sync_film(BL::SpaceView3D &b_v3d);
133   void sync_view();
134 
135   /* Shader */
136   void sync_world(BL::Depsgraph &b_depsgraph, BL::SpaceView3D &b_v3d, bool update_all);
137   void sync_shaders(BL::Depsgraph &b_depsgraph, BL::SpaceView3D &b_v3d);
138   void sync_nodes(Shader *shader, BL::ShaderNodeTree &b_ntree);
139 
140   /* Object */
141   Object *sync_object(BL::Depsgraph &b_depsgraph,
142                       BL::ViewLayer &b_view_layer,
143                       BL::DepsgraphObjectInstance &b_instance,
144                       float motion_time,
145                       bool use_particle_hair,
146                       bool show_lights,
147                       BlenderObjectCulling &culling,
148                       bool *use_portal);
149 
150   /* Volume */
151   void sync_volume(BL::Object &b_ob, Volume *volume, const vector<Shader *> &used_shaders);
152 
153   /* Mesh */
154   void sync_mesh(BL::Depsgraph b_depsgraph,
155                  BL::Object b_ob,
156                  Mesh *mesh,
157                  const vector<Shader *> &used_shaders);
158   void sync_mesh_motion(BL::Depsgraph b_depsgraph, BL::Object b_ob, Mesh *mesh, int motion_step);
159 
160   /* Hair */
161   void sync_hair(BL::Depsgraph b_depsgraph,
162                  BL::Object b_ob,
163                  Hair *hair,
164                  const vector<Shader *> &used_shaders);
165   void sync_hair_motion(BL::Depsgraph b_depsgraph, BL::Object b_ob, Hair *hair, int motion_step);
166   void sync_hair(Hair *hair, BL::Object &b_ob, bool motion, int motion_step = 0);
167   void sync_particle_hair(
168       Hair *hair, BL::Mesh &b_mesh, BL::Object &b_ob, bool motion, int motion_step = 0);
169   bool object_has_particle_hair(BL::Object b_ob);
170 
171   /* Camera */
172   void sync_camera_motion(
173       BL::RenderSettings &b_render, BL::Object &b_ob, int width, int height, float motion_time);
174 
175   /* Geometry */
176   Geometry *sync_geometry(BL::Depsgraph &b_depsgrpah,
177                           BL::Object &b_ob,
178                           BL::Object &b_ob_instance,
179                           bool object_updated,
180                           bool use_particle_hair);
181   void sync_geometry_motion(BL::Depsgraph &b_depsgraph,
182                             BL::Object &b_ob,
183                             Object *object,
184                             float motion_time,
185                             bool use_particle_hair);
186 
187   /* Light */
188   void sync_light(BL::Object &b_parent,
189                   int persistent_id[OBJECT_PERSISTENT_ID_SIZE],
190                   BL::Object &b_ob,
191                   BL::Object &b_ob_instance,
192                   int random_id,
193                   Transform &tfm,
194                   bool *use_portal);
195   void sync_background_light(BL::SpaceView3D &b_v3d, bool use_portal);
196 
197   /* Particles */
198   bool sync_dupli_particle(BL::Object &b_ob,
199                            BL::DepsgraphObjectInstance &b_instance,
200                            Object *object);
201 
202   /* Images. */
203   void sync_images();
204 
205   /* Early data free. */
206   void free_data_after_sync(BL::Depsgraph &b_depsgraph);
207 
208   /* util */
209   void find_shader(BL::ID &id, vector<Shader *> &used_shaders, Shader *default_shader);
210   bool BKE_object_is_modified(BL::Object &b_ob);
211   bool object_is_geometry(BL::Object &b_ob);
212   bool object_is_light(BL::Object &b_ob);
213 
214   /* variables */
215   BL::RenderEngine b_engine;
216   BL::BlendData b_data;
217   BL::Scene b_scene;
218 
219   id_map<void *, Shader> shader_map;
220   id_map<ObjectKey, Object> object_map;
221   id_map<GeometryKey, Geometry> geometry_map;
222   id_map<ObjectKey, Light> light_map;
223   id_map<ParticleSystemKey, ParticleSystem> particle_system_map;
224   set<Geometry *> geometry_synced;
225   set<Geometry *> geometry_motion_synced;
226   set<float> motion_times;
227   void *world_map;
228   bool world_recalc;
229   BlenderViewportParameters viewport_parameters;
230 
231   Scene *scene;
232   bool preview;
233   bool experimental;
234 
235   float dicing_rate;
236   int max_subdivisions;
237 
238   struct RenderLayerInfo {
RenderLayerInfoRenderLayerInfo239     RenderLayerInfo()
240         : material_override(PointerRNA_NULL),
241           use_background_shader(true),
242           use_background_ao(true),
243           use_surfaces(true),
244           use_hair(true),
245           use_volumes(true),
246           samples(0),
247           bound_samples(false)
248     {
249     }
250 
251     string name;
252     BL::Material material_override;
253     bool use_background_shader;
254     bool use_background_ao;
255     bool use_surfaces;
256     bool use_hair;
257     bool use_volumes;
258     int samples;
259     bool bound_samples;
260   } view_layer;
261 
262   Progress &progress;
263 };
264 
265 CCL_NAMESPACE_END
266 
267 #endif /* __BLENDER_SYNC_H__ */
268