Welcome to mirror list, hosted at ThFree Co, Russian Federation.

kernel_adaptive_sampling.h « kernel « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ee4d1507ef12da91df6fac797d156b343649b8dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Copyright 2019 Blender Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __KERNEL_ADAPTIVE_SAMPLING_H__
#define __KERNEL_ADAPTIVE_SAMPLING_H__

CCL_NAMESPACE_BEGIN

/* Determines whether to continue sampling a given pixel or if it has sufficiently converged. */

ccl_device void kernel_do_adaptive_stopping(KernelGlobals *kg,
                                            ccl_global float *buffer,
                                            int sample)
{
  /* TODO Stefan: Is this better in linear, sRGB or something else? */
  float4 I = *((ccl_global float4 *)buffer);
  float4 A = *(ccl_global float4 *)(buffer + kernel_data.film.pass_adaptive_aux_buffer);
  /* The per pixel error as seen in section 2.1 of
   * "A hierarchical automatic stopping condition for Monte Carlo global illumination"
   * A small epsilon is added to the divisor to prevent division by zero. */
  float error = (fabsf(I.x - A.x) + fabsf(I.y - A.y) + fabsf(I.z - A.z)) /
                (sample * 0.0001f + sqrtf(I.x + I.y + I.z));
  if (error < kernel_data.integrator.adaptive_threshold * (float)sample) {
    /* Set the fourth component to non-zero value to indicate that this pixel has converged. */
    buffer[kernel_data.film.pass_adaptive_aux_buffer + 3] += 1.0f;
  }
}

/* Adjust the values of an adaptively sampled pixel. */

