Squashed commit of the following:
[profile/ivi/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_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 "sp_context.h"
34 #include "sp_screen.h"
35 #include "sp_state.h"
36 #include "sp_texture.h"
37 #include "sp_tex_tile_cache.h"
38
39
40 /**
41  * Mark the current vertex layout as "invalid".
42  * We'll validate the vertex layout later, when we start to actually
43  * render a point or line or tri.
44  */
45 static void
46 invalidate_vertex_layout(struct softpipe_context *softpipe)
47 {
48    softpipe->vertex_info.num_attribs =  0;
49 }
50
51
52 /**
53  * The vertex info describes how to convert the post-transformed vertices
54  * (simple float[][4]) used by the 'draw' module into vertices for
55  * rasterization.
56  *
57  * This function validates the vertex layout and returns a pointer to a
58  * vertex_info object.
59  */
60 struct vertex_info *
61 softpipe_get_vertex_info(struct softpipe_context *softpipe)
62 {
63    struct vertex_info *vinfo = &softpipe->vertex_info;
64
65    if (vinfo->num_attribs == 0) {
66       /* compute vertex layout now */
67       const struct sp_fragment_shader *spfs = softpipe->fs;
68       struct vertex_info *vinfo_vbuf = &softpipe->vertex_info_vbuf;
69       const uint num = draw_num_shader_outputs(softpipe->draw);
70       uint i;
71
72       /* Tell draw_vbuf to simply emit the whole post-xform vertex
73        * as-is.  No longer any need to try and emit draw vertex_header
74        * info.
75        */
76       vinfo_vbuf->num_attribs = 0;
77       for (i = 0; i < num; i++) {
78          draw_emit_vertex_attr(vinfo_vbuf, EMIT_4F, INTERP_PERSPECTIVE, i);
79       }
80       draw_compute_vertex_size(vinfo_vbuf);
81
82       /*
83        * Loop over fragment shader inputs, searching for the matching output
84        * from the vertex shader.
85        */
86       vinfo->num_attribs = 0;
87       for (i = 0; i < spfs->info.num_inputs; i++) {
88          int src;
89          enum interp_mode interp;
90
91          switch (spfs->info.input_interpolate[i]) {
92          case TGSI_INTERPOLATE_CONSTANT:
93             interp = INTERP_CONSTANT;
94             break;
95          case TGSI_INTERPOLATE_LINEAR:
96             interp = INTERP_LINEAR;
97             break;
98          case TGSI_INTERPOLATE_PERSPECTIVE:
99             interp = INTERP_PERSPECTIVE;
100             break;
101          default:
102             assert(0);
103             interp = INTERP_LINEAR;
104          }
105
106          switch (spfs->info.input_semantic_name[i]) {
107          case TGSI_SEMANTIC_POSITION:
108             interp = INTERP_POS;
109             break;
110
111          case TGSI_SEMANTIC_COLOR:
112             if (softpipe->rasterizer->flatshade) {
113                interp = INTERP_CONSTANT;
114             }
115             break;
116          }
117
118          /* this includes texcoords and varying vars */
119          src = draw_find_shader_output(softpipe->draw,
120                                        spfs->info.input_semantic_name[i],
121                                        spfs->info.input_semantic_index[i]);
122          draw_emit_vertex_attr(vinfo, EMIT_4F, interp, src);
123       }
124
125       softpipe->psize_slot = draw_find_shader_output(softpipe->draw,
126                                                  TGSI_SEMANTIC_PSIZE, 0);
127       if (softpipe->psize_slot > 0) {
128          draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT,
129                                softpipe->psize_slot);
130       }
131
132       draw_compute_vertex_size(vinfo);
133    }
134
135    return vinfo;
136 }
137
138
139 /**
140  * Called from vbuf module.
141  *
142  * Note that there's actually two different vertex layouts in softpipe.
143  *
144  * The normal one is computed in softpipe_get_vertex_info() above and is
145  * used by the point/line/tri "setup" code.
146  *
147  * The other one (this one) is only used by the vbuf module (which is
148  * not normally used by default but used in testing).  For the vbuf module,
149  * we basically want to pass-through the draw module's vertex layout as-is.
150  * When the softpipe vbuf code begins drawing, the normal vertex layout
151  * will come into play again.
152  */
153 struct vertex_info *
154 softpipe_get_vbuf_vertex_info(struct softpipe_context *softpipe)
155 {
156    (void) softpipe_get_vertex_info(softpipe);
157    return &softpipe->vertex_info_vbuf;
158 }
159
160
161 /**
162  * Recompute cliprect from scissor bounds, scissor enable and surface size.
163  */
164 static void
165 compute_cliprect(struct softpipe_context *sp)
166 {
167    /* SP_NEW_FRAMEBUFFER
168     */
169    uint surfWidth = sp->framebuffer.width;
170    uint surfHeight = sp->framebuffer.height;
171
172    /* SP_NEW_RASTERIZER
173     */
174    if (sp->rasterizer->scissor) {
175
176       /* SP_NEW_SCISSOR
177        *
178        * clip to scissor rect:
179        */
180       sp->cliprect.minx = MAX2(sp->scissor.minx, 0);
181       sp->cliprect.miny = MAX2(sp->scissor.miny, 0);
182       sp->cliprect.maxx = MIN2(sp->scissor.maxx, surfWidth);
183       sp->cliprect.maxy = MIN2(sp->scissor.maxy, surfHeight);
184    }
185    else {
186       /* clip to surface bounds */
187       sp->cliprect.minx = 0;
188       sp->cliprect.miny = 0;
189       sp->cliprect.maxx = surfWidth;
190       sp->cliprect.maxy = surfHeight;
191    }
192 }
193
194
195 static void
196 update_tgsi_samplers( struct softpipe_context *softpipe )
197 {
198    unsigned i;
199
200    softpipe_reset_sampler_varients( softpipe );
201
202    for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
203       struct softpipe_tex_tile_cache *tc = softpipe->tex_cache[i];
204       if (tc->texture) {
205          struct softpipe_resource *spt = softpipe_resource(tc->texture);
206          if (spt->timestamp != tc->timestamp) {
207             sp_tex_tile_cache_validate_texture( tc );
208             /*
209             _debug_printf("INV %d %d\n", tc->timestamp, spt->timestamp);
210             */
211             tc->timestamp = spt->timestamp;
212          }
213       }
214    }
215
216    for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
217       struct softpipe_tex_tile_cache *tc = softpipe->vertex_tex_cache[i];
218
219       if (tc->texture) {
220          struct softpipe_resource *spt = softpipe_resource(tc->texture);
221
222          if (spt->timestamp != tc->timestamp) {
223             sp_tex_tile_cache_validate_texture(tc);
224             tc->timestamp = spt->timestamp;
225          }
226       }
227    }
228 }
229
230
231 /* Hopefully this will remain quite simple, otherwise need to pull in
232  * something like the state tracker mechanism.
233  */
234 void softpipe_update_derived( struct softpipe_context *softpipe )
235 {
236    struct softpipe_screen *sp_screen = softpipe_screen(softpipe->pipe.screen);
237
238    /* Check for updated textures.
239     */
240    if (softpipe->tex_timestamp != sp_screen->timestamp) {
241       softpipe->tex_timestamp = sp_screen->timestamp;
242       softpipe->dirty |= SP_NEW_TEXTURE;
243    }
244       
245    if (softpipe->dirty & (SP_NEW_SAMPLER |
246                           SP_NEW_TEXTURE |
247                           SP_NEW_FS | 
248                           SP_NEW_VS))
249       update_tgsi_samplers( softpipe );
250
251    if (softpipe->dirty & (SP_NEW_RASTERIZER |
252                           SP_NEW_FS |
253                           SP_NEW_VS))
254       invalidate_vertex_layout( softpipe );
255
256    if (softpipe->dirty & (SP_NEW_SCISSOR |
257                           SP_NEW_RASTERIZER |
258                           SP_NEW_FRAMEBUFFER))
259       compute_cliprect(softpipe);
260
261    if (softpipe->dirty & (SP_NEW_BLEND |
262                           SP_NEW_DEPTH_STENCIL_ALPHA |
263                           SP_NEW_FRAMEBUFFER |
264                           SP_NEW_FS))
265       sp_build_quad_pipeline(softpipe);
266
267    softpipe->dirty = 0;
268 }