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