2 * Mesa 3-D graphics library
4 * Copyright (C) 2012-2013 LunarG, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Chia-I Wu <olv@lunarg.com>
28 #include "tgsi/tgsi_dump.h"
29 #include "tgsi/tgsi_util.h"
30 #include "toy_compiler.h"
32 #include "toy_legalize.h"
33 #include "toy_optimize.h"
34 #include "toy_helpers.h"
35 #include "ilo_context.h"
36 #include "ilo_shader_internal.h"
38 struct vs_compile_context
{
39 struct ilo_shader
*shader
;
40 const struct ilo_shader_variant
*variant
;
42 struct toy_compiler tc
;
44 enum brw_message_target const_cache
;
46 int output_map
[PIPE_MAX_SHADER_OUTPUTS
];
60 vs_lower_opcode_tgsi_in(struct vs_compile_context
*vcc
,
61 struct toy_dst dst
, int dim
, int idx
)
63 struct toy_compiler
*tc
= &vcc
->tc
;
68 slot
= toy_tgsi_find_input(&vcc
->tgsi
, idx
);
70 const int first_in_grf
= vcc
->first_vue_grf
+
71 (vcc
->shader
->in
.count
- vcc
->tgsi
.num_inputs
);
72 const int grf
= first_in_grf
+ vcc
->tgsi
.inputs
[slot
].semantic_index
;
73 const struct toy_src src
= tsrc(TOY_FILE_GRF
, grf
, 0);
78 /* undeclared input */
79 tc_MOV(tc
, dst
, tsrc_imm_f(0.0f
));
84 vs_lower_opcode_tgsi_const_pcb(struct vs_compile_context
*vcc
,
85 struct toy_dst dst
, int dim
,
88 const int i
= idx
.val32
;
89 const int grf
= vcc
->first_const_grf
+ i
/ 2;
90 const int grf_subreg
= (i
& 1) * 16;
93 if (!vcc
->variant
->use_pcb
|| dim
!= 0 || idx
.file
!= TOY_FILE_IMM
||
94 grf
>= vcc
->first_ucp_grf
)
98 src
= tsrc_rect(tsrc(TOY_FILE_GRF
, grf
, grf_subreg
), TOY_RECT_041
);
99 tc_MOV(&vcc
->tc
, dst
, src
);
105 vs_lower_opcode_tgsi_const_gen6(struct vs_compile_context
*vcc
,
106 struct toy_dst dst
, int dim
,
109 const struct toy_dst header
=
110 tdst_ud(tdst(TOY_FILE_MRF
, vcc
->first_free_mrf
, 0));
111 const struct toy_dst block_offsets
=
112 tdst_ud(tdst(TOY_FILE_MRF
, vcc
->first_free_mrf
+ 1, 0));
113 const struct toy_src r0
= tsrc_ud(tsrc(TOY_FILE_GRF
, 0, 0));
114 struct toy_compiler
*tc
= &vcc
->tc
;
115 unsigned msg_type
, msg_ctrl
, msg_len
;
116 struct toy_inst
*inst
;
119 if (vs_lower_opcode_tgsi_const_pcb(vcc
, dst
, dim
, idx
))
122 /* set message header */
123 inst
= tc_MOV(tc
, header
, r0
);
124 inst
->mask_ctrl
= BRW_MASK_DISABLE
;
126 /* set block offsets */
127 tc_MOV(tc
, block_offsets
, idx
);
129 msg_type
= GEN6_DATAPORT_READ_MESSAGE_OWORD_DUAL_BLOCK_READ
;
130 msg_ctrl
= BRW_DATAPORT_OWORD_DUAL_BLOCK_1OWORD
<< 8;;
133 desc
= tsrc_imm_mdesc_data_port(tc
, false, msg_len
, 1, true, false,
134 msg_type
, msg_ctrl
, ILO_VS_CONST_SURFACE(dim
));
136 tc_SEND(tc
, dst
, tsrc_from(header
), desc
, vcc
->const_cache
);
140 vs_lower_opcode_tgsi_const_gen7(struct vs_compile_context
*vcc
,
141 struct toy_dst dst
, int dim
,
144 struct toy_compiler
*tc
= &vcc
->tc
;
145 const struct toy_dst offset
=
146 tdst_ud(tdst(TOY_FILE_MRF
, vcc
->first_free_mrf
, 0));
149 if (vs_lower_opcode_tgsi_const_pcb(vcc
, dst
, dim
, idx
))
153 * In 259b65e2e7938de4aab323033cfe2b33369ddb07, pull constant load was
154 * changed from OWord Dual Block Read to ld to increase performance in the
155 * classic driver. Since we use the constant cache instead of the data
156 * cache, I wonder if we still want to follow the classic driver.
160 tc_MOV(tc
, offset
, idx
);
162 desc
= tsrc_imm_mdesc_sampler(tc
, 1, 1, false,
163 BRW_SAMPLER_SIMD_MODE_SIMD4X2
,
164 GEN5_SAMPLER_MESSAGE_SAMPLE_LD
,
166 ILO_VS_CONST_SURFACE(dim
));
168 tc_SEND(tc
, dst
, tsrc_from(offset
), desc
, BRW_SFID_SAMPLER
);
172 vs_lower_opcode_tgsi_imm(struct vs_compile_context
*vcc
,
173 struct toy_dst dst
, int idx
)
178 imm
= toy_tgsi_get_imm(&vcc
->tgsi
, idx
, NULL
);
180 for (ch
= 0; ch
< 4; ch
++) {
183 tdst_writemask(tdst_ud(dst
), 1 << ch
),
184 tsrc_imm_ud(imm
[ch
]));
190 vs_lower_opcode_tgsi_sv(struct vs_compile_context
*vcc
,
191 struct toy_dst dst
, int dim
, int idx
)
193 struct toy_compiler
*tc
= &vcc
->tc
;
194 const struct toy_tgsi
*tgsi
= &vcc
->tgsi
;
199 slot
= toy_tgsi_find_system_value(tgsi
, idx
);
203 switch (tgsi
->system_values
[slot
].semantic_name
) {
204 case TGSI_SEMANTIC_INSTANCEID
:
205 case TGSI_SEMANTIC_VERTEXID
:
207 * In 3DSTATE_VERTEX_ELEMENTS, we prepend an extra vertex element for
208 * the generated IDs, with VID in the X channel and IID in the Y
212 const int grf
= vcc
->first_vue_grf
;
213 const struct toy_src src
= tsrc(TOY_FILE_GRF
, grf
, 0);
214 const enum toy_swizzle swizzle
=
215 (tgsi
->system_values
[slot
].semantic_name
==
216 TGSI_SEMANTIC_INSTANCEID
) ? TOY_SWIZZLE_Y
: TOY_SWIZZLE_X
;
218 tc_MOV(tc
, tdst_d(dst
), tsrc_d(tsrc_swizzle1(src
, swizzle
)));
221 case TGSI_SEMANTIC_PRIMID
:
223 tc_fail(tc
, "unhandled system value");
224 tc_MOV(tc
, dst
, tsrc_imm_d(0));
230 vs_lower_opcode_tgsi_direct(struct vs_compile_context
*vcc
,
231 struct toy_inst
*inst
)
233 struct toy_compiler
*tc
= &vcc
->tc
;
236 assert(inst
->src
[0].file
== TOY_FILE_IMM
);
237 dim
= inst
->src
[0].val32
;
239 assert(inst
->src
[1].file
== TOY_FILE_IMM
);
240 idx
= inst
->src
[1].val32
;
242 switch (inst
->opcode
) {
243 case TOY_OPCODE_TGSI_IN
:
244 vs_lower_opcode_tgsi_in(vcc
, inst
->dst
, dim
, idx
);
246 case TOY_OPCODE_TGSI_CONST
:
247 if (tc
->dev
->gen
>= ILO_GEN(7))
248 vs_lower_opcode_tgsi_const_gen7(vcc
, inst
->dst
, dim
, inst
->src
[1]);
250 vs_lower_opcode_tgsi_const_gen6(vcc
, inst
->dst
, dim
, inst
->src
[1]);
252 case TOY_OPCODE_TGSI_SV
:
253 vs_lower_opcode_tgsi_sv(vcc
, inst
->dst
, dim
, idx
);
255 case TOY_OPCODE_TGSI_IMM
:
257 vs_lower_opcode_tgsi_imm(vcc
, inst
->dst
, idx
);
260 tc_fail(tc
, "unhandled TGSI fetch");
264 tc_discard_inst(tc
, inst
);
268 vs_lower_opcode_tgsi_indirect(struct vs_compile_context
*vcc
,
269 struct toy_inst
*inst
)
271 struct toy_compiler
*tc
= &vcc
->tc
;
272 enum tgsi_file_type file
;
274 struct toy_src indirect_dim
, indirect_idx
;
276 assert(inst
->src
[0].file
== TOY_FILE_IMM
);
277 file
= inst
->src
[0].val32
;
279 assert(inst
->src
[1].file
== TOY_FILE_IMM
);
280 dim
= inst
->src
[1].val32
;
281 indirect_dim
= inst
->src
[2];
283 assert(inst
->src
[3].file
== TOY_FILE_IMM
);
284 idx
= inst
->src
[3].val32
;
285 indirect_idx
= inst
->src
[4];
287 /* no dimension indirection */
288 assert(indirect_dim
.file
== TOY_FILE_IMM
);
289 dim
+= indirect_dim
.val32
;
291 switch (inst
->opcode
) {
292 case TOY_OPCODE_TGSI_INDIRECT_FETCH
:
293 if (file
== TGSI_FILE_CONSTANT
) {
295 struct toy_dst tmp
= tc_alloc_tmp(tc
);
297 tc_ADD(tc
, tmp
, indirect_idx
, tsrc_imm_d(idx
));
298 indirect_idx
= tsrc_from(tmp
);
301 if (tc
->dev
->gen
>= ILO_GEN(7))
302 vs_lower_opcode_tgsi_const_gen7(vcc
, inst
->dst
, dim
, indirect_idx
);
304 vs_lower_opcode_tgsi_const_gen6(vcc
, inst
->dst
, dim
, indirect_idx
);
308 case TOY_OPCODE_TGSI_INDIRECT_STORE
:
310 tc_fail(tc
, "unhandled TGSI indirection");
314 tc_discard_inst(tc
, inst
);
318 * Emit instructions to move sampling parameters to the message registers.
321 vs_add_sampler_params(struct toy_compiler
*tc
, int msg_type
, int base_mrf
,
322 struct toy_src coords
, int num_coords
,
323 struct toy_src bias_or_lod
, struct toy_src ref_or_si
,
324 struct toy_src ddx
, struct toy_src ddy
, int num_derivs
)
326 const unsigned coords_writemask
= (1 << num_coords
) - 1;
330 assert(num_coords
<= 4);
331 assert(num_derivs
<= 3 && num_derivs
<= num_coords
);
333 for (i
= 0; i
< Elements(m
); i
++)
334 m
[i
] = tdst(TOY_FILE_MRF
, base_mrf
+ i
, 0);
337 case GEN5_SAMPLER_MESSAGE_SAMPLE_LOD
:
338 tc_MOV(tc
, tdst_writemask(m
[0], coords_writemask
), coords
);
339 tc_MOV(tc
, tdst_writemask(m
[1], TOY_WRITEMASK_X
), bias_or_lod
);
342 case GEN5_SAMPLER_MESSAGE_SAMPLE_DERIVS
:
343 tc_MOV(tc
, tdst_writemask(m
[0], coords_writemask
), coords
);
344 tc_MOV(tc
, tdst_writemask(m
[1], TOY_WRITEMASK_XZ
),
345 tsrc_swizzle(ddx
, 0, 0, 1, 1));
346 tc_MOV(tc
, tdst_writemask(m
[1], TOY_WRITEMASK_YW
),
347 tsrc_swizzle(ddy
, 0, 0, 1, 1));
348 if (num_derivs
> 2) {
349 tc_MOV(tc
, tdst_writemask(m
[2], TOY_WRITEMASK_X
),
350 tsrc_swizzle1(ddx
, 2));
351 tc_MOV(tc
, tdst_writemask(m
[2], TOY_WRITEMASK_Y
),
352 tsrc_swizzle1(ddy
, 2));
354 num_params
= 4 + num_derivs
* 2;
356 case GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE
:
357 tc_MOV(tc
, tdst_writemask(m
[0], coords_writemask
), coords
);
358 tc_MOV(tc
, tdst_writemask(m
[1], TOY_WRITEMASK_X
), ref_or_si
);
359 tc_MOV(tc
, tdst_writemask(m
[1], TOY_WRITEMASK_Y
), bias_or_lod
);
362 case GEN5_SAMPLER_MESSAGE_SAMPLE_LD
:
363 assert(num_coords
<= 3);
364 tc_MOV(tc
, tdst_writemask(tdst_d(m
[0]), coords_writemask
), coords
);
365 tc_MOV(tc
, tdst_writemask(tdst_d(m
[0]), TOY_WRITEMASK_W
), bias_or_lod
);
366 if (tc
->dev
->gen
>= ILO_GEN(7)) {
370 tc_MOV(tc
, tdst_writemask(tdst_d(m
[1]), TOY_WRITEMASK_X
), ref_or_si
);
374 case GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO
:
375 tc_MOV(tc
, tdst_writemask(tdst_d(m
[0]), TOY_WRITEMASK_X
), bias_or_lod
);
379 tc_fail(tc
, "unknown sampler opcode");
384 return (num_params
+ 3) / 4;
388 * Set up message registers and return the message descriptor for sampling.
390 static struct toy_src
391 vs_prepare_tgsi_sampling(struct toy_compiler
*tc
, const struct toy_inst
*inst
,
392 int base_mrf
, unsigned *ret_sampler_index
)
394 unsigned simd_mode
, msg_type
, msg_len
, sampler_index
, binding_table_index
;
395 struct toy_src coords
, ddx
, ddy
, bias_or_lod
, ref_or_si
;
396 int num_coords
, ref_pos
, num_derivs
;
399 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD4X2
;
401 coords
= inst
->src
[0];
404 bias_or_lod
= tsrc_null();
405 ref_or_si
= tsrc_null();
409 num_coords
= tgsi_util_get_texture_coord_dim(inst
->tex
.target
, &ref_pos
);
411 /* extract the parameters */
412 switch (inst
->opcode
) {
413 case TOY_OPCODE_TGSI_TXD
:
415 tc_fail(tc
, "TXD with shadow sampler not supported");
417 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_DERIVS
;
420 num_derivs
= num_coords
;
423 case TOY_OPCODE_TGSI_TXL
:
427 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE
;
428 ref_or_si
= tsrc_swizzle1(coords
, ref_pos
);
431 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LOD
;
434 bias_or_lod
= tsrc_swizzle1(coords
, TOY_SWIZZLE_W
);
436 case TOY_OPCODE_TGSI_TXF
:
437 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LD
;
439 switch (inst
->tex
.target
) {
440 case TGSI_TEXTURE_2D_MSAA
:
441 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
442 assert(ref_pos
>= 0 && ref_pos
< 4);
443 /* lod is always 0 */
444 bias_or_lod
= tsrc_imm_d(0);
445 ref_or_si
= tsrc_swizzle1(coords
, ref_pos
);
448 bias_or_lod
= tsrc_swizzle1(coords
, TOY_SWIZZLE_W
);
452 /* offset the coordinates */
453 if (!tsrc_is_null(inst
->tex
.offsets
[0])) {
456 tmp
= tc_alloc_tmp(tc
);
457 tc_ADD(tc
, tmp
, coords
, inst
->tex
.offsets
[0]);
458 coords
= tsrc_from(tmp
);
463 case TOY_OPCODE_TGSI_TXQ
:
464 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO
;
466 bias_or_lod
= tsrc_swizzle1(coords
, TOY_SWIZZLE_X
);
468 case TOY_OPCODE_TGSI_TXQ_LZ
:
469 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO
;
473 case TOY_OPCODE_TGSI_TXL2
:
477 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE
;
478 ref_or_si
= tsrc_swizzle1(coords
, ref_pos
);
481 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LOD
;
484 bias_or_lod
= tsrc_swizzle1(inst
->src
[1], TOY_SWIZZLE_X
);
488 assert(!"unhandled sampling opcode");
489 if (ret_sampler_index
)
490 *ret_sampler_index
= 0;
495 assert(inst
->src
[sampler_src
].file
== TOY_FILE_IMM
);
496 sampler_index
= inst
->src
[sampler_src
].val32
;
497 binding_table_index
= ILO_VS_TEXTURE_SURFACE(sampler_index
);
500 * From the Sandy Bridge PRM, volume 4 part 1, page 18:
502 * "Note that the (cube map) coordinates delivered to the sampling
503 * engine must already have been divided by the component with the
504 * largest absolute value."
506 switch (inst
->tex
.target
) {
507 case TGSI_TEXTURE_CUBE
:
508 case TGSI_TEXTURE_SHADOWCUBE
:
509 case TGSI_TEXTURE_CUBE_ARRAY
:
510 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
511 /* TXQ does not need coordinates */
512 if (num_coords
>= 3) {
513 struct toy_dst tmp
, max
;
514 struct toy_src abs_coords
[3];
517 tmp
= tc_alloc_tmp(tc
);
518 max
= tdst_writemask(tmp
, TOY_WRITEMASK_W
);
520 for (i
= 0; i
< 3; i
++)
521 abs_coords
[i
] = tsrc_absolute(tsrc_swizzle1(coords
, i
));
523 tc_SEL(tc
, max
, abs_coords
[0], abs_coords
[0], BRW_CONDITIONAL_GE
);
524 tc_SEL(tc
, max
, tsrc_from(max
), abs_coords
[0], BRW_CONDITIONAL_GE
);
525 tc_INV(tc
, max
, tsrc_from(max
));
527 for (i
= 0; i
< 3; i
++)
528 tc_MUL(tc
, tdst_writemask(tmp
, 1 << i
), coords
, tsrc_from(max
));
530 coords
= tsrc_from(tmp
);
535 /* set up sampler parameters */
536 msg_len
= vs_add_sampler_params(tc
, msg_type
, base_mrf
,
537 coords
, num_coords
, bias_or_lod
, ref_or_si
, ddx
, ddy
, num_derivs
);
540 * From the Sandy Bridge PRM, volume 4 part 1, page 136:
542 * "The maximum message length allowed to the sampler is 11. This would
543 * disallow sample_d, sample_b_c, and sample_l_c with a SIMD Mode of
547 tc_fail(tc
, "maximum length for messages to the sampler is 11");
549 if (ret_sampler_index
)
550 *ret_sampler_index
= sampler_index
;
552 return tsrc_imm_mdesc_sampler(tc
, msg_len
, 1,
553 false, simd_mode
, msg_type
, sampler_index
, binding_table_index
);
557 vs_lower_opcode_tgsi_sampling(struct vs_compile_context
*vcc
,
558 struct toy_inst
*inst
)
560 struct toy_compiler
*tc
= &vcc
->tc
;
562 struct toy_dst dst
, tmp
;
563 unsigned sampler_index
;
565 unsigned swizzle_zero_mask
, swizzle_one_mask
, swizzle_normal_mask
;
568 desc
= vs_prepare_tgsi_sampling(tc
, inst
,
569 vcc
->first_free_mrf
, &sampler_index
);
571 switch (inst
->opcode
) {
572 case TOY_OPCODE_TGSI_TXF
:
573 case TOY_OPCODE_TGSI_TXQ
:
574 case TOY_OPCODE_TGSI_TXQ_LZ
:
582 toy_compiler_lower_to_send(tc
, inst
, false, BRW_SFID_SAMPLER
);
583 inst
->src
[0] = tsrc(TOY_FILE_MRF
, vcc
->first_free_mrf
, 0);
586 /* write to a temp first */
587 tmp
= tc_alloc_tmp(tc
);
588 tmp
.type
= inst
->dst
.type
;
592 tc_move_inst(tc
, inst
);
595 assert(sampler_index
< vcc
->variant
->num_sampler_views
);
596 swizzles
[0] = vcc
->variant
->sampler_view_swizzles
[sampler_index
].r
;
597 swizzles
[1] = vcc
->variant
->sampler_view_swizzles
[sampler_index
].g
;
598 swizzles
[2] = vcc
->variant
->sampler_view_swizzles
[sampler_index
].b
;
599 swizzles
[3] = vcc
->variant
->sampler_view_swizzles
[sampler_index
].a
;
602 swizzles
[0] = PIPE_SWIZZLE_RED
;
603 swizzles
[1] = PIPE_SWIZZLE_GREEN
;
604 swizzles
[2] = PIPE_SWIZZLE_BLUE
;
605 swizzles
[3] = PIPE_SWIZZLE_ALPHA
;
608 swizzle_zero_mask
= 0;
609 swizzle_one_mask
= 0;
610 swizzle_normal_mask
= 0;
611 for (i
= 0; i
< 4; i
++) {
612 switch (swizzles
[i
]) {
613 case PIPE_SWIZZLE_ZERO
:
614 swizzle_zero_mask
|= 1 << i
;
617 case PIPE_SWIZZLE_ONE
:
618 swizzle_one_mask
|= 1 << i
;
622 swizzle_normal_mask
|= 1 << i
;
627 /* swizzle the results */
628 if (swizzle_normal_mask
) {
629 tc_MOV(tc
, tdst_writemask(dst
, swizzle_normal_mask
),
630 tsrc_swizzle(tsrc_from(tmp
), swizzles
[0],
631 swizzles
[1], swizzles
[2], swizzles
[3]));
633 if (swizzle_zero_mask
)
634 tc_MOV(tc
, tdst_writemask(dst
, swizzle_zero_mask
), tsrc_imm_f(0.0f
));
635 if (swizzle_one_mask
)
636 tc_MOV(tc
, tdst_writemask(dst
, swizzle_one_mask
), tsrc_imm_f(1.0f
));
640 vs_lower_opcode_urb_write(struct toy_compiler
*tc
, struct toy_inst
*inst
)
642 /* vs_write_vue() has set up the message registers */
643 toy_compiler_lower_to_send(tc
, inst
, false, BRW_SFID_URB
);
647 vs_lower_virtual_opcodes(struct vs_compile_context
*vcc
)
649 struct toy_compiler
*tc
= &vcc
->tc
;
650 struct toy_inst
*inst
;
653 while ((inst
= tc_next(tc
)) != NULL
) {
654 switch (inst
->opcode
) {
655 case TOY_OPCODE_TGSI_IN
:
656 case TOY_OPCODE_TGSI_CONST
:
657 case TOY_OPCODE_TGSI_SV
:
658 case TOY_OPCODE_TGSI_IMM
:
659 vs_lower_opcode_tgsi_direct(vcc
, inst
);
661 case TOY_OPCODE_TGSI_INDIRECT_FETCH
:
662 case TOY_OPCODE_TGSI_INDIRECT_STORE
:
663 vs_lower_opcode_tgsi_indirect(vcc
, inst
);
665 case TOY_OPCODE_TGSI_TEX
:
666 case TOY_OPCODE_TGSI_TXB
:
667 case TOY_OPCODE_TGSI_TXD
:
668 case TOY_OPCODE_TGSI_TXL
:
669 case TOY_OPCODE_TGSI_TXP
:
670 case TOY_OPCODE_TGSI_TXF
:
671 case TOY_OPCODE_TGSI_TXQ
:
672 case TOY_OPCODE_TGSI_TXQ_LZ
:
673 case TOY_OPCODE_TGSI_TEX2
:
674 case TOY_OPCODE_TGSI_TXB2
:
675 case TOY_OPCODE_TGSI_TXL2
:
676 case TOY_OPCODE_TGSI_SAMPLE
:
677 case TOY_OPCODE_TGSI_SAMPLE_I
:
678 case TOY_OPCODE_TGSI_SAMPLE_I_MS
:
679 case TOY_OPCODE_TGSI_SAMPLE_B
:
680 case TOY_OPCODE_TGSI_SAMPLE_C
:
681 case TOY_OPCODE_TGSI_SAMPLE_C_LZ
:
682 case TOY_OPCODE_TGSI_SAMPLE_D
:
683 case TOY_OPCODE_TGSI_SAMPLE_L
:
684 case TOY_OPCODE_TGSI_GATHER4
:
685 case TOY_OPCODE_TGSI_SVIEWINFO
:
686 case TOY_OPCODE_TGSI_SAMPLE_POS
:
687 case TOY_OPCODE_TGSI_SAMPLE_INFO
:
688 vs_lower_opcode_tgsi_sampling(vcc
, inst
);
693 case TOY_OPCODE_SQRT
:
697 case TOY_OPCODE_FDIV
:
699 case TOY_OPCODE_INT_DIV_QUOTIENT
:
700 case TOY_OPCODE_INT_DIV_REMAINDER
:
701 toy_compiler_lower_math(tc
, inst
);
703 case TOY_OPCODE_URB_WRITE
:
704 vs_lower_opcode_urb_write(tc
, inst
);
707 if (inst
->opcode
> 127)
708 tc_fail(tc
, "unhandled virtual opcode");
715 * Compile the shader.
718 vs_compile(struct vs_compile_context
*vcc
)
720 struct toy_compiler
*tc
= &vcc
->tc
;
721 struct ilo_shader
*sh
= vcc
->shader
;
723 vs_lower_virtual_opcodes(vcc
);
724 toy_compiler_legalize_for_ra(tc
);
725 toy_compiler_optimize(tc
);
726 toy_compiler_allocate_registers(tc
,
729 vcc
->num_grf_per_vrf
);
730 toy_compiler_legalize_for_asm(tc
);
733 ilo_err("failed to legalize VS instructions: %s\n", tc
->reason
);
737 if (ilo_debug
& ILO_DEBUG_VS
) {
738 ilo_printf("legalized instructions:\n");
739 toy_compiler_dump(tc
);
744 sh
->kernel
= toy_compiler_assemble(tc
, &sh
->kernel_size
);
747 static const uint32_t microcode
[] = {
748 /* fill in the microcode here */
751 const bool swap
= true;
753 sh
->kernel_size
= sizeof(microcode
);
754 sh
->kernel
= MALLOC(sh
->kernel_size
);
757 const int num_dwords
= sizeof(microcode
) / 4;
758 const uint32_t *src
= microcode
;
759 uint32_t *dst
= (uint32_t *) sh
->kernel
;
762 for (i
= 0; i
< num_dwords
; i
+= 4) {
764 dst
[i
+ 0] = src
[i
+ 3];
765 dst
[i
+ 1] = src
[i
+ 2];
766 dst
[i
+ 2] = src
[i
+ 1];
767 dst
[i
+ 3] = src
[i
+ 0];
770 memcpy(dst
, src
, 16);
777 ilo_err("failed to compile VS: %s\n", tc
->reason
);
781 if (ilo_debug
& ILO_DEBUG_VS
) {
782 ilo_printf("disassembly:\n");
783 toy_compiler_disassemble(tc
, sh
->kernel
, sh
->kernel_size
);
791 * Collect the toy registers to be written to the VUE.
794 vs_collect_outputs(struct vs_compile_context
*vcc
, struct toy_src
*outs
)
796 const struct toy_tgsi
*tgsi
= &vcc
->tgsi
;
799 for (i
= 0; i
< vcc
->shader
->out
.count
; i
++) {
800 const int slot
= vcc
->output_map
[i
];
801 const int vrf
= (slot
>= 0) ? toy_tgsi_get_vrf(tgsi
,
802 TGSI_FILE_OUTPUT
, 0, tgsi
->outputs
[slot
].index
) : -1;
808 dst
= tdst(TOY_FILE_VRF
, vrf
, 0);
809 src
= tsrc_from(dst
);
812 /* PSIZE is at channel W */
813 tc_MOV(&vcc
->tc
, tdst_writemask(dst
, TOY_WRITEMASK_W
),
814 tsrc_swizzle1(src
, TOY_SWIZZLE_X
));
816 /* the other channels are for the header */
818 tc_MOV(&vcc
->tc
, tdst_writemask(dst
, TOY_WRITEMASK_XYZ
),
822 /* initialize unused channels to 0.0f */
823 if (tgsi
->outputs
[slot
].undefined_mask
) {
824 dst
= tdst_writemask(dst
, tgsi
->outputs
[slot
].undefined_mask
);
825 tc_MOV(&vcc
->tc
, dst
, tsrc_imm_f(0.0f
));
830 /* XXX this is too ugly */
831 if (vcc
->shader
->out
.semantic_names
[i
] == TGSI_SEMANTIC_CLIPDIST
&&
833 /* ok, we need to compute clip distance */
834 int clipvert_slot
= -1, clipvert_vrf
, j
;
836 for (j
= 0; j
< tgsi
->num_outputs
; j
++) {
837 if (tgsi
->outputs
[j
].semantic_name
==
838 TGSI_SEMANTIC_CLIPVERTEX
) {
842 else if (tgsi
->outputs
[j
].semantic_name
==
843 TGSI_SEMANTIC_POSITION
) {
844 /* remember pos, but keep looking */
849 clipvert_vrf
= (clipvert_slot
>= 0) ? toy_tgsi_get_vrf(tgsi
,
850 TGSI_FILE_OUTPUT
, 0, tgsi
->outputs
[clipvert_slot
].index
) : -1;
851 if (clipvert_vrf
>= 0) {
852 struct toy_dst tmp
= tc_alloc_tmp(&vcc
->tc
);
853 struct toy_src clipvert
= tsrc(TOY_FILE_VRF
, clipvert_vrf
, 0);
854 int first_ucp
, last_ucp
;
856 if (vcc
->shader
->out
.semantic_indices
[i
]) {
858 last_ucp
= MIN2(7, vcc
->variant
->u
.vs
.num_ucps
- 1);
862 last_ucp
= MIN2(3, vcc
->variant
->u
.vs
.num_ucps
- 1);
865 for (j
= first_ucp
; j
<= last_ucp
; j
++) {
866 const int plane_grf
= vcc
->first_ucp_grf
+ j
/ 2;
867 const int plane_subreg
= (j
& 1) * 16;
868 const struct toy_src plane
= tsrc_rect(tsrc(TOY_FILE_GRF
,
869 plane_grf
, plane_subreg
), TOY_RECT_041
);
870 const unsigned writemask
= 1 << ((j
>= 4) ? j
- 4 : j
);
872 tc_DP4(&vcc
->tc
, tdst_writemask(tmp
, writemask
),
876 src
= tsrc_from(tmp
);
879 src
= tsrc_imm_f(0.0f
);
883 src
= (i
== 0) ? tsrc_imm_d(0) : tsrc_imm_f(0.0f
);
894 * Emit instructions to write the VUE.
897 vs_write_vue(struct vs_compile_context
*vcc
)
899 struct toy_compiler
*tc
= &vcc
->tc
;
900 struct toy_src outs
[PIPE_MAX_SHADER_OUTPUTS
];
901 struct toy_dst header
;
903 struct toy_inst
*inst
;
904 int sent_attrs
, total_attrs
;
906 header
= tdst_ud(tdst(TOY_FILE_MRF
, vcc
->first_free_mrf
, 0));
907 r0
= tsrc_ud(tsrc(TOY_FILE_GRF
, 0, 0));
908 inst
= tc_MOV(tc
, header
, r0
);
909 inst
->mask_ctrl
= BRW_MASK_DISABLE
;
911 if (tc
->dev
->gen
>= ILO_GEN(7)) {
912 inst
= tc_OR(tc
, tdst_offset(header
, 0, 5),
913 tsrc_rect(tsrc_offset(r0
, 0, 5), TOY_RECT_010
),
914 tsrc_rect(tsrc_imm_ud(0xff00), TOY_RECT_010
));
915 inst
->exec_size
= BRW_EXECUTE_1
;
916 inst
->access_mode
= BRW_ALIGN_1
;
917 inst
->mask_ctrl
= BRW_MASK_DISABLE
;
920 total_attrs
= vs_collect_outputs(vcc
, outs
);
922 while (sent_attrs
< total_attrs
) {
924 int mrf
= vcc
->first_free_mrf
+ 1, avail_mrf_for_attrs
;
925 int num_attrs
, msg_len
, i
;
928 num_attrs
= total_attrs
- sent_attrs
;
931 /* see if we need another message */
932 avail_mrf_for_attrs
= vcc
->last_free_mrf
- mrf
+ 1;
933 if (num_attrs
> avail_mrf_for_attrs
) {
935 * From the Sandy Bridge PRM, volume 4 part 2, page 22:
937 * "Offset. This field specifies a destination offset (in 256-bit
938 * units) from the start of the URB entry(s), as referenced by
939 * URB Return Handle n, at which the data (if any) will be
942 * As we need to offset the following messages, we must make sure
943 * this one writes an even number of attributes.
945 num_attrs
= avail_mrf_for_attrs
& ~1;
949 if (tc
->dev
->gen
>= ILO_GEN(7)) {
950 /* do not forget about the header */
951 msg_len
= 1 + num_attrs
;
955 * From the Sandy Bridge PRM, volume 4 part 2, page 26:
957 * "At least 256 bits per vertex (512 bits total, M1 & M2) must
958 * be written. Writing only 128 bits per vertex (256 bits
959 * total, M1 only) results in UNDEFINED operation."
961 * "[DevSNB] Interleave writes must be in multiples of 256 per
964 * That is, we must write or appear to write an even number of
965 * attributes, starting from two.
967 if (num_attrs
% 2 && num_attrs
== avail_mrf_for_attrs
) {
972 msg_len
= 1 + align(num_attrs
, 2);
975 for (i
= 0; i
< num_attrs
; i
++)
976 tc_MOV(tc
, tdst(TOY_FILE_MRF
, mrf
++, 0), outs
[sent_attrs
+ i
]);
978 assert(sent_attrs
% 2 == 0);
979 desc
= tsrc_imm_mdesc_urb(tc
, eot
, msg_len
, 0,
980 eot
, true, false, BRW_URB_SWIZZLE_INTERLEAVE
, sent_attrs
/ 2, 0);
982 tc_add2(tc
, TOY_OPCODE_URB_WRITE
, tdst_null(), tsrc_from(header
), desc
);
984 sent_attrs
+= num_attrs
;
989 * Set up shader inputs for fixed-function units.
992 vs_setup_shader_in(struct ilo_shader
*sh
, const struct toy_tgsi
*tgsi
)
996 /* vertex/instance id is the first VE if exists */
997 for (i
= 0; i
< tgsi
->num_system_values
; i
++) {
1000 switch (tgsi
->system_values
[i
].semantic_name
) {
1001 case TGSI_SEMANTIC_INSTANCEID
:
1002 case TGSI_SEMANTIC_VERTEXID
:
1010 sh
->in
.semantic_names
[sh
->in
.count
] =
1011 tgsi
->system_values
[i
].semantic_name
;
1012 sh
->in
.semantic_indices
[sh
->in
.count
] =
1013 tgsi
->system_values
[i
].semantic_index
;
1014 sh
->in
.interp
[sh
->in
.count
] = TGSI_INTERPOLATE_CONSTANT
;
1015 sh
->in
.centroid
[sh
->in
.count
] = false;
1023 for (i
= 0; i
< tgsi
->num_inputs
; i
++) {
1024 assert(tgsi
->inputs
[i
].semantic_name
== TGSI_SEMANTIC_GENERIC
);
1025 if (tgsi
->inputs
[i
].semantic_index
>= num_attrs
)
1026 num_attrs
= tgsi
->inputs
[i
].semantic_index
+ 1;
1028 assert(num_attrs
<= PIPE_MAX_ATTRIBS
);
1030 /* VF cannot remap VEs. VE[i] must be used as GENERIC[i]. */
1031 for (i
= 0; i
< num_attrs
; i
++) {
1032 sh
->in
.semantic_names
[sh
->in
.count
+ i
] = TGSI_SEMANTIC_GENERIC
;
1033 sh
->in
.semantic_indices
[sh
->in
.count
+ i
] = i
;
1034 sh
->in
.interp
[sh
->in
.count
+ i
] = TGSI_INTERPOLATE_CONSTANT
;
1035 sh
->in
.centroid
[sh
->in
.count
+ i
] = false;
1038 sh
->in
.count
+= num_attrs
;
1040 sh
->in
.has_pos
= false;
1041 sh
->in
.has_linear_interp
= false;
1042 sh
->in
.barycentric_interpolation_mode
= 0;
1046 * Set up shader outputs for fixed-function units.
1049 vs_setup_shader_out(struct ilo_shader
*sh
, const struct toy_tgsi
*tgsi
,
1050 bool output_clipdist
, int *output_map
)
1052 int psize_slot
= -1, pos_slot
= -1;
1053 int clipdist_slot
[2] = { -1, -1 };
1054 int color_slot
[4] = { -1, -1, -1, -1 };
1057 /* find out the slots of outputs that need special care */
1058 for (i
= 0; i
< tgsi
->num_outputs
; i
++) {
1059 switch (tgsi
->outputs
[i
].semantic_name
) {
1060 case TGSI_SEMANTIC_PSIZE
:
1063 case TGSI_SEMANTIC_POSITION
:
1066 case TGSI_SEMANTIC_CLIPDIST
:
1067 if (tgsi
->outputs
[i
].semantic_index
)
1068 clipdist_slot
[1] = i
;
1070 clipdist_slot
[0] = i
;
1072 case TGSI_SEMANTIC_COLOR
:
1073 if (tgsi
->outputs
[i
].semantic_index
)
1078 case TGSI_SEMANTIC_BCOLOR
:
1079 if (tgsi
->outputs
[i
].semantic_index
)
1089 /* the first two VUEs are always PSIZE and POSITION */
1091 output_map
[0] = psize_slot
;
1092 output_map
[1] = pos_slot
;
1094 sh
->out
.register_indices
[0] =
1095 (psize_slot
>= 0) ? tgsi
->outputs
[psize_slot
].index
: -1;
1096 sh
->out
.semantic_names
[0] = TGSI_SEMANTIC_PSIZE
;
1097 sh
->out
.semantic_indices
[0] = 0;
1099 sh
->out
.register_indices
[1] =
1100 (pos_slot
>= 0) ? tgsi
->outputs
[pos_slot
].index
: -1;
1101 sh
->out
.semantic_names
[1] = TGSI_SEMANTIC_POSITION
;
1102 sh
->out
.semantic_indices
[1] = 0;
1104 sh
->out
.has_pos
= true;
1106 /* followed by optional clip distances */
1107 if (output_clipdist
) {
1108 sh
->out
.register_indices
[num_outs
] =
1109 (clipdist_slot
[0] >= 0) ? tgsi
->outputs
[clipdist_slot
[0]].index
: -1;
1110 sh
->out
.semantic_names
[num_outs
] = TGSI_SEMANTIC_CLIPDIST
;
1111 sh
->out
.semantic_indices
[num_outs
] = 0;
1112 output_map
[num_outs
++] = clipdist_slot
[0];
1114 sh
->out
.register_indices
[num_outs
] =
1115 (clipdist_slot
[1] >= 0) ? tgsi
->outputs
[clipdist_slot
[1]].index
: -1;
1116 sh
->out
.semantic_names
[num_outs
] = TGSI_SEMANTIC_CLIPDIST
;
1117 sh
->out
.semantic_indices
[num_outs
] = 1;
1118 output_map
[num_outs
++] = clipdist_slot
[1];
1122 * make BCOLOR follow COLOR so that we can make use of
1123 * ATTRIBUTE_SWIZZLE_INPUTATTR_FACING in 3DSTATE_SF
1125 for (i
= 0; i
< 4; i
++) {
1126 const int slot
= color_slot
[i
];
1131 sh
->out
.register_indices
[num_outs
] = tgsi
->outputs
[slot
].index
;
1132 sh
->out
.semantic_names
[num_outs
] = tgsi
->outputs
[slot
].semantic_name
;
1133 sh
->out
.semantic_indices
[num_outs
] = tgsi
->outputs
[slot
].semantic_index
;
1135 output_map
[num_outs
++] = slot
;
1138 /* add the rest of the outputs */
1139 for (i
= 0; i
< tgsi
->num_outputs
; i
++) {
1140 switch (tgsi
->outputs
[i
].semantic_name
) {
1141 case TGSI_SEMANTIC_PSIZE
:
1142 case TGSI_SEMANTIC_POSITION
:
1143 case TGSI_SEMANTIC_CLIPDIST
:
1144 case TGSI_SEMANTIC_COLOR
:
1145 case TGSI_SEMANTIC_BCOLOR
:
1148 sh
->out
.register_indices
[num_outs
] = tgsi
->outputs
[i
].index
;
1149 sh
->out
.semantic_names
[num_outs
] = tgsi
->outputs
[i
].semantic_name
;
1150 sh
->out
.semantic_indices
[num_outs
] = tgsi
->outputs
[i
].semantic_index
;
1151 output_map
[num_outs
++] = i
;
1156 sh
->out
.count
= num_outs
;
1160 * Translate the TGSI tokens.
1163 vs_setup_tgsi(struct toy_compiler
*tc
, const struct tgsi_token
*tokens
,
1164 struct toy_tgsi
*tgsi
)
1166 if (ilo_debug
& ILO_DEBUG_VS
) {
1167 ilo_printf("dumping vertex shader\n");
1170 tgsi_dump(tokens
, 0);
1174 toy_compiler_translate_tgsi(tc
, tokens
, true, tgsi
);
1176 ilo_err("failed to translate VS TGSI tokens: %s\n", tc
->reason
);
1180 if (ilo_debug
& ILO_DEBUG_VS
) {
1181 ilo_printf("TGSI translator:\n");
1182 toy_tgsi_dump(tgsi
);
1184 toy_compiler_dump(tc
);
1192 * Set up VS compile context. This includes translating the TGSI tokens.
1195 vs_setup(struct vs_compile_context
*vcc
,
1196 const struct ilo_shader_state
*state
,
1197 const struct ilo_shader_variant
*variant
)
1201 memset(vcc
, 0, sizeof(*vcc
));
1203 vcc
->shader
= CALLOC_STRUCT(ilo_shader
);
1207 vcc
->variant
= variant
;
1209 toy_compiler_init(&vcc
->tc
, state
->info
.dev
);
1210 vcc
->tc
.templ
.access_mode
= BRW_ALIGN_16
;
1211 vcc
->tc
.templ
.exec_size
= BRW_EXECUTE_8
;
1212 vcc
->tc
.rect_linear_width
= 4;
1215 * The classic driver uses the sampler cache (gen6) or the data cache
1218 vcc
->const_cache
= GEN6_SFID_DATAPORT_CONSTANT_CACHE
;
1220 if (!vs_setup_tgsi(&vcc
->tc
, state
->info
.tokens
, &vcc
->tgsi
)) {
1221 toy_compiler_cleanup(&vcc
->tc
);
1226 vs_setup_shader_in(vcc
->shader
, &vcc
->tgsi
);
1227 vs_setup_shader_out(vcc
->shader
, &vcc
->tgsi
,
1228 (vcc
->variant
->u
.vs
.num_ucps
> 0), vcc
->output_map
);
1230 if (vcc
->variant
->use_pcb
&& !vcc
->tgsi
.const_indirect
) {
1231 num_consts
= (vcc
->tgsi
.const_count
+ 1) / 2;
1234 * From the Sandy Bridge PRM, volume 2 part 1, page 138:
1236 * "The sum of all four read length fields (each incremented to
1237 * represent the actual read length) must be less than or equal to
1240 if (num_consts
> 32)
1247 vcc
->shader
->skip_cbuf0_upload
= (!vcc
->tgsi
.const_count
|| num_consts
);
1248 vcc
->shader
->pcb
.cbuf0_size
= num_consts
* (sizeof(float) * 8);
1250 /* r0 is reserved for payload header */
1251 vcc
->first_const_grf
= 1;
1252 vcc
->first_ucp_grf
= vcc
->first_const_grf
+ num_consts
;
1254 /* fit each pair of user clip planes into a register */
1255 vcc
->first_vue_grf
= vcc
->first_ucp_grf
+
1256 (vcc
->variant
->u
.vs
.num_ucps
+ 1) / 2;
1258 vcc
->first_free_grf
= vcc
->first_vue_grf
+ vcc
->shader
->in
.count
;
1259 vcc
->last_free_grf
= 127;
1261 /* m0 is reserved for system routines */
1262 vcc
->first_free_mrf
= 1;
1263 vcc
->last_free_mrf
= 15;
1265 vcc
->num_grf_per_vrf
= 1;
1267 if (vcc
->tc
.dev
->gen
>= ILO_GEN(7)) {
1268 vcc
->last_free_grf
-= 15;
1269 vcc
->first_free_mrf
= vcc
->last_free_grf
+ 1;
1270 vcc
->last_free_mrf
= vcc
->first_free_mrf
+ 14;
1273 vcc
->shader
->in
.start_grf
= vcc
->first_const_grf
;
1274 vcc
->shader
->pcb
.clip_state_size
=
1275 vcc
->variant
->u
.vs
.num_ucps
* (sizeof(float) * 4);
1281 * Compile the vertex shader.
1284 ilo_shader_compile_vs(const struct ilo_shader_state
*state
,
1285 const struct ilo_shader_variant
*variant
)
1287 struct vs_compile_context vcc
;
1290 if (!vs_setup(&vcc
, state
, variant
))
1293 if (vcc
.tc
.dev
->gen
>= ILO_GEN(7)) {
1297 need_gs
= variant
->u
.vs
.rasterizer_discard
||
1298 state
->info
.stream_output
.num_outputs
;
1303 if (!vs_compile(&vcc
)) {
1308 toy_tgsi_cleanup(&vcc
.tgsi
);
1309 toy_compiler_cleanup(&vcc
.tc
);
1312 int so_mapping
[PIPE_MAX_SHADER_OUTPUTS
];
1315 for (i
= 0; i
< vcc
.tgsi
.num_outputs
; i
++) {
1318 for (j
= 0; j
< vcc
.shader
->out
.count
; j
++) {
1319 if (vcc
.tgsi
.outputs
[i
].semantic_name
==
1320 vcc
.shader
->out
.semantic_names
[j
] &&
1321 vcc
.tgsi
.outputs
[i
].semantic_index
==
1322 vcc
.shader
->out
.semantic_indices
[j
]) {
1328 so_mapping
[i
] = attr
;
1331 if (!ilo_shader_compile_gs_passthrough(state
, variant
,
1332 so_mapping
, vcc
.shader
)) {
1333 ilo_shader_destroy_kernel(vcc
.shader
);