Repository navigation
Expand file tree
/
Copy pathsinglecolourfit_ccr_flat.cpp
More file actions
300 lines (248 loc) · 9.21 KB
/
Copy pathsinglecolourfit_ccr_flat.cpp
File metadata and controls
300 lines (248 loc) · 9.21 KB
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
/* -----------------------------------------------------------------------------
Copyright (c) 2006 Simon Brown si@sjbrown.co.uk
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.
-------------------------------------------------------------------------- */
namespace squish {
#if defined(USE_AMP) || defined(USE_COMPUTE)
void SingleColourFit_CCR::AssignSet(tile_barrier barrier, const int thread, ColourSet_CCRr m_colours, const int metric, const int fit) amp_restricted
{
ColourFit_CCR::AssignSet(barrier, thread, m_colours, metric, fit);
// grab the single colour
point16 values = m_colours.GetPoints();
threaded_cse(0) {
// initialise the color
m_colour = FloatToInt(values[0] * float3(255.0f, 255.0f, 255.0f), int3(255, 255, 255));
// initialise the index
// m_index = 0;
}
}
void SingleColourFit_CCR::Compress(tile_barrier barrier, const int thread, ColourSet_CCRr m_colours, out code64 block, const bool trans,
IndexBlockLUT yArr, SingleColourLUT lArr) amp_restricted
{
/* all or nothing branches, OK, same for all threads */
const bool isDxt1 = (trans);
if (isDxt1 && m_colours.IsTransparent())
Compress3 (barrier, thread, m_colours, block, yArr, lArr);
else if (!isDxt1)
Compress4 (barrier, thread, m_colours, block, yArr, lArr);
else
Compress34(barrier, thread, m_colours, block, yArr, lArr);
}
#define CASE3 0
#define CASE4 1
void SingleColourFit_CCR::Compress3(tile_barrier barrier, const int thread, ColourSet_CCRr m_colours, out code64 block,
IndexBlockLUT yArr, SingleColourLUT lArr) amp_restricted
{
// find the best end-points and index
ComputeEndPoints(barrier, thread, CASE3, lArr);
// build the block if we win
{
// remap the one index (AMP: prevent writing to same location)
// m_matches[1][ 0 ] = m_index;
m_matches[1][thread] = m_index;
// save the block
ColourFit_CCR::Compress3(barrier, thread, m_colours, block, yArr);
}
}
void SingleColourFit_CCR::Compress4(tile_barrier barrier, const int thread, ColourSet_CCRr m_colours, out code64 block,
IndexBlockLUT yArr, SingleColourLUT lArr) amp_restricted
{
// find the best end-points and index
ComputeEndPoints(barrier, thread, CASE4, lArr);
// build the block if we win
{
// remap the one index (AMP: prevent writing to same location)
// m_matches[1][ 0 ] = m_index;
m_matches[1][thread] = m_index;
// save the block
ColourFit_CCR::Compress4(barrier, thread, m_colours, block, yArr);
}
}
void SingleColourFit_CCR::Compress34(tile_barrier barrier, const int thread, ColourSet_CCRr m_colours, out code64 block,
IndexBlockLUT yArr, SingleColourLUT lArr) amp_restricted
{
// find the best end-points and index
int is4 = ComputeEndPoints(barrier, thread, lArr);
// build the block if we win
{
// remap the one index (AMP: prevent writing to same location)
// m_matches[1][ 0 ] = m_index;
m_matches[1][thread] = m_index;
// save the block
ColourFit_CCR::Compress34(barrier, thread, m_colours, block, is4, yArr);
}
}
#if defined(USE_COMPUTE)
tile_static ::SourceBlock_CCR sources[3 * 4];
tile_static float3 lines[4][2];
tile_static int /*error[4],*/ err[2];
#endif
#define SIDEL 0
#define SIDEH 1
#define SIDES 2
#define CBITS 1 // number of bits
#define CMASK 1 // mask
void SingleColourFit_CCR::ComputeEndPoints(tile_barrier barrier, const int thread, const int is4,
SingleColourLUT lArr) amp_restricted
{
#if !defined(USE_COMPUTE)
tile_static ::SourceBlock_CCR sources[3 * SIDES];
tile_static float3 lines[SIDES][CVALS];
tile_static int error[SIDES];
#endif
int e;
/* 3 colors, by 2 sides (this or next) */
threaded_for(sjoint, SIDES) {
const int index = sjoint;
// AMP: pull from constant to shared
{
// grab the lookup table and index for this channel
// store a pointer to the source for this channel
#if defined(USE_COMPUTE)
sources[(0 << CBITS) + index] = lArr[is4][m_colour.r][0 /*5*/].sources[index];
sources[(1 << CBITS) + index] = lArr[is4][m_colour.g][1 /*6*/].sources[index];
sources[(2 << CBITS) + index] = lArr[is4][m_colour.b][0 /*5*/].sources[index];
#else
sources[(0 << CBITS) + index] = lArr(is4, m_colour.r, 0 /*5*/).sources[index];
sources[(1 << CBITS) + index] = lArr(is4, m_colour.g, 1 /*6*/).sources[index];
sources[(2 << CBITS) + index] = lArr(is4, m_colour.b, 0 /*5*/).sources[index];
#endif
}
}
// make it visible
tile_static_memory_fence(barrier);
// calculate the interpolation lines (2 choices)
threaded_for(ljoint, CVALS * SIDES) {
const int side = ljoint & CMASK;
const int cpos = ljoint >> CBITS;
lines[side][cpos] = float3(
(float)sources[(0 << CBITS) + side].cline[cpos],
(float)sources[(1 << CBITS) + side].cline[cpos],
(float)sources[(2 << CBITS) + side].cline[cpos]
) /
float3(31.0f, 63.0f, 31.0f);
}
// accumulate the errors
threaded_for(ejoint, SIDES) {
const int index = ejoint & CMASK;
error[index] =
sources[(0 << CBITS) + index].error_sqr +
sources[(1 << CBITS) + index].error_sqr +
sources[(2 << CBITS) + index].error_sqr;
}
// make it visible
tile_static_memory_fence(barrier);
// AMP: final reduction (make the choice)
threaded_cse(0) {
// keep it if the error is lower
e = (error[SIDEH] < error[SIDEL] ? SIDEH : SIDEL);
m_line[CSTRT] = lines[e][CSTRT];
m_line[CSTOP] = lines[e][CSTOP];
m_index = (ccr8)(2 * e);
}
}
#undef CASE3
#undef CASE4
#undef CBITS
#undef CMASK
#undef SIDEL
#undef SIDEH
#undef SIDES
#define SIDEL 0
#define SIDEH 2
#define CASE3 0
#define CASE4 1
#define CASES 2
#define CNUMS 4
#define CBITS 2 // number of bits
#define CMASK 3 // mask
int SingleColourFit_CCR::ComputeEndPoints(tile_barrier barrier, const int thread,
SingleColourLUT lArr) amp_restricted
{
#if !defined(USE_COMPUTE)
tile_static ::SourceBlock_CCR sources[3 * CNUMS];
tile_static float3 lines[CNUMS][CVALS];
tile_static int error[CNUMS], err[CASES];
#endif
/* 3 colors, by 2 sides (this or next), by 2 types (3 or 4) */
threaded_for(sjoint, CNUMS) {
const int which = (sjoint & 1);
const int index = (sjoint >> 1) & 1;
{
// store a pointer to the source for this channel
#if defined(USE_COMPUTE)
sources[(0 << CBITS) + sjoint] = lArr[which][m_colour.r][0 /*5*/].sources[index];
sources[(1 << CBITS) + sjoint] = lArr[which][m_colour.g][1 /*6*/].sources[index];
sources[(2 << CBITS) + sjoint] = lArr[which][m_colour.b][0 /*5*/].sources[index];
#else
sources[(0 << CBITS) + sjoint] = lArr(which, m_colour.r, 0 /*5*/).sources[index];
sources[(1 << CBITS) + sjoint] = lArr(which, m_colour.g, 1 /*6*/).sources[index];
sources[(2 << CBITS) + sjoint] = lArr(which, m_colour.b, 0 /*5*/).sources[index];
#endif
}
}
// make it visible
tile_static_memory_fence(barrier);
// calculate the interpolation lines (4 choices)
threaded_for(ljoint, CVALS * CNUMS) {
const int wsde = (ljoint & CMASK);
const int cpos = (ljoint >> CBITS);
lines[wsde][cpos] = float3(
(float)sources[(0 << CBITS) + wsde].cline[cpos],
(float)sources[(1 << CBITS) + wsde].cline[cpos],
(float)sources[(2 << CBITS) + wsde].cline[cpos]
) /
float3(31.0f, 63.0f, 31.0f);
}
// accumulate the errors
threaded_for(ejoint, CNUMS) {
const int wsde = ejoint;
error[wsde] =
sources[(0 << CBITS) + wsde].error_sqr +
sources[(1 << CBITS) + wsde].error_sqr +
sources[(2 << CBITS) + wsde].error_sqr;
}
// make it visible
tile_static_memory_fence(barrier);
// choose each side for 3 and 4
threaded_for(which, CASES) {
err[which] = (error[which + SIDEL] < error[which + SIDEH] ? which + SIDEL : which + SIDEH);
}
// make it visible
tile_static_memory_fence(barrier);
// AMP: all thread end up here, and all make this compare
int e = (error[err[CASE3]] < error[err[CASE4]] ? err[CASE3] : err[CASE4]);
// AMP: final reduction (make the choice)
threaded_cse(0) {
// keep it if the error is lower
m_line[CSTRT] = lines[e][CSTRT];
m_line[CSTOP] = lines[e][CSTOP];
m_index = (ccr8)(e & SIDEH);
}
// is4
return (e & CASE4);
}
#undef CASE3
#undef CASE4
#undef CBITS
#undef CMASK
#undef SIDEL
#undef SIDEH
#undef SIDES
#endif
} // namespace squish