1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 **********************************************************/
26 #include "util/u_inlines.h"
27 #include "pipe/p_defines.h"
28 #include "util/u_math.h"
30 #include "svga_context.h"
31 #include "svga_state.h"
33 #include "svga_debug.h"
34 #include "svga_screen.h"
38 * flush our command buffer after the 8th distinct render target
40 * This helps improve the surface cache behaviour in the face of the
41 * large number of single-use render targets generated by EXA and the xorg
42 * state tracker. Without this we can reference hundreds of individual
43 * render targets from a command buffer, which leaves little scope for
44 * sharing or reuse of those targets.
46 #define MAX_RT_PER_BATCH 8
49 /***********************************************************************
50 * Hardware state update
54 static enum pipe_error
55 emit_framebuffer( struct svga_context
*svga
,
58 struct svga_screen
*svgascreen
= svga_screen(svga
->pipe
.screen
);
59 const struct pipe_framebuffer_state
*curr
= &svga
->curr
.framebuffer
;
60 struct pipe_framebuffer_state
*hw
= &svga
->state
.hw_clear
.framebuffer
;
61 boolean reemit
= svga
->rebind
.rendertargets
;
66 * We need to reemit non-null surface bindings, even when they are not
67 * dirty, to ensure that the resources are paged in.
70 for (i
= 0; i
< svgascreen
->max_color_buffers
; i
++) {
71 if (curr
->cbufs
[i
] != hw
->cbufs
[i
] ||
72 (reemit
&& hw
->cbufs
[i
])) {
73 if (svga
->curr
.nr_fbs
++ > MAX_RT_PER_BATCH
)
74 return PIPE_ERROR_OUT_OF_MEMORY
;
76 ret
= SVGA3D_SetRenderTarget(svga
->swc
, SVGA3D_RT_COLOR0
+ i
,
81 pipe_surface_reference(&hw
->cbufs
[i
], curr
->cbufs
[i
]);
85 if (curr
->zsbuf
!= hw
->zsbuf
||
86 (reemit
&& hw
->zsbuf
)) {
87 ret
= SVGA3D_SetRenderTarget(svga
->swc
, SVGA3D_RT_DEPTH
, curr
->zsbuf
);
92 curr
->zsbuf
->format
== PIPE_FORMAT_S8_UINT_Z24_UNORM
) {
93 ret
= SVGA3D_SetRenderTarget(svga
->swc
, SVGA3D_RT_STENCIL
,
99 ret
= SVGA3D_SetRenderTarget(svga
->swc
, SVGA3D_RT_STENCIL
, NULL
);
104 pipe_surface_reference(&hw
->zsbuf
, curr
->zsbuf
);
107 svga
->rebind
.rendertargets
= FALSE
;
114 * Rebind rendertargets.
116 * Similar to emit_framebuffer, but without any state checking/update.
118 * Called at the beginning of every new command buffer to ensure that
119 * non-dirty rendertargets are properly paged-in.
122 svga_reemit_framebuffer_bindings(struct svga_context
*svga
)
124 struct svga_screen
*svgascreen
= svga_screen(svga
->pipe
.screen
);
125 struct pipe_framebuffer_state
*hw
= &svga
->state
.hw_clear
.framebuffer
;
129 assert(svga
->rebind
.rendertargets
);
131 for (i
= 0; i
< svgascreen
->max_color_buffers
; i
++) {
133 ret
= SVGA3D_SetRenderTarget(svga
->swc
, SVGA3D_RT_COLOR0
+ i
,
135 if (ret
!= PIPE_OK
) {
142 ret
= SVGA3D_SetRenderTarget(svga
->swc
, SVGA3D_RT_DEPTH
, hw
->zsbuf
);
143 if (ret
!= PIPE_OK
) {
148 hw
->zsbuf
->format
== PIPE_FORMAT_S8_UINT_Z24_UNORM
) {
149 ret
= SVGA3D_SetRenderTarget(svga
->swc
, SVGA3D_RT_STENCIL
, hw
->zsbuf
);
150 if (ret
!= PIPE_OK
) {
155 ret
= SVGA3D_SetRenderTarget(svga
->swc
, SVGA3D_RT_STENCIL
, NULL
);
156 if (ret
!= PIPE_OK
) {
162 svga
->rebind
.rendertargets
= FALSE
;
168 struct svga_tracked_state svga_hw_framebuffer
=
170 "hw framebuffer state",
171 SVGA_NEW_FRAME_BUFFER
,
178 /***********************************************************************
181 static enum pipe_error
182 emit_viewport( struct svga_context
*svga
,
185 const struct pipe_viewport_state
*viewport
= &svga
->curr
.viewport
;
186 struct svga_prescale prescale
;
188 /* Not sure if this state is relevant with POSITIONT. Probably
189 * not, but setting to 0,1 avoids some state pingponging.
191 float range_min
= 0.0;
192 float range_max
= 1.0;
194 boolean degenerate
= FALSE
;
195 boolean invertY
= FALSE
;
198 float fb_width
= (float) svga
->curr
.framebuffer
.width
;
199 float fb_height
= (float) svga
->curr
.framebuffer
.height
;
201 float fx
= viewport
->scale
[0] * -1.0f
+ viewport
->translate
[0];
202 float fy
= flip
* viewport
->scale
[1] * -1.0f
+ viewport
->translate
[1];
203 float fw
= viewport
->scale
[0] * 2.0f
;
204 float fh
= flip
* viewport
->scale
[1] * 2.0f
;
206 memset( &prescale
, 0, sizeof(prescale
) );
208 /* Examine gallium viewport transformation and produce a screen
209 * rectangle and possibly vertex shader pre-transformation to
210 * get the same results.
213 SVGA_DBG(DEBUG_VIEWPORT
,
214 "\ninitial %f,%f %fx%f\n",
220 prescale
.scale
[0] = 1.0;
221 prescale
.scale
[1] = 1.0;
222 prescale
.scale
[2] = 1.0;
223 prescale
.scale
[3] = 1.0;
224 prescale
.translate
[0] = 0;
225 prescale
.translate
[1] = 0;
226 prescale
.translate
[2] = 0;
227 prescale
.translate
[3] = 0;
228 prescale
.enabled
= TRUE
;
231 prescale
.scale
[0] *= -1.0f
;
232 prescale
.translate
[0] += -fw
;
234 fx
= viewport
->scale
[0] * 1.0f
+ viewport
->translate
[0];
238 prescale
.translate
[1] = fh
- 1.0f
+ fy
* 2.0f
;
241 prescale
.scale
[1] = -1.0f
;
246 prescale
.translate
[0] += fx
;
247 prescale
.scale
[0] *= fw
/ (fw
+ fx
);
254 prescale
.translate
[1] -= fy
;
257 prescale
.translate
[1] += fy
;
259 prescale
.scale
[1] *= fh
/ (fh
+ fy
);
264 if (fx
+ fw
> fb_width
) {
265 prescale
.scale
[0] *= fw
/ (fb_width
- fx
);
266 prescale
.translate
[0] -= fx
* (fw
/ (fb_width
- fx
));
267 prescale
.translate
[0] += fx
;
271 if (fy
+ fh
> fb_height
) {
272 prescale
.scale
[1] *= fh
/ (fb_height
- fy
);
274 float in
= fb_height
- fy
; /* number of vp pixels inside view */
275 float out
= fy
+ fh
- fb_height
; /* number of vp pixels out of view */
276 prescale
.translate
[1] += fy
* out
/ in
;
279 prescale
.translate
[1] -= fy
* (fh
/ (fb_height
- fy
));
280 prescale
.translate
[1] += fy
;
285 if (fw
< 0 || fh
< 0) {
286 fw
= fh
= fx
= fy
= 0;
291 /* D3D viewport is integer space. Convert fx,fy,etc. to
294 * TODO: adjust pretranslate correct for any subpixel error
295 * introduced converting to integers.
297 rect
.x
= (uint32
) fx
;
298 rect
.y
= (uint32
) fy
;
299 rect
.w
= (uint32
) fw
;
300 rect
.h
= (uint32
) fh
;
302 SVGA_DBG(DEBUG_VIEWPORT
,
303 "viewport error %f,%f %fx%f\n",
304 fabs((float)rect
.x
- fx
),
305 fabs((float)rect
.y
- fy
),
306 fabs((float)rect
.w
- fw
),
307 fabs((float)rect
.h
- fh
));
309 SVGA_DBG(DEBUG_VIEWPORT
,
310 "viewport %d,%d %dx%d\n",
316 /* Finally, to get GL rasterization rules, need to tweak the
317 * screen-space coordinates slightly relative to D3D which is
318 * what hardware implements natively.
320 if (svga
->curr
.rast
->templ
.half_pixel_center
) {
321 float adjust_x
= 0.0;
322 float adjust_y
= 0.0;
324 switch (svga
->curr
.reduced_prim
) {
325 case PIPE_PRIM_POINTS
:
329 case PIPE_PRIM_LINES
:
333 case PIPE_PRIM_TRIANGLES
:
340 adjust_y
= -adjust_y
;
342 prescale
.translate
[0] += adjust_x
;
343 prescale
.translate
[1] += adjust_y
;
344 prescale
.translate
[2] = 0.5; /* D3D clip space */
345 prescale
.scale
[2] = 0.5; /* D3D clip space */
348 range_min
= viewport
->scale
[2] * -1.0f
+ viewport
->translate
[2];
349 range_max
= viewport
->scale
[2] * 1.0f
+ viewport
->translate
[2];
351 /* D3D (and by implication SVGA) doesn't like dealing with zmax
352 * less than zmin. Detect that case, flip the depth range and
353 * invert our z-scale factor to achieve the same effect.
355 if (range_min
> range_max
) {
357 range_tmp
= range_min
;
358 range_min
= range_max
;
359 range_max
= range_tmp
;
360 prescale
.scale
[2] = -prescale
.scale
[2];
363 if (prescale
.enabled
) {
368 SVGA_DBG(DEBUG_VIEWPORT
,
369 "prescale %f,%f %fx%f\n",
370 prescale
.translate
[0],
371 prescale
.translate
[1],
375 H
[0] = (float)rect
.w
/ 2.0f
;
376 H
[1] = -(float)rect
.h
/ 2.0f
;
377 J
[0] = (float)rect
.x
+ (float)rect
.w
/ 2.0f
;
378 J
[1] = (float)rect
.y
+ (float)rect
.h
/ 2.0f
;
380 SVGA_DBG(DEBUG_VIEWPORT
,
388 /* Adjust prescale to take into account the fact that it is
389 * going to be applied prior to the perspective divide and
390 * viewport transformation.
392 * Vwin = H(Vc/Vc.w) + J
394 * We want to tweak Vwin with scale and translation from above,
399 * But we can only modify the values at Vc. Plugging all the
400 * above together, and rearranging, eventually we get:
402 * Vwin' = H(Vc'/Vc'.w) + J
405 * K = (T + (S-1)J) / H
407 * Overwrite prescale.translate with values for K:
409 for (i
= 0; i
< 2; i
++) {
410 prescale
.translate
[i
] = ((prescale
.translate
[i
] +
411 (prescale
.scale
[i
] - 1.0f
) * J
[i
]) / H
[i
]);
414 SVGA_DBG(DEBUG_VIEWPORT
,
415 "clipspace %f,%f %fx%f\n",
416 prescale
.translate
[0],
417 prescale
.translate
[1],
428 prescale
.enabled
= FALSE
;
431 if (memcmp(&rect
, &svga
->state
.hw_clear
.viewport
, sizeof(rect
)) != 0) {
432 ret
= SVGA3D_SetViewport(svga
->swc
, &rect
);
436 memcpy(&svga
->state
.hw_clear
.viewport
, &rect
, sizeof(rect
));
437 assert(sizeof(rect
) == sizeof(svga
->state
.hw_clear
.viewport
));
440 if (svga
->state
.hw_clear
.depthrange
.zmin
!= range_min
||
441 svga
->state
.hw_clear
.depthrange
.zmax
!= range_max
) {
442 ret
= SVGA3D_SetZRange(svga
->swc
, range_min
, range_max
);
446 svga
->state
.hw_clear
.depthrange
.zmin
= range_min
;
447 svga
->state
.hw_clear
.depthrange
.zmax
= range_max
;
450 if (memcmp(&prescale
, &svga
->state
.hw_clear
.prescale
, sizeof prescale
) != 0) {
451 svga
->dirty
|= SVGA_NEW_PRESCALE
;
452 svga
->state
.hw_clear
.prescale
= prescale
;
459 struct svga_tracked_state svga_hw_viewport
=
462 ( SVGA_NEW_FRAME_BUFFER
|
465 SVGA_NEW_REDUCED_PRIMITIVE
),
470 /***********************************************************************
473 static enum pipe_error
474 emit_scissor_rect( struct svga_context
*svga
,
477 const struct pipe_scissor_state
*scissor
= &svga
->curr
.scissor
;
480 rect
.x
= scissor
->minx
;
481 rect
.y
= scissor
->miny
;
482 rect
.w
= scissor
->maxx
- scissor
->minx
; /* + 1 ?? */
483 rect
.h
= scissor
->maxy
- scissor
->miny
; /* + 1 ?? */
485 return SVGA3D_SetScissorRect(svga
->swc
, &rect
);
489 struct svga_tracked_state svga_hw_scissor
=
497 /***********************************************************************
501 static enum pipe_error
502 emit_clip_planes( struct svga_context
*svga
,
508 /* TODO: just emit directly from svga_set_clip_state()?
510 for (i
= 0; i
< SVGA3D_MAX_CLIP_PLANES
; i
++) {
511 /* need to express the plane in D3D-style coordinate space.
512 * GL coords get converted to D3D coords with the matrix:
517 * Apply that matrix to our plane equation, and invert Y.
519 float a
= svga
->curr
.clip
.ucp
[i
][0];
520 float b
= svga
->curr
.clip
.ucp
[i
][1];
521 float c
= svga
->curr
.clip
.ucp
[i
][2];
522 float d
= svga
->curr
.clip
.ucp
[i
][3];
530 ret
= SVGA3D_SetClipPlane(svga
->swc
, i
, plane
);
539 struct svga_tracked_state svga_hw_clip_planes
=