svga: update driver for version 10 GPU interface
[mesa.git] / src / gallium / drivers / svga / svga_context.h
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 #ifndef SVGA_CONTEXT_H
27 #define SVGA_CONTEXT_H
28
29
30 #include "pipe/p_context.h"
31 #include "pipe/p_defines.h"
32 #include "pipe/p_state.h"
33
34 #include "util/u_blitter.h"
35 #include "util/list.h"
36
37 #include "tgsi/tgsi_scan.h"
38
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"
44
45
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)
51
52 /**
53 * Maximum supported number of constant buffers per shader
54 */
55 #define SVGA_MAX_CONST_BUFS 14
56
57 /**
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
61 */
62 #define SVGA_MAX_CONST_BUF_SIZE (4096 * 4 * sizeof(int))
63
64 struct draw_vertex_shader;
65 struct draw_fragment_shader;
66 struct svga_shader_variant;
67 struct SVGACmdMemory;
68 struct util_bitmask;
69
70
71 struct svga_cache_context;
72 struct svga_tracked_state;
73
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 */
79
80 /** Per-render target state */
81 struct {
82 uint8_t writemask;
83
84 boolean blend_enable;
85 uint8_t srcblend;
86 uint8_t dstblend;
87 uint8_t blendeq;
88
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];
94
95 SVGA3dBlendStateId id; /**< vgpu10 */
96 };
97
98 struct svga_depth_stencil_state {
99 unsigned zfunc:8;
100 unsigned zenable:1;
101 unsigned zwriteenable:1;
102
103 unsigned alphatestenable:1;
104 unsigned alphafunc:8;
105
106 struct {
107 unsigned enabled:1;
108 unsigned func:8;
109 unsigned fail:8;
110 unsigned zfail:8;
111 unsigned pass:8;
112 } stencil[2];
113
114 /* SVGA3D has one ref/mask/writemask triple shared between front &
115 * back face stencil. We really need two:
116 */
117 unsigned stencil_mask:8;
118 unsigned stencil_writemask:8;
119
120 float alpharef;
121
122 SVGA3dDepthStencilStateId id; /**< vgpu10 */
123 };
124
125 #define SVGA_UNFILLED_DISABLE 0
126 #define SVGA_UNFILLED_LINE 1
127 #define SVGA_UNFILLED_POINT 2
128
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)
132
133 struct svga_rasterizer_state {
134 struct pipe_rasterizer_state templ; /* needed for draw module */
135
136 unsigned shademode:8;
137 unsigned cullmode:8;
138 unsigned scissortestenable:1;
139 unsigned multisampleantialias:1;
140 unsigned antialiasedlineenable:1;
141 unsigned lastpixel:1;
142 unsigned pointsprite:1;
143
144 unsigned linepattern;
145
146 float slopescaledepthbias;
147 float depthbias;
148 float pointsize;
149 float linewidth;
150
151 unsigned hw_fillmode:2; /* PIPE_POLYGON_MODE_x */
152
153 /** Which prims do we need help for? Bitmask of (1 << PIPE_PRIM_x) flags */
154 unsigned need_pipeline:16;
155
156 SVGA3dRasterizerStateId id; /**< vgpu10 */
157
158 /** For debugging: */
159 const char* need_pipeline_tris_str;
160 const char* need_pipeline_lines_str;
161 const char* need_pipeline_points_str;
162 };
163
164 struct svga_sampler_state {
165 unsigned mipfilter;
166 unsigned magfilter;
167 unsigned minfilter;
168 unsigned aniso_level;
169 float lod_bias;
170 unsigned addressu;
171 unsigned addressv;
172 unsigned addressw;
173 unsigned bordercolor;
174 unsigned normalized_coords:1;
175 unsigned compare_mode:1;
176 unsigned compare_func:3;
177
178 unsigned min_lod;
179 unsigned view_min_lod;
180 unsigned view_max_lod;
181
182 SVGA3dSamplerId id;
183 };
184
185
186 struct svga_pipe_sampler_view
187 {
188 struct pipe_sampler_view base;
189
190 SVGA3dShaderResourceViewId id;
191 };
192
193
194 static inline struct svga_pipe_sampler_view *
195 svga_pipe_sampler_view(struct pipe_sampler_view *v)
196 {
197 return (struct svga_pipe_sampler_view *) v;
198 }
199
200
201 struct svga_velems_state {
202 unsigned count;
203 struct pipe_vertex_element velem[PIPE_MAX_ATTRIBS];
204 SVGA3dDeclType decl_type[PIPE_MAX_ATTRIBS]; /**< vertex attrib formats */
205
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 */
216
217 boolean need_swvfetch;
218
219 SVGA3dElementLayoutId id; /**< VGPU10 */
220 };
221
222 /* Use to calculate differences between state emitted to hardware and
223 * current driver-calculated state.
224 */
225 struct svga_state
226 {
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;
232
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 */
238
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.
244 */
245 struct pipe_constant_buffer constbufs[PIPE_SHADER_TYPES][SVGA_MAX_CONST_BUFS];
246
247 struct pipe_framebuffer_state framebuffer;
248 float depthscale;
249
250 /* Hack to limit the number of different render targets between
251 * flushes. Helps avoid blowing out our surface cache in EXA.
252 */
253 int nr_fbs;
254
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;
261
262 unsigned num_samplers[PIPE_SHADER_TYPES];
263 unsigned num_sampler_views[PIPE_SHADER_TYPES];
264 unsigned num_vertex_buffers;
265 unsigned reduced_prim;
266
267 struct {
268 unsigned flag_1d;
269 unsigned flag_srgb;
270 } tex_flags;
271
272 unsigned sample_mask;
273 };
274
275 struct svga_prescale {
276 float translate[4];
277 float scale[4];
278 boolean enabled;
279 };
280
281
282 /* Updated by calling svga_update_state( SVGA_STATE_HW_CLEAR )
283 */
284 struct svga_hw_clear_state
285 {
286 SVGA3dRect viewport;
287
288 struct {
289 float zmin, zmax;
290 } depthrange;
291
292 struct pipe_framebuffer_state framebuffer;
293 struct svga_prescale prescale;
294 };
295
296 struct svga_hw_view_state
297 {
298 struct pipe_resource *texture;
299 struct svga_sampler_view *v;
300 unsigned min_lod;
301 unsigned max_lod;
302 int dirty;
303 };
304
305 /* Updated by calling svga_update_state( SVGA_STATE_HW_DRAW )
306 */
307 struct svga_hw_draw_state
308 {
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];
312
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];
317 unsigned num_views;
318 struct pipe_resource *constbuf[PIPE_SHADER_TYPES];
319
320 /* Bitmask of enabled constant bufffers */
321 unsigned enabled_constbufs[PIPE_SHADER_TYPES];
322
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;
332
333 /* used for rebinding */
334 unsigned num_sampler_views[PIPE_SHADER_TYPES];
335 unsigned default_constbuf_size[PIPE_SHADER_TYPES];
336 };
337
338
339 /* Updated by calling svga_update_state( SVGA_STATE_NEED_SWTNL )
340 */
341 struct svga_sw_state
342 {
343 /* which parts we need */
344 boolean need_swvfetch;
345 boolean need_pipeline;
346 boolean need_swtnl;
347
348 /* Flag to make sure that need sw is on while
349 * updating state within a swtnl call.
350 */
351 boolean in_swtnl_draw;
352 };
353
354
355 /* Queue some state updates (like rss) and submit them to hardware in
356 * a single packet.
357 */
358 struct svga_hw_queue;
359
360 struct svga_query;
361 struct svga_qmem_alloc_entry;
362
363 struct svga_context
364 {
365 struct pipe_context pipe;
366 struct svga_winsys_context *swc;
367 struct blitter_context *blitter;
368 struct u_upload_mgr *const0_upload;
369
370 struct {
371 boolean no_swtnl;
372 boolean force_swtnl;
373 boolean use_min_mipmap;
374
375 /* incremented for each shader */
376 unsigned shader_id;
377
378 unsigned disable_shader;
379
380 boolean no_line_width;
381 boolean force_hw_line_stipple;
382 } debug;
383
384 struct {
385 struct draw_context *draw;
386 struct vbuf_render *backend;
387 unsigned hw_prim;
388 boolean new_vbuf;
389 boolean new_vdecl;
390 } swtnl;
391
392 /* Bitmask of blend state objects IDs */
393 struct util_bitmask *blend_object_id_bm;
394
395 /* Bitmask of depth/stencil state objects IDs */
396 struct util_bitmask *ds_object_id_bm;
397
398 /* Bitmaks of input element object IDs */
399 struct util_bitmask *input_element_object_id_bm;
400
401 /* Bitmask of rasterizer object IDs */
402 struct util_bitmask *rast_object_id_bm;
403
404 /* Bitmask of sampler state objects IDs */
405 struct util_bitmask *sampler_object_id_bm;
406
407 /* Bitmask of sampler view IDs */
408 struct util_bitmask *sampler_view_id_bm;
409
410 /* Bitmask of used shader IDs */
411 struct util_bitmask *shader_id_bm;
412
413 /* Bitmask of used surface view IDs */
414 struct util_bitmask *surface_view_id_bm;
415
416 /* Bitmask of used stream output IDs */
417 struct util_bitmask *stream_output_id_bm;
418
419 /* Bitmask of used query IDs */
420 struct util_bitmask *query_id_bm;
421
422 struct {
423 unsigned dirty[SVGA_STATE_MAX];
424
425 /** bitmasks of which const buffers are changed */
426 unsigned dirty_constbufs[PIPE_SHADER_TYPES];
427
428 unsigned texture_timestamp;
429
430 /*
431 */
432 struct svga_sw_state sw;
433 struct svga_hw_draw_state hw_draw;
434 struct svga_hw_clear_state hw_clear;
435 } state;
436
437 struct svga_state curr; /* state from the state tracker */
438 unsigned dirty; /* statechanges since last update_state() */
439
440 union {
441 struct {
442 unsigned rendertargets:1;
443 unsigned texture_samplers:1;
444 unsigned constbufs:1;
445 unsigned vs:1;
446 unsigned fs:1;
447 unsigned gs:1;
448 unsigned query:1;
449 } flags;
450 unsigned val;
451 } rebind;
452
453 struct svga_hwtnl *hwtnl;
454
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 */
462
463 /** List of buffers with queued transfers */
464 struct list_head dirty_buffers;
465
466 /** performance / info queries */
467 uint64_t num_draw_calls; /**< SVGA_QUERY_DRAW_CALLS */
468 uint64_t num_fallbacks; /**< SVGA_QUERY_FALLBACKS */
469
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;
475
476 /** A blend state with blending disabled, for falling back to when blending
477 * is illegal (e.g. an integer texture is bound)
478 */
479 struct svga_blend_state *noop_blend;
480
481 struct {
482 struct pipe_resource *texture;
483 struct svga_pipe_sampler_view *sampler_view;
484 void *sampler;
485 } polygon_stipple;
486
487 /** Alternate rasterizer states created for point sprite */
488 struct svga_rasterizer_state *rasterizer_no_cull[2];
489 };
490
491 /* A flag for each state_tracker state object:
492 */
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
523
524
525
526
527 /***********************************************************************
528 * svga_clear.c:
529 */
530 void svga_clear(struct pipe_context *pipe,
531 unsigned buffers,
532 const union pipe_color_union *color,
533 double depth,
534 unsigned stencil);
535
536
537 /***********************************************************************
538 * svga_screen_texture.c:
539 */
540 void svga_mark_surfaces_dirty(struct svga_context *svga);
541
542
543
544
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);
549
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 );
564
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 );
568
569 void svga_context_flush( struct svga_context *svga,
570 struct pipe_fence_handle **pfence );
571
572 void svga_context_finish(struct svga_context *svga);
573
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 );
577
578 void svga_surfaces_flush(struct svga_context *svga);
579
580 struct pipe_context *
581 svga_context_create(struct pipe_screen *screen,
582 void *priv, unsigned flags);
583
584
585 /***********************************************************************
586 * Inline conversion functions. These are better-typed than the
587 * macros used previously:
588 */
589 static inline struct svga_context *
590 svga_context( struct pipe_context *pipe )
591 {
592 return (struct svga_context *)pipe;
593 }
594
595
596 static inline boolean
597 svga_have_gb_objects(const struct svga_context *svga)
598 {
599 return svga_screen(svga->pipe.screen)->sws->have_gb_objects;
600 }
601
602 static inline boolean
603 svga_have_gb_dma(const struct svga_context *svga)
604 {
605 return svga_screen(svga->pipe.screen)->sws->have_gb_dma;
606 }
607
608 static inline boolean
609 svga_have_vgpu10(const struct svga_context *svga)
610 {
611 return svga_screen(svga->pipe.screen)->sws->have_vgpu10;
612 }
613
614 static inline boolean
615 svga_need_to_rebind_resources(const struct svga_context *svga)
616 {
617 return svga_screen(svga->pipe.screen)->sws->need_to_rebind_resources;
618 }
619
620 static inline boolean
621 svga_rects_equal(const SVGA3dRect *r1, const SVGA3dRect *r2)
622 {
623 return memcmp(r1, r2, sizeof(*r1)) == 0;
624 }
625
626 #endif