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