Code refactor: move more memory allocation logic into device API.
[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_debug.h"
23 #include "util/util_foreach.h"
24 #include "util/util_hash.h"
25 #include "util/util_image.h"
26 #include "util/util_math.h"
27 #include "util/util_opengl.h"
28 #include "util/util_time.h"
29 #include "util/util_types.h"
30
31 CCL_NAMESPACE_BEGIN
32
33 /* Buffer Params */
34
35 BufferParams::BufferParams()
36 {
37         width = 0;
38         height = 0;
39
40         full_x = 0;
41         full_y = 0;
42         full_width = 0;
43         full_height = 0;
44
45         denoising_data_pass = false;
46         denoising_clean_pass = false;
47
48         Pass::add(PASS_COMBINED, passes);
49 }
50
51 void BufferParams::get_offset_stride(int& offset, int& stride)
52 {
53         offset = -(full_x + full_y*width);
54         stride = width;
55 }
56
57 bool BufferParams::modified(const BufferParams& params)
58 {
59         return !(full_x == params.full_x
60                 && full_y == params.full_y
61                 && width == params.width
62                 && height == params.height
63                 && full_width == params.full_width
64                 && full_height == params.full_height
65                 && Pass::equals(passes, params.passes));
66 }
67
68 int BufferParams::get_passes_size()
69 {
70         int size = 0;
71
72         for(size_t i = 0; i < passes.size(); i++)
73                 size += passes[i].components;
74
75         if(denoising_data_pass) {
76                 size += DENOISING_PASS_SIZE_BASE;
77                 if(denoising_clean_pass) size += DENOISING_PASS_SIZE_CLEAN;
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 /* Render Buffer Task */
94
95 RenderTile::RenderTile()
96 {
97         x = 0;
98         y = 0;
99         w = 0;
100         h = 0;
101
102         sample = 0;
103         start_sample = 0;
104         num_samples = 0;
105         resolution = 0;
106
107         offset = 0;
108         stride = 0;
109
110         buffer = 0;
111
112         buffers = NULL;
113 }
114
115 /* Render Buffers */
116
117 RenderBuffers::RenderBuffers(Device *device)
118 : buffer(device, "RenderBuffers", MEM_READ_WRITE)
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         float invsample = 1.0f/sample;
154         float scale = invsample;
155         bool variance = (offset == DENOISING_PASS_NORMAL_VAR) ||
156                         (offset == DENOISING_PASS_ALBEDO_VAR) ||
157                         (offset == DENOISING_PASS_DEPTH_VAR) ||
158                         (offset == DENOISING_PASS_COLOR_VAR);
159
160         if(offset == DENOISING_PASS_COLOR) {
161                 scale *= exposure;
162         }
163         else if(offset == DENOISING_PASS_COLOR_VAR) {
164                 scale *= exposure*exposure;
165         }
166
167         offset += params.get_denoising_offset();
168         int pass_stride = params.get_passes_size();
169         int size = params.width*params.height;
170
171         if(variance) {
172                 /* Approximate variance as E[x^2] - 1/N * (E[x])^2, since online variance
173                  * update does not work efficiently with atomics in the kernel. */
174                 int mean_offset = offset - components;
175                 float *mean = (float*)buffer.data_pointer + mean_offset;
176                 float *var = (float*)buffer.data_pointer + offset;
177                 assert(mean_offset >= 0);
178
179                 if(components == 1) {
180                         for(int i = 0; i < size; i++, mean += pass_stride, var += pass_stride, pixels++) {
181                                 pixels[0] = max(0.0f, var[0] - mean[0]*mean[0]*invsample)*scale;
182                         }
183                 }
184                 else if(components == 3) {
185                         for(int i = 0; i < size; i++, mean += pass_stride, var += pass_stride, pixels += 3) {
186                                 pixels[0] = max(0.0f, var[0] - mean[0]*mean[0]*invsample)*scale;
187                                 pixels[1] = max(0.0f, var[1] - mean[1]*mean[1]*invsample)*scale;
188                                 pixels[2] = max(0.0f, var[2] - mean[2]*mean[2]*invsample)*scale;
189                         }
190                 }
191                 else {
192                         return false;
193                 }
194         }
195         else {
196                 float *in = (float*)buffer.data_pointer + offset;
197
198                 if(components == 1) {
199                         for(int i = 0; i < size; i++, in += pass_stride, pixels++) {
200                                 pixels[0] = in[0]*scale;
201                         }
202                 }
203                 else if(components == 3) {
204                         for(int i = 0; i < size; i++, in += pass_stride, pixels += 3) {
205                                 pixels[0] = in[0]*scale;
206                                 pixels[1] = in[1]*scale;
207                                 pixels[2] = in[2]*scale;
208                         }
209                 }
210                 else {
211                         return false;
212                 }
213         }
214
215         return true;
216 }
217
218 bool RenderBuffers::get_pass_rect(PassType type, float exposure, int sample, int components, float *pixels)
219 {
220         int pass_offset = 0;
221
222         for(size_t j = 0; j < params.passes.size(); j++) {
223                 Pass& pass = params.passes[j];
224
225                 if(pass.type != type) {
226                         pass_offset += pass.components;
227                         continue;
228                 }
229
230                 float *in = (float*)buffer.data_pointer + pass_offset;
231                 int pass_stride = params.get_passes_size();
232
233                 float scale = (pass.filter)? 1.0f/(float)sample: 1.0f;
234                 float scale_exposure = (pass.exposure)? scale*exposure: scale;
235
236                 int size = params.width*params.height;
237
238                 if(components == 1) {
239                         assert(pass.components == components);
240
241                         /* scalar */
242                         if(type == PASS_DEPTH) {
243                                 for(int i = 0; i < size; i++, in += pass_stride, pixels++) {
244                                         float f = *in;
245                                         pixels[0] = (f == 0.0f)? 1e10f: f*scale_exposure;
246                                 }
247                         }
248                         else if(type == PASS_MIST) {
249                                 for(int i = 0; i < size; i++, in += pass_stride, pixels++) {
250                                         float f = *in;
251                                         pixels[0] = saturate(f*scale_exposure);
252                                 }
253                         }
254 #ifdef WITH_CYCLES_DEBUG
255                         else if(type == PASS_BVH_TRAVERSED_NODES ||
256                                 type == PASS_BVH_TRAVERSED_INSTANCES ||
257                                 type == PASS_BVH_INTERSECTIONS ||
258                                 type == PASS_RAY_BOUNCES)
259                         {
260                                 for(int i = 0; i < size; i++, in += pass_stride, pixels++) {
261                                         float f = *in;
262                                         pixels[0] = f*scale;
263                                 }
264                         }
265 #endif
266                         else {
267                                 for(int i = 0; i < size; i++, in += pass_stride, pixels++) {
268                                         float f = *in;
269                                         pixels[0] = f*scale_exposure;
270                                 }
271                         }
272                 }
273                 else if(components == 3) {
274                         assert(pass.components == 4);
275
276                         /* RGBA */
277                         if(type == PASS_SHADOW) {
278                                 for(int i = 0; i < size; i++, in += pass_stride, pixels += 3) {
279                                         float4 f = make_float4(in[0], in[1], in[2], in[3]);
280                                         float invw = (f.w > 0.0f)? 1.0f/f.w: 1.0f;
281
282                                         pixels[0] = f.x*invw;
283                                         pixels[1] = f.y*invw;
284                                         pixels[2] = f.z*invw;
285                                 }
286                         }
287                         else if(pass.divide_type != PASS_NONE) {
288                                 /* RGB lighting passes that need to divide out color */
289                                 pass_offset = 0;
290                                 for(size_t k = 0; k < params.passes.size(); k++) {
291                                         Pass& color_pass = params.passes[k];
292                                         if(color_pass.type == pass.divide_type)
293                                                 break;
294                                         pass_offset += color_pass.components;
295                                 }
296
297                                 float *in_divide = (float*)buffer.data_pointer + pass_offset;
298
299                                 for(int i = 0; i < size; i++, in += pass_stride, in_divide += pass_stride, pixels += 3) {
300                                         float3 f = make_float3(in[0], in[1], in[2]);
301                                         float3 f_divide = make_float3(in_divide[0], in_divide[1], in_divide[2]);
302
303                                         f = safe_divide_even_color(f*exposure, f_divide);
304
305                                         pixels[0] = f.x;
306                                         pixels[1] = f.y;
307                                         pixels[2] = f.z;
308                                 }
309                         }
310                         else {
311                                 /* RGB/vector */
312                                 for(int i = 0; i < size; i++, in += pass_stride, pixels += 3) {
313                                         float3 f = make_float3(in[0], in[1], in[2]);
314
315                                         pixels[0] = f.x*scale_exposure;
316                                         pixels[1] = f.y*scale_exposure;
317                                         pixels[2] = f.z*scale_exposure;
318                                 }
319                         }
320                 }
321                 else if(components == 4) {
322                         assert(pass.components == components);
323
324                         /* RGBA */
325                         if(type == PASS_SHADOW) {
326                                 for(int i = 0; i < size; i++, in += pass_stride, pixels += 4) {
327                                         float4 f = make_float4(in[0], in[1], in[2], in[3]);
328                                         float invw = (f.w > 0.0f)? 1.0f/f.w: 1.0f;
329
330                                         pixels[0] = f.x*invw;
331                                         pixels[1] = f.y*invw;
332                                         pixels[2] = f.z*invw;
333                                         pixels[3] = 1.0f;
334                                 }
335                         }
336                         else if(type == PASS_MOTION) {
337                                 /* need to normalize by number of samples accumulated for motion */
338                                 pass_offset = 0;
339                                 for(size_t k = 0; k < params.passes.size(); k++) {
340                                         Pass& color_pass = params.passes[k];
341                                         if(color_pass.type == PASS_MOTION_WEIGHT)
342                                                 break;
343                                         pass_offset += color_pass.components;
344                                 }
345
346                                 float *in_weight = (float*)buffer.data_pointer + pass_offset;
347
348                                 for(int i = 0; i < size; i++, in += pass_stride, in_weight += pass_stride, pixels += 4) {
349                                         float4 f = make_float4(in[0], in[1], in[2], in[3]);
350                                         float w = in_weight[0];
351                                         float invw = (w > 0.0f)? 1.0f/w: 0.0f;
352
353                                         pixels[0] = f.x*invw;
354                                         pixels[1] = f.y*invw;
355                                         pixels[2] = f.z*invw;
356                                         pixels[3] = f.w*invw;
357                                 }
358                         }
359                         else {
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
363                                         pixels[0] = f.x*scale_exposure;
364                                         pixels[1] = f.y*scale_exposure;
365                                         pixels[2] = f.z*scale_exposure;
366
367                                         /* clamp since alpha might be > 1.0 due to russian roulette */
368                                         pixels[3] = saturate(f.w*scale);
369                                 }
370                         }
371                 }
372
373                 return true;
374         }
375
376         return false;
377 }
378
379 /* Display Buffer */
380
381 DisplayBuffer::DisplayBuffer(Device *device, bool linear)
382 : draw_width(0),
383   draw_height(0),
384   transparent(true), /* todo: determine from background */
385   half_float(linear),
386   rgba_byte(device, "display buffer byte"),
387   rgba_half(device, "display buffer half")
388 {
389 }
390
391 DisplayBuffer::~DisplayBuffer()
392 {
393         rgba_byte.free();
394         rgba_half.free();
395 }
396
397 void DisplayBuffer::reset(BufferParams& params_)
398 {
399         draw_width = 0;
400         draw_height = 0;
401
402         params = params_;
403
404         /* allocate display pixels */
405         if(half_float) {
406                 rgba_half.alloc_to_device(params.width, params.height);
407         }
408         else {
409                 rgba_byte.alloc_to_device(params.width, params.height);
410         }
411 }
412
413 void DisplayBuffer::draw_set(int width, int height)
414 {
415         assert(width <= params.width && height <= params.height);
416
417         draw_width = width;
418         draw_height = height;
419 }
420
421 void DisplayBuffer::draw(Device *device, const DeviceDrawParams& draw_params)
422 {
423         if(draw_width != 0 && draw_height != 0) {
424                 device_memory& rgba = (half_float)? (device_memory&)rgba_half:
425                                                     (device_memory&)rgba_byte;
426
427                 device->draw_pixels(rgba, 0, draw_width, draw_height, params.full_x, params.full_y, params.width, params.height, transparent, draw_params);
428         }
429 }
430
431 bool DisplayBuffer::draw_ready()
432 {
433         return (draw_width != 0 && draw_height != 0);
434 }
435
436 void DisplayBuffer::write(const string& filename)
437 {
438         int w = draw_width;
439         int h = draw_height;
440
441         if(w == 0 || h == 0)
442                 return;
443         
444         if(half_float)
445                 return;
446
447         /* read buffer from device */
448         uchar4 *pixels = rgba_byte.copy_from_device(0, w, h);
449
450         /* write image */
451         ImageOutput *out = ImageOutput::create(filename);
452         ImageSpec spec(w, h, 4, TypeDesc::UINT8);
453
454         out->open(filename, spec);
455
456         /* conversion for different top/bottom convention */
457         out->write_image(TypeDesc::UINT8,
458                 (uchar*)(pixels + (h-1)*w),
459                 AutoStride,
460                 -w*sizeof(uchar4),
461                 AutoStride);
462
463         out->close();
464
465         delete out;
466 }
467
468 CCL_NAMESPACE_END
469