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 "st_context.h"
42 #include "st_texture.h"
43 #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 */
58 static inline struct st_external_sampler_key
59 st_get_external_sampler_key(struct st_context
*st
, struct gl_program
*prog
)
61 unsigned mask
= prog
->ExternalSamplersUsed
;
62 struct st_external_sampler_key key
;
64 memset(&key
, 0, sizeof(key
));
66 while (unlikely(mask
)) {
67 unsigned unit
= u_bit_scan(&mask
);
68 struct st_texture_object
*stObj
=
69 st_get_texture_object(st
->ctx
, prog
, unit
);
71 switch (st_get_view_format(stObj
)) {
72 case PIPE_FORMAT_NV12
:
73 key
.lower_nv12
|= (1 << unit
);
75 case PIPE_FORMAT_IYUV
:
76 key
.lower_iyuv
|= (1 << unit
);
86 /** Fragment program variant key */
87 struct st_fp_variant_key
89 struct st_context
*st
; /**< variants are per-context */
92 GLuint bitmap
:1; /**< glBitmap variant? */
94 /** for glDrawPixels */
95 GLuint drawpixels
:1; /**< glDrawPixels variant */
96 GLuint scaleAndBias
:1; /**< glDrawPixels w/ scale and/or bias? */
97 GLuint pixelMaps
:1; /**< glDrawPixels w/ pixel lookup map? */
99 /** for ARB_color_buffer_float */
100 GLuint clamp_color
:1;
102 /** for ARB_sample_shading */
103 GLuint persample_shading
:1;
105 /** needed for ATI_fragment_shader */
108 /** needed for ATI_fragment_shader */
109 char texture_targets
[MAX_NUM_FRAGMENT_REGISTERS_ATI
];
111 struct st_external_sampler_key external
;
116 * Variant of a fragment program.
120 /** Parameters which generated this version of fragment program */
121 struct st_fp_variant_key key
;
123 /** Driver's compiled shader */
126 /** For glBitmap variants */
129 /** For glDrawPixels variants */
130 unsigned drawpix_sampler
;
131 unsigned pixelmap_sampler
;
133 /** next in linked list */
134 struct st_fp_variant
*next
;
139 * Derived from Mesa gl_program:
141 struct st_fragment_program
143 struct gl_program Base
;
144 struct pipe_shader_state tgsi
;
145 struct glsl_to_tgsi_visitor
* glsl_to_tgsi
;
146 struct ati_fragment_shader
*ati_fs
;
147 uint64_t affected_states
; /**< ST_NEW_* flags to mark dirty when binding */
149 /* used when bypassing glsl_to_tgsi: */
150 struct gl_shader_program
*shader_program
;
152 struct st_fp_variant
*variants
;
154 /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
155 unsigned char sha1
[20];
160 /** Vertex program variant key */
161 struct st_vp_variant_key
163 struct st_context
*st
; /**< variants are per-context */
164 boolean passthrough_edgeflags
;
166 /** for ARB_color_buffer_float */
172 * This represents a vertex program, especially translated to match
173 * the inputs of a particular fragment shader.
177 /* Parameters which generated this translated version of a vertex
180 struct st_vp_variant_key key
;
183 * TGSI tokens (to later generate a 'draw' module shader for
184 * selection/feedback/rasterpos)
186 struct pipe_shader_state tgsi
;
188 /** Driver's compiled shader */
191 /** For using our private draw module (glRasterPos) */
192 struct draw_vertex_shader
*draw_shader
;
194 /** Next in linked list */
195 struct st_vp_variant
*next
;
197 /** similar to that in st_vertex_program, but with edgeflags info too */
203 * Derived from Mesa gl_program:
205 struct st_vertex_program
207 struct gl_program Base
; /**< The Mesa vertex program */
208 struct pipe_shader_state tgsi
;
209 struct glsl_to_tgsi_visitor
* glsl_to_tgsi
;
210 uint64_t affected_states
; /**< ST_NEW_* flags to mark dirty when binding */
212 /* used when bypassing glsl_to_tgsi: */
213 struct gl_shader_program
*shader_program
;
215 /** maps a Mesa VERT_ATTRIB_x to a packed TGSI input index */
216 /** maps a TGSI input index back to a Mesa VERT_ATTRIB_x */
217 ubyte index_to_input
[PIPE_MAX_ATTRIBS
];
220 /** Maps VARYING_SLOT_x to slot */
221 ubyte result_to_output
[VARYING_SLOT_MAX
];
223 /** List of translated variants of this vertex program.
225 struct st_vp_variant
*variants
;
227 /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
228 unsigned char sha1
[20];
233 /** Key shared by all shaders except VP, FP */
234 struct st_basic_variant_key
236 struct st_context
*st
; /**< variants are per-context */
241 * Geometry program variant.
243 struct st_basic_variant
245 /* Parameters which generated this variant. */
246 struct st_basic_variant_key key
;
250 struct st_basic_variant
*next
;
255 * Derived from Mesa gl_program:
257 struct st_common_program
259 struct gl_program Base
;
260 struct pipe_shader_state tgsi
;
261 struct glsl_to_tgsi_visitor
* glsl_to_tgsi
;
262 uint64_t affected_states
; /**< ST_NEW_* flags to mark dirty when binding */
264 struct st_basic_variant
*variants
;
266 /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
267 unsigned char sha1
[20];
272 * Derived from Mesa gl_program:
274 struct st_compute_program
276 struct gl_program Base
; /**< The Mesa compute program */
277 struct pipe_compute_state tgsi
;
278 struct glsl_to_tgsi_visitor
* glsl_to_tgsi
;
279 uint64_t affected_states
; /**< ST_NEW_* flags to mark dirty when binding */
281 /* used when bypassing glsl_to_tgsi: */
282 struct gl_shader_program
*shader_program
;
284 struct st_basic_variant
*variants
;
286 /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
287 unsigned char sha1
[20];
291 static inline struct st_fragment_program
*
292 st_fragment_program( struct gl_program
*fp
)
294 return (struct st_fragment_program
*)fp
;
298 static inline struct st_vertex_program
*
299 st_vertex_program( struct gl_program
*vp
)
301 return (struct st_vertex_program
*)vp
;
304 static inline struct st_common_program
*
305 st_common_program( struct gl_program
*gp
)
307 return (struct st_common_program
*)gp
;
310 static inline struct st_compute_program
*
311 st_compute_program( struct gl_program
*cp
)
313 return (struct st_compute_program
*)cp
;
317 st_reference_vertprog(struct st_context
*st
,
318 struct st_vertex_program
**ptr
,
319 struct st_vertex_program
*prog
)
321 _mesa_reference_program(st
->ctx
,
322 (struct gl_program
**) ptr
,
323 (struct gl_program
*) prog
);
327 st_reference_fragprog(struct st_context
*st
,
328 struct st_fragment_program
**ptr
,
329 struct st_fragment_program
*prog
)
331 _mesa_reference_program(st
->ctx
,
332 (struct gl_program
**) ptr
,
333 (struct gl_program
*) prog
);
337 st_reference_prog(struct st_context
*st
,
338 struct st_common_program
**ptr
,
339 struct st_common_program
*prog
)
341 _mesa_reference_program(st
->ctx
,
342 (struct gl_program
**) ptr
,
343 (struct gl_program
*) prog
);
347 st_reference_compprog(struct st_context
*st
,
348 struct st_compute_program
**ptr
,
349 struct st_compute_program
*prog
)
351 _mesa_reference_program(st
->ctx
,
352 (struct gl_program
**) ptr
,
353 (struct gl_program
*) prog
);
357 * This defines mapping from Mesa VARYING_SLOTs to TGSI GENERIC slots.
359 static inline unsigned
360 st_get_generic_varying_index(struct st_context
*st
, GLuint attr
)
362 if (attr
>= VARYING_SLOT_VAR0
) {
363 if (st
->needs_texcoord_semantic
)
364 return attr
- VARYING_SLOT_VAR0
;
366 return 9 + (attr
- VARYING_SLOT_VAR0
);
368 if (attr
== VARYING_SLOT_PNTC
) {
369 assert(!st
->needs_texcoord_semantic
);
372 if (attr
>= VARYING_SLOT_TEX0
&& attr
<= VARYING_SLOT_TEX7
) {
373 assert(!st
->needs_texcoord_semantic
);
374 return attr
- VARYING_SLOT_TEX0
;
382 st_set_prog_affected_state_flags(struct gl_program
*prog
);
384 extern struct st_vp_variant
*
385 st_get_vp_variant(struct st_context
*st
,
386 struct st_vertex_program
*stvp
,
387 const struct st_vp_variant_key
*key
);
390 extern struct st_fp_variant
*
391 st_get_fp_variant(struct st_context
*st
,
392 struct st_fragment_program
*stfp
,
393 const struct st_fp_variant_key
*key
);
395 extern struct st_basic_variant
*
396 st_get_cp_variant(struct st_context
*st
,
397 struct pipe_compute_state
*tgsi
,
398 struct st_basic_variant
**variants
);
400 extern struct st_basic_variant
*
401 st_get_basic_variant(struct st_context
*st
,
402 unsigned pipe_shader
,
403 struct pipe_shader_state
*tgsi
,
404 struct st_basic_variant
**variants
);
407 st_release_vp_variants( struct st_context
*st
,
408 struct st_vertex_program
*stvp
);
411 st_release_fp_variants( struct st_context
*st
,
412 struct st_fragment_program
*stfp
);
415 st_release_cp_variants(struct st_context
*st
,
416 struct st_compute_program
*stcp
);
419 st_release_basic_variants(struct st_context
*st
, GLenum target
,
420 struct st_basic_variant
**variants
,
421 struct pipe_shader_state
*tgsi
);
424 st_destroy_program_variants(struct st_context
*st
);
427 st_translate_vertex_program(struct st_context
*st
,
428 struct st_vertex_program
*stvp
);
431 st_translate_fragment_program(struct st_context
*st
,
432 struct st_fragment_program
*stfp
);
435 st_translate_geometry_program(struct st_context
*st
,
436 struct st_common_program
*stgp
);
439 st_translate_tessctrl_program(struct st_context
*st
,
440 struct st_common_program
*sttcp
);
443 st_translate_tesseval_program(struct st_context
*st
,
444 struct st_common_program
*sttep
);
447 st_translate_compute_program(struct st_context
*st
,
448 struct st_compute_program
*stcp
);
451 st_print_current_vertex_program(void);
454 st_precompile_shader_variant(struct st_context
*st
,
455 struct gl_program
*prog
);