ccl_device void kernel_adaptive_post_adjust(KernelGlobals *kg,
                                            ccl_global float *buffer,
                                            float sample_multiplier)
{
  *(ccl_global float4 *)(buffer) *= sample_multiplier;

  /* Scale the aux pass too, this is necessary for progressive rendering to work properly. */
  kernel_assert(kernel_data.film.pass_adaptive_aux_buffer);
  *(ccl_global float4 *)(buffer + kernel_data.film.pass_adaptive_aux_buffer) *= sample_multiplier;

#ifdef __PASSES__
  int flag = kernel_data.film.pass_flag;

  if (flag & PASSMASK(NORMAL))
    *(ccl_global float3 *)(buffer + kernel_data.film.pass_normal) *= sample_multiplier;

  if (flag & PASSMASK(UV))
    *(ccl_global float3 *)(buffer + kernel_data.film.pass_uv) *= sample_multiplier;

  if (flag & PASSMASK(MOTION)) {
    *(ccl_global float4 *)(buffer + kernel_data.film.pass_motion) *= sample_multiplier;
    *(ccl_global float *)(buffer + kernel_data.film.pass_motion_weight) *= sample_multiplier;
  }

  if (kernel_data.film.use_light_pass) {
    int light_flag = kernel_data.film.light_pass_flag;

    if (light_flag & PASSMASK(MIST))
      *(ccl_global float *)(buffer + kernel_data.film.pass_mist) *= sample_multiplier;

    /* Shadow pass omitted on purpose. It has its own scale parameter. */

    if (light_flag & PASSMASK(DIFFUSE_INDIRECT))
      *(ccl_global float3 *)(buffer + kernel_data.film.pass_diffuse_indirect) *= sample_multiplier;
    if (light_flag & PASSMASK(GLOSSY_INDIRECT))
      *(ccl_global float3 *)(buffer + kernel_data.film.pass_glossy_indirect) *= sample_multiplier;
    if (light_flag & PASSMASK(TRANSMISSION_INDIRECT))
      *(ccl_global float3 *)(buffer +
                             kernel_data.film.pass_transmission_indirect) *= sample_multiplier;
    if (light_flag & PASSMASK(VOLUME_INDIRECT))
      *(ccl_global float3 *)(buffer + kernel_data.film.pass_volume_indirect) *= sample_multiplier;
    if (light_flag & PASSMASK(DIFFUSE_DIRECT))
      *(ccl_global float3 *)(buffer + kernel_data.film.pass_diffuse_direct) *= sample_multiplier;
    if (light_flag & PASSMASK(GLOSSY_DIRECT))
      *(ccl_global float3 *)(buffer + kernel_data.film.pass_glossy_direct) *= sample_multiplier;
    if (light_flag & PASSMASK(TRANSMISSION_DIRECT))
      *(ccl_global float3 *)(buffer +
                             kernel_data.film.pass_transmission_direct) *= sample_multiplier;
    if (light_flag & PASSMASK(VOLUME_DIRECT))
      *(ccl_global float3 *)(buffer + kernel_data.film.pass_volume_direct) *= sample_multiplier;

    if (light_flag & PASSMASK(EMISSION))
      *(ccl_global float3 *)(buffer + kernel_data.film.pass_emission) *= sample_multiplier;
    if (light_flag & PASSMASK(BACKGROUND))
      *(ccl_global float3 *)(buffer + kernel_data.film.pass_background) *= sample_multiplier;
    if (light_flag & PASSMASK(AO))
      *(ccl_global float3 *)(buffer + kernel_data.film.pass_ao) *= sample_multiplier;

    if (light_flag & PASSMASK(DIFFUSE_COLOR))
      *(ccl_global float3 *)(buffer + kernel_data.film.pass_diffuse_color) *= sample_multiplier;
    if (light_flag & PASSMASK(GLOSSY_COLOR))
      *(ccl_global float3 *)(buffer + kernel_data.film.pass_glossy_color) *= sample_multiplier;
    if (light_flag & PASSMASK(TRANSMISSION_COLOR))
      *(ccl_global float3 *)(buffer +
                             kernel_data.film.pass_transmission_color) *= sample_multiplier;
  }
#endif

#ifdef __DENOISING_FEATURES__

#  define scale_float3_variance(buffer, offset, scale) \
    *(buffer + offset) *= scale; \
    *(buffer + offset + 1) *= scale; \
    *(buffer + offset + 2) *= scale; \
    *(buffer + offset + 3) *= scale * scale; \
    *(buffer + offset + 4) *= scale * scale; \
    *(buffer + offset + 5) *= scale * scale;

#  define scale_shadow_variance(buffer, offset, scale) \
    *(buffer + offset) *= scale; \
    *(buffer + offset + 1) *= scale; \
    *(buffer + offset + 2) *= scale * scale;

  if (kernel_data.film.pass_denoising_data) {
    scale_shadow_variance(
        buffer, kernel_data.film.pass_denoising_data + DENOISING_PASS_SHADOW_A, sample_multiplier);
    scale_shadow_variance(
        buffer, kernel_data.film.pass_denoising_data + DENOISING_PASS_SHADOW_B, sample_multiplier);
    if (kernel_data.film.pass_denoising_clean) {
      scale_float3_variance(
          buffer, kernel_data.film.pass_denoising_data + DENOISING_PASS_COLOR, sample_multiplier);
      *(buffer + kernel_data.film.pass_denoising_clean) *= sample_multiplier;
      *(buffer + kernel_data.film.pass_denoising_clean + 1) *= sample_multiplier;
      *(buffer + kernel_data.film.pass_denoising_clean + 2) *= sample_multiplier;
    }
    else {
      scale_float3_variance(
          buffer, kernel_data.film.pass_denoising_data + DENOISING_PASS_COLOR, sample_multiplier);
    }
    scale_float3_variance(
        buffer, kernel_data.film.pass_denoising_data + DENOISING_PASS_NORMAL, sample_multiplier);
    scale_float3_variance(
        buffer, kernel_data.film.pass_denoising_data + DENOISING_PASS_ALBEDO, sample_multiplier);
    *(buffer + kernel_data.film.pass_denoising_data + DENOISING_PASS_DEPTH) *= sample_multiplier;
    *(buffer + kernel_data.film.pass_denoising_data + DENOISING_PASS_DEPTH +
      1) *= sample_multiplier * sample_multiplier;
  }
#endif /* __DENOISING_FEATURES__ */

  /* Cryptomatte. */
  if (kernel_data.film.cryptomatte_passes) {
    int num_slots = 0;
    num_slots += (kernel_data.film.cryptomatte_passes & CRYPT_OBJECT) ? 1 : 0;
    num_slots += (kernel_data.film.cryptomatte_passes & CRYPT_MATERIAL) ? 1 : 0;
    num_slots += (kernel_data.film.cryptomatte_passes & CRYPT_ASSET) ? 1 : 0;
    num_slots = num_slots * 2 * kernel_data.film.cryptomatte_depth;
    ccl_global float2 *id_buffer = (ccl_global float2 *)(buffer +
                                                         kernel_data.film.pass_cryptomatte);
    for (int slot = 0; slot < num_slots; slot++) {
      id_buffer[slot].y *= sample_multiplier;
    }
  }

  /* AOVs. */
  for (int i = 0; i < kernel_data.film.pass_aov_value_num; i++) {
    *(buffer + kernel_data.film.pass_aov_value + i) *= sample_multiplier;
  }
  for (int i = 0; i < kernel_data.film.pass_aov_color_num; i++) {
    *((ccl_global float4 *)(buffer + kernel_data.film.pass_aov_color) + i) *= sample_multiplier;
  }
}

