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