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