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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
/*
* 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 2011, Blender Foundation.
*/
#include "COM_ZCombineOperation.h"
namespace blender::compositor {
ZCombineOperation::ZCombineOperation()
{
this->addInputSocket(DataType::Color);
this->addInputSocket(DataType::Value);
this->addInputSocket(DataType::Color);
this->addInputSocket(DataType::Value);
this->addOutputSocket(DataType::Color);
m_image1Reader = nullptr;
m_depth1Reader = nullptr;
m_image2Reader = nullptr;
m_depth2Reader = nullptr;
this->flags.can_be_constant = true;
}
void ZCombineOperation::initExecution()
{
m_image1Reader = this->getInputSocketReader(0);
m_depth1Reader = this->getInputSocketReader(1);
m_image2Reader = this->getInputSocketReader(2);
m_depth2Reader = this->getInputSocketReader(3);
}
void ZCombineOperation::executePixelSampled(float output[4],
float x,
float y,
PixelSampler sampler)
{
float depth1[4];
float depth2[4];
m_depth1Reader->readSampled(depth1, x, y, sampler);
m_depth2Reader->readSampled(depth2, x, y, sampler);
if (depth1[0] < depth2[0]) {
m_image1Reader->readSampled(output, x, y, sampler);
}
else {
m_image2Reader->readSampled(output, x, y, sampler);
}
}
void ZCombineOperation::update_memory_buffer_partial(MemoryBuffer *output,
const rcti &area,
Span<MemoryBuffer *> inputs)
{
for (BuffersIterator<float> it = output->iterate_with(inputs, area); !it.is_end(); ++it) {
const float depth1 = *it.in(1);
const float depth2 = *it.in(3);
const float *color = (depth1 < depth2) ? it.in(0) : it.in(2);
copy_v4_v4(it.out, color);
}
}
void ZCombineAlphaOperation::executePixelSampled(float output[4],
float x,
float y,
PixelSampler sampler)
{
float depth1[4];
float depth2[4];
float color1[4];
float color2[4];
m_depth1Reader->readSampled(depth1, x, y, sampler);
m_depth2Reader->readSampled(depth2, x, y, sampler);
if (depth1[0] <= depth2[0]) {
m_image1Reader->readSampled(color1, x, y, sampler);
m_image2Reader->readSampled(color2, x, y, sampler);
}
else {
m_image1Reader->readSampled(color2, x, y, sampler);
m_image2Reader->readSampled(color1, x, y, sampler);
}
float fac = color1[3];
float ifac = 1.0f - fac;
output[0] = fac * color1[0] + ifac * color2[0];
output[1] = fac * color1[1] + ifac * color2[1];
output[2] = fac * color1[2] + ifac * color2[2];
output[3] = MAX2(color1[3], color2[3]);
}
void ZCombineAlphaOperation::update_memory_buffer_partial(MemoryBuffer *output,
const rcti &area,
Span<MemoryBuffer *> inputs)
{
for (BuffersIterator<float> it = output->iterate_with(inputs, area); !it.is_end(); ++it) {
const float depth1 = *it.in(1);
const float depth2 = *it.in(3);
const float *color1;
const float *color2;
if (depth1 <= depth2) {
color1 = it.in(0);
color2 = it.in(2);
}
else {
color1 = it.in(2);
color2 = it.in(0);
}
const float fac = color1[3];
const float ifac = 1.0f - fac;
it.out[0] = fac * color1[0] + ifac * color2[0];
it.out[1] = fac * color1[1] + ifac * color2[1];
it.out[2] = fac * color1[2] + ifac * color2[2];
it.out[3] = MAX2(color1[3], color2[3]);
}
}
void ZCombineOperation::deinitExecution()
{
m_image1Reader = nullptr;
m_depth1Reader = nullptr;
m_image2Reader = nullptr;
m_depth2Reader = nullptr;
}
// MASK combine
ZCombineMaskOperation::ZCombineMaskOperation()
{
this->addInputSocket(DataType::Value); // mask
this->addInputSocket(DataType::Color);
this->addInputSocket(DataType::Color);
this->addOutputSocket(DataType::Color);
m_maskReader = nullptr;
m_image1Reader = nullptr;
m_image2Reader = nullptr;
}
void ZCombineMaskOperation::initExecution()
{
m_maskReader = this->getInputSocketReader(0);
m_image1Reader = this->getInputSocketReader(1);
m_image2Reader = this->getInputSocketReader(2);
}
void ZCombineMaskOperation::executePixelSampled(float output[4],
float x,
float y,
PixelSampler sampler)
{
float mask[4];
float color1[4];
float color2[4];
m_maskReader->readSampled(mask, x, y, sampler);
m_image1Reader->readSampled(color1, x, y, sampler);
m_image2Reader->readSampled(color2, x, y, sampler);
interp_v4_v4v4(output, color1, color2, 1.0f - mask[0]);
}
void ZCombineMaskOperation::update_memory_buffer_partial(MemoryBuffer *output,
const rcti &area,
Span<MemoryBuffer *> inputs)
{
for (BuffersIterator<float> it = output->iterate_with(inputs, area); !it.is_end(); ++it) {
const float mask = *it.in(0);
const float *color1 = it.in(1);
const float *color2 = it.in(2);
interp_v4_v4v4(it.out, color1, color2, 1.0f - mask);
}
}
void ZCombineMaskAlphaOperation::executePixelSampled(float output[4],
float x,
float y,
PixelSampler sampler)
{
float mask[4];
float color1[4];
float color2[4];
m_maskReader->readSampled(mask, x, y, sampler);
m_image1Reader->readSampled(color1, x, y, sampler);
m_image2Reader->readSampled(color2, x, y, sampler);
float fac = (1.0f - mask[0]) * (1.0f - color1[3]) + mask[0] * color2[3];
float mfac = 1.0f - fac;
output[0] = color1[0] * mfac + color2[0] * fac;
output[1] = color1[1] * mfac + color2[1] * fac;
output[2] = color1[2] * mfac + color2[2] * fac;
output[3] = MAX2(color1[3], color2[3]);
}
void ZCombineMaskAlphaOperation::update_memory_buffer_partial(MemoryBuffer *output,
const rcti &area,
Span<MemoryBuffer *> inputs)
{
for (BuffersIterator<float> it = output->iterate_with(inputs, area); !it.is_end(); ++it) {
const float mask = *it.in(0);
const float *color1 = it.in(1);
const float *color2 = it.in(2);
const float fac = (1.0f - mask) * (1.0f - color1[3]) + mask * color2[3];
const float mfac = 1.0f - fac;
it.out[0] = color1[0] * mfac + color2[0] * fac;
it.out[1] = color1[1] * mfac + color2[1] * fac;
it.out[2] = color1[2] * mfac + color2[2] * fac;
it.out[3] = MAX2(color1[3], color2[3]);
}
}
void ZCombineMaskOperation::deinitExecution()
{
m_image1Reader = nullptr;
m_maskReader = nullptr;
m_image2Reader = nullptr;
}
} // namespace blender::compositor
|