st/mesa: decrease the size of st_vertex_program
[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_texture.h"
43 #include "st_glsl_to_tgsi.h"
44
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50 #define ST_DOUBLE_ATTRIB_PLACEHOLDER 0xff
51
52 struct st_external_sampler_key
53 {
54 GLuint lower_nv12; /**< bitmask of 2 plane YUV samplers */
55 GLuint lower_iyuv; /**< bitmask of 3 plane YUV samplers */
56 };
57
58 static inline struct st_external_sampler_key
59 st_get_external_sampler_key(struct st_context *st, struct gl_program *prog)
60 {
61 unsigned mask = prog->ExternalSamplersUsed;
62 struct st_external_sampler_key key;
63
64 memset(&key, 0, sizeof(key));
65
66 while (unlikely(mask)) {
67 unsigned unit = u_bit_scan(&mask);
68 struct st_texture_object *stObj =
69 st_get_texture_object(st->ctx, prog, unit);
70
71 switch (st_get_view_format(stObj)) {
72 case PIPE_FORMAT_NV12:
73 key.lower_nv12 |= (1 << unit);
74 break;
75 case PIPE_FORMAT_IYUV:
76 key.lower_iyuv |= (1 << unit);
77 break;
78 default:
79 break;
80 }
81 }
82
83 return key;
84 }
85
86 /** Fragment program variant key */
87 struct st_fp_variant_key
88 {
89 struct st_context *st; /**< variants are per-context */
90
91 /** for glBitmap */
92 GLuint bitmap:1; /**< glBitmap variant? */
93
94 /** for glDrawPixels */
95 GLuint drawpixels:1; /**< glDrawPixels variant */
96 GLuint scaleAndBias:1; /**< glDrawPixels w/ scale and/or bias? */
97 GLuint pixelMaps:1; /**< glDrawPixels w/ pixel lookup map? */
98
99 /** for ARB_color_buffer_float */
100 GLuint clamp_color:1;
101
102 /** for ARB_sample_shading */
103 GLuint persample_shading:1;
104
105 /** needed for ATI_fragment_shader */
106 GLuint fog:2;
107
108 /** needed for ATI_fragment_shader */
109 char texture_targets[MAX_NUM_FRAGMENT_REGISTERS_ATI];
110
111 struct st_external_sampler_key external;
112 };
113
114
115 /**
116 * Variant of a fragment program.
117 */
118 struct st_fp_variant
119 {
120 /** Parameters which generated this version of fragment program */
121 struct st_fp_variant_key key;
122
123 /** Driver's compiled shader */
124 void *driver_shader;
125
126 /** For glBitmap variants */
127 uint bitmap_sampler;
128
129 /** For glDrawPixels variants */
130 unsigned drawpix_sampler;
131 unsigned pixelmap_sampler;
132
133 /** next in linked list */
134 struct st_fp_variant *next;
135 };
136
137
138 /**
139 * Derived from Mesa gl_program:
140 */
141 struct st_fragment_program
142 {
143 struct gl_program Base;
144 struct pipe_shader_state tgsi;
145 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
146 struct ati_fragment_shader *ati_fs;
147 uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
148
149 /* used when bypassing glsl_to_tgsi: */
150 struct gl_shader_program *shader_program;
151
152 struct st_fp_variant *variants;
153
154 /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
155 unsigned char sha1[20];
156 };
157
158
159
160 /** Vertex program variant key */
161 struct st_vp_variant_key
162 {
163 struct st_context *st; /**< variants are per-context */
164 boolean passthrough_edgeflags;
165
166 /** for ARB_color_buffer_float */
167 boolean clamp_color;
168 };
169
170
171 /**
172 * This represents a vertex program, especially translated to match
173 * the inputs of a particular fragment shader.
174 */
175 struct st_vp_variant
176 {
177 /* Parameters which generated this translated version of a vertex
178 * shader:
179 */
180 struct st_vp_variant_key key;
181
182 /**
183 * TGSI tokens (to later generate a 'draw' module shader for
184 * selection/feedback/rasterpos)
185 */
186 struct pipe_shader_state tgsi;
187
188 /** Driver's compiled shader */
189 void *driver_shader;
190
191 /** For using our private draw module (glRasterPos) */
192 struct draw_vertex_shader *draw_shader;
193
194 /** Next in linked list */
195 struct st_vp_variant *next;
196
197 /** similar to that in st_vertex_program, but with edgeflags info too */
198 GLuint num_inputs;
199 };
200
201
202 /**
203 * Derived from Mesa gl_program:
204 */
205 struct st_vertex_program
206 {
207 struct gl_program Base; /**< The Mesa vertex program */
208 struct pipe_shader_state tgsi;
209 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
210 uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
211
212 /* used when bypassing glsl_to_tgsi: */
213 struct gl_shader_program *shader_program;
214
215 /** maps a Mesa VERT_ATTRIB_x to a packed TGSI input index */
216 /** maps a TGSI input index back to a Mesa VERT_ATTRIB_x */
217 ubyte index_to_input[PIPE_MAX_ATTRIBS];
218 ubyte num_inputs;
219
220 /** Maps VARYING_SLOT_x to slot */
221 ubyte result_to_output[VARYING_SLOT_MAX];
222
223 /** List of translated variants of this vertex program.
224 */
225 struct st_vp_variant *variants;
226
227 /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
228 unsigned char sha1[20];
229 };
230
231
232
233 /** Key shared by all shaders except VP, FP */
234 struct st_basic_variant_key
235 {
236 struct st_context *st; /**< variants are per-context */
237 };
238
239
240 /**
241 * Geometry program variant.
242 */
243 struct st_basic_variant
244 {
245 /* Parameters which generated this variant. */
246 struct st_basic_variant_key key;
247
248 void *driver_shader;
249
250 struct st_basic_variant *next;
251 };
252
253
254 /**
255 * Derived from Mesa gl_program:
256 */
257 struct st_geometry_program
258 {
259 struct gl_program Base; /**< The Mesa geometry program */
260 struct pipe_shader_state tgsi;
261 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
262 uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
263
264 struct st_basic_variant *variants;
265
266 /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
267 unsigned char sha1[20];
268 };
269
270
271 /**
272 * Derived from Mesa gl_program:
273 */
274 struct st_tessctrl_program
275 {
276 struct gl_program Base; /**< The Mesa tess ctrl program */
277 struct pipe_shader_state tgsi;
278 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
279 uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
280
281 struct st_basic_variant *variants;
282
283 /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
284 unsigned char sha1[20];
285 };
286
287
288 /**
289 * Derived from Mesa gl_program:
290 */
291 struct st_tesseval_program
292 {
293 struct gl_program Base; /**< The Mesa tess eval program */
294 struct pipe_shader_state tgsi;
295 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
296 uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
297
298 struct st_basic_variant *variants;
299
300 /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
301 unsigned char sha1[20];
302 };
303
304
305 /**
306 * Derived from Mesa gl_program:
307 */
308 struct st_compute_program
309 {
310 struct gl_program Base; /**< The Mesa compute program */
311 struct pipe_compute_state tgsi;
312 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
313 uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
314
315 /* used when bypassing glsl_to_tgsi: */
316 struct gl_shader_program *shader_program;
317
318 struct st_basic_variant *variants;
319
320 /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
321 unsigned char sha1[20];
322 };
323
324
325 static inline struct st_fragment_program *
326 st_fragment_program( struct gl_program *fp )
327 {
328 return (struct st_fragment_program *)fp;
329 }
330
331
332 static inline struct st_vertex_program *
333 st_vertex_program( struct gl_program *vp )
334 {
335 return (struct st_vertex_program *)vp;
336 }
337
338 static inline struct st_geometry_program *
339 st_geometry_program( struct gl_program *gp )
340 {
341 return (struct st_geometry_program *)gp;
342 }
343
344 static inline struct st_tessctrl_program *
345 st_tessctrl_program( struct gl_program *tcp )
346 {
347 return (struct st_tessctrl_program *)tcp;
348 }
349
350 static inline struct st_tesseval_program *
351 st_tesseval_program( struct gl_program *tep )
352 {
353 return (struct st_tesseval_program *)tep;
354 }
355
356 static inline struct st_compute_program *
357 st_compute_program( struct gl_program *cp )
358 {
359 return (struct st_compute_program *)cp;
360 }
361
362 static inline void
363 st_reference_vertprog(struct st_context *st,
364 struct st_vertex_program **ptr,
365 struct st_vertex_program *prog)
366 {
367 _mesa_reference_program(st->ctx,
368 (struct gl_program **) ptr,
369 (struct gl_program *) prog);
370 }
371
372 static inline void
373 st_reference_geomprog(struct st_context *st,
374 struct st_geometry_program **ptr,
375 struct st_geometry_program *prog)
376 {
377 _mesa_reference_program(st->ctx,
378 (struct gl_program **) ptr,
379 (struct gl_program *) prog);
380 }
381
382 static inline void
383 st_reference_fragprog(struct st_context *st,
384 struct st_fragment_program **ptr,
385 struct st_fragment_program *prog)
386 {
387 _mesa_reference_program(st->ctx,
388 (struct gl_program **) ptr,
389 (struct gl_program *) prog);
390 }
391
392 static inline void
393 st_reference_tesscprog(struct st_context *st,
394 struct st_tessctrl_program **ptr,
395 struct st_tessctrl_program *prog)
396 {
397 _mesa_reference_program(st->ctx,
398 (struct gl_program **) ptr,
399 (struct gl_program *) prog);
400 }
401
402 static inline void
403 st_reference_tesseprog(struct st_context *st,
404 struct st_tesseval_program **ptr,
405 struct st_tesseval_program *prog)
406 {
407 _mesa_reference_program(st->ctx,
408 (struct gl_program **) ptr,
409 (struct gl_program *) prog);
410 }
411
412 static inline void
413 st_reference_compprog(struct st_context *st,
414 struct st_compute_program **ptr,
415 struct st_compute_program *prog)
416 {
417 _mesa_reference_program(st->ctx,
418 (struct gl_program **) ptr,
419 (struct gl_program *) prog);
420 }
421
422 /**
423 * This defines mapping from Mesa VARYING_SLOTs to TGSI GENERIC slots.
424 */
425 static inline unsigned
426 st_get_generic_varying_index(struct st_context *st, GLuint attr)
427 {
428 if (attr >= VARYING_SLOT_VAR0) {
429 if (st->needs_texcoord_semantic)
430 return attr - VARYING_SLOT_VAR0;
431 else
432 return 9 + (attr - VARYING_SLOT_VAR0);
433 }
434 if (attr == VARYING_SLOT_PNTC) {
435 assert(!st->needs_texcoord_semantic);
436 return 8;
437 }
438 if (attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7) {
439 assert(!st->needs_texcoord_semantic);
440 return attr - VARYING_SLOT_TEX0;
441 }
442
443 assert(0);
444 return 0;
445 }
446
447 extern void
448 st_set_prog_affected_state_flags(struct gl_program *prog);
449
450 extern struct st_vp_variant *
451 st_get_vp_variant(struct st_context *st,
452 struct st_vertex_program *stvp,
453 const struct st_vp_variant_key *key);
454
455
456 extern struct st_fp_variant *
457 st_get_fp_variant(struct st_context *st,
458 struct st_fragment_program *stfp,
459 const struct st_fp_variant_key *key);
460
461 extern struct st_basic_variant *
462 st_get_cp_variant(struct st_context *st,
463 struct pipe_compute_state *tgsi,
464 struct st_basic_variant **variants);
465
466 extern struct st_basic_variant *
467 st_get_basic_variant(struct st_context *st,
468 unsigned pipe_shader,
469 struct pipe_shader_state *tgsi,
470 struct st_basic_variant **variants);
471
472 extern void
473 st_release_vp_variants( struct st_context *st,
474 struct st_vertex_program *stvp );
475
476 extern void
477 st_release_fp_variants( struct st_context *st,
478 struct st_fragment_program *stfp );
479
480 extern void
481 st_release_cp_variants(struct st_context *st,
482 struct st_compute_program *stcp);
483
484 extern void
485 st_release_basic_variants(struct st_context *st, GLenum target,
486 struct st_basic_variant **variants,
487 struct pipe_shader_state *tgsi);
488
489 extern void
490 st_destroy_program_variants(struct st_context *st);
491
492 extern bool
493 st_translate_vertex_program(struct st_context *st,
494 struct st_vertex_program *stvp);
495
496 extern bool
497 st_translate_fragment_program(struct st_context *st,
498 struct st_fragment_program *stfp);
499
500 extern bool
501 st_translate_geometry_program(struct st_context *st,
502 struct st_geometry_program *stgp);
503
504 extern bool
505 st_translate_tessctrl_program(struct st_context *st,
506 struct st_tessctrl_program *sttcp);
507
508 extern bool
509 st_translate_tesseval_program(struct st_context *st,
510 struct st_tesseval_program *sttep);
511
512 extern bool
513 st_translate_compute_program(struct st_context *st,
514 struct st_compute_program *stcp);
515
516 extern void
517 st_print_current_vertex_program(void);
518
519 extern void
520 st_precompile_shader_variant(struct st_context *st,
521 struct gl_program *prog);
522
523 #ifdef __cplusplus
524 }
525 #endif
526
527 #endif