st/mesa: remove duplicate calculation of fp input mapping
[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 "main/macros.h"
41 #include "shader/program.h"
42
43 #include "pipe/p_context.h"
44 #include "pipe/p_shader_tokens.h"
45
46 #include "util/u_simple_shaders.h"
47
48 #include "cso_cache/cso_context.h"
49
50 #include "st_context.h"
51 #include "st_atom.h"
52 #include "st_program.h"
53
54
55
56 /**
57 * Translate fragment program if needed.
58 */
59 static void
60 translate_fp(struct st_context *st,
61 struct st_fragment_program *stfp)
62 {
63 if (!stfp->state.tokens) {
64 assert(stfp->Base.Base.NumInstructions > 0);
65
66 st_translate_fragment_program(st, stfp);
67 }
68 }
69
70
71
72 /**
73 * Find a translated vertex program that corresponds to stvp and
74 * has outputs matched to stfp's inputs.
75 * This performs vertex and fragment translation (to TGSI) when needed.
76 */
77 static struct st_vp_varient *
78 find_translated_vp(struct st_context *st,
79 struct st_vertex_program *stvp )
80 {
81 struct st_vp_varient *vpv;
82 struct st_vp_varient_key key;
83
84 /* Nothing in our key yet. This will change:
85 */
86 memset(&key, 0, sizeof key);
87
88 /* When this is true, we will add an extra input to the vertex
89 * shader translation (for edgeflags), an extra output with
90 * edgeflag semantics, and extend the vertex shader to pass through
91 * the input to the output. We'll need to use similar logic to set
92 * up the extra vertex_element input for edgeflags.
93 * _NEW_POLYGON, ST_NEW_EDGEFLAGS_DATA
94 */
95 key.passthrough_edgeflags = (st->vertdata_edgeflags && (
96 st->ctx->Polygon.FrontMode != GL_FILL ||
97 st->ctx->Polygon.BackMode != GL_FILL));
98
99
100 /* Do we need to throw away old translations after a change in the
101 * GL program string?
102 */
103 if (stvp->serialNo != stvp->lastSerialNo) {
104 /* These may have changed if the program string changed.
105 */
106 st_prepare_vertex_program( st, stvp );
107
108 /* We are now up-to-date:
109 */
110 stvp->lastSerialNo = stvp->serialNo;
111 }
112
113 /* See if we've got a translated vertex program whose outputs match
114 * the fragment program's inputs.
115 */
116 for (vpv = stvp->varients; vpv; vpv = vpv->next) {
117 if (memcmp(&vpv->key, &key, sizeof key) == 0) {
118 break;
119 }
120 }
121
122 /* No? Perform new translation here. */
123 if (!vpv) {
124 vpv = st_translate_vertex_program(st, stvp, &key);
125 if (!vpv)
126 return NULL;
127
128 vpv->next = stvp->varients;
129 stvp->varients = vpv;
130 }
131
132 return vpv;
133 }
134
135
136 /**
137 * Return pointer to a pass-through fragment shader.
138 * This shader is used when a texture is missing/incomplete.
139 */
140 static void *
141 get_passthrough_fs(struct st_context *st)
142 {
143 if (!st->passthrough_fs) {
144 st->passthrough_fs =
145 util_make_fragment_passthrough_shader(st->pipe);
146 }
147
148 return st->passthrough_fs;
149 }
150
151
152 /**
153 * Update fragment program state/atom. This involves translating the
154 * Mesa fragment program into a gallium fragment program and binding it.
155 */
156 static void
157 update_fp( struct st_context *st )
158 {
159 struct st_fragment_program *stfp;
160
161 assert(st->ctx->FragmentProgram._Current);
162 stfp = st_fragment_program(st->ctx->FragmentProgram._Current);
163 assert(stfp->Base.Base.Target == GL_FRAGMENT_PROGRAM_ARB);
164
165 translate_fp(st, stfp);
166
167 st_reference_fragprog(st, &st->fp, stfp);
168
169 if (st->missing_textures) {
170 /* use a pass-through frag shader that uses no textures */
171 void *fs = get_passthrough_fs(st);
172 cso_set_fragment_shader_handle(st->cso_context, fs);
173 }
174 else {
175 cso_set_fragment_shader_handle(st->cso_context, stfp->driver_shader);
176 }
177 }
178
179
180 const struct st_tracked_state st_update_fp = {
181 "st_update_fp", /* name */
182 { /* dirty */
183 0, /* mesa */
184 ST_NEW_FRAGMENT_PROGRAM /* st */
185 },
186 update_fp /* update */
187 };
188
189
190
191 /**
192 * Update vertex program state/atom. This involves translating the
193 * Mesa vertex program into a gallium fragment program and binding it.
194 */
195 static void
196 update_vp( struct st_context *st )
197 {
198 struct st_vertex_program *stvp;
199
200 /* find active shader and params -- Should be covered by
201 * ST_NEW_VERTEX_PROGRAM
202 */
203 assert(st->ctx->VertexProgram._Current);
204 stvp = st_vertex_program(st->ctx->VertexProgram._Current);
205 assert(stvp->Base.Base.Target == GL_VERTEX_PROGRAM_ARB);
206
207 st->vp_varient = find_translated_vp(st, stvp);
208
209 st_reference_vertprog(st, &st->vp, stvp);
210
211 cso_set_vertex_shader_handle(st->cso_context,
212 st->vp_varient->driver_shader);
213
214 st->vertex_result_to_slot = stvp->result_to_output;
215 }
216
217
218 const struct st_tracked_state st_update_vp = {
219 "st_update_vp", /* name */
220 { /* dirty */
221 _NEW_POLYGON, /* mesa */
222 ST_NEW_VERTEX_PROGRAM | ST_NEW_EDGEFLAGS_DATA /* st */
223 },
224 update_vp /* update */
225 };