st/glsl: support clamping color outputs in compat for gs/tes
[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 };
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 /* Used by the shader cache and ARB_get_program_binary */
155 unsigned num_tgsi_tokens;
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 /** Bitfield of VERT_BIT_* bits of mesa vertex processing inputs */
201 GLbitfield vert_attrib_mask;
202 };
203
204
205 /**
206 * Derived from Mesa gl_program:
207 */
208 struct st_vertex_program
209 {
210 struct gl_program Base; /**< The Mesa vertex program */
211 struct pipe_shader_state tgsi;
212 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
213 uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
214
215 /* used when bypassing glsl_to_tgsi: */
216 struct gl_shader_program *shader_program;
217
218 /** maps a TGSI input index back to a Mesa VERT_ATTRIB_x */
219 ubyte index_to_input[PIPE_MAX_ATTRIBS];
220 ubyte num_inputs;
221 /** Reverse mapping of the above */
222 ubyte input_to_index[VERT_ATTRIB_MAX];
223
224 /** Maps VARYING_SLOT_x to slot */
225 ubyte result_to_output[VARYING_SLOT_MAX];
226
227 /** List of translated variants of this vertex program.
228 */
229 struct st_vp_variant *variants;
230
231 /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
232 unsigned char sha1[20];
233
234 /* Used by the shader cache and ARB_get_program_binary */
235 unsigned num_tgsi_tokens;
236 };
237
238
239
240 /** Key shared by all shaders except VP, FP */
241 struct st_basic_variant_key
242 {
243 struct st_context *st; /**< variants are per-context */
244
245 /** For compat profile */
246 bool clamp_color;
247 };
248
249
250 /**
251 * Geometry program variant.
252 */
253 struct st_basic_variant
254 {
255 /* Parameters which generated this variant. */
256 struct st_basic_variant_key key;
257
258 void *driver_shader;
259
260 struct st_basic_variant *next;
261 };
262
263
264 /**
265 * Derived from Mesa gl_program:
266 */
267 struct st_common_program
268 {
269 struct gl_program Base;
270 struct pipe_shader_state tgsi;
271 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
272 uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
273
274 /* used when bypassing glsl_to_tgsi: */
275 struct gl_shader_program *shader_program;
276
277 struct st_basic_variant *variants;
278
279 /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
280 unsigned char sha1[20];
281
282 /* Used by the shader cache and ARB_get_program_binary */
283 unsigned num_tgsi_tokens;
284 };
285
286
287 /**
288 * Derived from Mesa gl_program:
289 */
290 struct st_compute_program
291 {
292 struct gl_program Base; /**< The Mesa compute program */
293 struct pipe_compute_state tgsi;
294 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
295 uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
296
297 /* used when bypassing glsl_to_tgsi: */
298 struct gl_shader_program *shader_program;
299
300 struct st_basic_variant *variants;
301
302 /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
303 unsigned char sha1[20];
304
305 /* Used by the shader cache and ARB_get_program_binary */
306 unsigned num_tgsi_tokens;
307 };
308
309
310 static inline struct st_fragment_program *
311 st_fragment_program( struct gl_program *fp )
312 {
313 return (struct st_fragment_program *)fp;
314 }
315
316
317 static inline struct st_vertex_program *
318 st_vertex_program( struct gl_program *vp )
319 {
320 return (struct st_vertex_program *)vp;
321 }
322
323 static inline struct st_common_program *
324 st_common_program( struct gl_program *gp )
325 {
326 return (struct st_common_program *)gp;
327 }
328
329 static inline struct st_compute_program *
330 st_compute_program( struct gl_program *cp )
331 {
332 return (struct st_compute_program *)cp;
333 }
334
335 static inline void
336 st_reference_vertprog(struct st_context *st,
337 struct st_vertex_program **ptr,
338 struct st_vertex_program *prog)
339 {
340 _mesa_reference_program(st->ctx,
341 (struct gl_program **) ptr,
342 (struct gl_program *) prog);
343 }
344
345 static inline void
346 st_reference_fragprog(struct st_context *st,
347 struct st_fragment_program **ptr,
348 struct st_fragment_program *prog)
349 {
350 _mesa_reference_program(st->ctx,
351 (struct gl_program **) ptr,
352 (struct gl_program *) prog);
353 }
354
355 static inline void
356 st_reference_prog(struct st_context *st,
357 struct st_common_program **ptr,
358 struct st_common_program *prog)
359 {
360 _mesa_reference_program(st->ctx,
361 (struct gl_program **) ptr,
362 (struct gl_program *) prog);
363 }
364
365 static inline void
366 st_reference_compprog(struct st_context *st,
367 struct st_compute_program **ptr,
368 struct st_compute_program *prog)
369 {
370 _mesa_reference_program(st->ctx,
371 (struct gl_program **) ptr,
372 (struct gl_program *) prog);
373 }
374
375 /**
376 * This defines mapping from Mesa VARYING_SLOTs to TGSI GENERIC slots.
377 */
378 static inline unsigned
379 st_get_generic_varying_index(struct st_context *st, GLuint attr)
380 {
381 return tgsi_get_generic_gl_varying_index((gl_varying_slot)attr,
382 st->needs_texcoord_semantic);
383 }
384
385 extern void
386 st_set_prog_affected_state_flags(struct gl_program *prog);
387
388 extern struct st_vp_variant *
389 st_get_vp_variant(struct st_context *st,
390 struct st_vertex_program *stvp,
391 const struct st_vp_variant_key *key);
392
393
394 extern struct st_fp_variant *
395 st_get_fp_variant(struct st_context *st,
396 struct st_fragment_program *stfp,
397 const struct st_fp_variant_key *key);
398
399 extern struct st_basic_variant *
400 st_get_cp_variant(struct st_context *st,
401 struct pipe_compute_state *tgsi,
402 struct st_basic_variant **variants);
403
404 extern struct st_basic_variant *
405 st_get_basic_variant(struct st_context *st,
406 unsigned pipe_shader,
407 struct st_common_program *p,
408 const struct st_basic_variant_key *key);
409
410 extern void
411 st_release_vp_variants( struct st_context *st,
412 struct st_vertex_program *stvp );
413
414 extern void
415 st_release_fp_variants( struct st_context *st,
416 struct st_fragment_program *stfp );
417
418 extern void
419 st_release_cp_variants(struct st_context *st,
420 struct st_compute_program *stcp);
421
422 extern void
423 st_release_basic_variants(struct st_context *st, GLenum target,
424 struct st_basic_variant **variants,
425 struct pipe_shader_state *tgsi);
426
427 extern void
428 st_destroy_program_variants(struct st_context *st);
429
430 extern bool
431 st_translate_vertex_program(struct st_context *st,
432 struct st_vertex_program *stvp);
433
434 extern bool
435 st_translate_fragment_program(struct st_context *st,
436 struct st_fragment_program *stfp);
437
438 extern bool
439 st_translate_geometry_program(struct st_context *st,
440 struct st_common_program *stgp);
441
442 extern bool
443 st_translate_tessctrl_program(struct st_context *st,
444 struct st_common_program *sttcp);
445
446 extern bool
447 st_translate_tesseval_program(struct st_context *st,
448 struct st_common_program *sttep);
449
450 extern bool
451 st_translate_compute_program(struct st_context *st,
452 struct st_compute_program *stcp);
453
454 extern void
455 st_print_current_vertex_program(void);
456
457 extern void
458 st_precompile_shader_variant(struct st_context *st,
459 struct gl_program *prog);
460
461 #ifdef __cplusplus
462 }
463 #endif
464
465 #endif