• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 };
57 
58 static inline struct st_external_sampler_key
st_get_external_sampler_key(struct st_context * st,struct gl_program * prog)59 st_get_external_sampler_key(struct st_context *st, struct gl_program *prog)
60 {
61    unsigned mask = prog->ExternalSamplersUsed;
62    struct st_external_sampler_key key;
63 
64    memset(&key, 0, sizeof(key));
65 
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);
70 
71       switch (st_get_view_format(stObj)) {
72       case PIPE_FORMAT_NV12:
73          key.lower_nv12 |= (1 << unit);
74          break;
75       case PIPE_FORMAT_IYUV:
76          key.lower_iyuv |= (1 << unit);
77          break;
78       default:
79          break;
80       }
81    }
82 
83    return key;
84 }
85 
86 /** Fragment program variant key */
87 struct st_fp_variant_key
88 {
89    struct st_context *st;         /**< variants are per-context */
90 
91    /** for glBitmap */
92    GLuint bitmap:1;               /**< glBitmap variant? */
93 
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? */
98 
99    /** for ARB_color_buffer_float */
100    GLuint clamp_color:1;
101 
102    /** for ARB_sample_shading */
103    GLuint persample_shading:1;
104 
105    /** needed for ATI_fragment_shader */
106    GLuint fog:2;
107 
108    /** needed for ATI_fragment_shader */
109    char texture_targets[MAX_NUM_FRAGMENT_REGISTERS_ATI];
110 
111    struct st_external_sampler_key external;
112 };
113 
114 
115 /**
116  * Variant of a fragment program.
117  */
118 struct st_fp_variant
119 {
120    /** Parameters which generated this version of fragment program */
121    struct st_fp_variant_key key;
122 
123    /** Driver's compiled shader */
124    void *driver_shader;
125 
126    /** For glBitmap variants */
127    uint bitmap_sampler;
128 
129    /** For glDrawPixels variants */
130    unsigned drawpix_sampler;
131    unsigned pixelmap_sampler;
132 
133    /** next in linked list */
134    struct st_fp_variant *next;
135 };
136 
137 
138 /**
139  * Derived from Mesa gl_program:
140  */
141 struct st_fragment_program
142 {
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 */
148 
149    /* used when bypassing glsl_to_tgsi: */
150    struct gl_shader_program *shader_program;
151 
152    struct st_fp_variant *variants;
153 
154    /* Used by the shader cache and ARB_get_program_binary */
155    unsigned num_tgsi_tokens;
156 };
157 
158 
159 
160 /** Vertex program variant key */
161 struct st_vp_variant_key
162 {
163    struct st_context *st;          /**< variants are per-context */
164    boolean passthrough_edgeflags;
165 
166    /** for ARB_color_buffer_float */
167    boolean clamp_color;
168 };
169 
170 
171 /**
172  * This represents a vertex program, especially translated to match
173  * the inputs of a particular fragment shader.
174  */
175 struct st_vp_variant
176 {
177    /* Parameters which generated this translated version of a vertex
178     * shader:
179     */
180    struct st_vp_variant_key key;
181 
182    /**
183     * TGSI tokens (to later generate a 'draw' module shader for
184     * selection/feedback/rasterpos)
185     */
186    struct pipe_shader_state tgsi;
187 
188    /** Driver's compiled shader */
189    void *driver_shader;
190 
191    /** For using our private draw module (glRasterPos) */
192    struct draw_vertex_shader *draw_shader;
193 
194    /** Next in linked list */
195    struct st_vp_variant *next;
196 
197    /** similar to that in st_vertex_program, but with edgeflags info too */
198    GLuint num_inputs;
199 };
200 
201 
202 /**
203  * Derived from Mesa gl_program:
204  */
205 struct st_vertex_program
206 {
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 */
211 
212    /* used when bypassing glsl_to_tgsi: */
213    struct gl_shader_program *shader_program;
214 
215    /** maps a TGSI input index back to a Mesa VERT_ATTRIB_x */
216    ubyte index_to_input[PIPE_MAX_ATTRIBS];
217    ubyte num_inputs;
218 
219    /** Maps VARYING_SLOT_x to slot */
220    ubyte result_to_output[VARYING_SLOT_MAX];
221 
222    /** List of translated variants of this vertex program.
223     */
224    struct st_vp_variant *variants;
225 
226    /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
227    unsigned char sha1[20];
228 
229    /* Used by the shader cache and ARB_get_program_binary */
230    unsigned num_tgsi_tokens;
231 };
232 
233 
234 
235 /** Key shared by all shaders except VP, FP */
236 struct st_basic_variant_key
237 {
238    struct st_context *st;          /**< variants are per-context */
239 };
240 
241 
242 /**
243  * Geometry program variant.
244  */
245 struct st_basic_variant
246 {
247    /* Parameters which generated this variant. */
248    struct st_basic_variant_key key;
249 
250    void *driver_shader;
251 
252    struct st_basic_variant *next;
253 };
254 
255 
256 /**
257  * Derived from Mesa gl_program:
258  */
259 struct st_common_program
260 {
261    struct gl_program Base;
262    struct pipe_shader_state tgsi;
263    struct glsl_to_tgsi_visitor* glsl_to_tgsi;
264    uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
265 
266   /* used when bypassing glsl_to_tgsi: */
267    struct gl_shader_program *shader_program;
268 
269    struct st_basic_variant *variants;
270 
271    /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
272    unsigned char sha1[20];
273 
274    /* Used by the shader cache and ARB_get_program_binary */
275    unsigned num_tgsi_tokens;
276 };
277 
278 
279 /**
280  * Derived from Mesa gl_program:
281  */
282 struct st_compute_program
283 {
284    struct gl_program Base;  /**< The Mesa compute program */
285    struct pipe_compute_state tgsi;
286    struct glsl_to_tgsi_visitor* glsl_to_tgsi;
287    uint64_t affected_states; /**< ST_NEW_* flags to mark dirty when binding */
288 
289    /* used when bypassing glsl_to_tgsi: */
290    struct gl_shader_program *shader_program;
291 
292    struct st_basic_variant *variants;
293 
294    /** SHA1 hash of linked tgsi shader program, used for on-disk cache */
295    unsigned char sha1[20];
296 
297    /* Used by the shader cache and ARB_get_program_binary */
298    unsigned num_tgsi_tokens;
299 };
300 
301 
302 static inline struct st_fragment_program *
st_fragment_program(struct gl_program * fp)303 st_fragment_program( struct gl_program *fp )
304 {
305    return (struct st_fragment_program *)fp;
306 }
307 
308 
309 static inline struct st_vertex_program *
st_vertex_program(struct gl_program * vp)310 st_vertex_program( struct gl_program *vp )
311 {
312    return (struct st_vertex_program *)vp;
313 }
314 
315 static inline struct st_common_program *
st_common_program(struct gl_program * gp)316 st_common_program( struct gl_program *gp )
317 {
318    return (struct st_common_program *)gp;
319 }
320 
321 static inline struct st_compute_program *
st_compute_program(struct gl_program * cp)322 st_compute_program( struct gl_program *cp )
323 {
324    return (struct st_compute_program *)cp;
325 }
326 
327 static inline void
st_reference_vertprog(struct st_context * st,struct st_vertex_program ** ptr,struct st_vertex_program * prog)328 st_reference_vertprog(struct st_context *st,
329                       struct st_vertex_program **ptr,
330                       struct st_vertex_program *prog)
331 {
332    _mesa_reference_program(st->ctx,
333                            (struct gl_program **) ptr,
334                            (struct gl_program *) prog);
335 }
336 
337 static inline void
st_reference_fragprog(struct st_context * st,struct st_fragment_program ** ptr,struct st_fragment_program * prog)338 st_reference_fragprog(struct st_context *st,
339                       struct st_fragment_program **ptr,
340                       struct st_fragment_program *prog)
341 {
342    _mesa_reference_program(st->ctx,
343                            (struct gl_program **) ptr,
344                            (struct gl_program *) prog);
345 }
346 
347 static inline void
st_reference_prog(struct st_context * st,struct st_common_program ** ptr,struct st_common_program * prog)348 st_reference_prog(struct st_context *st,
349                   struct st_common_program **ptr,
350                   struct st_common_program *prog)
351 {
352    _mesa_reference_program(st->ctx,
353                            (struct gl_program **) ptr,
354                            (struct gl_program *) prog);
355 }
356 
357 static inline void
st_reference_compprog(struct st_context * st,struct st_compute_program ** ptr,struct st_compute_program * prog)358 st_reference_compprog(struct st_context *st,
359                       struct st_compute_program **ptr,
360                       struct st_compute_program *prog)
361 {
362    _mesa_reference_program(st->ctx,
363                            (struct gl_program **) ptr,
364                            (struct gl_program *) prog);
365 }
366 
367 /**
368  * This defines mapping from Mesa VARYING_SLOTs to TGSI GENERIC slots.
369  */
370 static inline unsigned
st_get_generic_varying_index(struct st_context * st,GLuint attr)371 st_get_generic_varying_index(struct st_context *st, GLuint attr)
372 {
373    return tgsi_get_generic_gl_varying_index((gl_varying_slot)attr,
374                                             st->needs_texcoord_semantic);
375 }
376 
377 extern void
378 st_set_prog_affected_state_flags(struct gl_program *prog);
379 
380 extern struct st_vp_variant *
381 st_get_vp_variant(struct st_context *st,
382                   struct st_vertex_program *stvp,
383                   const struct st_vp_variant_key *key);
384 
385 
386 extern struct st_fp_variant *
387 st_get_fp_variant(struct st_context *st,
388                   struct st_fragment_program *stfp,
389                   const struct st_fp_variant_key *key);
390 
391 extern struct st_basic_variant *
392 st_get_cp_variant(struct st_context *st,
393                   struct pipe_compute_state *tgsi,
394                   struct st_basic_variant **variants);
395 
396 extern struct st_basic_variant *
397 st_get_basic_variant(struct st_context *st,
398                      unsigned pipe_shader,
399                      struct st_common_program *p);
400 
401 extern void
402 st_release_vp_variants( struct st_context *st,
403                         struct st_vertex_program *stvp );
404 
405 extern void
406 st_release_fp_variants( struct st_context *st,
407                         struct st_fragment_program *stfp );
408 
409 extern void
410 st_release_cp_variants(struct st_context *st,
411                         struct st_compute_program *stcp);
412 
413 extern void
414 st_release_basic_variants(struct st_context *st, GLenum target,
415                           struct st_basic_variant **variants,
416                           struct pipe_shader_state *tgsi);
417 
418 extern void
419 st_destroy_program_variants(struct st_context *st);
420 
421 extern bool
422 st_translate_vertex_program(struct st_context *st,
423                             struct st_vertex_program *stvp);
424 
425 extern bool
426 st_translate_fragment_program(struct st_context *st,
427                               struct st_fragment_program *stfp);
428 
429 extern bool
430 st_translate_geometry_program(struct st_context *st,
431                               struct st_common_program *stgp);
432 
433 extern bool
434 st_translate_tessctrl_program(struct st_context *st,
435                               struct st_common_program *sttcp);
436 
437 extern bool
438 st_translate_tesseval_program(struct st_context *st,
439                               struct st_common_program *sttep);
440 
441 extern bool
442 st_translate_compute_program(struct st_context *st,
443                              struct st_compute_program *stcp);
444 
445 extern void
446 st_print_current_vertex_program(void);
447 
448 extern void
449 st_precompile_shader_variant(struct st_context *st,
450                              struct gl_program *prog);
451 
452 #ifdef __cplusplus
453 }
454 #endif
455 
456 #endif
457