Blender  V2.93
COM_RenderLayersProg.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2011, Blender Foundation.
17  */
18 
19 #include "COM_RenderLayersProg.h"
20 
21 #include "COM_MetaData.h"
22 
23 #include "BKE_image.h"
24 #include "BKE_scene.h"
25 
26 #include "BLI_listbase.h"
27 #include "BLI_string.h"
28 #include "BLI_string_ref.hh"
29 
30 #include "DNA_scene_types.h"
31 
32 #include "RE_pipeline.h"
33 #include "RE_texture.h"
34 
35 namespace blender::compositor {
36 
37 /* ******** Render Layers Base Prog ******** */
38 
39 RenderLayersProg::RenderLayersProg(const char *passName, DataType type, int elementsize)
40  : m_passName(passName)
41 {
42  this->setScene(nullptr);
43  this->m_inputBuffer = nullptr;
44  this->m_elementsize = elementsize;
45  this->m_rd = nullptr;
46 
47  this->addOutputSocket(type);
48 }
49 
51 {
52  Scene *scene = this->getScene();
53  Render *re = (scene) ? RE_GetSceneRender(scene) : nullptr;
54  RenderResult *rr = nullptr;
55 
56  if (re) {
57  rr = RE_AcquireResultRead(re);
58  }
59 
60  if (rr) {
62  if (view_layer) {
63 
64  RenderLayer *rl = RE_GetRenderLayer(rr, view_layer->name);
65  if (rl) {
67  rl, this->m_passName.c_str(), this->m_viewName);
68  }
69  }
70  }
71  if (re) {
72  RE_ReleaseResult(re);
73  re = nullptr;
74  }
75 }
76 
77 void RenderLayersProg::doInterpolation(float output[4], float x, float y, PixelSampler sampler)
78 {
79  unsigned int offset;
80  int width = this->getWidth(), height = this->getHeight();
81 
82  int ix = x, iy = y;
83  if (ix < 0 || iy < 0 || ix >= width || iy >= height) {
84  if (this->m_elementsize == 1) {
85  output[0] = 0.0f;
86  }
87  else if (this->m_elementsize == 3) {
88  zero_v3(output);
89  }
90  else {
91  zero_v4(output);
92  }
93  return;
94  }
95 
96  switch (sampler) {
97  case PixelSampler::Nearest: {
98  offset = (iy * width + ix) * this->m_elementsize;
99 
100  if (this->m_elementsize == 1) {
101  output[0] = this->m_inputBuffer[offset];
102  }
103  else if (this->m_elementsize == 3) {
104  copy_v3_v3(output, &this->m_inputBuffer[offset]);
105  }
106  else {
107  copy_v4_v4(output, &this->m_inputBuffer[offset]);
108  }
109  break;
110  }
111 
114  this->m_inputBuffer, output, width, height, this->m_elementsize, x, y);
115  break;
116 
119  this->m_inputBuffer, output, width, height, this->m_elementsize, x, y);
120  break;
121  }
122 }
123 
124 void RenderLayersProg::executePixelSampled(float output[4], float x, float y, PixelSampler sampler)
125 {
126 #if 0
127  const RenderData *rd = this->m_rd;
128 
129  int dx = 0, dy = 0;
130 
131  if (rd->mode & R_BORDER && rd->mode & R_CROP) {
132  /* see comment in executeRegion describing coordinate mapping,
133  * here it simply goes other way around
134  */
135  int full_width = rd->xsch * rd->size / 100;
136  int full_height = rd->ysch * rd->size / 100;
137 
138  dx = rd->border.xmin * full_width - (full_width - this->getWidth()) / 2.0f;
139  dy = rd->border.ymin * full_height - (full_height - this->getHeight()) / 2.0f;
140  }
141 
142  int ix = x - dx;
143  int iy = y - dy;
144 #endif
145 
146 #ifndef NDEBUG
147  {
148  const DataType data_type = this->getOutputSocket()->getDataType();
149  int actual_element_size = this->m_elementsize;
150  int expected_element_size;
151  if (data_type == DataType::Value) {
152  expected_element_size = 1;
153  }
154  else if (data_type == DataType::Vector) {
155  expected_element_size = 3;
156  }
157  else if (data_type == DataType::Color) {
158  expected_element_size = 4;
159  }
160  else {
161  expected_element_size = 0;
162  BLI_assert(!"Something horribly wrong just happened");
163  }
164  BLI_assert(expected_element_size == actual_element_size);
165  }
166 #endif
167 
168  if (this->m_inputBuffer == nullptr) {
169  int elemsize = this->m_elementsize;
170  if (elemsize == 1) {
171  output[0] = 0.0f;
172  }
173  else if (elemsize == 3) {
174  zero_v3(output);
175  }
176  else {
177  BLI_assert(elemsize == 4);
178  zero_v4(output);
179  }
180  }
181  else {
182  doInterpolation(output, x, y, sampler);
183  }
184 }
185 
187 {
188  this->m_inputBuffer = nullptr;
189 }
190 
191 void RenderLayersProg::determineResolution(unsigned int resolution[2],
192  unsigned int /*preferredResolution*/[2])
193 {
194  Scene *sce = this->getScene();
195  Render *re = (sce) ? RE_GetSceneRender(sce) : nullptr;
196  RenderResult *rr = nullptr;
197 
198  resolution[0] = 0;
199  resolution[1] = 0;
200 
201  if (re) {
202  rr = RE_AcquireResultRead(re);
203  }
204 
205  if (rr) {
206  ViewLayer *view_layer = (ViewLayer *)BLI_findlink(&sce->view_layers, getLayerId());
207  if (view_layer) {
208  RenderLayer *rl = RE_GetRenderLayer(rr, view_layer->name);
209  if (rl) {
210  resolution[0] = rl->rectx;
211  resolution[1] = rl->recty;
212  }
213  }
214  }
215 
216  if (re) {
217  RE_ReleaseResult(re);
218  }
219 }
220 
221 std::unique_ptr<MetaData> RenderLayersProg::getMetaData()
222 {
223  Scene *scene = this->getScene();
224  Render *re = (scene) ? RE_GetSceneRender(scene) : nullptr;
225  RenderResult *render_result = nullptr;
226  MetaDataExtractCallbackData callback_data = {nullptr};
227 
228  if (re) {
229  render_result = RE_AcquireResultRead(re);
230  }
231 
232  if (render_result && render_result->stamp_data) {
234  if (view_layer) {
235  std::string full_layer_name = std::string(
236  view_layer->name,
237  BLI_strnlen(view_layer->name, sizeof(view_layer->name))) +
238  "." + m_passName;
239  blender::StringRef cryptomatte_layer_name =
241  callback_data.setCryptomatteKeys(cryptomatte_layer_name);
242 
243  BKE_stamp_info_callback(&callback_data,
244  render_result->stamp_data,
246  false);
247  }
248  }
249 
250  if (re) {
251  RE_ReleaseResult(re);
252  re = nullptr;
253  }
254 
255  return std::move(callback_data.meta_data);
256 }
257 
258 /* ******** Render Layers AO Operation ******** */
260  float x,
261  float y,
262  PixelSampler sampler)
263 {
264  float *inputBuffer = this->getInputBuffer();
265  if (inputBuffer == nullptr) {
266  zero_v3(output);
267  }
268  else {
269  doInterpolation(output, x, y, sampler);
270  }
271  output[3] = 1.0f;
272 }
273 
274 /* ******** Render Layers Alpha Operation ******** */
276  float x,
277  float y,
278  PixelSampler sampler)
279 {
280  float *inputBuffer = this->getInputBuffer();
281 
282  if (inputBuffer == nullptr) {
283  output[0] = 0.0f;
284  }
285  else {
286  float temp[4];
287  doInterpolation(temp, x, y, sampler);
288  output[0] = temp[3];
289  }
290 }
291 
292 /* ******** Render Layers Depth Operation ******** */
294  float x,
295  float y,
296  PixelSampler /*sampler*/)
297 {
298  int ix = x;
299  int iy = y;
300  float *inputBuffer = this->getInputBuffer();
301 
302  if (inputBuffer == nullptr || ix < 0 || iy < 0 || ix >= (int)this->getWidth() ||
303  iy >= (int)this->getHeight()) {
304  output[0] = 10e10f;
305  }
306  else {
307  unsigned int offset = (iy * this->getWidth() + ix);
308  output[0] = inputBuffer[offset];
309  }
310 }
311 
312 } // namespace blender::compositor
void BKE_stamp_info_callback(void *data, struct StampData *stamp_data, StampCallback callback, bool noskip)
Definition: image.c:2686
#define BLI_assert(a)
Definition: BLI_assert.h:58
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:461
void BLI_bicubic_interpolation_fl(const float *buffer, float *output, int width, int height, int components, float u, float v)
Definition: math_interp.c:246
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])
size_t BLI_strnlen(const char *str, const size_t maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:878
#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 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
_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 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
#define output
void addOutputSocket(DataType datatype)
NodeOperationOutput * getOutputSocket(unsigned int index=0)
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
const RenderData * m_rd
render data used for active rendering
void doInterpolation(float output[4], float x, float y, PixelSampler sampler)
void determineResolution(unsigned int resolution[2], unsigned int preferredResolution[2]) override
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
RenderLayersProg(const char *passName, DataType type, int elementsize)
std::unique_ptr< MetaData > getMetaData() override
Scene scene
DataType
possible data types for sockets
Definition: COM_defines.h:27
@ Vector
Vector data type.
StringRef BKE_cryptomatte_extract_layer_name(const StringRef render_pass_name)
Definition: cryptomatte.cc:496
RenderResult * RE_AcquireResultRead(Render *re)
Definition: pipeline.c:343
float * RE_RenderLayerGetPass(volatile RenderLayer *rl, const char *name, const char *viewname)
Definition: pipeline.c:270
Render * RE_GetSceneRender(const Scene *scene)
Definition: pipeline.c:591
RenderLayer * RE_GetRenderLayer(RenderResult *rr, const char *name)
Definition: pipeline.c:276
void RE_ReleaseResult(Render *re)
Definition: pipeline.c:379
struct StampData * stamp_data
Definition: RE_pipeline.h:157
ListBase view_layers
char name[64]
void setCryptomatteKeys(blender::StringRef cryptomatte_layer_name)
Definition: COM_MetaData.cc:82
static void extract_cryptomatte_meta_data(void *_data, const char *propname, char *propvalue, int UNUSED(len))
Definition: COM_MetaData.cc:92
float xmin
Definition: DNA_vec_types.h:85
float ymin
Definition: DNA_vec_types.h:86