xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/svga/include/svga3d_shaderdefs.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (c) 2007-2024 Broadcom. All Rights Reserved.
3  * The term “Broadcom” refers to Broadcom Inc.
4  * and/or its subsidiaries.
5  * SPDX-License-Identifier: GPL-2.0 OR MIT
6  */
7 
8 /*
9  * svga3d_shaderdefs.h --
10  *
11  * SVGA3D byte code format and limit definitions.
12  *
13  * The format of the byte code directly corresponds to that defined
14  * by Microsoft DirectX SDK 9.0c (file d3d9types.h). The format can
15  * also be extended so that different shader formats can be supported
16  * for example GLSL, ARB vp/fp, NV/ATI shader formats, etc.
17  *
18  */
19 
20 #ifndef __SVGA3D_SHADER_DEFS__
21 #define __SVGA3D_SHADER_DEFS__
22 
23 /* SVGA3D shader hardware limits. */
24 
25 #define SVGA3D_INPUTREG_MAX            16
26 #define SVGA3D_OUTPUTREG_MAX           12
27 #define SVGA3D_VERTEX_SAMPLERREG_MAX   4
28 #define SVGA3D_PIXEL_SAMPLERREG_MAX    16
29 #define SVGA3D_SAMPLERREG_MAX          (SVGA3D_PIXEL_SAMPLERREG_MAX+\
30                                         SVGA3D_VERTEX_SAMPLERREG_MAX)
31 #define SVGA3D_TEMPREG_MAX             32
32 #define SVGA3D_CONSTREG_MAX            256
33 #define SVGA3D_CONSTINTREG_MAX         16
34 #define SVGA3D_CONSTBOOLREG_MAX        16
35 #define SVGA3D_ADDRREG_MAX             1
36 #define SVGA3D_PREDREG_MAX             1
37 
38 /* SVGA3D byte code specific limits */
39 
40 #define SVGA3D_MAX_SRC_REGS      4
41 #define SVGA3D_MAX_NESTING_LEVEL 32
42 
43 /* SVGA3D version information. */
44 
45 #define SVGA3D_VS_TYPE  0xFFFE
46 #define SVGA3D_PS_TYPE  0xFFFF
47 
48 typedef struct {
49    union {
50       struct {
51          uint32 minor : 8;
52          uint32 major : 8;
53          uint32 type : 16;
54       };
55 
56       uint32 value;
57    };
58 } SVGA3dShaderVersion;
59 
60 #define SVGA3D_VS_10 ((SVGA3D_VS_TYPE << 16) | 1 << 8)
61 #define SVGA3D_VS_11 (SVGA3D_VS_10 | 1)
62 #define SVGA3D_VS_20 ((SVGA3D_VS_TYPE << 16) | 2 << 8)
63 #define SVGA3D_VS_21 (SVGA3D_VS_20 | 1)
64 #define SVGA3D_VS_30 ((SVGA3D_VS_TYPE << 16) | 3 << 8)
65 
66 #define SVGA3D_PS_10 ((SVGA3D_PS_TYPE << 16) | 1 << 8)
67 #define SVGA3D_PS_11 (SVGA3D_PS_10 | 1)
68 #define SVGA3D_PS_12 (SVGA3D_PS_10 | 2)
69 #define SVGA3D_PS_13 (SVGA3D_PS_10 | 3)
70 #define SVGA3D_PS_14 (SVGA3D_PS_10 | 4)
71 #define SVGA3D_PS_20 ((SVGA3D_PS_TYPE << 16) | 2 << 8)
72 #define SVGA3D_PS_21 (SVGA3D_PS_20 | 1)
73 #define SVGA3D_PS_30 ((SVGA3D_PS_TYPE << 16) | 3 << 8)
74 
75 /* The *_ENABLED are for backwards compatibility with old drivers */
76 typedef enum {
77    SVGA3DPSVERSION_NONE = 0,
78    SVGA3DPSVERSION_ENABLED = 1,
79    SVGA3DPSVERSION_11 = 3,
80    SVGA3DPSVERSION_12 = 5,
81    SVGA3DPSVERSION_13 = 7,
82    SVGA3DPSVERSION_14 = 9,
83    SVGA3DPSVERSION_20 = 11,
84    SVGA3DPSVERSION_30 = 13,
85    SVGA3DPSVERSION_40 = 15,
86    SVGA3DPSVERSION_MAX
87 } SVGA3dPixelShaderVersion;
88 
89 typedef enum {
90    SVGA3DVSVERSION_NONE = 0,
91    SVGA3DVSVERSION_ENABLED = 1,
92    SVGA3DVSVERSION_11 = 3,
93    SVGA3DVSVERSION_20 = 5,
94    SVGA3DVSVERSION_30 = 7,
95    SVGA3DVSVERSION_40 = 9,
96    SVGA3DVSVERSION_MAX
97 } SVGA3dVertexShaderVersion;
98 
99 /* SVGA3D instruction op codes. */
100 
101 typedef enum {
102    SVGA3DOP_NOP = 0,
103    SVGA3DOP_MOV,
104    SVGA3DOP_ADD,
105    SVGA3DOP_SUB,
106    SVGA3DOP_MAD,
107    SVGA3DOP_MUL,
108    SVGA3DOP_RCP,
109    SVGA3DOP_RSQ,
110    SVGA3DOP_DP3,
111    SVGA3DOP_DP4,
112    SVGA3DOP_MIN,
113    SVGA3DOP_MAX,
114    SVGA3DOP_SLT,
115    SVGA3DOP_SGE,
116    SVGA3DOP_EXP,
117    SVGA3DOP_LOG,
118    SVGA3DOP_LIT,
119    SVGA3DOP_DST,
120    SVGA3DOP_LRP,
121    SVGA3DOP_FRC,
122    SVGA3DOP_M4x4,
123    SVGA3DOP_M4x3,
124    SVGA3DOP_M3x4,
125    SVGA3DOP_M3x3,
126    SVGA3DOP_M3x2,
127    SVGA3DOP_CALL,
128    SVGA3DOP_CALLNZ,
129    SVGA3DOP_LOOP,
130    SVGA3DOP_RET,
131    SVGA3DOP_ENDLOOP,
132    SVGA3DOP_LABEL,
133    SVGA3DOP_DCL,
134    SVGA3DOP_POW,
135    SVGA3DOP_CRS,
136    SVGA3DOP_SGN,
137    SVGA3DOP_ABS,
138    SVGA3DOP_NRM,
139    SVGA3DOP_SINCOS,
140    SVGA3DOP_REP,
141    SVGA3DOP_ENDREP,
142    SVGA3DOP_IF,
143    SVGA3DOP_IFC,
144    SVGA3DOP_ELSE,
145    SVGA3DOP_ENDIF,
146    SVGA3DOP_BREAK,
147    SVGA3DOP_BREAKC,
148    SVGA3DOP_MOVA,
149    SVGA3DOP_DEFB,
150    SVGA3DOP_DEFI,
151    SVGA3DOP_TEXCOORD = 64,
152    SVGA3DOP_TEXKILL,
153    SVGA3DOP_TEX,
154    SVGA3DOP_TEXBEM,
155    SVGA3DOP_TEXBEML,
156    SVGA3DOP_TEXREG2AR,
157    SVGA3DOP_TEXREG2GB = 70,
158    SVGA3DOP_TEXM3x2PAD,
159    SVGA3DOP_TEXM3x2TEX,
160    SVGA3DOP_TEXM3x3PAD,
161    SVGA3DOP_TEXM3x3TEX,
162    SVGA3DOP_RESERVED0,
163    SVGA3DOP_TEXM3x3SPEC,
164    SVGA3DOP_TEXM3x3VSPEC,
165    SVGA3DOP_EXPP,
166    SVGA3DOP_LOGP,
167    SVGA3DOP_CND = 80,
168    SVGA3DOP_DEF,
169    SVGA3DOP_TEXREG2RGB,
170    SVGA3DOP_TEXDP3TEX,
171    SVGA3DOP_TEXM3x2DEPTH,
172    SVGA3DOP_TEXDP3,
173    SVGA3DOP_TEXM3x3,
174    SVGA3DOP_TEXDEPTH,
175    SVGA3DOP_CMP,
176    SVGA3DOP_BEM,
177    SVGA3DOP_DP2ADD = 90,
178    SVGA3DOP_DSX,
179    SVGA3DOP_DSY,
180    SVGA3DOP_TEXLDD,
181    SVGA3DOP_SETP,
182    SVGA3DOP_TEXLDL,
183    SVGA3DOP_BREAKP = 96,
184    SVGA3DOP_LAST_INST,
185    SVGA3DOP_PHASE = 0xFFFD,
186    SVGA3DOP_COMMENT = 0xFFFE,
187    SVGA3DOP_END = 0xFFFF,
188 } SVGA3dShaderOpCodeType;
189 
190 /* SVGA3D operation control/comparison function types */
191 
192 typedef enum {
193    SVGA3DOPCONT_NONE,
194    SVGA3DOPCONT_PROJECT,   /* Projective texturing */
195    SVGA3DOPCONT_BIAS,      /* Texturing with a LOD bias */
196 } SVGA3dShaderOpCodeControlFnType;
197 
198 typedef enum {
199    SVGA3DOPCOMP_RESERVED0 = 0,
200    SVGA3DOPCOMP_GT,
201    SVGA3DOPCOMP_EQ,
202    SVGA3DOPCOMP_GE,
203    SVGA3DOPCOMP_LT,
204    SVGA3DOPCOMPC_NE,
205    SVGA3DOPCOMP_LE,
206    SVGA3DOPCOMP_RESERVED1
207 } SVGA3dShaderOpCodeCompFnType;
208 
209 /* SVGA3D register types */
210 
211 typedef enum {
212     SVGA3DREG_TEMP = 0,       /* Temporary register file */
213     SVGA3DREG_INPUT,          /* Input register file */
214     SVGA3DREG_CONST,          /* Constant register file */
215     SVGA3DREG_ADDR,           /* Address register for VS */
216     SVGA3DREG_TEXTURE = 3,    /* Texture register file for PS */
217     SVGA3DREG_RASTOUT,        /* Rasterizer register file */
218     SVGA3DREG_ATTROUT,        /* Attribute output register file */
219     SVGA3DREG_TEXCRDOUT,      /* Texture coordinate output register file */
220     SVGA3DREG_OUTPUT = 6,     /* Output register file for VS 3.0+ */
221     SVGA3DREG_CONSTINT,       /* Constant integer vector register file */
222     SVGA3DREG_COLOROUT,       /* Color output register file */
223     SVGA3DREG_DEPTHOUT,       /* Depth output register file */
224     SVGA3DREG_SAMPLER,        /* Sampler state register file */
225     SVGA3DREG_CONST2,         /* Constant register file 2048 - 4095 */
226     SVGA3DREG_CONST3,         /* Constant register file 4096 - 6143 */
227     SVGA3DREG_CONST4,         /* Constant register file 6144 - 8191 */
228     SVGA3DREG_CONSTBOOL,      /* Constant boolean register file */
229     SVGA3DREG_LOOP,           /* Loop counter register file */
230     SVGA3DREG_TEMPFLOAT16,    /* 16-bit float temp register file */
231     SVGA3DREG_MISCTYPE,       /* Miscellaneous (single) registers */
232     SVGA3DREG_LABEL,          /* Label */
233     SVGA3DREG_PREDICATE,      /* Predicate register */
234 } SVGA3dShaderRegType;
235 
236 /* SVGA3D rasterizer output register types */
237 
238 typedef enum {
239    SVGA3DRASTOUT_POSITION = 0,
240    SVGA3DRASTOUT_FOG,
241    SVGA3DRASTOUT_PSIZE
242 } SVGA3dShaderRastOutRegType;
243 
244 /* SVGA3D miscellaneous register types */
245 
246 typedef enum {
247    SVGA3DMISCREG_POSITION = 0,   /* Input position x,y,z,rhw (PS) */
248    SVGA3DMISCREG_FACE            /* Floating point primitive area (PS) */
249 } SVGA3DShaderMiscRegType;
250 
251 /* SVGA3D sampler types */
252 
253 typedef enum {
254    SVGA3DSAMP_UNKNOWN = 0, /* Uninitialized value */
255    SVGA3DSAMP_2D = 2,      /* dcl_2d s# (for declaring a 2D texture) */
256    SVGA3DSAMP_CUBE,        /* dcl_cube s# (for declaring a cube texture) */
257    SVGA3DSAMP_VOLUME,      /* dcl_volume s# (for declaring a volume texture) */
258    SVGA3DSAMP_2D_SHADOW,   /* dcl_2d s# (for declaring a 2D shadow texture) */
259    SVGA3DSAMP_MAX,
260 } SVGA3dShaderSamplerType;
261 
262 /* SVGA3D write mask */
263 
264 #define SVGA3DWRITEMASK_0    1 /* Component 0 (X;Red) */
265 #define SVGA3DWRITEMASK_1    2 /* Component 1 (Y;Green) */
266 #define SVGA3DWRITEMASK_2    4 /* Component 2 (Z;Blue) */
267 #define SVGA3DWRITEMASK_3    8 /* Component 3 (W;Alpha) */
268 #define SVGA3DWRITEMASK_ALL 15 /* All components */
269 
270 /* SVGA3D destination modifiers */
271 
272 #define SVGA3DDSTMOD_NONE              0 /* nop */
273 #define SVGA3DDSTMOD_SATURATE          1 /* clamp to [0, 1] */
274 #define SVGA3DDSTMOD_PARTIALPRECISION  2 /* Partial precision hint */
275 
276 /*
277  * Relevant to multisampling only:
278  * When the pixel center is not covered, sample
279  * attribute or compute gradients/LOD
280  * using multisample "centroid" location.
281  * "Centroid" is some location within the covered
282  * region of the pixel.
283  */
284 
285 #define SVGA3DDSTMOD_MSAMPCENTROID     4
286 
287 /* SVGA3D destination shift scale */
288 
289 typedef enum {
290    SVGA3DDSTSHFSCALE_X1 = 0,  /* 1.0 */
291    SVGA3DDSTSHFSCALE_X2 = 1,  /* 2.0 */
292    SVGA3DDSTSHFSCALE_X4 = 2,  /* 4.0 */
293    SVGA3DDSTSHFSCALE_X8 = 3,  /* 8.0 */
294    SVGA3DDSTSHFSCALE_D8 = 13, /* 0.125 */
295    SVGA3DDSTSHFSCALE_D4 = 14, /* 0.25 */
296    SVGA3DDSTSHFSCALE_D2 = 15  /* 0.5 */
297 } SVGA3dShaderDstShfScaleType;
298 
299 /* SVGA3D source swizzle */
300 
301 #define SVGA3DSWIZZLE_REPLICATEX 0x00
302 #define SVGA3DSWIZZLE_REPLICATEY 0x55
303 #define SVGA3DSWIZZLE_REPLICATEZ 0xAA
304 #define SVGA3DSWIZZLE_REPLICATEW 0xFF
305 #define SVGA3DSWIZZLE_NONE       0xE4
306 #define SVGA3DSWIZZLE_YZXW       0xC9
307 #define SVGA3DSWIZZLE_ZXYW       0xD2
308 #define SVGA3DSWIZZLE_WXYZ       0x1B
309 
310 /* SVGA3D source modifiers */
311 
312 typedef enum {
313     SVGA3DSRCMOD_NONE = 0, /* nop */
314     SVGA3DSRCMOD_NEG,      /* negate */
315     SVGA3DSRCMOD_BIAS,     /* bias */
316     SVGA3DSRCMOD_BIASNEG,  /* bias and negate */
317     SVGA3DSRCMOD_SIGN,     /* sign */
318     SVGA3DSRCMOD_SIGNNEG,  /* sign and negate */
319     SVGA3DSRCMOD_COMP,     /* complement */
320     SVGA3DSRCMOD_X2,       /* x2 */
321     SVGA3DSRCMOD_X2NEG,    /* x2 and negate */
322     SVGA3DSRCMOD_DZ,       /* divide through by z component */
323     SVGA3DSRCMOD_DW,       /* divide through by w component */
324     SVGA3DSRCMOD_ABS,      /* abs() */
325     SVGA3DSRCMOD_ABSNEG,   /* -abs() */
326     SVGA3DSRCMOD_NOT,      /* ! (for predicate register) */
327 } SVGA3dShaderSrcModType;
328 
329 /* SVGA3D instruction token */
330 
331 typedef struct {
332    union {
333       struct {
334          uint32 comment_op : 16;
335          uint32 comment_size : 16;
336       };
337 
338       struct {
339          uint32 op : 16;
340          uint32 control : 3;
341          uint32 reserved2 : 5;
342          uint32 size : 4;
343          uint32 predicated : 1;
344          uint32 reserved1 : 1;
345          uint32 coissue : 1;
346          uint32 reserved0 : 1;
347       };
348 
349       uint32 value;
350    };
351 } SVGA3dShaderInstToken;
352 
353 /* SVGA3D destination parameter token */
354 
355 typedef struct {
356    union {
357       struct {
358          uint32 num : 11;
359          uint32 type_upper : 2;
360          uint32 relAddr : 1;
361          uint32 reserved1 : 2;
362          uint32 mask : 4;
363          uint32 dstMod : 4;
364          uint32 shfScale : 4;
365          uint32 type_lower : 3;
366          uint32 reserved0 : 1;
367       };
368 
369       uint32 value;
370    };
371 } SVGA3dShaderDestToken;
372 
373 /* SVGA3D source parameter token */
374 
375 typedef struct {
376    union {
377       struct {
378          uint32 num : 11;
379          uint32 type_upper : 2;
380          uint32 relAddr : 1;
381          uint32 reserved1 : 2;
382          uint32 swizzle : 8;
383          uint32 srcMod : 4;
384          uint32 type_lower : 3;
385          uint32 reserved0 : 1;
386       };
387 
388       uint32 value;
389    };
390 } SVGA3dShaderSrcToken;
391 
392 /* SVGA3DOP_DCL parameter tokens */
393 
394 typedef struct {
395    union {
396       struct {
397          union {
398             struct {
399                uint32 usage : 5;
400                uint32 reserved1 : 11;
401                uint32 index : 4;
402                uint32 reserved0 : 12;
403             }; /* input / output declaration */
404 
405             struct {
406                uint32 reserved3 : 27;
407                uint32 type : 4;
408                uint32 reserved2 : 1;
409             }; /* sampler declaration */
410          };
411 
412          SVGA3dShaderDestToken dst;
413       };
414 
415       uint32 values[2];
416    };
417 } SVGA3DOpDclArgs;
418 
419 /* SVGA3DOP_DEF parameter tokens */
420 
421 typedef struct {
422    union {
423       struct {
424          SVGA3dShaderDestToken dst;
425 
426          union {
427             float constValues[4];
428             int constIValues[4];
429             Bool constBValue;
430          };
431       };
432 
433       uint32 values[5];
434    };
435 } SVGA3DOpDefArgs;
436 
437 /* SVGA3D shader token */
438 
439 typedef union {
440    uint32 value;
441    SVGA3dShaderInstToken inst;
442    SVGA3dShaderDestToken dest;
443    SVGA3dShaderSrcToken src;
444 } SVGA3dShaderToken;
445 
446 /* SVGA3D shader program */
447 
448 typedef struct {
449    SVGA3dShaderVersion version;
450    /* SVGA3dShaderToken stream */
451 } SVGA3dShaderProgram;
452 
453 /* SVGA3D version specific register assignments */
454 
455 static const uint32 SVGA3D_INPUT_REG_POSITION_VS11 = 0;
456 static const uint32 SVGA3D_INPUT_REG_PSIZE_VS11 = 1;
457 static const uint32 SVGA3D_INPUT_REG_FOG_VS11 = 3;
458 static const uint32 SVGA3D_INPUT_REG_FOG_MASK_VS11 = SVGA3DWRITEMASK_3;
459 static const uint32 SVGA3D_INPUT_REG_COLOR_BASE_VS11 = 2;
460 static const uint32 SVGA3D_INPUT_REG_TEXCOORD_BASE_VS11 = 4;
461 
462 static const uint32 SVGA3D_INPUT_REG_COLOR_BASE_PS11 = 0;
463 static const uint32 SVGA3D_INPUT_REG_TEXCOORD_BASE_PS11 = 2;
464 static const uint32 SVGA3D_OUTPUT_REG_DEPTH_PS11 = 0;
465 static const uint32 SVGA3D_OUTPUT_REG_COLOR_PS11 = 1;
466 
467 static const uint32 SVGA3D_INPUT_REG_COLOR_BASE_PS20 = 0;
468 static const uint32 SVGA3D_INPUT_REG_COLOR_NUM_PS20 = 2;
469 static const uint32 SVGA3D_INPUT_REG_TEXCOORD_BASE_PS20 = 2;
470 static const uint32 SVGA3D_INPUT_REG_TEXCOORD_NUM_PS20 = 8;
471 static const uint32 SVGA3D_OUTPUT_REG_COLOR_BASE_PS20 = 1;
472 static const uint32 SVGA3D_OUTPUT_REG_COLOR_NUM_PS20 = 4;
473 static const uint32 SVGA3D_OUTPUT_REG_DEPTH_BASE_PS20 = 0;
474 static const uint32 SVGA3D_OUTPUT_REG_DEPTH_NUM_PS20 = 1;
475 
476 /*
477  *----------------------------------------------------------------------
478  *
479  * SVGA3dShaderGetRegType --
480  *
481  *      As the register type is split into two non sequential fields,
482  *      this function provides an useful way of accessing the actual
483  *      register type without having to manually concatenate the
484  *      type_upper and type_lower fields.
485  *
486  * Results:
487  *      Returns the register type.
488  *
489  *----------------------------------------------------------------------
490  */
491 
492 static inline SVGA3dShaderRegType
SVGA3dShaderGetRegType(uint32 token)493 SVGA3dShaderGetRegType(uint32 token)
494 {
495    SVGA3dShaderSrcToken src;
496    src.value = token;
497    return (SVGA3dShaderRegType)(src.type_upper << 3 | src.type_lower);
498 }
499 
500 #endif /* __SVGA3D_SHADER_DEFS__ */
501