1/**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 <keith@tungstengraphics.com>
31  */
32
33
34#ifndef ST_PROGRAM_H
35#define ST_PROGRAM_H
36
37#include "main/mtypes.h"
38#include "program/program.h"
39#include "pipe/p_state.h"
40#include "st_context.h"
41#include "st_glsl_to_tgsi.h"
42
43
44/** Fragment program variant key */
45struct st_fp_variant_key
46{
47   struct st_context *st;         /**< variants are per-context */
48
49   /** for glBitmap */
50   GLuint bitmap:1;               /**< glBitmap variant? */
51
52   /** for glDrawPixels */
53   GLuint drawpixels:1;           /**< glDrawPixels variant */
54   GLuint scaleAndBias:1;         /**< glDrawPixels w/ scale and/or bias? */
55   GLuint pixelMaps:1;            /**< glDrawPixels w/ pixel lookup map? */
56   GLuint drawpixels_z:1;         /**< glDrawPixels(GL_DEPTH) */
57   GLuint drawpixels_stencil:1;   /**< glDrawPixels(GL_STENCIL) */
58
59   /** for ARB_color_buffer_float */
60   GLuint clamp_color:1;
61};
62
63
64/**
65 * Variant of a fragment program.
66 */
67struct st_fp_variant
68{
69   /** Parameters which generated this version of fragment program */
70   struct st_fp_variant_key key;
71
72   struct pipe_shader_state tgsi;
73
74   /** Driver's compiled shader */
75   void *driver_shader;
76
77   /** For glBitmap variants */
78   struct gl_program_parameter_list *parameters;
79   uint bitmap_sampler;
80
81   /** next in linked list */
82   struct st_fp_variant *next;
83};
84
85
86/**
87 * Derived from Mesa gl_fragment_program:
88 */
89struct st_fragment_program
90{
91   struct gl_fragment_program Base;
92   struct glsl_to_tgsi_visitor* glsl_to_tgsi;
93
94   struct st_fp_variant *variants;
95};
96
97
98
99/** Vertex program variant key */
100struct st_vp_variant_key
101{
102   struct st_context *st;          /**< variants are per-context */
103   boolean passthrough_edgeflags;
104
105   /** for ARB_color_buffer_float */
106   boolean clamp_color;
107};
108
109
110/**
111 * This represents a vertex program, especially translated to match
112 * the inputs of a particular fragment shader.
113 */
114struct st_vp_variant
115{
116   /* Parameters which generated this translated version of a vertex
117    * shader:
118    */
119   struct st_vp_variant_key key;
120
121   /**
122    * TGSI tokens (to later generate a 'draw' module shader for
123    * selection/feedback/rasterpos)
124    */
125   struct pipe_shader_state tgsi;
126
127   /** Driver's compiled shader */
128   void *driver_shader;
129
130   /** For using our private draw module (glRasterPos) */
131   struct draw_vertex_shader *draw_shader;
132
133   /** Next in linked list */
134   struct st_vp_variant *next;
135
136   /** similar to that in st_vertex_program, but with edgeflags info too */
137   GLuint num_inputs;
138};
139
140
141/**
142 * Derived from Mesa gl_fragment_program:
143 */
144struct st_vertex_program
145{
146   struct gl_vertex_program Base;  /**< The Mesa vertex program */
147   struct glsl_to_tgsi_visitor* glsl_to_tgsi;
148
149   /** maps a Mesa VERT_ATTRIB_x to a packed TGSI input index */
150   GLuint input_to_index[VERT_ATTRIB_MAX];
151   /** maps a TGSI input index back to a Mesa VERT_ATTRIB_x */
152   GLuint index_to_input[PIPE_MAX_SHADER_INPUTS];
153   GLuint num_inputs;
154
155   /** Maps VERT_RESULT_x to slot */
156   GLuint result_to_output[VERT_RESULT_MAX];
157   ubyte output_semantic_name[VERT_RESULT_MAX];
158   ubyte output_semantic_index[VERT_RESULT_MAX];
159   GLuint num_outputs;
160
161   /** List of translated variants of this vertex program.
162    */
163   struct st_vp_variant *variants;
164};
165
166
167
168/** Geometry program variant key */
169struct st_gp_variant_key
170{
171   struct st_context *st;          /**< variants are per-context */
172   /* no other fields yet */
173};
174
175
176/**
177 * Geometry program variant.
178 */
179struct st_gp_variant
180{
181   /* Parameters which generated this translated version of a vertex */
182   struct st_gp_variant_key key;
183
184   void *driver_shader;
185
186   struct st_gp_variant *next;
187};
188
189
190/**
191 * Derived from Mesa gl_geometry_program:
192 */
193struct st_geometry_program
194{
195   struct gl_geometry_program Base;  /**< The Mesa geometry program */
196   struct glsl_to_tgsi_visitor* glsl_to_tgsi;
197
198   /** map GP input back to VP output */
199   GLuint input_map[PIPE_MAX_SHADER_INPUTS];
200
201   /** maps a Mesa GEOM_ATTRIB_x to a packed TGSI input index */
202   GLuint input_to_index[GEOM_ATTRIB_MAX];
203   /** maps a TGSI input index back to a Mesa GEOM_ATTRIB_x */
204   GLuint index_to_input[PIPE_MAX_SHADER_INPUTS];
205
206   GLuint num_inputs;
207
208   GLuint input_to_slot[GEOM_ATTRIB_MAX];  /**< Maps GEOM_ATTRIB_x to slot */
209   GLuint num_input_slots;
210
211   ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
212   ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
213
214   struct pipe_shader_state tgsi;
215
216   struct st_gp_variant *variants;
217};
218
219
220
221static INLINE struct st_fragment_program *
222st_fragment_program( struct gl_fragment_program *fp )
223{
224   return (struct st_fragment_program *)fp;
225}
226
227
228static INLINE struct st_vertex_program *
229st_vertex_program( struct gl_vertex_program *vp )
230{
231   return (struct st_vertex_program *)vp;
232}
233
234static INLINE struct st_geometry_program *
235st_geometry_program( struct gl_geometry_program *gp )
236{
237   return (struct st_geometry_program *)gp;
238}
239
240static INLINE void
241st_reference_vertprog(struct st_context *st,
242                      struct st_vertex_program **ptr,
243                      struct st_vertex_program *prog)
244{
245   _mesa_reference_program(st->ctx,
246                           (struct gl_program **) ptr,
247                           (struct gl_program *) prog);
248}
249
250static INLINE void
251st_reference_geomprog(struct st_context *st,
252                      struct st_geometry_program **ptr,
253                      struct st_geometry_program *prog)
254{
255   _mesa_reference_program(st->ctx,
256                           (struct gl_program **) ptr,
257                           (struct gl_program *) prog);
258}
259
260static INLINE void
261st_reference_fragprog(struct st_context *st,
262                      struct st_fragment_program **ptr,
263                      struct st_fragment_program *prog)
264{
265   _mesa_reference_program(st->ctx,
266                           (struct gl_program **) ptr,
267                           (struct gl_program *) prog);
268}
269
270
271extern struct st_vp_variant *
272st_get_vp_variant(struct st_context *st,
273                  struct st_vertex_program *stvp,
274                  const struct st_vp_variant_key *key);
275
276
277extern struct st_fp_variant *
278st_get_fp_variant(struct st_context *st,
279                  struct st_fragment_program *stfp,
280                  const struct st_fp_variant_key *key);
281
282
283extern struct st_gp_variant *
284st_get_gp_variant(struct st_context *st,
285                  struct st_geometry_program *stgp,
286                  const struct st_gp_variant_key *key);
287
288
289extern void
290st_prepare_vertex_program(struct gl_context *ctx,
291                          struct st_vertex_program *stvp);
292
293extern GLboolean
294st_prepare_fragment_program(struct gl_context *ctx,
295                            struct st_fragment_program *stfp);
296
297
298extern void
299st_release_vp_variants( struct st_context *st,
300                        struct st_vertex_program *stvp );
301
302extern void
303st_release_fp_variants( struct st_context *st,
304                        struct st_fragment_program *stfp );
305
306extern void
307st_release_gp_variants(struct st_context *st,
308                       struct st_geometry_program *stgp);
309
310
311extern void
312st_print_shaders(struct gl_context *ctx);
313
314extern void
315st_destroy_program_variants(struct st_context *st);
316
317
318extern void
319st_print_current_vertex_program(void);
320
321
322#endif
323