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
);
74 enum pipe_format format
= st_get_view_format(stObj
);
76 /* if resource format matches then YUV wasn't lowered */
77 if (format
== stObj
->pt
->format
)
81 case PIPE_FORMAT_NV12
:
82 case PIPE_FORMAT_P010
:
83 case PIPE_FORMAT_P016
:
84 key
.lower_nv12
|= (1 << unit
);
86 case PIPE_FORMAT_IYUV
:
87 key
.lower_iyuv
|= (1 << unit
);
89 case PIPE_FORMAT_YUYV
:
90 key
.lower_yx_xuxv
|= (1 << unit
);
92 case PIPE_FORMAT_UYVY
:
93 key
.lower_xy_uxvx
|= (1 << unit
);
95 case PIPE_FORMAT_AYUV
:
96 key
.lower_ayuv
|= (1 << unit
);
98 case PIPE_FORMAT_XYUV
:
99 key
.lower_xyuv
|= (1 << unit
);
102 printf("mesa: st_get_external_sampler_key: unhandled pipe format %u\n",
111 /** Fragment program variant key */
112 struct st_fp_variant_key
114 struct st_context
*st
; /**< variants are per-context */
117 GLuint bitmap
:1; /**< glBitmap variant? */
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? */
124 /** for ARB_color_buffer_float */
125 GLuint clamp_color
:1;
127 /** for ARB_sample_shading */
128 GLuint persample_shading
:1;
130 /** needed for ATI_fragment_shader */
133 /** for ARB_depth_clamp */
134 GLuint lower_depth_clamp
:1;
136 /** for OpenGL 1.0 on modern hardware */
137 GLuint lower_two_sided_color
:1;
139 GLuint lower_flatshade
:1;
140 unsigned lower_alpha_func
:3;
142 /** needed for ATI_fragment_shader */
143 char texture_targets
[MAX_NUM_FRAGMENT_REGISTERS_ATI
];
145 struct st_external_sampler_key external
;
149 * Base class for shader variants.
153 /** next in linked list */
154 struct st_variant
*next
;
156 /** st_context from the shader key */
157 struct st_context
*st
;
163 * Variant of a fragment program.
167 struct st_variant base
;
169 /** Parameters which generated this version of fragment program */
170 struct st_fp_variant_key key
;
172 /** For glBitmap variants */
175 /** For glDrawPixels variants */
176 unsigned drawpix_sampler
;
177 unsigned pixelmap_sampler
;
181 /** Shader key shared by other shaders */
182 struct st_common_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
;
194 /** lower glPointSize to gl_PointSize */
195 boolean lower_point_size
;
197 /* for user-defined clip-planes */
200 /* Whether st_variant::driver_shader is for the draw module,
201 * not for the driver.
208 * Common shader variant.
210 struct st_common_variant
212 struct st_variant base
;
214 /* Parameters which generated this variant. */
215 struct st_common_variant_key key
;
217 /* Bitfield of VERT_BIT_* bits matching vertex shader inputs,
218 * but not include the high part of doubles.
220 GLbitfield vert_attrib_mask
;
225 * Derived from Mesa gl_program:
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 */
235 void *serialized_nir
;
236 unsigned serialized_nir_size
;
238 /* used when bypassing glsl_to_tgsi: */
239 struct gl_shader_program
*shader_program
;
241 struct st_variant
*variants
;
245 struct st_vertex_program
247 struct st_program Base
;
249 /** maps a TGSI input index back to a Mesa VERT_ATTRIB_x */
250 ubyte index_to_input
[PIPE_MAX_ATTRIBS
];
252 /** Reverse mapping of the above */
253 ubyte input_to_index
[VERT_ATTRIB_MAX
];
255 /** Maps VARYING_SLOT_x to slot */
256 ubyte result_to_output
[VARYING_SLOT_MAX
];
260 static inline struct st_program
*
261 st_program( struct gl_program
*cp
)
263 return (struct st_program
*)cp
;
267 st_reference_prog(struct st_context
*st
,
268 struct st_program
**ptr
,
269 struct st_program
*prog
)
271 _mesa_reference_program(st
->ctx
,
272 (struct gl_program
**) ptr
,
273 (struct gl_program
*) prog
);
276 static inline struct st_common_variant
*
277 st_common_variant(struct st_variant
*v
)
279 return (struct st_common_variant
*)v
;
282 static inline struct st_fp_variant
*
283 st_fp_variant(struct st_variant
*v
)
285 return (struct st_fp_variant
*)v
;
289 * This defines mapping from Mesa VARYING_SLOTs to TGSI GENERIC slots.
291 static inline unsigned
292 st_get_generic_varying_index(struct st_context
*st
, GLuint attr
)
294 return tgsi_get_generic_gl_varying_index((gl_varying_slot
)attr
,
295 st
->needs_texcoord_semantic
);
299 st_set_prog_affected_state_flags(struct gl_program
*prog
);
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
);
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
);
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
);
318 st_release_variants(struct st_context
*st
, struct st_program
*p
);
321 st_release_program(struct st_context
*st
, struct st_program
**p
);
324 st_destroy_program_variants(struct st_context
*st
);
327 st_finalize_nir_before_variants(struct nir_shader
*nir
);
330 st_prepare_vertex_program(struct st_program
*stvp
);
333 st_translate_stream_output_info(struct gl_program
*prog
);
336 st_translate_vertex_program(struct st_context
*st
,
337 struct st_program
*stvp
);
340 st_translate_fragment_program(struct st_context
*st
,
341 struct st_program
*stfp
);
344 st_translate_common_program(struct st_context
*st
,
345 struct st_program
*stp
);
348 st_serialize_nir(struct st_program
*stp
);
351 st_finalize_program(struct st_context
*st
, struct gl_program
*prog
);