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