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