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 "main/texobj.h"
42 #include "main/texstate.h"
43 #include "program/program.h"
44
45 #include "pipe/p_context.h"
46 #include "pipe/p_shader_tokens.h"
47 #include "util/u_simple_shaders.h"
48 #include "cso_cache/cso_context.h"
49 #include "util/u_debug.h"
50
51 #include "st_context.h"
52 #include "st_atom.h"
53 #include "st_program.h"
54
55
56 /** Compress the fog function enums into a 2-bit value */
57 static GLuint
58 translate_fog_mode(GLenum mode)
59 {
60 switch (mode) {
61 case GL_LINEAR: return 1;
62 case GL_EXP: return 2;
63 case GL_EXP2: return 3;
64 default:
65 return 0;
66 }
67 }
68
69 static unsigned
70 get_texture_target(struct gl_context *ctx, const unsigned unit)
71 {
72 struct gl_texture_object *texObj = _mesa_get_tex_unit(ctx, unit)->_Current;
73 gl_texture_index index;
74
75 if (texObj) {
76 index = _mesa_tex_target_to_index(ctx, texObj->Target);
77 } else {
78 /* fallback for missing texture */
79 index = TEXTURE_2D_INDEX;
80 }
81
82 /* Map mesa texture target to TGSI texture target.
83 * Copied from st_mesa_to_tgsi.c, the shadow part is omitted */
84 switch(index) {
85 case TEXTURE_2D_MULTISAMPLE_INDEX: return TGSI_TEXTURE_2D_MSAA;
86 case TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX: return TGSI_TEXTURE_2D_ARRAY_MSAA;
87 case TEXTURE_BUFFER_INDEX: return TGSI_TEXTURE_BUFFER;
88 case TEXTURE_1D_INDEX: return TGSI_TEXTURE_1D;
89 case TEXTURE_2D_INDEX: return TGSI_TEXTURE_2D;
90 case TEXTURE_3D_INDEX: return TGSI_TEXTURE_3D;
91 case TEXTURE_CUBE_INDEX: return TGSI_TEXTURE_CUBE;
92 case TEXTURE_CUBE_ARRAY_INDEX: return TGSI_TEXTURE_CUBE_ARRAY;
93 case TEXTURE_RECT_INDEX: return TGSI_TEXTURE_RECT;
94 case TEXTURE_1D_ARRAY_INDEX: return TGSI_TEXTURE_1D_ARRAY;
95 case TEXTURE_2D_ARRAY_INDEX: return TGSI_TEXTURE_2D_ARRAY;
96 case TEXTURE_EXTERNAL_INDEX: return TGSI_TEXTURE_2D;
97 default:
98 debug_assert(0);
99 return TGSI_TEXTURE_1D;
100 }
101 }
102
103
104 /**
105 * Update fragment program state/atom. This involves translating the
106 * Mesa fragment program into a gallium fragment program and binding it.
107 */
108 static void
109 update_fp( struct st_context *st )
110 {
111 struct st_fragment_program *stfp;
112 struct st_fp_variant_key key;
113
114 assert(st->ctx->FragmentProgram._Current);
115 stfp = st_fragment_program(st->ctx->FragmentProgram._Current);
116 assert(stfp->Base.Base.Target == GL_FRAGMENT_PROGRAM_ARB);
117
118 memset(&key, 0, sizeof(key));
119 key.st = st->has_shareable_shaders ? NULL : st;
120
121 /* _NEW_FRAG_CLAMP */
122 key.clamp_color = st->clamp_frag_color_in_shader &&
123 st->ctx->Color._ClampFragmentColor;
124
125 /* _NEW_MULTISAMPLE | _NEW_BUFFERS */
126 key.persample_shading =
127 st->force_persample_in_shader &&
128 _mesa_is_multisample_enabled(st->ctx) &&
129 st->ctx->Multisample.SampleShading &&
130 st->ctx->Multisample.MinSampleShadingValue *
131 _mesa_geometric_samples(st->ctx->DrawBuffer) > 1;
132
133 if (stfp->ati_fs) {
134 unsigned u;
135
136 if (st->ctx->Fog.Enabled) {
137 key.fog = translate_fog_mode(st->ctx->Fog.Mode);
138 }
139
140 for (u = 0; u < MAX_NUM_FRAGMENT_REGISTERS_ATI; u++) {
141 key.texture_targets[u] = get_texture_target(st->ctx, u);
142 }
143 }
144
145 st->fp_variant = st_get_fp_variant(st, stfp, &key);
146
147 st_reference_fragprog(st, &st->fp, stfp);
148
149 cso_set_fragment_shader_handle(st->cso_context,
150 st->fp_variant->driver_shader);
151 }
152
153
154 const struct st_tracked_state st_update_fp = {
155 "st_update_fp", /* name */
156 { /* dirty */
157 _NEW_BUFFERS | _NEW_MULTISAMPLE | _NEW_FOG, /* mesa */
158 ST_NEW_FRAGMENT_PROGRAM /* st */
159 },
160 update_fp /* update */
161 };
162
163
164
165 /**
166 * Update vertex program state/atom. This involves translating the
167 * Mesa vertex program into a gallium fragment program and binding it.
168 */
169 static void
170 update_vp( struct st_context *st )
171 {
172 struct st_vertex_program *stvp;
173 struct st_vp_variant_key key;
174
175 /* find active shader and params -- Should be covered by
176 * ST_NEW_VERTEX_PROGRAM
177 */
178 assert(st->ctx->VertexProgram._Current);
179 stvp = st_vertex_program(st->ctx->VertexProgram._Current);
180 assert(stvp->Base.Base.Target == GL_VERTEX_PROGRAM_ARB);
181
182 memset(&key, 0, sizeof key);
183 key.st = st->has_shareable_shaders ? NULL : st;
184
185 /* When this is true, we will add an extra input to the vertex
186 * shader translation (for edgeflags), an extra output with
187 * edgeflag semantics, and extend the vertex shader to pass through
188 * the input to the output. We'll need to use similar logic to set
189 * up the extra vertex_element input for edgeflags.
190 */
191 key.passthrough_edgeflags = st->vertdata_edgeflags;
192
193 key.clamp_color = st->clamp_vert_color_in_shader &&
194 st->ctx->Light._ClampVertexColor &&
195 (stvp->Base.Base.OutputsWritten &
196 (VARYING_SLOT_COL0 |
197 VARYING_SLOT_COL1 |
198 VARYING_SLOT_BFC0 |
199 VARYING_SLOT_BFC1));
200
201 st->vp_variant = st_get_vp_variant(st, stvp, &key);
202
203 st_reference_vertprog(st, &st->vp, stvp);
204
205 cso_set_vertex_shader_handle(st->cso_context,
206 st->vp_variant->driver_shader);
207
208 st->vertex_result_to_slot = stvp->result_to_output;
209 }
210
211
212 const struct st_tracked_state st_update_vp = {
213 "st_update_vp", /* name */
214 { /* dirty */
215 0, /* mesa */
216 ST_NEW_VERTEX_PROGRAM /* st */
217 },
218 update_vp /* update */
219 };
220
221
222
223 static void
224 update_gp( struct st_context *st )
225 {
226 struct st_geometry_program *stgp;
227
228 if (!st->ctx->GeometryProgram._Current) {
229 cso_set_geometry_shader_handle(st->cso_context, NULL);
230 return;
231 }
232
233 stgp = st_geometry_program(st->ctx->GeometryProgram._Current);
234 assert(stgp->Base.Base.Target == GL_GEOMETRY_PROGRAM_NV);
235
236 st->gp_variant = st_get_basic_variant(st, PIPE_SHADER_GEOMETRY,
237 &stgp->tgsi, &stgp->variants);
238
239 st_reference_geomprog(st, &st->gp, stgp);
240
241 cso_set_geometry_shader_handle(st->cso_context,
242 st->gp_variant->driver_shader);
243 }
244
245 const struct st_tracked_state st_update_gp = {
246 "st_update_gp", /* name */
247 { /* dirty */
248 0, /* mesa */
249 ST_NEW_GEOMETRY_PROGRAM /* st */
250 },
251 update_gp /* update */
252 };
253
254
255
256 static void
257 update_tcp( struct st_context *st )
258 {
259 struct st_tessctrl_program *sttcp;
260
261 if (!st->ctx->TessCtrlProgram._Current) {
262 cso_set_tessctrl_shader_handle(st->cso_context, NULL);
263 return;
264 }
265
266 sttcp = st_tessctrl_program(st->ctx->TessCtrlProgram._Current);
267 assert(sttcp->Base.Base.Target == GL_TESS_CONTROL_PROGRAM_NV);
268
269 st->tcp_variant = st_get_basic_variant(st, PIPE_SHADER_TESS_CTRL,
270 &sttcp->tgsi, &sttcp->variants);
271
272 st_reference_tesscprog(st, &st->tcp, sttcp);
273
274 cso_set_tessctrl_shader_handle(st->cso_context,
275 st->tcp_variant->driver_shader);
276 }
277
278 const struct st_tracked_state st_update_tcp = {
279 "st_update_tcp", /* name */
280 { /* dirty */
281 0, /* mesa */
282 ST_NEW_TESSCTRL_PROGRAM /* st */
283 },
284 update_tcp /* update */
285 };
286
287
288
289 static void
290 update_tep( struct st_context *st )
291 {
292 struct st_tesseval_program *sttep;
293
294 if (!st->ctx->TessEvalProgram._Current) {
295 cso_set_tesseval_shader_handle(st->cso_context, NULL);
296 return;
297 }
298
299 sttep = st_tesseval_program(st->ctx->TessEvalProgram._Current);
300 assert(sttep->Base.Base.Target == GL_TESS_EVALUATION_PROGRAM_NV);
301
302 st->tep_variant = st_get_basic_variant(st, PIPE_SHADER_TESS_EVAL,
303 &sttep->tgsi, &sttep->variants);
304
305 st_reference_tesseprog(st, &st->tep, sttep);
306
307 cso_set_tesseval_shader_handle(st->cso_context,
308 st->tep_variant->driver_shader);
309 }
310
311 const struct st_tracked_state st_update_tep = {
312 "st_update_tep", /* name */
313 { /* dirty */
314 0, /* mesa */
315 ST_NEW_TESSEVAL_PROGRAM /* st */
316 },
317 update_tep /* update */
318 };
319
320
321
322 static void
323 update_cp( struct st_context *st )
324 {
325 struct st_compute_program *stcp;
326
327 if (!st->ctx->ComputeProgram._Current) {
328 cso_set_compute_shader_handle(st->cso_context, NULL);
329 return;
330 }
331
332 stcp = st_compute_program(st->ctx->ComputeProgram._Current);
333 assert(stcp->Base.Base.Target == GL_COMPUTE_PROGRAM_NV);
334
335 st->cp_variant = st_get_cp_variant(st, &stcp->tgsi, &stcp->variants);
336
337 st_reference_compprog(st, &st->cp, stcp);
338
339 cso_set_compute_shader_handle(st->cso_context,
340 st->cp_variant->driver_shader);
341 }
342
343 const struct st_tracked_state st_update_cp = {
344 "st_update_cp", /* name */
345 { /* dirty */
346 0, /* mesa */
347 ST_NEW_COMPUTE_PROGRAM /* st */
348 },
349 update_cp /* update */
350 };