xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/svga/svga_tgsi.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (c) 2008-2024 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 #include "util/compiler.h"
10 #include "pipe/p_shader_tokens.h"
11 #include "pipe/p_defines.h"
12 #include "tgsi/tgsi_dump.h"
13 #include "tgsi/tgsi_scan.h"
14 #include "util/u_math.h"
15 #include "util/u_memory.h"
16 #include "util/u_bitmask.h"
17 
18 #include "svgadump/svga_shader_dump.h"
19 
20 #include "svga_context.h"
21 #include "svga_shader.h"
22 #include "svga_tgsi.h"
23 #include "svga_tgsi_emit.h"
24 #include "svga_debug.h"
25 
26 #include "svga_hw_reg.h"
27 #include "svga3d_shaderdefs.h"
28 
29 
30 /* Sinkhole used only in error conditions.
31  */
32 static char err_buf[128];
33 
34 
35 static bool
svga_shader_expand(struct svga_shader_emitter * emit)36 svga_shader_expand(struct svga_shader_emitter *emit)
37 {
38    char *new_buf;
39    unsigned newsize = emit->size * 2;
40 
41    if (emit->buf != err_buf)
42       new_buf = REALLOC(emit->buf, emit->size, newsize);
43    else
44       new_buf = NULL;
45 
46    if (!new_buf) {
47       emit->ptr = err_buf;
48       emit->buf = err_buf;
49       emit->size = sizeof(err_buf);
50       return false;
51    }
52 
53    emit->size = newsize;
54    emit->ptr = new_buf + (emit->ptr - emit->buf);
55    emit->buf = new_buf;
56    return true;
57 }
58 
59 
60 static inline bool
reserve(struct svga_shader_emitter * emit,unsigned nr_dwords)61 reserve(struct svga_shader_emitter *emit, unsigned nr_dwords)
62 {
63    if (emit->ptr - emit->buf + nr_dwords * sizeof(unsigned) >= emit->size) {
64       if (!svga_shader_expand(emit)) {
65          return false;
66       }
67    }
68 
69    return true;
70 }
71 
72 
73 bool
svga_shader_emit_dword(struct svga_shader_emitter * emit,unsigned dword)74 svga_shader_emit_dword(struct svga_shader_emitter * emit, unsigned dword)
75 {
76    if (!reserve(emit, 1))
77       return false;
78 
79    *(unsigned *) emit->ptr = dword;
80    emit->ptr += sizeof dword;
81    return true;
82 }
83 
84 
85 bool
svga_shader_emit_dwords(struct svga_shader_emitter * emit,const unsigned * dwords,unsigned nr)86 svga_shader_emit_dwords(struct svga_shader_emitter * emit,
87                         const unsigned *dwords, unsigned nr)
88 {
89    if (!reserve(emit, nr))
90       return false;
91 
92    memcpy(emit->ptr, dwords, nr * sizeof *dwords);
93    emit->ptr += nr * sizeof *dwords;
94    return true;
95 }
96 
97 
98 bool
svga_shader_emit_opcode(struct svga_shader_emitter * emit,unsigned opcode)99 svga_shader_emit_opcode(struct svga_shader_emitter * emit, unsigned opcode)
100 {
101    SVGA3dShaderInstToken *here;
102 
103    if (!reserve(emit, 1))
104       return false;
105 
106    here = (SVGA3dShaderInstToken *) emit->ptr;
107    here->value = opcode;
108 
109    if (emit->insn_offset) {
110       SVGA3dShaderInstToken *prev =
111          (SVGA3dShaderInstToken *) (emit->buf + emit->insn_offset);
112       prev->size = (here - prev) - 1;
113    }
114 
115    emit->insn_offset = emit->ptr - emit->buf;
116    emit->ptr += sizeof(unsigned);
117    return true;
118 }
119 
120 
121 static bool
svga_shader_emit_header(struct svga_shader_emitter * emit)122 svga_shader_emit_header(struct svga_shader_emitter *emit)
123 {
124    SVGA3dShaderVersion header;
125 
126    memset(&header, 0, sizeof header);
127 
128    switch (emit->unit) {
129    case PIPE_SHADER_FRAGMENT:
130       header.value = SVGA3D_PS_30;
131       break;
132    case PIPE_SHADER_VERTEX:
133       header.value = SVGA3D_VS_30;
134       break;
135    }
136 
137    return svga_shader_emit_dword(emit, header.value);
138 }
139 
140 
141 /**
142  * Parse TGSI shader and translate to SVGA/DX9 serialized
143  * representation.
144  *
145  * In this function SVGA shader is emitted to an in-memory buffer that
146  * can be dynamically grown.  Once we've finished and know how large
147  * it is, it will be copied to a hardware buffer for upload.
148  */
149 struct svga_shader_variant *
svga_tgsi_vgpu9_translate(struct svga_context * svga,const struct svga_shader * shader,const struct svga_compile_key * key,enum pipe_shader_type unit)150 svga_tgsi_vgpu9_translate(struct svga_context *svga,
151                           const struct svga_shader *shader,
152                           const struct svga_compile_key *key,
153                           enum pipe_shader_type unit)
154 {
155    struct svga_shader_variant *variant = NULL;
156    struct svga_shader_emitter emit;
157 
158    SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_TGSIVGPU9TRANSLATE);
159 
160    memset(&emit, 0, sizeof(emit));
161 
162    emit.size = 1024;
163    emit.buf = MALLOC(emit.size);
164    if (emit.buf == NULL) {
165       goto fail;
166    }
167 
168    emit.ptr = emit.buf;
169    emit.unit = unit;
170    emit.key = *key;
171 
172    tgsi_scan_shader(shader->tokens, &emit.info);
173 
174    emit.imm_start = emit.info.file_max[TGSI_FILE_CONSTANT] + 1;
175 
176    if (unit == PIPE_SHADER_FRAGMENT)
177       emit.imm_start += key->num_unnormalized_coords;
178 
179    if (unit == PIPE_SHADER_VERTEX) {
180       emit.imm_start += key->vs.need_prescale ? 2 : 0;
181    }
182 
183    emit.nr_hw_float_const =
184       (emit.imm_start + emit.info.file_max[TGSI_FILE_IMMEDIATE] + 1);
185 
186    emit.nr_hw_temp = emit.info.file_max[TGSI_FILE_TEMPORARY] + 1;
187 
188    if (emit.nr_hw_temp >= SVGA3D_TEMPREG_MAX) {
189       debug_printf("svga: too many temporary registers (%u)\n",
190                    emit.nr_hw_temp);
191       goto fail;
192    }
193 
194    if (emit.info.indirect_files & (1 << TGSI_FILE_TEMPORARY)) {
195       debug_printf(
196          "svga: indirect indexing of temporary registers is not supported.\n");
197       goto fail;
198    }
199 
200    emit.in_main_func = true;
201 
202    if (!svga_shader_emit_header(&emit)) {
203       debug_printf("svga: emit header failed\n");
204       goto fail;
205    }
206 
207    if (!svga_shader_emit_instructions(&emit, shader->tokens)) {
208       debug_printf("svga: emit instructions failed\n");
209       goto fail;
210    }
211 
212    variant = svga_new_shader_variant(svga, unit);
213    if (!variant)
214       goto fail;
215 
216    variant->shader = shader;
217    variant->tokens = (const unsigned *) emit.buf;
218    variant->nr_tokens = (emit.ptr - emit.buf) / sizeof(unsigned);
219    memcpy(&variant->key, key, sizeof(*key));
220    variant->id = UTIL_BITMASK_INVALID_INDEX;
221 
222    if (unit == PIPE_SHADER_FRAGMENT) {
223       struct svga_fs_variant *fs_variant = svga_fs_variant(variant);
224 
225       fs_variant->pstipple_sampler_unit = emit.pstipple_sampler_unit;
226 
227       /* If there was exactly one write to a fragment shader output register
228        * and it came from a constant buffer, we know all fragments will have
229        * the same color (except for blending).
230        */
231       fs_variant->constant_color_output =
232          emit.constant_color_output && emit.num_output_writes == 1;
233    }
234 
235 #if 0
236    if (!svga_shader_verify(variant->tokens, variant->nr_tokens) ||
237        SVGA_DEBUG & DEBUG_TGSI) {
238       debug_printf("#####################################\n");
239       debug_printf("Shader %u below\n", shader->id);
240       tgsi_dump(shader->tokens, 0);
241       if (SVGA_DEBUG & DEBUG_TGSI) {
242          debug_printf("Shader %u compiled below\n", shader->id);
243          svga_shader_dump(variant->tokens, variant->nr_tokens, false);
244       }
245       debug_printf("#####################################\n");
246    }
247 #endif
248 
249    goto done;
250 
251 fail:
252    FREE(variant);
253    if (emit.buf != err_buf)
254       FREE(emit.buf);
255    variant = NULL;
256 
257 done:
258    SVGA_STATS_TIME_POP(svga_sws(svga));
259    return variant;
260 }
261 
262 
263 /**
264  * Helper function to convert tgsi semantic name to vertex attribute
265  * semantic name.
266  */
267 static gl_vert_attrib
svga_tgsi_to_gl_vert_attrib_semantic(unsigned sem_name,unsigned sem_index)268 svga_tgsi_to_gl_vert_attrib_semantic(unsigned sem_name,
269                                      unsigned sem_index)
270 {
271    switch (sem_name) {
272    case TGSI_SEMANTIC_POSITION:
273       return VERT_ATTRIB_POS;
274    case TGSI_SEMANTIC_COLOR:
275       assert(sem_index <= 1);
276       return VERT_ATTRIB_COLOR0;
277    case TGSI_SEMANTIC_FOG:
278       return VERT_ATTRIB_FOG;
279    case TGSI_SEMANTIC_PSIZE:
280       return VERT_ATTRIB_POINT_SIZE;
281    case TGSI_SEMANTIC_GENERIC:
282       return VERT_ATTRIB_GENERIC0;
283    case TGSI_SEMANTIC_EDGEFLAG:
284       return VERT_ATTRIB_EDGEFLAG;
285    case TGSI_SEMANTIC_TEXCOORD:
286       assert(sem_index <= 7);
287       return VERT_ATTRIB_TEX0;
288    default:
289       assert(0);
290       return VERT_ATTRIB_POS;
291    }
292 }
293 
294 
295 /**
296  * Helper function to convert tgsi semantic name to varying semantic name.
297  */
298 static gl_varying_slot
svga_tgsi_to_gl_varying_semantic(unsigned sem_name,unsigned sem_index)299 svga_tgsi_to_gl_varying_semantic(unsigned sem_name,
300                                  unsigned sem_index)
301 {
302    switch (sem_name) {
303    case TGSI_SEMANTIC_POSITION:
304       return VARYING_SLOT_POS;
305    case TGSI_SEMANTIC_COLOR:
306       assert(sem_index <= 1);
307       return VARYING_SLOT_COL0;
308    case TGSI_SEMANTIC_BCOLOR:
309       assert(sem_index <= 1);
310       return VARYING_SLOT_BFC0;
311    case TGSI_SEMANTIC_FOG:
312       return VARYING_SLOT_FOGC;
313    case TGSI_SEMANTIC_PSIZE:
314       return VARYING_SLOT_PSIZ;
315    case TGSI_SEMANTIC_GENERIC:
316       return VARYING_SLOT_VAR0;
317    case TGSI_SEMANTIC_FACE:
318       return VARYING_SLOT_FACE;
319    case TGSI_SEMANTIC_EDGEFLAG:
320       return VARYING_SLOT_EDGE;
321    case TGSI_SEMANTIC_CLIPDIST:
322       assert(sem_index <= 1);
323       return VARYING_SLOT_CLIP_DIST0;
324    case TGSI_SEMANTIC_CLIPVERTEX:
325       return VARYING_SLOT_CLIP_VERTEX;
326    case TGSI_SEMANTIC_TEXCOORD:
327       assert(sem_index <= 7);
328       return VARYING_SLOT_TEX0;
329    case TGSI_SEMANTIC_PCOORD:
330       return VARYING_SLOT_PNTC;
331    case TGSI_SEMANTIC_VIEWPORT_INDEX:
332       return VARYING_SLOT_VIEWPORT;
333    case TGSI_SEMANTIC_LAYER:
334       return VARYING_SLOT_LAYER;
335    case TGSI_SEMANTIC_PATCH:
336       return VARYING_SLOT_PATCH0;
337    case TGSI_SEMANTIC_TESSOUTER:
338       return VARYING_SLOT_TESS_LEVEL_OUTER;
339    case TGSI_SEMANTIC_TESSINNER:
340       return VARYING_SLOT_TESS_LEVEL_INNER;
341    case TGSI_SEMANTIC_VIEWPORT_MASK:
342       return VARYING_SLOT_VIEWPORT_MASK;
343    case TGSI_SEMANTIC_PRIMID:
344       return VARYING_SLOT_PRIMITIVE_ID;
345    default:
346       assert(0);
347       return VARYING_SLOT_POS;
348    }
349 }
350 
351 
352 /**
353  * Helper function to convert tgsi semantic name to fragment result
354  * semantic name.
355  */
356 static gl_frag_result
svga_tgsi_to_gl_frag_result_semantic(unsigned sem_name,unsigned sem_index)357 svga_tgsi_to_gl_frag_result_semantic(unsigned sem_name,
358                                      unsigned sem_index)
359 {
360    switch (sem_name) {
361    case TGSI_SEMANTIC_POSITION:
362       return FRAG_RESULT_DEPTH;
363    case TGSI_SEMANTIC_COLOR:
364       assert(sem_index <= 7);
365       return FRAG_RESULT_DATA0;
366    case TGSI_SEMANTIC_STENCIL:
367       return FRAG_RESULT_STENCIL;
368    case TGSI_SEMANTIC_SAMPLEMASK:
369       return FRAG_RESULT_SAMPLE_MASK;
370    default:
371       assert(0);
372       return FRAG_RESULT_DATA0;
373    }
374 }
375 
376 
377 /**
378  * svga_tgsi_scan_shader is called to collect information of the
379  * specified tgsi shader.
380  */
381 void
svga_tgsi_scan_shader(struct svga_shader * shader)382 svga_tgsi_scan_shader(struct svga_shader *shader)
383 {
384    struct tgsi_shader_info *tgsi_info = &shader->tgsi_info;
385    struct svga_shader_info *info = &shader->info;
386 
387    tgsi_scan_shader(shader->tokens, tgsi_info);
388 
389    /* Save some common shader info in IR neutral format */
390    info->num_inputs = tgsi_info->num_inputs;
391    info->num_outputs = tgsi_info->num_outputs;
392    info->writes_edgeflag = tgsi_info->writes_edgeflag;
393    info->writes_layer = tgsi_info->writes_layer;
394    info->writes_position = tgsi_info->writes_position;
395    info->writes_psize = tgsi_info->writes_psize;
396    info->writes_viewport_index = tgsi_info->writes_viewport_index;
397 
398    info->uses_grid_size = tgsi_info->uses_grid_size;
399    info->uses_const_buffers = tgsi_info->const_buffers_declared != 0;
400    info->uses_hw_atomic = tgsi_info->hw_atomic_declared != 0;
401    info->uses_images = tgsi_info->images_declared != 0;
402    info->uses_image_size = tgsi_info->opcode_count[TGSI_OPCODE_RESQ] ? 1 : 0;
403    info->uses_shader_buffers = tgsi_info->shader_buffers_declared != 0;
404    info->uses_samplers = tgsi_info->samplers_declared != 0;
405    info->const_buffers_declared = tgsi_info->const_buffers_declared;
406    info->shader_buffers_declared = tgsi_info->shader_buffers_declared;
407 
408    info->generic_inputs_mask = svga_get_generic_inputs_mask(tgsi_info);
409    info->generic_outputs_mask = svga_get_generic_outputs_mask(tgsi_info);
410 
411    /* Convert TGSI inputs semantic.
412     * Vertex shader does not have varying inputs but vertex attributes.
413     */
414    if (shader->stage == PIPE_SHADER_VERTEX) {
415       for (unsigned i = 0; i < info->num_inputs; i++) {
416          info->input_semantic_name[i] =
417             svga_tgsi_to_gl_vert_attrib_semantic(
418                tgsi_info->input_semantic_name[i],
419                tgsi_info->input_semantic_index[i]);
420          info->input_semantic_index[i] = tgsi_info->input_semantic_index[i];
421       }
422    }
423    else {
424       for (unsigned i = 0; i < info->num_inputs; i++) {
425          info->input_semantic_name[i] =
426             svga_tgsi_to_gl_varying_semantic(
427                tgsi_info->input_semantic_name[i],
428                tgsi_info->input_semantic_index[i]);
429          info->input_semantic_index[i] = tgsi_info->input_semantic_index[i];
430       }
431    }
432 
433    /* Convert TGSI outputs semantic.
434     * Fragment shader does not have varying outputs but fragment results.
435     */
436    if (shader->stage == PIPE_SHADER_FRAGMENT) {
437       for (unsigned i = 0; i < info->num_outputs; i++) {
438          info->output_semantic_name[i] =
439             svga_tgsi_to_gl_frag_result_semantic(
440                tgsi_info->output_semantic_name[i],
441                tgsi_info->output_semantic_index[i]);
442          info->output_semantic_index[i] = tgsi_info->output_semantic_index[i];
443       }
444    }
445    else {
446       for (unsigned i = 0; i < info->num_outputs; i++) {
447          info->output_semantic_name[i] =
448             svga_tgsi_to_gl_varying_semantic(
449                tgsi_info->output_semantic_name[i],
450                tgsi_info->output_semantic_index[i]);
451          info->output_semantic_index[i] = tgsi_info->output_semantic_index[i];
452       }
453    }
454 
455    info->constbuf0_num_uniforms = tgsi_info->const_file_max[0] + 1;
456 
457    switch (tgsi_info->processor) {
458    case PIPE_SHADER_FRAGMENT:
459       info->fs.color0_writes_all_cbufs =
460          tgsi_info->properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS];
461       break;
462    case PIPE_SHADER_GEOMETRY:
463       info->gs.out_prim = tgsi_info->properties[TGSI_PROPERTY_GS_OUTPUT_PRIM];
464       info->gs.in_prim = tgsi_info->properties[TGSI_PROPERTY_GS_INPUT_PRIM];
465       break;
466    case PIPE_SHADER_TESS_CTRL:
467       info->tcs.vertices_out =
468          tgsi_info->properties[TGSI_PROPERTY_TCS_VERTICES_OUT];
469 
470       for (unsigned i = 0; i < info->num_outputs; i++) {
471          switch (tgsi_info->output_semantic_name[i]) {
472          case TGSI_SEMANTIC_TESSOUTER:
473          case TGSI_SEMANTIC_TESSINNER:
474             info->tcs.writes_tess_factor = true;
475             break;
476          default:
477             break;
478          }
479       }
480       break;
481    case PIPE_SHADER_TESS_EVAL:
482       info->tes.prim_mode =
483          tgsi_info->properties[TGSI_PROPERTY_TES_PRIM_MODE];
484 
485       for (unsigned i = 0; i < info->num_inputs; i++) {
486          switch (tgsi_info->input_semantic_name[i]) {
487          case TGSI_SEMANTIC_PATCH:
488          case TGSI_SEMANTIC_TESSOUTER:
489          case TGSI_SEMANTIC_TESSINNER:
490             break;
491          default:
492               info->tes.reads_control_point = true;
493          }
494       }
495       break;
496    default:
497       break;
498    }
499 }
500 
501 
502 /**
503  * Compile a TGSI shader
504  */
505 struct svga_shader_variant *
svga_tgsi_compile_shader(struct svga_context * svga,struct svga_shader * shader,const struct svga_compile_key * key)506 svga_tgsi_compile_shader(struct svga_context *svga,
507                          struct svga_shader *shader,
508                          const struct svga_compile_key *key)
509 {
510    if (svga_have_vgpu10(svga)) {
511       return svga_tgsi_vgpu10_translate(svga, shader, key, shader->stage);
512    }
513    else {
514       return svga_tgsi_vgpu9_translate(svga, shader, key, shader->stage);
515    }
516 }
517