Improvements and fixes to Cycles metadata
[blender.git] / intern / cycles / blender / blender_session.h
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_SESSION_H__
18 #define __BLENDER_SESSION_H__
19
20 #include "device/device.h"
21 #include "render/scene.h"
22 #include "render/session.h"
23 #include "render/bake.h"
24
25 #include "util/util_vector.h"
26
27 CCL_NAMESPACE_BEGIN
28
29 class ImageMetaData;
30 class Scene;
31 class Session;
32 class RenderBuffers;
33 class RenderTile;
34
35 class BlenderSession {
36 public:
37         BlenderSession(BL::RenderEngine& b_engine,
38                        BL::Preferences& b_userpref,
39                        BL::BlendData& b_data,
40                        bool preview_osl);
41
42         BlenderSession(BL::RenderEngine& b_engine,
43                        BL::Preferences& b_userpref,
44                        BL::BlendData& b_data,
45                        BL::SpaceView3D& b_v3d,
46                        BL::RegionView3D& b_rv3d,
47                        int width, int height);
48
49         ~BlenderSession();
50
51         void create();
52
53         /* session */
54         void create_session();
55         void free_session();
56
57         void reset_session(BL::BlendData& b_data,
58                            BL::Depsgraph& b_depsgraph);
59
60         /* offline render */
61         void render(BL::Depsgraph& b_depsgraph);
62
63         void bake(BL::Depsgraph& b_depsgrah,
64                   BL::Object& b_object,
65                   const string& pass_type,
66                   const int custom_flag,
67                   const int object_id,
68                   BL::BakePixel& pixel_array,
69                   const size_t num_pixels,
70                   const int depth,
71                   float pixels[]);
72
73         void write_render_result(BL::RenderResult& b_rr,
74                                  BL::RenderLayer& b_rlay,
75                                  RenderTile& rtile);
76         void write_render_tile(RenderTile& rtile);
77
78         /* update functions are used to update display buffer only after sample was rendered
79          * only needed for better visual feedback */
80         void update_render_result(BL::RenderResult& b_rr,
81                                   BL::RenderLayer& b_rlay,
82                                   RenderTile& rtile);
83         void update_render_tile(RenderTile& rtile, bool highlight);
84
85         /* interactive updates */
86         void synchronize(BL::Depsgraph& b_depsgraph);
87
88         /* drawing */
89         bool draw(int w, int h);
90         void tag_redraw();
91         void tag_update();
92         void get_status(string& status, string& substatus);
93         void get_progress(float& progress, double& total_time, double& render_time);
94         void test_cancel();
95         void update_status_progress();
96         void update_bake_progress();
97
98         bool background;
99         Session *session;
100         Scene *scene;
101         BlenderSync *sync;
102         double last_redraw_time;
103
104         BL::RenderEngine b_engine;
105         BL::Preferences b_userpref;
106         BL::BlendData b_data;
107         BL::RenderSettings b_render;
108         BL::Depsgraph b_depsgraph;
109         /* NOTE: Blender's scene might become invalid after call
110          * free_blender_memory_if_possible().
111          */
112         BL::Scene b_scene;
113         BL::SpaceView3D b_v3d;
114         BL::RegionView3D b_rv3d;
115         string b_rlay_name;
116         string b_rview_name;
117
118         string last_status;
119         string last_error;
120         float last_progress;
121         double last_status_time;
122
123         int width, height;
124         bool preview_osl;
125         double start_resize_time;
126
127         void *python_thread_state;
128
129         /* Global state which is common for all render sessions created from Blender.
130          * Usually denotes command line arguments.
131          */
132
133         /* Blender is running from the command line, no windows are shown and some
134          * extra render optimization is possible (possible to free draw-only data and
135          * so on.
136          */
137         static bool headless;
138
139         /* ** Resumable render ** */
140
141         /* Overall number of chunks in which the sample range is to be devided. */
142         static int num_resumable_chunks;
143
144         /* Current resumable chunk index to render. */
145         static int current_resumable_chunk;
146
147         /* Alternative to single-chunk rendering to render a range of chunks. */
148         static int start_resumable_chunk;
149         static int end_resumable_chunk;
150
151         static bool print_render_stats;
152
153 protected:
154         void stamp_view_layer_metadata(const string& view_layer_name);
155         void stamp_view_layer_metadata_do(const string& prefix);
156
157         void do_write_update_render_result(BL::RenderResult& b_rr,
158                                            BL::RenderLayer& b_rlay,
159                                            RenderTile& rtile,
160                                            bool do_update_only);
161         void do_write_update_render_tile(RenderTile& rtile, bool do_update_only, bool highlight);
162
163         int builtin_image_frame(const string &builtin_name);
164         void builtin_image_info(const string &builtin_name,
165                                 void *builtin_data,
166                                 ImageMetaData& metadata);
167         bool builtin_image_pixels(const string &builtin_name,
168                                   void *builtin_data,
169                                   unsigned char *pixels,
170                                   const size_t pixels_size,
171                                   const bool free_cache);
172         bool builtin_image_float_pixels(const string &builtin_name,
173                                         void *builtin_data,
174                                         float *pixels,
175                                         const size_t pixels_size,
176                                         const bool free_cache);
177         void builtin_images_load();
178
179         /* Update tile manager to reflect resumable render settings. */
180         void update_resumable_tile_manager(int num_samples);
181
182         /* Is used after each render layer synchronization is done with the goal
183          * of freeing render engine data which is held from Blender side (for
184          * example, dependency graph).
185          */
186         void free_blender_memory_if_possible();
187 };
188
189 CCL_NAMESPACE_END
190
191 #endif  /* __BLENDER_SESSION_H__ */