Cleanup: pep8, cycles
[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     parser = argparse.ArgumentParser(description="Cycles Addon argument parser")
56     parser.add_argument("--cycles-resumable-num-chunks",
57                         help="Number of chunks to split sample range into",
58                         default=None)
59     parser.add_argument("--cycles-resumable-current-chunk",
60                         help="Current chunk of samples range to render",
61                         default=None)
62     parser.add_argument("--cycles-resumable-start-chunk",
63                         help="Start chunk to render",
64                         default=None)
65     parser.add_argument("--cycles-resumable-end-chunk",
66                         help="End chunk to render",
67                         default=None)
68     return parser
69
70
71 def _parse_command_line():
72     import sys
73
74     argv = sys.argv
75     if "--" not in argv:
76         return
77
78     parser = _configure_argument_parser()
79     args, unknown = parser.parse_known_args(argv[argv.index("--") + 1:])
80
81     if args.cycles_resumable_num_chunks is not None:
82         if args.cycles_resumable_current_chunk is not None:
83             import _cycles
84             _cycles.set_resumable_chunk(
85                 int(args.cycles_resumable_num_chunks),
86                 int(args.cycles_resumable_current_chunk),
87             )
88         elif args.cycles_resumable_start_chunk is not None and \
89                 args.cycles_resumable_end_chunk:
90             import _cycles
91             _cycles.set_resumable_chunk_range(
92                 int(args.cycles_resumable_num_chunks),
93                 int(args.cycles_resumable_start_chunk),
94                 int(args.cycles_resumable_end_chunk),
95             )
96
97
98 def init():
99     import bpy
100     import _cycles
101     import os.path
102
103     # Workaround possibly buggy legacy drivers which crashes on the OpenCL
104     # device enumeration.
105     #
106     # This checks are not really correct because they might still fail
107     # in the case of multiple GPUs. However, currently buggy drivers
108     # are really old and likely to be used in single GPU systems only
109     # anyway.
110     #
111     # Can't do it in the background mode, so we hope OpenCL is no enabled
112     # in the user preferences.
113     if not bpy.app.background:
114         _workaround_buggy_drivers()
115
116     path = os.path.dirname(__file__)
117     user_path = os.path.dirname(os.path.abspath(bpy.utils.user_resource('CONFIG', '')))
118
119     _cycles.init(path, user_path, bpy.app.background)
120     _parse_command_line()
121
122
123 def exit():
124     import _cycles
125     _cycles.exit()
126
127
128 def create(engine, data, scene, region=None, v3d=None, rv3d=None, preview_osl=False):
129     import bpy
130     import _cycles
131
132     data = data.as_pointer()
133     userpref = bpy.context.user_preferences.as_pointer()
134     scene = scene.as_pointer()
135     if region:
136         region = region.as_pointer()
137     if v3d:
138         v3d = v3d.as_pointer()
139     if rv3d:
140         rv3d = rv3d.as_pointer()
141
142     if bpy.app.debug_value == 256:
143         _cycles.debug_flags_update(scene)
144     else:
145         _cycles.debug_flags_reset()
146
147     engine.session = _cycles.create(engine.as_pointer(), userpref, data, scene, region, v3d, rv3d, preview_osl)
148
149
150 def free(engine):
151     if hasattr(engine, "session"):
152         if engine.session:
153             import _cycles
154             _cycles.free(engine.session)
155         del engine.session
156
157
158 def render(engine):
159     import _cycles
160     if hasattr(engine, "session"):
161         _cycles.render(engine.session)
162
163
164 def bake(engine, obj, pass_type, pass_filter, object_id, pixel_array, num_pixels, depth, result):
165     import _cycles
166     session = getattr(engine, "session", None)
167     if session is not None:
168         _cycles.bake(engine.session, obj.as_pointer(), pass_type, pass_filter, object_id, pixel_array.as_pointer(), num_pixels, depth, result.as_pointer())
169
170
171 def reset(engine, data, scene):
172     import _cycles
173     data = data.as_pointer()
174     scene = scene.as_pointer()
175     _cycles.reset(engine.session, data, scene)
176
177
178 def update(engine, data, scene):
179     import _cycles
180     _cycles.sync(engine.session)
181
182
183 def draw(engine, region, v3d, rv3d):
184     import _cycles
185     v3d = v3d.as_pointer()
186     rv3d = rv3d.as_pointer()
187
188     # draw render image
189     _cycles.draw(engine.session, v3d, rv3d)
190
191
192 def available_devices():
193     import _cycles
194     return _cycles.available_devices()
195
196
197 def with_osl():
198     import _cycles
199     return _cycles.with_osl
200
201
202 def with_network():
203     import _cycles
204     return _cycles.with_network
205
206
207 def system_info():
208     import _cycles
209     return _cycles.system_info()
210
211
212 def register_passes(engine, scene, srl):
213     engine.register_pass(scene, srl, "Combined", 4, "RGBA", 'COLOR')
214
215     if srl.use_pass_z:                     engine.register_pass(scene, srl, "Depth",         1, "Z",    'VALUE')
216     if srl.use_pass_mist:                  engine.register_pass(scene, srl, "Mist",          1, "Z",    'VALUE')
217     if srl.use_pass_normal:                engine.register_pass(scene, srl, "Normal",        3, "XYZ",  'VECTOR')
218     if srl.use_pass_vector:                engine.register_pass(scene, srl, "Vector",        4, "XYZW", 'VECTOR')
219     if srl.use_pass_uv:                    engine.register_pass(scene, srl, "UV",            3, "UVA",  'VECTOR')
220     if srl.use_pass_object_index:          engine.register_pass(scene, srl, "IndexOB",       1, "X",    'VALUE')
221     if srl.use_pass_material_index:        engine.register_pass(scene, srl, "IndexMA",       1, "X",    'VALUE')
222     if srl.use_pass_shadow:                engine.register_pass(scene, srl, "Shadow",        3, "RGB",  'COLOR')
223     if srl.use_pass_ambient_occlusion:     engine.register_pass(scene, srl, "AO",            3, "RGB",  'COLOR')
224     if srl.use_pass_diffuse_direct:        engine.register_pass(scene, srl, "DiffDir",       3, "RGB",  'COLOR')
225     if srl.use_pass_diffuse_indirect:      engine.register_pass(scene, srl, "DiffInd",       3, "RGB",  'COLOR')
226     if srl.use_pass_diffuse_color:         engine.register_pass(scene, srl, "DiffCol",       3, "RGB",  'COLOR')
227     if srl.use_pass_glossy_direct:         engine.register_pass(scene, srl, "GlossDir",      3, "RGB",  'COLOR')
228     if srl.use_pass_glossy_indirect:       engine.register_pass(scene, srl, "GlossInd",      3, "RGB",  'COLOR')
229     if srl.use_pass_glossy_color:          engine.register_pass(scene, srl, "GlossCol",      3, "RGB",  'COLOR')
230     if srl.use_pass_transmission_direct:   engine.register_pass(scene, srl, "TransDir",      3, "RGB",  'COLOR')
231     if srl.use_pass_transmission_indirect: engine.register_pass(scene, srl, "TransInd",      3, "RGB",  'COLOR')
232     if srl.use_pass_transmission_color:    engine.register_pass(scene, srl, "TransCol",      3, "RGB",  'COLOR')
233     if srl.use_pass_subsurface_direct:     engine.register_pass(scene, srl, "SubsurfaceDir", 3, "RGB",  'COLOR')
234     if srl.use_pass_subsurface_indirect:   engine.register_pass(scene, srl, "SubsurfaceInd", 3, "RGB",  'COLOR')
235     if srl.use_pass_subsurface_color:      engine.register_pass(scene, srl, "SubsurfaceCol", 3, "RGB",  'COLOR')
236     if srl.use_pass_emit:                  engine.register_pass(scene, srl, "Emit",          3, "RGB",  'COLOR')
237     if srl.use_pass_environment:           engine.register_pass(scene, srl, "Env",           3, "RGB",  'COLOR')
238
239     crl = srl.cycles
240     if crl.pass_debug_render_time:             engine.register_pass(scene, srl, "Debug Render Time",             1, "X",   'VALUE')
241     if crl.pass_debug_bvh_traversed_nodes:     engine.register_pass(scene, srl, "Debug BVH Traversed Nodes",     1, "X",   'VALUE')
242     if crl.pass_debug_bvh_traversed_instances: engine.register_pass(scene, srl, "Debug BVH Traversed Instances", 1, "X",   'VALUE')
243     if crl.pass_debug_bvh_intersections:       engine.register_pass(scene, srl, "Debug BVH Intersections",       1, "X",   'VALUE')
244     if crl.pass_debug_ray_bounces:             engine.register_pass(scene, srl, "Debug Ray Bounces",             1, "X",   'VALUE')
245     if crl.use_pass_volume_direct:             engine.register_pass(scene, srl, "VolumeDir",                     3, "RGB", 'COLOR')
246     if crl.use_pass_volume_indirect:           engine.register_pass(scene, srl, "VolumeInd",                     3, "RGB", 'COLOR')
247
248     cscene = scene.cycles
249     if crl.use_denoising and crl.denoising_store_passes and not cscene.use_progressive_refine:
250         engine.register_pass(scene, srl, "Denoising Normal",          3, "XYZ", 'VECTOR')
251         engine.register_pass(scene, srl, "Denoising Normal Variance", 3, "XYZ", 'VECTOR')
252         engine.register_pass(scene, srl, "Denoising Albedo",          3, "RGB", 'COLOR')
253         engine.register_pass(scene, srl, "Denoising Albedo Variance", 3, "RGB", 'COLOR')
254         engine.register_pass(scene, srl, "Denoising Depth",           1, "Z",   'VALUE')
255         engine.register_pass(scene, srl, "Denoising Depth Variance",  1, "Z",   'VALUE')
256         engine.register_pass(scene, srl, "Denoising Shadow A",        3, "XYV", 'VECTOR')
257         engine.register_pass(scene, srl, "Denoising Shadow B",        3, "XYV", 'VECTOR')
258         engine.register_pass(scene, srl, "Denoising Image",           3, "RGB", 'COLOR')
259         engine.register_pass(scene, srl, "Denoising Image Variance",  3, "RGB", 'COLOR')