Merge branch 'master' into blender2.8
[blender.git] / intern / cycles / blender / addon / engine.py
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 # <pep8 compliant>
18
19
20 def _is_using_buggy_driver():
21     import bgl
22     # We need to be conservative here because in multi-GPU systems display card
23     # might be quite old, but others one might be just good.
24     #
25     # So We shouldn't disable possible good dedicated cards just because display
26     # card seems weak. And instead we only blacklist configurations which are
27     # proven to cause problems.
28     if bgl.glGetString(bgl.GL_VENDOR) == "ATI Technologies Inc.":
29         import re
30         version = bgl.glGetString(bgl.GL_VERSION)
31         if version.endswith("Compatibility Profile Context"):
32             # Old HD 4xxx and 5xxx series drivers did not have driver version
33             # in the version string, but those cards do not quite work and
34             # causing crashes.
35             return True
36         regex = re.compile(".*Compatibility Profile Context ([0-9]+(\.[0-9]+)+)$")
37         if not regex.match(version):
38             # Skip cards like FireGL
39             return False
40         version = regex.sub("\\1", version).split('.')
41         return int(version[0]) == 8
42     return False
43
44
45 def _workaround_buggy_drivers():
46     if _is_using_buggy_driver():
47         import _cycles
48         if hasattr(_cycles, "opencl_disable"):
49             print("Cycles: OpenGL driver known to be buggy, disabling OpenCL platform.")
50             _cycles.opencl_disable()
51
52
53 def _configure_argument_parser():
54     import argparse
55     # No help because it conflicts with general Python scripts argument parsing
56     parser = argparse.ArgumentParser(description="Cycles Addon argument parser",
57                                      add_help=False)
58     parser.add_argument("--cycles-resumable-num-chunks",
59                         help="Number of chunks to split sample range into",
60                         default=None)
61     parser.add_argument("--cycles-resumable-current-chunk",
62                         help="Current chunk of samples range to render",
63                         default=None)
64     parser.add_argument("--cycles-resumable-start-chunk",
65                         help="Start chunk to render",
66                         default=None)
67     parser.add_argument("--cycles-resumable-end-chunk",
68                         help="End chunk to render",
69                         default=None)
70     parser.add_argument("--cycles-print-stats",
71                         help="Print rendering statistics to stderr",
72                         action='store_true')
73     return parser
74
75
76 def _parse_command_line():
77     import sys
78
79     argv = sys.argv
80     if "--" not in argv:
81         return
82
83     parser = _configure_argument_parser()
84     args, unknown = parser.parse_known_args(argv[argv.index("--") + 1:])
85
86     if args.cycles_resumable_num_chunks is not None:
87         if args.cycles_resumable_current_chunk is not None:
88             import _cycles
89             _cycles.set_resumable_chunk(
90                 int(args.cycles_resumable_num_chunks),
91                 int(args.cycles_resumable_current_chunk),
92             )
93         elif args.cycles_resumable_start_chunk is not None and \
94                 args.cycles_resumable_end_chunk:
95             import _cycles
96             _cycles.set_resumable_chunk_range(
97                 int(args.cycles_resumable_num_chunks),
98                 int(args.cycles_resumable_start_chunk),
99                 int(args.cycles_resumable_end_chunk),
100             )
101     if args.cycles_print_stats:
102         import _cycles
103         _cycles.enable_print_stats()
104
105
106 def init():
107     import bpy
108     import _cycles
109     import os.path
110
111     # Workaround possibly buggy legacy drivers which crashes on the OpenCL
112     # device enumeration.
113     #
114     # This checks are not really correct because they might still fail
115     # in the case of multiple GPUs. However, currently buggy drivers
116     # are really old and likely to be used in single GPU systems only
117     # anyway.
118     #
119     # Can't do it in the background mode, so we hope OpenCL is no enabled
120     # in the user preferences.
121     if not bpy.app.background:
122         _workaround_buggy_drivers()
123
124     path = os.path.dirname(__file__)
125     user_path = os.path.dirname(os.path.abspath(bpy.utils.user_resource('CONFIG', '')))
126
127     _cycles.init(path, user_path, bpy.app.background)
128     _parse_command_line()
129
130
131 def exit():
132     import _cycles
133     _cycles.exit()
134
135
136 def create(engine, data, region=None, v3d=None, rv3d=None, preview_osl=False):
137     import _cycles
138     import bpy
139
140     data = data.as_pointer()
141     userpref = bpy.context.user_preferences.as_pointer()
142     if region:
143         region = region.as_pointer()
144     if v3d:
145         v3d = v3d.as_pointer()
146     if rv3d:
147         rv3d = rv3d.as_pointer()
148
149     engine.session = _cycles.create(
150             engine.as_pointer(), userpref, data, region, v3d, rv3d, preview_osl)
151
152
153 def free(engine):
154     if hasattr(engine, "session"):
155         if engine.session:
156             import _cycles
157             _cycles.free(engine.session)
158         del engine.session
159
160
161 def render(engine, depsgraph):
162     import _cycles
163     if hasattr(engine, "session"):
164         _cycles.render(engine.session, depsgraph.as_pointer())
165
166
167 def bake(engine, depsgraph, obj, pass_type, pass_filter, object_id, pixel_array, num_pixels, depth, result):
168     import _cycles
169     session = getattr(engine, "session", None)
170     if session is not None:
171         _cycles.bake(engine.session, depsgraph.as_pointer(), obj.as_pointer(), pass_type, pass_filter, object_id, pixel_array.as_pointer(), num_pixels, depth, result.as_pointer())
172
173
174 def reset(engine, data, depsgraph):
175     import _cycles
176     import bpy
177
178     if bpy.app.debug_value == 256:
179         _cycles.debug_flags_update(depsgraph.scene.as_pointer())
180     else:
181         _cycles.debug_flags_reset()
182
183     data = data.as_pointer()
184     depsgraph = depsgraph.as_pointer()
185     _cycles.reset(engine.session, data, depsgraph)
186
187
188 def sync(engine, depsgraph, data):
189     import _cycles
190     _cycles.sync(engine.session, depsgraph.as_pointer())
191
192
193 def draw(engine, depsgraph, region, v3d, rv3d):
194     import _cycles
195     depsgraph = depsgraph.as_pointer()
196     v3d = v3d.as_pointer()
197     rv3d = rv3d.as_pointer()
198
199     # draw render image
200     _cycles.draw(engine.session, depsgraph, v3d, rv3d)
201
202
203 def available_devices():
204     import _cycles
205     return _cycles.available_devices()
206
207
208 def with_osl():
209     import _cycles
210     return _cycles.with_osl
211
212
213 def with_network():
214     import _cycles
215     return _cycles.with_network
216
217
218 def system_info():
219     import _cycles
220     return _cycles.system_info()
221
222
223 def register_passes(engine, scene, srl):
224     engine.register_pass(scene, srl, "Combined", 4, "RGBA", 'COLOR')
225
226     if srl.use_pass_z:                     engine.register_pass(scene, srl, "Depth",         1, "Z",    'VALUE')
227     if srl.use_pass_mist:                  engine.register_pass(scene, srl, "Mist",          1, "Z",    'VALUE')
228     if srl.use_pass_normal:                engine.register_pass(scene, srl, "Normal",        3, "XYZ",  'VECTOR')
229     if srl.use_pass_vector:                engine.register_pass(scene, srl, "Vector",        4, "XYZW", 'VECTOR')
230     if srl.use_pass_uv:                    engine.register_pass(scene, srl, "UV",            3, "UVA",  'VECTOR')
231     if srl.use_pass_object_index:          engine.register_pass(scene, srl, "IndexOB",       1, "X",    'VALUE')
232     if srl.use_pass_material_index:        engine.register_pass(scene, srl, "IndexMA",       1, "X",    'VALUE')
233     if srl.use_pass_shadow:                engine.register_pass(scene, srl, "Shadow",        3, "RGB",  'COLOR')
234     if srl.use_pass_ambient_occlusion:     engine.register_pass(scene, srl, "AO",            3, "RGB",  'COLOR')
235     if srl.use_pass_diffuse_direct:        engine.register_pass(scene, srl, "DiffDir",       3, "RGB",  'COLOR')
236     if srl.use_pass_diffuse_indirect:      engine.register_pass(scene, srl, "DiffInd",       3, "RGB",  'COLOR')
237     if srl.use_pass_diffuse_color:         engine.register_pass(scene, srl, "DiffCol",       3, "RGB",  'COLOR')
238     if srl.use_pass_glossy_direct:         engine.register_pass(scene, srl, "GlossDir",      3, "RGB",  'COLOR')
239     if srl.use_pass_glossy_indirect:       engine.register_pass(scene, srl, "GlossInd",      3, "RGB",  'COLOR')
240     if srl.use_pass_glossy_color:          engine.register_pass(scene, srl, "GlossCol",      3, "RGB",  'COLOR')
241     if srl.use_pass_transmission_direct:   engine.register_pass(scene, srl, "TransDir",      3, "RGB",  'COLOR')
242     if srl.use_pass_transmission_indirect: engine.register_pass(scene, srl, "TransInd",      3, "RGB",  'COLOR')
243     if srl.use_pass_transmission_color:    engine.register_pass(scene, srl, "TransCol",      3, "RGB",  'COLOR')
244     if srl.use_pass_subsurface_direct:     engine.register_pass(scene, srl, "SubsurfaceDir", 3, "RGB",  'COLOR')
245     if srl.use_pass_subsurface_indirect:   engine.register_pass(scene, srl, "SubsurfaceInd", 3, "RGB",  'COLOR')
246     if srl.use_pass_subsurface_color:      engine.register_pass(scene, srl, "SubsurfaceCol", 3, "RGB",  'COLOR')
247     if srl.use_pass_emit:                  engine.register_pass(scene, srl, "Emit",          3, "RGB",  'COLOR')
248     if srl.use_pass_environment:           engine.register_pass(scene, srl, "Env",           3, "RGB",  'COLOR')
249
250     crl = srl.cycles
251     if crl.pass_debug_render_time:             engine.register_pass(scene, srl, "Debug Render Time",             1, "X",   'VALUE')
252     if crl.pass_debug_bvh_traversed_nodes:     engine.register_pass(scene, srl, "Debug BVH Traversed Nodes",     1, "X",   'VALUE')
253     if crl.pass_debug_bvh_traversed_instances: engine.register_pass(scene, srl, "Debug BVH Traversed Instances", 1, "X",   'VALUE')
254     if crl.pass_debug_bvh_intersections:       engine.register_pass(scene, srl, "Debug BVH Intersections",       1, "X",   'VALUE')
255     if crl.pass_debug_ray_bounces:             engine.register_pass(scene, srl, "Debug Ray Bounces",             1, "X",   'VALUE')
256     if crl.use_pass_volume_direct:             engine.register_pass(scene, srl, "VolumeDir",                     3, "RGB", 'COLOR')
257     if crl.use_pass_volume_indirect:           engine.register_pass(scene, srl, "VolumeInd",                     3, "RGB", 'COLOR')
258
259     cscene = scene.cycles
260     if crl.use_denoising and crl.denoising_store_passes and not cscene.use_progressive_refine:
261         engine.register_pass(scene, srl, "Denoising Normal",          3, "XYZ", 'VECTOR')
262         engine.register_pass(scene, srl, "Denoising Normal Variance", 3, "XYZ", 'VECTOR')
263         engine.register_pass(scene, srl, "Denoising Albedo",          3, "RGB", 'COLOR')
264         engine.register_pass(scene, srl, "Denoising Albedo Variance", 3, "RGB", 'COLOR')
265         engine.register_pass(scene, srl, "Denoising Depth",           1, "Z",   'VALUE')
266         engine.register_pass(scene, srl, "Denoising Depth Variance",  1, "Z",   'VALUE')
267         engine.register_pass(scene, srl, "Denoising Shadow A",        3, "XYV", 'VECTOR')
268         engine.register_pass(scene, srl, "Denoising Shadow B",        3, "XYV", 'VECTOR')
269         engine.register_pass(scene, srl, "Denoising Image",           3, "RGB", 'COLOR')
270         engine.register_pass(scene, srl, "Denoising Image Variance",  3, "RGB", 'COLOR')
271
272         clean_options = ("denoising_diffuse_direct", "denoising_diffuse_indirect",
273                          "denoising_glossy_direct", "denoising_glossy_indirect",
274                          "denoising_transmission_direct", "denoising_transmission_indirect",
275                          "denoising_subsurface_direct", "denoising_subsurface_indirect")
276         if any(getattr(crl, option) for option in clean_options):
277             engine.register_pass(scene, srl, "Denoising Clean", 3, "RGB", 'COLOR')