Cycles: Remove unneeded include statements
[blender-staging.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_image.h"
25 #include "util/util_math.h"
26 #include "util/util_opengl.h"
27 #include "util/util_time.h"
28 #include "util/util_types.h"
29
30 CCL_NAMESPACE_BEGIN
31
32 /* Buffer Params */
33
34 BufferParams::BufferParams()
35 {
36         width = 0;
37         height = 0;
38
39         full_x = 0;
40         full_y = 0;
41         full_width = 0;
42         full_height = 0;
43
44         denoising_data_pass = false;
45         denoising_clean_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         }
78
79         return align_up(size, 4);
80 }
81
82 int BufferParams::get_denoising_offset()
83 {
84         int offset = 0;
85
86         for(size_t i = 0; i < passes.size(); i++)
87                 offset += passes[i].components;
88
89         return offset;
90 }
91
92 /* Render Buffer Task */
93
94 RenderTile::RenderTile()
95 {
96         x = 0;
97         y = 0;
98         w = 0;
99         h = 0;
100
101         sample = 0;
102         start_sample = 0;
103         num_samples = 0;
104         resolution = 0;
105
106         offset = 0;
107         stride = 0;
108
109         buffer = 0;
110
111         buffers = NULL;
112 }
113
114 /* Render Buffers */
115
116 RenderBuffers::RenderBuffers(Device *device)
117 : buffer(device, "RenderBuffers", MEM_READ_WRITE),
118   map_neighbor_copied(false), render_time(0.0f)
119 {
120 }
121
122 RenderBuffers::~RenderBuffers()
123 {
124         buffer.free();
125 }
126
127 void RenderBuffers::reset(BufferParams& params_)
128 {
129         params = params_;
130
131         /* re-allocate buffer */
132         buffer.alloc(params.width*params.height*params.get_passes_size());
133         buffer.zero_to_device();
134 }
135
136 void RenderBuffers::zero()
137 {
138         buffer.zero_to_device();
139 }
140
141 bool RenderBuffers::copy_from_device()
142 {
143         if(!buffer.device_pointer)
144                 return false;
145
146         buffer.copy_from_device(0, params.width * params.get_passes_size(), params.height);
147
148         return true;
149 }
150
151 bool RenderBuffers::get_denoising_pass_rect(int offset, float exposure, int sample, int components, float *pixels)
152 {
153         if(buffer.data() == NULL) {
154                 return false;
155         }
156
157         float invsample = 1.0f/sample;
158         float scale = invsample;
159         bool variance = (offset == DENOISING_PASS_NORMAL_VAR) ||
160                         (offset == DENOISING_PASS_ALBEDO_VAR) ||
161                         (offset == DENOISING_PASS_DEPTH_VAR) ||
162                         (offset == DENOISING_PASS_COLOR_VAR);
163
164         if(offset == DENOISING_PASS_COLOR) {
165                 scale *= exposure;
166         }
167         else if(offset == DENOISING_PASS_COLOR_VAR) {
168                 scale *= exposure*exposure;
169         }
170
171         offset += params.get_denoising_offset();
172         int pass_stride = params.get_passes_size();
173         int size = params.width*params.height;
174
175         if(variance) {
176                 /* Approximate variance as E[x^2] - 1/N * (E[x])^2, since online variance
177                  * update does not work efficiently with atomics in the kernel. */
178                 int mean_offset = offset - components;
179                 float *mean = buffer.data() + mean_offset;
180                 float *var = buffer.data() + offset;
181                 assert(mean_offset >= 0);
182
183                 if(components == 1) {
184                         for(int i = 0; i < size; i++, mean += pass_stride, var += pass_stride, pixels++) {
185                                 pixels[0] = max(0.0f, var[0] - mean[0]*mean[0]*invsample)*scale;
186                         }
187                 }
188                 else if(components == 3) {
189                         for(int i = 0; i < size; i++, mean += pass_stride, var += pass_stride, pixels += 3) {
190                                 pixels[0] = max(0.0f, var[0] - mean[0]*mean[0]*invsample)*scale;
191                                 pixels[1] = max(0.0f, var[1] - mean[1]*mean[1]*invsample)*scale;
192                                 pixels[2] = max(0.0f, var[2] - mean[2]*mean[2]*invsample)*scale;
193                         }
194                 }
195                 else {
196                         return false;
197                 }
198         }
199         else {
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 {
215                         return false;
216                 }
217         }
218
219         return true;
220 }
221
222 bool RenderBuffers::get_pass_rect(PassType type, float exposure, int sample, int components, float *pixels)
223 {
224         if(buffer.data() == NULL) {
225                 return false;
226         }
227
228         int pass_offset = 0;
229
230         for(size_t j = 0; j < params.passes.size(); j++) {
231                 Pass& pass = params.passes[j];
232
233                 if(pass.type != type) {
234                         pass_offset += pass.components;
235                         continue;
236                 }
237
238                 float *in = buffer.data() + pass_offset;
239                 int pass_stride = params.get_passes_size();
240
241                 float scale = (pass.filter)? 1.0f/(float)sample: 1.0f;
242                 float scale_exposure = (pass.exposure)? scale*exposure: scale;
243
244                 int size = params.width*params.height;
245
246                 if(components == 1 && type == PASS_RENDER_TIME) {
247                         /* Render time is not stored by kernel, but measured per tile. */
248                         float val = (float) (1000.0 * render_time/(params.width * params.height * sample));
249                         for(int i = 0; i < size; i++, pixels++) {
250                                 pixels[0] = val;
251                         }
252                 }
253                 else if(components == 1) {
254                         assert(pass.components == components);
255
256                         /* Scalar */
257                         if(type == PASS_DEPTH) {
258                                 for(int i = 0; i < size; i++, in += pass_stride, pixels++) {
259                                         float f = *in;
260                                         pixels[0] = (f == 0.0f)? 1e10f: f*scale_exposure;
261                                 }
262                         }
263                         else if(type == PASS_MIST) {
264                                 for(int i = 0; i < size; i++, in += pass_stride, pixels++) {
265                                         float f = *in;
266                                         pixels[0] = saturate(f*scale_exposure);
267                                 }
268                         }
269 #ifdef WITH_CYCLES_DEBUG
270                         else if(type == PASS_BVH_TRAVERSED_NODES ||
271                                 type == PASS_BVH_TRAVERSED_INSTANCES ||
272                                 type == PASS_BVH_INTERSECTIONS ||
273                                 type == PASS_RAY_BOUNCES)
274                         {
275                                 for(int i = 0; i < size; i++, in += pass_stride, pixels++) {
276                                         float f = *in;
277                                         pixels[0] = f*scale;
278                                 }
279                         }
280 #endif
281                         else {
282                                 for(int i = 0; i < size; i++, in += pass_stride, pixels++) {
283                                         float f = *in;
284                                         pixels[0] = f*scale_exposure;
285                                 }
286                         }
287                 }
288                 else if(components == 3) {
289                         assert(pass.components == 4);
290
291                         /* RGBA */
292                         if(type == PASS_SHADOW) {
293                                 for(int i = 0; i < size; i++, in += pass_stride, pixels += 3) {
294                                         float4 f = make_float4(in[0], in[1], in[2], in[3]);
295                                         float invw = (f.w > 0.0f)? 1.0f/f.w: 1.0f;
296
297                                         pixels[0] = f.x*invw;
298                                         pixels[1] = f.y*invw;
299                                         pixels[2] = f.z*invw;
300                                 }
301                         }
302                         else if(pass.divide_type != PASS_NONE) {
303                                 /* RGB lighting passes that need to divide out color */
304                                 pass_offset = 0;
305                                 for(size_t k = 0; k < params.passes.size(); k++) {
306                                         Pass& color_pass = params.passes[k];
307                                         if(color_pass.type == pass.divide_type)
308                                                 break;
309                                         pass_offset += color_pass.components;
310                                 }
311
312                                 float *in_divide = buffer.data() + pass_offset;
313
314                                 for(int i = 0; i < size; i++, in += pass_stride, in_divide += pass_stride, pixels += 3) {
315                                         float3 f = make_float3(in[0], in[1], in[2]);
316                                         float3 f_divide = make_float3(in_divide[0], in_divide[1], in_divide[2]);
317
318                                         f = safe_divide_even_color(f*exposure, f_divide);
319
320                                         pixels[0] = f.x;
321                                         pixels[1] = f.y;
322                                         pixels[2] = f.z;
323                                 }
324                         }
325                         else {
326                                 /* RGB/vector */
327                                 for(int i = 0; i < size; i++, in += pass_stride, pixels += 3) {
328                                         float3 f = make_float3(in[0], in[1], in[2]);
329
330                                         pixels[0] = f.x*scale_exposure;
331                                         pixels[1] = f.y*scale_exposure;
332                                         pixels[2] = f.z*scale_exposure;
333                                 }
334                         }
335                 }
336                 else if(components == 4) {
337                         assert(pass.components == components);
338
339                         /* RGBA */
340                         if(type == PASS_SHADOW) {
341                                 for(int i = 0; i < size; i++, in += pass_stride, pixels += 4) {
342                                         float4 f = make_float4(in[0], in[1], in[2], in[3]);
343                                         float invw = (f.w > 0.0f)? 1.0f/f.w: 1.0f;
344
345                                         pixels[0] = f.x*invw;
346                                         pixels[1] = f.y*invw;
347                                         pixels[2] = f.z*invw;
348                                         pixels[3] = 1.0f;
349                                 }
350                         }
351                         else if(type == PASS_MOTION) {
352                                 /* need to normalize by number of samples accumulated for motion */
353                                 pass_offset = 0;
354                                 for(size_t k = 0; k < params.passes.size(); k++) {
355                                         Pass& color_pass = params.passes[k];
356                                         if(color_pass.type == PASS_MOTION_WEIGHT)
357                                                 break;
358                                         pass_offset += color_pass.components;
359                                 }
360
361                                 float *in_weight = buffer.data() + pass_offset;
362
363                                 for(int i = 0; i < size; i++, in += pass_stride, in_weight += pass_stride, pixels += 4) {
364                                         float4 f = make_float4(in[0], in[1], in[2], in[3]);
365                                         float w = in_weight[0];
366                                         float invw = (w > 0.0f)? 1.0f/w: 0.0f;
367
368                                         pixels[0] = f.x*invw;
369                                         pixels[1] = f.y*invw;
370                                         pixels[2] = f.z*invw;
371                                         pixels[3] = f.w*invw;
372                                 }
373                         }
374                         else {
375                                 for(int i = 0; i < size; i++, in += pass_stride, pixels += 4) {
376                                         float4 f = make_float4(in[0], in[1], in[2], in[3]);
377
378                                         pixels[0] = f.x*scale_exposure;
379                                         pixels[1] = f.y*scale_exposure;
380                                         pixels[2] = f.z*scale_exposure;
381
382                                         /* clamp since alpha might be > 1.0 due to russian roulette */
383                                         pixels[3] = saturate(f.w*scale);
384                                 }
385                         }
386                 }
387
388                 return true;
389         }
390
391         return false;
392 }
393
394 /* Display Buffer */
395
396 DisplayBuffer::DisplayBuffer(Device *device, bool linear)
397 : draw_width(0),
398   draw_height(0),
399   transparent(true), /* todo: determine from background */
400   half_float(linear),
401   rgba_byte(device, "display buffer byte"),
402   rgba_half(device, "display buffer half")
403 {
404 }
405
406 DisplayBuffer::~DisplayBuffer()
407 {
408         rgba_byte.free();
409         rgba_half.free();
410 }
411
412 void DisplayBuffer::reset(BufferParams& params_)
413 {
414         draw_width = 0;
415         draw_height = 0;
416
417         params = params_;
418
419         /* allocate display pixels */
420         if(half_float) {
421                 rgba_half.alloc_to_device(params.width, params.height);
422         }
423         else {
424                 rgba_byte.alloc_to_device(params.width, params.height);
425         }
426 }
427
428 void DisplayBuffer::draw_set(int width, int height)
429 {
430         assert(width <= params.width && height <= params.height);
431
432         draw_width = width;
433         draw_height = height;
434 }
435
436 void DisplayBuffer::draw(Device *device, const DeviceDrawParams& draw_params)
437 {
438         if(draw_width != 0 && draw_height != 0) {
439                 device_memory& rgba = (half_float)? (device_memory&)rgba_half:
440                                                     (device_memory&)rgba_byte;
441
442                 device->draw_pixels(rgba, 0, draw_width, draw_height, params.full_x, params.full_y, params.width, params.height, transparent, draw_params);
443         }
444 }
445
446 bool DisplayBuffer::draw_ready()
447 {
448         return (draw_width != 0 && draw_height != 0);
449 }
450
451 void DisplayBuffer::write(const string& filename)
452 {
453         int w = draw_width;
454         int h = draw_height;
455
456         if(w == 0 || h == 0)
457                 return;
458         
459         if(half_float)
460                 return;
461
462         /* read buffer from device */
463         uchar4 *pixels = rgba_byte.copy_from_device(0, w, h);
464
465         /* write image */
466         ImageOutput *out = ImageOutput::create(filename);
467         ImageSpec spec(w, h, 4, TypeDesc::UINT8);
468
469         out->open(filename, spec);
470
471         /* conversion for different top/bottom convention */
472         out->write_image(TypeDesc::UINT8,
473                 (uchar*)(pixels + (h-1)*w),
474                 AutoStride,
475                 -w*sizeof(uchar4),
476                 AutoStride);
477
478         out->close();
479
480         delete out;
481 }
482
483 CCL_NAMESPACE_END
484