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