st/mesa: Use compressed fog mode for atifs.
[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 static void
97 update_fp( struct st_context *st )
98 {
99 struct st_fragment_program *stfp;
100 struct st_fp_variant_key key;
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 memset(&key, 0, sizeof(key));
107 key.st = st->has_shareable_shaders ? NULL : st;
108
109 /* _NEW_FRAG_CLAMP */
110 key.clamp_color = st->clamp_frag_color_in_shader &&
111 st->ctx->Color._ClampFragmentColor;
112
113 /* _NEW_MULTISAMPLE | _NEW_BUFFERS */
114 key.persample_shading =
115 st->force_persample_in_shader &&
116 _mesa_is_multisample_enabled(st->ctx) &&
117 st->ctx->Multisample.SampleShading &&
118 st->ctx->Multisample.MinSampleShadingValue *
119 _mesa_geometric_samples(st->ctx->DrawBuffer) > 1;
120
121 if (stfp->ati_fs) {
122 key.fog = st->ctx->Fog._PackedEnabledMode;
123
124 for (unsigned u = 0; u < MAX_NUM_FRAGMENT_REGISTERS_ATI; u++) {
125 key.texture_targets[u] = get_texture_target(st->ctx, u);
126 }
127 }
128
129 key.external = st_get_external_sampler_key(st, &stfp->Base);
130
131 st->fp_variant = st_get_fp_variant(st, stfp, &key);
132
133 st_reference_fragprog(st, &st->fp, stfp);
134
135 cso_set_fragment_shader_handle(st->cso_context,
136 st->fp_variant->driver_shader);
137 }
138
139
140 const struct st_tracked_state st_update_fp = {
141 update_fp /* update */
142 };
143
144
145
146 /**
147 * Update vertex program state/atom. This involves translating the
148 * Mesa vertex program into a gallium fragment program and binding it.
149 */
150 static void
151 update_vp( struct st_context *st )
152 {
153 struct st_vertex_program *stvp;
154 struct st_vp_variant_key key;
155
156 /* find active shader and params -- Should be covered by
157 * ST_NEW_VERTEX_PROGRAM
158 */
159 assert(st->ctx->VertexProgram._Current);
160 stvp = st_vertex_program(st->ctx->VertexProgram._Current);
161 assert(stvp->Base.Target == GL_VERTEX_PROGRAM_ARB);
162
163 memset(&key, 0, sizeof key);
164 key.st = st->has_shareable_shaders ? NULL : st;
165
166 /* When this is true, we will add an extra input to the vertex
167 * shader translation (for edgeflags), an extra output with
168 * edgeflag semantics, and extend the vertex shader to pass through
169 * the input to the output. We'll need to use similar logic to set
170 * up the extra vertex_element input for edgeflags.
171 */
172 key.passthrough_edgeflags = st->vertdata_edgeflags;
173
174 key.clamp_color = st->clamp_vert_color_in_shader &&
175 st->ctx->Light._ClampVertexColor &&
176 (stvp->Base.info.outputs_written &
177 (VARYING_SLOT_COL0 |
178 VARYING_SLOT_COL1 |
179 VARYING_SLOT_BFC0 |
180 VARYING_SLOT_BFC1));
181
182 st->vp_variant = st_get_vp_variant(st, stvp, &key);
183
184 st_reference_vertprog(st, &st->vp, stvp);
185
186 cso_set_vertex_shader_handle(st->cso_context,
187 st->vp_variant->driver_shader);
188
189 st->vertex_result_to_slot = stvp->result_to_output;
190 }
191
192
193 const struct st_tracked_state st_update_vp = {
194 update_vp /* update */
195 };
196
197
198
199 static void
200 update_gp( struct st_context *st )
201 {
202 struct st_geometry_program *stgp;
203
204 if (!st->ctx->GeometryProgram._Current) {
205 cso_set_geometry_shader_handle(st->cso_context, NULL);
206 st_reference_geomprog(st, &st->gp, NULL);
207 return;
208 }
209
210 stgp = st_geometry_program(st->ctx->GeometryProgram._Current);
211 assert(stgp->Base.Target == GL_GEOMETRY_PROGRAM_NV);
212
213 st->gp_variant = st_get_basic_variant(st, PIPE_SHADER_GEOMETRY,
214 &stgp->tgsi, &stgp->variants);
215
216 st_reference_geomprog(st, &st->gp, stgp);
217
218 cso_set_geometry_shader_handle(st->cso_context,
219 st->gp_variant->driver_shader);
220 }
221
222 const struct st_tracked_state st_update_gp = {
223 update_gp /* update */
224 };
225
226
227
228 static void
229 update_tcp( struct st_context *st )
230 {
231 struct st_tessctrl_program *sttcp;
232
233 if (!st->ctx->TessCtrlProgram._Current) {
234 cso_set_tessctrl_shader_handle(st->cso_context, NULL);
235 st_reference_tesscprog(st, &st->tcp, NULL);
236 return;
237 }
238
239 sttcp = st_tessctrl_program(st->ctx->TessCtrlProgram._Current);
240 assert(sttcp->Base.Target == GL_TESS_CONTROL_PROGRAM_NV);
241
242 st->tcp_variant = st_get_basic_variant(st, PIPE_SHADER_TESS_CTRL,
243 &sttcp->tgsi, &sttcp->variants);
244
245 st_reference_tesscprog(st, &st->tcp, sttcp);
246
247 cso_set_tessctrl_shader_handle(st->cso_context,
248 st->tcp_variant->driver_shader);
249 }
250
251 const struct st_tracked_state st_update_tcp = {
252 update_tcp /* update */
253 };
254
255
256
257 static void
258 update_tep( struct st_context *st )
259 {
260 struct st_tesseval_program *sttep;
261
262 if (!st->ctx->TessEvalProgram._Current) {
263 cso_set_tesseval_shader_handle(st->cso_context, NULL);
264 st_reference_tesseprog(st, &st->tep, NULL);
265 return;
266 }
267
268 sttep = st_tesseval_program(st->ctx->TessEvalProgram._Current);
269 assert(sttep->Base.Target == GL_TESS_EVALUATION_PROGRAM_NV);
270
271 st->tep_variant = st_get_basic_variant(st, PIPE_SHADER_TESS_EVAL,
272 &sttep->tgsi, &sttep->variants);
273
274 st_reference_tesseprog(st, &st->tep, sttep);
275
276 cso_set_tesseval_shader_handle(st->cso_context,
277 st->tep_variant->driver_shader);
278 }
279
280 const struct st_tracked_state st_update_tep = {
281 update_tep /* update */
282 };
283
284
285
286 static void
287 update_cp( struct st_context *st )
288 {
289 struct st_compute_program *stcp;
290
291 if (!st->ctx->ComputeProgram._Current) {
292 cso_set_compute_shader_handle(st->cso_context, NULL);
293 st_reference_compprog(st, &st->cp, NULL);
294 return;
295 }
296
297 stcp = st_compute_program(st->ctx->ComputeProgram._Current);
298 assert(stcp->Base.Target == GL_COMPUTE_PROGRAM_NV);
299
300 st->cp_variant = st_get_cp_variant(st, &stcp->tgsi, &stcp->variants);
301
302 st_reference_compprog(st, &st->cp, stcp);
303
304 cso_set_compute_shader_handle(st->cso_context,
305 st->cp_variant->driver_shader);
306 }
307
308 const struct st_tracked_state st_update_cp = {
309 update_cp /* update */
310 };