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

jitter.cpp « scene « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e2162195ad89d7379716aa2c13d132d4a0d75698 (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * 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.
 */

/* This file is based on "Progressive Multi-Jittered Sample Sequences"
 * by Per Christensen, Andrew Kensler and Charlie Kilpatrick.
 * http://graphics.pixar.com/library/ProgressiveMultiJitteredSampling/paper.pdf
 *
 * Performance can be improved in the future by implementing the new
 * algorithm from Matt Pharr in  http://jcgt.org/published/0008/01/04/
 * "Efficient Generation of Points that Satisfy Two-Dimensional Elementary Intervals"
 */

#include "scene/jitter.h"

#include <math.h>
#include <vector>

CCL_NAMESPACE_BEGIN

static 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;
}

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

class PMJ_Generator {
 public:
  static void generate_2D(float2 points[], int size, int rng_seed_in)
  {
    PMJ_Generator g(rng_seed_in);
    points[0].x = g.rnd();
    points[0].y = g.rnd();
    int N = 1;
    while (N < size) {
      g.extend_sequence_even(points, N);
      g.extend_sequence_odd(points, 2 * N);
      N = 4 * N;
    }
  }

 protected:
  PMJ_Generator(int rnd_seed_in) : num_samples(1), rnd_index(2), rnd_seed(rnd_seed_in)
  {
  }

  float rnd()
  {
    return cmj_randfloat(++rnd_index, rnd_seed);
  }

  virtual void mark_occupied_strata(float2 points[], int N)
  {
    int NN = 2 * N;
    for (int s = 0; s < NN; ++s) {
      occupied1Dx[s] = occupied1Dy[s] = false;
    }
    for (int s = 0; s < N; ++s) {
      int xstratum = (int)(NN * points[s].x);
      int ystratum = (int)(NN * points[s].y);
      occupied1Dx[xstratum] = true;
      occupied1Dy[ystratum] = true;
    }
  }

  virtual void generate_sample_point(
      float2 points[], float i, float j, float xhalf, float yhalf, int n, int N)
  {
    int NN = 2 * N;
    float2 pt;
    int xstratum, ystratum;
    do {
      pt.x = (i + 0.5f * (xhalf + rnd())) / n;
      xstratum = (int)(NN * pt.x);
    } while (occupied1Dx[xstratum]);
    do {
      pt.y = (j + 0.5f * (yhalf + rnd())) / n;
      ystratum = (int)(NN * pt.y);
    } while (occupied1Dy[ystratum]);
    occupied1Dx[xstratum] = true;
    occupied1Dy[ystratum] = true;
    points[num_samples] = pt;
    ++num_samples;
  }

  void extend_sequence_even(float2 points[], int N)
  {
    int n = (int)sqrtf(N);
    occupied1Dx.resize(2 * N);
    occupied1Dy.resize(2 * N);
    mark_occupied_strata(points, N);
    for (int s = 0; s < N; ++s) {
      float2 oldpt = points[s];
      float i = floorf(n * oldpt.x);
      float j = floorf(n * oldpt.y);
      float xhalf = floorf(2.0f * (n * oldpt.x - i));
      float yhalf = floorf(2.0f * (n * oldpt.y - j));
      xhalf = 1.0f - xhalf;
      yhalf = 1.0f - yhalf;
      generate_sample_point(points, i, j, xhalf, yhalf, n, N);
    }
  }

  void extend_sequence_odd(float2 points[], int N)
  {
    int n = (int)sqrtf(N / 2);
    occupied1Dx.resize(2 * N);
    occupied1Dy.resize(2 * N);
    mark_occupied_strata(points, N);
    std::vector<float> xhalves(N / 2);
    std::vector<float> yhalves(N / 2);
    for (int s = 0; s < N / 2; ++s) {
      float2 oldpt = points[s];
      float i = floorf(n * oldpt.x);
      float j = floorf(n * oldpt.y);
      float xhalf = floorf(2.0f * (n * oldpt.x - i));
      float yhalf = floorf(2.0f * (n * oldpt.y - j));
      if (rnd() > 0.5f) {
        xhalf = 1.0f - xhalf;
      }
      else {
        yhalf = 1.0f - yhalf;
      }
      xhalves[s] = xhalf;
      yhalves[s] = yhalf;
      generate_sample_point(points, i, j, xhalf, yhalf, n, N);
    }
    for (int s = 0; s < N / 2; ++s) {
      float2 oldpt = points[s];
      float i = floorf(n * oldpt.x);
      float j = floorf(n * oldpt.y);
      float xhalf = 1.0f - xhalves[s];
      float yhalf = 1.0f - yhalves[s];
      generate_sample_point(points, i, j, xhalf, yhalf, n, N);
    }
  }

