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