1 /*
2 * Copyright (C) 2017-2018 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 /* 500 gets us LDIB but doesn't change any other a4xx instructions */
28 #define GPU 500
29
30 #include "ir3_context.h"
31 #include "ir3_image.h"
32
33 /* SSBO data is available at this CB address, addressed like regular consts
34 * containing the following data in each vec4:
35 *
36 * [ base address, pitch, array_pitch, cpp ]
37 *
38 * These mirror the values uploaded to A4XX_SSBO_0 state. For A5XX, these are
39 * uploaded manually by the driver.
40 */
41 #define A4XX_SSBO_CB_BASE(i) (0x700 + ((i) << 2))
42
43 /*
44 * Handlers for instructions changed/added in a4xx:
45 */
46
47 /* Convert byte offset to address of appropriate width for GPU */
48 static struct ir3_instruction *
byte_offset_to_address(struct ir3_context * ctx,nir_src * ssbo,struct ir3_instruction * byte_offset)49 byte_offset_to_address(struct ir3_context *ctx,
50 nir_src *ssbo,
51 struct ir3_instruction *byte_offset)
52 {
53 struct ir3_block *b = ctx->block;
54
55 if (ctx->compiler->gen == 4) {
56 uint32_t index = nir_src_as_uint(*ssbo);
57 unsigned cb = A4XX_SSBO_CB_BASE(index);
58 byte_offset = ir3_ADD_U(b, create_uniform(b, cb), 0, byte_offset, 0);
59 }
60
61 if (ctx->compiler->is_64bit) {
62 return ir3_collect(b, byte_offset, create_immed(b, 0));
63 } else {
64 return byte_offset;
65 }
66 }
67
68 /* src[] = { buffer_index, offset }. No const_index */
69 static void
emit_intrinsic_load_ssbo(struct ir3_context * ctx,nir_intrinsic_instr * intr,struct ir3_instruction ** dst)70 emit_intrinsic_load_ssbo(struct ir3_context *ctx, nir_intrinsic_instr *intr,
71 struct ir3_instruction **dst)
72 {
73 struct ir3_block *b = ctx->block;
74 struct ir3_instruction *ldgb, *src0, *src1, *byte_offset, *offset;
75
76 struct ir3_instruction *ssbo = ir3_ssbo_to_ibo(ctx, intr->src[0]);
77
78 byte_offset = ir3_get_src(ctx, &intr->src[1])[0];
79 offset = ir3_get_src(ctx, &intr->src[2])[0];
80
81 /* src0 is uvec2(offset*4, 0), src1 is offset.. nir already *= 4: */
82 src0 = byte_offset_to_address(ctx, &intr->src[0], byte_offset);
83 src1 = offset;
84
85 ldgb = ir3_LDGB(b, ssbo, 0, src0, 0, src1, 0);
86 ldgb->dsts[0]->wrmask = MASK(intr->num_components);
87 ldgb->cat6.iim_val = intr->num_components;
88 ldgb->cat6.d = 4;
89 ldgb->cat6.type = TYPE_U32;
90 ldgb->barrier_class = IR3_BARRIER_BUFFER_R;
91 ldgb->barrier_conflict = IR3_BARRIER_BUFFER_W;
92
93 ir3_split_dest(b, dst, ldgb, 0, intr->num_components);
94 }
95
96 /* src[] = { value, block_index, offset }. const_index[] = { write_mask } */
97 static void
emit_intrinsic_store_ssbo(struct ir3_context * ctx,nir_intrinsic_instr * intr)98 emit_intrinsic_store_ssbo(struct ir3_context *ctx, nir_intrinsic_instr *intr)
99 {
100 struct ir3_block *b = ctx->block;
101 struct ir3_instruction *stgb, *src0, *src1, *src2, *byte_offset, *offset;
102 unsigned wrmask = nir_intrinsic_write_mask(intr);
103 unsigned ncomp = ffs(~wrmask) - 1;
104
105 assert(wrmask == BITFIELD_MASK(intr->num_components));
106
107 struct ir3_instruction *ssbo = ir3_ssbo_to_ibo(ctx, intr->src[1]);
108
109 byte_offset = ir3_get_src(ctx, &intr->src[2])[0];
110 offset = ir3_get_src(ctx, &intr->src[3])[0];
111
112 /* src0 is value, src1 is offset, src2 is uvec2(offset*4, 0)..
113 * nir already *= 4:
114 */
115 src0 = ir3_create_collect(b, ir3_get_src(ctx, &intr->src[0]), ncomp);
116 src1 = offset;
117 src2 = byte_offset_to_address(ctx, &intr->src[1], byte_offset);
118
119 stgb = ir3_STGB(b, ssbo, 0, src0, 0, src1, 0, src2, 0);
120 stgb->cat6.iim_val = ncomp;
121 stgb->cat6.d = 4;
122 stgb->cat6.type = TYPE_U32;
123 stgb->barrier_class = IR3_BARRIER_BUFFER_W;
124 stgb->barrier_conflict = IR3_BARRIER_BUFFER_R | IR3_BARRIER_BUFFER_W;
125
126 array_insert(b, b->keeps, stgb);
127 }
128
129 static struct ir3_instruction *
emit_atomic(struct ir3_block * b,nir_atomic_op op,struct ir3_instruction * bo,struct ir3_instruction * data,struct ir3_instruction * offset,struct ir3_instruction * byte_offset)130 emit_atomic(struct ir3_block *b,
131 nir_atomic_op op,
132 struct ir3_instruction *bo,
133 struct ir3_instruction *data,
134 struct ir3_instruction *offset,
135 struct ir3_instruction *byte_offset)
136 {
137 switch (op) {
138 case nir_atomic_op_iadd:
139 return ir3_ATOMIC_S_ADD(b, bo, 0, data, 0, offset, 0, byte_offset, 0);
140 case nir_atomic_op_imin:
141 return ir3_ATOMIC_S_MIN(b, bo, 0, data, 0, offset, 0, byte_offset, 0);
142 case nir_atomic_op_umin:
143 return ir3_ATOMIC_S_MIN(b, bo, 0, data, 0, offset, 0, byte_offset, 0);
144 case nir_atomic_op_imax:
145 return ir3_ATOMIC_S_MAX(b, bo, 0, data, 0, offset, 0, byte_offset, 0);
146 case nir_atomic_op_umax:
147 return ir3_ATOMIC_S_MAX(b, bo, 0, data, 0, offset, 0, byte_offset, 0);
148 case nir_atomic_op_iand:
149 return ir3_ATOMIC_S_AND(b, bo, 0, data, 0, offset, 0, byte_offset, 0);
150 case nir_atomic_op_ior:
151 return ir3_ATOMIC_S_OR(b, bo, 0, data, 0, offset, 0, byte_offset, 0);
152 case nir_atomic_op_ixor:
153 return ir3_ATOMIC_S_XOR(b, bo, 0, data, 0, offset, 0, byte_offset, 0);
154 case nir_atomic_op_xchg:
155 return ir3_ATOMIC_S_XCHG(b, bo, 0, data, 0, offset, 0, byte_offset, 0);
156 case nir_atomic_op_cmpxchg:
157 return ir3_ATOMIC_S_CMPXCHG(b, bo, 0, data, 0, offset, 0, byte_offset, 0);
158 default:
159 unreachable("boo");
160 }
161 }
162
163 /*
164 * SSBO atomic intrinsics
165 *
166 * All of the SSBO atomic memory operations read a value from memory,
167 * compute a new value using one of the operations below, write the new
168 * value to memory, and return the original value read.
169 *
170 * All operations take 3 sources except CompSwap that takes 4. These
171 * sources represent:
172 *
173 * 0: The SSBO buffer index.
174 * 1: The byte offset into the SSBO buffer of the variable that the atomic
175 * operation will operate on.
176 * 2: The data parameter to the atomic function (i.e. the value to add
177 * in, etc).
178 * 3: CompSwap: the second data parameter.
179 * Non-CompSwap: The dword offset into the SSBO buffer variable.
180 * 4: CompSwap: The dword offset into the SSBO buffer variable.
181 *
182 * We use custom ssbo_*_ir3 intrinsics generated by ir3_nir_lower_io_offsets()
183 * so we can have the dword offset generated in NIR.
184 */
185 static struct ir3_instruction *
emit_intrinsic_atomic_ssbo(struct ir3_context * ctx,nir_intrinsic_instr * intr)186 emit_intrinsic_atomic_ssbo(struct ir3_context *ctx, nir_intrinsic_instr *intr)
187 {
188 struct ir3_block *b = ctx->block;
189 nir_atomic_op op = nir_intrinsic_atomic_op(intr);
190 type_t type = nir_atomic_op_type(op) == nir_type_int ? TYPE_S32 : TYPE_U32;
191
192 struct ir3_instruction *ssbo = ir3_ssbo_to_ibo(ctx, intr->src[0]);
193
194 struct ir3_instruction *data = ir3_get_src(ctx, &intr->src[2])[0];
195 /* 64b byte offset */
196 struct ir3_instruction *byte_offset =
197 byte_offset_to_address(ctx, &intr->src[0], ir3_get_src(ctx, &intr->src[1])[0]);
198 /* dword offset for everything but cmpxchg */
199 struct ir3_instruction *src3 = ir3_get_src(ctx, &intr->src[3])[0];
200
201 if (op == nir_atomic_op_cmpxchg) {
202 /* for cmpxchg, src0 is [ui]vec2(data, compare): */
203 data = ir3_collect(b, src3, data);
204 src3 = ir3_get_src(ctx, &intr->src[4])[0];
205 }
206
207 struct ir3_instruction *atomic =
208 emit_atomic(b, op, ssbo, data, src3, byte_offset);
209
210 atomic->cat6.iim_val = 1;
211 atomic->cat6.d = 4;
212 atomic->cat6.type = type;
213 atomic->barrier_class = IR3_BARRIER_BUFFER_W;
214 atomic->barrier_conflict = IR3_BARRIER_BUFFER_R | IR3_BARRIER_BUFFER_W;
215
216 /* even if nothing consume the result, we can't DCE the instruction: */
217 array_insert(b, b->keeps, atomic);
218
219 return atomic;
220 }
221
222 static struct ir3_instruction *
get_image_offset(struct ir3_context * ctx,const nir_intrinsic_instr * instr,struct ir3_instruction * const * coords,bool byteoff)223 get_image_offset(struct ir3_context *ctx, const nir_intrinsic_instr *instr,
224 struct ir3_instruction *const *coords, bool byteoff)
225 {
226 struct ir3_block *b = ctx->block;
227 struct ir3_instruction *offset;
228 unsigned index = nir_src_as_uint(instr->src[0]);
229 unsigned ncoords = ir3_get_image_coords(instr, NULL);
230
231 /* to calculate the byte offset (yes, uggg) we need (up to) three
232 * const values to know the bytes per pixel, and y and z stride:
233 */
234 unsigned cb;
235 if (ctx->compiler->gen > 4) {
236 const struct ir3_const_state *const_state = ir3_const_state(ctx->so);
237 assert(const_state->image_dims.mask & (1 << index));
238
239 cb = regid(const_state->offsets.image_dims, 0) +
240 const_state->image_dims.off[index];
241 } else {
242 index += ctx->s->info.num_ssbos;
243 cb = A4XX_SSBO_CB_BASE(index);
244 }
245
246 /* offset = coords.x * bytes_per_pixel: */
247 if (ctx->compiler->gen == 4)
248 offset = ir3_MUL_S24(b, coords[0], 0, create_uniform(b, cb + 3), 0);
249 else
250 offset = ir3_MUL_S24(b, coords[0], 0, create_uniform(b, cb + 0), 0);
251 if (ncoords > 1) {
252 /* offset += coords.y * y_pitch: */
253 offset =
254 ir3_MAD_S24(b, create_uniform(b, cb + 1), 0, coords[1], 0, offset, 0);
255 }
256 if (ncoords > 2) {
257 /* offset += coords.z * z_pitch: */
258 offset =
259 ir3_MAD_S24(b, create_uniform(b, cb + 2), 0, coords[2], 0, offset, 0);
260 }
261
262 /* a4xx: must add in the base address: */
263 if (ctx->compiler->gen == 4)
264 offset = ir3_ADD_U(b, offset, 0, create_uniform(b, cb + 0), 0);
265
266 if (!byteoff) {
267 /* Some cases, like atomics, seem to use dword offset instead
268 * of byte offsets.. blob just puts an extra shr.b in there
269 * in those cases:
270 */
271 offset = ir3_SHR_B(b, offset, 0, create_immed(b, 2), 0);
272 }
273
274 if (ctx->compiler->is_64bit)
275 return ir3_collect(b, offset, create_immed(b, 0));
276 else
277 return offset;
278 }
279
280 /* src[] = { deref, coord, sample_index }. const_index[] = {} */
281 static void
emit_intrinsic_load_image(struct ir3_context * ctx,nir_intrinsic_instr * intr,struct ir3_instruction ** dst)282 emit_intrinsic_load_image(struct ir3_context *ctx, nir_intrinsic_instr *intr,
283 struct ir3_instruction **dst)
284 {
285 struct ir3_block *b = ctx->block;
286 struct ir3_instruction *const *coords = ir3_get_src(ctx, &intr->src[1]);
287 struct ir3_instruction *ibo = ir3_image_to_ibo(ctx, intr->src[0]);
288 struct ir3_instruction *offset = get_image_offset(ctx, intr, coords, true);
289 unsigned ncoords = ir3_get_image_coords(intr, NULL);
290 unsigned ncomp =
291 ir3_get_num_components_for_image_format(nir_intrinsic_format(intr));
292
293 struct ir3_instruction *ldib;
294 /* At least A420 does not have LDIB. Use LDGB and perform conversion
295 * ourselves.
296 *
297 * TODO: Actually do the conversion. ES 3.1 only requires this for
298 * single-component 32-bit types anyways.
299 */
300 if (ctx->compiler->gen > 4) {
301 ldib = ir3_LDIB(
302 b, ibo, 0, offset, 0, ir3_create_collect(b, coords, ncoords), 0);
303 } else {
304 ldib = ir3_LDGB(
305 b, ibo, 0, offset, 0, ir3_create_collect(b, coords, ncoords), 0);
306 switch (nir_intrinsic_format(intr)) {
307 case PIPE_FORMAT_R32_UINT:
308 case PIPE_FORMAT_R32_SINT:
309 case PIPE_FORMAT_R32_FLOAT:
310 break;
311 default:
312 /* For some reason even more 32-bit components don't work. */
313 assert(0);
314 break;
315 }
316 }
317 ldib->dsts[0]->wrmask = MASK(intr->num_components);
318 ldib->cat6.iim_val = ncomp;
319 ldib->cat6.d = ncoords;
320 ldib->cat6.type = ir3_get_type_for_image_intrinsic(intr);
321 ldib->cat6.typed = true;
322 ldib->barrier_class = IR3_BARRIER_IMAGE_R;
323 ldib->barrier_conflict = IR3_BARRIER_IMAGE_W;
324
325 ir3_split_dest(b, dst, ldib, 0, intr->num_components);
326 }
327
328 /* src[] = { index, coord, sample_index, value }. const_index[] = {} */
329 static void
emit_intrinsic_store_image(struct ir3_context * ctx,nir_intrinsic_instr * intr)330 emit_intrinsic_store_image(struct ir3_context *ctx, nir_intrinsic_instr *intr)
331 {
332 struct ir3_block *b = ctx->block;
333 struct ir3_instruction *stib, *offset;
334 struct ir3_instruction *const *value = ir3_get_src(ctx, &intr->src[3]);
335 struct ir3_instruction *const *coords = ir3_get_src(ctx, &intr->src[1]);
336 struct ir3_instruction *ibo = ir3_image_to_ibo(ctx, intr->src[0]);
337 unsigned ncoords = ir3_get_image_coords(intr, NULL);
338 unsigned ncomp =
339 ir3_get_num_components_for_image_format(nir_intrinsic_format(intr));
340
341 /* src0 is value
342 * src1 is coords
343 * src2 is 64b byte offset
344 */
345
346 offset = get_image_offset(ctx, intr, coords, true);
347
348 /* NOTE: stib seems to take byte offset, but stgb.typed can be used
349 * too and takes a dword offset.. not quite sure yet why blob uses
350 * one over the other in various cases.
351 */
352
353 stib = ir3_STIB(b, ibo, 0, ir3_create_collect(b, value, ncomp), 0,
354 ir3_create_collect(b, coords, ncoords), 0, offset, 0);
355 stib->cat6.iim_val = ncomp;
356 stib->cat6.d = ncoords;
357 stib->cat6.type = ir3_get_type_for_image_intrinsic(intr);
358 stib->cat6.typed = true;
359 stib->barrier_class = IR3_BARRIER_IMAGE_W;
360 stib->barrier_conflict = IR3_BARRIER_IMAGE_R | IR3_BARRIER_IMAGE_W;
361
362 array_insert(b, b->keeps, stib);
363 }
364
365 /* src[] = { deref, coord, sample_index, value, compare }. const_index[] = {} */
366 static struct ir3_instruction *
emit_intrinsic_atomic_image(struct ir3_context * ctx,nir_intrinsic_instr * intr)367 emit_intrinsic_atomic_image(struct ir3_context *ctx, nir_intrinsic_instr *intr)
368 {
369 struct ir3_block *b = ctx->block;
370 struct ir3_instruction *atomic, *src0, *src1, *src2;
371 struct ir3_instruction *const *coords = ir3_get_src(ctx, &intr->src[1]);
372 struct ir3_instruction *image = ir3_image_to_ibo(ctx, intr->src[0]);
373 unsigned ncoords = ir3_get_image_coords(intr, NULL);
374 nir_atomic_op op = nir_intrinsic_atomic_op(intr);
375
376 /* src0 is value (or uvec2(value, compare))
377 * src1 is coords
378 * src2 is 64b byte offset
379 */
380 src0 = ir3_get_src(ctx, &intr->src[3])[0];
381 src1 = ir3_create_collect(b, coords, ncoords);
382 src2 = get_image_offset(ctx, intr, coords, ctx->compiler->gen == 4);
383
384 if (op == nir_atomic_op_cmpxchg)
385 src0 = ir3_collect(b, ir3_get_src(ctx, &intr->src[4])[0], src0);
386
387 atomic = emit_atomic(b, op, image, src0, src1, src2);
388 atomic->cat6.iim_val = 1;
389 atomic->cat6.d = ncoords;
390 atomic->cat6.type = ir3_get_type_for_image_intrinsic(intr);
391 atomic->cat6.typed = ctx->compiler->gen == 5;
392 atomic->barrier_class = IR3_BARRIER_IMAGE_W;
393 atomic->barrier_conflict = IR3_BARRIER_IMAGE_R | IR3_BARRIER_IMAGE_W;
394
395 /* even if nothing consume the result, we can't DCE the instruction: */
396 array_insert(b, b->keeps, atomic);
397
398 return atomic;
399 }
400
401 static struct ir3_instruction *
emit_intrinsic_atomic_global(struct ir3_context * ctx,nir_intrinsic_instr * intr)402 emit_intrinsic_atomic_global(struct ir3_context *ctx, nir_intrinsic_instr *intr)
403 {
404 unreachable("Global atomic are unimplemented on A5xx");
405 }
406
407 const struct ir3_context_funcs ir3_a4xx_funcs = {
408 .emit_intrinsic_load_ssbo = emit_intrinsic_load_ssbo,
409 .emit_intrinsic_store_ssbo = emit_intrinsic_store_ssbo,
410 .emit_intrinsic_atomic_ssbo = emit_intrinsic_atomic_ssbo,
411 .emit_intrinsic_load_image = emit_intrinsic_load_image,
412 .emit_intrinsic_store_image = emit_intrinsic_store_image,
413 .emit_intrinsic_atomic_image = emit_intrinsic_atomic_image,
414 .emit_intrinsic_image_size = emit_intrinsic_image_size_tex,
415 .emit_intrinsic_load_global_ir3 = NULL,
416 .emit_intrinsic_store_global_ir3 = NULL,
417 .emit_intrinsic_atomic_global = emit_intrinsic_atomic_global,
418 };
419