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