Merge remote branch 'origin/master' into nv50-compiler
[mesa.git] / src / gallium / drivers / svga / svga_state_framebuffer.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "util/u_inlines.h"
27 #include "pipe/p_defines.h"
28 #include "util/u_math.h"
29
30 #include "svga_context.h"
31 #include "svga_state.h"
32 #include "svga_cmd.h"
33 #include "svga_debug.h"
34
35
36 /***********************************************************************
37 * Hardware state update
38 */
39
40
41 static int emit_framebuffer( struct svga_context *svga,
42 unsigned dirty )
43 {
44 const struct pipe_framebuffer_state *curr = &svga->curr.framebuffer;
45 struct pipe_framebuffer_state *hw = &svga->state.hw_clear.framebuffer;
46 boolean reemit = !!(dirty & SVGA_NEW_COMMAND_BUFFER);
47 unsigned i;
48 enum pipe_error ret;
49
50 /*
51 * We need to reemit non-null surface bindings, even when they are not
52 * dirty, to ensure that the resources are paged in.
53 */
54
55 for(i = 0; i < PIPE_MAX_COLOR_BUFS; ++i) {
56 if (curr->cbufs[i] != hw->cbufs[i] ||
57 (reemit && hw->cbufs[i])) {
58 if (svga->curr.nr_fbs++ > 8)
59 return PIPE_ERROR_OUT_OF_MEMORY;
60
61 ret = SVGA3D_SetRenderTarget(svga->swc, SVGA3D_RT_COLOR0 + i, curr->cbufs[i]);
62 if (ret != PIPE_OK)
63 return ret;
64
65 pipe_surface_reference(&hw->cbufs[i], curr->cbufs[i]);
66 }
67 }
68
69
70 if (curr->zsbuf != hw->zsbuf ||
71 (reemit && hw->zsbuf)) {
72 ret = SVGA3D_SetRenderTarget(svga->swc, SVGA3D_RT_DEPTH, curr->zsbuf);
73 if (ret != PIPE_OK)
74 return ret;
75
76 if (curr->zsbuf &&
77 curr->zsbuf->format == PIPE_FORMAT_S8_USCALED_Z24_UNORM) {
78 ret = SVGA3D_SetRenderTarget(svga->swc, SVGA3D_RT_STENCIL, curr->zsbuf);
79 if (ret != PIPE_OK)
80 return ret;
81 }
82 else {
83 ret = SVGA3D_SetRenderTarget(svga->swc, SVGA3D_RT_STENCIL, NULL);
84 if (ret != PIPE_OK)
85 return ret;
86 }
87
88 pipe_surface_reference(&hw->zsbuf, curr->zsbuf);
89 }
90
91
92 return 0;
93 }
94
95
96 struct svga_tracked_state svga_hw_framebuffer =
97 {
98 "hw framebuffer state",
99 SVGA_NEW_FRAME_BUFFER |
100 SVGA_NEW_COMMAND_BUFFER,
101 emit_framebuffer
102 };
103
104
105
106
107 /***********************************************************************
108 */
109
110 static int emit_viewport( struct svga_context *svga,
111 unsigned dirty )
112 {
113 const struct pipe_viewport_state *viewport = &svga->curr.viewport;
114 struct svga_prescale prescale;
115 SVGA3dRect rect;
116 /* Not sure if this state is relevant with POSITIONT. Probably
117 * not, but setting to 0,1 avoids some state pingponging.
118 */
119 float range_min = 0.0;
120 float range_max = 1.0;
121 float flip = -1.0;
122 boolean degenerate = FALSE;
123 enum pipe_error ret;
124
125 float fb_width = svga->curr.framebuffer.width;
126 float fb_height = svga->curr.framebuffer.height;
127
128 float fx = viewport->scale[0] * -1.0 + viewport->translate[0];
129 float fy = flip * viewport->scale[1] * -1.0 + viewport->translate[1];
130 float fw = viewport->scale[0] * 2;
131 float fh = flip * viewport->scale[1] * 2;
132
133 memset( &prescale, 0, sizeof(prescale) );
134
135 /* Examine gallium viewport transformation and produce a screen
136 * rectangle and possibly vertex shader pre-transformation to
137 * get the same results.
138 */
139
140 SVGA_DBG(DEBUG_VIEWPORT,
141 "\ninitial %f,%f %fx%f\n",
142 fx,
143 fy,
144 fw,
145 fh);
146
147 prescale.scale[0] = 1.0;
148 prescale.scale[1] = 1.0;
149 prescale.scale[2] = 1.0;
150 prescale.scale[3] = 1.0;
151 prescale.translate[0] = 0;
152 prescale.translate[1] = 0;
153 prescale.translate[2] = 0;
154 prescale.translate[3] = 0;
155 prescale.enabled = TRUE;
156
157
158
159 if (fw < 0) {
160 prescale.scale[0] *= -1.0;
161 prescale.translate[0] += -fw;
162 fw = -fw;
163 fx = viewport->scale[0] * 1.0 + viewport->translate[0];
164 }
165
166 if (fh < 0) {
167 prescale.scale[1] *= -1.0;
168 prescale.translate[1] += -fh;
169 fh = -fh;
170 fy = flip * viewport->scale[1] * 1.0 + viewport->translate[1];
171 }
172
173 if (fx < 0) {
174 prescale.translate[0] += fx;
175 prescale.scale[0] *= fw / (fw + fx);
176 fw += fx;
177 fx = 0;
178 }
179
180 if (fy < 0) {
181 prescale.translate[1] += fy;
182 prescale.scale[1] *= fh / (fh + fy);
183 fh += fy;
184 fy = 0;
185 }
186
187 if (fx + fw > fb_width) {
188 prescale.scale[0] *= fw / (fb_width - fx);
189 prescale.translate[0] -= fx * (fw / (fb_width - fx));
190 prescale.translate[0] += fx;
191 fw = fb_width - fx;
192
193 }
194
195 if (fy + fh > fb_height) {
196 prescale.scale[1] *= fh / (fb_height - fy);
197 prescale.translate[1] -= fy * (fh / (fb_height - fy));
198 prescale.translate[1] += fy;
199 fh = fb_height - fy;
200 }
201
202 if (fw < 0 || fh < 0) {
203 fw = fh = fx = fy = 0;
204 degenerate = TRUE;
205 goto out;
206 }
207
208
209 /* D3D viewport is integer space. Convert fx,fy,etc. to
210 * integers.
211 *
212 * TODO: adjust pretranslate correct for any subpixel error
213 * introduced converting to integers.
214 */
215 rect.x = fx;
216 rect.y = fy;
217 rect.w = fw;
218 rect.h = fh;
219
220 SVGA_DBG(DEBUG_VIEWPORT,
221 "viewport error %f,%f %fx%f\n",
222 fabs((float)rect.x - fx),
223 fabs((float)rect.y - fy),
224 fabs((float)rect.w - fw),
225 fabs((float)rect.h - fh));
226
227 SVGA_DBG(DEBUG_VIEWPORT,
228 "viewport %d,%d %dx%d\n",
229 rect.x,
230 rect.y,
231 rect.w,
232 rect.h);
233
234
235 /* Finally, to get GL rasterization rules, need to tweak the
236 * screen-space coordinates slightly relative to D3D which is
237 * what hardware implements natively.
238 */
239 if (svga->curr.rast->templ.gl_rasterization_rules) {
240 float adjust_x = 0.0;
241 float adjust_y = 0.0;
242
243 switch (svga->curr.reduced_prim) {
244 case PIPE_PRIM_LINES:
245 adjust_x = -0.5;
246 adjust_y = 0;
247 break;
248 case PIPE_PRIM_POINTS:
249 case PIPE_PRIM_TRIANGLES:
250 adjust_x = -0.375;
251 adjust_y = -0.5;
252 break;
253 }
254
255 prescale.translate[0] += adjust_x;
256 prescale.translate[1] += adjust_y;
257 prescale.translate[2] = 0.5; /* D3D clip space */
258 prescale.scale[2] = 0.5; /* D3D clip space */
259 }
260
261
262 range_min = viewport->scale[2] * -1.0 + viewport->translate[2];
263 range_max = viewport->scale[2] * 1.0 + viewport->translate[2];
264
265 /* D3D (and by implication SVGA) doesn't like dealing with zmax
266 * less than zmin. Detect that case, flip the depth range and
267 * invert our z-scale factor to achieve the same effect.
268 */
269 if (range_min > range_max) {
270 float range_tmp;
271 range_tmp = range_min;
272 range_min = range_max;
273 range_max = range_tmp;
274 prescale.scale[2] = -prescale.scale[2];
275 }
276
277 if (prescale.enabled) {
278 float H[2];
279 float J[2];
280 int i;
281
282 SVGA_DBG(DEBUG_VIEWPORT,
283 "prescale %f,%f %fx%f\n",
284 prescale.translate[0],
285 prescale.translate[1],
286 prescale.scale[0],
287 prescale.scale[1]);
288
289 H[0] = (float)rect.w / 2.0;
290 H[1] = -(float)rect.h / 2.0;
291 J[0] = (float)rect.x + (float)rect.w / 2.0;
292 J[1] = (float)rect.y + (float)rect.h / 2.0;
293
294 SVGA_DBG(DEBUG_VIEWPORT,
295 "H %f,%f\n"
296 "J %fx%f\n",
297 H[0],
298 H[1],
299 J[0],
300 J[1]);
301
302 /* Adjust prescale to take into account the fact that it is
303 * going to be applied prior to the perspective divide and
304 * viewport transformation.
305 *
306 * Vwin = H(Vc/Vc.w) + J
307 *
308 * We want to tweak Vwin with scale and translation from above,
309 * as in:
310 *
311 * Vwin' = S Vwin + T
312 *
313 * But we can only modify the values at Vc. Plugging all the
314 * above together, and rearranging, eventually we get:
315 *
316 * Vwin' = H(Vc'/Vc'.w) + J
317 * where:
318 * Vc' = SVc + KVc.w
319 * K = (T + (S-1)J) / H
320 *
321 * Overwrite prescale.translate with values for K:
322 */
323 for (i = 0; i < 2; i++) {
324 prescale.translate[i] = ((prescale.translate[i] +
325 (prescale.scale[i] - 1.0) * J[i]) / H[i]);
326 }
327
328 SVGA_DBG(DEBUG_VIEWPORT,
329 "clipspace %f,%f %fx%f\n",
330 prescale.translate[0],
331 prescale.translate[1],
332 prescale.scale[0],
333 prescale.scale[1]);
334 }
335
336 out:
337 if (degenerate) {
338 rect.x = 0;
339 rect.y = 0;
340 rect.w = 1;
341 rect.h = 1;
342 prescale.enabled = FALSE;
343 }
344
345 if (memcmp(&rect, &svga->state.hw_clear.viewport, sizeof(rect)) != 0) {
346 ret = SVGA3D_SetViewport(svga->swc, &rect);
347 if(ret != PIPE_OK)
348 return ret;
349
350 memcpy(&svga->state.hw_clear.viewport, &rect, sizeof(rect));
351 assert(sizeof(rect) == sizeof(svga->state.hw_clear.viewport));
352 }
353
354 if (svga->state.hw_clear.depthrange.zmin != range_min ||
355 svga->state.hw_clear.depthrange.zmax != range_max)
356 {
357 ret = SVGA3D_SetZRange(svga->swc, range_min, range_max );
358 if(ret != PIPE_OK)
359 return ret;
360
361 svga->state.hw_clear.depthrange.zmin = range_min;
362 svga->state.hw_clear.depthrange.zmax = range_max;
363 }
364
365 if (memcmp(&prescale, &svga->state.hw_clear.prescale, sizeof prescale) != 0) {
366 svga->dirty |= SVGA_NEW_PRESCALE;
367 svga->state.hw_clear.prescale = prescale;
368 }
369
370 return 0;
371 }
372
373
374 struct svga_tracked_state svga_hw_viewport =
375 {
376 "hw viewport state",
377 ( SVGA_NEW_FRAME_BUFFER |
378 SVGA_NEW_VIEWPORT |
379 SVGA_NEW_RAST |
380 SVGA_NEW_REDUCED_PRIMITIVE ),
381 emit_viewport
382 };
383
384
385 /***********************************************************************
386 * Scissor state
387 */
388 static int emit_scissor_rect( struct svga_context *svga,
389 unsigned dirty )
390 {
391 const struct pipe_scissor_state *scissor = &svga->curr.scissor;
392 SVGA3dRect rect;
393
394 rect.x = scissor->minx;
395 rect.y = scissor->miny;
396 rect.w = scissor->maxx - scissor->minx; /* + 1 ?? */
397 rect.h = scissor->maxy - scissor->miny; /* + 1 ?? */
398
399 return SVGA3D_SetScissorRect(svga->swc, &rect);
400 }
401
402
403 struct svga_tracked_state svga_hw_scissor =
404 {
405 "hw scissor state",
406 SVGA_NEW_SCISSOR,
407 emit_scissor_rect
408 };
409
410
411 /***********************************************************************
412 * Userclip state
413 */
414
415 static int emit_clip_planes( struct svga_context *svga,
416 unsigned dirty )
417 {
418 unsigned i;
419 enum pipe_error ret;
420
421 /* TODO: just emit directly from svga_set_clip_state()?
422 */
423 for (i = 0; i < svga->curr.clip.nr; i++) {
424 ret = SVGA3D_SetClipPlane( svga->swc,
425 i,
426 svga->curr.clip.ucp[i] );
427 if(ret != PIPE_OK)
428 return ret;
429 }
430
431 return 0;
432 }
433
434
435 struct svga_tracked_state svga_hw_clip_planes =
436 {
437 "hw viewport state",
438 SVGA_NEW_CLIP,
439 emit_clip_planes
440 };