summaryrefslogtreecommitdiffstats
path: root/src/freedreno/ir3/ir3_a4xx.c
blob: 5fe15cf8e274361056977f1dea439fe1653c48a9 (plain)
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
/*
 * Copyright (C) 2017-2018 Rob Clark <robclark@freedesktop.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors:
 *    Rob Clark <robclark@freedesktop.org>
 */

#define GPU 400

#include "ir3_context.h"
#include "ir3_image.h"

/*
 * Handlers for instructions changed/added in a4xx:
 */


/* src[] = { buffer_index, offset }. No const_index */
static void
emit_intrinsic_load_ssbo(struct ir3_context *ctx, nir_intrinsic_instr *intr,
		struct ir3_instruction **dst)
{
	struct ir3_block *b = ctx->block;
	struct ir3_instruction *ldgb, *src0, *src1, *byte_offset, *offset;

	/* can this be non-const buffer_index?  how do we handle that? */
	int ibo_idx = ir3_ssbo_to_ibo(&ctx->so->image_mapping, nir_src_as_uint(intr->src[0]));

	byte_offset = ir3_get_src(ctx, &intr->src[1])[0];
	offset = ir3_get_src(ctx, &intr->src[2])[0];

	/* src0 is uvec2(offset*4, 0), src1 is offset.. nir already *= 4: */
	src0 = ir3_create_collect(ctx, (struct ir3_instruction*[]){
		byte_offset,
		create_immed(b, 0),
	}, 2);
	src1 = offset;

	ldgb = ir3_LDGB(b, create_immed(b, ibo_idx), 0,
			src0, 0, src1, 0);
	ldgb->regs[0]->wrmask = MASK(intr->num_components);
	ldgb->cat6.iim_val = intr->num_components;
	ldgb->cat6.d = 4;
	ldgb->cat6.type = TYPE_U32;
	ldgb->barrier_class = IR3_BARRIER_BUFFER_R;
	ldgb->barrier_conflict = IR3_BARRIER_BUFFER_W;

	ir3_split_dest(b, dst, ldgb, 0, intr->num_components);
}

/* src[] = { value, block_index, offset }. const_index[] = { write_mask } */
static void
emit_intrinsic_store_ssbo(struct ir3_context *ctx, nir_intrinsic_instr *intr)
{
	struct ir3_block *b = ctx->block;
	struct ir3_instruction *stgb, *src0, *src1, *src2, *byte_offset, *offset;
	/* TODO handle wrmask properly, see _store_shared().. but I think
	 * it is more a PITA than that, since blob ends up loading the
	 * masked components and writing them back out.
	 */
	unsigned wrmask = intr->const_index[0];
	unsigned ncomp = ffs(~wrmask) - 1;

	/* can this be non-const buffer_index?  how do we handle that? */
	int ibo_idx = ir3_ssbo_to_ibo(&ctx->so->image_mapping, nir_src_as_uint(intr->src[1]));

	byte_offset = ir3_get_src(ctx, &intr->src[2])[0];
	offset = ir3_get_src(ctx, &intr->src[3])[0];

	/* src0 is value, src1 is offset, src2 is uvec2(offset*4, 0)..
	 * nir already *= 4:
	 */
	src0 = ir3_create_collect(ctx, ir3_get_src(ctx, &intr->src[0]), ncomp);
	src1 = offset;
	src2 = ir3_create_collect(ctx, (struct ir3_instruction*[]){
		byte_offset,
		create_immed(b, 0),
	}, 2);

	stgb = ir3_STGB(b, create_immed(b, ibo_idx), 0, src0, 0, src1, 0, src2, 0);
	stgb->cat6.iim_val = ncomp;
	stgb->cat6.d = 4;
	stgb->cat6.type = TYPE_U32;
	stgb->barrier_class = IR3_BARRIER_BUFFER_W;
	stgb->barrier_conflict = IR3_BARRIER_BUFFER_R | IR3_BARRIER_BUFFER_W;

	array_insert(b, b->keeps, stgb);
}

