st/mesa: translate fragment shaders into TGSI when we get them
[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 "program/program.h"
39 #include "pipe/p_state.h"
40 #include "st_context.h"
41 #include "st_glsl_to_tgsi.h"
42
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 #define ST_DOUBLE_ATTRIB_PLACEHOLDER 0xffffffff
49
50 /** Fragment program variant key */
51 struct st_fp_variant_key
52 {
53 struct st_context *st; /**< variants are per-context */
54
55 /** for glBitmap */
56 GLuint bitmap:1; /**< glBitmap variant? */
57
58 /** for glDrawPixels */
59 GLuint drawpixels:1; /**< glDrawPixels variant */
60 GLuint scaleAndBias:1; /**< glDrawPixels w/ scale and/or bias? */
61 GLuint pixelMaps:1; /**< glDrawPixels w/ pixel lookup map? */
62
63 /** for ARB_color_buffer_float */
64 GLuint clamp_color:1;
65
66 /** for ARB_sample_shading */
67 GLuint persample_shading:1;
68 };
69
70
71 /**
72 * Variant of a fragment program.
73 */
74 struct st_fp_variant
75 {
76 /** Parameters which generated this version of fragment program */
77 struct st_fp_variant_key key;
78
79 /** Driver's compiled shader */
80 void *driver_shader;
81
82 /** For glBitmap variants */
83 struct gl_program_parameter_list *parameters;
84 uint bitmap_sampler;
85
86 /** For glDrawPixels variants */
87 unsigned drawpix_sampler;
88 unsigned pixelmap_sampler;
89
90 /** next in linked list */
91 struct st_fp_variant *next;
92 };
93
94
95 /**
96 * Derived from Mesa gl_fragment_program:
97 */
98 struct st_fragment_program
99 {
100 struct gl_fragment_program Base;
101 struct pipe_shader_state tgsi;
102 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
103
104 struct st_fp_variant *variants;
105 };
106
107
108
109 /** Vertex program variant key */
110 struct st_vp_variant_key
111 {
112 struct st_context *st; /**< variants are per-context */
113 boolean passthrough_edgeflags;
114
115 /** for ARB_color_buffer_float */
116 boolean clamp_color;
117 };
118
119
120 /**
121 * This represents a vertex program, especially translated to match
122 * the inputs of a particular fragment shader.
123 */
124 struct st_vp_variant
125 {
126 /* Parameters which generated this translated version of a vertex
127 * shader:
128 */
129 struct st_vp_variant_key key;
130
131 /**
132 * TGSI tokens (to later generate a 'draw' module shader for
133 * selection/feedback/rasterpos)
134 */
135 struct pipe_shader_state tgsi;
136
137 /** Driver's compiled shader */
138 void *driver_shader;
139
140 /** For using our private draw module (glRasterPos) */
141 struct draw_vertex_shader *draw_shader;
142
143 /** Next in linked list */
144 struct st_vp_variant *next;
145
146 /** similar to that in st_vertex_program, but with edgeflags info too */
147 GLuint num_inputs;
148 };
149
150
151 /**
152 * Derived from Mesa gl_fragment_program:
153 */
154 struct st_vertex_program
155 {
156 struct gl_vertex_program Base; /**< The Mesa vertex program */
157 struct pipe_shader_state tgsi;
158 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
159
160 /** maps a Mesa VERT_ATTRIB_x to a packed TGSI input index */
161 /** maps a TGSI input index back to a Mesa VERT_ATTRIB_x */
162 GLuint index_to_input[PIPE_MAX_SHADER_INPUTS];
163 GLuint num_inputs;
164
165 /** Maps VARYING_SLOT_x to slot */
166 GLuint result_to_output[VARYING_SLOT_MAX];
167
168 /** List of translated variants of this vertex program.
169 */
170 struct st_vp_variant *variants;
171 };
172
173
174
175 /** Geometry program variant key */
176 struct st_gp_variant_key
177 {
178 struct st_context *st; /**< variants are per-context */
179 /* no other fields yet */
180 };
181
182
183 /**
184 * Geometry program variant.
185 */
186 struct st_gp_variant
187 {
188 /* Parameters which generated this variant. */
189 struct st_gp_variant_key key;
190
191 void *driver_shader;
192
193 struct st_gp_variant *next;
194 };
195
196
197 /**
198 * Derived from Mesa gl_geometry_program:
199 */
200 struct st_geometry_program
201 {
202 struct gl_geometry_program Base; /**< The Mesa geometry program */
203 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
204
205 struct st_gp_variant *variants;
206 };
207
208
209
210 /** Tessellation control program variant key */
211 struct st_tcp_variant_key
212 {
213 struct st_context *st; /**< variants are per-context */
214 /* no other fields yet */
215 };
216
217
218 /**
219 * Tessellation control program variant.
220 */
221 struct st_tcp_variant
222 {
223 /* Parameters which generated this variant. */
224 struct st_tcp_variant_key key;
225
226 void *driver_shader;
227
228 struct st_tcp_variant *next;
229 };
230
231
232 /**
233 * Derived from Mesa gl_tess_ctrl_program:
234 */
235 struct st_tessctrl_program
236 {
237 struct gl_tess_ctrl_program Base; /**< The Mesa tess ctrl program */
238 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
239
240 struct st_tcp_variant *variants;
241 };
242
243
244
245 /** Tessellation evaluation program variant key */
246 struct st_tep_variant_key
247 {
248 struct st_context *st; /**< variants are per-context */
249 /* no other fields yet */
250 };
251
252
253 /**
254 * Tessellation evaluation program variant.
255 */
256 struct st_tep_variant
257 {
258 /* Parameters which generated this variant. */
259 struct st_tep_variant_key key;
260
261 void *driver_shader;
262
263 struct st_tep_variant *next;
264 };
265
266
267 /**
268 * Derived from Mesa gl_tess_eval_program:
269 */
270 struct st_tesseval_program
271 {
272 struct gl_tess_eval_program Base; /**< The Mesa tess eval program */
273 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
274
275 struct st_tep_variant *variants;
276 };
277
278
279
280 static inline struct st_fragment_program *
281 st_fragment_program( struct gl_fragment_program *fp )
282 {
283 return (struct st_fragment_program *)fp;
284 }
285
286
287 static inline struct st_vertex_program *
288 st_vertex_program( struct gl_vertex_program *vp )
289 {
290 return (struct st_vertex_program *)vp;
291 }
292
293 static inline struct st_geometry_program *
294 st_geometry_program( struct gl_geometry_program *gp )
295 {
296 return (struct st_geometry_program *)gp;
297 }
298
299 static inline struct st_tessctrl_program *
300 st_tessctrl_program( struct gl_tess_ctrl_program *tcp )
301 {
302 return (struct st_tessctrl_program *)tcp;
303 }
304
305 static inline struct st_tesseval_program *
306 st_tesseval_program( struct gl_tess_eval_program *tep )
307 {
308 return (struct st_tesseval_program *)tep;
309 }
310
311 static inline void
312 st_reference_vertprog(struct st_context *st,
313 struct st_vertex_program **ptr,
314 struct st_vertex_program *prog)
315 {
316 _mesa_reference_program(st->ctx,
317 (struct gl_program **) ptr,
318 (struct gl_program *) prog);
319 }
320
321 static inline void
322 st_reference_geomprog(struct st_context *st,
323 struct st_geometry_program **ptr,
324 struct st_geometry_program *prog)
325 {
326 _mesa_reference_program(st->ctx,
327 (struct gl_program **) ptr,
328 (struct gl_program *) prog);
329 }
330
331 static inline void
332 st_reference_fragprog(struct st_context *st,
333 struct st_fragment_program **ptr,
334 struct st_fragment_program *prog)
335 {
336 _mesa_reference_program(st->ctx,
337 (struct gl_program **) ptr,
338 (struct gl_program *) prog);
339 }
340
341 static inline void
342 st_reference_tesscprog(struct st_context *st,
343 struct st_tessctrl_program **ptr,
344 struct st_tessctrl_program *prog)
345 {
346 _mesa_reference_program(st->ctx,
347 (struct gl_program **) ptr,
348 (struct gl_program *) prog);
349 }
350
351 static inline void
352 st_reference_tesseprog(struct st_context *st,
353 struct st_tesseval_program **ptr,
354 struct st_tesseval_program *prog)
355 {
356 _mesa_reference_program(st->ctx,
357 (struct gl_program **) ptr,
358 (struct gl_program *) prog);
359 }
360
361 /**
362 * This defines mapping from Mesa VARYING_SLOTs to TGSI GENERIC slots.
363 */
364 static inline unsigned
365 st_get_generic_varying_index(struct st_context *st, GLuint attr)
366 {
367 if (attr >= VARYING_SLOT_VAR0) {
368 if (st->needs_texcoord_semantic)
369 return attr - VARYING_SLOT_VAR0;
370 else
371 return 9 + (attr - VARYING_SLOT_VAR0);
372 }
373 if (attr == VARYING_SLOT_PNTC) {
374 assert(!st->needs_texcoord_semantic);
375 return 8;
376 }
377 if (attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7) {
378 assert(!st->needs_texcoord_semantic);
379 return attr - VARYING_SLOT_TEX0;
380 }
381
382 assert(0);
383 return 0;
384 }
385
386
387 extern struct st_vp_variant *
388 st_get_vp_variant(struct st_context *st,
389 struct st_vertex_program *stvp,
390 const struct st_vp_variant_key *key);
391
392
393 extern struct st_fp_variant *
394 st_get_fp_variant(struct st_context *st,
395 struct st_fragment_program *stfp,
396 const struct st_fp_variant_key *key);
397
398
399 extern struct st_gp_variant *
400 st_get_gp_variant(struct st_context *st,
401 struct st_geometry_program *stgp,
402 const struct st_gp_variant_key *key);
403
404 extern struct st_tcp_variant *
405 st_get_tcp_variant(struct st_context *st,
406 struct st_tessctrl_program *stgp,
407 const struct st_tcp_variant_key *key);
408
409 extern struct st_tep_variant *
410 st_get_tep_variant(struct st_context *st,
411 struct st_tesseval_program *stgp,
412 const struct st_tep_variant_key *key);
413
414 extern void
415 st_release_vp_variants( struct st_context *st,
416 struct st_vertex_program *stvp );
417
418 extern void
419 st_release_fp_variants( struct st_context *st,
420 struct st_fragment_program *stfp );
421
422 extern void
423 st_release_gp_variants(struct st_context *st,
424 struct st_geometry_program *stgp);
425
426 extern void
427 st_release_tcp_variants(struct st_context *st,
428 struct st_tessctrl_program *stgp);
429
430 extern void
431 st_release_tep_variants(struct st_context *st,
432 struct st_tesseval_program *stgp);
433
434 extern void
435 st_destroy_program_variants(struct st_context *st);
436
437 extern bool
438 st_translate_vertex_program(struct st_context *st,
439 struct st_vertex_program *stvp);
440
441 extern bool
442 st_translate_fragment_program(struct st_context *st,
443 struct st_fragment_program *stfp);
444
445 extern void
446 st_print_current_vertex_program(void);
447
448 extern void
449 st_precompile_shader_variant(struct st_context *st,
450 struct gl_program *prog);
451
452 #ifdef __cplusplus
453 }
454 #endif
455
456 #endif