summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/r600/r600_compiler_r700.c
blob: 0b43942866259052d47b61a9647bfad0c0151e2c (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
/*
 * Copyright 2010 Jerome Glisse <glisse@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
 * 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.
 */
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include "r600_context.h"
#include "r700_sq.h"

static int r700_shader_cf_node_bytecode(struct r600_shader *rshader,
					struct r600_shader_node *rnode,
					unsigned *cid)
{
	unsigned id = *cid;

	if (rnode->nfetch) {
		rshader->bcode[id++] = S_SQ_CF_WORD0_ADDR(rnode->cf_addr >> 1);
		rshader->bcode[id++] = S_SQ_CF_WORD1_CF_INST(V_SQ_CF_WORD1_SQ_CF_INST_VTX) |
					S_SQ_CF_WORD1_BARRIER(1) |
					S_SQ_CF_WORD1_COUNT(rnode->nfetch - 1);
	} else {
		rshader->bcode[id++] = S_SQ_CF_ALU_WORD0_ADDR(rnode->cf_addr >> 1);
		rshader->bcode[id++] = S_SQ_CF_ALU_WORD1_CF_INST(V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU) |
					S_SQ_CF_ALU_WORD1_BARRIER(1) |
					S_SQ_CF_ALU_WORD1_COUNT(rnode->nslot - 1);
	}
	*cid = id;
	return 0;
}

static int r700_shader_cf_output_bytecode(struct r600_shader *rshader,
						struct c_vector *v,
						unsigned *cid,
						unsigned end)
{
	struct r600_shader_operand out;
	unsigned id = *cid;
	int r;

	r = r600_shader_find_gpr(rshader, v, 0, &out);
	if (r)
		return r;
	rshader->bcode[id + 0] = S_SQ_CF_ALLOC_EXPORT_WORD0_RW_GPR(out.sel) |
				S_SQ_CF_ALLOC_EXPORT_WORD0_ELEM_SIZE(3);
	rshader->bcode[id + 1] = S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_X(0) |
		S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Y(1) |
		S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Z(2) |
		S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_W(3) |
		S_SQ_CF_ALLOC_EXPORT_WORD1_BARRIER(1) |
		S_SQ_CF_ALLOC_EXPORT_WORD1_CF_INST(V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT_DONE) |
		S_SQ_CF_ALLOC_EXPORT_WORD1_END_OF_PROGRAM(end);
	switch (v->name) {
	case C_SEMANTIC_POSITION:
		rshader->bcode[id + 0] |= S_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(60) |
			S_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS);
		break;
	case C_SEMANTIC_COLOR:
		if (rshader->cshader.type == C_PROGRAM_TYPE_VS) {
			rshader->output[rshader->noutput].gpr = out.sel;
			rshader->output[rshader->noutput].sid = v->sid;
			rshader->output[rshader->noutput].name = v->name;
			rshader->bcode[id + 0] |= S_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(rshader->noutput++) |
				S_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM);
		} else {
			rshader->bcode[id + 0] |= S_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(0) |
				S_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PIXEL);
		}
		break;
	case C_SEMANTIC_GENERIC:
		rshader->output[rshader->noutput].gpr = out.sel;
		rshader->output[rshader->noutput].sid = v->sid;
		rshader->output[rshader->noutput].name = v->name;
		rshader->bcode[id + 0] |= S_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(rshader->noutput++) |
			S_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM);
		break;
	default:
		fprintf(stderr, "%s:%d unsupported\n", __func__, __LINE__);
		return -EINVAL;
	}
	*cid = id + 2;
	return 0;
}

