blob: e27a94a2e259fa91a15aceeed27a3b63f7508ac7 [file] [log] [blame]
Marek Olšákac72a6b2018-08-02 17:40:05 -04001/*
2 * Copyright 2018 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 */
24
25#include "si_pipe.h"
26#include "tgsi/tgsi_text.h"
27#include "tgsi/tgsi_ureg.h"
28
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010029void *si_get_blitter_vs(struct si_context *sctx, enum blitter_attrib_type type, unsigned num_layers)
Marek Olšákac72a6b2018-08-02 17:40:05 -040030{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010031 unsigned vs_blit_property;
32 void **vs;
Marek Olšákac72a6b2018-08-02 17:40:05 -040033
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010034 switch (type) {
35 case UTIL_BLITTER_ATTRIB_NONE:
36 vs = num_layers > 1 ? &sctx->vs_blit_pos_layered : &sctx->vs_blit_pos;
37 vs_blit_property = SI_VS_BLIT_SGPRS_POS;
38 break;
39 case UTIL_BLITTER_ATTRIB_COLOR:
40 vs = num_layers > 1 ? &sctx->vs_blit_color_layered : &sctx->vs_blit_color;
41 vs_blit_property = SI_VS_BLIT_SGPRS_POS_COLOR;
42 break;
43 case UTIL_BLITTER_ATTRIB_TEXCOORD_XY:
44 case UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW:
45 assert(num_layers == 1);
46 vs = &sctx->vs_blit_texcoord;
47 vs_blit_property = SI_VS_BLIT_SGPRS_POS_TEXCOORD;
48 break;
49 default:
50 assert(0);
51 return NULL;
52 }
53 if (*vs)
54 return *vs;
Marek Olšákac72a6b2018-08-02 17:40:05 -040055
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010056 struct ureg_program *ureg = ureg_create(PIPE_SHADER_VERTEX);
57 if (!ureg)
58 return NULL;
Marek Olšákac72a6b2018-08-02 17:40:05 -040059
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010060 /* Tell the shader to load VS inputs from SGPRs: */
61 ureg_property(ureg, TGSI_PROPERTY_VS_BLIT_SGPRS_AMD, vs_blit_property);
62 ureg_property(ureg, TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION, true);
Marek Olšákac72a6b2018-08-02 17:40:05 -040063
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010064 /* This is just a pass-through shader with 1-3 MOV instructions. */
65 ureg_MOV(ureg, ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0), ureg_DECL_vs_input(ureg, 0));
Marek Olšákac72a6b2018-08-02 17:40:05 -040066
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010067 if (type != UTIL_BLITTER_ATTRIB_NONE) {
68 ureg_MOV(ureg, ureg_DECL_output(ureg, TGSI_SEMANTIC_GENERIC, 0), ureg_DECL_vs_input(ureg, 1));
69 }
Marek Olšákac72a6b2018-08-02 17:40:05 -040070
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010071 if (num_layers > 1) {
72 struct ureg_src instance_id = ureg_DECL_system_value(ureg, TGSI_SEMANTIC_INSTANCEID, 0);
73 struct ureg_dst layer = ureg_DECL_output(ureg, TGSI_SEMANTIC_LAYER, 0);
Marek Olšákac72a6b2018-08-02 17:40:05 -040074
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010075 ureg_MOV(ureg, ureg_writemask(layer, TGSI_WRITEMASK_X),
76 ureg_scalar(instance_id, TGSI_SWIZZLE_X));
77 }
78 ureg_END(ureg);
Marek Olšákac72a6b2018-08-02 17:40:05 -040079
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010080 *vs = ureg_create_shader_and_destroy(ureg, &sctx->b);
81 return *vs;
Marek Olšákac72a6b2018-08-02 17:40:05 -040082}
83
84/**
85 * This is used when TCS is NULL in the VS->TCS->TES chain. In this case,
86 * VS passes its outputs to TES directly, so the fixed-function shader only
87 * has to write TESSOUTER and TESSINNER.
88 */
89void *si_create_fixed_func_tcs(struct si_context *sctx)
90{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010091 struct ureg_src outer, inner;
92 struct ureg_dst tessouter, tessinner;
93 struct ureg_program *ureg = ureg_create(PIPE_SHADER_TESS_CTRL);
Marek Olšákac72a6b2018-08-02 17:40:05 -040094
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010095 if (!ureg)
96 return NULL;
Marek Olšákac72a6b2018-08-02 17:40:05 -040097
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010098 outer = ureg_DECL_system_value(ureg, TGSI_SEMANTIC_TESS_DEFAULT_OUTER_LEVEL, 0);
99 inner = ureg_DECL_system_value(ureg, TGSI_SEMANTIC_TESS_DEFAULT_INNER_LEVEL, 0);
Marek Olšákac72a6b2018-08-02 17:40:05 -0400100
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100101 tessouter = ureg_DECL_output(ureg, TGSI_SEMANTIC_TESSOUTER, 0);
102 tessinner = ureg_DECL_output(ureg, TGSI_SEMANTIC_TESSINNER, 0);
Marek Olšákac72a6b2018-08-02 17:40:05 -0400103
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100104 ureg_MOV(ureg, tessouter, outer);
105 ureg_MOV(ureg, tessinner, inner);
106 ureg_END(ureg);
Marek Olšákac72a6b2018-08-02 17:40:05 -0400107
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100108 return ureg_create_shader_and_destroy(ureg, &sctx->b);
Marek Olšákac72a6b2018-08-02 17:40:05 -0400109}
110
Marek Olšák93b8b982018-08-02 18:15:48 -0400111/* Create a compute shader implementing clear_buffer or copy_buffer. */
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100112void *si_create_dma_compute_shader(struct pipe_context *ctx, unsigned num_dwords_per_thread,
113 bool dst_stream_cache_policy, bool is_copy)
Marek Olšák93b8b982018-08-02 18:15:48 -0400114{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100115 struct si_screen *sscreen = (struct si_screen *)ctx->screen;
116 assert(util_is_power_of_two_nonzero(num_dwords_per_thread));
Marek Olšák93b8b982018-08-02 18:15:48 -0400117
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100118 unsigned store_qualifier = TGSI_MEMORY_COHERENT | TGSI_MEMORY_RESTRICT;
119 if (dst_stream_cache_policy)
120 store_qualifier |= TGSI_MEMORY_STREAM_CACHE_POLICY;
Marek Olšák93b8b982018-08-02 18:15:48 -0400121
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100122 /* Don't cache loads, because there is no reuse. */
123 unsigned load_qualifier = store_qualifier | TGSI_MEMORY_STREAM_CACHE_POLICY;
Marek Olšák93b8b982018-08-02 18:15:48 -0400124
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100125 unsigned num_mem_ops = MAX2(1, num_dwords_per_thread / 4);
126 unsigned *inst_dwords = alloca(num_mem_ops * sizeof(unsigned));
Marek Olšák93b8b982018-08-02 18:15:48 -0400127
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100128 for (unsigned i = 0; i < num_mem_ops; i++) {
129 if (i * 4 < num_dwords_per_thread)
130 inst_dwords[i] = MIN2(4, num_dwords_per_thread - i * 4);
131 }
Marek Olšák93b8b982018-08-02 18:15:48 -0400132
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100133 struct ureg_program *ureg = ureg_create(PIPE_SHADER_COMPUTE);
134 if (!ureg)
135 return NULL;
Marek Olšák93b8b982018-08-02 18:15:48 -0400136
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100137 ureg_property(ureg, TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH, sscreen->compute_wave_size);
138 ureg_property(ureg, TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT, 1);
139 ureg_property(ureg, TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH, 1);
Marek Olšák93b8b982018-08-02 18:15:48 -0400140
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100141 struct ureg_src value;
142 if (!is_copy) {
143 ureg_property(ureg, TGSI_PROPERTY_CS_USER_DATA_COMPONENTS_AMD, inst_dwords[0]);
144 value = ureg_DECL_system_value(ureg, TGSI_SEMANTIC_CS_USER_DATA_AMD, 0);
145 }
Marek Olšák93b8b982018-08-02 18:15:48 -0400146
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100147 struct ureg_src tid = ureg_DECL_system_value(ureg, TGSI_SEMANTIC_THREAD_ID, 0);
148 struct ureg_src blk = ureg_DECL_system_value(ureg, TGSI_SEMANTIC_BLOCK_ID, 0);
149 struct ureg_dst store_addr = ureg_writemask(ureg_DECL_temporary(ureg), TGSI_WRITEMASK_X);
150 struct ureg_dst load_addr = ureg_writemask(ureg_DECL_temporary(ureg), TGSI_WRITEMASK_X);
151 struct ureg_dst dstbuf = ureg_dst(ureg_DECL_buffer(ureg, 0, false));
152 struct ureg_src srcbuf;
153 struct ureg_src *values = NULL;
Marek Olšák93b8b982018-08-02 18:15:48 -0400154
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100155 if (is_copy) {
156 srcbuf = ureg_DECL_buffer(ureg, 1, false);
157 values = malloc(num_mem_ops * sizeof(struct ureg_src));
158 }
Marek Olšák93b8b982018-08-02 18:15:48 -0400159
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100160 /* If there are multiple stores, the first store writes into 0*wavesize+tid,
161 * the 2nd store writes into 1*wavesize+tid, the 3rd store writes into 2*wavesize+tid, etc.
162 */
163 ureg_UMAD(ureg, store_addr, blk, ureg_imm1u(ureg, sscreen->compute_wave_size * num_mem_ops),
164 tid);
165 /* Convert from a "store size unit" into bytes. */
166 ureg_UMUL(ureg, store_addr, ureg_src(store_addr), ureg_imm1u(ureg, 4 * inst_dwords[0]));
167 ureg_MOV(ureg, load_addr, ureg_src(store_addr));
Marek Olšák93b8b982018-08-02 18:15:48 -0400168
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100169 /* Distance between a load and a store for latency hiding. */
170 unsigned load_store_distance = is_copy ? 8 : 0;
Marek Olšák93b8b982018-08-02 18:15:48 -0400171
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100172 for (unsigned i = 0; i < num_mem_ops + load_store_distance; i++) {
173 int d = i - load_store_distance;
Marek Olšák93b8b982018-08-02 18:15:48 -0400174
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100175 if (is_copy && i < num_mem_ops) {
176 if (i) {
177 ureg_UADD(ureg, load_addr, ureg_src(load_addr),
178 ureg_imm1u(ureg, 4 * inst_dwords[i] * sscreen->compute_wave_size));
179 }
Marek Olšák93b8b982018-08-02 18:15:48 -0400180
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100181 values[i] = ureg_src(ureg_DECL_temporary(ureg));
182 struct ureg_dst dst =
183 ureg_writemask(ureg_dst(values[i]), u_bit_consecutive(0, inst_dwords[i]));
184 struct ureg_src srcs[] = {srcbuf, ureg_src(load_addr)};
185 ureg_memory_insn(ureg, TGSI_OPCODE_LOAD, &dst, 1, srcs, 2, load_qualifier,
186 TGSI_TEXTURE_BUFFER, 0);
187 }
Marek Olšák93b8b982018-08-02 18:15:48 -0400188
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100189 if (d >= 0) {
190 if (d) {
191 ureg_UADD(ureg, store_addr, ureg_src(store_addr),
192 ureg_imm1u(ureg, 4 * inst_dwords[d] * sscreen->compute_wave_size));
193 }
Marek Olšák93b8b982018-08-02 18:15:48 -0400194
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100195 struct ureg_dst dst = ureg_writemask(dstbuf, u_bit_consecutive(0, inst_dwords[d]));
196 struct ureg_src srcs[] = {ureg_src(store_addr), is_copy ? values[d] : value};
197 ureg_memory_insn(ureg, TGSI_OPCODE_STORE, &dst, 1, srcs, 2, store_qualifier,
198 TGSI_TEXTURE_BUFFER, 0);
199 }
200 }
201 ureg_END(ureg);
Marek Olšák93b8b982018-08-02 18:15:48 -0400202
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100203 struct pipe_compute_state state = {};
204 state.ir_type = PIPE_SHADER_IR_TGSI;
205 state.prog = ureg_get_tokens(ureg, NULL);
Marek Olšák93b8b982018-08-02 18:15:48 -0400206
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100207 void *cs = ctx->create_compute_state(ctx, &state);
208 ureg_destroy(ureg);
209 ureg_free_tokens(state.prog);
Gert Wollnyf1f36402019-01-31 14:50:41 +0100210
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100211 free(values);
212 return cs;
Marek Olšák93b8b982018-08-02 18:15:48 -0400213}
214
Marek Olšák1f213962019-01-04 19:39:01 -0500215/* Create a compute shader that copies DCC from one buffer to another
216 * where each DCC buffer has a different layout.
217 *
218 * image[0]: offset remap table (pairs of <src_offset, dst_offset>),
219 * 2 pairs are read
220 * image[1]: DCC source buffer, typed r8_uint
221 * image[2]: DCC destination buffer, typed r8_uint
222 */
223void *si_create_dcc_retile_cs(struct pipe_context *ctx)
224{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100225 struct ureg_program *ureg = ureg_create(PIPE_SHADER_COMPUTE);
226 if (!ureg)
227 return NULL;
Marek Olšák1f213962019-01-04 19:39:01 -0500228
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100229 ureg_property(ureg, TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH, 64);
230 ureg_property(ureg, TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT, 1);
231 ureg_property(ureg, TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH, 1);
Marek Olšák1f213962019-01-04 19:39:01 -0500232
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100233 /* Compute the global thread ID (in idx). */
234 struct ureg_src tid = ureg_DECL_system_value(ureg, TGSI_SEMANTIC_THREAD_ID, 0);
235 struct ureg_src blk = ureg_DECL_system_value(ureg, TGSI_SEMANTIC_BLOCK_ID, 0);
236 struct ureg_dst idx = ureg_writemask(ureg_DECL_temporary(ureg), TGSI_WRITEMASK_X);
237 ureg_UMAD(ureg, idx, blk, ureg_imm1u(ureg, 64), tid);
Marek Olšák1f213962019-01-04 19:39:01 -0500238
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100239 /* Load 2 pairs of offsets for DCC load & store. */
240 struct ureg_src map = ureg_DECL_image(ureg, 0, TGSI_TEXTURE_BUFFER, 0, false, false);
241 struct ureg_dst offsets = ureg_DECL_temporary(ureg);
242 struct ureg_src map_load_args[] = {map, ureg_src(idx)};
Marek Olšák1f213962019-01-04 19:39:01 -0500243
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100244 ureg_memory_insn(ureg, TGSI_OPCODE_LOAD, &offsets, 1, map_load_args, 2, TGSI_MEMORY_RESTRICT,
245 TGSI_TEXTURE_BUFFER, 0);
Marek Olšák1f213962019-01-04 19:39:01 -0500246
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100247 struct ureg_src dcc_src = ureg_DECL_image(ureg, 1, TGSI_TEXTURE_BUFFER, 0, false, false);
248 struct ureg_dst dcc_dst =
249 ureg_dst(ureg_DECL_image(ureg, 2, TGSI_TEXTURE_BUFFER, 0, true, false));
250 struct ureg_dst dcc_value[2];
Marek Olšák1f213962019-01-04 19:39:01 -0500251
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100252 /* Copy DCC values:
253 * dst[offsets.y] = src[offsets.x];
254 * dst[offsets.w] = src[offsets.z];
255 */
256 for (unsigned i = 0; i < 2; i++) {
257 dcc_value[i] = ureg_writemask(ureg_DECL_temporary(ureg), TGSI_WRITEMASK_X);
Marek Olšák1f213962019-01-04 19:39:01 -0500258
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100259 struct ureg_src load_args[] = {dcc_src,
260 ureg_scalar(ureg_src(offsets), TGSI_SWIZZLE_X + i * 2)};
261 ureg_memory_insn(ureg, TGSI_OPCODE_LOAD, &dcc_value[i], 1, load_args, 2, TGSI_MEMORY_RESTRICT,
262 TGSI_TEXTURE_BUFFER, 0);
263 }
Marek Olšák1f213962019-01-04 19:39:01 -0500264
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100265 dcc_dst = ureg_writemask(dcc_dst, TGSI_WRITEMASK_X);
Marek Olšák1f213962019-01-04 19:39:01 -0500266
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100267 for (unsigned i = 0; i < 2; i++) {
268 struct ureg_src store_args[] = {ureg_scalar(ureg_src(offsets), TGSI_SWIZZLE_Y + i * 2),
269 ureg_src(dcc_value[i])};
270 ureg_memory_insn(ureg, TGSI_OPCODE_STORE, &dcc_dst, 1, store_args, 2, TGSI_MEMORY_RESTRICT,
271 TGSI_TEXTURE_BUFFER, 0);
272 }
273 ureg_END(ureg);
Marek Olšák1f213962019-01-04 19:39:01 -0500274
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100275 struct pipe_compute_state state = {};
276 state.ir_type = PIPE_SHADER_IR_TGSI;
277 state.prog = ureg_get_tokens(ureg, NULL);
Marek Olšák1f213962019-01-04 19:39:01 -0500278
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100279 void *cs = ctx->create_compute_state(ctx, &state);
280 ureg_destroy(ureg);
Marek Olšákb7659c52020-11-22 03:18:18 -0500281 ureg_free_tokens(state.prog);
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100282 return cs;
Marek Olšák1f213962019-01-04 19:39:01 -0500283}
284
Marek Olšákac72a6b2018-08-02 17:40:05 -0400285/* Create the compute shader that is used to collect the results.
286 *
287 * One compute grid with a single thread is launched for every query result
288 * buffer. The thread (optionally) reads a previous summary buffer, then
289 * accumulates data from the query result buffer, and writes the result either
290 * to a summary buffer to be consumed by the next grid invocation or to the
291 * user-supplied buffer.
292 *
293 * Data layout:
294 *
295 * CONST
296 * 0.x = end_offset
297 * 0.y = result_stride
298 * 0.z = result_count
299 * 0.w = bit field:
300 * 1: read previously accumulated values
301 * 2: write accumulated values for chaining
302 * 4: write result available
303 * 8: convert result to boolean (0/1)
304 * 16: only read one dword and use that as result
305 * 32: apply timestamp conversion
306 * 64: store full 64 bits result
307 * 128: store signed 32 bits result
308 * 256: SO_OVERFLOW mode: take the difference of two successive half-pairs
309 * 1.x = fence_offset
310 * 1.y = pair_stride
311 * 1.z = pair_count
312 *
313 * BUFFER[0] = query result buffer
314 * BUFFER[1] = previous summary buffer
315 * BUFFER[2] = next summary buffer or user-supplied buffer
316 */
317void *si_create_query_result_cs(struct si_context *sctx)
318{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100319 /* TEMP[0].xy = accumulated result so far
320 * TEMP[0].z = result not available
321 *
322 * TEMP[1].x = current result index
323 * TEMP[1].y = current pair index
324 */
325 static const char text_tmpl[] =
326 "COMP\n"
327 "PROPERTY CS_FIXED_BLOCK_WIDTH 1\n"
328 "PROPERTY CS_FIXED_BLOCK_HEIGHT 1\n"
329 "PROPERTY CS_FIXED_BLOCK_DEPTH 1\n"
330 "DCL BUFFER[0]\n"
331 "DCL BUFFER[1]\n"
332 "DCL BUFFER[2]\n"
333 "DCL CONST[0][0..1]\n"
334 "DCL TEMP[0..5]\n"
335 "IMM[0] UINT32 {0, 31, 2147483647, 4294967295}\n"
336 "IMM[1] UINT32 {1, 2, 4, 8}\n"
337 "IMM[2] UINT32 {16, 32, 64, 128}\n"
338 "IMM[3] UINT32 {1000000, 0, %u, 0}\n" /* for timestamp conversion */
339 "IMM[4] UINT32 {256, 0, 0, 0}\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400340
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100341 "AND TEMP[5], CONST[0][0].wwww, IMM[2].xxxx\n"
342 "UIF TEMP[5]\n"
343 /* Check result availability. */
344 "LOAD TEMP[1].x, BUFFER[0], CONST[0][1].xxxx\n"
345 "ISHR TEMP[0].z, TEMP[1].xxxx, IMM[0].yyyy\n"
346 "MOV TEMP[1], TEMP[0].zzzz\n"
347 "NOT TEMP[0].z, TEMP[0].zzzz\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400348
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100349 /* Load result if available. */
350 "UIF TEMP[1]\n"
351 "LOAD TEMP[0].xy, BUFFER[0], IMM[0].xxxx\n"
352 "ENDIF\n"
353 "ELSE\n"
354 /* Load previously accumulated result if requested. */
355 "MOV TEMP[0], IMM[0].xxxx\n"
356 "AND TEMP[4], CONST[0][0].wwww, IMM[1].xxxx\n"
357 "UIF TEMP[4]\n"
358 "LOAD TEMP[0].xyz, BUFFER[1], IMM[0].xxxx\n"
359 "ENDIF\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400360
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100361 "MOV TEMP[1].x, IMM[0].xxxx\n"
362 "BGNLOOP\n"
363 /* Break if accumulated result so far is not available. */
364 "UIF TEMP[0].zzzz\n"
365 "BRK\n"
366 "ENDIF\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400367
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100368 /* Break if result_index >= result_count. */
369 "USGE TEMP[5], TEMP[1].xxxx, CONST[0][0].zzzz\n"
370 "UIF TEMP[5]\n"
371 "BRK\n"
372 "ENDIF\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400373
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100374 /* Load fence and check result availability */
375 "UMAD TEMP[5].x, TEMP[1].xxxx, CONST[0][0].yyyy, CONST[0][1].xxxx\n"
376 "LOAD TEMP[5].x, BUFFER[0], TEMP[5].xxxx\n"
377 "ISHR TEMP[0].z, TEMP[5].xxxx, IMM[0].yyyy\n"
378 "NOT TEMP[0].z, TEMP[0].zzzz\n"
379 "UIF TEMP[0].zzzz\n"
380 "BRK\n"
381 "ENDIF\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400382
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100383 "MOV TEMP[1].y, IMM[0].xxxx\n"
384 "BGNLOOP\n"
385 /* Load start and end. */
386 "UMUL TEMP[5].x, TEMP[1].xxxx, CONST[0][0].yyyy\n"
387 "UMAD TEMP[5].x, TEMP[1].yyyy, CONST[0][1].yyyy, TEMP[5].xxxx\n"
388 "LOAD TEMP[2].xy, BUFFER[0], TEMP[5].xxxx\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400389
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100390 "UADD TEMP[5].y, TEMP[5].xxxx, CONST[0][0].xxxx\n"
391 "LOAD TEMP[3].xy, BUFFER[0], TEMP[5].yyyy\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400392
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100393 "U64ADD TEMP[4].xy, TEMP[3], -TEMP[2]\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400394
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100395 "AND TEMP[5].z, CONST[0][0].wwww, IMM[4].xxxx\n"
396 "UIF TEMP[5].zzzz\n"
397 /* Load second start/end half-pair and
398 * take the difference
399 */
400 "UADD TEMP[5].xy, TEMP[5], IMM[1].wwww\n"
401 "LOAD TEMP[2].xy, BUFFER[0], TEMP[5].xxxx\n"
402 "LOAD TEMP[3].xy, BUFFER[0], TEMP[5].yyyy\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400403
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100404 "U64ADD TEMP[3].xy, TEMP[3], -TEMP[2]\n"
405 "U64ADD TEMP[4].xy, TEMP[4], -TEMP[3]\n"
406 "ENDIF\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400407
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100408 "U64ADD TEMP[0].xy, TEMP[0], TEMP[4]\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400409
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100410 /* Increment pair index */
411 "UADD TEMP[1].y, TEMP[1].yyyy, IMM[1].xxxx\n"
412 "USGE TEMP[5], TEMP[1].yyyy, CONST[0][1].zzzz\n"
413 "UIF TEMP[5]\n"
414 "BRK\n"
415 "ENDIF\n"
416 "ENDLOOP\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400417
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100418 /* Increment result index */
419 "UADD TEMP[1].x, TEMP[1].xxxx, IMM[1].xxxx\n"
420 "ENDLOOP\n"
421 "ENDIF\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400422
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100423 "AND TEMP[4], CONST[0][0].wwww, IMM[1].yyyy\n"
424 "UIF TEMP[4]\n"
425 /* Store accumulated data for chaining. */
426 "STORE BUFFER[2].xyz, IMM[0].xxxx, TEMP[0]\n"
427 "ELSE\n"
428 "AND TEMP[4], CONST[0][0].wwww, IMM[1].zzzz\n"
429 "UIF TEMP[4]\n"
430 /* Store result availability. */
431 "NOT TEMP[0].z, TEMP[0]\n"
432 "AND TEMP[0].z, TEMP[0].zzzz, IMM[1].xxxx\n"
433 "STORE BUFFER[2].x, IMM[0].xxxx, TEMP[0].zzzz\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400434
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100435 "AND TEMP[4], CONST[0][0].wwww, IMM[2].zzzz\n"
436 "UIF TEMP[4]\n"
437 "STORE BUFFER[2].y, IMM[0].xxxx, IMM[0].xxxx\n"
438 "ENDIF\n"
439 "ELSE\n"
440 /* Store result if it is available. */
441 "NOT TEMP[4], TEMP[0].zzzz\n"
442 "UIF TEMP[4]\n"
443 /* Apply timestamp conversion */
444 "AND TEMP[4], CONST[0][0].wwww, IMM[2].yyyy\n"
445 "UIF TEMP[4]\n"
446 "U64MUL TEMP[0].xy, TEMP[0], IMM[3].xyxy\n"
447 "U64DIV TEMP[0].xy, TEMP[0], IMM[3].zwzw\n"
448 "ENDIF\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400449
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100450 /* Convert to boolean */
451 "AND TEMP[4], CONST[0][0].wwww, IMM[1].wwww\n"
452 "UIF TEMP[4]\n"
453 "U64SNE TEMP[0].x, TEMP[0].xyxy, IMM[4].zwzw\n"
454 "AND TEMP[0].x, TEMP[0].xxxx, IMM[1].xxxx\n"
455 "MOV TEMP[0].y, IMM[0].xxxx\n"
456 "ENDIF\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400457
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100458 "AND TEMP[4], CONST[0][0].wwww, IMM[2].zzzz\n"
459 "UIF TEMP[4]\n"
460 "STORE BUFFER[2].xy, IMM[0].xxxx, TEMP[0].xyxy\n"
461 "ELSE\n"
462 /* Clamping */
463 "UIF TEMP[0].yyyy\n"
464 "MOV TEMP[0].x, IMM[0].wwww\n"
465 "ENDIF\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400466
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100467 "AND TEMP[4], CONST[0][0].wwww, IMM[2].wwww\n"
468 "UIF TEMP[4]\n"
469 "UMIN TEMP[0].x, TEMP[0].xxxx, IMM[0].zzzz\n"
470 "ENDIF\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400471
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100472 "STORE BUFFER[2].x, IMM[0].xxxx, TEMP[0].xxxx\n"
473 "ENDIF\n"
474 "ENDIF\n"
475 "ENDIF\n"
476 "ENDIF\n"
Marek Olšákac72a6b2018-08-02 17:40:05 -0400477
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100478 "END\n";
Marek Olšákac72a6b2018-08-02 17:40:05 -0400479
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100480 char text[sizeof(text_tmpl) + 32];
481 struct tgsi_token tokens[1024];
482 struct pipe_compute_state state = {};
Marek Olšákac72a6b2018-08-02 17:40:05 -0400483
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100484 /* Hard code the frequency into the shader so that the backend can
485 * use the full range of optimizations for divide-by-constant.
486 */
487 snprintf(text, sizeof(text), text_tmpl, sctx->screen->info.clock_crystal_freq);
Marek Olšákac72a6b2018-08-02 17:40:05 -0400488
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100489 if (!tgsi_text_translate(text, tokens, ARRAY_SIZE(tokens))) {
490 assert(false);
491 return NULL;
492 }
Marek Olšákac72a6b2018-08-02 17:40:05 -0400493
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100494 state.ir_type = PIPE_SHADER_IR_TGSI;
495 state.prog = tokens;
Marek Olšákac72a6b2018-08-02 17:40:05 -0400496
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100497 return sctx->b.create_compute_state(&sctx->b, &state);
Marek Olšákac72a6b2018-08-02 17:40:05 -0400498}
Sonny Jiang1b25d342018-12-03 12:36:33 -0500499
500/* Create a compute shader implementing copy_image.
501 * Luckily, this works with all texture targets except 1D_ARRAY.
502 */
503void *si_create_copy_image_compute_shader(struct pipe_context *ctx)
504{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100505 static const char text[] =
506 "COMP\n"
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100507 "DCL SV[0], THREAD_ID\n"
508 "DCL SV[1], BLOCK_ID\n"
Marek Olšákb158b112020-05-06 14:12:27 -0400509 "DCL SV[2], BLOCK_SIZE\n"
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100510 "DCL IMAGE[0], 2D_ARRAY, PIPE_FORMAT_R32G32B32A32_FLOAT, WR\n"
511 "DCL IMAGE[1], 2D_ARRAY, PIPE_FORMAT_R32G32B32A32_FLOAT, WR\n"
512 "DCL CONST[0][0..1]\n" // 0:xyzw 1:xyzw
513 "DCL TEMP[0..4], LOCAL\n"
Marek Olšákb158b112020-05-06 14:12:27 -0400514
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100515 "MOV TEMP[0].xyz, CONST[0][0].xyzw\n"
Marek Olšákb158b112020-05-06 14:12:27 -0400516 "UMAD TEMP[1].xyz, SV[1].xyzz, SV[2].xyzz, SV[0].xyzz\n"
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100517 "UADD TEMP[2].xyz, TEMP[1].xyzx, TEMP[0].xyzx\n"
518 "LOAD TEMP[3], IMAGE[0], TEMP[2].xyzx, 2D_ARRAY, PIPE_FORMAT_R32G32B32A32_FLOAT\n"
519 "MOV TEMP[4].xyz, CONST[0][1].xyzw\n"
520 "UADD TEMP[2].xyz, TEMP[1].xyzx, TEMP[4].xyzx\n"
521 "STORE IMAGE[1], TEMP[2].xyzz, TEMP[3], 2D_ARRAY, PIPE_FORMAT_R32G32B32A32_FLOAT\n"
522 "END\n";
Sonny Jiang1b25d342018-12-03 12:36:33 -0500523
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100524 struct tgsi_token tokens[1024];
525 struct pipe_compute_state state = {0};
Sonny Jiang1b25d342018-12-03 12:36:33 -0500526
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100527 if (!tgsi_text_translate(text, tokens, ARRAY_SIZE(tokens))) {
528 assert(false);
529 return NULL;
530 }
Sonny Jiang1b25d342018-12-03 12:36:33 -0500531
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100532 state.ir_type = PIPE_SHADER_IR_TGSI;
533 state.prog = tokens;
Sonny Jiang1b25d342018-12-03 12:36:33 -0500534
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100535 return ctx->create_compute_state(ctx, &state);
Sonny Jiang1b25d342018-12-03 12:36:33 -0500536}
537
538void *si_create_copy_image_compute_shader_1d_array(struct pipe_context *ctx)
539{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100540 static const char text[] =
541 "COMP\n"
542 "PROPERTY CS_FIXED_BLOCK_WIDTH 64\n"
543 "PROPERTY CS_FIXED_BLOCK_HEIGHT 1\n"
544 "PROPERTY CS_FIXED_BLOCK_DEPTH 1\n"
545 "DCL SV[0], THREAD_ID\n"
546 "DCL SV[1], BLOCK_ID\n"
547 "DCL IMAGE[0], 1D_ARRAY, PIPE_FORMAT_R32G32B32A32_FLOAT, WR\n"
548 "DCL IMAGE[1], 1D_ARRAY, PIPE_FORMAT_R32G32B32A32_FLOAT, WR\n"
549 "DCL CONST[0][0..1]\n" // 0:xyzw 1:xyzw
550 "DCL TEMP[0..4], LOCAL\n"
551 "IMM[0] UINT32 {64, 1, 0, 0}\n"
552 "MOV TEMP[0].xy, CONST[0][0].xzzw\n"
553 "UMAD TEMP[1].xy, SV[1].xyzz, IMM[0].xyyy, SV[0].xyzz\n"
554 "UADD TEMP[2].xy, TEMP[1].xyzx, TEMP[0].xyzx\n"
555 "LOAD TEMP[3], IMAGE[0], TEMP[2].xyzx, 1D_ARRAY, PIPE_FORMAT_R32G32B32A32_FLOAT\n"
556 "MOV TEMP[4].xy, CONST[0][1].xzzw\n"
557 "UADD TEMP[2].xy, TEMP[1].xyzx, TEMP[4].xyzx\n"
558 "STORE IMAGE[1], TEMP[2].xyzz, TEMP[3], 1D_ARRAY, PIPE_FORMAT_R32G32B32A32_FLOAT\n"
559 "END\n";
Sonny Jiang1b25d342018-12-03 12:36:33 -0500560
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100561 struct tgsi_token tokens[1024];
562 struct pipe_compute_state state = {0};
Sonny Jiang1b25d342018-12-03 12:36:33 -0500563
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100564 if (!tgsi_text_translate(text, tokens, ARRAY_SIZE(tokens))) {
565 assert(false);
566 return NULL;
567 }
Sonny Jiang1b25d342018-12-03 12:36:33 -0500568
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100569 state.ir_type = PIPE_SHADER_IR_TGSI;
570 state.prog = tokens;
Sonny Jiang1b25d342018-12-03 12:36:33 -0500571
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100572 return ctx->create_compute_state(ctx, &state);
Sonny Jiang1b25d342018-12-03 12:36:33 -0500573}
Sonny Jiang984fd732019-01-21 18:16:40 -0500574
Marek Olšákd6acdbd2020-04-26 08:38:54 -0400575/* Create a compute shader implementing DCC decompression via a blit.
576 * This is a trivial copy_image shader except that it has a variable block
577 * size and a barrier.
578 */
579void *si_create_dcc_decompress_cs(struct pipe_context *ctx)
580{
581 static const char text[] =
582 "COMP\n"
583 "DCL SV[0], THREAD_ID\n"
584 "DCL SV[1], BLOCK_ID\n"
585 "DCL SV[2], BLOCK_SIZE\n"
586 "DCL IMAGE[0], 2D_ARRAY, PIPE_FORMAT_R32G32B32A32_FLOAT, WR\n"
587 "DCL IMAGE[1], 2D_ARRAY, PIPE_FORMAT_R32G32B32A32_FLOAT, WR\n"
588 "DCL TEMP[0..1]\n"
589
590 "UMAD TEMP[0].xyz, SV[1].xyzz, SV[2].xyzz, SV[0].xyzz\n"
591 "LOAD TEMP[1], IMAGE[0], TEMP[0].xyzz, 2D_ARRAY, PIPE_FORMAT_R32G32B32A32_FLOAT\n"
592 /* Wait for the whole threadgroup (= DCC block) to load texels before
593 * overwriting them, because overwriting any pixel within a DCC block
594 * can break compression for the whole block.
595 */
596 "BARRIER\n"
597 "STORE IMAGE[1], TEMP[0].xyzz, TEMP[1], 2D_ARRAY, PIPE_FORMAT_R32G32B32A32_FLOAT\n"
598 "END\n";
599
600 struct tgsi_token tokens[1024];
601 struct pipe_compute_state state = {0};
602
603 if (!tgsi_text_translate(text, tokens, ARRAY_SIZE(tokens))) {
604 assert(false);
605 return NULL;
606 }
607
608 state.ir_type = PIPE_SHADER_IR_TGSI;
609 state.prog = tokens;
610
611 return ctx->create_compute_state(ctx, &state);
612}
613
Sonny Jiang984fd732019-01-21 18:16:40 -0500614void *si_clear_render_target_shader(struct pipe_context *ctx)
615{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100616 static const char text[] =
617 "COMP\n"
618 "PROPERTY CS_FIXED_BLOCK_WIDTH 8\n"
619 "PROPERTY CS_FIXED_BLOCK_HEIGHT 8\n"
620 "PROPERTY CS_FIXED_BLOCK_DEPTH 1\n"
621 "DCL SV[0], THREAD_ID\n"
622 "DCL SV[1], BLOCK_ID\n"
623 "DCL IMAGE[0], 2D_ARRAY, PIPE_FORMAT_R32G32B32A32_FLOAT, WR\n"
624 "DCL CONST[0][0..1]\n" // 0:xyzw 1:xyzw
625 "DCL TEMP[0..3], LOCAL\n"
626 "IMM[0] UINT32 {8, 1, 0, 0}\n"
627 "MOV TEMP[0].xyz, CONST[0][0].xyzw\n"
628 "UMAD TEMP[1].xyz, SV[1].xyzz, IMM[0].xxyy, SV[0].xyzz\n"
629 "UADD TEMP[2].xyz, TEMP[1].xyzx, TEMP[0].xyzx\n"
630 "MOV TEMP[3].xyzw, CONST[0][1].xyzw\n"
631 "STORE IMAGE[0], TEMP[2].xyzz, TEMP[3], 2D_ARRAY, PIPE_FORMAT_R32G32B32A32_FLOAT\n"
632 "END\n";
Sonny Jiang984fd732019-01-21 18:16:40 -0500633
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100634 struct tgsi_token tokens[1024];
635 struct pipe_compute_state state = {0};
Sonny Jiang984fd732019-01-21 18:16:40 -0500636
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100637 if (!tgsi_text_translate(text, tokens, ARRAY_SIZE(tokens))) {
638 assert(false);
639 return NULL;
640 }
Sonny Jiang984fd732019-01-21 18:16:40 -0500641
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100642 state.ir_type = PIPE_SHADER_IR_TGSI;
643 state.prog = tokens;
Sonny Jiang984fd732019-01-21 18:16:40 -0500644
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100645 return ctx->create_compute_state(ctx, &state);
Sonny Jiang984fd732019-01-21 18:16:40 -0500646}
647
648/* TODO: Didn't really test 1D_ARRAY */
649void *si_clear_render_target_shader_1d_array(struct pipe_context *ctx)
650{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100651 static const char text[] =
652 "COMP\n"
653 "PROPERTY CS_FIXED_BLOCK_WIDTH 64\n"
654 "PROPERTY CS_FIXED_BLOCK_HEIGHT 1\n"
655 "PROPERTY CS_FIXED_BLOCK_DEPTH 1\n"
656 "DCL SV[0], THREAD_ID\n"
657 "DCL SV[1], BLOCK_ID\n"
658 "DCL IMAGE[0], 1D_ARRAY, PIPE_FORMAT_R32G32B32A32_FLOAT, WR\n"
659 "DCL CONST[0][0..1]\n" // 0:xyzw 1:xyzw
660 "DCL TEMP[0..3], LOCAL\n"
661 "IMM[0] UINT32 {64, 1, 0, 0}\n"
662 "MOV TEMP[0].xy, CONST[0][0].xzzw\n"
663 "UMAD TEMP[1].xy, SV[1].xyzz, IMM[0].xyyy, SV[0].xyzz\n"
664 "UADD TEMP[2].xy, TEMP[1].xyzx, TEMP[0].xyzx\n"
665 "MOV TEMP[3].xyzw, CONST[0][1].xyzw\n"
666 "STORE IMAGE[0], TEMP[2].xyzz, TEMP[3], 1D_ARRAY, PIPE_FORMAT_R32G32B32A32_FLOAT\n"
667 "END\n";
Sonny Jiang984fd732019-01-21 18:16:40 -0500668
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100669 struct tgsi_token tokens[1024];
670 struct pipe_compute_state state = {0};
Sonny Jiang984fd732019-01-21 18:16:40 -0500671
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100672 if (!tgsi_text_translate(text, tokens, ARRAY_SIZE(tokens))) {
673 assert(false);
674 return NULL;
675 }
Sonny Jiang984fd732019-01-21 18:16:40 -0500676
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100677 state.ir_type = PIPE_SHADER_IR_TGSI;
678 state.prog = tokens;
Sonny Jiang984fd732019-01-21 18:16:40 -0500679
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100680 return ctx->create_compute_state(ctx, &state);
Sonny Jiang984fd732019-01-21 18:16:40 -0500681}
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200682
Sonny Jiang6c901f02019-11-29 18:04:54 -0500683void *si_clear_12bytes_buffer_shader(struct pipe_context *ctx)
684{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100685 static const char text[] = "COMP\n"
686 "PROPERTY CS_FIXED_BLOCK_WIDTH 64\n"
687 "PROPERTY CS_FIXED_BLOCK_HEIGHT 1\n"
688 "PROPERTY CS_FIXED_BLOCK_DEPTH 1\n"
689 "DCL SV[0], THREAD_ID\n"
690 "DCL SV[1], BLOCK_ID\n"
691 "DCL BUFFER[0]\n"
692 "DCL CONST[0][0..0]\n" // 0:xyzw
693 "DCL TEMP[0..0]\n"
694 "IMM[0] UINT32 {64, 1, 12, 0}\n"
695 "UMAD TEMP[0].x, SV[1].xyzz, IMM[0].xyyy, SV[0].xyzz\n"
696 "UMUL TEMP[0].x, TEMP[0].xyzz, IMM[0].zzzz\n" // 12 bytes
697 "STORE BUFFER[0].xyz, TEMP[0].xxxx, CONST[0][0].xyzw\n"
698 "END\n";
Sonny Jiang6c901f02019-11-29 18:04:54 -0500699
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100700 struct tgsi_token tokens[1024];
701 struct pipe_compute_state state = {0};
Sonny Jiang6c901f02019-11-29 18:04:54 -0500702
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100703 if (!tgsi_text_translate(text, tokens, ARRAY_SIZE(tokens))) {
704 assert(false);
705 return NULL;
706 }
Sonny Jiang6c901f02019-11-29 18:04:54 -0500707
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100708 state.ir_type = PIPE_SHADER_IR_TGSI;
709 state.prog = tokens;
Sonny Jiang6c901f02019-11-29 18:04:54 -0500710
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100711 return ctx->create_compute_state(ctx, &state);
Sonny Jiang6c901f02019-11-29 18:04:54 -0500712}
713
Marek Olšák095a5822019-09-12 20:20:53 -0400714/* Load samples from the image, and copy them to the same image. This looks like
715 * a no-op, but it's not. Loads use FMASK, while stores don't, so samples are
716 * reordered to match expanded FMASK.
717 *
718 * After the shader finishes, FMASK should be cleared to identity.
719 */
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100720void *si_create_fmask_expand_cs(struct pipe_context *ctx, unsigned num_samples, bool is_array)
Marek Olšák095a5822019-09-12 20:20:53 -0400721{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100722 enum tgsi_texture_type target = is_array ? TGSI_TEXTURE_2D_ARRAY_MSAA : TGSI_TEXTURE_2D_MSAA;
723 struct ureg_program *ureg = ureg_create(PIPE_SHADER_COMPUTE);
724 if (!ureg)
725 return NULL;
Marek Olšák095a5822019-09-12 20:20:53 -0400726
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100727 ureg_property(ureg, TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH, 8);
728 ureg_property(ureg, TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT, 8);
729 ureg_property(ureg, TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH, 1);
Marek Olšák095a5822019-09-12 20:20:53 -0400730
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100731 /* Compute the image coordinates. */
732 struct ureg_src image = ureg_DECL_image(ureg, 0, target, 0, true, false);
733 struct ureg_src tid = ureg_DECL_system_value(ureg, TGSI_SEMANTIC_THREAD_ID, 0);
734 struct ureg_src blk = ureg_DECL_system_value(ureg, TGSI_SEMANTIC_BLOCK_ID, 0);
735 struct ureg_dst coord = ureg_writemask(ureg_DECL_temporary(ureg), TGSI_WRITEMASK_XYZW);
736 ureg_UMAD(ureg, ureg_writemask(coord, TGSI_WRITEMASK_XY), ureg_swizzle(blk, 0, 1, 1, 1),
737 ureg_imm2u(ureg, 8, 8), ureg_swizzle(tid, 0, 1, 1, 1));
738 if (is_array) {
739 ureg_MOV(ureg, ureg_writemask(coord, TGSI_WRITEMASK_Z), ureg_scalar(blk, TGSI_SWIZZLE_Z));
740 }
Marek Olšák095a5822019-09-12 20:20:53 -0400741
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100742 /* Load samples, resolving FMASK. */
743 struct ureg_dst sample[8];
744 assert(num_samples <= ARRAY_SIZE(sample));
Marek Olšák095a5822019-09-12 20:20:53 -0400745
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100746 for (unsigned i = 0; i < num_samples; i++) {
747 sample[i] = ureg_DECL_temporary(ureg);
Marek Olšák095a5822019-09-12 20:20:53 -0400748
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100749 ureg_MOV(ureg, ureg_writemask(coord, TGSI_WRITEMASK_W), ureg_imm1u(ureg, i));
Marek Olšák095a5822019-09-12 20:20:53 -0400750
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100751 struct ureg_src srcs[] = {image, ureg_src(coord)};
752 ureg_memory_insn(ureg, TGSI_OPCODE_LOAD, &sample[i], 1, srcs, 2, TGSI_MEMORY_RESTRICT, target,
753 0);
754 }
Marek Olšák095a5822019-09-12 20:20:53 -0400755
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100756 /* Store samples, ignoring FMASK. */
757 for (unsigned i = 0; i < num_samples; i++) {
758 ureg_MOV(ureg, ureg_writemask(coord, TGSI_WRITEMASK_W), ureg_imm1u(ureg, i));
Marek Olšák095a5822019-09-12 20:20:53 -0400759
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100760 struct ureg_dst dst_image = ureg_dst(image);
761 struct ureg_src srcs[] = {ureg_src(coord), ureg_src(sample[i])};
762 ureg_memory_insn(ureg, TGSI_OPCODE_STORE, &dst_image, 1, srcs, 2, TGSI_MEMORY_RESTRICT,
763 target, 0);
764 }
765 ureg_END(ureg);
Marek Olšák095a5822019-09-12 20:20:53 -0400766
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100767 struct pipe_compute_state state = {};
768 state.ir_type = PIPE_SHADER_IR_TGSI;
769 state.prog = ureg_get_tokens(ureg, NULL);
Marek Olšák095a5822019-09-12 20:20:53 -0400770
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100771 void *cs = ctx->create_compute_state(ctx, &state);
772 ureg_destroy(ureg);
773 return cs;
Marek Olšák095a5822019-09-12 20:20:53 -0400774}
775
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200776/* Create the compute shader that is used to collect the results of gfx10+
777 * shader queries.
778 *
779 * One compute grid with a single thread is launched for every query result
780 * buffer. The thread (optionally) reads a previous summary buffer, then
781 * accumulates data from the query result buffer, and writes the result either
782 * to a summary buffer to be consumed by the next grid invocation or to the
783 * user-supplied buffer.
784 *
785 * Data layout:
786 *
787 * BUFFER[0] = query result buffer (layout is defined by gfx10_sh_query_buffer_mem)
788 * BUFFER[1] = previous summary buffer
789 * BUFFER[2] = next summary buffer or user-supplied buffer
790 *
791 * CONST
792 * 0.x = config; the low 3 bits indicate the mode:
793 * 0: sum up counts
794 * 1: determine result availability and write it as a boolean
795 * 2: SO_OVERFLOW
796 * 3: SO_ANY_OVERFLOW
797 * the remaining bits form a bitfield:
798 * 8: write result as a 64-bit value
799 * 0.y = offset in bytes to counts or stream for SO_OVERFLOW mode
800 * 0.z = chain bit field:
801 * 1: have previous summary buffer
802 * 2: write next summary buffer
803 * 0.w = result_count
804 */
805void *gfx10_create_sh_query_result_cs(struct si_context *sctx)
806{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100807 /* TEMP[0].x = accumulated result so far
808 * TEMP[0].y = result missing
809 * TEMP[0].z = whether we're in overflow mode
810 */
811 static const char text_tmpl[] = "COMP\n"
812 "PROPERTY CS_FIXED_BLOCK_WIDTH 1\n"
813 "PROPERTY CS_FIXED_BLOCK_HEIGHT 1\n"
814 "PROPERTY CS_FIXED_BLOCK_DEPTH 1\n"
815 "DCL BUFFER[0]\n"
816 "DCL BUFFER[1]\n"
817 "DCL BUFFER[2]\n"
818 "DCL CONST[0][0..0]\n"
819 "DCL TEMP[0..5]\n"
820 "IMM[0] UINT32 {0, 7, 0, 4294967295}\n"
821 "IMM[1] UINT32 {1, 2, 4, 8}\n"
822 "IMM[2] UINT32 {16, 32, 64, 128}\n"
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200823
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100824 /*
825 acc_result = 0;
826 acc_missing = 0;
827 if (chain & 1) {
828 acc_result = buffer[1][0];
829 acc_missing = buffer[1][1];
830 }
831 */
832 "MOV TEMP[0].xy, IMM[0].xxxx\n"
833 "AND TEMP[5], CONST[0][0].zzzz, IMM[1].xxxx\n"
834 "UIF TEMP[5]\n"
835 "LOAD TEMP[0].xy, BUFFER[1], IMM[0].xxxx\n"
836 "ENDIF\n"
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200837
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100838 /*
839 is_overflow (TEMP[0].z) = (config & 7) >= 2;
840 result_remaining (TEMP[1].x) = (is_overflow && acc_result) ? 0 :
841 result_count; base_offset (TEMP[1].y) = 0; for (;;) { if
842 (!result_remaining) break; result_remaining--;
843 */
844 "AND TEMP[5].x, CONST[0][0].xxxx, IMM[0].yyyy\n"
845 "USGE TEMP[0].z, TEMP[5].xxxx, IMM[1].yyyy\n"
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200846
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100847 "AND TEMP[5].x, TEMP[0].zzzz, TEMP[0].xxxx\n"
848 "UCMP TEMP[1].x, TEMP[5].xxxx, IMM[0].xxxx, CONST[0][0].wwww\n"
849 "MOV TEMP[1].y, IMM[0].xxxx\n"
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200850
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100851 "BGNLOOP\n"
852 "USEQ TEMP[5], TEMP[1].xxxx, IMM[0].xxxx\n"
853 "UIF TEMP[5]\n"
854 "BRK\n"
855 "ENDIF\n"
856 "UADD TEMP[1].x, TEMP[1].xxxx, IMM[0].wwww\n"
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200857
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100858 /*
859 fence = buffer[0]@(base_offset + 32);
860 if (!fence) {
861 acc_missing = ~0u;
862 break;
863 }
864 */
865 "UADD TEMP[5].x, TEMP[1].yyyy, IMM[2].yyyy\n"
866 "LOAD TEMP[5].x, BUFFER[0], TEMP[5].xxxx\n"
867 "USEQ TEMP[5], TEMP[5].xxxx, IMM[0].xxxx\n"
868 "UIF TEMP[5]\n"
869 "MOV TEMP[0].y, TEMP[5].xxxx\n"
870 "BRK\n"
871 "ENDIF\n"
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200872
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100873 /*
874 stream_offset (TEMP[2].x) = base_offset + offset;
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200875
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100876 if (!(config & 7)) {
877 acc_result += buffer[0]@stream_offset;
878 }
879 */
880 "UADD TEMP[2].x, TEMP[1].yyyy, CONST[0][0].yyyy\n"
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200881
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100882 "AND TEMP[5].x, CONST[0][0].xxxx, IMM[0].yyyy\n"
883 "USEQ TEMP[5], TEMP[5].xxxx, IMM[0].xxxx\n"
884 "UIF TEMP[5]\n"
885 "LOAD TEMP[5].x, BUFFER[0], TEMP[2].xxxx\n"
886 "UADD TEMP[0].x, TEMP[0].xxxx, TEMP[5].xxxx\n"
887 "ENDIF\n"
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200888
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100889 /*
890 if ((config & 7) >= 2) {
891 count (TEMP[2].y) = (config & 1) ? 4 : 1;
892 */
893 "AND TEMP[5].x, CONST[0][0].xxxx, IMM[0].yyyy\n"
894 "USGE TEMP[5], TEMP[5].xxxx, IMM[1].yyyy\n"
895 "UIF TEMP[5]\n"
896 "AND TEMP[5].x, CONST[0][0].xxxx, IMM[1].xxxx\n"
897 "UCMP TEMP[2].y, TEMP[5].xxxx, IMM[1].zzzz, IMM[1].xxxx\n"
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200898
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100899 /*
900 do {
901 generated = buffer[0]@stream_offset;
902 emitted = buffer[0]@(stream_offset + 16);
903 if (generated != emitted) {
904 acc_result = 1;
905 result_remaining = 0;
906 break;
907 }
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200908
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100909 stream_offset += 4;
910 } while (--count);
911 */
912 "BGNLOOP\n"
913 "UADD TEMP[5].x, TEMP[2].xxxx, IMM[2].xxxx\n"
914 "LOAD TEMP[4].x, BUFFER[0], TEMP[2].xxxx\n"
915 "LOAD TEMP[4].y, BUFFER[0], TEMP[5].xxxx\n"
916 "USNE TEMP[5], TEMP[4].xxxx, TEMP[4].yyyy\n"
917 "UIF TEMP[5]\n"
918 "MOV TEMP[0].x, IMM[1].xxxx\n"
919 "MOV TEMP[1].y, IMM[0].xxxx\n"
920 "BRK\n"
921 "ENDIF\n"
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200922
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100923 "UADD TEMP[2].y, TEMP[2].yyyy, IMM[0].wwww\n"
924 "USEQ TEMP[5], TEMP[2].yyyy, IMM[0].xxxx\n"
925 "UIF TEMP[5]\n"
926 "BRK\n"
927 "ENDIF\n"
928 "UADD TEMP[2].x, TEMP[2].xxxx, IMM[1].zzzz\n"
929 "ENDLOOP\n"
930 "ENDIF\n"
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200931
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100932 /*
933 base_offset += 64;
934 } // end outer loop
935 */
936 "UADD TEMP[1].y, TEMP[1].yyyy, IMM[2].zzzz\n"
937 "ENDLOOP\n"
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200938
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100939 /*
940 if (chain & 2) {
941 buffer[2][0] = acc_result;
942 buffer[2][1] = acc_missing;
943 } else {
944 */
945 "AND TEMP[5], CONST[0][0].zzzz, IMM[1].yyyy\n"
946 "UIF TEMP[5]\n"
947 "STORE BUFFER[2].xy, IMM[0].xxxx, TEMP[0]\n"
948 "ELSE\n"
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200949
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100950 /*
951 if ((config & 7) == 1) {
952 acc_result = acc_missing ? 0 : 1;
953 acc_missing = 0;
954 }
955 */
956 "AND TEMP[5], CONST[0][0].xxxx, IMM[0].yyyy\n"
957 "USEQ TEMP[5], TEMP[5].xxxx, IMM[1].xxxx\n"
958 "UIF TEMP[5]\n"
959 "UCMP TEMP[0].x, TEMP[0].yyyy, IMM[0].xxxx, IMM[1].xxxx\n"
960 "MOV TEMP[0].y, IMM[0].xxxx\n"
961 "ENDIF\n"
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200962
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100963 /*
964 if (!acc_missing) {
965 buffer[2][0] = acc_result;
966 if (config & 8)
967 buffer[2][1] = 0;
968 }
969 */
970 "USEQ TEMP[5], TEMP[0].yyyy, IMM[0].xxxx\n"
971 "UIF TEMP[5]\n"
972 "STORE BUFFER[2].x, IMM[0].xxxx, TEMP[0].xxxx\n"
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200973
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100974 "AND TEMP[5], CONST[0][0].xxxx, IMM[1].wwww\n"
975 "UIF TEMP[5]\n"
976 "STORE BUFFER[2].x, IMM[1].zzzz, TEMP[0].yyyy\n"
977 "ENDIF\n"
978 "ENDIF\n"
979 "ENDIF\n"
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200980
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100981 "END\n";
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200982
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100983 struct tgsi_token tokens[1024];
984 struct pipe_compute_state state = {};
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200985
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100986 if (!tgsi_text_translate(text_tmpl, tokens, ARRAY_SIZE(tokens))) {
987 assert(false);
988 return NULL;
989 }
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200990
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100991 state.ir_type = PIPE_SHADER_IR_TGSI;
992 state.prog = tokens;
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200993
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100994 return sctx->b.create_compute_state(&sctx->b, &state);
Nicolai Hähnle792a6382018-09-19 14:53:35 +0200995}