st/mesa: add compute shader states
[mesa.git] / src / mesa / state_tracker / st_atom_shader.c
1 /**************************************************************************
2 *
3 * Copyright 2003 VMware, Inc.
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 VMWARE 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 #include "util/u_simple_shaders.h"
45 #include "cso_cache/cso_context.h"
46
47 #include "st_context.h"
48 #include "st_atom.h"
49 #include "st_program.h"
50
51
52 /**
53 * Update fragment program state/atom. This involves translating the
54 * Mesa fragment program into a gallium fragment program and binding it.
55 */
56 static void
57 update_fp( struct st_context *st )
58 {
59 struct st_fragment_program *stfp;
60 struct st_fp_variant_key key;
61
62 assert(st->ctx->FragmentProgram._Current);
63 stfp = st_fragment_program(st->ctx->FragmentProgram._Current);
64 assert(stfp->Base.Base.Target == GL_FRAGMENT_PROGRAM_ARB);
65
66 memset(&key, 0, sizeof(key));
67 key.st = st->has_shareable_shaders ? NULL : st;
68
69 /* _NEW_FRAG_CLAMP */
70 key.clamp_color = st->clamp_frag_color_in_shader &&
71 st->ctx->Color._ClampFragmentColor;
72
73 /* Don't set it if the driver can force the interpolation by itself.
74 * If SAMPLE_ID or SAMPLE_POS are used, the interpolation is set
75 * automatically.
76 * Ignore sample qualifier while computing this flag.
77 */
78 key.persample_shading =
79 st->force_persample_in_shader &&
80 !(stfp->Base.Base.SystemValuesRead & (SYSTEM_BIT_SAMPLE_ID |
81 SYSTEM_BIT_SAMPLE_POS)) &&
82 _mesa_get_min_invocations_per_fragment(st->ctx, &stfp->Base, true) > 1;
83
84 st->fp_variant = st_get_fp_variant(st, stfp, &key);
85
86 st_reference_fragprog(st, &st->fp, stfp);
87
88 cso_set_fragment_shader_handle(st->cso_context,
89 st->fp_variant->driver_shader);
90 }
91
92
93 const struct st_tracked_state st_update_fp = {
94 "st_update_fp", /* name */
95 { /* dirty */
96 _NEW_BUFFERS | _NEW_MULTISAMPLE, /* mesa */
97 ST_NEW_FRAGMENT_PROGRAM /* st */
98 },
99 update_fp /* update */
100 };
101
102
103
104 /**
105 * Update vertex program state/atom. This involves translating the
106 * Mesa vertex program into a gallium fragment program and binding it.
107 */
108 static void
109 update_vp( struct st_context *st )
110 {
111 struct st_vertex_program *stvp;
112 struct st_vp_variant_key key;
113
114 /* find active shader and params -- Should be covered by
115 * ST_NEW_VERTEX_PROGRAM
116 */
117 assert(st->ctx->VertexProgram._Current);
118 stvp = st_vertex_program(st->ctx->VertexProgram._Current);
119 assert(stvp->Base.Base.Target == GL_VERTEX_PROGRAM_ARB);
120
121 memset(&key, 0, sizeof key);
122 key.st = st->has_shareable_shaders ? NULL : st;
123
124 /* When this is true, we will add an extra input to the vertex
125 * shader translation (for edgeflags), an extra output with
126 * edgeflag semantics, and extend the vertex shader to pass through
127 * the input to the output. We'll need to use similar logic to set
128 * up the extra vertex_element input for edgeflags.
129 */
130 key.passthrough_edgeflags = st->vertdata_edgeflags;
131
132 key.clamp_color = st->clamp_vert_color_in_shader &&
133 st->ctx->Light._ClampVertexColor &&
134 (stvp->Base.Base.OutputsWritten &
135 (VARYING_SLOT_COL0 |
136 VARYING_SLOT_COL1 |
137 VARYING_SLOT_BFC0 |
138 VARYING_SLOT_BFC1));
139
140 st->vp_variant = st_get_vp_variant(st, stvp, &key);
141
142 st_reference_vertprog(st, &st->vp, stvp);
143
144 cso_set_vertex_shader_handle(st->cso_context,
145 st->vp_variant->driver_shader);
146
147 st->vertex_result_to_slot = stvp->result_to_output;
148 }
149
150
151 const struct st_tracked_state st_update_vp = {
152 "st_update_vp", /* name */
153 { /* dirty */
154 0, /* mesa */
155 ST_NEW_VERTEX_PROGRAM /* st */
156 },
157 update_vp /* update */
158 };
159
160
161
162 static void
163 update_gp( struct st_context *st )
164 {
165 struct st_geometry_program *stgp;
166
167 if (!st->ctx->GeometryProgram._Current) {
168 cso_set_geometry_shader_handle(st->cso_context, NULL);
169 return;
170 }
171
172 stgp = st_geometry_program(st->ctx->GeometryProgram._Current);
173 assert(stgp->Base.Base.Target == GL_GEOMETRY_PROGRAM_NV);
174
175 st->gp_variant = st_get_basic_variant(st, PIPE_SHADER_GEOMETRY,
176 &stgp->tgsi, &stgp->variants);
177
178 st_reference_geomprog(st, &st->gp, stgp);
179
180 cso_set_geometry_shader_handle(st->cso_context,
181 st->gp_variant->driver_shader);
182 }
183
184 const struct st_tracked_state st_update_gp = {
185 "st_update_gp", /* name */
186 { /* dirty */
187 0, /* mesa */
188 ST_NEW_GEOMETRY_PROGRAM /* st */
189 },
190 update_gp /* update */
191 };
192
193
194
195 static void
196 update_tcp( struct st_context *st )
197 {
198 struct st_tessctrl_program *sttcp;
199
200 if (!st->ctx->TessCtrlProgram._Current) {
201 cso_set_tessctrl_shader_handle(st->cso_context, NULL);
202 return;
203 }
204
205 sttcp = st_tessctrl_program(st->ctx->TessCtrlProgram._Current);
206 assert(sttcp->Base.Base.Target == GL_TESS_CONTROL_PROGRAM_NV);
207
208 st->tcp_variant = st_get_basic_variant(st, PIPE_SHADER_TESS_CTRL,
209 &sttcp->tgsi, &sttcp->variants);
210
211 st_reference_tesscprog(st, &st->tcp, sttcp);
212
213 cso_set_tessctrl_shader_handle(st->cso_context,
214 st->tcp_variant->driver_shader);
215 }
216
217 const struct st_tracked_state st_update_tcp = {
218 "st_update_tcp", /* name */
219 { /* dirty */
220 0, /* mesa */
221 ST_NEW_TESSCTRL_PROGRAM /* st */
222 },
223 update_tcp /* update */
224 };
225
226
227
228 static void
229 update_tep( struct st_context *st )
230 {
231 struct st_tesseval_program *sttep;
232
233 if (!st->ctx->TessEvalProgram._Current) {
234 cso_set_tesseval_shader_handle(st->cso_context, NULL);
235 return;
236 }
237
238 sttep = st_tesseval_program(st->ctx->TessEvalProgram._Current);
239 assert(sttep->Base.Base.Target == GL_TESS_EVALUATION_PROGRAM_NV);
240
241 st->tep_variant = st_get_basic_variant(st, PIPE_SHADER_TESS_EVAL,
242 &sttep->tgsi, &sttep->variants);
243
244 st_reference_tesseprog(st, &st->tep, sttep);
245
246 cso_set_tesseval_shader_handle(st->cso_context,
247 st->tep_variant->driver_shader);
248 }
249
250 const struct st_tracked_state st_update_tep = {
251 "st_update_tep", /* name */
252 { /* dirty */
253 0, /* mesa */
254 ST_NEW_TESSEVAL_PROGRAM /* st */
255 },
256 update_tep /* update */
257 };
258
259
260
261 static void
262 update_cp( struct st_context *st )
263 {
264 struct st_compute_program *stcp;
265
266 if (!st->ctx->ComputeProgram._Current) {
267 cso_set_compute_shader_handle(st->cso_context, NULL);
268 return;
269 }
270
271 stcp = st_compute_program(st->ctx->ComputeProgram._Current);
272 assert(stcp->Base.Base.Target == GL_COMPUTE_PROGRAM_NV);
273
274 st->cp_variant = st_get_cp_variant(st, &stcp->tgsi, &stcp->variants);
275
276 st_reference_compprog(st, &st->cp, stcp);
277
278 cso_set_compute_shader_handle(st->cso_context,
279 st->cp_variant->driver_shader);
280 }
281
282 const struct st_tracked_state st_update_cp = {
283 "st_update_cp", /* name */
284 { /* dirty */
285 0, /* mesa */
286 ST_NEW_COMPUTE_PROGRAM /* st */
287 },
288 update_cp /* update */
289 };