mesa/st: add support for NIR as possible driver IR
[mesa.git] / src / mesa / state_tracker / st_program.h
1 /**************************************************************************
2 *
3 * Copyright 2003 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <keithw@vmware.com>
31 */
32
33
34 #ifndef ST_PROGRAM_H
35 #define ST_PROGRAM_H
36
37 #include "main/mtypes.h"
38 #include "main/atifragshader.h"
39 #include "program/program.h"
40 #include "pipe/p_state.h"
41 #include "st_context.h"
42 #include "st_glsl_to_tgsi.h"
43
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 #define ST_DOUBLE_ATTRIB_PLACEHOLDER 0xffffffff
50
51 /** Fragment program variant key */
52 struct st_fp_variant_key
53 {
54 struct st_context *st; /**< variants are per-context */
55
56 /** for glBitmap */
57 GLuint bitmap:1; /**< glBitmap variant? */
58
59 /** for glDrawPixels */
60 GLuint drawpixels:1; /**< glDrawPixels variant */
61 GLuint scaleAndBias:1; /**< glDrawPixels w/ scale and/or bias? */
62 GLuint pixelMaps:1; /**< glDrawPixels w/ pixel lookup map? */
63
64 /** for ARB_color_buffer_float */
65 GLuint clamp_color:1;
66
67 /** for ARB_sample_shading */
68 GLuint persample_shading:1;
69
70 /** needed for ATI_fragment_shader */
71 GLuint fog:2;
72
73 /** needed for ATI_fragment_shader */
74 char texture_targets[MAX_NUM_FRAGMENT_REGISTERS_ATI];
75 };
76
77
78 /**
79 * Variant of a fragment program.
80 */
81 struct st_fp_variant
82 {
83 /** Parameters which generated this version of fragment program */
84 struct st_fp_variant_key key;
85
86 /** Driver's compiled shader */
87 void *driver_shader;
88
89 /** For glBitmap variants */
90 uint bitmap_sampler;
91
92 /** For glDrawPixels variants */
93 unsigned drawpix_sampler;
94 unsigned pixelmap_sampler;
95
96 /** next in linked list */
97 struct st_fp_variant *next;
98 };
99
100
101 /**
102 * Derived from Mesa gl_fragment_program:
103 */
104 struct st_fragment_program
105 {
106 struct gl_fragment_program Base;
107 struct pipe_shader_state tgsi;
108 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
109 struct ati_fragment_shader *ati_fs;
110
111 /* used when bypassing glsl_to_tgsi: */
112 struct gl_shader_program *shader_program;
113
114 struct st_fp_variant *variants;
115 };
116
117
118
119 /** Vertex program variant key */
120 struct st_vp_variant_key
121 {
122 struct st_context *st; /**< variants are per-context */
123 boolean passthrough_edgeflags;
124
125 /** for ARB_color_buffer_float */
126 boolean clamp_color;
127 };
128
129
130 /**
131 * This represents a vertex program, especially translated to match
132 * the inputs of a particular fragment shader.
133 */
134 struct st_vp_variant
135 {
136 /* Parameters which generated this translated version of a vertex
137 * shader:
138 */
139 struct st_vp_variant_key key;
140
141 /**
142 * TGSI tokens (to later generate a 'draw' module shader for
143 * selection/feedback/rasterpos)
144 */
145 struct pipe_shader_state tgsi;
146
147 /** Driver's compiled shader */
148 void *driver_shader;
149
150 /** For using our private draw module (glRasterPos) */
151 struct draw_vertex_shader *draw_shader;
152
153 /** Next in linked list */
154 struct st_vp_variant *next;
155
156 /** similar to that in st_vertex_program, but with edgeflags info too */
157 GLuint num_inputs;
158 };
159
160
161 /**
162 * Derived from Mesa gl_fragment_program:
163 */
164 struct st_vertex_program
165 {
166 struct gl_vertex_program Base; /**< The Mesa vertex program */
167 struct pipe_shader_state tgsi;
168 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
169
170 /* used when bypassing glsl_to_tgsi: */
171 struct gl_shader_program *shader_program;
172
173 /** maps a Mesa VERT_ATTRIB_x to a packed TGSI input index */
174 /** maps a TGSI input index back to a Mesa VERT_ATTRIB_x */
175 GLuint index_to_input[PIPE_MAX_SHADER_INPUTS];
176 GLuint num_inputs;
177
178 /** Maps VARYING_SLOT_x to slot */
179 GLuint result_to_output[VARYING_SLOT_MAX];
180
181 /** List of translated variants of this vertex program.
182 */
183 struct st_vp_variant *variants;
184 };
185
186
187
188 /** Key shared by all shaders except VP, FP */
189 struct st_basic_variant_key
190 {
191 struct st_context *st; /**< variants are per-context */
192 };
193
194
195 /**
196 * Geometry program variant.
197 */
198 struct st_basic_variant
199 {
200 /* Parameters which generated this variant. */
201 struct st_basic_variant_key key;
202
203 void *driver_shader;
204
205 struct st_basic_variant *next;
206 };
207
208
209 /**
210 * Derived from Mesa gl_geometry_program:
211 */
212 struct st_geometry_program
213 {
214 struct gl_geometry_program Base; /**< The Mesa geometry program */
215 struct pipe_shader_state tgsi;
216 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
217
218 struct st_basic_variant *variants;
219 };
220
221
222 /**
223 * Derived from Mesa gl_tess_ctrl_program:
224 */
225 struct st_tessctrl_program
226 {
227 struct gl_tess_ctrl_program Base; /**< The Mesa tess ctrl program */
228 struct pipe_shader_state tgsi;
229 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
230
231 struct st_basic_variant *variants;
232 };
233
234
235 /**
236 * Derived from Mesa gl_tess_eval_program:
237 */
238 struct st_tesseval_program
239 {
240 struct gl_tess_eval_program Base; /**< The Mesa tess eval program */
241 struct pipe_shader_state tgsi;
242 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
243
244 struct st_basic_variant *variants;
245 };
246
247
248 /**
249 * Derived from Mesa gl_compute_program:
250 */
251 struct st_compute_program
252 {
253 struct gl_compute_program Base; /**< The Mesa compute program */
254 struct pipe_compute_state tgsi;
255 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
256
257 struct st_basic_variant *variants;
258 };
259
260
261 static inline struct st_fragment_program *
262 st_fragment_program( struct gl_fragment_program *fp )
263 {
264 return (struct st_fragment_program *)fp;
265 }
266
267
268 static inline struct st_vertex_program *
269 st_vertex_program( struct gl_vertex_program *vp )
270 {
271 return (struct st_vertex_program *)vp;
272 }
273
274 static inline struct st_geometry_program *
275 st_geometry_program( struct gl_geometry_program *gp )
276 {
277 return (struct st_geometry_program *)gp;
278 }
279
280 static inline struct st_tessctrl_program *
281 st_tessctrl_program( struct gl_tess_ctrl_program *tcp )
282 {
283 return (struct st_tessctrl_program *)tcp;
284 }
285
286 static inline struct st_tesseval_program *
287 st_tesseval_program( struct gl_tess_eval_program *tep )
288 {
289 return (struct st_tesseval_program *)tep;
290 }
291
292 static inline struct st_compute_program *
293 st_compute_program( struct gl_compute_program *cp )
294 {
295 return (struct st_compute_program *)cp;
296 }
297
298 static inline void
299 st_reference_vertprog(struct st_context *st,
300 struct st_vertex_program **ptr,
301 struct st_vertex_program *prog)
302 {
303 _mesa_reference_program(st->ctx,
304 (struct gl_program **) ptr,
305 (struct gl_program *) prog);
306 }
307
308 static inline void
309 st_reference_geomprog(struct st_context *st,
310 struct st_geometry_program **ptr,
311 struct st_geometry_program *prog)
312 {
313 _mesa_reference_program(st->ctx,
314 (struct gl_program **) ptr,
315 (struct gl_program *) prog);
316 }
317
318 static inline void
319 st_reference_fragprog(struct st_context *st,
320 struct st_fragment_program **ptr,
321 struct st_fragment_program *prog)
322 {
323 _mesa_reference_program(st->ctx,
324 (struct gl_program **) ptr,
325 (struct gl_program *) prog);
326 }
327
328 static inline void
329 st_reference_tesscprog(struct st_context *st,
330 struct st_tessctrl_program **ptr,
331 struct st_tessctrl_program *prog)
332 {
333 _mesa_reference_program(st->ctx,
334 (struct gl_program **) ptr,
335 (struct gl_program *) prog);
336 }
337
338 static inline void
339 st_reference_tesseprog(struct st_context *st,
340 struct st_tesseval_program **ptr,
341 struct st_tesseval_program *prog)
342 {
343 _mesa_reference_program(st->ctx,
344 (struct gl_program **) ptr,
345 (struct gl_program *) prog);
346 }
347
348 static inline void
349 st_reference_compprog(struct st_context *st,
350 struct st_compute_program **ptr,
351 struct st_compute_program *prog)
352 {
353 _mesa_reference_program(st->ctx,
354 (struct gl_program **) ptr,
355 (struct gl_program *) prog);
356 }
357
358 /**
359 * This defines mapping from Mesa VARYING_SLOTs to TGSI GENERIC slots.
360 */
361 static inline unsigned
362 st_get_generic_varying_index(struct st_context *st, GLuint attr)
363 {
364 if (attr >= VARYING_SLOT_VAR0) {
365 if (st->needs_texcoord_semantic)
366 return attr - VARYING_SLOT_VAR0;
367 else
368 return 9 + (attr - VARYING_SLOT_VAR0);
369 }
370 if (attr == VARYING_SLOT_PNTC) {
371 assert(!st->needs_texcoord_semantic);
372 return 8;
373 }
374 if (attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7) {
375 assert(!st->needs_texcoord_semantic);
376 return attr - VARYING_SLOT_TEX0;
377 }
378
379 assert(0);
380 return 0;
381 }
382
383
384 extern struct st_vp_variant *
385 st_get_vp_variant(struct st_context *st,
386 struct st_vertex_program *stvp,
387 const struct st_vp_variant_key *key);
388
389
390 extern struct st_fp_variant *
391 st_get_fp_variant(struct st_context *st,
392 struct st_fragment_program *stfp,
393 const struct st_fp_variant_key *key);
394
395 extern struct st_basic_variant *
396 st_get_cp_variant(struct st_context *st,
397 struct pipe_compute_state *tgsi,
398 struct st_basic_variant **variants);
399
400 extern struct st_basic_variant *
401 st_get_basic_variant(struct st_context *st,
402 unsigned pipe_shader,
403 struct pipe_shader_state *tgsi,
404 struct st_basic_variant **variants);
405
406 extern void
407 st_release_vp_variants( struct st_context *st,
408 struct st_vertex_program *stvp );
409
410 extern void
411 st_release_fp_variants( struct st_context *st,
412 struct st_fragment_program *stfp );
413
414 extern void
415 st_release_cp_variants(struct st_context *st,
416 struct st_compute_program *stcp);
417
418 extern void
419 st_release_basic_variants(struct st_context *st, GLenum target,
420 struct st_basic_variant **variants,
421 struct pipe_shader_state *tgsi);
422
423 extern void
424 st_destroy_program_variants(struct st_context *st);
425
426 extern bool
427 st_translate_vertex_program(struct st_context *st,
428 struct st_vertex_program *stvp);
429
430 extern bool
431 st_translate_fragment_program(struct st_context *st,
432 struct st_fragment_program *stfp);
433
434 extern bool
435 st_translate_geometry_program(struct st_context *st,
436 struct st_geometry_program *stgp);
437
438 extern bool
439 st_translate_tessctrl_program(struct st_context *st,
440 struct st_tessctrl_program *sttcp);
441
442 extern bool
443 st_translate_tesseval_program(struct st_context *st,
444 struct st_tesseval_program *sttep);
445
446 extern bool
447 st_translate_compute_program(struct st_context *st,
448 struct st_compute_program *stcp);
449
450 extern void
451 st_print_current_vertex_program(void);
452
453 extern void
454 st_precompile_shader_variant(struct st_context *st,
455 struct gl_program *prog);
456
457 #ifdef __cplusplus
458 }
459 #endif
460
461 #endif