Merge remote branch 'origin/master' into glsl2
[mesa.git] / src / mesa / state_tracker / st_atom_shader.c
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 * State validation for vertex/fragment shaders.
30 * Note that we have to delay most vertex/fragment shader translation
31 * until rendering time since the linkage between the vertex outputs and
32 * fragment inputs can vary depending on the pairing of shaders.
33 *
34 * Authors:
35 * Brian Paul
36 */
37
38 #include "main/imports.h"
39 #include "main/mtypes.h"
40 #include "program/program.h"
41
42 #include "pipe/p_context.h"
43 #include "pipe/p_shader_tokens.h"
44
45 #include "util/u_simple_shaders.h"
46
47 #include "cso_cache/cso_context.h"
48
49 #include "st_context.h"
50 #include "st_atom.h"
51 #include "st_program.h"
52
53
54
55 /**
56 * Translate fragment program if needed.
57 */
58 static void
59 translate_fp(struct st_context *st,
60 struct st_fragment_program *stfp)
61 {
62 if (!stfp->tgsi.tokens) {
63 assert(stfp->Base.Base.NumInstructions > 0);
64
65 st_translate_fragment_program(st, stfp);
66 }
67 }
68
69 /*
70 * Translate geometry program if needed.
71 */
72 static void
73 translate_gp(struct st_context *st,
74 struct st_geometry_program *stgp)
75 {
76 if (!stgp->tgsi.tokens) {
77 assert(stgp->Base.Base.NumInstructions > 1);
78
79 st_translate_geometry_program(st, stgp);
80 }
81 }
82
83 /**
84 * Find a translated vertex program that corresponds to stvp and
85 * has outputs matched to stfp's inputs.
86 * This performs vertex and fragment translation (to TGSI) when needed.
87 */
88 static struct st_vp_varient *
89 find_translated_vp(struct st_context *st,
90 struct st_vertex_program *stvp )
91 {
92 struct st_vp_varient *vpv;
93 struct st_vp_varient_key key;
94
95 /* Nothing in our key yet. This will change:
96 */
97 memset(&key, 0, sizeof key);
98
99 /* When this is true, we will add an extra input to the vertex
100 * shader translation (for edgeflags), an extra output with
101 * edgeflag semantics, and extend the vertex shader to pass through
102 * the input to the output. We'll need to use similar logic to set
103 * up the extra vertex_element input for edgeflags.
104 * _NEW_POLYGON, ST_NEW_EDGEFLAGS_DATA
105 */
106 key.passthrough_edgeflags = (st->vertdata_edgeflags && (
107 st->ctx->Polygon.FrontMode != GL_FILL ||
108 st->ctx->Polygon.BackMode != GL_FILL));
109
110
111 /* Do we need to throw away old translations after a change in the
112 * GL program string?
113 */
114 if (stvp->serialNo != stvp->lastSerialNo) {
115 /* These may have changed if the program string changed.
116 */
117 st_prepare_vertex_program( st, stvp );
118
119 /* We are now up-to-date:
120 */
121 stvp->lastSerialNo = stvp->serialNo;
122 }
123
124 /* See if we've got a translated vertex program whose outputs match
125 * the fragment program's inputs.
126 */
127 for (vpv = stvp->varients; vpv; vpv = vpv->next) {
128 if (memcmp(&vpv->key, &key, sizeof key) == 0) {
129 break;
130 }
131 }
132
133 /* No? Perform new translation here. */
134 if (!vpv) {
135 vpv = st_translate_vertex_program(st, stvp, &key);
136 if (!vpv)
137 return NULL;
138
139 vpv->next = stvp->varients;
140 stvp->varients = vpv;
141 }
142
143 return vpv;
144 }
145
146
147 /**
148 * Return pointer to a pass-through fragment shader.
149 * This shader is used when a texture is missing/incomplete.
150 */
151 static void *
152 get_passthrough_fs(struct st_context *st)
153 {
154 if (!st->passthrough_fs) {
155 st->passthrough_fs =
156 util_make_fragment_passthrough_shader(st->pipe);
157 }
158
159 return st->passthrough_fs;
160 }
161
162
163 /**
164 * Update fragment program state/atom. This involves translating the
165 * Mesa fragment program into a gallium fragment program and binding it.
166 */
167 static void
168 update_fp( struct st_context *st )
169 {
170 struct st_fragment_program *stfp;
171
172 assert(st->ctx->FragmentProgram._Current);
173 stfp = st_fragment_program(st->ctx->FragmentProgram._Current);
174 assert(stfp->Base.Base.Target == GL_FRAGMENT_PROGRAM_ARB);
175
176 translate_fp(st, stfp);
177
178 st_reference_fragprog(st, &st->fp, stfp);
179
180 if (st->missing_textures) {
181 /* use a pass-through frag shader that uses no textures */
182 void *fs = get_passthrough_fs(st);
183 cso_set_fragment_shader_handle(st->cso_context, fs);
184 }
185 else {
186 cso_set_fragment_shader_handle(st->cso_context, stfp->driver_shader);
187 }
188 }
189
190
191 const struct st_tracked_state st_update_fp = {
192 "st_update_fp", /* name */
193 { /* dirty */
194 0, /* mesa */
195 ST_NEW_FRAGMENT_PROGRAM /* st */
196 },
197 update_fp /* update */
198 };
199
200
201
202 /**
203 * Update vertex program state/atom. This involves translating the
204 * Mesa vertex program into a gallium fragment program and binding it.
205 */
206 static void
207 update_vp( struct st_context *st )
208 {
209 struct st_vertex_program *stvp;
210
211 /* find active shader and params -- Should be covered by
212 * ST_NEW_VERTEX_PROGRAM
213 */
214 assert(st->ctx->VertexProgram._Current);
215 stvp = st_vertex_program(st->ctx->VertexProgram._Current);
216 assert(stvp->Base.Base.Target == GL_VERTEX_PROGRAM_ARB);
217
218 st->vp_varient = find_translated_vp(st, stvp);
219
220 st_reference_vertprog(st, &st->vp, stvp);
221
222 cso_set_vertex_shader_handle(st->cso_context,
223 st->vp_varient->driver_shader);
224
225 st->vertex_result_to_slot = stvp->result_to_output;
226 }
227
228
229 const struct st_tracked_state st_update_vp = {
230 "st_update_vp", /* name */
231 { /* dirty */
232 _NEW_POLYGON, /* mesa */
233 ST_NEW_VERTEX_PROGRAM | ST_NEW_EDGEFLAGS_DATA /* st */
234 },
235 update_vp /* update */
236 };
237
238 static void
239 update_gp( struct st_context *st )
240 {
241
242 struct st_geometry_program *stgp;
243
244 if (!st->ctx->GeometryProgram._Current) {
245 cso_set_geometry_shader_handle(st->cso_context, NULL);
246 return;
247 }
248
249 stgp = st_geometry_program(st->ctx->GeometryProgram._Current);
250 assert(stgp->Base.Base.Target == MESA_GEOMETRY_PROGRAM);
251
252 translate_gp(st, stgp);
253
254 st_reference_geomprog(st, &st->gp, stgp);
255
256 cso_set_geometry_shader_handle(st->cso_context, stgp->driver_shader);
257 }
258
259 const struct st_tracked_state st_update_gp = {
260 "st_update_gp", /* name */
261 { /* dirty */
262 0, /* mesa */
263 ST_NEW_GEOMETRY_PROGRAM /* st */
264 },
265 update_gp /* update */
266 };