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
];
59 vs_lower_opcode_tgsi_in(struct vs_compile_context
*vcc
,
60 struct toy_dst dst
, int dim
, int idx
)
62 struct toy_compiler
*tc
= &vcc
->tc
;
67 slot
= toy_tgsi_find_input(&vcc
->tgsi
, idx
);
69 const int first_in_grf
= vcc
->first_vue_grf
+
70 (vcc
->shader
->in
.count
- vcc
->tgsi
.num_inputs
);
71 const int grf
= first_in_grf
+ vcc
->tgsi
.inputs
[slot
].semantic_index
;
72 const struct toy_src src
= tsrc(TOY_FILE_GRF
, grf
, 0);
77 /* undeclared input */
78 tc_MOV(tc
, dst
, tsrc_imm_f(0.0f
));
83 vs_lower_opcode_tgsi_const_gen6(struct vs_compile_context
*vcc
,
84 struct toy_dst dst
, int dim
,
87 const struct toy_dst header
=
88 tdst_ud(tdst(TOY_FILE_MRF
, vcc
->first_free_mrf
, 0));
89 const struct toy_dst block_offsets
=
90 tdst_ud(tdst(TOY_FILE_MRF
, vcc
->first_free_mrf
+ 1, 0));
91 const struct toy_src r0
= tsrc_ud(tsrc(TOY_FILE_GRF
, 0, 0));
92 struct toy_compiler
*tc
= &vcc
->tc
;
93 unsigned msg_type
, msg_ctrl
, msg_len
;
94 struct toy_inst
*inst
;
97 /* set message header */
98 inst
= tc_MOV(tc
, header
, r0
);
99 inst
->mask_ctrl
= BRW_MASK_DISABLE
;
101 /* set block offsets */
102 tc_MOV(tc
, block_offsets
, idx
);
104 msg_type
= GEN6_DATAPORT_READ_MESSAGE_OWORD_DUAL_BLOCK_READ
;
105 msg_ctrl
= BRW_DATAPORT_OWORD_DUAL_BLOCK_1OWORD
<< 8;;
108 desc
= tsrc_imm_mdesc_data_port(tc
, false, msg_len
, 1, true, false,
109 msg_type
, msg_ctrl
, ILO_VS_CONST_SURFACE(dim
));
111 tc_SEND(tc
, dst
, tsrc_from(header
), desc
, vcc
->const_cache
);
115 vs_lower_opcode_tgsi_const_gen7(struct vs_compile_context
*vcc
,
116 struct toy_dst dst
, int dim
,
119 struct toy_compiler
*tc
= &vcc
->tc
;
120 const struct toy_dst offset
=
121 tdst_ud(tdst(TOY_FILE_MRF
, vcc
->first_free_mrf
, 0));
125 * In 259b65e2e7938de4aab323033cfe2b33369ddb07, pull constant load was
126 * changed from OWord Dual Block Read to ld to increase performance in the
127 * classic driver. Since we use the constant cache instead of the data
128 * cache, I wonder if we still want to follow the classic driver.
132 tc_MOV(tc
, offset
, idx
);
134 desc
= tsrc_imm_mdesc_sampler(tc
, 1, 1, false,
135 BRW_SAMPLER_SIMD_MODE_SIMD4X2
,
136 GEN5_SAMPLER_MESSAGE_SAMPLE_LD
,
138 ILO_VS_CONST_SURFACE(dim
));
140 tc_SEND(tc
, dst
, tsrc_from(offset
), desc
, BRW_SFID_SAMPLER
);
144 vs_lower_opcode_tgsi_imm(struct vs_compile_context
*vcc
,
145 struct toy_dst dst
, int idx
)
150 imm
= toy_tgsi_get_imm(&vcc
->tgsi
, idx
, NULL
);
152 for (ch
= 0; ch
< 4; ch
++) {
155 tdst_writemask(tdst_ud(dst
), 1 << ch
),
156 tsrc_imm_ud(imm
[ch
]));
162 vs_lower_opcode_tgsi_sv(struct vs_compile_context
*vcc
,
163 struct toy_dst dst
, int dim
, int idx
)
165 struct toy_compiler
*tc
= &vcc
->tc
;
166 const struct toy_tgsi
*tgsi
= &vcc
->tgsi
;
171 slot
= toy_tgsi_find_system_value(tgsi
, idx
);
175 switch (tgsi
->system_values
[slot
].semantic_name
) {
176 case TGSI_SEMANTIC_INSTANCEID
:
177 case TGSI_SEMANTIC_VERTEXID
:
179 * In 3DSTATE_VERTEX_ELEMENTS, we prepend an extra vertex element for
180 * the generated IDs, with VID in the X channel and IID in the Y
184 const int grf
= vcc
->first_vue_grf
;
185 const struct toy_src src
= tsrc(TOY_FILE_GRF
, grf
, 0);
186 const enum toy_swizzle swizzle
=
187 (tgsi
->system_values
[slot
].semantic_name
==
188 TGSI_SEMANTIC_INSTANCEID
) ? TOY_SWIZZLE_Y
: TOY_SWIZZLE_X
;
190 tc_MOV(tc
, tdst_d(dst
), tsrc_d(tsrc_swizzle1(src
, swizzle
)));
193 case TGSI_SEMANTIC_PRIMID
:
195 tc_fail(tc
, "unhandled system value");
196 tc_MOV(tc
, dst
, tsrc_imm_d(0));
202 vs_lower_opcode_tgsi_direct(struct vs_compile_context
*vcc
,
203 struct toy_inst
*inst
)
205 struct toy_compiler
*tc
= &vcc
->tc
;
208 assert(inst
->src
[0].file
== TOY_FILE_IMM
);
209 dim
= inst
->src
[0].val32
;
211 assert(inst
->src
[1].file
== TOY_FILE_IMM
);
212 idx
= inst
->src
[1].val32
;
214 switch (inst
->opcode
) {
215 case TOY_OPCODE_TGSI_IN
:
216 vs_lower_opcode_tgsi_in(vcc
, inst
->dst
, dim
, idx
);
218 case TOY_OPCODE_TGSI_CONST
:
219 if (tc
->dev
->gen
>= ILO_GEN(7))
220 vs_lower_opcode_tgsi_const_gen7(vcc
, inst
->dst
, dim
, inst
->src
[1]);
222 vs_lower_opcode_tgsi_const_gen6(vcc
, inst
->dst
, dim
, inst
->src
[1]);
224 case TOY_OPCODE_TGSI_SV
:
225 vs_lower_opcode_tgsi_sv(vcc
, inst
->dst
, dim
, idx
);
227 case TOY_OPCODE_TGSI_IMM
:
229 vs_lower_opcode_tgsi_imm(vcc
, inst
->dst
, idx
);
232 tc_fail(tc
, "unhandled TGSI fetch");
236 tc_discard_inst(tc
, inst
);
240 vs_lower_opcode_tgsi_indirect(struct vs_compile_context
*vcc
,
241 struct toy_inst
*inst
)
243 struct toy_compiler
*tc
= &vcc
->tc
;
244 enum tgsi_file_type file
;
246 struct toy_src indirect_dim
, indirect_idx
;
248 assert(inst
->src
[0].file
== TOY_FILE_IMM
);
249 file
= inst
->src
[0].val32
;
251 assert(inst
->src
[1].file
== TOY_FILE_IMM
);
252 dim
= inst
->src
[1].val32
;
253 indirect_dim
= inst
->src
[2];
255 assert(inst
->src
[3].file
== TOY_FILE_IMM
);
256 idx
= inst
->src
[3].val32
;
257 indirect_idx
= inst
->src
[4];
259 /* no dimension indirection */
260 assert(indirect_dim
.file
== TOY_FILE_IMM
);
261 dim
+= indirect_dim
.val32
;
263 switch (inst
->opcode
) {
264 case TOY_OPCODE_TGSI_INDIRECT_FETCH
:
265 if (file
== TGSI_FILE_CONSTANT
) {
267 struct toy_dst tmp
= tc_alloc_tmp(tc
);
269 tc_ADD(tc
, tmp
, indirect_idx
, tsrc_imm_d(idx
));
270 indirect_idx
= tsrc_from(tmp
);
273 if (tc
->dev
->gen
>= ILO_GEN(7))
274 vs_lower_opcode_tgsi_const_gen7(vcc
, inst
->dst
, dim
, indirect_idx
);
276 vs_lower_opcode_tgsi_const_gen6(vcc
, inst
->dst
, dim
, indirect_idx
);
280 case TOY_OPCODE_TGSI_INDIRECT_STORE
:
282 tc_fail(tc
, "unhandled TGSI indirection");
286 tc_discard_inst(tc
, inst
);
290 * Emit instructions to move sampling parameters to the message registers.
293 vs_add_sampler_params(struct toy_compiler
*tc
, int msg_type
, int base_mrf
,
294 struct toy_src coords
, int num_coords
,
295 struct toy_src bias_or_lod
, struct toy_src ref_or_si
,
296 struct toy_src ddx
, struct toy_src ddy
, int num_derivs
)
298 const unsigned coords_writemask
= (1 << num_coords
) - 1;
302 assert(num_coords
<= 4);
303 assert(num_derivs
<= 3 && num_derivs
<= num_coords
);
305 for (i
= 0; i
< Elements(m
); i
++)
306 m
[i
] = tdst(TOY_FILE_MRF
, base_mrf
+ i
, 0);
309 case GEN5_SAMPLER_MESSAGE_SAMPLE_LOD
:
310 tc_MOV(tc
, tdst_writemask(m
[0], coords_writemask
), coords
);
311 tc_MOV(tc
, tdst_writemask(m
[1], TOY_WRITEMASK_X
), bias_or_lod
);
314 case GEN5_SAMPLER_MESSAGE_SAMPLE_DERIVS
:
315 tc_MOV(tc
, tdst_writemask(m
[0], coords_writemask
), coords
);
316 tc_MOV(tc
, tdst_writemask(m
[1], TOY_WRITEMASK_XZ
),
317 tsrc_swizzle(ddx
, 0, 0, 1, 1));
318 tc_MOV(tc
, tdst_writemask(m
[1], TOY_WRITEMASK_YW
),
319 tsrc_swizzle(ddy
, 0, 0, 1, 1));
320 if (num_derivs
> 2) {
321 tc_MOV(tc
, tdst_writemask(m
[2], TOY_WRITEMASK_X
),
322 tsrc_swizzle1(ddx
, 2));
323 tc_MOV(tc
, tdst_writemask(m
[2], TOY_WRITEMASK_Y
),
324 tsrc_swizzle1(ddy
, 2));
326 num_params
= 4 + num_derivs
* 2;
328 case GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE
:
329 tc_MOV(tc
, tdst_writemask(m
[0], coords_writemask
), coords
);
330 tc_MOV(tc
, tdst_writemask(m
[1], TOY_WRITEMASK_X
), ref_or_si
);
331 tc_MOV(tc
, tdst_writemask(m
[1], TOY_WRITEMASK_Y
), bias_or_lod
);
334 case GEN5_SAMPLER_MESSAGE_SAMPLE_LD
:
335 assert(num_coords
<= 3);
336 tc_MOV(tc
, tdst_writemask(tdst_d(m
[0]), coords_writemask
), coords
);
337 tc_MOV(tc
, tdst_writemask(tdst_d(m
[0]), TOY_WRITEMASK_W
), bias_or_lod
);
338 if (tc
->dev
->gen
>= ILO_GEN(7)) {
342 tc_MOV(tc
, tdst_writemask(tdst_d(m
[1]), TOY_WRITEMASK_X
), ref_or_si
);
346 case GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO
:
347 tc_MOV(tc
, tdst_writemask(tdst_d(m
[0]), TOY_WRITEMASK_X
), bias_or_lod
);
351 tc_fail(tc
, "unknown sampler opcode");
356 return (num_params
+ 3) / 4;
360 * Set up message registers and return the message descriptor for sampling.
362 static struct toy_src
363 vs_prepare_tgsi_sampling(struct toy_compiler
*tc
, const struct toy_inst
*inst
,
364 int base_mrf
, unsigned *ret_sampler_index
)
366 unsigned simd_mode
, msg_type
, msg_len
, sampler_index
, binding_table_index
;
367 struct toy_src coords
, ddx
, ddy
, bias_or_lod
, ref_or_si
;
368 int num_coords
, ref_pos
, num_derivs
;
371 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD4X2
;
373 coords
= inst
->src
[0];
376 bias_or_lod
= tsrc_null();
377 ref_or_si
= tsrc_null();
381 num_coords
= tgsi_util_get_texture_coord_dim(inst
->tex
.target
, &ref_pos
);
383 /* extract the parameters */
384 switch (inst
->opcode
) {
385 case TOY_OPCODE_TGSI_TXD
:
387 tc_fail(tc
, "TXD with shadow sampler not supported");
389 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_DERIVS
;
392 num_derivs
= num_coords
;
395 case TOY_OPCODE_TGSI_TXL
:
399 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE
;
400 ref_or_si
= tsrc_swizzle1(coords
, ref_pos
);
403 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LOD
;
406 bias_or_lod
= tsrc_swizzle1(coords
, TOY_SWIZZLE_W
);
408 case TOY_OPCODE_TGSI_TXF
:
409 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LD
;
411 switch (inst
->tex
.target
) {
412 case TGSI_TEXTURE_2D_MSAA
:
413 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
414 assert(ref_pos
>= 0 && ref_pos
< 4);
415 /* lod is always 0 */
416 bias_or_lod
= tsrc_imm_d(0);
417 ref_or_si
= tsrc_swizzle1(coords
, ref_pos
);
420 bias_or_lod
= tsrc_swizzle1(coords
, TOY_SWIZZLE_W
);
424 /* offset the coordinates */
425 if (!tsrc_is_null(inst
->tex
.offsets
[0])) {
428 tmp
= tc_alloc_tmp(tc
);
429 tc_ADD(tc
, tmp
, coords
, inst
->tex
.offsets
[0]);
430 coords
= tsrc_from(tmp
);
435 case TOY_OPCODE_TGSI_TXQ
:
436 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO
;
438 bias_or_lod
= tsrc_swizzle1(coords
, TOY_SWIZZLE_X
);
440 case TOY_OPCODE_TGSI_TXQ_LZ
:
441 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO
;
445 case TOY_OPCODE_TGSI_TXL2
:
449 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE
;
450 ref_or_si
= tsrc_swizzle1(coords
, ref_pos
);
453 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LOD
;
456 bias_or_lod
= tsrc_swizzle1(inst
->src
[1], TOY_SWIZZLE_X
);
460 assert(!"unhandled sampling opcode");
461 if (ret_sampler_index
)
462 *ret_sampler_index
= 0;
467 assert(inst
->src
[sampler_src
].file
== TOY_FILE_IMM
);
468 sampler_index
= inst
->src
[sampler_src
].val32
;
469 binding_table_index
= ILO_VS_TEXTURE_SURFACE(sampler_index
);
472 * From the Sandy Bridge PRM, volume 4 part 1, page 18:
474 * "Note that the (cube map) coordinates delivered to the sampling
475 * engine must already have been divided by the component with the
476 * largest absolute value."
478 switch (inst
->tex
.target
) {
479 case TGSI_TEXTURE_CUBE
:
480 case TGSI_TEXTURE_SHADOWCUBE
:
481 case TGSI_TEXTURE_CUBE_ARRAY
:
482 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
483 /* TXQ does not need coordinates */
484 if (num_coords
>= 3) {
485 struct toy_dst tmp
, max
;
486 struct toy_src abs_coords
[3];
489 tmp
= tc_alloc_tmp(tc
);
490 max
= tdst_writemask(tmp
, TOY_WRITEMASK_W
);
492 for (i
= 0; i
< 3; i
++)
493 abs_coords
[i
] = tsrc_absolute(tsrc_swizzle1(coords
, i
));
495 tc_SEL(tc
, max
, abs_coords
[0], abs_coords
[0], BRW_CONDITIONAL_GE
);
496 tc_SEL(tc
, max
, tsrc_from(max
), abs_coords
[0], BRW_CONDITIONAL_GE
);
497 tc_INV(tc
, max
, tsrc_from(max
));
499 for (i
= 0; i
< 3; i
++)
500 tc_MUL(tc
, tdst_writemask(tmp
, 1 << i
), coords
, tsrc_from(max
));
502 coords
= tsrc_from(tmp
);
507 /* set up sampler parameters */
508 msg_len
= vs_add_sampler_params(tc
, msg_type
, base_mrf
,
509 coords
, num_coords
, bias_or_lod
, ref_or_si
, ddx
, ddy
, num_derivs
);
512 * From the Sandy Bridge PRM, volume 4 part 1, page 136:
514 * "The maximum message length allowed to the sampler is 11. This would
515 * disallow sample_d, sample_b_c, and sample_l_c with a SIMD Mode of
519 tc_fail(tc
, "maximum length for messages to the sampler is 11");
521 if (ret_sampler_index
)
522 *ret_sampler_index
= sampler_index
;
524 return tsrc_imm_mdesc_sampler(tc
, msg_len
, 1,
525 false, simd_mode
, msg_type
, sampler_index
, binding_table_index
);
529 vs_lower_opcode_tgsi_sampling(struct vs_compile_context
*vcc
,
530 struct toy_inst
*inst
)
532 struct toy_compiler
*tc
= &vcc
->tc
;
534 struct toy_dst dst
, tmp
;
535 unsigned sampler_index
;
537 unsigned swizzle_zero_mask
, swizzle_one_mask
, swizzle_normal_mask
;
540 desc
= vs_prepare_tgsi_sampling(tc
, inst
,
541 vcc
->first_free_mrf
, &sampler_index
);
543 switch (inst
->opcode
) {
544 case TOY_OPCODE_TGSI_TXF
:
545 case TOY_OPCODE_TGSI_TXQ
:
546 case TOY_OPCODE_TGSI_TXQ_LZ
:
554 toy_compiler_lower_to_send(tc
, inst
, false, BRW_SFID_SAMPLER
);
555 inst
->src
[0] = tsrc(TOY_FILE_MRF
, vcc
->first_free_mrf
, 0);
558 /* write to a temp first */
559 tmp
= tc_alloc_tmp(tc
);
560 tmp
.type
= inst
->dst
.type
;
564 tc_move_inst(tc
, inst
);
567 assert(sampler_index
< vcc
->variant
->num_sampler_views
);
568 swizzles
[0] = vcc
->variant
->sampler_view_swizzles
[sampler_index
].r
;
569 swizzles
[1] = vcc
->variant
->sampler_view_swizzles
[sampler_index
].g
;
570 swizzles
[2] = vcc
->variant
->sampler_view_swizzles
[sampler_index
].b
;
571 swizzles
[3] = vcc
->variant
->sampler_view_swizzles
[sampler_index
].a
;
574 swizzles
[0] = PIPE_SWIZZLE_RED
;
575 swizzles
[1] = PIPE_SWIZZLE_GREEN
;
576 swizzles
[2] = PIPE_SWIZZLE_BLUE
;
577 swizzles
[3] = PIPE_SWIZZLE_ALPHA
;
580 swizzle_zero_mask
= 0;
581 swizzle_one_mask
= 0;
582 swizzle_normal_mask
= 0;
583 for (i
= 0; i
< 4; i
++) {
584 switch (swizzles
[i
]) {
585 case PIPE_SWIZZLE_ZERO
:
586 swizzle_zero_mask
|= 1 << i
;
589 case PIPE_SWIZZLE_ONE
:
590 swizzle_one_mask
|= 1 << i
;
594 swizzle_normal_mask
|= 1 << i
;
599 /* swizzle the results */
600 if (swizzle_normal_mask
) {
601 tc_MOV(tc
, tdst_writemask(dst
, swizzle_normal_mask
),
602 tsrc_swizzle(tsrc_from(tmp
), swizzles
[0],
603 swizzles
[1], swizzles
[2], swizzles
[3]));
605 if (swizzle_zero_mask
)
606 tc_MOV(tc
, tdst_writemask(dst
, swizzle_zero_mask
), tsrc_imm_f(0.0f
));
607 if (swizzle_one_mask
)
608 tc_MOV(tc
, tdst_writemask(dst
, swizzle_one_mask
), tsrc_imm_f(1.0f
));
612 vs_lower_opcode_urb_write(struct toy_compiler
*tc
, struct toy_inst
*inst
)
614 /* vs_write_vue() has set up the message registers */
615 toy_compiler_lower_to_send(tc
, inst
, false, BRW_SFID_URB
);
619 vs_lower_virtual_opcodes(struct vs_compile_context
*vcc
)
621 struct toy_compiler
*tc
= &vcc
->tc
;
622 struct toy_inst
*inst
;
625 while ((inst
= tc_next(tc
)) != NULL
) {
626 switch (inst
->opcode
) {
627 case TOY_OPCODE_TGSI_IN
:
628 case TOY_OPCODE_TGSI_CONST
:
629 case TOY_OPCODE_TGSI_SV
:
630 case TOY_OPCODE_TGSI_IMM
:
631 vs_lower_opcode_tgsi_direct(vcc
, inst
);
633 case TOY_OPCODE_TGSI_INDIRECT_FETCH
:
634 case TOY_OPCODE_TGSI_INDIRECT_STORE
:
635 vs_lower_opcode_tgsi_indirect(vcc
, inst
);
637 case TOY_OPCODE_TGSI_TEX
:
638 case TOY_OPCODE_TGSI_TXB
:
639 case TOY_OPCODE_TGSI_TXD
:
640 case TOY_OPCODE_TGSI_TXL
:
641 case TOY_OPCODE_TGSI_TXP
:
642 case TOY_OPCODE_TGSI_TXF
:
643 case TOY_OPCODE_TGSI_TXQ
:
644 case TOY_OPCODE_TGSI_TXQ_LZ
:
645 case TOY_OPCODE_TGSI_TEX2
:
646 case TOY_OPCODE_TGSI_TXB2
:
647 case TOY_OPCODE_TGSI_TXL2
:
648 case TOY_OPCODE_TGSI_SAMPLE
:
649 case TOY_OPCODE_TGSI_SAMPLE_I
:
650 case TOY_OPCODE_TGSI_SAMPLE_I_MS
:
651 case TOY_OPCODE_TGSI_SAMPLE_B
:
652 case TOY_OPCODE_TGSI_SAMPLE_C
:
653 case TOY_OPCODE_TGSI_SAMPLE_C_LZ
:
654 case TOY_OPCODE_TGSI_SAMPLE_D
:
655 case TOY_OPCODE_TGSI_SAMPLE_L
:
656 case TOY_OPCODE_TGSI_GATHER4
:
657 case TOY_OPCODE_TGSI_SVIEWINFO
:
658 case TOY_OPCODE_TGSI_SAMPLE_POS
:
659 case TOY_OPCODE_TGSI_SAMPLE_INFO
:
660 vs_lower_opcode_tgsi_sampling(vcc
, inst
);
665 case TOY_OPCODE_SQRT
:
669 case TOY_OPCODE_FDIV
:
671 case TOY_OPCODE_INT_DIV_QUOTIENT
:
672 case TOY_OPCODE_INT_DIV_REMAINDER
:
673 toy_compiler_lower_math(tc
, inst
);
675 case TOY_OPCODE_URB_WRITE
:
676 vs_lower_opcode_urb_write(tc
, inst
);
679 if (inst
->opcode
> 127)
680 tc_fail(tc
, "unhandled virtual opcode");
687 * Compile the shader.
690 vs_compile(struct vs_compile_context
*vcc
)
692 struct toy_compiler
*tc
= &vcc
->tc
;
693 struct ilo_shader
*sh
= vcc
->shader
;
695 vs_lower_virtual_opcodes(vcc
);
696 toy_compiler_legalize_for_ra(tc
);
697 toy_compiler_optimize(tc
);
698 toy_compiler_allocate_registers(tc
,
701 vcc
->num_grf_per_vrf
);
702 toy_compiler_legalize_for_asm(tc
);
705 ilo_err("failed to legalize VS instructions: %s\n", tc
->reason
);
709 if (ilo_debug
& ILO_DEBUG_VS
) {
710 ilo_printf("legalized instructions:\n");
711 toy_compiler_dump(tc
);
716 sh
->kernel
= toy_compiler_assemble(tc
, &sh
->kernel_size
);
719 static const uint32_t microcode
[] = {
720 /* fill in the microcode here */
723 const bool swap
= true;
725 sh
->kernel_size
= sizeof(microcode
);
726 sh
->kernel
= MALLOC(sh
->kernel_size
);
729 const int num_dwords
= sizeof(microcode
) / 4;
730 const uint32_t *src
= microcode
;
731 uint32_t *dst
= (uint32_t *) sh
->kernel
;
734 for (i
= 0; i
< num_dwords
; i
+= 4) {
736 dst
[i
+ 0] = src
[i
+ 3];
737 dst
[i
+ 1] = src
[i
+ 2];
738 dst
[i
+ 2] = src
[i
+ 1];
739 dst
[i
+ 3] = src
[i
+ 0];
742 memcpy(dst
, src
, 16);
749 ilo_err("failed to compile VS: %s\n", tc
->reason
);
753 if (ilo_debug
& ILO_DEBUG_VS
) {
754 ilo_printf("disassembly:\n");
755 toy_compiler_disassemble(tc
, sh
->kernel
, sh
->kernel_size
);
763 * Collect the toy registers to be written to the VUE.
766 vs_collect_outputs(struct vs_compile_context
*vcc
, struct toy_src
*outs
)
768 const struct toy_tgsi
*tgsi
= &vcc
->tgsi
;
771 for (i
= 0; i
< vcc
->shader
->out
.count
; i
++) {
772 const int slot
= vcc
->output_map
[i
];
773 const int vrf
= (slot
>= 0) ? toy_tgsi_get_vrf(tgsi
,
774 TGSI_FILE_OUTPUT
, 0, tgsi
->outputs
[slot
].index
) : -1;
780 dst
= tdst(TOY_FILE_VRF
, vrf
, 0);
781 src
= tsrc_from(dst
);
784 /* PSIZE is at channel W */
785 tc_MOV(&vcc
->tc
, tdst_writemask(dst
, TOY_WRITEMASK_W
),
786 tsrc_swizzle1(src
, TOY_SWIZZLE_X
));
788 /* the other channels are for the header */
790 tc_MOV(&vcc
->tc
, tdst_writemask(dst
, TOY_WRITEMASK_XYZ
),
794 /* initialize unused channels to 0.0f */
795 if (tgsi
->outputs
[slot
].undefined_mask
) {
796 dst
= tdst_writemask(dst
, tgsi
->outputs
[slot
].undefined_mask
);
797 tc_MOV(&vcc
->tc
, dst
, tsrc_imm_f(0.0f
));
802 /* XXX this is too ugly */
803 if (vcc
->shader
->out
.semantic_names
[i
] == TGSI_SEMANTIC_CLIPDIST
&&
805 /* ok, we need to compute clip distance */
806 int clipvert_slot
= -1, clipvert_vrf
, j
;
808 for (j
= 0; j
< tgsi
->num_outputs
; j
++) {
809 if (tgsi
->outputs
[j
].semantic_name
==
810 TGSI_SEMANTIC_CLIPVERTEX
) {
814 else if (tgsi
->outputs
[j
].semantic_name
==
815 TGSI_SEMANTIC_POSITION
) {
816 /* remember pos, but keep looking */
821 clipvert_vrf
= (clipvert_slot
>= 0) ? toy_tgsi_get_vrf(tgsi
,
822 TGSI_FILE_OUTPUT
, 0, tgsi
->outputs
[clipvert_slot
].index
) : -1;
823 if (clipvert_vrf
>= 0) {
824 struct toy_dst tmp
= tc_alloc_tmp(&vcc
->tc
);
825 struct toy_src clipvert
= tsrc(TOY_FILE_VRF
, clipvert_vrf
, 0);
826 int first_ucp
, last_ucp
;
828 if (vcc
->shader
->out
.semantic_indices
[i
]) {
830 last_ucp
= MIN2(7, vcc
->variant
->u
.vs
.num_ucps
- 1);
834 last_ucp
= MIN2(3, vcc
->variant
->u
.vs
.num_ucps
- 1);
837 for (j
= first_ucp
; j
<= last_ucp
; j
++) {
838 const int plane_grf
= vcc
->first_const_grf
+ j
/ 2;
839 const int plane_subreg
= (j
& 1) * 16;
840 const struct toy_src plane
= tsrc_rect(tsrc(TOY_FILE_GRF
,
841 plane_grf
, plane_subreg
), TOY_RECT_041
);
842 const unsigned writemask
= 1 << ((j
>= 4) ? j
- 4 : j
);
844 tc_DP4(&vcc
->tc
, tdst_writemask(tmp
, writemask
),
848 src
= tsrc_from(tmp
);
851 src
= tsrc_imm_f(0.0f
);
855 src
= (i
== 0) ? tsrc_imm_d(0) : tsrc_imm_f(0.0f
);
866 * Emit instructions to write the VUE.
869 vs_write_vue(struct vs_compile_context
*vcc
)
871 struct toy_compiler
*tc
= &vcc
->tc
;
872 struct toy_src outs
[PIPE_MAX_SHADER_OUTPUTS
];
873 struct toy_dst header
;
875 struct toy_inst
*inst
;
876 int sent_attrs
, total_attrs
;
878 header
= tdst_ud(tdst(TOY_FILE_MRF
, vcc
->first_free_mrf
, 0));
879 r0
= tsrc_ud(tsrc(TOY_FILE_GRF
, 0, 0));
880 inst
= tc_MOV(tc
, header
, r0
);
881 inst
->mask_ctrl
= BRW_MASK_DISABLE
;
883 if (tc
->dev
->gen
>= ILO_GEN(7)) {
884 inst
= tc_OR(tc
, tdst_offset(header
, 0, 5),
885 tsrc_rect(tsrc_offset(r0
, 0, 5), TOY_RECT_010
),
886 tsrc_rect(tsrc_imm_ud(0xff00), TOY_RECT_010
));
887 inst
->exec_size
= BRW_EXECUTE_1
;
888 inst
->access_mode
= BRW_ALIGN_1
;
889 inst
->mask_ctrl
= BRW_MASK_DISABLE
;
892 total_attrs
= vs_collect_outputs(vcc
, outs
);
894 while (sent_attrs
< total_attrs
) {
896 int mrf
= vcc
->first_free_mrf
+ 1, avail_mrf_for_attrs
;
897 int num_attrs
, msg_len
, i
;
900 num_attrs
= total_attrs
- sent_attrs
;
903 /* see if we need another message */
904 avail_mrf_for_attrs
= vcc
->last_free_mrf
- mrf
+ 1;
905 if (num_attrs
> avail_mrf_for_attrs
) {
907 * From the Sandy Bridge PRM, volume 4 part 2, page 22:
909 * "Offset. This field specifies a destination offset (in 256-bit
910 * units) from the start of the URB entry(s), as referenced by
911 * URB Return Handle n, at which the data (if any) will be
914 * As we need to offset the following messages, we must make sure
915 * this one writes an even number of attributes.
917 num_attrs
= avail_mrf_for_attrs
& ~1;
921 if (tc
->dev
->gen
>= ILO_GEN(7)) {
922 /* do not forget about the header */
923 msg_len
= 1 + num_attrs
;
927 * From the Sandy Bridge PRM, volume 4 part 2, page 26:
929 * "At least 256 bits per vertex (512 bits total, M1 & M2) must
930 * be written. Writing only 128 bits per vertex (256 bits
931 * total, M1 only) results in UNDEFINED operation."
933 * "[DevSNB] Interleave writes must be in multiples of 256 per
936 * That is, we must write or appear to write an even number of
937 * attributes, starting from two.
939 if (num_attrs
% 2 && num_attrs
== avail_mrf_for_attrs
) {
944 msg_len
= 1 + align(num_attrs
, 2);
947 for (i
= 0; i
< num_attrs
; i
++)
948 tc_MOV(tc
, tdst(TOY_FILE_MRF
, mrf
++, 0), outs
[sent_attrs
+ i
]);
950 assert(sent_attrs
% 2 == 0);
951 desc
= tsrc_imm_mdesc_urb(tc
, eot
, msg_len
, 0,
952 eot
, true, false, BRW_URB_SWIZZLE_INTERLEAVE
, sent_attrs
/ 2, 0);
954 tc_add2(tc
, TOY_OPCODE_URB_WRITE
, tdst_null(), tsrc_from(header
), desc
);
956 sent_attrs
+= num_attrs
;
961 * Set up shader inputs for fixed-function units.
964 vs_setup_shader_in(struct ilo_shader
*sh
, const struct toy_tgsi
*tgsi
)
968 /* vertex/instance id is the first VE if exists */
969 for (i
= 0; i
< tgsi
->num_system_values
; i
++) {
972 switch (tgsi
->system_values
[i
].semantic_name
) {
973 case TGSI_SEMANTIC_INSTANCEID
:
974 case TGSI_SEMANTIC_VERTEXID
:
982 sh
->in
.semantic_names
[sh
->in
.count
] =
983 tgsi
->system_values
[i
].semantic_name
;
984 sh
->in
.semantic_indices
[sh
->in
.count
] =
985 tgsi
->system_values
[i
].semantic_index
;
986 sh
->in
.interp
[sh
->in
.count
] = TGSI_INTERPOLATE_CONSTANT
;
987 sh
->in
.centroid
[sh
->in
.count
] = false;
995 for (i
= 0; i
< tgsi
->num_inputs
; i
++) {
996 assert(tgsi
->inputs
[i
].semantic_name
== TGSI_SEMANTIC_GENERIC
);
997 if (tgsi
->inputs
[i
].semantic_index
>= num_attrs
)
998 num_attrs
= tgsi
->inputs
[i
].semantic_index
+ 1;
1000 assert(num_attrs
<= PIPE_MAX_ATTRIBS
);
1002 /* VF cannot remap VEs. VE[i] must be used as GENERIC[i]. */
1003 for (i
= 0; i
< num_attrs
; i
++) {
1004 sh
->in
.semantic_names
[sh
->in
.count
+ i
] = TGSI_SEMANTIC_GENERIC
;
1005 sh
->in
.semantic_indices
[sh
->in
.count
+ i
] = i
;
1006 sh
->in
.interp
[sh
->in
.count
+ i
] = TGSI_INTERPOLATE_CONSTANT
;
1007 sh
->in
.centroid
[sh
->in
.count
+ i
] = false;
1010 sh
->in
.count
+= num_attrs
;
1012 sh
->in
.has_pos
= false;
1013 sh
->in
.has_linear_interp
= false;
1014 sh
->in
.barycentric_interpolation_mode
= 0;
1018 * Set up shader outputs for fixed-function units.
1021 vs_setup_shader_out(struct ilo_shader
*sh
, const struct toy_tgsi
*tgsi
,
1022 bool output_clipdist
, int *output_map
)
1024 int psize_slot
= -1, pos_slot
= -1;
1025 int clipdist_slot
[2] = { -1, -1 };
1026 int color_slot
[4] = { -1, -1, -1, -1 };
1029 /* find out the slots of outputs that need special care */
1030 for (i
= 0; i
< tgsi
->num_outputs
; i
++) {
1031 switch (tgsi
->outputs
[i
].semantic_name
) {
1032 case TGSI_SEMANTIC_PSIZE
:
1035 case TGSI_SEMANTIC_POSITION
:
1038 case TGSI_SEMANTIC_CLIPDIST
:
1039 if (tgsi
->outputs
[i
].semantic_index
)
1040 clipdist_slot
[1] = i
;
1042 clipdist_slot
[0] = i
;
1044 case TGSI_SEMANTIC_COLOR
:
1045 if (tgsi
->outputs
[i
].semantic_index
)
1050 case TGSI_SEMANTIC_BCOLOR
:
1051 if (tgsi
->outputs
[i
].semantic_index
)
1061 /* the first two VUEs are always PSIZE and POSITION */
1063 output_map
[0] = psize_slot
;
1064 output_map
[1] = pos_slot
;
1066 sh
->out
.register_indices
[0] =
1067 (psize_slot
>= 0) ? tgsi
->outputs
[psize_slot
].index
: -1;
1068 sh
->out
.semantic_names
[0] = TGSI_SEMANTIC_PSIZE
;
1069 sh
->out
.semantic_indices
[0] = 0;
1071 sh
->out
.register_indices
[1] =
1072 (pos_slot
>= 0) ? tgsi
->outputs
[pos_slot
].index
: -1;
1073 sh
->out
.semantic_names
[1] = TGSI_SEMANTIC_POSITION
;
1074 sh
->out
.semantic_indices
[1] = 0;
1076 sh
->out
.has_pos
= true;
1078 /* followed by optional clip distances */
1079 if (output_clipdist
) {
1080 sh
->out
.register_indices
[num_outs
] =
1081 (clipdist_slot
[0] >= 0) ? tgsi
->outputs
[clipdist_slot
[0]].index
: -1;
1082 sh
->out
.semantic_names
[num_outs
] = TGSI_SEMANTIC_CLIPDIST
;
1083 sh
->out
.semantic_indices
[num_outs
] = 0;
1084 output_map
[num_outs
++] = clipdist_slot
[0];
1086 sh
->out
.register_indices
[num_outs
] =
1087 (clipdist_slot
[1] >= 0) ? tgsi
->outputs
[clipdist_slot
[1]].index
: -1;
1088 sh
->out
.semantic_names
[num_outs
] = TGSI_SEMANTIC_CLIPDIST
;
1089 sh
->out
.semantic_indices
[num_outs
] = 1;
1090 output_map
[num_outs
++] = clipdist_slot
[1];
1094 * make BCOLOR follow COLOR so that we can make use of
1095 * ATTRIBUTE_SWIZZLE_INPUTATTR_FACING in 3DSTATE_SF
1097 for (i
= 0; i
< 4; i
++) {
1098 const int slot
= color_slot
[i
];
1103 sh
->out
.register_indices
[num_outs
] = tgsi
->outputs
[slot
].index
;
1104 sh
->out
.semantic_names
[num_outs
] = tgsi
->outputs
[slot
].semantic_name
;
1105 sh
->out
.semantic_indices
[num_outs
] = tgsi
->outputs
[slot
].semantic_index
;
1107 output_map
[num_outs
++] = slot
;
1110 /* add the rest of the outputs */
1111 for (i
= 0; i
< tgsi
->num_outputs
; i
++) {
1112 switch (tgsi
->outputs
[i
].semantic_name
) {
1113 case TGSI_SEMANTIC_PSIZE
:
1114 case TGSI_SEMANTIC_POSITION
:
1115 case TGSI_SEMANTIC_CLIPDIST
:
1116 case TGSI_SEMANTIC_COLOR
:
1117 case TGSI_SEMANTIC_BCOLOR
:
1120 sh
->out
.register_indices
[num_outs
] = tgsi
->outputs
[i
].index
;
1121 sh
->out
.semantic_names
[num_outs
] = tgsi
->outputs
[i
].semantic_name
;
1122 sh
->out
.semantic_indices
[num_outs
] = tgsi
->outputs
[i
].semantic_index
;
1123 output_map
[num_outs
++] = i
;
1128 sh
->out
.count
= num_outs
;
1132 * Translate the TGSI tokens.
1135 vs_setup_tgsi(struct toy_compiler
*tc
, const struct tgsi_token
*tokens
,
1136 struct toy_tgsi
*tgsi
)
1138 if (ilo_debug
& ILO_DEBUG_VS
) {
1139 ilo_printf("dumping vertex shader\n");
1142 tgsi_dump(tokens
, 0);
1146 toy_compiler_translate_tgsi(tc
, tokens
, true, tgsi
);
1148 ilo_err("failed to translate VS TGSI tokens: %s\n", tc
->reason
);
1152 if (ilo_debug
& ILO_DEBUG_VS
) {
1153 ilo_printf("TGSI translator:\n");
1154 toy_tgsi_dump(tgsi
);
1156 toy_compiler_dump(tc
);
1164 * Set up VS compile context. This includes translating the TGSI tokens.
1167 vs_setup(struct vs_compile_context
*vcc
,
1168 const struct ilo_shader_state
*state
,
1169 const struct ilo_shader_variant
*variant
)
1173 memset(vcc
, 0, sizeof(*vcc
));
1175 vcc
->shader
= CALLOC_STRUCT(ilo_shader
);
1179 vcc
->variant
= variant
;
1181 toy_compiler_init(&vcc
->tc
, state
->info
.dev
);
1182 vcc
->tc
.templ
.access_mode
= BRW_ALIGN_16
;
1183 vcc
->tc
.templ
.exec_size
= BRW_EXECUTE_8
;
1184 vcc
->tc
.rect_linear_width
= 4;
1187 * The classic driver uses the sampler cache (gen6) or the data cache
1190 vcc
->const_cache
= GEN6_SFID_DATAPORT_CONSTANT_CACHE
;
1192 if (!vs_setup_tgsi(&vcc
->tc
, state
->info
.tokens
, &vcc
->tgsi
)) {
1193 toy_compiler_cleanup(&vcc
->tc
);
1198 vs_setup_shader_in(vcc
->shader
, &vcc
->tgsi
);
1199 vs_setup_shader_out(vcc
->shader
, &vcc
->tgsi
,
1200 (vcc
->variant
->u
.vs
.num_ucps
> 0), vcc
->output_map
);
1202 /* fit each pair of user clip planes into a register */
1203 num_consts
= (vcc
->variant
->u
.vs
.num_ucps
+ 1) / 2;
1205 /* r0 is reserved for payload header */
1206 vcc
->first_const_grf
= 1;
1207 vcc
->first_vue_grf
= vcc
->first_const_grf
+ num_consts
;
1208 vcc
->first_free_grf
= vcc
->first_vue_grf
+ vcc
->shader
->in
.count
;
1209 vcc
->last_free_grf
= 127;
1211 /* m0 is reserved for system routines */
1212 vcc
->first_free_mrf
= 1;
1213 vcc
->last_free_mrf
= 15;
1215 vcc
->num_grf_per_vrf
= 1;
1217 if (vcc
->tc
.dev
->gen
>= ILO_GEN(7)) {
1218 vcc
->last_free_grf
-= 15;
1219 vcc
->first_free_mrf
= vcc
->last_free_grf
+ 1;
1220 vcc
->last_free_mrf
= vcc
->first_free_mrf
+ 14;
1223 vcc
->shader
->in
.start_grf
= vcc
->first_const_grf
;
1224 vcc
->shader
->pcb
.clip_state_size
=
1225 vcc
->variant
->u
.vs
.num_ucps
* (sizeof(float) * 4);
1231 * Compile the vertex shader.
1234 ilo_shader_compile_vs(const struct ilo_shader_state
*state
,
1235 const struct ilo_shader_variant
*variant
)
1237 struct vs_compile_context vcc
;
1240 if (!vs_setup(&vcc
, state
, variant
))
1243 if (vcc
.tc
.dev
->gen
>= ILO_GEN(7)) {
1247 need_gs
= variant
->u
.vs
.rasterizer_discard
||
1248 state
->info
.stream_output
.num_outputs
;
1253 if (!vs_compile(&vcc
)) {
1258 toy_tgsi_cleanup(&vcc
.tgsi
);
1259 toy_compiler_cleanup(&vcc
.tc
);
1262 int so_mapping
[PIPE_MAX_SHADER_OUTPUTS
];
1265 for (i
= 0; i
< vcc
.tgsi
.num_outputs
; i
++) {
1268 for (j
= 0; j
< vcc
.shader
->out
.count
; j
++) {
1269 if (vcc
.tgsi
.outputs
[i
].semantic_name
==
1270 vcc
.shader
->out
.semantic_names
[j
] &&
1271 vcc
.tgsi
.outputs
[i
].semantic_index
==
1272 vcc
.shader
->out
.semantic_indices
[j
]) {
1278 so_mapping
[i
] = attr
;
1281 if (!ilo_shader_compile_gs_passthrough(state
, variant
,
1282 so_mapping
, vcc
.shader
)) {
1283 ilo_shader_destroy(vcc
.shader
);