/*
 * SSBO atomic intrinsics
 *
 * All of the SSBO atomic memory operations read a value from memory,
 * compute a new value using one of the operations below, write the new
 * value to memory, and return the original value read.
 *
 * All operations take 3 sources except CompSwap that takes 4. These
 * sources represent:
 *
 * 0: The SSBO buffer index.
 * 1: The offset into the SSBO buffer of the variable that the atomic
 *    operation will operate on.
 * 2: The data parameter to the atomic function (i.e. the value to add
 *    in ssbo_atomic_add, etc).
 * 3: For CompSwap only: the second data parameter.
 */
static struct ir3_instruction *
emit_intrinsic_atomic_ssbo(struct ir3_context *ctx, nir_intrinsic_instr *intr)
{
	struct ir3_block *b = ctx->block;
	struct ir3_instruction *atomic, *ssbo, *src0, *src1, *src2, *byte_offset,
		*offset;
	type_t type = TYPE_U32;

	/* can this be non-const buffer_index?  how do we handle that? */
	int ibo_idx = ir3_ssbo_to_ibo(&ctx->so->image_mapping, nir_src_as_uint(intr->src[0]));
	ssbo = create_immed(b, ibo_idx);

	byte_offset = ir3_get_src(ctx, &intr->src[1])[0];
	offset = ir3_get_src(ctx, &intr->src[3])[0];

	/* src0 is data (or uvec2(data, compare))
	 * src1 is offset
	 * src2 is uvec2(offset*4, 0) (appears to be 64b byte offset)
	 *
	 * Note that nir already multiplies the offset by four
	 */
	src0 = ir3_get_src(ctx, &intr->src[2])[0];
	src1 = offset;
	src2 = ir3_create_collect(ctx, (struct ir3_instruction*[]){
		byte_offset,
		create_immed(b, 0),
	}, 2);

	switch (intr->intrinsic) {
	case nir_intrinsic_ssbo_atomic_add_ir3:
		atomic = ir3_ATOMIC_ADD_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);
		break;
	case nir_intrinsic_ssbo_atomic_imin_ir3:
		atomic = ir3_ATOMIC_MIN_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);
		type = TYPE_S32;
		break;
	case nir_intrinsic_ssbo_atomic_umin_ir3:
		atomic = ir3_ATOMIC_MIN_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);
		break;
	case nir_intrinsic_ssbo_atomic_imax_ir3:
		atomic = ir3_ATOMIC_MAX_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);
		type = TYPE_S32;
		break;
	case nir_intrinsic_ssbo_atomic_umax_ir3:
		atomic = ir3_ATOMIC_MAX_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);
		break;
	case nir_intrinsic_ssbo_atomic_and_ir3:
		atomic = ir3_ATOMIC_AND_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);
		break;
	case nir_intrinsic_ssbo_atomic_or_ir3:
		atomic = ir3_ATOMIC_OR_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);
		break;
	case nir_intrinsic_ssbo_atomic_xor_ir3:
		atomic = ir3_ATOMIC_XOR_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);
		break;
	case nir_intrinsic_ssbo_atomic_exchange_ir3:
		atomic = ir3_ATOMIC_XCHG_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);
		break;
	case nir_intrinsic_ssbo_atomic_comp_swap_ir3:
		/* for cmpxchg, src0 is [ui]vec2(data, compare): */
		src0 = ir3_create_collect(ctx, (struct ir3_instruction*[]){
			ir3_get_src(ctx, &intr->src[3])[0],
			src0,
		}, 2);
		src1 = ir3_get_src(ctx, &intr->src[4])[0];
		atomic = ir3_ATOMIC_CMPXCHG_G(b, ssbo, 0, src0, 0, src1, 0, src2, 0);
		break;
	default:
		unreachable("boo");
	}

	atomic->cat6.iim_val = 1;
	atomic->cat6.d = 4;
	atomic->cat6.type = type;
	atomic->barrier_class = IR3_BARRIER_BUFFER_W;
	atomic->barrier_conflict = IR3_BARRIER_BUFFER_R | IR3_BARRIER_BUFFER_W;

	/* even if nothing consume the result, we can't DCE the instruction: */
	array_insert(b, b->keeps, atomic);

	return atomic;
}

