2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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:
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
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.
24 #include "r600_context.h"
25 #include "util/u_memory.h"
30 int r700_bc_alu_build(struct r600_bc
*bc
, struct r600_bc_alu
*alu
, unsigned id
);
32 static struct r600_bc_cf
*r600_bc_cf(void)
34 struct r600_bc_cf
*cf
= CALLOC_STRUCT(r600_bc_cf
);
38 LIST_INITHEAD(&cf
->list
);
39 LIST_INITHEAD(&cf
->alu
);
40 LIST_INITHEAD(&cf
->vtx
);
41 LIST_INITHEAD(&cf
->tex
);
45 static struct r600_bc_alu
*r600_bc_alu(void)
47 struct r600_bc_alu
*alu
= CALLOC_STRUCT(r600_bc_alu
);
51 LIST_INITHEAD(&alu
->list
);
55 static struct r600_bc_vtx
*r600_bc_vtx(void)
57 struct r600_bc_vtx
*vtx
= CALLOC_STRUCT(r600_bc_vtx
);
61 LIST_INITHEAD(&vtx
->list
);
65 static struct r600_bc_tex
*r600_bc_tex(void)
67 struct r600_bc_tex
*tex
= CALLOC_STRUCT(r600_bc_tex
);
71 LIST_INITHEAD(&tex
->list
);
75 int r600_bc_init(struct r600_bc
*bc
, enum radeon_family family
)
77 LIST_INITHEAD(&bc
->cf
);
82 static int r600_bc_add_cf(struct r600_bc
*bc
)
84 struct r600_bc_cf
*cf
= r600_bc_cf();
88 LIST_ADDTAIL(&cf
->list
, &bc
->cf
);
90 cf
->id
= bc
->cf_last
->id
+ 2;
98 int r600_bc_add_output(struct r600_bc
*bc
, const struct r600_bc_output
*output
)
102 r
= r600_bc_add_cf(bc
);
105 bc
->cf_last
->inst
= output
->inst
;
106 memcpy(&bc
->cf_last
->output
, output
, sizeof(struct r600_bc_output
));
110 int r600_bc_add_alu(struct r600_bc
*bc
, const struct r600_bc_alu
*alu
)
112 struct r600_bc_alu
*nalu
= r600_bc_alu();
113 struct r600_bc_alu
*lalu
;
118 memcpy(nalu
, alu
, sizeof(struct r600_bc_alu
));
121 /* cf can contains only alu or only vtx or only tex */
122 if (bc
->cf_last
== NULL
|| bc
->cf_last
->inst
!= (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU
<< 3) ||
124 /* at most 128 slots, one add alu can add 4 slots + 4 constant worst case */
125 r
= r600_bc_add_cf(bc
);
130 bc
->cf_last
->inst
= V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU
<< 3;
132 if (alu
->last
&& (bc
->cf_last
->ndw
>> 1) >= 124) {
133 bc
->force_add_cf
= 1;
135 /* number of gpr == the last gpr used in any alu */
136 for (i
= 0; i
< 3; i
++) {
137 if (alu
->src
[i
].sel
>= bc
->ngpr
&& alu
->src
[i
].sel
< 128) {
138 bc
->ngpr
= alu
->src
[i
].sel
+ 1;
140 /* compute how many literal are needed
141 * either 2 or 4 literals
143 if (alu
->src
[i
].sel
== 253) {
144 if (((alu
->src
[i
].chan
+ 2) & 0x6) > nalu
->nliteral
) {
145 nalu
->nliteral
= (alu
->src
[i
].chan
+ 2) & 0x6;
149 if (!LIST_IS_EMPTY(&bc
->cf_last
->alu
)) {
150 lalu
= LIST_ENTRY(struct r600_bc_alu
, bc
->cf_last
->alu
.prev
, list
);
151 if (!lalu
->last
&& lalu
->nliteral
> nalu
->nliteral
) {
152 nalu
->nliteral
= lalu
->nliteral
;
155 if (alu
->dst
.sel
>= bc
->ngpr
) {
156 bc
->ngpr
= alu
->dst
.sel
+ 1;
158 LIST_ADDTAIL(&nalu
->list
, &bc
->cf_last
->alu
);
159 /* each alu use 2 dwords */
160 bc
->cf_last
->ndw
+= 2;
165 int r600_bc_add_literal(struct r600_bc
*bc
, const u32
*value
)
167 struct r600_bc_alu
*alu
;
169 if (bc
->cf_last
== NULL
) {
170 R600_ERR("no last CF\n");
173 if (bc
->cf_last
->inst
== V_SQ_CF_WORD1_SQ_CF_INST_TEX
) {
176 if (bc
->cf_last
->inst
!= (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU
<< 3) ||
177 LIST_IS_EMPTY(&bc
->cf_last
->alu
)) {
178 R600_ERR("last CF is not ALU (%p)\n", bc
->cf_last
);
181 alu
= LIST_ENTRY(struct r600_bc_alu
, bc
->cf_last
->alu
.prev
, list
);
182 if (!alu
->last
|| !alu
->nliteral
) {
185 memcpy(alu
->value
, value
, 4 * 4);
186 bc
->cf_last
->ndw
+= alu
->nliteral
;
187 bc
->ndw
+= alu
->nliteral
;
191 int r600_bc_add_vtx(struct r600_bc
*bc
, const struct r600_bc_vtx
*vtx
)
193 struct r600_bc_vtx
*nvtx
= r600_bc_vtx();
198 memcpy(nvtx
, vtx
, sizeof(struct r600_bc_vtx
));
200 /* cf can contains only alu or only vtx or only tex */
201 if (bc
->cf_last
== NULL
||
202 (bc
->cf_last
->inst
!= V_SQ_CF_WORD1_SQ_CF_INST_VTX
&&
203 bc
->cf_last
->inst
!= V_SQ_CF_WORD1_SQ_CF_INST_VTX_TC
)) {
204 r
= r600_bc_add_cf(bc
);
209 bc
->cf_last
->inst
= V_SQ_CF_WORD1_SQ_CF_INST_VTX
;
211 LIST_ADDTAIL(&nvtx
->list
, &bc
->cf_last
->vtx
);
212 /* each fetch use 4 dwords */
213 bc
->cf_last
->ndw
+= 4;
218 int r600_bc_add_tex(struct r600_bc
*bc
, const struct r600_bc_tex
*tex
)
220 struct r600_bc_tex
*ntex
= r600_bc_tex();
225 memcpy(ntex
, tex
, sizeof(struct r600_bc_tex
));
227 /* cf can contains only alu or only vtx or only tex */
228 if (bc
->cf_last
== NULL
||
229 bc
->cf_last
->inst
!= V_SQ_CF_WORD1_SQ_CF_INST_TEX
) {
230 r
= r600_bc_add_cf(bc
);
235 bc
->cf_last
->inst
= V_SQ_CF_WORD1_SQ_CF_INST_TEX
;
237 LIST_ADDTAIL(&ntex
->list
, &bc
->cf_last
->tex
);
238 /* each texture fetch use 4 dwords */
239 bc
->cf_last
->ndw
+= 4;
244 static int r600_bc_vtx_build(struct r600_bc
*bc
, struct r600_bc_vtx
*vtx
, unsigned id
)
246 bc
->bytecode
[id
++] = S_SQ_VTX_WORD0_BUFFER_ID(vtx
->buffer_id
) |
247 S_SQ_VTX_WORD0_SRC_GPR(vtx
->src_gpr
) |
248 S_SQ_VTX_WORD0_SRC_SEL_X(vtx
->src_sel_x
) |
249 S_SQ_VTX_WORD0_MEGA_FETCH_COUNT(vtx
->mega_fetch_count
);
250 bc
->bytecode
[id
++] = S_SQ_VTX_WORD1_DST_SEL_X(vtx
->dst_sel_x
) |
251 S_SQ_VTX_WORD1_DST_SEL_Y(vtx
->dst_sel_y
) |
252 S_SQ_VTX_WORD1_DST_SEL_Z(vtx
->dst_sel_z
) |
253 S_SQ_VTX_WORD1_DST_SEL_W(vtx
->dst_sel_w
) |
254 S_SQ_VTX_WORD1_USE_CONST_FIELDS(1) |
255 S_SQ_VTX_WORD1_GPR_DST_GPR(vtx
->dst_gpr
);
256 bc
->bytecode
[id
++] = S_SQ_VTX_WORD2_MEGA_FETCH(1);
257 bc
->bytecode
[id
++] = 0;
261 static int r600_bc_tex_build(struct r600_bc
*bc
, struct r600_bc_tex
*tex
, unsigned id
)
263 bc
->bytecode
[id
++] = S_SQ_TEX_WORD0_TEX_INST(tex
->inst
) |
264 S_SQ_TEX_WORD0_RESOURCE_ID(tex
->resource_id
) |
265 S_SQ_TEX_WORD0_SRC_GPR(tex
->src_gpr
) |
266 S_SQ_TEX_WORD0_SRC_REL(tex
->src_rel
);
267 bc
->bytecode
[id
++] = S_SQ_TEX_WORD1_DST_GPR(tex
->dst_gpr
) |
268 S_SQ_TEX_WORD1_DST_REL(tex
->dst_rel
) |
269 S_SQ_TEX_WORD1_DST_SEL_X(tex
->dst_sel_x
) |
270 S_SQ_TEX_WORD1_DST_SEL_Y(tex
->dst_sel_y
) |
271 S_SQ_TEX_WORD1_DST_SEL_Z(tex
->dst_sel_z
) |
272 S_SQ_TEX_WORD1_DST_SEL_W(tex
->dst_sel_w
) |
273 S_SQ_TEX_WORD1_LOD_BIAS(tex
->lod_bias
) |
274 S_SQ_TEX_WORD1_COORD_TYPE_X(tex
->coord_type_x
) |
275 S_SQ_TEX_WORD1_COORD_TYPE_Y(tex
->coord_type_y
) |
276 S_SQ_TEX_WORD1_COORD_TYPE_Z(tex
->coord_type_z
) |
277 S_SQ_TEX_WORD1_COORD_TYPE_W(tex
->coord_type_w
);
278 bc
->bytecode
[id
++] = S_SQ_TEX_WORD2_OFFSET_X(tex
->offset_x
) |
279 S_SQ_TEX_WORD2_OFFSET_Y(tex
->offset_y
) |
280 S_SQ_TEX_WORD2_OFFSET_Z(tex
->offset_z
) |
281 S_SQ_TEX_WORD2_SAMPLER_ID(tex
->sampler_id
) |
282 S_SQ_TEX_WORD2_SRC_SEL_X(tex
->src_sel_x
) |
283 S_SQ_TEX_WORD2_SRC_SEL_Y(tex
->src_sel_y
) |
284 S_SQ_TEX_WORD2_SRC_SEL_Z(tex
->src_sel_z
) |
285 S_SQ_TEX_WORD2_SRC_SEL_W(tex
->src_sel_w
);
286 bc
->bytecode
[id
++] = 0;
290 int r600_bc_alu_build(struct r600_bc
*bc
, struct r600_bc_alu
*alu
, unsigned id
)
294 /* don't replace gpr by pv or ps for destination register */
296 bc
->bytecode
[id
++] = S_SQ_ALU_WORD0_SRC0_SEL(alu
->src
[0].sel
) |
297 S_SQ_ALU_WORD0_SRC0_CHAN(alu
->src
[0].chan
) |
298 S_SQ_ALU_WORD0_SRC1_SEL(alu
->src
[1].sel
) |
299 S_SQ_ALU_WORD0_SRC1_CHAN(alu
->src
[1].chan
) |
300 S_SQ_ALU_WORD0_LAST(alu
->last
);
301 bc
->bytecode
[id
++] = S_SQ_ALU_WORD1_DST_GPR(alu
->dst
.sel
) |
302 S_SQ_ALU_WORD1_DST_CHAN(alu
->dst
.chan
) |
303 S_SQ_ALU_WORD1_CLAMP(alu
->dst
.clamp
) |
304 S_SQ_ALU_WORD1_OP3_SRC2_SEL(alu
->src
[2].sel
) |
305 S_SQ_ALU_WORD1_OP3_SRC2_CHAN(alu
->src
[2].chan
) |
306 S_SQ_ALU_WORD1_OP3_SRC2_NEG(alu
->src
[2].neg
) |
307 S_SQ_ALU_WORD1_OP3_ALU_INST(alu
->inst
) |
308 S_SQ_ALU_WORD1_BANK_SWIZZLE(0);
310 bc
->bytecode
[id
++] = S_SQ_ALU_WORD0_SRC0_SEL(alu
->src
[0].sel
) |
311 S_SQ_ALU_WORD0_SRC0_CHAN(alu
->src
[0].chan
) |
312 S_SQ_ALU_WORD0_SRC0_NEG(alu
->src
[0].neg
) |
313 S_SQ_ALU_WORD0_SRC1_SEL(alu
->src
[1].sel
) |
314 S_SQ_ALU_WORD0_SRC1_CHAN(alu
->src
[1].chan
) |
315 S_SQ_ALU_WORD0_SRC1_NEG(alu
->src
[1].neg
) |
316 S_SQ_ALU_WORD0_LAST(alu
->last
);
317 bc
->bytecode
[id
++] = S_SQ_ALU_WORD1_DST_GPR(alu
->dst
.sel
) |
318 S_SQ_ALU_WORD1_DST_CHAN(alu
->dst
.chan
) |
319 S_SQ_ALU_WORD1_CLAMP(alu
->dst
.clamp
) |
320 S_SQ_ALU_WORD1_OP2_SRC0_ABS(alu
->src
[0].abs
) |
321 S_SQ_ALU_WORD1_OP2_SRC1_ABS(alu
->src
[1].abs
) |
322 S_SQ_ALU_WORD1_OP2_WRITE_MASK(alu
->dst
.write
) |
323 S_SQ_ALU_WORD1_OP2_ALU_INST(alu
->inst
) |
324 S_SQ_ALU_WORD1_BANK_SWIZZLE(0);
327 for (i
= 0; i
< alu
->nliteral
; i
++) {
328 bc
->bytecode
[id
++] = alu
->value
[i
];
334 int r600_bc_cf_build(struct r600_bc
*bc
, struct r600_bc_cf
*cf
)
336 unsigned id
= cf
->id
;
339 case (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU
<< 3):
340 bc
->bytecode
[id
++] = S_SQ_CF_ALU_WORD0_ADDR(cf
->addr
>> 1);
341 bc
->bytecode
[id
++] = S_SQ_CF_ALU_WORD1_CF_INST(cf
->inst
>> 3) |
342 S_SQ_CF_ALU_WORD1_BARRIER(1) |
343 S_SQ_CF_ALU_WORD1_COUNT((cf
->ndw
/ 2) - 1);
345 case V_SQ_CF_WORD1_SQ_CF_INST_TEX
:
346 case V_SQ_CF_WORD1_SQ_CF_INST_VTX
:
347 case V_SQ_CF_WORD1_SQ_CF_INST_VTX_TC
:
348 bc
->bytecode
[id
++] = S_SQ_CF_WORD0_ADDR(cf
->addr
>> 1);
349 bc
->bytecode
[id
++] = S_SQ_CF_WORD1_CF_INST(cf
->inst
) |
350 S_SQ_CF_WORD1_BARRIER(1) |
351 S_SQ_CF_WORD1_COUNT((cf
->ndw
/ 4) - 1);
353 case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT
:
354 case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT_DONE
:
355 bc
->bytecode
[id
++] = S_SQ_CF_ALLOC_EXPORT_WORD0_RW_GPR(cf
->output
.gpr
) |
356 S_SQ_CF_ALLOC_EXPORT_WORD0_ELEM_SIZE(cf
->output
.elem_size
) |
357 S_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(cf
->output
.array_base
) |
358 S_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(cf
->output
.type
);
359 bc
->bytecode
[id
++] = S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_X(cf
->output
.swizzle_x
) |
360 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Y(cf
->output
.swizzle_y
) |
361 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Z(cf
->output
.swizzle_z
) |
362 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_W(cf
->output
.swizzle_w
) |
363 S_SQ_CF_ALLOC_EXPORT_WORD1_BARRIER(cf
->output
.barrier
) |
364 S_SQ_CF_ALLOC_EXPORT_WORD1_CF_INST(cf
->output
.inst
) |
365 S_SQ_CF_ALLOC_EXPORT_WORD1_END_OF_PROGRAM(cf
->output
.end_of_program
);
368 R600_ERR("unsupported CF instruction (0x%X)\n", cf
->inst
);
374 int r600_bc_build(struct r600_bc
*bc
)
376 struct r600_bc_cf
*cf
;
377 struct r600_bc_alu
*alu
;
378 struct r600_bc_vtx
*vtx
;
379 struct r600_bc_tex
*tex
;
384 /* first path compute addr of each CF block */
385 /* addr start after all the CF instructions */
386 addr
= bc
->cf_last
->id
+ 2;
387 LIST_FOR_EACH_ENTRY(cf
, &bc
->cf
, list
) {
389 case (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU
<< 3):
391 case V_SQ_CF_WORD1_SQ_CF_INST_TEX
:
392 case V_SQ_CF_WORD1_SQ_CF_INST_VTX
:
393 case V_SQ_CF_WORD1_SQ_CF_INST_VTX_TC
:
394 /* fetch node need to be 16 bytes aligned*/
396 addr
&= 0xFFFFFFFCUL
;
398 case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT
:
399 case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT_DONE
:
402 R600_ERR("unsupported CF instruction (0x%X)\n", cf
->inst
);
407 bc
->ndw
= cf
->addr
+ cf
->ndw
;
410 bc
->bytecode
= calloc(1, bc
->ndw
* 4);
411 if (bc
->bytecode
== NULL
)
413 LIST_FOR_EACH_ENTRY(cf
, &bc
->cf
, list
) {
415 r
= r600_bc_cf_build(bc
, cf
);
419 case (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU
<< 3):
420 LIST_FOR_EACH_ENTRY(alu
, &cf
->alu
, list
) {
421 switch (bc
->family
) {
430 r
= r600_bc_alu_build(bc
, alu
, addr
);
436 r
= r700_bc_alu_build(bc
, alu
, addr
);
439 R600_ERR("unknown family %d\n", bc
->family
);
446 addr
+= alu
->nliteral
;
450 case V_SQ_CF_WORD1_SQ_CF_INST_VTX
:
451 case V_SQ_CF_WORD1_SQ_CF_INST_VTX_TC
:
452 LIST_FOR_EACH_ENTRY(vtx
, &cf
->vtx
, list
) {
453 r
= r600_bc_vtx_build(bc
, vtx
, addr
);
459 case V_SQ_CF_WORD1_SQ_CF_INST_TEX
:
460 LIST_FOR_EACH_ENTRY(tex
, &cf
->tex
, list
) {
461 r
= r600_bc_tex_build(bc
, tex
, addr
);
467 case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT
:
468 case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT_DONE
:
471 R600_ERR("unsupported CF instruction (0x%X)\n", cf
->inst
);