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

COM_RenderLayersProg.cc « operations « compositor « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d622e14b585bad7bfe14eb7c135fde2f8dd096d2 (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
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Copyright 2011, Blender Foundation.
 */

#include "COM_RenderLayersProg.h"

#include "COM_MetaData.h"

#include "BKE_image.h"
#include "BKE_scene.h"

#include "BLI_listbase.h"
#include "BLI_string.h"
#include "BLI_string_ref.hh"

#include "DNA_scene_types.h"

#include "RE_pipeline.h"
#include "RE_texture.h"

/* ******** Render Layers Base Prog ******** */

RenderLayersProg::RenderLayersProg(const char *passName, DataType type, int elementsize)
    : m_passName(passName)
{
  this->setScene(nullptr);
  this->m_inputBuffer = nullptr;
  this->m_elementsize = elementsize;
  this->m_rd = nullptr;

  this->addOutputSocket(type);
}

void RenderLayersProg::initExecution()
{
  Scene *scene = this->getScene();
  Render *re = (scene) ? RE_GetSceneRender(scene) : nullptr;
  RenderResult *rr = nullptr;

  if (re) {
    rr = RE_AcquireResultRead(re);
  }

  if (rr) {
    ViewLayer *view_layer = (ViewLayer *)BLI_findlink(&scene->view_layers, getLayerId());
    if (view_layer) {

      RenderLayer *rl = RE_GetRenderLayer(rr, view_layer->name);
      if (rl) {
        this->m_inputBuffer = RE_RenderLayerGetPass(
            rl, this->m_passName.c_str(), this->m_viewName);
      }
    }
  }
  if (re) {
    RE_ReleaseResult(re);
    re = nullptr;
  }
}

void RenderLayersProg::doInterpolation(float output[4], float x, float y, PixelSampler sampler)
{
  unsigned int offset;
  int width = this->getWidth(), height = this->getHeight();

  int ix = x, iy = y;
  if (ix < 0 || iy < 0 || ix >= width || iy >= height) {
    if (this->m_elementsize == 1) {
      output[0] = 0.0f;
    }
    else if (this->m_elementsize == 3) {
      zero_v3(output);
    }
    else {
      zero_v4(output);
    }
    return;
  }

  switch (sampler) {
    case COM_PS_NEAREST: {
      offset = (iy * width + ix) * this->m_elementsize;

      if (this->m_elementsize == 1) {
        output[0] = this->m_inputBuffer[offset];
      }
      else if (this->m_elementsize == 3) {
        copy_v3_v3(output, &this->m_inputBuffer[offset]);
      }
      else {
        copy_v4_v4(output, &this->m_inputBuffer[offset]);
      }
      break;
    }

    case COM_PS_BILINEAR:
      BLI_bilinear_interpolation_fl(
          this->m_inputBuffer, output, width, height, this->m_elementsize, x, y);
      break;

    case COM_PS_BICUBIC:
      BLI_bicubic_interpolation_fl(
          this->m_inputBuffer, output, width, height, this->m_elementsize, x, y);
      break;
  }
}

void RenderLayersProg::executePixelSampled(float output[4], float x, float y, PixelSampler sampler)
{
#if 0
  const RenderData *rd = this->m_rd;

  int dx = 0, dy = 0;

  if (rd->mode & R_BORDER && rd->mode & R_CROP) {
    /* see comment in executeRegion describing coordinate mapping,
     * here it simply goes other way around
     */
    int full_width = rd->xsch * rd->size / 100;
    int full_height = rd->ysch * rd->size / 100;

    dx = rd->border.xmin * full_width - (full_width - this->getWidth()) / 2.0f;
    dy = rd->border.ymin * full_height - (full_height - this->getHeight()) / 2.0f;
  }

  int ix = x - dx;
  int iy = y - dy;
#endif

#ifndef NDEBUG
  {
    const DataType data_type = this->getOutputSocket()->getDataType();
    int actual_element_size = this->m_elementsize;
    int expected_element_size;
    if (data_type == DataType::Value) {
      expected_element_size = 1;
    }
    else if (data_type == DataType::Vector) {
      expected_element_size = 3;
    }
    else if (data_type == DataType::Color) {
      expected_element_size = 4;
    }
    else {
      expected_element_size = 0;
      BLI_assert(!"Something horribly wrong just happened");
    }
    BLI_assert(expected_element_size == actual_element_size);
  }
#endif

  if (this->m_inputBuffer == nullptr) {
    int elemsize = this->m_elementsize;
    if (elemsize == 1) {
      output[0] = 0.0f;
    }
    else if (elemsize == 3) {
      zero_v3(output);
    }
    else {
      BLI_assert(elemsize == 4);
      zero_v4(output);
    }
  }
  else {
    doInterpolation(output, x, y, sampler);
  }
}

void RenderLayersProg::deinitExecution()
{
  this->m_inputBuffer = nullptr;
}

void RenderLayersProg::determineResolution(unsigned int resolution[2],
                                           unsigned int /*preferredResolution*/[2])
{
  Scene *sce = this->getScene();
  Render *re = (sce) ? RE_GetSceneRender(sce) : nullptr;
  RenderResult *rr = nullptr;

  resolution[0] = 0;
  resolution[1] = 0;

  if (re) {
    rr = RE_AcquireResultRead(re);
  }

  if (rr) {
    ViewLayer *view_layer = (ViewLayer *)BLI_findlink(&sce->view_layers, getLayerId());
    if (view_layer) {
      RenderLayer *rl = RE_GetRenderLayer(rr, view_layer->name);
      if (rl) {
        resolution[0] = rl->rectx;
        resolution[1] = rl->recty;
      }
    }
  }

  if (re) {
    RE_ReleaseResult(re);
  }
}

std::unique_ptr<MetaData> RenderLayersProg::getMetaData() const
{
  Scene *scene = this->getScene();
  Render *re = (scene) ? RE_GetSceneRender(scene) : nullptr;
  RenderResult *render_result = nullptr;
  MetaDataExtractCallbackData callback_data = {nullptr};

  if (re) {
    render_result = RE_AcquireResultRead(re);
  }

  if (render_result && render_result->stamp_data) {
    ViewLayer *view_layer = (ViewLayer *)BLI_findlink(&scene->view_layers, getLayerId());
    if (view_layer) {
      std::string full_layer_name = std::string(
                                        view_layer->name,
                                        BLI_strnlen(view_layer->name, sizeof(view_layer->name))) +
                                    "." + m_passName;
      blender::StringRef cryptomatte_layer_name =
          blender::bke::cryptomatte::BKE_cryptomatte_extract_layer_name(full_layer_name);
      callback_data.setCryptomatteKeys(cryptomatte_layer_name);

      BKE_stamp_info_callback(&callback_data,
                              render_result->stamp_data,
                              MetaDataExtractCallbackData::extract_cryptomatte_meta_data,
                              false);
    }
  }

  if (re) {
    RE_ReleaseResult(re);
    re = nullptr;
  }

  return std::move(callback_data.meta_data);
}

/* ******** Render Layers AO Operation ******** */
void RenderLayersAOOperation::executePixelSampled(float output[4],
                                                  float x,
                                                  float y,
                                                  PixelSampler sampler)
{
  float *inputBuffer = this->getInputBuffer();
  if (inputBuffer == nullptr) {
    zero_v3(output);
  }
  else {
    doInterpolation(output, x, y, sampler);
  }
  output[3] = 1.0f;
}

/* ******** Render Layers Alpha Operation ******** */
void RenderLayersAlphaProg::executePixelSampled(float output[4],
                                                float x,
                                                float y,
                                                PixelSampler sampler)
{
  float *inputBuffer = this->getInputBuffer();

  if (inputBuffer == nullptr) {
    output[0] = 0.0f;
  }
  else {
    float temp[4];
    doInterpolation(temp, x, y, sampler);
    output[0] = temp[3];
  }
}

/* ******** Render Layers Depth Operation ******** */
void RenderLayersDepthProg::executePixelSampled(float output[4],
                                                float x,
                                                float y,
                                                PixelSampler /*sampler*/)
{
  int ix = x;
  int iy = y;
  float *inputBuffer = this->getInputBuffer();

  if (inputBuffer == nullptr || ix < 0 || iy < 0 || ix >= (int)this->getWidth() ||
      iy >= (int)this->getHeight()) {
    output[0] = 10e10f;
  }
  else {
    unsigned int offset = (iy * this->getWidth() + ix);
    output[0] = inputBuffer[offset];
  }
}