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