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