  std::vector<bool> occupied1Dx, occupied1Dy;
  int num_samples;
  int rnd_index, rnd_seed;
};

class PMJ02_Generator : public PMJ_Generator {
 protected:
  void generate_sample_point(
      float2 points[], float i, float j, float xhalf, float yhalf, int n, int N) override
  {
    int NN = 2 * N;
    float2 pt;
    do {
      pt.x = (i + 0.5f * (xhalf + rnd())) / n;
      pt.y = (j + 0.5f * (yhalf + rnd())) / n;
    } while (is_occupied(pt, NN));
    mark_occupied_strata1(pt, NN);
    points[num_samples] = pt;
    ++num_samples;
  }

  void mark_occupied_strata(float2 points[], int N) override
  {
    int NN = 2 * N;
    int num_shapes = (int)log2f(NN) + 1;
    occupiedStrata.resize(num_shapes);
    for (int shape = 0; shape < num_shapes; ++shape) {
      occupiedStrata[shape].resize(NN);
      for (int n = 0; n < NN; ++n) {
        occupiedStrata[shape][n] = false;
      }
    }
    for (int s = 0; s < N; ++s) {
      mark_occupied_strata1(points[s], NN);
    }
  }

  void mark_occupied_strata1(float2 pt, int NN)
  {
    int shape = 0;
    int xdivs = NN;
    int ydivs = 1;
    do {
      int xstratum = (int)(xdivs * pt.x);
      int ystratum = (int)(ydivs * pt.y);
      size_t index = ystratum * xdivs + xstratum;
      assert(index < NN);
      occupiedStrata[shape][index] = true;
      shape = shape + 1;
      xdivs = xdivs / 2;
      ydivs = ydivs * 2;
    } while (xdivs > 0);
  }

  bool is_occupied(float2 pt, int NN)
  {
    int shape = 0;
    int xdivs = NN;
    int ydivs = 1;
    do {
      int xstratum = (int)(xdivs * pt.x);
      int ystratum = (int)(ydivs * pt.y);
      size_t index = ystratum * xdivs + xstratum;
      assert(index < NN);
      if (occupiedStrata[shape][index]) {
        return true;
      }
      shape = shape + 1;
      xdivs = xdivs / 2;
      ydivs = ydivs * 2;
    } while (xdivs > 0);
    return false;
  }

 private:
  std::vector<std::vector<bool>> occupiedStrata;
};

static void shuffle(float2 points[], int size, int rng_seed)
{
  if (rng_seed == 0) {
    return;
  }

  constexpr int odd[8] = {0, 1, 4, 5, 10, 11, 14, 15};
  constexpr int even[8] = {2, 3, 6, 7, 8, 9, 12, 13};

  int rng_index = 0;
  for (int yy = 0; yy < size / 16; ++yy) {
    for (int xx = 0; xx < 8; ++xx) {
      int other = (int)(cmj_randfloat(++rng_index, rng_seed) * (8.0f - xx) + xx);
      float2 tmp = points[odd[other] + yy * 16];
      points[odd[other] + yy * 16] = points[odd[xx] + yy * 16];
      points[odd[xx] + yy * 16] = tmp;
    }
    for (int xx = 0; xx < 8; ++xx) {
      int other = (int)(cmj_randfloat(++rng_index, rng_seed) * (8.0f - xx) + xx);
      float2 tmp = points[even[other] + yy * 16];
      points[even[other] + yy * 16] = points[even[xx] + yy * 16];
      points[even[xx] + yy * 16] = tmp;
    }
  }
}

void progressive_multi_jitter_generate_2D(float2 points[], int size, int rng_seed)
{
  PMJ_Generator::generate_2D(points, size, rng_seed);
  shuffle(points, size, rng_seed);
}

void progressive_multi_jitter_02_generate_2D(float2 points[], int size, int rng_seed)
{
  PMJ02_Generator::generate_2D(points, size, rng_seed);
  shuffle(points, size, rng_seed);
}

CCL_NAMESPACE_END