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
39 #include "main/mtypes.h"
40 #include "main/framebuffer.h"
41 #include "main/state.h"
42 #include "main/texobj.h"
43 #include "main/teximage.h"
44 #include "main/texstate.h"
45 #include "program/program.h"
46
47 #include "pipe/p_context.h"
48 #include "pipe/p_shader_tokens.h"
49 #include "cso_cache/cso_context.h"
50 #include "util/u_debug.h"
51
52 #include "st_context.h"
53 #include "st_atom.h"
54 #include "st_program.h"
55 #include "st_texture.h"
56 #include "st_util.h"
57
58
59 static unsigned
get_texture_index(struct gl_context * ctx,const unsigned unit)60 get_texture_index(struct gl_context *ctx, const unsigned unit)
61 {
62 struct gl_texture_object *texObj = _mesa_get_tex_unit(ctx, unit)->_Current;
63 gl_texture_index index;
64
65 if (texObj) {
66 index = _mesa_tex_target_to_index(ctx, texObj->Target);
67 } else {
68 /* fallback for missing texture */
69 index = TEXTURE_2D_INDEX;
70 }
71
72 return index;
73 }
74
75 static void
update_gl_clamp(struct st_context * st,struct gl_program * prog,uint32_t * gl_clamp)76 update_gl_clamp(struct st_context *st, struct gl_program *prog, uint32_t *gl_clamp)
77 {
78 if (!st->emulate_gl_clamp)
79 return;
80
81 if (!st->ctx->Texture.NumSamplersWithClamp)
82 return;
83
84 gl_clamp[0] = gl_clamp[1] = gl_clamp[2] = 0;
85 GLbitfield samplers_used = prog->SamplersUsed;
86 unsigned unit;
87 /* same as st_atom_sampler.c */
88 for (unit = 0; samplers_used; unit++, samplers_used >>= 1) {
89 unsigned tex_unit = prog->SamplerUnits[unit];
90 if (samplers_used & 1 &&
91 (st->ctx->Texture.Unit[tex_unit]._Current->Target != GL_TEXTURE_BUFFER)) {
92 ASSERTED const struct gl_texture_object *texobj;
93 struct gl_context *ctx = st->ctx;
94 const struct gl_sampler_object *msamp;
95
96 texobj = ctx->Texture.Unit[tex_unit]._Current;
97 assert(texobj);
98
99 msamp = _mesa_get_samplerobj(ctx, tex_unit);
100 if (is_wrap_gl_clamp(msamp->Attrib.WrapS))
101 gl_clamp[0] |= BITFIELD64_BIT(unit);
102 if (is_wrap_gl_clamp(msamp->Attrib.WrapT))
103 gl_clamp[1] |= BITFIELD64_BIT(unit);
104 if (is_wrap_gl_clamp(msamp->Attrib.WrapR))
105 gl_clamp[2] |= BITFIELD64_BIT(unit);
106 }
107 }
108 }
109
110 /**
111 * Update fragment program state/atom. This involves translating the
112 * Mesa fragment program into a gallium fragment program and binding it.
113 */
114 void
st_update_fp(struct st_context * st)115 st_update_fp( struct st_context *st )
116 {
117 struct gl_program *fp;
118
119 assert(st->ctx->FragmentProgram._Current);
120 fp = st->ctx->FragmentProgram._Current;
121 assert(fp->Target == GL_FRAGMENT_PROGRAM_ARB);
122
123 void *shader;
124
125 if (st->shader_has_one_variant[MESA_SHADER_FRAGMENT] &&
126 !fp->ati_fs && /* ATI_fragment_shader always has multiple variants */
127 !fp->ExternalSamplersUsed && /* external samplers need variants */
128 !(!fp->shader_program && fp->ShadowSamplers)) {
129 shader = fp->variants->driver_shader;
130 } else {
131 struct st_fp_variant_key key;
132
133 /* use memset, not an initializer to be sure all memory is zeroed */
134 memset(&key, 0, sizeof(key));
135
136 key.st = st->has_shareable_shaders ? NULL : st;
137
138 key.lower_flatshade = st->lower_flatshade &&
139 st->ctx->Light.ShadeModel == GL_FLAT;
140
141 /* _NEW_COLOR */
142 key.lower_alpha_func = COMPARE_FUNC_ALWAYS;
143 if (st->lower_alpha_test && _mesa_is_alpha_test_enabled(st->ctx))
144 key.lower_alpha_func = st->ctx->Color.AlphaFunc;
145
146 /* _NEW_LIGHT_STATE | _NEW_PROGRAM */
147 key.lower_two_sided_color = st->lower_two_sided_color &&
148 _mesa_vertex_program_two_side_enabled(st->ctx);
149
150 /* gl_driver_flags::NewFragClamp */
151 key.clamp_color = st->clamp_frag_color_in_shader &&
152 st->ctx->Color._ClampFragmentColor;
153
154 /* _NEW_MULTISAMPLE | _NEW_BUFFERS */
155 key.persample_shading =
156 st->force_persample_in_shader &&
157 _mesa_is_multisample_enabled(st->ctx) &&
158 st->ctx->Multisample.SampleShading &&
159 st->ctx->Multisample.MinSampleShadingValue *
160 _mesa_geometric_samples(st->ctx->DrawBuffer) > 1;
161
162 if (fp->ati_fs) {
163 key.fog = st->ctx->Fog._PackedEnabledMode;
164
165 for (unsigned u = 0; u < MAX_NUM_FRAGMENT_REGISTERS_ATI; u++) {
166 key.texture_index[u] = get_texture_index(st->ctx, u);
167 }
168 }
169
170 if (!fp->shader_program && fp->ShadowSamplers) {
171 u_foreach_bit(i, fp->ShadowSamplers) {
172 struct gl_texture_object *tex_obj =
173 _mesa_get_tex_unit(st->ctx, fp->SamplerUnits[i])->_Current;
174 GLenum16 baseFormat = _mesa_base_tex_image(tex_obj)->_BaseFormat;
175
176 if (baseFormat == GL_DEPTH_COMPONENT ||
177 baseFormat == GL_DEPTH_STENCIL)
178 key.depth_textures |= BITFIELD_BIT(i);
179 }
180 }
181
182 key.external = st_get_external_sampler_key(st, fp);
183 update_gl_clamp(st, st->ctx->FragmentProgram._Current, key.gl_clamp);
184
185 simple_mtx_lock(&st->ctx->Shared->Mutex);
186 shader = st_get_fp_variant(st, fp, &key)->base.driver_shader;
187 simple_mtx_unlock(&st->ctx->Shared->Mutex);
188 }
189
190 _mesa_reference_program(st->ctx, &st->fp, fp);
191
192 cso_set_fragment_shader_handle(st->cso_context, shader);
193 }
194
195
196 /**
197 * Update vertex program state/atom. This involves translating the
198 * Mesa vertex program into a gallium fragment program and binding it.
199 */
200 void
st_update_vp(struct st_context * st)201 st_update_vp( struct st_context *st )
202 {
203 struct gl_program *vp;
204
205 /* find active shader and params -- Should be covered by
206 * ST_NEW_VERTEX_PROGRAM
207 */
208 assert(st->ctx->VertexProgram._Current);
209 vp = st->ctx->VertexProgram._Current;
210 assert(vp->Target == GL_VERTEX_PROGRAM_ARB);
211
212 if (st->shader_has_one_variant[MESA_SHADER_VERTEX] &&
213 !st->ctx->Array._PerVertexEdgeFlagsEnabled) {
214 st->vp_variant = st_common_variant(vp->variants);
215 } else {
216 struct st_common_variant_key key;
217
218 memset(&key, 0, sizeof(key));
219
220 key.st = st->has_shareable_shaders ? NULL : st;
221
222 /* When this is true, we will add an extra input to the vertex
223 * shader translation (for edgeflags), an extra output with
224 * edgeflag semantics, and extend the vertex shader to pass through
225 * the input to the output. We'll need to use similar logic to set
226 * up the extra vertex_element input for edgeflags.
227 */
228 key.passthrough_edgeflags = st->ctx->Array._PerVertexEdgeFlagsEnabled;
229
230 key.clamp_color = st->clamp_vert_color_in_shader &&
231 st->ctx->Light._ClampVertexColor &&
232 (vp->info.outputs_written &
233 (VARYING_SLOT_COL0 |
234 VARYING_SLOT_COL1 |
235 VARYING_SLOT_BFC0 |
236 VARYING_SLOT_BFC1));
237
238 if (!st->ctx->GeometryProgram._Current &&
239 !st->ctx->TessEvalProgram._Current) {
240 /* _NEW_POINT */
241 if (st->lower_point_size)
242 key.export_point_size = !st->ctx->VertexProgram.PointSizeEnabled && !st->ctx->PointSizeIsSet;
243 /* _NEW_TRANSFORM */
244 if (st->lower_ucp && st_user_clip_planes_enabled(st->ctx))
245 key.lower_ucp = st->ctx->Transform.ClipPlanesEnabled;
246 }
247
248 update_gl_clamp(st, st->ctx->VertexProgram._Current, key.gl_clamp);
249
250 simple_mtx_lock(&st->ctx->Shared->Mutex);
251 st->vp_variant = st_get_common_variant(st, vp, &key);
252 simple_mtx_unlock(&st->ctx->Shared->Mutex);
253 }
254
255 _mesa_reference_program(st->ctx, &st->vp, vp);
256
257 cso_set_vertex_shader_handle(st->cso_context,
258 st->vp_variant->base.driver_shader);
259 }
260
261
262 static void *
st_update_common_program(struct st_context * st,struct gl_program * prog,unsigned pipe_shader,struct gl_program ** dst)263 st_update_common_program(struct st_context *st, struct gl_program *prog,
264 unsigned pipe_shader, struct gl_program **dst)
265 {
266 if (!prog) {
267 _mesa_reference_program(st->ctx, dst, NULL);
268 return NULL;
269 }
270
271 _mesa_reference_program(st->ctx, dst, prog);
272
273 if (st->shader_has_one_variant[prog->info.stage])
274 return prog->variants->driver_shader;
275
276 struct st_common_variant_key key;
277
278 /* use memset, not an initializer to be sure all memory is zeroed */
279 memset(&key, 0, sizeof(key));
280
281 key.st = st->has_shareable_shaders ? NULL : st;
282
283 if (pipe_shader == PIPE_SHADER_GEOMETRY ||
284 pipe_shader == PIPE_SHADER_TESS_EVAL) {
285 key.clamp_color = st->clamp_vert_color_in_shader &&
286 st->ctx->Light._ClampVertexColor &&
287 (prog->info.outputs_written &
288 (VARYING_SLOT_COL0 |
289 VARYING_SLOT_COL1 |
290 VARYING_SLOT_BFC0 |
291 VARYING_SLOT_BFC1));
292
293 if (st->lower_ucp && st_user_clip_planes_enabled(st->ctx) &&
294 (pipe_shader == PIPE_SHADER_GEOMETRY ||
295 !st->ctx->GeometryProgram._Current))
296 key.lower_ucp = st->ctx->Transform.ClipPlanesEnabled;
297
298 if (st->lower_point_size)
299 key.export_point_size = !st->ctx->VertexProgram.PointSizeEnabled && !st->ctx->PointSizeIsSet;
300 }
301
302 update_gl_clamp(st, prog, key.gl_clamp);
303
304 simple_mtx_lock(&st->ctx->Shared->Mutex);
305 void *result = st_get_common_variant(st, prog, &key)->base.driver_shader;
306 simple_mtx_unlock(&st->ctx->Shared->Mutex);
307
308 return result;
309 }
310
311
312 void
st_update_gp(struct st_context * st)313 st_update_gp(struct st_context *st)
314 {
315 void *shader = st_update_common_program(st,
316 st->ctx->GeometryProgram._Current,
317 PIPE_SHADER_GEOMETRY, &st->gp);
318 cso_set_geometry_shader_handle(st->cso_context, shader);
319 }
320
321
322 void
st_update_tcp(struct st_context * st)323 st_update_tcp(struct st_context *st)
324 {
325 void *shader = st_update_common_program(st,
326 st->ctx->TessCtrlProgram._Current,
327 PIPE_SHADER_TESS_CTRL, &st->tcp);
328 cso_set_tessctrl_shader_handle(st->cso_context, shader);
329 }
330
331
332 void
st_update_tep(struct st_context * st)333 st_update_tep(struct st_context *st)
334 {
335 void *shader = st_update_common_program(st,
336 st->ctx->TessEvalProgram._Current,
337 PIPE_SHADER_TESS_EVAL, &st->tep);
338 cso_set_tesseval_shader_handle(st->cso_context, shader);
339 }
340
341
342 void
st_update_cp(struct st_context * st)343 st_update_cp(struct st_context *st)
344 {
345 void *shader = st_update_common_program(st,
346 st->ctx->ComputeProgram._Current,
347 PIPE_SHADER_COMPUTE, &st->cp);
348 cso_set_compute_shader_handle(st->cso_context, shader);
349 }
350