xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/tgsi/tgsi_exec.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2007-2008 VMware, Inc.
4  * All Rights Reserved.
5  * Copyright 2009-2010 VMware, Inc.  All rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  **************************************************************************/
28 
29 #ifndef TGSI_EXEC_H
30 #define TGSI_EXEC_H
31 
32 #include "util/compiler.h"
33 #include "pipe/p_state.h"
34 #include "pipe/p_shader_tokens.h"
35 
36 #if defined __cplusplus
37 extern "C" {
38 #endif
39 
40 #define TGSI_CHAN_X 0
41 #define TGSI_CHAN_Y 1
42 #define TGSI_CHAN_Z 2
43 #define TGSI_CHAN_W 3
44 
45 #define TGSI_NUM_CHANNELS 4  /* R,G,B,A */
46 #define TGSI_QUAD_SIZE    4  /* 4 pixel/quad */
47 
48 #define TGSI_FOR_EACH_CHANNEL( CHAN )\
49    for (CHAN = 0; CHAN < TGSI_NUM_CHANNELS; CHAN++)
50 
51 #define TGSI_IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
52    ((INST)->Dst[0].Register.WriteMask & (1 << (CHAN)))
53 
54 #define TGSI_IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
55    if (TGSI_IS_DST0_CHANNEL_ENABLED( INST, CHAN ))
56 
57 #define TGSI_FOR_EACH_DST0_ENABLED_CHANNEL( INST, CHAN )\
58    TGSI_FOR_EACH_CHANNEL( CHAN )\
59       TGSI_IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )
60 
61 #define TGSI_IS_DST1_CHANNEL_ENABLED( INST, CHAN )\
62    ((INST)->Dst[1].Register.WriteMask & (1 << (CHAN)))
63 
64 #define TGSI_IF_IS_DST1_CHANNEL_ENABLED( INST, CHAN )\
65    if (TGSI_IS_DST1_CHANNEL_ENABLED( INST, CHAN ))
66 
67 #define TGSI_FOR_EACH_DST1_ENABLED_CHANNEL( INST, CHAN )\
68    TGSI_FOR_EACH_CHANNEL( CHAN )\
69       TGSI_IF_IS_DST1_CHANNEL_ENABLED( INST, CHAN )
70 
71 /**
72   * Registers may be treated as float, signed int or unsigned int.
73   */
74 union tgsi_exec_channel
75 {
76    alignas(16)
77    float    f[TGSI_QUAD_SIZE];
78    int32_t  i[TGSI_QUAD_SIZE];
79    uint32_t u[TGSI_QUAD_SIZE];
80 };
81 
82 /**
83   * A vector[RGBA] of channels[4 pixels]
84   */
85 struct tgsi_exec_vector
86 {
87    alignas(16) union tgsi_exec_channel xyzw[TGSI_NUM_CHANNELS];
88 };
89 
90 /**
91  * For fragment programs, information for computing fragment input
92  * values from plane equation of the triangle/line.
93  */
94 struct tgsi_interp_coef
95 {
96    float a0[TGSI_NUM_CHANNELS];	/* in an xyzw layout */
97    float dadx[TGSI_NUM_CHANNELS];
98    float dady[TGSI_NUM_CHANNELS];
99 };
100 
101 enum tgsi_sampler_control
102 {
103    TGSI_SAMPLER_LOD_NONE,
104    TGSI_SAMPLER_LOD_BIAS,
105    TGSI_SAMPLER_LOD_EXPLICIT,
106    TGSI_SAMPLER_LOD_ZERO,
107    TGSI_SAMPLER_DERIVS_EXPLICIT,
108    TGSI_SAMPLER_GATHER,
109 };
110 
111 struct tgsi_image_params {
112    unsigned unit;
113    unsigned tgsi_tex_instr;
114    enum pipe_format format;
115    unsigned execmask;
116 };
117 
118 struct tgsi_image {
119    /* image interfaces */
120    void (*load)(const struct tgsi_image *image,
121                 const struct tgsi_image_params *params,
122                 const int s[TGSI_QUAD_SIZE],
123                 const int t[TGSI_QUAD_SIZE],
124                 const int r[TGSI_QUAD_SIZE],
125                 const int sample[TGSI_QUAD_SIZE],
126                 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE]);
127 
128    void (*store)(const struct tgsi_image *image,
129                  const struct tgsi_image_params *params,
130                  const int s[TGSI_QUAD_SIZE],
131                  const int t[TGSI_QUAD_SIZE],
132                  const int r[TGSI_QUAD_SIZE],
133                  const int sample[TGSI_QUAD_SIZE],
134                  float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE]);
135 
136    void (*op)(const struct tgsi_image *image,
137               const struct tgsi_image_params *params,
138               enum tgsi_opcode opcode,
139               const int s[TGSI_QUAD_SIZE],
140               const int t[TGSI_QUAD_SIZE],
141               const int r[TGSI_QUAD_SIZE],
142               const int sample[TGSI_QUAD_SIZE],
143               float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE],
144               float rgba2[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE]);
145 
146    void (*get_dims)(const struct tgsi_image *image,
147                     const struct tgsi_image_params *params,
148                     int dims[4]);
149 };
150 
151 struct tgsi_buffer_params {
152    unsigned unit;
153    unsigned execmask;
154    unsigned writemask;
155 };
156 
157 /* SSBO interfaces */
158 struct tgsi_buffer {
159    void *(*lookup)(const struct tgsi_buffer *buffer,
160                    uint32_t unit, uint32_t *size);
161 };
162 
163 /**
164  * Information for sampling textures, which must be implemented
165  * by code outside the TGSI executor.
166  */
167 struct tgsi_sampler
168 {
169    /** Get samples for four fragments in a quad */
170    /* this interface contains 5 sets of channels that vary
171     * depending on the sampler.
172     * s - the first texture coordinate for sampling.
173     * t - the second texture coordinate for sampling - unused for 1D,
174           layer for 1D arrays.
175     * r - the third coordinate for sampling for 3D, cube, cube arrays,
176     *     layer for 2D arrays. Compare value for 1D/2D shadows.
177     * c0 - Compare value for shadow cube and shadow 2d arrays,
178     *      layer for cube arrays.
179     * derivs - explicit derivatives.
180     * offset - texel offsets
181     * lod - lod value, except for shadow cube arrays (compare value there).
182     */
183    void (*get_samples)(struct tgsi_sampler *sampler,
184                        const unsigned sview_index,
185                        const unsigned sampler_index,
186                        const float s[TGSI_QUAD_SIZE],
187                        const float t[TGSI_QUAD_SIZE],
188                        const float r[TGSI_QUAD_SIZE],
189                        const float c0[TGSI_QUAD_SIZE],
190                        const float c1[TGSI_QUAD_SIZE],
191                        float derivs[3][2][TGSI_QUAD_SIZE],
192                        const int8_t offset[3],
193                        enum tgsi_sampler_control control,
194                        float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE]);
195    void (*get_dims)(struct tgsi_sampler *sampler,
196                     const unsigned sview_index,
197                     int level, int dims[4]);
198    void (*get_texel)(struct tgsi_sampler *sampler,
199                      const unsigned sview_index,
200                      const int i[TGSI_QUAD_SIZE],
201                      const int j[TGSI_QUAD_SIZE], const int k[TGSI_QUAD_SIZE],
202                      const int lod[TGSI_QUAD_SIZE], const int8_t offset[3],
203                      float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE]);
204    void (*query_lod)(const struct tgsi_sampler *tgsi_sampler,
205                      const unsigned sview_index,
206                      const unsigned sampler_index,
207                      const float s[TGSI_QUAD_SIZE],
208                      const float t[TGSI_QUAD_SIZE],
209                      const float p[TGSI_QUAD_SIZE],
210                      const float c0[TGSI_QUAD_SIZE],
211                      const enum tgsi_sampler_control control,
212                      float mipmap[TGSI_QUAD_SIZE],
213                      float lod[TGSI_QUAD_SIZE]);
214 };
215 
216 #define TGSI_EXEC_NUM_TEMPS       4096
217 
218 #define TGSI_EXEC_MAX_NESTING  32
219 #define TGSI_EXEC_MAX_COND_NESTING  TGSI_EXEC_MAX_NESTING
220 #define TGSI_EXEC_MAX_LOOP_NESTING  TGSI_EXEC_MAX_NESTING
221 #define TGSI_EXEC_MAX_SWITCH_NESTING TGSI_EXEC_MAX_NESTING
222 #define TGSI_EXEC_MAX_CALL_NESTING  TGSI_EXEC_MAX_NESTING
223 
224 /* The maximum number of input attributes per vertex. For 2D
225  * input register files, this is the stride between two 1D
226  * arrays.
227  */
228 #define TGSI_EXEC_MAX_INPUT_ATTRIBS 32
229 
230 /* The maximum number of bytes per constant buffer.
231  */
232 #define TGSI_EXEC_MAX_CONST_BUFFER_SIZE  (4096 * sizeof(float[4]))
233 
234 /* The maximum number of vertices per primitive */
235 #define TGSI_MAX_PRIM_VERTICES 6
236 
237 /* The maximum number of primitives to be generated */
238 #define TGSI_MAX_PRIMITIVES 64
239 
240 /* The maximum total number of vertices */
241 #define TGSI_MAX_TOTAL_VERTICES (TGSI_MAX_PRIM_VERTICES * TGSI_MAX_PRIMITIVES * PIPE_MAX_ATTRIBS)
242 
243 #define TGSI_MAX_MISC_INPUTS 8
244 
245 #define TGSI_MAX_VERTEX_STREAMS 4
246 
247 /** function call/activation record */
248 struct tgsi_call_record
249 {
250    unsigned CondStackTop;
251    unsigned LoopStackTop;
252    unsigned ContStackTop;
253    int SwitchStackTop;
254    int BreakStackTop;
255    unsigned ReturnAddr;
256 };
257 
258 /* should match draw_buffer_info */
259 struct tgsi_exec_consts_info {
260    const void *ptr;
261    unsigned size;
262 };
263 
264 /* Switch-case block state. */
265 struct tgsi_switch_record {
266    unsigned mask;                          /**< execution mask */
267    union tgsi_exec_channel selector;   /**< a value case statements are compared to */
268    unsigned defaultMask;                   /**< non-execute mask for default case */
269 };
270 
271 
272 enum tgsi_break_type {
273    TGSI_EXEC_BREAK_INSIDE_LOOP,
274    TGSI_EXEC_BREAK_INSIDE_SWITCH
275 };
276 
277 
278 #define TGSI_EXEC_MAX_BREAK_STACK (TGSI_EXEC_MAX_LOOP_NESTING + TGSI_EXEC_MAX_SWITCH_NESTING)
279 
280 typedef float float4[4];
281 
282 struct tgsi_exec_machine;
283 
284 typedef void (* apply_sample_offset_func)(
285    const struct tgsi_exec_machine *mach,
286    unsigned attrib,
287    unsigned chan,
288    float ofs_x,
289    float ofs_y,
290    union tgsi_exec_channel *out_chan);
291 
292 /**
293  * Run-time virtual machine state for executing TGSI shader.
294  */
295 struct tgsi_exec_machine
296 {
297    /* Total = program temporaries + internal temporaries
298     */
299    alignas(16)
300    struct tgsi_exec_vector       Temps[TGSI_EXEC_NUM_TEMPS];
301 
302    unsigned                       ImmsReserved;
303    float4                         *Imms;
304 
305    struct tgsi_exec_vector       *Inputs;
306    struct tgsi_exec_vector       *Outputs;
307    apply_sample_offset_func           *InputSampleOffsetApply;
308 
309    /* System values */
310    unsigned                      SysSemanticToIndex[TGSI_SEMANTIC_COUNT];
311    struct tgsi_exec_vector       SystemValue[TGSI_MAX_MISC_INPUTS];
312 
313    struct tgsi_exec_vector       Addrs[3];
314 
315    struct tgsi_sampler           *Sampler;
316 
317    struct tgsi_image             *Image;
318    struct tgsi_buffer            *Buffer;
319    unsigned                      ImmLimit;
320 
321    const void *Consts[PIPE_MAX_CONSTANT_BUFFERS];
322    unsigned ConstsSize[PIPE_MAX_CONSTANT_BUFFERS];
323 
324    const struct tgsi_token       *Tokens;   /**< Declarations, instructions */
325    enum pipe_shader_type         ShaderType; /**< PIPE_SHADER_x */
326 
327    /* GEOMETRY processor only. */
328    /* Number of vertices emitted per emitted primitive. */
329    unsigned                      *Primitives[TGSI_MAX_VERTEX_STREAMS];
330    /* Offsets in ->Outputs of the primitives' vertex output data */
331    unsigned                      *PrimitiveOffsets[TGSI_MAX_VERTEX_STREAMS];
332    unsigned                       NumOutputs;
333    unsigned                       MaxOutputVertices;
334    /* Offset in ->Outputs for the current vertex to be emitted. */
335    unsigned                       OutputVertexOffset;
336    /* Number of primitives emitted. */
337    unsigned                       OutputPrimCount[TGSI_MAX_VERTEX_STREAMS];
338 
339    /* FRAGMENT processor only. */
340    const struct tgsi_interp_coef *InterpCoefs;
341    struct tgsi_exec_vector       QuadPos;
342    float                         Face;    /**< +1 if front facing, -1 if back facing */
343    bool                          flatshade_color;
344 
345    /* Compute Only */
346    void                          *LocalMem;
347    unsigned                      LocalMemSize;
348 
349    /* See GLSL 4.50 specification for definition of helper invocations */
350    unsigned NonHelperMask;  /**< non-helpers */
351    /* Conditional execution masks */
352    unsigned CondMask;  /**< For IF/ELSE/ENDIF */
353    unsigned LoopMask;  /**< For BGNLOOP/ENDLOOP */
354    unsigned ContMask;  /**< For loop CONT statements */
355    unsigned FuncMask;  /**< For function calls */
356    unsigned ExecMask;  /**< = CondMask & LoopMask */
357    unsigned KillMask;  /**< Mask of channels killed in the current shader execution */
358 
359    /* Current switch-case state. */
360    struct tgsi_switch_record Switch;
361 
362    /* Current break type. */
363    enum tgsi_break_type BreakType;
364 
365    /** Condition mask stack (for nested conditionals) */
366    unsigned CondStack[TGSI_EXEC_MAX_COND_NESTING];
367    int CondStackTop;
368 
369    /** Loop mask stack (for nested loops) */
370    unsigned LoopStack[TGSI_EXEC_MAX_LOOP_NESTING];
371    int LoopStackTop;
372 
373    /** Loop label stack */
374    unsigned LoopLabelStack[TGSI_EXEC_MAX_LOOP_NESTING];
375    int LoopLabelStackTop;
376 
377    /** Loop continue mask stack (see comments in tgsi_exec.c) */
378    unsigned ContStack[TGSI_EXEC_MAX_LOOP_NESTING];
379    int ContStackTop;
380 
381    /** Switch case stack */
382    struct tgsi_switch_record SwitchStack[TGSI_EXEC_MAX_SWITCH_NESTING];
383    int SwitchStackTop;
384 
385    enum tgsi_break_type BreakStack[TGSI_EXEC_MAX_BREAK_STACK];
386    int BreakStackTop;
387 
388    /** Function execution mask stack (for executing subroutine code) */
389    unsigned FuncStack[TGSI_EXEC_MAX_CALL_NESTING];
390    int FuncStackTop;
391 
392    /** Function call stack for saving/restoring the program counter */
393    struct tgsi_call_record CallStack[TGSI_EXEC_MAX_CALL_NESTING];
394    int CallStackTop;
395 
396    struct tgsi_full_instruction *Instructions;
397    unsigned NumInstructions;
398 
399    struct tgsi_full_declaration *Declarations;
400    unsigned NumDeclarations;
401 
402    struct tgsi_declaration_sampler_view
403       SamplerViews[PIPE_MAX_SHADER_SAMPLER_VIEWS];
404 
405    bool UsedGeometryShader;
406 
407    int pc;
408 };
409 
410 struct tgsi_exec_machine *
411 tgsi_exec_machine_create(enum pipe_shader_type shader_type);
412 
413 void
414 tgsi_exec_machine_destroy(struct tgsi_exec_machine *mach);
415 
416 
417 void
418 tgsi_exec_machine_bind_shader(
419    struct tgsi_exec_machine *mach,
420    const struct tgsi_token *tokens,
421    struct tgsi_sampler *sampler,
422    struct tgsi_image *image,
423    struct tgsi_buffer *buffer);
424 
425 uint
426 tgsi_exec_machine_run(
427    struct tgsi_exec_machine *mach, int start_pc );
428 
429 
430 extern void
431 tgsi_exec_set_constant_buffers(struct tgsi_exec_machine *mach,
432                                unsigned num_bufs,
433                                const struct tgsi_exec_consts_info *bufs);
434 
435 
436 static inline int
tgsi_exec_get_shader_param(enum pipe_shader_cap param)437 tgsi_exec_get_shader_param(enum pipe_shader_cap param)
438 {
439    switch(param) {
440    case PIPE_SHADER_CAP_MAX_INSTRUCTIONS:
441    case PIPE_SHADER_CAP_MAX_ALU_INSTRUCTIONS:
442    case PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS:
443    case PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS:
444       return INT_MAX;
445    case PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH:
446       return TGSI_EXEC_MAX_NESTING;
447    case PIPE_SHADER_CAP_MAX_INPUTS:
448       return TGSI_EXEC_MAX_INPUT_ATTRIBS;
449    case PIPE_SHADER_CAP_MAX_OUTPUTS:
450       return 32;
451    case PIPE_SHADER_CAP_MAX_CONST_BUFFER0_SIZE:
452       return TGSI_EXEC_MAX_CONST_BUFFER_SIZE;
453    case PIPE_SHADER_CAP_MAX_CONST_BUFFERS:
454       return PIPE_MAX_CONSTANT_BUFFERS;
455    case PIPE_SHADER_CAP_MAX_TEMPS:
456       return TGSI_EXEC_NUM_TEMPS;
457    case PIPE_SHADER_CAP_CONT_SUPPORTED:
458       return 1;
459    case PIPE_SHADER_CAP_INDIRECT_INPUT_ADDR:
460    case PIPE_SHADER_CAP_INDIRECT_OUTPUT_ADDR:
461    case PIPE_SHADER_CAP_INDIRECT_TEMP_ADDR:
462    case PIPE_SHADER_CAP_INDIRECT_CONST_ADDR:
463       return 1;
464    case PIPE_SHADER_CAP_SUBROUTINES:
465       return 1;
466    case PIPE_SHADER_CAP_INTEGERS:
467       return 1;
468    case PIPE_SHADER_CAP_INT64_ATOMICS:
469    case PIPE_SHADER_CAP_FP16:
470    case PIPE_SHADER_CAP_FP16_DERIVATIVES:
471    case PIPE_SHADER_CAP_FP16_CONST_BUFFERS:
472    case PIPE_SHADER_CAP_INT16:
473    case PIPE_SHADER_CAP_GLSL_16BIT_CONSTS:
474       return 0;
475    case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS:
476       return PIPE_MAX_SAMPLERS;
477    case PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS:
478       return PIPE_MAX_SHADER_SAMPLER_VIEWS;
479    case PIPE_SHADER_CAP_SUPPORTED_IRS:
480       return 1 << PIPE_SHADER_IR_TGSI;
481    case PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED:
482       return 1;
483    case PIPE_SHADER_CAP_TGSI_ANY_INOUT_DECL_RANGE:
484       return 1;
485    case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTERS:
486    case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTER_BUFFERS:
487       return 0;
488    case PIPE_SHADER_CAP_MAX_SHADER_BUFFERS:
489       return PIPE_MAX_SHADER_BUFFERS;
490    case PIPE_SHADER_CAP_MAX_SHADER_IMAGES:
491       return PIPE_MAX_SHADER_IMAGES;
492    }
493    /* if we get here, we missed a shader cap above (and should have seen
494     * a compiler warning.)
495     */
496    return 0;
497 }
498 
499 #if defined __cplusplus
500 } /* extern "C" */
501 #endif
502 
503 #endif /* TGSI_EXEC_H */
504