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"
37 * flush our command buffer after the 8th distinct render target
39 * This helps improve the surface cache behaviour in the face of the
40 * large number of single-use render targets generated by EXA and the xorg
41 * state tracker. Without this we can reference hundreds of individual
42 * render targets from a command buffer, which leaves little scope for
43 * sharing or reuse of those targets.
45 #define MAX_RT_PER_BATCH 8
48 /***********************************************************************
49 * Hardware state update
53 static enum pipe_error
54 emit_framebuffer( struct svga_context
*svga
,
57 const struct pipe_framebuffer_state
*curr
= &svga
->curr
.framebuffer
;
58 struct pipe_framebuffer_state
*hw
= &svga
->state
.hw_clear
.framebuffer
;
59 boolean reemit
= svga
->rebind
.rendertargets
;
64 * We need to reemit non-null surface bindings, even when they are not
65 * dirty, to ensure that the resources are paged in.
68 for(i
= 0; i
< PIPE_MAX_COLOR_BUFS
; ++i
) {
69 if (curr
->cbufs
[i
] != hw
->cbufs
[i
] ||
70 (reemit
&& hw
->cbufs
[i
])) {
71 if (svga
->curr
.nr_fbs
++ > MAX_RT_PER_BATCH
)
72 return PIPE_ERROR_OUT_OF_MEMORY
;
74 ret
= SVGA3D_SetRenderTarget(svga
->swc
, SVGA3D_RT_COLOR0
+ i
, curr
->cbufs
[i
]);
78 pipe_surface_reference(&hw
->cbufs
[i
], curr
->cbufs
[i
]);
83 if (curr
->zsbuf
!= hw
->zsbuf
||
84 (reemit
&& hw
->zsbuf
)) {
85 ret
= SVGA3D_SetRenderTarget(svga
->swc
, SVGA3D_RT_DEPTH
, curr
->zsbuf
);
90 curr
->zsbuf
->format
== PIPE_FORMAT_S8_UINT_Z24_UNORM
) {
91 ret
= SVGA3D_SetRenderTarget(svga
->swc
, SVGA3D_RT_STENCIL
, curr
->zsbuf
);
96 ret
= SVGA3D_SetRenderTarget(svga
->swc
, SVGA3D_RT_STENCIL
, NULL
);
101 pipe_surface_reference(&hw
->zsbuf
, curr
->zsbuf
);
104 svga
->rebind
.rendertargets
= FALSE
;
111 * Rebind rendertargets.
113 * Similar to emit_framebuffer, but without any state checking/update.
115 * Called at the beginning of every new command buffer to ensure that
116 * non-dirty rendertargets are properly paged-in.
119 svga_reemit_framebuffer_bindings(struct svga_context
*svga
)
121 struct pipe_framebuffer_state
*hw
= &svga
->state
.hw_clear
.framebuffer
;
125 assert(svga
->rebind
.rendertargets
);
127 for (i
= 0; i
< MIN2(PIPE_MAX_COLOR_BUFS
, 8); ++i
) {
129 ret
= SVGA3D_SetRenderTarget(svga
->swc
, SVGA3D_RT_COLOR0
+ i
, hw
->cbufs
[i
]);
130 if (ret
!= PIPE_OK
) {
137 ret
= SVGA3D_SetRenderTarget(svga
->swc
, SVGA3D_RT_DEPTH
, hw
->zsbuf
);
138 if (ret
!= PIPE_OK
) {
143 hw
->zsbuf
->format
== PIPE_FORMAT_S8_UINT_Z24_UNORM
) {
144 ret
= SVGA3D_SetRenderTarget(svga
->swc
, SVGA3D_RT_STENCIL
, hw
->zsbuf
);
145 if (ret
!= PIPE_OK
) {
150 ret
= SVGA3D_SetRenderTarget(svga
->swc
, SVGA3D_RT_STENCIL
, NULL
);
151 if (ret
!= PIPE_OK
) {
157 svga
->rebind
.rendertargets
= FALSE
;
163 struct svga_tracked_state svga_hw_framebuffer
=
165 "hw framebuffer state",
166 SVGA_NEW_FRAME_BUFFER
,
173 /***********************************************************************
176 static enum pipe_error
177 emit_viewport( struct svga_context
*svga
,
180 const struct pipe_viewport_state
*viewport
= &svga
->curr
.viewport
;
181 struct svga_prescale prescale
;
183 /* Not sure if this state is relevant with POSITIONT. Probably
184 * not, but setting to 0,1 avoids some state pingponging.
186 float range_min
= 0.0;
187 float range_max
= 1.0;
189 boolean degenerate
= FALSE
;
190 boolean invertY
= FALSE
;
193 float fb_width
= (float) svga
->curr
.framebuffer
.width
;
194 float fb_height
= (float) svga
->curr
.framebuffer
.height
;
196 float fx
= viewport
->scale
[0] * -1.0f
+ viewport
->translate
[0];
197 float fy
= flip
* viewport
->scale
[1] * -1.0f
+ viewport
->translate
[1];
198 float fw
= viewport
->scale
[0] * 2.0f
;
199 float fh
= flip
* viewport
->scale
[1] * 2.0f
;
201 memset( &prescale
, 0, sizeof(prescale
) );
203 /* Examine gallium viewport transformation and produce a screen
204 * rectangle and possibly vertex shader pre-transformation to
205 * get the same results.
208 SVGA_DBG(DEBUG_VIEWPORT
,
209 "\ninitial %f,%f %fx%f\n",
215 prescale
.scale
[0] = 1.0;
216 prescale
.scale
[1] = 1.0;
217 prescale
.scale
[2] = 1.0;
218 prescale
.scale
[3] = 1.0;
219 prescale
.translate
[0] = 0;
220 prescale
.translate
[1] = 0;
221 prescale
.translate
[2] = 0;
222 prescale
.translate
[3] = 0;
223 prescale
.enabled
= TRUE
;
228 prescale
.scale
[0] *= -1.0f
;
229 prescale
.translate
[0] += -fw
;
231 fx
= viewport
->scale
[0] * 1.0f
+ viewport
->translate
[0];
235 prescale
.translate
[1] = fh
- 1.0f
+ fy
* 2.0f
;
238 prescale
.scale
[1] = -1.0f
;
243 prescale
.translate
[0] += fx
;
244 prescale
.scale
[0] *= fw
/ (fw
+ fx
);
251 prescale
.translate
[1] -= fy
;
254 prescale
.translate
[1] += fy
;
256 prescale
.scale
[1] *= fh
/ (fh
+ fy
);
261 if (fx
+ fw
> fb_width
) {
262 prescale
.scale
[0] *= fw
/ (fb_width
- fx
);
263 prescale
.translate
[0] -= fx
* (fw
/ (fb_width
- fx
));
264 prescale
.translate
[0] += fx
;
269 if (fy
+ fh
> fb_height
) {
270 prescale
.scale
[1] *= fh
/ (fb_height
- fy
);
272 float in
= fb_height
- fy
; /* number of vp pixels inside view */
273 float out
= fy
+ fh
- fb_height
; /* number of vp pixels out of view */
274 prescale
.translate
[1] += fy
* out
/ in
;
277 prescale
.translate
[1] -= fy
* (fh
/ (fb_height
- fy
));
278 prescale
.translate
[1] += fy
;
283 if (fw
< 0 || fh
< 0) {
284 fw
= fh
= fx
= fy
= 0;
290 /* D3D viewport is integer space. Convert fx,fy,etc. to
293 * TODO: adjust pretranslate correct for any subpixel error
294 * introduced converting to integers.
296 rect
.x
= (uint32
) fx
;
297 rect
.y
= (uint32
) fy
;
298 rect
.w
= (uint32
) fw
;
299 rect
.h
= (uint32
) fh
;
301 SVGA_DBG(DEBUG_VIEWPORT
,
302 "viewport error %f,%f %fx%f\n",
303 fabs((float)rect
.x
- fx
),
304 fabs((float)rect
.y
- fy
),
305 fabs((float)rect
.w
- fw
),
306 fabs((float)rect
.h
- fh
));
308 SVGA_DBG(DEBUG_VIEWPORT
,
309 "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_LINES
:
329 case PIPE_PRIM_POINTS
:
330 case PIPE_PRIM_TRIANGLES
:
337 adjust_y
= -adjust_y
;
339 prescale
.translate
[0] += adjust_x
;
340 prescale
.translate
[1] += adjust_y
;
341 prescale
.translate
[2] = 0.5; /* D3D clip space */
342 prescale
.scale
[2] = 0.5; /* D3D clip space */
346 range_min
= viewport
->scale
[2] * -1.0f
+ viewport
->translate
[2];
347 range_max
= viewport
->scale
[2] * 1.0f
+ viewport
->translate
[2];
349 /* D3D (and by implication SVGA) doesn't like dealing with zmax
350 * less than zmin. Detect that case, flip the depth range and
351 * invert our z-scale factor to achieve the same effect.
353 if (range_min
> range_max
) {
355 range_tmp
= range_min
;
356 range_min
= range_max
;
357 range_max
= range_tmp
;
358 prescale
.scale
[2] = -prescale
.scale
[2];
361 if (prescale
.enabled
) {
366 SVGA_DBG(DEBUG_VIEWPORT
,
367 "prescale %f,%f %fx%f\n",
368 prescale
.translate
[0],
369 prescale
.translate
[1],
373 H
[0] = (float)rect
.w
/ 2.0f
;
374 H
[1] = -(float)rect
.h
/ 2.0f
;
375 J
[0] = (float)rect
.x
+ (float)rect
.w
/ 2.0f
;
376 J
[1] = (float)rect
.y
+ (float)rect
.h
/ 2.0f
;
378 SVGA_DBG(DEBUG_VIEWPORT
,
386 /* Adjust prescale to take into account the fact that it is
387 * going to be applied prior to the perspective divide and
388 * viewport transformation.
390 * Vwin = H(Vc/Vc.w) + J
392 * We want to tweak Vwin with scale and translation from above,
397 * But we can only modify the values at Vc. Plugging all the
398 * above together, and rearranging, eventually we get:
400 * Vwin' = H(Vc'/Vc'.w) + J
403 * K = (T + (S-1)J) / H
405 * Overwrite prescale.translate with values for K:
407 for (i
= 0; i
< 2; i
++) {
408 prescale
.translate
[i
] = ((prescale
.translate
[i
] +
409 (prescale
.scale
[i
] - 1.0f
) * J
[i
]) / H
[i
]);
412 SVGA_DBG(DEBUG_VIEWPORT
,
413 "clipspace %f,%f %fx%f\n",
414 prescale
.translate
[0],
415 prescale
.translate
[1],
426 prescale
.enabled
= FALSE
;
429 if (memcmp(&rect
, &svga
->state
.hw_clear
.viewport
, sizeof(rect
)) != 0) {
430 ret
= SVGA3D_SetViewport(svga
->swc
, &rect
);
434 memcpy(&svga
->state
.hw_clear
.viewport
, &rect
, sizeof(rect
));
435 assert(sizeof(rect
) == sizeof(svga
->state
.hw_clear
.viewport
));
438 if (svga
->state
.hw_clear
.depthrange
.zmin
!= range_min
||
439 svga
->state
.hw_clear
.depthrange
.zmax
!= range_max
)
441 ret
= SVGA3D_SetZRange(svga
->swc
, range_min
, range_max
);
445 svga
->state
.hw_clear
.depthrange
.zmin
= range_min
;
446 svga
->state
.hw_clear
.depthrange
.zmax
= range_max
;
449 if (memcmp(&prescale
, &svga
->state
.hw_clear
.prescale
, sizeof prescale
) != 0) {
450 svga
->dirty
|= SVGA_NEW_PRESCALE
;
451 svga
->state
.hw_clear
.prescale
= prescale
;
458 struct svga_tracked_state svga_hw_viewport
=
461 ( SVGA_NEW_FRAME_BUFFER
|
464 SVGA_NEW_REDUCED_PRIMITIVE
),
469 /***********************************************************************
472 static enum pipe_error
473 emit_scissor_rect( struct svga_context
*svga
,
476 const struct pipe_scissor_state
*scissor
= &svga
->curr
.scissor
;
479 rect
.x
= scissor
->minx
;
480 rect
.y
= scissor
->miny
;
481 rect
.w
= scissor
->maxx
- scissor
->minx
; /* + 1 ?? */
482 rect
.h
= scissor
->maxy
- scissor
->miny
; /* + 1 ?? */
484 return SVGA3D_SetScissorRect(svga
->swc
, &rect
);
488 struct svga_tracked_state svga_hw_scissor
=
496 /***********************************************************************
500 static enum pipe_error
501 emit_clip_planes( struct svga_context
*svga
,
507 /* TODO: just emit directly from svga_set_clip_state()?
509 for (i
= 0; i
< SVGA3D_MAX_CLIP_PLANES
; i
++) {
510 /* need to express the plane in D3D-style coordinate space.
511 * GL coords get converted to D3D coords with the matrix:
516 * Apply that matrix to our plane equation, and invert Y.
518 float a
= svga
->curr
.clip
.ucp
[i
][0];
519 float b
= svga
->curr
.clip
.ucp
[i
][1];
520 float c
= svga
->curr
.clip
.ucp
[i
][2];
521 float d
= svga
->curr
.clip
.ucp
[i
][3];
529 ret
= SVGA3D_SetClipPlane(svga
->swc
, i
, plane
);
538 struct svga_tracked_state svga_hw_clip_planes
=