Merge branch 'master' into blender2.8
[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::UserPreferences& b_userpref,
39                        BL::BlendData& b_data,
40                        bool preview_osl);
41
42         BlenderSession(BL::RenderEngine& b_engine,
43                        BL::UserPreferences& 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::UserPreferences b_userpref;
106         BL::BlendData b_data;
107         BL::RenderSettings b_render;
108         BL::Depsgraph b_depsgraph;
109         BL::Scene b_scene;
110         BL::SpaceView3D b_v3d;
111         BL::RegionView3D b_rv3d;
112         string b_rlay_name;
113         string b_rview_name;
114
115         string last_status;
116         string last_error;
117         float last_progress;
118         double last_status_time;
119
120         int width, height;
121         bool preview_osl;
122         double start_resize_time;
123
124         void *python_thread_state;
125
126         /* Global state which is common for all render sessions created from Blender.
127          * Usually denotes command line arguments.
128          */
129
130         /* Blender is running from the command line, no windows are shown and some
131          * extra render optimization is possible (possible to free draw-only data and
132          * so on.
133          */
134         static bool headless;
135
136         /* ** Resumable render ** */
137
138         /* Overall number of chunks in which the sample range is to be devided. */
139         static int num_resumable_chunks;
140
141         /* Current resumable chunk index to render. */
142         static int current_resumable_chunk;
143
144         /* Alternative to single-chunk rendering to render a range of chunks. */
145         static int start_resumable_chunk;
146         static int end_resumable_chunk;
147
148         static bool print_render_stats;
149
150 protected:
151         void do_write_update_render_result(BL::RenderResult& b_rr,
152                                            BL::RenderLayer& b_rlay,
153                                            RenderTile& rtile,
154                                            bool do_update_only);
155         void do_write_update_render_tile(RenderTile& rtile, bool do_update_only, bool highlight);
156
157         int builtin_image_frame(const string &builtin_name);
158         void builtin_image_info(const string &builtin_name,
159                                 void *builtin_data,
160                                 ImageMetaData& metadata);
161         bool builtin_image_pixels(const string &builtin_name,
162                                   void *builtin_data,
163                                   unsigned char *pixels,
164                                   const size_t pixels_size,
165                                   const bool free_cache);
166         bool builtin_image_float_pixels(const string &builtin_name,
167                                         void *builtin_data,
168                                         float *pixels,
169                                         const size_t pixels_size,
170                                         const bool free_cache);
171         void builtin_images_load();
172
173         /* Update tile manager to reflect resumable render settings. */
174         void update_resumable_tile_manager(int num_samples);
175 };
176
177 CCL_NAMESPACE_END
178
179 #endif  /* __BLENDER_SESSION_H__ */