static int r700_shader_alu_bytecode(struct r600_shader *rshader,
					struct r600_shader_node *rnode,
					struct r600_shader_inst *alu,
					unsigned *cid)
{
	unsigned id = *cid;

	/* don't replace gpr by pv or ps for destination register */
	if (alu->is_op3) {
		rshader->bcode[id++] = S_SQ_ALU_WORD0_SRC0_SEL(alu->src[0].sel) |
					S_SQ_ALU_WORD0_SRC0_CHAN(alu->src[0].chan) |
					S_SQ_ALU_WORD0_SRC1_SEL(alu->src[1].sel) |
					S_SQ_ALU_WORD0_SRC1_CHAN(alu->src[1].chan) |
					S_SQ_ALU_WORD0_LAST(alu->last);
		rshader->bcode[id++] = S_SQ_ALU_WORD1_DST_GPR(alu->dst.sel) |
					S_SQ_ALU_WORD1_DST_CHAN(alu->dst.chan) |
					S_SQ_ALU_WORD1_OP3_SRC2_SEL(alu->src[2].sel) |
					S_SQ_ALU_WORD1_OP3_SRC2_CHAN(alu->src[2].chan) |
					S_SQ_ALU_WORD1_OP3_SRC2_NEG(alu->src[2].neg) |
					S_SQ_ALU_WORD1_OP3_ALU_INST(alu->opcode) |
					S_SQ_ALU_WORD1_BANK_SWIZZLE(0);
	} else {
		rshader->bcode[id++] = S_SQ_ALU_WORD0_SRC0_SEL(alu->src[0].sel) |
					S_SQ_ALU_WORD0_SRC0_CHAN(alu->src[0].chan) |
					S_SQ_ALU_WORD0_SRC0_NEG(alu->src[0].neg) |
					S_SQ_ALU_WORD0_SRC1_SEL(alu->src[1].sel) |
					S_SQ_ALU_WORD0_SRC1_CHAN(alu->src[1].chan) |
					S_SQ_ALU_WORD0_SRC1_NEG(alu->src[1].neg) |
					S_SQ_ALU_WORD0_LAST(alu->last);
		rshader->bcode[id++] = S_SQ_ALU_WORD1_DST_GPR(alu->dst.sel) |
					S_SQ_ALU_WORD1_DST_CHAN(alu->dst.chan) |
					S_SQ_ALU_WORD1_OP2_SRC0_ABS(alu->src[0].abs) |
					S_SQ_ALU_WORD1_OP2_SRC1_ABS(alu->src[1].abs) |
					S_SQ_ALU_WORD1_OP2_WRITE_MASK(1) |
					S_SQ_ALU_WORD1_OP2_ALU_INST(alu->opcode) |
					S_SQ_ALU_WORD1_BANK_SWIZZLE(0);
	}
	*cid = id;
	return 0;
}

static int r700_shader_alu_translate(struct r600_shader *rshader,
				     struct r600_shader_node *rnode,
				     unsigned *cid)
				     
{
	struct r600_shader_alu *alu;
	unsigned id = *cid;
	int i;
	int r = 0;
	LIST_FOR_EACH_ENTRY(alu, &rnode->alu, head) {
		for (i = 0; i < alu->nalu; i++) {
			r = r700_shader_alu_bytecode(rshader, rnode, &alu->alu[i], &id);
			if (r)
				goto out;
		}
		for (i = 0; i < alu->nliteral; i++) {
			rshader->bcode[id++] = alu->literal[i];
		}
	}
 out:
	*cid = id;
	return r;
}

int r700_shader_translate(struct r600_shader *rshader)
{
	struct c_shader *shader = &rshader->cshader;
	struct r600_shader_node *rnode;
	struct r600_shader_vfetch *vfetch;
	struct c_vector *v;
	unsigned id, end;
	int r;

	r = r600_shader_register(rshader);
	if (r) {
		fprintf(stderr, "%s %d register allocation failed\n", __FILE__, __LINE__);
		return r;
	}
	r = r600_shader_translate_rec(rshader, &shader->entry);
	if (r) {
		fprintf(stderr, "%s %d translation failed\n", __FILE__, __LINE__);
		return r;
	}
	r = r600_shader_legalize(rshader);
	if (r) {
		fprintf(stderr, "%s %d legalize failed\n", __FILE__, __LINE__);
		return r;
	}
	r600_shader_node_place(rshader);
	rshader->bcode = malloc(rshader->ndw * 4);
	if (rshader->bcode == NULL)
		return -ENOMEM;
	LIST_FOR_EACH_ENTRY(rnode, &rshader->nodes, head) {
		id = rnode->cf_addr;
		LIST_FOR_EACH_ENTRY(vfetch, &rnode->vfetch, head) {
			r = r600_shader_vfetch_bytecode(rshader, rnode, vfetch, &id);
			if (r)
				return r;
		}
		if (rshader->r6xx_compile)
			r = r6xx_shader_alu_translate(rshader, rnode, &id);
		else
			r = r700_shader_alu_translate(rshader, rnode, &id);
		if (r)
			return r;
	}
	id = 0;
	LIST_FOR_EACH_ENTRY(rnode, &rshader->nodes, head) {
		r = r700_shader_cf_node_bytecode(rshader, rnode, &id);
		if (r)
			return r;
	}
	LIST_FOR_EACH_ENTRY(v, &rshader->cshader.files[C_FILE_OUTPUT].vectors, head) {
		end = 0;
		if (v->head.next == &rshader->cshader.files[C_FILE_OUTPUT].vectors)
			end = 1;
		r = r700_shader_cf_output_bytecode(rshader, v, &id, end);
		if (r)
			return r;
	}
	LIST_FOR_EACH_ENTRY(v, &rshader->cshader.files[C_FILE_INPUT].vectors, head) {
		rshader->input[rshader->ninput].gpr = rshader->ninput;
		rshader->input[rshader->ninput].sid = v->sid;
		rshader->input[rshader->ninput].name = v->name;
		rshader->ninput++;
	}
	return 0;
}