Blender  V2.93
COM_CompositorOperation.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 
20 #include "BKE_global.h"
21 #include "BKE_image.h"
22 #include "BLI_listbase.h"
23 #include "MEM_guardedalloc.h"
24 
25 #include "BLI_threads.h"
26 
27 #include "RE_pipeline.h"
28 #include "RE_texture.h"
29 
30 #include "render_types.h"
31 
32 #include "PIL_time.h"
33 
34 namespace blender::compositor {
35 
37 {
41 
42  this->setRenderData(nullptr);
43  this->m_outputBuffer = nullptr;
44  this->m_depthBuffer = nullptr;
45  this->m_imageInput = nullptr;
46  this->m_alphaInput = nullptr;
47  this->m_depthInput = nullptr;
48 
49  this->m_useAlphaInput = false;
50  this->m_active = false;
51 
52  this->m_scene = nullptr;
53  this->m_sceneName[0] = '\0';
54  this->m_viewName = nullptr;
55 
56  flags.use_render_border = true;
57 }
58 
60 {
61  if (!this->m_active) {
62  return;
63  }
64 
65  // When initializing the tree during initial load the width and height can be zero.
66  this->m_imageInput = getInputSocketReader(0);
67  this->m_alphaInput = getInputSocketReader(1);
68  this->m_depthInput = getInputSocketReader(2);
69  if (this->getWidth() * this->getHeight() != 0) {
70  this->m_outputBuffer = (float *)MEM_callocN(
71  sizeof(float[4]) * this->getWidth() * this->getHeight(), "CompositorOperation");
72  }
73  if (this->m_depthInput != nullptr) {
74  this->m_depthBuffer = (float *)MEM_callocN(
75  sizeof(float) * this->getWidth() * this->getHeight(), "CompositorOperation");
76  }
77 }
78 
80 {
81  if (!this->m_active) {
82  return;
83  }
84 
85  if (!isBraked()) {
86  Render *re = RE_GetSceneRender(this->m_scene);
88 
89  if (rr) {
90  RenderView *rv = RE_RenderViewGetByName(rr, this->m_viewName);
91 
92  if (rv->rectf != nullptr) {
93  MEM_freeN(rv->rectf);
94  }
95  rv->rectf = this->m_outputBuffer;
96  if (rv->rectz != nullptr) {
97  MEM_freeN(rv->rectz);
98  }
99  rv->rectz = this->m_depthBuffer;
100  rr->have_combined = true;
101  }
102  else {
103  if (this->m_outputBuffer) {
104  MEM_freeN(this->m_outputBuffer);
105  }
106  if (this->m_depthBuffer) {
107  MEM_freeN(this->m_depthBuffer);
108  }
109  }
110 
111  if (re) {
112  RE_ReleaseResult(re);
113  re = nullptr;
114  }
115 
117  BKE_image_signal(G.main,
118  BKE_image_ensure_viewer(G.main, IMA_TYPE_R_RESULT, "Render Result"),
119  nullptr,
122  }
123  else {
124  if (this->m_outputBuffer) {
125  MEM_freeN(this->m_outputBuffer);
126  }
127  if (this->m_depthBuffer) {
128  MEM_freeN(this->m_depthBuffer);
129  }
130  }
131 
132  this->m_outputBuffer = nullptr;
133  this->m_depthBuffer = nullptr;
134  this->m_imageInput = nullptr;
135  this->m_alphaInput = nullptr;
136  this->m_depthInput = nullptr;
137 }
138 
139 void CompositorOperation::executeRegion(rcti *rect, unsigned int /*tileNumber*/)
140 {
141  float color[8]; // 7 is enough
142  float *buffer = this->m_outputBuffer;
143  float *zbuffer = this->m_depthBuffer;
144 
145  if (!buffer) {
146  return;
147  }
148  int x1 = rect->xmin;
149  int y1 = rect->ymin;
150  int x2 = rect->xmax;
151  int y2 = rect->ymax;
152  int offset = (y1 * this->getWidth() + x1);
153  int add = (this->getWidth() - (x2 - x1));
154  int offset4 = offset * COM_DATA_TYPE_COLOR_CHANNELS;
155  int x;
156  int y;
157  bool breaked = false;
158  int dx = 0, dy = 0;
159 
160 #if 0
161  const RenderData *rd = this->m_rd;
162 
163  if (rd->mode & R_BORDER && rd->mode & R_CROP) {
191  int full_width = rd->xsch * rd->size / 100;
192  int full_height = rd->ysch * rd->size / 100;
193 
194  dx = rd->border.xmin * full_width - (full_width - this->getWidth()) / 2.0f;
195  dy = rd->border.ymin * full_height - (full_height - this->getHeight()) / 2.0f;
196  }
197 #endif
198 
199  for (y = y1; y < y2 && (!breaked); y++) {
200  for (x = x1; x < x2 && (!breaked); x++) {
201  int input_x = x + dx, input_y = y + dy;
202 
203  this->m_imageInput->readSampled(color, input_x, input_y, PixelSampler::Nearest);
204  if (this->m_useAlphaInput) {
205  this->m_alphaInput->readSampled(&(color[3]), input_x, input_y, PixelSampler::Nearest);
206  }
207 
208  copy_v4_v4(buffer + offset4, color);
209 
210  this->m_depthInput->readSampled(color, input_x, input_y, PixelSampler::Nearest);
211  zbuffer[offset] = color[0];
212  offset4 += COM_DATA_TYPE_COLOR_CHANNELS;
213  offset++;
214  if (isBraked()) {
215  breaked = true;
216  }
217  }
218  offset += add;
219  offset4 += add * COM_DATA_TYPE_COLOR_CHANNELS;
220  }
221 }
222 
223 void CompositorOperation::determineResolution(unsigned int resolution[2],
224  unsigned int preferredResolution[2])
225 {
226  int width = this->m_rd->xsch * this->m_rd->size / 100;
227  int height = this->m_rd->ysch * this->m_rd->size / 100;
228 
229  // check actual render resolution with cropping it may differ with cropped border.rendering
230  // FIX for: [31777] Border Crop gives black (easy)
231  Render *re = RE_GetSceneRender(this->m_scene);
232  if (re) {
234  if (rr) {
235  width = rr->rectx;
236  height = rr->recty;
237  }
238  RE_ReleaseResult(re);
239  }
240 
241  preferredResolution[0] = width;
242  preferredResolution[1] = height;
243 
244  NodeOperation::determineResolution(resolution, preferredResolution);
245 
246  resolution[0] = width;
247  resolution[1] = height;
248 }
249 
250 } // namespace blender::compositor
struct Image * BKE_image_ensure_viewer(struct Main *bmain, int type, const char *name)
Definition: image.c:3162
#define IMA_SIGNAL_FREE
Definition: BKE_image.h:163
void BKE_image_signal(struct Main *bmain, struct Image *ima, struct ImageUser *iuser, int signal)
Definition: image.c:3499
MINLINE void copy_v4_v4(float r[4], const float a[4])
void BLI_thread_unlock(int type)
Definition: threads.cc:389
void BLI_thread_lock(int type)
Definition: threads.cc:384
@ LOCK_DRAW_IMAGE
Definition: BLI_threads.h:68
@ IMA_TYPE_R_RESULT
#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 GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble y1
_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 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 GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble x2
_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
Read Guarded memory(de)allocation.
Platform independent time functions.
void determineResolution(unsigned int resolution[2], unsigned int preferredResolution[2]) override
determine the resolution of this node
void executeRegion(rcti *rect, unsigned int tileNumber) override
when a chunk is executed by a CPUDevice, this method is called
void readSampled(float result[4], float x, float y, PixelSampler sampler)
void addInputSocket(DataType datatype, ResizeMode resize_mode=ResizeMode::Center)
virtual void determineResolution(unsigned int resolution[2], unsigned int preferredResolution[2])
determine the resolution of this node
SocketReader * getInputSocketReader(unsigned int inputSocketindex)
__kernel void ccl_constant KernelData ccl_global void ccl_global char ccl_global int ccl_global char ccl_global unsigned int ccl_global float * buffer
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
static void add(GHash *messages, MemArena *memarena, const Message *msg)
Definition: msgfmt.c:268
constexpr int COM_DATA_TYPE_COLOR_CHANNELS
Definition: COM_defines.h:53
RenderResult * RE_AcquireResultRead(Render *re)
Definition: pipeline.c:343
Render * RE_GetSceneRender(const Scene *scene)
Definition: pipeline.c:591
void RE_ReleaseResult(Render *re)
Definition: pipeline.c:379
RenderResult * RE_AcquireResultWrite(Render *re)
Definition: pipeline.c:353
RenderView * RE_RenderViewGetByName(RenderResult *rr, const char *viewname)
float * rectf
Definition: RE_pipeline.h:69
float * rectz
Definition: RE_pipeline.h:71
float xmin
Definition: DNA_vec_types.h:85
float ymin
Definition: DNA_vec_types.h:86
int ymin
Definition: DNA_vec_types.h:80
int ymax
Definition: DNA_vec_types.h:80
int xmin
Definition: DNA_vec_types.h:79
int xmax
Definition: DNA_vec_types.h:79
#define G(x, y, z)