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