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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
/**************************************************************************
*
* Copyright 2019 Advanced Micro Devices, Inc.
* All Rights Reserved.
*
* 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, sub license, 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 NON-INFRINGEMENT.
* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS 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.
*
* Authors: James Zhu <james.zhu<@amd.com>
*
**************************************************************************/
#include <assert.h>
#include "tgsi/tgsi_text.h"
#include "vl_compositor_cs.h"
struct cs_viewport {
float scale_x;
float scale_y;
struct u_rect area;
int translate_x;
int translate_y;
float sampler0_w;
float sampler0_h;
};
const char *compute_shader_video_buffer =
"COMP\n"
"PROPERTY CS_FIXED_BLOCK_WIDTH 8\n"
"PROPERTY CS_FIXED_BLOCK_HEIGHT 8\n"
"PROPERTY CS_FIXED_BLOCK_DEPTH 1\n"
"DCL SV[0], THREAD_ID\n"
"DCL SV[1], BLOCK_ID\n"
"DCL CONST[0..5]\n"
"DCL SVIEW[0..2], RECT, FLOAT\n"
"DCL SAMP[0..2]\n"
"DCL IMAGE[0], 2D, WR\n"
"DCL TEMP[0..7]\n"
"IMM[0] UINT32 { 8, 8, 1, 0}\n"
"IMM[1] FLT32 { 1.0, 2.0, 0.0, 0.0}\n"
"UMAD TEMP[0], SV[1], IMM[0], SV[0]\n"
/* Drawn area check */
"USGE TEMP[1].xy, TEMP[0].xyxy, CONST[4].xyxy\n"
"USLT TEMP[1].zw, TEMP[0].xyxy, CONST[4].zwzw\n"
"AND TEMP[1].x, TEMP[1].xxxx, TEMP[1].yyyy\n"
"AND TEMP[1].x, TEMP[1].xxxx, TEMP[1].zzzz\n"
"AND TEMP[1].x, TEMP[1].xxxx, TEMP[1].wwww\n"
"UIF TEMP[1]\n"
/* Translate */
"UADD TEMP[2].xy, TEMP[0], -CONST[5].xyxy\n"
"U2F TEMP[2], TEMP[2]\n"
"DIV TEMP[3], TEMP[2], IMM[1].yyyy\n"
/* Scale */
"DIV TEMP[2], TEMP[2], CONST[3].zwzw\n"
"DIV TEMP[3], TEMP[3], CONST[3].zwzw\n"
/* Fetch texels */
"TEX_LZ TEMP[4].x, TEMP[2], SAMP[0], RECT\n"
"TEX_LZ TEMP[4].y, TEMP[3], SAMP[1], RECT\n"
"TEX_LZ TEMP[4].z, TEMP[3], SAMP[2], RECT\n"
"MOV TEMP[4].w, IMM[1].xxxx\n"
/* Color Space Conversion */
"DP4 TEMP[7].x, CONST[0], TEMP[4]\n"
"DP4 TEMP[7].y, CONST[1], TEMP[4]\n"
"DP4 TEMP[7].z, CONST[2], TEMP[4]\n"
"MOV TEMP[5].w, TEMP[4].zzzz\n"
"SLE TEMP[6].w, TEMP[5], CONST[3].xxxx\n"
"SGT TEMP[5].w, TEMP[5], CONST[3].yyyy\n"
"MAX TEMP[7].w, TEMP[5], TEMP[6]\n"
"STORE IMAGE[0], TEMP[0], TEMP[7], 2D\n"
"ENDIF\n"
"END\n";
const char *compute_shader_weave =
"COMP\n"
"PROPERTY CS_FIXED_BLOCK_WIDTH 8\n"
"PROPERTY CS_FIXED_BLOCK_HEIGHT 8\n"
"PROPERTY CS_FIXED_BLOCK_DEPTH 1\n"
"DCL SV[0], THREAD_ID\n"
"DCL SV[1], BLOCK_ID\n"
"DCL CONST[0..5]\n"
"DCL SVIEW[0..2], 2D_ARRAY, FLOAT\n"
"DCL SAMP[0..2]\n"
"DCL IMAGE[0], 2D, WR\n"
"DCL TEMP[0..15]\n"
"IMM[0] UINT32 { 8, 8, 1, 0}\n"
"IMM[1] FLT32 { 1.0, 2.0, 0.0, 0.0}\n"
"IMM[2] UINT32 { 1, 2, 4, 0}\n"
"IMM[3] FLT32 { 0.25, 0.5, 0.125, 0.125}\n"
"UMAD TEMP[0], SV[1], IMM[0], SV[0]\n"
/* Drawn area check */
"USGE TEMP[1].xy, TEMP[0].xyxy, CONST[4].xyxy\n"
"USLT TEMP[1].zw, TEMP[0].xyxy, CONST[4].zwzw\n"
"AND TEMP[1].x, TEMP[1].xxxx, TEMP[1].yyyy\n"
"AND TEMP[1].x, TEMP[1].xxxx, TEMP[1].zzzz\n"
"AND TEMP[1].x, TEMP[1].xxxx, TEMP[1].wwww\n"
"UIF TEMP[1]\n"
"MOV TEMP[2], TEMP[0]\n"
/* Translate */
"UADD TEMP[2].xy, TEMP[2], -CONST[5].xyxy\n"
/* Top Y */
"U2F TEMP[2], TEMP[2]\n"
"DIV TEMP[2].y, TEMP[2].yyyy, IMM[1].yyyy\n"
/* Down Y */
"MOV TEMP[12], TEMP[2]\n"
/* Top UV */
"MOV TEMP[3], TEMP[2]\n"
"DIV TEMP[3].xy, TEMP[3], IMM[1].yyyy\n"
/* Down UV */
"MOV TEMP[13], TEMP[3]\n"
/* Texture offset */
"ADD TEMP[2].x, TEMP[2].xxxx, IMM[3].yyyy\n"
"ADD TEMP[2].y, TEMP[2].yyyy, IMM[3].xxxx\n"
"ADD TEMP[12].x, TEMP[12].xxxx, IMM[3].yyyy\n"
"ADD TEMP[12].y, TEMP[12].yyyy, IMM[3].xxxx\n"
"ADD TEMP[3].x, TEMP[3].xxxx, IMM[3].xxxx\n"
"ADD TEMP[3].y, TEMP[3].yyyy, IMM[3].wwww\n"
"ADD TEMP[13].x, TEMP[13].xxxx, IMM[3].xxxx\n"
"ADD TEMP[13].y, TEMP[13].yyyy, IMM[3].wwww\n"
/* Scale */
"DIV TEMP[2].xy, TEMP[2], CONST[3].zwzw\n"
"DIV TEMP[12].xy, TEMP[12], CONST[3].zwzw\n"
"DIV TEMP[3].xy, TEMP[3], CONST[3].zwzw\n"
"DIV TEMP[13].xy, TEMP[13], CONST[3].zwzw\n"
/* Weave offset */
"ADD TEMP[2].y, TEMP[2].yyyy, IMM[3].xxxx\n"
"ADD TEMP[12].y, TEMP[12].yyyy, -IMM[3].xxxx\n"
"ADD TEMP[3].y, TEMP[3].yyyy, IMM[3].xxxx\n"
"ADD TEMP[13].y, TEMP[13].yyyy, -IMM[3].xxxx\n"
/* Texture layer */
"MOV TEMP[14].x, TEMP[2].yyyy\n"
"MOV TEMP[14].yz, TEMP[3].yyyy\n"
"ROUND TEMP[15], TEMP[14]\n"
"ADD TEMP[14], TEMP[14], -TEMP[15]\n"
"MOV TEMP[14], |TEMP[14]|\n"
"MUL TEMP[14], TEMP[14], IMM[1].yyyy\n"
/* Normalize */
"DIV TEMP[2].xy, TEMP[2], CONST[5].zwzw\n"
"DIV TEMP[12].xy, TEMP[12], CONST[5].zwzw\n"
"DIV TEMP[15].xy, CONST[5].zwzw, IMM[1].yyyy\n"
"DIV TEMP[3].xy, TEMP[3], TEMP[15].xyxy\n"
"DIV TEMP[13].xy, TEMP[13], TEMP[15].xyxy\n"
/* Fetch texels */
"MOV TEMP[2].z, IMM[1].wwww\n"
"MOV TEMP[3].z, IMM[1].wwww\n"
"TEX_LZ TEMP[10].x, TEMP[2], SAMP[0], 2D_ARRAY\n"
"TEX_LZ TEMP[10].y, TEMP[3], SAMP[1], 2D_ARRAY\n"
"TEX_LZ TEMP[10].z, TEMP[3], SAMP[2], 2D_ARRAY\n"
"MOV TEMP[12].z, IMM[1].xxxx\n"
"MOV TEMP[13].z, IMM[1].xxxx\n"
"TEX_LZ TEMP[11].x, TEMP[12], SAMP[0], 2D_ARRAY\n"
"TEX_LZ TEMP[11].y, TEMP[13], SAMP[1], 2D_ARRAY\n"
"TEX_LZ TEMP[11].z, TEMP[13], SAMP[2], 2D_ARRAY\n"
"LRP TEMP[6], TEMP[14], TEMP[10], TEMP[11]\n"
"MOV TEMP[6].w, IMM[1].xxxx\n"
/* Color Space Conversion */
"DP4 TEMP[9].x, CONST[0], TEMP[6]\n"
"DP4 TEMP[9].y, CONST[1], TEMP[6]\n"
"DP4 TEMP[9].z, CONST[2], TEMP[6]\n"
"MOV TEMP[7].w, TEMP[6].zzzz\n"
"SLE TEMP[8].w, TEMP[7], CONST[3].xxxx\n"
"SGT TEMP[7].w, TEMP[7], CONST[3].yyyy\n"
"MAX TEMP[9].w, TEMP[7], TEMP[8]\n"
"STORE IMAGE[0], TEMP[0], TEMP[9], 2D\n"
"ENDIF\n"
"END\n";
const char *compute_shader_rgba =
"COMP\n"
"PROPERTY CS_FIXED_BLOCK_WIDTH 8\n"
"PROPERTY CS_FIXED_BLOCK_HEIGHT 8\n"
"PROPERTY CS_FIXED_BLOCK_DEPTH 1\n"
"DCL SV[0], THREAD_ID\n"
"DCL SV[1], BLOCK_ID\n"
"DCL CONST[0..5]\n"
"DCL SVIEW[0], RECT, FLOAT\n"
"DCL SAMP[0]\n"
"DCL IMAGE[0], 2D, WR\n"
"DCL TEMP[0..3]\n"
"IMM[0] UINT32 { 8, 8, 1, 0}\n"
"IMM[1] FLT32 { 1.0, 2.0, 0.0, 0.0}\n"
"UMAD TEMP[0], SV[1], IMM[0], SV[0]\n"
/* Drawn area check */
"USGE TEMP[1].xy, TEMP[0].xyxy, CONST[4].xyxy\n"
"USLT TEMP[1].zw, TEMP[0].xyxy, CONST[4].zwzw\n"
"AND TEMP[1].x, TEMP[1].xxxx, TEMP[1].yyyy\n"
"AND TEMP[1].x, TEMP[1].xxxx, TEMP[1].zzzz\n"
"AND TEMP[1].x, TEMP[1].xxxx, TEMP[1].wwww\n"
"UIF TEMP[1]\n"
/* Translate */
"UADD TEMP[2].xy, TEMP[0], -CONST[5].xyxy\n"
"U2F TEMP[2], TEMP[2]\n"
/* Scale */
"DIV TEMP[2], TEMP[2], CONST[3].zwzw\n"
/* Fetch texels */
"TEX_LZ TEMP[3], TEMP[2], SAMP[0], RECT\n"
"STORE IMAGE[0], TEMP[0], TEMP[3], 2D\n"
"ENDIF\n"
"END\n";
static void
cs_launch(struct vl_compositor *c,
void *cs,
const struct u_rect *draw_area)
{
struct pipe_context *ctx = c->pipe;
/* Bind the image */
struct pipe_image_view image = {};
image.resource = c->fb_state.cbufs[0]->texture;
image.shader_access = image.access = PIPE_IMAGE_ACCESS_READ_WRITE;
image.format = c->fb_state.cbufs[0]->texture->format;
ctx->set_shader_images(c->pipe, PIPE_SHADER_COMPUTE, 0, 1, &image);
/* Bind compute shader */
ctx->bind_compute_state(ctx, cs);
/* Dispatch compute */
struct pipe_grid_info info = {};
info.block[0] = 8;
info.block[1] = 8;
info.block[2] = 1;
info.grid[0] = DIV_ROUND_UP(draw_area->x1, info.block[0]);
info.grid[1] = DIV_ROUND_UP(draw_area->y1, info.block[1]);
info.grid[2] = 1;
ctx->launch_grid(ctx, &info);
/* Make the result visible to all clients. */
ctx->memory_barrier(ctx, PIPE_BARRIER_ALL);
}
static inline struct u_rect
calc_drawn_area(struct vl_compositor_state *s,
struct vl_compositor_layer *layer)
{
struct vertex2f tl, br;
struct u_rect result;
assert(s && layer);
tl = layer->dst.tl;
br = layer->dst.br;
/* Scale */
result.x0 = tl.x * layer->viewport.scale[0] + layer->viewport.translate[0];
result.y0 = tl.y * layer->viewport.scale[1] + layer->viewport.translate[1];
result.x1 = br.x * layer->viewport.scale[0] + layer->viewport.translate[0];
result.y1 = br.y * layer->viewport.scale[1] + layer->viewport.translate[1];
/* Clip */
result.x0 = MAX2(result.x0, s->scissor.minx);
result.y0 = MAX2(result.y0, s->scissor.miny);
result.x1 = MIN2(result.x1, s->scissor.maxx);
result.y1 = MIN2(result.y1, s->scissor.maxy);
return result;
}
static bool
set_viewport(struct vl_compositor_state *s,
struct cs_viewport *drawn)
{
struct pipe_transfer *buf_transfer;
assert(s && drawn);
void *ptr = pipe_buffer_map(s->pipe, s->shader_params,
PIPE_TRANSFER_READ | PIPE_TRANSFER_WRITE,
&buf_transfer);
if (!ptr)
return false;
float *ptr_float = (float *)ptr;
ptr_float += sizeof(vl_csc_matrix)/sizeof(float) + 2;
*ptr_float++ = drawn->scale_x;
*ptr_float++ = drawn->scale_y;
int *ptr_int = (int *)ptr_float;
*ptr_int++ = drawn->area.x0;
*ptr_int++ = drawn->area.y0;
*ptr_int++ = drawn->area.x1;
*ptr_int++ = drawn->area.y1;
*ptr_int++ = drawn->translate_x;
*ptr_int++ = drawn->translate_y;
ptr_float = (float *)ptr_int;
*ptr_float++ = drawn->sampler0_w;
*ptr_float = drawn->sampler0_h;
pipe_buffer_unmap(s->pipe, buf_transfer);
return true;
}
static void
draw_layers(struct vl_compositor *c,
struct vl_compositor_state *s,
struct u_rect *dirty)
{
unsigned i;
assert(c);
for (i = 0; i < VL_COMPOSITOR_MAX_LAYERS; ++i) {
if (s->used_layers & (1 << i)) {
struct vl_compositor_layer *layer = &s->layers[i];
struct pipe_sampler_view **samplers = &layer->sampler_views[0];
unsigned num_sampler_views = !samplers[1] ? 1 : !samplers[2] ? 2 : 3;
struct cs_viewport drawn;
drawn.area = calc_drawn_area(s, layer);
drawn.scale_x = layer->viewport.scale[0] /
(float)layer->sampler_views[0]->texture->width0;
drawn.scale_y = drawn.scale_x;
drawn.translate_x = (int)layer->viewport.translate[0];
drawn.translate_y = (int)layer->viewport.translate[1];
drawn.sampler0_w = (float)layer->sampler_views[0]->texture->width0;
drawn.sampler0_h = (float)layer->sampler_views[0]->texture->height0;
set_viewport(s, &drawn);
c->pipe->bind_sampler_states(c->pipe, PIPE_SHADER_COMPUTE, 0,
num_sampler_views, layer->samplers);
c->pipe->set_sampler_views(c->pipe, PIPE_SHADER_COMPUTE, 0,
num_sampler_views, samplers);
cs_launch(c, layer->cs, &(drawn.area));
/* Unbind. */
c->pipe->set_shader_images(c->pipe, PIPE_SHADER_COMPUTE, 0, 1, NULL);
c->pipe->set_constant_buffer(c->pipe, PIPE_SHADER_COMPUTE, 0, NULL);
c->pipe->set_sampler_views(c->pipe, PIPE_SHADER_FRAGMENT, 0,
num_sampler_views, NULL);
c->pipe->bind_compute_state(c->pipe, NULL);
c->pipe->bind_sampler_states(c->pipe, PIPE_SHADER_COMPUTE, 0,
num_sampler_views, NULL);
if (dirty) {
struct u_rect drawn = calc_drawn_area(s, layer);
dirty->x0 = MIN2(drawn.x0, dirty->x0);
dirty->y0 = MIN2(drawn.y0, dirty->y0);
dirty->x1 = MAX2(drawn.x1, dirty->x1);
dirty->y1 = MAX2(drawn.y1, dirty->y1);
}
}
}
}
void *
vl_compositor_cs_create_shader(struct vl_compositor *c,
const char *compute_shader_text)
{
assert(c && compute_shader_text);
struct tgsi_token tokens[1024];
if (!tgsi_text_translate(compute_shader_text, tokens, ARRAY_SIZE(tokens))) {
assert(0);
return NULL;
}
struct pipe_compute_state state = {};
state.ir_type = PIPE_SHADER_IR_TGSI;
state.prog = tokens;
/* create compute shader */
return c->pipe->create_compute_state(c->pipe, &state);
}
void
vl_compositor_cs_render(struct vl_compositor_state *s,
struct vl_compositor *c,
struct pipe_surface *dst_surface,
struct u_rect *dirty_area,
bool clear_dirty)
{
assert(c && s);
assert(dst_surface);
c->fb_state.width = dst_surface->width;
c->fb_state.height = dst_surface->height;
c->fb_state.cbufs[0] = dst_surface;
if (!s->scissor_valid) {
s->scissor.minx = 0;
s->scissor.miny = 0;
s->scissor.maxx = dst_surface->width;
s->scissor.maxy = dst_surface->height;
}
if (clear_dirty && dirty_area &&
(dirty_area->x0 < dirty_area->x1 || dirty_area->y0 < dirty_area->y1)) {
c->pipe->clear_render_target(c->pipe, dst_surface, &s->clear_color,
0, 0, dst_surface->width, dst_surface->height, false);
dirty_area->x0 = dirty_area->y0 = VL_COMPOSITOR_MAX_DIRTY;
dirty_area->x1 = dirty_area->y1 = VL_COMPOSITOR_MIN_DIRTY;
}
pipe_set_constant_buffer(c->pipe, PIPE_SHADER_COMPUTE, 0, s->shader_params);
draw_layers(c, s, dirty_area);
}
|