Blender  V3.3
COM_RenderLayersProg.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2011 Blender Foundation. */
3 
4 #include "COM_RenderLayersProg.h"
5 
6 #include "BKE_image.h"
7 
8 namespace blender::compositor {
9 
10 /* ******** Render Layers Base Prog ******** */
11 
12 RenderLayersProg::RenderLayersProg(const char *pass_name, DataType type, int elementsize)
13  : pass_name_(pass_name)
14 {
15  this->set_scene(nullptr);
16  input_buffer_ = nullptr;
17  elementsize_ = elementsize;
18  rd_ = nullptr;
19  layer_buffer_ = nullptr;
20 
21  this->add_output_socket(type);
22 }
23 
25 {
26  Scene *scene = this->get_scene();
27  Render *re = (scene) ? RE_GetSceneRender(scene) : nullptr;
28  RenderResult *rr = nullptr;
29 
30  if (re) {
31  rr = RE_AcquireResultRead(re);
32  }
33 
34  if (rr) {
36  if (view_layer) {
37 
38  RenderLayer *rl = RE_GetRenderLayer(rr, view_layer->name);
39  if (rl) {
41  if (input_buffer_) {
43  }
44  }
45  }
46  }
47  if (re) {
48  RE_ReleaseResult(re);
49  re = nullptr;
50  }
51 }
52 
54 {
55  unsigned int offset;
56  int width = this->get_width(), height = this->get_height();
57 
58  int ix = x, iy = y;
59  if (ix < 0 || iy < 0 || ix >= width || iy >= height) {
60  if (elementsize_ == 1) {
61  output[0] = 0.0f;
62  }
63  else if (elementsize_ == 3) {
64  zero_v3(output);
65  }
66  else {
67  zero_v4(output);
68  }
69  return;
70  }
71 
72  switch (sampler) {
73  case PixelSampler::Nearest: {
74  offset = (iy * width + ix) * elementsize_;
75 
76  if (elementsize_ == 1) {
78  }
79  else if (elementsize_ == 3) {
81  }
82  else {
84  }
85  break;
86  }
87 
90  break;
91 
94  break;
95  }
96 }
97 
99  float x,
100  float y,
102 {
103 #if 0
104  const RenderData *rd = rd_;
105 
106  int dx = 0, dy = 0;
107 
108  if (rd->mode & R_BORDER && rd->mode & R_CROP) {
109  /* see comment in execute_region describing coordinate mapping,
110  * here it simply goes other way around
111  */
112  int full_width, full_height;
113  BKE_render_resolution(rd, false, &full_width, &full_height);
114 
115  dx = rd->border.xmin * full_width - (full_width - this->get_width()) / 2.0f;
116  dy = rd->border.ymin * full_height - (full_height - this->get_height()) / 2.0f;
117  }
118 
119  int ix = x - dx;
120  int iy = y - dy;
121 #endif
122 
123 #ifndef NDEBUG
124  {
125  const DataType data_type = this->get_output_socket()->get_data_type();
126  int actual_element_size = elementsize_;
127  int expected_element_size;
128  if (data_type == DataType::Value) {
129  expected_element_size = 1;
130  }
131  else if (data_type == DataType::Vector) {
132  expected_element_size = 3;
133  }
134  else if (data_type == DataType::Color) {
135  expected_element_size = 4;
136  }
137  else {
138  expected_element_size = 0;
139  BLI_assert_msg(0, "Something horribly wrong just happened");
140  }
141  BLI_assert(expected_element_size == actual_element_size);
142  }
143 #endif
144 
145  if (input_buffer_ == nullptr) {
146  int elemsize = elementsize_;
147  if (elemsize == 1) {
148  output[0] = 0.0f;
149  }
150  else if (elemsize == 3) {
151  zero_v3(output);
152  }
153  else {
154  BLI_assert(elemsize == 4);
155  zero_v4(output);
156  }
157  }
158  else {
160  }
161 }
162 
164 {
165  input_buffer_ = nullptr;
166  if (layer_buffer_) {
167  delete layer_buffer_;
168  layer_buffer_ = nullptr;
169  }
170 }
171 
172 void RenderLayersProg::determine_canvas(const rcti &UNUSED(preferred_area), rcti &r_area)
173 {
174  Scene *sce = this->get_scene();
175  Render *re = (sce) ? RE_GetSceneRender(sce) : nullptr;
176  RenderResult *rr = nullptr;
177 
178  r_area = COM_AREA_NONE;
179 
180  if (re) {
181  rr = RE_AcquireResultRead(re);
182  }
183 
184  if (rr) {
185  ViewLayer *view_layer = (ViewLayer *)BLI_findlink(&sce->view_layers, get_layer_id());
186  if (view_layer) {
187  RenderLayer *rl = RE_GetRenderLayer(rr, view_layer->name);
188  if (rl) {
189  BLI_rcti_init(&r_area, 0, rl->rectx, 0, rl->recty);
190  }
191  }
192  }
193 
194  if (re) {
195  RE_ReleaseResult(re);
196  }
197 }
198 
199 std::unique_ptr<MetaData> RenderLayersProg::get_meta_data()
200 {
201  Scene *scene = this->get_scene();
202  Render *re = (scene) ? RE_GetSceneRender(scene) : nullptr;
203  RenderResult *render_result = nullptr;
204  MetaDataExtractCallbackData callback_data = {nullptr};
205 
206  if (re) {
207  render_result = RE_AcquireResultRead(re);
208  }
209 
210  if (render_result && render_result->stamp_data) {
212  if (view_layer) {
213  std::string full_layer_name = std::string(
214  view_layer->name,
215  BLI_strnlen(view_layer->name, sizeof(view_layer->name))) +
216  "." + pass_name_;
217  blender::StringRef cryptomatte_layer_name =
219  callback_data.set_cryptomatte_keys(cryptomatte_layer_name);
220 
221  BKE_stamp_info_callback(&callback_data,
222  render_result->stamp_data,
224  false);
225  }
226  }
227 
228  if (re) {
229  RE_ReleaseResult(re);
230  re = nullptr;
231  }
232 
233  return std::move(callback_data.meta_data);
234 }
235 
237  const rcti &area,
239 {
240  BLI_assert(output->get_num_channels() >= elementsize_);
241  if (layer_buffer_) {
242  output->copy_from(layer_buffer_, area, 0, elementsize_, 0);
243  }
244  else {
245  std::unique_ptr<float[]> zero_elem = std::make_unique<float[]>(elementsize_);
246  output->fill(area, 0, zero_elem.get(), elementsize_);
247  }
248 }
249 
250 /* ******** Render Layers AO Operation ******** */
251 
253  float x,
254  float y,
256 {
257  float *input_buffer = this->get_input_buffer();
258  if (input_buffer == nullptr) {
259  zero_v3(output);
260  }
261  else {
263  }
264  output[3] = 1.0f;
265 }
266 
268  const rcti &area,
270 {
271  BLI_assert(output->get_num_channels() == COM_DATA_TYPE_COLOR_CHANNELS);
273  if (layer_buffer_) {
275  }
276  else {
278  }
280 }
281 
282 /* ******** Render Layers Alpha Operation ******** */
283 
285  float x,
286  float y,
288 {
289  float *input_buffer = this->get_input_buffer();
290 
291  if (input_buffer == nullptr) {
292  output[0] = 0.0f;
293  }
294  else {
295  float temp[4];
296  do_interpolation(temp, x, y, sampler);
297  output[0] = temp[3];
298  }
299 }
300 
302  const rcti &area,
304 {
305  BLI_assert(output->get_num_channels() == COM_DATA_TYPE_VALUE_CHANNELS);
307  if (layer_buffer_) {
309  }
310  else {
311  output->fill(area, COM_VALUE_ZERO);
312  }
313 }
314 
315 /* ******** Render Layers Depth Operation ******** */
316 
318  float x,
319  float y,
320  PixelSampler /*sampler*/)
321 {
322  int ix = x;
323  int iy = y;
324  float *input_buffer = this->get_input_buffer();
325 
326  if (input_buffer == nullptr || ix < 0 || iy < 0 || ix >= (int)this->get_width() ||
327  iy >= (int)this->get_height()) {
328  output[0] = 10e10f;
329  }
330  else {
331  unsigned int offset = (iy * this->get_width() + ix);
332  output[0] = input_buffer[offset];
333  }
334 }
335 
337  const rcti &area,
339 {
340  BLI_assert(output->get_num_channels() == COM_DATA_TYPE_VALUE_CHANNELS);
342  if (layer_buffer_) {
343  output->copy_from(layer_buffer_, area);
344  }
345  else {
346  const float default_depth = 10e10f;
347  output->fill(area, &default_depth);
348  }
349 }
350 
351 } // namespace blender::compositor
void BKE_stamp_info_callback(void *data, struct StampData *stamp_data, StampCallback callback, bool noskip)
void BKE_render_resolution(const struct RenderData *r, const bool use_crop, int *r_width, int *r_height)
Definition: scene.cc:2960
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition: BLI_assert.h:53
void * BLI_findlink(const struct ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void BLI_bilinear_interpolation_fl(const float *buffer, float *output, int width, int height, int components, float u, float v)
Definition: math_interp.c:445
void BLI_bicubic_interpolation_fl(const float *buffer, float *output, int width, int height, int components, float u, float v)
Definition: math_interp.c:230
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void zero_v4(float r[4])
MINLINE void zero_v3(float r[3])
void BLI_rcti_init(struct rcti *rect, int xmin, int xmax, int ymin, int ymax)
Definition: rct.c:417
size_t BLI_strnlen(const char *str, size_t maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:899
#define UNUSED(x)
#define R_BORDER
#define R_CROP
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei height
_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
_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 type
_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 GLsizei width
a MemoryBuffer contains access to the data of a chunk
void add_output_socket(DataType datatype)
NodeOperationOutput * get_output_socket(unsigned int index=0)
void update_memory_buffer_partial(MemoryBuffer *output, const rcti &area, Span< MemoryBuffer * > inputs) override
void execute_pixel_sampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void update_memory_buffer_partial(MemoryBuffer *output, const rcti &area, Span< MemoryBuffer * > inputs) override
void execute_pixel_sampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void update_memory_buffer_partial(MemoryBuffer *output, const rcti &area, Span< MemoryBuffer * > inputs) override
void execute_pixel_sampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
RenderLayersProg(const char *pass_name, DataType type, int elementsize)
std::unique_ptr< MetaData > get_meta_data() override
const RenderData * rd_
render data used for active rendering
virtual void update_memory_buffer_partial(MemoryBuffer *output, const rcti &area, Span< MemoryBuffer * > inputs) override
void determine_canvas(const rcti &preferred_area, rcti &r_area) override
void execute_pixel_sampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void do_interpolation(float output[4], float x, float y, PixelSampler sampler)
Scene scene
depth_tx sampler(1, ImageType::FLOAT_2D, "combined_tx") .sampler(2
DataType
possible data types for sockets
Definition: COM_defines.h:30
@ Vector
Vector data type.
ccl_global KernelShaderEvalInput ccl_global float * output
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
StringRef BKE_cryptomatte_extract_layer_name(const StringRef render_pass_name)
Definition: cryptomatte.cc:475
static void area(int d1, int d2, int e1, int e2, float weights[2])
constexpr float COM_VECTOR_ZERO[3]
Definition: COM_defines.h:65
constexpr int COM_DATA_TYPE_VALUE_CHANNELS
Definition: COM_defines.h:60
constexpr int COM_DATA_TYPE_COLOR_CHANNELS
Definition: COM_defines.h:62
constexpr float COM_VALUE_ZERO[1]
Definition: COM_defines.h:67
constexpr rcti COM_AREA_NONE
Definition: COM_defines.h:112
constexpr float COM_VALUE_ONE[1]
Definition: COM_defines.h:68
constexpr int COM_DATA_TYPE_VECTOR_CHANNELS
Definition: COM_defines.h:61
static bNodeSocketTemplate inputs[]
RenderResult * RE_AcquireResultRead(Render *re)
Definition: pipeline.c:314
Render * RE_GetSceneRender(const Scene *scene)
Definition: pipeline.c:551
RenderLayer * RE_GetRenderLayer(RenderResult *rr, const char *name)
Definition: pipeline.c:244
void RE_ReleaseResult(Render *re)
Definition: pipeline.c:351
float * RE_RenderLayerGetPass(RenderLayer *rl, const char *name, const char *viewname)
Definition: pipeline.c:238
struct StampData * stamp_data
Definition: RE_pipeline.h:141
ListBase view_layers
char name[64]
void set_cryptomatte_keys(blender::StringRef cryptomatte_layer_name)
Definition: COM_MetaData.cc:62
static void extract_cryptomatte_meta_data(void *_data, const char *propname, char *propvalue, int UNUSED(len))
Definition: COM_MetaData.cc:72
float xmin
Definition: DNA_vec_types.h:69
float ymin
Definition: DNA_vec_types.h:70