Debian Codegraph @

src/gallium/drivers/svga/svga_tgsi_vgpu10.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1998-2025 Broadcom. All Rights Reserved.
3 * The term “Broadcom” refers to Broadcom Inc.
4 * and/or its subsidiaries.
5 * SPDX-License-Identifier: MIT
6 */
7
8 /**
9 * @file svga_tgsi_vgpu10.c
10 *
11 * TGSI -> VGPU10 shader translation.
12 *
13 * \author Mingcheng Chen
14 * \author Brian Paul
15 */
16
17 #include "util/compiler.h"
18 #include "pipe/p_shader_tokens.h"
19 #include "pipe/p_defines.h"
20 #include "tgsi/tgsi_dump.h"
21 #include "tgsi/tgsi_info.h"
22 #include "tgsi/tgsi_parse.h"
23 #include "tgsi/tgsi_scan.h"
24 #include "tgsi/tgsi_strings.h"
25 #include "tgsi/tgsi_two_side.h"
26 #include "tgsi/tgsi_aa_point.h"
27 #include "tgsi/tgsi_util.h"
28 #include "util/u_math.h"
29 #include "util/u_memory.h"
30 #include "util/u_bitmask.h"
31 #include "util/u_debug.h"
32 #include "util/u_pstipple.h"
33
34 #include "svga_context.h"
35 #include "svga_debug.h"
36 #include "svga_link.h"
37 #include "svga_shader.h"
38 #include "svga_tgsi.h"
39
40 #include "VGPU10ShaderTokens.h"
41
42
43 #define INVALID_INDEX 99999
44 #define MAX_INTERNAL_TEMPS 4
45 #define MAX_SYSTEM_VALUES 4
46 #define MAX_IMMEDIATE_COUNT \
47 (VGPU10_MAX_IMMEDIATE_CONSTANT_BUFFER_ELEMENT_COUNT/4)
48 #define MAX_TEMP_ARRAYS 64 /* Enough? */
49
50 /**
51 * Clipping is complicated. There's four different cases which we
52 * handle during VS/GS shader translation:
53 */
54 enum clipping_mode
55 {
56 CLIP_NONE, /**< No clipping enabled */
57 CLIP_LEGACY, /**< The shader has no clipping declarations or code but
58 * one or more user-defined clip planes are enabled. We
59 * generate extra code to emit clip distances.
60 */
61 CLIP_DISTANCE, /**< The shader already declares clip distance output
62 * registers and has code to write to them.
63 */
64 CLIP_VERTEX /**< The shader declares a clip vertex output register and
65 * has code that writes to the register. We convert the
66 * clipvertex position into one or more clip distances.
67 */
68 };
69
70
71 /* Shader signature info */
72 struct svga_shader_signature
73 {
74 SVGA3dDXShaderSignatureHeader header;
75 SVGA3dDXShaderSignatureEntry inputs[PIPE_MAX_SHADER_INPUTS];
76 SVGA3dDXShaderSignatureEntry outputs[PIPE_MAX_SHADER_OUTPUTS];
77 SVGA3dDXShaderSignatureEntry patchConstants[PIPE_MAX_SHADER_OUTPUTS];
78 };
79
80 static inline void
81 set_shader_signature_entry(SVGA3dDXShaderSignatureEntry *e,
82 unsigned index,
83 SVGA3dDXSignatureSemanticName sgnName,
84 unsigned mask,
85 SVGA3dDXSignatureRegisterComponentType compType,
86 SVGA3dDXSignatureMinPrecision minPrecision)
87 {
88 e->registerIndex = index;
89 e->semanticName = sgnName;
90 e->mask = mask;
91 e->componentType = compType;
92 e->minPrecision = minPrecision;
93 };
94
95 static const SVGA3dDXSignatureSemanticName
96 tgsi_semantic_to_sgn_name[TGSI_SEMANTIC_COUNT] = {
97 SVGADX_SIGNATURE_SEMANTIC_NAME_POSITION,
98 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
99 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
100 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
101 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
102 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
103 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
104 SVGADX_SIGNATURE_SEMANTIC_NAME_IS_FRONT_FACE,
105 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
106 SVGADX_SIGNATURE_SEMANTIC_NAME_PRIMITIVE_ID,
107 SVGADX_SIGNATURE_SEMANTIC_NAME_INSTANCE_ID,
108 SVGADX_SIGNATURE_SEMANTIC_NAME_VERTEX_ID,
109 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
110 SVGADX_SIGNATURE_SEMANTIC_NAME_CLIP_DISTANCE,
111 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
112 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
113 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
114 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
115 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
116 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
117 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
118 SVGADX_SIGNATURE_SEMANTIC_NAME_VIEWPORT_ARRAY_INDEX,
119 SVGADX_SIGNATURE_SEMANTIC_NAME_RENDER_TARGET_ARRAY_INDEX,
120 SVGADX_SIGNATURE_SEMANTIC_NAME_SAMPLE_INDEX,
121 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
122 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
123 SVGADX_SIGNATURE_SEMANTIC_NAME_INSTANCE_ID,
124 SVGADX_SIGNATURE_SEMANTIC_NAME_VERTEX_ID,
125 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
126 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
127 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
128 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
129 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
130 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
131 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
132 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
133 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
134 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
135 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
136 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
137 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
138 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
139 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
140 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED,
141 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED
142 };
143
144
145 /**
146 * Map tgsi semantic name to SVGA signature semantic name
147 */
148 static inline SVGA3dDXSignatureSemanticName
149 map_tgsi_semantic_to_sgn_name(enum tgsi_semantic name)
150 {
151 assert(name < TGSI_SEMANTIC_COUNT);
152
153 /* Do a few asserts here to spot check the mapping */
154 assert(tgsi_semantic_to_sgn_name[TGSI_SEMANTIC_PRIMID] ==
155 SVGADX_SIGNATURE_SEMANTIC_NAME_PRIMITIVE_ID);
156 assert(tgsi_semantic_to_sgn_name[TGSI_SEMANTIC_VIEWPORT_INDEX] ==
157 SVGADX_SIGNATURE_SEMANTIC_NAME_VIEWPORT_ARRAY_INDEX);
158 assert(tgsi_semantic_to_sgn_name[TGSI_SEMANTIC_INVOCATIONID] ==
159 SVGADX_SIGNATURE_SEMANTIC_NAME_INSTANCE_ID);
160
161 return tgsi_semantic_to_sgn_name[name];
162 }
163
164 enum reemit_mode {
165 REEMIT_FALSE = 0,
166 REEMIT_TRUE = 1,
167 REEMIT_IN_PROGRESS = 2
168 };
169
170 struct svga_raw_buf_tmp {
171 bool indirect;
172 unsigned buffer_index:8;
173 unsigned element_index:8;
174 unsigned element_rel:8;
175 };
176
177 struct svga_shader_emitter_v10
178 {
179 /* The token output buffer */
180 unsigned size;
181 char *buf;
182 char *ptr;
183
184 /* Information about the shader and state (does not change) */
185 struct svga_compile_key key;
186 struct tgsi_shader_info info;
187 unsigned unit;
188 unsigned version; /**< Either 40, 41, 50 or 51 at this time */
189
190 unsigned cur_tgsi_token; /**< current tgsi token position */
191 unsigned inst_start_token;
192 bool discard_instruction; /**< throw away current instruction? */
193 bool reemit_instruction; /**< reemit current instruction */
194 bool reemit_tgsi_instruction; /**< reemit current tgsi instruction */
195 bool skip_instruction; /**< skip current instruction */
196 bool use_sampler_state_mapping; /* use sampler state mapping */
197 enum reemit_mode reemit_rawbuf_instruction;
198
199 union tgsi_immediate_data immediates[MAX_IMMEDIATE_COUNT][4];
200 double (*immediates_dbl)[2];
201 unsigned num_immediates; /**< Number of immediates emitted */
202 unsigned common_immediate_pos[20]; /**< literals for common immediates */
203 unsigned num_common_immediates;
204 unsigned num_immediates_emitted;
205 unsigned num_new_immediates; /** pending immediates to be declared */
206 unsigned immediates_block_start_token;
207 unsigned immediates_block_next_token;
208
209 unsigned num_outputs; /**< include any extra outputs */
210 /** The first extra output is reserved for
211 * non-adjusted vertex position for
212 * stream output purpose
213 */
214
215 /* Temporary Registers */
216 unsigned num_shader_temps; /**< num of temps used by original shader */
217 unsigned internal_temp_count; /**< currently allocated internal temps */
218 struct {
219 unsigned start, size;
220 } temp_arrays[MAX_TEMP_ARRAYS];
221 unsigned num_temp_arrays;
222
223 /** Map TGSI temp registers to VGPU10 temp array IDs and indexes */
224 struct {
225 unsigned arrayId, index;
226 bool initialized;
227 } temp_map[VGPU10_MAX_TEMPS]; /**< arrayId, element */
228
229 unsigned initialize_temp_index;
230
231 /** Number of constants used by original shader for each constant buffer.
232 * The size should probably always match with that of svga_state.constbufs.
233 */
234 unsigned num_shader_consts[SVGA_MAX_CONST_BUFS];
235
236 /* Raw constant buffers */
237 unsigned raw_buf_srv_start_index; /* starting srv index for raw buffers */
238 unsigned raw_bufs; /* raw buffers bitmask */
239 unsigned raw_buf_tmp_index; /* starting temp index for raw buffers */
240 unsigned raw_buf_cur_tmp_index; /* current temp index for raw buffers */
241 struct svga_raw_buf_tmp raw_buf_tmp[3]; /* temporaries for raw buf source */
242
243 /* Samplers */
244 unsigned num_samplers;
245 bool sampler_view[PIPE_MAX_SAMPLERS]; /**< True if sampler view exists*/
246 uint8_t sampler_target[PIPE_MAX_SAMPLERS]; /**< TGSI_TEXTURE_x */
247 uint8_t sampler_return_type[PIPE_MAX_SAMPLERS]; /**< TGSI_RETURN_TYPE_x */
248
249 /* Images */
250 unsigned num_images;
251 unsigned image_mask;
252 struct tgsi_declaration_image image[PIPE_MAX_SHADER_IMAGES];
253 unsigned image_size_index; /* starting index to cbuf for image size */
254
255 /* Shader buffers */
256 unsigned num_shader_bufs;
257 unsigned raw_shaderbuf_srv_start_index; /* starting srv index for raw shaderbuf */
258 uint64_t raw_shaderbufs; /* raw shader buffers bitmask */
259
260 /* HW atomic buffers */
261 unsigned num_atomic_bufs;
262 unsigned atomic_bufs_mask;
263 unsigned max_atomic_counter_index;
264 VGPU10_OPCODE_TYPE cur_atomic_opcode; /* current atomic opcode */
265
266 bool uav_declared; /* True if uav is declared */
267
268 /* Index Range declaration */
269 struct {
270 unsigned start_index;
271 unsigned count;
272 bool required;
273 unsigned operandType;
274 unsigned size;
275 unsigned dim;
276 } index_range;
277
278 /* Address regs (really implemented with temps) */
279 unsigned num_address_regs;
280 unsigned address_reg_index[MAX_VGPU10_ADDR_REGS];
281
282 /* Output register usage masks */
283 uint8_t output_usage_mask[PIPE_MAX_SHADER_OUTPUTS];
284
285 /* To map TGSI system value index to VGPU shader input indexes */
286 uint8_t system_value_indexes[MAX_SYSTEM_VALUES];
287
288 struct {
289 /* vertex position scale/translation */
290 unsigned out_index; /**< the real position output reg */
291 unsigned tmp_index; /**< the fake/temp position output reg */
292 unsigned so_index; /**< the non-adjusted position output reg */
293 unsigned prescale_cbuf_index; /* index to the const buf for prescale */
294 unsigned prescale_scale_index, prescale_trans_index;
295 unsigned num_prescale; /* number of prescale factor in const buf */
296 unsigned viewport_index;
297 unsigned need_prescale:1;
298 unsigned have_prescale:1;
299 } vposition;
300
301 /* Shader limits */
302 unsigned max_vs_inputs;
303 unsigned max_vs_outputs;
304 unsigned max_gs_inputs;
305
306 /* For vertex shaders only */
307 struct {
308 /* viewport constant */
309 unsigned viewport_index;
310
311 unsigned vertex_id_bias_index;
312 unsigned vertex_id_sys_index;
313 unsigned vertex_id_tmp_index;
314
315 /* temp index of adjusted vertex attributes */
316 unsigned adjusted_input[PIPE_MAX_SHADER_INPUTS];
317 } vs;
318
319 /* For fragment shaders only */
320 struct {
321 unsigned color_out_index[PIPE_MAX_COLOR_BUFS]; /**< the real color output regs */
322 unsigned num_color_outputs;
323 unsigned color_tmp_index; /**< fake/temp color output reg */
324 unsigned alpha_ref_index; /**< immediate constant for alpha ref */
325
326 /* front-face */
327 unsigned face_input_index; /**< real fragment shader face reg (bool) */
328 unsigned face_tmp_index; /**< temp face reg converted to -1 / +1 */
329
330 unsigned pstipple_sampler_unit;
331 unsigned pstipple_sampler_state_index;
332
333 unsigned fragcoord_input_index; /**< real fragment position input reg */
334 unsigned fragcoord_tmp_index; /**< 1/w modified position temp reg */
335
336 unsigned sample_id_sys_index; /**< TGSI index of sample id sys value */
337
338 unsigned sample_pos_sys_index; /**< TGSI index of sample pos sys value */
339 unsigned sample_pos_tmp_index; /**< which temp reg has the sample pos */
340
341 /** TGSI index of sample mask input sys value */
342 unsigned sample_mask_in_sys_index;
343
344 /* layer */
345 unsigned layer_input_index; /**< TGSI index of layer */
346 unsigned layer_imm_index; /**< immediate for default layer 0 */
347
348 bool forceEarlyDepthStencil; /**< true if Early Depth stencil test is enabled */
349 } fs;
350
351 /* For geometry shaders only */
352 struct {
353 VGPU10_PRIMITIVE prim_type;/**< VGPU10 primitive type */
354 VGPU10_PRIMITIVE_TOPOLOGY prim_topology; /**< VGPU10 primitive topology */
355 unsigned input_size; /**< size of input arrays */
356 unsigned prim_id_index; /**< primitive id register index */
357 unsigned max_out_vertices; /**< maximum number of output vertices */
358 unsigned invocations;
359 unsigned invocation_id_sys_index;
360
361 unsigned viewport_index_out_index;
362 unsigned viewport_index_tmp_index;
363 } gs;
364
365 /* For tessellation control shaders only */
366 struct {
367 unsigned vertices_per_patch_index; /**< vertices_per_patch system value index */
368 unsigned imm_index; /**< immediate for tcs */
369 unsigned invocation_id_sys_index; /**< invocation id */
370 unsigned invocation_id_tmp_index;
371 unsigned instruction_token_pos; /* token pos for the first instruction */
372 unsigned control_point_input_index; /* control point input register index */
373 unsigned control_point_addr_index; /* control point input address register */
374 unsigned control_point_out_index; /* control point output register index */
375 unsigned control_point_tmp_index; /* control point temporary register */
376 unsigned control_point_out_count; /* control point output count */
377 bool control_point_phase; /* true if in control point phase */
378 bool fork_phase_add_signature; /* true if needs to add signature in fork phase */
379 unsigned patch_generic_out_count; /* per-patch generic output count */
380 unsigned patch_generic_out_index; /* per-patch generic output register index*/
381 unsigned patch_generic_tmp_index; /* per-patch generic temporary register index*/
382 unsigned prim_id_index; /* primitive id */
383 struct {
384 unsigned out_index; /* real tessinner output register */
385 unsigned temp_index; /* tessinner temp register */
386 unsigned tgsi_index; /* tgsi tessinner output register */
387 } inner;
388 struct {
389 unsigned out_index; /* real tessouter output register */
390 unsigned temp_index; /* tessouter temp register */
391 unsigned tgsi_index; /* tgsi tessouter output register */
392 } outer;
393 } tcs;
394
395 /* For tessellation evaluation shaders only */
396 struct {
397 enum mesa_prim prim_mode;
398 enum pipe_tess_spacing spacing;
399 bool vertices_order_cw;
400 bool point_mode;
401 unsigned tesscoord_sys_index;
402 unsigned swizzle_max;
403 unsigned prim_id_index; /* primitive id */
404 struct {
405 unsigned in_index; /* real tessinner input register */
406 unsigned temp_index; /* tessinner temp register */
407 unsigned tgsi_index; /* tgsi tessinner input register */
408 } inner;
409 struct {
410 unsigned in_index; /* real tessouter input register */
411 unsigned temp_index; /* tessouter temp register */
412 unsigned tgsi_index; /* tgsi tessouter input register */
413 } outer;
414 } tes;
415
416 struct {
417 unsigned block_width; /* thread group size in x dimension */
418 unsigned block_height; /* thread group size in y dimension */
419 unsigned block_depth; /* thread group size in z dimension */
420 unsigned thread_id_index; /* thread id tgsi index */
421 unsigned block_id_index; /* block id tgsi index */
422 bool shared_memory_declared; /* set if shared memory is declared */
423 struct {
424 unsigned tgsi_index; /* grid size tgsi index */
425 unsigned imm_index; /* grid size imm index */
426 } grid_size;
427 } cs;
428
429 /* For vertex or geometry shaders */
430 enum clipping_mode clip_mode;
431 unsigned clip_dist_out_index; /**< clip distance output register index */
432 unsigned clip_dist_tmp_index; /**< clip distance temporary register */
433 unsigned clip_dist_so_index; /**< clip distance shadow copy */
434
435 /** Index of temporary holding the clipvertex coordinate */
436 unsigned clip_vertex_out_index; /**< clip vertex output register index */
437 unsigned clip_vertex_tmp_index; /**< clip vertex temporary index */
438
439 /* user clip plane constant slot indexes */
440 unsigned clip_plane_const[PIPE_MAX_CLIP_PLANES];
441
442 unsigned num_output_writes;
443 bool constant_color_output;
444
445 bool uses_flat_interp;
446
447 bool uses_precise_qualifier;
448 unsigned reserved_token; /* index to the reserved token */
449
450 /* For all shaders: const reg index for RECT coord scaling */
451 unsigned texcoord_scale_index[PIPE_MAX_SAMPLERS];
452
453 /* For all shaders: const reg index for texture buffer size */
454 unsigned texture_buffer_size_index[PIPE_MAX_SAMPLERS];
455
456 /** Which texture units are doing shadow comparison in the shader code */
457 unsigned shadow_compare_units;
458
459 /* VS/TCS/TES/GS/FS Linkage info */
460 struct shader_linkage linkage;
461 struct tgsi_shader_info *prevShaderInfo;
462
463 /* Shader signature */
464 struct svga_shader_signature signature;
465
466 bool register_overflow; /**< Set if we exceed a VGPU10 register limit */
467
468 /* For util_debug_message */
469 struct util_debug_callback svga_debug_callback;
470
471 /* current loop depth in shader */
472 unsigned current_loop_depth;
473 };
474
475
476 static void emit_tcs_input_declarations(struct svga_shader_emitter_v10 *emit);
477 static void emit_tcs_output_declarations(struct svga_shader_emitter_v10 *emit);
478 static bool emit_temporaries_declaration(struct svga_shader_emitter_v10 *emit);
479 static bool emit_constant_declaration(struct svga_shader_emitter_v10 *emit);
480 static bool emit_sampler_declarations(struct svga_shader_emitter_v10 *emit);
481 static bool emit_resource_declarations(struct svga_shader_emitter_v10 *emit);
482 static bool emit_vgpu10_immediates_block(struct svga_shader_emitter_v10 *emit);
483 static bool emit_index_range_declaration(struct svga_shader_emitter_v10 *emit);
484 static void emit_image_declarations(struct svga_shader_emitter_v10 *emit);
485 static void emit_shader_buf_declarations(struct svga_shader_emitter_v10 *emit);
486 static void emit_atomic_buf_declarations(struct svga_shader_emitter_v10 *emit);
487 static void emit_temp_prescale_instructions(struct svga_shader_emitter_v10 *emit);
488
489 static bool
490 emit_post_helpers(struct svga_shader_emitter_v10 *emit);
491
492 static bool
493 emit_vertex(struct svga_shader_emitter_v10 *emit,
494 const struct tgsi_full_instruction *inst);
495
496 static bool
497 emit_vgpu10_instruction(struct svga_shader_emitter_v10 *emit,
498 unsigned inst_number,
499 const struct tgsi_full_instruction *inst);
500
501 static void
502 emit_input_declaration(struct svga_shader_emitter_v10 *emit,
503 VGPU10_OPCODE_TYPE opcodeType,
504 VGPU10_OPERAND_TYPE operandType,
505 VGPU10_OPERAND_INDEX_DIMENSION dim,
506 unsigned index, unsigned size,
507 VGPU10_SYSTEM_NAME name,
508 VGPU10_OPERAND_NUM_COMPONENTS numComp,
509 VGPU10_OPERAND_4_COMPONENT_SELECTION_MODE selMode,
510 unsigned usageMask,
511 VGPU10_INTERPOLATION_MODE interpMode,
512 bool addSignature,
513 SVGA3dDXSignatureSemanticName sgnName);
514
515 static bool
516 emit_rawbuf_instruction(struct svga_shader_emitter_v10 *emit,
517 unsigned inst_number,
518 const struct tgsi_full_instruction *inst);
519
520 static void
521 create_temp_array(struct svga_shader_emitter_v10 *emit,
522 unsigned arrayID, unsigned first, unsigned count,
523 unsigned startIndex);
524
525 static char err_buf[128];
526
527 static bool
528 expand(struct svga_shader_emitter_v10 *emit)
529 {
530 char *new_buf;
531 unsigned newsize = emit->size * 2;
532
533 if (emit->buf != err_buf)
534 new_buf = REALLOC(emit->buf, emit->size, newsize);
535 else
536 new_buf = NULL;
537
538 if (!new_buf) {
539 emit->ptr = err_buf;
540 emit->buf = err_buf;
541 emit->size = sizeof(err_buf);
542 return false;
543 }
544
545 emit->size = newsize;
546 emit->ptr = new_buf + (emit->ptr - emit->buf);
547 emit->buf = new_buf;
548 return true;
549 }
550
551 /**
552 * Create and initialize a new svga_shader_emitter_v10 object.
553 */
554 static struct svga_shader_emitter_v10 *
555 alloc_emitter(void)
556 {
557 struct svga_shader_emitter_v10 *emit = CALLOC(1, sizeof(*emit));
558
559 if (!emit)
560 return NULL;
561
562 /* to initialize the output buffer */
563 emit->size = 512;
564 if (!expand(emit)) {
565 FREE(emit);
566 return NULL;
567 }
568 return emit;
569 }
570
571 /**
572 * Free an svga_shader_emitter_v10 object.
573 */
574 static void
575 free_emitter(struct svga_shader_emitter_v10 *emit)
576 {
577 assert(emit);
578 FREE(emit->buf); /* will be NULL if translation succeeded */
579 FREE(emit);
580 }
581
582 static inline bool
583 reserve(struct svga_shader_emitter_v10 *emit,
584 unsigned nr_dwords)
585 {
586 while (emit->ptr - emit->buf + nr_dwords * sizeof(uint32) >= emit->size) {
587 if (!expand(emit))
588 return false;
589 }
590
591 return true;
592 }
593
594 static bool
595 emit_dword(struct svga_shader_emitter_v10 *emit, uint32 dword)
596 {
597 if (!reserve(emit, 1))
598 return false;
599
600 *(uint32 *)emit->ptr = dword;
601 emit->ptr += sizeof dword;
602 return true;
603 }
604
605 static bool
606 emit_dwords(struct svga_shader_emitter_v10 *emit,
607 const uint32 *dwords,
608 unsigned nr)
609 {
610 if (!reserve(emit, nr))
611 return false;
612
613 memcpy(emit->ptr, dwords, nr * sizeof *dwords);
614 emit->ptr += nr * sizeof *dwords;
615 return true;
616 }
617
618 /** Return the number of tokens in the emitter's buffer */
619 static unsigned
620 emit_get_num_tokens(const struct svga_shader_emitter_v10 *emit)
621 {
622 return (emit->ptr - emit->buf) / sizeof(unsigned);
623 }
624
625
626 /**
627 * Check for register overflow. If we overflow we'll set an
628 * error flag. This function can be called for register declarations
629 * or use as src/dst instruction operands.
630 * \param type register type. One of VGPU10_OPERAND_TYPE_x
631 or VGPU10_OPCODE_DCL_x
632 * \param index the register index
633 */
634 static void
635 check_register_index(struct svga_shader_emitter_v10 *emit,
636 unsigned operandType, unsigned index)
637 {
638 bool overflow_before = emit->register_overflow;
639
640 switch (operandType) {
641 case VGPU10_OPERAND_TYPE_TEMP:
642 case VGPU10_OPERAND_TYPE_INDEXABLE_TEMP:
643 case VGPU10_OPCODE_DCL_TEMPS:
644 if (index >= VGPU10_MAX_TEMPS) {
645 emit->register_overflow = true;
646 }
647 break;
648 case VGPU10_OPERAND_TYPE_CONSTANT_BUFFER:
649 case VGPU10_OPCODE_DCL_CONSTANT_BUFFER:
650 if (index >= VGPU10_MAX_CONSTANT_BUFFER_ELEMENT_COUNT) {
651 emit->register_overflow = true;
652 }
653 break;
654 case VGPU10_OPERAND_TYPE_INPUT:
655 case VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID:
656 case VGPU10_OPCODE_DCL_INPUT:
657 case VGPU10_OPCODE_DCL_INPUT_SGV:
658 case VGPU10_OPCODE_DCL_INPUT_SIV:
659 case VGPU10_OPCODE_DCL_INPUT_PS:
660 case VGPU10_OPCODE_DCL_INPUT_PS_SGV:
661 case VGPU10_OPCODE_DCL_INPUT_PS_SIV:
662 if ((emit->unit == MESA_SHADER_VERTEX &&
663 index >= emit->max_vs_inputs) ||
664 (emit->unit == MESA_SHADER_GEOMETRY &&
665 index >= emit->max_gs_inputs) ||
666 (emit->unit == MESA_SHADER_FRAGMENT &&
667 index >= VGPU10_MAX_PS_INPUTS) ||
668 (emit->unit == MESA_SHADER_TESS_CTRL &&
669 index >= VGPU11_MAX_HS_INPUT_CONTROL_POINTS) ||
670 (emit->unit == MESA_SHADER_TESS_EVAL &&
671 index >= VGPU11_MAX_DS_INPUT_CONTROL_POINTS)) {
672 emit->register_overflow = true;
673 }
674 break;
675 case VGPU10_OPERAND_TYPE_OUTPUT:
676 case VGPU10_OPCODE_DCL_OUTPUT:
677 case VGPU10_OPCODE_DCL_OUTPUT_SGV:
678 case VGPU10_OPCODE_DCL_OUTPUT_SIV:
679 /* Note: we are skipping two output indices in tcs for
680 * tessinner/outer levels. Implementation will not exceed
681 * number of output count but it allows index to go beyond
682 * VGPU11_MAX_HS_OUTPUTS.
683 * Index will never be >= index >= VGPU11_MAX_HS_OUTPUTS + 2
684 */
685 if ((emit->unit == MESA_SHADER_VERTEX &&
686 index >= emit->max_vs_outputs) ||
687 (emit->unit == MESA_SHADER_GEOMETRY &&
688 index >= VGPU10_MAX_GS_OUTPUTS) ||
689 (emit->unit == MESA_SHADER_FRAGMENT &&
690 index >= VGPU10_MAX_PS_OUTPUTS) ||
691 (emit->unit == MESA_SHADER_TESS_CTRL &&
692 index >= VGPU11_MAX_HS_OUTPUTS + 2) ||
693 (emit->unit == MESA_SHADER_TESS_EVAL &&
694 index >= VGPU11_MAX_DS_OUTPUTS)) {
695 emit->register_overflow = true;
696 }
697 break;
698 case VGPU10_OPERAND_TYPE_SAMPLER:
699 case VGPU10_OPCODE_DCL_SAMPLER:
700 if (index >= VGPU10_MAX_SAMPLERS) {
701 emit->register_overflow = true;
702 }
703 break;
704 case VGPU10_OPERAND_TYPE_RESOURCE:
705 case VGPU10_OPCODE_DCL_RESOURCE:
706 if (index >= VGPU10_MAX_RESOURCES) {
707 emit->register_overflow = true;
708 }
709 break;
710 case VGPU10_OPERAND_TYPE_IMMEDIATE_CONSTANT_BUFFER:
711 if (index >= MAX_IMMEDIATE_COUNT) {
712 emit->register_overflow = true;
713 }
714 break;
715 case VGPU10_OPERAND_TYPE_OUTPUT_COVERAGE_MASK:
716 case VGPU10_OPERAND_TYPE_INPUT_GS_INSTANCE_ID:
717 case VGPU10_OPERAND_TYPE_OUTPUT_CONTROL_POINT_ID:
718 case VGPU10_OPERAND_TYPE_INPUT_CONTROL_POINT:
719 case VGPU10_OPERAND_TYPE_INPUT_DOMAIN_POINT:
720 case VGPU10_OPERAND_TYPE_INPUT_PATCH_CONSTANT:
721 case VGPU10_OPERAND_TYPE_INPUT_THREAD_GROUP_ID:
722 case VGPU10_OPERAND_TYPE_INPUT_THREAD_ID_IN_GROUP:
723 /* nothing */
724 break;
725 default:
726 assert(0);
727 ; /* nothing */
728 }
729
730 if (emit->register_overflow && !overflow_before) {
731 debug_printf("svga: vgpu10 register overflow (reg %u, index %u)\n",
732 operandType, index);
733 }
734 }
735
736
737 /**
738 * Examine misc state to determine the clipping mode.
739 */
740 static void
741 determine_clipping_mode(struct svga_shader_emitter_v10 *emit)
742 {
743 /* num_written_clipdistance in the shader info for tessellation
744 * control shader is always 0 because the TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
745 * is not defined for this shader. So we go through all the output declarations
746 * to set the num_written_clipdistance. This is just to determine the
747 * clipping mode.
748 */
749 if (emit->unit == MESA_SHADER_TESS_CTRL) {
750 unsigned i;
751 for (i = 0; i < emit->info.num_outputs; i++) {
752 if (emit->info.output_semantic_name[i] == TGSI_SEMANTIC_CLIPDIST) {
753 emit->info.num_written_clipdistance =
754 4 * (emit->info.output_semantic_index[i] + 1);
755 }
756 }
757 }
758
759 if (emit->info.num_written_clipdistance > 0) {
760 emit->clip_mode = CLIP_DISTANCE;
761 }
762 else if (emit->info.writes_clipvertex) {
763 emit->clip_mode = CLIP_VERTEX;
764 }
765 else if (emit->key.clip_plane_enable && emit->key.last_vertex_stage) {
766 /*
767 * Only the last shader in the vertex processing stage needs to
768 * handle the legacy clip mode.
769 */
770 emit->clip_mode = CLIP_LEGACY;
771 }
772 else {
773 emit->clip_mode = CLIP_NONE;
774 }
775 }
776
777
778 /**
779 * For clip distance register declarations and clip distance register
780 * writes we need to mask the declaration usage or instruction writemask
781 * (respectively) against the set of the really-enabled clipping planes.
782 *
783 * The piglit test spec/glsl-1.30/execution/clipping/vs-clip-distance-enables
784 * has a VS that writes to all 8 clip distance registers, but the plane enable
785 * flags are a subset of that.
786 *
787 * This function is used to apply the plane enable flags to the register
788 * declaration or instruction writemask.
789 *
790 * \param writemask the declaration usage mask or instruction writemask
791 * \param clip_reg_index which clip plane register is being declared/written.
792 * The legal values are 0 and 1 (two clip planes per
793 * register, for a total of 8 clip planes)
794 */
795 static unsigned
796 apply_clip_plane_mask(struct svga_shader_emitter_v10 *emit,
797 unsigned writemask, unsigned clip_reg_index)
798 {
799 unsigned shift;
800
801 assert(clip_reg_index < 2);
802
803 /* four clip planes per clip register: */
804 shift = clip_reg_index * 4;
805 writemask &= ((emit->key.clip_plane_enable >> shift) & 0xf);
806
807 return writemask;
808 }
809
810
811 /**
812 * Translate gallium shader type into VGPU10 type.
813 */
814 static VGPU10_PROGRAM_TYPE
815 translate_shader_type(unsigned type)
816 {
817 switch (type) {
818 case MESA_SHADER_VERTEX:
819 return VGPU10_VERTEX_SHADER;
820 case MESA_SHADER_GEOMETRY:
821 return VGPU10_GEOMETRY_SHADER;
822 case MESA_SHADER_FRAGMENT:
823 return VGPU10_PIXEL_SHADER;
824 case MESA_SHADER_TESS_CTRL:
825 return VGPU10_HULL_SHADER;
826 case MESA_SHADER_TESS_EVAL:
827 return VGPU10_DOMAIN_SHADER;
828 case MESA_SHADER_COMPUTE:
829 return VGPU10_COMPUTE_SHADER;
830 default:
831 assert(!"Unexpected shader type");
832 return VGPU10_VERTEX_SHADER;
833 }
834 }
835
836
837 /**
838 * Translate a TGSI_OPCODE_x into a VGPU10_OPCODE_x
839 * Note: we only need to translate the opcodes for "simple" instructions,
840 * as seen below. All other opcodes are handled/translated specially.
841 */
842 static VGPU10_OPCODE_TYPE
843 translate_opcode(enum tgsi_opcode opcode)
844 {
845 switch (opcode) {
846 case TGSI_OPCODE_MOV:
847 return VGPU10_OPCODE_MOV;
848 case TGSI_OPCODE_MUL:
849 return VGPU10_OPCODE_MUL;
850 case TGSI_OPCODE_ADD:
851 return VGPU10_OPCODE_ADD;
852 case TGSI_OPCODE_DP3:
853 return VGPU10_OPCODE_DP3;
854 case TGSI_OPCODE_DP4:
855 return VGPU10_OPCODE_DP4;
856 case TGSI_OPCODE_MIN:
857 return VGPU10_OPCODE_MIN;
858 case TGSI_OPCODE_MAX:
859 return VGPU10_OPCODE_MAX;
860 case TGSI_OPCODE_MAD:
861 return VGPU10_OPCODE_MAD;
862 case TGSI_OPCODE_SQRT:
863 return VGPU10_OPCODE_SQRT;
864 case TGSI_OPCODE_FRC:
865 return VGPU10_OPCODE_FRC;
866 case TGSI_OPCODE_FLR:
867 return VGPU10_OPCODE_ROUND_NI;
868 case TGSI_OPCODE_FSEQ:
869 return VGPU10_OPCODE_EQ;
870 case TGSI_OPCODE_FSGE:
871 return VGPU10_OPCODE_GE;
872 case TGSI_OPCODE_FSNE:
873 return VGPU10_OPCODE_NE;
874 case TGSI_OPCODE_DDX:
875 return VGPU10_OPCODE_DERIV_RTX;
876 case TGSI_OPCODE_DDY:
877 return VGPU10_OPCODE_DERIV_RTY;
878 case TGSI_OPCODE_RET:
879 return VGPU10_OPCODE_RET;
880 case TGSI_OPCODE_DIV:
881 return VGPU10_OPCODE_DIV;
882 case TGSI_OPCODE_IDIV:
883 return VGPU10_OPCODE_VMWARE;
884 case TGSI_OPCODE_DP2:
885 return VGPU10_OPCODE_DP2;
886 case TGSI_OPCODE_BRK:
887 return VGPU10_OPCODE_BREAK;
888 case TGSI_OPCODE_IF:
889 return VGPU10_OPCODE_IF;
890 case TGSI_OPCODE_ELSE:
891 return VGPU10_OPCODE_ELSE;
892 case TGSI_OPCODE_ENDIF:
893 return VGPU10_OPCODE_ENDIF;
894 case TGSI_OPCODE_DDX_FINE:
895 return VGPU10_OPCODE_DERIV_RTX_FINE;
896 case TGSI_OPCODE_DDY_FINE:
897 return VGPU10_OPCODE_DERIV_RTY_FINE;
898 case TGSI_OPCODE_CEIL:
899 return VGPU10_OPCODE_ROUND_PI;
900 case TGSI_OPCODE_I2F:
901 return VGPU10_OPCODE_ITOF;
902 case TGSI_OPCODE_NOT:
903 return VGPU10_OPCODE_NOT;
904 case TGSI_OPCODE_TRUNC:
905 return VGPU10_OPCODE_ROUND_Z;
906 case TGSI_OPCODE_SHL:
907 return VGPU10_OPCODE_ISHL;
908 case TGSI_OPCODE_AND:
909 return VGPU10_OPCODE_AND;
910 case TGSI_OPCODE_OR:
911 return VGPU10_OPCODE_OR;
912 case TGSI_OPCODE_XOR:
913 return VGPU10_OPCODE_XOR;
914 case TGSI_OPCODE_CONT:
915 return VGPU10_OPCODE_CONTINUE;
916 case TGSI_OPCODE_EMIT:
917 return VGPU10_OPCODE_EMIT;
918 case TGSI_OPCODE_ENDPRIM:
919 return VGPU10_OPCODE_CUT;
920 case TGSI_OPCODE_BGNLOOP:
921 return VGPU10_OPCODE_LOOP;
922 case TGSI_OPCODE_ENDLOOP:
923 return VGPU10_OPCODE_ENDLOOP;
924 case TGSI_OPCODE_ENDSUB:
925 return VGPU10_OPCODE_RET;
926 case TGSI_OPCODE_NOP:
927 return VGPU10_OPCODE_NOP;
928 case TGSI_OPCODE_END:
929 return VGPU10_OPCODE_RET;
930 case TGSI_OPCODE_F2I:
931 return VGPU10_OPCODE_FTOI;
932 case TGSI_OPCODE_IMAX:
933 return VGPU10_OPCODE_IMAX;
934 case TGSI_OPCODE_IMIN:
935 return VGPU10_OPCODE_IMIN;
936 case TGSI_OPCODE_UDIV:
937 case TGSI_OPCODE_UMOD:
938 case TGSI_OPCODE_MOD:
939 return VGPU10_OPCODE_UDIV;
940 case TGSI_OPCODE_IMUL_HI:
941 return VGPU10_OPCODE_IMUL;
942 case TGSI_OPCODE_INEG:
943 return VGPU10_OPCODE_INEG;
944 case TGSI_OPCODE_ISHR:
945 return VGPU10_OPCODE_ISHR;
946 case TGSI_OPCODE_ISGE:
947 return VGPU10_OPCODE_IGE;
948 case TGSI_OPCODE_ISLT:
949 return VGPU10_OPCODE_ILT;
950 case TGSI_OPCODE_F2U:
951 return VGPU10_OPCODE_FTOU;
952 case TGSI_OPCODE_UADD:
953 return VGPU10_OPCODE_IADD;
954 case TGSI_OPCODE_U2F:
955 return VGPU10_OPCODE_UTOF;
956 case TGSI_OPCODE_UCMP:
957 return VGPU10_OPCODE_MOVC;
958 case TGSI_OPCODE_UMAD:
959 return VGPU10_OPCODE_UMAD;
960 case TGSI_OPCODE_UMAX:
961 return VGPU10_OPCODE_UMAX;
962 case TGSI_OPCODE_UMIN:
963 return VGPU10_OPCODE_UMIN;
964 case TGSI_OPCODE_UMUL:
965 case TGSI_OPCODE_UMUL_HI:
966 return VGPU10_OPCODE_UMUL;
967 case TGSI_OPCODE_USEQ:
968 return VGPU10_OPCODE_IEQ;
969 case TGSI_OPCODE_USGE:
970 return VGPU10_OPCODE_UGE;
971 case TGSI_OPCODE_USHR:
972 return VGPU10_OPCODE_USHR;
973 case TGSI_OPCODE_USLT:
974 return VGPU10_OPCODE_ULT;
975 case TGSI_OPCODE_USNE:
976 return VGPU10_OPCODE_INE;
977 case TGSI_OPCODE_SWITCH:
978 return VGPU10_OPCODE_SWITCH;
979 case TGSI_OPCODE_CASE:
980 return VGPU10_OPCODE_CASE;
981 case TGSI_OPCODE_DEFAULT:
982 return VGPU10_OPCODE_DEFAULT;
983 case TGSI_OPCODE_ENDSWITCH:
984 return VGPU10_OPCODE_ENDSWITCH;
985 case TGSI_OPCODE_FSLT:
986 return VGPU10_OPCODE_LT;
987 case TGSI_OPCODE_ROUND:
988 return VGPU10_OPCODE_ROUND_NE;
989 /* Begin SM5 opcodes */
990 case TGSI_OPCODE_F2D:
991 return VGPU10_OPCODE_FTOD;
992 case TGSI_OPCODE_D2F:
993 return VGPU10_OPCODE_DTOF;
994 case TGSI_OPCODE_DMUL:
995 return VGPU10_OPCODE_DMUL;
996 case TGSI_OPCODE_DADD:
997 return VGPU10_OPCODE_DADD;
998 case TGSI_OPCODE_DMAX:
999 return VGPU10_OPCODE_DMAX;
1000 case TGSI_OPCODE_DMIN:
1001 return VGPU10_OPCODE_DMIN;
1002 case TGSI_OPCODE_DSEQ:
1003 return VGPU10_OPCODE_DEQ;
1004 case TGSI_OPCODE_DSGE:
1005 return VGPU10_OPCODE_DGE;
1006 case TGSI_OPCODE_DSLT:
1007 return VGPU10_OPCODE_DLT;
1008 case TGSI_OPCODE_DSNE:
1009 return VGPU10_OPCODE_DNE;
1010 case TGSI_OPCODE_IBFE:
1011 return VGPU10_OPCODE_IBFE;
1012 case TGSI_OPCODE_UBFE:
1013 return VGPU10_OPCODE_UBFE;
1014 case TGSI_OPCODE_BFI:
1015 return VGPU10_OPCODE_BFI;
1016 case TGSI_OPCODE_BREV:
1017 return VGPU10_OPCODE_BFREV;
1018 case TGSI_OPCODE_POPC:
1019 return VGPU10_OPCODE_COUNTBITS;
1020 case TGSI_OPCODE_LSB:
1021 return VGPU10_OPCODE_FIRSTBIT_LO;
1022 case TGSI_OPCODE_IMSB:
1023 return VGPU10_OPCODE_FIRSTBIT_SHI;
1024 case TGSI_OPCODE_UMSB:
1025 return VGPU10_OPCODE_FIRSTBIT_HI;
1026 case TGSI_OPCODE_INTERP_CENTROID:
1027 return VGPU10_OPCODE_EVAL_CENTROID;
1028 case TGSI_OPCODE_INTERP_SAMPLE:
1029 return VGPU10_OPCODE_EVAL_SAMPLE_INDEX;
1030 case TGSI_OPCODE_BARRIER:
1031 return VGPU10_OPCODE_SYNC;
1032 case TGSI_OPCODE_DFMA:
1033 return VGPU10_OPCODE_DFMA;
1034 case TGSI_OPCODE_FMA:
1035 return VGPU10_OPCODE_MAD;
1036
1037 /* DX11.1 Opcodes */
1038 case TGSI_OPCODE_DDIV:
1039 return VGPU10_OPCODE_DDIV;
1040 case TGSI_OPCODE_DRCP:
1041 return VGPU10_OPCODE_DRCP;
1042 case TGSI_OPCODE_D2I:
1043 return VGPU10_OPCODE_DTOI;
1044 case TGSI_OPCODE_D2U:
1045 return VGPU10_OPCODE_DTOU;
1046 case TGSI_OPCODE_I2D:
1047 return VGPU10_OPCODE_ITOD;
1048 case TGSI_OPCODE_U2D:
1049 return VGPU10_OPCODE_UTOD;
1050
1051 case TGSI_OPCODE_SAMPLE_POS:
1052 /* Note: we never actually get this opcode because there's no GLSL
1053 * function to query multisample resource sample positions. There's
1054 * only the TGSI_SEMANTIC_SAMPLEPOS system value which contains the
1055 * position of the current sample in the render target.
1056 */
1057 FALLTHROUGH;
1058 case TGSI_OPCODE_SAMPLE_INFO:
1059 /* NOTE: we never actually get this opcode because the GLSL compiler
1060 * implements the gl_NumSamples variable with a simple constant in the
1061 * constant buffer.
1062 */
1063 FALLTHROUGH;
1064 default:
1065 assert(!"Unexpected TGSI opcode in translate_opcode()");
1066 return VGPU10_OPCODE_NOP;
1067 }
1068 }
1069
1070
1071 /**
1072 * Translate a TGSI register file type into a VGPU10 operand type.
1073 * \param array is the TGSI_FILE_TEMPORARY register an array?
1074 */
1075 static VGPU10_OPERAND_TYPE
1076 translate_register_file(enum tgsi_file_type file, bool array)
1077 {
1078 switch (file) {
1079 case TGSI_FILE_CONSTANT:
1080 return VGPU10_OPERAND_TYPE_CONSTANT_BUFFER;
1081 case TGSI_FILE_INPUT:
1082 return VGPU10_OPERAND_TYPE_INPUT;
1083 case TGSI_FILE_OUTPUT:
1084 return VGPU10_OPERAND_TYPE_OUTPUT;
1085 case TGSI_FILE_TEMPORARY:
1086 return array ? VGPU10_OPERAND_TYPE_INDEXABLE_TEMP
1087 : VGPU10_OPERAND_TYPE_TEMP;
1088 case TGSI_FILE_IMMEDIATE:
1089 /* all immediates are 32-bit values at this time so
1090 * VGPU10_OPERAND_TYPE_IMMEDIATE64 is not possible at this time.
1091 */
1092 return VGPU10_OPERAND_TYPE_IMMEDIATE_CONSTANT_BUFFER;
1093 case TGSI_FILE_SAMPLER:
1094 return VGPU10_OPERAND_TYPE_SAMPLER;
1095 case TGSI_FILE_SYSTEM_VALUE:
1096 return VGPU10_OPERAND_TYPE_INPUT;
1097
1098 /* XXX TODO more cases to finish */
1099
1100 default:
1101 assert(!"Bad tgsi register file!");
1102 return VGPU10_OPERAND_TYPE_NULL;
1103 }
1104 }
1105
1106
1107 /**
1108 * Emit a null dst register
1109 */
1110 static void
1111 emit_null_dst_register(struct svga_shader_emitter_v10 *emit)
1112 {
1113 VGPU10OperandToken0 operand;
1114
1115 operand.value = 0;
1116 operand.operandType = VGPU10_OPERAND_TYPE_NULL;
1117 operand.numComponents = VGPU10_OPERAND_0_COMPONENT;
1118
1119 emit_dword(emit, operand.value);
1120 }
1121
1122
1123 /**
1124 * If the given register is a temporary, return the array ID.
1125 * Else return zero.
1126 */
1127 static unsigned
1128 get_temp_array_id(const struct svga_shader_emitter_v10 *emit,
1129 enum tgsi_file_type file, unsigned index)
1130 {
1131 if (file == TGSI_FILE_TEMPORARY) {
1132 return emit->temp_map[index].arrayId;
1133 }
1134 else {
1135 return 0;
1136 }
1137 }
1138
1139
1140 /**
1141 * If the given register is a temporary, convert the index from a TGSI
1142 * TEMPORARY index to a VGPU10 temp index.
1143 */
1144 static unsigned
1145 remap_temp_index(const struct svga_shader_emitter_v10 *emit,
1146 enum tgsi_file_type file, unsigned index)
1147 {
1148 if (file == TGSI_FILE_TEMPORARY) {
1149 return emit->temp_map[index].index;
1150 }
1151 else {
1152 return index;
1153 }
1154 }
1155
1156
1157 /**
1158 * Setup the operand0 fields related to indexing (1D, 2D, relative, etc).
1159 * Note: the operandType field must already be initialized.
1160 * \param file the register file being accessed
1161 * \param indirect using indirect addressing of the register file?
1162 * \param index2D if true, 2-D indexing is being used (const or temp registers)
1163 * \param indirect2D if true, 2-D indirect indexing being used (for const buf)
1164 */
1165 static VGPU10OperandToken0
1166 setup_operand0_indexing(struct svga_shader_emitter_v10 *emit,
1167 VGPU10OperandToken0 operand0,
1168 enum tgsi_file_type file,
1169 bool indirect,
1170 bool index2D, bool indirect2D)
1171 {
1172 VGPU10_OPERAND_INDEX_REPRESENTATION index0Rep, index1Rep;
1173 VGPU10_OPERAND_INDEX_DIMENSION indexDim;
1174
1175 /*
1176 * Compute index dimensions
1177 */
1178 if (operand0.operandType == VGPU10_OPERAND_TYPE_IMMEDIATE32 ||
1179 operand0.operandType == VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID ||
1180 operand0.operandType == VGPU10_OPERAND_TYPE_INPUT_GS_INSTANCE_ID ||
1181 operand0.operandType == VGPU10_OPERAND_TYPE_INPUT_THREAD_ID ||
1182 operand0.operandType == VGPU10_OPERAND_TYPE_INPUT_THREAD_ID_IN_GROUP ||
1183 operand0.operandType == VGPU10_OPERAND_TYPE_OUTPUT_CONTROL_POINT_ID) {
1184 /* there's no swizzle for in-line immediates */
1185 indexDim = VGPU10_OPERAND_INDEX_0D;
1186 assert(operand0.selectionMode == 0);
1187 }
1188 else if (operand0.operandType == VGPU10_OPERAND_TYPE_INPUT_DOMAIN_POINT) {
1189 indexDim = VGPU10_OPERAND_INDEX_0D;
1190 }
1191 else {
1192 indexDim = index2D ? VGPU10_OPERAND_INDEX_2D : VGPU10_OPERAND_INDEX_1D;
1193 }
1194
1195 /*
1196 * Compute index representation(s) (immediate vs relative).
1197 */
1198 if (indexDim == VGPU10_OPERAND_INDEX_2D) {
1199 index0Rep = indirect2D ? VGPU10_OPERAND_INDEX_IMMEDIATE32_PLUS_RELATIVE
1200 : VGPU10_OPERAND_INDEX_IMMEDIATE32;
1201
1202 index1Rep = indirect ? VGPU10_OPERAND_INDEX_IMMEDIATE32_PLUS_RELATIVE
1203 : VGPU10_OPERAND_INDEX_IMMEDIATE32;
1204 }
1205 else if (indexDim == VGPU10_OPERAND_INDEX_1D) {
1206 index0Rep = indirect ? VGPU10_OPERAND_INDEX_IMMEDIATE32_PLUS_RELATIVE
1207 : VGPU10_OPERAND_INDEX_IMMEDIATE32;
1208
1209 index1Rep = 0;
1210 }
1211 else {
1212 index0Rep = 0;
1213 index1Rep = 0;
1214 }
1215
1216 operand0.indexDimension = indexDim;
1217 operand0.index0Representation = index0Rep;
1218 operand0.index1Representation = index1Rep;
1219
1220 return operand0;
1221 }
1222
1223
1224 /**
1225 * Emit the operand for expressing an address register for indirect indexing.
1226 * Note that the address register is really just a temp register.
1227 * \param addr_reg_index which address register to use
1228 */
1229 static void
1230 emit_indirect_register(struct svga_shader_emitter_v10 *emit,
1231 unsigned addr_reg_index)
1232 {
1233 unsigned tmp_reg_index;
1234 VGPU10OperandToken0 operand0;
1235
1236 assert(addr_reg_index < MAX_VGPU10_ADDR_REGS);
1237
1238 tmp_reg_index = emit->address_reg_index[addr_reg_index];
1239
1240 /* operand0 is a simple temporary register, selecting one component */
1241 operand0.value = 0;
1242 operand0.operandType = VGPU10_OPERAND_TYPE_TEMP;
1243 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
1244 operand0.indexDimension = VGPU10_OPERAND_INDEX_1D;
1245 operand0.index0Representation = VGPU10_OPERAND_INDEX_IMMEDIATE32;
1246 operand0.selectionMode = VGPU10_OPERAND_4_COMPONENT_SELECT_1_MODE;
1247 operand0.swizzleX = 0;
1248 operand0.swizzleY = 1;
1249 operand0.swizzleZ = 2;
1250 operand0.swizzleW = 3;
1251
1252 emit_dword(emit, operand0.value);
1253 emit_dword(emit, remap_temp_index(emit, TGSI_FILE_TEMPORARY, tmp_reg_index));
1254 }
1255
1256
1257 /**
1258 * Translate the dst register of a TGSI instruction and emit VGPU10 tokens.
1259 * \param emit the emitter context
1260 * \param reg the TGSI dst register to translate
1261 */
1262 static void
1263 emit_dst_register(struct svga_shader_emitter_v10 *emit,
1264 const struct tgsi_full_dst_register *reg)
1265 {
1266 enum tgsi_file_type file = reg->Register.File;
1267 unsigned index = reg->Register.Index;
1268 const enum tgsi_semantic sem_name = emit->info.output_semantic_name[index];
1269 const unsigned sem_index = emit->info.output_semantic_index[index];
1270 unsigned writemask = reg->Register.WriteMask;
1271 const bool indirect = reg->Register.Indirect;
1272 unsigned tempArrayId = get_temp_array_id(emit, file, index);
1273 bool index2d = reg->Register.Dimension || tempArrayId > 0;
1274 VGPU10OperandToken0 operand0;
1275
1276 if (file == TGSI_FILE_TEMPORARY) {
1277 emit->temp_map[index].initialized = true;
1278 }
1279
1280 if (file == TGSI_FILE_OUTPUT) {
1281 if (emit->unit == MESA_SHADER_VERTEX ||
1282 emit->unit == MESA_SHADER_GEOMETRY ||
1283 emit->unit == MESA_SHADER_TESS_EVAL) {
1284 if (index == emit->vposition.out_index &&
1285 emit->vposition.tmp_index != INVALID_INDEX) {
1286 /* replace OUTPUT[POS] with TEMP[POS]. We need to store the
1287 * vertex position result in a temporary so that we can modify
1288 * it in the post_helper() code.
1289 */
1290 file = TGSI_FILE_TEMPORARY;
1291 index = emit->vposition.tmp_index;
1292 }
1293 else if (sem_name == TGSI_SEMANTIC_CLIPDIST &&
1294 emit->clip_dist_tmp_index != INVALID_INDEX) {
1295 /* replace OUTPUT[CLIPDIST] with TEMP[CLIPDIST].
1296 * We store the clip distance in a temporary first, then
1297 * we'll copy it to the shadow copy and to CLIPDIST with the
1298 * enabled planes mask in emit_clip_distance_instructions().
1299 */
1300 file = TGSI_FILE_TEMPORARY;
1301 index = emit->clip_dist_tmp_index + sem_index;
1302 }
1303 else if (sem_name == TGSI_SEMANTIC_CLIPVERTEX &&
1304 emit->clip_vertex_tmp_index != INVALID_INDEX) {
1305 /* replace the CLIPVERTEX output register with a temporary */
1306 assert(emit->clip_mode == CLIP_VERTEX);
1307 assert(sem_index == 0);
1308 file = TGSI_FILE_TEMPORARY;
1309 index = emit->clip_vertex_tmp_index;
1310 }
1311 else if (sem_name == TGSI_SEMANTIC_COLOR &&
1312 emit->key.clamp_vertex_color) {
1313
1314 /* set the saturate modifier of the instruction
1315 * to clamp the vertex color.
1316 */
1317 VGPU10OpcodeToken0 *token =
1318 (VGPU10OpcodeToken0 *)emit->buf + emit->inst_start_token;
1319 token->saturate = true;
1320 }
1321 else if (sem_name == TGSI_SEMANTIC_VIEWPORT_INDEX &&
1322 emit->gs.viewport_index_out_index != INVALID_INDEX) {
1323 file = TGSI_FILE_TEMPORARY;
1324 index = emit->gs.viewport_index_tmp_index;
1325 }
1326 }
1327 else if (emit->unit == MESA_SHADER_FRAGMENT) {
1328 if (sem_name == TGSI_SEMANTIC_POSITION) {
1329 /* Fragment depth output register */
1330 operand0.value = 0;
1331 operand0.operandType = VGPU10_OPERAND_TYPE_OUTPUT_DEPTH;
1332 operand0.indexDimension = VGPU10_OPERAND_INDEX_0D;
1333 operand0.numComponents = VGPU10_OPERAND_1_COMPONENT;
1334 emit_dword(emit, operand0.value);
1335 return;
1336 }
1337 else if (sem_name == TGSI_SEMANTIC_SAMPLEMASK) {
1338 /* Fragment sample mask output */
1339 operand0.value = 0;
1340 operand0.operandType = VGPU10_OPERAND_TYPE_OUTPUT_COVERAGE_MASK;
1341 operand0.indexDimension = VGPU10_OPERAND_INDEX_0D;
1342 operand0.numComponents = VGPU10_OPERAND_1_COMPONENT;
1343 emit_dword(emit, operand0.value);
1344 return;
1345 }
1346 else if (index == emit->fs.color_out_index[0] &&
1347 emit->fs.color_tmp_index != INVALID_INDEX) {
1348 /* replace OUTPUT[COLOR] with TEMP[COLOR]. We need to store the
1349 * fragment color result in a temporary so that we can read it
1350 * it in the post_helper() code.
1351 */
1352 file = TGSI_FILE_TEMPORARY;
1353 index = emit->fs.color_tmp_index;
1354 }
1355 else {
1356 /* Typically, for fragment shaders, the output register index
1357 * matches the color semantic index. But not when we write to
1358 * the fragment depth register. In that case, OUT[0] will be
1359 * fragdepth and OUT[1] will be the 0th color output. We need
1360 * to use the semantic index for color outputs.
1361 */
1362 assert(sem_name == TGSI_SEMANTIC_COLOR);
1363 index = emit->info.output_semantic_index[index];
1364
1365 emit->num_output_writes++;
1366 }
1367 }
1368 else if (emit->unit == MESA_SHADER_TESS_CTRL) {
1369 if (index == emit->tcs.inner.tgsi_index) {
1370 /* replace OUTPUT[TESSLEVEL] with temp. We are storing it
1371 * in temporary for now so that will be store into appropriate
1372 * registers in post_helper() in patch constant phase.
1373 */
1374 if (emit->tcs.control_point_phase) {
1375 /* Discard writing into tessfactor in control point phase */
1376 emit->discard_instruction = true;
1377 }
1378 else {
1379 file = TGSI_FILE_TEMPORARY;
1380 index = emit->tcs.inner.temp_index;
1381 }
1382 }
1383 else if (index == emit->tcs.outer.tgsi_index) {
1384 /* replace OUTPUT[TESSLEVEL] with temp. We are storing it
1385 * in temporary for now so that will be store into appropriate
1386 * registers in post_helper().
1387 */
1388 if (emit->tcs.control_point_phase) {
1389 /* Discard writing into tessfactor in control point phase */
1390 emit->discard_instruction = true;
1391 }
1392 else {
1393 file = TGSI_FILE_TEMPORARY;
1394 index = emit->tcs.outer.temp_index;
1395 }
1396 }
1397 else if (index >= emit->tcs.patch_generic_out_index &&
1398 index < (emit->tcs.patch_generic_out_index +
1399 emit->tcs.patch_generic_out_count)) {
1400 if (emit->tcs.control_point_phase) {
1401 /* Discard writing into generic patch constant outputs in
1402 control point phase */
1403 emit->discard_instruction = true;
1404 }
1405 else {
1406 if (emit->reemit_instruction) {
1407 /* Store results of reemitted instruction in temporary register. */
1408 file = TGSI_FILE_TEMPORARY;
1409 index = emit->tcs.patch_generic_tmp_index +
1410 (index - emit->tcs.patch_generic_out_index);
1411 /**
1412 * Temporaries for patch constant data can be done
1413 * as indexable temporaries.
1414 */
1415 tempArrayId = get_temp_array_id(emit, file, index);
1416 index2d = tempArrayId > 0;
1417
1418 emit->reemit_instruction = false;
1419 }
1420 else {
1421 /* If per-patch outputs is been read in shader, we
1422 * reemit instruction and store results in temporaries in
1423 * patch constant phase. */
1424 if (emit->info.reads_perpatch_outputs) {
1425 emit->reemit_instruction = true;
1426 }
1427 }
1428 }
1429 }
1430 else if (reg->Register.Dimension) {
1431 /* Only control point outputs are declared 2D in tgsi */
1432 if (emit->tcs.control_point_phase) {
1433 if (emit->reemit_instruction) {
1434 /* Store results of reemitted instruction in temporary register. */
1435 index2d = false;
1436 file = TGSI_FILE_TEMPORARY;
1437 index = emit->tcs.control_point_tmp_index +
1438 (index - emit->tcs.control_point_out_index);
1439 emit->reemit_instruction = false;
1440 }
1441 else {
1442 /* The mapped control point outputs are 1-D */
1443 index2d = false;
1444 if (emit->info.reads_pervertex_outputs) {
1445 /* If per-vertex outputs is been read in shader, we
1446 * reemit instruction and store results in temporaries
1447 * control point phase. */
1448 emit->reemit_instruction = true;
1449 }
1450 }
1451
1452 if (sem_name == TGSI_SEMANTIC_CLIPDIST &&
1453 emit->clip_dist_tmp_index != INVALID_INDEX) {
1454 /* replace OUTPUT[CLIPDIST] with TEMP[CLIPDIST].
1455 * We store the clip distance in a temporary first, then
1456 * we'll copy it to the shadow copy and to CLIPDIST with the
1457 * enabled planes mask in emit_clip_distance_instructions().
1458 */
1459 file = TGSI_FILE_TEMPORARY;
1460 index = emit->clip_dist_tmp_index + sem_index;
1461 }
1462 else if (sem_name == TGSI_SEMANTIC_CLIPVERTEX &&
1463 emit->clip_vertex_tmp_index != INVALID_INDEX) {
1464 /* replace the CLIPVERTEX output register with a temporary */
1465 assert(emit->clip_mode == CLIP_VERTEX);
1466 assert(sem_index == 0);
1467 file = TGSI_FILE_TEMPORARY;
1468 index = emit->clip_vertex_tmp_index;
1469 }
1470 }
1471 else {
1472 /* Discard writing into control point outputs in
1473 patch constant phase */
1474 emit->discard_instruction = true;
1475 }
1476 }
1477 }
1478 }
1479
1480 /* init operand tokens to all zero */
1481 operand0.value = 0;
1482
1483 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
1484
1485 /* the operand has a writemask */
1486 operand0.selectionMode = VGPU10_OPERAND_4_COMPONENT_MASK_MODE;
1487
1488 /* Which of the four dest components to write to. Note that we can use a
1489 * simple assignment here since TGSI writemasks match VGPU10 writemasks.
1490 */
1491 STATIC_ASSERT(TGSI_WRITEMASK_X == VGPU10_OPERAND_4_COMPONENT_MASK_X);
1492 operand0.mask = writemask;
1493
1494 /* translate TGSI register file type to VGPU10 operand type */
1495 operand0.operandType = translate_register_file(file, tempArrayId > 0);
1496
1497 check_register_index(emit, operand0.operandType, index);
1498
1499 operand0 = setup_operand0_indexing(emit, operand0, file, indirect,
1500 index2d, false);
1501
1502 /* Emit tokens */
1503 emit_dword(emit, operand0.value);
1504 if (tempArrayId > 0) {
1505 emit_dword(emit, tempArrayId);
1506 }
1507
1508 emit_dword(emit, remap_temp_index(emit, file, index));
1509
1510 if (indirect) {
1511 emit_indirect_register(emit, reg->Indirect.Index);
1512 }
1513 }
1514
1515
1516 /**
1517 * Check if temporary register needs to be initialize when
1518 * shader is not using indirect addressing for temporary and uninitialized
1519 * temporary is not used in loop. In these two scenarios, we cannot
1520 * determine if temporary is initialized or not.
1521 */
1522 static bool
1523 need_temp_reg_initialization(struct svga_shader_emitter_v10 *emit,
1524 unsigned index)
1525 {
1526 if (!(emit->info.indirect_files & (1u << TGSI_FILE_TEMPORARY))
1527 && emit->current_loop_depth == 0) {
1528 if (!emit->temp_map[index].initialized &&
1529 emit->temp_map[index].index < emit->num_shader_temps) {
1530 return true;
1531 }
1532 }
1533
1534 return false;
1535 }
1536
1537
1538 /**
1539 * Translate a src register of a TGSI instruction and emit VGPU10 tokens.
1540 * In quite a few cases, we do register substitution. For example, if
1541 * the TGSI register is the front/back-face register, we replace that with
1542 * a temp register containing a value we computed earlier.
1543 */
1544 static void
1545 emit_src_register(struct svga_shader_emitter_v10 *emit,
1546 const struct tgsi_full_src_register *reg)
1547 {
1548 enum tgsi_file_type file = reg->Register.File;
1549 unsigned index = reg->Register.Index;
1550 bool indirect = reg->Register.Indirect;
1551 unsigned tempArrayId = get_temp_array_id(emit, file, index);
1552 bool index2d = (reg->Register.Dimension ||
1553 tempArrayId > 0 ||
1554 file == TGSI_FILE_CONSTANT);
1555 unsigned index2 = tempArrayId > 0 ? tempArrayId : reg->Dimension.Index;
1556 bool indirect2d = reg->Dimension.Indirect;
1557 unsigned swizzleX = reg->Register.SwizzleX;
1558 unsigned swizzleY = reg->Register.SwizzleY;
1559 unsigned swizzleZ = reg->Register.SwizzleZ;
1560 unsigned swizzleW = reg->Register.SwizzleW;
1561 const bool absolute = reg->Register.Absolute;
1562 const bool negate = reg->Register.Negate;
1563 VGPU10OperandToken0 operand0;
1564 VGPU10OperandToken1 operand1;
1565
1566 operand0.value = operand1.value = 0;
1567
1568 if (emit->unit == MESA_SHADER_FRAGMENT){
1569 if (file == TGSI_FILE_INPUT) {
1570 if (index == emit->fs.face_input_index) {
1571 /* Replace INPUT[FACE] with TEMP[FACE] */
1572 file = TGSI_FILE_TEMPORARY;
1573 index = emit->fs.face_tmp_index;
1574 }
1575 else if (index == emit->fs.fragcoord_input_index) {
1576 /* Replace INPUT[POSITION] with TEMP[POSITION] */
1577 file = TGSI_FILE_TEMPORARY;
1578 index = emit->fs.fragcoord_tmp_index;
1579 }
1580 else if (index == emit->fs.layer_input_index) {
1581 /* Replace INPUT[LAYER] with zero.x */
1582 file = TGSI_FILE_IMMEDIATE;
1583 index = emit->fs.layer_imm_index;
1584 swizzleX = swizzleY = swizzleZ = swizzleW = TGSI_SWIZZLE_X;
1585 }
1586 else {
1587 /* We remap fragment shader inputs to that FS input indexes
1588 * match up with VS/GS output indexes.
1589 */
1590 index = emit->linkage.input_map[index];
1591 }
1592 }
1593 else if (file == TGSI_FILE_SYSTEM_VALUE) {
1594 if (index == emit->fs.sample_pos_sys_index) {
1595 assert(emit->version >= 41);
1596 /* Current sample position is in a temp register */
1597 file = TGSI_FILE_TEMPORARY;
1598 index = emit->fs.sample_pos_tmp_index;
1599 }
1600 else if (index == emit->fs.sample_mask_in_sys_index) {
1601 /* Emitted as vCoverage0.x */
1602 /* According to GLSL spec, the gl_SampleMaskIn array has ceil(s / 32)
1603 * elements where s is the maximum number of color samples supported
1604 * by the implementation.
1605 */
1606 operand0.value = 0;
1607 operand0.operandType = VGPU10_OPERAND_TYPE_INPUT_COVERAGE_MASK;
1608 operand0.indexDimension = VGPU10_OPERAND_INDEX_0D;
1609 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
1610 operand0.selectionMode = VGPU10_OPERAND_4_COMPONENT_SELECT_1_MODE;
1611 emit_dword(emit, operand0.value);
1612 return;
1613 }
1614 else {
1615 /* Map the TGSI system value to a VGPU10 input register */
1616 assert(index < ARRAY_SIZE(emit->system_value_indexes));
1617 file = TGSI_FILE_INPUT;
1618 index = emit->system_value_indexes[index];
1619 }
1620 }
1621 }
1622 else if (emit->unit == MESA_SHADER_GEOMETRY) {
1623 if (file == TGSI_FILE_INPUT) {
1624 if (index == emit->gs.prim_id_index) {
1625 operand0.numComponents = VGPU10_OPERAND_0_COMPONENT;
1626 operand0.operandType = VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID;
1627 }
1628 index = emit->linkage.input_map[index];
1629 }
1630 else if (file == TGSI_FILE_SYSTEM_VALUE &&
1631 index == emit->gs.invocation_id_sys_index) {
1632 /* Emitted as vGSInstanceID0.x */
1633 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
1634 operand0.operandType = VGPU10_OPERAND_TYPE_INPUT_GS_INSTANCE_ID;
1635 index = 0;
1636 }
1637 }
1638 else if (emit->unit == MESA_SHADER_VERTEX) {
1639 if (file == TGSI_FILE_INPUT) {
1640 /* if input is adjusted... */
1641 if ((emit->key.vs.adjust_attrib_w_1 |
1642 emit->key.vs.adjust_attrib_itof |
1643 emit->key.vs.adjust_attrib_utof |
1644 emit->key.vs.attrib_is_bgra |
1645 emit->key.vs.attrib_puint_to_snorm |
1646 emit->key.vs.attrib_puint_to_uscaled |
1647 emit->key.vs.attrib_puint_to_sscaled) & (1 << index)) {
1648 file = TGSI_FILE_TEMPORARY;
1649 index = emit->vs.adjusted_input[index];
1650 }
1651 }
1652 else if (file == TGSI_FILE_SYSTEM_VALUE) {
1653 if (index == emit->vs.vertex_id_sys_index &&
1654 emit->vs.vertex_id_tmp_index != INVALID_INDEX) {
1655 file = TGSI_FILE_TEMPORARY;
1656 index = emit->vs.vertex_id_tmp_index;
1657 swizzleX = swizzleY = swizzleZ = swizzleW = TGSI_SWIZZLE_X;
1658 }
1659 else {
1660 /* Map the TGSI system value to a VGPU10 input register */
1661 assert(index < ARRAY_SIZE(emit->system_value_indexes));
1662 file = TGSI_FILE_INPUT;
1663 index = emit->system_value_indexes[index];
1664 }
1665 }
1666 }
1667 else if (emit->unit == MESA_SHADER_TESS_CTRL) {
1668
1669 if (file == TGSI_FILE_SYSTEM_VALUE) {
1670 if (index == emit->tcs.vertices_per_patch_index) {
1671 /**
1672 * if source register is the system value for vertices_per_patch,
1673 * replace it with the immediate.
1674 */
1675 file = TGSI_FILE_IMMEDIATE;
1676 index = emit->tcs.imm_index;
1677 swizzleX = swizzleY = swizzleZ = swizzleW = TGSI_SWIZZLE_X;
1678 }
1679 else if (index == emit->tcs.invocation_id_sys_index) {
1680 if (emit->tcs.control_point_phase) {
1681 /**
1682 * Emitted as vOutputControlPointID.x
1683 */
1684 operand0.numComponents = VGPU10_OPERAND_1_COMPONENT;
1685 operand0.operandType = VGPU10_OPERAND_TYPE_OUTPUT_CONTROL_POINT_ID;
1686 operand0.selectionMode = VGPU10_OPERAND_4_COMPONENT_MASK_MODE;
1687 operand0.mask = 0;
1688 emit_dword(emit, operand0.value);
1689 return;
1690 }
1691 else {
1692 /* There is no control point ID input declaration in
1693 * the patch constant phase in hull shader.
1694 * Since for now we are emitting all instructions in
1695 * the patch constant phase, we are replacing the
1696 * control point ID reference with the immediate 0.
1697 */
1698 file = TGSI_FILE_IMMEDIATE;
1699 index = emit->tcs.imm_index;
1700 swizzleX = swizzleY = swizzleZ = swizzleW = TGSI_SWIZZLE_W;
1701 }
1702 }
1703 else if (index == emit->tcs.prim_id_index) {
1704 /**
1705 * Emitted as vPrim.x
1706 */
1707 operand0.numComponents = VGPU10_OPERAND_1_COMPONENT;
1708 operand0.operandType = VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID;
1709 index = 0;
1710 }
1711 }
1712 else if (file == TGSI_FILE_INPUT) {
1713 index = emit->linkage.input_map[index];
1714 if (!emit->tcs.control_point_phase) {
1715 /* Emitted as vicp */
1716 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
1717 operand0.operandType = VGPU10_OPERAND_TYPE_INPUT_CONTROL_POINT;
1718 assert(reg->Register.Dimension);
1719 }
1720 }
1721 else if (file == TGSI_FILE_OUTPUT) {
1722 if ((index >= emit->tcs.patch_generic_out_index &&
1723 index < (emit->tcs.patch_generic_out_index +
1724 emit->tcs.patch_generic_out_count)) ||
1725 index == emit->tcs.inner.tgsi_index ||
1726 index == emit->tcs.outer.tgsi_index) {
1727 if (emit->tcs.control_point_phase) {
1728 emit->discard_instruction = true;
1729 }
1730 else {
1731 /* Device doesn't allow reading from output so
1732 * use corresponding temporary register as source */
1733 file = TGSI_FILE_TEMPORARY;
1734 if (index == emit->tcs.inner.tgsi_index) {
1735 index = emit->tcs.inner.temp_index;
1736 }
1737 else if (index == emit->tcs.outer.tgsi_index) {
1738 index = emit->tcs.outer.temp_index;
1739 }
1740 else {
1741 index = emit->tcs.patch_generic_tmp_index +
1742 (index - emit->tcs.patch_generic_out_index);
1743 }
1744
1745 /**
1746 * Temporaries for patch constant data can be done
1747 * as indexable temporaries.
1748 */
1749 tempArrayId = get_temp_array_id(emit, file, index);
1750 index2d = tempArrayId > 0;
1751 index2 = tempArrayId > 0 ? tempArrayId : reg->Dimension.Index;
1752 }
1753 }
1754 else if (index2d) {
1755 if (emit->tcs.control_point_phase) {
1756 /* Device doesn't allow reading from output so
1757 * use corresponding temporary register as source */
1758 file = TGSI_FILE_TEMPORARY;
1759 index2d = false;
1760 index = emit->tcs.control_point_tmp_index +
1761 (index - emit->tcs.control_point_out_index);
1762 }
1763 else {
1764 emit->discard_instruction = true;
1765 }
1766 }
1767 }
1768 }
1769 else if (emit->unit == MESA_SHADER_TESS_EVAL) {
1770 if (file == TGSI_FILE_SYSTEM_VALUE) {
1771 if (index == emit->tes.tesscoord_sys_index) {
1772 /**
1773 * Emitted as vDomain
1774 */
1775 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
1776 operand0.operandType = VGPU10_OPERAND_TYPE_INPUT_DOMAIN_POINT;
1777 index = 0;
1778
1779 /* Make sure swizzles are of those components allowed according
1780 * to the tessellator domain.
1781 */
1782 swizzleX = MIN2(swizzleX, emit->tes.swizzle_max);
1783 swizzleY = MIN2(swizzleY, emit->tes.swizzle_max);
1784 swizzleZ = MIN2(swizzleZ, emit->tes.swizzle_max);
1785 swizzleW = MIN2(swizzleW, emit->tes.swizzle_max);
1786 }
1787 else if (index == emit->tes.inner.tgsi_index) {
1788 file = TGSI_FILE_TEMPORARY;
1789 index = emit->tes.inner.temp_index;
1790 }
1791 else if (index == emit->tes.outer.tgsi_index) {
1792 file = TGSI_FILE_TEMPORARY;
1793 index = emit->tes.outer.temp_index;
1794 }
1795 else if (index == emit->tes.prim_id_index) {
1796 /**
1797 * Emitted as vPrim.x
1798 */
1799 operand0.numComponents = VGPU10_OPERAND_1_COMPONENT;
1800 operand0.operandType = VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID;
1801 index = 0;
1802 }
1803
1804 }
1805 else if (file == TGSI_FILE_INPUT) {
1806 if (index2d) {
1807 /* 2D input is emitted as vcp (input control point). */
1808 operand0.operandType = VGPU10_OPERAND_TYPE_INPUT_CONTROL_POINT;
1809 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
1810
1811 /* index specifies the element index and is remapped
1812 * to align with the tcs output index.
1813 */
1814 index = emit->linkage.input_map[index];
1815
1816 assert(index2 < emit->key.tes.vertices_per_patch);
1817 }
1818 else {
1819 if (index < emit->key.tes.tessfactor_index)
1820 /* index specifies the generic patch index.
1821 * Remapped to match up with the tcs output index.
1822 */
1823 index = emit->linkage.input_map[index];
1824
1825 operand0.operandType = VGPU10_OPERAND_TYPE_INPUT_PATCH_CONSTANT;
1826 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
1827 }
1828 }
1829 }
1830 else if (emit->unit == MESA_SHADER_COMPUTE) {
1831 if (file == TGSI_FILE_SYSTEM_VALUE) {
1832 if (index == emit->cs.thread_id_index) {
1833 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
1834 operand0.operandType = VGPU10_OPERAND_TYPE_INPUT_THREAD_ID_IN_GROUP;
1835 index = 0;
1836 } else if (index == emit->cs.block_id_index) {
1837 operand0.value = 0;
1838 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
1839 operand0.operandType = VGPU10_OPERAND_TYPE_INPUT_THREAD_GROUP_ID;
1840 operand0.indexDimension = VGPU10_OPERAND_INDEX_0D;
1841 operand0.selectionMode = VGPU10_OPERAND_4_COMPONENT_SWIZZLE_MODE;
1842 operand0.swizzleX = swizzleX;
1843 operand0.swizzleY = swizzleY;
1844 operand0.swizzleZ = swizzleZ;
1845 operand0.swizzleW = swizzleW;
1846 emit_dword(emit, operand0.value);
1847 return;
1848 } else if (index == emit->cs.grid_size.tgsi_index) {
1849 file = TGSI_FILE_IMMEDIATE;
1850 index = emit->cs.grid_size.imm_index;
1851 }
1852 }
1853 }
1854
1855 if (file == TGSI_FILE_ADDRESS) {
1856 index = emit->address_reg_index[index];
1857 file = TGSI_FILE_TEMPORARY;
1858 }
1859
1860 if (file == TGSI_FILE_CONSTANT) {
1861 /**
1862 * If this constant buffer is to be bound as srv raw buffer,
1863 * then we have to load the constant to a temp first before
1864 * it can be used as a source in the instruction.
1865 * This is accomplished in two passes. The first pass is to
1866 * identify if there is any constbuf to rawbuf translation.
1867 * If there isn't, emit the instruction as usual.
1868 * If there is, then we save the constant buffer reference info,
1869 * and then instead of emitting the instruction at the end
1870 * of the instruction, it will trigger a second pass of parsing
1871 * this instruction. Before it starts the parsing, it will
1872 * load the referenced raw buffer elements to temporaries.
1873 * Then it will emit the instruction that replaces the
1874 * constant buffer replaces with the corresponding temporaries.
1875 */
1876 if (emit->raw_bufs & (1 << index2)) {
1877 if (emit->reemit_rawbuf_instruction != REEMIT_IN_PROGRESS) {
1878 unsigned tmpIdx = emit->raw_buf_cur_tmp_index;
1879
1880 emit->raw_buf_tmp[tmpIdx].buffer_index = index2;
1881
1882 /* Save whether the element index is indirect indexing */
1883 emit->raw_buf_tmp[tmpIdx].indirect = indirect;
1884
1885 /* If it is indirect index, save the temporary
1886 * address index, otherwise, save the immediate index.
1887 */
1888 if (indirect) {
1889 emit->raw_buf_tmp[tmpIdx].element_index =
1890 emit->address_reg_index[reg->Indirect.Index];
1891 emit->raw_buf_tmp[tmpIdx].element_rel =
1892 reg->Register.Index;
1893 }
1894 else {
1895 emit->raw_buf_tmp[tmpIdx].element_index = index;
1896 emit->raw_buf_tmp[tmpIdx].element_rel = 0;
1897 }
1898
1899 emit->raw_buf_cur_tmp_index++;
1900 emit->reemit_rawbuf_instruction = REEMIT_TRUE;
1901 emit->discard_instruction = true;
1902 emit->reemit_tgsi_instruction = true;
1903 }
1904 else {
1905 /* In the reemitting process, replace the constant buffer
1906 * reference with temporary.
1907 */
1908 file = TGSI_FILE_TEMPORARY;
1909 index = emit->raw_buf_cur_tmp_index + emit->raw_buf_tmp_index;
1910 index2d = false;
1911 indirect = false;
1912 emit->raw_buf_cur_tmp_index++;
1913 }
1914 }
1915 }
1916
1917 if (file == TGSI_FILE_TEMPORARY) {
1918 if (need_temp_reg_initialization(emit, index)) {
1919 emit->initialize_temp_index = index;
1920 emit->discard_instruction = true;
1921 }
1922 }
1923
1924 if (operand0.value == 0) {
1925 /* if operand0 was not set above for a special case, do the general
1926 * case now.
1927 */
1928 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
1929 operand0.operandType = translate_register_file(file, tempArrayId > 0);
1930 }
1931 operand0 = setup_operand0_indexing(emit, operand0, file, indirect,
1932 index2d, indirect2d);
1933
1934 if (operand0.operandType != VGPU10_OPERAND_TYPE_IMMEDIATE32 &&
1935 operand0.operandType != VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID) {
1936 /* there's no swizzle for in-line immediates */
1937 if (swizzleX == swizzleY &&
1938 swizzleX == swizzleZ &&
1939 swizzleX == swizzleW) {
1940 operand0.selectionMode = VGPU10_OPERAND_4_COMPONENT_SELECT_1_MODE;
1941 }
1942 else {
1943 operand0.selectionMode = VGPU10_OPERAND_4_COMPONENT_SWIZZLE_MODE;
1944 }
1945
1946 operand0.swizzleX = swizzleX;
1947 operand0.swizzleY = swizzleY;
1948 operand0.swizzleZ = swizzleZ;
1949 operand0.swizzleW = swizzleW;
1950
1951 if (absolute || negate) {
1952 operand0.extended = 1;
1953 operand1.extendedOperandType = VGPU10_EXTENDED_OPERAND_MODIFIER;
1954 if (absolute && !negate)
1955 operand1.operandModifier = VGPU10_OPERAND_MODIFIER_ABS;
1956 if (!absolute && negate)
1957 operand1.operandModifier = VGPU10_OPERAND_MODIFIER_NEG;
1958 if (absolute && negate)
1959 operand1.operandModifier = VGPU10_OPERAND_MODIFIER_ABSNEG;
1960 }
1961 }
1962
1963 check_register_index(emit, operand0.operandType, index);
1964
1965 /* Emit the operand tokens */
1966 emit_dword(emit, operand0.value);
1967 if (operand0.extended)
1968 emit_dword(emit, operand1.value);
1969
1970 if (operand0.operandType == VGPU10_OPERAND_TYPE_IMMEDIATE32) {
1971 /* Emit the four float/int in-line immediate values */
1972 unsigned *c;
1973 assert(index < ARRAY_SIZE(emit->immediates));
1974 assert(file == TGSI_FILE_IMMEDIATE);
1975 assert(swizzleX < 4);
1976 assert(swizzleY < 4);
1977 assert(swizzleZ < 4);
1978 assert(swizzleW < 4);
1979 c = (unsigned *) emit->immediates[index];
1980 emit_dword(emit, c[swizzleX]);
1981 emit_dword(emit, c[swizzleY]);
1982 emit_dword(emit, c[swizzleZ]);
1983 emit_dword(emit, c[swizzleW]);
1984 }
1985 else if (operand0.indexDimension >= VGPU10_OPERAND_INDEX_1D) {
1986 /* Emit the register index(es) */
1987 if (index2d) {
1988 emit_dword(emit, index2);
1989
1990 if (indirect2d) {
1991 emit_indirect_register(emit, reg->DimIndirect.Index);
1992 }
1993 }
1994
1995 emit_dword(emit, remap_temp_index(emit, file, index));
1996
1997 if (indirect) {
1998 assert(operand0.operandType != VGPU10_OPERAND_TYPE_TEMP);
1999 emit_indirect_register(emit, reg->Indirect.Index);
2000 }
2001 }
2002 }
2003
2004
2005 /**
2006 * Emit a resource operand (for use with a SAMPLE instruction).
2007 */
2008 static void
2009 emit_resource_register(struct svga_shader_emitter_v10 *emit,
2010 unsigned resource_number)
2011 {
2012 VGPU10OperandToken0 operand0;
2013
2014 check_register_index(emit, VGPU10_OPERAND_TYPE_RESOURCE, resource_number);
2015
2016 /* init */
2017 operand0.value = 0;
2018
2019 operand0.operandType = VGPU10_OPERAND_TYPE_RESOURCE;
2020 operand0.indexDimension = VGPU10_OPERAND_INDEX_1D;
2021 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
2022 operand0.selectionMode = VGPU10_OPERAND_4_COMPONENT_SWIZZLE_MODE;
2023 operand0.swizzleX = VGPU10_COMPONENT_X;
2024 operand0.swizzleY = VGPU10_COMPONENT_Y;
2025 operand0.swizzleZ = VGPU10_COMPONENT_Z;
2026 operand0.swizzleW = VGPU10_COMPONENT_W;
2027
2028 emit_dword(emit, operand0.value);
2029 emit_dword(emit, resource_number);
2030 }
2031
2032
2033 /**
2034 * Emit a sampler operand (for use with a SAMPLE instruction).
2035 */
2036 static void
2037 emit_sampler_register(struct svga_shader_emitter_v10 *emit,
2038 unsigned unit)
2039 {
2040 VGPU10OperandToken0 operand0;
2041 unsigned sampler_number;
2042
2043 sampler_number = emit->key.tex[unit].sampler_index;
2044
2045 if ((emit->shadow_compare_units & (1 << unit)) && emit->use_sampler_state_mapping)
2046 sampler_number++;
2047
2048 check_register_index(emit, VGPU10_OPERAND_TYPE_SAMPLER, sampler_number);
2049
2050 /* init */
2051 operand0.value = 0;
2052
2053 operand0.operandType = VGPU10_OPERAND_TYPE_SAMPLER;
2054 operand0.indexDimension = VGPU10_OPERAND_INDEX_1D;
2055
2056 emit_dword(emit, operand0.value);
2057 emit_dword(emit, sampler_number);
2058 }
2059
2060
2061 /**
2062 * Emit an operand which reads the IS_FRONT_FACING register.
2063 */
2064 static void
2065 emit_face_register(struct svga_shader_emitter_v10 *emit)
2066 {
2067 VGPU10OperandToken0 operand0;
2068 unsigned index = emit->linkage.input_map[emit->fs.face_input_index];
2069
2070 /* init */
2071 operand0.value = 0;
2072
2073 operand0.operandType = VGPU10_OPERAND_TYPE_INPUT;
2074 operand0.indexDimension = VGPU10_OPERAND_INDEX_1D;
2075 operand0.selectionMode = VGPU10_OPERAND_4_COMPONENT_SELECT_1_MODE;
2076 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
2077
2078 operand0.swizzleX = VGPU10_COMPONENT_X;
2079 operand0.swizzleY = VGPU10_COMPONENT_X;
2080 operand0.swizzleZ = VGPU10_COMPONENT_X;
2081 operand0.swizzleW = VGPU10_COMPONENT_X;
2082
2083 emit_dword(emit, operand0.value);
2084 emit_dword(emit, index);
2085 }
2086
2087
2088 /**
2089 * Emit tokens for the "rasterizer" register used by the SAMPLE_POS
2090 * instruction.
2091 */
2092 static void
2093 emit_rasterizer_register(struct svga_shader_emitter_v10 *emit)
2094 {
2095 VGPU10OperandToken0 operand0;
2096
2097 /* init */
2098 operand0.value = 0;
2099
2100 /* No register index for rasterizer index (there's only one) */
2101 operand0.operandType = VGPU10_OPERAND_TYPE_RASTERIZER;
2102 operand0.indexDimension = VGPU10_OPERAND_INDEX_0D;
2103 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
2104 operand0.selectionMode = VGPU10_OPERAND_4_COMPONENT_SWIZZLE_MODE;
2105 operand0.swizzleX = VGPU10_COMPONENT_X;
2106 operand0.swizzleY = VGPU10_COMPONENT_Y;
2107 operand0.swizzleZ = VGPU10_COMPONENT_Z;
2108 operand0.swizzleW = VGPU10_COMPONENT_W;
2109
2110 emit_dword(emit, operand0.value);
2111 }
2112
2113
2114 /**
2115 * Emit tokens for the "stream" register used by the
2116 * DCL_STREAM, CUT_STREAM, EMIT_STREAM instructions.
2117 */
2118 static void
2119 emit_stream_register(struct svga_shader_emitter_v10 *emit, unsigned index)
2120 {
2121 VGPU10OperandToken0 operand0;
2122
2123 /* init */
2124 operand0.value = 0;
2125
2126 /* No register index for rasterizer index (there's only one) */
2127 operand0.operandType = VGPU10_OPERAND_TYPE_STREAM;
2128 operand0.indexDimension = VGPU10_OPERAND_INDEX_1D;
2129 operand0.numComponents = VGPU10_OPERAND_0_COMPONENT;
2130
2131 emit_dword(emit, operand0.value);
2132 emit_dword(emit, index);
2133 }
2134
2135
2136 /**
2137 * Emit the token for a VGPU10 opcode, with precise parameter.
2138 * \param saturate clamp result to [0,1]?
2139 */
2140 static void
2141 emit_opcode_precise(struct svga_shader_emitter_v10 *emit,
2142 unsigned vgpu10_opcode, bool saturate, bool precise)
2143 {
2144 VGPU10OpcodeToken0 token0;
2145
2146 token0.value = 0; /* init all fields to zero */
2147 token0.opcodeType = vgpu10_opcode;
2148 token0.instructionLength = 0; /* Filled in by end_emit_instruction() */
2149 token0.saturate = saturate;
2150
2151 /* Mesa's GLSL IR -> TGSI translator will set the TGSI precise flag for
2152 * 'invariant' declarations. Only set preciseValues=1 if we have SM5.
2153 */
2154 token0.preciseValues = precise && emit->version >= 50;
2155
2156 emit_dword(emit, token0.value);
2157
2158 emit->uses_precise_qualifier |= token0.preciseValues;
2159 }
2160
2161
2162 /**
2163 * Emit the token for a VGPU10 opcode.
2164 * \param saturate clamp result to [0,1]?
2165 */
2166 static void
2167 emit_opcode(struct svga_shader_emitter_v10 *emit,
2168 unsigned vgpu10_opcode, bool saturate)
2169 {
2170 emit_opcode_precise(emit, vgpu10_opcode, saturate, false);
2171 }
2172
2173
2174 /**
2175 * Emit the token for a VGPU10 resinfo instruction.
2176 * \param modifier return type modifier, _uint or _rcpFloat.
2177 * TODO: We may want to remove this parameter if it will
2178 * only ever be used as _uint.
2179 */
2180 static void
2181 emit_opcode_resinfo(struct svga_shader_emitter_v10 *emit,
2182 VGPU10_RESINFO_RETURN_TYPE modifier)
2183 {
2184 VGPU10OpcodeToken0 token0;
2185
2186 token0.value = 0; /* init all fields to zero */
2187 token0.opcodeType = VGPU10_OPCODE_RESINFO;
2188 token0.instructionLength = 0; /* Filled in by end_emit_instruction() */
2189 token0.resinfoReturnType = modifier;
2190
2191 emit_dword(emit, token0.value);
2192 }
2193
2194
2195 /**
2196 * Emit opcode tokens for a texture sample instruction. Texture instructions
2197 * can be rather complicated (texel offsets, etc) so we have this specialized
2198 * function.
2199 */
2200 static void
2201 emit_sample_opcode(struct svga_shader_emitter_v10 *emit,
2202 unsigned vgpu10_opcode, bool saturate,
2203 const int offsets[3])
2204 {
2205 VGPU10OpcodeToken0 token0;
2206 VGPU10OpcodeToken1 token1;
2207
2208 token0.value = 0; /* init all fields to zero */
2209 token0.opcodeType = vgpu10_opcode;
2210 token0.instructionLength = 0; /* Filled in by end_emit_instruction() */
2211 token0.saturate = saturate;
2212
2213 if (offsets[0] || offsets[1] || offsets[2]) {
2214 assert(offsets[0] >= VGPU10_MIN_TEXEL_FETCH_OFFSET);
2215 assert(offsets[1] >= VGPU10_MIN_TEXEL_FETCH_OFFSET);
2216 assert(offsets[2] >= VGPU10_MIN_TEXEL_FETCH_OFFSET);
2217 assert(offsets[0] <= VGPU10_MAX_TEXEL_FETCH_OFFSET);
2218 assert(offsets[1] <= VGPU10_MAX_TEXEL_FETCH_OFFSET);
2219 assert(offsets[2] <= VGPU10_MAX_TEXEL_FETCH_OFFSET);
2220
2221 token0.extended = 1;
2222 token1.value = 0;
2223 token1.opcodeType = VGPU10_EXTENDED_OPCODE_SAMPLE_CONTROLS;
2224 token1.offsetU = offsets[0];
2225 token1.offsetV = offsets[1];
2226 token1.offsetW = offsets[2];
2227 }
2228
2229 emit_dword(emit, token0.value);
2230 if (token0.extended) {
2231 emit_dword(emit, token1.value);
2232 }
2233 }
2234
2235
2236 /**
2237 * Emit a DISCARD opcode token.
2238 * If nonzero is set, we'll discard the fragment if the X component is not 0.
2239 * Otherwise, we'll discard the fragment if the X component is 0.
2240 */
2241 static void
2242 emit_discard_opcode(struct svga_shader_emitter_v10 *emit, bool nonzero)
2243 {
2244 VGPU10OpcodeToken0 opcode0;
2245
2246 opcode0.value = 0;
2247 opcode0.opcodeType = VGPU10_OPCODE_DISCARD;
2248 if (nonzero)
2249 opcode0.testBoolean = VGPU10_INSTRUCTION_TEST_NONZERO;
2250
2251 emit_dword(emit, opcode0.value);
2252 }
2253
2254
2255 /**
2256 * We need to call this before we begin emitting a VGPU10 instruction.
2257 */
2258 static void
2259 begin_emit_instruction(struct svga_shader_emitter_v10 *emit)
2260 {
2261 assert(emit->inst_start_token == 0);
2262 /* Save location of the instruction's VGPU10OpcodeToken0 token.
2263 * Note, we can't save a pointer because it would become invalid if
2264 * we have to realloc the output buffer.
2265 */
2266 emit->inst_start_token = emit_get_num_tokens(emit);
2267 }
2268
2269
2270 /**
2271 * We need to call this after we emit the last token of a VGPU10 instruction.
2272 * This function patches in the opcode token's instructionLength field.
2273 */
2274 static void
2275 end_emit_instruction(struct svga_shader_emitter_v10 *emit)
2276 {
2277 VGPU10OpcodeToken0 *tokens = (VGPU10OpcodeToken0 *) emit->buf;
2278 unsigned inst_length;
2279
2280 assert(emit->inst_start_token > 0);
2281
2282 if (emit->discard_instruction) {
2283 /* Back up the emit->ptr to where this instruction started so
2284 * that we discard the current instruction.
2285 */
2286 emit->ptr = (char *) (tokens + emit->inst_start_token);
2287 }
2288 else {
2289 /* Compute instruction length and patch that into the start of
2290 * the instruction.
2291 */
2292 inst_length = emit_get_num_tokens(emit) - emit->inst_start_token;
2293
2294 assert(inst_length > 0);
2295
2296 tokens[emit->inst_start_token].instructionLength = inst_length;
2297 }
2298
2299 emit->inst_start_token = 0; /* reset to zero for error checking */
2300 emit->discard_instruction = false;
2301 }
2302
2303
2304 /**
2305 * Return index for a free temporary register.
2306 */
2307 static unsigned
2308 get_temp_index(struct svga_shader_emitter_v10 *emit)
2309 {
2310 assert(emit->internal_temp_count < MAX_INTERNAL_TEMPS);
2311 return emit->num_shader_temps + emit->internal_temp_count++;
2312 }
2313
2314
2315 /**
2316 * Release the temporaries which were generated by get_temp_index().
2317 */
2318 static void
2319 free_temp_indexes(struct svga_shader_emitter_v10 *emit)
2320 {
2321 emit->internal_temp_count = 0;
2322 }
2323
2324
2325 /**
2326 * Create a tgsi_full_src_register.
2327 */
2328 static struct tgsi_full_src_register
2329 make_src_reg(enum tgsi_file_type file, unsigned index)
2330 {
2331 struct tgsi_full_src_register reg;
2332
2333 memset(&reg, 0, sizeof(reg));
2334 reg.Register.File = file;
2335 reg.Register.Index = index;
2336 reg.Register.SwizzleX = TGSI_SWIZZLE_X;
2337 reg.Register.SwizzleY = TGSI_SWIZZLE_Y;
2338 reg.Register.SwizzleZ = TGSI_SWIZZLE_Z;
2339 reg.Register.SwizzleW = TGSI_SWIZZLE_W;
2340 return reg;
2341 }
2342
2343
2344 /**
2345 * Create a tgsi_full_src_register with a swizzle such that all four
2346 * vector components have the same scalar value.
2347 */
2348 static struct tgsi_full_src_register
2349 make_src_scalar_reg(enum tgsi_file_type file, unsigned index, unsigned component)
2350 {
2351 struct tgsi_full_src_register reg;
2352
2353 assert(component >= TGSI_SWIZZLE_X);
2354 assert(component <= TGSI_SWIZZLE_W);
2355
2356 memset(&reg, 0, sizeof(reg));
2357 reg.Register.File = file;
2358 reg.Register.Index = index;
2359 reg.Register.SwizzleX =
2360 reg.Register.SwizzleY =
2361 reg.Register.SwizzleZ =
2362 reg.Register.SwizzleW = component;
2363 return reg;
2364 }
2365
2366
2367 /**
2368 * Create a tgsi_full_src_register for a temporary.
2369 */
2370 static struct tgsi_full_src_register
2371 make_src_temp_reg(unsigned index)
2372 {
2373 return make_src_reg(TGSI_FILE_TEMPORARY, index);
2374 }
2375
2376
2377 /**
2378 * Create a tgsi_full_src_register for a constant.
2379 */
2380 static struct tgsi_full_src_register
2381 make_src_const_reg(unsigned index)
2382 {
2383 return make_src_reg(TGSI_FILE_CONSTANT, index);
2384 }
2385
2386
2387 /**
2388 * Create a tgsi_full_src_register for an immediate constant.
2389 */
2390 static struct tgsi_full_src_register
2391 make_src_immediate_reg(unsigned index)
2392 {
2393 return make_src_reg(TGSI_FILE_IMMEDIATE, index);
2394 }
2395
2396
2397 /**
2398 * Create a tgsi_full_dst_register.
2399 */
2400 static struct tgsi_full_dst_register
2401 make_dst_reg(enum tgsi_file_type file, unsigned index)
2402 {
2403 struct tgsi_full_dst_register reg;
2404
2405 memset(&reg, 0, sizeof(reg));
2406 reg.Register.File = file;
2407 reg.Register.Index = index;
2408 reg.Register.WriteMask = TGSI_WRITEMASK_XYZW;
2409 return reg;
2410 }
2411
2412
2413 /**
2414 * Create a tgsi_full_dst_register for a temporary.
2415 */
2416 static struct tgsi_full_dst_register
2417 make_dst_temp_reg(unsigned index)
2418 {
2419 return make_dst_reg(TGSI_FILE_TEMPORARY, index);
2420 }
2421
2422
2423 /**
2424 * Create a tgsi_full_dst_register for an output.
2425 */
2426 static struct tgsi_full_dst_register
2427 make_dst_output_reg(unsigned index)
2428 {
2429 return make_dst_reg(TGSI_FILE_OUTPUT, index);
2430 }
2431
2432
2433 /**
2434 * Create negated tgsi_full_src_register.
2435 */
2436 static struct tgsi_full_src_register
2437 negate_src(const struct tgsi_full_src_register *reg)
2438 {
2439 struct tgsi_full_src_register neg = *reg;
2440 neg.Register.Negate = !reg->Register.Negate;
2441 return neg;
2442 }
2443
2444 /**
2445 * Create absolute value of a tgsi_full_src_register.
2446 */
2447 static struct tgsi_full_src_register
2448 absolute_src(const struct tgsi_full_src_register *reg)
2449 {
2450 struct tgsi_full_src_register absolute = *reg;
2451 absolute.Register.Absolute = 1;
2452 return absolute;
2453 }
2454
2455
2456 /** Return the named swizzle term from the src register */
2457 static inline unsigned
2458 get_swizzle(const struct tgsi_full_src_register *reg, enum tgsi_swizzle term)
2459 {
2460 switch (term) {
2461 case TGSI_SWIZZLE_X:
2462 return reg->Register.SwizzleX;
2463 case TGSI_SWIZZLE_Y:
2464 return reg->Register.SwizzleY;
2465 case TGSI_SWIZZLE_Z:
2466 return reg->Register.SwizzleZ;
2467 case TGSI_SWIZZLE_W:
2468 return reg->Register.SwizzleW;
2469 default:
2470 assert(!"Bad swizzle");
2471 return TGSI_SWIZZLE_X;
2472 }
2473 }
2474
2475
2476 /**
2477 * Create swizzled tgsi_full_src_register.
2478 */
2479 static struct tgsi_full_src_register
2480 swizzle_src(const struct tgsi_full_src_register *reg,
2481 enum tgsi_swizzle swizzleX, enum tgsi_swizzle swizzleY,
2482 enum tgsi_swizzle swizzleZ, enum tgsi_swizzle swizzleW)
2483 {
2484 struct tgsi_full_src_register swizzled = *reg;
2485 /* Note: we swizzle the current swizzle */
2486 swizzled.Register.SwizzleX = get_swizzle(reg, swizzleX);
2487 swizzled.Register.SwizzleY = get_swizzle(reg, swizzleY);
2488 swizzled.Register.SwizzleZ = get_swizzle(reg, swizzleZ);
2489 swizzled.Register.SwizzleW = get_swizzle(reg, swizzleW);
2490 return swizzled;
2491 }
2492
2493
2494 /**
2495 * Create swizzled tgsi_full_src_register where all the swizzle
2496 * terms are the same.
2497 */
2498 static struct tgsi_full_src_register
2499 scalar_src(const struct tgsi_full_src_register *reg, enum tgsi_swizzle swizzle)
2500 {
2501 struct tgsi_full_src_register swizzled = *reg;
2502 /* Note: we swizzle the current swizzle */
2503 swizzled.Register.SwizzleX =
2504 swizzled.Register.SwizzleY =
2505 swizzled.Register.SwizzleZ =
2506 swizzled.Register.SwizzleW = get_swizzle(reg, swizzle);
2507 return swizzled;
2508 }
2509
2510
2511 /**
2512 * Create new tgsi_full_dst_register with writemask.
2513 * \param mask bitmask of TGSI_WRITEMASK_[XYZW]
2514 */
2515 static struct tgsi_full_dst_register
2516 writemask_dst(const struct tgsi_full_dst_register *reg, unsigned mask)
2517 {
2518 struct tgsi_full_dst_register masked = *reg;
2519 masked.Register.WriteMask = mask;
2520 return masked;
2521 }
2522
2523
2524 /**
2525 * Check if the register's swizzle is XXXX, YYYY, ZZZZ, or WWWW.
2526 */
2527 static bool
2528 same_swizzle_terms(const struct tgsi_full_src_register *reg)
2529 {
2530 return (reg->Register.SwizzleX == reg->Register.SwizzleY &&
2531 reg->Register.SwizzleY == reg->Register.SwizzleZ &&
2532 reg->Register.SwizzleZ == reg->Register.SwizzleW);
2533 }
2534
2535
2536 /**
2537 * Search the vector for the value 'x' and return its position.
2538 */
2539 static int
2540 find_imm_in_vec4(const union tgsi_immediate_data vec[4],
2541 union tgsi_immediate_data x)
2542 {
2543 unsigned i;
2544 for (i = 0; i < 4; i++) {
2545 if (vec[i].Int == x.Int)
2546 return i;
2547 }
2548 return -1;
2549 }
2550
2551
2552 /**
2553 * Helper used by make_immediate_reg(), make_immediate_reg_4().
2554 */
2555 static int
2556 find_immediate(struct svga_shader_emitter_v10 *emit,
2557 union tgsi_immediate_data x, unsigned startIndex)
2558 {
2559 const unsigned endIndex = emit->num_immediates;
2560 unsigned i;
2561
2562 assert(emit->num_immediates_emitted > 0);
2563
2564 /* Search immediates for x, y, z, w */
2565 for (i = startIndex; i < endIndex; i++) {
2566 if (x.Int == emit->immediates[i][0].Int ||
2567 x.Int == emit->immediates[i][1].Int ||
2568 x.Int == emit->immediates[i][2].Int ||
2569 x.Int == emit->immediates[i][3].Int) {
2570 return i;
2571 }
2572 }
2573 /* immediate not declared yet */
2574 return -1;
2575 }
2576
2577
2578 /**
2579 * As above, but search for a double[2] pair.
2580 */
2581 static int
2582 find_immediate_dbl(struct svga_shader_emitter_v10 *emit,
2583 double x, double y)
2584 {
2585 const unsigned endIndex = emit->num_immediates;
2586 unsigned i;
2587
2588 assert(emit->num_immediates_emitted > 0);
2589
2590 /* Search immediates for x, y, z, w */
2591 for (i = 0; i < endIndex; i++) {
2592 if (x == emit->immediates_dbl[i][0] &&
2593 y == emit->immediates_dbl[i][1]) {
2594 return i;
2595 }
2596 }
2597 /* Should never try to use an immediate value that wasn't pre-declared */
2598 assert(!"find_immediate_dbl() failed!");
2599 return -1;
2600 }
2601
2602
2603
2604 /**
2605 * Return a tgsi_full_src_register for an immediate/literal
2606 * union tgsi_immediate_data[4] value.
2607 * Note: the values must have been previously declared/allocated in
2608 * emit_pre_helpers(). And, all of x,y,z,w must be located in the same
2609 * vec4 immediate.
2610 */
2611 static struct tgsi_full_src_register
2612 make_immediate_reg_4(struct svga_shader_emitter_v10 *emit,
2613 const union tgsi_immediate_data imm[4])
2614 {
2615 struct tgsi_full_src_register reg;
2616 unsigned i;
2617
2618 for (i = 0; i < emit->num_common_immediates; i++) {
2619 /* search for first component value */
2620 int immpos = find_immediate(emit, imm[0], i);
2621 int x, y, z, w;
2622
2623 assert(immpos >= 0);
2624
2625 /* find remaining components within the immediate vector */
2626 x = find_imm_in_vec4(emit->immediates[immpos], imm[0]);
2627 y = find_imm_in_vec4(emit->immediates[immpos], imm[1]);
2628 z = find_imm_in_vec4(emit->immediates[immpos], imm[2]);
2629 w = find_imm_in_vec4(emit->immediates[immpos], imm[3]);
2630
2631 if (x >=0 && y >= 0 && z >= 0 && w >= 0) {
2632 /* found them all */
2633 memset(&reg, 0, sizeof(reg));
2634 reg.Register.File = TGSI_FILE_IMMEDIATE;
2635 reg.Register.Index = immpos;
2636 reg.Register.SwizzleX = x;
2637 reg.Register.SwizzleY = y;
2638 reg.Register.SwizzleZ = z;
2639 reg.Register.SwizzleW = w;
2640 return reg;
2641 }
2642 /* else, keep searching */
2643 }
2644
2645 assert(!"Failed to find immediate register!");
2646
2647 /* Just return IMM[0].xxxx */
2648 memset(&reg, 0, sizeof(reg));
2649 reg.Register.File = TGSI_FILE_IMMEDIATE;
2650 return reg;
2651 }
2652
2653
2654 /**
2655 * Return a tgsi_full_src_register for an immediate/literal
2656 * union tgsi_immediate_data value of the form {value, value, value, value}.
2657 * \sa make_immediate_reg_4() regarding allowed values.
2658 */
2659 static struct tgsi_full_src_register
2660 make_immediate_reg(struct svga_shader_emitter_v10 *emit,
2661 union tgsi_immediate_data value)
2662 {
2663 struct tgsi_full_src_register reg;
2664 int immpos = find_immediate(emit, value, 0);
2665
2666 assert(immpos >= 0);
2667
2668 memset(&reg, 0, sizeof(reg));
2669 reg.Register.File = TGSI_FILE_IMMEDIATE;
2670 reg.Register.Index = immpos;
2671 reg.Register.SwizzleX =
2672 reg.Register.SwizzleY =
2673 reg.Register.SwizzleZ =
2674 reg.Register.SwizzleW = find_imm_in_vec4(emit->immediates[immpos], value);
2675
2676 return reg;
2677 }
2678
2679
2680 /**
2681 * Return a tgsi_full_src_register for an immediate/literal float[4] value.
2682 * \sa make_immediate_reg_4() regarding allowed values.
2683 */
2684 static struct tgsi_full_src_register
2685 make_immediate_reg_float4(struct svga_shader_emitter_v10 *emit,
2686 float x, float y, float z, float w)
2687 {
2688 union tgsi_immediate_data imm[4];
2689 imm[0].Float = x;
2690 imm[1].Float = y;
2691 imm[2].Float = z;
2692 imm[3].Float = w;
2693 return make_immediate_reg_4(emit, imm);
2694 }
2695
2696
2697 /**
2698 * Return a tgsi_full_src_register for an immediate/literal float value
2699 * of the form {value, value, value, value}.
2700 * \sa make_immediate_reg_4() regarding allowed values.
2701 */
2702 static struct tgsi_full_src_register
2703 make_immediate_reg_float(struct svga_shader_emitter_v10 *emit, float value)
2704 {
2705 union tgsi_immediate_data imm;
2706 imm.Float = value;
2707 return make_immediate_reg(emit, imm);
2708 }
2709
2710
2711 /**
2712 * Return a tgsi_full_src_register for an immediate/literal int[4] vector.
2713 */
2714 static struct tgsi_full_src_register
2715 make_immediate_reg_int4(struct svga_shader_emitter_v10 *emit,
2716 int x, int y, int z, int w)
2717 {
2718 union tgsi_immediate_data imm[4];
2719 imm[0].Int = x;
2720 imm[1].Int = y;
2721 imm[2].Int = z;
2722 imm[3].Int = w;
2723 return make_immediate_reg_4(emit, imm);
2724 }
2725
2726
2727 /**
2728 * Return a tgsi_full_src_register for an immediate/literal int value
2729 * of the form {value, value, value, value}.
2730 * \sa make_immediate_reg_4() regarding allowed values.
2731 */
2732 static struct tgsi_full_src_register
2733 make_immediate_reg_int(struct svga_shader_emitter_v10 *emit, int value)
2734 {
2735 union tgsi_immediate_data imm;
2736 imm.Int = value;
2737 return make_immediate_reg(emit, imm);
2738 }
2739
2740
2741 static struct tgsi_full_src_register
2742 make_immediate_reg_double(struct svga_shader_emitter_v10 *emit, double value)
2743 {
2744 struct tgsi_full_src_register reg;
2745 int immpos = find_immediate_dbl(emit, value, value);
2746
2747 assert(immpos >= 0);
2748
2749 memset(&reg, 0, sizeof(reg));
2750 reg.Register.File = TGSI_FILE_IMMEDIATE;
2751 reg.Register.Index = immpos;
2752 reg.Register.SwizzleX = TGSI_SWIZZLE_X;
2753 reg.Register.SwizzleY = TGSI_SWIZZLE_Y;
2754 reg.Register.SwizzleZ = TGSI_SWIZZLE_Z;
2755 reg.Register.SwizzleW = TGSI_SWIZZLE_W;
2756
2757 return reg;
2758 }
2759
2760
2761 /**
2762 * Allocate space for a union tgsi_immediate_data[4] immediate.
2763 * \return the index/position of the immediate.
2764 */
2765 static unsigned
2766 alloc_immediate_4(struct svga_shader_emitter_v10 *emit,
2767 const union tgsi_immediate_data imm[4])
2768 {
2769 unsigned n = emit->num_immediates++;
2770 assert(n < ARRAY_SIZE(emit->immediates));
2771 emit->immediates[n][0] = imm[0];
2772 emit->immediates[n][1] = imm[1];
2773 emit->immediates[n][2] = imm[2];
2774 emit->immediates[n][3] = imm[3];
2775 return n;
2776 }
2777
2778
2779 /**
2780 * Allocate space for a float[4] immediate.
2781 * \return the index/position of the immediate.
2782 */
2783 static unsigned
2784 alloc_immediate_float4(struct svga_shader_emitter_v10 *emit,
2785 float x, float y, float z, float w)
2786 {
2787 union tgsi_immediate_data imm[4];
2788 imm[0].Float = x;
2789 imm[1].Float = y;
2790 imm[2].Float = z;
2791 imm[3].Float = w;
2792 return alloc_immediate_4(emit, imm);
2793 }
2794
2795
2796 /**
2797 * Allocate space for an int[4] immediate.
2798 * \return the index/position of the immediate.
2799 */
2800 static unsigned
2801 alloc_immediate_int4(struct svga_shader_emitter_v10 *emit,
2802 int x, int y, int z, int w)
2803 {
2804 union tgsi_immediate_data imm[4];
2805 imm[0].Int = x;
2806 imm[1].Int = y;
2807 imm[2].Int = z;
2808 imm[3].Int = w;
2809 return alloc_immediate_4(emit, imm);
2810 }
2811
2812
2813 /**
2814 * Add a new immediate after the immediate block has been declared.
2815 * Any new immediates will be appended to the immediate block after the
2816 * shader has been parsed.
2817 * \return the index/position of the immediate.
2818 */
2819 static unsigned
2820 add_immediate_int(struct svga_shader_emitter_v10 *emit, int x)
2821 {
2822 union tgsi_immediate_data imm[4];
2823 imm[0].Int = x;
2824 imm[1].Int = x+1;
2825 imm[2].Int = x+2;
2826 imm[3].Int = x+3;
2827
2828 unsigned immpos = alloc_immediate_4(emit, imm);
2829 emit->num_new_immediates++;
2830
2831 return immpos;
2832 }
2833
2834
2835 static unsigned
2836 alloc_immediate_double2(struct svga_shader_emitter_v10 *emit,
2837 double x, double y)
2838 {
2839 unsigned n = emit->num_immediates++;
2840 assert(!emit->num_immediates_emitted);
2841 assert(n < ARRAY_SIZE(emit->immediates));
2842 emit->immediates_dbl[n][0] = x;
2843 emit->immediates_dbl[n][1] = y;
2844 return n;
2845
2846 }
2847
2848
2849 /**
2850 * Allocate a shader input to store a system value.
2851 */
2852 static unsigned
2853 alloc_system_value_index(struct svga_shader_emitter_v10 *emit, unsigned index)
2854 {
2855 const unsigned n = emit->linkage.input_map_max + 1 + index;
2856 assert(index < ARRAY_SIZE(emit->system_value_indexes));
2857 emit->system_value_indexes[index] = n;
2858 return n;
2859 }
2860
2861
2862 /**
2863 * Translate a TGSI immediate value (union tgsi_immediate_data[4]) to VGPU10.
2864 */
2865 static bool
2866 emit_vgpu10_immediate(struct svga_shader_emitter_v10 *emit,
2867 const struct tgsi_full_immediate *imm)
2868 {
2869 /* We don't actually emit any code here. We just save the
2870 * immediate values and emit them later.
2871 */
2872 alloc_immediate_4(emit, imm->u);
2873 return true;
2874 }
2875
2876
2877 /**
2878 * Emit a VGPU10_CUSTOMDATA_DCL_IMMEDIATE_CONSTANT_BUFFER block
2879 * containing all the immediate values previously allocated
2880 * with alloc_immediate_4().
2881 */
2882 static bool
2883 emit_vgpu10_immediates_block(struct svga_shader_emitter_v10 *emit)
2884 {
2885 VGPU10OpcodeToken0 token;
2886
2887 assert(!emit->num_immediates_emitted);
2888
2889 token.value = 0;
2890 token.opcodeType = VGPU10_OPCODE_CUSTOMDATA;
2891 token.customDataClass = VGPU10_CUSTOMDATA_DCL_IMMEDIATE_CONSTANT_BUFFER;
2892
2893 emit->immediates_block_start_token =
2894 (emit->ptr - emit->buf) / sizeof(VGPU10OpcodeToken0);
2895
2896 /* Note: no begin/end_emit_instruction() calls */
2897 emit_dword(emit, token.value);
2898 emit_dword(emit, 2 + 4 * emit->num_immediates);
2899 emit_dwords(emit, (unsigned *) emit->immediates, 4 * emit->num_immediates);
2900
2901 emit->num_immediates_emitted = emit->num_immediates;
2902
2903 emit->immediates_block_next_token =
2904 (emit->ptr - emit->buf) / sizeof(VGPU10OpcodeToken0);
2905
2906 return true;
2907 }
2908
2909
2910 /**
2911 * Reemit the immediate constant buffer block to include the new
2912 * immediates that are allocated after the block is declared. Those
2913 * immediates are used as constant indices to constant buffers.
2914 */
2915 static bool
2916 reemit_immediates_block(struct svga_shader_emitter_v10 *emit)
2917 {
2918 unsigned num_tokens = emit_get_num_tokens(emit);
2919 unsigned num_new_immediates = emit->num_new_immediates;
2920
2921 /* Reserve room for the new immediates */
2922 if (!reserve(emit, 4 * num_new_immediates))
2923 return false;
2924
2925 /* Move the tokens after the immediates block to make room for the
2926 * new immediates.
2927 */
2928 VGPU10ProgramToken *tokens = (VGPU10ProgramToken *)emit->buf;
2929 char *next = (char *) (tokens + emit->immediates_block_next_token);
2930 char *new_next = (char *) (tokens + emit->immediates_block_next_token +
2931 num_new_immediates * 4);
2932
2933 char *end = emit->ptr;
2934 unsigned len = end - next;
2935 memmove(new_next, next, len);
2936
2937 /* Append the new immediates to the end of the immediates block */
2938 char *start = (char *) (tokens + emit->immediates_block_start_token+1);
2939 unsigned immediates_block_size = *(uint32 *)start;
2940
2941 char *new_immediates = (char *)&emit->immediates[emit->num_immediates_emitted][0];
2942 *(uint32 *)start = immediates_block_size + 4 * num_new_immediates;
2943 memcpy(next, new_immediates, 4 * num_new_immediates * sizeof(uint32));
2944
2945 emit->ptr = (char *) (tokens + num_tokens + 4 * num_new_immediates);
2946
2947 return true;
2948 }
2949
2950
2951
2952 /**
2953 * Translate a fragment shader's TGSI_INTERPOLATE_x mode to a vgpu10
2954 * interpolation mode.
2955 * \return a VGPU10_INTERPOLATION_x value
2956 */
2957 static unsigned
2958 translate_interpolation(const struct svga_shader_emitter_v10 *emit,
2959 enum tgsi_interpolate_mode interp,
2960 enum tgsi_interpolate_loc interpolate_loc)
2961 {
2962 if (interp == TGSI_INTERPOLATE_COLOR) {
2963 interp = emit->key.fs.flatshade ?
2964 TGSI_INTERPOLATE_CONSTANT : TGSI_INTERPOLATE_PERSPECTIVE;
2965 }
2966
2967 switch (interp) {
2968 case TGSI_INTERPOLATE_CONSTANT:
2969 return VGPU10_INTERPOLATION_CONSTANT;
2970 case TGSI_INTERPOLATE_LINEAR:
2971 if (interpolate_loc == TGSI_INTERPOLATE_LOC_CENTROID) {
2972 return VGPU10_INTERPOLATION_LINEAR_NOPERSPECTIVE_CENTROID;
2973 } else if (interpolate_loc == TGSI_INTERPOLATE_LOC_SAMPLE &&
2974 emit->version >= 41) {
2975 return VGPU10_INTERPOLATION_LINEAR_NOPERSPECTIVE_SAMPLE;
2976 } else {
2977 return VGPU10_INTERPOLATION_LINEAR_NOPERSPECTIVE;
2978 }
2979 break;
2980 case TGSI_INTERPOLATE_PERSPECTIVE:
2981 if (interpolate_loc == TGSI_INTERPOLATE_LOC_CENTROID) {
2982 return VGPU10_INTERPOLATION_LINEAR_CENTROID;
2983 } else if (interpolate_loc == TGSI_INTERPOLATE_LOC_SAMPLE &&
2984 emit->version >= 41) {
2985 return VGPU10_INTERPOLATION_LINEAR_SAMPLE;
2986 } else {
2987 return VGPU10_INTERPOLATION_LINEAR;
2988 }
2989 break;
2990 default:
2991 assert(!"Unexpected interpolation mode");
2992 return VGPU10_INTERPOLATION_CONSTANT;
2993 }
2994 }
2995
2996
2997 /**
2998 * Translate a TGSI property to VGPU10.
2999 * Don't emit any instructions yet, only need to gather the primitive property
3000 * information. The output primitive topology might be changed later. The
3001 * final property instructions will be emitted as part of the pre-helper code.
3002 */
3003 static bool
3004 emit_vgpu10_property(struct svga_shader_emitter_v10 *emit,
3005 const struct tgsi_full_property *prop)
3006 {
3007 static const VGPU10_PRIMITIVE primType[] = {
3008 VGPU10_PRIMITIVE_POINT, /* MESA_PRIM_POINTS */
3009 VGPU10_PRIMITIVE_LINE, /* MESA_PRIM_LINES */
3010 VGPU10_PRIMITIVE_LINE, /* MESA_PRIM_LINE_LOOP */
3011 VGPU10_PRIMITIVE_LINE, /* MESA_PRIM_LINE_STRIP */
3012 VGPU10_PRIMITIVE_TRIANGLE, /* MESA_PRIM_TRIANGLES */
3013 VGPU10_PRIMITIVE_TRIANGLE, /* MESA_PRIM_TRIANGLE_STRIP */
3014 VGPU10_PRIMITIVE_TRIANGLE, /* MESA_PRIM_TRIANGLE_FAN */
3015 VGPU10_PRIMITIVE_UNDEFINED, /* MESA_PRIM_QUADS */
3016 VGPU10_PRIMITIVE_UNDEFINED, /* MESA_PRIM_QUAD_STRIP */
3017 VGPU10_PRIMITIVE_UNDEFINED, /* MESA_PRIM_POLYGON */
3018 VGPU10_PRIMITIVE_LINE_ADJ, /* MESA_PRIM_LINES_ADJACENCY */
3019 VGPU10_PRIMITIVE_LINE_ADJ, /* MESA_PRIM_LINE_STRIP_ADJACENCY */
3020 VGPU10_PRIMITIVE_TRIANGLE_ADJ, /* MESA_PRIM_TRIANGLES_ADJACENCY */
3021 VGPU10_PRIMITIVE_TRIANGLE_ADJ /* MESA_PRIM_TRIANGLE_STRIP_ADJACENCY */
3022 };
3023
3024 static const VGPU10_PRIMITIVE_TOPOLOGY primTopology[] = {
3025 VGPU10_PRIMITIVE_TOPOLOGY_POINTLIST, /* MESA_PRIM_POINTS */
3026 VGPU10_PRIMITIVE_TOPOLOGY_LINELIST, /* MESA_PRIM_LINES */
3027 VGPU10_PRIMITIVE_TOPOLOGY_LINELIST, /* MESA_PRIM_LINE_LOOP */
3028 VGPU10_PRIMITIVE_TOPOLOGY_LINESTRIP, /* MESA_PRIM_LINE_STRIP */
3029 VGPU10_PRIMITIVE_TOPOLOGY_TRIANGLELIST, /* MESA_PRIM_TRIANGLES */
3030 VGPU10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, /* MESA_PRIM_TRIANGLE_STRIP */
3031 VGPU10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, /* MESA_PRIM_TRIANGLE_FAN */
3032 VGPU10_PRIMITIVE_TOPOLOGY_UNDEFINED, /* MESA_PRIM_QUADS */
3033 VGPU10_PRIMITIVE_TOPOLOGY_UNDEFINED, /* MESA_PRIM_QUAD_STRIP */
3034 VGPU10_PRIMITIVE_TOPOLOGY_UNDEFINED, /* MESA_PRIM_POLYGON */
3035 VGPU10_PRIMITIVE_TOPOLOGY_LINELIST_ADJ, /* MESA_PRIM_LINES_ADJACENCY */
3036 VGPU10_PRIMITIVE_TOPOLOGY_LINELIST_ADJ, /* MESA_PRIM_LINE_STRIP_ADJACENCY */
3037 VGPU10_PRIMITIVE_TOPOLOGY_TRIANGLELIST_ADJ, /* MESA_PRIM_TRIANGLES_ADJACENCY */
3038 VGPU10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP_ADJ /* MESA_PRIM_TRIANGLE_STRIP_ADJACENCY */
3039 };
3040
3041 static const unsigned inputArraySize[] = {
3042 0, /* VGPU10_PRIMITIVE_UNDEFINED */
3043 1, /* VGPU10_PRIMITIVE_POINT */
3044 2, /* VGPU10_PRIMITIVE_LINE */
3045 3, /* VGPU10_PRIMITIVE_TRIANGLE */
3046 0,
3047 0,
3048 4, /* VGPU10_PRIMITIVE_LINE_ADJ */
3049 6 /* VGPU10_PRIMITIVE_TRIANGLE_ADJ */
3050 };
3051
3052 switch (prop->Property.PropertyName) {
3053 case TGSI_PROPERTY_GS_INPUT_PRIM:
3054 assert(prop->u[0].Data < ARRAY_SIZE(primType));
3055 emit->gs.prim_type = primType[prop->u[0].Data];
3056 assert(emit->gs.prim_type != VGPU10_PRIMITIVE_UNDEFINED);
3057 emit->gs.input_size = inputArraySize[emit->gs.prim_type];
3058 break;
3059
3060 case TGSI_PROPERTY_GS_OUTPUT_PRIM:
3061 assert(prop->u[0].Data < ARRAY_SIZE(primTopology));
3062 emit->gs.prim_topology = primTopology[prop->u[0].Data];
3063 assert(emit->gs.prim_topology != VGPU10_PRIMITIVE_TOPOLOGY_UNDEFINED);
3064 break;
3065
3066 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES:
3067 emit->gs.max_out_vertices = prop->u[0].Data;
3068 break;
3069
3070 case TGSI_PROPERTY_GS_INVOCATIONS:
3071 emit->gs.invocations = prop->u[0].Data;
3072 break;
3073
3074 case TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS:
3075 case TGSI_PROPERTY_NEXT_SHADER:
3076 case TGSI_PROPERTY_NUM_CLIPDIST_ENABLED:
3077 /* no-op */
3078 break;
3079
3080 case TGSI_PROPERTY_TCS_VERTICES_OUT:
3081 /* This info is already captured in the shader key */
3082 break;
3083
3084 case TGSI_PROPERTY_TES_PRIM_MODE:
3085 emit->tes.prim_mode = prop->u[0].Data;
3086 break;
3087
3088 case TGSI_PROPERTY_TES_SPACING:
3089 emit->tes.spacing = prop->u[0].Data;
3090 break;
3091
3092 case TGSI_PROPERTY_TES_VERTEX_ORDER_CW:
3093 emit->tes.vertices_order_cw = prop->u[0].Data;
3094 break;
3095
3096 case TGSI_PROPERTY_TES_POINT_MODE:
3097 emit->tes.point_mode = prop->u[0].Data;
3098 break;
3099
3100 case TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH:
3101 emit->cs.block_width = prop->u[0].Data;
3102 break;
3103
3104 case TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT:
3105 emit->cs.block_height = prop->u[0].Data;
3106 break;
3107
3108 case TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH:
3109 emit->cs.block_depth = prop->u[0].Data;
3110 break;
3111
3112 case TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL:
3113 emit->fs.forceEarlyDepthStencil = true;
3114 break;
3115
3116 default:
3117 debug_printf("Unexpected TGSI property %s\n",
3118 tgsi_property_names[prop->Property.PropertyName]);
3119 }
3120
3121 return true;
3122 }
3123
3124
3125 static void
3126 emit_property_instruction(struct svga_shader_emitter_v10 *emit,
3127 VGPU10OpcodeToken0 opcode0, unsigned nData,
3128 unsigned data)
3129 {
3130 begin_emit_instruction(emit);
3131 emit_dword(emit, opcode0.value);
3132 if (nData)
3133 emit_dword(emit, data);
3134 end_emit_instruction(emit);
3135 }
3136
3137
3138 /**
3139 * Emit property instructions
3140 */
3141 static void
3142 emit_property_instructions(struct svga_shader_emitter_v10 *emit)
3143 {
3144 VGPU10OpcodeToken0 opcode0;
3145
3146 assert(emit->unit == MESA_SHADER_GEOMETRY);
3147
3148 /* emit input primitive type declaration */
3149 opcode0.value = 0;
3150 opcode0.opcodeType = VGPU10_OPCODE_DCL_GS_INPUT_PRIMITIVE;
3151 opcode0.primitive = emit->gs.prim_type;
3152 emit_property_instruction(emit, opcode0, 0, 0);
3153
3154 /* emit max output vertices */
3155 opcode0.value = 0;
3156 opcode0.opcodeType = VGPU10_OPCODE_DCL_MAX_OUTPUT_VERTEX_COUNT;
3157 emit_property_instruction(emit, opcode0, 1, emit->gs.max_out_vertices);
3158
3159 if (emit->version >= 50 && emit->gs.invocations > 0) {
3160 opcode0.value = 0;
3161 opcode0.opcodeType = VGPU10_OPCODE_DCL_GS_INSTANCE_COUNT;
3162 emit_property_instruction(emit, opcode0, 1, emit->gs.invocations);
3163 }
3164 }
3165
3166
3167 /**
3168 * A helper function to declare tessellator domain in a hull shader or
3169 * in the domain shader.
3170 */
3171 static void
3172 emit_tessellator_domain(struct svga_shader_emitter_v10 *emit,
3173 enum mesa_prim prim_mode)
3174 {
3175 VGPU10OpcodeToken0 opcode0;
3176
3177 opcode0.value = 0;
3178 opcode0.opcodeType = VGPU10_OPCODE_DCL_TESS_DOMAIN;
3179 switch (prim_mode) {
3180 case MESA_PRIM_QUADS:
3181 case MESA_PRIM_LINES:
3182 opcode0.tessDomain = VGPU10_TESSELLATOR_DOMAIN_QUAD;
3183 break;
3184 case MESA_PRIM_TRIANGLES:
3185 opcode0.tessDomain = VGPU10_TESSELLATOR_DOMAIN_TRI;
3186 break;
3187 default:
3188 debug_printf("Invalid tessellator prim mode %d\n", prim_mode);
3189 opcode0.tessDomain = VGPU10_TESSELLATOR_DOMAIN_UNDEFINED;
3190 }
3191 begin_emit_instruction(emit);
3192 emit_dword(emit, opcode0.value);
3193 end_emit_instruction(emit);
3194 }
3195
3196
3197 /**
3198 * Emit domain shader declarations.
3199 */
3200 static void
3201 emit_domain_shader_declarations(struct svga_shader_emitter_v10 *emit)
3202 {
3203 VGPU10OpcodeToken0 opcode0;
3204
3205 assert(emit->unit == MESA_SHADER_TESS_EVAL);
3206
3207 /* Emit the input control point count */
3208 assert(emit->key.tes.vertices_per_patch >= 0 &&
3209 emit->key.tes.vertices_per_patch <= 32);
3210
3211 opcode0.value = 0;
3212 opcode0.opcodeType = VGPU10_OPCODE_DCL_INPUT_CONTROL_POINT_COUNT;
3213 opcode0.controlPointCount = emit->key.tes.vertices_per_patch;
3214 begin_emit_instruction(emit);
3215 emit_dword(emit, opcode0.value);
3216 end_emit_instruction(emit);
3217
3218 emit_tessellator_domain(emit, emit->tes.prim_mode);
3219
3220 /* Specify a max for swizzles of the domain point according to the
3221 * tessellator domain type.
3222 */
3223 emit->tes.swizzle_max = emit->tes.prim_mode == MESA_PRIM_TRIANGLES ?
3224 TGSI_SWIZZLE_Z : TGSI_SWIZZLE_Y;
3225 }
3226
3227
3228 /**
3229 * Some common values like 0.0, 1.0, 0.5, etc. are frequently needed
3230 * to implement some instructions. We pre-allocate those values here
3231 * in the immediate constant buffer.
3232 */
3233 static void
3234 alloc_common_immediates(struct svga_shader_emitter_v10 *emit)
3235 {
3236 unsigned n = 0;
3237
3238 emit->common_immediate_pos[n++] =
3239 alloc_immediate_float4(emit, 0.0f, 1.0f, 0.5f, -1.0f);
3240
3241 if (emit->info.opcode_count[TGSI_OPCODE_LIT] > 0) {
3242 emit->common_immediate_pos[n++] =
3243 alloc_immediate_float4(emit, 128.0f, -128.0f, 0.0f, 0.0f);
3244 }
3245
3246 emit->common_immediate_pos[n++] =
3247 alloc_immediate_int4(emit, 0, 1, 2, -1);
3248
3249 emit->common_immediate_pos[n++] =
3250 alloc_immediate_int4(emit, 3, 4, 5, 6);
3251
3252 if (emit->info.opcode_count[TGSI_OPCODE_IMSB] > 0 ||
3253 emit->info.opcode_count[TGSI_OPCODE_UMSB] > 0) {
3254 emit->common_immediate_pos[n++] =
3255 alloc_immediate_int4(emit, 31, 0, 0, 0);
3256 }
3257
3258 if (emit->info.opcode_count[TGSI_OPCODE_UBFE] > 0 ||
3259 emit->info.opcode_count[TGSI_OPCODE_IBFE] > 0 ||
3260 emit->info.opcode_count[TGSI_OPCODE_BFI] > 0) {
3261 emit->common_immediate_pos[n++] =
3262 alloc_immediate_int4(emit, 32, 0, 0, 0);
3263 }
3264
3265 if (emit->key.vs.attrib_puint_to_snorm) {
3266 emit->common_immediate_pos[n++] =
3267 alloc_immediate_float4(emit, -2.0f, 2.0f, 3.0f, -1.66666f);
3268 }
3269
3270 if (emit->key.vs.attrib_puint_to_uscaled) {
3271 emit->common_immediate_pos[n++] =
3272 alloc_immediate_float4(emit, 1023.0f, 3.0f, 0.0f, 0.0f);
3273 }
3274
3275 if (emit->key.vs.attrib_puint_to_sscaled) {
3276 emit->common_immediate_pos[n++] =
3277 alloc_immediate_int4(emit, 22, 12, 2, 0);
3278
3279 emit->common_immediate_pos[n++] =
3280 alloc_immediate_int4(emit, 22, 30, 0, 0);
3281 }
3282
3283 if (emit->vposition.num_prescale > 1) {
3284 unsigned i;
3285 for (i = 0; i < emit->vposition.num_prescale; i+=4) {
3286 emit->common_immediate_pos[n++] =
3287 alloc_immediate_int4(emit, i, i+1, i+2, i+3);
3288 }
3289 }
3290
3291 emit->immediates_dbl = (double (*)[2]) emit->immediates;
3292
3293 if (emit->info.opcode_count[TGSI_OPCODE_DNEG] > 0) {
3294 emit->common_immediate_pos[n++] =
3295 alloc_immediate_double2(emit, -1.0, -1.0);
3296 }
3297
3298 if (emit->info.opcode_count[TGSI_OPCODE_DSQRT] > 0 ||
3299 emit->info.opcode_count[TGSI_OPCODE_DTRUNC] > 0) {
3300 emit->common_immediate_pos[n++] =
3301 alloc_immediate_double2(emit, 0.0, 0.0);
3302 emit->common_immediate_pos[n++] =
3303 alloc_immediate_double2(emit, 1.0, 1.0);
3304 }
3305
3306 if (emit->info.opcode_count[TGSI_OPCODE_INTERP_OFFSET] > 0) {
3307 emit->common_immediate_pos[n++] =
3308 alloc_immediate_float4(emit, 16.0f, -16.0f, 0.0, 0.0);
3309 }
3310
3311 assert(n <= ARRAY_SIZE(emit->common_immediate_pos));
3312
3313 unsigned i;
3314
3315 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
3316 if (emit->key.tex[i].texel_bias) {
3317 /* Replace 0.0f if more immediate float value is needed */
3318 emit->common_immediate_pos[n++] =
3319 alloc_immediate_float4(emit, 0.0001f, 0.0f, 0.0f, 0.0f);
3320 break;
3321 }
3322 }
3323
3324 /** TODO: allocate immediates for all possible element byte offset?
3325 */
3326 if (emit->raw_bufs) {
3327 unsigned i;
3328 for (i = 7; i < 12; i+=4) {
3329 emit->common_immediate_pos[n++] =
3330 alloc_immediate_int4(emit, i, (i+1), (i+2), (i+3));
3331 }
3332 }
3333
3334 if (emit->info.indirect_files &
3335 (1 << TGSI_FILE_IMAGE | 1 << TGSI_FILE_BUFFER)) {
3336 unsigned i;
3337 for (i = 7; i < 8; i+=4) {
3338 emit->common_immediate_pos[n++] =
3339 alloc_immediate_int4(emit, i, (i+1), (i+2), (i+3));
3340 }
3341 }
3342
3343 assert(n <= ARRAY_SIZE(emit->common_immediate_pos));
3344 emit->num_common_immediates = n;
3345 }
3346
3347
3348 /**
3349 * Emit hull shader declarations.
3350 */
3351 static void
3352 emit_hull_shader_declarations(struct svga_shader_emitter_v10 *emit)
3353 {
3354 VGPU10OpcodeToken0 opcode0;
3355
3356 /* Emit the input control point count */
3357 assert(emit->key.tcs.vertices_per_patch > 0 &&
3358 emit->key.tcs.vertices_per_patch <= 32);
3359
3360 opcode0.value = 0;
3361 opcode0.opcodeType = VGPU10_OPCODE_DCL_INPUT_CONTROL_POINT_COUNT;
3362 opcode0.controlPointCount = emit->key.tcs.vertices_per_patch;
3363 begin_emit_instruction(emit);
3364 emit_dword(emit, opcode0.value);
3365 end_emit_instruction(emit);
3366
3367 /* Emit the output control point count */
3368 assert(emit->key.tcs.vertices_out >= 0 && emit->key.tcs.vertices_out <= 32);
3369
3370 opcode0.value = 0;
3371 opcode0.opcodeType = VGPU10_OPCODE_DCL_OUTPUT_CONTROL_POINT_COUNT;
3372 opcode0.controlPointCount = emit->key.tcs.vertices_out;
3373 begin_emit_instruction(emit);
3374 emit_dword(emit, opcode0.value);
3375 end_emit_instruction(emit);
3376
3377 /* Emit tessellator domain */
3378 emit_tessellator_domain(emit, emit->key.tcs.prim_mode);
3379
3380 /* Emit tessellator output primitive */
3381 opcode0.value = 0;
3382 opcode0.opcodeType = VGPU10_OPCODE_DCL_TESS_OUTPUT_PRIMITIVE;
3383 if (emit->key.tcs.point_mode) {
3384 opcode0.tessOutputPrimitive = VGPU10_TESSELLATOR_OUTPUT_POINT;
3385 }
3386 else if (emit->key.tcs.prim_mode == MESA_PRIM_LINES) {
3387 opcode0.tessOutputPrimitive = VGPU10_TESSELLATOR_OUTPUT_LINE;
3388 }
3389 else {
3390 assert(emit->key.tcs.prim_mode == MESA_PRIM_QUADS ||
3391 emit->key.tcs.prim_mode == MESA_PRIM_TRIANGLES);
3392
3393 if (emit->key.tcs.vertices_order_cw)
3394 opcode0.tessOutputPrimitive = VGPU10_TESSELLATOR_OUTPUT_TRIANGLE_CCW;
3395 else
3396 opcode0.tessOutputPrimitive = VGPU10_TESSELLATOR_OUTPUT_TRIANGLE_CW;
3397 }
3398 begin_emit_instruction(emit);
3399 emit_dword(emit, opcode0.value);
3400 end_emit_instruction(emit);
3401
3402 /* Emit tessellator partitioning */
3403 opcode0.value = 0;
3404 opcode0.opcodeType = VGPU10_OPCODE_DCL_TESS_PARTITIONING;
3405 switch (emit->key.tcs.spacing) {
3406 case PIPE_TESS_SPACING_FRACTIONAL_ODD:
3407 opcode0.tessPartitioning = VGPU10_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD;
3408 break;
3409 case PIPE_TESS_SPACING_FRACTIONAL_EVEN:
3410 opcode0.tessPartitioning = VGPU10_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN;
3411 break;
3412 case PIPE_TESS_SPACING_EQUAL:
3413 opcode0.tessPartitioning = VGPU10_TESSELLATOR_PARTITIONING_INTEGER;
3414 break;
3415 default:
3416 debug_printf("invalid tessellator spacing %d\n", emit->key.tcs.spacing);
3417 opcode0.tessPartitioning = VGPU10_TESSELLATOR_PARTITIONING_UNDEFINED;
3418 }
3419 begin_emit_instruction(emit);
3420 emit_dword(emit, opcode0.value);
3421 end_emit_instruction(emit);
3422
3423 alloc_common_immediates(emit);
3424
3425 /* Declare constant registers */
3426 emit_constant_declaration(emit);
3427
3428 /* Declare samplers and resources */
3429 emit_sampler_declarations(emit);
3430 emit_resource_declarations(emit);
3431
3432 /* Declare images */
3433 emit_image_declarations(emit);
3434
3435 /* Declare shader buffers */
3436 emit_shader_buf_declarations(emit);
3437
3438 /* Declare atomic buffers */
3439 emit_atomic_buf_declarations(emit);
3440
3441 int nVertices = emit->key.tcs.vertices_per_patch;
3442 emit->tcs.imm_index =
3443 alloc_immediate_int4(emit, nVertices, nVertices, nVertices, 0);
3444
3445 /* Now, emit the constant block containing all the immediates
3446 * declared by shader, as well as the extra ones seen above.
3447 */
3448 emit_vgpu10_immediates_block(emit);
3449
3450 }
3451
3452
3453 /**
3454 * A helper function to determine if control point phase is needed.
3455 * Returns TRUE if there is control point output.
3456 */
3457 static bool
3458 needs_control_point_phase(struct svga_shader_emitter_v10 *emit)
3459 {
3460 unsigned i;
3461
3462 assert(emit->unit == MESA_SHADER_TESS_CTRL);
3463
3464 /* If output control point count does not match the input count,
3465 * we need a control point phase to explicitly set the output control
3466 * points.
3467 */
3468 if ((emit->key.tcs.vertices_per_patch != emit->key.tcs.vertices_out) &&
3469 emit->key.tcs.vertices_out)
3470 return true;
3471
3472 for (i = 0; i < emit->info.num_outputs; i++) {
3473 switch (emit->info.output_semantic_name[i]) {
3474 case TGSI_SEMANTIC_PATCH:
3475 case TGSI_SEMANTIC_TESSOUTER:
3476 case TGSI_SEMANTIC_TESSINNER:
3477 break;
3478 default:
3479 return true;
3480 }
3481 }
3482 return false;
3483 }
3484
3485
3486 /**
3487 * A helper function to add shader signature for passthrough control point
3488 * phase. This signature is also generated for passthrough control point
3489 * phase from HLSL compiler and is needed by Metal Renderer.
3490 */
3491 static void
3492 emit_passthrough_control_point_signature(struct svga_shader_emitter_v10 *emit)
3493 {
3494 struct svga_shader_signature *sgn = &emit->signature;
3495 SVGA3dDXShaderSignatureEntry *sgnEntry;
3496 unsigned i;
3497
3498 for (i = 0; i < emit->info.num_inputs; i++) {
3499 unsigned index = emit->linkage.input_map[i];
3500 enum tgsi_semantic sem_name = emit->info.input_semantic_name[i];
3501
3502 sgnEntry = &sgn->inputs[sgn->header.numInputSignatures++];
3503
3504 set_shader_signature_entry(sgnEntry, index,
3505 tgsi_semantic_to_sgn_name[sem_name],
3506 VGPU10_OPERAND_4_COMPONENT_MASK_ALL,
3507 SVGADX_SIGNATURE_REGISTER_COMPONENT_UNKNOWN,
3508 SVGADX_SIGNATURE_MIN_PRECISION_DEFAULT);
3509
3510 sgnEntry = &sgn->outputs[sgn->header.numOutputSignatures++];
3511
3512 set_shader_signature_entry(sgnEntry, i,
3513 tgsi_semantic_to_sgn_name[sem_name],
3514 VGPU10_OPERAND_4_COMPONENT_MASK_ALL,
3515 SVGADX_SIGNATURE_REGISTER_COMPONENT_UNKNOWN,
3516 SVGADX_SIGNATURE_MIN_PRECISION_DEFAULT);
3517 }
3518 }
3519
3520
3521 /**
3522 * A helper function to emit an instruction to start the control point phase
3523 * in the hull shader.
3524 */
3525 static void
3526 emit_control_point_phase_instruction(struct svga_shader_emitter_v10 *emit)
3527 {
3528 VGPU10OpcodeToken0 opcode0;
3529
3530 opcode0.value = 0;
3531 opcode0.opcodeType = VGPU10_OPCODE_HS_CONTROL_POINT_PHASE;
3532 begin_emit_instruction(emit);
3533 emit_dword(emit, opcode0.value);
3534 end_emit_instruction(emit);
3535 }
3536
3537
3538 /**
3539 * Start the hull shader control point phase
3540 */
3541 static bool
3542 emit_hull_shader_control_point_phase(struct svga_shader_emitter_v10 *emit)
3543 {
3544 /* If there is no control point output, skip the control point phase. */
3545 if (!needs_control_point_phase(emit)) {
3546 if (!emit->key.tcs.vertices_out) {
3547 /**
3548 * If the tcs does not explicitly generate any control point output
3549 * and the tes does not use any input control point, then
3550 * emit an empty control point phase with zero output control
3551 * point count.
3552 */
3553 emit_control_point_phase_instruction(emit);
3554
3555 /**
3556 * Since this is an empty control point phase, we will need to
3557 * add input signatures when we parse the tcs again in the
3558 * patch constant phase.
3559 */
3560 emit->tcs.fork_phase_add_signature = true;
3561 }
3562 else {
3563 /**
3564 * Before skipping the control point phase, add the signature for
3565 * the passthrough control point.
3566 */
3567 emit_passthrough_control_point_signature(emit);
3568 }
3569 return false;
3570 }
3571
3572 /* Start the control point phase in the hull shader */
3573 emit_control_point_phase_instruction(emit);
3574
3575 /* Declare the output control point ID */
3576 if (emit->tcs.invocation_id_sys_index == INVALID_INDEX) {
3577 /* Add invocation id declaration if it does not exist */
3578 emit->tcs.invocation_id_sys_index = emit->info.num_system_values + 1;
3579 }
3580
3581 emit_input_declaration(emit, VGPU10_OPCODE_DCL_INPUT,
3582 VGPU10_OPERAND_TYPE_OUTPUT_CONTROL_POINT_ID,
3583 VGPU10_OPERAND_INDEX_0D,
3584 0, 1,
3585 VGPU10_NAME_UNDEFINED,
3586 VGPU10_OPERAND_0_COMPONENT, 0,
3587 0,
3588 VGPU10_INTERPOLATION_CONSTANT, true,
3589 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED);
3590
3591 if (emit->tcs.prim_id_index != INVALID_INDEX) {
3592 emit_input_declaration(emit, VGPU10_OPCODE_DCL_INPUT,
3593 VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID,
3594 VGPU10_OPERAND_INDEX_0D,
3595 0, 1,
3596 VGPU10_NAME_UNDEFINED,
3597 VGPU10_OPERAND_0_COMPONENT,
3598 VGPU10_OPERAND_4_COMPONENT_MASK_MODE,
3599 0,
3600 VGPU10_INTERPOLATION_UNDEFINED, true,
3601 SVGADX_SIGNATURE_SEMANTIC_NAME_PRIMITIVE_ID);
3602 }
3603
3604 return true;
3605 }
3606
3607
3608 /**
3609 * Start the hull shader patch constant phase and
3610 * do the second pass of the tcs translation and emit
3611 * the relevant declarations and instructions for this phase.
3612 */
3613 static bool
3614 emit_hull_shader_patch_constant_phase(struct svga_shader_emitter_v10 *emit,
3615 struct tgsi_parse_context *parse)
3616 {
3617 unsigned inst_number = 0;
3618 bool ret = true;
3619 VGPU10OpcodeToken0 opcode0;
3620
3621 emit->skip_instruction = false;
3622
3623 /* Start the patch constant phase */
3624 opcode0.value = 0;
3625 opcode0.opcodeType = VGPU10_OPCODE_HS_FORK_PHASE;
3626 begin_emit_instruction(emit);
3627 emit_dword(emit, opcode0.value);
3628 end_emit_instruction(emit);
3629
3630 /* Set the current phase to patch constant phase */
3631 emit->tcs.control_point_phase = false;
3632
3633 if (emit->tcs.prim_id_index != INVALID_INDEX) {
3634 emit_input_declaration(emit, VGPU10_OPCODE_DCL_INPUT,
3635 VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID,
3636 VGPU10_OPERAND_INDEX_0D,
3637 0, 1,
3638 VGPU10_NAME_UNDEFINED,
3639 VGPU10_OPERAND_0_COMPONENT,
3640 VGPU10_OPERAND_4_COMPONENT_MASK_MODE,
3641 0,
3642 VGPU10_INTERPOLATION_UNDEFINED, true,
3643 SVGADX_SIGNATURE_SEMANTIC_NAME_PRIMITIVE_ID);
3644 }
3645
3646 /* Emit declarations for this phase */
3647 emit->index_range.required =
3648 emit->info.indirect_files & (1 << TGSI_FILE_INPUT) ? true : false;
3649 emit_tcs_input_declarations(emit);
3650
3651 if (emit->index_range.start_index != INVALID_INDEX) {
3652 emit_index_range_declaration(emit);
3653 }
3654
3655 emit->index_range.required =
3656 emit->info.indirect_files & (1 << TGSI_FILE_OUTPUT) ? true : false;
3657 emit_tcs_output_declarations(emit);
3658
3659 if (emit->index_range.start_index != INVALID_INDEX) {
3660 emit_index_range_declaration(emit);
3661 }
3662 emit->index_range.required = false;
3663
3664 emit_temporaries_declaration(emit);
3665
3666 /* Reset the token position to the first instruction token
3667 * in preparation for the second pass of the shader
3668 */
3669 parse->Position = emit->tcs.instruction_token_pos;
3670
3671 while (!tgsi_parse_end_of_tokens(parse)) {
3672 tgsi_parse_token(parse);
3673
3674 assert(parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_INSTRUCTION);
3675 ret = emit_vgpu10_instruction(emit, inst_number++,
3676 &parse->FullToken.FullInstruction);
3677
3678 /* Usually this applies to TCS only. If shader is reading output of
3679 * patch constant in fork phase, we should reemit all instructions
3680 * which are writting into output of patch constant in fork phase
3681 * to store results into temporaries.
3682 */
3683 assert(!(emit->reemit_instruction && emit->reemit_rawbuf_instruction));
3684 if (emit->reemit_instruction) {
3685 assert(emit->unit == MESA_SHADER_TESS_CTRL);
3686 ret = emit_vgpu10_instruction(emit, inst_number,
3687 &parse->FullToken.FullInstruction);
3688 } else if (emit->reemit_rawbuf_instruction) {
3689 ret = emit_rawbuf_instruction(emit, inst_number,
3690 &parse->FullToken.FullInstruction);
3691 }
3692
3693 if (!ret)
3694 return false;
3695 }
3696
3697 return true;
3698 }
3699
3700
3701 /**
3702 * Emit the thread group declaration for compute shader.
3703 */
3704 static void
3705 emit_compute_shader_declarations(struct svga_shader_emitter_v10 *emit)
3706 {
3707 VGPU10OpcodeToken0 opcode0;
3708
3709 opcode0.value = 0;
3710 opcode0.opcodeType = VGPU10_OPCODE_DCL_THREAD_GROUP;
3711 begin_emit_instruction(emit);
3712 emit_dword(emit, opcode0.value);
3713 emit_dword(emit, emit->cs.block_width);
3714 emit_dword(emit, emit->cs.block_height);
3715 emit_dword(emit, emit->cs.block_depth);
3716 end_emit_instruction(emit);
3717 }
3718
3719
3720 /**
3721 * Emit index range declaration.
3722 */
3723 static bool
3724 emit_index_range_declaration(struct svga_shader_emitter_v10 *emit)
3725 {
3726 if (emit->version < 50)
3727 return true;
3728
3729 assert(emit->index_range.start_index != INVALID_INDEX);
3730 assert(emit->index_range.count != 0);
3731 assert(emit->index_range.required);
3732 assert(emit->index_range.operandType != VGPU10_NUM_OPERANDS);
3733 assert(emit->index_range.dim != 0);
3734 assert(emit->index_range.size != 0);
3735
3736 VGPU10OpcodeToken0 opcode0;
3737 VGPU10OperandToken0 operand0;
3738
3739 opcode0.value = 0;
3740 opcode0.opcodeType = VGPU10_OPCODE_DCL_INDEX_RANGE;
3741
3742 operand0.value = 0;
3743 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
3744 operand0.indexDimension = emit->index_range.dim;
3745 operand0.operandType = emit->index_range.operandType;
3746 operand0.mask = VGPU10_OPERAND_4_COMPONENT_MASK_ALL;
3747 operand0.index0Representation = VGPU10_OPERAND_INDEX_IMMEDIATE32;
3748
3749 if (emit->index_range.dim == VGPU10_OPERAND_INDEX_2D)
3750 operand0.index1Representation = VGPU10_OPERAND_INDEX_IMMEDIATE32;
3751
3752 begin_emit_instruction(emit);
3753 emit_dword(emit, opcode0.value);
3754 emit_dword(emit, operand0.value);
3755
3756 if (emit->index_range.dim == VGPU10_OPERAND_INDEX_2D) {
3757 emit_dword(emit, emit->index_range.size);
3758 emit_dword(emit, emit->index_range.start_index);
3759 emit_dword(emit, emit->index_range.count);
3760 }
3761 else {
3762 emit_dword(emit, emit->index_range.start_index);
3763 emit_dword(emit, emit->index_range.count);
3764 }
3765
3766 end_emit_instruction(emit);
3767
3768 /* Reset fields in emit->index_range struct except
3769 * emit->index_range.required which will be reset afterwards
3770 */
3771 emit->index_range.count = 0;
3772 emit->index_range.operandType = VGPU10_NUM_OPERANDS;
3773 emit->index_range.start_index = INVALID_INDEX;
3774 emit->index_range.size = 0;
3775 emit->index_range.dim = 0;
3776
3777 return true;
3778 }
3779
3780
3781 /**
3782 * Emit a vgpu10 declaration "instruction".
3783 * \param index the register index
3784 * \param size array size of the operand. In most cases, it is 1,
3785 * but for inputs to geometry shader, the array size varies
3786 * depending on the primitive type.
3787 */
3788 static void
3789 emit_decl_instruction(struct svga_shader_emitter_v10 *emit,
3790 VGPU10OpcodeToken0 opcode0,
3791 VGPU10OperandToken0 operand0,
3792 VGPU10NameToken name_token,
3793 unsigned index, unsigned size)
3794 {
3795 assert(opcode0.opcodeType);
3796 assert(operand0.mask ||
3797 (operand0.operandType == VGPU10_OPERAND_TYPE_OUTPUT) ||
3798 (operand0.operandType == VGPU10_OPERAND_TYPE_OUTPUT_DEPTH) ||
3799 (operand0.operandType == VGPU10_OPERAND_TYPE_OUTPUT_COVERAGE_MASK) ||
3800 (operand0.operandType == VGPU10_OPERAND_TYPE_OUTPUT_CONTROL_POINT_ID) ||
3801 (operand0.operandType == VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID) ||
3802 (operand0.operandType == VGPU10_OPERAND_TYPE_INPUT_GS_INSTANCE_ID) ||
3803 (operand0.operandType == VGPU10_OPERAND_TYPE_INPUT_COVERAGE_MASK) ||
3804 (operand0.operandType == VGPU10_OPERAND_TYPE_STREAM));
3805
3806 begin_emit_instruction(emit);
3807 emit_dword(emit, opcode0.value);
3808
3809 emit_dword(emit, operand0.value);
3810
3811 if (operand0.indexDimension == VGPU10_OPERAND_INDEX_1D) {
3812 /* Next token is the index of the register to declare */
3813 emit_dword(emit, index);
3814 }
3815 else if (operand0.indexDimension >= VGPU10_OPERAND_INDEX_2D) {
3816 /* Next token is the size of the register */
3817 emit_dword(emit, size);
3818
3819 /* Followed by the index of the register */
3820 emit_dword(emit, index);
3821 }
3822
3823 if (name_token.value) {
3824 emit_dword(emit, name_token.value);
3825 }
3826
3827 end_emit_instruction(emit);
3828 }
3829
3830
3831 /**
3832 * Emit the declaration for a shader input.
3833 * \param opcodeType opcode type, one of VGPU10_OPCODE_DCL_INPUTx
3834 * \param operandType operand type, one of VGPU10_OPERAND_TYPE_INPUT_x
3835 * \param dim index dimension
3836 * \param index the input register index
3837 * \param size array size of the operand. In most cases, it is 1,
3838 * but for inputs to geometry shader, the array size varies
3839 * depending on the primitive type. For tessellation control
3840 * shader, the array size is the vertex count per patch.
3841 * \param name one of VGPU10_NAME_x
3842 * \parma numComp number of components
3843 * \param selMode component selection mode
3844 * \param usageMask bitfield of VGPU10_OPERAND_4_COMPONENT_MASK_x values
3845 * \param interpMode interpolation mode
3846 */
3847 static void
3848 emit_input_declaration(struct svga_shader_emitter_v10 *emit,
3849 VGPU10_OPCODE_TYPE opcodeType,
3850 VGPU10_OPERAND_TYPE operandType,
3851 VGPU10_OPERAND_INDEX_DIMENSION dim,
3852 unsigned index, unsigned size,
3853 VGPU10_SYSTEM_NAME name,
3854 VGPU10_OPERAND_NUM_COMPONENTS numComp,
3855 VGPU10_OPERAND_4_COMPONENT_SELECTION_MODE selMode,
3856 unsigned usageMask,
3857 VGPU10_INTERPOLATION_MODE interpMode,
3858 bool addSignature,
3859 SVGA3dDXSignatureSemanticName sgnName)
3860 {
3861 VGPU10OpcodeToken0 opcode0;
3862 VGPU10OperandToken0 operand0;
3863 VGPU10NameToken name_token;
3864
3865 assert(usageMask <= VGPU10_OPERAND_4_COMPONENT_MASK_ALL);
3866 assert(opcodeType == VGPU10_OPCODE_DCL_INPUT ||
3867 opcodeType == VGPU10_OPCODE_DCL_INPUT_SIV ||
3868 opcodeType == VGPU10_OPCODE_DCL_INPUT_SGV ||
3869 opcodeType == VGPU10_OPCODE_DCL_INPUT_PS ||
3870 opcodeType == VGPU10_OPCODE_DCL_INPUT_PS_SIV ||
3871 opcodeType == VGPU10_OPCODE_DCL_INPUT_PS_SGV);
3872 assert(operandType == VGPU10_OPERAND_TYPE_INPUT ||
3873 operandType == VGPU10_OPERAND_TYPE_INPUT_GS_INSTANCE_ID ||
3874 operandType == VGPU10_OPERAND_TYPE_INPUT_COVERAGE_MASK ||
3875 operandType == VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID ||
3876 operandType == VGPU10_OPERAND_TYPE_OUTPUT_CONTROL_POINT_ID ||
3877 operandType == VGPU10_OPERAND_TYPE_INPUT_DOMAIN_POINT ||
3878 operandType == VGPU10_OPERAND_TYPE_INPUT_CONTROL_POINT ||
3879 operandType == VGPU10_OPERAND_TYPE_INPUT_PATCH_CONSTANT ||
3880 operandType == VGPU10_OPERAND_TYPE_INPUT_THREAD_ID ||
3881 operandType == VGPU10_OPERAND_TYPE_INPUT_THREAD_GROUP_ID ||
3882 operandType == VGPU10_OPERAND_TYPE_INPUT_THREAD_ID_IN_GROUP);
3883
3884 assert(numComp <= VGPU10_OPERAND_4_COMPONENT);
3885 assert(selMode <= VGPU10_OPERAND_4_COMPONENT_MASK_MODE);
3886 assert(dim <= VGPU10_OPERAND_INDEX_3D);
3887 assert(name == VGPU10_NAME_UNDEFINED ||
3888 name == VGPU10_NAME_POSITION ||
3889 name == VGPU10_NAME_INSTANCE_ID ||
3890 name == VGPU10_NAME_VERTEX_ID ||
3891 name == VGPU10_NAME_PRIMITIVE_ID ||
3892 name == VGPU10_NAME_IS_FRONT_FACE ||
3893 name == VGPU10_NAME_SAMPLE_INDEX ||
3894 name == VGPU10_NAME_RENDER_TARGET_ARRAY_INDEX ||
3895 name == VGPU10_NAME_VIEWPORT_ARRAY_INDEX);
3896
3897 assert(interpMode == VGPU10_INTERPOLATION_UNDEFINED ||
3898 interpMode == VGPU10_INTERPOLATION_CONSTANT ||
3899 interpMode == VGPU10_INTERPOLATION_LINEAR ||
3900 interpMode == VGPU10_INTERPOLATION_LINEAR_CENTROID ||
3901 interpMode == VGPU10_INTERPOLATION_LINEAR_NOPERSPECTIVE ||
3902 interpMode == VGPU10_INTERPOLATION_LINEAR_NOPERSPECTIVE_CENTROID ||
3903 interpMode == VGPU10_INTERPOLATION_LINEAR_SAMPLE ||
3904 interpMode == VGPU10_INTERPOLATION_LINEAR_NOPERSPECTIVE_SAMPLE);
3905
3906 check_register_index(emit, opcodeType, index);
3907
3908 opcode0.value = operand0.value = name_token.value = 0;
3909
3910 opcode0.opcodeType = opcodeType;
3911 opcode0.interpolationMode = interpMode;
3912
3913 operand0.operandType = operandType;
3914 operand0.numComponents = numComp;
3915 operand0.selectionMode = selMode;
3916 operand0.mask = usageMask;
3917 operand0.indexDimension = dim;
3918 operand0.index0Representation = VGPU10_OPERAND_INDEX_IMMEDIATE32;
3919 if (dim == VGPU10_OPERAND_INDEX_2D)
3920 operand0.index1Representation = VGPU10_OPERAND_INDEX_IMMEDIATE32;
3921
3922 name_token.name = name;
3923
3924 emit_decl_instruction(emit, opcode0, operand0, name_token, index, size);
3925
3926 if (addSignature) {
3927 struct svga_shader_signature *sgn = &emit->signature;
3928 if (operandType == VGPU10_OPERAND_TYPE_INPUT_PATCH_CONSTANT) {
3929 /* Set patch constant signature */
3930 SVGA3dDXShaderSignatureEntry *sgnEntry =
3931 &sgn->patchConstants[sgn->header.numPatchConstantSignatures++];
3932 set_shader_signature_entry(sgnEntry, index,
3933 sgnName, usageMask,
3934 SVGADX_SIGNATURE_REGISTER_COMPONENT_UNKNOWN,
3935 SVGADX_SIGNATURE_MIN_PRECISION_DEFAULT);
3936
3937 } else if (operandType == VGPU10_OPERAND_TYPE_INPUT ||
3938 operandType == VGPU10_OPERAND_TYPE_INPUT_CONTROL_POINT) {
3939 /* Set input signature */
3940 SVGA3dDXShaderSignatureEntry *sgnEntry =
3941 &sgn->inputs[sgn->header.numInputSignatures++];
3942 set_shader_signature_entry(sgnEntry, index,
3943 sgnName, usageMask,
3944 SVGADX_SIGNATURE_REGISTER_COMPONENT_UNKNOWN,
3945 SVGADX_SIGNATURE_MIN_PRECISION_DEFAULT);
3946 }
3947 }
3948
3949 if (emit->index_range.required) {
3950 /* Here, index_range declaration is only applicable for opcodeType
3951 * VGPU10_OPCODE_DCL_INPUT and VGPU10_OPCODE_DCL_INPUT_PS and
3952 * for operandType VGPU10_OPERAND_TYPE_INPUT,
3953 * VGPU10_OPERAND_TYPE_INPUT_CONTROL_POINT and
3954 * VGPU10_OPERAND_TYPE_INPUT_PATCH_CONSTANT.
3955 */
3956 if ((opcodeType != VGPU10_OPCODE_DCL_INPUT &&
3957 opcodeType != VGPU10_OPCODE_DCL_INPUT_PS) ||
3958 (operandType != VGPU10_OPERAND_TYPE_INPUT &&
3959 operandType != VGPU10_OPERAND_TYPE_INPUT_CONTROL_POINT &&
3960 operandType != VGPU10_OPERAND_TYPE_INPUT_PATCH_CONSTANT)) {
3961 if (emit->index_range.start_index != INVALID_INDEX) {
3962 emit_index_range_declaration(emit);
3963 }
3964 return;
3965 }
3966
3967 if (emit->index_range.operandType == VGPU10_NUM_OPERANDS) {
3968 /* Need record new index_range */
3969 emit->index_range.count = 1;
3970 emit->index_range.operandType = operandType;
3971 emit->index_range.start_index = index;
3972 emit->index_range.size = size;
3973 emit->index_range.dim = dim;
3974 }
3975 else if (index !=
3976 (emit->index_range.start_index + emit->index_range.count) ||
3977 emit->index_range.operandType != operandType) {
3978 /* Input index is not contiguous with index range or operandType is
3979 * different from index range's operandType. We need to emit current
3980 * index_range first and then start recording next index range.
3981 */
3982 emit_index_range_declaration(emit);
3983
3984 emit->index_range.count = 1;
3985 emit->index_range.operandType = operandType;
3986 emit->index_range.start_index = index;
3987 emit->index_range.size = size;
3988 emit->index_range.dim = dim;
3989 }
3990 else if (emit->index_range.operandType == operandType) {
3991 /* Since input index is contiguous with index range and operandType
3992 * is same as index range's operandType, increment index range count.
3993 */
3994 emit->index_range.count++;
3995 }
3996 }
3997 }
3998
3999
4000 /**
4001 * Emit the declaration for a shader output.
4002 * \param type one of VGPU10_OPCODE_DCL_OUTPUTx
4003 * \param index the output register index
4004 * \param name one of VGPU10_NAME_x
4005 * \param usageMask bitfield of VGPU10_OPERAND_4_COMPONENT_MASK_x values
4006 */
4007 static void
4008 emit_output_declaration(struct svga_shader_emitter_v10 *emit,
4009 VGPU10_OPCODE_TYPE type, unsigned index,
4010 VGPU10_SYSTEM_NAME name,
4011 unsigned writemask,
4012 bool addSignature,
4013 SVGA3dDXSignatureSemanticName sgnName)
4014 {
4015 VGPU10OpcodeToken0 opcode0;
4016 VGPU10OperandToken0 operand0;
4017 VGPU10NameToken name_token;
4018
4019 assert(writemask <= VGPU10_OPERAND_4_COMPONENT_MASK_ALL);
4020 assert(type == VGPU10_OPCODE_DCL_OUTPUT ||
4021 type == VGPU10_OPCODE_DCL_OUTPUT_SGV ||
4022 type == VGPU10_OPCODE_DCL_OUTPUT_SIV);
4023 assert(name == VGPU10_NAME_UNDEFINED ||
4024 name == VGPU10_NAME_POSITION ||
4025 name == VGPU10_NAME_PRIMITIVE_ID ||
4026 name == VGPU10_NAME_RENDER_TARGET_ARRAY_INDEX ||
4027 name == VGPU10_NAME_VIEWPORT_ARRAY_INDEX ||
4028 name == VGPU10_NAME_CLIP_DISTANCE);
4029
4030 check_register_index(emit, type, index);
4031
4032 opcode0.value = operand0.value = name_token.value = 0;
4033
4034 opcode0.opcodeType = type;
4035 operand0.operandType = VGPU10_OPERAND_TYPE_OUTPUT;
4036 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
4037 operand0.selectionMode = VGPU10_OPERAND_4_COMPONENT_MASK_MODE;
4038 operand0.mask = writemask;
4039 operand0.indexDimension = VGPU10_OPERAND_INDEX_1D;
4040 operand0.index0Representation = VGPU10_OPERAND_INDEX_IMMEDIATE32;
4041
4042 name_token.name = name;
4043
4044 emit_decl_instruction(emit, opcode0, operand0, name_token, index, 1);
4045
4046 /* Capture output signature */
4047 if (addSignature) {
4048 struct svga_shader_signature *sgn = &emit->signature;
4049 SVGA3dDXShaderSignatureEntry *sgnEntry =
4050 &sgn->outputs[sgn->header.numOutputSignatures++];
4051 set_shader_signature_entry(sgnEntry, index,
4052 sgnName, writemask,
4053 SVGADX_SIGNATURE_REGISTER_COMPONENT_UNKNOWN,
4054 SVGADX_SIGNATURE_MIN_PRECISION_DEFAULT);
4055 }
4056
4057 if (emit->index_range.required) {
4058 /* Here, index_range declaration is only applicable for opcodeType
4059 * VGPU10_OPCODE_DCL_OUTPUT and for operandType
4060 * VGPU10_OPERAND_TYPE_OUTPUT.
4061 */
4062 if (type != VGPU10_OPCODE_DCL_OUTPUT) {
4063 if (emit->index_range.start_index != INVALID_INDEX) {
4064 emit_index_range_declaration(emit);
4065 }
4066 return;
4067 }
4068
4069 if (emit->index_range.operandType == VGPU10_NUM_OPERANDS) {
4070 /* Need record new index_range */
4071 emit->index_range.count = 1;
4072 emit->index_range.operandType = VGPU10_OPERAND_TYPE_OUTPUT;
4073 emit->index_range.start_index = index;
4074 emit->index_range.size = 1;
4075 emit->index_range.dim = VGPU10_OPERAND_INDEX_1D;
4076 }
4077 else if (index !=
4078 (emit->index_range.start_index + emit->index_range.count)) {
4079 /* Output index is not contiguous with index range. We need to
4080 * emit current index_range first and then start recording next
4081 * index range.
4082 */
4083 emit_index_range_declaration(emit);
4084
4085 emit->index_range.count = 1;
4086 emit->index_range.operandType = VGPU10_OPERAND_TYPE_OUTPUT;
4087 emit->index_range.start_index = index;
4088 emit->index_range.size = 1;
4089 emit->index_range.dim = VGPU10_OPERAND_INDEX_1D;
4090 }
4091 else {
4092 /* Since output index is contiguous with index range, increment
4093 * index range count.
4094 */
4095 emit->index_range.count++;
4096 }
4097 }
4098 }
4099
4100
4101 /**
4102 * Emit the declaration for the fragment depth output.
4103 */
4104 static void
4105 emit_fragdepth_output_declaration(struct svga_shader_emitter_v10 *emit)
4106 {
4107 VGPU10OpcodeToken0 opcode0;
4108 VGPU10OperandToken0 operand0;
4109 VGPU10NameToken name_token;
4110
4111 assert(emit->unit == MESA_SHADER_FRAGMENT);
4112
4113 opcode0.value = operand0.value = name_token.value = 0;
4114
4115 opcode0.opcodeType = VGPU10_OPCODE_DCL_OUTPUT;
4116 operand0.operandType = VGPU10_OPERAND_TYPE_OUTPUT_DEPTH;
4117 operand0.numComponents = VGPU10_OPERAND_1_COMPONENT;
4118 operand0.indexDimension = VGPU10_OPERAND_INDEX_0D;
4119 operand0.mask = 0;
4120
4121 emit_decl_instruction(emit, opcode0, operand0, name_token, 0, 1);
4122 }
4123
4124
4125 /**
4126 * Emit the declaration for the fragment sample mask/coverage output.
4127 */
4128 static void
4129 emit_samplemask_output_declaration(struct svga_shader_emitter_v10 *emit)
4130 {
4131 VGPU10OpcodeToken0 opcode0;
4132 VGPU10OperandToken0 operand0;
4133 VGPU10NameToken name_token;
4134
4135 assert(emit->unit == MESA_SHADER_FRAGMENT);
4136 assert(emit->version >= 41);
4137
4138 opcode0.value = operand0.value = name_token.value = 0;
4139
4140 opcode0.opcodeType = VGPU10_OPCODE_DCL_OUTPUT;
4141 operand0.operandType = VGPU10_OPERAND_TYPE_OUTPUT_COVERAGE_MASK;
4142 operand0.numComponents = VGPU10_OPERAND_0_COMPONENT;
4143 operand0.indexDimension = VGPU10_OPERAND_INDEX_0D;
4144 operand0.mask = 0;
4145
4146 emit_decl_instruction(emit, opcode0, operand0, name_token, 0, 1);
4147 }
4148
4149
4150 /**
4151 * Emit output declarations for fragment shader.
4152 */
4153 static void
4154 emit_fs_output_declarations(struct svga_shader_emitter_v10 *emit)
4155 {
4156 unsigned int i;
4157
4158 for (i = 0; i < emit->info.num_outputs; i++) {
4159 /*const unsigned usage_mask = emit->info.output_usage_mask[i];*/
4160 const enum tgsi_semantic semantic_name =
4161 emit->info.output_semantic_name[i];
4162 const unsigned semantic_index = emit->info.output_semantic_index[i];
4163 unsigned index = i;
4164
4165 if (semantic_name == TGSI_SEMANTIC_COLOR) {
4166 assert(semantic_index < ARRAY_SIZE(emit->fs.color_out_index));
4167
4168 emit->fs.color_out_index[semantic_index] = index;
4169
4170 emit->fs.num_color_outputs = MAX2(emit->fs.num_color_outputs,
4171 index + 1);
4172
4173 /* The semantic index is the shader's color output/buffer index */
4174 emit_output_declaration(emit,
4175 VGPU10_OPCODE_DCL_OUTPUT, semantic_index,
4176 VGPU10_NAME_UNDEFINED,
4177 VGPU10_OPERAND_4_COMPONENT_MASK_ALL,
4178 true,
4179 map_tgsi_semantic_to_sgn_name(semantic_name));
4180
4181 if (semantic_index == 0) {
4182 if (emit->key.fs.write_color0_to_n_cbufs > 1) {
4183 /* Emit declarations for the additional color outputs
4184 * for broadcasting.
4185 */
4186 unsigned j;
4187 for (j = 1; j < emit->key.fs.write_color0_to_n_cbufs; j++) {
4188 /* Allocate a new output index */
4189 unsigned idx = emit->info.num_outputs + j - 1;
4190 emit->fs.color_out_index[j] = idx;
4191 emit_output_declaration(emit,
4192 VGPU10_OPCODE_DCL_OUTPUT, idx,
4193 VGPU10_NAME_UNDEFINED,
4194 VGPU10_OPERAND_4_COMPONENT_MASK_ALL,
4195 true,
4196 map_tgsi_semantic_to_sgn_name(semantic_name));
4197 emit->info.output_semantic_index[idx] = j;
4198 }
4199
4200 emit->fs.num_color_outputs =
4201 emit->key.fs.write_color0_to_n_cbufs;
4202 }
4203 }
4204 }
4205 else if (semantic_name == TGSI_SEMANTIC_POSITION) {
4206 /* Fragment depth output */
4207 emit_fragdepth_output_declaration(emit);
4208 }
4209 else if (semantic_name == TGSI_SEMANTIC_SAMPLEMASK) {
4210 /* Sample mask output */
4211 emit_samplemask_output_declaration(emit);
4212 }
4213 else {
4214 assert(!"Bad output semantic name");
4215 }
4216 }
4217 }
4218
4219
4220 /**
4221 * Emit common output declaration for vertex processing.
4222 */
4223 static void
4224 emit_vertex_output_declaration(struct svga_shader_emitter_v10 *emit,
4225 unsigned index, unsigned writemask,
4226 bool addSignature)
4227 {
4228 const enum tgsi_semantic semantic_name =
4229 emit->info.output_semantic_name[index];
4230 const unsigned semantic_index = emit->info.output_semantic_index[index];
4231 unsigned name, type;
4232 unsigned final_mask = VGPU10_OPERAND_4_COMPONENT_MASK_ALL;
4233
4234 assert(emit->unit != MESA_SHADER_FRAGMENT &&
4235 emit->unit != MESA_SHADER_COMPUTE);
4236
4237 switch (semantic_name) {
4238 case TGSI_SEMANTIC_POSITION:
4239 if (emit->unit == MESA_SHADER_TESS_CTRL) {
4240 /* position will be declared in control point only */
4241 assert(emit->tcs.control_point_phase);
4242 type = VGPU10_OPCODE_DCL_OUTPUT;
4243 name = VGPU10_NAME_UNDEFINED;
4244 emit_output_declaration(emit, type, index, name, final_mask, true,
4245 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED);
4246 return;
4247 }
4248 else {
4249 type = VGPU10_OPCODE_DCL_OUTPUT_SIV;
4250 name = VGPU10_NAME_POSITION;
4251 }
4252 /* Save the index of the vertex position output register */
4253 emit->vposition.out_index = index;
4254 break;
4255 case TGSI_SEMANTIC_CLIPDIST:
4256 type = VGPU10_OPCODE_DCL_OUTPUT_SIV;
4257 name = VGPU10_NAME_CLIP_DISTANCE;
4258 /* save the starting index of the clip distance output register */
4259 if (semantic_index == 0)
4260 emit->clip_dist_out_index = index;
4261 final_mask = apply_clip_plane_mask(emit, writemask, semantic_index);
4262 if (final_mask == 0x0)
4263 return; /* discard this do-nothing declaration */
4264 break;
4265 case TGSI_SEMANTIC_CLIPVERTEX:
4266 type = VGPU10_OPCODE_DCL_OUTPUT;
4267 name = VGPU10_NAME_UNDEFINED;
4268 emit->clip_vertex_out_index = index;
4269 break;
4270 default:
4271 /* generic output */
4272 type = VGPU10_OPCODE_DCL_OUTPUT;
4273 name = VGPU10_NAME_UNDEFINED;
4274 }
4275
4276 emit_output_declaration(emit, type, index, name, final_mask, addSignature,
4277 map_tgsi_semantic_to_sgn_name(semantic_name));
4278 }
4279
4280
4281 /**
4282 * Emit declaration for outputs in vertex shader.
4283 */
4284 static void
4285 emit_vs_output_declarations(struct svga_shader_emitter_v10 *emit)
4286 {
4287 unsigned i;
4288 for (i = 0; i < emit->info.num_outputs; i++) {
4289 emit_vertex_output_declaration(emit, i, emit->output_usage_mask[i], true);
4290 }
4291 }
4292
4293
4294 /**
4295 * A helper function to determine the writemask for an output
4296 * for the specified stream.
4297 */
4298 static unsigned
4299 output_writemask_for_stream(unsigned stream, uint8_t output_streams,
4300 uint8_t output_usagemask)
4301 {
4302 unsigned i;
4303 unsigned writemask = 0;
4304
4305 for (i = 0; i < 4; i++) {
4306 if ((output_streams & 0x3) == stream)
4307 writemask |= (VGPU10_OPERAND_4_COMPONENT_MASK_X << i);
4308 output_streams >>= 2;
4309 }
4310 return writemask & output_usagemask;
4311 }
4312
4313
4314 /**
4315 * Emit declaration for outputs in geometry shader.
4316 */
4317 static void
4318 emit_gs_output_declarations(struct svga_shader_emitter_v10 *emit)
4319 {
4320 unsigned i;
4321 VGPU10OpcodeToken0 opcode0;
4322 unsigned numStreamsSupported = 1;
4323
4324 if (emit->version >= 50) {
4325 numStreamsSupported = ARRAY_SIZE(emit->info.num_stream_output_components);
4326 }
4327
4328 /**
4329 * Start emitting from the last stream first, so we end with
4330 * stream 0, so any of the auxiliary output declarations will
4331 * go to stream 0.
4332 */
4333 for (int s = numStreamsSupported-1; s >= 0; s--) {
4334
4335 if (emit->info.num_stream_output_components[s] == 0)
4336 continue;
4337
4338 if (emit->version >= 50) {
4339 /* DCL_STREAM stream */
4340 begin_emit_instruction(emit);
4341 emit_opcode(emit, VGPU10_OPCODE_DCL_STREAM, false);
4342 emit_stream_register(emit, s);
4343 end_emit_instruction(emit);
4344 }
4345
4346 /* emit output primitive topology declaration */
4347 opcode0.value = 0;
4348 opcode0.opcodeType = VGPU10_OPCODE_DCL_GS_OUTPUT_PRIMITIVE_TOPOLOGY;
4349 opcode0.primitiveTopology = emit->gs.prim_topology;
4350 emit_property_instruction(emit, opcode0, 0, 0);
4351
4352 for (unsigned i = 0; i < emit->info.num_outputs; i++) {
4353 /* find out the writemask for this stream */
4354 unsigned writemask =
4355 output_writemask_for_stream(s, emit->info.output_streams[i],
4356 emit->output_usage_mask[i]);
4357
4358 if (writemask) {
4359 enum tgsi_semantic semantic_name =
4360 emit->info.output_semantic_name[i];
4361
4362 /* TODO: Still need to take care of a special case where a
4363 * single varying spans across multiple output registers.
4364 */
4365 switch (semantic_name) {
4366 case TGSI_SEMANTIC_PRIMID:
4367 emit_output_declaration(emit,
4368 VGPU10_OPCODE_DCL_OUTPUT_SGV, i,
4369 VGPU10_NAME_PRIMITIVE_ID,
4370 VGPU10_OPERAND_4_COMPONENT_MASK_ALL,
4371 false,
4372 map_tgsi_semantic_to_sgn_name(semantic_name));
4373 break;
4374 case TGSI_SEMANTIC_LAYER:
4375 emit_output_declaration(emit,
4376 VGPU10_OPCODE_DCL_OUTPUT_SIV, i,
4377 VGPU10_NAME_RENDER_TARGET_ARRAY_INDEX,
4378 VGPU10_OPERAND_4_COMPONENT_MASK_X,
4379 false,
4380 map_tgsi_semantic_to_sgn_name(semantic_name));
4381 break;
4382 case TGSI_SEMANTIC_VIEWPORT_INDEX:
4383 emit_output_declaration(emit,
4384 VGPU10_OPCODE_DCL_OUTPUT_SIV, i,
4385 VGPU10_NAME_VIEWPORT_ARRAY_INDEX,
4386 VGPU10_OPERAND_4_COMPONENT_MASK_X,
4387 false,
4388 map_tgsi_semantic_to_sgn_name(semantic_name));
4389 emit->gs.viewport_index_out_index = i;
4390 break;
4391 default:
4392 emit_vertex_output_declaration(emit, i, writemask, false);
4393 }
4394 }
4395 }
4396 }
4397
4398 /* For geometry shader outputs, it is possible the same register is
4399 * declared multiple times for different streams. So to avoid
4400 * redundant signature entries, geometry shader output signature is done
4401 * outside of the declaration.
4402 */
4403 struct svga_shader_signature *sgn = &emit->signature;
4404 SVGA3dDXShaderSignatureEntry *sgnEntry;
4405
4406 for (i = 0; i < emit->info.num_outputs; i++) {
4407 if (emit->output_usage_mask[i]) {
4408 enum tgsi_semantic sem_name = emit->info.output_semantic_name[i];
4409
4410 sgnEntry = &sgn->outputs[sgn->header.numOutputSignatures++];
4411 set_shader_signature_entry(sgnEntry, i,
4412 map_tgsi_semantic_to_sgn_name(sem_name),
4413 emit->output_usage_mask[i],
4414 SVGADX_SIGNATURE_REGISTER_COMPONENT_UNKNOWN,
4415 SVGADX_SIGNATURE_MIN_PRECISION_DEFAULT);
4416 }
4417 }
4418 }
4419
4420
4421 /**
4422 * Emit the declaration for the tess inner/outer output.
4423 * \param opcodeType either VGPU10_OPCODE_DCL_OUTPUT_SIV or _INPUT_SIV
4424 * \param operandType either VGPU10_OPERAND_TYPE_OUTPUT or _INPUT
4425 * \param name VGPU10_NAME_FINAL_*_TESSFACTOR value
4426 */
4427 static void
4428 emit_tesslevel_declaration(struct svga_shader_emitter_v10 *emit,
4429 unsigned index, unsigned opcodeType,
4430 unsigned operandType, VGPU10_SYSTEM_NAME name,
4431 SVGA3dDXSignatureSemanticName sgnName)
4432 {
4433 VGPU10OpcodeToken0 opcode0;
4434 VGPU10OperandToken0 operand0;
4435 VGPU10NameToken name_token;
4436
4437 assert(emit->version >= 50);
4438 assert(name >= VGPU10_NAME_FINAL_QUAD_U_EQ_0_EDGE_TESSFACTOR ||
4439 (emit->key.tcs.prim_mode == MESA_PRIM_LINES &&
4440 name == VGPU10_NAME_UNDEFINED));
4441 assert(name <= VGPU10_NAME_FINAL_LINE_DENSITY_TESSFACTOR);
4442
4443 assert(operandType == VGPU10_OPERAND_TYPE_OUTPUT ||
4444 operandType == VGPU10_OPERAND_TYPE_INPUT_PATCH_CONSTANT);
4445
4446 opcode0.value = operand0.value = name_token.value = 0;
4447
4448 opcode0.opcodeType = opcodeType;
4449 operand0.operandType = operandType;
4450 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
4451 operand0.indexDimension = VGPU10_OPERAND_INDEX_1D;
4452 operand0.mask = VGPU10_OPERAND_4_COMPONENT_MASK_X;
4453 operand0.selectionMode = VGPU10_OPERAND_4_COMPONENT_MASK_MODE;
4454 operand0.index0Representation = VGPU10_OPERAND_INDEX_IMMEDIATE32;
4455
4456 name_token.name = name;
4457 emit_decl_instruction(emit, opcode0, operand0, name_token, index, 1);
4458
4459 /* Capture patch constant signature */
4460 struct svga_shader_signature *sgn = &emit->signature;
4461 SVGA3dDXShaderSignatureEntry *sgnEntry =
4462 &sgn->patchConstants[sgn->header.numPatchConstantSignatures++];
4463 set_shader_signature_entry(sgnEntry, index,
4464 sgnName, VGPU10_OPERAND_4_COMPONENT_MASK_X,
4465 SVGADX_SIGNATURE_REGISTER_COMPONENT_UNKNOWN,
4466 SVGADX_SIGNATURE_MIN_PRECISION_DEFAULT);
4467 }
4468
4469
4470 /**
4471 * Emit output declarations for tessellation control shader.
4472 */
4473 static void
4474 emit_tcs_output_declarations(struct svga_shader_emitter_v10 *emit)
4475 {
4476 unsigned int i;
4477 unsigned outputIndex = emit->num_outputs;
4478 struct svga_shader_signature *sgn = &emit->signature;
4479
4480 /**
4481 * Initialize patch_generic_out_count so it won't be counted twice
4482 * since this function is called twice, one for control point phase
4483 * and another time for patch constant phase.
4484 */
4485 emit->tcs.patch_generic_out_count = 0;
4486
4487 for (i = 0; i < emit->info.num_outputs; i++) {
4488 unsigned index = i;
4489 const enum tgsi_semantic semantic_name =
4490 emit->info.output_semantic_name[i];
4491
4492 switch (semantic_name) {
4493 case TGSI_SEMANTIC_TESSINNER:
4494 emit->tcs.inner.tgsi_index = i;
4495
4496 /* skip per-patch output declarations in control point phase */
4497 if (emit->tcs.control_point_phase)
4498 break;
4499
4500 emit->tcs.inner.out_index = outputIndex;
4501 switch (emit->key.tcs.prim_mode) {
4502 case MESA_PRIM_QUADS:
4503 emit_tesslevel_declaration(emit, outputIndex++,
4504 VGPU10_OPCODE_DCL_OUTPUT_SIV, VGPU10_OPERAND_TYPE_OUTPUT,
4505 VGPU10_NAME_FINAL_QUAD_U_INSIDE_TESSFACTOR,
4506 SVGADX_SIGNATURE_SEMANTIC_NAME_FINAL_QUAD_U_INSIDE_TESSFACTOR);
4507
4508 emit_tesslevel_declaration(emit, outputIndex++,
4509 VGPU10_OPCODE_DCL_OUTPUT_SIV, VGPU10_OPERAND_TYPE_OUTPUT,
4510 VGPU10_NAME_FINAL_QUAD_V_INSIDE_TESSFACTOR,
4511 SVGADX_SIGNATURE_SEMANTIC_NAME_FINAL_QUAD_V_INSIDE_TESSFACTOR);
4512 break;
4513 case MESA_PRIM_TRIANGLES:
4514 emit_tesslevel_declaration(emit, outputIndex++,
4515 VGPU10_OPCODE_DCL_OUTPUT_SIV, VGPU10_OPERAND_TYPE_OUTPUT,
4516 VGPU10_NAME_FINAL_TRI_INSIDE_TESSFACTOR,
4517 SVGADX_SIGNATURE_SEMANTIC_NAME_FINAL_TRI_INSIDE_TESSFACTOR);
4518 break;
4519 case MESA_PRIM_LINES:
4520 break;
4521 default:
4522 debug_printf("Unsupported primitive type");
4523 }
4524 break;
4525
4526 case TGSI_SEMANTIC_TESSOUTER:
4527 emit->tcs.outer.tgsi_index = i;
4528
4529 /* skip per-patch output declarations in control point phase */
4530 if (emit->tcs.control_point_phase)
4531 break;
4532
4533 emit->tcs.outer.out_index = outputIndex;
4534 switch (emit->key.tcs.prim_mode) {
4535 case MESA_PRIM_QUADS:
4536 for (int j = 0; j < 4; j++) {
4537 emit_tesslevel_declaration(emit, outputIndex++,
4538 VGPU10_OPCODE_DCL_OUTPUT_SIV, VGPU10_OPERAND_TYPE_OUTPUT,
4539 VGPU10_NAME_FINAL_QUAD_U_EQ_0_EDGE_TESSFACTOR + j,
4540 SVGADX_SIGNATURE_SEMANTIC_NAME_FINAL_QUAD_U_EQ_0_EDGE_TESSFACTOR + j);
4541 }
4542 break;
4543 case MESA_PRIM_TRIANGLES:
4544 for (int j = 0; j < 3; j++) {
4545 emit_tesslevel_declaration(emit, outputIndex++,
4546 VGPU10_OPCODE_DCL_OUTPUT_SIV, VGPU10_OPERAND_TYPE_OUTPUT,
4547 VGPU10_NAME_FINAL_TRI_U_EQ_0_EDGE_TESSFACTOR + j,
4548 SVGADX_SIGNATURE_SEMANTIC_NAME_FINAL_TRI_U_EQ_0_EDGE_TESSFACTOR + j);
4549 }
4550 break;
4551 case MESA_PRIM_LINES:
4552 for (int j = 0; j < 2; j++) {
4553 emit_tesslevel_declaration(emit, outputIndex++,
4554 VGPU10_OPCODE_DCL_OUTPUT_SIV, VGPU10_OPERAND_TYPE_OUTPUT,
4555 VGPU10_NAME_FINAL_LINE_DETAIL_TESSFACTOR + j,
4556 SVGADX_SIGNATURE_SEMANTIC_NAME_FINAL_LINE_DETAIL_TESSFACTOR + j);
4557 }
4558 break;
4559 default:
4560 debug_printf("Unsupported primitive type");
4561 }
4562 break;
4563
4564 case TGSI_SEMANTIC_PATCH:
4565 if (emit->tcs.patch_generic_out_index == INVALID_INDEX)
4566 emit->tcs.patch_generic_out_index= i;
4567 emit->tcs.patch_generic_out_count++;
4568
4569 /* skip per-patch output declarations in control point phase */
4570 if (emit->tcs.control_point_phase)
4571 break;
4572
4573 emit_output_declaration(emit, VGPU10_OPCODE_DCL_OUTPUT, index,
4574 VGPU10_NAME_UNDEFINED,
4575 VGPU10_OPERAND_4_COMPONENT_MASK_ALL,
4576 false,
4577 map_tgsi_semantic_to_sgn_name(semantic_name));
4578
4579 SVGA3dDXShaderSignatureEntry *sgnEntry =
4580 &sgn->patchConstants[sgn->header.numPatchConstantSignatures++];
4581 set_shader_signature_entry(sgnEntry, index,
4582 map_tgsi_semantic_to_sgn_name(semantic_name),
4583 VGPU10_OPERAND_4_COMPONENT_MASK_ALL,
4584 SVGADX_SIGNATURE_REGISTER_COMPONENT_UNKNOWN,
4585 SVGADX_SIGNATURE_MIN_PRECISION_DEFAULT);
4586
4587 break;
4588
4589 default:
4590 /* save the starting index of control point outputs */
4591 if (emit->tcs.control_point_out_index == INVALID_INDEX)
4592 emit->tcs.control_point_out_index = i;
4593 emit->tcs.control_point_out_count++;
4594
4595 /* skip control point output declarations in patch constant phase */
4596 if (!emit->tcs.control_point_phase)
4597 break;
4598
4599 emit_vertex_output_declaration(emit, i, emit->output_usage_mask[i],
4600 true);
4601
4602 }
4603 }
4604
4605 if (emit->tcs.control_point_phase) {
4606 /**
4607 * Add missing control point output in control point phase.
4608 */
4609 if (emit->tcs.control_point_out_index == INVALID_INDEX) {
4610 /* use register index after tessellation factors */
4611 switch (emit->key.tcs.prim_mode) {
4612 case MESA_PRIM_QUADS:
4613 emit->tcs.control_point_out_index = outputIndex + 6;
4614 break;
4615 case MESA_PRIM_TRIANGLES:
4616 emit->tcs.control_point_out_index = outputIndex + 4;
4617 break;
4618 default:
4619 emit->tcs.control_point_out_index = outputIndex + 2;
4620 break;
4621 }
4622 emit->tcs.control_point_out_count++;
4623 emit_output_declaration(emit, VGPU10_OPCODE_DCL_OUTPUT_SIV,
4624 emit->tcs.control_point_out_index,
4625 VGPU10_NAME_POSITION,
4626 VGPU10_OPERAND_4_COMPONENT_MASK_ALL,
4627 true,
4628 SVGADX_SIGNATURE_SEMANTIC_NAME_POSITION);
4629
4630 /* If tcs does not output any control point output,
4631 * we can end the hull shader control point phase here
4632 * after emitting the default control point output.
4633 */
4634 emit->skip_instruction = true;
4635 }
4636 }
4637 else {
4638 if (emit->tcs.outer.out_index == INVALID_INDEX) {
4639 /* since the TCS did not declare out outer tess level output register,
4640 * we declare it here for patch constant phase only.
4641 */
4642 emit->tcs.outer.out_index = outputIndex;
4643 if (emit->key.tcs.prim_mode == MESA_PRIM_QUADS) {
4644 for (int i = 0; i < 4; i++) {
4645 emit_tesslevel_declaration(emit, outputIndex++,
4646 VGPU10_OPCODE_DCL_OUTPUT_SIV, VGPU10_OPERAND_TYPE_OUTPUT,
4647 VGPU10_NAME_FINAL_QUAD_U_EQ_0_EDGE_TESSFACTOR + i,
4648 SVGADX_SIGNATURE_SEMANTIC_NAME_FINAL_QUAD_U_EQ_0_EDGE_TESSFACTOR + i);
4649 }
4650 }
4651 else if (emit->key.tcs.prim_mode == MESA_PRIM_TRIANGLES) {
4652 for (int i = 0; i < 3; i++) {
4653 emit_tesslevel_declaration(emit, outputIndex++,
4654 VGPU10_OPCODE_DCL_OUTPUT_SIV, VGPU10_OPERAND_TYPE_OUTPUT,
4655 VGPU10_NAME_FINAL_TRI_U_EQ_0_EDGE_TESSFACTOR + i,
4656 SVGADX_SIGNATURE_SEMANTIC_NAME_FINAL_TRI_U_EQ_0_EDGE_TESSFACTOR + i);
4657 }
4658 }
4659 }
4660
4661 if (emit->tcs.inner.out_index == INVALID_INDEX) {
4662 /* since the TCS did not declare out inner tess level output register,
4663 * we declare it here
4664 */
4665 emit->tcs.inner.out_index = outputIndex;
4666 if (emit->key.tcs.prim_mode == MESA_PRIM_QUADS) {
4667 emit_tesslevel_declaration(emit, outputIndex++,
4668 VGPU10_OPCODE_DCL_OUTPUT_SIV, VGPU10_OPERAND_TYPE_OUTPUT,
4669 VGPU10_NAME_FINAL_QUAD_U_INSIDE_TESSFACTOR,
4670 SVGADX_SIGNATURE_SEMANTIC_NAME_FINAL_QUAD_U_INSIDE_TESSFACTOR);
4671 emit_tesslevel_declaration(emit, outputIndex++,
4672 VGPU10_OPCODE_DCL_OUTPUT_SIV, VGPU10_OPERAND_TYPE_OUTPUT,
4673 VGPU10_NAME_FINAL_QUAD_V_INSIDE_TESSFACTOR,
4674 SVGADX_SIGNATURE_SEMANTIC_NAME_FINAL_QUAD_V_INSIDE_TESSFACTOR);
4675 }
4676 else if (emit->key.tcs.prim_mode == MESA_PRIM_TRIANGLES) {
4677 emit_tesslevel_declaration(emit, outputIndex++,
4678 VGPU10_OPCODE_DCL_OUTPUT_SIV, VGPU10_OPERAND_TYPE_OUTPUT,
4679 VGPU10_NAME_FINAL_TRI_INSIDE_TESSFACTOR,
4680 SVGADX_SIGNATURE_SEMANTIC_NAME_FINAL_TRI_INSIDE_TESSFACTOR);
4681 }
4682 }
4683 }
4684 emit->num_outputs = outputIndex;
4685 }
4686
4687
4688 /**
4689 * Emit output declarations for tessellation evaluation shader.
4690 */
4691 static void
4692 emit_tes_output_declarations(struct svga_shader_emitter_v10 *emit)
4693 {
4694 unsigned int i;
4695
4696 for (i = 0; i < emit->info.num_outputs; i++) {
4697 emit_vertex_output_declaration(emit, i, emit->output_usage_mask[i], true);
4698 }
4699 }
4700
4701
4702 /**
4703 * Emit the declaration for a system value input/output.
4704 */
4705 static void
4706 emit_system_value_declaration(struct svga_shader_emitter_v10 *emit,
4707 enum tgsi_semantic semantic_name, unsigned index)
4708 {
4709 switch (semantic_name) {
4710 case TGSI_SEMANTIC_INSTANCEID:
4711 index = alloc_system_value_index(emit, index);
4712 emit_input_declaration(emit, VGPU10_OPCODE_DCL_INPUT_SIV,
4713 VGPU10_OPERAND_TYPE_INPUT,
4714 VGPU10_OPERAND_INDEX_1D,
4715 index, 1,
4716 VGPU10_NAME_INSTANCE_ID,
4717 VGPU10_OPERAND_4_COMPONENT,
4718 VGPU10_OPERAND_4_COMPONENT_MASK_MODE,
4719 VGPU10_OPERAND_4_COMPONENT_MASK_X,
4720 VGPU10_INTERPOLATION_UNDEFINED, true,
4721 map_tgsi_semantic_to_sgn_name(semantic_name));
4722 break;
4723 case TGSI_SEMANTIC_VERTEXID:
4724 emit->vs.vertex_id_sys_index = index;
4725 index = alloc_system_value_index(emit, index);
4726 emit_input_declaration(emit, VGPU10_OPCODE_DCL_INPUT_SIV,
4727 VGPU10_OPERAND_TYPE_INPUT,
4728 VGPU10_OPERAND_INDEX_1D,
4729 index, 1,
4730 VGPU10_NAME_VERTEX_ID,
4731 VGPU10_OPERAND_4_COMPONENT,
4732 VGPU10_OPERAND_4_COMPONENT_MASK_MODE,
4733 VGPU10_OPERAND_4_COMPONENT_MASK_X,
4734 VGPU10_INTERPOLATION_UNDEFINED, true,
4735 map_tgsi_semantic_to_sgn_name(semantic_name));
4736 break;
4737 case TGSI_SEMANTIC_SAMPLEID:
4738 assert(emit->unit == MESA_SHADER_FRAGMENT);
4739 emit->fs.sample_id_sys_index = index;
4740 index = alloc_system_value_index(emit, index);
4741 emit_input_declaration(emit, VGPU10_OPCODE_DCL_INPUT_PS_SIV,
4742 VGPU10_OPERAND_TYPE_INPUT,
4743 VGPU10_OPERAND_INDEX_1D,
4744 index, 1,
4745 VGPU10_NAME_SAMPLE_INDEX,
4746 VGPU10_OPERAND_4_COMPONENT,
4747 VGPU10_OPERAND_4_COMPONENT_MASK_MODE,
4748 VGPU10_OPERAND_4_COMPONENT_MASK_X,
4749 VGPU10_INTERPOLATION_CONSTANT, true,
4750 map_tgsi_semantic_to_sgn_name(semantic_name));
4751 break;
4752 case TGSI_SEMANTIC_SAMPLEPOS:
4753 /* This system value contains the position of the current sample
4754 * when using per-sample shading. We implement this by calling
4755 * the VGPU10_OPCODE_SAMPLE_POS instruction with the current sample
4756 * index as the argument. See emit_sample_position_instructions().
4757 */
4758 assert(emit->version >= 41);
4759 emit->fs.sample_pos_sys_index = index;
4760 index = alloc_system_value_index(emit, index);
4761 break;
4762 case TGSI_SEMANTIC_INVOCATIONID:
4763 /* Note: invocation id input is mapped to different register depending
4764 * on the shader type. In GS, it will be mapped to vGSInstanceID#.
4765 * In TCS, it will be mapped to vOutputControlPointID#.
4766 * Since in both cases, the mapped name is unique rather than
4767 * just a generic input name ("v#"), so there is no need to remap
4768 * the index value.
4769 */
4770 assert(emit->unit == MESA_SHADER_GEOMETRY ||
4771 emit->unit == MESA_SHADER_TESS_CTRL);
4772 assert(emit->version >= 50);
4773
4774 if (emit->unit == MESA_SHADER_GEOMETRY) {
4775 emit->gs.invocation_id_sys_index = index;
4776 emit_input_declaration(emit, VGPU10_OPCODE_DCL_INPUT,
4777 VGPU10_OPERAND_TYPE_INPUT_GS_INSTANCE_ID,
4778 VGPU10_OPERAND_INDEX_0D,
4779 index, 1,
4780 VGPU10_NAME_UNDEFINED,
4781 VGPU10_OPERAND_0_COMPONENT,
4782 VGPU10_OPERAND_4_COMPONENT_MASK_MODE,
4783 0,
4784 VGPU10_INTERPOLATION_UNDEFINED, true,
4785 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED);
4786 } else if (emit->unit == MESA_SHADER_TESS_CTRL) {
4787 /* The emission of the control point id will be done
4788 * in the control point phase in emit_hull_shader_control_point_phase().
4789 */
4790 emit->tcs.invocation_id_sys_index = index;
4791 }
4792 break;
4793 case TGSI_SEMANTIC_SAMPLEMASK:
4794 /* Note: the PS sample mask input has a unique name ("vCoverage#")
4795 * rather than just a generic input name ("v#") so no need to remap the
4796 * index value.
4797 */
4798 assert(emit->unit == MESA_SHADER_FRAGMENT);
4799 assert(emit->version >= 50);
4800 emit->fs.sample_mask_in_sys_index = index;
4801 emit_input_declaration(emit, VGPU10_OPCODE_DCL_INPUT,
4802 VGPU10_OPERAND_TYPE_INPUT_COVERAGE_MASK,
4803 VGPU10_OPERAND_INDEX_0D,
4804 index, 1,
4805 VGPU10_NAME_UNDEFINED,
4806 VGPU10_OPERAND_1_COMPONENT,
4807 VGPU10_OPERAND_4_COMPONENT_MASK_MODE,
4808 0,
4809 VGPU10_INTERPOLATION_CONSTANT, true,
4810 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED);
4811 break;
4812 case TGSI_SEMANTIC_TESSCOORD:
4813 assert(emit->version >= 50);
4814
4815 unsigned usageMask = 0;
4816
4817 if (emit->tes.prim_mode == MESA_PRIM_TRIANGLES) {
4818 usageMask = VGPU10_OPERAND_4_COMPONENT_MASK_XYZ;
4819 }
4820 else if (emit->tes.prim_mode == MESA_PRIM_LINES ||
4821 emit->tes.prim_mode == MESA_PRIM_QUADS) {
4822 usageMask = VGPU10_OPERAND_4_COMPONENT_MASK_XY;
4823 }
4824
4825 emit->tes.tesscoord_sys_index = index;
4826 emit_input_declaration(emit, VGPU10_OPCODE_DCL_INPUT,
4827 VGPU10_OPERAND_TYPE_INPUT_DOMAIN_POINT,
4828 VGPU10_OPERAND_INDEX_0D,
4829 index, 1,
4830 VGPU10_NAME_UNDEFINED,
4831 VGPU10_OPERAND_4_COMPONENT,
4832 VGPU10_OPERAND_4_COMPONENT_MASK_MODE,
4833 usageMask,
4834 VGPU10_INTERPOLATION_UNDEFINED, true,
4835 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED);
4836 break;
4837 case TGSI_SEMANTIC_TESSINNER:
4838 assert(emit->version >= 50);
4839 emit->tes.inner.tgsi_index = index;
4840 break;
4841 case TGSI_SEMANTIC_TESSOUTER:
4842 assert(emit->version >= 50);
4843 emit->tes.outer.tgsi_index = index;
4844 break;
4845 case TGSI_SEMANTIC_VERTICESIN:
4846 assert(emit->unit == MESA_SHADER_TESS_CTRL);
4847 assert(emit->version >= 50);
4848
4849 /* save the system value index */
4850 emit->tcs.vertices_per_patch_index = index;
4851 break;
4852 case TGSI_SEMANTIC_PRIMID:
4853 assert(emit->version >= 50);
4854 if (emit->unit == MESA_SHADER_TESS_CTRL) {
4855 emit->tcs.prim_id_index = index;
4856 }
4857 else if (emit->unit == MESA_SHADER_TESS_EVAL) {
4858 emit->tes.prim_id_index = index;
4859 emit_input_declaration(emit, VGPU10_OPCODE_DCL_INPUT,
4860 VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID,
4861 VGPU10_OPERAND_INDEX_0D,
4862 index, 1,
4863 VGPU10_NAME_UNDEFINED,
4864 VGPU10_OPERAND_0_COMPONENT,
4865 VGPU10_OPERAND_4_COMPONENT_MASK_MODE,
4866 0,
4867 VGPU10_INTERPOLATION_UNDEFINED, true,
4868 map_tgsi_semantic_to_sgn_name(semantic_name));
4869 }
4870 break;
4871 case TGSI_SEMANTIC_THREAD_ID:
4872 assert(emit->unit >= MESA_SHADER_COMPUTE);
4873 assert(emit->version >= 50);
4874 emit->cs.thread_id_index = index;
4875 emit_input_declaration(emit, VGPU10_OPCODE_DCL_INPUT,
4876 VGPU10_OPERAND_TYPE_INPUT_THREAD_ID_IN_GROUP,
4877 VGPU10_OPERAND_INDEX_0D,
4878 index, 1,
4879 VGPU10_NAME_UNDEFINED,
4880 VGPU10_OPERAND_4_COMPONENT,
4881 VGPU10_OPERAND_4_COMPONENT_MASK_MODE,
4882 VGPU10_OPERAND_4_COMPONENT_MASK_ALL,
4883 VGPU10_INTERPOLATION_UNDEFINED, true,
4884 map_tgsi_semantic_to_sgn_name(semantic_name));
4885 break;
4886 case TGSI_SEMANTIC_BLOCK_ID:
4887 assert(emit->unit >= MESA_SHADER_COMPUTE);
4888 assert(emit->version >= 50);
4889 emit->cs.block_id_index = index;
4890 emit_input_declaration(emit, VGPU10_OPCODE_DCL_INPUT,
4891 VGPU10_OPERAND_TYPE_INPUT_THREAD_GROUP_ID,
4892 VGPU10_OPERAND_INDEX_0D,
4893 index, 1,
4894 VGPU10_NAME_UNDEFINED,
4895 VGPU10_OPERAND_4_COMPONENT,
4896 VGPU10_OPERAND_4_COMPONENT_MASK_MODE,
4897 VGPU10_OPERAND_4_COMPONENT_MASK_ALL,
4898 VGPU10_INTERPOLATION_UNDEFINED, true,
4899 map_tgsi_semantic_to_sgn_name(semantic_name));
4900 break;
4901 case TGSI_SEMANTIC_GRID_SIZE:
4902 assert(emit->unit == MESA_SHADER_COMPUTE);
4903 assert(emit->version >= 50);
4904 emit->cs.grid_size.tgsi_index = index;
4905 break;
4906 default:
4907 debug_printf("unexpected system value semantic index %u / %s\n",
4908 semantic_name, tgsi_semantic_names[semantic_name]);
4909 }
4910 }
4911
4912 /**
4913 * Translate a TGSI declaration to VGPU10.
4914 */
4915 static bool
4916 emit_vgpu10_declaration(struct svga_shader_emitter_v10 *emit,
4917 const struct tgsi_full_declaration *decl)
4918 {
4919 switch (decl->Declaration.File) {
4920 case TGSI_FILE_INPUT:
4921 /* do nothing - see emit_input_declarations() */
4922 return true;
4923
4924 case TGSI_FILE_OUTPUT:
4925 assert(decl->Range.First == decl->Range.Last);
4926 emit->output_usage_mask[decl->Range.First] = decl->Declaration.UsageMask;
4927 return true;
4928
4929 case TGSI_FILE_TEMPORARY:
4930 /* Don't declare the temps here. Just keep track of how many
4931 * and emit the declaration later.
4932 */
4933 if (decl->Declaration.Array) {
4934 /* Indexed temporary array. Save the start index of the array
4935 * and the size of the array.
4936 */
4937 const unsigned arrayID = MIN2(decl->Array.ArrayID, MAX_TEMP_ARRAYS);
4938 assert(arrayID < ARRAY_SIZE(emit->temp_arrays));
4939
4940 /* Save this array so we can emit the declaration for it later */
4941 create_temp_array(emit, arrayID, decl->Range.First,
4942 decl->Range.Last - decl->Range.First + 1,
4943 decl->Range.First);
4944 }
4945
4946 /* for all temps, indexed or not, keep track of highest index */
4947 emit->num_shader_temps = MAX2(emit->num_shader_temps,
4948 decl->Range.Last + 1);
4949 return true;
4950
4951 case TGSI_FILE_CONSTANT:
4952 /* Don't declare constants here. Just keep track and emit later. */
4953 {
4954 unsigned constbuf = 0, num_consts;
4955 if (decl->Declaration.Dimension) {
4956 constbuf = decl->Dim.Index2D;
4957 }
4958 /* We throw an assertion here when, in fact, the shader should never
4959 * have linked due to constbuf index out of bounds, so we shouldn't
4960 * have reached here.
4961 */
4962 assert(constbuf < ARRAY_SIZE(emit->num_shader_consts));
4963
4964 num_consts = MAX2(emit->num_shader_consts[constbuf],
4965 decl->Range.Last + 1);
4966
4967 if (num_consts > VGPU10_MAX_CONSTANT_BUFFER_ELEMENT_COUNT) {
4968 debug_printf("Warning: constant buffer is declared to size [%u]"
4969 " but [%u] is the limit.\n",
4970 num_consts,
4971 VGPU10_MAX_CONSTANT_BUFFER_ELEMENT_COUNT);
4972 emit->register_overflow = true;
4973 }
4974 /* The linker doesn't enforce the max UBO size so we clamp here */
4975 emit->num_shader_consts[constbuf] =
4976 MIN2(num_consts, VGPU10_MAX_CONSTANT_BUFFER_ELEMENT_COUNT);
4977 }
4978 return true;
4979
4980 case TGSI_FILE_IMMEDIATE:
4981 assert(!"TGSI_FILE_IMMEDIATE not handled yet!");
4982 return false;
4983
4984 case TGSI_FILE_SYSTEM_VALUE:
4985 emit_system_value_declaration(emit, decl->Semantic.Name,
4986 decl->Range.First);
4987 return true;
4988
4989 case TGSI_FILE_SAMPLER:
4990 /* Don't declare samplers here. Just keep track and emit later. */
4991 emit->num_samplers = MAX2(emit->num_samplers, decl->Range.Last + 1);
4992 return true;
4993
4994 #if 0
4995 case TGSI_FILE_RESOURCE:
4996 /*opcode0.opcodeType = VGPU10_OPCODE_DCL_RESOURCE;*/
4997 /* XXX more, VGPU10_RETURN_TYPE_FLOAT */
4998 assert(!"TGSI_FILE_RESOURCE not handled yet");
4999 return false;
5000 #endif
5001
5002 case TGSI_FILE_ADDRESS:
5003 emit->num_address_regs = MAX2(emit->num_address_regs,
5004 decl->Range.Last + 1);
5005 return true;
5006
5007 case TGSI_FILE_SAMPLER_VIEW:
5008 {
5009 unsigned unit = decl->Range.First;
5010 assert(decl->Range.First == decl->Range.Last);
5011 emit->sampler_target[unit] = decl->SamplerView.Resource;
5012
5013 /* Note: we can ignore YZW return types for now */
5014 emit->sampler_return_type[unit] = decl->SamplerView.ReturnTypeX;
5015 emit->sampler_view[unit] = true;
5016 }
5017 return true;
5018
5019 case TGSI_FILE_IMAGE:
5020 {
5021 unsigned unit = decl->Range.First;
5022 assert(decl->Range.First == decl->Range.Last);
5023 assert(unit < PIPE_MAX_SHADER_IMAGES);
5024 emit->image[unit] = decl->Image;
5025 emit->image_mask |= 1 << unit;
5026 emit->num_images++;
5027 }
5028 return true;
5029
5030 case TGSI_FILE_HW_ATOMIC:
5031 /* Declare the atomic buffer if it is not already declared. */
5032 if (!(emit->atomic_bufs_mask & (1 << decl->Dim.Index2D))) {
5033 emit->num_atomic_bufs++;
5034 emit->atomic_bufs_mask |= (1 << decl->Dim.Index2D);
5035 }
5036
5037 /* Remember the maximum atomic counter index encountered */
5038 emit->max_atomic_counter_index =
5039 MAX2(emit->max_atomic_counter_index, decl->Range.Last);
5040 return true;
5041
5042 case TGSI_FILE_MEMORY:
5043 /* Record memory has been used. */
5044 if (emit->unit == MESA_SHADER_COMPUTE &&
5045 decl->Declaration.MemType == TGSI_MEMORY_TYPE_SHARED) {
5046 emit->cs.shared_memory_declared = true;
5047 }
5048
5049 return true;
5050
5051 case TGSI_FILE_BUFFER:
5052 assert(emit->version >= 50);
5053 emit->num_shader_bufs++;
5054 return true;
5055
5056 default:
5057 assert(!"Unexpected type of declaration");
5058 return false;
5059 }
5060 }
5061
5062
5063 /**
5064 * Emit input declarations for fragment shader.
5065 */
5066 static void
5067 emit_fs_input_declarations(struct svga_shader_emitter_v10 *emit)
5068 {
5069 unsigned i;
5070
5071 for (i = 0; i < emit->linkage.num_inputs; i++) {
5072 enum tgsi_semantic semantic_name = emit->info.input_semantic_name[i];
5073 unsigned usage_mask = emit->info.input_usage_mask[i];
5074 unsigned index = emit->linkage.input_map[i];
5075 unsigned type, interpolationMode, name;
5076 unsigned mask = VGPU10_OPERAND_4_COMPONENT_MASK_ALL;
5077
5078 if (usage_mask == 0)
5079 continue; /* register is not actually used */
5080
5081 if (semantic_name == TGSI_SEMANTIC_POSITION) {
5082 /* fragment position input */
5083 type = VGPU10_OPCODE_DCL_INPUT_PS_SGV;
5084 interpolationMode = VGPU10_INTERPOLATION_LINEAR;
5085 name = VGPU10_NAME_POSITION;
5086 if (usage_mask & TGSI_WRITEMASK_W) {
5087 /* we need to replace use of 'w' with '1/w' */
5088 emit->fs.fragcoord_input_index = i;
5089 }
5090 }
5091 else if (semantic_name == TGSI_SEMANTIC_FACE) {
5092 /* fragment front-facing input */
5093 type = VGPU10_OPCODE_DCL_INPUT_PS_SGV;
5094 interpolationMode = VGPU10_INTERPOLATION_CONSTANT;
5095 name = VGPU10_NAME_IS_FRONT_FACE;
5096 emit->fs.face_input_index = i;
5097 }
5098 else if (semantic_name == TGSI_SEMANTIC_PRIMID) {
5099 /* primitive ID */
5100 type = VGPU10_OPCODE_DCL_INPUT_PS_SGV;
5101 interpolationMode = VGPU10_INTERPOLATION_CONSTANT;
5102 name = VGPU10_NAME_PRIMITIVE_ID;
5103 }
5104 else if (semantic_name == TGSI_SEMANTIC_SAMPLEID) {
5105 /* sample index / ID */
5106 type = VGPU10_OPCODE_DCL_INPUT_PS_SGV;
5107 interpolationMode = VGPU10_INTERPOLATION_CONSTANT;
5108 name = VGPU10_NAME_SAMPLE_INDEX;
5109 }
5110 else if (semantic_name == TGSI_SEMANTIC_LAYER) {
5111 /* render target array index */
5112 if (emit->key.fs.layer_to_zero) {
5113 /**
5114 * The shader from the previous stage does not write to layer,
5115 * so reading the layer index in fragment shader should return 0.
5116 */
5117 emit->fs.layer_input_index = i;
5118 continue;
5119 } else {
5120 type = VGPU10_OPCODE_DCL_INPUT_PS_SGV;
5121 interpolationMode = VGPU10_INTERPOLATION_CONSTANT;
5122 name = VGPU10_NAME_RENDER_TARGET_ARRAY_INDEX;
5123 mask = VGPU10_OPERAND_4_COMPONENT_MASK_X;
5124 }
5125 }
5126 else if (semantic_name == TGSI_SEMANTIC_VIEWPORT_INDEX) {
5127 /* viewport index */
5128 type = VGPU10_OPCODE_DCL_INPUT_PS_SGV;
5129 interpolationMode = VGPU10_INTERPOLATION_CONSTANT;
5130 name = VGPU10_NAME_VIEWPORT_ARRAY_INDEX;
5131 mask = VGPU10_OPERAND_4_COMPONENT_MASK_X;
5132 }
5133 else {
5134 /* general fragment input */
5135 type = VGPU10_OPCODE_DCL_INPUT_PS;
5136 interpolationMode =
5137 translate_interpolation(emit,
5138 emit->info.input_interpolate[i],
5139 emit->info.input_interpolate_loc[i]);
5140
5141 /* keeps track if flat interpolation mode is being used */
5142 emit->uses_flat_interp = emit->uses_flat_interp ||
5143 (interpolationMode == VGPU10_INTERPOLATION_CONSTANT);
5144
5145 name = VGPU10_NAME_UNDEFINED;
5146 }
5147
5148 emit_input_declaration(emit, type,
5149 VGPU10_OPERAND_TYPE_INPUT,
5150 VGPU10_OPERAND_INDEX_1D, index, 1,
5151 name,
5152 VGPU10_OPERAND_4_COMPONENT,
5153 VGPU10_OPERAND_4_COMPONENT_MASK_MODE,
5154 mask,
5155 interpolationMode, true,
5156 map_tgsi_semantic_to_sgn_name(semantic_name));
5157 }
5158 }
5159
5160
5161 /**
5162 * Emit input declarations for vertex shader.
5163 */
5164 static void
5165 emit_vs_input_declarations(struct svga_shader_emitter_v10 *emit)
5166 {
5167 unsigned i;
5168
5169 for (i = 0; i < emit->info.file_max[TGSI_FILE_INPUT] + 1; i++) {
5170 unsigned usage_mask = emit->info.input_usage_mask[i];
5171 unsigned index = i;
5172
5173 if (usage_mask == 0)
5174 continue; /* register is not actually used */
5175
5176 emit_input_declaration(emit, VGPU10_OPCODE_DCL_INPUT,
5177 VGPU10_OPERAND_TYPE_INPUT,
5178 VGPU10_OPERAND_INDEX_1D, index, 1,
5179 VGPU10_NAME_UNDEFINED,
5180 VGPU10_OPERAND_4_COMPONENT,
5181 VGPU10_OPERAND_4_COMPONENT_MASK_MODE,
5182 VGPU10_OPERAND_4_COMPONENT_MASK_ALL,
5183 VGPU10_INTERPOLATION_UNDEFINED, true,
5184 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED);
5185 }
5186 }
5187
5188
5189 /**
5190 * Emit input declarations for geometry shader.
5191 */
5192 static void
5193 emit_gs_input_declarations(struct svga_shader_emitter_v10 *emit)
5194 {
5195 unsigned i;
5196
5197 for (i = 0; i < emit->info.num_inputs; i++) {
5198 enum tgsi_semantic semantic_name = emit->info.input_semantic_name[i];
5199 unsigned usage_mask = emit->info.input_usage_mask[i];
5200 unsigned index = emit->linkage.input_map[i];
5201 unsigned opcodeType, operandType;
5202 unsigned numComp, selMode;
5203 unsigned name;
5204 unsigned dim;
5205
5206 if (usage_mask == 0)
5207 continue; /* register is not actually used */
5208
5209 opcodeType = VGPU10_OPCODE_DCL_INPUT;
5210 operandType = VGPU10_OPERAND_TYPE_INPUT;
5211 numComp = VGPU10_OPERAND_4_COMPONENT;
5212 selMode = VGPU10_OPERAND_4_COMPONENT_MASK_MODE;
5213 name = VGPU10_NAME_UNDEFINED;
5214
5215 /* all geometry shader inputs are two dimensional except
5216 * gl_PrimitiveID
5217 */
5218 dim = VGPU10_OPERAND_INDEX_2D;
5219
5220 if (semantic_name == TGSI_SEMANTIC_PRIMID) {
5221 /* Primitive ID */
5222 operandType = VGPU10_OPERAND_TYPE_INPUT_PRIMITIVEID;
5223 dim = VGPU10_OPERAND_INDEX_0D;
5224 numComp = VGPU10_OPERAND_0_COMPONENT;
5225 selMode = 0;
5226
5227 /* also save the register index so we can check for
5228 * primitive id when emit src register. We need to modify the
5229 * operand type, index dimension when emit primitive id src reg.
5230 */
5231 emit->gs.prim_id_index = i;
5232 }
5233 else if (semantic_name == TGSI_SEMANTIC_POSITION) {
5234 /* vertex position input */
5235 opcodeType = VGPU10_OPCODE_DCL_INPUT_SIV;
5236 name = VGPU10_NAME_POSITION;
5237 }
5238
5239 emit_input_declaration(emit, opcodeType, operandType,
5240 dim, index,
5241 emit->gs.input_size,
5242 name,
5243 numComp, selMode,
5244 VGPU10_OPERAND_4_COMPONENT_MASK_ALL,
5245 VGPU10_INTERPOLATION_UNDEFINED, true,
5246 map_tgsi_semantic_to_sgn_name(semantic_name));
5247 }
5248 }
5249
5250
5251 /**
5252 * Emit input declarations for tessellation control shader.
5253 */
5254 static void
5255 emit_tcs_input_declarations(struct svga_shader_emitter_v10 *emit)
5256 {
5257 unsigned i;
5258 unsigned size = emit->key.tcs.vertices_per_patch;
5259 bool addSignature = true;
5260
5261 if (!emit->tcs.control_point_phase)
5262 addSignature = emit->tcs.fork_phase_add_signature;
5263
5264 for (i = 0; i < emit->info.num_inputs; i++) {
5265 unsigned usage_mask = emit->info.input_usage_mask[i];
5266 unsigned index = emit->linkage.input_map[i];
5267 enum tgsi_semantic semantic_name = emit->info.input_semantic_name[i];
5268 VGPU10_SYSTEM_NAME name = VGPU10_NAME_UNDEFINED;
5269 VGPU10_OPERAND_TYPE operandType = VGPU10_OPERAND_TYPE_INPUT;
5270 SVGA3dDXSignatureSemanticName sgn_name =
5271 map_tgsi_semantic_to_sgn_name(semantic_name);
5272
5273 if (semantic_name == TGSI_SEMANTIC_POSITION ||
5274 index == emit->linkage.position_index) {
5275 /* save the input control point index for later use */
5276 emit->tcs.control_point_input_index = i;
5277 }
5278 else if (usage_mask == 0) {
5279 continue; /* register is not actually used */
5280 }
5281 else if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
5282 /* The shadow copy is being used here. So set the signature name
5283 * to UNDEFINED.
5284 */
5285 sgn_name = SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED;
5286 }
5287
5288 /* input control points in the patch constant phase are emitted in the
5289 * vicp register rather than the v register.
5290 */
5291 if (!emit->tcs.control_point_phase) {
5292 operandType = VGPU10_OPERAND_TYPE_INPUT_CONTROL_POINT;
5293 }
5294
5295 /* Tessellation control shader inputs are two dimensional.
5296 * The array size is determined by the patch vertex count.
5297 */
5298 emit_input_declaration(emit, VGPU10_OPCODE_DCL_INPUT,
5299 operandType,
5300 VGPU10_OPERAND_INDEX_2D,
5301 index, size, name,
5302 VGPU10_OPERAND_4_COMPONENT,
5303 VGPU10_OPERAND_4_COMPONENT_MASK_MODE,
5304 VGPU10_OPERAND_4_COMPONENT_MASK_ALL,
5305 VGPU10_INTERPOLATION_UNDEFINED,
5306 addSignature, sgn_name);
5307 }
5308
5309 if (emit->tcs.control_point_phase) {
5310
5311 /* Also add an address register for the indirection to the
5312 * input control points
5313 */
5314 emit->tcs.control_point_addr_index = emit->num_address_regs++;
5315 }
5316 }
5317
5318
5319 static void
5320 emit_tessfactor_input_declarations(struct svga_shader_emitter_v10 *emit)
5321 {
5322
5323 /* In tcs, tess factors are emitted as extra outputs.
5324 * The starting register index for the tess factors is captured
5325 * in the compile key.
5326 */
5327 unsigned inputIndex = emit->key.tes.tessfactor_index;
5328
5329 if (emit->tes.prim_mode == MESA_PRIM_QUADS) {
5330 if (emit->key.tes.need_tessouter) {
5331 emit->tes.outer.in_index = inputIndex;
5332 for (int i = 0; i < 4; i++) {
5333 emit_tesslevel_declaration(emit, inputIndex++,
5334 VGPU10_OPCODE_DCL_INPUT_SIV,
5335 VGPU10_OPERAND_TYPE_INPUT_PATCH_CONSTANT,
5336 VGPU10_NAME_FINAL_QUAD_U_EQ_0_EDGE_TESSFACTOR + i,
5337 SVGADX_SIGNATURE_SEMANTIC_NAME_FINAL_QUAD_U_EQ_0_EDGE_TESSFACTOR + i);
5338 }
5339 }
5340
5341 if (emit->key.tes.need_tessinner) {
5342 emit->tes.inner.in_index = inputIndex;
5343 emit_tesslevel_declaration(emit, inputIndex++,
5344 VGPU10_OPCODE_DCL_INPUT_SIV,
5345 VGPU10_OPERAND_TYPE_INPUT_PATCH_CONSTANT,
5346 VGPU10_NAME_FINAL_QUAD_U_INSIDE_TESSFACTOR,
5347 SVGADX_SIGNATURE_SEMANTIC_NAME_FINAL_QUAD_U_INSIDE_TESSFACTOR);
5348
5349 emit_tesslevel_declaration(emit, inputIndex++,
5350 VGPU10_OPCODE_DCL_INPUT_SIV,
5351 VGPU10_OPERAND_TYPE_INPUT_PATCH_CONSTANT,
5352 VGPU10_NAME_FINAL_QUAD_V_INSIDE_TESSFACTOR,
5353 SVGADX_SIGNATURE_SEMANTIC_NAME_FINAL_QUAD_V_INSIDE_TESSFACTOR);
5354 }
5355 }
5356 else if (emit->tes.prim_mode == MESA_PRIM_TRIANGLES) {
5357 if (emit->key.tes.need_tessouter) {
5358 emit->tes.outer.in_index = inputIndex;
5359 for (int i = 0; i < 3; i++) {
5360 emit_tesslevel_declaration(emit, inputIndex++,
5361 VGPU10_OPCODE_DCL_INPUT_SIV,
5362 VGPU10_OPERAND_TYPE_INPUT_PATCH_CONSTANT,
5363 VGPU10_NAME_FINAL_TRI_U_EQ_0_EDGE_TESSFACTOR + i,
5364 SVGADX_SIGNATURE_SEMANTIC_NAME_FINAL_TRI_U_EQ_0_EDGE_TESSFACTOR + i);
5365 }
5366 }
5367
5368 if (emit->key.tes.need_tessinner) {
5369 emit->tes.inner.in_index = inputIndex;
5370 emit_tesslevel_declaration(emit, inputIndex++,
5371 VGPU10_OPCODE_DCL_INPUT_SIV,
5372 VGPU10_OPERAND_TYPE_INPUT_PATCH_CONSTANT,
5373 VGPU10_NAME_FINAL_TRI_INSIDE_TESSFACTOR,
5374 SVGADX_SIGNATURE_SEMANTIC_NAME_FINAL_TRI_INSIDE_TESSFACTOR);
5375 }
5376 }
5377 else if (emit->tes.prim_mode == MESA_PRIM_LINES) {
5378 if (emit->key.tes.need_tessouter) {
5379 emit->tes.outer.in_index = inputIndex;
5380 emit_tesslevel_declaration(emit, inputIndex++,
5381 VGPU10_OPCODE_DCL_INPUT_SIV,
5382 VGPU10_OPERAND_TYPE_INPUT_PATCH_CONSTANT,
5383 VGPU10_NAME_FINAL_LINE_DETAIL_TESSFACTOR,
5384 SVGADX_SIGNATURE_SEMANTIC_NAME_FINAL_LINE_DETAIL_TESSFACTOR);
5385
5386 emit_tesslevel_declaration(emit, inputIndex++,
5387 VGPU10_OPCODE_DCL_INPUT_SIV,
5388 VGPU10_OPERAND_TYPE_INPUT_PATCH_CONSTANT,
5389 VGPU10_NAME_FINAL_LINE_DENSITY_TESSFACTOR,
5390 SVGADX_SIGNATURE_SEMANTIC_NAME_FINAL_LINE_DENSITY_TESSFACTOR);
5391 }
5392 }
5393 }
5394
5395
5396 /**
5397 * Emit input declarations for tessellation evaluation shader.
5398 */
5399 static void
5400 emit_tes_input_declarations(struct svga_shader_emitter_v10 *emit)
5401 {
5402 unsigned i;
5403
5404 for (i = 0; i < emit->info.num_inputs; i++) {
5405 unsigned usage_mask = emit->info.input_usage_mask[i];
5406 unsigned index = emit->linkage.input_map[i];
5407 unsigned size;
5408 const enum tgsi_semantic semantic_name =
5409 emit->info.input_semantic_name[i];
5410 SVGA3dDXSignatureSemanticName sgn_name;
5411 VGPU10_OPERAND_TYPE operandType;
5412 VGPU10_OPERAND_INDEX_DIMENSION dim;
5413
5414 if (usage_mask == 0)
5415 usage_mask = 1; /* at least set usage mask to one */
5416
5417 if (semantic_name == TGSI_SEMANTIC_PATCH) {
5418 operandType = VGPU10_OPERAND_TYPE_INPUT_PATCH_CONSTANT;
5419 dim = VGPU10_OPERAND_INDEX_1D;
5420 size = 1;
5421 sgn_name = map_tgsi_semantic_to_sgn_name(semantic_name);
5422 }
5423 else {
5424 operandType = VGPU10_OPERAND_TYPE_INPUT_CONTROL_POINT;
5425 dim = VGPU10_OPERAND_INDEX_2D;
5426 size = emit->key.tes.vertices_per_patch;
5427 sgn_name = SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED;
5428 }
5429
5430 emit_input_declaration(emit, VGPU10_OPCODE_DCL_INPUT, operandType,
5431 dim, index, size, VGPU10_NAME_UNDEFINED,
5432 VGPU10_OPERAND_4_COMPONENT,
5433 VGPU10_OPERAND_4_COMPONENT_MASK_MODE,
5434 VGPU10_OPERAND_4_COMPONENT_MASK_ALL,
5435 VGPU10_INTERPOLATION_UNDEFINED,
5436 true, sgn_name);
5437 }
5438
5439 emit_tessfactor_input_declarations(emit);
5440
5441 /* DX spec requires DS input controlpoint/patch-constant signatures to match
5442 * the HS output controlpoint/patch-constant signatures exactly.
5443 * Add missing input declarations even if they are not used in the shader.
5444 */
5445 if (emit->linkage.num_inputs < emit->linkage.prevShader.num_outputs) {
5446 struct tgsi_shader_info *prevInfo = emit->prevShaderInfo;
5447 for (i = 0; i < emit->linkage.prevShader.num_outputs; i++) {
5448
5449 /* If a tcs output does not have a corresponding input register in
5450 * tes, add one.
5451 */
5452 if (emit->linkage.prevShader.output_map[i] >
5453 emit->linkage.input_map_max) {
5454 const enum tgsi_semantic sem_name = prevInfo->output_semantic_name[i];
5455
5456 if (sem_name == TGSI_SEMANTIC_PATCH) {
5457 emit_input_declaration(emit, VGPU10_OPCODE_DCL_INPUT,
5458 VGPU10_OPERAND_TYPE_INPUT_PATCH_CONSTANT,
5459 VGPU10_OPERAND_INDEX_1D,
5460 i, 1, VGPU10_NAME_UNDEFINED,
5461 VGPU10_OPERAND_4_COMPONENT,
5462 VGPU10_OPERAND_4_COMPONENT_MASK_MODE,
5463 VGPU10_OPERAND_4_COMPONENT_MASK_ALL,
5464 VGPU10_INTERPOLATION_UNDEFINED,
5465 true,
5466 map_tgsi_semantic_to_sgn_name(sem_name));
5467
5468 } else if (sem_name != TGSI_SEMANTIC_TESSINNER &&
5469 sem_name != TGSI_SEMANTIC_TESSOUTER) {
5470 emit_input_declaration(emit, VGPU10_OPCODE_DCL_INPUT,
5471 VGPU10_OPERAND_TYPE_INPUT_CONTROL_POINT,
5472 VGPU10_OPERAND_INDEX_2D,
5473 i, emit->key.tes.vertices_per_patch,
5474 VGPU10_NAME_UNDEFINED,
5475 VGPU10_OPERAND_4_COMPONENT,
5476 VGPU10_OPERAND_4_COMPONENT_MASK_MODE,
5477 VGPU10_OPERAND_4_COMPONENT_MASK_ALL,
5478 VGPU10_INTERPOLATION_UNDEFINED,
5479 true,
5480 map_tgsi_semantic_to_sgn_name(sem_name));
5481 }
5482 /* tessellation factors are taken care of in
5483 * emit_tessfactor_input_declarations().
5484 */
5485 }
5486 }
5487 }
5488 }
5489
5490
5491 /**
5492 * Emit all input declarations.
5493 */
5494 static bool
5495 emit_input_declarations(struct svga_shader_emitter_v10 *emit)
5496 {
5497 emit->index_range.required =
5498 emit->info.indirect_files & (1 << TGSI_FILE_INPUT) ? true : false;
5499
5500 switch (emit->unit) {
5501 case MESA_SHADER_FRAGMENT:
5502 emit_fs_input_declarations(emit);
5503 break;
5504 case MESA_SHADER_GEOMETRY:
5505 emit_gs_input_declarations(emit);
5506 break;
5507 case MESA_SHADER_VERTEX:
5508 emit_vs_input_declarations(emit);
5509 break;
5510 case MESA_SHADER_TESS_CTRL:
5511 emit_tcs_input_declarations(emit);
5512 break;
5513 case MESA_SHADER_TESS_EVAL:
5514 emit_tes_input_declarations(emit);
5515 break;
5516 case MESA_SHADER_COMPUTE:
5517 //XXX emit_cs_input_declarations(emit);
5518 break;
5519 default:
5520 assert(0);
5521 }
5522
5523 if (emit->index_range.start_index != INVALID_INDEX) {
5524 emit_index_range_declaration(emit);
5525 }
5526 emit->index_range.required = false;
5527 return true;
5528 }
5529
5530
5531 /**
5532 * Emit all output declarations.
5533 */
5534 static bool
5535 emit_output_declarations(struct svga_shader_emitter_v10 *emit)
5536 {
5537 emit->index_range.required =
5538 emit->info.indirect_files & (1 << TGSI_FILE_OUTPUT) ? true : false;
5539
5540 switch (emit->unit) {
5541 case MESA_SHADER_FRAGMENT:
5542 emit_fs_output_declarations(emit);
5543 break;
5544 case MESA_SHADER_GEOMETRY:
5545 emit_gs_output_declarations(emit);
5546 break;
5547 case MESA_SHADER_VERTEX:
5548 emit_vs_output_declarations(emit);
5549 break;
5550 case MESA_SHADER_TESS_CTRL:
5551 emit_tcs_output_declarations(emit);
5552 break;
5553 case MESA_SHADER_TESS_EVAL:
5554 emit_tes_output_declarations(emit);
5555 break;
5556 case MESA_SHADER_COMPUTE:
5557 //XXX emit_cs_output_declarations(emit);
5558 break;
5559 default:
5560 assert(0);
5561 }
5562
5563 if (emit->vposition.so_index != INVALID_INDEX &&
5564 emit->vposition.out_index != INVALID_INDEX) {
5565
5566 assert(emit->unit != MESA_SHADER_FRAGMENT);
5567
5568 /* Emit the declaration for the non-adjusted vertex position
5569 * for stream output purpose
5570 */
5571 emit_output_declaration(emit, VGPU10_OPCODE_DCL_OUTPUT,
5572 emit->vposition.so_index,
5573 VGPU10_NAME_UNDEFINED,
5574 VGPU10_OPERAND_4_COMPONENT_MASK_ALL,
5575 true,
5576 SVGADX_SIGNATURE_SEMANTIC_NAME_POSITION);
5577 }
5578
5579 if (emit->clip_dist_so_index != INVALID_INDEX &&
5580 emit->clip_dist_out_index != INVALID_INDEX) {
5581
5582 assert(emit->unit != MESA_SHADER_FRAGMENT);
5583
5584 /* Emit the declaration for the clip distance shadow copy which
5585 * will be used for stream output purpose and for clip distance
5586 * varying variable. Note all clip distances
5587 * will be written regardless of the enabled clipping planes.
5588 */
5589 emit_output_declaration(emit, VGPU10_OPCODE_DCL_OUTPUT,
5590 emit->clip_dist_so_index,
5591 VGPU10_NAME_UNDEFINED,
5592 VGPU10_OPERAND_4_COMPONENT_MASK_ALL,
5593 true,
5594 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED);
5595
5596 if (emit->info.num_written_clipdistance > 4) {
5597 /* for the second clip distance register, each handles 4 planes */
5598 emit_output_declaration(emit, VGPU10_OPCODE_DCL_OUTPUT,
5599 emit->clip_dist_so_index + 1,
5600 VGPU10_NAME_UNDEFINED,
5601 VGPU10_OPERAND_4_COMPONENT_MASK_ALL,
5602 true,
5603 SVGADX_SIGNATURE_SEMANTIC_NAME_UNDEFINED);
5604 }
5605 }
5606
5607 if (emit->index_range.start_index != INVALID_INDEX) {
5608 emit_index_range_declaration(emit);
5609 }
5610 emit->index_range.required = false;
5611 return true;
5612 }
5613
5614
5615 /**
5616 * A helper function to create a temporary indexable array
5617 * and initialize the corresponding entries in the temp_map array.
5618 */
5619 static void
5620 create_temp_array(struct svga_shader_emitter_v10 *emit,
5621 unsigned arrayID, unsigned first, unsigned count,
5622 unsigned startIndex)
5623 {
5624 unsigned i, tempIndex = startIndex;
5625
5626 emit->num_temp_arrays = MAX2(emit->num_temp_arrays, arrayID + 1);
5627 assert(emit->num_temp_arrays <= MAX_TEMP_ARRAYS);
5628 emit->num_temp_arrays = MIN2(emit->num_temp_arrays, MAX_TEMP_ARRAYS);
5629
5630 emit->temp_arrays[arrayID].start = first;
5631 emit->temp_arrays[arrayID].size = count;
5632
5633 /* Fill in the temp_map entries for this temp array */
5634 for (i = 0; i < count; i++, tempIndex++) {
5635 emit->temp_map[tempIndex].arrayId = arrayID;
5636 emit->temp_map[tempIndex].index = i;
5637 }
5638 }
5639
5640
5641 /**
5642 * Emit the declaration for the temporary registers.
5643 */
5644 static bool
5645 emit_temporaries_declaration(struct svga_shader_emitter_v10 *emit)
5646 {
5647 unsigned total_temps, reg, i;
5648
5649 total_temps = emit->num_shader_temps;
5650
5651 /* If there is indirect access to non-indexable temps in the shader,
5652 * convert those temps to indexable temps. This works around a bug
5653 * in the GLSL->TGSI translator exposed in piglit test
5654 * glsl-1.20/execution/fs-const-array-of-struct-of-array.shader_test.
5655 * Internal temps added by the driver remain as non-indexable temps.
5656 */
5657 if ((emit->info.indirect_files & (1 << TGSI_FILE_TEMPORARY)) &&
5658 emit->num_temp_arrays == 0) {
5659 create_temp_array(emit, 1, 0, total_temps, 0);
5660 }
5661
5662 /* Allocate extra temps for specially-implemented instructions,
5663 * such as LIT.
5664 */
5665 total_temps += MAX_INTERNAL_TEMPS;
5666
5667 /* Allocate extra temps for clip distance or clip vertex.
5668 */
5669 if (emit->clip_mode == CLIP_DISTANCE) {
5670 /* We need to write the clip distance to a temporary register
5671 * first. Then it will be copied to the shadow copy for
5672 * the clip distance varying variable and stream output purpose.
5673 * It will also be copied to the actual CLIPDIST register
5674 * according to the enabled clip planes
5675 */
5676 emit->clip_dist_tmp_index = total_temps++;
5677 if (emit->info.num_written_clipdistance > 4)
5678 total_temps++; /* second clip register */
5679 }
5680 else if (emit->clip_mode == CLIP_VERTEX && emit->key.last_vertex_stage) {
5681 /* If the current shader is in the last vertex processing stage,
5682 * We need to convert the TGSI CLIPVERTEX output to one or more
5683 * clip distances. Allocate a temp reg for the clipvertex here.
5684 */
5685 assert(emit->info.writes_clipvertex > 0);
5686 emit->clip_vertex_tmp_index = total_temps;
5687 total_temps++;
5688 }
5689
5690 if (emit->info.uses_vertexid) {
5691 assert(emit->unit == MESA_SHADER_VERTEX);
5692 emit->vs.vertex_id_tmp_index = total_temps++;
5693 }
5694
5695 if (emit->unit == MESA_SHADER_VERTEX || emit->unit == MESA_SHADER_GEOMETRY) {
5696 if (emit->vposition.need_prescale || emit->key.vs.undo_viewport ||
5697 emit->key.clip_plane_enable ||
5698 emit->vposition.so_index != INVALID_INDEX) {
5699 emit->vposition.tmp_index = total_temps;
5700 total_temps += 1;
5701 }
5702
5703 if (emit->vposition.need_prescale) {
5704 emit->vposition.prescale_scale_index = total_temps++;
5705 emit->vposition.prescale_trans_index = total_temps++;
5706 }
5707
5708 if (emit->unit == MESA_SHADER_VERTEX) {
5709 unsigned attrib_mask = (emit->key.vs.adjust_attrib_w_1 |
5710 emit->key.vs.adjust_attrib_itof |
5711 emit->key.vs.adjust_attrib_utof |
5712 emit->key.vs.attrib_is_bgra |
5713 emit->key.vs.attrib_puint_to_snorm |
5714 emit->key.vs.attrib_puint_to_uscaled |
5715 emit->key.vs.attrib_puint_to_sscaled);
5716 while (attrib_mask) {
5717 unsigned index = u_bit_scan(&attrib_mask);
5718 emit->vs.adjusted_input[index] = total_temps++;
5719 }
5720 }
5721 else if (emit->unit == MESA_SHADER_GEOMETRY) {
5722 if (emit->key.gs.writes_viewport_index)
5723 emit->gs.viewport_index_tmp_index = total_temps++;
5724 }
5725 }
5726 else if (emit->unit == MESA_SHADER_FRAGMENT) {
5727 if (emit->key.fs.alpha_func != SVGA3D_CMP_ALWAYS ||
5728 emit->key.fs.write_color0_to_n_cbufs > 1) {
5729 /* Allocate a temp to hold the output color */
5730 emit->fs.color_tmp_index = total_temps;
5731 total_temps += 1;
5732 }
5733
5734 if (emit->fs.face_input_index != INVALID_INDEX) {
5735 /* Allocate a temp for the +/-1 face register */
5736 emit->fs.face_tmp_index = total_temps;
5737 total_temps += 1;
5738 }
5739
5740 if (emit->fs.fragcoord_input_index != INVALID_INDEX) {
5741 /* Allocate a temp for modified fragment position register */
5742 emit->fs.fragcoord_tmp_index = total_temps;
5743 total_temps += 1;
5744 }
5745
5746 if (emit->fs.sample_pos_sys_index != INVALID_INDEX) {
5747 /* Allocate a temp for the sample position */
5748 emit->fs.sample_pos_tmp_index = total_temps++;
5749 }
5750 }
5751 else if (emit->unit == MESA_SHADER_TESS_EVAL) {
5752 if (emit->vposition.need_prescale) {
5753 emit->vposition.tmp_index = total_temps++;
5754 emit->vposition.prescale_scale_index = total_temps++;
5755 emit->vposition.prescale_trans_index = total_temps++;
5756 }
5757
5758 if (emit->tes.inner.tgsi_index) {
5759 emit->tes.inner.temp_index = total_temps;
5760 total_temps += 1;
5761 }
5762
5763 if (emit->tes.outer.tgsi_index) {
5764 emit->tes.outer.temp_index = total_temps;
5765 total_temps += 1;
5766 }
5767 }
5768 else if (emit->unit == MESA_SHADER_TESS_CTRL) {
5769 if (emit->tcs.inner.tgsi_index != INVALID_INDEX) {
5770 if (!emit->tcs.control_point_phase) {
5771 emit->tcs.inner.temp_index = total_temps;
5772 total_temps += 1;
5773 }
5774 }
5775 if (emit->tcs.outer.tgsi_index != INVALID_INDEX) {
5776 if (!emit->tcs.control_point_phase) {
5777 emit->tcs.outer.temp_index = total_temps;
5778 total_temps += 1;
5779 }
5780 }
5781
5782 if (emit->tcs.control_point_phase &&
5783 emit->info.reads_pervertex_outputs) {
5784 emit->tcs.control_point_tmp_index = total_temps;
5785 total_temps += emit->tcs.control_point_out_count;
5786 }
5787 else if (!emit->tcs.control_point_phase &&
5788 emit->info.reads_perpatch_outputs) {
5789
5790 /* If there is indirect access to the patch constant outputs
5791 * in the control point phase, then an indexable temporary array
5792 * will be created for these patch constant outputs.
5793 * Note, indirect access can only be applicable to
5794 * patch constant outputs in the control point phase.
5795 */
5796 if (emit->info.indirect_files & (1 << TGSI_FILE_OUTPUT)) {
5797 unsigned arrayID =
5798 emit->num_temp_arrays ? emit->num_temp_arrays : 1;
5799 create_temp_array(emit, arrayID, 0,
5800 emit->tcs.patch_generic_out_count, total_temps);
5801 }
5802 emit->tcs.patch_generic_tmp_index = total_temps;
5803 total_temps += emit->tcs.patch_generic_out_count;
5804 }
5805
5806 emit->tcs.invocation_id_tmp_index = total_temps++;
5807 }
5808
5809 if (emit->raw_bufs) {
5810 /**
5811 * Add 3 more temporaries if we need to translate constant buffer
5812 * to srv raw buffer. Since we need to load the value to a temporary
5813 * before it can be used as a source. There could be three source
5814 * register in an instruction.
5815 */
5816 emit->raw_buf_tmp_index = total_temps;
5817 total_temps+=3;
5818 }
5819
5820 for (i = 0; i < emit->num_address_regs; i++) {
5821 emit->address_reg_index[i] = total_temps++;
5822 }
5823
5824 /* Initialize the temp_map array which maps TGSI temp indexes to VGPU10
5825 * temp indexes. Basically, we compact all the non-array temp register
5826 * indexes into a consecutive series.
5827 *
5828 * Before, we may have some TGSI declarations like:
5829 * DCL TEMP[0..1], LOCAL
5830 * DCL TEMP[2..4], ARRAY(1), LOCAL
5831 * DCL TEMP[5..7], ARRAY(2), LOCAL
5832 * plus, some extra temps, like TEMP[8], TEMP[9] for misc things
5833 *
5834 * After, we'll have a map like this:
5835 * temp_map[0] = { array 0, index 0 }
5836 * temp_map[1] = { array 0, index 1 }
5837 * temp_map[2] = { array 1, index 0 }
5838 * temp_map[3] = { array 1, index 1 }
5839 * temp_map[4] = { array 1, index 2 }
5840 * temp_map[5] = { array 2, index 0 }
5841 * temp_map[6] = { array 2, index 1 }
5842 * temp_map[7] = { array 2, index 2 }
5843 * temp_map[8] = { array 0, index 2 }
5844 * temp_map[9] = { array 0, index 3 }
5845 *
5846 * We'll declare two arrays of 3 elements, plus a set of four non-indexed
5847 * temps numbered 0..3
5848 *
5849 * Any time we emit a temporary register index, we'll have to use the
5850 * temp_map[] table to convert the TGSI index to the VGPU10 index.
5851 *
5852 * Finally, we recompute the total_temps value here.
5853 */
5854 reg = 0;
5855 for (i = 0; i < total_temps; i++) {
5856 if (emit->temp_map[i].arrayId == 0) {
5857 emit->temp_map[i].index = reg++;
5858 }
5859 }
5860
5861 if (0) {
5862 debug_printf("total_temps %u\n", total_temps);
5863 for (i = 0; i < total_temps; i++) {
5864 debug_printf("temp %u -> array %u index %u\n",
5865 i, emit->temp_map[i].arrayId, emit->temp_map[i].index);
5866 }
5867 }
5868
5869 total_temps = reg;
5870
5871 /* Emit declaration of ordinary temp registers */
5872 if (total_temps > 0) {
5873 VGPU10OpcodeToken0 opcode0;
5874
5875 opcode0.value = 0;
5876 opcode0.opcodeType = VGPU10_OPCODE_DCL_TEMPS;
5877
5878 begin_emit_instruction(emit);
5879 emit_dword(emit, opcode0.value);
5880 emit_dword(emit, total_temps);
5881 end_emit_instruction(emit);
5882 }
5883
5884 /* Emit declarations for indexable temp arrays. Skip 0th entry since
5885 * it's unused.
5886 */
5887 for (i = 1; i < emit->num_temp_arrays; i++) {
5888 unsigned num_temps = emit->temp_arrays[i].size;
5889
5890 if (num_temps > 0) {
5891 VGPU10OpcodeToken0 opcode0;
5892
5893 opcode0.value = 0;
5894 opcode0.opcodeType = VGPU10_OPCODE_DCL_INDEXABLE_TEMP;
5895
5896 begin_emit_instruction(emit);
5897 emit_dword(emit, opcode0.value);
5898 emit_dword(emit, i); /* which array */
5899 emit_dword(emit, num_temps);
5900 emit_dword(emit, 4); /* num components */
5901 end_emit_instruction(emit);
5902
5903 total_temps += num_temps;
5904 }
5905 }
5906
5907 /* Check that the grand total of all regular and indexed temps is
5908 * under the limit.
5909 */
5910 check_register_index(emit, VGPU10_OPCODE_DCL_TEMPS, total_temps - 1);
5911
5912 return true;
5913 }
5914
5915
5916 static bool
5917 emit_rawbuf_declaration(struct svga_shader_emitter_v10 *emit,
5918 unsigned index)
5919 {
5920 VGPU10OpcodeToken0 opcode1;
5921 VGPU10OperandToken0 operand1;
5922
5923 opcode1.value = 0;
5924 opcode1.opcodeType = VGPU10_OPCODE_DCL_RESOURCE_RAW;
5925 opcode1.resourceDimension = VGPU10_RESOURCE_DIMENSION_UNKNOWN;
5926
5927 operand1.value = 0;
5928 operand1.numComponents = VGPU10_OPERAND_0_COMPONENT;
5929 operand1.operandType = VGPU10_OPERAND_TYPE_RESOURCE;
5930 operand1.indexDimension = VGPU10_OPERAND_INDEX_1D;
5931 operand1.index0Representation = VGPU10_OPERAND_INDEX_IMMEDIATE32;
5932
5933 begin_emit_instruction(emit);
5934 emit_dword(emit, opcode1.value);
5935 emit_dword(emit, operand1.value);
5936 emit_dword(emit, index);
5937 end_emit_instruction(emit);
5938
5939 return true;
5940 }
5941
5942
5943 static bool
5944 emit_constant_declaration(struct svga_shader_emitter_v10 *emit)
5945 {
5946 VGPU10OpcodeToken0 opcode0;
5947 VGPU10OperandToken0 operand0;
5948 unsigned total_consts, i;
5949
5950 opcode0.value = 0;
5951 opcode0.opcodeType = VGPU10_OPCODE_DCL_CONSTANT_BUFFER;
5952 opcode0.accessPattern = VGPU10_CB_IMMEDIATE_INDEXED;
5953 /* XXX or, access pattern = VGPU10_CB_DYNAMIC_INDEXED */
5954
5955 operand0.value = 0;
5956 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
5957 operand0.indexDimension = VGPU10_OPERAND_INDEX_2D;
5958 operand0.index0Representation = VGPU10_OPERAND_INDEX_IMMEDIATE32;
5959 operand0.index1Representation = VGPU10_OPERAND_INDEX_IMMEDIATE32;
5960 operand0.operandType = VGPU10_OPERAND_TYPE_CONSTANT_BUFFER;
5961 operand0.selectionMode = VGPU10_OPERAND_4_COMPONENT_SWIZZLE_MODE;
5962 operand0.swizzleX = 0;
5963 operand0.swizzleY = 1;
5964 operand0.swizzleZ = 2;
5965 operand0.swizzleW = 3;
5966
5967 /**
5968 * Emit declaration for constant buffer [0]. We also allocate
5969 * room for the extra constants here.
5970 */
5971 total_consts = emit->num_shader_consts[0];
5972
5973 /* Now, allocate constant slots for the "extra" constants.
5974 * Note: it's critical that these extra constant locations
5975 * exactly match what's emitted by the "extra" constants code
5976 * in svga_state_constants.c
5977 */
5978
5979 /* Vertex position scale/translation */
5980 if (emit->vposition.need_prescale) {
5981 emit->vposition.prescale_cbuf_index = total_consts;
5982 total_consts += (2 * emit->vposition.num_prescale);
5983 }
5984
5985 if (emit->unit == MESA_SHADER_VERTEX) {
5986 if (emit->key.vs.undo_viewport) {
5987 emit->vs.viewport_index = total_consts++;
5988 }
5989 if (emit->key.vs.need_vertex_id_bias) {
5990 emit->vs.vertex_id_bias_index = total_consts++;
5991 }
5992 }
5993
5994 /* user-defined clip planes */
5995 if (emit->key.clip_plane_enable) {
5996 unsigned n = util_bitcount(emit->key.clip_plane_enable);
5997 assert(emit->unit != MESA_SHADER_FRAGMENT &&
5998 emit->unit != MESA_SHADER_COMPUTE);
5999 for (i = 0; i < n; i++) {
6000 emit->clip_plane_const[i] = total_consts++;
6001 }
6002 }
6003
6004 for (i = 0; i < emit->num_samplers; i++) {
6005
6006 if (emit->key.tex[i].sampler_view) {
6007 /* Texcoord scale factors for RECT textures */
6008 if (emit->key.tex[i].unnormalized) {
6009 emit->texcoord_scale_index[i] = total_consts++;
6010 }
6011
6012 /* Texture buffer sizes */
6013 if (emit->key.tex[i].target == PIPE_BUFFER) {
6014 emit->texture_buffer_size_index[i] = total_consts++;
6015 }
6016 }
6017 }
6018 if (emit->key.image_size_used) {
6019 emit->image_size_index = total_consts;
6020 total_consts += emit->num_images;
6021 }
6022
6023 if (total_consts > 0) {
6024 if (total_consts > VGPU10_MAX_CONSTANT_BUFFER_ELEMENT_COUNT) {
6025 debug_printf("Warning: Too many constants [%u] declared in constant"
6026 " buffer 0. %u is the limit.\n",
6027 total_consts,
6028 VGPU10_MAX_CONSTANT_BUFFER_ELEMENT_COUNT);
6029 total_consts = VGPU10_MAX_CONSTANT_BUFFER_ELEMENT_COUNT;
6030 emit->register_overflow = true;
6031 }
6032 begin_emit_instruction(emit);
6033 emit_dword(emit, opcode0.value);
6034 emit_dword(emit, operand0.value);
6035 emit_dword(emit, 0); /* which const buffer slot */
6036 emit_dword(emit, total_consts);
6037 end_emit_instruction(emit);
6038 }
6039
6040 /* Declare remaining constant buffers (UBOs) */
6041
6042 for (i = 1; i < ARRAY_SIZE(emit->num_shader_consts); i++) {
6043 if (emit->num_shader_consts[i] > 0) {
6044 if (emit->raw_bufs & (1 << i)) {
6045 /* UBO declared as srv raw buffer */
6046 emit_rawbuf_declaration(emit, i + emit->raw_buf_srv_start_index);
6047 }
6048 else {
6049
6050 /* UBO declared as const buffer */
6051 begin_emit_instruction(emit);
6052 emit_dword(emit, opcode0.value);
6053 emit_dword(emit, operand0.value);
6054 emit_dword(emit, i); /* which const buffer slot */
6055 emit_dword(emit, emit->num_shader_consts[i]);
6056 end_emit_instruction(emit);
6057 }
6058 }
6059 }
6060
6061 return true;
6062 }
6063
6064
6065 /**
6066 * Emit declarations for samplers.
6067 */
6068 static bool
6069 emit_sampler_declarations(struct svga_shader_emitter_v10 *emit)
6070 {
6071 unsigned i;
6072
6073 for (i = 0; i < emit->key.num_samplers; i++) {
6074
6075 VGPU10OpcodeToken0 opcode0;
6076 VGPU10OperandToken0 operand0;
6077
6078 opcode0.value = 0;
6079 opcode0.opcodeType = VGPU10_OPCODE_DCL_SAMPLER;
6080 opcode0.samplerMode = VGPU10_SAMPLER_MODE_DEFAULT;
6081
6082 operand0.value = 0;
6083 operand0.numComponents = VGPU10_OPERAND_0_COMPONENT;
6084 operand0.operandType = VGPU10_OPERAND_TYPE_SAMPLER;
6085 operand0.indexDimension = VGPU10_OPERAND_INDEX_1D;
6086 operand0.index0Representation = VGPU10_OPERAND_INDEX_IMMEDIATE32;
6087
6088 begin_emit_instruction(emit);
6089 emit_dword(emit, opcode0.value);
6090 emit_dword(emit, operand0.value);
6091 emit_dword(emit, i);
6092 end_emit_instruction(emit);
6093 }
6094
6095 return true;
6096 }
6097
6098
6099 /**
6100 * Translate PIPE_TEXTURE_x to VGPU10_RESOURCE_DIMENSION_x.
6101 */
6102 static unsigned
6103 pipe_texture_to_resource_dimension(enum tgsi_texture_type target,
6104 unsigned num_samples,
6105 bool is_array,
6106 bool is_uav)
6107 {
6108 switch (target) {
6109 case PIPE_BUFFER:
6110 return VGPU10_RESOURCE_DIMENSION_BUFFER;
6111 case PIPE_TEXTURE_1D:
6112 return VGPU10_RESOURCE_DIMENSION_TEXTURE1D;
6113 case PIPE_TEXTURE_2D:
6114 return num_samples > 2 ? VGPU10_RESOURCE_DIMENSION_TEXTURE2DMS :
6115 VGPU10_RESOURCE_DIMENSION_TEXTURE2D;
6116 case PIPE_TEXTURE_RECT:
6117 return VGPU10_RESOURCE_DIMENSION_TEXTURE2D;
6118 case PIPE_TEXTURE_3D:
6119 return VGPU10_RESOURCE_DIMENSION_TEXTURE3D;
6120 case PIPE_TEXTURE_CUBE:
6121 return VGPU10_RESOURCE_DIMENSION_TEXTURECUBE;
6122 case PIPE_TEXTURE_1D_ARRAY:
6123 return is_array ? VGPU10_RESOURCE_DIMENSION_TEXTURE1DARRAY
6124 : VGPU10_RESOURCE_DIMENSION_TEXTURE1D;
6125 case PIPE_TEXTURE_2D_ARRAY:
6126 if (num_samples > 2 && is_array)
6127 return VGPU10_RESOURCE_DIMENSION_TEXTURE2DMSARRAY;
6128 else if (is_array)
6129 return VGPU10_RESOURCE_DIMENSION_TEXTURE2DARRAY;
6130 else
6131 return VGPU10_RESOURCE_DIMENSION_TEXTURE2D;
6132 case PIPE_TEXTURE_CUBE_ARRAY:
6133 return is_uav ? VGPU10_RESOURCE_DIMENSION_TEXTURE2DARRAY :
6134 (is_array ? VGPU10_RESOURCE_DIMENSION_TEXTURECUBEARRAY :
6135 VGPU10_RESOURCE_DIMENSION_TEXTURECUBE);
6136 default:
6137 assert(!"Unexpected resource type");
6138 return VGPU10_RESOURCE_DIMENSION_TEXTURE2D;
6139 }
6140 }
6141
6142
6143 /**
6144 * Translate TGSI_TEXTURE_x to VGPU10_RESOURCE_DIMENSION_x.
6145 */
6146 static unsigned
6147 tgsi_texture_to_resource_dimension(enum tgsi_texture_type target,
6148 unsigned num_samples,
6149 bool is_array,
6150 bool is_uav)
6151 {
6152 if (target == TGSI_TEXTURE_2D_MSAA && num_samples < 2) {
6153 target = TGSI_TEXTURE_2D;
6154 }
6155 else if (target == TGSI_TEXTURE_2D_ARRAY_MSAA && num_samples < 2) {
6156 target = TGSI_TEXTURE_2D_ARRAY;
6157 }
6158
6159 switch (target) {
6160 case TGSI_TEXTURE_BUFFER:
6161 return VGPU10_RESOURCE_DIMENSION_BUFFER;
6162 case TGSI_TEXTURE_1D:
6163 return VGPU10_RESOURCE_DIMENSION_TEXTURE1D;
6164 case TGSI_TEXTURE_2D:
6165 case TGSI_TEXTURE_RECT:
6166 return VGPU10_RESOURCE_DIMENSION_TEXTURE2D;
6167 case TGSI_TEXTURE_3D:
6168 return VGPU10_RESOURCE_DIMENSION_TEXTURE3D;
6169 case TGSI_TEXTURE_CUBE:
6170 case TGSI_TEXTURE_SHADOWCUBE:
6171 return is_uav ? VGPU10_RESOURCE_DIMENSION_TEXTURE2DARRAY :
6172 VGPU10_RESOURCE_DIMENSION_TEXTURECUBE;
6173 case TGSI_TEXTURE_SHADOW1D:
6174 return VGPU10_RESOURCE_DIMENSION_TEXTURE1D;
6175 case TGSI_TEXTURE_SHADOW2D:
6176 case TGSI_TEXTURE_SHADOWRECT:
6177 return VGPU10_RESOURCE_DIMENSION_TEXTURE2D;
6178 case TGSI_TEXTURE_1D_ARRAY:
6179 case TGSI_TEXTURE_SHADOW1D_ARRAY:
6180 return is_array ? VGPU10_RESOURCE_DIMENSION_TEXTURE1DARRAY
6181 : VGPU10_RESOURCE_DIMENSION_TEXTURE1D;
6182 case TGSI_TEXTURE_2D_ARRAY:
6183 case TGSI_TEXTURE_SHADOW2D_ARRAY:
6184 return is_array ? VGPU10_RESOURCE_DIMENSION_TEXTURE2DARRAY
6185 : VGPU10_RESOURCE_DIMENSION_TEXTURE2D;
6186 case TGSI_TEXTURE_2D_MSAA:
6187 return VGPU10_RESOURCE_DIMENSION_TEXTURE2DMS;
6188 case TGSI_TEXTURE_2D_ARRAY_MSAA:
6189 return is_array ? VGPU10_RESOURCE_DIMENSION_TEXTURE2DMSARRAY
6190 : VGPU10_RESOURCE_DIMENSION_TEXTURE2DMS;
6191 case TGSI_TEXTURE_CUBE_ARRAY:
6192 return is_uav ? VGPU10_RESOURCE_DIMENSION_TEXTURE2DARRAY :
6193 (is_array ? VGPU10_RESOURCE_DIMENSION_TEXTURECUBEARRAY :
6194 VGPU10_RESOURCE_DIMENSION_TEXTURECUBE);
6195 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
6196 return is_array ? VGPU10_RESOURCE_DIMENSION_TEXTURECUBEARRAY
6197 : VGPU10_RESOURCE_DIMENSION_TEXTURECUBE;
6198 default:
6199 assert(!"Unexpected resource type");
6200 return VGPU10_RESOURCE_DIMENSION_TEXTURE2D;
6201 }
6202 }
6203
6204
6205 /**
6206 * Given a tgsi_return_type, return true iff it is an integer type.
6207 */
6208 static bool
6209 is_integer_type(enum tgsi_return_type type)
6210 {
6211 switch (type) {
6212 case TGSI_RETURN_TYPE_SINT:
6213 case TGSI_RETURN_TYPE_UINT:
6214 return true;
6215 case TGSI_RETURN_TYPE_FLOAT:
6216 case TGSI_RETURN_TYPE_UNORM:
6217 case TGSI_RETURN_TYPE_SNORM:
6218 return false;
6219 case TGSI_RETURN_TYPE_COUNT:
6220 default:
6221 assert(!"is_integer_type: Unknown tgsi_return_type");
6222 return false;
6223 }
6224 }
6225
6226
6227 /**
6228 * Emit declarations for resources.
6229 * XXX When we're sure that all TGSI shaders will be generated with
6230 * sampler view declarations (Ex: DCL SVIEW[n], 2D, UINT) we may
6231 * rework this code.
6232 */
6233 static bool
6234 emit_resource_declarations(struct svga_shader_emitter_v10 *emit)
6235 {
6236 unsigned i;
6237
6238 /* Emit resource decl for each sampler */
6239 for (i = 0; i < emit->num_samplers; i++) {
6240 if (!(emit->info.samplers_declared & (1 << i)))
6241 continue;
6242
6243 VGPU10OpcodeToken0 opcode0;
6244 VGPU10OperandToken0 operand0;
6245 VGPU10ResourceReturnTypeToken return_type;
6246 VGPU10_RESOURCE_RETURN_TYPE rt;
6247
6248 opcode0.value = 0;
6249 opcode0.opcodeType = VGPU10_OPCODE_DCL_RESOURCE;
6250 if (emit->sampler_view[i] || !emit->key.tex[i].sampler_view) {
6251 opcode0.resourceDimension =
6252 tgsi_texture_to_resource_dimension(emit->sampler_target[i],
6253 emit->key.tex[i].num_samples,
6254 emit->key.tex[i].is_array,
6255 false);
6256 }
6257 else {
6258 opcode0.resourceDimension =
6259 pipe_texture_to_resource_dimension(emit->key.tex[i].target,
6260 emit->key.tex[i].num_samples,
6261 emit->key.tex[i].is_array,
6262 false);
6263 }
6264 opcode0.sampleCount = emit->key.tex[i].num_samples;
6265 operand0.value = 0;
6266 operand0.numComponents = VGPU10_OPERAND_0_COMPONENT;
6267 operand0.operandType = VGPU10_OPERAND_TYPE_RESOURCE;
6268 operand0.indexDimension = VGPU10_OPERAND_INDEX_1D;
6269 operand0.index0Representation = VGPU10_OPERAND_INDEX_IMMEDIATE32;
6270
6271 #if 1
6272 /* convert TGSI_RETURN_TYPE_x to VGPU10_RETURN_TYPE_x */
6273 STATIC_ASSERT(VGPU10_RETURN_TYPE_UNORM == TGSI_RETURN_TYPE_UNORM + 1);
6274 STATIC_ASSERT(VGPU10_RETURN_TYPE_SNORM == TGSI_RETURN_TYPE_SNORM + 1);
6275 STATIC_ASSERT(VGPU10_RETURN_TYPE_SINT == TGSI_RETURN_TYPE_SINT + 1);
6276 STATIC_ASSERT(VGPU10_RETURN_TYPE_UINT == TGSI_RETURN_TYPE_UINT + 1);
6277 STATIC_ASSERT(VGPU10_RETURN_TYPE_FLOAT == TGSI_RETURN_TYPE_FLOAT + 1);
6278 assert(emit->sampler_return_type[i] <= TGSI_RETURN_TYPE_FLOAT);
6279 if (emit->sampler_view[i] || !emit->key.tex[i].sampler_view) {
6280 rt = emit->sampler_return_type[i] + 1;
6281 }
6282 else {
6283 rt = emit->key.tex[i].sampler_return_type;
6284 }
6285 #else
6286 switch (emit->sampler_return_type[i]) {
6287 case TGSI_RETURN_TYPE_UNORM: rt = VGPU10_RETURN_TYPE_UNORM; break;
6288 case TGSI_RETURN_TYPE_SNORM: rt = VGPU10_RETURN_TYPE_SNORM; break;
6289 case TGSI_RETURN_TYPE_SINT: rt = VGPU10_RETURN_TYPE_SINT; break;
6290 case TGSI_RETURN_TYPE_UINT: rt = VGPU10_RETURN_TYPE_UINT; break;
6291 case TGSI_RETURN_TYPE_FLOAT: rt = VGPU10_RETURN_TYPE_FLOAT; break;
6292 case TGSI_RETURN_TYPE_COUNT:
6293 default:
6294 rt = VGPU10_RETURN_TYPE_FLOAT;
6295 assert(!"emit_resource_declarations: Unknown tgsi_return_type");
6296 }
6297 #endif
6298
6299 return_type.value = 0;
6300 return_type.component0 = rt;
6301 return_type.component1 = rt;
6302 return_type.component2 = rt;
6303 return_type.component3 = rt;
6304
6305 begin_emit_instruction(emit);
6306 emit_dword(emit, opcode0.value);
6307 emit_dword(emit, operand0.value);
6308 emit_dword(emit, i);
6309 emit_dword(emit, return_type.value);
6310 end_emit_instruction(emit);
6311 }
6312
6313 return true;
6314 }
6315
6316
6317 /**
6318 * Emit instruction to declare uav for the shader image
6319 */
6320 static void
6321 emit_image_declarations(struct svga_shader_emitter_v10 *emit)
6322 {
6323 unsigned i = 0;
6324 unsigned unit = 0;
6325 unsigned uav_mask = 0;
6326
6327 /* Emit uav decl for each image */
6328 for (i = 0; i < emit->num_images; i++, unit++) {
6329
6330 /* Find the unit index of the next declared image.
6331 */
6332 while (!(emit->image_mask & (1 << unit))) {
6333 unit++;
6334 }
6335
6336 VGPU10OpcodeToken0 opcode0;
6337 VGPU10OperandToken0 operand0;
6338 VGPU10ResourceReturnTypeToken return_type;
6339
6340 /* If the corresponding uav for the image is already declared,
6341 * skip this image declaration.
6342 */
6343 if (uav_mask & (1 << emit->key.images[unit].uav_index))
6344 continue;
6345
6346 opcode0.value = 0;
6347 opcode0.opcodeType = VGPU10_OPCODE_DCL_UAV_TYPED;
6348 opcode0.uavResourceDimension =
6349 tgsi_texture_to_resource_dimension(emit->image[unit].Resource,
6350 0, emit->key.images[unit].is_array,
6351 true);
6352
6353 if (emit->key.images[unit].is_single_layer &&
6354 emit->key.images[unit].resource_target == PIPE_TEXTURE_3D) {
6355 opcode0.uavResourceDimension = VGPU10_RESOURCE_DIMENSION_TEXTURE3D;
6356 }
6357
6358 /* Declare the uav as global coherent if the shader includes memory
6359 * barrier instructions.
6360 */
6361 opcode0.globallyCoherent =
6362 (emit->info.opcode_count[TGSI_OPCODE_MEMBAR] > 0) ? 1 : 0;
6363
6364 operand0.value = 0;
6365 operand0.numComponents = VGPU10_OPERAND_0_COMPONENT;
6366 operand0.operandType = VGPU10_OPERAND_TYPE_UAV;
6367 operand0.indexDimension = VGPU10_OPERAND_INDEX_1D;
6368 operand0.index0Representation = VGPU10_OPERAND_INDEX_IMMEDIATE32;
6369
6370 return_type.value = 0;
6371 return_type.component0 =
6372 return_type.component1 =
6373 return_type.component2 =
6374 return_type.component3 = emit->key.images[unit].return_type;
6375
6376 assert(emit->key.images[unit].uav_index != SVGA3D_INVALID_ID);
6377 begin_emit_instruction(emit);
6378 emit_dword(emit, opcode0.value);
6379 emit_dword(emit, operand0.value);
6380 emit_dword(emit, emit->key.images[unit].uav_index);
6381 emit_dword(emit, return_type.value);
6382 end_emit_instruction(emit);
6383
6384 /* Mark the uav is already declared */
6385 uav_mask |= 1 << emit->key.images[unit].uav_index;
6386 }
6387
6388 emit->uav_declared |= uav_mask;
6389 }
6390
6391
6392 /**
6393 * Emit instruction to declare uav for the shader buffer
6394 */
6395 static void
6396 emit_shader_buf_declarations(struct svga_shader_emitter_v10 *emit)
6397 {
6398 unsigned i;
6399 unsigned uav_mask = 0;
6400
6401 /* Emit uav decl for each shader buffer */
6402 for (i = 0; i < emit->num_shader_bufs; i++) {
6403 VGPU10OpcodeToken0 opcode0;
6404 VGPU10OperandToken0 operand0;
6405
6406 if (emit->raw_shaderbufs & (1 << i)) {
6407 emit_rawbuf_declaration(emit, i + emit->raw_shaderbuf_srv_start_index);
6408 continue;
6409 }
6410
6411 /* If the corresponding uav for the shader buf is already declared,
6412 * skip this shader buffer declaration.
6413 */
6414 if (uav_mask & (1 << emit->key.shader_buf_uav_index[i]))
6415 continue;
6416
6417 opcode0.value = 0;
6418 opcode0.opcodeType = VGPU10_OPCODE_DCL_UAV_RAW;
6419
6420 /* Declare the uav as global coherent if the shader includes memory
6421 * barrier instructions.
6422 */
6423 opcode0.globallyCoherent =
6424 (emit->info.opcode_count[TGSI_OPCODE_MEMBAR] > 0) ? 1 : 0;
6425
6426 operand0.value = 0;
6427 operand0.numComponents = VGPU10_OPERAND_0_COMPONENT;
6428 operand0.operandType = VGPU10_OPERAND_TYPE_UAV;
6429 operand0.indexDimension = VGPU10_OPERAND_INDEX_1D;
6430 operand0.index0Representation = VGPU10_OPERAND_INDEX_IMMEDIATE32;
6431
6432 assert(emit->key.shader_buf_uav_index[i] != SVGA3D_INVALID_ID);
6433 begin_emit_instruction(emit);
6434 emit_dword(emit, opcode0.value);
6435 emit_dword(emit, operand0.value);
6436 emit_dword(emit, emit->key.shader_buf_uav_index[i]);
6437 end_emit_instruction(emit);
6438
6439 /* Mark the uav is already declared */
6440 uav_mask |= 1 << emit->key.shader_buf_uav_index[i];
6441 }
6442
6443 emit->uav_declared |= uav_mask;
6444 }
6445
6446
6447 /**
6448 * Emit instruction to declare thread group shared memory(tgsm) for shared memory
6449 */
6450 static void
6451 emit_memory_declarations(struct svga_shader_emitter_v10 *emit)
6452 {
6453 if (emit->cs.shared_memory_declared) {
6454 VGPU10OpcodeToken0 opcode0;
6455 VGPU10OperandToken0 operand0;
6456
6457 opcode0.value = 0;
6458 opcode0.opcodeType = VGPU10_OPCODE_DCL_TGSM_RAW;
6459
6460 /* Declare the uav as global coherent if the shader includes memory
6461 * barrier instructions.
6462 */
6463 opcode0.globallyCoherent =
6464 (emit->info.opcode_count[TGSI_OPCODE_MEMBAR] > 0) ? 1 : 0;
6465
6466 operand0.value = 0;
6467 operand0.numComponents = VGPU10_OPERAND_0_COMPONENT;
6468 operand0.operandType = VGPU10_OPERAND_TYPE_THREAD_GROUP_SHARED_MEMORY;
6469 operand0.indexDimension = VGPU10_OPERAND_INDEX_1D;
6470 operand0.index0Representation = VGPU10_OPERAND_INDEX_IMMEDIATE32;
6471
6472 begin_emit_instruction(emit);
6473 emit_dword(emit, opcode0.value);
6474 emit_dword(emit, operand0.value);
6475
6476 /* Current state tracker only declares one shared memory for GLSL.
6477 * Use index 0 for this shared memory.
6478 */
6479 emit_dword(emit, 0);
6480 emit_dword(emit, emit->key.cs.mem_size); /* byte Count */
6481 end_emit_instruction(emit);
6482 }
6483 }
6484
6485
6486 /**
6487 * Emit instruction to declare uav for atomic buffers
6488 */
6489 static void
6490 emit_atomic_buf_declarations(struct svga_shader_emitter_v10 *emit)
6491 {
6492 unsigned atomic_bufs_mask = emit->atomic_bufs_mask;
6493 unsigned uav_mask = 0;
6494
6495 /* Emit uav decl for each atomic buffer */
6496 while (atomic_bufs_mask) {
6497 unsigned buf_index = u_bit_scan(&atomic_bufs_mask);
6498 unsigned uav_index = emit->key.atomic_buf_uav_index[buf_index];
6499
6500 /* If the corresponding uav for the shader buf is already declared,
6501 * skip this shader buffer declaration.
6502 */
6503 if (uav_mask & (1 << uav_index))
6504 continue;
6505
6506 VGPU10OpcodeToken0 opcode0;
6507 VGPU10OperandToken0 operand0;
6508
6509 assert(uav_index != SVGA3D_INVALID_ID);
6510
6511 opcode0.value = 0;
6512 opcode0.opcodeType = VGPU10_OPCODE_DCL_UAV_RAW;
6513 opcode0.uavResourceDimension = VGPU10_RESOURCE_DIMENSION_BUFFER;
6514
6515 /* Declare the uav as global coherent if the shader includes memory
6516 * barrier instructions.
6517 */
6518 opcode0.globallyCoherent =
6519 (emit->info.opcode_count[TGSI_OPCODE_MEMBAR] > 0) ? 1 : 0;
6520 opcode0.uavHasCounter = 1;
6521
6522 operand0.value = 0;
6523 operand0.numComponents = VGPU10_OPERAND_0_COMPONENT;
6524 operand0.operandType = VGPU10_OPERAND_TYPE_UAV;
6525 operand0.indexDimension = VGPU10_OPERAND_INDEX_1D;
6526 operand0.index0Representation = VGPU10_OPERAND_INDEX_IMMEDIATE32;
6527
6528 begin_emit_instruction(emit);
6529 emit_dword(emit, opcode0.value);
6530 emit_dword(emit, operand0.value);
6531 emit_dword(emit, uav_index);
6532 end_emit_instruction(emit);
6533
6534 /* Mark the uav is already declared */
6535 uav_mask |= 1 << uav_index;
6536 }
6537
6538 emit->uav_declared |= uav_mask;
6539
6540 /* Allocate immediates to be used for index to the atomic buffers */
6541 unsigned j = 0;
6542 for (unsigned i = 0; i <= emit->num_atomic_bufs / 4; i++, j+=4) {
6543 alloc_immediate_int4(emit, j+0, j+1, j+2, j+3);
6544 }
6545
6546 /* Allocate immediates for the atomic counter index */
6547 for (; j <= emit->max_atomic_counter_index; j+=4) {
6548 alloc_immediate_int4(emit, j+0, j+1, j+2, j+3);
6549 }
6550 }
6551
6552
6553 /**
6554 * Emit instruction with n=1, 2 or 3 source registers.
6555 */
6556 static void
6557 emit_instruction_opn(struct svga_shader_emitter_v10 *emit,
6558 unsigned opcode,
6559 const struct tgsi_full_dst_register *dst,
6560 const struct tgsi_full_src_register *src1,
6561 const struct tgsi_full_src_register *src2,
6562 const struct tgsi_full_src_register *src3,
6563 bool saturate, bool precise)
6564 {
6565 begin_emit_instruction(emit);
6566 emit_opcode_precise(emit, opcode, saturate, precise);
6567 emit_dst_register(emit, dst);
6568 emit_src_register(emit, src1);
6569 if (src2) {
6570 emit_src_register(emit, src2);
6571 }
6572 if (src3) {
6573 emit_src_register(emit, src3);
6574 }
6575 end_emit_instruction(emit);
6576 }
6577
6578 static void
6579 emit_instruction_op1(struct svga_shader_emitter_v10 *emit,
6580 unsigned opcode,
6581 const struct tgsi_full_dst_register *dst,
6582 const struct tgsi_full_src_register *src)
6583 {
6584 emit_instruction_opn(emit, opcode, dst, src, NULL, NULL, false, false);
6585 }
6586
6587 static void
6588 emit_instruction_op2(struct svga_shader_emitter_v10 *emit,
6589 VGPU10_OPCODE_TYPE opcode,
6590 const struct tgsi_full_dst_register *dst,
6591 const struct tgsi_full_src_register *src1,
6592 const struct tgsi_full_src_register *src2)
6593 {
6594 emit_instruction_opn(emit, opcode, dst, src1, src2, NULL, false, false);
6595 }
6596
6597 static void
6598 emit_instruction_op3(struct svga_shader_emitter_v10 *emit,
6599 VGPU10_OPCODE_TYPE opcode,
6600 const struct tgsi_full_dst_register *dst,
6601 const struct tgsi_full_src_register *src1,
6602 const struct tgsi_full_src_register *src2,
6603 const struct tgsi_full_src_register *src3)
6604 {
6605 emit_instruction_opn(emit, opcode, dst, src1, src2, src3, false, false);
6606 }
6607
6608 static void
6609 emit_instruction_op0(struct svga_shader_emitter_v10 *emit,
6610 VGPU10_OPCODE_TYPE opcode)
6611 {
6612 begin_emit_instruction(emit);
6613 emit_opcode(emit, opcode, false);
6614 end_emit_instruction(emit);
6615 }
6616
6617 /**
6618 * Tessellation inner/outer levels needs to be store into its
6619 * appropriate registers depending on prim_mode.
6620 */
6621 static void
6622 store_tesslevels(struct svga_shader_emitter_v10 *emit)
6623 {
6624 int i;
6625
6626 /* tessellation levels are required input/out in hull shader.
6627 * emitting the inner/outer tessellation levels, either from
6628 * values provided in tcs or fallback default values which is 1.0
6629 */
6630 if (emit->key.tcs.prim_mode == MESA_PRIM_QUADS) {
6631 struct tgsi_full_src_register temp_src;
6632
6633 if (emit->tcs.inner.tgsi_index != INVALID_INDEX)
6634 temp_src = make_src_temp_reg(emit->tcs.inner.temp_index);
6635 else
6636 temp_src = make_immediate_reg_float(emit, 1.0f);
6637
6638 for (i = 0; i < 2; i++) {
6639 struct tgsi_full_src_register src =
6640 scalar_src(&temp_src, TGSI_SWIZZLE_X + i);
6641 struct tgsi_full_dst_register dst =
6642 make_dst_reg(TGSI_FILE_OUTPUT, emit->tcs.inner.out_index + i);
6643 dst = writemask_dst(&dst, TGSI_WRITEMASK_X);
6644 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst, &src);
6645 }
6646
6647 if (emit->tcs.outer.tgsi_index != INVALID_INDEX)
6648 temp_src = make_src_temp_reg(emit->tcs.outer.temp_index);
6649 else
6650 temp_src = make_immediate_reg_float(emit, 1.0f);
6651
6652 for (i = 0; i < 4; i++) {
6653 struct tgsi_full_src_register src =
6654 scalar_src(&temp_src, TGSI_SWIZZLE_X + i);
6655 struct tgsi_full_dst_register dst =
6656 make_dst_reg(TGSI_FILE_OUTPUT, emit->tcs.outer.out_index + i);
6657 dst = writemask_dst(&dst, TGSI_WRITEMASK_X);
6658 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst, &src);
6659 }
6660 }
6661 else if (emit->key.tcs.prim_mode == MESA_PRIM_TRIANGLES) {
6662 struct tgsi_full_src_register temp_src;
6663
6664 if (emit->tcs.inner.tgsi_index != INVALID_INDEX)
6665 temp_src = make_src_temp_reg(emit->tcs.inner.temp_index);
6666 else
6667 temp_src = make_immediate_reg_float(emit, 1.0f);
6668
6669 struct tgsi_full_src_register src =
6670 scalar_src(&temp_src, TGSI_SWIZZLE_X);
6671 struct tgsi_full_dst_register dst =
6672 make_dst_reg(TGSI_FILE_OUTPUT, emit->tcs.inner.out_index);
6673 dst = writemask_dst(&dst, TGSI_WRITEMASK_X);
6674 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst, &src);
6675
6676 if (emit->tcs.outer.tgsi_index != INVALID_INDEX)
6677 temp_src = make_src_temp_reg(emit->tcs.outer.temp_index);
6678 else
6679 temp_src = make_immediate_reg_float(emit, 1.0f);
6680
6681 for (i = 0; i < 3; i++) {
6682 struct tgsi_full_src_register src =
6683 scalar_src(&temp_src, TGSI_SWIZZLE_X + i);
6684 struct tgsi_full_dst_register dst =
6685 make_dst_reg(TGSI_FILE_OUTPUT, emit->tcs.outer.out_index + i);
6686 dst = writemask_dst(&dst, TGSI_WRITEMASK_X);
6687 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst, &src);
6688 }
6689 }
6690 else if (emit->key.tcs.prim_mode == MESA_PRIM_LINES) {
6691 if (emit->tcs.outer.tgsi_index != INVALID_INDEX) {
6692 struct tgsi_full_src_register temp_src =
6693 make_src_temp_reg(emit->tcs.outer.temp_index);
6694 for (i = 0; i < 2; i++) {
6695 struct tgsi_full_src_register src =
6696 scalar_src(&temp_src, TGSI_SWIZZLE_X + i);
6697 struct tgsi_full_dst_register dst =
6698 make_dst_reg(TGSI_FILE_OUTPUT,
6699 emit->tcs.outer.out_index + i);
6700 dst = writemask_dst(&dst, TGSI_WRITEMASK_X);
6701 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst, &src);
6702 }
6703 }
6704 }
6705 else {
6706 debug_printf("Unsupported primitive type");
6707 }
6708 }
6709
6710
6711 /**
6712 * Emit the actual clip distance instructions to be used for clipping
6713 * by copying the clip distance from the temporary registers to the
6714 * CLIPDIST registers written with the enabled planes mask.
6715 * Also copy the clip distance from the temporary to the clip distance
6716 * shadow copy register which will be referenced by the input shader
6717 */
6718 static void
6719 emit_clip_distance_instructions(struct svga_shader_emitter_v10 *emit)
6720 {
6721 struct tgsi_full_src_register tmp_clip_dist_src;
6722 struct tgsi_full_dst_register clip_dist_dst;
6723
6724 unsigned i;
6725 unsigned clip_plane_enable = emit->key.clip_plane_enable;
6726 unsigned clip_dist_tmp_index = emit->clip_dist_tmp_index;
6727 int num_written_clipdist = emit->info.num_written_clipdistance;
6728
6729 assert(emit->clip_dist_out_index != INVALID_INDEX);
6730 assert(emit->clip_dist_tmp_index != INVALID_INDEX);
6731
6732 /**
6733 * Temporary reset the temporary clip dist register index so
6734 * that the copy to the real clip dist register will not
6735 * attempt to copy to the temporary register again
6736 */
6737 emit->clip_dist_tmp_index = INVALID_INDEX;
6738
6739 for (i = 0; i < 2 && num_written_clipdist > 0; i++, num_written_clipdist-=4) {
6740
6741 tmp_clip_dist_src = make_src_temp_reg(clip_dist_tmp_index + i);
6742
6743 /**
6744 * copy to the shadow copy for use by varying variable and
6745 * stream output. All clip distances
6746 * will be written regardless of the enabled clipping planes.
6747 */
6748 clip_dist_dst = make_dst_reg(TGSI_FILE_OUTPUT,
6749 emit->clip_dist_so_index + i);
6750
6751 /* MOV clip_dist_so, tmp_clip_dist */
6752 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &clip_dist_dst,
6753 &tmp_clip_dist_src);
6754
6755 /**
6756 * copy those clip distances to enabled clipping planes
6757 * to CLIPDIST registers for clipping
6758 */
6759 if (clip_plane_enable & 0xf) {
6760 clip_dist_dst = make_dst_reg(TGSI_FILE_OUTPUT,
6761 emit->clip_dist_out_index + i);
6762 clip_dist_dst = writemask_dst(&clip_dist_dst, clip_plane_enable & 0xf);
6763
6764 /* MOV CLIPDIST, tmp_clip_dist */
6765 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &clip_dist_dst,
6766 &tmp_clip_dist_src);
6767 }
6768 /* four clip planes per clip register */
6769 clip_plane_enable >>= 4;
6770 }
6771 /**
6772 * set the temporary clip dist register index back to the
6773 * temporary index for the next vertex
6774 */
6775 emit->clip_dist_tmp_index = clip_dist_tmp_index;
6776 }
6777
6778 /* Declare clip distance output registers for user-defined clip planes
6779 * or the TGSI_CLIPVERTEX output.
6780 */
6781 static void
6782 emit_clip_distance_declarations(struct svga_shader_emitter_v10 *emit)
6783 {
6784 unsigned num_clip_planes = util_bitcount(emit->key.clip_plane_enable);
6785 unsigned index = emit->num_outputs;
6786 unsigned plane_mask;
6787
6788 assert(emit->unit != MESA_SHADER_FRAGMENT);
6789 assert(num_clip_planes <= 8);
6790
6791 if (emit->clip_mode != CLIP_LEGACY &&
6792 emit->clip_mode != CLIP_VERTEX) {
6793 return;
6794 }
6795
6796 if (num_clip_planes == 0)
6797 return;
6798
6799 /* Convert clip vertex to clip distances only in the last vertex stage */
6800 if (!emit->key.last_vertex_stage)
6801 return;
6802
6803 /* Declare one or two clip output registers. The number of components
6804 * in the mask reflects the number of clip planes. For example, if 5
6805 * clip planes are needed, we'll declare outputs similar to:
6806 * dcl_output_siv o2.xyzw, clip_distance
6807 * dcl_output_siv o3.x, clip_distance
6808 */
6809 emit->clip_dist_out_index = index; /* save the starting clip dist reg index */
6810
6811 plane_mask = (1 << num_clip_planes) - 1;
6812 if (plane_mask & 0xf) {
6813 unsigned cmask = plane_mask & VGPU10_OPERAND_4_COMPONENT_MASK_ALL;
6814 emit_output_declaration(emit, VGPU10_OPCODE_DCL_OUTPUT_SIV, index,
6815 VGPU10_NAME_CLIP_DISTANCE, cmask, true,
6816 SVGADX_SIGNATURE_SEMANTIC_NAME_CLIP_DISTANCE);
6817 emit->num_outputs++;
6818 }
6819 if (plane_mask & 0xf0) {
6820 unsigned cmask = (plane_mask >> 4) & VGPU10_OPERAND_4_COMPONENT_MASK_ALL;
6821 emit_output_declaration(emit, VGPU10_OPCODE_DCL_OUTPUT_SIV, index + 1,
6822 VGPU10_NAME_CLIP_DISTANCE, cmask, true,
6823 SVGADX_SIGNATURE_SEMANTIC_NAME_CLIP_DISTANCE);
6824 emit->num_outputs++;
6825 }
6826 }
6827
6828
6829 /**
6830 * Emit the instructions for writing to the clip distance registers
6831 * to handle legacy/automatic clip planes.
6832 * For each clip plane, the distance is the dot product of the vertex
6833 * position (found in TEMP[vpos_tmp_index]) and the clip plane coefficients.
6834 * This is not used when the shader has an explicit CLIPVERTEX or CLIPDISTANCE
6835 * output registers already declared.
6836 */
6837 static void
6838 emit_clip_distance_from_vpos(struct svga_shader_emitter_v10 *emit,
6839 unsigned vpos_tmp_index)
6840 {
6841 unsigned i, num_clip_planes = util_bitcount(emit->key.clip_plane_enable);
6842
6843 assert(emit->clip_mode == CLIP_LEGACY);
6844 assert(num_clip_planes <= 8);
6845
6846 assert(emit->unit == MESA_SHADER_VERTEX ||
6847 emit->unit == MESA_SHADER_GEOMETRY ||
6848 emit->unit == MESA_SHADER_TESS_EVAL);
6849
6850 for (i = 0; i < num_clip_planes; i++) {
6851 struct tgsi_full_dst_register dst;
6852 struct tgsi_full_src_register plane_src, vpos_src;
6853 unsigned reg_index = emit->clip_dist_out_index + i / 4;
6854 unsigned comp = i % 4;
6855 unsigned writemask = VGPU10_OPERAND_4_COMPONENT_MASK_X << comp;
6856
6857 /* create dst, src regs */
6858 dst = make_dst_reg(TGSI_FILE_OUTPUT, reg_index);
6859 dst = writemask_dst(&dst, writemask);
6860
6861 plane_src = make_src_const_reg(emit->clip_plane_const[i]);
6862 vpos_src = make_src_temp_reg(vpos_tmp_index);
6863
6864 /* DP4 clip_dist, plane, vpos */
6865 emit_instruction_op2(emit, VGPU10_OPCODE_DP4, &dst,
6866 &plane_src, &vpos_src);
6867 }
6868 }
6869
6870
6871 /**
6872 * Emit the instructions for computing the clip distance results from
6873 * the clip vertex temporary.
6874 * For each clip plane, the distance is the dot product of the clip vertex
6875 * position (found in a temp reg) and the clip plane coefficients.
6876 */
6877 static void
6878 emit_clip_vertex_instructions(struct svga_shader_emitter_v10 *emit)
6879 {
6880 const unsigned num_clip = util_bitcount(emit->key.clip_plane_enable);
6881 unsigned i;
6882 struct tgsi_full_dst_register dst;
6883 struct tgsi_full_src_register clipvert_src;
6884 const unsigned clip_vertex_tmp = emit->clip_vertex_tmp_index;
6885
6886 assert(emit->unit == MESA_SHADER_VERTEX ||
6887 emit->unit == MESA_SHADER_GEOMETRY ||
6888 emit->unit == MESA_SHADER_TESS_EVAL);
6889
6890 assert(emit->clip_mode == CLIP_VERTEX);
6891
6892 clipvert_src = make_src_temp_reg(clip_vertex_tmp);
6893
6894 for (i = 0; i < num_clip; i++) {
6895 struct tgsi_full_src_register plane_src;
6896 unsigned reg_index = emit->clip_dist_out_index + i / 4;
6897 unsigned comp = i % 4;
6898 unsigned writemask = VGPU10_OPERAND_4_COMPONENT_MASK_X << comp;
6899
6900 /* create dst, src regs */
6901 dst = make_dst_reg(TGSI_FILE_OUTPUT, reg_index);
6902 dst = writemask_dst(&dst, writemask);
6903
6904 plane_src = make_src_const_reg(emit->clip_plane_const[i]);
6905
6906 /* DP4 clip_dist, plane, vpos */
6907 emit_instruction_op2(emit, VGPU10_OPCODE_DP4, &dst,
6908 &plane_src, &clipvert_src);
6909 }
6910
6911 /* copy temporary clip vertex register to the clip vertex register */
6912
6913 assert(emit->clip_vertex_out_index != INVALID_INDEX);
6914
6915 /**
6916 * temporary reset the temporary clip vertex register index so
6917 * that copy to the clip vertex register will not attempt
6918 * to copy to the temporary register again
6919 */
6920 emit->clip_vertex_tmp_index = INVALID_INDEX;
6921
6922 /* MOV clip_vertex, clip_vertex_tmp */
6923 dst = make_dst_reg(TGSI_FILE_OUTPUT, emit->clip_vertex_out_index);
6924 emit_instruction_op1(emit, VGPU10_OPCODE_MOV,
6925 &dst, &clipvert_src);
6926
6927 /**
6928 * set the temporary clip vertex register index back to the
6929 * temporary index for the next vertex
6930 */
6931 emit->clip_vertex_tmp_index = clip_vertex_tmp;
6932 }
6933
6934 /**
6935 * Emit code to convert RGBA to BGRA
6936 */
6937 static void
6938 emit_swap_r_b(struct svga_shader_emitter_v10 *emit,
6939 const struct tgsi_full_dst_register *dst,
6940 const struct tgsi_full_src_register *src)
6941 {
6942 struct tgsi_full_src_register bgra_src =
6943 swizzle_src(src, TGSI_SWIZZLE_Z, TGSI_SWIZZLE_Y, TGSI_SWIZZLE_X, TGSI_SWIZZLE_W);
6944
6945 begin_emit_instruction(emit);
6946 emit_opcode(emit, VGPU10_OPCODE_MOV, false);
6947 emit_dst_register(emit, dst);
6948 emit_src_register(emit, &bgra_src);
6949 end_emit_instruction(emit);
6950 }
6951
6952
6953 /** Convert from 10_10_10_2 normalized to 10_10_10_2_snorm */
6954 static void
6955 emit_puint_to_snorm(struct svga_shader_emitter_v10 *emit,
6956 const struct tgsi_full_dst_register *dst,
6957 const struct tgsi_full_src_register *src)
6958 {
6959 struct tgsi_full_src_register half = make_immediate_reg_float(emit, 0.5f);
6960 struct tgsi_full_src_register two =
6961 make_immediate_reg_float4(emit, 2.0f, 2.0f, 2.0f, 3.0f);
6962 struct tgsi_full_src_register neg_two =
6963 make_immediate_reg_float4(emit, -2.0f, -2.0f, -2.0f, -1.66666f);
6964
6965 unsigned val_tmp = get_temp_index(emit);
6966 struct tgsi_full_dst_register val_dst = make_dst_temp_reg(val_tmp);
6967 struct tgsi_full_src_register val_src = make_src_temp_reg(val_tmp);
6968
6969 unsigned bias_tmp = get_temp_index(emit);
6970 struct tgsi_full_dst_register bias_dst = make_dst_temp_reg(bias_tmp);
6971 struct tgsi_full_src_register bias_src = make_src_temp_reg(bias_tmp);
6972
6973 /* val = src * 2.0 */
6974 emit_instruction_op2(emit, VGPU10_OPCODE_MUL, &val_dst, src, &two);
6975
6976 /* bias = src > 0.5 */
6977 emit_instruction_op2(emit, VGPU10_OPCODE_GE, &bias_dst, src, &half);
6978
6979 /* bias = bias & -2.0 */
6980 emit_instruction_op2(emit, VGPU10_OPCODE_AND, &bias_dst,
6981 &bias_src, &neg_two);
6982
6983 /* dst = val + bias */
6984 emit_instruction_op2(emit, VGPU10_OPCODE_ADD, dst,
6985 &val_src, &bias_src);
6986
6987 free_temp_indexes(emit);
6988 }
6989
6990
6991 /** Convert from 10_10_10_2_unorm to 10_10_10_2_uscaled */
6992 static void
6993 emit_puint_to_uscaled(struct svga_shader_emitter_v10 *emit,
6994 const struct tgsi_full_dst_register *dst,
6995 const struct tgsi_full_src_register *src)
6996 {
6997 struct tgsi_full_src_register scale =
6998 make_immediate_reg_float4(emit, 1023.0f, 1023.0f, 1023.0f, 3.0f);
6999
7000 /* dst = src * scale */
7001 emit_instruction_op2(emit, VGPU10_OPCODE_MUL, dst, src, &scale);
7002 }
7003
7004
7005 /** Convert from R32_UINT to 10_10_10_2_sscaled */
7006 static void
7007 emit_puint_to_sscaled(struct svga_shader_emitter_v10 *emit,
7008 const struct tgsi_full_dst_register *dst,
7009 const struct tgsi_full_src_register *src)
7010 {
7011 struct tgsi_full_src_register lshift =
7012 make_immediate_reg_int4(emit, 22, 12, 2, 0);
7013 struct tgsi_full_src_register rshift =
7014 make_immediate_reg_int4(emit, 22, 22, 22, 30);
7015
7016 struct tgsi_full_src_register src_xxxx = scalar_src(src, TGSI_SWIZZLE_X);
7017
7018 unsigned tmp = get_temp_index(emit);
7019 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
7020 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
7021
7022 /*
7023 * r = (pixel << 22) >> 22; # signed int in [511, -512]
7024 * g = (pixel << 12) >> 22; # signed int in [511, -512]
7025 * b = (pixel << 2) >> 22; # signed int in [511, -512]
7026 * a = (pixel << 0) >> 30; # signed int in [1, -2]
7027 * dst = i_to_f(r,g,b,a); # convert to float
7028 */
7029 emit_instruction_op2(emit, VGPU10_OPCODE_ISHL, &tmp_dst,
7030 &src_xxxx, &lshift);
7031 emit_instruction_op2(emit, VGPU10_OPCODE_ISHR, &tmp_dst,
7032 &tmp_src, &rshift);
7033 emit_instruction_op1(emit, VGPU10_OPCODE_ITOF, dst, &tmp_src);
7034
7035 free_temp_indexes(emit);
7036 }
7037
7038
7039 /**
7040 * Emit code for TGSI_OPCODE_ARL or TGSI_OPCODE_UARL instruction.
7041 */
7042 static bool
7043 emit_arl_uarl(struct svga_shader_emitter_v10 *emit,
7044 const struct tgsi_full_instruction *inst)
7045 {
7046 unsigned index = inst->Dst[0].Register.Index;
7047 struct tgsi_full_dst_register dst;
7048 VGPU10_OPCODE_TYPE opcode;
7049
7050 assert(index < MAX_VGPU10_ADDR_REGS);
7051 dst = make_dst_temp_reg(emit->address_reg_index[index]);
7052 dst = writemask_dst(&dst, inst->Dst[0].Register.WriteMask);
7053
7054 /* ARL dst, s0
7055 * Translates into:
7056 * FTOI address_tmp, s0
7057 *
7058 * UARL dst, s0
7059 * Translates into:
7060 * MOV address_tmp, s0
7061 */
7062 if (inst->Instruction.Opcode == TGSI_OPCODE_ARL)
7063 opcode = VGPU10_OPCODE_FTOI;
7064 else
7065 opcode = VGPU10_OPCODE_MOV;
7066
7067 emit_instruction_op1(emit, opcode, &dst, &inst->Src[0]);
7068
7069 return true;
7070 }
7071
7072
7073 /**
7074 * Emit code for TGSI_OPCODE_CAL instruction.
7075 */
7076 static bool
7077 emit_cal(struct svga_shader_emitter_v10 *emit,
7078 const struct tgsi_full_instruction *inst)
7079 {
7080 unsigned label = inst->Label.Label;
7081 VGPU10OperandToken0 operand;
7082 operand.value = 0;
7083 operand.operandType = VGPU10_OPERAND_TYPE_LABEL;
7084
7085 begin_emit_instruction(emit);
7086 emit_dword(emit, operand.value);
7087 emit_dword(emit, label);
7088 end_emit_instruction(emit);
7089
7090 return true;
7091 }
7092
7093
7094 /**
7095 * Emit code for TGSI_OPCODE_IABS instruction.
7096 */
7097 static bool
7098 emit_iabs(struct svga_shader_emitter_v10 *emit,
7099 const struct tgsi_full_instruction *inst)
7100 {
7101 /* dst.x = (src0.x < 0) ? -src0.x : src0.x
7102 * dst.y = (src0.y < 0) ? -src0.y : src0.y
7103 * dst.z = (src0.z < 0) ? -src0.z : src0.z
7104 * dst.w = (src0.w < 0) ? -src0.w : src0.w
7105 *
7106 * Translates into
7107 * IMAX dst, src, neg(src)
7108 */
7109 struct tgsi_full_src_register neg_src = negate_src(&inst->Src[0]);
7110 emit_instruction_op2(emit, VGPU10_OPCODE_IMAX, &inst->Dst[0],
7111 &inst->Src[0], &neg_src);
7112
7113 return true;
7114 }
7115
7116
7117 /**
7118 * Emit code for TGSI_OPCODE_CMP instruction.
7119 */
7120 static bool
7121 emit_cmp(struct svga_shader_emitter_v10 *emit,
7122 const struct tgsi_full_instruction *inst)
7123 {
7124 /* dst.x = (src0.x < 0) ? src1.x : src2.x
7125 * dst.y = (src0.y < 0) ? src1.y : src2.y
7126 * dst.z = (src0.z < 0) ? src1.z : src2.z
7127 * dst.w = (src0.w < 0) ? src1.w : src2.w
7128 *
7129 * Translates into
7130 * LT tmp, src0, 0.0
7131 * MOVC dst, tmp, src1, src2
7132 */
7133 struct tgsi_full_src_register zero = make_immediate_reg_float(emit, 0.0f);
7134 unsigned tmp = get_temp_index(emit);
7135 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
7136 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
7137
7138 emit_instruction_opn(emit, VGPU10_OPCODE_LT, &tmp_dst,
7139 &inst->Src[0], &zero, NULL, false,
7140 inst->Instruction.Precise);
7141 emit_instruction_opn(emit, VGPU10_OPCODE_MOVC, &inst->Dst[0],
7142 &tmp_src, &inst->Src[1], &inst->Src[2],
7143 inst->Instruction.Saturate, false);
7144
7145 free_temp_indexes(emit);
7146
7147 return true;
7148 }
7149
7150
7151 /**
7152 * Emit code for TGSI_OPCODE_DST instruction.
7153 */
7154 static bool
7155 emit_dst(struct svga_shader_emitter_v10 *emit,
7156 const struct tgsi_full_instruction *inst)
7157 {
7158 /*
7159 * dst.x = 1
7160 * dst.y = src0.y * src1.y
7161 * dst.z = src0.z
7162 * dst.w = src1.w
7163 */
7164
7165 struct tgsi_full_src_register s0_yyyy =
7166 scalar_src(&inst->Src[0], TGSI_SWIZZLE_Y);
7167 struct tgsi_full_src_register s0_zzzz =
7168 scalar_src(&inst->Src[0], TGSI_SWIZZLE_Z);
7169 struct tgsi_full_src_register s1_yyyy =
7170 scalar_src(&inst->Src[1], TGSI_SWIZZLE_Y);
7171 struct tgsi_full_src_register s1_wwww =
7172 scalar_src(&inst->Src[1], TGSI_SWIZZLE_W);
7173
7174 /*
7175 * If dst and either src0 and src1 are the same we need
7176 * to create a temporary for it and insert a extra move.
7177 */
7178 unsigned tmp_move = get_temp_index(emit);
7179 struct tgsi_full_src_register move_src = make_src_temp_reg(tmp_move);
7180 struct tgsi_full_dst_register move_dst = make_dst_temp_reg(tmp_move);
7181
7182 /* MOV dst.x, 1.0 */
7183 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_X) {
7184 struct tgsi_full_dst_register dst_x =
7185 writemask_dst(&move_dst, TGSI_WRITEMASK_X);
7186 struct tgsi_full_src_register one = make_immediate_reg_float(emit, 1.0f);
7187
7188 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst_x, &one);
7189 }
7190
7191 /* MUL dst.y, s0.y, s1.y */
7192 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Y) {
7193 struct tgsi_full_dst_register dst_y =
7194 writemask_dst(&move_dst, TGSI_WRITEMASK_Y);
7195
7196 emit_instruction_opn(emit, VGPU10_OPCODE_MUL, &dst_y, &s0_yyyy,
7197 &s1_yyyy, NULL, inst->Instruction.Saturate,
7198 inst->Instruction.Precise);
7199 }
7200
7201 /* MOV dst.z, s0.z */
7202 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Z) {
7203 struct tgsi_full_dst_register dst_z =
7204 writemask_dst(&move_dst, TGSI_WRITEMASK_Z);
7205
7206 emit_instruction_opn(emit, VGPU10_OPCODE_MOV,
7207 &dst_z, &s0_zzzz, NULL, NULL,
7208 inst->Instruction.Saturate,
7209 inst->Instruction.Precise);
7210 }
7211
7212 /* MOV dst.w, s1.w */
7213 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_W) {
7214 struct tgsi_full_dst_register dst_w =
7215 writemask_dst(&move_dst, TGSI_WRITEMASK_W);
7216
7217 emit_instruction_opn(emit, VGPU10_OPCODE_MOV,
7218 &dst_w, &s1_wwww, NULL, NULL,
7219 inst->Instruction.Saturate,
7220 inst->Instruction.Precise);
7221 }
7222
7223 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &inst->Dst[0], &move_src);
7224 free_temp_indexes(emit);
7225
7226 return true;
7227 }
7228
7229
7230 /**
7231 * A helper function to return the stream index as specified in
7232 * the immediate register
7233 */
7234 static inline unsigned
7235 find_stream_index(struct svga_shader_emitter_v10 *emit,
7236 const struct tgsi_full_src_register *src)
7237 {
7238 return emit->immediates[src->Register.Index][src->Register.SwizzleX].Int;
7239 }
7240
7241
7242 /**
7243 * Emit code for TGSI_OPCODE_ENDPRIM (GS only)
7244 */
7245 static bool
7246 emit_endprim(struct svga_shader_emitter_v10 *emit,
7247 const struct tgsi_full_instruction *inst)
7248 {
7249 assert(emit->unit == MESA_SHADER_GEOMETRY);
7250
7251 begin_emit_instruction(emit);
7252 if (emit->version >= 50) {
7253 unsigned streamIndex = find_stream_index(emit, &inst->Src[0]);
7254
7255 if (emit->info.num_stream_output_components[streamIndex] == 0) {
7256 /**
7257 * If there is no output for this stream, discard this instruction.
7258 */
7259 emit->discard_instruction = true;
7260 }
7261 else {
7262 emit_opcode(emit, VGPU10_OPCODE_CUT_STREAM, false);
7263 assert(inst->Src[0].Register.File == TGSI_FILE_IMMEDIATE);
7264 emit_stream_register(emit, streamIndex);
7265 }
7266 }
7267 else {
7268 emit_opcode(emit, VGPU10_OPCODE_CUT, false);
7269 }
7270 end_emit_instruction(emit);
7271 return true;
7272 }
7273
7274
7275 /**
7276 * Emit code for TGSI_OPCODE_EX2 (2^x) instruction.
7277 */
7278 static bool
7279 emit_ex2(struct svga_shader_emitter_v10 *emit,
7280 const struct tgsi_full_instruction *inst)
7281 {
7282 /* Note that TGSI_OPCODE_EX2 computes only one value from src.x
7283 * while VGPU10 computes four values.
7284 *
7285 * dst = EX2(src):
7286 * dst.xyzw = 2.0 ^ src.x
7287 */
7288
7289 struct tgsi_full_src_register src_xxxx =
7290 swizzle_src(&inst->Src[0], TGSI_SWIZZLE_X, TGSI_SWIZZLE_X,
7291 TGSI_SWIZZLE_X, TGSI_SWIZZLE_X);
7292
7293 /* EXP tmp, s0.xxxx */
7294 emit_instruction_opn(emit, VGPU10_OPCODE_EXP, &inst->Dst[0], &src_xxxx,
7295 NULL, NULL,
7296 inst->Instruction.Saturate,
7297 inst->Instruction.Precise);
7298
7299 return true;
7300 }
7301
7302
7303 /**
7304 * Emit code for TGSI_OPCODE_EXP instruction.
7305 */
7306 static bool
7307 emit_exp(struct svga_shader_emitter_v10 *emit,
7308 const struct tgsi_full_instruction *inst)
7309 {
7310 /*
7311 * dst.x = 2 ^ floor(s0.x)
7312 * dst.y = s0.x - floor(s0.x)
7313 * dst.z = 2 ^ s0.x
7314 * dst.w = 1.0
7315 */
7316
7317 struct tgsi_full_src_register src_xxxx =
7318 scalar_src(&inst->Src[0], TGSI_SWIZZLE_X);
7319 unsigned tmp = get_temp_index(emit);
7320 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
7321 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
7322
7323 /*
7324 * If dst and src are the same we need to create
7325 * a temporary for it and insert a extra move.
7326 */
7327 unsigned tmp_move = get_temp_index(emit);
7328 struct tgsi_full_src_register move_src = make_src_temp_reg(tmp_move);
7329 struct tgsi_full_dst_register move_dst = make_dst_temp_reg(tmp_move);
7330
7331 /* only use X component of temp reg */
7332 tmp_dst = writemask_dst(&tmp_dst, TGSI_WRITEMASK_X);
7333 tmp_src = scalar_src(&tmp_src, TGSI_SWIZZLE_X);
7334
7335 /* ROUND_NI tmp.x, s0.x */
7336 emit_instruction_op1(emit, VGPU10_OPCODE_ROUND_NI, &tmp_dst,
7337 &src_xxxx); /* round to -infinity */
7338
7339 /* EXP dst.x, tmp.x */
7340 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_X) {
7341 struct tgsi_full_dst_register dst_x =
7342 writemask_dst(&move_dst, TGSI_WRITEMASK_X);
7343
7344 emit_instruction_opn(emit, VGPU10_OPCODE_EXP, &dst_x, &tmp_src,
7345 NULL, NULL,
7346 inst->Instruction.Saturate,
7347 inst->Instruction.Precise);
7348 }
7349
7350 /* ADD dst.y, s0.x, -tmp */
7351 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Y) {
7352 struct tgsi_full_dst_register dst_y =
7353 writemask_dst(&move_dst, TGSI_WRITEMASK_Y);
7354 struct tgsi_full_src_register neg_tmp_src = negate_src(&tmp_src);
7355
7356 emit_instruction_opn(emit, VGPU10_OPCODE_ADD, &dst_y, &src_xxxx,
7357 &neg_tmp_src, NULL,
7358 inst->Instruction.Saturate,
7359 inst->Instruction.Precise);
7360 }
7361
7362 /* EXP dst.z, s0.x */
7363 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Z) {
7364 struct tgsi_full_dst_register dst_z =
7365 writemask_dst(&move_dst, TGSI_WRITEMASK_Z);
7366
7367 emit_instruction_opn(emit, VGPU10_OPCODE_EXP, &dst_z, &src_xxxx,
7368 NULL, NULL,
7369 inst->Instruction.Saturate,
7370 inst->Instruction.Precise);
7371 }
7372
7373 /* MOV dst.w, 1.0 */
7374 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_W) {
7375 struct tgsi_full_dst_register dst_w =
7376 writemask_dst(&move_dst, TGSI_WRITEMASK_W);
7377 struct tgsi_full_src_register one = make_immediate_reg_float(emit, 1.0f);
7378
7379 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst_w, &one);
7380 }
7381
7382 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &inst->Dst[0], &move_src);
7383
7384 free_temp_indexes(emit);
7385
7386 return true;
7387 }
7388
7389
7390 /**
7391 * Emit code for TGSI_OPCODE_IF instruction.
7392 */
7393 static bool
7394 emit_if(struct svga_shader_emitter_v10 *emit,
7395 const struct tgsi_full_src_register *src)
7396 {
7397 VGPU10OpcodeToken0 opcode0;
7398
7399 /* The src register should be a scalar */
7400 assert(src->Register.SwizzleX == src->Register.SwizzleY &&
7401 src->Register.SwizzleX == src->Register.SwizzleZ &&
7402 src->Register.SwizzleX == src->Register.SwizzleW);
7403
7404 /* The only special thing here is that we need to set the
7405 * VGPU10_INSTRUCTION_TEST_NONZERO flag since we want to test if
7406 * src.x is non-zero.
7407 */
7408 opcode0.value = 0;
7409 opcode0.opcodeType = VGPU10_OPCODE_IF;
7410 opcode0.testBoolean = VGPU10_INSTRUCTION_TEST_NONZERO;
7411
7412 begin_emit_instruction(emit);
7413 emit_dword(emit, opcode0.value);
7414 emit_src_register(emit, src);
7415 end_emit_instruction(emit);
7416
7417 return true;
7418 }
7419
7420
7421 /**
7422 * Emit code for conditional discard instruction (discard fragment if any of
7423 * the register components are negative).
7424 */
7425 static bool
7426 emit_cond_discard(struct svga_shader_emitter_v10 *emit,
7427 const struct tgsi_full_instruction *inst)
7428 {
7429 unsigned tmp = get_temp_index(emit);
7430 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
7431 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
7432
7433 struct tgsi_full_src_register zero = make_immediate_reg_float(emit, 0.0f);
7434
7435 struct tgsi_full_dst_register tmp_dst_x =
7436 writemask_dst(&tmp_dst, TGSI_WRITEMASK_X);
7437 struct tgsi_full_src_register tmp_src_xxxx =
7438 scalar_src(&tmp_src, TGSI_SWIZZLE_X);
7439
7440 /* tmp = src[0] < 0.0 */
7441 emit_instruction_op2(emit, VGPU10_OPCODE_LT, &tmp_dst, &inst->Src[0], &zero);
7442
7443 if (!same_swizzle_terms(&inst->Src[0])) {
7444 /* If the swizzle is not XXXX, YYYY, ZZZZ or WWWW we need to
7445 * logically OR the swizzle terms. Most uses of this conditional
7446 * discard instruction only test one channel so it's good to
7447 * avoid these extra steps.
7448 */
7449 struct tgsi_full_src_register tmp_src_yyyy =
7450 scalar_src(&tmp_src, TGSI_SWIZZLE_Y);
7451 struct tgsi_full_src_register tmp_src_zzzz =
7452 scalar_src(&tmp_src, TGSI_SWIZZLE_Z);
7453 struct tgsi_full_src_register tmp_src_wwww =
7454 scalar_src(&tmp_src, TGSI_SWIZZLE_W);
7455
7456 emit_instruction_op2(emit, VGPU10_OPCODE_OR, &tmp_dst_x, &tmp_src_xxxx,
7457 &tmp_src_yyyy);
7458 emit_instruction_op2(emit, VGPU10_OPCODE_OR, &tmp_dst_x, &tmp_src_xxxx,
7459 &tmp_src_zzzz);
7460 emit_instruction_op2(emit, VGPU10_OPCODE_OR, &tmp_dst_x, &tmp_src_xxxx,
7461 &tmp_src_wwww);
7462 }
7463
7464 begin_emit_instruction(emit);
7465 emit_discard_opcode(emit, true); /* discard if src0.x is non-zero */
7466 emit_src_register(emit, &tmp_src_xxxx);
7467 end_emit_instruction(emit);
7468
7469 free_temp_indexes(emit);
7470
7471 return true;
7472 }
7473
7474
7475 /**
7476 * Emit code for the unconditional discard instruction.
7477 */
7478 static bool
7479 emit_discard(struct svga_shader_emitter_v10 *emit,
7480 const struct tgsi_full_instruction *inst)
7481 {
7482 struct tgsi_full_src_register zero = make_immediate_reg_float(emit, 0.0f);
7483
7484 /* DISCARD if 0.0 is zero */
7485 begin_emit_instruction(emit);
7486 emit_discard_opcode(emit, false);
7487 emit_src_register(emit, &zero);
7488 end_emit_instruction(emit);
7489
7490 return true;
7491 }
7492
7493
7494 /**
7495 * Emit code for TGSI_OPCODE_LG2 instruction.
7496 */
7497 static bool
7498 emit_lg2(struct svga_shader_emitter_v10 *emit,
7499 const struct tgsi_full_instruction *inst)
7500 {
7501 /* Note that TGSI_OPCODE_LG2 computes only one value from src.x
7502 * while VGPU10 computes four values.
7503 *
7504 * dst = LG2(src):
7505 * dst.xyzw = log2(src.x)
7506 */
7507
7508 struct tgsi_full_src_register src_xxxx =
7509 swizzle_src(&inst->Src[0], TGSI_SWIZZLE_X, TGSI_SWIZZLE_X,
7510 TGSI_SWIZZLE_X, TGSI_SWIZZLE_X);
7511
7512 /* LOG tmp, s0.xxxx */
7513 emit_instruction_opn(emit, VGPU10_OPCODE_LOG,
7514 &inst->Dst[0], &src_xxxx, NULL, NULL,
7515 inst->Instruction.Saturate,
7516 inst->Instruction.Precise);
7517
7518 return true;
7519 }
7520
7521
7522 /**
7523 * Emit code for TGSI_OPCODE_LIT instruction.
7524 */
7525 static bool
7526 emit_lit(struct svga_shader_emitter_v10 *emit,
7527 const struct tgsi_full_instruction *inst)
7528 {
7529 struct tgsi_full_src_register one = make_immediate_reg_float(emit, 1.0f);
7530
7531 /*
7532 * If dst and src are the same we need to create
7533 * a temporary for it and insert a extra move.
7534 */
7535 unsigned tmp_move = get_temp_index(emit);
7536 struct tgsi_full_src_register move_src = make_src_temp_reg(tmp_move);
7537 struct tgsi_full_dst_register move_dst = make_dst_temp_reg(tmp_move);
7538
7539 /*
7540 * dst.x = 1
7541 * dst.y = max(src.x, 0)
7542 * dst.z = (src.x > 0) ? max(src.y, 0)^{clamp(src.w, -128, 128))} : 0
7543 * dst.w = 1
7544 */
7545
7546 /* MOV dst.x, 1.0 */
7547 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_X) {
7548 struct tgsi_full_dst_register dst_x =
7549 writemask_dst(&move_dst, TGSI_WRITEMASK_X);
7550 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst_x, &one);
7551 }
7552
7553 /* MOV dst.w, 1.0 */
7554 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_W) {
7555 struct tgsi_full_dst_register dst_w =
7556 writemask_dst(&move_dst, TGSI_WRITEMASK_W);
7557 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst_w, &one);
7558 }
7559
7560 /* MAX dst.y, src.x, 0.0 */
7561 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Y) {
7562 struct tgsi_full_dst_register dst_y =
7563 writemask_dst(&move_dst, TGSI_WRITEMASK_Y);
7564 struct tgsi_full_src_register zero =
7565 make_immediate_reg_float(emit, 0.0f);
7566 struct tgsi_full_src_register src_xxxx =
7567 swizzle_src(&inst->Src[0], TGSI_SWIZZLE_X, TGSI_SWIZZLE_X,
7568 TGSI_SWIZZLE_X, TGSI_SWIZZLE_X);
7569
7570 emit_instruction_opn(emit, VGPU10_OPCODE_MAX, &dst_y, &src_xxxx,
7571 &zero, NULL, inst->Instruction.Saturate, false);
7572 }
7573
7574 /*
7575 * tmp1 = clamp(src.w, -128, 128);
7576 * MAX tmp1, src.w, -128
7577 * MIN tmp1, tmp1, 128
7578 *
7579 * tmp2 = max(tmp2, 0);
7580 * MAX tmp2, src.y, 0
7581 *
7582 * tmp1 = pow(tmp2, tmp1);
7583 * LOG tmp2, tmp2
7584 * MUL tmp1, tmp2, tmp1
7585 * EXP tmp1, tmp1
7586 *
7587 * tmp1 = (src.w == 0) ? 1 : tmp1;
7588 * EQ tmp2, 0, src.w
7589 * MOVC tmp1, tmp2, 1.0, tmp1
7590 *
7591 * dst.z = (0 < src.x) ? tmp1 : 0;
7592 * LT tmp2, 0, src.x
7593 * MOVC dst.z, tmp2, tmp1, 0.0
7594 */
7595 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Z) {
7596 struct tgsi_full_dst_register dst_z =
7597 writemask_dst(&move_dst, TGSI_WRITEMASK_Z);
7598
7599 unsigned tmp1 = get_temp_index(emit);
7600 struct tgsi_full_src_register tmp1_src = make_src_temp_reg(tmp1);
7601 struct tgsi_full_dst_register tmp1_dst = make_dst_temp_reg(tmp1);
7602 unsigned tmp2 = get_temp_index(emit);
7603 struct tgsi_full_src_register tmp2_src = make_src_temp_reg(tmp2);
7604 struct tgsi_full_dst_register tmp2_dst = make_dst_temp_reg(tmp2);
7605
7606 struct tgsi_full_src_register src_xxxx =
7607 scalar_src(&inst->Src[0], TGSI_SWIZZLE_X);
7608 struct tgsi_full_src_register src_yyyy =
7609 scalar_src(&inst->Src[0], TGSI_SWIZZLE_Y);
7610 struct tgsi_full_src_register src_wwww =
7611 scalar_src(&inst->Src[0], TGSI_SWIZZLE_W);
7612
7613 struct tgsi_full_src_register zero =
7614 make_immediate_reg_float(emit, 0.0f);
7615 struct tgsi_full_src_register lowerbound =
7616 make_immediate_reg_float(emit, -128.0f);
7617 struct tgsi_full_src_register upperbound =
7618 make_immediate_reg_float(emit, 128.0f);
7619
7620 emit_instruction_op2(emit, VGPU10_OPCODE_MAX, &tmp1_dst, &src_wwww,
7621 &lowerbound);
7622 emit_instruction_op2(emit, VGPU10_OPCODE_MIN, &tmp1_dst, &tmp1_src,
7623 &upperbound);
7624 emit_instruction_op2(emit, VGPU10_OPCODE_MAX, &tmp2_dst, &src_yyyy,
7625 &zero);
7626
7627 /* POW tmp1, tmp2, tmp1 */
7628 /* LOG tmp2, tmp2 */
7629 emit_instruction_op1(emit, VGPU10_OPCODE_LOG, &tmp2_dst, &tmp2_src);
7630
7631 /* MUL tmp1, tmp2, tmp1 */
7632 emit_instruction_op2(emit, VGPU10_OPCODE_MUL, &tmp1_dst, &tmp2_src,
7633 &tmp1_src);
7634
7635 /* EXP tmp1, tmp1 */
7636 emit_instruction_op1(emit, VGPU10_OPCODE_EXP, &tmp1_dst, &tmp1_src);
7637
7638 /* EQ tmp2, 0, src.w */
7639 emit_instruction_op2(emit, VGPU10_OPCODE_EQ, &tmp2_dst, &zero, &src_wwww);
7640 /* MOVC tmp1.z, tmp2, tmp1, 1.0 */
7641 emit_instruction_op3(emit, VGPU10_OPCODE_MOVC, &tmp1_dst,
7642 &tmp2_src, &one, &tmp1_src);
7643
7644 /* LT tmp2, 0, src.x */
7645 emit_instruction_op2(emit, VGPU10_OPCODE_LT, &tmp2_dst, &zero, &src_xxxx);
7646 /* MOVC dst.z, tmp2, tmp1, 0.0 */
7647 emit_instruction_op3(emit, VGPU10_OPCODE_MOVC, &dst_z,
7648 &tmp2_src, &tmp1_src, &zero);
7649 }
7650
7651 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &inst->Dst[0], &move_src);
7652 free_temp_indexes(emit);
7653
7654 return true;
7655 }
7656
7657
7658 /**
7659 * Emit Level Of Detail Query (LODQ) instruction.
7660 */
7661 static bool
7662 emit_lodq(struct svga_shader_emitter_v10 *emit,
7663 const struct tgsi_full_instruction *inst)
7664 {
7665 const uint unit = inst->Src[1].Register.Index;
7666
7667 assert(emit->version >= 41);
7668
7669 /* LOD dst, coord, resource, sampler */
7670 begin_emit_instruction(emit);
7671 emit_opcode(emit, VGPU10_OPCODE_LOD, false);
7672 emit_dst_register(emit, &inst->Dst[0]);
7673 emit_src_register(emit, &inst->Src[0]); /* coord */
7674 emit_resource_register(emit, unit);
7675 emit_sampler_register(emit, unit);
7676 end_emit_instruction(emit);
7677
7678 return true;
7679 }
7680
7681
7682 /**
7683 * Emit code for TGSI_OPCODE_LOG instruction.
7684 */
7685 static bool
7686 emit_log(struct svga_shader_emitter_v10 *emit,
7687 const struct tgsi_full_instruction *inst)
7688 {
7689 /*
7690 * dst.x = floor(lg2(abs(s0.x)))
7691 * dst.y = abs(s0.x) / (2 ^ floor(lg2(abs(s0.x))))
7692 * dst.z = lg2(abs(s0.x))
7693 * dst.w = 1.0
7694 */
7695
7696 struct tgsi_full_src_register src_xxxx =
7697 scalar_src(&inst->Src[0], TGSI_SWIZZLE_X);
7698 unsigned tmp = get_temp_index(emit);
7699 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
7700 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
7701 struct tgsi_full_src_register abs_src_xxxx = absolute_src(&src_xxxx);
7702
7703 /* only use X component of temp reg */
7704 tmp_dst = writemask_dst(&tmp_dst, TGSI_WRITEMASK_X);
7705 tmp_src = scalar_src(&tmp_src, TGSI_SWIZZLE_X);
7706
7707 /* LOG tmp.x, abs(s0.x) */
7708 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_XYZ) {
7709 emit_instruction_op1(emit, VGPU10_OPCODE_LOG, &tmp_dst, &abs_src_xxxx);
7710 }
7711
7712 /* MOV dst.z, tmp.x */
7713 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Z) {
7714 struct tgsi_full_dst_register dst_z =
7715 writemask_dst(&inst->Dst[0], TGSI_WRITEMASK_Z);
7716
7717 emit_instruction_opn(emit, VGPU10_OPCODE_MOV,
7718 &dst_z, &tmp_src, NULL, NULL,
7719 inst->Instruction.Saturate, false);
7720 }
7721
7722 /* FLR tmp.x, tmp.x */
7723 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_XY) {
7724 emit_instruction_op1(emit, VGPU10_OPCODE_ROUND_NI, &tmp_dst, &tmp_src);
7725 }
7726
7727 /* MOV dst.x, tmp.x */
7728 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_X) {
7729 struct tgsi_full_dst_register dst_x =
7730 writemask_dst(&inst->Dst[0], TGSI_WRITEMASK_X);
7731
7732 emit_instruction_opn(emit, VGPU10_OPCODE_MOV,
7733 &dst_x, &tmp_src, NULL, NULL,
7734 inst->Instruction.Saturate, false);
7735 }
7736
7737 /* EXP tmp.x, tmp.x */
7738 /* DIV dst.y, abs(s0.x), tmp.x */
7739 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Y) {
7740 struct tgsi_full_dst_register dst_y =
7741 writemask_dst(&inst->Dst[0], TGSI_WRITEMASK_Y);
7742
7743 emit_instruction_op1(emit, VGPU10_OPCODE_EXP, &tmp_dst, &tmp_src);
7744 emit_instruction_opn(emit, VGPU10_OPCODE_DIV, &dst_y, &abs_src_xxxx,
7745 &tmp_src, NULL, inst->Instruction.Saturate, false);
7746 }
7747
7748 /* MOV dst.w, 1.0 */
7749 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_W) {
7750 struct tgsi_full_dst_register dst_w =
7751 writemask_dst(&inst->Dst[0], TGSI_WRITEMASK_W);
7752 struct tgsi_full_src_register one =
7753 make_immediate_reg_float(emit, 1.0f);
7754
7755 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst_w, &one);
7756 }
7757
7758 free_temp_indexes(emit);
7759
7760 return true;
7761 }
7762
7763
7764 /**
7765 * Emit code for TGSI_OPCODE_LRP instruction.
7766 */
7767 static bool
7768 emit_lrp(struct svga_shader_emitter_v10 *emit,
7769 const struct tgsi_full_instruction *inst)
7770 {
7771 /* dst = LRP(s0, s1, s2):
7772 * dst = s0 * (s1 - s2) + s2
7773 * Translates into:
7774 * SUB tmp, s1, s2; tmp = s1 - s2
7775 * MAD dst, s0, tmp, s2; dst = s0 * t1 + s2
7776 */
7777 unsigned tmp = get_temp_index(emit);
7778 struct tgsi_full_src_register src_tmp = make_src_temp_reg(tmp);
7779 struct tgsi_full_dst_register dst_tmp = make_dst_temp_reg(tmp);
7780 struct tgsi_full_src_register neg_src2 = negate_src(&inst->Src[2]);
7781
7782 /* ADD tmp, s1, -s2 */
7783 emit_instruction_opn(emit, VGPU10_OPCODE_ADD, &dst_tmp,
7784 &inst->Src[1], &neg_src2, NULL, false,
7785 inst->Instruction.Precise);
7786
7787 /* MAD dst, s1, tmp, s3 */
7788 emit_instruction_opn(emit, VGPU10_OPCODE_MAD, &inst->Dst[0],
7789 &inst->Src[0], &src_tmp, &inst->Src[2],
7790 inst->Instruction.Saturate,
7791 inst->Instruction.Precise);
7792
7793 free_temp_indexes(emit);
7794
7795 return true;
7796 }
7797
7798
7799 /**
7800 * Emit code for TGSI_OPCODE_POW instruction.
7801 */
7802 static bool
7803 emit_pow(struct svga_shader_emitter_v10 *emit,
7804 const struct tgsi_full_instruction *inst)
7805 {
7806 /* Note that TGSI_OPCODE_POW computes only one value from src0.x and
7807 * src1.x while VGPU10 computes four values.
7808 *
7809 * dst = POW(src0, src1):
7810 * dst.xyzw = src0.x ^ src1.x
7811 */
7812 unsigned tmp = get_temp_index(emit);
7813 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
7814 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
7815 struct tgsi_full_src_register src0_xxxx =
7816 swizzle_src(&inst->Src[0], TGSI_SWIZZLE_X, TGSI_SWIZZLE_X,
7817 TGSI_SWIZZLE_X, TGSI_SWIZZLE_X);
7818 struct tgsi_full_src_register src1_xxxx =
7819 swizzle_src(&inst->Src[1], TGSI_SWIZZLE_X, TGSI_SWIZZLE_X,
7820 TGSI_SWIZZLE_X, TGSI_SWIZZLE_X);
7821
7822 /* LOG tmp, s0.xxxx */
7823 emit_instruction_opn(emit, VGPU10_OPCODE_LOG,
7824 &tmp_dst, &src0_xxxx, NULL, NULL,
7825 false, inst->Instruction.Precise);
7826
7827 /* MUL tmp, tmp, s1.xxxx */
7828 emit_instruction_opn(emit, VGPU10_OPCODE_MUL,
7829 &tmp_dst, &tmp_src, &src1_xxxx, NULL,
7830 false, inst->Instruction.Precise);
7831
7832 /* EXP tmp, s0.xxxx */
7833 emit_instruction_opn(emit, VGPU10_OPCODE_EXP,
7834 &inst->Dst[0], &tmp_src, NULL, NULL,
7835 inst->Instruction.Saturate,
7836 inst->Instruction.Precise);
7837
7838 /* free tmp */
7839 free_temp_indexes(emit);
7840
7841 return true;
7842 }
7843
7844
7845 /**
7846 * Emit code for TGSI_OPCODE_RCP (reciprocal) instruction.
7847 */
7848 static bool
7849 emit_rcp(struct svga_shader_emitter_v10 *emit,
7850 const struct tgsi_full_instruction *inst)
7851 {
7852 if (emit->version >= 50) {
7853 /* use new RCP instruction. But VGPU10_OPCODE_RCP is component-wise
7854 * while TGSI_OPCODE_RCP computes dst.xyzw = 1.0 / src.xxxx so we need
7855 * to manipulate the src register's swizzle.
7856 */
7857 struct tgsi_full_src_register src = inst->Src[0];
7858 src.Register.SwizzleY =
7859 src.Register.SwizzleZ =
7860 src.Register.SwizzleW = src.Register.SwizzleX;
7861
7862 begin_emit_instruction(emit);
7863 emit_opcode_precise(emit, VGPU10_OPCODE_RCP,
7864 inst->Instruction.Saturate,
7865 inst->Instruction.Precise);
7866 emit_dst_register(emit, &inst->Dst[0]);
7867 emit_src_register(emit, &src);
7868 end_emit_instruction(emit);
7869 }
7870 else {
7871 struct tgsi_full_src_register one = make_immediate_reg_float(emit, 1.0f);
7872
7873 unsigned tmp = get_temp_index(emit);
7874 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
7875 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
7876
7877 struct tgsi_full_dst_register tmp_dst_x =
7878 writemask_dst(&tmp_dst, TGSI_WRITEMASK_X);
7879 struct tgsi_full_src_register tmp_src_xxxx =
7880 scalar_src(&tmp_src, TGSI_SWIZZLE_X);
7881
7882 /* DIV tmp.x, 1.0, s0 */
7883 emit_instruction_opn(emit, VGPU10_OPCODE_DIV,
7884 &tmp_dst_x, &one, &inst->Src[0], NULL,
7885 false, inst->Instruction.Precise);
7886
7887 /* MOV dst, tmp.xxxx */
7888 emit_instruction_opn(emit, VGPU10_OPCODE_MOV,
7889 &inst->Dst[0], &tmp_src_xxxx, NULL, NULL,
7890 inst->Instruction.Saturate,
7891 inst->Instruction.Precise);
7892
7893 free_temp_indexes(emit);
7894 }
7895
7896 return true;
7897 }
7898
7899
7900 /**
7901 * Emit code for TGSI_OPCODE_RSQ instruction.
7902 */
7903 static bool
7904 emit_rsq(struct svga_shader_emitter_v10 *emit,
7905 const struct tgsi_full_instruction *inst)
7906 {
7907 /* dst = RSQ(src):
7908 * dst.xyzw = 1 / sqrt(src.x)
7909 * Translates into:
7910 * RSQ tmp, src.x
7911 * MOV dst, tmp.xxxx
7912 */
7913
7914 unsigned tmp = get_temp_index(emit);
7915 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
7916 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
7917
7918 struct tgsi_full_dst_register tmp_dst_x =
7919 writemask_dst(&tmp_dst, TGSI_WRITEMASK_X);
7920 struct tgsi_full_src_register tmp_src_xxxx =
7921 scalar_src(&tmp_src, TGSI_SWIZZLE_X);
7922
7923 /* RSQ tmp, src.x */
7924 emit_instruction_opn(emit, VGPU10_OPCODE_RSQ,
7925 &tmp_dst_x, &inst->Src[0], NULL, NULL,
7926 false, inst->Instruction.Precise);
7927
7928 /* MOV dst, tmp.xxxx */
7929 emit_instruction_opn(emit, VGPU10_OPCODE_MOV,
7930 &inst->Dst[0], &tmp_src_xxxx, NULL, NULL,
7931 inst->Instruction.Saturate,
7932 inst->Instruction.Precise);
7933
7934 /* free tmp */
7935 free_temp_indexes(emit);
7936
7937 return true;
7938 }
7939
7940
7941 /**
7942 * Emit code for TGSI_OPCODE_SEQ (Set Equal) instruction.
7943 */
7944 static bool
7945 emit_seq(struct svga_shader_emitter_v10 *emit,
7946 const struct tgsi_full_instruction *inst)
7947 {
7948 /* dst = SEQ(s0, s1):
7949 * dst = s0 == s1 ? 1.0 : 0.0 (per component)
7950 * Translates into:
7951 * EQ tmp, s0, s1; tmp = s0 == s1 : 0xffffffff : 0 (per comp)
7952 * MOVC dst, tmp, 1.0, 0.0; dst = tmp ? 1.0 : 0.0 (per component)
7953 */
7954 unsigned tmp = get_temp_index(emit);
7955 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
7956 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
7957 struct tgsi_full_src_register zero = make_immediate_reg_float(emit, 0.0f);
7958 struct tgsi_full_src_register one = make_immediate_reg_float(emit, 1.0f);
7959
7960 /* EQ tmp, s0, s1 */
7961 emit_instruction_op2(emit, VGPU10_OPCODE_EQ, &tmp_dst, &inst->Src[0],
7962 &inst->Src[1]);
7963
7964 /* MOVC dst, tmp, one, zero */
7965 emit_instruction_op3(emit, VGPU10_OPCODE_MOVC, &inst->Dst[0], &tmp_src,
7966 &one, &zero);
7967
7968 free_temp_indexes(emit);
7969
7970 return true;
7971 }
7972
7973
7974 /**
7975 * Emit code for TGSI_OPCODE_SGE (Set Greater than or Equal) instruction.
7976 */
7977 static bool
7978 emit_sge(struct svga_shader_emitter_v10 *emit,
7979 const struct tgsi_full_instruction *inst)
7980 {
7981 /* dst = SGE(s0, s1):
7982 * dst = s0 >= s1 ? 1.0 : 0.0 (per component)
7983 * Translates into:
7984 * GE tmp, s0, s1; tmp = s0 >= s1 : 0xffffffff : 0 (per comp)
7985 * MOVC dst, tmp, 1.0, 0.0; dst = tmp ? 1.0 : 0.0 (per component)
7986 */
7987 unsigned tmp = get_temp_index(emit);
7988 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
7989 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
7990 struct tgsi_full_src_register zero = make_immediate_reg_float(emit, 0.0f);
7991 struct tgsi_full_src_register one = make_immediate_reg_float(emit, 1.0f);
7992
7993 /* GE tmp, s0, s1 */
7994 emit_instruction_op2(emit, VGPU10_OPCODE_GE, &tmp_dst, &inst->Src[0],
7995 &inst->Src[1]);
7996
7997 /* MOVC dst, tmp, one, zero */
7998 emit_instruction_op3(emit, VGPU10_OPCODE_MOVC, &inst->Dst[0], &tmp_src,
7999 &one, &zero);
8000
8001 free_temp_indexes(emit);
8002
8003 return true;
8004 }
8005
8006
8007 /**
8008 * Emit code for TGSI_OPCODE_SGT (Set Greater than) instruction.
8009 */
8010 static bool
8011 emit_sgt(struct svga_shader_emitter_v10 *emit,
8012 const struct tgsi_full_instruction *inst)
8013 {
8014 /* dst = SGT(s0, s1):
8015 * dst = s0 > s1 ? 1.0 : 0.0 (per component)
8016 * Translates into:
8017 * LT tmp, s1, s0; tmp = s1 < s0 ? 0xffffffff : 0 (per comp)
8018 * MOVC dst, tmp, 1.0, 0.0; dst = tmp ? 1.0 : 0.0 (per component)
8019 */
8020 unsigned tmp = get_temp_index(emit);
8021 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
8022 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
8023 struct tgsi_full_src_register zero = make_immediate_reg_float(emit, 0.0f);
8024 struct tgsi_full_src_register one = make_immediate_reg_float(emit, 1.0f);
8025
8026 /* LT tmp, s1, s0 */
8027 emit_instruction_op2(emit, VGPU10_OPCODE_LT, &tmp_dst, &inst->Src[1],
8028 &inst->Src[0]);
8029
8030 /* MOVC dst, tmp, one, zero */
8031 emit_instruction_op3(emit, VGPU10_OPCODE_MOVC, &inst->Dst[0], &tmp_src,
8032 &one, &zero);
8033
8034 free_temp_indexes(emit);
8035
8036 return true;
8037 }
8038
8039
8040 /**
8041 * Emit code for TGSI_OPCODE_SIN and TGSI_OPCODE_COS instructions.
8042 */
8043 static bool
8044 emit_sincos(struct svga_shader_emitter_v10 *emit,
8045 const struct tgsi_full_instruction *inst)
8046 {
8047 unsigned tmp = get_temp_index(emit);
8048 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
8049 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
8050
8051 struct tgsi_full_src_register tmp_src_xxxx =
8052 scalar_src(&tmp_src, TGSI_SWIZZLE_X);
8053 struct tgsi_full_dst_register tmp_dst_x =
8054 writemask_dst(&tmp_dst, TGSI_WRITEMASK_X);
8055
8056 begin_emit_instruction(emit);
8057 emit_opcode(emit, VGPU10_OPCODE_SINCOS, false);
8058
8059 if (inst->Instruction.Opcode == TGSI_OPCODE_SIN) {
8060 emit_dst_register(emit, &tmp_dst_x); /* first destination register */
8061 emit_null_dst_register(emit); /* second destination register */
8062 } else {
8063 emit_null_dst_register(emit);
8064 emit_dst_register(emit, &tmp_dst_x);
8065 }
8066
8067 emit_src_register(emit, &inst->Src[0]);
8068 end_emit_instruction(emit);
8069
8070 emit_instruction_opn(emit, VGPU10_OPCODE_MOV,
8071 &inst->Dst[0], &tmp_src_xxxx, NULL, NULL,
8072 inst->Instruction.Saturate,
8073 inst->Instruction.Precise);
8074
8075 free_temp_indexes(emit);
8076
8077 return true;
8078 }
8079
8080
8081 /**
8082 * Emit code for TGSI_OPCODE_SLE (Set Less than or Equal) instruction.
8083 */
8084 static bool
8085 emit_sle(struct svga_shader_emitter_v10 *emit,
8086 const struct tgsi_full_instruction *inst)
8087 {
8088 /* dst = SLE(s0, s1):
8089 * dst = s0 <= s1 ? 1.0 : 0.0 (per component)
8090 * Translates into:
8091 * GE tmp, s1, s0; tmp = s1 >= s0 : 0xffffffff : 0 (per comp)
8092 * MOVC dst, tmp, 1.0, 0.0; dst = tmp ? 1.0 : 0.0 (per component)
8093 */
8094 unsigned tmp = get_temp_index(emit);
8095 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
8096 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
8097 struct tgsi_full_src_register zero = make_immediate_reg_float(emit, 0.0f);
8098 struct tgsi_full_src_register one = make_immediate_reg_float(emit, 1.0f);
8099
8100 /* GE tmp, s1, s0 */
8101 emit_instruction_op2(emit, VGPU10_OPCODE_GE, &tmp_dst, &inst->Src[1],
8102 &inst->Src[0]);
8103
8104 /* MOVC dst, tmp, one, zero */
8105 emit_instruction_op3(emit, VGPU10_OPCODE_MOVC, &inst->Dst[0], &tmp_src,
8106 &one, &zero);
8107
8108 free_temp_indexes(emit);
8109
8110 return true;
8111 }
8112
8113
8114 /**
8115 * Emit code for TGSI_OPCODE_SLT (Set Less than) instruction.
8116 */
8117 static bool
8118 emit_slt(struct svga_shader_emitter_v10 *emit,
8119 const struct tgsi_full_instruction *inst)
8120 {
8121 /* dst = SLT(s0, s1):
8122 * dst = s0 < s1 ? 1.0 : 0.0 (per component)
8123 * Translates into:
8124 * LT tmp, s0, s1; tmp = s0 < s1 ? 0xffffffff : 0 (per comp)
8125 * MOVC dst, tmp, 1.0, 0.0; dst = tmp ? 1.0 : 0.0 (per component)
8126 */
8127 unsigned tmp = get_temp_index(emit);
8128 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
8129 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
8130 struct tgsi_full_src_register zero = make_immediate_reg_float(emit, 0.0f);
8131 struct tgsi_full_src_register one = make_immediate_reg_float(emit, 1.0f);
8132
8133 /* LT tmp, s0, s1 */
8134 emit_instruction_op2(emit, VGPU10_OPCODE_LT, &tmp_dst, &inst->Src[0],
8135 &inst->Src[1]);
8136
8137 /* MOVC dst, tmp, one, zero */
8138 emit_instruction_op3(emit, VGPU10_OPCODE_MOVC, &inst->Dst[0], &tmp_src,
8139 &one, &zero);
8140
8141 free_temp_indexes(emit);
8142
8143 return true;
8144 }
8145
8146
8147 /**
8148 * Emit code for TGSI_OPCODE_SNE (Set Not Equal) instruction.
8149 */
8150 static bool
8151 emit_sne(struct svga_shader_emitter_v10 *emit,
8152 const struct tgsi_full_instruction *inst)
8153 {
8154 /* dst = SNE(s0, s1):
8155 * dst = s0 != s1 ? 1.0 : 0.0 (per component)
8156 * Translates into:
8157 * EQ tmp, s0, s1; tmp = s0 == s1 : 0xffffffff : 0 (per comp)
8158 * MOVC dst, tmp, 1.0, 0.0; dst = tmp ? 1.0 : 0.0 (per component)
8159 */
8160 unsigned tmp = get_temp_index(emit);
8161 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
8162 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
8163 struct tgsi_full_src_register zero = make_immediate_reg_float(emit, 0.0f);
8164 struct tgsi_full_src_register one = make_immediate_reg_float(emit, 1.0f);
8165
8166 /* NE tmp, s0, s1 */
8167 emit_instruction_op2(emit, VGPU10_OPCODE_NE, &tmp_dst, &inst->Src[0],
8168 &inst->Src[1]);
8169
8170 /* MOVC dst, tmp, one, zero */
8171 emit_instruction_op3(emit, VGPU10_OPCODE_MOVC, &inst->Dst[0], &tmp_src,
8172 &one, &zero);
8173
8174 free_temp_indexes(emit);
8175
8176 return true;
8177 }
8178
8179
8180 /**
8181 * Emit code for TGSI_OPCODE_SSG (Set Sign) instruction.
8182 */
8183 static bool
8184 emit_ssg(struct svga_shader_emitter_v10 *emit,
8185 const struct tgsi_full_instruction *inst)
8186 {
8187 /* dst.x = (src.x > 0.0) ? 1.0 : (src.x < 0.0) ? -1.0 : 0.0
8188 * dst.y = (src.y > 0.0) ? 1.0 : (src.y < 0.0) ? -1.0 : 0.0
8189 * dst.z = (src.z > 0.0) ? 1.0 : (src.z < 0.0) ? -1.0 : 0.0
8190 * dst.w = (src.w > 0.0) ? 1.0 : (src.w < 0.0) ? -1.0 : 0.0
8191 * Translates into:
8192 * LT tmp1, src, zero; tmp1 = src < zero ? 0xffffffff : 0 (per comp)
8193 * MOVC tmp2, tmp1, -1.0, 0.0; tmp2 = tmp1 ? -1.0 : 0.0 (per component)
8194 * LT tmp1, zero, src; tmp1 = zero < src ? 0xffffffff : 0 (per comp)
8195 * MOVC dst, tmp1, 1.0, tmp2; dst = tmp1 ? 1.0 : tmp2 (per component)
8196 */
8197 struct tgsi_full_src_register zero =
8198 make_immediate_reg_float(emit, 0.0f);
8199 struct tgsi_full_src_register one =
8200 make_immediate_reg_float(emit, 1.0f);
8201 struct tgsi_full_src_register neg_one =
8202 make_immediate_reg_float(emit, -1.0f);
8203
8204 unsigned tmp1 = get_temp_index(emit);
8205 struct tgsi_full_src_register tmp1_src = make_src_temp_reg(tmp1);
8206 struct tgsi_full_dst_register tmp1_dst = make_dst_temp_reg(tmp1);
8207
8208 unsigned tmp2 = get_temp_index(emit);
8209 struct tgsi_full_src_register tmp2_src = make_src_temp_reg(tmp2);
8210 struct tgsi_full_dst_register tmp2_dst = make_dst_temp_reg(tmp2);
8211
8212 emit_instruction_op2(emit, VGPU10_OPCODE_LT, &tmp1_dst, &inst->Src[0],
8213 &zero);
8214 emit_instruction_op3(emit, VGPU10_OPCODE_MOVC, &tmp2_dst, &tmp1_src,
8215 &neg_one, &zero);
8216 emit_instruction_op2(emit, VGPU10_OPCODE_LT, &tmp1_dst, &zero,
8217 &inst->Src[0]);
8218 emit_instruction_op3(emit, VGPU10_OPCODE_MOVC, &inst->Dst[0], &tmp1_src,
8219 &one, &tmp2_src);
8220
8221 free_temp_indexes(emit);
8222
8223 return true;
8224 }
8225
8226
8227 /**
8228 * Emit code for TGSI_OPCODE_ISSG (Integer Set Sign) instruction.
8229 */
8230 static bool
8231 emit_issg(struct svga_shader_emitter_v10 *emit,
8232 const struct tgsi_full_instruction *inst)
8233 {
8234 /* dst.x = (src.x > 0) ? 1 : (src.x < 0) ? -1 : 0
8235 * dst.y = (src.y > 0) ? 1 : (src.y < 0) ? -1 : 0
8236 * dst.z = (src.z > 0) ? 1 : (src.z < 0) ? -1 : 0
8237 * dst.w = (src.w > 0) ? 1 : (src.w < 0) ? -1 : 0
8238 * Translates into:
8239 * ILT tmp1, src, 0 tmp1 = src < 0 ? -1 : 0 (per component)
8240 * ILT tmp2, 0, src tmp2 = 0 < src ? -1 : 0 (per component)
8241 * IADD dst, tmp1, neg(tmp2) dst = tmp1 - tmp2 (per component)
8242 */
8243 struct tgsi_full_src_register zero = make_immediate_reg_float(emit, 0.0f);
8244
8245 unsigned tmp1 = get_temp_index(emit);
8246 struct tgsi_full_src_register tmp1_src = make_src_temp_reg(tmp1);
8247 struct tgsi_full_dst_register tmp1_dst = make_dst_temp_reg(tmp1);
8248
8249 unsigned tmp2 = get_temp_index(emit);
8250 struct tgsi_full_src_register tmp2_src = make_src_temp_reg(tmp2);
8251 struct tgsi_full_dst_register tmp2_dst = make_dst_temp_reg(tmp2);
8252
8253 struct tgsi_full_src_register neg_tmp2 = negate_src(&tmp2_src);
8254
8255 emit_instruction_op2(emit, VGPU10_OPCODE_ILT, &tmp1_dst,
8256 &inst->Src[0], &zero);
8257 emit_instruction_op2(emit, VGPU10_OPCODE_ILT, &tmp2_dst,
8258 &zero, &inst->Src[0]);
8259 emit_instruction_op2(emit, VGPU10_OPCODE_IADD, &inst->Dst[0],
8260 &tmp1_src, &neg_tmp2);
8261
8262 free_temp_indexes(emit);
8263
8264 return true;
8265 }
8266
8267
8268 /**
8269 * Emit a comparison instruction. The dest register will get
8270 * 0 or ~0 values depending on the outcome of comparing src0 to src1.
8271 */
8272 static void
8273 emit_comparison(struct svga_shader_emitter_v10 *emit,
8274 SVGA3dCmpFunc func,
8275 const struct tgsi_full_dst_register *dst,
8276 const struct tgsi_full_src_register *src0,
8277 const struct tgsi_full_src_register *src1)
8278 {
8279 struct tgsi_full_src_register immediate;
8280 VGPU10OpcodeToken0 opcode0;
8281 bool swapSrc = false;
8282
8283 /* Sanity checks for svga vs. gallium enums */
8284 STATIC_ASSERT(SVGA3D_CMP_LESS == (PIPE_FUNC_LESS + 1));
8285 STATIC_ASSERT(SVGA3D_CMP_GREATEREQUAL == (PIPE_FUNC_GEQUAL + 1));
8286
8287 opcode0.value = 0;
8288
8289 switch (func) {
8290 case SVGA3D_CMP_NEVER:
8291 immediate = make_immediate_reg_int(emit, 0);
8292 /* MOV dst, {0} */
8293 begin_emit_instruction(emit);
8294 emit_dword(emit, VGPU10_OPCODE_MOV);
8295 emit_dst_register(emit, dst);
8296 emit_src_register(emit, &immediate);
8297 end_emit_instruction(emit);
8298 return;
8299 case SVGA3D_CMP_ALWAYS:
8300 immediate = make_immediate_reg_int(emit, -1);
8301 /* MOV dst, {-1} */
8302 begin_emit_instruction(emit);
8303 emit_dword(emit, VGPU10_OPCODE_MOV);
8304 emit_dst_register(emit, dst);
8305 emit_src_register(emit, &immediate);
8306 end_emit_instruction(emit);
8307 return;
8308 case SVGA3D_CMP_LESS:
8309 opcode0.opcodeType = VGPU10_OPCODE_LT;
8310 break;
8311 case SVGA3D_CMP_EQUAL:
8312 opcode0.opcodeType = VGPU10_OPCODE_EQ;
8313 break;
8314 case SVGA3D_CMP_LESSEQUAL:
8315 opcode0.opcodeType = VGPU10_OPCODE_GE;
8316 swapSrc = true;
8317 break;
8318 case SVGA3D_CMP_GREATER:
8319 opcode0.opcodeType = VGPU10_OPCODE_LT;
8320 swapSrc = true;
8321 break;
8322 case SVGA3D_CMP_NOTEQUAL:
8323 opcode0.opcodeType = VGPU10_OPCODE_NE;
8324 break;
8325 case SVGA3D_CMP_GREATEREQUAL:
8326 opcode0.opcodeType = VGPU10_OPCODE_GE;
8327 break;
8328 default:
8329 assert(!"Unexpected comparison mode");
8330 opcode0.opcodeType = VGPU10_OPCODE_EQ;
8331 }
8332
8333 begin_emit_instruction(emit);
8334 emit_dword(emit, opcode0.value);
8335 emit_dst_register(emit, dst);
8336 if (swapSrc) {
8337 emit_src_register(emit, src1);
8338 emit_src_register(emit, src0);
8339 } else {
8340 emit_src_register(emit, src0);
8341 emit_src_register(emit, src1);
8342 }
8343 end_emit_instruction(emit);
8344 }
8345
8346
8347 /**
8348 * Get texel/address offsets for a texture instruction.
8349 */
8350 static void
8351 get_texel_offsets(const struct svga_shader_emitter_v10 *emit,
8352 const struct tgsi_full_instruction *inst, int offsets[3])
8353 {
8354 if (inst->Texture.NumOffsets == 1) {
8355 /* According to OpenGL Shader Language spec the offsets are only
8356 * fetched from a previously-declared immediate/literal.
8357 */
8358 const struct tgsi_texture_offset *off = inst->TexOffsets;
8359 const unsigned index = off[0].Index;
8360 const unsigned swizzleX = off[0].SwizzleX;
8361 const unsigned swizzleY = off[0].SwizzleY;
8362 const unsigned swizzleZ = off[0].SwizzleZ;
8363 const union tgsi_immediate_data *imm = emit->immediates[index];
8364
8365 assert(inst->TexOffsets[0].File == TGSI_FILE_IMMEDIATE);
8366
8367 offsets[0] = imm[swizzleX].Int;
8368 offsets[1] = imm[swizzleY].Int;
8369 offsets[2] = imm[swizzleZ].Int;
8370 }
8371 else {
8372 offsets[0] = offsets[1] = offsets[2] = 0;
8373 }
8374 }
8375
8376
8377 /**
8378 * Set up the coordinate register for texture sampling.
8379 * When we're sampling from a RECT texture we have to scale the
8380 * unnormalized coordinate to a normalized coordinate.
8381 * We do that by multiplying the coordinate by an "extra" constant.
8382 * An alternative would be to use the RESINFO instruction to query the
8383 * texture's size.
8384 */
8385 static struct tgsi_full_src_register
8386 setup_texcoord(struct svga_shader_emitter_v10 *emit,
8387 unsigned unit,
8388 const struct tgsi_full_src_register *coord)
8389 {
8390 if (emit->key.tex[unit].sampler_view && emit->key.tex[unit].unnormalized) {
8391 unsigned scale_index = emit->texcoord_scale_index[unit];
8392 unsigned tmp = get_temp_index(emit);
8393 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
8394 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
8395 struct tgsi_full_src_register scale_src = make_src_const_reg(scale_index);
8396
8397 if (emit->key.tex[unit].texel_bias) {
8398 /* to fix texture coordinate rounding issue, 0.0001 offset is
8399 * been added. This fixes piglit test fbo-blit-scaled-linear. */
8400 struct tgsi_full_src_register offset =
8401 make_immediate_reg_float(emit, 0.0001f);
8402
8403 /* ADD tmp, coord, offset */
8404 emit_instruction_op2(emit, VGPU10_OPCODE_ADD, &tmp_dst,
8405 coord, &offset);
8406 /* MUL tmp, tmp, scale */
8407 emit_instruction_op2(emit, VGPU10_OPCODE_MUL, &tmp_dst,
8408 &tmp_src, &scale_src);
8409 }
8410 else {
8411 /* MUL tmp, coord, const[] */
8412 emit_instruction_op2(emit, VGPU10_OPCODE_MUL, &tmp_dst,
8413 coord, &scale_src);
8414 }
8415 return tmp_src;
8416 }
8417 else {
8418 /* use texcoord as-is */
8419 return *coord;
8420 }
8421 }
8422
8423
8424 /**
8425 * For SAMPLE_C instructions, emit the extra src register which indicates
8426 * the reference/comparision value.
8427 */
8428 static void
8429 emit_tex_compare_refcoord(struct svga_shader_emitter_v10 *emit,
8430 enum tgsi_texture_type target,
8431 const struct tgsi_full_src_register *coord)
8432 {
8433 struct tgsi_full_src_register coord_src_ref;
8434 int component;
8435
8436 assert(tgsi_is_shadow_target(target));
8437
8438 component = tgsi_util_get_shadow_ref_src_index(target) % 4;
8439 assert(component >= 0);
8440
8441 coord_src_ref = scalar_src(coord, component);
8442
8443 emit_src_register(emit, &coord_src_ref);
8444 }
8445
8446
8447 /**
8448 * Info for implementing texture swizzles.
8449 * The begin_tex_swizzle(), get_tex_swizzle_dst() and end_tex_swizzle()
8450 * functions use this to encapsulate the extra steps needed to perform
8451 * a texture swizzle, or shadow/depth comparisons.
8452 * The shadow/depth comparison is only done here if for the cases where
8453 * there's no VGPU10 opcode (like texture bias lookup w/ shadow compare).
8454 */
8455 struct tex_swizzle_info
8456 {
8457 bool swizzled;
8458 bool shadow_compare;
8459 unsigned unit;
8460 enum tgsi_texture_type texture_target; /**< TGSI_TEXTURE_x */
8461 struct tgsi_full_src_register tmp_src;
8462 struct tgsi_full_dst_register tmp_dst;
8463 const struct tgsi_full_dst_register *inst_dst;
8464 const struct tgsi_full_src_register *coord_src;
8465 };
8466
8467
8468 /**
8469 * Do setup for handling texture swizzles or shadow compares.
8470 * \param unit the texture unit
8471 * \param inst the TGSI texture instruction
8472 * \param shadow_compare do shadow/depth comparison?
8473 * \param swz returns the swizzle info
8474 */
8475 static void
8476 begin_tex_swizzle(struct svga_shader_emitter_v10 *emit,
8477 unsigned unit,
8478 const struct tgsi_full_instruction *inst,
8479 bool shadow_compare,
8480 struct tex_swizzle_info *swz)
8481 {
8482 swz->swizzled = (emit->key.tex[unit].swizzle_r != TGSI_SWIZZLE_X ||
8483 emit->key.tex[unit].swizzle_g != TGSI_SWIZZLE_Y ||
8484 emit->key.tex[unit].swizzle_b != TGSI_SWIZZLE_Z ||
8485 emit->key.tex[unit].swizzle_a != TGSI_SWIZZLE_W);
8486
8487 swz->shadow_compare = shadow_compare;
8488 swz->texture_target = inst->Texture.Texture;
8489
8490 if (swz->swizzled || shadow_compare) {
8491 /* Allocate temp register for the result of the SAMPLE instruction
8492 * and the source of the MOV/compare/swizzle instructions.
8493 */
8494 unsigned tmp = get_temp_index(emit);
8495 swz->tmp_src = make_src_temp_reg(tmp);
8496 swz->tmp_dst = make_dst_temp_reg(tmp);
8497
8498 swz->unit = unit;
8499 }
8500 swz->inst_dst = &inst->Dst[0];
8501 swz->coord_src = &inst->Src[0];
8502
8503 emit->shadow_compare_units |= shadow_compare << unit;
8504 }
8505
8506
8507 /**
8508 * Returns the register to put the SAMPLE instruction results into.
8509 * This will either be the original instruction dst reg (if no swizzle
8510 * and no shadow comparison) or a temporary reg if there is a swizzle.
8511 */
8512 static const struct tgsi_full_dst_register *
8513 get_tex_swizzle_dst(const struct tex_swizzle_info *swz)
8514 {
8515 return (swz->swizzled || swz->shadow_compare)
8516 ? &swz->tmp_dst : swz->inst_dst;
8517 }
8518
8519
8520 /**
8521 * This emits the MOV instruction that actually implements a texture swizzle
8522 * and/or shadow comparison.
8523 */
8524 static void
8525 end_tex_swizzle(struct svga_shader_emitter_v10 *emit,
8526 const struct tex_swizzle_info *swz)
8527 {
8528 if (swz->shadow_compare) {
8529 /* Emit extra instructions to compare the fetched texel value against
8530 * a texture coordinate component. The result of the comparison
8531 * is 0.0 or 1.0.
8532 */
8533 struct tgsi_full_src_register coord_src;
8534 struct tgsi_full_src_register texel_src =
8535 scalar_src(&swz->tmp_src, TGSI_SWIZZLE_X);
8536 struct tgsi_full_src_register one =
8537 make_immediate_reg_float(emit, 1.0f);
8538 /* convert gallium comparison func to SVGA comparison func */
8539 SVGA3dCmpFunc compare_func = emit->key.tex[swz->unit].compare_func + 1;
8540
8541 int component =
8542 tgsi_util_get_shadow_ref_src_index(swz->texture_target) % 4;
8543 assert(component >= 0);
8544 coord_src = scalar_src(swz->coord_src, component);
8545
8546 /* COMPARE tmp, coord, texel */
8547 emit_comparison(emit, compare_func,
8548 &swz->tmp_dst, &coord_src, &texel_src);
8549
8550 /* AND dest, tmp, {1.0} */
8551 begin_emit_instruction(emit);
8552 emit_opcode(emit, VGPU10_OPCODE_AND, false);
8553 if (swz->swizzled) {
8554 emit_dst_register(emit, &swz->tmp_dst);
8555 } else {
8556 emit_dst_register(emit, swz->inst_dst);
8557 }
8558 emit_src_register(emit, &swz->tmp_src);
8559 emit_src_register(emit, &one);
8560 end_emit_instruction(emit);
8561 }
8562
8563 if (swz->swizzled) {
8564 unsigned swz_r = emit->key.tex[swz->unit].swizzle_r;
8565 unsigned swz_g = emit->key.tex[swz->unit].swizzle_g;
8566 unsigned swz_b = emit->key.tex[swz->unit].swizzle_b;
8567 unsigned swz_a = emit->key.tex[swz->unit].swizzle_a;
8568 unsigned writemask_0 = 0, writemask_1 = 0;
8569 bool int_tex = is_integer_type(emit->sampler_return_type[swz->unit]);
8570
8571 /* Swizzle w/out zero/one terms */
8572 struct tgsi_full_src_register src_swizzled =
8573 swizzle_src(&swz->tmp_src,
8574 swz_r < PIPE_SWIZZLE_0 ? swz_r : PIPE_SWIZZLE_X,
8575 swz_g < PIPE_SWIZZLE_0 ? swz_g : PIPE_SWIZZLE_Y,
8576 swz_b < PIPE_SWIZZLE_0 ? swz_b : PIPE_SWIZZLE_Z,
8577 swz_a < PIPE_SWIZZLE_0 ? swz_a : PIPE_SWIZZLE_W);
8578
8579 /* MOV dst, color(tmp).<swizzle> */
8580 emit_instruction_op1(emit, VGPU10_OPCODE_MOV,
8581 swz->inst_dst, &src_swizzled);
8582
8583 /* handle swizzle zero terms */
8584 writemask_0 = (((swz_r == PIPE_SWIZZLE_0) << 0) |
8585 ((swz_g == PIPE_SWIZZLE_0) << 1) |
8586 ((swz_b == PIPE_SWIZZLE_0) << 2) |
8587 ((swz_a == PIPE_SWIZZLE_0) << 3));
8588 writemask_0 &= swz->inst_dst->Register.WriteMask;
8589
8590 if (writemask_0) {
8591 struct tgsi_full_src_register zero = int_tex ?
8592 make_immediate_reg_int(emit, 0) :
8593 make_immediate_reg_float(emit, 0.0f);
8594 struct tgsi_full_dst_register dst =
8595 writemask_dst(swz->inst_dst, writemask_0);
8596
8597 /* MOV dst.writemask_0, {0,0,0,0} */
8598 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst, &zero);
8599 }
8600
8601 /* handle swizzle one terms */
8602 writemask_1 = (((swz_r == PIPE_SWIZZLE_1) << 0) |
8603 ((swz_g == PIPE_SWIZZLE_1) << 1) |
8604 ((swz_b == PIPE_SWIZZLE_1) << 2) |
8605 ((swz_a == PIPE_SWIZZLE_1) << 3));
8606 writemask_1 &= swz->inst_dst->Register.WriteMask;
8607
8608 if (writemask_1) {
8609 struct tgsi_full_src_register one = int_tex ?
8610 make_immediate_reg_int(emit, 1) :
8611 make_immediate_reg_float(emit, 1.0f);
8612 struct tgsi_full_dst_register dst =
8613 writemask_dst(swz->inst_dst, writemask_1);
8614
8615 /* MOV dst.writemask_1, {1,1,1,1} */
8616 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst, &one);
8617 }
8618 }
8619 }
8620
8621
8622 /**
8623 * Emit code for TGSI_OPCODE_SAMPLE instruction.
8624 */
8625 static bool
8626 emit_sample(struct svga_shader_emitter_v10 *emit,
8627 const struct tgsi_full_instruction *inst)
8628 {
8629 const unsigned resource_unit = inst->Src[1].Register.Index;
8630 const unsigned sampler_unit = inst->Src[2].Register.Index;
8631 struct tgsi_full_src_register coord;
8632 int offsets[3];
8633 struct tex_swizzle_info swz_info;
8634
8635 begin_tex_swizzle(emit, sampler_unit, inst, false, &swz_info);
8636
8637 get_texel_offsets(emit, inst, offsets);
8638
8639 coord = setup_texcoord(emit, resource_unit, &inst->Src[0]);
8640
8641 /* SAMPLE dst, coord(s0), resource, sampler */
8642 begin_emit_instruction(emit);
8643
8644 /* NOTE: for non-fragment shaders, we should use VGPU10_OPCODE_SAMPLE_L
8645 * with LOD=0. But our virtual GPU accepts this as-is.
8646 */
8647 emit_sample_opcode(emit, VGPU10_OPCODE_SAMPLE,
8648 inst->Instruction.Saturate, offsets);
8649 emit_dst_register(emit, get_tex_swizzle_dst(&swz_info));
8650 emit_src_register(emit, &coord);
8651 emit_resource_register(emit, resource_unit);
8652 emit_sampler_register(emit, sampler_unit);
8653 end_emit_instruction(emit);
8654
8655 end_tex_swizzle(emit, &swz_info);
8656
8657 free_temp_indexes(emit);
8658
8659 return true;
8660 }
8661
8662
8663 /**
8664 * Check if a texture instruction is valid.
8665 * An example of an invalid texture instruction is doing shadow comparison
8666 * with an integer-valued texture.
8667 * If we detect an invalid texture instruction, we replace it with:
8668 * MOV dst, {1,1,1,1};
8669 * \return TRUE if valid, FALSE if invalid.
8670 */
8671 static bool
8672 is_valid_tex_instruction(struct svga_shader_emitter_v10 *emit,
8673 const struct tgsi_full_instruction *inst)
8674 {
8675 const unsigned unit = inst->Src[1].Register.Index;
8676 const enum tgsi_texture_type target = inst->Texture.Texture;
8677 bool valid = true;
8678
8679 if (tgsi_is_shadow_target(target) &&
8680 is_integer_type(emit->sampler_return_type[unit])) {
8681 debug_printf("Invalid SAMPLE_C with an integer texture!\n");
8682 valid = false;
8683 }
8684 /* XXX might check for other conditions in the future here */
8685
8686 if (!valid) {
8687 /* emit a MOV dst, {1,1,1,1} instruction. */
8688 struct tgsi_full_src_register one = make_immediate_reg_float(emit, 1.0f);
8689 begin_emit_instruction(emit);
8690 emit_opcode(emit, VGPU10_OPCODE_MOV, false);
8691 emit_dst_register(emit, &inst->Dst[0]);
8692 emit_src_register(emit, &one);
8693 end_emit_instruction(emit);
8694 }
8695
8696 return valid;
8697 }
8698
8699
8700 /**
8701 * Emit code for TGSI_OPCODE_TEX (simple texture lookup)
8702 */
8703 static bool
8704 emit_tex(struct svga_shader_emitter_v10 *emit,
8705 const struct tgsi_full_instruction *inst)
8706 {
8707 const uint unit = inst->Src[1].Register.Index;
8708 const enum tgsi_texture_type target = inst->Texture.Texture;
8709 VGPU10_OPCODE_TYPE opcode;
8710 struct tgsi_full_src_register coord;
8711 int offsets[3];
8712 struct tex_swizzle_info swz_info;
8713 bool compare_in_shader;
8714
8715 /* check that the sampler returns a float */
8716 if (!is_valid_tex_instruction(emit, inst))
8717 return true;
8718
8719 compare_in_shader = tgsi_is_shadow_target(target) &&
8720 emit->key.tex[unit].compare_in_shader;
8721
8722 begin_tex_swizzle(emit, unit, inst, compare_in_shader, &swz_info);
8723
8724 get_texel_offsets(emit, inst, offsets);
8725
8726 coord = setup_texcoord(emit, unit, &inst->Src[0]);
8727
8728 /* SAMPLE dst, coord(s0), resource, sampler */
8729 begin_emit_instruction(emit);
8730
8731 if (tgsi_is_shadow_target(target) && !compare_in_shader)
8732 opcode = VGPU10_OPCODE_SAMPLE_C;
8733 else
8734 opcode = VGPU10_OPCODE_SAMPLE;
8735
8736 emit_sample_opcode(emit, opcode, inst->Instruction.Saturate, offsets);
8737 emit_dst_register(emit, get_tex_swizzle_dst(&swz_info));
8738 emit_src_register(emit, &coord);
8739 emit_resource_register(emit, unit);
8740 emit_sampler_register(emit, unit);
8741 if (opcode == VGPU10_OPCODE_SAMPLE_C) {
8742 emit_tex_compare_refcoord(emit, target, &coord);
8743 }
8744 end_emit_instruction(emit);
8745
8746 end_tex_swizzle(emit, &swz_info);
8747
8748 free_temp_indexes(emit);
8749
8750 return true;
8751 }
8752
8753 /**
8754 * Emit code for TGSI_OPCODE_TG4 (texture lookup for texture gather)
8755 */
8756 static bool
8757 emit_tg4(struct svga_shader_emitter_v10 *emit,
8758 const struct tgsi_full_instruction *inst)
8759 {
8760 const uint unit = inst->Src[2].Register.Index;
8761 struct tgsi_full_src_register src;
8762 struct tgsi_full_src_register offset_src, sampler, ref;
8763 int offsets[3];
8764
8765 /* check that the sampler returns a float */
8766 if (!is_valid_tex_instruction(emit, inst))
8767 return true;
8768
8769 if (emit->version >= 50) {
8770 unsigned target = inst->Texture.Texture;
8771 int index = inst->Src[1].Register.Index;
8772 const union tgsi_immediate_data *imm = emit->immediates[index];
8773 int select_comp = imm[inst->Src[1].Register.SwizzleX].Int;
8774 unsigned select_swizzle = PIPE_SWIZZLE_X;
8775
8776 if (!tgsi_is_shadow_target(target)) {
8777 switch (select_comp) {
8778 case 0:
8779 select_swizzle = emit->key.tex[unit].swizzle_r;
8780 break;
8781 case 1:
8782 select_swizzle = emit->key.tex[unit].swizzle_g;
8783 break;
8784 case 2:
8785 select_swizzle = emit->key.tex[unit].swizzle_b;
8786 break;
8787 case 3:
8788 select_swizzle = emit->key.tex[unit].swizzle_a;
8789 break;
8790 default:
8791 assert(!"Unexpected component in texture gather swizzle");
8792 }
8793 } else {
8794 select_swizzle = emit->key.tex[unit].swizzle_r;
8795 }
8796
8797 if (select_swizzle == PIPE_SWIZZLE_1) {
8798 src = make_immediate_reg_float(emit, 1.0);
8799 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &inst->Dst[0], &src);
8800 return true;
8801 } else if (select_swizzle == PIPE_SWIZZLE_0) {
8802 src = make_immediate_reg_float(emit, 0.0);
8803 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &inst->Dst[0], &src);
8804 return true;
8805 }
8806
8807 src = setup_texcoord(emit, unit, &inst->Src[0]);
8808
8809 /* GATHER4 dst, coord, resource, sampler */
8810 /* GATHER4_C dst, coord, resource, sampler ref */
8811 /* GATHER4_PO dst, coord, offset resource, sampler */
8812 /* GATHER4_PO_C dst, coord, offset resource, sampler, ref */
8813 begin_emit_instruction(emit);
8814 if (inst->Texture.NumOffsets == 1) {
8815 if (tgsi_is_shadow_target(target)) {
8816 emit_opcode(emit, VGPU10_OPCODE_GATHER4_PO_C,
8817 inst->Instruction.Saturate);
8818 } else {
8819 emit_opcode(emit, VGPU10_OPCODE_GATHER4_PO,
8820 inst->Instruction.Saturate);
8821 }
8822 } else {
8823 if (tgsi_is_shadow_target(target)) {
8824 emit_opcode(emit, VGPU10_OPCODE_GATHER4_C,
8825 inst->Instruction.Saturate);
8826 } else {
8827 emit_opcode(emit, VGPU10_OPCODE_GATHER4,
8828 inst->Instruction.Saturate);
8829 }
8830 }
8831
8832 emit_dst_register(emit, &inst->Dst[0]);
8833 emit_src_register(emit, &src);
8834 if (inst->Texture.NumOffsets == 1) {
8835 /* offset */
8836 offset_src = make_src_reg(inst->TexOffsets[0].File,
8837 inst->TexOffsets[0].Index);
8838 offset_src = swizzle_src(&offset_src, inst->TexOffsets[0].SwizzleX,
8839 inst->TexOffsets[0].SwizzleY,
8840 inst->TexOffsets[0].SwizzleZ,
8841 TGSI_SWIZZLE_W);
8842 emit_src_register(emit, &offset_src);
8843 }
8844
8845 /* resource */
8846 emit_resource_register(emit, unit);
8847
8848 /* sampler */
8849 sampler = make_src_reg(TGSI_FILE_SAMPLER,
8850 emit->key.tex[unit].sampler_index);
8851 sampler.Register.SwizzleX =
8852 sampler.Register.SwizzleY =
8853 sampler.Register.SwizzleZ =
8854 sampler.Register.SwizzleW = select_swizzle;
8855 emit_src_register(emit, &sampler);
8856
8857 if (tgsi_is_shadow_target(target)) {
8858 /* ref */
8859 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
8860 ref = scalar_src(&inst->Src[1], TGSI_SWIZZLE_X);
8861 emit_tex_compare_refcoord(emit, target, &ref);
8862 } else {
8863 emit_tex_compare_refcoord(emit, target, &src);
8864 }
8865 }
8866
8867 end_emit_instruction(emit);
8868 free_temp_indexes(emit);
8869 } else {
8870 /* Only a single channel is supported in SM4_1 and we report
8871 * pipe_caps.max_texture_gather_components = 1.
8872 * Only the 0th component will be gathered.
8873 */
8874 switch (emit->key.tex[unit].swizzle_r) {
8875 case PIPE_SWIZZLE_X:
8876 get_texel_offsets(emit, inst, offsets);
8877 src = setup_texcoord(emit, unit, &inst->Src[0]);
8878
8879 /* Gather dst, coord, resource, sampler */
8880 begin_emit_instruction(emit);
8881 emit_sample_opcode(emit, VGPU10_OPCODE_GATHER4,
8882 inst->Instruction.Saturate, offsets);
8883 emit_dst_register(emit, &inst->Dst[0]);
8884 emit_src_register(emit, &src);
8885 emit_resource_register(emit, unit);
8886
8887 /* sampler */
8888 sampler = make_src_reg(TGSI_FILE_SAMPLER,
8889 emit->key.tex[unit].sampler_index);
8890 sampler.Register.SwizzleX =
8891 sampler.Register.SwizzleY =
8892 sampler.Register.SwizzleZ =
8893 sampler.Register.SwizzleW = PIPE_SWIZZLE_X;
8894 emit_src_register(emit, &sampler);
8895
8896 end_emit_instruction(emit);
8897 break;
8898 case PIPE_SWIZZLE_W:
8899 case PIPE_SWIZZLE_1:
8900 src = make_immediate_reg_float(emit, 1.0);
8901 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &inst->Dst[0], &src);
8902 break;
8903 case PIPE_SWIZZLE_Y:
8904 case PIPE_SWIZZLE_Z:
8905 case PIPE_SWIZZLE_0:
8906 default:
8907 src = make_immediate_reg_float(emit, 0.0);
8908 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &inst->Dst[0], &src);
8909 break;
8910 }
8911 }
8912
8913 return true;
8914 }
8915
8916
8917
8918 /**
8919 * Emit code for TGSI_OPCODE_TEX2 (texture lookup for shadow cube map arrays)
8920 */
8921 static bool
8922 emit_tex2(struct svga_shader_emitter_v10 *emit,
8923 const struct tgsi_full_instruction *inst)
8924 {
8925 const uint unit = inst->Src[2].Register.Index;
8926 unsigned target = inst->Texture.Texture;
8927 struct tgsi_full_src_register coord, ref;
8928 int offsets[3];
8929 struct tex_swizzle_info swz_info;
8930 VGPU10_OPCODE_TYPE opcode;
8931 bool compare_in_shader;
8932
8933 /* check that the sampler returns a float */
8934 if (!is_valid_tex_instruction(emit, inst))
8935 return true;
8936
8937 compare_in_shader = emit->key.tex[unit].compare_in_shader;
8938 if (compare_in_shader)
8939 opcode = VGPU10_OPCODE_SAMPLE;
8940 else
8941 opcode = VGPU10_OPCODE_SAMPLE_C;
8942
8943 begin_tex_swizzle(emit, unit, inst, compare_in_shader, &swz_info);
8944
8945 get_texel_offsets(emit, inst, offsets);
8946
8947 coord = setup_texcoord(emit, unit, &inst->Src[0]);
8948 ref = scalar_src(&inst->Src[1], TGSI_SWIZZLE_X);
8949
8950 /* SAMPLE_C dst, coord, resource, sampler, ref */
8951 begin_emit_instruction(emit);
8952 emit_sample_opcode(emit, opcode,
8953 inst->Instruction.Saturate, offsets);
8954 emit_dst_register(emit, get_tex_swizzle_dst(&swz_info));
8955 emit_src_register(emit, &coord);
8956 emit_resource_register(emit, unit);
8957 emit_sampler_register(emit, unit);
8958 if (opcode == VGPU10_OPCODE_SAMPLE_C) {
8959 emit_tex_compare_refcoord(emit, target, &ref);
8960 }
8961 end_emit_instruction(emit);
8962
8963 end_tex_swizzle(emit, &swz_info);
8964
8965 free_temp_indexes(emit);
8966
8967 return true;
8968 }
8969
8970
8971 /**
8972 * Emit code for TGSI_OPCODE_TXP (projective texture)
8973 */
8974 static bool
8975 emit_txp(struct svga_shader_emitter_v10 *emit,
8976 const struct tgsi_full_instruction *inst)
8977 {
8978 const uint unit = inst->Src[1].Register.Index;
8979 const enum tgsi_texture_type target = inst->Texture.Texture;
8980 VGPU10_OPCODE_TYPE opcode;
8981 int offsets[3];
8982 unsigned tmp = get_temp_index(emit);
8983 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
8984 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
8985 struct tgsi_full_src_register src0_wwww =
8986 scalar_src(&inst->Src[0], TGSI_SWIZZLE_W);
8987 struct tgsi_full_src_register coord;
8988 struct tex_swizzle_info swz_info;
8989 bool compare_in_shader;
8990
8991 /* check that the sampler returns a float */
8992 if (!is_valid_tex_instruction(emit, inst))
8993 return true;
8994
8995 compare_in_shader = tgsi_is_shadow_target(target) &&
8996 emit->key.tex[unit].compare_in_shader;
8997
8998 begin_tex_swizzle(emit, unit, inst, compare_in_shader, &swz_info);
8999
9000 get_texel_offsets(emit, inst, offsets);
9001
9002 coord = setup_texcoord(emit, unit, &inst->Src[0]);
9003
9004 /* DIV tmp, coord, coord.wwww */
9005 emit_instruction_op2(emit, VGPU10_OPCODE_DIV, &tmp_dst,
9006 &coord, &src0_wwww);
9007
9008 /* SAMPLE dst, coord(tmp), resource, sampler */
9009 begin_emit_instruction(emit);
9010
9011 if (tgsi_is_shadow_target(target) && !compare_in_shader)
9012 /* NOTE: for non-fragment shaders, we should use
9013 * VGPU10_OPCODE_SAMPLE_C_LZ, but our virtual GPU accepts this as-is.
9014 */
9015 opcode = VGPU10_OPCODE_SAMPLE_C;
9016 else
9017 opcode = VGPU10_OPCODE_SAMPLE;
9018
9019 emit_sample_opcode(emit, opcode, inst->Instruction.Saturate, offsets);
9020 emit_dst_register(emit, get_tex_swizzle_dst(&swz_info));
9021 emit_src_register(emit, &tmp_src); /* projected coord */
9022 emit_resource_register(emit, unit);
9023 emit_sampler_register(emit, unit);
9024 if (opcode == VGPU10_OPCODE_SAMPLE_C) {
9025 emit_tex_compare_refcoord(emit, target, &tmp_src);
9026 }
9027 end_emit_instruction(emit);
9028
9029 end_tex_swizzle(emit, &swz_info);
9030
9031 free_temp_indexes(emit);
9032
9033 return true;
9034 }
9035
9036
9037 /**
9038 * Emit code for TGSI_OPCODE_TXD (explicit derivatives)
9039 */
9040 static bool
9041 emit_txd(struct svga_shader_emitter_v10 *emit,
9042 const struct tgsi_full_instruction *inst)
9043 {
9044 const uint unit = inst->Src[3].Register.Index;
9045 const enum tgsi_texture_type target = inst->Texture.Texture;
9046 int offsets[3];
9047 struct tgsi_full_src_register coord;
9048 struct tex_swizzle_info swz_info;
9049
9050 begin_tex_swizzle(emit, unit, inst, tgsi_is_shadow_target(target),
9051 &swz_info);
9052
9053 get_texel_offsets(emit, inst, offsets);
9054
9055 coord = setup_texcoord(emit, unit, &inst->Src[0]);
9056
9057 /* SAMPLE_D dst, coord(s0), resource, sampler, Xderiv(s1), Yderiv(s2) */
9058 begin_emit_instruction(emit);
9059 emit_sample_opcode(emit, VGPU10_OPCODE_SAMPLE_D,
9060 inst->Instruction.Saturate, offsets);
9061 emit_dst_register(emit, get_tex_swizzle_dst(&swz_info));
9062 emit_src_register(emit, &coord);
9063 emit_resource_register(emit, unit);
9064 emit_sampler_register(emit, unit);
9065 emit_src_register(emit, &inst->Src[1]); /* Xderiv */
9066 emit_src_register(emit, &inst->Src[2]); /* Yderiv */
9067 end_emit_instruction(emit);
9068
9069 end_tex_swizzle(emit, &swz_info);
9070
9071 free_temp_indexes(emit);
9072
9073 return true;
9074 }
9075
9076
9077 /**
9078 * Emit code for TGSI_OPCODE_TXF (texel fetch)
9079 */
9080 static bool
9081 emit_txf(struct svga_shader_emitter_v10 *emit,
9082 const struct tgsi_full_instruction *inst)
9083 {
9084 const uint unit = inst->Src[1].Register.Index;
9085 const bool msaa = tgsi_is_msaa_target(inst->Texture.Texture)
9086 && emit->key.tex[unit].num_samples > 1;
9087 int offsets[3];
9088 struct tex_swizzle_info swz_info;
9089
9090 begin_tex_swizzle(emit, unit, inst, false, &swz_info);
9091
9092 get_texel_offsets(emit, inst, offsets);
9093
9094 if (msaa) {
9095 assert(emit->key.tex[unit].num_samples > 1);
9096
9097 /* Fetch one sample from an MSAA texture */
9098 struct tgsi_full_src_register sampleIndex =
9099 scalar_src(&inst->Src[0], TGSI_SWIZZLE_W);
9100 /* LD_MS dst, coord(s0), resource, sampleIndex */
9101 begin_emit_instruction(emit);
9102 emit_sample_opcode(emit, VGPU10_OPCODE_LD_MS,
9103 inst->Instruction.Saturate, offsets);
9104 emit_dst_register(emit, get_tex_swizzle_dst(&swz_info));
9105 emit_src_register(emit, &inst->Src[0]);
9106 emit_resource_register(emit, unit);
9107 emit_src_register(emit, &sampleIndex);
9108 end_emit_instruction(emit);
9109 } else {
9110 /* Fetch one texel specified by integer coordinate */
9111 /* LD dst, coord(s0), resource */
9112 begin_emit_instruction(emit);
9113 emit_sample_opcode(emit, VGPU10_OPCODE_LD,
9114 inst->Instruction.Saturate, offsets);
9115 emit_dst_register(emit, get_tex_swizzle_dst(&swz_info));
9116 emit_src_register(emit, &inst->Src[0]);
9117 emit_resource_register(emit, unit);
9118 end_emit_instruction(emit);
9119 }
9120
9121 end_tex_swizzle(emit, &swz_info);
9122
9123 free_temp_indexes(emit);
9124
9125 return true;
9126 }
9127
9128
9129 /**
9130 * Emit code for TGSI_OPCODE_TXL (explicit LOD) or TGSI_OPCODE_TXB (LOD bias)
9131 * or TGSI_OPCODE_TXB2 (for cube shadow maps).
9132 */
9133 static bool
9134 emit_txl_txb(struct svga_shader_emitter_v10 *emit,
9135 const struct tgsi_full_instruction *inst)
9136 {
9137 const enum tgsi_texture_type target = inst->Texture.Texture;
9138 VGPU10_OPCODE_TYPE opcode;
9139 unsigned unit;
9140 int offsets[3];
9141 struct tgsi_full_src_register coord, lod_bias;
9142 struct tex_swizzle_info swz_info;
9143
9144 assert(inst->Instruction.Opcode == TGSI_OPCODE_TXL ||
9145 inst->Instruction.Opcode == TGSI_OPCODE_TXB ||
9146 inst->Instruction.Opcode == TGSI_OPCODE_TXB2);
9147
9148 if (inst->Instruction.Opcode == TGSI_OPCODE_TXB2) {
9149 lod_bias = scalar_src(&inst->Src[1], TGSI_SWIZZLE_X);
9150 unit = inst->Src[2].Register.Index;
9151 } else {
9152 lod_bias = scalar_src(&inst->Src[0], TGSI_SWIZZLE_W);
9153 unit = inst->Src[1].Register.Index;
9154 }
9155
9156 begin_tex_swizzle(emit, unit, inst, tgsi_is_shadow_target(target),
9157 &swz_info);
9158
9159 get_texel_offsets(emit, inst, offsets);
9160
9161 coord = setup_texcoord(emit, unit, &inst->Src[0]);
9162
9163 /* SAMPLE_L/B dst, coord(s0), resource, sampler, lod(s3) */
9164 begin_emit_instruction(emit);
9165 if (inst->Instruction.Opcode == TGSI_OPCODE_TXL) {
9166 opcode = VGPU10_OPCODE_SAMPLE_L;
9167 } else {
9168 opcode = VGPU10_OPCODE_SAMPLE_B;
9169 }
9170 emit_sample_opcode(emit, opcode, inst->Instruction.Saturate, offsets);
9171 emit_dst_register(emit, get_tex_swizzle_dst(&swz_info));
9172 emit_src_register(emit, &coord);
9173 emit_resource_register(emit, unit);
9174 emit_sampler_register(emit, unit);
9175 emit_src_register(emit, &lod_bias);
9176 end_emit_instruction(emit);
9177
9178 end_tex_swizzle(emit, &swz_info);
9179
9180 free_temp_indexes(emit);
9181
9182 return true;
9183 }
9184
9185
9186 /**
9187 * Emit code for TGSI_OPCODE_TXL2 (explicit LOD) for cubemap array.
9188 */
9189 static bool
9190 emit_txl2(struct svga_shader_emitter_v10 *emit,
9191 const struct tgsi_full_instruction *inst)
9192 {
9193 unsigned target = inst->Texture.Texture;
9194 unsigned opcode, unit;
9195 int offsets[3];
9196 struct tgsi_full_src_register coord, lod;
9197 struct tex_swizzle_info swz_info;
9198
9199 assert(inst->Instruction.Opcode == TGSI_OPCODE_TXL2);
9200
9201 lod = scalar_src(&inst->Src[1], TGSI_SWIZZLE_X);
9202 unit = inst->Src[2].Register.Index;
9203
9204 begin_tex_swizzle(emit, unit, inst, tgsi_is_shadow_target(target),
9205 &swz_info);
9206
9207 get_texel_offsets(emit, inst, offsets);
9208
9209 coord = setup_texcoord(emit, unit, &inst->Src[0]);
9210
9211 /* SAMPLE_L dst, coord(s0), resource, sampler, lod(s3) */
9212 begin_emit_instruction(emit);
9213 opcode = VGPU10_OPCODE_SAMPLE_L;
9214 emit_sample_opcode(emit, opcode, inst->Instruction.Saturate, offsets);
9215 emit_dst_register(emit, get_tex_swizzle_dst(&swz_info));
9216 emit_src_register(emit, &coord);
9217 emit_resource_register(emit, unit);
9218 emit_sampler_register(emit, unit);
9219 emit_src_register(emit, &lod);
9220 end_emit_instruction(emit);
9221
9222 end_tex_swizzle(emit, &swz_info);
9223
9224 free_temp_indexes(emit);
9225
9226 return true;
9227 }
9228
9229
9230 /**
9231 * Emit code for TGSI_OPCODE_TXQ (texture query) instruction.
9232 */
9233 static bool
9234 emit_txq(struct svga_shader_emitter_v10 *emit,
9235 const struct tgsi_full_instruction *inst)
9236 {
9237 const uint unit = inst->Src[1].Register.Index;
9238
9239 if (emit->key.tex[unit].target == PIPE_BUFFER) {
9240 /* RESINFO does not support querying texture buffers, so we instead
9241 * store texture buffer sizes in shader constants, then copy them to
9242 * implement TXQ instead of emitting RESINFO.
9243 * MOV dst, const[texture_buffer_size_index[unit]]
9244 */
9245 struct tgsi_full_src_register size_src =
9246 make_src_const_reg(emit->texture_buffer_size_index[unit]);
9247 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &inst->Dst[0], &size_src);
9248 } else {
9249 /* RESINFO dst, srcMipLevel, resource */
9250 begin_emit_instruction(emit);
9251 emit_opcode_resinfo(emit, VGPU10_RESINFO_RETURN_UINT);
9252 emit_dst_register(emit, &inst->Dst[0]);
9253 emit_src_register(emit, &inst->Src[0]);
9254 emit_resource_register(emit, unit);
9255 end_emit_instruction(emit);
9256 }
9257
9258 free_temp_indexes(emit);
9259
9260 return true;
9261 }
9262
9263
9264 /**
9265 * Does this opcode produce a double-precision result?
9266 * XXX perhaps move this to a TGSI utility.
9267 */
9268 static bool
9269 opcode_has_dbl_dst(unsigned opcode)
9270 {
9271 switch (opcode) {
9272 case TGSI_OPCODE_F2D:
9273 case TGSI_OPCODE_DABS:
9274 case TGSI_OPCODE_DADD:
9275 case TGSI_OPCODE_DFRAC:
9276 case TGSI_OPCODE_DMAX:
9277 case TGSI_OPCODE_DMIN:
9278 case TGSI_OPCODE_DMUL:
9279 case TGSI_OPCODE_DNEG:
9280 case TGSI_OPCODE_I2D:
9281 case TGSI_OPCODE_U2D:
9282 case TGSI_OPCODE_DFMA:
9283 case TGSI_OPCODE_DTRUNC:
9284 case TGSI_OPCODE_DCEIL:
9285 case TGSI_OPCODE_DROUND:
9286 case TGSI_OPCODE_DSSG:
9287 case TGSI_OPCODE_DLDEXP:
9288 case TGSI_OPCODE_DMAD:
9289 case TGSI_OPCODE_DDIV:
9290 case TGSI_OPCODE_DRCP:
9291 case TGSI_OPCODE_DSQRT:
9292 case TGSI_OPCODE_DRSQ:
9293 return true;
9294 default:
9295 return false;
9296 }
9297 }
9298
9299
9300 /**
9301 * Does this opcode use double-precision source registers?
9302 */
9303 static bool
9304 opcode_has_dbl_src(unsigned opcode)
9305 {
9306 switch (opcode) {
9307 case TGSI_OPCODE_D2F:
9308 case TGSI_OPCODE_DABS:
9309 case TGSI_OPCODE_DADD:
9310 case TGSI_OPCODE_DFRAC:
9311 case TGSI_OPCODE_DMAX:
9312 case TGSI_OPCODE_DMIN:
9313 case TGSI_OPCODE_DMUL:
9314 case TGSI_OPCODE_DNEG:
9315 case TGSI_OPCODE_D2I:
9316 case TGSI_OPCODE_D2U:
9317 case TGSI_OPCODE_DFMA:
9318 case TGSI_OPCODE_DSLT:
9319 case TGSI_OPCODE_DSGE:
9320 case TGSI_OPCODE_DSEQ:
9321 case TGSI_OPCODE_DSNE:
9322 case TGSI_OPCODE_DRCP:
9323 case TGSI_OPCODE_DSQRT:
9324 case TGSI_OPCODE_DMAD:
9325 case TGSI_OPCODE_DLDEXP:
9326 case TGSI_OPCODE_DRSQ:
9327 case TGSI_OPCODE_DTRUNC:
9328 case TGSI_OPCODE_DCEIL:
9329 case TGSI_OPCODE_DFLR:
9330 case TGSI_OPCODE_DROUND:
9331 case TGSI_OPCODE_DSSG:
9332 case TGSI_OPCODE_DDIV:
9333 return true;
9334 default:
9335 return false;
9336 }
9337 }
9338
9339
9340 /**
9341 * Check that the swizzle for reading from a double-precision register
9342 * is valid. If not valid, move the source to a temporary register first.
9343 */
9344 static struct tgsi_full_src_register
9345 check_double_src(struct svga_shader_emitter_v10 *emit,
9346 const struct tgsi_full_src_register *reg)
9347 {
9348 struct tgsi_full_src_register src;
9349
9350 if (((reg->Register.SwizzleX == PIPE_SWIZZLE_X &&
9351 reg->Register.SwizzleY == PIPE_SWIZZLE_Y) ||
9352 (reg->Register.SwizzleX == PIPE_SWIZZLE_Z &&
9353 reg->Register.SwizzleY == PIPE_SWIZZLE_W)) &&
9354 ((reg->Register.SwizzleZ == PIPE_SWIZZLE_X &&
9355 reg->Register.SwizzleW == PIPE_SWIZZLE_Y) ||
9356 (reg->Register.SwizzleZ == PIPE_SWIZZLE_Z &&
9357 reg->Register.SwizzleW == PIPE_SWIZZLE_W))) {
9358 src = *reg;
9359 } else {
9360 /* move the src to a temporary to fix the swizzle */
9361 unsigned tmp = get_temp_index(emit);
9362 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
9363 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
9364 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &tmp_dst, reg);
9365 src = tmp_src;
9366
9367 /* The temporary index will be released in the caller */
9368 }
9369 return src;
9370 }
9371
9372 /**
9373 * Check that the writemask for a double-precision instruction is valid.
9374 */
9375 static void
9376 check_double_dst_writemask(const struct tgsi_full_instruction *inst)
9377 {
9378 ASSERTED unsigned writemask = inst->Dst[0].Register.WriteMask;
9379
9380 switch (inst->Instruction.Opcode) {
9381 case TGSI_OPCODE_DABS:
9382 case TGSI_OPCODE_DADD:
9383 case TGSI_OPCODE_DFRAC:
9384 case TGSI_OPCODE_DNEG:
9385 case TGSI_OPCODE_DMAD:
9386 case TGSI_OPCODE_DMAX:
9387 case TGSI_OPCODE_DMIN:
9388 case TGSI_OPCODE_DMUL:
9389 case TGSI_OPCODE_DRCP:
9390 case TGSI_OPCODE_DSQRT:
9391 case TGSI_OPCODE_F2D:
9392 case TGSI_OPCODE_DFMA:
9393 assert(writemask == TGSI_WRITEMASK_XYZW ||
9394 writemask == TGSI_WRITEMASK_XY ||
9395 writemask == TGSI_WRITEMASK_ZW);
9396 break;
9397 case TGSI_OPCODE_DSEQ:
9398 case TGSI_OPCODE_DSGE:
9399 case TGSI_OPCODE_DSNE:
9400 case TGSI_OPCODE_DSLT:
9401 case TGSI_OPCODE_D2I:
9402 case TGSI_OPCODE_D2U:
9403 /* Write to 1 or 2 components only */
9404 assert(util_bitcount(writemask) <= 2);
9405 break;
9406 default:
9407 /* XXX this list may be incomplete */
9408 ;
9409 }
9410 }
9411
9412
9413 /**
9414 * Double-precision absolute value.
9415 */
9416 static bool
9417 emit_dabs(struct svga_shader_emitter_v10 *emit,
9418 const struct tgsi_full_instruction *inst)
9419 {
9420 assert(emit->version >= 50);
9421
9422 struct tgsi_full_src_register src = check_double_src(emit, &inst->Src[0]);
9423 check_double_dst_writemask(inst);
9424
9425 struct tgsi_full_src_register abs_src = absolute_src(&src);
9426
9427 /* DMOV dst, |src| */
9428 emit_instruction_op1(emit, VGPU10_OPCODE_DMOV, &inst->Dst[0], &abs_src);
9429
9430 free_temp_indexes(emit);
9431 return true;
9432 }
9433
9434
9435 /**
9436 * Double-precision negation
9437 */
9438 static bool
9439 emit_dneg(struct svga_shader_emitter_v10 *emit,
9440 const struct tgsi_full_instruction *inst)
9441 {
9442 assert(emit->version >= 50);
9443 struct tgsi_full_src_register src = check_double_src(emit, &inst->Src[0]);
9444 check_double_dst_writemask(inst);
9445
9446 struct tgsi_full_src_register neg_src = negate_src(&src);
9447
9448 /* DMOV dst, -src */
9449 emit_instruction_op1(emit, VGPU10_OPCODE_DMOV, &inst->Dst[0], &neg_src);
9450
9451 free_temp_indexes(emit);
9452 return true;
9453 }
9454
9455
9456 /**
9457 * SM5 has no DMAD opcode. Implement negation with DMUL/DADD.
9458 */
9459 static bool
9460 emit_dmad(struct svga_shader_emitter_v10 *emit,
9461 const struct tgsi_full_instruction *inst)
9462 {
9463 assert(emit->version >= 50);
9464 struct tgsi_full_src_register src0 = check_double_src(emit, &inst->Src[0]);
9465 struct tgsi_full_src_register src1 = check_double_src(emit, &inst->Src[1]);
9466 struct tgsi_full_src_register src2 = check_double_src(emit, &inst->Src[2]);
9467 check_double_dst_writemask(inst);
9468
9469 unsigned tmp = get_temp_index(emit);
9470 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
9471 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
9472
9473 /* DMUL tmp, src[0], src[1] */
9474 emit_instruction_opn(emit, VGPU10_OPCODE_DMUL,
9475 &tmp_dst, &src0, &src1, NULL,
9476 false, inst->Instruction.Precise);
9477
9478 /* DADD dst, tmp, src[2] */
9479 emit_instruction_opn(emit, VGPU10_OPCODE_DADD,
9480 &inst->Dst[0], &tmp_src, &src2, NULL,
9481 inst->Instruction.Saturate, inst->Instruction.Precise);
9482 free_temp_indexes(emit);
9483
9484 return true;
9485 }
9486
9487
9488 /**
9489 * Double precision reciprocal square root
9490 */
9491 static bool
9492 emit_drsq(struct svga_shader_emitter_v10 *emit,
9493 const struct tgsi_full_dst_register *dst,
9494 const struct tgsi_full_src_register *src)
9495 {
9496 assert(emit->version >= 50);
9497
9498 VGPU10OpcodeToken0 token0;
9499 struct tgsi_full_src_register dsrc = check_double_src(emit, src);
9500
9501 begin_emit_instruction(emit);
9502
9503 token0.value = 0;
9504 token0.opcodeType = VGPU10_OPCODE_VMWARE;
9505 token0.vmwareOpcodeType = VGPU10_VMWARE_OPCODE_DRSQ;
9506 emit_dword(emit, token0.value);
9507 emit_dst_register(emit, dst);
9508 emit_src_register(emit, &dsrc);
9509 end_emit_instruction(emit);
9510
9511 free_temp_indexes(emit);
9512
9513 return true;
9514 }
9515
9516
9517 /**
9518 * There is no SM5 opcode for double precision square root.
9519 * It will be implemented with DRSQ.
9520 * dst = src * DRSQ(src)
9521 */
9522 static bool
9523 emit_dsqrt(struct svga_shader_emitter_v10 *emit,
9524 const struct tgsi_full_instruction *inst)
9525 {
9526 assert(emit->version >= 50);
9527
9528 struct tgsi_full_src_register src = check_double_src(emit, &inst->Src[0]);
9529
9530 /* temporary register to hold the source */
9531 unsigned tmp = get_temp_index(emit);
9532 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
9533 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
9534
9535 /* temporary register to hold the DEQ result */
9536 unsigned tmp_cond = get_temp_index(emit);
9537 struct tgsi_full_dst_register tmp_cond_dst = make_dst_temp_reg(tmp_cond);
9538 struct tgsi_full_dst_register tmp_cond_dst_xy =
9539 writemask_dst(&tmp_cond_dst, TGSI_WRITEMASK_X | TGSI_WRITEMASK_Y);
9540 struct tgsi_full_src_register tmp_cond_src = make_src_temp_reg(tmp_cond);
9541 struct tgsi_full_src_register tmp_cond_src_xy =
9542 swizzle_src(&tmp_cond_src,
9543 PIPE_SWIZZLE_X, PIPE_SWIZZLE_Y,
9544 PIPE_SWIZZLE_X, PIPE_SWIZZLE_Y);
9545
9546 /* The reciprocal square root of zero yields INF.
9547 * So if the source is 0, we replace it with 1 in the tmp register.
9548 * The later multiplication of zero in the original source will yield 0
9549 * in the result.
9550 */
9551
9552 /* tmp1 = (src == 0) ? 1 : src;
9553 * EQ tmp1, 0, src
9554 * MOVC tmp, tmp1, 1.0, src
9555 */
9556 struct tgsi_full_src_register zero =
9557 make_immediate_reg_double(emit, 0);
9558
9559 struct tgsi_full_src_register one =
9560 make_immediate_reg_double(emit, 1.0);
9561
9562 emit_instruction_op2(emit, VGPU10_OPCODE_DEQ, &tmp_cond_dst_xy,
9563 &zero, &src);
9564 emit_instruction_op3(emit, VGPU10_OPCODE_DMOVC, &tmp_dst,
9565 &tmp_cond_src_xy, &one, &src);
9566
9567 struct tgsi_full_dst_register tmp_rsq_dst = make_dst_temp_reg(tmp);
9568 struct tgsi_full_src_register tmp_rsq_src = make_src_temp_reg(tmp);
9569
9570 /* DRSQ tmp_rsq, tmp */
9571 emit_drsq(emit, &tmp_rsq_dst, &tmp_src);
9572
9573 /* DMUL dst, tmp_rsq, src[0] */
9574 emit_instruction_op2(emit, VGPU10_OPCODE_DMUL, &inst->Dst[0],
9575 &tmp_rsq_src, &src);
9576
9577 free_temp_indexes(emit);
9578
9579 return true;
9580 }
9581
9582
9583 /**
9584 * glsl-nir path does not lower DTRUNC, so we need to
9585 * add the translation here.
9586 *
9587 * frac = DFRAC(src)
9588 * tmp = src - frac
9589 * dst = src >= 0 ? tmp : (tmp + (frac==0 ? 0 : 1))
9590 */
9591 static bool
9592 emit_dtrunc(struct svga_shader_emitter_v10 *emit,
9593 const struct tgsi_full_instruction *inst)
9594 {
9595 assert(emit->version >= 50);
9596
9597 struct tgsi_full_src_register src = check_double_src(emit, &inst->Src[0]);
9598
9599 /* frac = DFRAC(src) */
9600 unsigned frac_index = get_temp_index(emit);
9601 struct tgsi_full_dst_register frac_dst = make_dst_temp_reg(frac_index);
9602 struct tgsi_full_src_register frac_src = make_src_temp_reg(frac_index);
9603
9604 VGPU10OpcodeToken0 token0;
9605 begin_emit_instruction(emit);
9606 token0.value = 0;
9607 token0.opcodeType = VGPU10_OPCODE_VMWARE;
9608 token0.vmwareOpcodeType = VGPU10_VMWARE_OPCODE_DFRC;
9609 emit_dword(emit, token0.value);
9610 emit_dst_register(emit, &frac_dst);
9611 emit_src_register(emit, &src);
9612 end_emit_instruction(emit);
9613
9614 /* tmp = src - frac */
9615 unsigned tmp_index = get_temp_index(emit);
9616 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp_index);
9617 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp_index);
9618 struct tgsi_full_src_register negate_frac_src = negate_src(&frac_src);
9619 emit_instruction_opn(emit, VGPU10_OPCODE_DADD,
9620 &tmp_dst, &src, &negate_frac_src, NULL,
9621 inst->Instruction.Saturate, inst->Instruction.Precise);
9622
9623 /* cond = frac==0 */
9624 unsigned cond_index = get_temp_index(emit);
9625 struct tgsi_full_dst_register cond_dst = make_dst_temp_reg(cond_index);
9626 struct tgsi_full_src_register cond_src = make_src_temp_reg(cond_index);
9627 struct tgsi_full_src_register zero =
9628 make_immediate_reg_double(emit, 0);
9629
9630 /* Only use one or two components for double opcode */
9631 cond_dst = writemask_dst(&cond_dst, TGSI_WRITEMASK_X | TGSI_WRITEMASK_Y);
9632
9633 emit_instruction_opn(emit, VGPU10_OPCODE_DEQ,
9634 &cond_dst, &frac_src, &zero, NULL,
9635 inst->Instruction.Saturate, inst->Instruction.Precise);
9636
9637 /* tmp2 = cond ? 0 : 1 */
9638 unsigned tmp2_index = get_temp_index(emit);
9639 struct tgsi_full_dst_register tmp2_dst = make_dst_temp_reg(tmp2_index);
9640 struct tgsi_full_src_register tmp2_src = make_src_temp_reg(tmp2_index);
9641 struct tgsi_full_src_register cond_src_xy =
9642 swizzle_src(&cond_src, PIPE_SWIZZLE_X, PIPE_SWIZZLE_Y,
9643 PIPE_SWIZZLE_X, PIPE_SWIZZLE_Y);
9644 struct tgsi_full_src_register one =
9645 make_immediate_reg_double(emit, 1.0);
9646
9647 emit_instruction_opn(emit, VGPU10_OPCODE_DMOVC,
9648 &tmp2_dst, &cond_src_xy, &zero, &one,
9649 inst->Instruction.Saturate, inst->Instruction.Precise);
9650
9651 /* tmp2 = tmp + tmp2 */
9652 emit_instruction_opn(emit, VGPU10_OPCODE_DADD,
9653 &tmp2_dst, &tmp_src, &tmp2_src, NULL,
9654 inst->Instruction.Saturate, inst->Instruction.Precise);
9655
9656 /* cond = src>=0 */
9657 emit_instruction_opn(emit, VGPU10_OPCODE_DGE,
9658 &cond_dst, &src, &zero, NULL,
9659 inst->Instruction.Saturate, inst->Instruction.Precise);
9660
9661 /* dst = cond ? tmp : tmp2 */
9662 emit_instruction_opn(emit, VGPU10_OPCODE_DMOVC,
9663 &inst->Dst[0], &cond_src_xy, &tmp_src, &tmp2_src,
9664 inst->Instruction.Saturate, inst->Instruction.Precise);
9665
9666 free_temp_indexes(emit);
9667 return true;
9668 }
9669
9670
9671 static bool
9672 emit_interp_offset(struct svga_shader_emitter_v10 *emit,
9673 const struct tgsi_full_instruction *inst)
9674 {
9675 assert(emit->version >= 50);
9676
9677 /* The src1.xy offset is a float with values in the range [-0.5, 0.5]
9678 * where (0,0) is the center of the pixel. We need to translate that
9679 * into an integer offset on a 16x16 grid in the range [-8/16, 7/16].
9680 * Also need to flip the Y axis (I think).
9681 */
9682 unsigned tmp = get_temp_index(emit);
9683 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
9684 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
9685 struct tgsi_full_dst_register tmp_dst_xy =
9686 writemask_dst(&tmp_dst, TGSI_WRITEMASK_X | TGSI_WRITEMASK_Y);
9687 struct tgsi_full_src_register const16 =
9688 make_immediate_reg_float4(emit, 16.0f, -16.0, 0, 0);
9689
9690 /* MUL tmp.xy, src1, {16, -16, 0, 0} */
9691 emit_instruction_op2(emit, VGPU10_OPCODE_MUL,
9692 &tmp_dst_xy, &inst->Src[1], &const16);
9693
9694 /* FTOI tmp.xy, tmp */
9695 emit_instruction_op1(emit, VGPU10_OPCODE_FTOI, &tmp_dst_xy, &tmp_src);
9696
9697 /* EVAL_SNAPPED dst, src0, tmp */
9698 emit_instruction_op2(emit, VGPU10_OPCODE_EVAL_SNAPPED,
9699 &inst->Dst[0], &inst->Src[0], &tmp_src);
9700
9701 free_temp_indexes(emit);
9702
9703 return true;
9704 }
9705
9706
9707 /**
9708 * Emit a simple instruction (like ADD, MUL, MIN, etc).
9709 */
9710 static bool
9711 emit_simple(struct svga_shader_emitter_v10 *emit,
9712 const struct tgsi_full_instruction *inst)
9713 {
9714 const enum tgsi_opcode opcode = inst->Instruction.Opcode;
9715 const struct tgsi_opcode_info *op = tgsi_get_opcode_info(opcode);
9716 const bool dbl_dst = opcode_has_dbl_dst(inst->Instruction.Opcode);
9717 const bool dbl_src = opcode_has_dbl_src(inst->Instruction.Opcode);
9718 unsigned i;
9719
9720 struct tgsi_full_src_register src[3];
9721
9722 if (inst->Instruction.Opcode == TGSI_OPCODE_BGNLOOP) {
9723 emit->current_loop_depth++;
9724 } else if (inst->Instruction.Opcode == TGSI_OPCODE_ENDLOOP) {
9725 emit->current_loop_depth--;
9726 }
9727
9728 for (i = 0; i < op->num_src; i++) {
9729 if (dbl_src)
9730 src[i] = check_double_src(emit, &inst->Src[i]);
9731 else
9732 src[i] = inst->Src[i];
9733 }
9734
9735 begin_emit_instruction(emit);
9736 emit_opcode_precise(emit, translate_opcode(inst->Instruction.Opcode),
9737 inst->Instruction.Saturate,
9738 inst->Instruction.Precise);
9739 for (i = 0; i < op->num_dst; i++) {
9740 if (dbl_dst) {
9741 check_double_dst_writemask(inst);
9742 }
9743 emit_dst_register(emit, &inst->Dst[i]);
9744 }
9745 for (i = 0; i < op->num_src; i++) {
9746 emit_src_register(emit, &src[i]);
9747 }
9748 end_emit_instruction(emit);
9749
9750 free_temp_indexes(emit);
9751 return true;
9752 }
9753
9754
9755 /**
9756 * Emit MSB instruction (like IMSB, UMSB).
9757 *
9758 * GLSL returns the index starting from the LSB;
9759 * whereas in SM5, firstbit_hi/shi returns the index starting from the MSB.
9760 * To get correct location as per glsl from SM5 device, we should
9761 * return (31 - index) if returned index is not -1.
9762 */
9763 static bool
9764 emit_msb(struct svga_shader_emitter_v10 *emit,
9765 const struct tgsi_full_instruction *inst)
9766 {
9767 const struct tgsi_full_dst_register *index_dst = &inst->Dst[0];
9768
9769 assert(index_dst->Register.File != TGSI_FILE_OUTPUT);
9770
9771 struct tgsi_full_src_register index_src =
9772 make_src_reg(index_dst->Register.File, index_dst->Register.Index);
9773 struct tgsi_full_src_register imm31 =
9774 make_immediate_reg_int(emit, 31);
9775 imm31 = scalar_src(&imm31, TGSI_SWIZZLE_X);
9776 struct tgsi_full_src_register neg_one =
9777 make_immediate_reg_int(emit, -1);
9778 neg_one = scalar_src(&neg_one, TGSI_SWIZZLE_X);
9779 unsigned tmp = get_temp_index(emit);
9780 const struct tgsi_full_dst_register tmp_dst =
9781 make_dst_temp_reg(tmp);
9782 const struct tgsi_full_dst_register tmp_dst_x =
9783 writemask_dst(&tmp_dst, TGSI_WRITEMASK_X);
9784 const struct tgsi_full_src_register tmp_src_x =
9785 make_src_scalar_reg(TGSI_FILE_TEMPORARY, tmp, TGSI_SWIZZLE_X);
9786 int writemask = TGSI_WRITEMASK_X;
9787 int src_swizzle = TGSI_SWIZZLE_X;
9788 int dst_writemask = index_dst->Register.WriteMask;
9789
9790 emit_simple(emit, inst);
9791
9792 /* index conversion from SM5 to GLSL */
9793 while (writemask & dst_writemask) {
9794 struct tgsi_full_src_register index_src_comp =
9795 scalar_src(&index_src, src_swizzle);
9796 struct tgsi_full_dst_register index_dst_comp =
9797 writemask_dst(index_dst, writemask);
9798
9799 /* check if index_src_comp != -1 */
9800 emit_instruction_op2(emit, VGPU10_OPCODE_INE,
9801 &tmp_dst_x, &index_src_comp, &neg_one);
9802
9803 /* if */
9804 emit_if(emit, &tmp_src_x);
9805
9806 index_src_comp = negate_src(&index_src_comp);
9807 /* SUB DST, IMM{31}, DST */
9808 emit_instruction_op2(emit, VGPU10_OPCODE_IADD,
9809 &index_dst_comp, &imm31, &index_src_comp);
9810
9811 /* endif */
9812 emit_instruction_op0(emit, VGPU10_OPCODE_ENDIF);
9813
9814 writemask = writemask << 1;
9815 src_swizzle = src_swizzle + 1;
9816 }
9817 free_temp_indexes(emit);
9818 return true;
9819 }
9820
9821
9822 /**
9823 * Emit a BFE instruction (like UBFE, IBFE).
9824 * tgsi representation:
9825 * U/IBFE dst, value, offset, width
9826 * SM5 representation:
9827 * U/IBFE dst, width, offset, value
9828 * Note: SM5 has width & offset range (0-31);
9829 * whereas GLSL has width & offset range (0-32)
9830 */
9831 static bool
9832 emit_bfe(struct svga_shader_emitter_v10 *emit,
9833 const struct tgsi_full_instruction *inst)
9834 {
9835 const enum tgsi_opcode opcode = inst->Instruction.Opcode;
9836 struct tgsi_full_src_register imm32 = make_immediate_reg_int(emit, 32);
9837 imm32 = scalar_src(&imm32, TGSI_SWIZZLE_X);
9838 struct tgsi_full_src_register zero = make_immediate_reg_int(emit, 0);
9839 zero = scalar_src(&zero, TGSI_SWIZZLE_X);
9840
9841 unsigned tmp1 = get_temp_index(emit);
9842 const struct tgsi_full_dst_register cond1_dst = make_dst_temp_reg(tmp1);
9843 const struct tgsi_full_dst_register cond1_dst_x =
9844 writemask_dst(&cond1_dst, TGSI_WRITEMASK_X);
9845 const struct tgsi_full_src_register cond1_src_x =
9846 make_src_scalar_reg(TGSI_FILE_TEMPORARY, tmp1, TGSI_SWIZZLE_X);
9847
9848 unsigned tmp2 = get_temp_index(emit);
9849 const struct tgsi_full_dst_register cond2_dst = make_dst_temp_reg(tmp2);
9850 const struct tgsi_full_dst_register cond2_dst_x =
9851 writemask_dst(&cond2_dst, TGSI_WRITEMASK_X);
9852 const struct tgsi_full_src_register cond2_src_x =
9853 make_src_scalar_reg(TGSI_FILE_TEMPORARY, tmp2, TGSI_SWIZZLE_X);
9854
9855 /**
9856 * In SM5, when width = 32 and offset = 0, it returns 0.
9857 * On the other hand GLSL, expects value to be copied as it is, to dst.
9858 */
9859
9860 /* cond1 = width ! = 32 */
9861 emit_instruction_op2(emit, VGPU10_OPCODE_IEQ,
9862 &cond1_dst_x, &inst->Src[2], &imm32);
9863
9864 /* cond2 = offset ! = 0 */
9865 emit_instruction_op2(emit, VGPU10_OPCODE_IEQ,
9866 &cond2_dst_x, &inst->Src[1], &zero);
9867
9868 /* cond 2 = cond1 & cond 2 */
9869 emit_instruction_op2(emit, VGPU10_OPCODE_AND, &cond2_dst_x,
9870 &cond2_src_x,
9871 &cond1_src_x);
9872 /* IF */
9873 emit_if(emit, &cond2_src_x);
9874
9875 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &inst->Dst[0],
9876 &inst->Src[0]);
9877
9878 /* ELSE */
9879 emit_instruction_op0(emit, VGPU10_OPCODE_ELSE);
9880
9881 /* U/IBFE dst, width, offset, value */
9882 emit_instruction_op3(emit, translate_opcode(opcode), &inst->Dst[0],
9883 &inst->Src[2], &inst->Src[1], &inst->Src[0]);
9884
9885 /* ENDIF */
9886 emit_instruction_op0(emit, VGPU10_OPCODE_ENDIF);
9887
9888 free_temp_indexes(emit);
9889 return true;
9890 }
9891
9892
9893 /**
9894 * Emit BFI instruction
9895 * tgsi representation:
9896 * BFI dst, base, insert, offset, width
9897 * SM5 representation:
9898 * BFI dst, width, offset, insert, base
9899 * Note: SM5 has width & offset range (0-31);
9900 * whereas GLSL has width & offset range (0-32)
9901 */
9902 static bool
9903 emit_bfi(struct svga_shader_emitter_v10 *emit,
9904 const struct tgsi_full_instruction *inst)
9905 {
9906 const enum tgsi_opcode opcode = inst->Instruction.Opcode;
9907 struct tgsi_full_src_register imm32 = make_immediate_reg_int(emit, 32);
9908 imm32 = scalar_src(&imm32, TGSI_SWIZZLE_X);
9909
9910 struct tgsi_full_src_register zero = make_immediate_reg_int(emit, 0);
9911 zero = scalar_src(&zero, TGSI_SWIZZLE_X);
9912
9913 unsigned tmp1 = get_temp_index(emit);
9914 const struct tgsi_full_dst_register cond1_dst = make_dst_temp_reg(tmp1);
9915 const struct tgsi_full_dst_register cond1_dst_x =
9916 writemask_dst(&cond1_dst, TGSI_WRITEMASK_X);
9917 const struct tgsi_full_src_register cond1_src_x =
9918 make_src_scalar_reg(TGSI_FILE_TEMPORARY, tmp1, TGSI_SWIZZLE_X);
9919
9920 unsigned tmp2 = get_temp_index(emit);
9921 const struct tgsi_full_dst_register cond2_dst = make_dst_temp_reg(tmp2);
9922 const struct tgsi_full_dst_register cond2_dst_x =
9923 writemask_dst(&cond2_dst, TGSI_WRITEMASK_X);
9924 const struct tgsi_full_src_register cond2_src_x =
9925 make_src_scalar_reg(TGSI_FILE_TEMPORARY, tmp2, TGSI_SWIZZLE_X);
9926
9927 /**
9928 * In SM5, when width = 32 and offset = 0, it returns 0.
9929 * On the other hand GLSL, expects insert to be copied as it is, to dst.
9930 */
9931
9932 /* cond1 = width == 32 */
9933 emit_instruction_op2(emit, VGPU10_OPCODE_IEQ,
9934 &cond1_dst_x, &inst->Src[3], &imm32);
9935
9936 /* cond1 = offset == 0 */
9937 emit_instruction_op2(emit, VGPU10_OPCODE_IEQ,
9938 &cond2_dst_x, &inst->Src[2], &zero);
9939
9940 /* cond2 = cond1 & cond2 */
9941 emit_instruction_op2(emit, VGPU10_OPCODE_AND,
9942 &cond2_dst_x, &cond2_src_x, &cond1_src_x);
9943
9944 /* if */
9945 emit_if(emit, &cond2_src_x);
9946
9947 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &inst->Dst[0],
9948 &inst->Src[1]);
9949
9950 /* else */
9951 emit_instruction_op0(emit, VGPU10_OPCODE_ELSE);
9952
9953 /* BFI dst, width, offset, insert, base */
9954 begin_emit_instruction(emit);
9955 emit_opcode(emit, translate_opcode(opcode), inst->Instruction.Saturate);
9956 emit_dst_register(emit, &inst->Dst[0]);
9957 emit_src_register(emit, &inst->Src[3]);
9958 emit_src_register(emit, &inst->Src[2]);
9959 emit_src_register(emit, &inst->Src[1]);
9960 emit_src_register(emit, &inst->Src[0]);
9961 end_emit_instruction(emit);
9962
9963 /* endif */
9964 emit_instruction_op0(emit, VGPU10_OPCODE_ENDIF);
9965
9966 free_temp_indexes(emit);
9967 return true;
9968 }
9969
9970
9971 /**
9972 * We only special case the MOV instruction to try to detect constant
9973 * color writes in the fragment shader.
9974 */
9975 static bool
9976 emit_mov(struct svga_shader_emitter_v10 *emit,
9977 const struct tgsi_full_instruction *inst)
9978 {
9979 const struct tgsi_full_src_register *src = &inst->Src[0];
9980 const struct tgsi_full_dst_register *dst = &inst->Dst[0];
9981
9982 if (emit->unit == MESA_SHADER_FRAGMENT &&
9983 dst->Register.File == TGSI_FILE_OUTPUT &&
9984 dst->Register.Index == 0 &&
9985 src->Register.File == TGSI_FILE_CONSTANT &&
9986 !src->Register.Indirect) {
9987 emit->constant_color_output = true;
9988 }
9989
9990 return emit_simple(emit, inst);
9991 }
9992
9993
9994 /**
9995 * Emit a simple VGPU10 instruction which writes to multiple dest registers,
9996 * where TGSI only uses one dest register.
9997 */
9998 static bool
9999 emit_simple_1dst(struct svga_shader_emitter_v10 *emit,
10000 const struct tgsi_full_instruction *inst,
10001 unsigned dst_count,
10002 unsigned dst_index)
10003 {
10004 const enum tgsi_opcode opcode = inst->Instruction.Opcode;
10005 const struct tgsi_opcode_info *op = tgsi_get_opcode_info(opcode);
10006 unsigned i;
10007
10008 begin_emit_instruction(emit);
10009 emit_opcode(emit, translate_opcode(opcode), inst->Instruction.Saturate);
10010
10011 for (i = 0; i < dst_count; i++) {
10012 if (i == dst_index) {
10013 emit_dst_register(emit, &inst->Dst[0]);
10014 } else {
10015 emit_null_dst_register(emit);
10016 }
10017 }
10018
10019 for (i = 0; i < op->num_src; i++) {
10020 emit_src_register(emit, &inst->Src[i]);
10021 }
10022 end_emit_instruction(emit);
10023
10024 return true;
10025 }
10026
10027
10028 /**
10029 * Emit a vmware specific VGPU10 instruction.
10030 */
10031 static bool
10032 emit_vmware(struct svga_shader_emitter_v10 *emit,
10033 const struct tgsi_full_instruction *inst,
10034 VGPU10_VMWARE_OPCODE_TYPE subopcode)
10035 {
10036 VGPU10OpcodeToken0 token0;
10037 const enum tgsi_opcode opcode = inst->Instruction.Opcode;
10038 const struct tgsi_opcode_info *op = tgsi_get_opcode_info(opcode);
10039 const bool dbl_dst = opcode_has_dbl_dst(inst->Instruction.Opcode);
10040 const bool dbl_src = opcode_has_dbl_src(inst->Instruction.Opcode);
10041 struct tgsi_full_src_register src[3];
10042
10043 for (unsigned i = 0; i < op->num_src; i++) {
10044 if (dbl_src)
10045 src[i] = check_double_src(emit, &inst->Src[i]);
10046 else
10047 src[i] = inst->Src[i];
10048 }
10049
10050 begin_emit_instruction(emit);
10051
10052 assert((subopcode > 0 && emit->version >= 50) || subopcode == 0);
10053
10054 token0.value = 0;
10055 token0.opcodeType = VGPU10_OPCODE_VMWARE;
10056 token0.vmwareOpcodeType = subopcode;
10057 emit_dword(emit, token0.value);
10058
10059 if (subopcode == VGPU10_VMWARE_OPCODE_IDIV) {
10060 /* IDIV only uses the first dest register. */
10061 emit_dst_register(emit, &inst->Dst[0]);
10062 emit_null_dst_register(emit);
10063 } else {
10064 for (unsigned i = 0; i < op->num_dst; i++) {
10065 if (dbl_dst) {
10066 check_double_dst_writemask(inst);
10067 }
10068 emit_dst_register(emit, &inst->Dst[i]);
10069 }
10070 }
10071
10072 for (unsigned i = 0; i < op->num_src; i++) {
10073 emit_src_register(emit, &src[i]);
10074 }
10075 end_emit_instruction(emit);
10076
10077 free_temp_indexes(emit);
10078 return true;
10079 }
10080
10081 /**
10082 * Emit a memory register
10083 */
10084
10085 typedef enum {
10086 MEM_STORE = 0,
10087 MEM_LOAD = 1,
10088 MEM_ATOMIC_COUNTER
10089 } memory_op;
10090
10091 static void
10092 emit_memory_register(struct svga_shader_emitter_v10 *emit,
10093 memory_op mem_op,
10094 const struct tgsi_full_instruction *inst,
10095 unsigned regIndex, unsigned writemask)
10096 {
10097 VGPU10OperandToken0 operand0;
10098 unsigned resIndex = 0;
10099
10100 operand0.value = 0;
10101 operand0.operandType = VGPU10_OPERAND_TYPE_THREAD_GROUP_SHARED_MEMORY;
10102 operand0.indexDimension = VGPU10_OPERAND_INDEX_1D;
10103 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
10104
10105 switch (mem_op) {
10106 case MEM_ATOMIC_COUNTER:
10107 {
10108 operand0.numComponents = VGPU10_OPERAND_0_COMPONENT;
10109 resIndex = inst->Src[regIndex].Register.Index;
10110 break;
10111 }
10112 case MEM_STORE:
10113 {
10114 const struct tgsi_full_dst_register *reg = &inst->Dst[regIndex];
10115
10116 operand0.selectionMode = VGPU10_OPERAND_4_COMPONENT_MASK_MODE;
10117 operand0.mask = writemask;
10118 resIndex = reg->Register.Index;
10119 break;
10120 }
10121 case MEM_LOAD:
10122 {
10123 const struct tgsi_full_src_register *reg = &inst->Src[regIndex];
10124
10125 operand0.selectionMode = VGPU10_OPERAND_4_COMPONENT_SWIZZLE_MODE;
10126 operand0.swizzleX = reg->Register.SwizzleX;
10127 operand0.swizzleY = reg->Register.SwizzleY;
10128 operand0.swizzleZ = reg->Register.SwizzleZ;
10129 operand0.swizzleW = reg->Register.SwizzleW;
10130 resIndex = reg->Register.Index;
10131 break;
10132 }
10133 default:
10134 assert(!"Unexpected memory opcode");
10135 break;
10136 }
10137
10138 emit_dword(emit, operand0.value);
10139 emit_dword(emit, resIndex);
10140 }
10141
10142
10143 typedef enum {
10144 UAV_STORE = 0,
10145 UAV_LOAD = 1,
10146 UAV_ATOMIC = 2,
10147 UAV_RESQ = 3,
10148 } UAV_OP;
10149
10150
10151 /**
10152 * Emit a uav register
10153 * \param uav_index index of resource register
10154 * \param uav_op UAV_STORE/ UAV_LOAD/ UAV_ATOMIC depending on opcode
10155 * \param resourceType resource file type
10156 * \param writemask resource writemask
10157 */
10158
10159 static void
10160 emit_uav_register(struct svga_shader_emitter_v10 *emit,
10161 unsigned res_index, UAV_OP uav_op,
10162 enum tgsi_file_type resourceType, unsigned writemask)
10163 {
10164 VGPU10OperandToken0 operand0;
10165 unsigned uav_index = INVALID_INDEX;
10166
10167 operand0.value = 0;
10168 operand0.operandType = VGPU10_OPERAND_TYPE_UAV;
10169 operand0.indexDimension = VGPU10_OPERAND_INDEX_1D;
10170 operand0.numComponents = VGPU10_OPERAND_4_COMPONENT;
10171
10172 switch (resourceType) {
10173 case TGSI_FILE_IMAGE:
10174 uav_index = emit->key.images[res_index].uav_index;
10175 break;
10176 case TGSI_FILE_BUFFER:
10177 uav_index = emit->key.shader_buf_uav_index[res_index];
10178 break;
10179 case TGSI_FILE_HW_ATOMIC:
10180 uav_index = emit->key.atomic_buf_uav_index[res_index];
10181 break;
10182 default:
10183 assert(0);
10184 }
10185
10186 switch (uav_op) {
10187 case UAV_ATOMIC:
10188 operand0.numComponents = VGPU10_OPERAND_0_COMPONENT;
10189 break;
10190
10191 case UAV_STORE:
10192 operand0.selectionMode = VGPU10_OPERAND_4_COMPONENT_MASK_MODE;
10193 operand0.mask = writemask;
10194 break;
10195
10196 case UAV_LOAD:
10197 case UAV_RESQ:
10198 operand0.selectionMode = VGPU10_OPERAND_4_COMPONENT_SWIZZLE_MODE;
10199 operand0.swizzleX = VGPU10_COMPONENT_X;
10200 operand0.swizzleY = VGPU10_COMPONENT_Y;
10201 operand0.swizzleZ = VGPU10_COMPONENT_Z;
10202 operand0.swizzleW = VGPU10_COMPONENT_W;
10203 break;
10204
10205 default:
10206 break;
10207 }
10208
10209 emit_dword(emit, operand0.value);
10210 emit_dword(emit, uav_index);
10211 }
10212
10213
10214 /**
10215 * A helper function to emit the uav address.
10216 * For memory, buffer, and image resource, it is set to the specified address.
10217 * For HW atomic counter, the address is the sum of the address offset and the
10218 * offset into the HW atomic buffer as specified by the register index.
10219 * It is also possible to specify the counter index as an indirect address.
10220 * And in this case, the uav address will be the sum of the address offset and the
10221 * counter index specified in the indirect address.
10222 */
10223 static
10224 struct tgsi_full_src_register
10225 emit_uav_addr_offset(struct svga_shader_emitter_v10 *emit,
10226 enum tgsi_file_type resourceType,
10227 unsigned resourceIndex,
10228 unsigned resourceIndirect,
10229 unsigned resourceIndirectIndex,
10230 const struct tgsi_full_src_register *addr_reg)
10231 {
10232 unsigned addr_tmp;
10233 struct tgsi_full_dst_register addr_dst;
10234 struct tgsi_full_src_register addr_src;
10235 struct tgsi_full_src_register two = make_immediate_reg_int(emit, 2);
10236 struct tgsi_full_src_register zero = make_immediate_reg_int(emit, 0);
10237
10238 addr_tmp = get_temp_index(emit);
10239 addr_dst = make_dst_temp_reg(addr_tmp);
10240 addr_src = make_src_temp_reg(addr_tmp);
10241
10242 /* specified address offset */
10243 if (addr_reg)
10244 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &addr_dst, addr_reg);
10245 else
10246 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &addr_dst, &zero);
10247
10248 /* For HW atomic counter, we need to find the index to the
10249 * HW atomic buffer.
10250 */
10251 if (resourceType == TGSI_FILE_HW_ATOMIC) {
10252 if (resourceIndirect) {
10253
10254 /**
10255 * uav addr offset = counter layout offset +
10256 * counter indirect index address + address offset
10257 */
10258
10259 /* counter layout offset */
10260 struct tgsi_full_src_register layout_offset;
10261 layout_offset =
10262 make_immediate_reg_int(emit, resourceIndex);
10263
10264 /* counter layout offset + address offset */
10265 emit_instruction_op2(emit, VGPU10_OPCODE_IADD, &addr_dst,
10266 &addr_src, &layout_offset);
10267
10268 /* counter indirect index address */
10269 unsigned indirect_addr =
10270 emit->address_reg_index[resourceIndirectIndex];
10271
10272 struct tgsi_full_src_register indirect_addr_src =
10273 make_src_temp_reg(indirect_addr);
10274
10275 indirect_addr_src = scalar_src(&indirect_addr_src, TGSI_SWIZZLE_X);
10276
10277 /* counter layout offset + address offset + counter indirect address */
10278 emit_instruction_op2(emit, VGPU10_OPCODE_IADD, &addr_dst,
10279 &addr_src, &indirect_addr_src);
10280
10281 } else {
10282 struct tgsi_full_src_register index_src;
10283
10284 index_src = make_immediate_reg_int(emit, resourceIndex);
10285
10286 /* uav addr offset = counter index address + address offset */
10287 emit_instruction_op2(emit, VGPU10_OPCODE_ADD, &addr_dst,
10288 &addr_src, &index_src);
10289 }
10290
10291 /* HW atomic buffer is declared as raw buffer, so the buffer address is
10292 * the byte offset, so we need to multiple the counter addr offset by 4.
10293 */
10294 emit_instruction_op2(emit, VGPU10_OPCODE_ISHL, &addr_dst,
10295 &addr_src, &two);
10296 }
10297 else if (resourceType == TGSI_FILE_IMAGE) {
10298 if ((emit->key.images[resourceIndex].resource_target == PIPE_TEXTURE_3D)
10299 && emit->key.images[resourceIndex].is_single_layer) {
10300
10301 struct tgsi_full_dst_register addr_dst_z =
10302 writemask_dst(&addr_dst, TGSI_WRITEMASK_Z);
10303
10304 /* For non-layered 3D texture image view, we have to make sure the z
10305 * component of the address offset is set to 0.
10306 */
10307 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &addr_dst_z,
10308 &zero);
10309 }
10310 }
10311
10312 return addr_src;
10313 }
10314
10315
10316
10317 /**
10318 * A helper function to expand indirect indexing to uav resource
10319 * by looping through the resource array, compare the indirect index and
10320 * emit the instruction for each resource in the array.
10321 */
10322 static void
10323 loop_instruction(unsigned index, unsigned count,
10324 struct tgsi_full_src_register *addr_index,
10325 void (*fb)(struct svga_shader_emitter_v10 *,
10326 const struct tgsi_full_instruction *, unsigned),
10327 struct svga_shader_emitter_v10 *emit,
10328 const struct tgsi_full_instruction *inst)
10329 {
10330 if (count == 0)
10331 return;
10332
10333 if (index > 0) {
10334 /* ELSE */
10335 emit_instruction_op0(emit, VGPU10_OPCODE_ELSE);
10336 }
10337
10338 struct tgsi_full_src_register index_src =
10339 make_immediate_reg_int(emit, index);
10340
10341 unsigned tmp_index = get_temp_index(emit);
10342 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp_index);
10343 struct tgsi_full_src_register tmp_src_x =
10344 scalar_src(&tmp_src, TGSI_SWIZZLE_X);
10345 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp_index);
10346
10347 /* IEQ tmp, addr_tmp_index, index */
10348 emit_instruction_op2(emit, VGPU10_OPCODE_IEQ, &tmp_dst,
10349 addr_index, &index_src);
10350
10351 /* IF tmp */
10352 emit_if(emit, &tmp_src_x);
10353
10354 free_temp_indexes(emit);
10355
10356 (*fb)(emit, inst, index);
10357
10358 loop_instruction(index+1, count-1, addr_index, fb, emit, inst);
10359
10360 /* ENDIF */
10361 emit_instruction_op0(emit, VGPU10_OPCODE_ENDIF);
10362 }
10363
10364
10365 /**
10366 * A helper function to emit the load instruction.
10367 */
10368 static void
10369 emit_load_instruction(struct svga_shader_emitter_v10 *emit,
10370 const struct tgsi_full_instruction *inst,
10371 unsigned resourceIndex)
10372 {
10373 VGPU10OpcodeToken0 token0;
10374 struct tgsi_full_src_register addr_src;
10375 enum tgsi_file_type resourceType = inst->Src[0].Register.File;
10376
10377 /* Resolve the resource address for this resource first */
10378 addr_src = emit_uav_addr_offset(emit, resourceType, resourceIndex,
10379 inst->Src[0].Register.Indirect,
10380 inst->Src[0].Indirect.Index,
10381 &inst->Src[1]);
10382
10383 /* LOAD resource, address, src */
10384 begin_emit_instruction(emit);
10385
10386 token0.value = 0;
10387
10388 if (resourceType == TGSI_FILE_MEMORY ||
10389 resourceType == TGSI_FILE_BUFFER ||
10390 resourceType == TGSI_FILE_HW_ATOMIC) {
10391 token0.opcodeType = VGPU10_OPCODE_LD_RAW;
10392 addr_src = scalar_src(&addr_src, TGSI_SWIZZLE_X);
10393 }
10394 else {
10395 token0.opcodeType = VGPU10_OPCODE_LD_UAV_TYPED;
10396 }
10397
10398 token0.saturate = inst->Instruction.Saturate,
10399 emit_dword(emit, token0.value);
10400
10401 emit_dst_register(emit, &inst->Dst[0]);
10402 emit_src_register(emit, &addr_src);
10403
10404 if (resourceType == TGSI_FILE_MEMORY) {
10405 emit_memory_register(emit, MEM_LOAD, inst, 0, 0);
10406 } else if (resourceType == TGSI_FILE_HW_ATOMIC) {
10407 emit_uav_register(emit, inst->Src[0].Dimension.Index,
10408 UAV_LOAD, inst->Src[0].Register.File, 0);
10409 } else if (resourceType == TGSI_FILE_BUFFER) {
10410 if (emit->raw_shaderbufs & (1 << resourceIndex))
10411 emit_resource_register(emit, resourceIndex +
10412 emit->raw_shaderbuf_srv_start_index);
10413 else
10414 emit_uav_register(emit, resourceIndex,
10415 UAV_LOAD, inst->Src[0].Register.File, 0);
10416 } else {
10417 emit_uav_register(emit, resourceIndex,
10418 UAV_LOAD, inst->Src[0].Register.File, 0);
10419 }
10420
10421 end_emit_instruction(emit);
10422
10423 free_temp_indexes(emit);
10424 }
10425
10426
10427 /**
10428 * Emit uav / memory load instruction
10429 */
10430 static bool
10431 emit_load(struct svga_shader_emitter_v10 *emit,
10432 const struct tgsi_full_instruction *inst)
10433 {
10434 enum tgsi_file_type resourceType = inst->Src[0].Register.File;
10435 unsigned resourceIndex = inst->Src[0].Register.Index;
10436
10437 /* If the resource register has indirect index, we will need
10438 * to expand it since SM5 device does not support indirect indexing
10439 * for uav.
10440 */
10441 if (inst->Src[0].Register.Indirect &&
10442 (resourceType == TGSI_FILE_BUFFER || resourceType == TGSI_FILE_IMAGE)) {
10443
10444 unsigned indirect_index = inst->Src[0].Indirect.Index;
10445 unsigned num_resources =
10446 resourceType == TGSI_FILE_BUFFER ? emit->num_shader_bufs :
10447 emit->num_images;
10448
10449 /* indirect index tmp register */
10450 unsigned indirect_addr = emit->address_reg_index[indirect_index];
10451 struct tgsi_full_src_register indirect_addr_src =
10452 make_src_temp_reg(indirect_addr);
10453 indirect_addr_src = scalar_src(&indirect_addr_src, TGSI_SWIZZLE_X);
10454
10455 /* Add offset to the indirect index */
10456 if (inst->Src[0].Register.Index != 0) {
10457 struct tgsi_full_src_register offset =
10458 make_immediate_reg_int(emit, inst->Src[0].Register.Index);
10459 struct tgsi_full_dst_register indirect_addr_dst =
10460 make_dst_temp_reg(indirect_addr);
10461 emit_instruction_op2(emit, VGPU10_OPCODE_IADD, &indirect_addr_dst,
10462 &indirect_addr_src, &offset);
10463 }
10464
10465 /* Loop through the resource array to find which resource to use.
10466 */
10467 loop_instruction(0, num_resources, &indirect_addr_src,
10468 emit_load_instruction, emit, inst);
10469 }
10470 else {
10471 emit_load_instruction(emit, inst, resourceIndex);
10472 }
10473
10474 free_temp_indexes(emit);
10475
10476 return true;
10477 }
10478
10479
10480 /**
10481 * A helper function to emit a store instruction.
10482 */
10483 static void
10484 emit_store_instruction(struct svga_shader_emitter_v10 *emit,
10485 const struct tgsi_full_instruction *inst,
10486 unsigned resourceIndex)
10487 {
10488 VGPU10OpcodeToken0 token0;
10489 enum tgsi_file_type resourceType = inst->Dst[0].Register.File;
10490 unsigned writemask = inst->Dst[0].Register.WriteMask;
10491 struct tgsi_full_src_register addr_src;
10492
10493 unsigned tmp_index = get_temp_index(emit);
10494 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp_index);
10495 struct tgsi_full_dst_register tmp_dst_xyzw = make_dst_temp_reg(tmp_index);
10496 struct tgsi_full_dst_register tmp_dst;
10497
10498 struct tgsi_full_src_register src = inst->Src[1];
10499 struct tgsi_full_src_register four = make_immediate_reg_int(emit, 4);
10500
10501 bool needLoad = false;
10502 bool needPerComponentStore = false;
10503 unsigned swizzles = 0;
10504
10505 /* Resolve the resource address for this resource first */
10506 addr_src = emit_uav_addr_offset(emit, resourceType,
10507 inst->Dst[0].Register.Index,
10508 inst->Dst[0].Register.Indirect,
10509 inst->Dst[0].Indirect.Index,
10510 &inst->Src[0]);
10511
10512 /* First check the writemask to see if it can be supported
10513 * by the store instruction.
10514 * store_raw only allows .x, .xy, .xyz, .xyzw. For the typeless memory,
10515 * we can adjust the address offset, and do a per-component store.
10516 * store_uav_typed only allows .xyzw. In this case, we need to
10517 * do a load first, update the temporary and then issue the
10518 * store. This does have a small risk that if different threads
10519 * update different components of the same address, data might not be
10520 * in sync.
10521 */
10522 if (resourceType == TGSI_FILE_IMAGE) {
10523 needLoad = (writemask == TGSI_WRITEMASK_XYZW) ? false : true;
10524 }
10525 else if (resourceType == TGSI_FILE_BUFFER ||
10526 resourceType == TGSI_FILE_MEMORY) {
10527 if (!(writemask == TGSI_WRITEMASK_X || writemask == TGSI_WRITEMASK_XY ||
10528 writemask == TGSI_WRITEMASK_XYZ ||
10529 writemask == TGSI_WRITEMASK_XYZW)) {
10530 needPerComponentStore = true;
10531 }
10532 }
10533
10534 if (needLoad) {
10535 assert(resourceType == TGSI_FILE_IMAGE);
10536
10537 /* LOAD resource, address, src */
10538 begin_emit_instruction(emit);
10539
10540 token0.value = 0;
10541 token0.opcodeType = VGPU10_OPCODE_LD_UAV_TYPED;
10542 token0.saturate = inst->Instruction.Saturate,
10543 emit_dword(emit, token0.value);
10544
10545 emit_dst_register(emit, &tmp_dst_xyzw);
10546 emit_src_register(emit, &addr_src);
10547 emit_uav_register(emit, resourceIndex, UAV_LOAD, resourceType, 0);
10548
10549 end_emit_instruction(emit);
10550
10551 /* MOV tmp(writemask) src */
10552 tmp_dst = writemask_dst(&tmp_dst_xyzw, writemask);
10553 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &tmp_dst, &inst->Src[1]);
10554
10555 /* Now set the writemask to xyzw for the store_uav_typed instruction */
10556 writemask = TGSI_WRITEMASK_XYZW;
10557 }
10558 else if (needPerComponentStore) {
10559 /* Save the src swizzles */
10560 swizzles = src.Register.SwizzleX |
10561 src.Register.SwizzleY << 2 |
10562 src.Register.SwizzleZ << 4 |
10563 src.Register.SwizzleW << 6;
10564 }
10565
10566 bool storeDone = false;
10567 unsigned perComponentWritemask = writemask;
10568 unsigned shift = 0;
10569 struct tgsi_full_src_register shift_src;
10570
10571 while (!storeDone) {
10572
10573 if (needPerComponentStore) {
10574 assert(perComponentWritemask);
10575 while (!(perComponentWritemask & TGSI_WRITEMASK_X)) {
10576 shift++;
10577 perComponentWritemask >>= 1;
10578 }
10579
10580 /* First adjust the addr_src to the next component */
10581 if (shift != 0) {
10582 struct tgsi_full_dst_register addr_dst =
10583 make_dst_temp_reg(addr_src.Register.Index);
10584 shift_src = make_immediate_reg_int(emit, shift);
10585 emit_instruction_op3(emit, VGPU10_OPCODE_UMAD, &addr_dst, &four,
10586 &shift_src, &addr_src);
10587
10588 /* Adjust the src swizzle as well */
10589 swizzles >>= (shift * 2);
10590 }
10591
10592 /* Now the address offset is set to the next component,
10593 * we can set the writemask to .x and make sure to set
10594 * the src swizzle as well.
10595 */
10596 src.Register.SwizzleX = swizzles & 0x3;
10597 writemask = TGSI_WRITEMASK_X;
10598
10599 /* Shift for the next component check */
10600 perComponentWritemask >>= 1;
10601 shift = 1;
10602 }
10603
10604 /* STORE resource, address, src */
10605 begin_emit_instruction(emit);
10606
10607 token0.value = 0;
10608 token0.saturate = inst->Instruction.Saturate;
10609
10610 if (resourceType == TGSI_FILE_MEMORY) {
10611 token0.opcodeType = VGPU10_OPCODE_STORE_RAW;
10612 addr_src = scalar_src(&addr_src, TGSI_SWIZZLE_X);
10613 emit_dword(emit, token0.value);
10614 emit_memory_register(emit, MEM_STORE, inst, 0, writemask);
10615 }
10616 else if (resourceType == TGSI_FILE_BUFFER ||
10617 resourceType == TGSI_FILE_HW_ATOMIC) {
10618 token0.opcodeType = VGPU10_OPCODE_STORE_RAW;
10619 addr_src = scalar_src(&addr_src, TGSI_SWIZZLE_X);
10620 emit_dword(emit, token0.value);
10621 emit_uav_register(emit, resourceIndex, UAV_STORE,
10622 resourceType, writemask);
10623 }
10624 else {
10625 token0.opcodeType = VGPU10_OPCODE_STORE_UAV_TYPED;
10626 emit_dword(emit, token0.value);
10627 emit_uav_register(emit, resourceIndex, UAV_STORE,
10628 resourceType, writemask);
10629 }
10630
10631 emit_src_register(emit, &addr_src);
10632
10633 if (needLoad)
10634 emit_src_register(emit, &tmp_src);
10635 else
10636 emit_src_register(emit, &src);
10637
10638 end_emit_instruction(emit);
10639
10640 if (!needPerComponentStore || !perComponentWritemask)
10641 storeDone = true;
10642 }
10643
10644 free_temp_indexes(emit);
10645 }
10646
10647
10648 /**
10649 * Emit uav / memory store instruction
10650 */
10651 static bool
10652 emit_store(struct svga_shader_emitter_v10 *emit,
10653 const struct tgsi_full_instruction *inst)
10654 {
10655 enum tgsi_file_type resourceType = inst->Dst[0].Register.File;
10656 unsigned resourceIndex = inst->Dst[0].Register.Index;
10657
10658 /* If the resource register has indirect index, we will need
10659 * to expand it since SM5 device does not support indirect indexing
10660 * for uav.
10661 */
10662 if (inst->Dst[0].Register.Indirect &&
10663 (resourceType == TGSI_FILE_BUFFER || resourceType == TGSI_FILE_IMAGE)) {
10664
10665 unsigned indirect_index = inst->Dst[0].Indirect.Index;
10666 unsigned num_resources =
10667 resourceType == TGSI_FILE_BUFFER ? emit->num_shader_bufs :
10668 emit->num_images;
10669
10670 /* Indirect index tmp register */
10671 unsigned indirect_addr = emit->address_reg_index[indirect_index];
10672 struct tgsi_full_src_register indirect_addr_src =
10673 make_src_temp_reg(indirect_addr);
10674 indirect_addr_src = scalar_src(&indirect_addr_src, TGSI_SWIZZLE_X);
10675
10676 /* Add offset to the indirect index */
10677 if (inst->Dst[0].Register.Index != 0) {
10678 struct tgsi_full_src_register offset =
10679 make_immediate_reg_int(emit, inst->Dst[0].Register.Index);
10680 struct tgsi_full_dst_register indirect_addr_dst =
10681 make_dst_temp_reg(indirect_addr);
10682 emit_instruction_op2(emit, VGPU10_OPCODE_IADD, &indirect_addr_dst,
10683 &indirect_addr_src, &offset);
10684 }
10685
10686 /* Loop through the resource array to find which resource to use.
10687 */
10688 loop_instruction(0, num_resources, &indirect_addr_src,
10689 emit_store_instruction, emit, inst);
10690 }
10691 else {
10692 emit_store_instruction(emit, inst, resourceIndex);
10693 }
10694
10695 free_temp_indexes(emit);
10696
10697 return true;
10698 }
10699
10700
10701 /**
10702 * A helper function to emit an atomic instruction.
10703 */
10704
10705 static void
10706 emit_atomic_instruction(struct svga_shader_emitter_v10 *emit,
10707 const struct tgsi_full_instruction *inst,
10708 unsigned resourceIndex)
10709 {
10710 VGPU10OpcodeToken0 token0;
10711 enum tgsi_file_type resourceType = inst->Src[0].Register.File;
10712 struct tgsi_full_src_register addr_src;
10713 VGPU10_OPCODE_TYPE opcode = emit->cur_atomic_opcode;
10714 const struct tgsi_full_src_register *offset;
10715
10716 /* ntt does not specify offset for HWATOMIC. So just set offset to NULL. */
10717 offset = resourceType == TGSI_FILE_HW_ATOMIC ? NULL : &inst->Src[1];
10718
10719 /* Resolve the resource address */
10720 addr_src = emit_uav_addr_offset(emit, resourceType,
10721 inst->Src[0].Register.Index,
10722 inst->Src[0].Register.Indirect,
10723 inst->Src[0].Indirect.Index,
10724 offset);
10725
10726 /* Emit the atomic operation */
10727 begin_emit_instruction(emit);
10728
10729 token0.value = 0;
10730 token0.opcodeType = opcode;
10731 token0.saturate = inst->Instruction.Saturate,
10732 emit_dword(emit, token0.value);
10733
10734 emit_dst_register(emit, &inst->Dst[0]);
10735
10736 if (inst->Src[0].Register.File == TGSI_FILE_MEMORY) {
10737 emit_memory_register(emit, MEM_ATOMIC_COUNTER, inst, 0, 0);
10738 } else if (inst->Src[0].Register.File == TGSI_FILE_HW_ATOMIC) {
10739 assert(inst->Src[0].Register.Dimension == 1);
10740 emit_uav_register(emit, inst->Src[0].Dimension.Index,
10741 UAV_ATOMIC, inst->Src[0].Register.File, 0);
10742 } else {
10743 emit_uav_register(emit, resourceIndex,
10744 UAV_ATOMIC, inst->Src[0].Register.File, 0);
10745 }
10746
10747 /* resource address offset */
10748 emit_src_register(emit, &addr_src);
10749
10750 struct tgsi_full_src_register src0_x =
10751 swizzle_src(&inst->Src[2], TGSI_SWIZZLE_X, TGSI_SWIZZLE_X,
10752 TGSI_SWIZZLE_X, TGSI_SWIZZLE_X);
10753 emit_src_register(emit, &src0_x);
10754
10755 if (opcode == VGPU10_OPCODE_IMM_ATOMIC_CMP_EXCH) {
10756 struct tgsi_full_src_register src1_x =
10757 swizzle_src(&inst->Src[3], TGSI_SWIZZLE_X, TGSI_SWIZZLE_X,
10758 TGSI_SWIZZLE_X, TGSI_SWIZZLE_X);
10759
10760 emit_src_register(emit, &src1_x);
10761 }
10762
10763 end_emit_instruction(emit);
10764
10765 free_temp_indexes(emit);
10766 }
10767
10768
10769 /**
10770 * Emit atomic instruction
10771 */
10772 static bool
10773 emit_atomic(struct svga_shader_emitter_v10 *emit,
10774 const struct tgsi_full_instruction *inst,
10775 VGPU10_OPCODE_TYPE opcode)
10776 {
10777 enum tgsi_file_type resourceType = inst->Src[0].Register.File;
10778 unsigned resourceIndex = inst->Src[0].Register.Index;
10779
10780 emit->cur_atomic_opcode = opcode;
10781
10782 /* Float types are only supported for image atomic exchange instruction,
10783 * if this check fails then don't emit any intruction.
10784 */
10785 if (resourceType == TGSI_FILE_IMAGE &&
10786 emit->key.images[resourceIndex].return_type == VGPU10_RETURN_TYPE_FLOAT &&
10787 opcode != VGPU10_OPCODE_IMM_ATOMIC_EXCH) {
10788 debug_printf("Unexpected atomic TGSI opcode %s with float type image.",
10789 tgsi_get_opcode_name(inst->Instruction.Opcode));
10790 return true;
10791 }
10792
10793 /* If the resource register has indirect index, we will need
10794 * to expand it since SM5 device does not support indirect indexing
10795 * for uav.
10796 */
10797 if (inst->Dst[0].Register.Indirect &&
10798 (resourceType == TGSI_FILE_BUFFER || resourceType == TGSI_FILE_IMAGE)) {
10799
10800 unsigned indirect_index = inst->Dst[0].Indirect.Index;
10801 unsigned num_resources =
10802 resourceType == TGSI_FILE_BUFFER ? emit->num_shader_bufs :
10803 emit->num_images;
10804
10805 /* indirect index tmp register */
10806 unsigned indirect_addr = emit->address_reg_index[indirect_index];
10807 struct tgsi_full_src_register indirect_addr_src =
10808 make_src_temp_reg(indirect_addr);
10809 indirect_addr_src = scalar_src(&indirect_addr_src, TGSI_SWIZZLE_X);
10810
10811 /* Loop through the resource array to find which resource to use.
10812 */
10813 loop_instruction(0, num_resources, &indirect_addr_src,
10814 emit_atomic_instruction, emit, inst);
10815 }
10816 else {
10817 emit_atomic_instruction(emit, inst, resourceIndex);
10818 }
10819
10820 free_temp_indexes(emit);
10821
10822 return true;
10823 }
10824
10825
10826 /**
10827 * Emit barrier instruction
10828 */
10829 static bool
10830 emit_barrier(struct svga_shader_emitter_v10 *emit,
10831 const struct tgsi_full_instruction *inst)
10832 {
10833 VGPU10OpcodeToken0 token0;
10834
10835 assert(emit->version >= 50);
10836
10837 token0.value = 0;
10838 token0.opcodeType = VGPU10_OPCODE_SYNC;
10839
10840 if (emit->unit == MESA_SHADER_TESS_CTRL && emit->version == 50) {
10841 /* SM5 device doesn't support BARRIER in tcs . If barrier is used
10842 * in shader, don't do anything for this opcode and continue rest
10843 * of shader translation
10844 */
10845 util_debug_message(&emit->svga_debug_callback, INFO,
10846 "barrier instruction is not supported in tessellation control shader\n");
10847 return true;
10848 }
10849 else if (emit->unit == MESA_SHADER_COMPUTE) {
10850 if (emit->cs.shared_memory_declared)
10851 token0.syncThreadGroupShared = 1;
10852
10853 if (emit->uav_declared)
10854 token0.syncUAVMemoryGroup = 1;
10855
10856 token0.syncThreadsInGroup = 1;
10857 } else {
10858 token0.syncUAVMemoryGlobal = 1;
10859 }
10860
10861 assert(token0.syncUAVMemoryGlobal || token0.syncUAVMemoryGroup ||
10862 token0.syncThreadGroupShared);
10863
10864 begin_emit_instruction(emit);
10865 emit_dword(emit, token0.value);
10866 end_emit_instruction(emit);
10867
10868 return true;
10869 }
10870
10871 /**
10872 * Emit memory barrier instruction
10873 */
10874 static bool
10875 emit_memory_barrier(struct svga_shader_emitter_v10 *emit,
10876 const struct tgsi_full_instruction *inst)
10877 {
10878 unsigned index = inst->Src[0].Register.Index;
10879 unsigned swizzle = inst->Src[0].Register.SwizzleX;
10880 unsigned bartype = emit->immediates[index][swizzle].Int;
10881 VGPU10OpcodeToken0 token0;
10882
10883 token0.value = 0;
10884 token0.opcodeType = VGPU10_OPCODE_SYNC;
10885
10886 if (emit->unit == MESA_SHADER_COMPUTE) {
10887
10888 /* For compute shader, issue sync opcode with different options
10889 * depending on the memory barrier type.
10890 *
10891 * Bit 0: Shader storage buffers
10892 * Bit 1: Atomic buffers
10893 * Bit 2: Images
10894 * Bit 3: Shared memory
10895 * Bit 4: Thread group
10896 */
10897
10898 if (bartype & (TGSI_MEMBAR_SHADER_BUFFER | TGSI_MEMBAR_ATOMIC_BUFFER |
10899 TGSI_MEMBAR_SHADER_IMAGE))
10900 token0.syncUAVMemoryGlobal = 1;
10901 else if (bartype & TGSI_MEMBAR_THREAD_GROUP)
10902 token0.syncUAVMemoryGroup = 1;
10903
10904 if (bartype & TGSI_MEMBAR_SHARED)
10905 token0.syncThreadGroupShared = 1;
10906 }
10907 else {
10908 /**
10909 * For graphics stages, only sync_uglobal is available.
10910 */
10911 if (bartype & (TGSI_MEMBAR_SHADER_BUFFER | TGSI_MEMBAR_ATOMIC_BUFFER |
10912 TGSI_MEMBAR_SHADER_IMAGE))
10913 token0.syncUAVMemoryGlobal = 1;
10914 }
10915
10916 assert(token0.syncUAVMemoryGlobal || token0.syncUAVMemoryGroup ||
10917 token0.syncThreadGroupShared);
10918
10919 begin_emit_instruction(emit);
10920 emit_dword(emit, token0.value);
10921 end_emit_instruction(emit);
10922
10923 return true;
10924 }
10925
10926
10927 /**
10928 * Emit code for TGSI_OPCODE_RESQ (image size) instruction.
10929 */
10930 static bool
10931 emit_resq(struct svga_shader_emitter_v10 *emit,
10932 const struct tgsi_full_instruction *inst)
10933 {
10934 struct tgsi_full_src_register zero =
10935 make_immediate_reg_int(emit, 0);
10936
10937 unsigned uav_resource = emit->image[inst->Src[0].Register.Index].Resource;
10938
10939 if (uav_resource == TGSI_TEXTURE_CUBE_ARRAY) {
10940 struct tgsi_full_src_register image_src;
10941
10942 image_src = make_src_const_reg(emit->image_size_index + inst->Src[0].Register.Index);
10943
10944 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &inst->Dst[0], &image_src);
10945 return true;
10946 }
10947
10948 begin_emit_instruction(emit);
10949 if (uav_resource == TGSI_TEXTURE_BUFFER) {
10950 emit_opcode(emit, VGPU10_OPCODE_BUFINFO, false);
10951 emit_dst_register(emit, &inst->Dst[0]);
10952 }
10953 else {
10954 emit_opcode_resinfo(emit, VGPU10_RESINFO_RETURN_UINT);
10955 emit_dst_register(emit, &inst->Dst[0]);
10956 emit_src_register(emit, &zero);
10957 }
10958 emit_uav_register(emit, inst->Src[0].Register.Index,
10959 UAV_RESQ, inst->Src[0].Register.File, 0);
10960 end_emit_instruction(emit);
10961
10962 return true;
10963 }
10964
10965
10966 static bool
10967 emit_instruction(struct svga_shader_emitter_v10 *emit,
10968 unsigned inst_number,
10969 const struct tgsi_full_instruction *inst)
10970 {
10971 const enum tgsi_opcode opcode = inst->Instruction.Opcode;
10972
10973 switch (opcode) {
10974 case TGSI_OPCODE_ADD:
10975 case TGSI_OPCODE_AND:
10976 case TGSI_OPCODE_BGNLOOP:
10977 case TGSI_OPCODE_BRK:
10978 case TGSI_OPCODE_CEIL:
10979 case TGSI_OPCODE_CONT:
10980 case TGSI_OPCODE_DDX:
10981 case TGSI_OPCODE_DDY:
10982 case TGSI_OPCODE_DIV:
10983 case TGSI_OPCODE_DP2:
10984 case TGSI_OPCODE_DP3:
10985 case TGSI_OPCODE_DP4:
10986 case TGSI_OPCODE_ELSE:
10987 case TGSI_OPCODE_ENDIF:
10988 case TGSI_OPCODE_DDX_FINE:
10989 case TGSI_OPCODE_DDY_FINE:
10990 case TGSI_OPCODE_ENDLOOP:
10991 case TGSI_OPCODE_ENDSUB:
10992 case TGSI_OPCODE_F2I:
10993 case TGSI_OPCODE_F2U:
10994 case TGSI_OPCODE_FLR:
10995 case TGSI_OPCODE_FRC:
10996 case TGSI_OPCODE_FSEQ:
10997 case TGSI_OPCODE_FSGE:
10998 case TGSI_OPCODE_FSLT:
10999 case TGSI_OPCODE_FSNE:
11000 case TGSI_OPCODE_I2F:
11001 case TGSI_OPCODE_IMAX:
11002 case TGSI_OPCODE_IMIN:
11003 case TGSI_OPCODE_INEG:
11004 case TGSI_OPCODE_ISGE:
11005 case TGSI_OPCODE_ISHR:
11006 case TGSI_OPCODE_ISLT:
11007 case TGSI_OPCODE_MAD:
11008 case TGSI_OPCODE_MAX:
11009 case TGSI_OPCODE_MIN:
11010 case TGSI_OPCODE_MUL:
11011 case TGSI_OPCODE_NOP:
11012 case TGSI_OPCODE_NOT:
11013 case TGSI_OPCODE_OR:
11014 case TGSI_OPCODE_UADD:
11015 case TGSI_OPCODE_USEQ:
11016 case TGSI_OPCODE_USGE:
11017 case TGSI_OPCODE_USLT:
11018 case TGSI_OPCODE_UMIN:
11019 case TGSI_OPCODE_UMAD:
11020 case TGSI_OPCODE_UMAX:
11021 case TGSI_OPCODE_ROUND:
11022 case TGSI_OPCODE_SQRT:
11023 case TGSI_OPCODE_SHL:
11024 case TGSI_OPCODE_TRUNC:
11025 case TGSI_OPCODE_U2F:
11026 case TGSI_OPCODE_UCMP:
11027 case TGSI_OPCODE_USHR:
11028 case TGSI_OPCODE_USNE:
11029 case TGSI_OPCODE_XOR:
11030 /* Begin SM5 opcodes */
11031 case TGSI_OPCODE_F2D:
11032 case TGSI_OPCODE_D2F:
11033 case TGSI_OPCODE_DADD:
11034 case TGSI_OPCODE_DMUL:
11035 case TGSI_OPCODE_DMAX:
11036 case TGSI_OPCODE_DMIN:
11037 case TGSI_OPCODE_DSGE:
11038 case TGSI_OPCODE_DSLT:
11039 case TGSI_OPCODE_DSEQ:
11040 case TGSI_OPCODE_DSNE:
11041 case TGSI_OPCODE_BREV:
11042 case TGSI_OPCODE_POPC:
11043 case TGSI_OPCODE_LSB:
11044 case TGSI_OPCODE_INTERP_CENTROID:
11045 case TGSI_OPCODE_INTERP_SAMPLE:
11046 /* simple instructions */
11047 return emit_simple(emit, inst);
11048 case TGSI_OPCODE_RET:
11049 if (emit->unit == MESA_SHADER_TESS_CTRL &&
11050 !emit->tcs.control_point_phase) {
11051
11052 /* store the tessellation levels in the patch constant phase only */
11053 store_tesslevels(emit);
11054 }
11055 return emit_simple(emit, inst);
11056
11057 case TGSI_OPCODE_IMSB:
11058 case TGSI_OPCODE_UMSB:
11059 return emit_msb(emit, inst);
11060 case TGSI_OPCODE_IBFE:
11061 case TGSI_OPCODE_UBFE:
11062 return emit_bfe(emit, inst);
11063 case TGSI_OPCODE_BFI:
11064 return emit_bfi(emit, inst);
11065 case TGSI_OPCODE_MOV:
11066 return emit_mov(emit, inst);
11067 case TGSI_OPCODE_EMIT:
11068 return emit_vertex(emit, inst);
11069 case TGSI_OPCODE_ENDPRIM:
11070 return emit_endprim(emit, inst);
11071 case TGSI_OPCODE_IABS:
11072 return emit_iabs(emit, inst);
11073 case TGSI_OPCODE_ARL:
11074 FALLTHROUGH;
11075 case TGSI_OPCODE_UARL:
11076 return emit_arl_uarl(emit, inst);
11077 case TGSI_OPCODE_BGNSUB:
11078 /* no-op */
11079 return true;
11080 case TGSI_OPCODE_CAL:
11081 return emit_cal(emit, inst);
11082 case TGSI_OPCODE_CMP:
11083 return emit_cmp(emit, inst);
11084 case TGSI_OPCODE_COS:
11085 return emit_sincos(emit, inst);
11086 case TGSI_OPCODE_DST:
11087 return emit_dst(emit, inst);
11088 case TGSI_OPCODE_EX2:
11089 return emit_ex2(emit, inst);
11090 case TGSI_OPCODE_EXP:
11091 return emit_exp(emit, inst);
11092 case TGSI_OPCODE_IF:
11093 return emit_if(emit, &inst->Src[0]);
11094 case TGSI_OPCODE_KILL:
11095 return emit_discard(emit, inst);
11096 case TGSI_OPCODE_KILL_IF:
11097 return emit_cond_discard(emit, inst);
11098 case TGSI_OPCODE_LG2:
11099 return emit_lg2(emit, inst);
11100 case TGSI_OPCODE_LIT:
11101 return emit_lit(emit, inst);
11102 case TGSI_OPCODE_LODQ:
11103 return emit_lodq(emit, inst);
11104 case TGSI_OPCODE_LOG:
11105 return emit_log(emit, inst);
11106 case TGSI_OPCODE_LRP:
11107 return emit_lrp(emit, inst);
11108 case TGSI_OPCODE_POW:
11109 return emit_pow(emit, inst);
11110 case TGSI_OPCODE_RCP:
11111 return emit_rcp(emit, inst);
11112 case TGSI_OPCODE_RSQ:
11113 return emit_rsq(emit, inst);
11114 case TGSI_OPCODE_SAMPLE:
11115 return emit_sample(emit, inst);
11116 case TGSI_OPCODE_SEQ:
11117 return emit_seq(emit, inst);
11118 case TGSI_OPCODE_SGE:
11119 return emit_sge(emit, inst);
11120 case TGSI_OPCODE_SGT:
11121 return emit_sgt(emit, inst);
11122 case TGSI_OPCODE_SIN:
11123 return emit_sincos(emit, inst);
11124 case TGSI_OPCODE_SLE:
11125 return emit_sle(emit, inst);
11126 case TGSI_OPCODE_SLT:
11127 return emit_slt(emit, inst);
11128 case TGSI_OPCODE_SNE:
11129 return emit_sne(emit, inst);
11130 case TGSI_OPCODE_SSG:
11131 return emit_ssg(emit, inst);
11132 case TGSI_OPCODE_ISSG:
11133 return emit_issg(emit, inst);
11134 case TGSI_OPCODE_TEX:
11135 return emit_tex(emit, inst);
11136 case TGSI_OPCODE_TG4:
11137 return emit_tg4(emit, inst);
11138 case TGSI_OPCODE_TEX2:
11139 return emit_tex2(emit, inst);
11140 case TGSI_OPCODE_TXP:
11141 return emit_txp(emit, inst);
11142 case TGSI_OPCODE_TXB:
11143 case TGSI_OPCODE_TXB2:
11144 case TGSI_OPCODE_TXL:
11145 return emit_txl_txb(emit, inst);
11146 case TGSI_OPCODE_TXD:
11147 return emit_txd(emit, inst);
11148 case TGSI_OPCODE_TXF:
11149 return emit_txf(emit, inst);
11150 case TGSI_OPCODE_TXL2:
11151 return emit_txl2(emit, inst);
11152 case TGSI_OPCODE_TXQ:
11153 return emit_txq(emit, inst);
11154 case TGSI_OPCODE_UIF:
11155 return emit_if(emit, &inst->Src[0]);
11156 case TGSI_OPCODE_UMUL_HI:
11157 case TGSI_OPCODE_IMUL_HI:
11158 case TGSI_OPCODE_UDIV:
11159 /* These cases use only the FIRST of two destination registers */
11160 return emit_simple_1dst(emit, inst, 2, 0);
11161 case TGSI_OPCODE_IDIV:
11162 return emit_vmware(emit, inst, VGPU10_VMWARE_OPCODE_IDIV);
11163 case TGSI_OPCODE_UMUL:
11164 case TGSI_OPCODE_UMOD:
11165 case TGSI_OPCODE_MOD:
11166 /* These cases use only the SECOND of two destination registers */
11167 return emit_simple_1dst(emit, inst, 2, 1);
11168
11169 /* Begin SM5 opcodes */
11170 case TGSI_OPCODE_DABS:
11171 return emit_dabs(emit, inst);
11172 case TGSI_OPCODE_DNEG:
11173 return emit_dneg(emit, inst);
11174 case TGSI_OPCODE_DRCP:
11175 return emit_simple(emit, inst);
11176 case TGSI_OPCODE_DSQRT:
11177 return emit_dsqrt(emit, inst);
11178 case TGSI_OPCODE_DMAD:
11179 return emit_dmad(emit, inst);
11180 case TGSI_OPCODE_DFRAC:
11181 return emit_vmware(emit, inst, VGPU10_VMWARE_OPCODE_DFRC);
11182 case TGSI_OPCODE_D2I:
11183 case TGSI_OPCODE_D2U:
11184 return emit_simple(emit, inst);
11185 case TGSI_OPCODE_I2D:
11186 case TGSI_OPCODE_U2D:
11187 return emit_simple(emit, inst);
11188 case TGSI_OPCODE_DRSQ:
11189 return emit_drsq(emit, &inst->Dst[0], &inst->Src[0]);
11190 case TGSI_OPCODE_DDIV:
11191 return emit_simple(emit, inst);
11192 case TGSI_OPCODE_INTERP_OFFSET:
11193 return emit_interp_offset(emit, inst);
11194 case TGSI_OPCODE_FMA:
11195 case TGSI_OPCODE_DFMA:
11196 return emit_simple(emit, inst);
11197
11198 case TGSI_OPCODE_DTRUNC:
11199 return emit_dtrunc(emit, inst);
11200
11201 /* The following opcodes should never be seen here. We return zero
11202 * for pipe_caps.tgsi_dround_supported.
11203 */
11204 case TGSI_OPCODE_LDEXP:
11205 case TGSI_OPCODE_DSSG:
11206 case TGSI_OPCODE_DLDEXP:
11207 case TGSI_OPCODE_DCEIL:
11208 case TGSI_OPCODE_DFLR:
11209 debug_printf("Unexpected TGSI opcode %s. "
11210 "Should have been translated away by the GLSL compiler.\n",
11211 tgsi_get_opcode_name(opcode));
11212 return false;
11213
11214 case TGSI_OPCODE_LOAD:
11215 return emit_load(emit, inst);
11216
11217 case TGSI_OPCODE_STORE:
11218 return emit_store(emit, inst);
11219
11220 case TGSI_OPCODE_ATOMAND:
11221 return emit_atomic(emit, inst, VGPU10_OPCODE_IMM_ATOMIC_AND);
11222
11223 case TGSI_OPCODE_ATOMCAS:
11224 return emit_atomic(emit, inst, VGPU10_OPCODE_IMM_ATOMIC_CMP_EXCH);
11225
11226 case TGSI_OPCODE_ATOMIMAX:
11227 return emit_atomic(emit, inst, VGPU10_OPCODE_IMM_ATOMIC_IMAX);
11228
11229 case TGSI_OPCODE_ATOMIMIN:
11230 return emit_atomic(emit, inst, VGPU10_OPCODE_IMM_ATOMIC_IMIN);
11231
11232 case TGSI_OPCODE_ATOMOR:
11233 return emit_atomic(emit, inst, VGPU10_OPCODE_IMM_ATOMIC_OR);
11234
11235 case TGSI_OPCODE_ATOMUADD:
11236 return emit_atomic(emit, inst, VGPU10_OPCODE_IMM_ATOMIC_IADD);
11237
11238 case TGSI_OPCODE_ATOMUMAX:
11239 return emit_atomic(emit, inst, VGPU10_OPCODE_IMM_ATOMIC_UMAX);
11240
11241 case TGSI_OPCODE_ATOMUMIN:
11242 return emit_atomic(emit, inst, VGPU10_OPCODE_IMM_ATOMIC_UMIN);
11243
11244 case TGSI_OPCODE_ATOMXCHG:
11245 return emit_atomic(emit, inst, VGPU10_OPCODE_IMM_ATOMIC_EXCH);
11246
11247 case TGSI_OPCODE_ATOMXOR:
11248 return emit_atomic(emit, inst, VGPU10_OPCODE_IMM_ATOMIC_XOR);
11249
11250 case TGSI_OPCODE_BARRIER:
11251 return emit_barrier(emit, inst);
11252
11253 case TGSI_OPCODE_MEMBAR:
11254 return emit_memory_barrier(emit, inst);
11255
11256 case TGSI_OPCODE_RESQ:
11257 return emit_resq(emit, inst);
11258
11259 case TGSI_OPCODE_END:
11260 if (!emit_post_helpers(emit))
11261 return false;
11262 return emit_simple(emit, inst);
11263
11264 default:
11265 debug_printf("Unimplemented tgsi instruction %s\n",
11266 tgsi_get_opcode_name(opcode));
11267 return false;
11268 }
11269
11270 return true;
11271 }
11272
11273
11274 /**
11275 * Translate a single TGSI instruction to VGPU10.
11276 */
11277 static bool
11278 emit_vgpu10_instruction(struct svga_shader_emitter_v10 *emit,
11279 unsigned inst_number,
11280 const struct tgsi_full_instruction *inst)
11281 {
11282 if (emit->skip_instruction)
11283 return true;
11284
11285 bool ret = true;
11286 unsigned start_token = emit_get_num_tokens(emit);
11287
11288 emit->reemit_tgsi_instruction = false;
11289
11290 ret = emit_instruction(emit, inst_number, inst);
11291
11292 if (emit->reemit_tgsi_instruction) {
11293 /**
11294 * Reset emit->ptr to where the translation of this tgsi instruction
11295 * started.
11296 */
11297 VGPU10OpcodeToken0 *tokens = (VGPU10OpcodeToken0 *) emit->buf;
11298 emit->ptr = (char *) (tokens + start_token);
11299
11300 emit->reemit_tgsi_instruction = false;
11301 }
11302 return ret;
11303 }
11304
11305
11306 /**
11307 * Emit the extra instructions to adjust the vertex position.
11308 * There are two possible adjustments:
11309 * 1. Converting from Gallium to VGPU10 coordinate space by applying the
11310 * "prescale" and "pretranslate" values.
11311 * 2. Undoing the viewport transformation when we use the swtnl/draw path.
11312 * \param vs_pos_tmp_index which temporary register contains the vertex pos.
11313 */
11314 static void
11315 emit_vpos_instructions(struct svga_shader_emitter_v10 *emit)
11316 {
11317 struct tgsi_full_src_register tmp_pos_src;
11318 struct tgsi_full_dst_register pos_dst;
11319 const unsigned vs_pos_tmp_index = emit->vposition.tmp_index;
11320
11321 /* Don't bother to emit any extra vertex instructions if vertex position is
11322 * not written out
11323 */
11324 if (emit->vposition.out_index == INVALID_INDEX)
11325 return;
11326
11327 /**
11328 * Reset the temporary vertex position register index
11329 * so that emit_dst_register() will use the real vertex position output
11330 */
11331 emit->vposition.tmp_index = INVALID_INDEX;
11332
11333 tmp_pos_src = make_src_temp_reg(vs_pos_tmp_index);
11334 pos_dst = make_dst_output_reg(emit->vposition.out_index);
11335
11336 /* If non-adjusted vertex position register index
11337 * is valid, copy the vertex position from the temporary
11338 * vertex position register before it is modified by the
11339 * prescale computation.
11340 */
11341 if (emit->vposition.so_index != INVALID_INDEX) {
11342 struct tgsi_full_dst_register pos_so_dst =
11343 make_dst_output_reg(emit->vposition.so_index);
11344
11345 /* MOV pos_so, tmp_pos */
11346 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &pos_so_dst, &tmp_pos_src);
11347 }
11348
11349 if (emit->vposition.need_prescale) {
11350 /* This code adjusts the vertex position to match the VGPU10 convention.
11351 * If p is the position computed by the shader (usually by applying the
11352 * modelview and projection matrices), the new position q is computed by:
11353 *
11354 * q.x = p.w * trans.x + p.x * scale.x
11355 * q.y = p.w * trans.y + p.y * scale.y
11356 * q.z = p.w * trans.z + p.z * scale.z;
11357 * q.w = p.w * trans.w + p.w;
11358 */
11359 struct tgsi_full_src_register tmp_pos_src_w =
11360 scalar_src(&tmp_pos_src, TGSI_SWIZZLE_W);
11361 struct tgsi_full_dst_register tmp_pos_dst =
11362 make_dst_temp_reg(vs_pos_tmp_index);
11363 struct tgsi_full_dst_register tmp_pos_dst_xyz =
11364 writemask_dst(&tmp_pos_dst, TGSI_WRITEMASK_XYZ);
11365
11366 struct tgsi_full_src_register prescale_scale =
11367 make_src_temp_reg(emit->vposition.prescale_scale_index);
11368 struct tgsi_full_src_register prescale_trans =
11369 make_src_temp_reg(emit->vposition.prescale_trans_index);
11370
11371 /* MUL tmp_pos.xyz, tmp_pos, prescale.scale */
11372 emit_instruction_op2(emit, VGPU10_OPCODE_MUL, &tmp_pos_dst_xyz,
11373 &tmp_pos_src, &prescale_scale);
11374
11375 /* MAD pos, tmp_pos.wwww, prescale.trans, tmp_pos */
11376 emit_instruction_op3(emit, VGPU10_OPCODE_MAD, &pos_dst, &tmp_pos_src_w,
11377 &prescale_trans, &tmp_pos_src);
11378 }
11379 else if (emit->key.vs.undo_viewport) {
11380 /* This code computes the final vertex position from the temporary
11381 * vertex position by undoing the viewport transformation and the
11382 * divide-by-W operation (we convert window coords back to clip coords).
11383 * This is needed when we use the 'draw' module for fallbacks.
11384 * If p is the temp pos in window coords, then the NDC coord q is:
11385 * q.x = (p.x - vp.x_trans) / vp.x_scale * p.w
11386 * q.y = (p.y - vp.y_trans) / vp.y_scale * p.w
11387 * q.z = p.z * p.w
11388 * q.w = p.w
11389 * CONST[vs_viewport_index] contains:
11390 * { 1/vp.x_scale, 1/vp.y_scale, -vp.x_trans, -vp.y_trans }
11391 */
11392 struct tgsi_full_dst_register tmp_pos_dst =
11393 make_dst_temp_reg(vs_pos_tmp_index);
11394 struct tgsi_full_dst_register tmp_pos_dst_xy =
11395 writemask_dst(&tmp_pos_dst, TGSI_WRITEMASK_XY);
11396 struct tgsi_full_src_register tmp_pos_src_wwww =
11397 scalar_src(&tmp_pos_src, TGSI_SWIZZLE_W);
11398
11399 struct tgsi_full_dst_register pos_dst_xyz =
11400 writemask_dst(&pos_dst, TGSI_WRITEMASK_XYZ);
11401 struct tgsi_full_dst_register pos_dst_w =
11402 writemask_dst(&pos_dst, TGSI_WRITEMASK_W);
11403
11404 struct tgsi_full_src_register vp_xyzw =
11405 make_src_const_reg(emit->vs.viewport_index);
11406 struct tgsi_full_src_register vp_zwww =
11407 swizzle_src(&vp_xyzw, TGSI_SWIZZLE_Z, TGSI_SWIZZLE_W,
11408 TGSI_SWIZZLE_W, TGSI_SWIZZLE_W);
11409
11410 /* ADD tmp_pos.xy, tmp_pos.xy, viewport.zwww */
11411 emit_instruction_op2(emit, VGPU10_OPCODE_ADD, &tmp_pos_dst_xy,
11412 &tmp_pos_src, &vp_zwww);
11413
11414 /* MUL tmp_pos.xy, tmp_pos.xyzw, viewport.xyzy */
11415 emit_instruction_op2(emit, VGPU10_OPCODE_MUL, &tmp_pos_dst_xy,
11416 &tmp_pos_src, &vp_xyzw);
11417
11418 /* MUL pos.xyz, tmp_pos.xyz, tmp_pos.www */
11419 emit_instruction_op2(emit, VGPU10_OPCODE_MUL, &pos_dst_xyz,
11420 &tmp_pos_src, &tmp_pos_src_wwww);
11421
11422 /* MOV pos.w, tmp_pos.w */
11423 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &pos_dst_w, &tmp_pos_src);
11424 }
11425 else if (vs_pos_tmp_index != INVALID_INDEX) {
11426 /* This code is to handle the case where the temporary vertex
11427 * position register is created when the vertex shader has stream
11428 * output and prescale is disabled because rasterization is to be
11429 * discarded.
11430 */
11431 struct tgsi_full_dst_register pos_dst =
11432 make_dst_output_reg(emit->vposition.out_index);
11433
11434 /* MOV pos, tmp_pos */
11435 begin_emit_instruction(emit);
11436 emit_opcode(emit, VGPU10_OPCODE_MOV, false);
11437 emit_dst_register(emit, &pos_dst);
11438 emit_src_register(emit, &tmp_pos_src);
11439 end_emit_instruction(emit);
11440 }
11441
11442 /* Restore original vposition.tmp_index value for the next GS vertex.
11443 * It doesn't matter for VS.
11444 */
11445 emit->vposition.tmp_index = vs_pos_tmp_index;
11446 }
11447
11448 static void
11449 emit_clipping_instructions(struct svga_shader_emitter_v10 *emit)
11450 {
11451 if (emit->clip_mode == CLIP_DISTANCE) {
11452 /* Copy from copy distance temporary to CLIPDIST & the shadow copy */
11453 emit_clip_distance_instructions(emit);
11454
11455 } else if (emit->clip_mode == CLIP_VERTEX &&
11456 emit->key.last_vertex_stage) {
11457 /* Convert TGSI CLIPVERTEX to CLIPDIST */
11458 emit_clip_vertex_instructions(emit);
11459 }
11460
11461 /**
11462 * Emit vertex position and take care of legacy user planes only if
11463 * there is a valid vertex position register index.
11464 * This is to take care of the case
11465 * where the shader doesn't output vertex position. Then in
11466 * this case, don't bother to emit more vertex instructions.
11467 */
11468 if (emit->vposition.out_index == INVALID_INDEX)
11469 return;
11470
11471 /**
11472 * Emit per-vertex clipping instructions for legacy user defined clip planes.
11473 * NOTE: we must emit the clip distance instructions before the
11474 * emit_vpos_instructions() call since the later function will change
11475 * the TEMP[vs_pos_tmp_index] value.
11476 */
11477 if (emit->clip_mode == CLIP_LEGACY && emit->key.last_vertex_stage) {
11478 /* Emit CLIPDIST for legacy user defined clip planes */
11479 emit_clip_distance_from_vpos(emit, emit->vposition.tmp_index);
11480 }
11481 }
11482
11483
11484 /**
11485 * Emit extra per-vertex instructions. This includes clip-coordinate
11486 * space conversion and computing clip distances. This is called for
11487 * each GS emit-vertex instruction and at the end of VS translation.
11488 */
11489 static void
11490 emit_vertex_instructions(struct svga_shader_emitter_v10 *emit)
11491 {
11492 /* Emit clipping instructions based on clipping mode */
11493 emit_clipping_instructions(emit);
11494
11495 /* Emit vertex position instructions */
11496 emit_vpos_instructions(emit);
11497 }
11498
11499
11500 /**
11501 * Translate the TGSI_OPCODE_EMIT GS instruction.
11502 */
11503 static bool
11504 emit_vertex(struct svga_shader_emitter_v10 *emit,
11505 const struct tgsi_full_instruction *inst)
11506 {
11507 unsigned ret = true;
11508
11509 assert(emit->unit == MESA_SHADER_GEOMETRY);
11510
11511 /**
11512 * Emit the viewport array index for the first vertex.
11513 */
11514 if (emit->gs.viewport_index_out_index != INVALID_INDEX) {
11515 struct tgsi_full_dst_register viewport_index_out =
11516 make_dst_output_reg(emit->gs.viewport_index_out_index);
11517 struct tgsi_full_dst_register viewport_index_out_x =
11518 writemask_dst(&viewport_index_out, TGSI_WRITEMASK_X);
11519 struct tgsi_full_src_register viewport_index_tmp =
11520 make_src_temp_reg(emit->gs.viewport_index_tmp_index);
11521
11522 /* Set the out index to INVALID_INDEX, so it will not
11523 * be assigned to a temp again in emit_dst_register, and
11524 * the viewport index will not be assigned again in the
11525 * subsequent vertices.
11526 */
11527 emit->gs.viewport_index_out_index = INVALID_INDEX;
11528 emit_instruction_op1(emit, VGPU10_OPCODE_MOV,
11529 &viewport_index_out_x, &viewport_index_tmp);
11530 }
11531
11532 /**
11533 * Find the stream index associated with this emit vertex instruction.
11534 */
11535 assert(inst->Src[0].Register.File == TGSI_FILE_IMMEDIATE);
11536 unsigned streamIndex = find_stream_index(emit, &inst->Src[0]);
11537
11538 /**
11539 * According to the ARB_gpu_shader5 spec, the built-in geometry shader
11540 * outputs are always associated with vertex stream zero.
11541 * So emit the extra vertex instructions for position or clip distance
11542 * for stream zero only.
11543 */
11544 if (streamIndex == 0) {
11545 /**
11546 * Before emitting vertex instructions, emit the temporaries for
11547 * the prescale constants based on the viewport index if needed.
11548 */
11549 if (emit->vposition.need_prescale && !emit->vposition.have_prescale)
11550 emit_temp_prescale_instructions(emit);
11551
11552 emit_vertex_instructions(emit);
11553 }
11554
11555 begin_emit_instruction(emit);
11556 if (emit->version >= 50) {
11557 if (emit->info.num_stream_output_components[streamIndex] == 0) {
11558 /**
11559 * If there is no output for this stream, discard this instruction.
11560 */
11561 emit->discard_instruction = true;
11562 }
11563 else {
11564 emit_opcode(emit, VGPU10_OPCODE_EMIT_STREAM, false);
11565 emit_stream_register(emit, streamIndex);
11566 }
11567 }
11568 else {
11569 emit_opcode(emit, VGPU10_OPCODE_EMIT, false);
11570 }
11571 end_emit_instruction(emit);
11572
11573 return ret;
11574 }
11575
11576
11577 /**
11578 * Emit the extra code to convert from VGPU10's boolean front-face
11579 * register to TGSI's signed front-face register.
11580 *
11581 * TODO: Make temporary front-face register a scalar.
11582 */
11583 static void
11584 emit_frontface_instructions(struct svga_shader_emitter_v10 *emit)
11585 {
11586 assert(emit->unit == MESA_SHADER_FRAGMENT);
11587
11588 if (emit->fs.face_input_index != INVALID_INDEX) {
11589 /* convert vgpu10 boolean face register to gallium +/-1 value */
11590 struct tgsi_full_dst_register tmp_dst =
11591 make_dst_temp_reg(emit->fs.face_tmp_index);
11592 struct tgsi_full_src_register one =
11593 make_immediate_reg_float(emit, 1.0f);
11594 struct tgsi_full_src_register neg_one =
11595 make_immediate_reg_float(emit, -1.0f);
11596
11597 /* MOVC face_tmp, IS_FRONT_FACE.x, 1.0, -1.0 */
11598 begin_emit_instruction(emit);
11599 emit_opcode(emit, VGPU10_OPCODE_MOVC, false);
11600 emit_dst_register(emit, &tmp_dst);
11601 emit_face_register(emit);
11602 emit_src_register(emit, &one);
11603 emit_src_register(emit, &neg_one);
11604 end_emit_instruction(emit);
11605 }
11606 }
11607
11608
11609 /**
11610 * Emit the extra code to convert from VGPU10's fragcoord.w value to 1/w.
11611 */
11612 static void
11613 emit_fragcoord_instructions(struct svga_shader_emitter_v10 *emit)
11614 {
11615 assert(emit->unit == MESA_SHADER_FRAGMENT);
11616
11617 if (emit->fs.fragcoord_input_index != INVALID_INDEX) {
11618 struct tgsi_full_dst_register tmp_dst =
11619 make_dst_temp_reg(emit->fs.fragcoord_tmp_index);
11620 struct tgsi_full_dst_register tmp_dst_xyz =
11621 writemask_dst(&tmp_dst, TGSI_WRITEMASK_XYZ);
11622 struct tgsi_full_dst_register tmp_dst_w =
11623 writemask_dst(&tmp_dst, TGSI_WRITEMASK_W);
11624 struct tgsi_full_src_register one =
11625 make_immediate_reg_float(emit, 1.0f);
11626 struct tgsi_full_src_register fragcoord =
11627 make_src_reg(TGSI_FILE_INPUT, emit->fs.fragcoord_input_index);
11628
11629 /* save the input index */
11630 unsigned fragcoord_input_index = emit->fs.fragcoord_input_index;
11631 /* set to invalid to prevent substitution in emit_src_register() */
11632 emit->fs.fragcoord_input_index = INVALID_INDEX;
11633
11634 /* MOV fragcoord_tmp.xyz, fragcoord.xyz */
11635 begin_emit_instruction(emit);
11636 emit_opcode(emit, VGPU10_OPCODE_MOV, false);
11637 emit_dst_register(emit, &tmp_dst_xyz);
11638 emit_src_register(emit, &fragcoord);
11639 end_emit_instruction(emit);
11640
11641 /* DIV fragcoord_tmp.w, 1.0, fragcoord.w */
11642 begin_emit_instruction(emit);
11643 emit_opcode(emit, VGPU10_OPCODE_DIV, false);
11644 emit_dst_register(emit, &tmp_dst_w);
11645 emit_src_register(emit, &one);
11646 emit_src_register(emit, &fragcoord);
11647 end_emit_instruction(emit);
11648
11649 /* restore saved value */
11650 emit->fs.fragcoord_input_index = fragcoord_input_index;
11651 }
11652 }
11653
11654
11655 /**
11656 * Emit the extra code to get the current sample position value and
11657 * put it into a temp register.
11658 */
11659 static void
11660 emit_sample_position_instructions(struct svga_shader_emitter_v10 *emit)
11661 {
11662 assert(emit->unit == MESA_SHADER_FRAGMENT);
11663
11664 if (emit->fs.sample_pos_sys_index != INVALID_INDEX) {
11665 assert(emit->version >= 41);
11666
11667 struct tgsi_full_dst_register tmp_dst =
11668 make_dst_temp_reg(emit->fs.sample_pos_tmp_index);
11669 struct tgsi_full_src_register half =
11670 make_immediate_reg_float4(emit, 0.5, 0.5, 0.0, 0.0);
11671
11672 struct tgsi_full_src_register tmp_src =
11673 make_src_temp_reg(emit->fs.sample_pos_tmp_index);
11674 struct tgsi_full_src_register sample_index_reg =
11675 make_src_scalar_reg(TGSI_FILE_SYSTEM_VALUE,
11676 emit->fs.sample_id_sys_index, TGSI_SWIZZLE_X);
11677
11678 /* The first src register is a shader resource (if we want a
11679 * multisampled resource sample position) or the rasterizer register
11680 * (if we want the current sample position in the color buffer). We
11681 * want the later.
11682 */
11683
11684 /* SAMPLE_POS dst, RASTERIZER, sampleIndex */
11685 begin_emit_instruction(emit);
11686 emit_opcode(emit, VGPU10_OPCODE_SAMPLE_POS, false);
11687 emit_dst_register(emit, &tmp_dst);
11688 emit_rasterizer_register(emit);
11689 emit_src_register(emit, &sample_index_reg);
11690 end_emit_instruction(emit);
11691
11692 /* Convert from D3D coords to GL coords by adding 0.5 bias */
11693 /* ADD dst, dst, half */
11694 begin_emit_instruction(emit);
11695 emit_opcode(emit, VGPU10_OPCODE_ADD, false);
11696 emit_dst_register(emit, &tmp_dst);
11697 emit_src_register(emit, &tmp_src);
11698 emit_src_register(emit, &half);
11699 end_emit_instruction(emit);
11700 }
11701 }
11702
11703
11704 /**
11705 * Emit extra instructions to adjust VS inputs/attributes. This can
11706 * mean casting a vertex attribute from int to float or setting the
11707 * W component to 1, or both.
11708 */
11709 static void
11710 emit_vertex_attrib_instructions(struct svga_shader_emitter_v10 *emit)
11711 {
11712 const unsigned save_w_1_mask = emit->key.vs.adjust_attrib_w_1;
11713 const unsigned save_itof_mask = emit->key.vs.adjust_attrib_itof;
11714 const unsigned save_utof_mask = emit->key.vs.adjust_attrib_utof;
11715 const unsigned save_is_bgra_mask = emit->key.vs.attrib_is_bgra;
11716 const unsigned save_puint_to_snorm_mask = emit->key.vs.attrib_puint_to_snorm;
11717 const unsigned save_puint_to_uscaled_mask = emit->key.vs.attrib_puint_to_uscaled;
11718 const unsigned save_puint_to_sscaled_mask = emit->key.vs.attrib_puint_to_sscaled;
11719
11720 unsigned adjust_mask = (save_w_1_mask |
11721 save_itof_mask |
11722 save_utof_mask |
11723 save_is_bgra_mask |
11724 save_puint_to_snorm_mask |
11725 save_puint_to_uscaled_mask |
11726 save_puint_to_sscaled_mask);
11727
11728 assert(emit->unit == MESA_SHADER_VERTEX);
11729
11730 if (adjust_mask) {
11731 struct tgsi_full_src_register one =
11732 make_immediate_reg_float(emit, 1.0f);
11733
11734 struct tgsi_full_src_register one_int =
11735 make_immediate_reg_int(emit, 1);
11736
11737 /* We need to turn off these bitmasks while emitting the
11738 * instructions below, then restore them afterward.
11739 */
11740 emit->key.vs.adjust_attrib_w_1 = 0;
11741 emit->key.vs.adjust_attrib_itof = 0;
11742 emit->key.vs.adjust_attrib_utof = 0;
11743 emit->key.vs.attrib_is_bgra = 0;
11744 emit->key.vs.attrib_puint_to_snorm = 0;
11745 emit->key.vs.attrib_puint_to_uscaled = 0;
11746 emit->key.vs.attrib_puint_to_sscaled = 0;
11747
11748 while (adjust_mask) {
11749 unsigned index = u_bit_scan(&adjust_mask);
11750
11751 /* skip the instruction if this vertex attribute is not being used */
11752 if (emit->info.input_usage_mask[index] == 0)
11753 continue;
11754
11755 unsigned tmp = emit->vs.adjusted_input[index];
11756 struct tgsi_full_src_register input_src =
11757 make_src_reg(TGSI_FILE_INPUT, index);
11758
11759 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
11760 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
11761 struct tgsi_full_dst_register tmp_dst_w =
11762 writemask_dst(&tmp_dst, TGSI_WRITEMASK_W);
11763
11764 /* ITOF/UTOF/MOV tmp, input[index] */
11765 if (save_itof_mask & (1 << index)) {
11766 emit_instruction_op1(emit, VGPU10_OPCODE_ITOF,
11767 &tmp_dst, &input_src);
11768 }
11769 else if (save_utof_mask & (1 << index)) {
11770 emit_instruction_op1(emit, VGPU10_OPCODE_UTOF,
11771 &tmp_dst, &input_src);
11772 }
11773 else if (save_puint_to_snorm_mask & (1 << index)) {
11774 emit_puint_to_snorm(emit, &tmp_dst, &input_src);
11775 }
11776 else if (save_puint_to_uscaled_mask & (1 << index)) {
11777 emit_puint_to_uscaled(emit, &tmp_dst, &input_src);
11778 }
11779 else if (save_puint_to_sscaled_mask & (1 << index)) {
11780 emit_puint_to_sscaled(emit, &tmp_dst, &input_src);
11781 }
11782 else {
11783 assert((save_w_1_mask | save_is_bgra_mask) & (1 << index));
11784 emit_instruction_op1(emit, VGPU10_OPCODE_MOV,
11785 &tmp_dst, &input_src);
11786 }
11787
11788 if (save_is_bgra_mask & (1 << index)) {
11789 emit_swap_r_b(emit, &tmp_dst, &tmp_src);
11790 }
11791
11792 if (save_w_1_mask & (1 << index)) {
11793 /* MOV tmp.w, 1.0 */
11794 if (emit->key.vs.attrib_is_pure_int & (1 << index)) {
11795 emit_instruction_op1(emit, VGPU10_OPCODE_MOV,
11796 &tmp_dst_w, &one_int);
11797 }
11798 else {
11799 emit_instruction_op1(emit, VGPU10_OPCODE_MOV,
11800 &tmp_dst_w, &one);
11801 }
11802 }
11803 }
11804
11805 emit->key.vs.adjust_attrib_w_1 = save_w_1_mask;
11806 emit->key.vs.adjust_attrib_itof = save_itof_mask;
11807 emit->key.vs.adjust_attrib_utof = save_utof_mask;
11808 emit->key.vs.attrib_is_bgra = save_is_bgra_mask;
11809 emit->key.vs.attrib_puint_to_snorm = save_puint_to_snorm_mask;
11810 emit->key.vs.attrib_puint_to_uscaled = save_puint_to_uscaled_mask;
11811 emit->key.vs.attrib_puint_to_sscaled = save_puint_to_sscaled_mask;
11812 }
11813 }
11814
11815
11816 /* Find zero-value immedate for default layer index */
11817 static void
11818 emit_default_layer_instructions(struct svga_shader_emitter_v10 *emit)
11819 {
11820 assert(emit->unit == MESA_SHADER_FRAGMENT);
11821
11822 /* immediate for default layer index 0 */
11823 if (emit->fs.layer_input_index != INVALID_INDEX) {
11824 union tgsi_immediate_data imm;
11825 imm.Int = 0;
11826 emit->fs.layer_imm_index = find_immediate(emit, imm, 0);
11827 }
11828 }
11829
11830
11831 static void
11832 emit_temp_prescale_from_cbuf(struct svga_shader_emitter_v10 *emit,
11833 unsigned cbuf_index,
11834 struct tgsi_full_dst_register *scale,
11835 struct tgsi_full_dst_register *translate)
11836 {
11837 struct tgsi_full_src_register scale_cbuf = make_src_const_reg(cbuf_index);
11838 struct tgsi_full_src_register trans_cbuf = make_src_const_reg(cbuf_index+1);
11839
11840 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, scale, &scale_cbuf);
11841 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, translate, &trans_cbuf);
11842 }
11843
11844
11845 /**
11846 * A recursive helper function to find the prescale from the constant buffer
11847 */
11848 static void
11849 find_prescale_from_cbuf(struct svga_shader_emitter_v10 *emit,
11850 unsigned index, unsigned num_prescale,
11851 struct tgsi_full_src_register *vp_index,
11852 struct tgsi_full_dst_register *scale,
11853 struct tgsi_full_dst_register *translate,
11854 struct tgsi_full_src_register *tmp_src,
11855 struct tgsi_full_dst_register *tmp_dst)
11856 {
11857 if (num_prescale == 0)
11858 return;
11859
11860 if (index > 0) {
11861 /* ELSE */
11862 emit_instruction_op0(emit, VGPU10_OPCODE_ELSE);
11863 }
11864
11865 struct tgsi_full_src_register index_src =
11866 make_immediate_reg_int(emit, index);
11867
11868 if (index == 0) {
11869 /* GE tmp, vp_index, index */
11870 emit_instruction_op2(emit, VGPU10_OPCODE_GE, tmp_dst,
11871 vp_index, &index_src);
11872 } else {
11873 /* EQ tmp, vp_index, index */
11874 emit_instruction_op2(emit, VGPU10_OPCODE_EQ, tmp_dst,
11875 vp_index, &index_src);
11876 }
11877
11878 /* IF tmp */
11879 emit_if(emit, tmp_src);
11880 emit_temp_prescale_from_cbuf(emit,
11881 emit->vposition.prescale_cbuf_index + 2 * index,
11882 scale, translate);
11883
11884 find_prescale_from_cbuf(emit, index+1, num_prescale-1,
11885 vp_index, scale, translate,
11886 tmp_src, tmp_dst);
11887
11888 /* ENDIF */
11889 emit_instruction_op0(emit, VGPU10_OPCODE_ENDIF);
11890 }
11891
11892
11893 /**
11894 * This helper function emits instructions to set the prescale
11895 * and translate temporaries to the correct constants from the
11896 * constant buffer according to the designated viewport.
11897 */
11898 static void
11899 emit_temp_prescale_instructions(struct svga_shader_emitter_v10 *emit)
11900 {
11901 struct tgsi_full_dst_register prescale_scale =
11902 make_dst_temp_reg(emit->vposition.prescale_scale_index);
11903 struct tgsi_full_dst_register prescale_translate =
11904 make_dst_temp_reg(emit->vposition.prescale_trans_index);
11905
11906 unsigned prescale_cbuf_index = emit->vposition.prescale_cbuf_index;
11907
11908 if (emit->vposition.num_prescale == 1) {
11909 emit_temp_prescale_from_cbuf(emit,
11910 prescale_cbuf_index,
11911 &prescale_scale, &prescale_translate);
11912 } else {
11913 /**
11914 * Since SM5 device does not support dynamic indexing, we need
11915 * to do the if-else to find the prescale constants for the
11916 * specified viewport.
11917 */
11918 struct tgsi_full_src_register vp_index_src =
11919 make_src_temp_reg(emit->gs.viewport_index_tmp_index);
11920
11921 struct tgsi_full_src_register vp_index_src_x =
11922 scalar_src(&vp_index_src, TGSI_SWIZZLE_X);
11923
11924 unsigned tmp = get_temp_index(emit);
11925 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
11926 struct tgsi_full_src_register tmp_src_x =
11927 scalar_src(&tmp_src, TGSI_SWIZZLE_X);
11928 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
11929
11930 find_prescale_from_cbuf(emit, 0, emit->vposition.num_prescale,
11931 &vp_index_src_x,
11932 &prescale_scale, &prescale_translate,
11933 &tmp_src_x, &tmp_dst);
11934 }
11935
11936 /* Mark prescale temporaries are emitted */
11937 emit->vposition.have_prescale = 1;
11938 }
11939
11940
11941 /**
11942 * A helper function to emit an instruction in a vertex shader to add a bias
11943 * to the VertexID system value. This patches the VertexID in the SVGA vertex
11944 * shader to include the base vertex of an indexed primitive or the start index
11945 * of a non-indexed primitive.
11946 */
11947 static void
11948 emit_vertex_id_nobase_instruction(struct svga_shader_emitter_v10 *emit)
11949 {
11950 struct tgsi_full_src_register vertex_id_bias_index =
11951 make_src_const_reg(emit->vs.vertex_id_bias_index);
11952 struct tgsi_full_src_register vertex_id_sys_src =
11953 make_src_reg(TGSI_FILE_SYSTEM_VALUE, emit->vs.vertex_id_sys_index);
11954 struct tgsi_full_src_register vertex_id_sys_src_x =
11955 scalar_src(&vertex_id_sys_src, TGSI_SWIZZLE_X);
11956 struct tgsi_full_dst_register vertex_id_tmp_dst =
11957 make_dst_temp_reg(emit->vs.vertex_id_tmp_index);
11958
11959 /* IADD vertex_id_tmp, vertex_id_sys, vertex_id_bias */
11960 unsigned vertex_id_tmp_index = emit->vs.vertex_id_tmp_index;
11961 emit->vs.vertex_id_tmp_index = INVALID_INDEX;
11962 emit_instruction_opn(emit, VGPU10_OPCODE_IADD, &vertex_id_tmp_dst,
11963 &vertex_id_sys_src_x, &vertex_id_bias_index, NULL, false,
11964 false);
11965 emit->vs.vertex_id_tmp_index = vertex_id_tmp_index;
11966 }
11967
11968 /**
11969 * Hull Shader must have control point outputs. But tessellation
11970 * control shader can return without writing to control point output.
11971 * In this case, the control point output is assumed to be passthrough
11972 * from the control point input.
11973 * This helper function is to write out a control point output first in case
11974 * the tessellation control shader returns before writing a
11975 * control point output.
11976 */
11977 static void
11978 emit_tcs_default_control_point_output(struct svga_shader_emitter_v10 *emit)
11979 {
11980 assert(emit->unit == MESA_SHADER_TESS_CTRL);
11981 assert(emit->tcs.control_point_phase);
11982 assert(emit->tcs.control_point_out_index != INVALID_INDEX);
11983 assert(emit->tcs.invocation_id_sys_index != INVALID_INDEX);
11984
11985 struct tgsi_full_dst_register output_control_point;
11986 output_control_point =
11987 make_dst_output_reg(emit->tcs.control_point_out_index);
11988
11989 if (emit->tcs.control_point_input_index == INVALID_INDEX) {
11990 /* MOV OUTPUT 0.0f */
11991 struct tgsi_full_src_register zero = make_immediate_reg_float(emit, 0.0f);
11992 begin_emit_instruction(emit);
11993 emit_opcode_precise(emit, VGPU10_OPCODE_MOV, false, false);
11994 emit_dst_register(emit, &output_control_point);
11995 emit_src_register(emit, &zero);
11996 end_emit_instruction(emit);
11997 }
11998 else {
11999 /* UARL ADDR[INDEX].x INVOCATION.xxxx */
12000
12001 struct tgsi_full_src_register invocation_src;
12002 struct tgsi_full_dst_register addr_dst;
12003 struct tgsi_full_dst_register addr_dst_x;
12004 unsigned addr_tmp;
12005
12006 addr_tmp = emit->address_reg_index[emit->tcs.control_point_addr_index];
12007 addr_dst = make_dst_temp_reg(addr_tmp);
12008 addr_dst_x = writemask_dst(&addr_dst, TGSI_WRITEMASK_X);
12009
12010 invocation_src = make_src_reg(TGSI_FILE_SYSTEM_VALUE,
12011 emit->tcs.invocation_id_sys_index);
12012
12013 begin_emit_instruction(emit);
12014 emit_opcode_precise(emit, VGPU10_OPCODE_MOV, false, false);
12015 emit_dst_register(emit, &addr_dst_x);
12016 emit_src_register(emit, &invocation_src);
12017 end_emit_instruction(emit);
12018
12019
12020 /* MOV OUTPUT INPUT[ADDR[INDEX].x][POSITION] */
12021
12022 struct tgsi_full_src_register input_control_point;
12023 input_control_point = make_src_reg(TGSI_FILE_INPUT,
12024 emit->tcs.control_point_input_index);
12025 input_control_point.Register.Dimension = 1;
12026 input_control_point.Dimension.Indirect = 1;
12027 input_control_point.DimIndirect.File = TGSI_FILE_ADDRESS;
12028 input_control_point.DimIndirect.Index =
12029 emit->tcs.control_point_addr_index;
12030
12031 begin_emit_instruction(emit);
12032 emit_opcode_precise(emit, VGPU10_OPCODE_MOV, false, false);
12033 emit_dst_register(emit, &output_control_point);
12034 emit_src_register(emit, &input_control_point);
12035 end_emit_instruction(emit);
12036 }
12037 }
12038
12039 /**
12040 * This functions constructs temporary tessfactor from VGPU10*_TESSFACTOR
12041 * values in domain shader. SM5 has tessfactors as floating point values where
12042 * as tgsi emit them as vector. This function allows to construct temp
12043 * tessfactor vector similar to TGSI_SEMANTIC_TESSINNER/OUTER filled with
12044 * values from VGPU10*_TESSFACTOR. Use this constructed vector whenever
12045 * TGSI_SEMANTIC_TESSINNER/OUTER is used in shader.
12046 */
12047 static void
12048 emit_temp_tessfactor_instructions(struct svga_shader_emitter_v10 *emit)
12049 {
12050 struct tgsi_full_src_register src;
12051 struct tgsi_full_dst_register dst;
12052
12053 if (emit->tes.inner.tgsi_index != INVALID_INDEX) {
12054 dst = make_dst_temp_reg(emit->tes.inner.temp_index);
12055
12056 switch (emit->tes.prim_mode) {
12057 case MESA_PRIM_QUADS:
12058 src = make_src_scalar_reg(TGSI_FILE_INPUT,
12059 emit->tes.inner.in_index + 1, TGSI_SWIZZLE_X);
12060 dst = writemask_dst(&dst, TGSI_WRITEMASK_Y);
12061 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst, &src);
12062 FALLTHROUGH;
12063 case MESA_PRIM_TRIANGLES:
12064 src = make_src_scalar_reg(TGSI_FILE_INPUT,
12065 emit->tes.inner.in_index, TGSI_SWIZZLE_X);
12066 dst = writemask_dst(&dst, TGSI_WRITEMASK_X);
12067 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst, &src);
12068 break;
12069 case MESA_PRIM_LINES:
12070 /**
12071 * As per SM5 spec, InsideTessFactor for isolines are unused.
12072 * In fact glsl tessInnerLevel for isolines doesn't mean anything but if
12073 * any application try to read tessInnerLevel in TES when primitive type
12074 * is isolines, then instead of driver throwing segfault for accesing it,
12075 * return atleast vec(1.0f)
12076 */
12077 src = make_immediate_reg_float(emit, 1.0f);
12078 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst, &src);
12079 break;
12080 default:
12081 break;
12082 }
12083 }
12084
12085 if (emit->tes.outer.tgsi_index != INVALID_INDEX) {
12086 dst = make_dst_temp_reg(emit->tes.outer.temp_index);
12087
12088 switch (emit->tes.prim_mode) {
12089 case MESA_PRIM_QUADS:
12090 src = make_src_scalar_reg(TGSI_FILE_INPUT,
12091 emit->tes.outer.in_index + 3, TGSI_SWIZZLE_X);
12092 dst = writemask_dst(&dst, TGSI_WRITEMASK_W);
12093 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst, &src);
12094 FALLTHROUGH;
12095 case MESA_PRIM_TRIANGLES:
12096 src = make_src_scalar_reg(TGSI_FILE_INPUT,
12097 emit->tes.outer.in_index + 2, TGSI_SWIZZLE_X);
12098 dst = writemask_dst(&dst, TGSI_WRITEMASK_Z);
12099 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst, &src);
12100 FALLTHROUGH;
12101 case MESA_PRIM_LINES:
12102 src = make_src_scalar_reg(TGSI_FILE_INPUT,
12103 emit->tes.outer.in_index + 1, TGSI_SWIZZLE_X);
12104 dst = writemask_dst(&dst, TGSI_WRITEMASK_Y);
12105 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst, &src);
12106
12107 src = make_src_scalar_reg(TGSI_FILE_INPUT,
12108 emit->tes.outer.in_index , TGSI_SWIZZLE_X);
12109 dst = writemask_dst(&dst, TGSI_WRITEMASK_X);
12110 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst, &src);
12111
12112 break;
12113 default:
12114 break;
12115 }
12116 }
12117 }
12118
12119
12120 static void
12121 emit_initialize_temp_instruction(struct svga_shader_emitter_v10 *emit)
12122 {
12123 struct tgsi_full_src_register src;
12124 struct tgsi_full_dst_register dst;
12125 unsigned vgpu10_temp_index = remap_temp_index(emit, TGSI_FILE_TEMPORARY,
12126 emit->initialize_temp_index);
12127 src = make_immediate_reg_float(emit, 0.0f);
12128 dst = make_dst_temp_reg(vgpu10_temp_index);
12129 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &dst, &src);
12130 emit->temp_map[emit->initialize_temp_index].initialized = true;
12131 emit->initialize_temp_index = INVALID_INDEX;
12132 }
12133
12134
12135 /**
12136 * Emit any extra/helper declarations/code that we might need between
12137 * the declaration section and code section.
12138 */
12139 static bool
12140 emit_pre_helpers(struct svga_shader_emitter_v10 *emit)
12141 {
12142 /* Properties */
12143 if (emit->unit == MESA_SHADER_GEOMETRY)
12144 emit_property_instructions(emit);
12145 else if (emit->unit == MESA_SHADER_TESS_CTRL) {
12146 emit_hull_shader_declarations(emit);
12147
12148 /* Save the position of the first instruction token so that we can
12149 * do a second pass of the instructions for the patch constant phase.
12150 */
12151 emit->tcs.instruction_token_pos = emit->cur_tgsi_token;
12152 emit->tcs.fork_phase_add_signature = false;
12153
12154 if (!emit_hull_shader_control_point_phase(emit)) {
12155 emit->skip_instruction = true;
12156 return true;
12157 }
12158
12159 /* Set the current tcs phase to control point phase */
12160 emit->tcs.control_point_phase = true;
12161 }
12162 else if (emit->unit == MESA_SHADER_TESS_EVAL) {
12163 emit_domain_shader_declarations(emit);
12164 }
12165 else if (emit->unit == MESA_SHADER_COMPUTE) {
12166 emit_compute_shader_declarations(emit);
12167 }
12168
12169 /* Declare inputs */
12170 if (!emit_input_declarations(emit))
12171 return false;
12172
12173 /* Declare outputs */
12174 if (!emit_output_declarations(emit))
12175 return false;
12176
12177 /* Declare temporary registers */
12178 emit_temporaries_declaration(emit);
12179
12180 /* For MESA_SHADER_TESS_CTRL, constants, samplers, resources and immediates
12181 * will already be declared in hs_decls (emit_hull_shader_declarations)
12182 */
12183 if (emit->unit != MESA_SHADER_TESS_CTRL) {
12184
12185 alloc_common_immediates(emit);
12186
12187 /* Declare constant registers */
12188 emit_constant_declaration(emit);
12189
12190 /* Declare samplers and resources */
12191 emit_sampler_declarations(emit);
12192 emit_resource_declarations(emit);
12193
12194 /* Declare images */
12195 emit_image_declarations(emit);
12196
12197 /* Declare shader buffers */
12198 emit_shader_buf_declarations(emit);
12199
12200 /* Declare atomic buffers */
12201 emit_atomic_buf_declarations(emit);
12202 }
12203
12204 if (emit->unit != MESA_SHADER_FRAGMENT &&
12205 emit->unit != MESA_SHADER_COMPUTE) {
12206 /*
12207 * Declare clip distance output registers for ClipVertex or
12208 * user defined planes
12209 */
12210 emit_clip_distance_declarations(emit);
12211 }
12212
12213 if (emit->unit == MESA_SHADER_COMPUTE) {
12214 emit_memory_declarations(emit);
12215
12216 if (emit->cs.grid_size.tgsi_index != INVALID_INDEX) {
12217 emit->cs.grid_size.imm_index =
12218 alloc_immediate_int4(emit,
12219 emit->key.cs.grid_size[0],
12220 emit->key.cs.grid_size[1],
12221 emit->key.cs.grid_size[2], 0);
12222 }
12223 }
12224
12225 if (emit->unit == MESA_SHADER_FRAGMENT &&
12226 emit->key.fs.alpha_func != SVGA3D_CMP_ALWAYS) {
12227 float alpha = emit->key.fs.alpha_ref;
12228 emit->fs.alpha_ref_index =
12229 alloc_immediate_float4(emit, alpha, alpha, alpha, alpha);
12230 }
12231
12232 if (emit->unit != MESA_SHADER_TESS_CTRL) {
12233 /**
12234 * For MESA_SHADER_TESS_CTRL, immediates are already declared in
12235 * hs_decls
12236 */
12237 emit_vgpu10_immediates_block(emit);
12238 }
12239 else {
12240 emit_tcs_default_control_point_output(emit);
12241 }
12242
12243 if (emit->unit == MESA_SHADER_FRAGMENT) {
12244 emit_frontface_instructions(emit);
12245 emit_fragcoord_instructions(emit);
12246 emit_sample_position_instructions(emit);
12247 emit_default_layer_instructions(emit);
12248 }
12249 else if (emit->unit == MESA_SHADER_VERTEX) {
12250 emit_vertex_attrib_instructions(emit);
12251
12252 if (emit->info.uses_vertexid)
12253 emit_vertex_id_nobase_instruction(emit);
12254 }
12255 else if (emit->unit == MESA_SHADER_TESS_EVAL) {
12256 emit_temp_tessfactor_instructions(emit);
12257 }
12258
12259 /**
12260 * For geometry shader that writes to viewport index, the prescale
12261 * temporaries will be done at the first vertex emission.
12262 */
12263 if (emit->vposition.need_prescale && emit->vposition.num_prescale == 1)
12264 emit_temp_prescale_instructions(emit);
12265
12266 return true;
12267 }
12268
12269
12270 /**
12271 * The device has no direct support for the pipe_blend_state::alpha_to_one
12272 * option so we implement it here with shader code.
12273 *
12274 * Note that this is kind of pointless, actually. Here we're clobbering
12275 * the alpha value with 1.0. So if alpha-to-coverage is enabled, we'll wind
12276 * up with 100% coverage. That's almost certainly not what the user wants.
12277 * The work-around is to add extra shader code to compute coverage from alpha
12278 * and write it to the coverage output register (if the user's shader doesn't
12279 * do so already). We'll probably do that in the future.
12280 */
12281 static void
12282 emit_alpha_to_one_instructions(struct svga_shader_emitter_v10 *emit,
12283 unsigned fs_color_tmp_index)
12284 {
12285 struct tgsi_full_src_register one = make_immediate_reg_float(emit, 1.0f);
12286
12287 /* Note: it's not 100% clear from the spec if we're supposed to clobber
12288 * the alpha for all render targets. But that's what NVIDIA does and
12289 * that's what Piglit tests.
12290 */
12291 for (unsigned i = 0; i < emit->fs.num_color_outputs; i++) {
12292 struct tgsi_full_dst_register color_dst;
12293
12294 if (fs_color_tmp_index != INVALID_INDEX && i == 0) {
12295 /* write to the temp color register */
12296 color_dst = make_dst_temp_reg(fs_color_tmp_index);
12297 }
12298 else {
12299 /* write directly to the color[i] output */
12300 color_dst = make_dst_output_reg(emit->fs.color_out_index[i]);
12301 }
12302
12303 color_dst = writemask_dst(&color_dst, TGSI_WRITEMASK_W);
12304
12305 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &color_dst, &one);
12306 }
12307 }
12308
12309
12310 /**
12311 * Emit alpha test code. This compares TEMP[fs_color_tmp_index].w
12312 * against the alpha reference value and discards the fragment if the
12313 * comparison fails.
12314 */
12315 static void
12316 emit_alpha_test_instructions(struct svga_shader_emitter_v10 *emit,
12317 unsigned fs_color_tmp_index)
12318 {
12319 /* compare output color's alpha to alpha ref and discard if comparison
12320 * fails.
12321 */
12322 unsigned tmp = get_temp_index(emit);
12323 struct tgsi_full_src_register tmp_src = make_src_temp_reg(tmp);
12324 struct tgsi_full_src_register tmp_src_x =
12325 scalar_src(&tmp_src, TGSI_SWIZZLE_X);
12326 struct tgsi_full_dst_register tmp_dst = make_dst_temp_reg(tmp);
12327 struct tgsi_full_src_register color_src =
12328 make_src_temp_reg(fs_color_tmp_index);
12329 struct tgsi_full_src_register color_src_w =
12330 scalar_src(&color_src, TGSI_SWIZZLE_W);
12331 struct tgsi_full_src_register ref_src =
12332 make_src_immediate_reg(emit->fs.alpha_ref_index);
12333 struct tgsi_full_dst_register color_dst =
12334 make_dst_output_reg(emit->fs.color_out_index[0]);
12335
12336 assert(emit->unit == MESA_SHADER_FRAGMENT);
12337
12338 /* dst = src0 'alpha_func' src1 */
12339 emit_comparison(emit, emit->key.fs.alpha_func, &tmp_dst,
12340 &color_src_w, &ref_src);
12341
12342 /* DISCARD if dst.x == 0 */
12343 begin_emit_instruction(emit);
12344 emit_discard_opcode(emit, false); /* discard if src0.x is zero */
12345 emit_src_register(emit, &tmp_src_x);
12346 end_emit_instruction(emit);
12347
12348 /* If we don't need to broadcast the color below, emit the final color here.
12349 */
12350 if (emit->key.fs.write_color0_to_n_cbufs <= 1) {
12351 /* MOV output.color, tempcolor */
12352 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &color_dst, &color_src);
12353 }
12354
12355 free_temp_indexes(emit);
12356 }
12357
12358
12359 /**
12360 * Emit instructions for writing a single color output to multiple
12361 * color buffers.
12362 * This is used when the TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS (or
12363 * when key.fs.white_fragments is true).
12364 * property is set and the number of render targets is greater than one.
12365 * \param fs_color_tmp_index index of the temp register that holds the
12366 * color to broadcast.
12367 */
12368 static void
12369 emit_broadcast_color_instructions(struct svga_shader_emitter_v10 *emit,
12370 unsigned fs_color_tmp_index)
12371 {
12372 const unsigned n = emit->key.fs.write_color0_to_n_cbufs;
12373 struct tgsi_full_src_register color_src;
12374
12375 if (emit->key.fs.white_fragments) {
12376 /* set all color outputs to white */
12377 color_src = make_immediate_reg_float(emit, 1.0f);
12378 }
12379 else {
12380 /* set all color outputs to TEMP[fs_color_tmp_index] */
12381 assert(fs_color_tmp_index != INVALID_INDEX);
12382 color_src = make_src_temp_reg(fs_color_tmp_index);
12383 }
12384
12385 assert(emit->unit == MESA_SHADER_FRAGMENT);
12386
12387 for (unsigned i = 0; i < n; i++) {
12388 unsigned output_reg = emit->fs.color_out_index[i];
12389 struct tgsi_full_dst_register color_dst =
12390 make_dst_output_reg(output_reg);
12391
12392 /* Fill in this semantic here since we'll use it later in
12393 * emit_dst_register().
12394 */
12395 emit->info.output_semantic_name[output_reg] = TGSI_SEMANTIC_COLOR;
12396
12397 /* MOV output.color[i], tempcolor */
12398 emit_instruction_op1(emit, VGPU10_OPCODE_MOV, &color_dst, &color_src);
12399 }
12400 }
12401
12402
12403 /**
12404 * Emit extra helper code after the original shader code, but before the
12405 * last END/RET instruction.
12406 * For vertex shaders this means emitting the extra code to apply the
12407 * prescale scale/translation.
12408 */
12409 static bool
12410 emit_post_helpers(struct svga_shader_emitter_v10 *emit)
12411 {
12412 if (emit->unit == MESA_SHADER_VERTEX) {
12413 emit_vertex_instructions(emit);
12414 }
12415 else if (emit->unit == MESA_SHADER_FRAGMENT) {
12416 const unsigned fs_color_tmp_index = emit->fs.color_tmp_index;
12417
12418 assert(!(emit->key.fs.white_fragments &&
12419 emit->key.fs.write_color0_to_n_cbufs == 0));
12420
12421 /* We no longer want emit_dst_register() to substitute the
12422 * temporary fragment color register for the real color output.
12423 */
12424 emit->fs.color_tmp_index = INVALID_INDEX;
12425
12426 if (emit->key.fs.alpha_to_one) {
12427 emit_alpha_to_one_instructions(emit, fs_color_tmp_index);
12428 }
12429 if (emit->key.fs.alpha_func != SVGA3D_CMP_ALWAYS) {
12430 emit_alpha_test_instructions(emit, fs_color_tmp_index);
12431 }
12432 if (emit->key.fs.write_color0_to_n_cbufs > 1 ||
12433 emit->key.fs.white_fragments) {
12434 emit_broadcast_color_instructions(emit, fs_color_tmp_index);
12435 }
12436 }
12437 else if (emit->unit == MESA_SHADER_TESS_CTRL) {
12438 if (!emit->tcs.control_point_phase) {
12439 /* store the tessellation levels in the patch constant phase only */
12440 store_tesslevels(emit);
12441 }
12442 else {
12443 emit_clipping_instructions(emit);
12444 }
12445 }
12446 else if (emit->unit == MESA_SHADER_TESS_EVAL) {
12447 emit_vertex_instructions(emit);
12448 }
12449
12450 return true;
12451 }
12452
12453
12454 /**
12455 * Reemit rawbuf instruction
12456 */
12457 static bool
12458 emit_rawbuf_instruction(struct svga_shader_emitter_v10 *emit,
12459 unsigned inst_number,
12460 const struct tgsi_full_instruction *inst)
12461 {
12462 bool ret;
12463
12464 /* For all the rawbuf references in this instruction,
12465 * load the rawbuf reference and assign to the designated temporary.
12466 * Then reeemit the instruction.
12467 */
12468 emit->reemit_rawbuf_instruction = REEMIT_IN_PROGRESS;
12469
12470 unsigned offset_tmp = get_temp_index(emit);
12471 struct tgsi_full_dst_register offset_dst = make_dst_temp_reg(offset_tmp);
12472 struct tgsi_full_src_register offset_src = make_src_temp_reg(offset_tmp);
12473 struct tgsi_full_src_register four = make_immediate_reg_int(emit, 4);
12474
12475 for (unsigned i = 0; i < emit->raw_buf_cur_tmp_index; i++) {
12476 struct tgsi_full_src_register element_src;
12477
12478 /* First get the element index register. */
12479
12480 if (emit->raw_buf_tmp[i].indirect) {
12481 unsigned tmp = get_temp_index(emit);
12482 struct tgsi_full_dst_register element_dst = make_dst_temp_reg(tmp);
12483 struct tgsi_full_src_register element_index =
12484 make_src_temp_reg(emit->raw_buf_tmp[i].element_index);
12485 struct tgsi_full_src_register element_rel =
12486 make_immediate_reg_int(emit, emit->raw_buf_tmp[i].element_rel);
12487
12488 element_src = make_src_temp_reg(tmp);
12489 element_src = scalar_src(&element_src, TGSI_SWIZZLE_X);
12490 element_dst = writemask_dst(&element_dst, TGSI_WRITEMASK_X);
12491
12492 /* element index from the indirect register */
12493 element_index = make_src_temp_reg(emit->raw_buf_tmp[i].element_index);
12494 element_index = scalar_src(&element_index, TGSI_SWIZZLE_X);
12495
12496 /* IADD element_src element_index element_index_relative */
12497 emit_instruction_op2(emit, VGPU10_OPCODE_IADD, &element_dst,
12498 &element_index, &element_rel);
12499 }
12500 else {
12501 unsigned element_index = emit->raw_buf_tmp[i].element_index;
12502 union tgsi_immediate_data imm;
12503 imm.Int = element_index;
12504 int immpos = find_immediate(emit, imm, 0);
12505 if (immpos < 0) {
12506 UNUSED unsigned element_index_imm =
12507 add_immediate_int(emit, element_index);
12508 }
12509 element_src = make_immediate_reg_int(emit, element_index);
12510 }
12511
12512 /* byte offset = element index << 4 */
12513 emit_instruction_op2(emit, VGPU10_OPCODE_ISHL, &offset_dst,
12514 &element_src, &four);
12515
12516 struct tgsi_full_dst_register dst_tmp =
12517 make_dst_temp_reg(i + emit->raw_buf_tmp_index);
12518
12519 /* LD_RAW tmp, rawbuf byte offset, rawbuf */
12520
12521 begin_emit_instruction(emit);
12522 emit_opcode(emit, VGPU10_OPCODE_LD_RAW, false);
12523 emit_dst_register(emit, &dst_tmp);
12524
12525 struct tgsi_full_src_register offset_x =
12526 scalar_src(&offset_src, TGSI_SWIZZLE_X);
12527 emit_src_register(emit, &offset_x);
12528
12529 emit_resource_register(emit,
12530 emit->raw_buf_tmp[i].buffer_index + emit->raw_buf_srv_start_index);
12531 end_emit_instruction(emit);
12532 }
12533
12534 emit->raw_buf_cur_tmp_index = 0;
12535
12536 ret = emit_vgpu10_instruction(emit, inst_number, inst);
12537
12538 /* reset raw buf state */
12539 emit->raw_buf_cur_tmp_index = 0;
12540 emit->reemit_rawbuf_instruction = REEMIT_FALSE;
12541
12542 free_temp_indexes(emit);
12543
12544 return ret;
12545 }
12546
12547
12548 /**
12549 * Translate the TGSI tokens into VGPU10 tokens.
12550 */
12551 static bool
12552 emit_vgpu10_instructions(struct svga_shader_emitter_v10 *emit,
12553 const struct tgsi_token *tokens)
12554 {
12555 struct tgsi_parse_context parse;
12556 bool ret = true;
12557 bool pre_helpers_emitted = false;
12558 unsigned inst_number = 0;
12559
12560 tgsi_parse_init(&parse, tokens);
12561
12562 while (!tgsi_parse_end_of_tokens(&parse)) {
12563
12564 /* Save the current tgsi token starting position */
12565 emit->cur_tgsi_token = parse.Position;
12566
12567 tgsi_parse_token(&parse);
12568
12569 switch (parse.FullToken.Token.Type) {
12570 case TGSI_TOKEN_TYPE_IMMEDIATE:
12571 ret = emit_vgpu10_immediate(emit, &parse.FullToken.FullImmediate);
12572 if (!ret)
12573 goto done;
12574 break;
12575
12576 case TGSI_TOKEN_TYPE_DECLARATION:
12577 ret = emit_vgpu10_declaration(emit, &parse.FullToken.FullDeclaration);
12578 if (!ret)
12579 goto done;
12580 break;
12581
12582 case TGSI_TOKEN_TYPE_INSTRUCTION:
12583 if (!pre_helpers_emitted) {
12584 ret = emit_pre_helpers(emit);
12585 if (!ret)
12586 goto done;
12587 pre_helpers_emitted = true;
12588 }
12589 ret = emit_vgpu10_instruction(emit, inst_number++,
12590 &parse.FullToken.FullInstruction);
12591
12592 /* Usually this applies to TCS only. If shader is reading control
12593 * point outputs in control point phase, we should reemit all
12594 * instructions which are writting into control point output in
12595 * control phase to store results into temporaries.
12596 */
12597 if (emit->reemit_instruction) {
12598 assert(emit->unit == MESA_SHADER_TESS_CTRL);
12599 ret = emit_vgpu10_instruction(emit, inst_number,
12600 &parse.FullToken.FullInstruction);
12601 }
12602 else if (emit->initialize_temp_index != INVALID_INDEX) {
12603 emit_initialize_temp_instruction(emit);
12604 emit->initialize_temp_index = INVALID_INDEX;
12605 ret = emit_vgpu10_instruction(emit, inst_number - 1,
12606 &parse.FullToken.FullInstruction);
12607 }
12608 else if (emit->reemit_rawbuf_instruction) {
12609 ret = emit_rawbuf_instruction(emit, inst_number - 1,
12610 &parse.FullToken.FullInstruction);
12611 }
12612
12613 if (!ret)
12614 goto done;
12615 break;
12616
12617 case TGSI_TOKEN_TYPE_PROPERTY:
12618 ret = emit_vgpu10_property(emit, &parse.FullToken.FullProperty);
12619 if (!ret)
12620 goto done;
12621 break;
12622
12623 default:
12624 break;
12625 }
12626 }
12627
12628 if (emit->unit == MESA_SHADER_TESS_CTRL) {
12629 ret = emit_hull_shader_patch_constant_phase(emit, &parse);
12630 }
12631
12632 done:
12633 tgsi_parse_free(&parse);
12634 return ret;
12635 }
12636
12637
12638 /**
12639 * Emit the first VGPU10 shader tokens.
12640 */
12641 static bool
12642 emit_vgpu10_header(struct svga_shader_emitter_v10 *emit)
12643 {
12644 VGPU10OpcodeToken0 token;
12645 VGPU10ProgramToken ptoken;
12646
12647 /* First token: VGPU10ProgramToken (version info, program type (VS,GS,PS)) */
12648
12649 /* Maximum supported shader version is 50 */
12650 unsigned version = MIN2(emit->version, 50);
12651
12652 ptoken.value = 0; /* init whole token to zero */
12653 ptoken.majorVersion = version / 10;
12654 ptoken.minorVersion = version % 10;
12655 ptoken.programType = translate_shader_type(emit->unit);
12656 if (!emit_dword(emit, ptoken.value))
12657 return false;
12658
12659 /* Second token: total length of shader, in tokens. We can't fill this
12660 * in until we're all done. Emit zero for now.
12661 */
12662 if (!emit_dword(emit, 0))
12663 return false;
12664
12665 if (emit->version >= 50) {
12666
12667 if (emit->unit == MESA_SHADER_TESS_CTRL) {
12668 /* For hull shader, we need to start the declarations phase first before
12669 * emitting any declarations including the global flags.
12670 */
12671 token.value = 0;
12672 token.opcodeType = VGPU10_OPCODE_HS_DECLS;
12673 begin_emit_instruction(emit);
12674 emit_dword(emit, token.value);
12675 end_emit_instruction(emit);
12676 }
12677 }
12678
12679 /* Reserve token for global flags such as refactoringAllowed that we
12680 * determine as we scan through the shader. Fixed up in emit_vgpu10_tail.
12681 */
12682 emit->reserved_token = (emit->ptr - emit->buf) / sizeof(VGPU10OpcodeToken0);
12683
12684 token.value = 0;
12685 token.opcodeType = VGPU10_OPCODE_DCL_GLOBAL_FLAGS;
12686 token.instructionLength = 1;
12687 token.enableDoublePrecisionFloatOps = (version >= 50);
12688 return emit_dword(emit, token.value);
12689 }
12690
12691
12692 static bool
12693 emit_vgpu10_tail(struct svga_shader_emitter_v10 *emit)
12694 {
12695 VGPU10ProgramToken *tokens;
12696
12697 /* Replace the second token with total shader length */
12698 tokens = (VGPU10ProgramToken *) emit->buf;
12699 tokens[1].value = emit_get_num_tokens(emit);
12700
12701 /* Fixup global decls flags token with the RefactoringAllowed global flag */
12702 VGPU10OpcodeToken0 *ptoken;
12703 ptoken = (VGPU10OpcodeToken0 *)&tokens[emit->reserved_token];
12704 ptoken->refactoringAllowed = !emit->uses_precise_qualifier;
12705 ptoken->forceEarlyDepthStencil = (emit->version >= 50 && emit->fs.forceEarlyDepthStencil);
12706
12707 return true;
12708 }
12709
12710
12711 /**
12712 * Modify the FS to read the BCOLORs and use the FACE register
12713 * to choose between the front/back colors.
12714 */
12715 static const struct tgsi_token *
12716 transform_fs_twoside(const struct tgsi_token *tokens)
12717 {
12718 if (0) {
12719 debug_printf("Before tgsi_add_two_side ------------------\n");
12720 tgsi_dump(tokens,0);
12721 }
12722 tokens = tgsi_add_two_side(tokens);
12723 if (0) {
12724 debug_printf("After tgsi_add_two_side ------------------\n");
12725 tgsi_dump(tokens, 0);
12726 }
12727 return tokens;
12728 }
12729
12730
12731 /**
12732 * Modify the FS to do polygon stipple.
12733 */
12734 static const struct tgsi_token *
12735 transform_fs_pstipple(struct svga_shader_emitter_v10 *emit,
12736 const struct tgsi_token *tokens)
12737 {
12738 const struct tgsi_token *new_tokens;
12739 unsigned unit;
12740
12741 if (0) {
12742 debug_printf("Before pstipple ------------------\n");
12743 tgsi_dump(tokens,0);
12744 }
12745
12746 new_tokens = util_pstipple_create_fragment_shader(tokens, &unit, 0,
12747 TGSI_FILE_INPUT);
12748
12749 emit->fs.pstipple_sampler_unit = unit;
12750
12751 /* The new sampler state is appended to the end of the samplers list */
12752 emit->fs.pstipple_sampler_state_index = emit->key.num_samplers++;
12753
12754 /* Setup texture state for stipple */
12755 emit->sampler_target[unit] = TGSI_TEXTURE_2D;
12756 emit->key.tex[unit].swizzle_r = TGSI_SWIZZLE_X;
12757 emit->key.tex[unit].swizzle_g = TGSI_SWIZZLE_Y;
12758 emit->key.tex[unit].swizzle_b = TGSI_SWIZZLE_Z;
12759 emit->key.tex[unit].swizzle_a = TGSI_SWIZZLE_W;
12760 emit->key.tex[unit].target = PIPE_TEXTURE_2D;
12761 emit->key.tex[unit].sampler_index = emit->fs.pstipple_sampler_state_index;
12762
12763 if (0) {
12764 debug_printf("After pstipple ------------------\n");
12765 tgsi_dump(new_tokens, 0);
12766 }
12767
12768 return new_tokens;
12769 }
12770
12771 /**
12772 * Modify the FS to support anti-aliasing point.
12773 */
12774 static const struct tgsi_token *
12775 transform_fs_aapoint(struct svga_context *svga,
12776 const struct tgsi_token *tokens,
12777 int aa_coord_index)
12778 {
12779 bool need_texcoord_semantic =
12780 svga->pipe.screen->caps.tgsi_texcoord;
12781
12782 if (0) {
12783 debug_printf("Before tgsi_add_aa_point ------------------\n");
12784 tgsi_dump(tokens,0);
12785 }
12786 tokens = tgsi_add_aa_point(tokens, aa_coord_index, need_texcoord_semantic);
12787 if (0) {
12788 debug_printf("After tgsi_add_aa_point ------------------\n");
12789 tgsi_dump(tokens, 0);
12790 }
12791 return tokens;
12792 }
12793
12794
12795 /**
12796 * A helper function to determine the shader in the previous stage and
12797 * then call the linker function to determine the input mapping for this
12798 * shader to match the output indices from the shader in the previous stage.
12799 */
12800 static void
12801 compute_input_mapping(struct svga_context *svga,
12802 struct svga_shader_emitter_v10 *emit,
12803 mesa_shader_stage unit)
12804 {
12805 struct svga_shader *prevShader = NULL; /* shader in the previous stage */
12806
12807 if (unit == MESA_SHADER_FRAGMENT) {
12808 prevShader = svga->curr.gs ?
12809 &svga->curr.gs->base : (svga->curr.tes ?
12810 &svga->curr.tes->base : &svga->curr.vs->base);
12811 } else if (unit == MESA_SHADER_GEOMETRY) {
12812 prevShader = svga->curr.tes ? &svga->curr.tes->base : &svga->curr.vs->base;
12813 } else if (unit == MESA_SHADER_TESS_EVAL) {
12814 assert(svga->curr.tcs);
12815 prevShader = &svga->curr.tcs->base;
12816 } else if (unit == MESA_SHADER_TESS_CTRL) {
12817 assert(svga->curr.vs);
12818 prevShader = &svga->curr.vs->base;
12819 }
12820
12821 if (prevShader != NULL) {
12822 svga_link_shaders(&prevShader->tgsi_info, &emit->info, &emit->linkage);
12823 emit->prevShaderInfo = &prevShader->tgsi_info;
12824 } else {
12825 /**
12826 * Since vertex shader does not need to go through the linker to
12827 * establish the input map, we need to make sure the highest index
12828 * of input registers is set properly here.
12829 */
12830 emit->linkage.input_map_max = MAX2((int)emit->linkage.input_map_max,
12831 emit->info.file_max[TGSI_FILE_INPUT]);
12832 }
12833 }
12834
12835
12836 /**
12837 * Copies the shader signature info to the shader variant
12838 */
12839 static void
12840 copy_shader_signature(struct svga_shader_signature *sgn,
12841 struct svga_shader_variant *variant)
12842 {
12843 SVGA3dDXShaderSignatureHeader *header = &sgn->header;
12844
12845 /* Calculate the signature length */
12846 variant->signatureLen = sizeof(SVGA3dDXShaderSignatureHeader) +
12847 (header->numInputSignatures +
12848 header->numOutputSignatures +
12849 header->numPatchConstantSignatures) *
12850 sizeof(SVGA3dDXShaderSignatureEntry);
12851
12852 /* Allocate buffer for the signature info */
12853 variant->signature =
12854 (SVGA3dDXShaderSignatureHeader *)CALLOC(1, variant->signatureLen);
12855
12856 char *sgnBuf = (char *)variant->signature;
12857 unsigned sgnLen;
12858
12859 /* Copy the signature info to the shader variant structure */
12860 memcpy(sgnBuf, &sgn->header, sizeof(SVGA3dDXShaderSignatureHeader));
12861 sgnBuf += sizeof(SVGA3dDXShaderSignatureHeader);
12862
12863 if (header->numInputSignatures) {
12864 sgnLen =
12865 header->numInputSignatures * sizeof(SVGA3dDXShaderSignatureEntry);
12866 memcpy(sgnBuf, &sgn->inputs[0], sgnLen);
12867 sgnBuf += sgnLen;
12868 }
12869
12870 if (header->numOutputSignatures) {
12871 sgnLen =
12872 header->numOutputSignatures * sizeof(SVGA3dDXShaderSignatureEntry);
12873 memcpy(sgnBuf, &sgn->outputs[0], sgnLen);
12874 sgnBuf += sgnLen;
12875 }
12876
12877 if (header->numPatchConstantSignatures) {
12878 sgnLen =
12879 header->numPatchConstantSignatures * sizeof(SVGA3dDXShaderSignatureEntry);
12880 memcpy(sgnBuf, &sgn->patchConstants[0], sgnLen);
12881 }
12882 }
12883
12884
12885 /**
12886 * This is the main entrypoint for the TGSI -> VPGU10 translator.
12887 */
12888 struct svga_shader_variant *
12889 svga_tgsi_vgpu10_translate(struct svga_context *svga,
12890 const struct svga_shader *shader,
12891 const struct svga_compile_key *key,
12892 mesa_shader_stage unit)
12893 {
12894 struct svga_screen *svgascreen = svga_screen(svga->pipe.screen);
12895 struct svga_shader_variant *variant = NULL;
12896 struct svga_shader_emitter_v10 *emit;
12897 const struct tgsi_token *tokens = shader->tokens;
12898
12899 (void) make_immediate_reg_double; /* unused at this time */
12900
12901 assert(unit == MESA_SHADER_VERTEX ||
12902 unit == MESA_SHADER_GEOMETRY ||
12903 unit == MESA_SHADER_FRAGMENT ||
12904 unit == MESA_SHADER_TESS_CTRL ||
12905 unit == MESA_SHADER_TESS_EVAL ||
12906 unit == MESA_SHADER_COMPUTE);
12907
12908 /* These two flags cannot be used together */
12909 assert(key->vs.need_prescale + key->vs.undo_viewport <= 1);
12910
12911 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_TGSIVGPU10TRANSLATE);
12912 /*
12913 * Setup the code emitter
12914 */
12915 emit = alloc_emitter();
12916 if (!emit)
12917 goto done;
12918
12919 emit->unit = unit;
12920 if (svga_have_gl43(svga)) {
12921 emit->version = 51;
12922 } else if (svga_have_sm5(svga)) {
12923 emit->version = 50;
12924 } else if (svga_have_sm4_1(svga)) {
12925 emit->version = 41;
12926 } else {
12927 emit->version = 40;
12928 }
12929
12930 emit->use_sampler_state_mapping = emit->key.sampler_state_mapping;
12931
12932 emit->signature.header.headerVersion = SVGADX_SIGNATURE_HEADER_VERSION_0;
12933
12934 emit->key = *key;
12935
12936 emit->vposition.need_prescale = (emit->key.vs.need_prescale ||
12937 emit->key.gs.need_prescale ||
12938 emit->key.tes.need_prescale);
12939
12940 /* Determine how many prescale factors in the constant buffer */
12941 emit->vposition.num_prescale = 1;
12942 if (emit->vposition.need_prescale && emit->key.gs.writes_viewport_index) {
12943 assert(emit->unit == MESA_SHADER_GEOMETRY);
12944 emit->vposition.num_prescale = emit->key.gs.num_prescale;
12945 }
12946
12947 emit->vposition.tmp_index = INVALID_INDEX;
12948 emit->vposition.so_index = INVALID_INDEX;
12949 emit->vposition.out_index = INVALID_INDEX;
12950
12951 emit->vs.vertex_id_sys_index = INVALID_INDEX;
12952 emit->vs.vertex_id_tmp_index = INVALID_INDEX;
12953 emit->vs.vertex_id_bias_index = INVALID_INDEX;
12954
12955 emit->fs.color_tmp_index = INVALID_INDEX;
12956 emit->fs.face_input_index = INVALID_INDEX;
12957 emit->fs.fragcoord_input_index = INVALID_INDEX;
12958 emit->fs.sample_id_sys_index = INVALID_INDEX;
12959 emit->fs.sample_pos_sys_index = INVALID_INDEX;
12960 emit->fs.sample_mask_in_sys_index = INVALID_INDEX;
12961 emit->fs.layer_input_index = INVALID_INDEX;
12962 emit->fs.layer_imm_index = INVALID_INDEX;
12963
12964 emit->gs.prim_id_index = INVALID_INDEX;
12965 emit->gs.invocation_id_sys_index = INVALID_INDEX;
12966 emit->gs.viewport_index_out_index = INVALID_INDEX;
12967 emit->gs.viewport_index_tmp_index = INVALID_INDEX;
12968
12969 emit->tcs.vertices_per_patch_index = INVALID_INDEX;
12970 emit->tcs.invocation_id_sys_index = INVALID_INDEX;
12971 emit->tcs.control_point_input_index = INVALID_INDEX;
12972 emit->tcs.control_point_addr_index = INVALID_INDEX;
12973 emit->tcs.control_point_out_index = INVALID_INDEX;
12974 emit->tcs.control_point_tmp_index = INVALID_INDEX;
12975 emit->tcs.control_point_out_count = 0;
12976 emit->tcs.inner.out_index = INVALID_INDEX;
12977 emit->tcs.inner.temp_index = INVALID_INDEX;
12978 emit->tcs.inner.tgsi_index = INVALID_INDEX;
12979 emit->tcs.outer.out_index = INVALID_INDEX;
12980 emit->tcs.outer.temp_index = INVALID_INDEX;
12981 emit->tcs.outer.tgsi_index = INVALID_INDEX;
12982 emit->tcs.patch_generic_out_count = 0;
12983 emit->tcs.patch_generic_out_index = INVALID_INDEX;
12984 emit->tcs.patch_generic_tmp_index = INVALID_INDEX;
12985 emit->tcs.prim_id_index = INVALID_INDEX;
12986
12987 emit->tes.tesscoord_sys_index = INVALID_INDEX;
12988 emit->tes.inner.in_index = INVALID_INDEX;
12989 emit->tes.inner.temp_index = INVALID_INDEX;
12990 emit->tes.inner.tgsi_index = INVALID_INDEX;
12991 emit->tes.outer.in_index = INVALID_INDEX;
12992 emit->tes.outer.temp_index = INVALID_INDEX;
12993 emit->tes.outer.tgsi_index = INVALID_INDEX;
12994 emit->tes.prim_id_index = INVALID_INDEX;
12995
12996 emit->cs.thread_id_index = INVALID_INDEX;
12997 emit->cs.block_id_index = INVALID_INDEX;
12998 emit->cs.grid_size.tgsi_index = INVALID_INDEX;
12999 emit->cs.grid_size.imm_index = INVALID_INDEX;
13000 emit->cs.block_width = 1;
13001 emit->cs.block_height = 1;
13002 emit->cs.block_depth = 1;
13003
13004 emit->clip_dist_out_index = INVALID_INDEX;
13005 emit->clip_dist_tmp_index = INVALID_INDEX;
13006 emit->clip_dist_so_index = INVALID_INDEX;
13007 emit->clip_vertex_out_index = INVALID_INDEX;
13008 emit->clip_vertex_tmp_index = INVALID_INDEX;
13009 emit->svga_debug_callback = svga->debug.callback;
13010
13011 emit->index_range.start_index = INVALID_INDEX;
13012 emit->index_range.count = 0;
13013 emit->index_range.required = false;
13014 emit->index_range.operandType = VGPU10_NUM_OPERANDS;
13015 emit->index_range.dim = 0;
13016 emit->index_range.size = 0;
13017
13018 emit->current_loop_depth = 0;
13019
13020 emit->initialize_temp_index = INVALID_INDEX;
13021 emit->image_size_index = INVALID_INDEX;
13022
13023 emit->max_vs_inputs = svgascreen->max_vs_inputs;
13024 emit->max_vs_outputs = svgascreen->max_vs_outputs;
13025 emit->max_gs_inputs = svgascreen->max_gs_inputs;
13026
13027 if (emit->key.fs.alpha_func == SVGA3D_CMP_INVALID) {
13028 emit->key.fs.alpha_func = SVGA3D_CMP_ALWAYS;
13029 }
13030
13031 if (unit == MESA_SHADER_FRAGMENT) {
13032 if (key->fs.light_twoside) {
13033 tokens = transform_fs_twoside(tokens);
13034 }
13035 if (key->fs.pstipple) {
13036 const struct tgsi_token *new_tokens =
13037 transform_fs_pstipple(emit, tokens);
13038 if (tokens != shader->tokens) {
13039 /* free the two-sided shader tokens */
13040 tgsi_free_tokens(tokens);
13041 }
13042 tokens = new_tokens;
13043 }
13044 if (key->fs.aa_point) {
13045 tokens = transform_fs_aapoint(svga, tokens,
13046 key->fs.aa_point_coord_index);
13047 }
13048 }
13049
13050 if (SVGA_DEBUG & DEBUG_TGSI) {
13051 debug_printf("#####################################\n");
13052 debug_printf("### TGSI Shader %u\n", shader->id);
13053 tgsi_dump(tokens, 0);
13054 }
13055
13056 /**
13057 * Rescan the header if the token string is different from the one
13058 * included in the shader; otherwise, the header info is already up-to-date
13059 */
13060 if (tokens != shader->tokens) {
13061 tgsi_scan_shader(tokens, &emit->info);
13062 } else {
13063 emit->info = shader->tgsi_info;
13064 }
13065
13066 emit->num_outputs = emit->info.num_outputs;
13067
13068 /**
13069 * Compute input mapping to match the outputs from shader
13070 * in the previous stage
13071 */
13072 compute_input_mapping(svga, emit, unit);
13073
13074 determine_clipping_mode(emit);
13075
13076 if (unit == MESA_SHADER_GEOMETRY || unit == MESA_SHADER_VERTEX ||
13077 unit == MESA_SHADER_TESS_CTRL || unit == MESA_SHADER_TESS_EVAL) {
13078 if (shader->stream_output != NULL || emit->clip_mode == CLIP_DISTANCE) {
13079 /* if there is stream output declarations associated
13080 * with this shader or the shader writes to ClipDistance
13081 * then reserve extra registers for the non-adjusted vertex position
13082 * and the ClipDistance shadow copy.
13083 */
13084 emit->vposition.so_index = emit->num_outputs++;
13085
13086 if (emit->clip_mode == CLIP_DISTANCE) {
13087 emit->clip_dist_so_index = emit->num_outputs++;
13088 if (emit->info.num_written_clipdistance > 4)
13089 emit->num_outputs++;
13090 }
13091 }
13092 }
13093
13094 /* Determine if constbuf to rawbuf translation is needed */
13095 emit->raw_buf_srv_start_index = emit->key.srv_raw_constbuf_index;
13096 if (emit->info.const_buffers_declared)
13097 emit->raw_bufs = emit->key.raw_constbufs;
13098
13099 emit->raw_shaderbuf_srv_start_index = emit->key.srv_raw_shaderbuf_index;
13100 if (emit->info.shader_buffers_declared)
13101 emit->raw_shaderbufs = emit->key.raw_shaderbufs;
13102
13103 /*
13104 * Do actual shader translation.
13105 */
13106 if (!emit_vgpu10_header(emit)) {
13107 debug_printf("svga: emit VGPU10 header failed\n");
13108 goto cleanup;
13109 }
13110
13111 if (!emit_vgpu10_instructions(emit, tokens)) {
13112 debug_printf("svga: emit VGPU10 instructions failed\n");
13113 goto cleanup;
13114 }
13115
13116 if (emit->num_new_immediates > 0) {
13117 reemit_immediates_block(emit);
13118 }
13119
13120 if (!emit_vgpu10_tail(emit)) {
13121 debug_printf("svga: emit VGPU10 tail failed\n");
13122 goto cleanup;
13123 }
13124
13125 if (emit->register_overflow) {
13126 goto cleanup;
13127 }
13128
13129 /*
13130 * Create, initialize the 'variant' object.
13131 */
13132 variant = svga_new_shader_variant(svga, unit);
13133 if (!variant)
13134 goto cleanup;
13135
13136 variant->shader = shader;
13137 variant->nr_tokens = emit_get_num_tokens(emit);
13138 variant->tokens = (const unsigned *)emit->buf;
13139
13140 /* Copy shader signature info to the shader variant */
13141 if (svga_have_sm5(svga)) {
13142 copy_shader_signature(&emit->signature, variant);
13143 }
13144
13145 emit->buf = NULL; /* buffer is no longer owed by emitter context */
13146 memcpy(&variant->key, key, sizeof(*key));
13147 variant->id = UTIL_BITMASK_INVALID_INDEX;
13148
13149 /* The extra constant starting offset starts with the number of
13150 * shader constants declared in the shader.
13151 */
13152 variant->extra_const_start = emit->num_shader_consts[0];
13153 if (key->gs.wide_point) {
13154 /**
13155 * The extra constant added in the transformed shader
13156 * for inverse viewport scale is to be supplied by the driver.
13157 * So the extra constant starting offset needs to be reduced by 1.
13158 */
13159 assert(variant->extra_const_start > 0);
13160 variant->extra_const_start--;
13161 }
13162
13163 if (unit == MESA_SHADER_FRAGMENT) {
13164 struct svga_fs_variant *fs_variant = svga_fs_variant(variant);
13165
13166 fs_variant->pstipple_sampler_unit = emit->fs.pstipple_sampler_unit;
13167 fs_variant->pstipple_sampler_state_index =
13168 emit->fs.pstipple_sampler_state_index;
13169
13170 /* If there was exactly one write to a fragment shader output register
13171 * and it came from a constant buffer, we know all fragments will have
13172 * the same color (except for blending).
13173 */
13174 fs_variant->constant_color_output =
13175 emit->constant_color_output && emit->num_output_writes == 1;
13176
13177 /** keep track in the variant if flat interpolation is used
13178 * for any of the varyings.
13179 */
13180 fs_variant->uses_flat_interp = emit->uses_flat_interp;
13181
13182 fs_variant->fs_shadow_compare_units = emit->shadow_compare_units;
13183 }
13184 else if (unit == MESA_SHADER_TESS_EVAL) {
13185 struct svga_tes_variant *tes_variant = svga_tes_variant(variant);
13186
13187 /* Keep track in the tes variant some of the layout parameters.
13188 * These parameters will be referenced by the tcs to emit
13189 * the necessary declarations for the hull shader.
13190 */
13191 tes_variant->prim_mode = emit->tes.prim_mode;
13192 tes_variant->spacing = emit->tes.spacing;
13193 tes_variant->vertices_order_cw = emit->tes.vertices_order_cw;
13194 tes_variant->point_mode = emit->tes.point_mode;
13195 }
13196
13197
13198 if (tokens != shader->tokens) {
13199 tgsi_free_tokens(tokens);
13200 }
13201
13202 cleanup:
13203 free_emitter(emit);
13204
13205 done:
13206 SVGA_STATS_TIME_POP(svga_sws(svga));
13207 return variant;
13208 }