summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/r600/evergreen_compute_internal.c
blob: d846cbe758ab2f99803beb5c523a04a38bc3527a (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
/*
 * 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
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *      Adam Rak <adam.rak@streamnovation.com>
 */

#include <stdlib.h>
#include <stdio.h>

#include "pipe/p_defines.h"
#include "pipe/p_state.h"
#include "pipe/p_context.h"
#include "util/u_blitter.h"
#include "util/u_double_list.h"
#include "util/u_transfer.h"
#include "util/u_surface.h"
#include "util/u_pack_color.h"
#include "util/u_memory.h"
#include "util/u_inlines.h"
#include "util/u_framebuffer.h"
#include "r600.h"
#include "r600_resource.h"
#include "r600_shader.h"
#include "r600_pipe.h"
#include "r600_formats.h"
#include "evergreend.h"
#include "evergreen_compute_internal.h"
#include "r600_hw_context_priv.h"

int get_compute_resource_num(void)
{
	int num = 0;
#define DECL_COMPUTE_RESOURCE(name, n) num += n;
#include "compute_resource.def"
#undef DECL_COMPUTE_RESOURCE
	return num;
}

void evergreen_emit_raw_value(
	struct evergreen_compute_resource* res,
	unsigned value)
{
	res->cs[res->cs_end++] = value;
}

void evergreen_emit_ctx_value(struct r600_context *ctx, unsigned value)
{
	ctx->cs->buf[ctx->cs->cdw++] = value;
}

void evergreen_mult_reg_set_(
	struct evergreen_compute_resource* res,
	int index,
	u32* array,
	int size)
{
	int i = 0;

	evergreen_emit_raw_reg_set(res, index, size / 4);

	for (i = 0; i < size; i+=4) {
		res->cs[res->cs_end++] = array[i / 4];
	}
}

void evergreen_reg_set(
	struct evergreen_compute_resource* res,
	unsigned index,
	unsigned value)
{
	evergreen_emit_raw_reg_set(res, index, 1);
	res->cs[res->cs_end++] = value;
}

struct evergreen_compute_resource* get_empty_res(
	struct r600_pipe_compute* pipe,
	enum evergreen_compute_resources res_code,
	int offset_index)
{
	int code_index = -1;
	int code_size = -1;

	{
		int i = 0;
		#define DECL_COMPUTE_RESOURCE(name, n) if (COMPUTE_RESOURCE_ ## name	== res_code) {code_index = i; code_size = n;} i += n;
		#include "compute_resource.def"
		#undef DECL_COMPUTE_RESOURCE
	}

	assert(code_index != -1 && "internal error: resouce index not found");
	assert(offset_index < code_size && "internal error: overindexing resource");

	int index = code_index + offset_index;

	struct evergreen_compute_resource* res = &pipe->resources[index];

	res->enabled = true;
	res->bo = NULL;
	res->cs_end = 0;
	bzero(&res->do_reloc, sizeof(res->do_reloc));

	return res;
}

void evergreen_emit_raw_reg_set(
	struct evergreen_compute_resource* res,
	unsigned index,
	int num)
{
	res->enabled = 1;
	int cs_end = res->cs_end;

	if (index >= EVERGREEN_CONFIG_REG_OFFSET
			&& index < EVERGREEN_CONFIG_REG_END) {
		res->cs[cs_end] = PKT3C(PKT3_SET_CONFIG_REG, num, 0);
		res->cs[cs_end+1] = (index - EVERGREEN_CONFIG_REG_OFFSET) >> 2;
	} else if (index >= EVERGREEN_CONTEXT_REG_OFFSET
			&& index < EVERGREEN_CONTEXT_REG_END) {
		res->cs[cs_end] = PKT3C(PKT3_SET_CONTEXT_REG, num, 0);
		res->cs[cs_end+1] = (index - EVERGREEN_CONTEXT_REG_OFFSET) >> 2;
	} else if (index >= EVERGREEN_RESOURCE_OFFSET
			&& index < EVERGREEN_RESOURCE_END) {
		res->cs[cs_end] = PKT3C(PKT3_SET_RESOURCE, num, 0);
		res->cs[cs_end+1] = (index - EVERGREEN_RESOURCE_OFFSET) >> 2;
	} else if (index >= EVERGREEN_SAMPLER_OFFSET
			&& index < EVERGREEN_SAMPLER_END) {
		res->cs[cs_end] = PKT3C(PKT3_SET_SAMPLER, num, 0);
		res->cs[cs_end+1] = (index - EVERGREEN_SAMPLER_OFFSET) >> 2;
	} else if (index >= EVERGREEN_CTL_CONST_OFFSET
			&& index < EVERGREEN_CTL_CONST_END) {
		res->cs[cs_end] = PKT3C(PKT3_SET_CTL_CONST, num, 0);
		res->cs[cs_end+1] = (index - EVERGREEN_CTL_CONST_OFFSET) >> 2;
	} else if (index >= EVERGREEN_LOOP_CONST_OFFSET
			&& index < EVERGREEN_LOOP_CONST_END) {
		res->cs[cs_end] = PKT3C(PKT3_SET_LOOP_CONST, num, 0);
		res->cs[cs_end+1] = (index - EVERGREEN_LOOP_CONST_OFFSET) >> 2;
	} else if (index >= EVERGREEN_BOOL_CONST_OFFSET
			&& index < EVERGREEN_BOOL_CONST_END) {
		res->cs[cs_end] = PKT3C(PKT3_SET_BOOL_CONST, num, 0);
		res->cs[cs_end+1] = (index - EVERGREEN_BOOL_CONST_OFFSET) >> 2;
	} else {
		res->cs[cs_end] = PKT0(index, num-1);
		res->cs_end--;
	}

	res->cs_end += 2;
}

void evergreen_emit_force_reloc(struct evergreen_compute_resource* res)
{
	res->do_reloc[res->cs_end] += 1;
}

void evergreen_emit_ctx_reg_set(
	struct r600_context *ctx,
	unsigned index,
	int num)
{

	if (index >= EVERGREEN_CONFIG_REG_OFFSET
			&& index < EVERGREEN_CONFIG_REG_END) {
		ctx->cs->buf[ctx->cs->cdw++] = PKT3C(PKT3_SET_CONFIG_REG, num, 0);
		ctx->cs->buf[ctx->cs->cdw++] = (index - EVERGREEN_CONFIG_REG_OFFSET) >> 2;
	} else if (index >= EVERGREEN_CONTEXT_REG_OFFSET
			&& index < EVERGREEN_CONTEXT_REG_END) {
		ctx->cs->buf[ctx->cs->cdw++] = PKT3C(PKT3_SET_CONTEXT_REG, num, 0);
		ctx->cs->buf[ctx->cs->cdw++] = (index - EVERGREEN_CONTEXT_REG_OFFSET) >> 2;
	} else if (index >= EVERGREEN_RESOURCE_OFFSET
			&& index < EVERGREEN_RESOURCE_END) {
		ctx->cs->buf[ctx->cs->cdw++] = PKT3C(PKT3_SET_RESOURCE, num, 0);
		ctx->cs->buf[ctx->cs->cdw++] = (index - EVERGREEN_RESOURCE_OFFSET) >> 2;
	} else if (index >= EVERGREEN_SAMPLER_OFFSET
			&& index < EVERGREEN_SAMPLER_END) {
		ctx->cs->buf[ctx->cs->cdw++] = PKT3C(PKT3_SET_SAMPLER, num, 0);
		ctx->cs->buf[ctx->cs->cdw++] = (index - EVERGREEN_SAMPLER_OFFSET) >> 2;
	} else if (index >= EVERGREEN_CTL_CONST_OFFSET
			&& index < EVERGREEN_CTL_CONST_END) {
		ctx->cs->buf[ctx->cs->cdw++] = PKT3C(PKT3_SET_CTL_CONST, num, 0);
		ctx->cs->buf[ctx->cs->cdw++] = (index - EVERGREEN_CTL_CONST_OFFSET) >> 2;
	} else if (index >= EVERGREEN_LOOP_CONST_OFFSET
			&& index < EVERGREEN_LOOP_CONST_END) {
		ctx->cs->buf[ctx->cs->cdw++] = PKT3C(PKT3_SET_LOOP_CONST, num, 0);
		ctx->cs->buf[ctx->cs->cdw++] = (index - EVERGREEN_LOOP_CONST_OFFSET) >> 2;
	} else if (index >= EVERGREEN_BOOL_CONST_OFFSET
			&& index < EVERGREEN_BOOL_CONST_END) {
		ctx->cs->buf[ctx->cs->cdw++] = PKT3C(PKT3_SET_BOOL_CONST, num, 0);
		ctx->cs->buf[ctx->cs->cdw++] = (index - EVERGREEN_BOOL_CONST_OFFSET) >> 2;
	} else {
		ctx->cs->buf[ctx->cs->cdw++] = PKT0(index, num-1);
	}
}

void evergreen_emit_ctx_reloc(
	struct r600_context *ctx,
	struct r600_resource *bo,
	enum radeon_bo_usage usage)
{
	assert(bo);

	ctx->cs->buf[ctx->cs->cdw++] = PKT3(PKT3_NOP, 0, 0);
	u32 rr = r600_context_bo_reloc(ctx, bo, usage);
	ctx->cs->buf[ctx->cs->cdw++] = rr;
}

void evergreen_set_buffer_sync(
	struct r600_context *ctx,
	struct r600_resource* bo,
	int size,
	int flags,
	enum radeon_bo_usage usage)
{
	assert(bo);
	int32_t cp_coher_size = 0;

	if (size == 0xffffffff || size == 0) {
		cp_coher_size = 0xffffffff;
	}
	else {
		cp_coher_size = ((size + 255) >> 8);
	}

	uint32_t sync_flags = 0;

	if ((flags & COMPUTE_RES_TC_FLUSH) == COMPUTE_RES_TC_FLUSH) {
		sync_flags |= S_0085F0_TC_ACTION_ENA(1);
	}

	if ((flags & COMPUTE_RES_VC_FLUSH) == COMPUTE_RES_VC_FLUSH) {
		sync_flags |= S_0085F0_VC_ACTION_ENA(1);
	}

	if ((flags & COMPUTE_RES_SH_FLUSH) == COMPUTE_RES_SH_FLUSH) {
		sync_flags |= S_0085F0_SH_ACTION_ENA(1);
	}

	if ((flags & COMPUTE_RES_CB_FLUSH(0)) == COMPUTE_RES_CB_FLUSH(0)) {
		sync_flags |= S_0085F0_CB_ACTION_ENA(1);

		switch((flags >> 8) & 0xF) {
		case 0:
			sync_flags |= S_0085F0_CB0_DEST_BASE_ENA(1);
			break;
		case 1:
			sync_flags |= S_0085F0_CB1_DEST_BASE_ENA(1);
			break;
		case 2:
			sync_flags |= S_0085F0_CB2_DEST_BASE_ENA(1);
			break;
		case 3:
			sync_flags |= S_0085F0_CB3_DEST_BASE_ENA(1);
			break;
		case 4:
			sync_flags |= S_0085F0_CB4_DEST_BASE_ENA(1);
			break;
		case 5:
			sync_flags |= S_0085F0_CB5_DEST_BASE_ENA(1);
			break;
		case 6:
			sync_flags |= S_0085F0_CB6_DEST_BASE_ENA(1);
			break;
		case 7:
			sync_flags |= S_0085F0_CB7_DEST_BASE_ENA(1);
			break;
		case 8:
			sync_flags |= S_0085F0_CB8_DEST_BASE_ENA(1);
			break;
		case 9:
			sync_flags |= S_0085F0_CB9_DEST_BASE_ENA(1);
			break;
		case 10:
			sync_flags |= S_0085F0_CB10_DEST_BASE_ENA(1);
			break;
		case 11:
			sync_flags |= S_0085F0_CB11_DEST_BASE_ENA(1);
			break;
		default:
			assert(0);
		}
	}

	int32_t poll_interval = 10;

	ctx->cs->buf[ctx->cs->cdw++] = PKT3(PKT3_SURFACE_SYNC, 3, 0);
	ctx->cs->buf[ctx->cs->cdw++] = sync_flags;
	ctx->cs->buf[ctx->cs->cdw++] = cp_coher_size;
	ctx->cs->buf[ctx->cs->cdw++] = 0;
	ctx->cs->buf[ctx->cs->cdw++] = poll_interval;

	if (cp_coher_size != 0xffffffff) {
		evergreen_emit_ctx_reloc(ctx, bo, usage);
	}
}

int evergreen_compute_get_gpu_format(
	struct number_type_and_format* fmt,
	struct r600_resource *bo)
{
	switch (bo->b.b.format)
	{
		case PIPE_FORMAT_R8_UNORM:
		case PIPE_FORMAT_R32_UNORM:
			fmt->format = V_028C70_COLOR_32;
			fmt->number_type = V_028C70_NUMBER_UNORM;
			fmt->num_format_all = 0;
		break;
		case PIPE_FORMAT_R32_FLOAT:
			fmt->format = V_028C70_COLOR_32_FLOAT;
			fmt->number_type = V_028C70_NUMBER_FLOAT;
			fmt->num_format_all = 0;
		break;
		case PIPE_FORMAT_R32G32B32A32_FLOAT:
			fmt->format = V_028C70_COLOR_32_32_32_32_FLOAT;
			fmt->number_type = V_028C70_NUMBER_FLOAT;
			fmt->num_format_all = 0;
		break;

		///TODO: other formats...

		default:
			return 0;
	}

	return 1;
}

void evergreen_set_rat(
	struct r600_pipe_compute *pipe,
	int id,
	struct r600_resource* bo,
	int start,
	int size)
{
	assert(id < 12);
	assert((size & 3) == 0);
	assert((start & 0xFF) == 0);

	int offset;
	COMPUTE_DBG("bind rat: %i \n", id);

	if (id < 8) {
		offset = id*0x3c;
	}
	else {
		offset = 8*0x3c + (id-8)*0x1c;
	}

	int linear = 0;

	if (bo->b.b.height0 <= 1 && bo->b.b.depth0 <= 1
			&& bo->b.b.target == PIPE_BUFFER) {
		linear = 1;
	}

	struct evergreen_compute_resource* res =
		get_empty_res(pipe, COMPUTE_RESOURCE_RAT, id);

	evergreen_emit_force_reloc(res);

	evergreen_reg_set(res, R_028C64_CB_COLOR0_PITCH, 0); ///TODO: for 2D?
	evergreen_reg_set(res, R_028C68_CB_COLOR0_SLICE, 0);

	struct number_type_and_format fmt;

	///default config
	if (bo->b.b.format == PIPE_FORMAT_NONE) {
		 fmt.format = V_028C70_COLOR_32;
		 fmt.number_type = V_028C70_NUMBER_FLOAT;
	} else {
		evergreen_compute_get_gpu_format(&fmt, bo);
	}

	if (linear) {
		evergreen_reg_set(res,
			R_028C70_CB_COLOR0_INFO, S_028C70_RAT(1)
			| S_028C70_ARRAY_MODE(V_028C70_ARRAY_LINEAR_ALIGNED)
			| S_028C70_FORMAT(fmt.format)
			| S_028C70_NUMBER_TYPE(fmt.number_type)
		);
		evergreen_emit_force_reloc(res);
	} else {
		assert(0 && "TODO");
		///TODO
//	 evergreen_reg_set(res, R_028C70_CB_COLOR0_INFO, S_028C70_RAT(1) | S_028C70_ARRAY_MODE(????));
//	 evergreen_emit_force_reloc(res);
	}

	evergreen_reg_set(res, R_028C74_CB_COLOR0_ATTRIB, S_028C74_NON_DISP_TILING_ORDER(1));
	evergreen_emit_force_reloc(res);

	if (linear) {
		/* XXX: Why are we using size instead of bo->b.b.b.width0 ? */
		evergreen_reg_set(res, R_028C78_CB_COLOR0_DIM, size);
	} else {
		evergreen_reg_set(res, R_028C78_CB_COLOR0_DIM,
			S_028C78_WIDTH_MAX(bo->b.b.width0)
			| S_028C78_HEIGHT_MAX(bo->b.b.height0));
	}

	if (id < 8) {
		evergreen_reg_set(res, R_028C7C_CB_COLOR0_CMASK, 0);
		evergreen_emit_force_reloc(res);
		evergreen_reg_set(res, R_028C84_CB_COLOR0_FMASK, 0);
		evergreen_emit_force_reloc(res);
	}

	evergreen_reg_set(res, R_028C60_CB_COLOR0_BASE + offset, start >> 8);

	res->bo = bo;
	res->usage = RADEON_USAGE_READWRITE;
	res->coher_bo_size = size;
	res->flags = COMPUTE_RES_CB_FLUSH(id);
}

void evergreen_set_lds(
	struct r600_pipe_compute *pipe,
	int num_lds,
	int size,
	int num_waves)
{
	struct evergreen_compute_resource* res =
		get_empty_res(pipe, COMPUTE_RESOURCE_LDS, 0);

	if (pipe->ctx->chip_class < CAYMAN) {
		evergreen_reg_set(res, R_008E2C_SQ_LDS_RESOURCE_MGMT,
			S_008E2C_NUM_LS_LDS(num_lds));
	} else {
		evergreen_reg_set(res, CM_R_0286FC_SPI_LDS_MGMT,
					S_0286FC_NUM_LS_LDS(num_lds));
	}
	evergreen_reg_set(res, CM_R_0288E8_SQ_LDS_ALLOC, size | num_waves << 14);
}

void evergreen_set_gds(
	struct r600_pipe_compute *pipe,
	uint32_t addr,
	uint32_t size)
{
	struct evergreen_compute_resource* res =
		get_empty_res(pipe, COMPUTE_RESOURCE_GDS, 0);

	evergreen_reg_set(res, R_028728_GDS_ORDERED_WAVE_PER_SE, 1);
	evergreen_reg_set(res, R_028720_GDS_ADDR_BASE, addr);
	evergreen_reg_set(res, R_028724_GDS_ADDR_SIZE, size);
}

void evergreen_set_export(
	struct r600_pipe_compute *pipe,
	struct r600_resource* bo,
	int offset, int size)
{
	#define SX_MEMORY_EXPORT_BASE 0x9010
	#define SX_MEMORY_EXPORT_SIZE 0x9014

	struct evergreen_compute_resource* res =
		get_empty_res(pipe, COMPUTE_RESOURCE_EXPORT, 0);

	evergreen_reg_set(res, SX_MEMORY_EXPORT_SIZE, size);

	if (size) {
		evergreen_reg_set(res, SX_MEMORY_EXPORT_BASE, offset);
		res->bo = bo;
		res->usage = RADEON_USAGE_WRITE;
		res->coher_bo_size = size;
		res->flags = 0;
	}
}

void evergreen_set_loop_const(
	struct r600_pipe_compute *pipe,
	int id, int count, int init, int inc) {

	struct evergreen_compute_resource* res =
		get_empty_res(pipe, COMPUTE_RESOURCE_LOOP, id);

	assert(id < 32);
	assert(count <= 0xFFF);
	assert(init <= 0xFF);
	assert(inc <= 0xFF);

	/* Compute shaders use LOOP_CONST registers SQ_LOOP_CONST_160 to
         * SQ_LOOP_CONST_191 */
	evergreen_reg_set(res, R_03A200_SQ_LOOP_CONST_0 + (160 * 4) + (id * 4),
		count | init << 12 | inc << 24);
}

void evergreen_set_tmp_ring(
	struct r600_pipe_compute *pipe,
	struct r600_resource* bo,
	int offset, int size, int se)
{
	#define SQ_LSTMP_RING_BASE 0x00008e10
	#define SQ_LSTMP_RING_SIZE 0x00008e14
	#define GRBM_GFX_INDEX                                  0x802C
	#define         INSTANCE_INDEX(x)                       ((x) << 0)
	#define         SE_INDEX(x)                             ((x) << 16)
	#define         INSTANCE_BROADCAST_WRITES               (1 << 30)
	#define         SE_BROADCAST_WRITES                     (1 << 31)

	struct evergreen_compute_resource* res =
		get_empty_res(pipe, COMPUTE_RESOURCE_TMPRING, se);

	evergreen_reg_set(res,
		GRBM_GFX_INDEX,INSTANCE_INDEX(0)
		| SE_INDEX(se)
		| INSTANCE_BROADCAST_WRITES);
	evergreen_reg_set(res, SQ_LSTMP_RING_SIZE, size);

	if (size) {
		assert(bo);

		evergreen_reg_set(res, SQ_LSTMP_RING_BASE, offset);
		res->bo = bo;
		res->usage = RADEON_USAGE_WRITE;
		res->coher_bo_size = 0;
		res->flags = 0;
	}

	if (size) {
		evergreen_emit_force_reloc(res);
	}

	evergreen_reg_set(res,
		GRBM_GFX_INDEX,INSTANCE_INDEX(0)
		| SE_INDEX(0)
		| INSTANCE_BROADCAST_WRITES
		| SE_BROADCAST_WRITES);
}

static uint32_t r600_colorformat_endian_swap(uint32_t colorformat)
{
	if (R600_BIG_ENDIAN) {
		switch(colorformat) {
		case V_028C70_COLOR_4_4:
			return ENDIAN_NONE;

		/* 8-bit buffers. */
		case V_028C70_COLOR_8:
			return ENDIAN_NONE;

		/* 16-bit buffers. */
		case V_028C70_COLOR_5_6_5:
		case V_028C70_COLOR_1_5_5_5:
		case V_028C70_COLOR_4_4_4_4:
		case V_028C70_COLOR_16:
		case V_028C70_COLOR_8_8:
			return ENDIAN_8IN16;

		/* 32-bit buffers. */
		case V_028C70_COLOR_8_8_8_8:
		case V_028C70_COLOR_2_10_10_10:
		case V_028C70_COLOR_8_24:
		case V_028C70_COLOR_24_8:
		case V_028C70_COLOR_32_FLOAT:
		case V_028C70_COLOR_16_16_FLOAT:
		case V_028C70_COLOR_16_16:
			return ENDIAN_8IN32;

		/* 64-bit buffers. */
		case V_028C70_COLOR_16_16_16_16:
		case V_028C70_COLOR_16_16_16_16_FLOAT:
			return ENDIAN_8IN16;

		case V_028C70_COLOR_32_32_FLOAT:
		case V_028C70_COLOR_32_32:
		case V_028C70_COLOR_X24_8_32_FLOAT:
			return ENDIAN_8IN32;

		/* 96-bit buffers. */
		case V_028C70_COLOR_32_32_32_FLOAT:
		/* 128-bit buffers. */
		case V_028C70_COLOR_32_32_32_32_FLOAT:
		case V_028C70_COLOR_32_32_32_32:
			return ENDIAN_8IN32;
		default:
			return ENDIAN_NONE; /* Unsupported. */
		}
	} else {
		return ENDIAN_NONE;
	}
}

static unsigned r600_tex_dim(unsigned dim)
{
	switch (dim) {
	default:
	case PIPE_TEXTURE_1D:
		return V_030000_SQ_TEX_DIM_1D;
	case PIPE_TEXTURE_1D_ARRAY:
		return V_030000_SQ_TEX_DIM_1D_ARRAY;
	case PIPE_TEXTURE_2D:
	case PIPE_TEXTURE_RECT:
		return V_030000_SQ_TEX_DIM_2D;
	case PIPE_TEXTURE_2D_ARRAY:
		return V_030000_SQ_TEX_DIM_2D_ARRAY;
	case PIPE_TEXTURE_3D:
		return V_030000_SQ_TEX_DIM_3D;
	case PIPE_TEXTURE_CUBE:
		return V_030000_SQ_TEX_DIM_CUBEMAP;
	}
}

void evergreen_set_vtx_resource(
	struct r600_pipe_compute *pipe,
	struct r600_resource* bo,
	int id, uint64_t offset, int writable)
{
	assert(id < 16);
	uint32_t sq_vtx_constant_word2, sq_vtx_constant_word3, sq_vtx_constant_word4;
	struct number_type_and_format fmt;
	uint64_t va;

	fmt.format = 0;

	assert(bo->b.b.height0 <= 1);
	assert(bo->b.b.depth0 <= 1);

	int e = evergreen_compute_get_gpu_format(&fmt, bo);

	assert(e && "unknown format");

	struct evergreen_compute_resource* res =
		get_empty_res(pipe, COMPUTE_RESOURCE_VERT, id);

	unsigned size = bo->b.b.width0;
	unsigned stride = 1;

//	size = (size * util_format_get_blockwidth(bo->b.b.b.format) *
//		util_format_get_blocksize(bo->b.b.b.format));

	va = r600_resource_va(&pipe->ctx->screen->screen, &bo->b.b) + offset;

	COMPUTE_DBG("id: %i vtx size: %i byte,	width0: %i elem\n",
		id, size, bo->b.b.width0);

	sq_vtx_constant_word2 =
		S_030008_BASE_ADDRESS_HI(va >> 32) |
		S_030008_STRIDE(stride) |
		S_030008_DATA_FORMAT(fmt.format) |
		S_030008_NUM_FORMAT_ALL(fmt.num_format_all) |
		S_030008_ENDIAN_SWAP(0);

	COMPUTE_DBG("%08X %i %i %i %i\n", sq_vtx_constant_word2, offset,
			stride, fmt.format, fmt.num_format_all);

	sq_vtx_constant_word3 =
		S_03000C_DST_SEL_X(0) |
		S_03000C_DST_SEL_Y(1) |
		S_03000C_DST_SEL_Z(2) |
		S_03000C_DST_SEL_W(3);

	sq_vtx_constant_word4 = 0;

	evergreen_emit_raw_value(res, PKT3C(PKT3_SET_RESOURCE, 8, 0));
	evergreen_emit_raw_value(res, (id+816)*32 >> 2);
	evergreen_emit_raw_value(res, (unsigned)((va) & 0xffffffff));
	evergreen_emit_raw_value(res, size - 1);
	evergreen_emit_raw_value(res, sq_vtx_constant_word2);
	evergreen_emit_raw_value(res, sq_vtx_constant_word3);
	evergreen_emit_raw_value(res, sq_vtx_constant_word4);
	evergreen_emit_raw_value(res, 0);
	evergreen_emit_raw_value(res, 0);
	evergreen_emit_raw_value(res, S_03001C_TYPE(V_03001C_SQ_TEX_VTX_VALID_BUFFER));

	res->bo = bo;

	if (writable) {
		res->usage = RADEON_USAGE_READWRITE;
	}
	else {
		res->usage = RADEON_USAGE_READ;
	}

	res->coher_bo_size = size;
	res->flags = COMPUTE_RES_TC_FLUSH | COMPUTE_RES_VC_FLUSH;
}

void evergreen_set_tex_resource(
	struct r600_pipe_compute *pipe,
	struct r600_pipe_sampler_view* view,
	int id)
{
	struct evergreen_compute_resource* res =
		get_empty_res(pipe, COMPUTE_RESOURCE_TEX, id);
	struct r600_resource_texture *tmp =
		(struct r600_resource_texture*)view->base.texture;

	unsigned format, endian;
	uint32_t word4 = 0, yuv_format = 0, pitch = 0;
	unsigned char swizzle[4], array_mode = 0, tile_type = 0;
	unsigned height, depth;

	swizzle[0] = 0;
	swizzle[1] = 1;
	swizzle[2] = 2;
	swizzle[3] = 3;

	format = r600_translate_texformat((struct pipe_screen *)pipe->ctx->screen,
		view->base.format, swizzle, &word4, &yuv_format);

	if (format == ~0) {
		format = 0;
	}

	endian = r600_colorformat_endian_swap(format);

	height = view->base.texture->height0;
	depth = view->base.texture->depth0;

	pitch = align(tmp->pitch_in_blocks[0] *
		util_format_get_blockwidth(tmp->real_format), 8);
	array_mode = tmp->array_mode[0];
	tile_type = tmp->tile_type;

	assert(view->base.texture->target != PIPE_TEXTURE_1D_ARRAY);
	assert(view->base.texture->target != PIPE_TEXTURE_2D_ARRAY);

	evergreen_emit_raw_value(res, PKT3C(PKT3_SET_RESOURCE, 8, 0));
	evergreen_emit_raw_value(res, (id+816)*32 >> 2); ///TODO: check this line
	evergreen_emit_raw_value(res,
				(S_030000_DIM(r600_tex_dim(view->base.texture->target)) |
				S_030000_PITCH((pitch / 8) - 1) |
				S_030000_NON_DISP_TILING_ORDER(tile_type) |
				S_030000_TEX_WIDTH(view->base.texture->width0 - 1)));
	evergreen_emit_raw_value(res, (S_030004_TEX_HEIGHT(height - 1) |
				S_030004_TEX_DEPTH(depth - 1) |
				S_030004_ARRAY_MODE(array_mode)));
	evergreen_emit_raw_value(res, tmp->offset[0] >> 8);
	evergreen_emit_raw_value(res, tmp->offset[0] >> 8);
	evergreen_emit_raw_value(res, (word4 |
				S_030010_SRF_MODE_ALL(V_030010_SRF_MODE_ZERO_CLAMP_MINUS_ONE) |
				S_030010_ENDIAN_SWAP(endian) |
				S_030010_BASE_LEVEL(0)));
	evergreen_emit_raw_value(res, (S_030014_LAST_LEVEL(0) |
				S_030014_BASE_ARRAY(0) |
				S_030014_LAST_ARRAY(0)));
	evergreen_emit_raw_value(res, (S_030018_MAX_ANISO(4 /* max 16 samples */)));
	evergreen_emit_raw_value(res,
		S_03001C_TYPE(V_03001C_SQ_TEX_VTX_VALID_TEXTURE)
		| S_03001C_DATA_FORMAT(format));

	res->bo = (struct r600_resource*)view->base.texture;

	res->usage = RADEON_USAGE_READ;

	res->coher_bo_size = tmp->offset[0] + util_format_get_blockwidth(tmp->real_format)*view->base.texture->width0*height*depth;
	res->flags = COMPUTE_RES_TC_FLUSH;

	evergreen_emit_force_reloc(res);
	evergreen_emit_force_reloc(res);
}

void evergreen_set_sampler_resource(
	struct r600_pipe_compute *pipe,
	struct compute_sampler_state *sampler,
	int id)
{
	struct evergreen_compute_resource* res =
		get_empty_res(pipe, COMPUTE_RESOURCE_SAMPLER, id);

	unsigned aniso_flag_offset = sampler->state.max_anisotropy > 1 ? 2 : 0;

	evergreen_emit_raw_value(res, PKT3C(PKT3_SET_SAMPLER, 3, 0));
	evergreen_emit_raw_value(res, (id + 90)*3);
	evergreen_emit_raw_value(res,
		S_03C000_CLAMP_X(r600_tex_wrap(sampler->state.wrap_s)) |
		S_03C000_CLAMP_Y(r600_tex_wrap(sampler->state.wrap_t)) |
		S_03C000_CLAMP_Z(r600_tex_wrap(sampler->state.wrap_r)) |
		S_03C000_XY_MAG_FILTER(r600_tex_filter(sampler->state.mag_img_filter) | aniso_flag_offset) |
		S_03C000_XY_MIN_FILTER(r600_tex_filter(sampler->state.min_img_filter) | aniso_flag_offset) |
		S_03C000_BORDER_COLOR_TYPE(V_03C000_SQ_TEX_BORDER_COLOR_OPAQUE_BLACK)
	);
	evergreen_emit_raw_value(res,
		S_03C004_MIN_LOD(S_FIXED(CLAMP(sampler->state.min_lod, 0, 15), 8)) |
		S_03C004_MAX_LOD(S_FIXED(CLAMP(sampler->state.max_lod, 0, 15), 8))
	);
	evergreen_emit_raw_value(res,
		S_03C008_LOD_BIAS(S_FIXED(CLAMP(sampler->state.lod_bias, -16, 16), 8)) |
		(sampler->state.seamless_cube_map ? 0 : S_03C008_DISABLE_CUBE_WRAP(1)) |
		S_03C008_TYPE(1)
	);
}

void evergreen_set_const_cache(
	struct r600_pipe_compute *pipe,
	int cache_id,
	struct r600_resource* cbo,
	int size, int offset)
{
	#define SQ_ALU_CONST_BUFFER_SIZE_LS_0 0x00028fc0
	#define SQ_ALU_CONST_CACHE_LS_0 0x00028f40

	struct evergreen_compute_resource* res =
		get_empty_res(pipe, COMPUTE_RESOURCE_CONST_MEM, cache_id);

	assert(size < 0x200);
	assert((offset & 0xFF) == 0);
	assert(cache_id < 16);

	evergreen_reg_set(res, SQ_ALU_CONST_BUFFER_SIZE_LS_0 + cache_id*4, size);
	evergreen_reg_set(res, SQ_ALU_CONST_CACHE_LS_0 + cache_id*4, offset >> 8);
	res->bo = cbo;
	res->usage = RADEON_USAGE_READ;
	res->coher_bo_size = size;
	res->flags = COMPUTE_RES_SH_FLUSH;
}

struct r600_resource* r600_compute_buffer_alloc_vram(
	struct r600_screen *screen,
	unsigned size)
{
	assert(size);

	struct pipe_resource * buffer = pipe_buffer_create(
			(struct pipe_screen*) screen,
			PIPE_BIND_CUSTOM,
			PIPE_USAGE_IMMUTABLE,
			size);

	return (struct r600_resource *)buffer;
}