svga: adjust triangle rasterization offset
[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 /*
97 * Rebind rendertargets.
98 *
99 * Similar to emit_framebuffer, but without any state checking/update.
100 *
101 * Called at the beginning of every new command buffer to ensure that
102 * non-dirty rendertargets are properly paged-in.
103 */
104 enum pipe_error
105 svga_reemit_framebuffer_bindings(struct svga_context *svga)
106 {
107 struct pipe_framebuffer_state *hw = &svga->state.hw_clear.framebuffer;
108 unsigned i;
109 enum pipe_error ret;
110
111 for (i = 0; i < MIN2(PIPE_MAX_COLOR_BUFS, 8); ++i) {
112 if (hw->cbufs[i]) {
113 ret = SVGA3D_SetRenderTarget(svga->swc, SVGA3D_RT_COLOR0 + i, hw->cbufs[i]);
114 if (ret != PIPE_OK) {
115 return ret;
116 }
117 }
118 }
119
120 if (hw->zsbuf) {
121 ret = SVGA3D_SetRenderTarget(svga->swc, SVGA3D_RT_DEPTH, hw->zsbuf);
122 if (ret != PIPE_OK) {
123 return ret;
124 }
125
126 if (hw->zsbuf &&
127 hw->zsbuf->format == PIPE_FORMAT_S8_USCALED_Z24_UNORM) {
128 ret = SVGA3D_SetRenderTarget(svga->swc, SVGA3D_RT_STENCIL, hw->zsbuf);
129 if (ret != PIPE_OK) {
130 return ret;
131 }
132 }
133 else {
134 ret = SVGA3D_SetRenderTarget(svga->swc, SVGA3D_RT_STENCIL, NULL);
135 if (ret != PIPE_OK) {
136 return ret;
137 }
138 }
139 }
140
141 return PIPE_OK;
142 }
143
144
145 struct svga_tracked_state svga_hw_framebuffer =
146 {
147 "hw framebuffer state",
148 SVGA_NEW_FRAME_BUFFER |
149 SVGA_NEW_COMMAND_BUFFER,
150 emit_framebuffer
151 };
152
153
154
155
156 /***********************************************************************
157 */
158
159 static int emit_viewport( struct svga_context *svga,
160 unsigned dirty )
161 {
162 const struct pipe_viewport_state *viewport = &svga->curr.viewport;
163 struct svga_prescale prescale;
164 SVGA3dRect rect;
165 /* Not sure if this state is relevant with POSITIONT. Probably
166 * not, but setting to 0,1 avoids some state pingponging.
167 */
168 float range_min = 0.0;
169 float range_max = 1.0;
170 float flip = -1.0;
171 boolean degenerate = FALSE;
172 enum pipe_error ret;
173
174 float fb_width = svga->curr.framebuffer.width;
175 float fb_height = svga->curr.framebuffer.height;
176
177 float fx = viewport->scale[0] * -1.0 + viewport->translate[0];
178 float fy = flip * viewport->scale[1] * -1.0 + viewport->translate[1];
179 float fw = viewport->scale[0] * 2;
180 float fh = flip * viewport->scale[1] * 2;
181
182 memset( &prescale, 0, sizeof(prescale) );
183
184 /* Examine gallium viewport transformation and produce a screen
185 * rectangle and possibly vertex shader pre-transformation to
186 * get the same results.
187 */
188
189 SVGA_DBG(DEBUG_VIEWPORT,
190 "\ninitial %f,%f %fx%f\n",
191 fx,
192 fy,
193 fw,
194 fh);
195
196 prescale.scale[0] = 1.0;
197 prescale.scale[1] = 1.0;
198 prescale.scale[2] = 1.0;
199 prescale.scale[3] = 1.0;
200 prescale.translate[0] = 0;
201 prescale.translate[1] = 0;
202 prescale.translate[2] = 0;
203 prescale.translate[3] = 0;
204 prescale.enabled = TRUE;
205
206
207
208 if (fw < 0) {
209 prescale.scale[0] *= -1.0;
210 prescale.translate[0] += -fw;
211 fw = -fw;
212 fx = viewport->scale[0] * 1.0 + viewport->translate[0];
213 }
214
215 if (fh < 0) {
216 prescale.scale[1] *= -1.0;
217 prescale.translate[1] += -fh;
218 fh = -fh;
219 fy = flip * viewport->scale[1] * 1.0 + viewport->translate[1];
220 }
221
222 if (fx < 0) {
223 prescale.translate[0] += fx;
224 prescale.scale[0] *= fw / (fw + fx);
225 fw += fx;
226 fx = 0;
227 }
228
229 if (fy < 0) {
230 prescale.translate[1] += fy;
231 prescale.scale[1] *= fh / (fh + fy);
232 fh += fy;
233 fy = 0;
234 }
235
236 if (fx + fw > fb_width) {
237 prescale.scale[0] *= fw / (fb_width - fx);
238 prescale.translate[0] -= fx * (fw / (fb_width - fx));
239 prescale.translate[0] += fx;
240 fw = fb_width - fx;
241
242 }
243
244 if (fy + fh > fb_height) {
245 prescale.scale[1] *= fh / (fb_height - fy);
246 prescale.translate[1] -= fy * (fh / (fb_height - fy));
247 prescale.translate[1] += fy;
248 fh = fb_height - fy;
249 }
250
251 if (fw < 0 || fh < 0) {
252 fw = fh = fx = fy = 0;
253 degenerate = TRUE;
254 goto out;
255 }
256
257
258 /* D3D viewport is integer space. Convert fx,fy,etc. to
259 * integers.
260 *
261 * TODO: adjust pretranslate correct for any subpixel error
262 * introduced converting to integers.
263 */
264 rect.x = fx;
265 rect.y = fy;
266 rect.w = fw;
267 rect.h = fh;
268
269 SVGA_DBG(DEBUG_VIEWPORT,
270 "viewport error %f,%f %fx%f\n",
271 fabs((float)rect.x - fx),
272 fabs((float)rect.y - fy),
273 fabs((float)rect.w - fw),
274 fabs((float)rect.h - fh));
275
276 SVGA_DBG(DEBUG_VIEWPORT,
277 "viewport %d,%d %dx%d\n",
278 rect.x,
279 rect.y,
280 rect.w,
281 rect.h);
282
283
284 /* Finally, to get GL rasterization rules, need to tweak the
285 * screen-space coordinates slightly relative to D3D which is
286 * what hardware implements natively.
287 */
288 if (svga->curr.rast->templ.gl_rasterization_rules) {
289 float adjust_x = 0.0;
290 float adjust_y = 0.0;
291
292 switch (svga->curr.reduced_prim) {
293 case PIPE_PRIM_LINES:
294 adjust_x = -0.5;
295 adjust_y = 0;
296 break;
297 case PIPE_PRIM_POINTS:
298 case PIPE_PRIM_TRIANGLES:
299 adjust_x = -0.25;
300 adjust_y = -0.5;
301 break;
302 }
303
304 prescale.translate[0] += adjust_x;
305 prescale.translate[1] += adjust_y;
306 prescale.translate[2] = 0.5; /* D3D clip space */
307 prescale.scale[2] = 0.5; /* D3D clip space */
308 }
309
310
311 range_min = viewport->scale[2] * -1.0 + viewport->translate[2];
312 range_max = viewport->scale[2] * 1.0 + viewport->translate[2];
313
314 /* D3D (and by implication SVGA) doesn't like dealing with zmax
315 * less than zmin. Detect that case, flip the depth range and
316 * invert our z-scale factor to achieve the same effect.
317 */
318 if (range_min > range_max) {
319 float range_tmp;
320 range_tmp = range_min;
321 range_min = range_max;
322 range_max = range_tmp;
323 prescale.scale[2] = -prescale.scale[2];
324 }
325
326 if (prescale.enabled) {
327 float H[2];
328 float J[2];
329 int i;
330
331 SVGA_DBG(DEBUG_VIEWPORT,
332 "prescale %f,%f %fx%f\n",
333 prescale.translate[0],
334 prescale.translate[1],
335 prescale.scale[0],
336 prescale.scale[1]);
337
338 H[0] = (float)rect.w / 2.0;
339 H[1] = -(float)rect.h / 2.0;
340 J[0] = (float)rect.x + (float)rect.w / 2.0;
341 J[1] = (float)rect.y + (float)rect.h / 2.0;
342
343 SVGA_DBG(DEBUG_VIEWPORT,
344 "H %f,%f\n"
345 "J %fx%f\n",
346 H[0],
347 H[1],
348 J[0],
349 J[1]);
350
351 /* Adjust prescale to take into account the fact that it is
352 * going to be applied prior to the perspective divide and
353 * viewport transformation.
354 *
355 * Vwin = H(Vc/Vc.w) + J
356 *
357 * We want to tweak Vwin with scale and translation from above,
358 * as in:
359 *
360 * Vwin' = S Vwin + T
361 *
362 * But we can only modify the values at Vc. Plugging all the
363 * above together, and rearranging, eventually we get:
364 *
365 * Vwin' = H(Vc'/Vc'.w) + J
366 * where:
367 * Vc' = SVc + KVc.w
368 * K = (T + (S-1)J) / H
369 *
370 * Overwrite prescale.translate with values for K:
371 */
372 for (i = 0; i < 2; i++) {
373 prescale.translate[i] = ((prescale.translate[i] +
374 (prescale.scale[i] - 1.0) * J[i]) / H[i]);
375 }
376
377 SVGA_DBG(DEBUG_VIEWPORT,
378 "clipspace %f,%f %fx%f\n",
379 prescale.translate[0],
380 prescale.translate[1],
381 prescale.scale[0],
382 prescale.scale[1]);
383 }
384
385 out:
386 if (degenerate) {
387 rect.x = 0;
388 rect.y = 0;
389 rect.w = 1;
390 rect.h = 1;
391 prescale.enabled = FALSE;
392 }
393
394 if (memcmp(&rect, &svga->state.hw_clear.viewport, sizeof(rect)) != 0) {
395 ret = SVGA3D_SetViewport(svga->swc, &rect);
396 if(ret != PIPE_OK)
397 return ret;
398
399 memcpy(&svga->state.hw_clear.viewport, &rect, sizeof(rect));
400 assert(sizeof(rect) == sizeof(svga->state.hw_clear.viewport));
401 }
402
403 if (svga->state.hw_clear.depthrange.zmin != range_min ||
404 svga->state.hw_clear.depthrange.zmax != range_max)
405 {
406 ret = SVGA3D_SetZRange(svga->swc, range_min, range_max );
407 if(ret != PIPE_OK)
408 return ret;
409
410 svga->state.hw_clear.depthrange.zmin = range_min;
411 svga->state.hw_clear.depthrange.zmax = range_max;
412 }
413
414 if (memcmp(&prescale, &svga->state.hw_clear.prescale, sizeof prescale) != 0) {
415 svga->dirty |= SVGA_NEW_PRESCALE;
416 svga->state.hw_clear.prescale = prescale;
417 }
418
419 return 0;
420 }
421
422
423 struct svga_tracked_state svga_hw_viewport =
424 {
425 "hw viewport state",
426 ( SVGA_NEW_FRAME_BUFFER |
427 SVGA_NEW_VIEWPORT |
428 SVGA_NEW_RAST |
429 SVGA_NEW_REDUCED_PRIMITIVE ),
430 emit_viewport
431 };
432
433
434 /***********************************************************************
435 * Scissor state
436 */
437 static int emit_scissor_rect( struct svga_context *svga,
438 unsigned dirty )
439 {
440 const struct pipe_scissor_state *scissor = &svga->curr.scissor;
441 SVGA3dRect rect;
442
443 rect.x = scissor->minx;
444 rect.y = scissor->miny;
445 rect.w = scissor->maxx - scissor->minx; /* + 1 ?? */
446 rect.h = scissor->maxy - scissor->miny; /* + 1 ?? */
447
448 return SVGA3D_SetScissorRect(svga->swc, &rect);
449 }
450
451
452 struct svga_tracked_state svga_hw_scissor =
453 {
454 "hw scissor state",
455 SVGA_NEW_SCISSOR,
456 emit_scissor_rect
457 };
458
459
460 /***********************************************************************
461 * Userclip state
462 */
463
464 static int emit_clip_planes( struct svga_context *svga,
465 unsigned dirty )
466 {
467 unsigned i;
468 enum pipe_error ret;
469
470 /* TODO: just emit directly from svga_set_clip_state()?
471 */
472 for (i = 0; i < svga->curr.clip.nr; i++) {
473 ret = SVGA3D_SetClipPlane( svga->swc,
474 i,
475 svga->curr.clip.ucp[i] );
476 if(ret != PIPE_OK)
477 return ret;
478 }
479
480 return 0;
481 }
482
483
484 struct svga_tracked_state svga_hw_clip_planes =
485 {
486 "hw viewport state",
487 SVGA_NEW_CLIP,
488 emit_clip_planes
489 };