Blender  V3.3
kernel/film/adaptive_sampling.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2019-2022 Blender Foundation */
3 
4 #pragma once
5 
7 
9 
10 /* Check whether the pixel has converged and should not be sampled anymore. */
11 
15 {
16  if (kernel_data.film.pass_adaptive_aux_buffer == PASS_UNUSED) {
17  return true;
18  }
19 
21  const uint64_t render_buffer_offset = (uint64_t)render_pixel_index *
22  kernel_data.film.pass_stride;
23  ccl_global float *buffer = render_buffer + render_buffer_offset;
24 
25  const uint aux_w_offset = kernel_data.film.pass_adaptive_aux_buffer + 3;
26  return buffer[aux_w_offset] == 0.0f;
27 }
28 
29 /* Determines whether to continue sampling a given pixel or if it has sufficiently converged. */
30 
33  int x,
34  int y,
35  float threshold,
36  bool reset,
37  int offset,
38  int stride)
39 {
40  kernel_assert(kernel_data.film.pass_adaptive_aux_buffer != PASS_UNUSED);
41  kernel_assert(kernel_data.film.pass_sample_count != PASS_UNUSED);
42 
43  const int render_pixel_index = offset + x + y * stride;
45  (uint64_t)render_pixel_index * kernel_data.film.pass_stride;
46 
47  /* TODO(Stefan): Is this better in linear, sRGB or something else? */
48 
49  const float4 A = kernel_read_pass_float4(buffer + kernel_data.film.pass_adaptive_aux_buffer);
50  if (!reset && A.w != 0.0f) {
51  /* If the pixel was considered converged, its state will not change in this kernel. Early
52  * output before doing any math.
53  *
54  * TODO(sergey): On a GPU it might be better to keep thread alive for better coherency? */
55  return true;
56  }
57 
58  const float4 I = kernel_read_pass_float4(buffer + kernel_data.film.pass_combined);
59 
60  const float sample = __float_as_uint(buffer[kernel_data.film.pass_sample_count]);
61  const float inv_sample = 1.0f / sample;
62 
63  /* The per pixel error as seen in section 2.1 of
64  * "A hierarchical automatic stopping condition for Monte Carlo global illumination" */
65  const float error_difference = (fabsf(I.x - A.x) + fabsf(I.y - A.y) + fabsf(I.z - A.z)) *
66  inv_sample;
67  const float error_normalize = sqrtf((I.x + I.y + I.z) * inv_sample);
68  /* A small epsilon is added to the divisor to prevent division by zero. */
69  const float error = error_difference / (0.0001f + error_normalize);
70  const bool did_converge = (error < threshold);
71 
72  const uint aux_w_offset = kernel_data.film.pass_adaptive_aux_buffer + 3;
73  buffer[aux_w_offset] = did_converge;
74 
75  return did_converge;
76 }
77 
78 /* This is a simple box filter in two passes.
79  * When a pixel demands more adaptive samples, let its neighboring pixels draw more samples too. */
80 
83  int y,
84  int start_x,
85  int width,
86  int offset,
87  int stride)
88 {
89  kernel_assert(kernel_data.film.pass_adaptive_aux_buffer != PASS_UNUSED);
90 
91  bool prev = false;
92  for (int x = start_x; x < start_x + width; ++x) {
93  int index = offset + x + y * stride;
94  ccl_global float *buffer = render_buffer + (uint64_t)index * kernel_data.film.pass_stride;
95  const uint aux_w_offset = kernel_data.film.pass_adaptive_aux_buffer + 3;
96 
97  if (buffer[aux_w_offset] == 0.0f) {
98  if (x > start_x && !prev) {
99  index = index - 1;
100  buffer = render_buffer + (uint64_t)index * kernel_data.film.pass_stride;
101  buffer[aux_w_offset] = 0.0f;
102  }
103  prev = true;
104  }
105  else {
106  if (prev) {
107  buffer[aux_w_offset] = 0.0f;
108  }
109  prev = false;
110  }
111  }
112 }
113 
115  ccl_global float *render_buffer,
116  int x,
117  int start_y,
118  int height,
119  int offset,
120  int stride)
121 {
122  kernel_assert(kernel_data.film.pass_adaptive_aux_buffer != PASS_UNUSED);
123 
124  bool prev = false;
125  for (int y = start_y; y < start_y + height; ++y) {
126  int index = offset + x + y * stride;
127  ccl_global float *buffer = render_buffer + (uint64_t)index * kernel_data.film.pass_stride;
128  const uint aux_w_offset = kernel_data.film.pass_adaptive_aux_buffer + 3;
129 
130  if (buffer[aux_w_offset] == 0.0f) {
131  if (y > start_y && !prev) {
132  index = index - stride;
133  buffer = render_buffer + (uint64_t)index * kernel_data.film.pass_stride;
134  buffer[aux_w_offset] = 0.0f;
135  }
136  prev = true;
137  }
138  else {
139  if (prev) {
140  buffer[aux_w_offset] = 0.0f;
141  }
142  prev = false;
143  }
144  }
145 }
146 
unsigned int uint
Definition: BLI_sys_types.h:67
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei height
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei width
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei stride
float float4[4]
#define A
#define kernel_assert(cond)
Definition: cpu/compat.h:34
#define ccl_device_forceinline
Definition: cuda/compat.h:35
#define ccl_device
Definition: cuda/compat.h:32
#define ccl_global
Definition: cuda/compat.h:43
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
#define kernel_data
const KernelGlobalsCPU *ccl_restrict KernelGlobals
ccl_global float * buffer
ccl_gpu_kernel_postfix ccl_global float int int int int float threshold
ccl_gpu_kernel_postfix ccl_global KernelWorkTile const int ccl_global float * render_buffer
const uint64_t render_pixel_index
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
const int state
ccl_gpu_kernel_postfix ccl_global float int int int int float bool reset
clear internal cached data and reset random seed
ccl_device void kernel_adaptive_sampling_filter_x(KernelGlobals kg, ccl_global float *render_buffer, int y, int start_x, int width, int offset, int stride)
ccl_device void kernel_adaptive_sampling_filter_y(KernelGlobals kg, ccl_global float *render_buffer, int x, int start_y, int height, int offset, int stride)
CCL_NAMESPACE_BEGIN ccl_device_forceinline bool kernel_need_sample_pixel(KernelGlobals kg, ConstIntegratorState state, ccl_global float *render_buffer)
ccl_device bool kernel_adaptive_sampling_convergence_check(KernelGlobals kg, ccl_global float *render_buffer, int x, int y, float threshold, bool reset, int offset, int stride)
#define PASS_UNUSED
Definition: kernel/types.h:44
static void error(const char *str)
Definition: meshlaplacian.c:51
#define fabsf(x)
Definition: metal/compat.h:219
#define sqrtf(x)
Definition: metal/compat.h:243
SymEdge< T > * prev(const SymEdge< T > *se)
Definition: delaunay_2d.cc:105
#define I
const IntegratorStateCPU *ccl_restrict ConstIntegratorState
Definition: state.h:148
#define INTEGRATOR_STATE(state, nested_struct, member)
Definition: state.h:154
unsigned int uint32_t
Definition: stdint.h:80
unsigned __int64 uint64_t
Definition: stdint.h:90
ccl_device_inline uint __float_as_uint(float f)
Definition: util/math.h:263
ccl_device_inline float4 kernel_read_pass_float4(ccl_global float *ccl_restrict buffer)
Definition: write_passes.h:70