st/mesa: merge st_fragment_program into st_common_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 "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 /** for ARB_depth_clamp */
127 GLuint lower_depth_clamp:1;
128
129 /** for OpenGL 1.0 on modern hardware */
130 GLuint lower_two_sided_color:1;
131
132 /** needed for ATI_fragment_shader */
133 char texture_targets[MAX_NUM_FRAGMENT_REGISTERS_ATI];
134
135 struct st_external_sampler_key external;
136
137 GLuint lower_flatshade:1;
138 enum compare_func lower_alpha_func:3;
139 };
140
141
142 /**
143 * Variant of a fragment program.
144 */
145 struct st_fp_variant
146 {
147 /** Parameters which generated this version of fragment program */
148 struct st_fp_variant_key key;
149
150 /** Driver's compiled shader */
151 void *driver_shader;
152
153 /** For glBitmap variants */
154 uint bitmap_sampler;
155
156 /** For glDrawPixels variants */
157 unsigned drawpix_sampler;
158 unsigned pixelmap_sampler;
159
160 /** next in linked list */
161 struct st_fp_variant *next;
162 };
163
164
165 /** Shader key shared by other shaders */
166 struct st_common_variant_key
167 {
168 struct st_context *st; /**< variants are per-context */
169 bool passthrough_edgeflags;
170
171 /** for ARB_color_buffer_float */
172 bool clamp_color;
173
174 /** both for ARB_depth_clamp */
175 bool lower_depth_clamp;
176 bool clip_negative_one_to_one;
177
178 /** lower glPointSize to gl_PointSize */
179 boolean lower_point_size;
180
181 /* for user-defined clip-planes */
182 uint8_t lower_ucp;
183 };
184
185
186 /**
187 * This represents a vertex program, especially translated to match
188 * the inputs of a particular fragment shader.
189 */
190 struct st_vp_variant
191 {
192 /* Parameters which generated this translated version of a vertex
193 * shader:
194 */
195 struct st_common_variant_key key;
196
197 /**
198 * The shader variant saved for the draw module to later emulate
199 * selection/feedback/rasterpos.
200 */
201 struct pipe_shader_state state;
202
203 /** Driver's compiled shader */
204 void *driver_shader;
205
206 /** For using our private draw module (glRasterPos) */
207 struct draw_vertex_shader *draw_shader;
208
209 /** Next in linked list */
210 struct st_vp_variant *next;
211
212 /** similar to that in st_vertex_program, but with edgeflags info too */
213 GLuint num_inputs;
214
215 /** Bitfield of VERT_BIT_* bits of mesa vertex processing inputs */
216 GLbitfield vert_attrib_mask;
217 };
218
219
220 /**
221 * Derived from Mesa gl_program:
222 */
223 struct st_vertex_program
224 {
225 struct gl_program Base; /**< The Mesa vertex program */
226 struct pipe_shader_state state;
227 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
228 uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
229
230 /* used when bypassing glsl_to_tgsi: */
231 struct gl_shader_program *shader_program;
232
233 /** maps a TGSI input index back to a Mesa VERT_ATTRIB_x */
234 ubyte index_to_input[PIPE_MAX_ATTRIBS];
235 ubyte num_inputs;
236 /** Reverse mapping of the above */
237 ubyte input_to_index[VERT_ATTRIB_MAX];
238
239 /** Maps VARYING_SLOT_x to slot */
240 ubyte result_to_output[VARYING_SLOT_MAX];
241
242 /** List of translated variants of this vertex program.
243 */
244 struct st_vp_variant *variants;
245 };
246
247
248 /**
249 * Geometry program variant.
250 */
251 struct st_common_variant
252 {
253 /* Parameters which generated this variant. */
254 struct st_common_variant_key key;
255
256 void *driver_shader;
257
258 struct st_common_variant *next;
259 };
260
261
262 /**
263 * Derived from Mesa gl_program:
264 */
265 struct st_common_program
266 {
267 struct gl_program Base;
268 struct pipe_shader_state state;
269 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
270 struct ati_fragment_shader *ati_fs;
271 uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
272
273 /* used when bypassing glsl_to_tgsi: */
274 struct gl_shader_program *shader_program;
275
276 union {
277 struct st_common_variant *variants;
278 struct st_fp_variant *fp_variants;
279 };
280 };
281
282
283 static inline struct st_vertex_program *
284 st_vertex_program( struct gl_program *vp )
285 {
286 return (struct st_vertex_program *)vp;
287 }
288
289 static inline struct st_common_program *
290 st_common_program( struct gl_program *cp )
291 {
292 return (struct st_common_program *)cp;
293 }
294
295 static inline void
296 st_reference_vertprog(struct st_context *st,
297 struct st_vertex_program **ptr,
298 struct st_vertex_program *prog)
299 {
300 _mesa_reference_program(st->ctx,
301 (struct gl_program **) ptr,
302 (struct gl_program *) prog);
303 }
304
305 static inline void
306 st_reference_prog(struct st_context *st,
307 struct st_common_program **ptr,
308 struct st_common_program *prog)
309 {
310 _mesa_reference_program(st->ctx,
311 (struct gl_program **) ptr,
312 (struct gl_program *) prog);
313 }
314
315 /**
316 * This defines mapping from Mesa VARYING_SLOTs to TGSI GENERIC slots.
317 */
318 static inline unsigned
319 st_get_generic_varying_index(struct st_context *st, GLuint attr)
320 {
321 return tgsi_get_generic_gl_varying_index((gl_varying_slot)attr,
322 st->needs_texcoord_semantic);
323 }
324
325 extern void
326 st_set_prog_affected_state_flags(struct gl_program *prog);
327
328 extern struct st_vp_variant *
329 st_get_vp_variant(struct st_context *st,
330 struct st_vertex_program *stvp,
331 const struct st_common_variant_key *key);
332
333
334 extern struct st_fp_variant *
335 st_get_fp_variant(struct st_context *st,
336 struct st_common_program *stfp,
337 const struct st_fp_variant_key *key);
338
339 extern struct st_common_variant *
340 st_get_common_variant(struct st_context *st,
341 struct st_common_program *p,
342 const struct st_common_variant_key *key);
343
344 extern void
345 st_release_vp_variants( struct st_context *st,
346 struct st_vertex_program *stvp );
347
348 extern void
349 st_release_fp_variants( struct st_context *st,
350 struct st_common_program *stfp );
351
352 extern void
353 st_release_common_variants(struct st_context *st, struct st_common_program *p);
354
355 extern void
356 st_destroy_program_variants(struct st_context *st);
357
358 extern void
359 st_prepare_vertex_program(struct st_vertex_program *stvp);
360
361 extern void
362 st_translate_stream_output_info(struct gl_program *prog);
363
364 extern bool
365 st_translate_vertex_program(struct st_context *st,
366 struct st_vertex_program *stvp);
367
368 extern bool
369 st_translate_fragment_program(struct st_context *st,
370 struct st_common_program *stfp);
371
372 extern bool
373 st_translate_common_program(struct st_context *st,
374 struct st_common_program *stcp);
375
376 extern void
377 st_print_current_vertex_program(void);
378
379 extern void
380 st_precompile_shader_variant(struct st_context *st,
381 struct gl_program *prog);
382
383 #ifdef __cplusplus
384 }
385 #endif
386
387 #endif