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 0xffffffff
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_fragment_program:
141 struct st_fragment_program
143 struct gl_fragment_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
;
157 /** Vertex program variant key */
158 struct st_vp_variant_key
160 struct st_context
*st
; /**< variants are per-context */
161 boolean passthrough_edgeflags
;
163 /** for ARB_color_buffer_float */
169 * This represents a vertex program, especially translated to match
170 * the inputs of a particular fragment shader.
174 /* Parameters which generated this translated version of a vertex
177 struct st_vp_variant_key key
;
180 * TGSI tokens (to later generate a 'draw' module shader for
181 * selection/feedback/rasterpos)
183 struct pipe_shader_state tgsi
;
185 /** Driver's compiled shader */
188 /** For using our private draw module (glRasterPos) */
189 struct draw_vertex_shader
*draw_shader
;
191 /** Next in linked list */
192 struct st_vp_variant
*next
;
194 /** similar to that in st_vertex_program, but with edgeflags info too */
200 * Derived from Mesa gl_fragment_program:
202 struct st_vertex_program
204 struct gl_vertex_program Base
; /**< The Mesa vertex program */
205 struct pipe_shader_state tgsi
;
206 struct glsl_to_tgsi_visitor
* glsl_to_tgsi
;
207 uint64_t affected_states
; /**< ST_NEW_* flags to mark dirty when binding */
209 /* used when bypassing glsl_to_tgsi: */
210 struct gl_shader_program
*shader_program
;
212 /** maps a Mesa VERT_ATTRIB_x to a packed TGSI input index */
213 /** maps a TGSI input index back to a Mesa VERT_ATTRIB_x */
214 GLuint index_to_input
[PIPE_MAX_SHADER_INPUTS
];
217 /** Maps VARYING_SLOT_x to slot */
218 GLuint result_to_output
[VARYING_SLOT_MAX
];
220 /** List of translated variants of this vertex program.
222 struct st_vp_variant
*variants
;
227 /** Key shared by all shaders except VP, FP */
228 struct st_basic_variant_key
230 struct st_context
*st
; /**< variants are per-context */
235 * Geometry program variant.
237 struct st_basic_variant
239 /* Parameters which generated this variant. */
240 struct st_basic_variant_key key
;
244 struct st_basic_variant
*next
;
249 * Derived from Mesa gl_program:
251 struct st_geometry_program
253 struct gl_program Base
; /**< The Mesa geometry program */
254 struct pipe_shader_state tgsi
;
255 struct glsl_to_tgsi_visitor
* glsl_to_tgsi
;
256 uint64_t affected_states
; /**< ST_NEW_* flags to mark dirty when binding */
258 struct st_basic_variant
*variants
;
263 * Derived from Mesa gl_program:
265 struct st_tessctrl_program
267 struct gl_program Base
; /**< The Mesa tess ctrl program */
268 struct pipe_shader_state tgsi
;
269 struct glsl_to_tgsi_visitor
* glsl_to_tgsi
;
270 uint64_t affected_states
; /**< ST_NEW_* flags to mark dirty when binding */
272 struct st_basic_variant
*variants
;
277 * Derived from Mesa gl_program:
279 struct st_tesseval_program
281 struct gl_program Base
; /**< The Mesa tess eval program */
282 struct pipe_shader_state tgsi
;
283 struct glsl_to_tgsi_visitor
* glsl_to_tgsi
;
284 uint64_t affected_states
; /**< ST_NEW_* flags to mark dirty when binding */
286 struct st_basic_variant
*variants
;
291 * Derived from Mesa gl_compute_program:
293 struct st_compute_program
295 struct gl_compute_program Base
; /**< The Mesa compute program */
296 struct pipe_compute_state tgsi
;
297 struct glsl_to_tgsi_visitor
* glsl_to_tgsi
;
298 uint64_t affected_states
; /**< ST_NEW_* flags to mark dirty when binding */
300 struct st_basic_variant
*variants
;
304 static inline struct st_fragment_program
*
305 st_fragment_program( struct gl_fragment_program
*fp
)
307 return (struct st_fragment_program
*)fp
;
311 static inline struct st_vertex_program
*
312 st_vertex_program( struct gl_vertex_program
*vp
)
314 return (struct st_vertex_program
*)vp
;
317 static inline struct st_geometry_program
*
318 st_geometry_program( struct gl_program
*gp
)
320 return (struct st_geometry_program
*)gp
;
323 static inline struct st_tessctrl_program
*
324 st_tessctrl_program( struct gl_program
*tcp
)
326 return (struct st_tessctrl_program
*)tcp
;
329 static inline struct st_tesseval_program
*
330 st_tesseval_program( struct gl_program
*tep
)
332 return (struct st_tesseval_program
*)tep
;
335 static inline struct st_compute_program
*
336 st_compute_program( struct gl_compute_program
*cp
)
338 return (struct st_compute_program
*)cp
;
342 st_reference_vertprog(struct st_context
*st
,
343 struct st_vertex_program
**ptr
,
344 struct st_vertex_program
*prog
)
346 _mesa_reference_program(st
->ctx
,
347 (struct gl_program
**) ptr
,
348 (struct gl_program
*) prog
);
352 st_reference_geomprog(struct st_context
*st
,
353 struct st_geometry_program
**ptr
,
354 struct st_geometry_program
*prog
)
356 _mesa_reference_program(st
->ctx
,
357 (struct gl_program
**) ptr
,
358 (struct gl_program
*) prog
);
362 st_reference_fragprog(struct st_context
*st
,
363 struct st_fragment_program
**ptr
,
364 struct st_fragment_program
*prog
)
366 _mesa_reference_program(st
->ctx
,
367 (struct gl_program
**) ptr
,
368 (struct gl_program
*) prog
);
372 st_reference_tesscprog(struct st_context
*st
,
373 struct st_tessctrl_program
**ptr
,
374 struct st_tessctrl_program
*prog
)
376 _mesa_reference_program(st
->ctx
,
377 (struct gl_program
**) ptr
,
378 (struct gl_program
*) prog
);
382 st_reference_tesseprog(struct st_context
*st
,
383 struct st_tesseval_program
**ptr
,
384 struct st_tesseval_program
*prog
)
386 _mesa_reference_program(st
->ctx
,
387 (struct gl_program
**) ptr
,
388 (struct gl_program
*) prog
);
392 st_reference_compprog(struct st_context
*st
,
393 struct st_compute_program
**ptr
,
394 struct st_compute_program
*prog
)
396 _mesa_reference_program(st
->ctx
,
397 (struct gl_program
**) ptr
,
398 (struct gl_program
*) prog
);
402 * This defines mapping from Mesa VARYING_SLOTs to TGSI GENERIC slots.
404 static inline unsigned
405 st_get_generic_varying_index(struct st_context
*st
, GLuint attr
)
407 if (attr
>= VARYING_SLOT_VAR0
) {
408 if (st
->needs_texcoord_semantic
)
409 return attr
- VARYING_SLOT_VAR0
;
411 return 9 + (attr
- VARYING_SLOT_VAR0
);
413 if (attr
== VARYING_SLOT_PNTC
) {
414 assert(!st
->needs_texcoord_semantic
);
417 if (attr
>= VARYING_SLOT_TEX0
&& attr
<= VARYING_SLOT_TEX7
) {
418 assert(!st
->needs_texcoord_semantic
);
419 return attr
- VARYING_SLOT_TEX0
;
427 extern struct st_vp_variant
*
428 st_get_vp_variant(struct st_context
*st
,
429 struct st_vertex_program
*stvp
,
430 const struct st_vp_variant_key
*key
);
433 extern struct st_fp_variant
*
434 st_get_fp_variant(struct st_context
*st
,
435 struct st_fragment_program
*stfp
,
436 const struct st_fp_variant_key
*key
);
438 extern struct st_basic_variant
*
439 st_get_cp_variant(struct st_context
*st
,
440 struct pipe_compute_state
*tgsi
,
441 struct st_basic_variant
**variants
);
443 extern struct st_basic_variant
*
444 st_get_basic_variant(struct st_context
*st
,
445 unsigned pipe_shader
,
446 struct pipe_shader_state
*tgsi
,
447 struct st_basic_variant
**variants
);
450 st_release_vp_variants( struct st_context
*st
,
451 struct st_vertex_program
*stvp
);
454 st_release_fp_variants( struct st_context
*st
,
455 struct st_fragment_program
*stfp
);
458 st_release_cp_variants(struct st_context
*st
,
459 struct st_compute_program
*stcp
);
462 st_release_basic_variants(struct st_context
*st
, GLenum target
,
463 struct st_basic_variant
**variants
,
464 struct pipe_shader_state
*tgsi
);
467 st_destroy_program_variants(struct st_context
*st
);
470 st_translate_vertex_program(struct st_context
*st
,
471 struct st_vertex_program
*stvp
);
474 st_translate_fragment_program(struct st_context
*st
,
475 struct st_fragment_program
*stfp
);
478 st_translate_geometry_program(struct st_context
*st
,
479 struct st_geometry_program
*stgp
);
482 st_translate_tessctrl_program(struct st_context
*st
,
483 struct st_tessctrl_program
*sttcp
);
486 st_translate_tesseval_program(struct st_context
*st
,
487 struct st_tesseval_program
*sttep
);
490 st_translate_compute_program(struct st_context
*st
,
491 struct st_compute_program
*stcp
);
494 st_print_current_vertex_program(void);
497 st_precompile_shader_variant(struct st_context
*st
,
498 struct gl_program
*prog
);