1 /*
2  * Copyright 2017 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * on the rights to use, copy, modify, merge, publish, distribute, sub
8  * license, and/or sell copies of the Software, and to permit persons to whom
9  * the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21  * USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "tgsi/tgsi_from_mesa.h"
25 
26 #include "pipe/p_compiler.h"
27 
28 /**
29  * Determine the semantic index that is used when the given varying is mapped
30  * to TGSI_SEMANTIC_GENERIC.
31  */
32 unsigned
tgsi_get_generic_gl_varying_index(gl_varying_slot attr,bool needs_texcoord_semantic)33 tgsi_get_generic_gl_varying_index(gl_varying_slot attr,
34                                   bool needs_texcoord_semantic)
35 {
36    if (attr >= VARYING_SLOT_VAR0) {
37       if (needs_texcoord_semantic)
38          return attr - VARYING_SLOT_VAR0;
39       else
40          return 9 + (attr - VARYING_SLOT_VAR0);
41    }
42    if (attr == VARYING_SLOT_PNTC) {
43       assert(!needs_texcoord_semantic);
44       return 8;
45    }
46    if (attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7) {
47       assert(!needs_texcoord_semantic);
48       return attr - VARYING_SLOT_TEX0;
49    }
50 
51    assert(0);
52    return 0;
53 }
54 
55 /**
56  * Determine the semantic name and index used for the given varying.
57  */
58 void
tgsi_get_gl_varying_semantic(gl_varying_slot attr,bool needs_texcoord_semantic,unsigned * semantic_name,unsigned * semantic_index)59 tgsi_get_gl_varying_semantic(gl_varying_slot attr,
60                              bool needs_texcoord_semantic,
61                              unsigned *semantic_name,
62                              unsigned *semantic_index)
63 {
64    switch (attr) {
65    case VARYING_SLOT_PRIMITIVE_ID:
66       *semantic_name = TGSI_SEMANTIC_PRIMID;
67       *semantic_index = 0;
68       break;
69    case VARYING_SLOT_POS:
70       *semantic_name = TGSI_SEMANTIC_POSITION;
71       *semantic_index = 0;
72       break;
73    case VARYING_SLOT_COL0:
74       *semantic_name = TGSI_SEMANTIC_COLOR;
75       *semantic_index = 0;
76       break;
77    case VARYING_SLOT_COL1:
78       *semantic_name = TGSI_SEMANTIC_COLOR;
79       *semantic_index = 1;
80       break;
81    case VARYING_SLOT_BFC0:
82       *semantic_name = TGSI_SEMANTIC_BCOLOR;
83       *semantic_index = 0;
84       break;
85    case VARYING_SLOT_BFC1:
86       *semantic_name = TGSI_SEMANTIC_BCOLOR;
87       *semantic_index = 1;
88       break;
89    case VARYING_SLOT_FOGC:
90       *semantic_name = TGSI_SEMANTIC_FOG;
91       *semantic_index = 0;
92       break;
93    case VARYING_SLOT_PSIZ:
94       *semantic_name = TGSI_SEMANTIC_PSIZE;
95       *semantic_index = 0;
96       break;
97    case VARYING_SLOT_CLIP_DIST0:
98       *semantic_name = TGSI_SEMANTIC_CLIPDIST;
99       *semantic_index = 0;
100       break;
101    case VARYING_SLOT_CLIP_DIST1:
102       *semantic_name = TGSI_SEMANTIC_CLIPDIST;
103       *semantic_index = 1;
104       break;
105    case VARYING_SLOT_CULL_DIST0:
106    case VARYING_SLOT_CULL_DIST1:
107       /* these should have been lowered by GLSL */
108       assert(0);
109       break;
110    case VARYING_SLOT_EDGE:
111       *semantic_name = TGSI_SEMANTIC_EDGEFLAG;
112       *semantic_index = 0;
113       break;
114    case VARYING_SLOT_CLIP_VERTEX:
115       *semantic_name = TGSI_SEMANTIC_CLIPVERTEX;
116       *semantic_index = 0;
117       break;
118    case VARYING_SLOT_LAYER:
119       *semantic_name = TGSI_SEMANTIC_LAYER;
120       *semantic_index = 0;
121       break;
122    case VARYING_SLOT_VIEWPORT:
123       *semantic_name = TGSI_SEMANTIC_VIEWPORT_INDEX;
124       *semantic_index = 0;
125       break;
126    case VARYING_SLOT_FACE:
127       *semantic_name = TGSI_SEMANTIC_FACE;
128       *semantic_index = 0;
129       break;
130    case VARYING_SLOT_PNTC:
131       *semantic_name = TGSI_SEMANTIC_PCOORD;
132       *semantic_index = 0;
133       break;
134    case VARYING_SLOT_TESS_LEVEL_OUTER:
135       *semantic_name = TGSI_SEMANTIC_TESSOUTER;
136       *semantic_index = 0;
137       break;
138    case VARYING_SLOT_TESS_LEVEL_INNER:
139       *semantic_name = TGSI_SEMANTIC_TESSINNER;
140       *semantic_index = 0;
141       break;
142    case VARYING_SLOT_VIEWPORT_MASK:
143       *semantic_name = TGSI_SEMANTIC_VIEWPORT_MASK;
144       *semantic_index = 0;
145       break;
146 
147    case VARYING_SLOT_TEX0:
148    case VARYING_SLOT_TEX1:
149    case VARYING_SLOT_TEX2:
150    case VARYING_SLOT_TEX3:
151    case VARYING_SLOT_TEX4:
152    case VARYING_SLOT_TEX5:
153    case VARYING_SLOT_TEX6:
154    case VARYING_SLOT_TEX7:
155       if (needs_texcoord_semantic) {
156          *semantic_name = TGSI_SEMANTIC_TEXCOORD;
157          *semantic_index = attr - VARYING_SLOT_TEX0;
158          break;
159       }
160       /* fall through */
161    case VARYING_SLOT_VAR0:
162    default:
163       assert(attr >= VARYING_SLOT_VAR0 ||
164              (attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7));
165       if (attr >= VARYING_SLOT_PATCH0) {
166          *semantic_name = TGSI_SEMANTIC_PATCH;
167          *semantic_index = attr - VARYING_SLOT_PATCH0;
168       } else {
169          *semantic_name = TGSI_SEMANTIC_GENERIC;
170          *semantic_index =
171             tgsi_get_generic_gl_varying_index(attr, needs_texcoord_semantic);
172       }
173       break;
174    }
175 }
176 
177 /**
178  * Determine the semantic name and index used for the given fragment shader
179  * result.
180  */
181 void
tgsi_get_gl_frag_result_semantic(gl_frag_result frag_result,unsigned * semantic_name,unsigned * semantic_index)182 tgsi_get_gl_frag_result_semantic(gl_frag_result frag_result,
183                                  unsigned *semantic_name,
184                                  unsigned *semantic_index)
185 {
186    if (frag_result >= FRAG_RESULT_DATA0) {
187       *semantic_name = TGSI_SEMANTIC_COLOR;
188       *semantic_index = frag_result - FRAG_RESULT_DATA0;
189       return;
190    }
191 
192    *semantic_index = 0;
193 
194    switch (frag_result) {
195    case FRAG_RESULT_DEPTH:
196       *semantic_name = TGSI_SEMANTIC_POSITION;
197       break;
198    case FRAG_RESULT_STENCIL:
199       *semantic_name = TGSI_SEMANTIC_STENCIL;
200       break;
201    case FRAG_RESULT_COLOR:
202       *semantic_name = TGSI_SEMANTIC_COLOR;
203       break;
204    case FRAG_RESULT_SAMPLE_MASK:
205       *semantic_name = TGSI_SEMANTIC_SAMPLEMASK;
206       break;
207    default:
208       assert(false);
209    }
210 }
211 
212 /** Map Mesa's SYSTEM_VALUE_x to TGSI_SEMANTIC_x */
213 enum tgsi_semantic
tgsi_get_sysval_semantic(unsigned sysval)214 tgsi_get_sysval_semantic(unsigned sysval)
215 {
216    switch (sysval) {
217    /* Vertex shader */
218    case SYSTEM_VALUE_VERTEX_ID:
219       return TGSI_SEMANTIC_VERTEXID;
220    case SYSTEM_VALUE_INSTANCE_ID:
221       return TGSI_SEMANTIC_INSTANCEID;
222    case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE:
223       return TGSI_SEMANTIC_VERTEXID_NOBASE;
224    case SYSTEM_VALUE_BASE_VERTEX:
225       return TGSI_SEMANTIC_BASEVERTEX;
226    case SYSTEM_VALUE_BASE_INSTANCE:
227       return TGSI_SEMANTIC_BASEINSTANCE;
228    case SYSTEM_VALUE_DRAW_ID:
229       return TGSI_SEMANTIC_DRAWID;
230 
231    /* Geometry shader */
232    case SYSTEM_VALUE_INVOCATION_ID:
233       return TGSI_SEMANTIC_INVOCATIONID;
234 
235    /* Fragment shader */
236    case SYSTEM_VALUE_FRAG_COORD:
237       return TGSI_SEMANTIC_POSITION;
238    case SYSTEM_VALUE_POINT_COORD:
239       return TGSI_SEMANTIC_PCOORD;
240    case SYSTEM_VALUE_FRONT_FACE:
241       return TGSI_SEMANTIC_FACE;
242    case SYSTEM_VALUE_SAMPLE_ID:
243       return TGSI_SEMANTIC_SAMPLEID;
244    case SYSTEM_VALUE_SAMPLE_POS:
245       return TGSI_SEMANTIC_SAMPLEPOS;
246    case SYSTEM_VALUE_SAMPLE_MASK_IN:
247       return TGSI_SEMANTIC_SAMPLEMASK;
248    case SYSTEM_VALUE_HELPER_INVOCATION:
249       return TGSI_SEMANTIC_HELPER_INVOCATION;
250 
251    /* Tessellation shader */
252    case SYSTEM_VALUE_TESS_COORD:
253       return TGSI_SEMANTIC_TESSCOORD;
254    case SYSTEM_VALUE_VERTICES_IN:
255       return TGSI_SEMANTIC_VERTICESIN;
256    case SYSTEM_VALUE_PRIMITIVE_ID:
257       return TGSI_SEMANTIC_PRIMID;
258    case SYSTEM_VALUE_TESS_LEVEL_OUTER:
259       return TGSI_SEMANTIC_TESSOUTER;
260    case SYSTEM_VALUE_TESS_LEVEL_INNER:
261       return TGSI_SEMANTIC_TESSINNER;
262 
263    /* Compute shader */
264    case SYSTEM_VALUE_LOCAL_INVOCATION_ID:
265       return TGSI_SEMANTIC_THREAD_ID;
266    case SYSTEM_VALUE_WORK_GROUP_ID:
267       return TGSI_SEMANTIC_BLOCK_ID;
268    case SYSTEM_VALUE_NUM_WORK_GROUPS:
269       return TGSI_SEMANTIC_GRID_SIZE;
270    case SYSTEM_VALUE_LOCAL_GROUP_SIZE:
271       return TGSI_SEMANTIC_BLOCK_SIZE;
272 
273    /* ARB_shader_ballot */
274    case SYSTEM_VALUE_SUBGROUP_SIZE:
275       return TGSI_SEMANTIC_SUBGROUP_SIZE;
276    case SYSTEM_VALUE_SUBGROUP_INVOCATION:
277       return TGSI_SEMANTIC_SUBGROUP_INVOCATION;
278    case SYSTEM_VALUE_SUBGROUP_EQ_MASK:
279       return TGSI_SEMANTIC_SUBGROUP_EQ_MASK;
280    case SYSTEM_VALUE_SUBGROUP_GE_MASK:
281       return TGSI_SEMANTIC_SUBGROUP_GE_MASK;
282    case SYSTEM_VALUE_SUBGROUP_GT_MASK:
283       return TGSI_SEMANTIC_SUBGROUP_GT_MASK;
284    case SYSTEM_VALUE_SUBGROUP_LE_MASK:
285       return TGSI_SEMANTIC_SUBGROUP_LE_MASK;
286    case SYSTEM_VALUE_SUBGROUP_LT_MASK:
287       return TGSI_SEMANTIC_SUBGROUP_LT_MASK;
288 
289    default:
290       unreachable("Unexpected system value to TGSI");
291    }
292 }
293 
294 enum tgsi_interpolate_mode
tgsi_get_interp_mode(enum glsl_interp_mode mode,bool color)295 tgsi_get_interp_mode(enum glsl_interp_mode mode, bool color)
296 {
297    switch (mode) {
298    case INTERP_MODE_NONE:
299       return color ? TGSI_INTERPOLATE_COLOR : TGSI_INTERPOLATE_PERSPECTIVE;
300    case INTERP_MODE_FLAT:
301       return TGSI_INTERPOLATE_CONSTANT;
302    case INTERP_MODE_NOPERSPECTIVE:
303       return TGSI_INTERPOLATE_LINEAR;
304    case INTERP_MODE_SMOOTH:
305       return TGSI_INTERPOLATE_PERSPECTIVE;
306    default:
307       unreachable("unknown interpolation mode");
308    }
309 }
310