static struct ir3_instruction *
get_image_offset(struct ir3_context *ctx, const nir_variable *var,
		struct ir3_instruction * const *coords, bool byteoff)
{
	struct ir3_block *b = ctx->block;
	struct ir3_instruction *offset;
	unsigned ncoords = ir3_get_image_coords(var, NULL);

	/* to calculate the byte offset (yes, uggg) we need (up to) three
	 * const values to know the bytes per pixel, and y and z stride:
	 */
	struct ir3_const_state *const_state = &ctx->so->const_state;
	unsigned cb = regid(const_state->offsets.image_dims, 0) +
		const_state->image_dims.off[var->data.driver_location];

	debug_assert(const_state->image_dims.mask &
			(1 << var->data.driver_location));

	/* offset = coords.x * bytes_per_pixel: */
	offset = ir3_MUL_S(b, coords[0], 0, create_uniform(b, cb + 0), 0);
	if (ncoords > 1) {
		/* offset += coords.y * y_pitch: */
		offset = ir3_MAD_S24(b, create_uniform(b, cb + 1), 0,
				coords[1], 0, offset, 0);
	}
	if (ncoords > 2) {
		/* offset += coords.z * z_pitch: */
		offset = ir3_MAD_S24(b, create_uniform(b, cb + 2), 0,
				coords[2], 0, offset, 0);
	}

	if (!byteoff) {
		/* Some cases, like atomics, seem to use dword offset instead
		 * of byte offsets.. blob just puts an extra shr.b in there
		 * in those cases:
		 */
		offset = ir3_SHR_B(b, offset, 0, create_immed(b, 2), 0);
	}

	return ir3_create_collect(ctx, (struct ir3_instruction*[]){
		offset,
		create_immed(b, 0),
	}, 2);
}

/* src[] = { deref, coord, sample_index, value }. const_index[] = {} */
static void
emit_intrinsic_store_image(struct ir3_context *ctx, nir_intrinsic_instr *intr)
{
	struct ir3_block *b = ctx->block;
	const nir_variable *var = nir_intrinsic_get_var(intr, 0);
	struct ir3_instruction *stib, *offset;
	struct ir3_instruction * const *value = ir3_get_src(ctx, &intr->src[3]);
	struct ir3_instruction * const *coords = ir3_get_src(ctx, &intr->src[1]);
	unsigned ncoords = ir3_get_image_coords(var, NULL);
	unsigned slot = ir3_get_image_slot(nir_src_as_deref(intr->src[0]));
	unsigned ibo_idx = ir3_image_to_ibo(&ctx->so->image_mapping, slot);
	unsigned ncomp = ir3_get_num_components_for_glformat(var->data.image.format);

	/* src0 is value
	 * src1 is coords
	 * src2 is 64b byte offset
	 */

	offset = get_image_offset(ctx, var, coords, true);

	/* NOTE: stib seems to take byte offset, but stgb.typed can be used
	 * too and takes a dword offset.. not quite sure yet why blob uses
	 * one over the other in various cases.
	 */

	stib = ir3_STIB(b, create_immed(b, ibo_idx), 0,
			ir3_create_collect(ctx, value, ncomp), 0,
			ir3_create_collect(ctx, coords, ncoords), 0,
			offset, 0);
	stib->cat6.iim_val = ncomp;
	stib->cat6.d = ncoords;
	stib->cat6.type = ir3_get_image_type(var);
	stib->cat6.typed = true;
	stib->barrier_class = IR3_BARRIER_IMAGE_W;
	stib->barrier_conflict = IR3_BARRIER_IMAGE_R | IR3_BARRIER_IMAGE_W;

	array_insert(b, b->keeps, stib);
}

