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

buffers.cpp « session « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e060e0c682983a80d4fba8960c0091fd7ad51db7 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* SPDX-License-Identifier: Apache-2.0
 * Copyright 2011-2022 Blender Foundation */

#include <stdlib.h>

#include "device/device.h"
#include "session/buffers.h"

#include "util/foreach.h"
#include "util/hash.h"
#include "util/math.h"
#include "util/time.h"
#include "util/types.h"

CCL_NAMESPACE_BEGIN

/* --------------------------------------------------------------------
 * Convert part information to an index of `BufferParams::pass_offset_`.
 */

static int pass_type_mode_to_index(PassType pass_type, PassMode mode)
{
  int index = static_cast<int>(pass_type) * 2;

  if (mode == PassMode::DENOISED) {
    ++index;
  }

  return index;
}

static int pass_to_index(const BufferPass &pass)
{
  return pass_type_mode_to_index(pass.type, pass.mode);
}

/* --------------------------------------------------------------------
 * Buffer pass.
 */

NODE_DEFINE(BufferPass)
{
  NodeType *type = NodeType::add("buffer_pass", create);

  const NodeEnum *pass_type_enum = Pass::get_type_enum();
  const NodeEnum *pass_mode_enum = Pass::get_mode_enum();

  SOCKET_ENUM(type, "Type", *pass_type_enum, PASS_COMBINED);
  SOCKET_ENUM(mode, "Mode", *pass_mode_enum, static_cast<int>(PassMode::DENOISED));
  SOCKET_STRING(name, "Name", ustring());
  SOCKET_BOOLEAN(include_albedo, "Include Albedo", false);
  SOCKET_STRING(lightgroup, "Light Group", ustring());

  SOCKET_INT(offset, "Offset", -1);

  return type;
}

BufferPass::BufferPass() : Node(get_node_type())
{
}

BufferPass::BufferPass(const Pass *scene_pass)
    : Node(get_node_type()),
      type(scene_pass->get_type()),
      mode(scene_pass->get_mode()),
      name(scene_pass->get_name()),
      include_albedo(scene_pass->get_include_albedo()),
      lightgroup(scene_pass->get_lightgroup())
{
}

PassInfo BufferPass::get_info() const
{
  return Pass::get_info(type, include_albedo, !lightgroup.empty());
}

/* --------------------------------------------------------------------
 * Buffer Params.
 */

NODE_DEFINE(BufferParams)
{
  NodeType *type = NodeType::add("buffer_params", create);

  SOCKET_INT(width, "Width", 0);
  SOCKET_INT(height, "Height", 0);

  SOCKET_INT(window_x, "Window X", 0);
  SOCKET_INT(window_y, "Window Y", 0);
  SOCKET_INT(window_width, "Window Width", 0);
  SOCKET_INT(window_height, "Window Height", 0);

  SOCKET_INT(full_x, "Full X", 0);
  SOCKET_INT(full_y, "Full Y", 0);
  SOCKET_INT(full_width, "Full Width", 0);
  SOCKET_INT(full_height, "Full Height", 0);

  SOCKET_STRING(layer, "Layer", ustring());
  SOCKET_STRING(view, "View", ustring());
  SOCKET_INT(samples, "Samples", 0);
  SOCKET_FLOAT(exposure, "Exposure", 1.0f);
  SOCKET_BOOLEAN(use_approximate_shadow_catcher, "Use Approximate Shadow Catcher", false);
  SOCKET_BOOLEAN(use_transparent_background, "Transparent Background", false);

  /* Notes:
   *  - Skip passes since they do not follow typical container socket definition.
   *    Might look into covering those as a socket in the future.
   *
   *  - Skip offset, stride, and pass stride since those can be delivered from the passes and
   *    rest of the sockets. */

  return type;
}

BufferParams::BufferParams() : Node(get_node_type())
{
  reset_pass_offset();
}

void BufferParams::update_passes()
{
  update_offset_stride();
  reset_pass_offset();

  pass_stride = 0;
  for (const BufferPass &pass : passes) {
    if (pass.offset != PASS_UNUSED) {
      const int index = pass_to_index(pass);
      if (pass_offset_[index] == PASS_UNUSED) {
        pass_offset_[index] = pass_stride;
      }

      pass_stride += pass.get_info().num_components;
    }
  }
}

void BufferParams::update_passes(const vector<Pass *> &scene_passes)
{
  passes.clear();

  pass_stride = 0;
  for (const Pass *scene_pass : scene_passes) {
    BufferPass buffer_pass(scene_pass);

    if (scene_pass->is_written()) {
      buffer_pass.offset = pass_stride;
      pass_stride += scene_pass->get_info().num_components;
    }
    else {
      buffer_pass.offset = PASS_UNUSED;
    }

    passes.emplace_back(std::move(buffer_pass));
  }

  update_passes();
}

void BufferParams::reset_pass_offset()
{
  for (int i = 0; i < kNumPassOffsets; ++i) {
    pass_offset_[i] = PASS_UNUSED;
  }
}

int BufferParams::get_pass_offset(PassType pass_type, PassMode mode) const
{
  if (pass_type == PASS_NONE) {
    return PASS_UNUSED;
  }

  const int index = pass_type_mode_to_index(pass_type, mode);
  return pass_offset_[index];
}

const BufferPass *BufferParams::find_pass(string_view name) const
{
  for (const BufferPass &pass : passes) {
    if (pass.name == name) {
      return &pass;
    }
  }

  return nullptr;
}

