2c3bfefd8b08604535c6d0b0c93ec5c97a186186
[blender-staging.git] / intern / cycles / device / device_denoising.cpp
1 /*
2  * Copyright 2011-2017 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 "device/device_denoising.h"
18
19 #include "kernel/filter/filter_defines.h"
20
21 CCL_NAMESPACE_BEGIN
22
23 void DenoisingTask::init_from_devicetask(const DeviceTask &task)
24 {
25         radius = task.denoising_radius;
26         nlm_k_2 = powf(2.0f, lerp(-5.0f, 3.0f, task.denoising_strength));
27         if(task.denoising_relative_pca) {
28                 pca_threshold = -powf(10.0f, lerp(-8.0f, 0.0f, task.denoising_feature_strength));
29         }
30         else {
31                 pca_threshold = powf(10.0f, lerp(-5.0f, 3.0f, task.denoising_feature_strength));
32         }
33
34         render_buffer.pass_stride = task.pass_stride;
35         render_buffer.denoising_data_offset  = task.pass_denoising_data;
36         render_buffer.denoising_clean_offset = task.pass_denoising_clean;
37
38         /* Expand filter_area by radius pixels and clamp the result to the extent of the neighboring tiles */
39         rect = make_int4(max(tiles->x[0], filter_area.x - radius),
40                          max(tiles->y[0], filter_area.y - radius),
41                          min(tiles->x[3], filter_area.x + filter_area.z + radius),
42                          min(tiles->y[3], filter_area.y + filter_area.w + radius));
43 }
44
45 void DenoisingTask::tiles_from_rendertiles(RenderTile *rtiles)
46 {
47         tiles = (TilesInfo*) tiles_mem.resize(sizeof(TilesInfo)/sizeof(int));
48
49         device_ptr buffers[9];
50         for(int i = 0; i < 9; i++) {
51                 buffers[i] = rtiles[i].buffer;
52                 tiles->offsets[i] = rtiles[i].offset;
53                 tiles->strides[i] = rtiles[i].stride;
54         }
55         tiles->x[0] = rtiles[3].x;
56         tiles->x[1] = rtiles[4].x;
57         tiles->x[2] = rtiles[5].x;
58         tiles->x[3] = rtiles[5].x + rtiles[5].w;
59         tiles->y[0] = rtiles[1].y;
60         tiles->y[1] = rtiles[4].y;
61         tiles->y[2] = rtiles[7].y;
62         tiles->y[3] = rtiles[7].y + rtiles[7].h;
63
64         render_buffer.offset = rtiles[4].offset;
65         render_buffer.stride = rtiles[4].stride;
66         render_buffer.ptr    = rtiles[4].buffer;
67
68         functions.set_tiles(buffers);
69 }
70
71 bool DenoisingTask::run_denoising()
72 {
73         /* Allocate denoising buffer. */
74         buffer.passes = 14;
75         buffer.w = align_up(rect.z - rect.x, 4);
76         buffer.h = rect.w - rect.y;
77         buffer.pass_stride = align_up(buffer.w * buffer.h, divide_up(device->mem_address_alignment(), sizeof(float)));
78         buffer.mem.resize(buffer.pass_stride * buffer.passes);
79         device->mem_alloc(buffer.mem);
80
81         device_ptr null_ptr = (device_ptr) 0;
82
83         /* Prefilter shadow feature. */
84         {
85                 device_sub_ptr unfiltered_a   (buffer.mem, 0,                    buffer.pass_stride);
86                 device_sub_ptr unfiltered_b   (buffer.mem, 1*buffer.pass_stride, buffer.pass_stride);
87                 device_sub_ptr sample_var     (buffer.mem, 2*buffer.pass_stride, buffer.pass_stride);
88                 device_sub_ptr sample_var_var (buffer.mem, 3*buffer.pass_stride, buffer.pass_stride);
89                 device_sub_ptr buffer_var     (buffer.mem, 5*buffer.pass_stride, buffer.pass_stride);
90                 device_sub_ptr filtered_var   (buffer.mem, 6*buffer.pass_stride, buffer.pass_stride);
91                 device_sub_ptr nlm_temporary_1(buffer.mem, 7*buffer.pass_stride, buffer.pass_stride);
92                 device_sub_ptr nlm_temporary_2(buffer.mem, 8*buffer.pass_stride, buffer.pass_stride);
93                 device_sub_ptr nlm_temporary_3(buffer.mem, 9*buffer.pass_stride, buffer.pass_stride);
94
95                 nlm_state.temporary_1_ptr = *nlm_temporary_1;
96                 nlm_state.temporary_2_ptr = *nlm_temporary_2;
97                 nlm_state.temporary_3_ptr = *nlm_temporary_3;
98
99                 /* Get the A/B unfiltered passes, the combined sample variance, the estimated variance of the sample variance and the buffer variance. */
100                 functions.divide_shadow(*unfiltered_a, *unfiltered_b, *sample_var, *sample_var_var, *buffer_var);
101
102                 /* Smooth the (generally pretty noisy) buffer variance using the spatial information from the sample variance. */
103                 nlm_state.set_parameters(6, 3, 4.0f, 1.0f);
104                 functions.non_local_means(*buffer_var, *sample_var, *sample_var_var, *filtered_var);
105
106                 /* Reuse memory, the previous data isn't needed anymore. */
107                 device_ptr filtered_a = *buffer_var,
108                            filtered_b = *sample_var;
109                 /* Use the smoothed variance to filter the two shadow half images using each other for weight calculation. */
110                 nlm_state.set_parameters(5, 3, 1.0f, 0.25f);
111                 functions.non_local_means(*unfiltered_a, *unfiltered_b, *filtered_var, filtered_a);
112                 functions.non_local_means(*unfiltered_b, *unfiltered_a, *filtered_var, filtered_b);
113
114                 device_ptr residual_var = *sample_var_var;
115                 /* Estimate the residual variance between the two filtered halves. */
116                 functions.combine_halves(filtered_a, filtered_b, null_ptr, residual_var, 2, rect);
117
118                 device_ptr final_a = *unfiltered_a,
119                            final_b = *unfiltered_b;
120                 /* Use the residual variance for a second filter pass. */
121                 nlm_state.set_parameters(4, 2, 1.0f, 0.5f);
122                 functions.non_local_means(filtered_a, filtered_b, residual_var, final_a);
123                 functions.non_local_means(filtered_b, filtered_a, residual_var, final_b);
124
125                 /* Combine the two double-filtered halves to a final shadow feature. */
126                 device_sub_ptr shadow_pass(buffer.mem, 4*buffer.pass_stride, buffer.pass_stride);
127                 functions.combine_halves(final_a, final_b, *shadow_pass, null_ptr, 0, rect);
128         }
129
130         /* Prefilter general features. */
131         {
132                 device_sub_ptr unfiltered     (buffer.mem,  8*buffer.pass_stride, buffer.pass_stride);
133                 device_sub_ptr variance       (buffer.mem,  9*buffer.pass_stride, buffer.pass_stride);
134                 device_sub_ptr nlm_temporary_1(buffer.mem, 10*buffer.pass_stride, buffer.pass_stride);
135                 device_sub_ptr nlm_temporary_2(buffer.mem, 11*buffer.pass_stride, buffer.pass_stride);
136                 device_sub_ptr nlm_temporary_3(buffer.mem, 12*buffer.pass_stride, buffer.pass_stride);
137
138                 nlm_state.temporary_1_ptr = *nlm_temporary_1;
139                 nlm_state.temporary_2_ptr = *nlm_temporary_2;
140                 nlm_state.temporary_3_ptr = *nlm_temporary_3;
141
142                 int mean_from[]     = { 0, 1, 2, 12, 6,  7, 8 };
143                 int variance_from[] = { 3, 4, 5, 13, 9, 10, 11};
144                 int pass_to[]       = { 1, 2, 3, 0,  5,  6,  7};
145                 for(int pass = 0; pass < 7; pass++) {
146                         device_sub_ptr feature_pass(buffer.mem, pass_to[pass]*buffer.pass_stride, buffer.pass_stride);
147                         /* Get the unfiltered pass and its variance from the RenderBuffers. */
148                         functions.get_feature(mean_from[pass], variance_from[pass], *unfiltered, *variance);
149                         /* Smooth the pass and store the result in the denoising buffers. */
150                         nlm_state.set_parameters(2, 2, 1.0f, 0.25f);
151                         functions.non_local_means(*unfiltered, *unfiltered, *variance, *feature_pass);
152                 }
153         }
154
155         /* Copy color passes. */
156         {
157                 int mean_from[]     = {20, 21, 22};
158                 int variance_from[] = {23, 24, 25};
159                 int mean_to[]       = { 8,  9, 10};
160                 int variance_to[]   = {11, 12, 13};
161                 int num_color_passes = 3;
162
163                 device_only_memory<float> temp_color(device, "Denoising temporary color");
164                 temp_color.resize(3*buffer.pass_stride);
165                 device->mem_alloc(temp_color);
166
167                 for(int pass = 0; pass < num_color_passes; pass++) {
168                         device_sub_ptr color_pass(temp_color, pass*buffer.pass_stride, buffer.pass_stride);
169                         device_sub_ptr color_var_pass(buffer.mem, variance_to[pass]*buffer.pass_stride, buffer.pass_stride);
170                         functions.get_feature(mean_from[pass], variance_from[pass], *color_pass, *color_var_pass);
171                 }
172
173                 {
174                         device_sub_ptr depth_pass    (buffer.mem,                                 0,   buffer.pass_stride);
175                         device_sub_ptr color_var_pass(buffer.mem, variance_to[0]*buffer.pass_stride, 3*buffer.pass_stride);
176                         device_sub_ptr output_pass   (buffer.mem,     mean_to[0]*buffer.pass_stride, 3*buffer.pass_stride);
177                         functions.detect_outliers(temp_color.device_pointer, *color_var_pass, *depth_pass, *output_pass);
178                 }
179
180                 device->mem_free(temp_color);
181         }
182
183         storage.w = filter_area.z;
184         storage.h = filter_area.w;
185         storage.transform.resize(storage.w*storage.h*TRANSFORM_SIZE);
186         storage.rank.resize(storage.w*storage.h);
187         device->mem_alloc(storage.transform);
188         device->mem_alloc(storage.rank);
189
190         functions.construct_transform();
191
192         device_only_memory<float> temporary_1(device, "Denoising NLM temporary 1");
193         device_only_memory<float> temporary_2(device, "Denoising NLM temporary 2");
194         temporary_1.resize(buffer.w*buffer.h);
195         temporary_2.resize(buffer.w*buffer.h);
196         device->mem_alloc(temporary_1);
197         device->mem_alloc(temporary_2);
198         reconstruction_state.temporary_1_ptr = temporary_1.device_pointer;
199         reconstruction_state.temporary_2_ptr = temporary_2.device_pointer;
200
201         storage.XtWX.resize(storage.w*storage.h*XTWX_SIZE);
202         storage.XtWY.resize(storage.w*storage.h*XTWY_SIZE);
203         device->mem_alloc(storage.XtWX);
204         device->mem_alloc(storage.XtWY);
205
206         reconstruction_state.filter_rect = make_int4(filter_area.x-rect.x, filter_area.y-rect.y, storage.w, storage.h);
207         int tile_coordinate_offset = filter_area.y*render_buffer.stride + filter_area.x;
208         reconstruction_state.buffer_params = make_int4(render_buffer.offset + tile_coordinate_offset,
209                                                        render_buffer.stride,
210                                                        render_buffer.pass_stride,
211                                                        render_buffer.denoising_clean_offset);
212         reconstruction_state.source_w = rect.z-rect.x;
213         reconstruction_state.source_h = rect.w-rect.y;
214
215         {
216                 device_sub_ptr color_ptr    (buffer.mem,  8*buffer.pass_stride, 3*buffer.pass_stride);
217                 device_sub_ptr color_var_ptr(buffer.mem, 11*buffer.pass_stride, 3*buffer.pass_stride);
218                 functions.reconstruct(*color_ptr, *color_var_ptr, render_buffer.ptr);
219         }
220
221         device->mem_free(storage.XtWX);
222         device->mem_free(storage.XtWY);
223         device->mem_free(storage.transform);
224         device->mem_free(storage.rank);
225         device->mem_free(temporary_1);
226         device->mem_free(temporary_2);
227         device->mem_free(buffer.mem);
228         device->mem_free(tiles_mem);
229         return true;
230 }
231
232 CCL_NAMESPACE_END