glx: Don't try to swap a front buffer if we don't have one.
[mesa.git] / src / gallium / drivers / r600 / r600_compiler_r700.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include "r600_context.h"
29 #include "r700_sq.h"
30
31 static int r700_shader_cf_node_bytecode(struct r600_shader *rshader,
32 struct r600_shader_node *rnode,
33 unsigned *cid)
34 {
35 unsigned id = *cid;
36
37 if (rnode->nfetch) {
38 rshader->bcode[id++] = S_SQ_CF_WORD0_ADDR(rnode->cf_addr >> 1);
39 rshader->bcode[id++] = S_SQ_CF_WORD1_CF_INST(V_SQ_CF_WORD1_SQ_CF_INST_VTX) |
40 S_SQ_CF_WORD1_BARRIER(1) |
41 S_SQ_CF_WORD1_COUNT(rnode->nfetch - 1);
42 } else {
43 rshader->bcode[id++] = S_SQ_CF_ALU_WORD0_ADDR(rnode->cf_addr >> 1);
44 rshader->bcode[id++] = S_SQ_CF_ALU_WORD1_CF_INST(V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU) |
45 S_SQ_CF_ALU_WORD1_BARRIER(1) |
46 S_SQ_CF_ALU_WORD1_COUNT(rnode->nslot - 1);
47 }
48 *cid = id;
49 return 0;
50 }
51
52 static int r700_shader_cf_output_bytecode(struct r600_shader *rshader,
53 struct c_vector *v,
54 unsigned *cid,
55 unsigned end)
56 {
57 struct r600_shader_operand out;
58 unsigned id = *cid;
59 int r;
60
61 r = r600_shader_find_gpr(rshader, v, 0, &out);
62 if (r)
63 return r;
64 rshader->bcode[id + 0] = S_SQ_CF_ALLOC_EXPORT_WORD0_RW_GPR(out.sel) |
65 S_SQ_CF_ALLOC_EXPORT_WORD0_ELEM_SIZE(3);
66 rshader->bcode[id + 1] = S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_X(0) |
67 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Y(1) |
68 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Z(2) |
69 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_W(3) |
70 S_SQ_CF_ALLOC_EXPORT_WORD1_BARRIER(1) |
71 S_SQ_CF_ALLOC_EXPORT_WORD1_CF_INST(V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT_DONE) |
72 S_SQ_CF_ALLOC_EXPORT_WORD1_END_OF_PROGRAM(end);
73 switch (v->name) {
74 case C_SEMANTIC_POSITION:
75 rshader->bcode[id + 0] |= S_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(60) |
76 S_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS);
77 break;
78 case C_SEMANTIC_COLOR:
79 if (rshader->cshader.type == C_PROGRAM_TYPE_VS) {
80 rshader->output[rshader->noutput].gpr = out.sel;
81 rshader->output[rshader->noutput].sid = v->sid;
82 rshader->output[rshader->noutput].name = v->name;
83 rshader->bcode[id + 0] |= S_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(rshader->noutput++) |
84 S_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM);
85 } else {
86 rshader->bcode[id + 0] |= S_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(0) |
87 S_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PIXEL);
88 }
89 break;
90 case C_SEMANTIC_GENERIC:
91 rshader->output[rshader->noutput].gpr = out.sel;
92 rshader->output[rshader->noutput].sid = v->sid;
93 rshader->output[rshader->noutput].name = v->name;
94 rshader->bcode[id + 0] |= S_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(rshader->noutput++) |
95 S_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM);
96 break;
97 default:
98 fprintf(stderr, "%s:%d unsupported\n", __func__, __LINE__);
99 return -EINVAL;
100 }
101 *cid = id + 2;
102 return 0;
103 }
104
105 static int r700_shader_alu_bytecode(struct r600_shader *rshader,
106 struct r600_shader_node *rnode,
107 struct r600_shader_inst *alu,
108 unsigned *cid)
109 {
110 unsigned id = *cid;
111
112 /* don't replace gpr by pv or ps for destination register */
113 if (alu->is_op3) {
114 rshader->bcode[id++] = S_SQ_ALU_WORD0_SRC0_SEL(alu->src[0].sel) |
115 S_SQ_ALU_WORD0_SRC0_CHAN(alu->src[0].chan) |
116 S_SQ_ALU_WORD0_SRC1_SEL(alu->src[1].sel) |
117 S_SQ_ALU_WORD0_SRC1_CHAN(alu->src[1].chan) |
118 S_SQ_ALU_WORD0_LAST(alu->last);
119 rshader->bcode[id++] = S_SQ_ALU_WORD1_DST_GPR(alu->dst.sel) |
120 S_SQ_ALU_WORD1_DST_CHAN(alu->dst.chan) |
121 S_SQ_ALU_WORD1_OP3_SRC2_SEL(alu->src[2].sel) |
122 S_SQ_ALU_WORD1_OP3_SRC2_CHAN(alu->src[2].chan) |
123 S_SQ_ALU_WORD1_OP3_SRC2_NEG(alu->src[2].neg) |
124 S_SQ_ALU_WORD1_OP3_ALU_INST(alu->opcode) |
125 S_SQ_ALU_WORD1_BANK_SWIZZLE(0);
126 } else {
127 rshader->bcode[id++] = S_SQ_ALU_WORD0_SRC0_SEL(alu->src[0].sel) |
128 S_SQ_ALU_WORD0_SRC0_CHAN(alu->src[0].chan) |
129 S_SQ_ALU_WORD0_SRC0_NEG(alu->src[0].neg) |
130 S_SQ_ALU_WORD0_SRC1_SEL(alu->src[1].sel) |
131 S_SQ_ALU_WORD0_SRC1_CHAN(alu->src[1].chan) |
132 S_SQ_ALU_WORD0_SRC1_NEG(alu->src[1].neg) |
133 S_SQ_ALU_WORD0_LAST(alu->last);
134 rshader->bcode[id++] = S_SQ_ALU_WORD1_DST_GPR(alu->dst.sel) |
135 S_SQ_ALU_WORD1_DST_CHAN(alu->dst.chan) |
136 S_SQ_ALU_WORD1_OP2_SRC0_ABS(alu->src[0].abs) |
137 S_SQ_ALU_WORD1_OP2_SRC1_ABS(alu->src[1].abs) |
138 S_SQ_ALU_WORD1_OP2_WRITE_MASK(1) |
139 S_SQ_ALU_WORD1_OP2_ALU_INST(alu->opcode) |
140 S_SQ_ALU_WORD1_BANK_SWIZZLE(0);
141 }
142 *cid = id;
143 return 0;
144 }
145
146 static int r700_shader_alu_translate(struct r600_shader *rshader,
147 struct r600_shader_node *rnode,
148 unsigned *cid)
149
150 {
151 struct r600_shader_alu *alu;
152 unsigned id = *cid;
153 int i;
154 int r = 0;
155 LIST_FOR_EACH_ENTRY(alu, &rnode->alu, head) {
156 for (i = 0; i < alu->nalu; i++) {
157 r = r700_shader_alu_bytecode(rshader, rnode, &alu->alu[i], &id);
158 if (r)
159 goto out;
160 }
161 for (i = 0; i < alu->nliteral; i++) {
162 rshader->bcode[id++] = alu->literal[i];
163 }
164 }
165 out:
166 *cid = id;
167 return r;
168 }
169
170 int r700_shader_translate(struct r600_shader *rshader)
171 {
172 struct c_shader *shader = &rshader->cshader;
173 struct r600_shader_node *rnode;
174 struct r600_shader_vfetch *vfetch;
175 struct c_vector *v;
176 unsigned id, end;
177 int r;
178
179 r = r600_shader_register(rshader);
180 if (r) {
181 fprintf(stderr, "%s %d register allocation failed\n", __FILE__, __LINE__);
182 return r;
183 }
184 r = r600_shader_translate_rec(rshader, &shader->entry);
185 if (r) {
186 fprintf(stderr, "%s %d translation failed\n", __FILE__, __LINE__);
187 return r;
188 }
189 r = r600_shader_legalize(rshader);
190 if (r) {
191 fprintf(stderr, "%s %d legalize failed\n", __FILE__, __LINE__);
192 return r;
193 }
194 r600_shader_node_place(rshader);
195 rshader->bcode = malloc(rshader->ndw * 4);
196 if (rshader->bcode == NULL)
197 return -ENOMEM;
198 LIST_FOR_EACH_ENTRY(rnode, &rshader->nodes, head) {
199 id = rnode->cf_addr;
200 LIST_FOR_EACH_ENTRY(vfetch, &rnode->vfetch, head) {
201 r = r600_shader_vfetch_bytecode(rshader, rnode, vfetch, &id);
202 if (r)
203 return r;
204 }
205 if (rshader->r6xx_compile)
206 r = r6xx_shader_alu_translate(rshader, rnode, &id);
207 else
208 r = r700_shader_alu_translate(rshader, rnode, &id);
209 if (r)
210 return r;
211 }
212 id = 0;
213 LIST_FOR_EACH_ENTRY(rnode, &rshader->nodes, head) {
214 r = r700_shader_cf_node_bytecode(rshader, rnode, &id);
215 if (r)
216 return r;
217 }
218 LIST_FOR_EACH_ENTRY(v, &rshader->cshader.files[C_FILE_OUTPUT].vectors, head) {
219 end = 0;
220 if (v->head.next == &rshader->cshader.files[C_FILE_OUTPUT].vectors)
221 end = 1;
222 r = r700_shader_cf_output_bytecode(rshader, v, &id, end);
223 if (r)
224 return r;
225 }
226 LIST_FOR_EACH_ENTRY(v, &rshader->cshader.files[C_FILE_INPUT].vectors, head) {
227 rshader->input[rshader->ninput].gpr = rshader->ninput;
228 rshader->input[rshader->ninput].sid = v->sid;
229 rshader->input[rshader->ninput].name = v->name;
230 rshader->ninput++;
231 }
232 return 0;
233 }