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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
/*
* Copyright © 2016 Red Hat
*
* 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 (including the next
* paragraph) 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 <stdbool.h>
#include "st_tgsi_lower_yuv.h"
#include "tgsi/tgsi_transform.h"
#include "tgsi/tgsi_scan.h"
#include "tgsi/tgsi_dump.h"
#include "util/u_debug.h"
#include "util/bitscan.h"
struct tgsi_yuv_transform {
struct tgsi_transform_context base;
struct tgsi_shader_info info;
struct tgsi_full_src_register imm[4];
struct {
struct tgsi_full_src_register src;
struct tgsi_full_dst_register dst;
} tmp[2];
#define A 0
#define B 1
/* Maps a primary sampler (used for Y) to the U or UV sampler. In
* case of 3-plane YUV format, the V plane is next sampler after U.
*/
unsigned char sampler_map[PIPE_MAX_SAMPLERS][2];
bool first_instruction_emitted;
unsigned free_slots;
unsigned lower_nv12;
unsigned lower_iyuv;
};
static inline struct tgsi_yuv_transform *
tgsi_yuv_transform(struct tgsi_transform_context *tctx)
{
return (struct tgsi_yuv_transform *)tctx;
}
static void
reg_dst(struct tgsi_full_dst_register *dst,
const struct tgsi_full_dst_register *orig_dst, unsigned wrmask)
{
*dst = *orig_dst;
dst->Register.WriteMask &= wrmask;
assert(dst->Register.WriteMask);
}
static inline void
get_swiz(unsigned *swiz, const struct tgsi_src_register *src)
{
swiz[0] = src->SwizzleX;
swiz[1] = src->SwizzleY;
swiz[2] = src->SwizzleZ;
swiz[3] = src->SwizzleW;
}
static void
reg_src(struct tgsi_full_src_register *src,
const struct tgsi_full_src_register *orig_src,
unsigned sx, unsigned sy, unsigned sz, unsigned sw)
{
unsigned swiz[4];
get_swiz(swiz, &orig_src->Register);
*src = *orig_src;
src->Register.SwizzleX = swiz[sx];
src->Register.SwizzleY = swiz[sy];
src->Register.SwizzleZ = swiz[sz];
src->Register.SwizzleW = swiz[sw];
}
#define TGSI_SWIZZLE__ TGSI_SWIZZLE_X /* don't-care value! */
#define SWIZ(x,y,z,w) TGSI_SWIZZLE_ ## x, TGSI_SWIZZLE_ ## y, \
TGSI_SWIZZLE_ ## z, TGSI_SWIZZLE_ ## w
static inline struct tgsi_full_instruction
tex_instruction(unsigned samp)
{
struct tgsi_full_instruction inst;
inst = tgsi_default_full_instruction();
inst.Instruction.Opcode = TGSI_OPCODE_TEX;
inst.Instruction.Texture = 1;
inst.Texture.Texture = TGSI_TEXTURE_2D;
inst.Instruction.NumDstRegs = 1;
inst.Instruction.NumSrcRegs = 2;
inst.Src[1].Register.File = TGSI_FILE_SAMPLER;
inst.Src[1].Register.Index = samp;
return inst;
}
static inline struct tgsi_full_instruction
mov_instruction(void)
{
struct tgsi_full_instruction inst;
inst = tgsi_default_full_instruction();
inst.Instruction.Opcode = TGSI_OPCODE_MOV;
inst.Instruction.Saturate = 0;
inst.Instruction.NumDstRegs = 1;
inst.Instruction.NumSrcRegs = 1;
return inst;
}
static inline struct tgsi_full_instruction
dp3_instruction(void)
{
struct tgsi_full_instruction inst;
inst = tgsi_default_full_instruction();
inst.Instruction.Opcode = TGSI_OPCODE_DP3;
inst.Instruction.NumDstRegs = 1;
inst.Instruction.NumSrcRegs = 2;
return inst;
}
static void
emit_immed(struct tgsi_transform_context *tctx, int idx,
float x, float y, float z, float w)
{
struct tgsi_yuv_transform *ctx = tgsi_yuv_transform(tctx);
struct tgsi_shader_info *info = &ctx->info;
struct tgsi_full_immediate immed;
immed = tgsi_default_full_immediate();
immed.Immediate.NrTokens = 1 + 4; /* one for the token itself */
immed.u[0].Float = x;
immed.u[1].Float = y;
immed.u[2].Float = z;
immed.u[3].Float = w;
tctx->emit_immediate(tctx, &immed);
ctx->imm[idx].Register.File = TGSI_FILE_IMMEDIATE;
ctx->imm[idx].Register.Index = info->immediate_count + idx;
ctx->imm[idx].Register.SwizzleX = TGSI_SWIZZLE_X;
ctx->imm[idx].Register.SwizzleY = TGSI_SWIZZLE_Y;
ctx->imm[idx].Register.SwizzleZ = TGSI_SWIZZLE_Z;
ctx->imm[idx].Register.SwizzleW = TGSI_SWIZZLE_W;
}
static void
emit_samp(struct tgsi_transform_context *tctx, unsigned samp)
{
tgsi_transform_sampler_decl(tctx, samp);
tgsi_transform_sampler_view_decl(tctx, samp, PIPE_TEXTURE_2D,
TGSI_RETURN_TYPE_FLOAT);
}
/* Emit extra declarations we need:
* + 2 TEMP to hold intermediate results
* + 1 (for 2-plane YUV) or 2 (for 3-plane YUV) extra samplers per
* lowered YUV sampler
* + extra immediates for doing CSC
*/
static void
emit_decls(struct tgsi_transform_context *tctx)
{
struct tgsi_yuv_transform *ctx = tgsi_yuv_transform(tctx);
struct tgsi_shader_info *info = &ctx->info;
unsigned mask, tempbase, i;
struct tgsi_full_declaration decl;
/*
* Declare immediates for CSC conversion:
*/
/* ITU-R BT.601 conversion */
emit_immed(tctx, 0, 1.164, 0.000, 1.596, 0.0);
emit_immed(tctx, 1, 1.164, -0.392, -0.813, 0.0);
emit_immed(tctx, 2, 1.164, 2.017, 0.000, 0.0);
emit_immed(tctx, 3, 0.0625, 0.500, 0.500, 1.0);
/*
* Declare extra samplers / sampler-views:
*/
mask = ctx->lower_nv12 | ctx->lower_iyuv;
while (mask) {
unsigned extra, y_samp = u_bit_scan(&mask);
extra = u_bit_scan(&ctx->free_slots);
ctx->sampler_map[y_samp][0] = extra;
emit_samp(tctx, extra);
if (ctx->lower_iyuv & (1 << y_samp)) {
extra = u_bit_scan(&ctx->free_slots);
ctx->sampler_map[y_samp][1] = extra;
emit_samp(tctx, extra);
}
}
/*
* Declare extra temp:
*/
tempbase = info->file_max[TGSI_FILE_TEMPORARY] + 1;
for (i = 0; i < 2; i++) {
decl = tgsi_default_full_declaration();
decl.Declaration.File = TGSI_FILE_TEMPORARY;
decl.Range.First = decl.Range.Last = tempbase + i;
tctx->emit_declaration(tctx, &decl);
ctx->tmp[i].src.Register.File = TGSI_FILE_TEMPORARY;
ctx->tmp[i].src.Register.Index = tempbase + i;
ctx->tmp[i].src.Register.SwizzleX = TGSI_SWIZZLE_X;
ctx->tmp[i].src.Register.SwizzleY = TGSI_SWIZZLE_Y;
ctx->tmp[i].src.Register.SwizzleZ = TGSI_SWIZZLE_Z;
ctx->tmp[i].src.Register.SwizzleW = TGSI_SWIZZLE_W;
ctx->tmp[i].dst.Register.File = TGSI_FILE_TEMPORARY;
ctx->tmp[i].dst.Register.Index = tempbase + i;
ctx->tmp[i].dst.Register.WriteMask = TGSI_WRITEMASK_XYZW;
}
}
/* call with YUV in tmpA.xyz */
static void
yuv_to_rgb(struct tgsi_transform_context *tctx,
struct tgsi_full_dst_register *dst)
{
struct tgsi_yuv_transform *ctx = tgsi_yuv_transform(tctx);
struct tgsi_full_instruction inst;
/*
* IMM[0] FLT32 { 1.164, 0.000, 1.596, 0.0 }
* IMM[1] FLT32 { 1.164, -0.392, -0.813, 0.0 }
* IMM[2] FLT32 { 1.164, 2.017, 0.000, 0.0 }
* IMM[3] FLT32 { 0.0625, 0.500, 0.500, 1.0 }
*/
/* SUB tmpA.xyz, tmpA, imm[3] */
inst = tgsi_default_full_instruction();
inst.Instruction.Opcode = TGSI_OPCODE_ADD;
inst.Instruction.Saturate = 0;
inst.Instruction.NumDstRegs = 1;
inst.Instruction.NumSrcRegs = 2;
reg_dst(&inst.Dst[0], &ctx->tmp[A].dst, TGSI_WRITEMASK_XYZ);
reg_src(&inst.Src[0], &ctx->tmp[A].src, SWIZ(X, Y, Z, _));
reg_src(&inst.Src[1], &ctx->imm[3], SWIZ(X, Y, Z, _));
inst.Src[1].Register.Negate = 1;
tctx->emit_instruction(tctx, &inst);
/* DP3 dst.x, tmpA, imm[0] */
inst = dp3_instruction();
reg_dst(&inst.Dst[0], dst, TGSI_WRITEMASK_X);
reg_src(&inst.Src[0], &ctx->tmp[A].src, SWIZ(X, Y, Z, W));
reg_src(&inst.Src[1], &ctx->imm[0], SWIZ(X, Y, Z, W));
tctx->emit_instruction(tctx, &inst);
/* DP3 dst.y, tmpA, imm[1] */
inst = dp3_instruction();
reg_dst(&inst.Dst[0], dst, TGSI_WRITEMASK_Y);
reg_src(&inst.Src[0], &ctx->tmp[A].src, SWIZ(X, Y, Z, W));
reg_src(&inst.Src[1], &ctx->imm[1], SWIZ(X, Y, Z, W));
tctx->emit_instruction(tctx, &inst);
/* DP3 dst.z, tmpA, imm[2] */
inst = dp3_instruction();
reg_dst(&inst.Dst[0], dst, TGSI_WRITEMASK_Z);
reg_src(&inst.Src[0], &ctx->tmp[A].src, SWIZ(X, Y, Z, W));
reg_src(&inst.Src[1], &ctx->imm[2], SWIZ(X, Y, Z, W));
tctx->emit_instruction(tctx, &inst);
/* MOV dst.w, imm[0].x */
inst = mov_instruction();
reg_dst(&inst.Dst[0], dst, TGSI_WRITEMASK_W);
reg_src(&inst.Src[0], &ctx->imm[3], SWIZ(_, _, _, W));
tctx->emit_instruction(tctx, &inst);
}
static void
lower_nv12(struct tgsi_transform_context *tctx,
struct tgsi_full_instruction *originst)
{
struct tgsi_yuv_transform *ctx = tgsi_yuv_transform(tctx);
struct tgsi_full_instruction inst;
struct tgsi_full_src_register *coord = &originst->Src[0];
unsigned samp = originst->Src[1].Register.Index;
/* sample Y:
* TEX tempA.x, coord, texture[samp], 2D;
*/
inst = tex_instruction(samp);
reg_dst(&inst.Dst[0], &ctx->tmp[A].dst, TGSI_WRITEMASK_X);
reg_src(&inst.Src[0], coord, SWIZ(X, Y, Z, W));
tctx->emit_instruction(tctx, &inst);
/* sample UV:
* TEX tempB.xy, coord, texture[sampler_map[samp][0]], 2D;
* MOV tempA.yz, tempB._xy_
*/
inst = tex_instruction(ctx->sampler_map[samp][0]);
reg_dst(&inst.Dst[0], &ctx->tmp[B].dst, TGSI_WRITEMASK_XY);
reg_src(&inst.Src[0], coord, SWIZ(X, Y, Z, W));
tctx->emit_instruction(tctx, &inst);
inst = mov_instruction();
reg_dst(&inst.Dst[0], &ctx->tmp[A].dst, TGSI_WRITEMASK_YZ);
reg_src(&inst.Src[0], &ctx->tmp[B].src, SWIZ(_, X, Y, _));
tctx->emit_instruction(tctx, &inst);
/* At this point, we have YUV in tempA.xyz, rest is common: */
yuv_to_rgb(tctx, &originst->Dst[0]);
}
static void
lower_iyuv(struct tgsi_transform_context *tctx,
struct tgsi_full_instruction *originst)
{
struct tgsi_yuv_transform *ctx = tgsi_yuv_transform(tctx);
struct tgsi_full_instruction inst;
struct tgsi_full_src_register *coord = &originst->Src[0];
unsigned samp = originst->Src[1].Register.Index;
/* sample Y:
* TEX tempA.x, coord, texture[samp], 2D;
*/
inst = tex_instruction(samp);
reg_dst(&inst.Dst[0], &ctx->tmp[A].dst, TGSI_WRITEMASK_X);
reg_src(&inst.Src[0], coord, SWIZ(X, Y, Z, W));
tctx->emit_instruction(tctx, &inst);
/* sample U:
* TEX tempB.x, coord, texture[sampler_map[samp][0]], 2D;
* MOV tempA.y, tempB._x__
*/
inst = tex_instruction(ctx->sampler_map[samp][0]);
reg_dst(&inst.Dst[0], &ctx->tmp[B].dst, TGSI_WRITEMASK_X);
reg_src(&inst.Src[0], coord, SWIZ(X, Y, Z, W));
tctx->emit_instruction(tctx, &inst);
inst = mov_instruction();
reg_dst(&inst.Dst[0], &ctx->tmp[A].dst, TGSI_WRITEMASK_Y);
reg_src(&inst.Src[0], &ctx->tmp[B].src, SWIZ(_, X, _, _));
tctx->emit_instruction(tctx, &inst);
/* sample V:
* TEX tempB.x, coord, texture[sampler_map[samp][1]], 2D;
* MOV tempA.z, tempB.__x_
*/
inst = tex_instruction(ctx->sampler_map[samp][1]);
reg_dst(&inst.Dst[0], &ctx->tmp[B].dst, TGSI_WRITEMASK_X);
reg_src(&inst.Src[0], coord, SWIZ(X, Y, Z, W));
tctx->emit_instruction(tctx, &inst);
inst = mov_instruction();
reg_dst(&inst.Dst[0], &ctx->tmp[A].dst, TGSI_WRITEMASK_Z);
reg_src(&inst.Src[0], &ctx->tmp[B].src, SWIZ(_, _, X, _));
tctx->emit_instruction(tctx, &inst);
/* At this point, we have YUV in tempA.xyz, rest is common: */
yuv_to_rgb(tctx, &originst->Dst[0]);
}
static void
transform_instr(struct tgsi_transform_context *tctx,
struct tgsi_full_instruction *inst)
{
struct tgsi_yuv_transform *ctx = tgsi_yuv_transform(tctx);
if (!ctx->first_instruction_emitted) {
emit_decls(tctx);
ctx->first_instruction_emitted = true;
}
switch (inst->Instruction.Opcode) {
/* TODO what other tex opcode's can be used w/ external eglimgs? */
case TGSI_OPCODE_TEX: {
unsigned samp = inst->Src[1].Register.Index;
if (ctx->lower_nv12 & (1 << samp)) {
lower_nv12(tctx, inst);
} else if (ctx->lower_iyuv & (1 << samp)) {
lower_iyuv(tctx, inst);
} else {
goto skip;
}
break;
}
default:
skip:
tctx->emit_instruction(tctx, inst);
return;
}
}
extern const struct tgsi_token *
st_tgsi_lower_yuv(const struct tgsi_token *tokens, unsigned free_slots,
unsigned lower_nv12, unsigned lower_iyuv)
{
struct tgsi_yuv_transform ctx;
struct tgsi_token *newtoks;
int newlen;
assert(!(lower_nv12 & lower_iyuv)); /* bitmasks should be mutually exclusive */
// tgsi_dump(tokens, 0);
// debug_printf("\n");
memset(&ctx, 0, sizeof(ctx));
ctx.base.transform_instruction = transform_instr;
ctx.free_slots = free_slots;
ctx.lower_nv12 = lower_nv12;
ctx.lower_iyuv = lower_iyuv;
tgsi_scan_shader(tokens, &ctx.info);
/* TODO better job of figuring out how many extra tokens we need..
* this is a pain about tgsi_transform :-/
*/
newlen = tgsi_num_tokens(tokens) + 120;
newtoks = tgsi_alloc_tokens(newlen);
if (!newtoks)
return NULL;
tgsi_transform_shader(tokens, newtoks, newlen, &ctx.base);
// tgsi_dump(newtoks, 0);
// debug_printf("\n");
return newtoks;
}
|