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