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 /** needed for ATI_fragment_shader */
130 char texture_targets
[MAX_NUM_FRAGMENT_REGISTERS_ATI
];
132 struct st_external_sampler_key external
;
137 * Variant of a fragment program.
141 /** Parameters which generated this version of fragment program */
142 struct st_fp_variant_key key
;
144 /** Driver's compiled shader */
147 /** For glBitmap variants */
150 /** For glDrawPixels variants */
151 unsigned drawpix_sampler
;
152 unsigned pixelmap_sampler
;
154 /** next in linked list */
155 struct st_fp_variant
*next
;
160 * Derived from Mesa gl_program:
162 struct st_fragment_program
164 struct gl_program Base
;
165 struct pipe_shader_state tgsi
;
166 struct glsl_to_tgsi_visitor
* glsl_to_tgsi
;
167 struct ati_fragment_shader
*ati_fs
;
168 uint64_t affected_states
; /**< ST_NEW_* flags to mark dirty when binding */
170 /* used when bypassing glsl_to_tgsi: */
171 struct gl_shader_program
*shader_program
;
173 struct st_fp_variant
*variants
;
175 /* Used by the shader cache and ARB_get_program_binary */
176 unsigned num_tgsi_tokens
;
181 /** Vertex program variant key */
182 struct st_vp_variant_key
184 struct st_context
*st
; /**< variants are per-context */
185 bool passthrough_edgeflags
;
187 /** for ARB_color_buffer_float */
190 /** both for ARB_depth_clamp */
191 bool lower_depth_clamp
;
192 bool clip_negative_one_to_one
;
197 * This represents a vertex program, especially translated to match
198 * the inputs of a particular fragment shader.
202 /* Parameters which generated this translated version of a vertex
205 struct st_vp_variant_key key
;
208 * TGSI tokens (to later generate a 'draw' module shader for
209 * selection/feedback/rasterpos)
211 struct pipe_shader_state tgsi
;
213 /** Driver's compiled shader */
216 /** For using our private draw module (glRasterPos) */
217 struct draw_vertex_shader
*draw_shader
;
219 /** Next in linked list */
220 struct st_vp_variant
*next
;
222 /** similar to that in st_vertex_program, but with edgeflags info too */
225 /** Bitfield of VERT_BIT_* bits of mesa vertex processing inputs */
226 GLbitfield vert_attrib_mask
;
231 * Derived from Mesa gl_program:
233 struct st_vertex_program
235 struct gl_program Base
; /**< The Mesa vertex program */
236 struct pipe_shader_state tgsi
;
237 struct glsl_to_tgsi_visitor
* glsl_to_tgsi
;
238 uint64_t affected_states
; /**< ST_NEW_* flags to mark dirty when binding */
240 /* used when bypassing glsl_to_tgsi: */
241 struct gl_shader_program
*shader_program
;
243 /** maps a TGSI input index back to a Mesa VERT_ATTRIB_x */
244 ubyte index_to_input
[PIPE_MAX_ATTRIBS
];
246 /** Reverse mapping of the above */
247 ubyte input_to_index
[VERT_ATTRIB_MAX
];
249 /** Maps VARYING_SLOT_x to slot */
250 ubyte result_to_output
[VARYING_SLOT_MAX
];
252 /** List of translated variants of this vertex program.
254 struct st_vp_variant
*variants
;
256 /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
257 unsigned char sha1
[20];
259 /* Used by the shader cache and ARB_get_program_binary */
260 unsigned num_tgsi_tokens
;
265 /** Key shared by all shaders except VP, FP */
266 struct st_basic_variant_key
268 struct st_context
*st
; /**< variants are per-context */
270 /** For compat profile */
273 /** both for ARB_depth_clamp */
274 bool lower_depth_clamp
;
275 bool clip_negative_one_to_one
;
281 * Geometry program variant.
283 struct st_basic_variant
285 /* Parameters which generated this variant. */
286 struct st_basic_variant_key key
;
290 struct st_basic_variant
*next
;
295 * Derived from Mesa gl_program:
297 struct st_common_program
299 struct gl_program Base
;
300 struct pipe_shader_state tgsi
;
301 struct glsl_to_tgsi_visitor
* glsl_to_tgsi
;
302 uint64_t affected_states
; /**< ST_NEW_* flags to mark dirty when binding */
304 /* used when bypassing glsl_to_tgsi: */
305 struct gl_shader_program
*shader_program
;
307 struct st_basic_variant
*variants
;
309 /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
310 unsigned char sha1
[20];
312 /* Used by the shader cache and ARB_get_program_binary */
313 unsigned num_tgsi_tokens
;
318 * Derived from Mesa gl_program:
320 struct st_compute_program
322 struct gl_program Base
; /**< The Mesa compute program */
323 struct pipe_compute_state tgsi
;
324 struct glsl_to_tgsi_visitor
* glsl_to_tgsi
;
325 uint64_t affected_states
; /**< ST_NEW_* flags to mark dirty when binding */
327 /* used when bypassing glsl_to_tgsi: */
328 struct gl_shader_program
*shader_program
;
330 struct st_basic_variant
*variants
;
332 /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
333 unsigned char sha1
[20];
335 /* Used by the shader cache and ARB_get_program_binary */
336 unsigned num_tgsi_tokens
;
340 static inline struct st_fragment_program
*
341 st_fragment_program( struct gl_program
*fp
)
343 return (struct st_fragment_program
*)fp
;
347 static inline struct st_vertex_program
*
348 st_vertex_program( struct gl_program
*vp
)
350 return (struct st_vertex_program
*)vp
;
353 static inline struct st_common_program
*
354 st_common_program( struct gl_program
*gp
)
356 return (struct st_common_program
*)gp
;
359 static inline struct st_compute_program
*
360 st_compute_program( struct gl_program
*cp
)
362 return (struct st_compute_program
*)cp
;
366 st_reference_vertprog(struct st_context
*st
,
367 struct st_vertex_program
**ptr
,
368 struct st_vertex_program
*prog
)
370 _mesa_reference_program(st
->ctx
,
371 (struct gl_program
**) ptr
,
372 (struct gl_program
*) prog
);
376 st_reference_fragprog(struct st_context
*st
,
377 struct st_fragment_program
**ptr
,
378 struct st_fragment_program
*prog
)
380 _mesa_reference_program(st
->ctx
,
381 (struct gl_program
**) ptr
,
382 (struct gl_program
*) prog
);
386 st_reference_prog(struct st_context
*st
,
387 struct st_common_program
**ptr
,
388 struct st_common_program
*prog
)
390 _mesa_reference_program(st
->ctx
,
391 (struct gl_program
**) ptr
,
392 (struct gl_program
*) prog
);
396 st_reference_compprog(struct st_context
*st
,
397 struct st_compute_program
**ptr
,
398 struct st_compute_program
*prog
)
400 _mesa_reference_program(st
->ctx
,
401 (struct gl_program
**) ptr
,
402 (struct gl_program
*) prog
);
406 * This defines mapping from Mesa VARYING_SLOTs to TGSI GENERIC slots.
408 static inline unsigned
409 st_get_generic_varying_index(struct st_context
*st
, GLuint attr
)
411 return tgsi_get_generic_gl_varying_index((gl_varying_slot
)attr
,
412 st
->needs_texcoord_semantic
);
416 st_set_prog_affected_state_flags(struct gl_program
*prog
);
418 extern struct st_vp_variant
*
419 st_get_vp_variant(struct st_context
*st
,
420 struct st_vertex_program
*stvp
,
421 const struct st_vp_variant_key
*key
);
424 extern struct st_fp_variant
*
425 st_get_fp_variant(struct st_context
*st
,
426 struct st_fragment_program
*stfp
,
427 const struct st_fp_variant_key
*key
);
429 extern struct st_basic_variant
*
430 st_get_cp_variant(struct st_context
*st
,
431 struct pipe_compute_state
*tgsi
,
432 struct st_basic_variant
**variants
);
434 extern struct st_basic_variant
*
435 st_get_basic_variant(struct st_context
*st
,
436 unsigned pipe_shader
,
437 struct st_common_program
*p
,
438 const struct st_basic_variant_key
*key
);
441 st_release_vp_variants( struct st_context
*st
,
442 struct st_vertex_program
*stvp
);
445 st_release_fp_variants( struct st_context
*st
,
446 struct st_fragment_program
*stfp
);
449 st_release_cp_variants(struct st_context
*st
,
450 struct st_compute_program
*stcp
);
453 st_release_basic_variants(struct st_context
*st
, GLenum target
,
454 struct st_basic_variant
**variants
,
455 struct pipe_shader_state
*tgsi
);
458 st_destroy_program_variants(struct st_context
*st
);
461 st_translate_vertex_program(struct st_context
*st
,
462 struct st_vertex_program
*stvp
);
465 st_translate_fragment_program(struct st_context
*st
,
466 struct st_fragment_program
*stfp
);
469 st_translate_geometry_program(struct st_context
*st
,
470 struct st_common_program
*stgp
);
473 st_translate_tessctrl_program(struct st_context
*st
,
474 struct st_common_program
*sttcp
);
477 st_translate_tesseval_program(struct st_context
*st
,
478 struct st_common_program
*sttep
);
481 st_translate_compute_program(struct st_context
*st
,
482 struct st_compute_program
*stcp
);
485 st_print_current_vertex_program(void);
488 st_precompile_shader_variant(struct st_context
*st
,
489 struct gl_program
*prog
);