glsl: lower mediump temporaries to 16 bits except structures (v2)
[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 enum pipe_format format = st_get_view_format(stObj);
75
76 /* if resource format matches then YUV wasn't lowered */
77 if (format == stObj->pt->format)
78 continue;
79
80 switch (format) {
81 case PIPE_FORMAT_NV12:
82 case PIPE_FORMAT_P010:
83 case PIPE_FORMAT_P016:
84 key.lower_nv12 |= (1 << unit);
85 break;
86 case PIPE_FORMAT_IYUV:
87 key.lower_iyuv |= (1 << unit);
88 break;
89 case PIPE_FORMAT_YUYV:
90 key.lower_yx_xuxv |= (1 << unit);
91 break;
92 case PIPE_FORMAT_UYVY:
93 key.lower_xy_uxvx |= (1 << unit);
94 break;
95 case PIPE_FORMAT_AYUV:
96 key.lower_ayuv |= (1 << unit);
97 break;
98 case PIPE_FORMAT_XYUV:
99 key.lower_xyuv |= (1 << unit);
100 break;
101 default:
102 printf("mesa: st_get_external_sampler_key: unhandled pipe format %u\n",
103 format);
104 break;
105 }
106 }
107
108 return key;
109 }
110
111 /** Fragment program variant key */
112 struct st_fp_variant_key
113 {
114 struct st_context *st; /**< variants are per-context */
115
116 /** for glBitmap */
117 GLuint bitmap:1; /**< glBitmap variant? */
118
119 /** for glDrawPixels */
120 GLuint drawpixels:1; /**< glDrawPixels variant */
121 GLuint scaleAndBias:1; /**< glDrawPixels w/ scale and/or bias? */
122 GLuint pixelMaps:1; /**< glDrawPixels w/ pixel lookup map? */
123
124 /** for ARB_color_buffer_float */
125 GLuint clamp_color:1;
126
127 /** for ARB_sample_shading */
128 GLuint persample_shading:1;
129
130 /** needed for ATI_fragment_shader */
131 GLuint fog:2;
132
133 /** for ARB_depth_clamp */
134 GLuint lower_depth_clamp:1;
135
136 /** for OpenGL 1.0 on modern hardware */
137 GLuint lower_two_sided_color:1;
138
139 GLuint lower_flatshade:1;
140 unsigned lower_alpha_func:3;
141
142 /** needed for ATI_fragment_shader */
143 char texture_targets[MAX_NUM_FRAGMENT_REGISTERS_ATI];
144
145 struct st_external_sampler_key external;
146 };
147
148 /**
149 * Base class for shader variants.
150 */
151 struct st_variant
152 {
153 /** next in linked list */
154 struct st_variant *next;
155
156 /** st_context from the shader key */
157 struct st_context *st;
158
159 void *driver_shader;
160 };
161
162 /**
163 * Variant of a fragment program.
164 */
165 struct st_fp_variant
166 {
167 struct st_variant base;
168
169 /** Parameters which generated this version of fragment program */
170 struct st_fp_variant_key key;
171
172 /** For glBitmap variants */
173 uint bitmap_sampler;
174
175 /** For glDrawPixels variants */
176 unsigned drawpix_sampler;
177 unsigned pixelmap_sampler;
178 };
179
180
181 /** Shader key shared by other shaders */
182 struct st_common_variant_key
183 {
184 struct st_context *st; /**< variants are per-context */
185 bool passthrough_edgeflags;
186
187 /** for ARB_color_buffer_float */
188 bool clamp_color;
189
190 /** both for ARB_depth_clamp */
191 bool lower_depth_clamp;
192 bool clip_negative_one_to_one;
193
194 /** lower glPointSize to gl_PointSize */
195 boolean lower_point_size;
196
197 /* for user-defined clip-planes */
198 uint8_t lower_ucp;
199
200 /* Whether st_variant::driver_shader is for the draw module,
201 * not for the driver.
202 */
203 bool is_draw_shader;
204 };
205
206
207 /**
208 * Common shader variant.
209 */
210 struct st_common_variant
211 {
212 struct st_variant base;
213
214 /* Parameters which generated this variant. */
215 struct st_common_variant_key key;
216
217 /* Bitfield of VERT_BIT_* bits matching vertex shader inputs,
218 * but not include the high part of doubles.
219 */
220 GLbitfield vert_attrib_mask;
221 };
222
223
224 /**
225 * Derived from Mesa gl_program:
226 */
227 struct st_program
228 {
229 struct gl_program Base;
230 struct pipe_shader_state state;
231 struct glsl_to_tgsi_visitor* glsl_to_tgsi;
232 struct ati_fragment_shader *ati_fs;
233 uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
234
235 void *serialized_nir;
236 unsigned serialized_nir_size;
237
238 /* used when bypassing glsl_to_tgsi: */
239 struct gl_shader_program *shader_program;
240
241 struct st_variant *variants;
242 };
243
244
245 struct st_vertex_program
246 {
247 struct st_program Base;
248
249 /** maps a TGSI input index back to a Mesa VERT_ATTRIB_x */
250 ubyte index_to_input[PIPE_MAX_ATTRIBS];
251 ubyte num_inputs;
252 /** Reverse mapping of the above */
253 ubyte input_to_index[VERT_ATTRIB_MAX];
254
255 /** Maps VARYING_SLOT_x to slot */
256 ubyte result_to_output[VARYING_SLOT_MAX];
257 };
258
259
260 static inline struct st_program *
261 st_program( struct gl_program *cp )
262 {
263 return (struct st_program *)cp;
264 }
265
266 static inline void
267 st_reference_prog(struct st_context *st,
268 struct st_program **ptr,
269 struct st_program *prog)
270 {
271 _mesa_reference_program(st->ctx,
272 (struct gl_program **) ptr,
273 (struct gl_program *) prog);
274 }
275
276 static inline struct st_common_variant *
277 st_common_variant(struct st_variant *v)
278 {
279 return (struct st_common_variant*)v;
280 }
281
282 static inline struct st_fp_variant *
283 st_fp_variant(struct st_variant *v)
284 {
285 return (struct st_fp_variant*)v;
286 }
287
288 /**
289 * This defines mapping from Mesa VARYING_SLOTs to TGSI GENERIC slots.
290 */
291 static inline unsigned
292 st_get_generic_varying_index(struct st_context *st, GLuint attr)
293 {
294 return tgsi_get_generic_gl_varying_index((gl_varying_slot)attr,
295 st->needs_texcoord_semantic);
296 }
297
298 extern void
299 st_set_prog_affected_state_flags(struct gl_program *prog);
300
301 extern struct st_common_variant *
302 st_get_vp_variant(struct st_context *st,
303 struct st_program *stvp,
304 const struct st_common_variant_key *key);
305
306
307 extern struct st_fp_variant *
308 st_get_fp_variant(struct st_context *st,
309 struct st_program *stfp,
310 const struct st_fp_variant_key *key);
311
312 extern struct st_variant *
313 st_get_common_variant(struct st_context *st,
314 struct st_program *p,
315 const struct st_common_variant_key *key);
316
317 extern void
318 st_release_variants(struct st_context *st, struct st_program *p);
319
320 extern void
321 st_release_program(struct st_context *st, struct st_program **p);
322
323 extern void
324 st_destroy_program_variants(struct st_context *st);
325
326 extern void
327 st_finalize_nir_before_variants(struct nir_shader *nir);
328
329 extern void
330 st_prepare_vertex_program(struct st_program *stvp);
331
332 extern void
333 st_translate_stream_output_info(struct gl_program *prog);
334
335 extern bool
336 st_translate_vertex_program(struct st_context *st,
337 struct st_program *stvp);
338
339 extern bool
340 st_translate_fragment_program(struct st_context *st,
341 struct st_program *stfp);
342
343 extern bool
344 st_translate_common_program(struct st_context *st,
345 struct st_program *stp);
346
347 extern void
348 st_serialize_nir(struct st_program *stp);
349
350 extern void
351 st_finalize_program(struct st_context *st, struct gl_program *prog);
352
353 #ifdef __cplusplus
354 }
355 #endif
356
357 #endif