1 /**************************************************************************
3 * Copyright 2003 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keithw@vmware.com>
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"
50 #define ST_DOUBLE_ATTRIB_PLACEHOLDER 0xff
52 struct st_external_sampler_key
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 */
62 static inline struct st_external_sampler_key
63 st_get_external_sampler_key(struct st_context
*st
, struct gl_program
*prog
)
65 unsigned mask
= prog
->ExternalSamplersUsed
;
66 struct st_external_sampler_key key
;
68 memset(&key
, 0, sizeof(key
));
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
);
75 switch (st_get_view_format(stObj
)) {
76 case PIPE_FORMAT_NV12
:
77 case PIPE_FORMAT_P016
:
78 key
.lower_nv12
|= (1 << unit
);
80 case PIPE_FORMAT_IYUV
:
81 key
.lower_iyuv
|= (1 << unit
);
83 case PIPE_FORMAT_YUYV
:
84 key
.lower_yx_xuxv
|= (1 << unit
);
86 case PIPE_FORMAT_UYVY
:
87 key
.lower_xy_uxvx
|= (1 << unit
);
89 case PIPE_FORMAT_AYUV
:
90 key
.lower_ayuv
|= (1 << unit
);
92 case PIPE_FORMAT_XYUV
:
93 key
.lower_xyuv
|= (1 << unit
);
96 printf("unhandled %u\n", st_get_view_format(stObj
));
104 /** Fragment program variant key */
105 struct st_fp_variant_key
107 struct st_context
*st
; /**< variants are per-context */
110 GLuint bitmap
:1; /**< glBitmap variant? */
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? */
117 /** for ARB_color_buffer_float */
118 GLuint clamp_color
:1;
120 /** for ARB_sample_shading */
121 GLuint persample_shading
:1;
123 /** needed for ATI_fragment_shader */
126 /** for ARB_depth_clamp */
127 GLuint lower_depth_clamp
:1;
129 /** for OpenGL 1.0 on modern hardware */
130 GLuint lower_two_sided_color
:1;
132 /** needed for ATI_fragment_shader */
133 char texture_targets
[MAX_NUM_FRAGMENT_REGISTERS_ATI
];
135 struct st_external_sampler_key external
;
137 GLuint lower_flatshade
:1;
138 enum compare_func lower_alpha_func
:3;
143 * Variant of a fragment program.
147 /** Parameters which generated this version of fragment program */
148 struct st_fp_variant_key key
;
150 /** Driver's compiled shader */
153 /** For glBitmap variants */
156 /** For glDrawPixels variants */
157 unsigned drawpix_sampler
;
158 unsigned pixelmap_sampler
;
160 /** next in linked list */
161 struct st_fp_variant
*next
;
166 * Derived from Mesa gl_program:
168 struct st_fragment_program
170 struct gl_program Base
;
171 struct pipe_shader_state state
;
172 struct glsl_to_tgsi_visitor
* glsl_to_tgsi
;
173 struct ati_fragment_shader
*ati_fs
;
174 uint64_t affected_states
; /**< ST_NEW_* flags to mark dirty when binding */
176 /* used when bypassing glsl_to_tgsi: */
177 struct gl_shader_program
*shader_program
;
179 struct st_fp_variant
*variants
;
181 /* Used by the shader cache and ARB_get_program_binary */
182 unsigned num_tgsi_tokens
;
187 /** Vertex program variant key */
188 struct st_vp_variant_key
190 struct st_context
*st
; /**< variants are per-context */
191 bool passthrough_edgeflags
;
193 /** for ARB_color_buffer_float */
196 /** both for ARB_depth_clamp */
197 bool lower_depth_clamp
;
198 bool clip_negative_one_to_one
;
200 /** lower glPointSize to gl_PointSize */
201 boolean lower_point_size
;
203 /* for user-defined clip-planes */
209 * This represents a vertex program, especially translated to match
210 * the inputs of a particular fragment shader.
214 /* Parameters which generated this translated version of a vertex
217 struct st_vp_variant_key key
;
220 * The shader variant saved for the draw module to later emulate
221 * selection/feedback/rasterpos.
223 struct pipe_shader_state state
;
225 /** Driver's compiled shader */
228 /** For using our private draw module (glRasterPos) */
229 struct draw_vertex_shader
*draw_shader
;
231 /** Next in linked list */
232 struct st_vp_variant
*next
;
234 /** similar to that in st_vertex_program, but with edgeflags info too */
237 /** Bitfield of VERT_BIT_* bits of mesa vertex processing inputs */
238 GLbitfield vert_attrib_mask
;
243 * Derived from Mesa gl_program:
245 struct st_vertex_program
247 struct gl_program Base
; /**< The Mesa vertex program */
248 struct pipe_shader_state state
;
249 struct glsl_to_tgsi_visitor
* glsl_to_tgsi
;
250 uint64_t affected_states
; /**< ST_NEW_* flags to mark dirty when binding */
252 /* used when bypassing glsl_to_tgsi: */
253 struct gl_shader_program
*shader_program
;
255 /** maps a TGSI input index back to a Mesa VERT_ATTRIB_x */
256 ubyte index_to_input
[PIPE_MAX_ATTRIBS
];
258 /** Reverse mapping of the above */
259 ubyte input_to_index
[VERT_ATTRIB_MAX
];
261 /** Maps VARYING_SLOT_x to slot */
262 ubyte result_to_output
[VARYING_SLOT_MAX
];
264 /** List of translated variants of this vertex program.
266 struct st_vp_variant
*variants
;
268 /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
269 unsigned char sha1
[20];
271 /* Used by the shader cache and ARB_get_program_binary */
272 unsigned num_tgsi_tokens
;
277 /** Shader key shared by other shaders */
278 struct st_common_variant_key
280 struct st_context
*st
; /**< variants are per-context */
282 /** For compat profile */
285 /** both for ARB_depth_clamp */
286 bool lower_depth_clamp
;
287 bool clip_negative_one_to_one
;
293 * Geometry program variant.
295 struct st_common_variant
297 /* Parameters which generated this variant. */
298 struct st_common_variant_key key
;
302 struct st_common_variant
*next
;
307 * Derived from Mesa gl_program:
309 struct st_common_program
311 struct gl_program Base
;
312 struct pipe_shader_state state
;
313 struct glsl_to_tgsi_visitor
* glsl_to_tgsi
;
314 uint64_t affected_states
; /**< ST_NEW_* flags to mark dirty when binding */
316 /* used when bypassing glsl_to_tgsi: */
317 struct gl_shader_program
*shader_program
;
319 struct st_common_variant
*variants
;
321 /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
322 unsigned char sha1
[20];
324 /* Used by the shader cache and ARB_get_program_binary */
325 unsigned num_tgsi_tokens
;
329 static inline struct st_fragment_program
*
330 st_fragment_program( struct gl_program
*fp
)
332 return (struct st_fragment_program
*)fp
;
336 static inline struct st_vertex_program
*
337 st_vertex_program( struct gl_program
*vp
)
339 return (struct st_vertex_program
*)vp
;
342 static inline struct st_common_program
*
343 st_common_program( struct gl_program
*cp
)
345 return (struct st_common_program
*)cp
;
349 st_reference_vertprog(struct st_context
*st
,
350 struct st_vertex_program
**ptr
,
351 struct st_vertex_program
*prog
)
353 _mesa_reference_program(st
->ctx
,
354 (struct gl_program
**) ptr
,
355 (struct gl_program
*) prog
);
359 st_reference_fragprog(struct st_context
*st
,
360 struct st_fragment_program
**ptr
,
361 struct st_fragment_program
*prog
)
363 _mesa_reference_program(st
->ctx
,
364 (struct gl_program
**) ptr
,
365 (struct gl_program
*) prog
);
369 st_reference_prog(struct st_context
*st
,
370 struct st_common_program
**ptr
,
371 struct st_common_program
*prog
)
373 _mesa_reference_program(st
->ctx
,
374 (struct gl_program
**) ptr
,
375 (struct gl_program
*) prog
);
379 st_reference_compprog(struct st_context
*st
,
380 struct st_common_program
**ptr
,
381 struct st_common_program
*prog
)
383 _mesa_reference_program(st
->ctx
,
384 (struct gl_program
**) ptr
,
385 (struct gl_program
*) prog
);
389 * This defines mapping from Mesa VARYING_SLOTs to TGSI GENERIC slots.
391 static inline unsigned
392 st_get_generic_varying_index(struct st_context
*st
, GLuint attr
)
394 return tgsi_get_generic_gl_varying_index((gl_varying_slot
)attr
,
395 st
->needs_texcoord_semantic
);
399 st_set_prog_affected_state_flags(struct gl_program
*prog
);
401 extern struct st_vp_variant
*
402 st_get_vp_variant(struct st_context
*st
,
403 struct st_vertex_program
*stvp
,
404 const struct st_vp_variant_key
*key
);
407 extern struct st_fp_variant
*
408 st_get_fp_variant(struct st_context
*st
,
409 struct st_fragment_program
*stfp
,
410 const struct st_fp_variant_key
*key
);
412 extern struct st_common_variant
*
413 st_get_common_variant(struct st_context
*st
,
414 struct st_common_program
*p
,
415 const struct st_common_variant_key
*key
);
418 st_release_vp_variants( struct st_context
*st
,
419 struct st_vertex_program
*stvp
);
422 st_release_fp_variants( struct st_context
*st
,
423 struct st_fragment_program
*stfp
);
426 st_release_common_variants(struct st_context
*st
, struct st_common_program
*p
);
429 st_destroy_program_variants(struct st_context
*st
);
432 st_prepare_vertex_program(struct st_vertex_program
*stvp
);
435 st_translate_stream_output_info(struct gl_program
*prog
);
438 st_translate_vertex_program(struct st_context
*st
,
439 struct st_vertex_program
*stvp
);
442 st_translate_fragment_program(struct st_context
*st
,
443 struct st_fragment_program
*stfp
);
446 st_translate_common_program(struct st_context
*st
,
447 struct st_common_program
*stcp
);
450 st_print_current_vertex_program(void);
453 st_precompile_shader_variant(struct st_context
*st
,
454 struct gl_program
*prog
);