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 #ifndef SVGA_CONTEXT_H
27 #define SVGA_CONTEXT_H
30 #include "pipe/p_context.h"
31 #include "pipe/p_defines.h"
32 #include "pipe/p_state.h"
34 #include "util/u_blitter.h"
35 #include "util/list.h"
37 #include "tgsi/tgsi_scan.h"
39 #include "svga_screen.h"
40 #include "svga_state.h"
41 #include "svga_winsys.h"
42 #include "svga_hw_reg.h"
43 #include "svga3d_shaderdefs.h"
46 /** Non-GPU queries for gallium HUD */
47 #define SVGA_QUERY_DRAW_CALLS (PIPE_QUERY_DRIVER_SPECIFIC + 0)
48 #define SVGA_QUERY_FALLBACKS (PIPE_QUERY_DRIVER_SPECIFIC + 1)
49 #define SVGA_QUERY_MEMORY_USED (PIPE_QUERY_DRIVER_SPECIFIC + 2)
50 #define SVGA_QUERY_MAX (PIPE_QUERY_DRIVER_SPECIFIC + 3)
53 * Maximum supported number of constant buffers per shader
55 #define SVGA_MAX_CONST_BUFS 14
58 * Maximum constant buffer size that can be set in the
59 * DXSetSingleConstantBuffer command is
60 * DX10 constant buffer element count * 4 4-bytes components
62 #define SVGA_MAX_CONST_BUF_SIZE (4096 * 4 * sizeof(int))
64 struct draw_vertex_shader
;
65 struct draw_fragment_shader
;
66 struct svga_shader_variant
;
71 struct svga_cache_context
;
72 struct svga_tracked_state
;
74 struct svga_blend_state
{
75 unsigned need_white_fragments
:1;
76 unsigned independent_blend_enable
:1;
77 unsigned alpha_to_coverage
:1;
78 unsigned blend_color_alpha
:1; /**< set blend color to alpha value */
80 /** Per-render target state */
89 boolean separate_alpha_blend_enable
;
90 uint8_t srcblend_alpha
;
91 uint8_t dstblend_alpha
;
92 uint8_t blendeq_alpha
;
93 } rt
[PIPE_MAX_COLOR_BUFS
];
95 SVGA3dBlendStateId id
; /**< vgpu10 */
98 struct svga_depth_stencil_state
{
101 unsigned zwriteenable
:1;
103 unsigned alphatestenable
:1;
104 unsigned alphafunc
:8;
114 /* SVGA3D has one ref/mask/writemask triple shared between front &
115 * back face stencil. We really need two:
117 unsigned stencil_mask
:8;
118 unsigned stencil_writemask
:8;
122 SVGA3dDepthStencilStateId id
; /**< vgpu10 */
125 #define SVGA_UNFILLED_DISABLE 0
126 #define SVGA_UNFILLED_LINE 1
127 #define SVGA_UNFILLED_POINT 2
129 #define SVGA_PIPELINE_FLAG_POINTS (1<<PIPE_PRIM_POINTS)
130 #define SVGA_PIPELINE_FLAG_LINES (1<<PIPE_PRIM_LINES)
131 #define SVGA_PIPELINE_FLAG_TRIS (1<<PIPE_PRIM_TRIANGLES)
133 struct svga_rasterizer_state
{
134 struct pipe_rasterizer_state templ
; /* needed for draw module */
136 unsigned shademode
:8;
138 unsigned scissortestenable
:1;
139 unsigned multisampleantialias
:1;
140 unsigned antialiasedlineenable
:1;
141 unsigned lastpixel
:1;
142 unsigned pointsprite
:1;
144 unsigned linepattern
;
146 float slopescaledepthbias
;
151 unsigned hw_fillmode
:2; /* PIPE_POLYGON_MODE_x */
153 /** Which prims do we need help for? Bitmask of (1 << PIPE_PRIM_x) flags */
154 unsigned need_pipeline
:16;
156 SVGA3dRasterizerStateId id
; /**< vgpu10 */
158 /** For debugging: */
159 const char* need_pipeline_tris_str
;
160 const char* need_pipeline_lines_str
;
161 const char* need_pipeline_points_str
;
164 struct svga_sampler_state
{
168 unsigned aniso_level
;
173 unsigned bordercolor
;
174 unsigned normalized_coords
:1;
175 unsigned compare_mode
:1;
176 unsigned compare_func
:3;
179 unsigned view_min_lod
;
180 unsigned view_max_lod
;
186 struct svga_pipe_sampler_view
188 struct pipe_sampler_view base
;
190 SVGA3dShaderResourceViewId id
;
194 static inline struct svga_pipe_sampler_view
*
195 svga_pipe_sampler_view(struct pipe_sampler_view
*v
)
197 return (struct svga_pipe_sampler_view
*) v
;
201 struct svga_velems_state
{
203 struct pipe_vertex_element velem
[PIPE_MAX_ATTRIBS
];
204 SVGA3dDeclType decl_type
[PIPE_MAX_ATTRIBS
]; /**< vertex attrib formats */
206 /** Bitmasks indicating which attributes need format conversion */
207 unsigned adjust_attrib_range
; /**< range adjustment */
208 unsigned attrib_is_pure_int
; /**< pure int */
209 unsigned adjust_attrib_w_1
; /**< set w = 1 */
210 unsigned adjust_attrib_itof
; /**< int->float */
211 unsigned adjust_attrib_utof
; /**< uint->float */
212 unsigned attrib_is_bgra
; /**< R / B swizzling */
213 unsigned attrib_puint_to_snorm
; /**< 10_10_10_2 packed uint -> snorm */
214 unsigned attrib_puint_to_uscaled
; /**< 10_10_10_2 packed uint -> uscaled */
215 unsigned attrib_puint_to_sscaled
; /**< 10_10_10_2 packed uint -> sscaled */
217 boolean need_swvfetch
;
219 SVGA3dElementLayoutId id
; /**< VGPU10 */
222 /* Use to calculate differences between state emitted to hardware and
223 * current driver-calculated state.
227 const struct svga_blend_state
*blend
;
228 const struct svga_depth_stencil_state
*depth
;
229 const struct svga_rasterizer_state
*rast
;
230 const struct svga_sampler_state
*sampler
[PIPE_SHADER_TYPES
][PIPE_MAX_SAMPLERS
];
231 const struct svga_velems_state
*velems
;
233 struct pipe_sampler_view
*sampler_views
[PIPE_SHADER_TYPES
][PIPE_MAX_SAMPLERS
]; /* or texture ID's? */
234 struct svga_fragment_shader
*fs
;
235 struct svga_vertex_shader
*vs
;
236 struct svga_geometry_shader
*user_gs
; /* user-specified GS */
237 struct svga_geometry_shader
*gs
; /* derived GS */
239 struct pipe_vertex_buffer vb
[PIPE_MAX_ATTRIBS
];
240 struct pipe_index_buffer ib
;
241 /** Constant buffers for each shader.
242 * The size should probably always match with that of
243 * svga_shader_emitter_v10.num_shader_consts.
245 struct pipe_constant_buffer constbufs
[PIPE_SHADER_TYPES
][SVGA_MAX_CONST_BUFS
];
247 struct pipe_framebuffer_state framebuffer
;
250 /* Hack to limit the number of different render targets between
251 * flushes. Helps avoid blowing out our surface cache in EXA.
255 struct pipe_poly_stipple poly_stipple
;
256 struct pipe_scissor_state scissor
;
257 struct pipe_blend_color blend_color
;
258 struct pipe_stencil_ref stencil_ref
;
259 struct pipe_clip_state clip
;
260 struct pipe_viewport_state viewport
;
262 unsigned num_samplers
[PIPE_SHADER_TYPES
];
263 unsigned num_sampler_views
[PIPE_SHADER_TYPES
];
264 unsigned num_vertex_buffers
;
265 unsigned reduced_prim
;
272 unsigned sample_mask
;
275 struct svga_prescale
{
282 /* Updated by calling svga_update_state( SVGA_STATE_HW_CLEAR )
284 struct svga_hw_clear_state
292 struct pipe_framebuffer_state framebuffer
;
293 struct svga_prescale prescale
;
296 struct svga_hw_view_state
298 struct pipe_resource
*texture
;
299 struct svga_sampler_view
*v
;
305 /* Updated by calling svga_update_state( SVGA_STATE_HW_DRAW )
307 struct svga_hw_draw_state
309 unsigned rs
[SVGA3D_RS_MAX
];
310 unsigned ts
[SVGA3D_PIXEL_SAMPLERREG_MAX
][SVGA3D_TS_MAX
];
311 float cb
[PIPE_SHADER_TYPES
][SVGA3D_CONSTREG_MAX
][4];
313 struct svga_shader_variant
*fs
;
314 struct svga_shader_variant
*vs
;
315 struct svga_shader_variant
*gs
;
316 struct svga_hw_view_state views
[PIPE_MAX_SAMPLERS
];
318 struct pipe_resource
*constbuf
[PIPE_SHADER_TYPES
];
320 /* Bitmask of enabled constant bufffers */
321 unsigned enabled_constbufs
[PIPE_SHADER_TYPES
];
323 /* VGPU10 HW state (used to prevent emitting redundant state) */
324 SVGA3dDepthStencilStateId depth_stencil_id
;
325 unsigned stencil_ref
;
326 SVGA3dBlendStateId blend_id
;
327 float blend_factor
[4];
328 unsigned blend_sample_mask
;
329 SVGA3dRasterizerStateId rasterizer_id
;
330 SVGA3dElementLayoutId layout_id
;
331 SVGA3dPrimitiveType topology
;
333 /* used for rebinding */
334 unsigned num_sampler_views
[PIPE_SHADER_TYPES
];
335 unsigned default_constbuf_size
[PIPE_SHADER_TYPES
];
339 /* Updated by calling svga_update_state( SVGA_STATE_NEED_SWTNL )
343 /* which parts we need */
344 boolean need_swvfetch
;
345 boolean need_pipeline
;
348 /* Flag to make sure that need sw is on while
349 * updating state within a swtnl call.
351 boolean in_swtnl_draw
;
355 /* Queue some state updates (like rss) and submit them to hardware in
358 struct svga_hw_queue
;
361 struct svga_qmem_alloc_entry
;
365 struct pipe_context pipe
;
366 struct svga_winsys_context
*swc
;
367 struct blitter_context
*blitter
;
368 struct u_upload_mgr
*const0_upload
;
373 boolean use_min_mipmap
;
375 /* incremented for each shader */
378 unsigned disable_shader
;
380 boolean no_line_width
;
381 boolean force_hw_line_stipple
;
385 struct draw_context
*draw
;
386 struct vbuf_render
*backend
;
392 /* Bitmask of blend state objects IDs */
393 struct util_bitmask
*blend_object_id_bm
;
395 /* Bitmask of depth/stencil state objects IDs */
396 struct util_bitmask
*ds_object_id_bm
;
398 /* Bitmaks of input element object IDs */
399 struct util_bitmask
*input_element_object_id_bm
;
401 /* Bitmask of rasterizer object IDs */
402 struct util_bitmask
*rast_object_id_bm
;
404 /* Bitmask of sampler state objects IDs */
405 struct util_bitmask
*sampler_object_id_bm
;
407 /* Bitmask of sampler view IDs */
408 struct util_bitmask
*sampler_view_id_bm
;
410 /* Bitmask of used shader IDs */
411 struct util_bitmask
*shader_id_bm
;
413 /* Bitmask of used surface view IDs */
414 struct util_bitmask
*surface_view_id_bm
;
416 /* Bitmask of used stream output IDs */
417 struct util_bitmask
*stream_output_id_bm
;
419 /* Bitmask of used query IDs */
420 struct util_bitmask
*query_id_bm
;
423 unsigned dirty
[SVGA_STATE_MAX
];
425 /** bitmasks of which const buffers are changed */
426 unsigned dirty_constbufs
[PIPE_SHADER_TYPES
];
428 unsigned texture_timestamp
;
432 struct svga_sw_state sw
;
433 struct svga_hw_draw_state hw_draw
;
434 struct svga_hw_clear_state hw_clear
;
437 struct svga_state curr
; /* state from the state tracker */
438 unsigned dirty
; /* statechanges since last update_state() */
442 unsigned rendertargets
:1;
443 unsigned texture_samplers
:1;
444 unsigned constbufs
:1;
453 struct svga_hwtnl
*hwtnl
;
455 /** Queries states */
456 struct svga_winsys_gb_query
*gb_query
; /**< gb query object, one per context */
457 unsigned gb_query_len
; /**< gb query object size */
458 struct util_bitmask
*gb_query_alloc_mask
; /**< gb query object allocation mask */
459 struct svga_qmem_alloc_entry
*gb_query_map
[SVGA_QUERY_MAX
];
460 /**< query mem block mapping */
461 struct svga_query
*sq
[SVGA_QUERY_MAX
]; /**< queries currently in progress */
463 /** List of buffers with queued transfers */
464 struct list_head dirty_buffers
;
466 /** performance / info queries */
467 uint64_t num_draw_calls
; /**< SVGA_QUERY_DRAW_CALLS */
468 uint64_t num_fallbacks
; /**< SVGA_QUERY_FALLBACKS */
470 /** The currently bound stream output targets */
471 unsigned num_so_targets
;
472 struct svga_winsys_surface
*so_surfaces
[SVGA3D_DX_MAX_SOTARGETS
];
473 struct pipe_stream_output_target
*so_targets
[SVGA3D_DX_MAX_SOTARGETS
];
474 struct svga_stream_output
*current_so
;
476 /** A blend state with blending disabled, for falling back to when blending
477 * is illegal (e.g. an integer texture is bound)
479 struct svga_blend_state
*noop_blend
;
482 struct pipe_resource
*texture
;
483 struct svga_pipe_sampler_view
*sampler_view
;
487 /** Alternate rasterizer states created for point sprite */
488 struct svga_rasterizer_state
*rasterizer_no_cull
[2];
491 /* A flag for each state_tracker state object:
493 #define SVGA_NEW_BLEND 0x1
494 #define SVGA_NEW_DEPTH_STENCIL_ALPHA 0x2
495 #define SVGA_NEW_RAST 0x4
496 #define SVGA_NEW_SAMPLER 0x8
497 #define SVGA_NEW_TEXTURE 0x10
498 #define SVGA_NEW_VBUFFER 0x20
499 #define SVGA_NEW_VELEMENT 0x40
500 #define SVGA_NEW_FS 0x80
501 #define SVGA_NEW_VS 0x100
502 #define SVGA_NEW_FS_CONST_BUFFER 0x200
503 #define SVGA_NEW_VS_CONST_BUFFER 0x400
504 #define SVGA_NEW_FRAME_BUFFER 0x800
505 #define SVGA_NEW_STIPPLE 0x1000
506 #define SVGA_NEW_SCISSOR 0x2000
507 #define SVGA_NEW_BLEND_COLOR 0x4000
508 #define SVGA_NEW_CLIP 0x8000
509 #define SVGA_NEW_VIEWPORT 0x10000
510 #define SVGA_NEW_PRESCALE 0x20000
511 #define SVGA_NEW_REDUCED_PRIMITIVE 0x40000
512 #define SVGA_NEW_TEXTURE_BINDING 0x80000
513 #define SVGA_NEW_NEED_PIPELINE 0x100000
514 #define SVGA_NEW_NEED_SWVFETCH 0x200000
515 #define SVGA_NEW_NEED_SWTNL 0x400000
516 #define SVGA_NEW_FS_VARIANT 0x800000
517 #define SVGA_NEW_VS_VARIANT 0x1000000
518 #define SVGA_NEW_TEXTURE_FLAGS 0x4000000
519 #define SVGA_NEW_STENCIL_REF 0x8000000
520 #define SVGA_NEW_GS 0x10000000
521 #define SVGA_NEW_GS_CONST_BUFFER 0x20000000
522 #define SVGA_NEW_GS_VARIANT 0x40000000
527 /***********************************************************************
530 void svga_clear(struct pipe_context
*pipe
,
532 const union pipe_color_union
*color
,
537 /***********************************************************************
538 * svga_screen_texture.c:
540 void svga_mark_surfaces_dirty(struct svga_context
*svga
);
545 void svga_init_state_functions( struct svga_context
*svga
);
546 void svga_init_flush_functions( struct svga_context
*svga
);
547 void svga_init_string_functions( struct svga_context
*svga
);
548 void svga_init_blit_functions(struct svga_context
*svga
);
550 void svga_init_blend_functions( struct svga_context
*svga
);
551 void svga_init_depth_stencil_functions( struct svga_context
*svga
);
552 void svga_init_misc_functions( struct svga_context
*svga
);
553 void svga_init_rasterizer_functions( struct svga_context
*svga
);
554 void svga_init_sampler_functions( struct svga_context
*svga
);
555 void svga_init_fs_functions( struct svga_context
*svga
);
556 void svga_init_vs_functions( struct svga_context
*svga
);
557 void svga_init_gs_functions( struct svga_context
*svga
);
558 void svga_init_vertex_functions( struct svga_context
*svga
);
559 void svga_init_constbuffer_functions( struct svga_context
*svga
);
560 void svga_init_draw_functions( struct svga_context
*svga
);
561 void svga_init_query_functions( struct svga_context
*svga
);
562 void svga_init_surface_functions(struct svga_context
*svga
);
563 void svga_init_stream_output_functions( struct svga_context
*svga
);
565 void svga_cleanup_vertex_state( struct svga_context
*svga
);
566 void svga_cleanup_tss_binding( struct svga_context
*svga
);
567 void svga_cleanup_framebuffer( struct svga_context
*svga
);
569 void svga_context_flush( struct svga_context
*svga
,
570 struct pipe_fence_handle
**pfence
);
572 void svga_context_finish(struct svga_context
*svga
);
574 void svga_hwtnl_flush_retry( struct svga_context
*svga
);
575 void svga_hwtnl_flush_buffer( struct svga_context
*svga
,
576 struct pipe_resource
*buffer
);
578 void svga_surfaces_flush(struct svga_context
*svga
);
580 struct pipe_context
*
581 svga_context_create(struct pipe_screen
*screen
,
582 void *priv
, unsigned flags
);
585 /***********************************************************************
586 * Inline conversion functions. These are better-typed than the
587 * macros used previously:
589 static inline struct svga_context
*
590 svga_context( struct pipe_context
*pipe
)
592 return (struct svga_context
*)pipe
;
596 static inline boolean
597 svga_have_gb_objects(const struct svga_context
*svga
)
599 return svga_screen(svga
->pipe
.screen
)->sws
->have_gb_objects
;
602 static inline boolean
603 svga_have_gb_dma(const struct svga_context
*svga
)
605 return svga_screen(svga
->pipe
.screen
)->sws
->have_gb_dma
;
608 static inline boolean
609 svga_have_vgpu10(const struct svga_context
*svga
)
611 return svga_screen(svga
->pipe
.screen
)->sws
->have_vgpu10
;
614 static inline boolean
615 svga_need_to_rebind_resources(const struct svga_context
*svga
)
617 return svga_screen(svga
->pipe
.screen
)->sws
->need_to_rebind_resources
;
620 static inline boolean
621 svga_rects_equal(const SVGA3dRect
*r1
, const SVGA3dRect
*r2
)
623 return memcmp(r1
, r2
, sizeof(*r1
)) == 0;