vc4: Don't try the SF coalescing unless it's on a def.
[mesa.git] / src / gallium / drivers / llvmpipe / lp_state_derived.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 #include "util/u_math.h"
29 #include "util/u_memory.h"
30 #include "pipe/p_shader_tokens.h"
31 #include "draw/draw_context.h"
32 #include "draw/draw_vertex.h"
33 #include "draw/draw_private.h"
34 #include "lp_context.h"
35 #include "lp_screen.h"
36 #include "lp_setup.h"
37 #include "lp_state.h"
38
39
40
41 /**
42 * The vertex info describes how to convert the post-transformed vertices
43 * (simple float[][4]) used by the 'draw' module into vertices for
44 * rasterization.
45 *
46 * This function validates the vertex layout.
47 */
48 static void
49 compute_vertex_info(struct llvmpipe_context *llvmpipe)
50 {
51 const struct lp_fragment_shader *lpfs = llvmpipe->fs;
52 struct vertex_info *vinfo = &llvmpipe->vertex_info;
53 int vs_index;
54 uint i;
55
56 draw_prepare_shader_outputs(llvmpipe->draw);
57
58 llvmpipe->color_slot[0] = 0;
59 llvmpipe->color_slot[1] = 0;
60 llvmpipe->bcolor_slot[0] = 0;
61 llvmpipe->bcolor_slot[1] = 0;
62 llvmpipe->viewport_index_slot = 0;
63 llvmpipe->layer_slot = 0;
64 llvmpipe->face_slot = 0;
65 llvmpipe->psize_slot = 0;
66
67 /*
68 * Match FS inputs against VS outputs, emitting the necessary
69 * attributes. Could cache these structs and look them up with a
70 * combination of fragment shader, vertex shader ids.
71 */
72
73 vinfo->num_attribs = 0;
74
75 vs_index = draw_find_shader_output(llvmpipe->draw,
76 TGSI_SEMANTIC_POSITION,
77 0);
78
79 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, vs_index);
80
81 for (i = 0; i < lpfs->info.base.num_inputs; i++) {
82 /*
83 * Search for each input in current vs output:
84 */
85
86 vs_index = draw_find_shader_output(llvmpipe->draw,
87 lpfs->info.base.input_semantic_name[i],
88 lpfs->info.base.input_semantic_index[i]);
89
90 if (lpfs->info.base.input_semantic_name[i] == TGSI_SEMANTIC_COLOR &&
91 lpfs->info.base.input_semantic_index[i] < 2) {
92 int idx = lpfs->info.base.input_semantic_index[i];
93 llvmpipe->color_slot[idx] = vinfo->num_attribs;
94 }
95
96 if (lpfs->info.base.input_semantic_name[i] == TGSI_SEMANTIC_FACE) {
97 llvmpipe->face_slot = vinfo->num_attribs;
98 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT, vs_index);
99 } else if (lpfs->info.base.input_semantic_name[i] == TGSI_SEMANTIC_PRIMID) {
100 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT, vs_index);
101 /*
102 * For vp index and layer, if the fs requires them but the vs doesn't
103 * provide them, store the slot - we'll later replace the data directly
104 * with zero (as required by ARB_fragment_layer_viewport). This is
105 * because draw itself just redirects them to whatever was at output 0.
106 * We'll also store the real vpindex/layer slot for setup use.
107 */
108 } else if (lpfs->info.base.input_semantic_name[i] ==
109 TGSI_SEMANTIC_VIEWPORT_INDEX) {
110 if (vs_index >= 0) {
111 llvmpipe->viewport_index_slot = vinfo->num_attribs;
112 }
113 else {
114 llvmpipe->fake_vpindex_slot = vinfo->num_attribs;
115 }
116 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT, vs_index);
117 } else if (lpfs->info.base.input_semantic_name[i] == TGSI_SEMANTIC_LAYER) {
118 if (vs_index >= 0) {
119 llvmpipe->layer_slot = vinfo->num_attribs;
120 }
121 else {
122 llvmpipe->fake_layer_slot = vinfo->num_attribs;
123 }
124 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT, vs_index);
125 } else {
126 /*
127 * Emit the requested fs attribute for all but position.
128 */
129 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, vs_index);
130 }
131 }
132
133 /* Figure out if we need bcolor as well.
134 */
135 for (i = 0; i < 2; i++) {
136 vs_index = draw_find_shader_output(llvmpipe->draw,
137 TGSI_SEMANTIC_BCOLOR, i);
138
139 if (vs_index >= 0) {
140 llvmpipe->bcolor_slot[i] = vinfo->num_attribs;
141 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, vs_index);
142 }
143 }
144
145 /* Figure out if we need pointsize as well.
146 */
147 vs_index = draw_find_shader_output(llvmpipe->draw,
148 TGSI_SEMANTIC_PSIZE, 0);
149
150 if (vs_index >= 0) {
151 llvmpipe->psize_slot = vinfo->num_attribs;
152 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT, vs_index);
153 }
154
155 /* Figure out if we need viewport index (if it wasn't already in fs input) */
156 if (llvmpipe->viewport_index_slot == 0) {
157 vs_index = draw_find_shader_output(llvmpipe->draw,
158 TGSI_SEMANTIC_VIEWPORT_INDEX,
159 0);
160 if (vs_index >= 0) {
161 llvmpipe->viewport_index_slot = vinfo->num_attribs;
162 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT, vs_index);
163 }
164 }
165
166 /* Figure out if we need layer (if it wasn't already in fs input) */
167 if (llvmpipe->layer_slot == 0) {
168 vs_index = draw_find_shader_output(llvmpipe->draw,
169 TGSI_SEMANTIC_LAYER,
170 0);
171 if (vs_index >= 0) {
172 llvmpipe->layer_slot = vinfo->num_attribs;
173 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT, vs_index);
174 }
175 }
176
177 draw_compute_vertex_size(vinfo);
178 lp_setup_set_vertex_info(llvmpipe->setup, vinfo);
179 }
180
181
182 /**
183 * Handle state changes.
184 * Called just prior to drawing anything (pipe::draw_arrays(), etc).
185 *
186 * Hopefully this will remain quite simple, otherwise need to pull in
187 * something like the state tracker mechanism.
188 */
189 void llvmpipe_update_derived( struct llvmpipe_context *llvmpipe )
190 {
191 struct llvmpipe_screen *lp_screen = llvmpipe_screen(llvmpipe->pipe.screen);
192
193 /* Check for updated textures.
194 */
195 if (llvmpipe->tex_timestamp != lp_screen->timestamp) {
196 llvmpipe->tex_timestamp = lp_screen->timestamp;
197 llvmpipe->dirty |= LP_NEW_SAMPLER_VIEW;
198 }
199
200 if (llvmpipe->dirty & (LP_NEW_RASTERIZER |
201 LP_NEW_FS |
202 LP_NEW_VS))
203 compute_vertex_info( llvmpipe );
204
205 if (llvmpipe->dirty & (LP_NEW_FS |
206 LP_NEW_FRAMEBUFFER |
207 LP_NEW_BLEND |
208 LP_NEW_SCISSOR |
209 LP_NEW_DEPTH_STENCIL_ALPHA |
210 LP_NEW_RASTERIZER |
211 LP_NEW_SAMPLER |
212 LP_NEW_SAMPLER_VIEW |
213 LP_NEW_OCCLUSION_QUERY))
214 llvmpipe_update_fs( llvmpipe );
215
216 if (llvmpipe->dirty & (LP_NEW_RASTERIZER)) {
217 boolean discard =
218 (llvmpipe->sample_mask & 1) == 0 ||
219 (llvmpipe->rasterizer ? llvmpipe->rasterizer->rasterizer_discard : FALSE);
220
221 lp_setup_set_rasterizer_discard(llvmpipe->setup, discard);
222 }
223
224 if (llvmpipe->dirty & (LP_NEW_FS |
225 LP_NEW_FRAMEBUFFER |
226 LP_NEW_RASTERIZER))
227 llvmpipe_update_setup( llvmpipe );
228
229 if (llvmpipe->dirty & LP_NEW_BLEND_COLOR)
230 lp_setup_set_blend_color(llvmpipe->setup,
231 &llvmpipe->blend_color);
232
233 if (llvmpipe->dirty & LP_NEW_SCISSOR)
234 lp_setup_set_scissors(llvmpipe->setup, llvmpipe->scissors);
235
236 if (llvmpipe->dirty & LP_NEW_DEPTH_STENCIL_ALPHA) {
237 lp_setup_set_alpha_ref_value(llvmpipe->setup,
238 llvmpipe->depth_stencil->alpha.ref_value);
239 lp_setup_set_stencil_ref_values(llvmpipe->setup,
240 llvmpipe->stencil_ref.ref_value);
241 }
242
243 if (llvmpipe->dirty & LP_NEW_CONSTANTS)
244 lp_setup_set_fs_constants(llvmpipe->setup,
245 Elements(llvmpipe->constants[PIPE_SHADER_FRAGMENT]),
246 llvmpipe->constants[PIPE_SHADER_FRAGMENT]);
247
248 if (llvmpipe->dirty & (LP_NEW_SAMPLER_VIEW))
249 lp_setup_set_fragment_sampler_views(llvmpipe->setup,
250 llvmpipe->num_sampler_views[PIPE_SHADER_FRAGMENT],
251 llvmpipe->sampler_views[PIPE_SHADER_FRAGMENT]);
252
253 if (llvmpipe->dirty & (LP_NEW_SAMPLER))
254 lp_setup_set_fragment_sampler_state(llvmpipe->setup,
255 llvmpipe->num_samplers[PIPE_SHADER_FRAGMENT],
256 llvmpipe->samplers[PIPE_SHADER_FRAGMENT]);
257
258 if (llvmpipe->dirty & LP_NEW_VIEWPORT) {
259 /*
260 * Update setup and fragment's view of the active viewport state.
261 *
262 * XXX TODO: It is possible to only loop over the active viewports
263 * instead of all viewports (PIPE_MAX_VIEWPORTS).
264 */
265 lp_setup_set_viewports(llvmpipe->setup,
266 PIPE_MAX_VIEWPORTS,
267 llvmpipe->viewports);
268 }
269
270 llvmpipe->dirty = 0;
271 }
272