st/mesa: determine states used or affected by shaders at compile time
[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 uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
111
112 /* used when bypassing glsl_to_tgsi: */
113 struct gl_shader_program *shader_program;
114
115 struct st_fp_variant *variants;
116 };
117
118
119
120 /** Vertex program variant key */
121 struct st_vp_variant_key
122 {
123 struct st_context *st; /**< variants are per-context */
124 boolean passthrough_edgeflags;
125
126 /** for ARB_color_buffer_float */
127 boolean clamp_color;
128 };
129
130
131 /**
132 * This represents a vertex program, especially translated to match
133 * the inputs of a particular fragment shader.
134 */
135 struct st_vp_variant
136 {
137 /* Parameters which generated this translated version of a vertex
138 * shader:
139 */
140 struct st_vp_variant_key key;
141
142 /**
143 * TGSI tokens (to later generate a 'draw' module shader for
144 * selection/feedback/rasterpos)
145 */
146 struct pipe_shader_state tgsi;
147
148 /** Driver's compiled shader */
149 void *driver_shader;
150
151 /** For using our private draw module (glRasterPos) */
152 struct draw_vertex_shader *draw_shader;
153
154 /** Next in linked list */
155 struct st_vp_variant *next;
156
157 /** similar to that in st_vertex_program, but with edgeflags info too */
158 GLuint num_inputs;
159 };
160
161
162 /**
163 * Derived from Mesa gl_fragment_program:
164 */
165 struct st_vertex_program
166 {
167 struct gl_vertex_program Base; /**< The Mesa vertex program */
168 struct pipe_shader_state tgsi;
169 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
170 uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
171
172 /* used when bypassing glsl_to_tgsi: */
173 struct gl_shader_program *shader_program;
174
175 /** maps a Mesa VERT_ATTRIB_x to a packed TGSI input index */
176 /** maps a TGSI input index back to a Mesa VERT_ATTRIB_x */
177 GLuint index_to_input[PIPE_MAX_SHADER_INPUTS];
178 GLuint num_inputs;
179
180 /** Maps VARYING_SLOT_x to slot */
181 GLuint result_to_output[VARYING_SLOT_MAX];
182
183 /** List of translated variants of this vertex program.
184 */
185 struct st_vp_variant *variants;
186 };
187
188
189
190 /** Key shared by all shaders except VP, FP */
191 struct st_basic_variant_key
192 {
193 struct st_context *st; /**< variants are per-context */
194 };
195
196
197 /**
198 * Geometry program variant.
199 */
200 struct st_basic_variant
201 {
202 /* Parameters which generated this variant. */
203 struct st_basic_variant_key key;
204
205 void *driver_shader;
206
207 struct st_basic_variant *next;
208 };
209
210
211 /**
212 * Derived from Mesa gl_geometry_program:
213 */
214 struct st_geometry_program
215 {
216 struct gl_geometry_program Base; /**< The Mesa geometry program */
217 struct pipe_shader_state tgsi;
218 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
219 uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
220
221 struct st_basic_variant *variants;
222 };
223
224
225 /**
226 * Derived from Mesa gl_tess_ctrl_program:
227 */
228 struct st_tessctrl_program
229 {
230 struct gl_tess_ctrl_program Base; /**< The Mesa tess ctrl program */
231 struct pipe_shader_state tgsi;
232 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
233 uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
234
235 struct st_basic_variant *variants;
236 };
237
238
239 /**
240 * Derived from Mesa gl_tess_eval_program:
241 */
242 struct st_tesseval_program
243 {
244 struct gl_tess_eval_program Base; /**< The Mesa tess eval program */
245 struct pipe_shader_state tgsi;
246 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
247 uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
248
249 struct st_basic_variant *variants;
250 };
251
252
253 /**
254 * Derived from Mesa gl_compute_program:
255 */
256 struct st_compute_program
257 {
258 struct gl_compute_program Base; /**< The Mesa compute program */
259 struct pipe_compute_state tgsi;
260 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
261 uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
262
263 struct st_basic_variant *variants;
264 };
265
266
267 static inline struct st_fragment_program *
268 st_fragment_program( struct gl_fragment_program *fp )
269 {
270 return (struct st_fragment_program *)fp;
271 }
272
273
274 static inline struct st_vertex_program *
275 st_vertex_program( struct gl_vertex_program *vp )
276 {
277 return (struct st_vertex_program *)vp;
278 }
279
280 static inline struct st_geometry_program *
281 st_geometry_program( struct gl_geometry_program *gp )
282 {
283 return (struct st_geometry_program *)gp;
284 }
285
286 static inline struct st_tessctrl_program *
287 st_tessctrl_program( struct gl_tess_ctrl_program *tcp )
288 {
289 return (struct st_tessctrl_program *)tcp;
290 }
291
292 static inline struct st_tesseval_program *
293 st_tesseval_program( struct gl_tess_eval_program *tep )
294 {
295 return (struct st_tesseval_program *)tep;
296 }
297
298 static inline struct st_compute_program *
299 st_compute_program( struct gl_compute_program *cp )
300 {
301 return (struct st_compute_program *)cp;
302 }
303
304 static inline void
305 st_reference_vertprog(struct st_context *st,
306 struct st_vertex_program **ptr,
307 struct st_vertex_program *prog)
308 {
309 _mesa_reference_program(st->ctx,
310 (struct gl_program **) ptr,
311 (struct gl_program *) prog);
312 }
313
314 static inline void
315 st_reference_geomprog(struct st_context *st,
316 struct st_geometry_program **ptr,
317 struct st_geometry_program *prog)
318 {
319 _mesa_reference_program(st->ctx,
320 (struct gl_program **) ptr,
321 (struct gl_program *) prog);
322 }
323
324 static inline void
325 st_reference_fragprog(struct st_context *st,
326 struct st_fragment_program **ptr,
327 struct st_fragment_program *prog)
328 {
329 _mesa_reference_program(st->ctx,
330 (struct gl_program **) ptr,
331 (struct gl_program *) prog);
332 }
333
334 static inline void
335 st_reference_tesscprog(struct st_context *st,
336 struct st_tessctrl_program **ptr,
337 struct st_tessctrl_program *prog)
338 {
339 _mesa_reference_program(st->ctx,
340 (struct gl_program **) ptr,
341 (struct gl_program *) prog);
342 }
343
344 static inline void
345 st_reference_tesseprog(struct st_context *st,
346 struct st_tesseval_program **ptr,
347 struct st_tesseval_program *prog)
348 {
349 _mesa_reference_program(st->ctx,
350 (struct gl_program **) ptr,
351 (struct gl_program *) prog);
352 }
353
354 static inline void
355 st_reference_compprog(struct st_context *st,
356 struct st_compute_program **ptr,
357 struct st_compute_program *prog)
358 {
359 _mesa_reference_program(st->ctx,
360 (struct gl_program **) ptr,
361 (struct gl_program *) prog);
362 }
363
364 /**
365 * This defines mapping from Mesa VARYING_SLOTs to TGSI GENERIC slots.
366 */
367 static inline unsigned
368 st_get_generic_varying_index(struct st_context *st, GLuint attr)
369 {
370 if (attr >= VARYING_SLOT_VAR0) {
371 if (st->needs_texcoord_semantic)
372 return attr - VARYING_SLOT_VAR0;
373 else
374 return 9 + (attr - VARYING_SLOT_VAR0);
375 }
376 if (attr == VARYING_SLOT_PNTC) {
377 assert(!st->needs_texcoord_semantic);
378 return 8;
379 }
380 if (attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7) {
381 assert(!st->needs_texcoord_semantic);
382 return attr - VARYING_SLOT_TEX0;
383 }
384
385 assert(0);
386 return 0;
387 }
388
389
390 extern struct st_vp_variant *
391 st_get_vp_variant(struct st_context *st,
392 struct st_vertex_program *stvp,
393 const struct st_vp_variant_key *key);
394
395
396 extern struct st_fp_variant *
397 st_get_fp_variant(struct st_context *st,
398 struct st_fragment_program *stfp,
399 const struct st_fp_variant_key *key);
400
401 extern struct st_basic_variant *
402 st_get_cp_variant(struct st_context *st,
403 struct pipe_compute_state *tgsi,
404 struct st_basic_variant **variants);
405
406 extern struct st_basic_variant *
407 st_get_basic_variant(struct st_context *st,
408 unsigned pipe_shader,
409 struct pipe_shader_state *tgsi,
410 struct st_basic_variant **variants);
411
412 extern void
413 st_release_vp_variants( struct st_context *st,
414 struct st_vertex_program *stvp );
415
416 extern void
417 st_release_fp_variants( struct st_context *st,
418 struct st_fragment_program *stfp );
419
420 extern void
421 st_release_cp_variants(struct st_context *st,
422 struct st_compute_program *stcp);
423
424 extern void
425 st_release_basic_variants(struct st_context *st, GLenum target,
426 struct st_basic_variant **variants,
427 struct pipe_shader_state *tgsi);
428
429 extern void
430 st_destroy_program_variants(struct st_context *st);
431
432 extern bool
433 st_translate_vertex_program(struct st_context *st,
434 struct st_vertex_program *stvp);
435
436 extern bool
437 st_translate_fragment_program(struct st_context *st,
438 struct st_fragment_program *stfp);
439
440 extern bool
441 st_translate_geometry_program(struct st_context *st,
442 struct st_geometry_program *stgp);
443
444 extern bool
445 st_translate_tessctrl_program(struct st_context *st,
446 struct st_tessctrl_program *sttcp);
447
448 extern bool
449 st_translate_tesseval_program(struct st_context *st,
450 struct st_tesseval_program *sttep);
451
452 extern bool
453 st_translate_compute_program(struct st_context *st,
454 struct st_compute_program *stcp);
455
456 extern void
457 st_print_current_vertex_program(void);
458
459 extern void
460 st_precompile_shader_variant(struct st_context *st,
461 struct gl_program *prog);
462
463 #ifdef __cplusplus
464 }
465 #endif
466
467 #endif