-
Notifications
You must be signed in to change notification settings - Fork 5
/
ConvectionKernels_API.cpp
346 lines (280 loc) · 14.4 KB
/
ConvectionKernels_API.cpp
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
Convection Texture Tools
Copyright (c) 2018-2019 Eric Lasota
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "ConvectionKernels_Config.h"
#if !defined(CVTT_SINGLE_FILE) || defined(CVTT_SINGLE_FILE_IMPL)
#include <stdint.h>
#include "ConvectionKernels.h"
#include "ConvectionKernels_Util.h"
#include "ConvectionKernels_BC67.h"
#include "ConvectionKernels_ETC.h"
#include "ConvectionKernels_S3TC.h"
#include <assert.h>
namespace cvtt
{
namespace Kernels
{
void EncodeBC7(uint8_t *pBC, const PixelBlockU8 *pBlocks, const cvtt::Options &options, const BC7EncodingPlan &encodingPlan)
{
assert(pBlocks);
assert(pBC);
float channelWeights[4];
Util::FillWeights(options, channelWeights);
for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
{
Internal::BC7Computer::Pack(options.flags, pBlocks + blockBase, pBC, channelWeights, encodingPlan, options.refineRoundsBC7);
pBC += ParallelMath::ParallelSize * 16;
}
}
void EncodeBC6HU(uint8_t *pBC, const PixelBlockF16 *pBlocks, const cvtt::Options &options)
{
assert(pBlocks);
assert(pBC);
float channelWeights[4];
Util::FillWeights(options, channelWeights);
for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
{
Internal::BC6HComputer::Pack(options.flags, pBlocks + blockBase, pBC, channelWeights, false, options.seedPoints, options.refineRoundsBC6H);
pBC += ParallelMath::ParallelSize * 16;
}
}
void EncodeBC6HS(uint8_t *pBC, const PixelBlockF16 *pBlocks, const cvtt::Options &options)
{
assert(pBlocks);
assert(pBC);
float channelWeights[4];
Util::FillWeights(options, channelWeights);
for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
{
Internal::BC6HComputer::Pack(options.flags, pBlocks + blockBase, pBC, channelWeights, true, options.seedPoints, options.refineRoundsBC6H);
pBC += ParallelMath::ParallelSize * 16;
}
}
void EncodeBC1(uint8_t *pBC, const PixelBlockU8 *pBlocks, const cvtt::Options &options)
{
assert(pBlocks);
assert(pBC);
float channelWeights[4];
Util::FillWeights(options, channelWeights);
for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
{
Internal::S3TCComputer::PackRGB(options.flags, pBlocks + blockBase, pBC, 8, channelWeights, true, options.threshold, (options.flags & Flags::S3TC_Exhaustive) != 0, options.seedPoints, options.refineRoundsS3TC);
pBC += ParallelMath::ParallelSize * 8;
}
}
void EncodeBC2(uint8_t *pBC, const PixelBlockU8 *pBlocks, const Options &options)
{
assert(pBlocks);
assert(pBC);
float channelWeights[4];
Util::FillWeights(options, channelWeights);
for (size_t blockBase = 0; blockBase < NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
{
Internal::S3TCComputer::PackRGB(options.flags, pBlocks + blockBase, pBC + 8, 16, channelWeights, false, 1.0f, (options.flags & Flags::S3TC_Exhaustive) != 0, options.seedPoints, options.refineRoundsS3TC);
Internal::S3TCComputer::PackExplicitAlpha(options.flags, pBlocks + blockBase, 3, pBC, 16);
pBC += ParallelMath::ParallelSize * 16;
}
}
void EncodeBC3(uint8_t *pBC, const PixelBlockU8 *pBlocks, const Options &options)
{
assert(pBlocks);
assert(pBC);
float channelWeights[4];
Util::FillWeights(options, channelWeights);
for (size_t blockBase = 0; blockBase < NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
{
Internal::S3TCComputer::PackRGB(options.flags, pBlocks + blockBase, pBC + 8, 16, channelWeights, false, 1.0f, (options.flags & Flags::S3TC_Exhaustive) != 0, options.seedPoints, options.refineRoundsS3TC);
Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, pBlocks + blockBase, 3, pBC, 16, false, options.seedPoints, options.refineRoundsIIC);
pBC += ParallelMath::ParallelSize * 16;
}
}
void EncodeBC4U(uint8_t *pBC, const PixelBlockU8 *pBlocks, const Options &options)
{
assert(pBlocks);
assert(pBC);
float channelWeights[4];
Util::FillWeights(options, channelWeights);
for (size_t blockBase = 0; blockBase < NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
{
Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, pBlocks + blockBase, 0, pBC, 8, false, options.seedPoints, options.refineRoundsIIC);
pBC += ParallelMath::ParallelSize * 8;
}
}
void EncodeBC4S(uint8_t *pBC, const PixelBlockS8 *pBlocks, const Options &options)
{
assert(pBlocks);
assert(pBC);
float channelWeights[4];
Util::FillWeights(options, channelWeights);
for (size_t blockBase = 0; blockBase < NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
{
PixelBlockU8 inputBlocks[ParallelMath::ParallelSize];
Util::BiasSignedInput(inputBlocks, pBlocks + blockBase);
Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, inputBlocks, 0, pBC, 8, true, options.seedPoints, options.refineRoundsIIC);
pBC += ParallelMath::ParallelSize * 8;
}
}
void EncodeBC5U(uint8_t *pBC, const PixelBlockU8 *pBlocks, const Options &options)
{
assert(pBlocks);
assert(pBC);
float channelWeights[4];
Util::FillWeights(options, channelWeights);
for (size_t blockBase = 0; blockBase < NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
{
Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, pBlocks + blockBase, 0, pBC, 16, false, options.seedPoints, options.refineRoundsIIC);
Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, pBlocks + blockBase, 1, pBC + 8, 16, false, options.seedPoints, options.refineRoundsIIC);
pBC += ParallelMath::ParallelSize * 16;
}
}
void EncodeBC5S(uint8_t *pBC, const PixelBlockS8 *pBlocks, const Options &options)
{
assert(pBlocks);
assert(pBC);
float channelWeights[4];
Util::FillWeights(options, channelWeights);
for (size_t blockBase = 0; blockBase < NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
{
PixelBlockU8 inputBlocks[ParallelMath::ParallelSize];
Util::BiasSignedInput(inputBlocks, pBlocks + blockBase);
Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, inputBlocks, 0, pBC, 16, true, options.seedPoints, options.refineRoundsIIC);
Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, inputBlocks, 1, pBC + 8, 16, true, options.seedPoints, options.refineRoundsIIC);
pBC += ParallelMath::ParallelSize * 16;
}
}
void EncodeETC1(uint8_t *pBC, const PixelBlockU8 *pBlocks, const cvtt::Options &options, cvtt::ETC1CompressionData *compressionData)
{
assert(pBlocks);
assert(pBC);
float channelWeights[4];
Util::FillWeights(options, channelWeights);
for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
{
Internal::ETCComputer::CompressETC1Block(pBC, pBlocks + blockBase, compressionData, options);
pBC += ParallelMath::ParallelSize * 8;
}
}
void EncodeETC2(uint8_t *pBC, const PixelBlockU8 *pBlocks, const cvtt::Options &options, cvtt::ETC2CompressionData *compressionData)
{
assert(pBlocks);
assert(pBC);
float channelWeights[4];
Util::FillWeights(options, channelWeights);
for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
{
Internal::ETCComputer::CompressETC2Block(pBC, pBlocks + blockBase, compressionData, options, false);
pBC += ParallelMath::ParallelSize * 8;
}
}
void EncodeETC2PunchthroughAlpha(uint8_t *pBC, const PixelBlockU8 *pBlocks, const cvtt::Options &options, cvtt::ETC2CompressionData *compressionData)
{
assert(pBlocks);
assert(pBC);
float channelWeights[4];
Util::FillWeights(options, channelWeights);
for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
{
Internal::ETCComputer::CompressETC2Block(pBC, pBlocks + blockBase, compressionData, options, true);
pBC += ParallelMath::ParallelSize * 8;
}
}
void EncodeETC2Alpha(uint8_t *pBC, const PixelBlockU8 *pBlocks, const cvtt::Options &options)
{
assert(pBlocks);
assert(pBC);
for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
{
Internal::ETCComputer::CompressETC2AlphaBlock(pBC, pBlocks + blockBase, options);
pBC += ParallelMath::ParallelSize * 8;
}
}
void EncodeETC2Alpha11(uint8_t *pBC, const PixelBlockScalarS16 *pBlocks, bool isSigned, const cvtt::Options &options)
{
assert(pBlocks);
assert(pBC);
for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
{
Internal::ETCComputer::CompressEACBlock(pBC, pBlocks + blockBase, isSigned, options);
pBC += ParallelMath::ParallelSize * 8;
}
}
void EncodeETC2RGBA(uint8_t *pBC, const PixelBlockU8 *pBlocks, const cvtt::Options &options, cvtt::ETC2CompressionData *compressionData)
{
uint8_t alphaBlockData[cvtt::NumParallelBlocks * 8];
uint8_t colorBlockData[cvtt::NumParallelBlocks * 8];
EncodeETC2(colorBlockData, pBlocks, options, compressionData);
EncodeETC2Alpha(alphaBlockData, pBlocks, options);
for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase++)
{
for (size_t blockData = 0; blockData < 8; blockData++)
pBC[blockBase * 16 + blockData] = alphaBlockData[blockBase * 8 + blockData];
for (size_t blockData = 0; blockData < 8; blockData++)
pBC[blockBase * 16 + 8 + blockData] = colorBlockData[blockBase * 8 + blockData];
}
}
void DecodeBC7(PixelBlockU8 *pBlocks, const uint8_t *pBC)
{
assert(pBlocks);
assert(pBC);
for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase++)
{
Internal::BC7Computer::UnpackOne(pBlocks[blockBase], pBC);
pBC += 16;
}
}
void DecodeBC6HU(PixelBlockF16 *pBlocks, const uint8_t *pBC)
{
assert(pBlocks);
assert(pBC);
for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase++)
{
Internal::BC6HComputer::UnpackOne(pBlocks[blockBase], pBC, false);
pBC += 16;
}
}
void DecodeBC6HS(PixelBlockF16 *pBlocks, const uint8_t *pBC)
{
assert(pBlocks);
assert(pBC);
for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase++)
{
Internal::BC6HComputer::UnpackOne(pBlocks[blockBase], pBC, true);
pBC += 16;
}
}
ETC1CompressionData *AllocETC1Data(allocFunc_t allocFunc, void *context)
{
return cvtt::Internal::ETCComputer::AllocETC1Data(allocFunc, context);
}
void ReleaseETC1Data(ETC1CompressionData *compressionData, freeFunc_t freeFunc)
{
cvtt::Internal::ETCComputer::ReleaseETC1Data(compressionData, freeFunc);
}
ETC2CompressionData *AllocETC2Data(allocFunc_t allocFunc, void *context, const cvtt::Options &options)
{
return cvtt::Internal::ETCComputer::AllocETC2Data(allocFunc, context, options);
}
void ReleaseETC2Data(ETC2CompressionData *compressionData, freeFunc_t freeFunc)
{
cvtt::Internal::ETCComputer::ReleaseETC2Data(compressionData, freeFunc);
}
}
}
#endif