Blender  V3.3
kernel/sample/jitter.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #pragma once
6 
8 {
9  x += seed;
10  x ^= (x * 0x6c50b47cu);
11  x ^= x * 0xb82f1e52u;
12  x ^= x * 0xc7afe638u;
13  x ^= x * 0x8d22f6e6u;
14 
15  return x;
16 }
17 
19 {
23 
24  return x;
25 }
26 
28 {
29  i ^= p;
30  i ^= i >> 17;
31  i ^= i >> 10;
32  i *= 0xb36534e5;
33  i ^= i >> 12;
34  i ^= i >> 21;
35  i *= 0x93fc4795;
36  i ^= 0xdf6e307f;
37  i ^= i >> 17;
38  i *= 1 | p >> 18;
39 
40  return i;
41 }
42 
44 {
45  i = (i ^ 61) ^ p;
46  i += i << 3;
47  i ^= i >> 4;
48  i *= 0x27d4eb2d;
49  return i;
50 }
51 
53 {
54  return cmj_hash(i, p) * (1.0f / 4294967808.0f);
55 }
56 
58 {
59  return cmj_hash_simple(i, p) * (1.0f / (float)0xFFFFFFFF);
60 }
61 
63 {
64  return cmj_hash_simple(i, p) * (d / (float)0xFFFFFFFF);
65 }
66 
67 ccl_device float pmj_sample_1D(KernelGlobals kg, uint sample, uint rng_hash, uint dimension)
68 {
69  uint hash = rng_hash;
70  float jitter_x = 0.0f;
71  if (kernel_data.integrator.scrambling_distance < 1.0f) {
72  hash = kernel_data.integrator.seed;
73 
74  jitter_x = cmj_randfloat_simple_dist(
75  dimension, rng_hash, kernel_data.integrator.scrambling_distance);
76  }
77 
78  /* Perform Owen shuffle of the sample number to reorder the samples. */
79 #ifdef _SIMPLE_HASH_
80  const uint rv = cmj_hash_simple(dimension, hash);
81 #else /* Use a _REGULAR_HASH_. */
82  const uint rv = cmj_hash(dimension, hash);
83 #endif
84 #ifdef _XOR_SHUFFLE_
85 # warning "Using XOR shuffle."
86  const uint s = sample ^ rv;
87 #else /* Use _OWEN_SHUFFLE_ for reordering. */
88  const uint s = nested_uniform_scramble(sample, rv);
89 #endif
90 
91  /* Based on the sample number a sample pattern is selected and offset by the dimension. */
92  const uint sample_set = s / NUM_PMJ_SAMPLES;
93  const uint d = (dimension + sample_set);
94  const uint dim = d % NUM_PMJ_PATTERNS;
95 
96  /* The PMJ sample sets contain a sample with (x,y) with NUM_PMJ_SAMPLES so for 1D
97  * the x part is used for even dims and the y for odd. */
98  int index = 2 * ((dim >> 1) * NUM_PMJ_SAMPLES + (s % NUM_PMJ_SAMPLES)) + (dim & 1);
99 
100  float fx = kernel_data_fetch(sample_pattern_lut, index);
101 
102 #ifndef _NO_CRANLEY_PATTERSON_ROTATION_
103  /* Use Cranley-Patterson rotation to displace the sample pattern. */
104 # ifdef _SIMPLE_HASH_
105  float dx = cmj_randfloat_simple(d, hash);
106 # else
107  float dx = cmj_randfloat(d, hash);
108 # endif
109  /* Jitter sample locations and map back into [0 1]. */
110  fx = fx + dx + jitter_x;
111  fx = fx - floorf(fx);
112 #else
113 # warning "Not using Cranley-Patterson Rotation."
114 #endif
115 
116  return fx;
117 }
118 
120  uint sample,
121  uint rng_hash,
122  uint dimension,
123  ccl_private float *x,
124  ccl_private float *y)
125 {
126  uint hash = rng_hash;
127  float jitter_x = 0.0f;
128  float jitter_y = 0.0f;
129  if (kernel_data.integrator.scrambling_distance < 1.0f) {
130  hash = kernel_data.integrator.seed;
131 
132  jitter_x = cmj_randfloat_simple_dist(
133  dimension, rng_hash, kernel_data.integrator.scrambling_distance);
134  jitter_y = cmj_randfloat_simple_dist(
135  dimension + 1, rng_hash, kernel_data.integrator.scrambling_distance);
136  }
137 
138  /* Perform a shuffle on the sample number to reorder the samples. */
139 #ifdef _SIMPLE_HASH_
140  const uint rv = cmj_hash_simple(dimension, hash);
141 #else /* Use a _REGULAR_HASH_. */
142  const uint rv = cmj_hash(dimension, hash);
143 #endif
144 #ifdef _XOR_SHUFFLE_
145 # warning "Using XOR shuffle."
146  const uint s = sample ^ rv;
147 #else /* Use _OWEN_SHUFFLE_ for reordering. */
148  const uint s = nested_uniform_scramble(sample, rv);
149 #endif
150 
151  /* Based on the sample number a sample pattern is selected and offset by the dimension. */
152  const uint sample_set = s / NUM_PMJ_SAMPLES;
153  const uint d = dimension + sample_set;
154  uint dim = d % NUM_PMJ_PATTERNS;
155  int index = 2 * (dim * NUM_PMJ_SAMPLES + (s % NUM_PMJ_SAMPLES));
156 
157  float fx = kernel_data_fetch(sample_pattern_lut, index);
158  float fy = kernel_data_fetch(sample_pattern_lut, index + 1);
159 
160 #ifndef _NO_CRANLEY_PATTERSON_ROTATION_
161  /* Use Cranley-Patterson rotation to displace the sample pattern. */
162 # ifdef _SIMPLE_HASH_
163  float dx = cmj_randfloat_simple(d, hash);
164  float dy = cmj_randfloat_simple(d + 1, hash);
165 # else
166  float dx = cmj_randfloat(d, hash);
167  float dy = cmj_randfloat(d + 1, hash);
168 # endif
169  /* Jitter sample locations and map back to the unit square [0 1]x[0 1]. */
170  float sx = fx + dx + jitter_x;
171  float sy = fy + dy + jitter_y;
172  sx = sx - floorf(sx);
173  sy = sy - floorf(sy);
174 #else
175 # warning "Not using Cranley Patterson Rotation."
176 #endif
177 
178  (*x) = sx;
179  (*y) = sy;
180 }
181 
typedef float(TangentPoint)[2]
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 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
static unsigned long seed
Definition: btSoftBody.h:39
#define ccl_device
Definition: cuda/compat.h:32
#define ccl_private
Definition: cuda/compat.h:48
#define ccl_device_inline
Definition: cuda/compat.h:34
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
#define kernel_data
const KernelGlobalsCPU *ccl_restrict KernelGlobals
#define kernel_data_fetch(name, index)
ccl_gpu_kernel_postfix ccl_global float int int sy
ccl_gpu_kernel_postfix ccl_global float int sx
ccl_device_inline float cmj_randfloat_simple_dist(uint i, uint p, float d)
ccl_device void pmj_sample_2D(KernelGlobals kg, uint sample, uint rng_hash, uint dimension, ccl_private float *x, ccl_private float *y)
CCL_NAMESPACE_BEGIN ccl_device_inline uint32_t laine_karras_permutation(uint32_t x, uint32_t seed)
ccl_device_inline float cmj_randfloat(uint i, uint p)
ccl_device_inline uint32_t nested_uniform_scramble(uint32_t x, uint32_t seed)
ccl_device_inline float cmj_randfloat_simple(uint i, uint p)
ccl_device float pmj_sample_1D(KernelGlobals kg, uint sample, uint rng_hash, uint dimension)
ccl_device_inline uint cmj_hash(uint i, uint p)
ccl_device_inline uint cmj_hash_simple(uint i, uint p)
#define NUM_PMJ_SAMPLES
#define NUM_PMJ_PATTERNS
#define floorf(x)
Definition: metal/compat.h:224
#define hash
Definition: noise.c:153
unsigned int uint32_t
Definition: stdint.h:80
ccl_device_inline uint32_t reverse_integer_bits(uint32_t x)
Definition: util/math.h:949