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

jitter.h « sample « kernel « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b5cfa624406fd474a87611f13414443e40f186de (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
/* SPDX-License-Identifier: Apache-2.0
 * Copyright 2011-2022 Blender Foundation */

#pragma once
CCL_NAMESPACE_BEGIN

ccl_device_inline uint32_t laine_karras_permutation(uint32_t x, uint32_t seed)
{
  x += seed;
  x ^= (x * 0x6c50b47cu);
  x ^= x * 0xb82f1e52u;
  x ^= x * 0xc7afe638u;
  x ^= x * 0x8d22f6e6u;

  return x;
}

ccl_device_inline uint32_t nested_uniform_scramble(uint32_t x, uint32_t seed)
{
  x = reverse_integer_bits(x);
  x = laine_karras_permutation(x, seed);
  x = reverse_integer_bits(x);

  return x;
}

ccl_device_inline uint cmj_hash(uint i, uint p)
{
  i ^= p;
  i ^= i >> 17;
  i ^= i >> 10;
  i *= 0xb36534e5;
  i ^= i >> 12;
  i ^= i >> 21;
  i *= 0x93fc4795;
  i ^= 0xdf6e307f;
  i ^= i >> 17;
  i *= 1 | p >> 18;

  return i;
}

ccl_device_inline uint cmj_hash_simple(uint i, uint p)
{
  i = (i ^ 61) ^ p;
  i += i << 3;
  i ^= i >> 4;
  i *= 0x27d4eb2d;
  return i;
}

ccl_device_inline float cmj_randfloat(uint i, uint p)
{
  return cmj_hash(i, p) * (1.0f / 4294967808.0f);
}

ccl_device_inline float cmj_randfloat_simple(uint i, uint p)
{
  return cmj_hash_simple(i, p) * (1.0f / (float)0xFFFFFFFF);
}

ccl_device_inline float cmj_randfloat_simple_dist(uint i, uint p, float d)
{
  return cmj_hash_simple(i, p) * (d / (float)0xFFFFFFFF);
}

ccl_device float pmj_sample_1D(KernelGlobals kg, uint sample, uint rng_hash, uint dimension)
{
  uint hash = rng_hash;
  float jitter_x = 0.0f;
  if (kernel_data.integrator.scrambling_distance < 1.0f) {
    hash = kernel_data.integrator.seed;

    jitter_x = cmj_randfloat_simple_dist(
        dimension, rng_hash, kernel_data.integrator.scrambling_distance);
  }

  /* Perform Owen shuffle of the sample number to reorder the samples. */
#ifdef _SIMPLE_HASH_
  const uint rv = cmj_hash_simple(dimension, hash);
#else /* Use a _REGULAR_HASH_. */
  const uint rv = cmj_hash(dimension, hash);
#endif
#ifdef _XOR_SHUFFLE_
#  warning "Using XOR shuffle."
  const uint s = sample ^ rv;
#else /* Use _OWEN_SHUFFLE_ for reordering. */
  const uint s = nested_uniform_scramble(sample, rv);
#endif

  /* Based on the sample number a sample pattern is selected and offset by the dimension. */
  const uint sample_set = s / NUM_PMJ_SAMPLES;
  const uint d = (dimension + sample_set);
  const uint dim = d % NUM_PMJ_PATTERNS;

  /* The PMJ sample sets contain a sample with (x,y) with NUM_PMJ_SAMPLES so for 1D
   *  the x part is used for even dims and the y for odd. */
  int index = 2 * ((dim >> 1) * NUM_PMJ_SAMPLES + (s % NUM_PMJ_SAMPLES)) + (dim & 1);

  float fx = kernel_data_fetch(sample_pattern_lut, index);

#ifndef _NO_CRANLEY_PATTERSON_ROTATION_
  /* Use Cranley-Patterson rotation to displace the sample pattern. */
#  ifdef _SIMPLE_HASH_
  float dx = cmj_randfloat_simple(d, hash);
#  else
  float dx = cmj_randfloat(d, hash);
#  endif
  /* Jitter sample locations and map back into [0 1]. */
  fx = fx + dx + jitter_x;
  fx = fx - floorf(fx);
#else
#  warning "Not using Cranley-Patterson Rotation."
#endif

  return fx;
}

ccl_device void pmj_sample_2D(KernelGlobals kg,
                              uint sample,
                              uint rng_hash,
                              uint dimension,
                              ccl_private float *x,
                              ccl_private float *y)
{
  uint hash = rng_hash;
  float jitter_x = 0.0f;
  float jitter_y = 0.0f;
  if (kernel_data.integrator.scrambling_distance < 1.0f) {
    hash = kernel_data.integrator.seed;

    jitter_x = cmj_randfloat_simple_dist(
        dimension, rng_hash, kernel_data.integrator.scrambling_distance);
    jitter_y = cmj_randfloat_simple_dist(
        dimension + 1, rng_hash, kernel_data.integrator.scrambling_distance);
  }

  /* Perform a shuffle on the sample number to reorder the samples. */
#ifdef _SIMPLE_HASH_
  const uint rv = cmj_hash_simple(dimension, hash);
#else /* Use a _REGULAR_HASH_. */
  const uint rv = cmj_hash(dimension, hash);
#endif
#ifdef _XOR_SHUFFLE_
#  warning "Using XOR shuffle."
  const uint s = sample ^ rv;
#else /* Use _OWEN_SHUFFLE_ for reordering. */
  const uint s = nested_uniform_scramble(sample, rv);
#endif

  /* Based on the sample number a sample pattern is selected and offset by the dimension. */
  const uint sample_set = s / NUM_PMJ_SAMPLES;
  const uint d = dimension + sample_set;
  uint dim = d % NUM_PMJ_PATTERNS;
  int index = 2 * (dim * NUM_PMJ_SAMPLES + (s % NUM_PMJ_SAMPLES));

  float fx = kernel_data_fetch(sample_pattern_lut, index);
  float fy = kernel_data_fetch(sample_pattern_lut, index + 1);

#ifndef _NO_CRANLEY_PATTERSON_ROTATION_
  /* Use Cranley-Patterson rotation to displace the sample pattern. */
#  ifdef _SIMPLE_HASH_
  float dx = cmj_randfloat_simple(d, hash);
  float dy = cmj_randfloat_simple(d + 1, hash);
#  else
  float dx = cmj_randfloat(d, hash);
  float dy = cmj_randfloat(d + 1, hash);
#  endif
  /* Jitter sample locations and map back to the unit square [0 1]x[0 1]. */
  float sx = fx + dx + jitter_x;
  float sy = fy + dy + jitter_y;
  sx = sx - floorf(sx);
  sy = sy - floorf(sy);
#else
#  warning "Not using Cranley Patterson Rotation."
#endif

  (*x) = sx;
  (*y) = sy;
}

CCL_NAMESPACE_END