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
|
/*
* 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 2012, Blender Foundation.
*/
#include "COM_DisplaceSimpleOperation.h"
namespace blender::compositor {
DisplaceSimpleOperation::DisplaceSimpleOperation()
{
this->addInputSocket(DataType::Color);
this->addInputSocket(DataType::Vector);
this->addInputSocket(DataType::Value);
this->addInputSocket(DataType::Value);
this->addOutputSocket(DataType::Color);
m_inputColorProgram = nullptr;
m_inputVectorProgram = nullptr;
m_inputScaleXProgram = nullptr;
m_inputScaleYProgram = nullptr;
}
void DisplaceSimpleOperation::initExecution()
{
m_inputColorProgram = this->getInputSocketReader(0);
m_inputVectorProgram = this->getInputSocketReader(1);
m_inputScaleXProgram = this->getInputSocketReader(2);
m_inputScaleYProgram = this->getInputSocketReader(3);
m_width_x4 = this->getWidth() * 4;
m_height_x4 = this->getHeight() * 4;
}
/* minimum distance (in pixels) a pixel has to be displaced
* in order to take effect */
// #define DISPLACE_EPSILON 0.01f
void DisplaceSimpleOperation::executePixelSampled(float output[4],
float x,
float y,
PixelSampler sampler)
{
float inVector[4];
float inScale[4];
float p_dx, p_dy; /* main displacement in pixel space */
float u, v;
m_inputScaleXProgram->readSampled(inScale, x, y, sampler);
float xs = inScale[0];
m_inputScaleYProgram->readSampled(inScale, x, y, sampler);
float ys = inScale[0];
/* clamp x and y displacement to triple image resolution -
* to prevent hangs from huge values mistakenly plugged in eg. z buffers */
CLAMP(xs, -m_width_x4, m_width_x4);
CLAMP(ys, -m_height_x4, m_height_x4);
m_inputVectorProgram->readSampled(inVector, x, y, sampler);
p_dx = inVector[0] * xs;
p_dy = inVector[1] * ys;
/* displaced pixel in uv coords, for image sampling */
/* clamp nodes to avoid glitches */
u = x - p_dx + 0.5f;
v = y - p_dy + 0.5f;
CLAMP(u, 0.0f, this->getWidth() - 1.0f);
CLAMP(v, 0.0f, this->getHeight() - 1.0f);
m_inputColorProgram->readSampled(output, u, v, sampler);
}
void DisplaceSimpleOperation::deinitExecution()
{
m_inputColorProgram = nullptr;
m_inputVectorProgram = nullptr;
m_inputScaleXProgram = nullptr;
m_inputScaleYProgram = nullptr;
}
bool DisplaceSimpleOperation::determineDependingAreaOfInterest(rcti *input,
ReadBufferOperation *readOperation,
rcti *output)
{
rcti colorInput;
NodeOperation *operation = nullptr;
/* the vector buffer only needs a 2x2 buffer. The image needs whole buffer */
/* image */
operation = getInputOperation(0);
colorInput.xmax = operation->getWidth();
colorInput.xmin = 0;
colorInput.ymax = operation->getHeight();
colorInput.ymin = 0;
if (operation->determineDependingAreaOfInterest(&colorInput, readOperation, output)) {
return true;
}
/* vector */
if (operation->determineDependingAreaOfInterest(input, readOperation, output)) {
return true;
}
/* scale x */
operation = getInputOperation(2);
if (operation->determineDependingAreaOfInterest(input, readOperation, output)) {
return true;
}
/* scale y */
operation = getInputOperation(3);
if (operation->determineDependingAreaOfInterest(input, readOperation, output)) {
return true;
}
return false;
}
void DisplaceSimpleOperation::get_area_of_interest(const int input_idx,
const rcti &output_area,
rcti &r_input_area)
{
switch (input_idx) {
case 0: {
r_input_area = get_input_operation(input_idx)->get_canvas();
break;
}
default: {
r_input_area = output_area;
break;
}
}
}
void DisplaceSimpleOperation::update_memory_buffer_partial(MemoryBuffer *output,
const rcti &area,
Span<MemoryBuffer *> inputs)
{
const float width = this->getWidth();
const float height = this->getHeight();
const MemoryBuffer *input_color = inputs[0];
for (BuffersIterator<float> it = output->iterate_with(inputs.drop_front(1), area); !it.is_end();
++it) {
float scale_x = *it.in(1);
float scale_y = *it.in(2);
/* Clamp x and y displacement to triple image resolution -
* to prevent hangs from huge values mistakenly plugged in eg. z buffers. */
CLAMP(scale_x, -m_width_x4, m_width_x4);
CLAMP(scale_y, -m_height_x4, m_height_x4);
/* Main displacement in pixel space. */
const float *vector = it.in(0);
const float p_dx = vector[0] * scale_x;
const float p_dy = vector[1] * scale_y;
/* Displaced pixel in uv coords, for image sampling. */
/* Clamp nodes to avoid glitches. */
float u = it.x - p_dx + 0.5f;
float v = it.y - p_dy + 0.5f;
CLAMP(u, 0.0f, width - 1.0f);
CLAMP(v, 0.0f, height - 1.0f);
input_color->read_elem_checked(u, v, it.out);
}
}
} // namespace blender::compositor
|