mesa/gallium: automatically lower alpha-testing
[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 #include "st_texture.h"
55
56
57 static unsigned
58 get_texture_target(struct gl_context *ctx, const unsigned unit)
59 {
60 struct gl_texture_object *texObj = _mesa_get_tex_unit(ctx, unit)->_Current;
61 gl_texture_index index;
62
63 if (texObj) {
64 index = _mesa_tex_target_to_index(ctx, texObj->Target);
65 } else {
66 /* fallback for missing texture */
67 index = TEXTURE_2D_INDEX;
68 }
69
70 /* Map mesa texture target to TGSI texture target.
71 * Copied from st_mesa_to_tgsi.c, the shadow part is omitted */
72 switch(index) {
73 case TEXTURE_2D_MULTISAMPLE_INDEX: return TGSI_TEXTURE_2D_MSAA;
74 case TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX: return TGSI_TEXTURE_2D_ARRAY_MSAA;
75 case TEXTURE_BUFFER_INDEX: return TGSI_TEXTURE_BUFFER;
76 case TEXTURE_1D_INDEX: return TGSI_TEXTURE_1D;
77 case TEXTURE_2D_INDEX: return TGSI_TEXTURE_2D;
78 case TEXTURE_3D_INDEX: return TGSI_TEXTURE_3D;
79 case TEXTURE_CUBE_INDEX: return TGSI_TEXTURE_CUBE;
80 case TEXTURE_CUBE_ARRAY_INDEX: return TGSI_TEXTURE_CUBE_ARRAY;
81 case TEXTURE_RECT_INDEX: return TGSI_TEXTURE_RECT;
82 case TEXTURE_1D_ARRAY_INDEX: return TGSI_TEXTURE_1D_ARRAY;
83 case TEXTURE_2D_ARRAY_INDEX: return TGSI_TEXTURE_2D_ARRAY;
84 case TEXTURE_EXTERNAL_INDEX: return TGSI_TEXTURE_2D;
85 default:
86 debug_assert(0);
87 return TGSI_TEXTURE_1D;
88 }
89 }
90
91
92 /**
93 * Update fragment program state/atom. This involves translating the
94 * Mesa fragment program into a gallium fragment program and binding it.
95 */
96 void
97 st_update_fp( struct st_context *st )
98 {
99 struct st_fragment_program *stfp;
100
101 assert(st->ctx->FragmentProgram._Current);
102 stfp = st_fragment_program(st->ctx->FragmentProgram._Current);
103 assert(stfp->Base.Target == GL_FRAGMENT_PROGRAM_ARB);
104
105 void *shader;
106
107 if (st->shader_has_one_variant[MESA_SHADER_FRAGMENT] &&
108 !stfp->ati_fs && /* ATI_fragment_shader always has multiple variants */
109 !stfp->Base.ExternalSamplersUsed && /* external samplers need variants */
110 stfp->variants &&
111 !stfp->variants->key.drawpixels &&
112 !stfp->variants->key.bitmap) {
113 shader = stfp->variants->driver_shader;
114 } else {
115 struct st_fp_variant_key key;
116
117 /* use memset, not an initializer to be sure all memory is zeroed */
118 memset(&key, 0, sizeof(key));
119
120 key.st = st->has_shareable_shaders ? NULL : st;
121
122 key.lower_flatshade = st->lower_flatshade &&
123 st->ctx->Light.ShadeModel == GL_FLAT;
124
125 /* _NEW_COLOR */
126 key.lower_alpha_func = COMPARE_FUNC_NEVER;
127 if (st->lower_alpha_test && _mesa_is_alpha_test_enabled(st->ctx))
128 key.lower_alpha_func = st->ctx->Color.AlphaFunc;
129
130 /* _NEW_FRAG_CLAMP */
131 key.clamp_color = st->clamp_frag_color_in_shader &&
132 st->ctx->Color._ClampFragmentColor;
133
134 /* _NEW_MULTISAMPLE | _NEW_BUFFERS */
135 key.persample_shading =
136 st->force_persample_in_shader &&
137 _mesa_is_multisample_enabled(st->ctx) &&
138 st->ctx->Multisample.SampleShading &&
139 st->ctx->Multisample.MinSampleShadingValue *
140 _mesa_geometric_samples(st->ctx->DrawBuffer) > 1;
141
142 key.lower_depth_clamp =
143 st->clamp_frag_depth_in_shader &&
144 (st->ctx->Transform.DepthClampNear ||
145 st->ctx->Transform.DepthClampFar);
146
147 if (stfp->ati_fs) {
148 key.fog = st->ctx->Fog._PackedEnabledMode;
149
150 for (unsigned u = 0; u < MAX_NUM_FRAGMENT_REGISTERS_ATI; u++) {
151 key.texture_targets[u] = get_texture_target(st->ctx, u);
152 }
153 }
154
155 key.external = st_get_external_sampler_key(st, &stfp->Base);
156
157 shader = st_get_fp_variant(st, stfp, &key)->driver_shader;
158 }
159
160 st_reference_fragprog(st, &st->fp, stfp);
161
162 cso_set_fragment_shader_handle(st->cso_context, shader);
163 }
164
165
166 /**
167 * Update vertex program state/atom. This involves translating the
168 * Mesa vertex program into a gallium fragment program and binding it.
169 */
170 void
171 st_update_vp( struct st_context *st )
172 {
173 struct st_vertex_program *stvp;
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.Target == GL_VERTEX_PROGRAM_ARB);
181
182 if (st->shader_has_one_variant[MESA_SHADER_VERTEX] &&
183 stvp->variants &&
184 stvp->variants->key.passthrough_edgeflags == st->vertdata_edgeflags) {
185 st->vp_variant = stvp->variants;
186 } else {
187 struct st_vp_variant_key key;
188
189 memset(&key, 0, sizeof(key));
190
191 key.st = st->has_shareable_shaders ? NULL : st;
192
193 /* When this is true, we will add an extra input to the vertex
194 * shader translation (for edgeflags), an extra output with
195 * edgeflag semantics, and extend the vertex shader to pass through
196 * the input to the output. We'll need to use similar logic to set
197 * up the extra vertex_element input for edgeflags.
198 */
199 key.passthrough_edgeflags = st->vertdata_edgeflags;
200
201 key.clamp_color = st->clamp_vert_color_in_shader &&
202 st->ctx->Light._ClampVertexColor &&
203 (stvp->Base.info.outputs_written &
204 (VARYING_SLOT_COL0 |
205 VARYING_SLOT_COL1 |
206 VARYING_SLOT_BFC0 |
207 VARYING_SLOT_BFC1));
208
209 key.lower_depth_clamp =
210 !st->gp && !st->tep &&
211 st->clamp_frag_depth_in_shader &&
212 (st->ctx->Transform.DepthClampNear ||
213 st->ctx->Transform.DepthClampFar);
214
215 if (key.lower_depth_clamp)
216 key.clip_negative_one_to_one =
217 st->ctx->Transform.ClipDepthMode == GL_NEGATIVE_ONE_TO_ONE;
218
219 st->vp_variant = st_get_vp_variant(st, stvp, &key);
220 }
221
222 st_reference_vertprog(st, &st->vp, stvp);
223
224 cso_set_vertex_shader_handle(st->cso_context,
225 st->vp_variant->driver_shader);
226 }
227
228
229 static void *
230 st_update_common_program(struct st_context *st, struct gl_program *prog,
231 unsigned pipe_shader, struct st_common_program **dst)
232 {
233 struct st_common_program *stp;
234
235 if (!prog) {
236 st_reference_prog(st, dst, NULL);
237 return NULL;
238 }
239
240 stp = st_common_program(prog);
241 st_reference_prog(st, dst, stp);
242
243 if (st->shader_has_one_variant[prog->info.stage] && stp->variants)
244 return stp->variants->driver_shader;
245
246 struct st_basic_variant_key key;
247
248 /* use memset, not an initializer to be sure all memory is zeroed */
249 memset(&key, 0, sizeof(key));
250
251 key.st = st->has_shareable_shaders ? NULL : st;
252
253 if (pipe_shader == PIPE_SHADER_GEOMETRY ||
254 pipe_shader == PIPE_SHADER_TESS_EVAL) {
255 key.clamp_color = st->clamp_vert_color_in_shader &&
256 st->ctx->Light._ClampVertexColor &&
257 (stp->Base.info.outputs_written &
258 (VARYING_SLOT_COL0 |
259 VARYING_SLOT_COL1 |
260 VARYING_SLOT_BFC0 |
261 VARYING_SLOT_BFC1));
262
263 key.lower_depth_clamp =
264 (pipe_shader == PIPE_SHADER_GEOMETRY || !st->gp) &&
265 st->clamp_frag_depth_in_shader &&
266 (st->ctx->Transform.DepthClampNear ||
267 st->ctx->Transform.DepthClampFar);
268
269 if (key.lower_depth_clamp)
270 key.clip_negative_one_to_one =
271 st->ctx->Transform.ClipDepthMode == GL_NEGATIVE_ONE_TO_ONE;
272
273 }
274
275 return st_get_basic_variant(st, stp, &key)->driver_shader;
276 }
277
278
279 void
280 st_update_gp(struct st_context *st)
281 {
282 void *shader = st_update_common_program(st,
283 st->ctx->GeometryProgram._Current,
284 PIPE_SHADER_GEOMETRY, &st->gp);
285 cso_set_geometry_shader_handle(st->cso_context, shader);
286 }
287
288
289 void
290 st_update_tcp(struct st_context *st)
291 {
292 void *shader = st_update_common_program(st,
293 st->ctx->TessCtrlProgram._Current,
294 PIPE_SHADER_TESS_CTRL, &st->tcp);
295 cso_set_tessctrl_shader_handle(st->cso_context, shader);
296 }
297
298
299 void
300 st_update_tep(struct st_context *st)
301 {
302 void *shader = st_update_common_program(st,
303 st->ctx->TessEvalProgram._Current,
304 PIPE_SHADER_TESS_EVAL, &st->tep);
305 cso_set_tesseval_shader_handle(st->cso_context, shader);
306 }
307
308
309 void
310 st_update_cp(struct st_context *st)
311 {
312 void *shader = st_update_common_program(st,
313 st->ctx->ComputeProgram._Current,
314 PIPE_SHADER_COMPUTE, &st->cp);
315 cso_set_compute_shader_handle(st->cso_context, shader);
316 }