1 /**************************************************************************
3 * Copyright 2003 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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.
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"
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"
51 #include "st_context.h"
53 #include "st_program.h"
54 #include "st_texture.h"
58 get_texture_target(struct gl_context
*ctx
, const unsigned unit
)
60 struct gl_texture_object
*texObj
= _mesa_get_tex_unit(ctx
, unit
)->_Current
;
61 gl_texture_index index
;
64 index
= _mesa_tex_target_to_index(ctx
, texObj
->Target
);
66 /* fallback for missing texture */
67 index
= TEXTURE_2D_INDEX
;
70 /* Map mesa texture target to TGSI texture target.
71 * Copied from st_mesa_to_tgsi.c, the shadow part is omitted */
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
;
87 return TGSI_TEXTURE_1D
;
93 * Update fragment program state/atom. This involves translating the
94 * Mesa fragment program into a gallium fragment program and binding it.
97 st_update_fp( struct st_context
*st
)
99 struct st_fragment_program
*stfp
;
101 assert(st
->ctx
->FragmentProgram
._Current
);
102 stfp
= st_fragment_program(st
->ctx
->FragmentProgram
._Current
);
103 assert(stfp
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
);
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 */
111 !stfp
->variants
->key
.drawpixels
&&
112 !stfp
->variants
->key
.bitmap
) {
113 shader
= stfp
->variants
->driver_shader
;
115 struct st_fp_variant_key key
;
117 /* use memset, not an initializer to be sure all memory is zeroed */
118 memset(&key
, 0, sizeof(key
));
120 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
122 /* _NEW_FRAG_CLAMP */
123 key
.clamp_color
= st
->clamp_frag_color_in_shader
&&
124 st
->ctx
->Color
._ClampFragmentColor
;
126 /* _NEW_MULTISAMPLE | _NEW_BUFFERS */
127 key
.persample_shading
=
128 st
->force_persample_in_shader
&&
129 _mesa_is_multisample_enabled(st
->ctx
) &&
130 st
->ctx
->Multisample
.SampleShading
&&
131 st
->ctx
->Multisample
.MinSampleShadingValue
*
132 _mesa_geometric_samples(st
->ctx
->DrawBuffer
) > 1;
134 key
.lower_depth_clamp
=
135 st
->clamp_frag_depth_in_shader
&&
136 (st
->ctx
->Transform
.DepthClampNear
||
137 st
->ctx
->Transform
.DepthClampFar
);
140 key
.fog
= st
->ctx
->Fog
._PackedEnabledMode
;
142 for (unsigned u
= 0; u
< MAX_NUM_FRAGMENT_REGISTERS_ATI
; u
++) {
143 key
.texture_targets
[u
] = get_texture_target(st
->ctx
, u
);
147 key
.external
= st_get_external_sampler_key(st
, &stfp
->Base
);
149 shader
= st_get_fp_variant(st
, stfp
, &key
)->driver_shader
;
152 st_reference_fragprog(st
, &st
->fp
, stfp
);
154 cso_set_fragment_shader_handle(st
->cso_context
, shader
);
159 * Update vertex program state/atom. This involves translating the
160 * Mesa vertex program into a gallium fragment program and binding it.
163 st_update_vp( struct st_context
*st
)
165 struct st_vertex_program
*stvp
;
167 /* find active shader and params -- Should be covered by
168 * ST_NEW_VERTEX_PROGRAM
170 assert(st
->ctx
->VertexProgram
._Current
);
171 stvp
= st_vertex_program(st
->ctx
->VertexProgram
._Current
);
172 assert(stvp
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
);
174 if (st
->shader_has_one_variant
[MESA_SHADER_VERTEX
] &&
176 stvp
->variants
->key
.passthrough_edgeflags
== st
->vertdata_edgeflags
) {
177 st
->vp_variant
= stvp
->variants
;
179 struct st_vp_variant_key key
;
181 memset(&key
, 0, sizeof(key
));
183 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
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.
191 key
.passthrough_edgeflags
= st
->vertdata_edgeflags
;
193 key
.clamp_color
= st
->clamp_vert_color_in_shader
&&
194 st
->ctx
->Light
._ClampVertexColor
&&
195 (stvp
->Base
.info
.outputs_written
&
201 key
.lower_depth_clamp
=
202 !st
->gp
&& !st
->tep
&&
203 st
->clamp_frag_depth_in_shader
&&
204 (st
->ctx
->Transform
.DepthClampNear
||
205 st
->ctx
->Transform
.DepthClampFar
);
207 if (key
.lower_depth_clamp
)
208 key
.clip_negative_one_to_one
=
209 st
->ctx
->Transform
.ClipDepthMode
== GL_NEGATIVE_ONE_TO_ONE
;
211 st
->vp_variant
= st_get_vp_variant(st
, stvp
, &key
);
214 st_reference_vertprog(st
, &st
->vp
, stvp
);
216 cso_set_vertex_shader_handle(st
->cso_context
,
217 st
->vp_variant
->driver_shader
);
222 st_update_common_program(struct st_context
*st
, struct gl_program
*prog
,
223 unsigned pipe_shader
, struct st_common_program
**dst
)
225 struct st_common_program
*stp
;
228 st_reference_prog(st
, dst
, NULL
);
232 stp
= st_common_program(prog
);
233 st_reference_prog(st
, dst
, stp
);
235 if (st
->shader_has_one_variant
[prog
->info
.stage
] && stp
->variants
)
236 return stp
->variants
->driver_shader
;
238 struct st_basic_variant_key key
;
240 /* use memset, not an initializer to be sure all memory is zeroed */
241 memset(&key
, 0, sizeof(key
));
243 key
.st
= st
->has_shareable_shaders
? NULL
: st
;
245 if (pipe_shader
== PIPE_SHADER_GEOMETRY
||
246 pipe_shader
== PIPE_SHADER_TESS_EVAL
) {
247 key
.clamp_color
= st
->clamp_vert_color_in_shader
&&
248 st
->ctx
->Light
._ClampVertexColor
&&
249 (stp
->Base
.info
.outputs_written
&
256 return st_get_basic_variant(st
, pipe_shader
, stp
, &key
)->driver_shader
;
261 st_update_gp(struct st_context
*st
)
263 void *shader
= st_update_common_program(st
,
264 st
->ctx
->GeometryProgram
._Current
,
265 PIPE_SHADER_GEOMETRY
, &st
->gp
);
266 cso_set_geometry_shader_handle(st
->cso_context
, shader
);
271 st_update_tcp(struct st_context
*st
)
273 void *shader
= st_update_common_program(st
,
274 st
->ctx
->TessCtrlProgram
._Current
,
275 PIPE_SHADER_TESS_CTRL
, &st
->tcp
);
276 cso_set_tessctrl_shader_handle(st
->cso_context
, shader
);
281 st_update_tep(struct st_context
*st
)
283 void *shader
= st_update_common_program(st
,
284 st
->ctx
->TessEvalProgram
._Current
,
285 PIPE_SHADER_TESS_EVAL
, &st
->tep
);
286 cso_set_tesseval_shader_handle(st
->cso_context
, shader
);
291 st_update_cp( struct st_context
*st
)
293 struct st_compute_program
*stcp
;
295 if (!st
->ctx
->ComputeProgram
._Current
) {
296 cso_set_compute_shader_handle(st
->cso_context
, NULL
);
297 st_reference_compprog(st
, &st
->cp
, NULL
);
301 stcp
= st_compute_program(st
->ctx
->ComputeProgram
._Current
);
302 assert(stcp
->Base
.Target
== GL_COMPUTE_PROGRAM_NV
);
306 if (st
->shader_has_one_variant
[MESA_SHADER_COMPUTE
] && stcp
->variants
) {
307 shader
= stcp
->variants
->driver_shader
;
309 shader
= st_get_cp_variant(st
, &stcp
->tgsi
,
310 &stcp
->variants
)->driver_shader
;
313 st_reference_compprog(st
, &st
->cp
, stcp
);
315 cso_set_compute_shader_handle(st
->cso_context
, shader
);