llvmpipe: import experimental softpipe rasterizer code, wip binning code
[mesa.git] / src / gallium / drivers / llvmpipe / lp_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 "draw/draw_private.h"
34 #include "lp_context.h"
35 #include "lp_screen.h"
36 #include "lp_tex_cache.h"
37 #include "lp_state.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 llvmpipe_context *llvmpipe)
47 {
48 llvmpipe->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 llvmpipe_get_vertex_info(struct llvmpipe_context *llvmpipe)
62 {
63 struct vertex_info *vinfo = &llvmpipe->vertex_info;
64
65 if (vinfo->num_attribs == 0) {
66 /* compute vertex layout now */
67 const struct lp_fragment_shader *lpfs = llvmpipe->fs;
68 const enum interp_mode colorInterp
69 = llvmpipe->rasterizer->flatshade ? INTERP_CONSTANT : INTERP_LINEAR;
70 struct vertex_info *vinfo_vbuf = &llvmpipe->vertex_info_vbuf;
71 const uint num = draw_num_vs_outputs(llvmpipe->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 < lpfs->info.num_inputs; i++) {
90 int src;
91 enum interp_mode interp;
92
93 switch (lpfs->info.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 default:
104 assert(0);
105 interp = INTERP_LINEAR;
106 }
107
108 switch (lpfs->info.input_semantic_name[i]) {
109 case TGSI_SEMANTIC_POSITION:
110 src = draw_find_vs_output(llvmpipe->draw,
111 TGSI_SEMANTIC_POSITION, 0);
112 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_POS, src);
113 break;
114
115 case TGSI_SEMANTIC_COLOR:
116 src = draw_find_vs_output(llvmpipe->draw, TGSI_SEMANTIC_COLOR,
117 lpfs->info.input_semantic_index[i]);
118 draw_emit_vertex_attr(vinfo, EMIT_4F, colorInterp, src);
119 break;
120
121 case TGSI_SEMANTIC_FOG:
122 src = draw_find_vs_output(llvmpipe->draw, TGSI_SEMANTIC_FOG, 0);
123 draw_emit_vertex_attr(vinfo, EMIT_4F, interp, src);
124 break;
125
126 case TGSI_SEMANTIC_GENERIC:
127 case TGSI_SEMANTIC_FACE:
128 /* this includes texcoords and varying vars */
129 src = draw_find_vs_output(llvmpipe->draw, TGSI_SEMANTIC_GENERIC,
130 lpfs->info.input_semantic_index[i]);
131 draw_emit_vertex_attr(vinfo, EMIT_4F, interp, src);
132 break;
133
134 default:
135 assert(0);
136 }
137 }
138
139 llvmpipe->psize_slot = draw_find_vs_output(llvmpipe->draw,
140 TGSI_SEMANTIC_PSIZE, 0);
141 if (llvmpipe->psize_slot > 0) {
142 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT,
143 llvmpipe->psize_slot);
144 }
145
146 draw_compute_vertex_size(vinfo);
147 }
148
149 return vinfo;
150 }
151
152
153 /**
154 * Called from vbuf module.
155 *
156 * Note that there's actually two different vertex layouts in llvmpipe.
157 *
158 * The normal one is computed in llvmpipe_get_vertex_info() above and is
159 * used by the point/line/tri "setup" code.
160 *
161 * The other one (this one) is only used by the vbuf module (which is
162 * not normally used by default but used in testing). For the vbuf module,
163 * we basically want to pass-through the draw module's vertex layout as-is.
164 * When the llvmpipe vbuf code begins drawing, the normal vertex layout
165 * will come into play again.
166 */
167 struct vertex_info *
168 llvmpipe_get_vbuf_vertex_info(struct llvmpipe_context *llvmpipe)
169 {
170 (void) llvmpipe_get_vertex_info(llvmpipe);
171 return &llvmpipe->vertex_info_vbuf;
172 }
173
174
175 /**
176 * Recompute cliprect from scissor bounds, scissor enable and surface size.
177 */
178 static void
179 compute_cliprect(struct llvmpipe_context *lp)
180 {
181 /* LP_NEW_FRAMEBUFFER
182 */
183 uint surfWidth = lp->framebuffer.width;
184 uint surfHeight = lp->framebuffer.height;
185
186 /* LP_NEW_RASTERIZER
187 */
188 if (lp->rasterizer->scissor) {
189
190 /* LP_NEW_SCISSOR
191 *
192 * clip to scissor rect:
193 */
194 lp->cliprect.minx = MAX2(lp->scissor.minx, 0);
195 lp->cliprect.miny = MAX2(lp->scissor.miny, 0);
196 lp->cliprect.maxx = MIN2(lp->scissor.maxx, surfWidth);
197 lp->cliprect.maxy = MIN2(lp->scissor.maxy, surfHeight);
198 }
199 else {
200 /* clip to surface bounds */
201 lp->cliprect.minx = 0;
202 lp->cliprect.miny = 0;
203 lp->cliprect.maxx = surfWidth;
204 lp->cliprect.maxy = surfHeight;
205 }
206 }
207
208
209 static void
210 update_tgsi_samplers( struct llvmpipe_context *llvmpipe )
211 {
212 unsigned i;
213
214 /* vertex shader samplers */
215 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
216 llvmpipe->tgsi.vert_samplers[i].sampler = llvmpipe->sampler[i];
217 llvmpipe->tgsi.vert_samplers[i].texture = llvmpipe->texture[i];
218 llvmpipe->tgsi.frag_samplers[i].base.get_samples = lp_get_samples;
219 }
220
221 /* fragment shader samplers */
222 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
223 llvmpipe->tgsi.frag_samplers[i].sampler = llvmpipe->sampler[i];
224 llvmpipe->tgsi.frag_samplers[i].texture = llvmpipe->texture[i];
225 llvmpipe->tgsi.frag_samplers[i].base.get_samples = lp_get_samples;
226 }
227
228 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
229 lp_tex_tile_cache_validate_texture( llvmpipe->tex_cache[i] );
230 }
231
232 llvmpipe->jit_context.samplers = (struct tgsi_sampler **)llvmpipe->tgsi.frag_samplers_list;
233 }
234
235 /* Hopefully this will remain quite simple, otherwise need to pull in
236 * something like the state tracker mechanism.
237 */
238 void llvmpipe_update_derived( struct llvmpipe_context *llvmpipe )
239 {
240 struct llvmpipe_screen *lp_screen = llvmpipe_screen(llvmpipe->pipe.screen);
241
242 /* Check for updated textures.
243 */
244 if (llvmpipe->tex_timestamp != lp_screen->timestamp) {
245 llvmpipe->tex_timestamp = lp_screen->timestamp;
246 llvmpipe->dirty |= LP_NEW_TEXTURE;
247 }
248
249 if (llvmpipe->dirty & (LP_NEW_SAMPLER |
250 LP_NEW_TEXTURE))
251 update_tgsi_samplers( llvmpipe );
252
253 if (llvmpipe->dirty & (LP_NEW_RASTERIZER |
254 LP_NEW_FS |
255 LP_NEW_VS))
256 invalidate_vertex_layout( llvmpipe );
257
258 if (llvmpipe->dirty & (LP_NEW_SCISSOR |
259 LP_NEW_RASTERIZER |
260 LP_NEW_FRAMEBUFFER))
261 compute_cliprect(llvmpipe);
262
263 if (llvmpipe->dirty & (LP_NEW_FS |
264 LP_NEW_BLEND |
265 LP_NEW_DEPTH_STENCIL_ALPHA |
266 LP_NEW_SAMPLER |
267 LP_NEW_TEXTURE))
268 llvmpipe_update_fs( llvmpipe );
269
270
271 llvmpipe->dirty = 0;
272 }