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