2 * Copyright 2018 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "tgsi/tgsi_text.h"
27 #include "tgsi/tgsi_ureg.h"
29 void *si_get_blitter_vs(struct si_context
*sctx
, enum blitter_attrib_type type
,
32 unsigned vs_blit_property
;
36 case UTIL_BLITTER_ATTRIB_NONE
:
37 vs
= num_layers
> 1 ? &sctx
->vs_blit_pos_layered
:
39 vs_blit_property
= SI_VS_BLIT_SGPRS_POS
;
41 case UTIL_BLITTER_ATTRIB_COLOR
:
42 vs
= num_layers
> 1 ? &sctx
->vs_blit_color_layered
:
44 vs_blit_property
= SI_VS_BLIT_SGPRS_POS_COLOR
;
46 case UTIL_BLITTER_ATTRIB_TEXCOORD_XY
:
47 case UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
:
48 assert(num_layers
== 1);
49 vs
= &sctx
->vs_blit_texcoord
;
50 vs_blit_property
= SI_VS_BLIT_SGPRS_POS_TEXCOORD
;
59 struct ureg_program
*ureg
= ureg_create(PIPE_SHADER_VERTEX
);
63 /* Tell the shader to load VS inputs from SGPRs: */
64 ureg_property(ureg
, TGSI_PROPERTY_VS_BLIT_SGPRS
, vs_blit_property
);
65 ureg_property(ureg
, TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION
, true);
67 /* This is just a pass-through shader with 1-3 MOV instructions. */
69 ureg_DECL_output(ureg
, TGSI_SEMANTIC_POSITION
, 0),
70 ureg_DECL_vs_input(ureg
, 0));
72 if (type
!= UTIL_BLITTER_ATTRIB_NONE
) {
74 ureg_DECL_output(ureg
, TGSI_SEMANTIC_GENERIC
, 0),
75 ureg_DECL_vs_input(ureg
, 1));
79 struct ureg_src instance_id
=
80 ureg_DECL_system_value(ureg
, TGSI_SEMANTIC_INSTANCEID
, 0);
81 struct ureg_dst layer
=
82 ureg_DECL_output(ureg
, TGSI_SEMANTIC_LAYER
, 0);
84 ureg_MOV(ureg
, ureg_writemask(layer
, TGSI_WRITEMASK_X
),
85 ureg_scalar(instance_id
, TGSI_SWIZZLE_X
));
89 *vs
= ureg_create_shader_and_destroy(ureg
, &sctx
->b
);
94 * This is used when TCS is NULL in the VS->TCS->TES chain. In this case,
95 * VS passes its outputs to TES directly, so the fixed-function shader only
96 * has to write TESSOUTER and TESSINNER.
98 void *si_create_fixed_func_tcs(struct si_context
*sctx
)
100 struct ureg_src outer
, inner
;
101 struct ureg_dst tessouter
, tessinner
;
102 struct ureg_program
*ureg
= ureg_create(PIPE_SHADER_TESS_CTRL
);
107 outer
= ureg_DECL_system_value(ureg
,
108 TGSI_SEMANTIC_DEFAULT_TESSOUTER_SI
, 0);
109 inner
= ureg_DECL_system_value(ureg
,
110 TGSI_SEMANTIC_DEFAULT_TESSINNER_SI
, 0);
112 tessouter
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_TESSOUTER
, 0);
113 tessinner
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_TESSINNER
, 0);
115 ureg_MOV(ureg
, tessouter
, outer
);
116 ureg_MOV(ureg
, tessinner
, inner
);
119 return ureg_create_shader_and_destroy(ureg
, &sctx
->b
);
122 /* Create a compute shader implementing clear_buffer or copy_buffer. */
123 void *si_create_dma_compute_shader(struct pipe_context
*ctx
,
124 unsigned num_dwords_per_thread
,
125 bool dst_stream_cache_policy
, bool is_copy
)
127 assert(util_is_power_of_two_nonzero(num_dwords_per_thread
));
129 unsigned store_qualifier
= TGSI_MEMORY_COHERENT
| TGSI_MEMORY_RESTRICT
;
130 if (dst_stream_cache_policy
)
131 store_qualifier
|= TGSI_MEMORY_STREAM_CACHE_POLICY
;
133 /* Don't cache loads, because there is no reuse. */
134 unsigned load_qualifier
= store_qualifier
| TGSI_MEMORY_STREAM_CACHE_POLICY
;
136 unsigned num_mem_ops
= MAX2(1, num_dwords_per_thread
/ 4);
137 unsigned *inst_dwords
= alloca(num_mem_ops
* sizeof(unsigned));
139 for (unsigned i
= 0; i
< num_mem_ops
; i
++) {
140 if (i
*4 < num_dwords_per_thread
)
141 inst_dwords
[i
] = MIN2(4, num_dwords_per_thread
- i
*4);
144 struct ureg_program
*ureg
= ureg_create(PIPE_SHADER_COMPUTE
);
148 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
, 64);
149 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
, 1);
150 ureg_property(ureg
, TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
, 1);
152 struct ureg_src value
;
154 ureg_property(ureg
, TGSI_PROPERTY_CS_USER_DATA_DWORDS
, inst_dwords
[0]);
155 value
= ureg_DECL_system_value(ureg
, TGSI_SEMANTIC_CS_USER_DATA
, 0);
158 struct ureg_src tid
= ureg_DECL_system_value(ureg
, TGSI_SEMANTIC_THREAD_ID
, 0);
159 struct ureg_src blk
= ureg_DECL_system_value(ureg
, TGSI_SEMANTIC_BLOCK_ID
, 0);
160 struct ureg_dst store_addr
= ureg_writemask(ureg_DECL_temporary(ureg
), TGSI_WRITEMASK_X
);
161 struct ureg_dst load_addr
= ureg_writemask(ureg_DECL_temporary(ureg
), TGSI_WRITEMASK_X
);
162 struct ureg_dst dstbuf
= ureg_dst(ureg_DECL_buffer(ureg
, 0, false));
163 struct ureg_src srcbuf
;
164 struct ureg_src
*values
= NULL
;
167 srcbuf
= ureg_DECL_buffer(ureg
, 1, false);
168 values
= malloc(num_mem_ops
* sizeof(struct ureg_src
));
171 /* If there are multiple stores, the first store writes into 0+tid,
172 * the 2nd store writes into 64+tid, the 3rd store writes into 128+tid, etc.
174 ureg_UMAD(ureg
, store_addr
, blk
, ureg_imm1u(ureg
, 64 * num_mem_ops
), tid
);
175 /* Convert from a "store size unit" into bytes. */
176 ureg_UMUL(ureg
, store_addr
, ureg_src(store_addr
),
177 ureg_imm1u(ureg
, 4 * inst_dwords
[0]));
178 ureg_MOV(ureg
, load_addr
, ureg_src(store_addr
));
180 /* Distance between a load and a store for latency hiding. */
181 unsigned load_store_distance
= is_copy
? 8 : 0;
183 for (unsigned i
= 0; i
< num_mem_ops
+ load_store_distance
; i
++) {
184 int d
= i
- load_store_distance
;
186 if (is_copy
&& i
< num_mem_ops
) {
188 ureg_UADD(ureg
, load_addr
, ureg_src(load_addr
),
189 ureg_imm1u(ureg
, 4 * inst_dwords
[i
] * 64));
192 values
[i
] = ureg_src(ureg_DECL_temporary(ureg
));
193 struct ureg_dst dst
=
194 ureg_writemask(ureg_dst(values
[i
]),
195 u_bit_consecutive(0, inst_dwords
[i
]));
196 struct ureg_src srcs
[] = {srcbuf
, ureg_src(load_addr
)};
197 ureg_memory_insn(ureg
, TGSI_OPCODE_LOAD
, &dst
, 1, srcs
, 2,
198 load_qualifier
, TGSI_TEXTURE_BUFFER
, 0);
203 ureg_UADD(ureg
, store_addr
, ureg_src(store_addr
),
204 ureg_imm1u(ureg
, 4 * inst_dwords
[d
] * 64));
207 struct ureg_dst dst
=
208 ureg_writemask(dstbuf
, u_bit_consecutive(0, inst_dwords
[d
]));
209 struct ureg_src srcs
[] =
210 {ureg_src(store_addr
), is_copy
? values
[d
] : value
};
211 ureg_memory_insn(ureg
, TGSI_OPCODE_STORE
, &dst
, 1, srcs
, 2,
212 store_qualifier
, TGSI_TEXTURE_BUFFER
, 0);
217 struct pipe_compute_state state
= {};
218 state
.ir_type
= PIPE_SHADER_IR_TGSI
;
219 state
.prog
= ureg_get_tokens(ureg
, NULL
);
221 void *cs
= ctx
->create_compute_state(ctx
, &state
);
227 /* Create the compute shader that is used to collect the results.
229 * One compute grid with a single thread is launched for every query result
230 * buffer. The thread (optionally) reads a previous summary buffer, then
231 * accumulates data from the query result buffer, and writes the result either
232 * to a summary buffer to be consumed by the next grid invocation or to the
233 * user-supplied buffer.
239 * 0.y = result_stride
242 * 1: read previously accumulated values
243 * 2: write accumulated values for chaining
244 * 4: write result available
245 * 8: convert result to boolean (0/1)
246 * 16: only read one dword and use that as result
247 * 32: apply timestamp conversion
248 * 64: store full 64 bits result
249 * 128: store signed 32 bits result
250 * 256: SO_OVERFLOW mode: take the difference of two successive half-pairs
255 * BUFFER[0] = query result buffer
256 * BUFFER[1] = previous summary buffer
257 * BUFFER[2] = next summary buffer or user-supplied buffer
259 void *si_create_query_result_cs(struct si_context
*sctx
)
261 /* TEMP[0].xy = accumulated result so far
262 * TEMP[0].z = result not available
264 * TEMP[1].x = current result index
265 * TEMP[1].y = current pair index
267 static const char text_tmpl
[] =
269 "PROPERTY CS_FIXED_BLOCK_WIDTH 1\n"
270 "PROPERTY CS_FIXED_BLOCK_HEIGHT 1\n"
271 "PROPERTY CS_FIXED_BLOCK_DEPTH 1\n"
275 "DCL CONST[0][0..1]\n"
277 "IMM[0] UINT32 {0, 31, 2147483647, 4294967295}\n"
278 "IMM[1] UINT32 {1, 2, 4, 8}\n"
279 "IMM[2] UINT32 {16, 32, 64, 128}\n"
280 "IMM[3] UINT32 {1000000, 0, %u, 0}\n" /* for timestamp conversion */
281 "IMM[4] UINT32 {256, 0, 0, 0}\n"
283 "AND TEMP[5], CONST[0][0].wwww, IMM[2].xxxx\n"
285 /* Check result availability. */
286 "LOAD TEMP[1].x, BUFFER[0], CONST[0][1].xxxx\n"
287 "ISHR TEMP[0].z, TEMP[1].xxxx, IMM[0].yyyy\n"
288 "MOV TEMP[1], TEMP[0].zzzz\n"
289 "NOT TEMP[0].z, TEMP[0].zzzz\n"
291 /* Load result if available. */
293 "LOAD TEMP[0].xy, BUFFER[0], IMM[0].xxxx\n"
296 /* Load previously accumulated result if requested. */
297 "MOV TEMP[0], IMM[0].xxxx\n"
298 "AND TEMP[4], CONST[0][0].wwww, IMM[1].xxxx\n"
300 "LOAD TEMP[0].xyz, BUFFER[1], IMM[0].xxxx\n"
303 "MOV TEMP[1].x, IMM[0].xxxx\n"
305 /* Break if accumulated result so far is not available. */
310 /* Break if result_index >= result_count. */
311 "USGE TEMP[5], TEMP[1].xxxx, CONST[0][0].zzzz\n"
316 /* Load fence and check result availability */
317 "UMAD TEMP[5].x, TEMP[1].xxxx, CONST[0][0].yyyy, CONST[0][1].xxxx\n"
318 "LOAD TEMP[5].x, BUFFER[0], TEMP[5].xxxx\n"
319 "ISHR TEMP[0].z, TEMP[5].xxxx, IMM[0].yyyy\n"
320 "NOT TEMP[0].z, TEMP[0].zzzz\n"
325 "MOV TEMP[1].y, IMM[0].xxxx\n"
327 /* Load start and end. */
328 "UMUL TEMP[5].x, TEMP[1].xxxx, CONST[0][0].yyyy\n"
329 "UMAD TEMP[5].x, TEMP[1].yyyy, CONST[0][1].yyyy, TEMP[5].xxxx\n"
330 "LOAD TEMP[2].xy, BUFFER[0], TEMP[5].xxxx\n"
332 "UADD TEMP[5].y, TEMP[5].xxxx, CONST[0][0].xxxx\n"
333 "LOAD TEMP[3].xy, BUFFER[0], TEMP[5].yyyy\n"
335 "U64ADD TEMP[4].xy, TEMP[3], -TEMP[2]\n"
337 "AND TEMP[5].z, CONST[0][0].wwww, IMM[4].xxxx\n"
339 /* Load second start/end half-pair and
340 * take the difference
342 "UADD TEMP[5].xy, TEMP[5], IMM[1].wwww\n"
343 "LOAD TEMP[2].xy, BUFFER[0], TEMP[5].xxxx\n"
344 "LOAD TEMP[3].xy, BUFFER[0], TEMP[5].yyyy\n"
346 "U64ADD TEMP[3].xy, TEMP[3], -TEMP[2]\n"
347 "U64ADD TEMP[4].xy, TEMP[4], -TEMP[3]\n"
350 "U64ADD TEMP[0].xy, TEMP[0], TEMP[4]\n"
352 /* Increment pair index */
353 "UADD TEMP[1].y, TEMP[1].yyyy, IMM[1].xxxx\n"
354 "USGE TEMP[5], TEMP[1].yyyy, CONST[0][1].zzzz\n"
360 /* Increment result index */
361 "UADD TEMP[1].x, TEMP[1].xxxx, IMM[1].xxxx\n"
365 "AND TEMP[4], CONST[0][0].wwww, IMM[1].yyyy\n"
367 /* Store accumulated data for chaining. */
368 "STORE BUFFER[2].xyz, IMM[0].xxxx, TEMP[0]\n"
370 "AND TEMP[4], CONST[0][0].wwww, IMM[1].zzzz\n"
372 /* Store result availability. */
373 "NOT TEMP[0].z, TEMP[0]\n"
374 "AND TEMP[0].z, TEMP[0].zzzz, IMM[1].xxxx\n"
375 "STORE BUFFER[2].x, IMM[0].xxxx, TEMP[0].zzzz\n"
377 "AND TEMP[4], CONST[0][0].wwww, IMM[2].zzzz\n"
379 "STORE BUFFER[2].y, IMM[0].xxxx, IMM[0].xxxx\n"
382 /* Store result if it is available. */
383 "NOT TEMP[4], TEMP[0].zzzz\n"
385 /* Apply timestamp conversion */
386 "AND TEMP[4], CONST[0][0].wwww, IMM[2].yyyy\n"
388 "U64MUL TEMP[0].xy, TEMP[0], IMM[3].xyxy\n"
389 "U64DIV TEMP[0].xy, TEMP[0], IMM[3].zwzw\n"
392 /* Convert to boolean */
393 "AND TEMP[4], CONST[0][0].wwww, IMM[1].wwww\n"
395 "U64SNE TEMP[0].x, TEMP[0].xyxy, IMM[4].zwzw\n"
396 "AND TEMP[0].x, TEMP[0].xxxx, IMM[1].xxxx\n"
397 "MOV TEMP[0].y, IMM[0].xxxx\n"
400 "AND TEMP[4], CONST[0][0].wwww, IMM[2].zzzz\n"
402 "STORE BUFFER[2].xy, IMM[0].xxxx, TEMP[0].xyxy\n"
406 "MOV TEMP[0].x, IMM[0].wwww\n"
409 "AND TEMP[4], CONST[0][0].wwww, IMM[2].wwww\n"
411 "UMIN TEMP[0].x, TEMP[0].xxxx, IMM[0].zzzz\n"
414 "STORE BUFFER[2].x, IMM[0].xxxx, TEMP[0].xxxx\n"
422 char text
[sizeof(text_tmpl
) + 32];
423 struct tgsi_token tokens
[1024];
424 struct pipe_compute_state state
= {};
426 /* Hard code the frequency into the shader so that the backend can
427 * use the full range of optimizations for divide-by-constant.
429 snprintf(text
, sizeof(text
), text_tmpl
,
430 sctx
->screen
->info
.clock_crystal_freq
);
432 if (!tgsi_text_translate(text
, tokens
, ARRAY_SIZE(tokens
))) {
437 state
.ir_type
= PIPE_SHADER_IR_TGSI
;
440 return sctx
->b
.create_compute_state(&sctx
->b
, &state
);