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 struct st_external_sampler_key
51 {
52    GLuint lower_nv12;             /**< bitmask of 2 plane YUV samplers */
53    GLuint lower_iyuv;             /**< bitmask of 3 plane YUV samplers */
54    GLuint lower_xy_uxvx;          /**< bitmask of 2 plane YUV samplers */
55    GLuint lower_yx_xuxv;          /**< bitmask of 2 plane YUV samplers */
56    GLuint lower_ayuv;
57    GLuint lower_xyuv;
58    GLuint lower_yuv;
59    GLuint lower_yu_yv;
60    GLuint lower_y41x;
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          if (stObj->pt->format == PIPE_FORMAT_R8G8_R8B8_UNORM) {
98             key.lower_yu_yv |= (1 << unit);
99             break;
100          }
101          FALLTHROUGH;
102       case PIPE_FORMAT_Y210:
103       case PIPE_FORMAT_Y212:
104       case PIPE_FORMAT_Y216:
105          key.lower_yx_xuxv |= (1 << unit);
106          break;
107       case PIPE_FORMAT_UYVY:
108          if (stObj->pt->format == PIPE_FORMAT_G8R8_B8R8_UNORM) {
109             key.lower_yu_yv |= (1 << unit);
110             break;
111          }
112          key.lower_xy_uxvx |= (1 << unit);
113          break;
114       case PIPE_FORMAT_AYUV:
115          key.lower_ayuv |= (1 << unit);
116          break;
117       case PIPE_FORMAT_XYUV:
118          key.lower_xyuv |= (1 << unit);
119          break;
120       case PIPE_FORMAT_Y410:
121       case PIPE_FORMAT_Y412:
122       case PIPE_FORMAT_Y416:
123          key.lower_y41x |= (1 << unit);
124          break;
125       default:
126          printf("mesa: st_get_external_sampler_key: unhandled pipe format %u\n",
127                 format);
128          break;
129       }
130    }
131 
132    return key;
133 }
134 
135 /** Fragment program variant key
136  *
137  * Please update st_get_fp_variant() perf_debug() when adding fields.
138  */
139 struct st_fp_variant_key
140 {
141    struct st_context *st;         /**< variants are per-context */
142 
143    /** for glBitmap */
144    GLuint bitmap:1;               /**< glBitmap variant? */
145 
146    /** for glDrawPixels */
147    GLuint drawpixels:1;           /**< glDrawPixels variant */
148    GLuint scaleAndBias:1;         /**< glDrawPixels w/ scale and/or bias? */
149    GLuint pixelMaps:1;            /**< glDrawPixels w/ pixel lookup map? */
150 
151    /** for ARB_color_buffer_float */
152    GLuint clamp_color:1;
153 
154    /** for ARB_sample_shading */
155    GLuint persample_shading:1;
156 
157    /** needed for ATI_fragment_shader */
158    GLuint fog:2;
159 
160    /** for ARB_depth_clamp */
161    GLuint lower_depth_clamp:1;
162 
163    /** for OpenGL 1.0 on modern hardware */
164    GLuint lower_two_sided_color:1;
165 
166    GLuint lower_flatshade:1;
167    GLuint lower_texcoord_replace:MAX_TEXTURE_COORD_UNITS;
168    unsigned lower_alpha_func:3;
169 
170    /** needed for ATI_fragment_shader */
171    uint8_t texture_index[MAX_NUM_FRAGMENT_REGISTERS_ATI];
172 
173    struct st_external_sampler_key external;
174 
175    /* bitmask of sampler units; PIPE_CAP_GL_CLAMP */
176    uint32_t gl_clamp[3];
177 };
178 
179 /**
180  * Base class for shader variants.
181  */
182 struct st_variant
183 {
184    /** next in linked list */
185    struct st_variant *next;
186 
187    /** st_context from the shader key */
188    struct st_context *st;
189 
190    void *driver_shader;
191 };
192 
193 /**
194  * Variant of a fragment program.
195  */
196 struct st_fp_variant
197 {
198    struct st_variant base;
199 
200    /** Parameters which generated this version of fragment program */
201    struct st_fp_variant_key key;
202 
203    /** For glBitmap variants */
204    uint bitmap_sampler;
205 
206    /** For glDrawPixels variants */
207    unsigned drawpix_sampler;
208    unsigned pixelmap_sampler;
209 };
210 
211 
212 /** Shader key shared by other shaders.
213  *
214  * Please update st_get_common_variant() perf_debug() when adding fields.
215  */
216 struct st_common_variant_key
217 {
218    struct st_context *st;          /**< variants are per-context */
219    bool passthrough_edgeflags;
220 
221    /** for ARB_color_buffer_float */
222    bool clamp_color;
223 
224    /** both for ARB_depth_clamp */
225    bool lower_depth_clamp;
226    bool clip_negative_one_to_one;
227 
228    /** lower glPointSize to gl_PointSize */
229    boolean lower_point_size;
230 
231    /* for user-defined clip-planes */
232    uint8_t lower_ucp;
233 
234    /* Whether st_variant::driver_shader is for the draw module,
235     * not for the driver.
236     */
237    bool is_draw_shader;
238 
239    /* bitmask of sampler units; PIPE_CAP_GL_CLAMP */
240    uint32_t gl_clamp[3];
241 };
242 
243 
244 /**
245  * Common shader variant.
246  */
247 struct st_common_variant
248 {
249    struct st_variant base;
250 
251    /* Parameters which generated this variant. */
252    struct st_common_variant_key key;
253 
254    /* Bitfield of VERT_BIT_* bits matching vertex shader inputs. */
255    GLbitfield vert_attrib_mask;
256 };
257 
258 
259 /**
260  * Derived from Mesa gl_program:
261  */
262 struct st_program
263 {
264    struct gl_program Base;
265    struct pipe_shader_state state;
266    struct glsl_to_tgsi_visitor* glsl_to_tgsi;
267    struct ati_fragment_shader *ati_fs;
268    uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
269 
270    void *serialized_nir;
271    unsigned serialized_nir_size;
272 
273    /* used when bypassing glsl_to_tgsi: */
274    struct gl_shader_program *shader_program;
275 
276    struct st_variant *variants;
277 };
278 
279 
280 struct st_vertex_program
281 {
282    struct st_program Base;
283 
284    uint32_t vert_attrib_mask; /**< mask of sourced vertex attribs */
285    ubyte num_inputs;
286 
287    /** Maps VARYING_SLOT_x to slot */
288    ubyte result_to_output[VARYING_SLOT_MAX];
289 };
290 
291 
292 static inline struct st_program *
st_program(struct gl_program * cp)293 st_program( struct gl_program *cp )
294 {
295    return (struct st_program *)cp;
296 }
297 
298 static inline void
st_reference_prog(struct st_context * st,struct st_program ** ptr,struct st_program * prog)299 st_reference_prog(struct st_context *st,
300                   struct st_program **ptr,
301                   struct st_program *prog)
302 {
303    _mesa_reference_program(st->ctx,
304                            (struct gl_program **) ptr,
305                            (struct gl_program *) prog);
306 }
307 
308 static inline struct st_common_variant *
st_common_variant(struct st_variant * v)309 st_common_variant(struct st_variant *v)
310 {
311    return (struct st_common_variant*)v;
312 }
313 
314 static inline struct st_fp_variant *
st_fp_variant(struct st_variant * v)315 st_fp_variant(struct st_variant *v)
316 {
317    return (struct st_fp_variant*)v;
318 }
319 
320 /**
321  * This defines mapping from Mesa VARYING_SLOTs to TGSI GENERIC slots.
322  */
323 static inline unsigned
st_get_generic_varying_index(struct st_context * st,GLuint attr)324 st_get_generic_varying_index(struct st_context *st, GLuint attr)
325 {
326    return tgsi_get_generic_gl_varying_index((gl_varying_slot)attr,
327                                             st->needs_texcoord_semantic);
328 }
329 
330 extern void
331 st_set_prog_affected_state_flags(struct gl_program *prog);
332 
333 
334 extern struct st_fp_variant *
335 st_get_fp_variant(struct st_context *st,
336                   struct st_program *stfp,
337                   const struct st_fp_variant_key *key);
338 
339 extern struct st_common_variant *
340 st_get_common_variant(struct st_context *st,
341                       struct st_program *p,
342                       const struct st_common_variant_key *key);
343 
344 extern void
345 st_release_variants(struct st_context *st, struct st_program *p);
346 
347 extern void
348 st_release_program(struct st_context *st, struct st_program **p);
349 
350 extern void
351 st_destroy_program_variants(struct st_context *st);
352 
353 extern void
354 st_finalize_nir_before_variants(struct nir_shader *nir);
355 
356 extern void
357 st_prepare_vertex_program(struct st_program *stvp, uint8_t *attrib_to_index);
358 
359 extern void
360 st_translate_stream_output_info(struct gl_program *prog);
361 
362 extern bool
363 st_translate_vertex_program(struct st_context *st,
364                             struct st_program *stvp);
365 
366 extern bool
367 st_translate_fragment_program(struct st_context *st,
368                               struct st_program *stfp);
369 
370 extern bool
371 st_translate_common_program(struct st_context *st,
372                             struct st_program *stp);
373 
374 extern void
375 st_serialize_nir(struct st_program *stp);
376 
377 extern void
378 st_finalize_program(struct st_context *st, struct gl_program *prog);
379 
380 struct pipe_shader_state *
381 st_create_nir_shader(struct st_context *st, struct pipe_shader_state *state);
382 
383 #ifdef __cplusplus
384 }
385 #endif
386 
387 #endif
388