Merge branch 'blender2.7'
[blender.git] / intern / cycles / render / buffers.cpp
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 #include <stdlib.h>
18
19 #include "render/buffers.h"
20 #include "device/device.h"
21
22 #include "util/util_foreach.h"
23 #include "util/util_hash.h"
24 #include "util/util_math.h"
25 #include "util/util_opengl.h"
26 #include "util/util_time.h"
27 #include "util/util_types.h"
28
29 CCL_NAMESPACE_BEGIN
30
31 /* Buffer Params */
32
33 BufferParams::BufferParams()
34 {
35         width = 0;
36         height = 0;
37
38         full_x = 0;
39         full_y = 0;
40         full_width = 0;
41         full_height = 0;
42
43         denoising_data_pass = false;
44         denoising_clean_pass = false;
45         denoising_prefiltered_pass = false;
46
47         Pass::add(PASS_COMBINED, passes);
48 }
49
50 void BufferParams::get_offset_stride(int& offset, int& stride)
51 {
52         offset = -(full_x + full_y*width);
53         stride = width;
54 }
55
56 bool BufferParams::modified(const BufferParams& params)
57 {
58         return !(full_x == params.full_x
59                 && full_y == params.full_y
60                 && width == params.width
61                 && height == params.height
62                 && full_width == params.full_width
63                 && full_height == params.full_height
64                 && Pass::equals(passes, params.passes));
65 }
66
67 int BufferParams::get_passes_size()
68 {
69         int size = 0;
70
71         for(size_t i = 0; i < passes.size(); i++)
72                 size += passes[i].components;
73
74         if(denoising_data_pass) {
75                 size += DENOISING_PASS_SIZE_BASE;
76                 if(denoising_clean_pass) size += DENOISING_PASS_SIZE_CLEAN;
77                 if(denoising_prefiltered_pass) size += DENOISING_PASS_SIZE_PREFILTERED;
78         }
79
80         return align_up(size, 4);
81 }
82
83 int BufferParams::get_denoising_offset()
84 {
85         int offset = 0;
86
87         for(size_t i = 0; i < passes.size(); i++)
88                 offset += passes[i].components;
89
90         return offset;
91 }
92
93 int BufferParams::get_denoising_prefiltered_offset()
94 {
95         assert(denoising_prefiltered_pass);
96
97         int offset = get_denoising_offset();
98
99         offset += DENOISING_PASS_SIZE_BASE;
100         if(denoising_clean_pass) {
101                 offset += DENOISING_PASS_SIZE_CLEAN;
102         }
103
104         return offset;
105 }
106
107 /* Render Buffer Task */
108
109 RenderTile::RenderTile()
110 {
111         x = 0;
112         y = 0;
113         w = 0;
114         h = 0;
115
116         sample = 0;
117         start_sample = 0;
118         num_samples = 0;
119         resolution = 0;
120
121         offset = 0;
122         stride = 0;
123
124         buffer = 0;
125
126         buffers = NULL;
127 }
128
129 /* Render Buffers */
130
131 RenderBuffers::RenderBuffers(Device *device)
132 : buffer(device, "RenderBuffers", MEM_READ_WRITE),
133   map_neighbor_copied(false), render_time(0.0f)
134 {
135 }
136
137 RenderBuffers::~RenderBuffers()
138 {
139         buffer.free();
140 }
141
142 void RenderBuffers::reset(BufferParams& params_)
143 {
144         params = params_;
145
146         /* re-allocate buffer */
147         buffer.alloc(params.width*params.height*params.get_passes_size());
148         buffer.zero_to_device();
149 }
150
151 void RenderBuffers::zero()
152 {
153         buffer.zero_to_device();
154 }
155
156 bool RenderBuffers::copy_from_device()
157 {
158         if(!buffer.device_pointer)
159                 return false;
160
161         buffer.copy_from_device(0, params.width * params.get_passes_size(), params.height);
162
163         return true;
164 }
165
166 bool RenderBuffers::get_denoising_pass_rect(int type, float exposure, int sample, int components, float *pixels)
167 {
168         if(buffer.data() == NULL) {
169                 return false;
170         }
171
172         float scale = 1.0f;
173         float alpha_scale = 1.0f/sample;
174         if(type == DENOISING_PASS_PREFILTERED_COLOR ||
175            type == DENOISING_PASS_CLEAN ||
176            type == DENOISING_PASS_PREFILTERED_INTENSITY) {
177                 scale *= exposure;
178         }
179         else if(type == DENOISING_PASS_PREFILTERED_VARIANCE) {
180                 scale *= exposure*exposure * (sample - 1);
181         }
182
183         int offset;
184         if(type == DENOISING_PASS_CLEAN) {
185                 /* The clean pass isn't changed by prefiltering, so we use the original one there. */
186                 offset = type + params.get_denoising_offset();
187         }
188         else if (type == DENOISING_PASS_PREFILTERED_COLOR && !params.denoising_prefiltered_pass) {
189                 /* If we're not saving the prefiltering result, return the original noisy pass. */
190                 offset = params.get_denoising_offset() + DENOISING_PASS_COLOR;
191                 scale /= sample;
192         }
193         else {
194                 offset = type + params.get_denoising_prefiltered_offset();
195         }
196
197         int pass_stride = params.get_passes_size();
198         int size = params.width*params.height;
199
200         float *in = buffer.data() + offset;
201
202         if(components == 1) {
203                 for(int i = 0; i < size; i++, in += pass_stride, pixels++) {
204                         pixels[0] = in[0]*scale;
205                 }
206         }
207         else if(components == 3) {
208                 for(int i = 0; i < size; i++, in += pass_stride, pixels += 3) {
209                         pixels[0] = in[0]*scale;
210                         pixels[1] = in[1]*scale;
211                         pixels[2] = in[2]*scale;
212                 }
213         }
214         else if(components == 4) {
215                 /* Since the alpha channel is not involved in denoising, output the Combined alpha channel. */
216                 assert(params.passes[0].type == PASS_COMBINED);
217                 float *in_combined = buffer.data();
218
219                 for(int i = 0; i < size; i++, in += pass_stride, in_combined += pass_stride, pixels += 4) {
220                         pixels[0] = in[0]*scale;
221                         pixels[1] = in[1]*scale;
222                         pixels[2] = in[2]*scale;
223                         pixels[3] = saturate(in_combined[3]*alpha_scale);
224                 }
225         }
226         else {
227                 return false;
228         }
229
230         return true;
231 }
232
233 bool RenderBuffers::get_pass_rect(PassType type, float exposure, int sample, int components, float *pixels, const string &name)
234 {
235         if(buffer.data() == NULL) {
236                 return false;
237         }
238
239         int pass_offset = 0;
240
241         for(size_t j = 0; j < params.passes.size(); j++) {
242                 Pass& pass = params.passes[j];
243
244                 if(pass.type != type) {
245                         pass_offset += pass.components;
246                         continue;
247                 }
248
249                 /* Tell Cryptomatte passes apart by their name. */
250                 if(pass.type == PASS_CRYPTOMATTE) {
251                         if(pass.name != name) {
252                                 pass_offset += pass.components;
253                                 continue;
254                         }
255                 }
256
257                 float *in = buffer.data() + pass_offset;
258                 int pass_stride = params.get_passes_size();
259
260                 float scale = (pass.filter)? 1.0f/(float)sample: 1.0f;
261                 float scale_exposure = (pass.exposure)? scale*exposure: scale;
262
263                 int size = params.width*params.height;
264
265                 if(components == 1 && type == PASS_RENDER_TIME) {
266                         /* Render time is not stored by kernel, but measured per tile. */
267                         float val = (float) (1000.0 * render_time/(params.width * params.height * sample));
268                         for(int i = 0; i < size; i++, pixels++) {
269                                 pixels[0] = val;
270                         }
271                 }
272                 else if(components == 1) {
273                         assert(pass.components == components);
274
275                         /* Scalar */
276                         if(type == PASS_DEPTH) {
277                                 for(int i = 0; i < size; i++, in += pass_stride, pixels++) {
278                                         float f = *in;
279                                         pixels[0] = (f == 0.0f)? 1e10f: f*scale_exposure;
280                                 }
281                         }
282                         else if(type == PASS_MIST) {
283                                 for(int i = 0; i < size; i++, in += pass_stride, pixels++) {
284                                         float f = *in;
285                                         pixels[0] = saturate(f*scale_exposure);
286                                 }
287                         }
288 #ifdef WITH_CYCLES_DEBUG
289                         else if(type == PASS_BVH_TRAVERSED_NODES ||
290                                 type == PASS_BVH_TRAVERSED_INSTANCES ||
291                                 type == PASS_BVH_INTERSECTIONS ||
292                                 type == PASS_RAY_BOUNCES)
293                         {
294                                 for(int i = 0; i < size; i++, in += pass_stride, pixels++) {
295                                         float f = *in;
296                                         pixels[0] = f*scale;
297                                 }
298                         }
299 #endif
300                         else {
301                                 for(int i = 0; i < size; i++, in += pass_stride, pixels++) {
302                                         float f = *in;
303                                         pixels[0] = f*scale_exposure;
304                                 }
305                         }
306                 }
307                 else if(components == 3) {
308                         assert(pass.components == 4);
309
310                         /* RGBA */
311                         if(type == PASS_SHADOW) {
312                                 for(int i = 0; i < size; i++, in += pass_stride, pixels += 3) {
313                                         float4 f = make_float4(in[0], in[1], in[2], in[3]);
314                                         float invw = (f.w > 0.0f)? 1.0f/f.w: 1.0f;
315
316                                         pixels[0] = f.x*invw;
317                                         pixels[1] = f.y*invw;
318                                         pixels[2] = f.z*invw;
319                                 }
320                         }
321                         else if(pass.divide_type != PASS_NONE) {
322                                 /* RGB lighting passes that need to divide out color */
323                                 pass_offset = 0;
324                                 for(size_t k = 0; k < params.passes.size(); k++) {
325                                         Pass& color_pass = params.passes[k];
326                                         if(color_pass.type == pass.divide_type)
327                                                 break;
328                                         pass_offset += color_pass.components;
329                                 }
330
331                                 float *in_divide = buffer.data() + pass_offset;
332
333                                 for(int i = 0; i < size; i++, in += pass_stride, in_divide += pass_stride, pixels += 3) {
334                                         float3 f = make_float3(in[0], in[1], in[2]);
335                                         float3 f_divide = make_float3(in_divide[0], in_divide[1], in_divide[2]);
336
337                                         f = safe_divide_even_color(f*exposure, f_divide);
338
339                                         pixels[0] = f.x;
340                                         pixels[1] = f.y;
341                                         pixels[2] = f.z;
342                                 }
343                         }
344                         else {
345                                 /* RGB/vector */
346                                 for(int i = 0; i < size; i++, in += pass_stride, pixels += 3) {
347                                         float3 f = make_float3(in[0], in[1], in[2]);
348
349                                         pixels[0] = f.x*scale_exposure;
350                                         pixels[1] = f.y*scale_exposure;
351                                         pixels[2] = f.z*scale_exposure;
352                                 }
353                         }
354                 }
355                 else if(components == 4) {
356                         assert(pass.components == components);
357
358                         /* RGBA */
359                         if(type == PASS_SHADOW) {
360                                 for(int i = 0; i < size; i++, in += pass_stride, pixels += 4) {
361                                         float4 f = make_float4(in[0], in[1], in[2], in[3]);
362                                         float invw = (f.w > 0.0f)? 1.0f/f.w: 1.0f;
363
364                                         pixels[0] = f.x*invw;
365                                         pixels[1] = f.y*invw;
366                                         pixels[2] = f.z*invw;
367                                         pixels[3] = 1.0f;
368                                 }
369                         }
370                         else if(type == PASS_MOTION) {
371                                 /* need to normalize by number of samples accumulated for motion */
372                                 pass_offset = 0;
373                                 for(size_t k = 0; k < params.passes.size(); k++) {
374                                         Pass& color_pass = params.passes[k];
375                                         if(color_pass.type == PASS_MOTION_WEIGHT)
376                                                 break;
377                                         pass_offset += color_pass.components;
378                                 }
379
380                                 float *in_weight = buffer.data() + pass_offset;
381
382                                 for(int i = 0; i < size; i++, in += pass_stride, in_weight += pass_stride, pixels += 4) {
383                                         float4 f = make_float4(in[0], in[1], in[2], in[3]);
384                                         float w = in_weight[0];
385                                         float invw = (w > 0.0f)? 1.0f/w: 0.0f;
386
387                                         pixels[0] = f.x*invw;
388                                         pixels[1] = f.y*invw;
389                                         pixels[2] = f.z*invw;
390                                         pixels[3] = f.w*invw;
391                                 }
392                         }
393                         else if(type == PASS_CRYPTOMATTE) {
394                                 for(int i = 0; i < size; i++, in += pass_stride, pixels += 4) {
395                                         float4 f = make_float4(in[0], in[1], in[2], in[3]);
396                                         /* x and z contain integer IDs, don't rescale them.
397                                            y and w contain matte weights, they get scaled. */
398                                         pixels[0] = f.x;
399                                         pixels[1] = f.y * scale;
400                                         pixels[2] = f.z;
401                                         pixels[3] = f.w * scale;
402                                 }
403                         }
404                         else {
405                                 for(int i = 0; i < size; i++, in += pass_stride, pixels += 4) {
406                                         float4 f = make_float4(in[0], in[1], in[2], in[3]);
407
408                                         pixels[0] = f.x*scale_exposure;
409                                         pixels[1] = f.y*scale_exposure;
410                                         pixels[2] = f.z*scale_exposure;
411
412                                         /* clamp since alpha might be > 1.0 due to russian roulette */
413                                         pixels[3] = saturate(f.w*scale);
414                                 }
415                         }
416                 }
417
418                 return true;
419         }
420
421         return false;
422 }
423
424 /* Display Buffer */
425
426 DisplayBuffer::DisplayBuffer(Device *device, bool linear)
427 : draw_width(0),
428   draw_height(0),
429   transparent(true), /* todo: determine from background */
430   half_float(linear),
431   rgba_byte(device, "display buffer byte"),
432   rgba_half(device, "display buffer half")
433 {
434 }
435
436 DisplayBuffer::~DisplayBuffer()
437 {
438         rgba_byte.free();
439         rgba_half.free();
440 }
441
442 void DisplayBuffer::reset(BufferParams& params_)
443 {
444         draw_width = 0;
445         draw_height = 0;
446
447         params = params_;
448
449         /* allocate display pixels */
450         if(half_float) {
451                 rgba_half.alloc_to_device(params.width, params.height);
452         }
453         else {
454                 rgba_byte.alloc_to_device(params.width, params.height);
455         }
456 }
457
458 void DisplayBuffer::draw_set(int width, int height)
459 {
460         assert(width <= params.width && height <= params.height);
461
462         draw_width = width;
463         draw_height = height;
464 }
465
466 void DisplayBuffer::draw(Device *device, const DeviceDrawParams& draw_params)
467 {
468         if(draw_width != 0 && draw_height != 0) {
469                 device_memory& rgba = (half_float)? (device_memory&)rgba_half:
470                                                     (device_memory&)rgba_byte;
471
472                 device->draw_pixels(
473                             rgba, 0,
474                             draw_width, draw_height, params.width, params.height,
475                             params.full_x, params.full_y, params.full_width, params.full_height,
476                             transparent, draw_params);
477         }
478 }
479
480 bool DisplayBuffer::draw_ready()
481 {
482         return (draw_width != 0 && draw_height != 0);
483 }
484
485 CCL_NAMESPACE_END