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