Blender  V2.93
COM_ViewerOperation.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_ViewerOperation.h"
20 #include "BKE_image.h"
21 #include "BKE_scene.h"
22 #include "BLI_listbase.h"
23 #include "BLI_math_color.h"
24 #include "BLI_math_vector.h"
25 #include "BLI_utildefines.h"
26 #include "MEM_guardedalloc.h"
27 #include "PIL_time.h"
28 #include "WM_api.h"
29 #include "WM_types.h"
30 
31 #include "IMB_colormanagement.h"
32 #include "IMB_imbuf.h"
33 #include "IMB_imbuf_types.h"
34 
35 namespace blender::compositor {
36 
38 {
39  this->setImage(nullptr);
40  this->setImageUser(nullptr);
41  this->m_outputBuffer = nullptr;
42  this->m_depthBuffer = nullptr;
43  this->m_active = false;
44  this->m_doDepthBuffer = false;
45  this->m_viewSettings = nullptr;
46  this->m_displaySettings = nullptr;
47  this->m_useAlphaInput = false;
48 
52 
53  this->m_imageInput = nullptr;
54  this->m_alphaInput = nullptr;
55  this->m_depthInput = nullptr;
56  this->m_rd = nullptr;
57  this->m_viewName = nullptr;
58  flags.use_viewer_border = true;
60 }
61 
63 {
64  // When initializing the tree during initial load the width and height can be zero.
65  this->m_imageInput = getInputSocketReader(0);
66  this->m_alphaInput = getInputSocketReader(1);
67  this->m_depthInput = getInputSocketReader(2);
68  this->m_doDepthBuffer = (this->m_depthInput != nullptr);
69 
70  if (isActiveViewerOutput()) {
71  initImage();
72  }
73 }
74 
76 {
77  this->m_imageInput = nullptr;
78  this->m_alphaInput = nullptr;
79  this->m_depthInput = nullptr;
80  this->m_outputBuffer = nullptr;
81 }
82 
83 void ViewerOperation::executeRegion(rcti *rect, unsigned int /*tileNumber*/)
84 {
85  float *buffer = this->m_outputBuffer;
86  float *depthbuffer = this->m_depthBuffer;
87  if (!buffer) {
88  return;
89  }
90  const int x1 = rect->xmin;
91  const int y1 = rect->ymin;
92  const int x2 = rect->xmax;
93  const int y2 = rect->ymax;
94  const int offsetadd = (this->getWidth() - (x2 - x1));
95  const int offsetadd4 = offsetadd * 4;
96  int offset = (y1 * this->getWidth() + x1);
97  int offset4 = offset * 4;
98  float alpha[4], depth[4];
99  int x;
100  int y;
101  bool breaked = false;
102 
103  for (y = y1; y < y2 && (!breaked); y++) {
104  for (x = x1; x < x2; x++) {
105  this->m_imageInput->readSampled(&(buffer[offset4]), x, y, PixelSampler::Nearest);
106  if (this->m_useAlphaInput) {
107  this->m_alphaInput->readSampled(alpha, x, y, PixelSampler::Nearest);
108  buffer[offset4 + 3] = alpha[0];
109  }
110  this->m_depthInput->readSampled(depth, x, y, PixelSampler::Nearest);
111  depthbuffer[offset] = depth[0];
112 
113  offset++;
114  offset4 += 4;
115  }
116  if (isBraked()) {
117  breaked = true;
118  }
119  offset += offsetadd;
120  offset4 += offsetadd4;
121  }
122  updateImage(rect);
123 }
124 
125 void ViewerOperation::determineResolution(unsigned int resolution[2],
126  unsigned int /*preferredResolution*/[2])
127 {
128  const int sceneRenderWidth = this->m_rd->xsch * this->m_rd->size / 100;
129  const int sceneRenderHeight = this->m_rd->ysch * this->m_rd->size / 100;
130 
131  unsigned int localPrefRes[2] = {static_cast<unsigned int>(sceneRenderWidth),
132  static_cast<unsigned int>(sceneRenderHeight)};
133  NodeOperation::determineResolution(resolution, localPrefRes);
134 }
135 
136 void ViewerOperation::initImage()
137 {
138  Image *ima = this->m_image;
139  ImageUser iuser = *this->m_imageUser;
140  void *lock;
141  ImBuf *ibuf;
142 
143  /* make sure the image has the correct number of views */
144  if (ima && BKE_scene_multiview_is_render_view_first(this->m_rd, this->m_viewName)) {
145  BKE_image_ensure_viewer_views(this->m_rd, ima, this->m_imageUser);
146  }
147 
149 
150  /* local changes to the original ImageUser */
151  iuser.multi_index = BKE_scene_multiview_view_id_get(this->m_rd, this->m_viewName);
152  ibuf = BKE_image_acquire_ibuf(ima, &iuser, &lock);
153 
154  if (!ibuf) {
156  return;
157  }
158  if (ibuf->x != (int)getWidth() || ibuf->y != (int)getHeight()) {
159 
160  imb_freerectImBuf(ibuf);
163  ibuf->x = getWidth();
164  ibuf->y = getHeight();
165  /* zero size can happen if no image buffers exist to define a sensible resolution */
166  if (ibuf->x > 0 && ibuf->y > 0) {
167  imb_addrectfloatImBuf(ibuf);
168  }
169  ImageTile *tile = BKE_image_get_tile(ima, 0);
170  tile->ok = IMA_OK_LOADED;
171 
173  }
174 
175  if (m_doDepthBuffer) {
176  addzbuffloatImBuf(ibuf);
177  }
178 
179  /* now we combine the input with ibuf */
180  this->m_outputBuffer = ibuf->rect_float;
181 
182  /* needed for display buffer update */
183  this->m_ibuf = ibuf;
184 
185  if (m_doDepthBuffer) {
186  this->m_depthBuffer = ibuf->zbuf_float;
187  }
188 
189  BKE_image_release_ibuf(this->m_image, this->m_ibuf, lock);
190 
192 }
193 
194 void ViewerOperation::updateImage(rcti *rect)
195 {
197  this->m_outputBuffer,
198  nullptr,
199  getWidth(),
200  0,
201  0,
202  this->m_viewSettings,
203  this->m_displaySettings,
204  rect->xmin,
205  rect->ymin,
206  rect->xmax,
207  rect->ymax);
208  this->m_image->gpuflag |= IMA_GPU_REFRESH;
209  this->updateDraw();
210 }
211 
213 {
214  if (this->isActiveViewerOutput()) {
216  }
217 
219 }
220 
221 } // namespace blender::compositor
void BKE_image_release_ibuf(struct Image *ima, struct ImBuf *ibuf, void *lock)
Definition: image.c:5113
#define IMA_OK_LOADED
Definition: BKE_image.h:158
struct ImBuf * BKE_image_acquire_ibuf(struct Image *ima, struct ImageUser *iuser, void **r_lock)
Definition: image.c:5100
void BKE_image_ensure_viewer_views(const struct RenderData *rd, struct Image *ima, struct ImageUser *iuser)
bool BKE_scene_multiview_is_render_view_first(const struct RenderData *rd, const char *viewname)
int BKE_scene_multiview_view_id_get(const struct RenderData *rd, const char *viewname)
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_GPU_REFRESH
_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 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 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
void IMB_partial_display_buffer_update(struct ImBuf *ibuf, const float *linear_buffer, const unsigned char *byte_buffer, int stride, int offset_x, int offset_y, const struct ColorManagedViewSettings *view_settings, const struct ColorManagedDisplaySettings *display_settings, int xmin, int ymin, int xmax, int ymax)
void imb_freerectfloatImBuf(struct ImBuf *ibuf)
Definition: allocimbuf.c:97
void IMB_freezbuffloatImBuf(struct ImBuf *ibuf)
Definition: allocimbuf.c:186
void imb_freerectImBuf(struct ImBuf *ibuf)
Definition: allocimbuf.c:115
bool addzbuffloatImBuf(struct ImBuf *ibuf)
Definition: allocimbuf.c:289
bool imb_addrectfloatImBuf(struct ImBuf *ibuf)
Definition: allocimbuf.c:386
Contains defines and structs used throughout the imbuf module.
@ IB_DISPLAY_BUFFER_INVALID
Read Guarded memory(de)allocation.
Platform independent time functions.
short gpuflag
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)
bool isActiveViewerOutput() const override
is this operation the active viewer output user can select an ViewerNode to be active (the result of ...
void executeRegion(rcti *rect, unsigned int tileNumber) override
when a chunk is executed by a CPUDevice, this method is called
void determineResolution(unsigned int resolution[2], unsigned int preferredResolution[2]) override
determine the resolution of this node
void setImageUser(ImageUser *imageUser)
eCompositorPriority getRenderPriority() const override
get the render priority of this node.
static CCL_NAMESPACE_BEGIN const double alpha
eCompositorPriority
Possible priority settings.
Definition: COM_Enums.h:45
__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
float * zbuf_float
int userflags
float * rect_float
short multi_index
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