draw: inject frontface info into wireframe outputs
[mesa.git] / src / gallium / drivers / softpipe / sp_state_derived.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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_inlines.h"
29 #include "util/u_math.h"
30 #include "util/u_memory.h"
31 #include "util/u_pstipple.h"
32 #include "pipe/p_shader_tokens.h"
33 #include "draw/draw_context.h"
34 #include "draw/draw_vertex.h"
35 #include "sp_context.h"
36 #include "sp_screen.h"
37 #include "sp_state.h"
38 #include "sp_texture.h"
39 #include "sp_tex_sample.h"
40 #include "sp_tex_tile_cache.h"
41
42
43 /**
44 * Mark the current vertex layout as "invalid".
45 * We'll validate the vertex layout later, when we start to actually
46 * render a point or line or tri.
47 */
48 static void
49 invalidate_vertex_layout(struct softpipe_context *softpipe)
50 {
51 softpipe->vertex_info.num_attribs = 0;
52 }
53
54
55 /**
56 * The vertex info describes how to convert the post-transformed vertices
57 * (simple float[][4]) used by the 'draw' module into vertices for
58 * rasterization.
59 *
60 * This function validates the vertex layout and returns a pointer to a
61 * vertex_info object.
62 */
63 struct vertex_info *
64 softpipe_get_vertex_info(struct softpipe_context *softpipe)
65 {
66 struct vertex_info *vinfo = &softpipe->vertex_info;
67
68 draw_prepare_shader_outputs(softpipe->draw);
69
70 if (vinfo->num_attribs == 0) {
71 /* compute vertex layout now */
72 const struct tgsi_shader_info *fsInfo = &softpipe->fs_variant->info;
73 struct vertex_info *vinfo_vbuf = &softpipe->vertex_info_vbuf;
74 const uint num = draw_num_shader_outputs(softpipe->draw);
75 uint i;
76
77 /* Tell draw_vbuf to simply emit the whole post-xform vertex
78 * as-is. No longer any need to try and emit draw vertex_header
79 * info.
80 */
81 vinfo_vbuf->num_attribs = 0;
82 for (i = 0; i < num; i++) {
83 draw_emit_vertex_attr(vinfo_vbuf, EMIT_4F, INTERP_PERSPECTIVE, i);
84 }
85 draw_compute_vertex_size(vinfo_vbuf);
86
87 /*
88 * Loop over fragment shader inputs, searching for the matching output
89 * from the vertex shader.
90 */
91 vinfo->num_attribs = 0;
92 for (i = 0; i < fsInfo->num_inputs; i++) {
93 int src;
94 enum interp_mode interp = INTERP_LINEAR;
95
96 switch (fsInfo->input_interpolate[i]) {
97 case TGSI_INTERPOLATE_CONSTANT:
98 interp = INTERP_CONSTANT;
99 break;
100 case TGSI_INTERPOLATE_LINEAR:
101 interp = INTERP_LINEAR;
102 break;
103 case TGSI_INTERPOLATE_PERSPECTIVE:
104 interp = INTERP_PERSPECTIVE;
105 break;
106 case TGSI_INTERPOLATE_COLOR:
107 assert(fsInfo->input_semantic_name[i] == TGSI_SEMANTIC_COLOR);
108 break;
109 default:
110 assert(0);
111 }
112
113 switch (fsInfo->input_semantic_name[i]) {
114 case TGSI_SEMANTIC_POSITION:
115 interp = INTERP_POS;
116 break;
117
118 case TGSI_SEMANTIC_COLOR:
119 if (fsInfo->input_interpolate[i] == TGSI_INTERPOLATE_COLOR) {
120 if (softpipe->rasterizer->flatshade)
121 interp = INTERP_CONSTANT;
122 else
123 interp = INTERP_PERSPECTIVE;
124 }
125 break;
126 }
127
128 /* this includes texcoords and varying vars */
129 src = draw_find_shader_output(softpipe->draw,
130 fsInfo->input_semantic_name[i],
131 fsInfo->input_semantic_index[i]);
132 if (fsInfo->input_semantic_name[i] == TGSI_SEMANTIC_COLOR && src == -1)
133 /* try and find a bcolor */
134 src = draw_find_shader_output(softpipe->draw,
135 TGSI_SEMANTIC_BCOLOR, fsInfo->input_semantic_index[i]);
136
137 draw_emit_vertex_attr(vinfo, EMIT_4F, interp, src);
138 }
139
140 softpipe->psize_slot = draw_find_shader_output(softpipe->draw,
141 TGSI_SEMANTIC_PSIZE, 0);
142 if (softpipe->psize_slot >= 0) {
143 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT,
144 softpipe->psize_slot);
145 }
146
147 draw_compute_vertex_size(vinfo);
148 }
149
150 return vinfo;
151 }
152
153
154 /**
155 * Called from vbuf module.
156 *
157 * Note that there's actually two different vertex layouts in softpipe.
158 *
159 * The normal one is computed in softpipe_get_vertex_info() above and is
160 * used by the point/line/tri "setup" code.
161 *
162 * The other one (this one) is only used by the vbuf module (which is
163 * not normally used by default but used in testing). For the vbuf module,
164 * we basically want to pass-through the draw module's vertex layout as-is.
165 * When the softpipe vbuf code begins drawing, the normal vertex layout
166 * will come into play again.
167 */
168 struct vertex_info *
169 softpipe_get_vbuf_vertex_info(struct softpipe_context *softpipe)
170 {
171 (void) softpipe_get_vertex_info(softpipe);
172 return &softpipe->vertex_info_vbuf;
173 }
174
175
176 /**
177 * Recompute cliprect from scissor bounds, scissor enable and surface size.
178 */
179 static void
180 compute_cliprect(struct softpipe_context *sp)
181 {
182 /* SP_NEW_FRAMEBUFFER
183 */
184 uint surfWidth = sp->framebuffer.width;
185 uint surfHeight = sp->framebuffer.height;
186
187 /* SP_NEW_RASTERIZER
188 */
189 if (sp->rasterizer->scissor) {
190
191 /* SP_NEW_SCISSOR
192 *
193 * clip to scissor rect:
194 */
195 sp->cliprect.minx = MAX2(sp->scissor.minx, 0);
196 sp->cliprect.miny = MAX2(sp->scissor.miny, 0);
197 sp->cliprect.maxx = MIN2(sp->scissor.maxx, surfWidth);
198 sp->cliprect.maxy = MIN2(sp->scissor.maxy, surfHeight);
199 }
200 else {
201 /* clip to surface bounds */
202 sp->cliprect.minx = 0;
203 sp->cliprect.miny = 0;
204 sp->cliprect.maxx = surfWidth;
205 sp->cliprect.maxy = surfHeight;
206 }
207 }
208
209
210 static void
211 set_shader_sampler(struct softpipe_context *softpipe,
212 unsigned shader,
213 int max_sampler)
214 {
215 int i;
216 for (i = 0; i <= max_sampler; i++) {
217 softpipe->tgsi.sampler[shader]->sp_sampler[i] =
218 (struct sp_sampler *)(softpipe->samplers[shader][i]);
219 }
220 }
221
222 static void
223 update_tgsi_samplers( struct softpipe_context *softpipe )
224 {
225 unsigned i, sh;
226
227 set_shader_sampler(softpipe, PIPE_SHADER_VERTEX,
228 softpipe->vs->max_sampler);
229 set_shader_sampler(softpipe, PIPE_SHADER_FRAGMENT,
230 softpipe->fs_variant->info.file_max[TGSI_FILE_SAMPLER]);
231 if (softpipe->gs) {
232 set_shader_sampler(softpipe, PIPE_SHADER_GEOMETRY,
233 softpipe->gs->max_sampler);
234 }
235
236 /* XXX is this really necessary here??? */
237 for (sh = 0; sh < Elements(softpipe->tex_cache); sh++) {
238 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
239 struct softpipe_tex_tile_cache *tc = softpipe->tex_cache[sh][i];
240 if (tc && tc->texture) {
241 struct softpipe_resource *spt = softpipe_resource(tc->texture);
242 if (spt->timestamp != tc->timestamp) {
243 sp_tex_tile_cache_validate_texture( tc );
244 /*
245 _debug_printf("INV %d %d\n", tc->timestamp, spt->timestamp);
246 */
247 tc->timestamp = spt->timestamp;
248 }
249 }
250 }
251 }
252 }
253
254
255 static void
256 update_fragment_shader(struct softpipe_context *softpipe, unsigned prim)
257 {
258 struct sp_fragment_shader_variant_key key;
259
260 memset(&key, 0, sizeof(key));
261
262 if (prim == PIPE_PRIM_TRIANGLES)
263 key.polygon_stipple = softpipe->rasterizer->poly_stipple_enable;
264
265 if (softpipe->fs) {
266 softpipe->fs_variant = softpipe_find_fs_variant(softpipe,
267 softpipe->fs, &key);
268
269 /* prepare the TGSI interpreter for FS execution */
270 softpipe->fs_variant->prepare(softpipe->fs_variant,
271 softpipe->fs_machine,
272 (struct tgsi_sampler *) softpipe->
273 tgsi.sampler[PIPE_SHADER_FRAGMENT]);
274 }
275 else {
276 softpipe->fs_variant = NULL;
277 }
278
279 /* This would be the logical place to pass the fragment shader
280 * to the draw module. However, doing this here, during state
281 * validation, causes problems with the 'draw' module helpers for
282 * wide/AA/stippled lines.
283 * In principle, the draw's fragment shader should be per-variant
284 * but that doesn't work. So we use a single draw fragment shader
285 * per fragment shader, not per variant.
286 */
287 #if 0
288 if (softpipe->fs_variant) {
289 draw_bind_fragment_shader(softpipe->draw,
290 softpipe->fs_variant->draw_shader);
291 }
292 else {
293 draw_bind_fragment_shader(softpipe->draw, NULL);
294 }
295 #endif
296 }
297
298
299 /**
300 * This should be called when the polygon stipple pattern changes.
301 * We create a new texture from the stipple pattern and create a new
302 * sampler view.
303 */
304 static void
305 update_polygon_stipple_pattern(struct softpipe_context *softpipe)
306 {
307 struct pipe_resource *tex;
308 struct pipe_sampler_view *view;
309
310 tex = util_pstipple_create_stipple_texture(&softpipe->pipe,
311 softpipe->poly_stipple.stipple);
312 pipe_resource_reference(&softpipe->pstipple.texture, tex);
313 pipe_resource_reference(&tex, NULL);
314
315 view = util_pstipple_create_sampler_view(&softpipe->pipe,
316 softpipe->pstipple.texture);
317 pipe_sampler_view_reference(&softpipe->pstipple.sampler_view, view);
318 pipe_sampler_view_reference(&view, NULL);
319 }
320
321
322 /**
323 * Should be called when polygon stipple is enabled/disabled or when
324 * the fragment shader changes.
325 * We add/update the fragment sampler and sampler views to sample from
326 * the polygon stipple texture. The texture unit that we use depends on
327 * the fragment shader (we need to use a unit not otherwise used by the
328 * shader).
329 */
330 static void
331 update_polygon_stipple_enable(struct softpipe_context *softpipe, unsigned prim)
332 {
333 if (prim == PIPE_PRIM_TRIANGLES &&
334 softpipe->fs_variant->key.polygon_stipple) {
335 const unsigned unit = softpipe->fs_variant->stipple_sampler_unit;
336
337 /* sampler state */
338 softpipe->samplers[PIPE_SHADER_FRAGMENT][unit] = softpipe->pstipple.sampler;
339
340 /* sampler view state */
341 softpipe_set_sampler_views(&softpipe->pipe, PIPE_SHADER_FRAGMENT,
342 unit, 1, &softpipe->pstipple.sampler_view);
343
344 softpipe->dirty |= SP_NEW_SAMPLER;
345 }
346 }
347
348
349 /* Hopefully this will remain quite simple, otherwise need to pull in
350 * something like the state tracker mechanism.
351 */
352 void
353 softpipe_update_derived(struct softpipe_context *softpipe, unsigned prim)
354 {
355 struct softpipe_screen *sp_screen = softpipe_screen(softpipe->pipe.screen);
356
357 /* Check for updated textures.
358 */
359 if (softpipe->tex_timestamp != sp_screen->timestamp) {
360 softpipe->tex_timestamp = sp_screen->timestamp;
361 softpipe->dirty |= SP_NEW_TEXTURE;
362 }
363
364 #if DO_PSTIPPLE_IN_HELPER_MODULE
365 if (softpipe->dirty & SP_NEW_STIPPLE)
366 /* before updating samplers! */
367 update_polygon_stipple_pattern(softpipe);
368 #endif
369
370 if (softpipe->dirty & (SP_NEW_RASTERIZER |
371 SP_NEW_FS))
372 update_fragment_shader(softpipe, prim);
373
374 #if DO_PSTIPPLE_IN_HELPER_MODULE
375 if (softpipe->dirty & (SP_NEW_RASTERIZER |
376 SP_NEW_STIPPLE |
377 SP_NEW_FS))
378 update_polygon_stipple_enable(softpipe, prim);
379 #endif
380
381 /* TODO: this looks suboptimal */
382 if (softpipe->dirty & (SP_NEW_SAMPLER |
383 SP_NEW_TEXTURE |
384 SP_NEW_FS |
385 SP_NEW_VS))
386 update_tgsi_samplers( softpipe );
387
388 if (softpipe->dirty & (SP_NEW_RASTERIZER |
389 SP_NEW_FS |
390 SP_NEW_VS))
391 invalidate_vertex_layout( softpipe );
392
393 if (softpipe->dirty & (SP_NEW_SCISSOR |
394 SP_NEW_RASTERIZER |
395 SP_NEW_FRAMEBUFFER))
396 compute_cliprect(softpipe);
397
398 if (softpipe->dirty & (SP_NEW_BLEND |
399 SP_NEW_DEPTH_STENCIL_ALPHA |
400 SP_NEW_FRAMEBUFFER |
401 SP_NEW_FS))
402 sp_build_quad_pipeline(softpipe);
403
404 softpipe->dirty = 0;
405 }