1 /**************************************************************************
2  *
3  * Copyright 2003 VMware, Inc.
4  * All Rights Reserved.
5  *
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:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
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.
25  *
26  **************************************************************************/
27 
28  /*
29   * Authors:
30   *   Keith Whitwell <keithw@vmware.com>
31   */
32 
33 
34 #ifndef ST_PROGRAM_H
35 #define ST_PROGRAM_H
36 
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"
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 #define ST_DOUBLE_ATTRIB_PLACEHOLDER 0xff
51 
52 struct st_external_sampler_key
53 {
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 */
58    GLuint lower_ayuv;
59    GLuint lower_xyuv;
60    GLuint lower_yuv;
61 };
62 
63 static inline struct st_external_sampler_key
st_get_external_sampler_key(struct st_context * st,struct gl_program * prog)64 st_get_external_sampler_key(struct st_context *st, struct gl_program *prog)
65 {
66    unsigned mask = prog->ExternalSamplersUsed;
67    struct st_external_sampler_key key;
68 
69    memset(&key, 0, sizeof(key));
70 
71    while (unlikely(mask)) {
72       unsigned unit = u_bit_scan(&mask);
73       struct st_texture_object *stObj =
74             st_get_texture_object(st->ctx, prog, unit);
75       enum pipe_format format = st_get_view_format(stObj);
76 
77       /* if resource format matches then YUV wasn't lowered */
78       if (format == stObj->pt->format)
79          continue;
80 
81       switch (format) {
82       case PIPE_FORMAT_NV12:
83          if (stObj->pt->format == PIPE_FORMAT_R8_G8B8_420_UNORM) {
84             key.lower_yuv |= (1 << unit);
85             break;
86          }
87          /* fallthrough */
88       case PIPE_FORMAT_P010:
89       case PIPE_FORMAT_P012:
90       case PIPE_FORMAT_P016:
91          key.lower_nv12 |= (1 << unit);
92          break;
93       case PIPE_FORMAT_IYUV:
94          key.lower_iyuv |= (1 << unit);
95          break;
96       case PIPE_FORMAT_YUYV:
97          key.lower_yx_xuxv |= (1 << unit);
98          break;
99       case PIPE_FORMAT_UYVY:
100          key.lower_xy_uxvx |= (1 << unit);
101          break;
102       case PIPE_FORMAT_AYUV:
103          key.lower_ayuv |= (1 << unit);
104          break;
105       case PIPE_FORMAT_XYUV:
106          key.lower_xyuv |= (1 << unit);
107          break;
108       default:
109          printf("mesa: st_get_external_sampler_key: unhandled pipe format %u\n",
110                 format);
111          break;
112       }
113    }
114 
115    return key;
116 }
117 
118 /** Fragment program variant key */
119 struct st_fp_variant_key
120 {
121    struct st_context *st;         /**< variants are per-context */
122 
123    /** for glBitmap */
124    GLuint bitmap:1;               /**< glBitmap variant? */
125 
126    /** for glDrawPixels */
127    GLuint drawpixels:1;           /**< glDrawPixels variant */
128    GLuint scaleAndBias:1;         /**< glDrawPixels w/ scale and/or bias? */
129    GLuint pixelMaps:1;            /**< glDrawPixels w/ pixel lookup map? */
130 
131    /** for ARB_color_buffer_float */
132    GLuint clamp_color:1;
133 
134    /** for ARB_sample_shading */
135    GLuint persample_shading:1;
136 
137    /** needed for ATI_fragment_shader */
138    GLuint fog:2;
139 
140    /** for ARB_depth_clamp */
141    GLuint lower_depth_clamp:1;
142 
143    /** for OpenGL 1.0 on modern hardware */
144    GLuint lower_two_sided_color:1;
145 
146    GLuint lower_flatshade:1;
147    unsigned lower_alpha_func:3;
148 
149    /** needed for ATI_fragment_shader */
150    char texture_targets[MAX_NUM_FRAGMENT_REGISTERS_ATI];
151 
152    struct st_external_sampler_key external;
153 };
154 
155 /**
156  * Base class for shader variants.
157  */
158 struct st_variant
159 {
160    /** next in linked list */
161    struct st_variant *next;
162 
163    /** st_context from the shader key */
164    struct st_context *st;
165 
166    void *driver_shader;
167 };
168 
169 /**
170  * Variant of a fragment program.
171  */
172 struct st_fp_variant
173 {
174    struct st_variant base;
175 
176    /** Parameters which generated this version of fragment program */
177    struct st_fp_variant_key key;
178 
179    /** For glBitmap variants */
180    uint bitmap_sampler;
181 
182    /** For glDrawPixels variants */
183    unsigned drawpix_sampler;
184    unsigned pixelmap_sampler;
185 };
186 
187 
188 /** Shader key shared by other shaders */
189 struct st_common_variant_key
190 {
191    struct st_context *st;          /**< variants are per-context */
192    bool passthrough_edgeflags;
193 
194    /** for ARB_color_buffer_float */
195    bool clamp_color;
196 
197    /** both for ARB_depth_clamp */
198    bool lower_depth_clamp;
199    bool clip_negative_one_to_one;
200 
201    /** lower glPointSize to gl_PointSize */
202    boolean lower_point_size;
203 
204    /* for user-defined clip-planes */
205    uint8_t lower_ucp;
206 
207    /* Whether st_variant::driver_shader is for the draw module,
208     * not for the driver.
209     */
210    bool is_draw_shader;
211 };
212 
213 
214 /**
215  * Common shader variant.
216  */
217 struct st_common_variant
218 {
219    struct st_variant base;
220 
221    /* Parameters which generated this variant. */
222    struct st_common_variant_key key;
223 
224    /* Bitfield of VERT_BIT_* bits matching vertex shader inputs,
225     * but not include the high part of doubles.
226     */
227    GLbitfield vert_attrib_mask;
228 };
229 
230 
231 /**
232  * Derived from Mesa gl_program:
233  */
234 struct st_program
235 {
236    struct gl_program Base;
237    struct pipe_shader_state state;
238    struct glsl_to_tgsi_visitor* glsl_to_tgsi;
239    struct ati_fragment_shader *ati_fs;
240    uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
241 
242    void *serialized_nir;
243    unsigned serialized_nir_size;
244 
245    /* used when bypassing glsl_to_tgsi: */
246    struct gl_shader_program *shader_program;
247 
248    struct st_variant *variants;
249 };
250 
251 
252 struct st_vertex_program
253 {
254    struct st_program Base;
255 
256    /** maps a TGSI input index back to a Mesa VERT_ATTRIB_x */
257    ubyte index_to_input[PIPE_MAX_ATTRIBS];
258    ubyte num_inputs;
259    /** Reverse mapping of the above */
260    ubyte input_to_index[VERT_ATTRIB_MAX];
261 
262    /** Maps VARYING_SLOT_x to slot */
263    ubyte result_to_output[VARYING_SLOT_MAX];
264 };
265 
266 
267 static inline struct st_program *
st_program(struct gl_program * cp)268 st_program( struct gl_program *cp )
269 {
270    return (struct st_program *)cp;
271 }
272 
273 static inline void
st_reference_prog(struct st_context * st,struct st_program ** ptr,struct st_program * prog)274 st_reference_prog(struct st_context *st,
275                   struct st_program **ptr,
276                   struct st_program *prog)
277 {
278    _mesa_reference_program(st->ctx,
279                            (struct gl_program **) ptr,
280                            (struct gl_program *) prog);
281 }
282 
283 static inline struct st_common_variant *
st_common_variant(struct st_variant * v)284 st_common_variant(struct st_variant *v)
285 {
286    return (struct st_common_variant*)v;
287 }
288 
289 static inline struct st_fp_variant *
st_fp_variant(struct st_variant * v)290 st_fp_variant(struct st_variant *v)
291 {
292    return (struct st_fp_variant*)v;
293 }
294 
295 /**
296  * This defines mapping from Mesa VARYING_SLOTs to TGSI GENERIC slots.
297  */
298 static inline unsigned
st_get_generic_varying_index(struct st_context * st,GLuint attr)299 st_get_generic_varying_index(struct st_context *st, GLuint attr)
300 {
301    return tgsi_get_generic_gl_varying_index((gl_varying_slot)attr,
302                                             st->needs_texcoord_semantic);
303 }
304 
305 extern void
306 st_set_prog_affected_state_flags(struct gl_program *prog);
307 
308 extern struct st_common_variant *
309 st_get_vp_variant(struct st_context *st,
310                   struct st_program *stvp,
311                   const struct st_common_variant_key *key);
312 
313 
314 extern struct st_fp_variant *
315 st_get_fp_variant(struct st_context *st,
316                   struct st_program *stfp,
317                   const struct st_fp_variant_key *key);
318 
319 extern struct st_variant *
320 st_get_common_variant(struct st_context *st,
321                       struct st_program *p,
322                       const struct st_common_variant_key *key);
323 
324 extern void
325 st_release_variants(struct st_context *st, struct st_program *p);
326 
327 extern void
328 st_release_program(struct st_context *st, struct st_program **p);
329 
330 extern void
331 st_destroy_program_variants(struct st_context *st);
332 
333 extern void
334 st_finalize_nir_before_variants(struct nir_shader *nir);
335 
336 extern void
337 st_prepare_vertex_program(struct st_program *stvp);
338 
339 extern void
340 st_translate_stream_output_info(struct gl_program *prog);
341 
342 extern bool
343 st_translate_vertex_program(struct st_context *st,
344                             struct st_program *stvp);
345 
346 extern bool
347 st_translate_fragment_program(struct st_context *st,
348                               struct st_program *stfp);
349 
350 extern bool
351 st_translate_common_program(struct st_context *st,
352                             struct st_program *stp);
353 
354 extern void
355 st_serialize_nir(struct st_program *stp);
356 
357 extern void
358 st_finalize_program(struct st_context *st, struct gl_program *prog);
359 
360 #ifdef __cplusplus
361 }
362 #endif
363 
364 #endif
365