/* src[] = { deref, coord, sample_index, value, compare }. const_index[] = {} */
static struct ir3_instruction *
emit_intrinsic_atomic_image(struct ir3_context *ctx, nir_intrinsic_instr *intr)
{
	struct ir3_block *b = ctx->block;
	const nir_variable *var = nir_intrinsic_get_var(intr, 0);
	struct ir3_instruction *atomic, *image, *src0, *src1, *src2;
	struct ir3_instruction * const *coords = ir3_get_src(ctx, &intr->src[1]);
	unsigned ncoords = ir3_get_image_coords(var, NULL);
	unsigned slot = ir3_get_image_slot(nir_src_as_deref(intr->src[0]));
	unsigned ibo_idx = ir3_image_to_ibo(&ctx->so->image_mapping, slot);

	image = create_immed(b, ibo_idx);

	/* src0 is value (or uvec2(value, compare))
	 * src1 is coords
	 * src2 is 64b byte offset
	 */
	src0 = ir3_get_src(ctx, &intr->src[3])[0];
	src1 = ir3_create_collect(ctx, coords, ncoords);
	src2 = get_image_offset(ctx, var, coords, false);

	switch (intr->intrinsic) {
	case nir_intrinsic_image_deref_atomic_add:
		atomic = ir3_ATOMIC_ADD_G(b, image, 0, src0, 0, src1, 0, src2, 0);
		break;
	case nir_intrinsic_image_deref_atomic_min:
		atomic = ir3_ATOMIC_MIN_G(b, image, 0, src0, 0, src1, 0, src2, 0);
		break;
	case nir_intrinsic_image_deref_atomic_max:
		atomic = ir3_ATOMIC_MAX_G(b, image, 0, src0, 0, src1, 0, src2, 0);
		break;
	case nir_intrinsic_image_deref_atomic_and:
		atomic = ir3_ATOMIC_AND_G(b, image, 0, src0, 0, src1, 0, src2, 0);
		break;
	case nir_intrinsic_image_deref_atomic_or:
		atomic = ir3_ATOMIC_OR_G(b, image, 0, src0, 0, src1, 0, src2, 0);
		break;
	case nir_intrinsic_image_deref_atomic_xor:
		atomic = ir3_ATOMIC_XOR_G(b, image, 0, src0, 0, src1, 0, src2, 0);
		break;
	case nir_intrinsic_image_deref_atomic_exchange:
		atomic = ir3_ATOMIC_XCHG_G(b, image, 0, src0, 0, src1, 0, src2, 0);
		break;
	case nir_intrinsic_image_deref_atomic_comp_swap:
		/* for cmpxchg, src0 is [ui]vec2(data, compare): */
		src0 = ir3_create_collect(ctx, (struct ir3_instruction*[]){
			ir3_get_src(ctx, &intr->src[4])[0],
			src0,
		}, 2);
		atomic = ir3_ATOMIC_CMPXCHG_G(b, image, 0, src0, 0, src1, 0, src2, 0);
		break;
	default:
		unreachable("boo");
	}

	atomic->cat6.iim_val = 1;
	atomic->cat6.d = ncoords;
	atomic->cat6.type = ir3_get_image_type(var);
	atomic->cat6.typed = true;
	atomic->barrier_class = IR3_BARRIER_IMAGE_W;
	atomic->barrier_conflict = IR3_BARRIER_IMAGE_R | IR3_BARRIER_IMAGE_W;

	/* even if nothing consume the result, we can't DCE the instruction: */
	array_insert(b, b->keeps, atomic);

	return atomic;
}

const struct ir3_context_funcs ir3_a4xx_funcs = {
		.emit_intrinsic_load_ssbo = emit_intrinsic_load_ssbo,
		.emit_intrinsic_store_ssbo = emit_intrinsic_store_ssbo,
		.emit_intrinsic_atomic_ssbo = emit_intrinsic_atomic_ssbo,
		.emit_intrinsic_store_image = emit_intrinsic_store_image,
		.emit_intrinsic_atomic_image = emit_intrinsic_atomic_image,
};