/* This is a simple box filter in two passes.
 * When a pixel demands more adaptive samples, let its neighboring pixels draw more samples too. */

ccl_device bool kernel_do_adaptive_filter_x(KernelGlobals *kg, int y, ccl_global WorkTile *tile)
{
  bool any = false;
  bool prev = false;
  for (int x = tile->x; x < tile->x + tile->w; ++x) {
    int index = tile->offset + x + y * tile->stride;
    ccl_global float *buffer = tile->buffer + index * kernel_data.film.pass_stride;
    ccl_global float4 *aux = (ccl_global float4 *)(buffer +
                                                   kernel_data.film.pass_adaptive_aux_buffer);
    if (aux->w == 0.0f) {
      any = true;
      if (x > tile->x && !prev) {
        index = index - 1;
        buffer = tile->buffer + index * kernel_data.film.pass_stride;
        aux = (ccl_global float4 *)(buffer + kernel_data.film.pass_adaptive_aux_buffer);
        aux->w = 0.0f;
      }
      prev = true;
    }
    else {
      if (prev) {
        aux->w = 0.0f;
      }
      prev = false;
    }
  }
  return any;
}

ccl_device bool kernel_do_adaptive_filter_y(KernelGlobals *kg, int x, ccl_global WorkTile *tile)
{
  bool prev = false;
  bool any = false;
  for (int y = tile->y; y < tile->y + tile->h; ++y) {
    int index = tile->offset + x + y * tile->stride;
    ccl_global float *buffer = tile->buffer + index * kernel_data.film.pass_stride;
    ccl_global float4 *aux = (ccl_global float4 *)(buffer +
                                                   kernel_data.film.pass_adaptive_aux_buffer);
    if (aux->w == 0.0f) {
      any = true;
      if (y > tile->y && !prev) {
        index = index - tile->stride;
        buffer = tile->buffer + index * kernel_data.film.pass_stride;
        aux = (ccl_global float4 *)(buffer + kernel_data.film.pass_adaptive_aux_buffer);
        aux->w = 0.0f;
      }
      prev = true;
    }
    else {
      if (prev) {
        aux->w = 0.0f;
      }
      prev = false;
    }
  }
  return any;
}

CCL_NAMESPACE_END

#endif /* __KERNEL_ADAPTIVE_SAMPLING_H__ */