const BufferPass *BufferParams::find_pass(PassType type, PassMode mode) const
{
  for (const BufferPass &pass : passes) {
    if (pass.type == type && pass.mode == mode) {
      return &pass;
    }
  }

  return nullptr;
}

const BufferPass *BufferParams::get_actual_display_pass(PassType type, PassMode mode) const
{
  const BufferPass *pass = find_pass(type, mode);
  return get_actual_display_pass(pass);
}

const BufferPass *BufferParams::get_actual_display_pass(const BufferPass *pass) const
{
  if (!pass) {
    return nullptr;
  }

  if (pass->type == PASS_COMBINED && pass->lightgroup.empty()) {
    const BufferPass *shadow_catcher_matte_pass = find_pass(PASS_SHADOW_CATCHER_MATTE, pass->mode);
    if (shadow_catcher_matte_pass) {
      pass = shadow_catcher_matte_pass;
    }
  }

  return pass;
}

void BufferParams::update_offset_stride()
{
  offset = -(full_x + full_y * width);
  stride = width;
}

bool BufferParams::modified(const BufferParams &other) const
{
  if (width != other.width || height != other.height) {
    return true;
  }

  if (full_x != other.full_x || full_y != other.full_y || full_width != other.full_width ||
      full_height != other.full_height) {
    return true;
  }

  if (window_x != other.window_x || window_y != other.window_y ||
      window_width != other.window_width || window_height != other.window_height) {
    return true;
  }

  if (offset != other.offset || stride != other.stride || pass_stride != other.pass_stride) {
    return true;
  }

  if (layer != other.layer || view != other.view) {
    return true;
  }

  if (exposure != other.exposure ||
      use_approximate_shadow_catcher != other.use_approximate_shadow_catcher ||
      use_transparent_background != other.use_transparent_background) {
    return true;
  }

  return !(passes == other.passes);
}

/* --------------------------------------------------------------------
 * Render Buffers.
 */

RenderBuffers::RenderBuffers(Device *device) : buffer(device, "RenderBuffers", MEM_READ_WRITE)
{
}

RenderBuffers::~RenderBuffers()
{
  buffer.free();
}

void RenderBuffers::reset(const BufferParams &params_)
{
  DCHECK(params_.pass_stride != -1);

  params = params_;

  /* re-allocate buffer */
  buffer.alloc(params.width * params.pass_stride, params.height);
}

void RenderBuffers::zero()
{
  buffer.zero_to_device();
}

bool RenderBuffers::copy_from_device()
{
  DCHECK(params.pass_stride != -1);

  if (!buffer.device_pointer)
    return false;

  buffer.copy_from_device(0, params.width * params.pass_stride, params.height);

  return true;
}

void RenderBuffers::copy_to_device()
{
  buffer.copy_to_device();
}

void render_buffers_host_copy_denoised(RenderBuffers *dst,
                                       const BufferParams &dst_params,
                                       const RenderBuffers *src,
                                       const BufferParams &src_params,
                                       const size_t src_offset)
{
  DCHECK_EQ(dst_params.width, src_params.width);
  /* TODO(sergey): More sanity checks to avoid buffer overrun. */

  /* Create a map of pass offsets to be copied.
   * Assume offsets are different to allow copying passes between buffers with different set of
   * passes. */

  struct {
    int dst_offset;
    int src_offset;
  } pass_offsets[PASS_NUM];

  int num_passes = 0;

  for (int i = 0; i < PASS_NUM; ++i) {
    const PassType pass_type = static_cast<PassType>(i);

    const int dst_pass_offset = dst_params.get_pass_offset(pass_type, PassMode::DENOISED);
    if (dst_pass_offset == PASS_UNUSED) {
      continue;
    }

    const int src_pass_offset = src_params.get_pass_offset(pass_type, PassMode::DENOISED);
    if (src_pass_offset == PASS_UNUSED) {
      continue;
    }

    pass_offsets[num_passes].dst_offset = dst_pass_offset;
    pass_offsets[num_passes].src_offset = src_pass_offset;
    ++num_passes;
  }

  /* Copy passes. */
  /* TODO(sergey): Make it more reusable, allowing implement copy of noisy passes. */

  const int64_t dst_width = dst_params.width;
  const int64_t dst_height = dst_params.height;
  const int64_t dst_pass_stride = dst_params.pass_stride;
  const int64_t dst_num_pixels = dst_width * dst_height;

  const int64_t src_pass_stride = src_params.pass_stride;
  const int64_t src_offset_in_floats = src_offset * src_pass_stride;

  const float *src_pixel = src->buffer.data() + src_offset_in_floats;
  float *dst_pixel = dst->buffer.data();

  for (int i = 0; i < dst_num_pixels;
       ++i, src_pixel += src_pass_stride, dst_pixel += dst_pass_stride) {
    for (int pass_offset_idx = 0; pass_offset_idx < num_passes; ++pass_offset_idx) {
      const int dst_pass_offset = pass_offsets[pass_offset_idx].dst_offset;
      const int src_pass_offset = pass_offsets[pass_offset_idx].src_offset;

      /* TODO(sergey): Support non-RGBA passes. */
      dst_pixel[dst_pass_offset + 0] = src_pixel[src_pass_offset + 0];
      dst_pixel[dst_pass_offset + 1] = src_pixel[src_pass_offset + 1];
      dst_pixel[dst_pass_offset + 2] = src_pixel[src_pass_offset + 2];
      dst_pixel[dst_pass_offset + 3] = src_pixel[src_pass_offset + 3];
    }
  }
}

CCL_NAMESPACE_END