svga: Add GL4.1(compatibility profile) support in svga driver
[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/os_time.h"
35
36 #include "util/u_blitter.h"
37 #include "util/list.h"
38
39 #include "tgsi/tgsi_scan.h"
40
41 #include "svga_screen.h"
42 #include "svga_state.h"
43 #include "svga_winsys.h"
44 #include "svga_hw_reg.h"
45 #include "svga3d_shaderdefs.h"
46 #include "svga_debug.h"
47
48 /** Non-GPU queries for gallium HUD */
49 enum svga_hud {
50 /* per-frame counters */
51 SVGA_QUERY_NUM_DRAW_CALLS = PIPE_QUERY_DRIVER_SPECIFIC,
52 SVGA_QUERY_NUM_FALLBACKS,
53 SVGA_QUERY_NUM_FLUSHES,
54 SVGA_QUERY_NUM_VALIDATIONS,
55 SVGA_QUERY_MAP_BUFFER_TIME,
56 SVGA_QUERY_NUM_BUFFERS_MAPPED,
57 SVGA_QUERY_NUM_TEXTURES_MAPPED,
58 SVGA_QUERY_NUM_BYTES_UPLOADED,
59 SVGA_QUERY_NUM_COMMAND_BUFFERS,
60 SVGA_QUERY_COMMAND_BUFFER_SIZE,
61 SVGA_QUERY_FLUSH_TIME,
62 SVGA_QUERY_SURFACE_WRITE_FLUSHES,
63 SVGA_QUERY_NUM_READBACKS,
64 SVGA_QUERY_NUM_RESOURCE_UPDATES,
65 SVGA_QUERY_NUM_BUFFER_UPLOADS,
66 SVGA_QUERY_NUM_CONST_BUF_UPDATES,
67 SVGA_QUERY_NUM_CONST_UPDATES,
68 SVGA_QUERY_NUM_SHADER_RELOCATIONS,
69 SVGA_QUERY_NUM_SURFACE_RELOCATIONS,
70
71 /* running total counters */
72 SVGA_QUERY_MEMORY_USED,
73 SVGA_QUERY_NUM_SHADERS,
74 SVGA_QUERY_NUM_RESOURCES,
75 SVGA_QUERY_NUM_STATE_OBJECTS,
76 SVGA_QUERY_NUM_SURFACE_VIEWS,
77 SVGA_QUERY_NUM_GENERATE_MIPMAP,
78 SVGA_QUERY_NUM_FAILED_ALLOCATIONS,
79 SVGA_QUERY_NUM_COMMANDS_PER_DRAW,
80 SVGA_QUERY_SHADER_MEM_USED,
81
82 /*SVGA_QUERY_MAX has to be last because it is size of an array*/
83 SVGA_QUERY_MAX
84 };
85
86 /**
87 * Maximum supported number of constant buffers per shader
88 */
89 #define SVGA_MAX_CONST_BUFS 14
90
91 /**
92 * Maximum constant buffer size that can be set in the
93 * DXSetSingleConstantBuffer command is
94 * DX10 constant buffer element count * 4 4-bytes components
95 */
96 #define SVGA_MAX_CONST_BUF_SIZE (4096 * 4 * sizeof(int))
97
98 #define CONST0_UPLOAD_ALIGNMENT 256
99
100 struct draw_vertex_shader;
101 struct draw_fragment_shader;
102 struct svga_shader_variant;
103 struct SVGACmdMemory;
104 struct util_bitmask;
105
106
107 struct svga_cache_context;
108 struct svga_tracked_state;
109
110 struct svga_blend_state {
111 unsigned need_white_fragments:1;
112 unsigned independent_blend_enable:1;
113 unsigned alpha_to_coverage:1;
114 unsigned alpha_to_one:1;
115 unsigned blend_color_alpha:1; /**< set blend color to alpha value */
116 unsigned logicop_enabled:1;
117 unsigned logicop_mode:5;
118
119 /** Per-render target state */
120 struct {
121 uint8_t writemask;
122
123 boolean blend_enable;
124 uint8_t srcblend;
125 uint8_t dstblend;
126 uint8_t blendeq;
127
128 boolean separate_alpha_blend_enable;
129 uint8_t srcblend_alpha;
130 uint8_t dstblend_alpha;
131 uint8_t blendeq_alpha;
132 } rt[PIPE_MAX_COLOR_BUFS];
133
134 SVGA3dBlendStateId id; /**< vgpu10 */
135 };
136
137 struct svga_depth_stencil_state {
138 unsigned zfunc:8;
139 unsigned zenable:1;
140 unsigned zwriteenable:1;
141
142 unsigned alphatestenable:1;
143 unsigned alphafunc:8;
144
145 struct {
146 unsigned enabled:1;
147 unsigned func:8;
148 unsigned fail:8;
149 unsigned zfail:8;
150 unsigned pass:8;
151 } stencil[2];
152
153 /* SVGA3D has one ref/mask/writemask triple shared between front &
154 * back face stencil. We really need two:
155 */
156 unsigned stencil_mask:8;
157 unsigned stencil_writemask:8;
158
159 float alpharef;
160
161 SVGA3dDepthStencilStateId id; /**< vgpu10 */
162 };
163
164 #define SVGA_UNFILLED_DISABLE 0
165 #define SVGA_UNFILLED_LINE 1
166 #define SVGA_UNFILLED_POINT 2
167
168 #define SVGA_PIPELINE_FLAG_POINTS (1<<PIPE_PRIM_POINTS)
169 #define SVGA_PIPELINE_FLAG_LINES (1<<PIPE_PRIM_LINES)
170 #define SVGA_PIPELINE_FLAG_TRIS (1<<PIPE_PRIM_TRIANGLES)
171
172 struct svga_rasterizer_state {
173 struct pipe_rasterizer_state templ; /* needed for draw module */
174
175 unsigned shademode:8;
176 unsigned cullmode:8;
177 unsigned scissortestenable:1;
178 unsigned multisampleantialias:1;
179 unsigned antialiasedlineenable:1;
180 unsigned lastpixel:1;
181 unsigned pointsprite:1;
182
183 unsigned linepattern;
184
185 float slopescaledepthbias;
186 float depthbias;
187 float pointsize;
188 float linewidth;
189
190 unsigned hw_fillmode:2; /* PIPE_POLYGON_MODE_x */
191
192 /** Which prims do we need help for? Bitmask of (1 << PIPE_PRIM_x) flags */
193 unsigned need_pipeline:16;
194
195 SVGA3dRasterizerStateId id; /**< vgpu10 */
196
197 /** For debugging: */
198 const char* need_pipeline_tris_str;
199 const char* need_pipeline_lines_str;
200 const char* need_pipeline_points_str;
201 };
202
203 struct svga_sampler_state {
204 unsigned mipfilter;
205 unsigned magfilter;
206 unsigned minfilter;
207 unsigned aniso_level;
208 float lod_bias;
209 unsigned addressu;
210 unsigned addressv;
211 unsigned addressw;
212 unsigned bordercolor;
213 unsigned normalized_coords:1;
214 unsigned compare_mode:1;
215 unsigned compare_func:3;
216
217 unsigned min_lod;
218 unsigned view_min_lod;
219 unsigned view_max_lod;
220
221 SVGA3dSamplerId id[2];
222 };
223
224
225 struct svga_pipe_sampler_view
226 {
227 struct pipe_sampler_view base;
228
229 SVGA3dShaderResourceViewId id;
230 };
231
232
233 static inline struct svga_pipe_sampler_view *
234 svga_pipe_sampler_view(struct pipe_sampler_view *v)
235 {
236 return (struct svga_pipe_sampler_view *) v;
237 }
238
239
240 struct svga_velems_state {
241 unsigned count;
242 struct pipe_vertex_element velem[PIPE_MAX_ATTRIBS];
243 SVGA3dDeclType decl_type[PIPE_MAX_ATTRIBS]; /**< vertex attrib formats */
244
245 /** Bitmasks indicating which attributes need format conversion */
246 unsigned adjust_attrib_range; /**< range adjustment */
247 unsigned attrib_is_pure_int; /**< pure int */
248 unsigned adjust_attrib_w_1; /**< set w = 1 */
249 unsigned adjust_attrib_itof; /**< int->float */
250 unsigned adjust_attrib_utof; /**< uint->float */
251 unsigned attrib_is_bgra; /**< R / B swizzling */
252 unsigned attrib_puint_to_snorm; /**< 10_10_10_2 packed uint -> snorm */
253 unsigned attrib_puint_to_uscaled; /**< 10_10_10_2 packed uint -> uscaled */
254 unsigned attrib_puint_to_sscaled; /**< 10_10_10_2 packed uint -> sscaled */
255
256 boolean need_swvfetch;
257
258 SVGA3dElementLayoutId id; /**< VGPU10 */
259 };
260
261
262 /* Use to calculate differences between state emitted to hardware and
263 * current driver-calculated state.
264 */
265 struct svga_state
266 {
267 const struct svga_blend_state *blend;
268 const struct svga_depth_stencil_state *depth;
269 const struct svga_rasterizer_state *rast;
270 const struct svga_sampler_state *sampler[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
271 const struct svga_velems_state *velems;
272
273 struct pipe_sampler_view *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS]; /* or texture ID's? */
274 struct svga_fragment_shader *fs;
275 struct svga_vertex_shader *vs;
276 struct svga_geometry_shader *user_gs; /* user-specified GS */
277 struct svga_geometry_shader *gs; /* derived GS */
278 /* derived tessellation control shader */
279 struct svga_tcs_shader *tcs;
280 /* derived tessellation evaluation shader */
281 struct svga_tes_shader *tes;
282 struct svga_compute_shader *cs;
283
284 struct pipe_vertex_buffer vb[PIPE_MAX_ATTRIBS];
285 /** Constant buffers for each shader.
286 * The size should probably always match with that of
287 * svga_shader_emitter_v10.num_shader_consts.
288 */
289 struct pipe_constant_buffer constbufs[PIPE_SHADER_TYPES][SVGA_MAX_CONST_BUFS];
290
291 struct pipe_framebuffer_state framebuffer;
292 float depthscale;
293
294 /* Hack to limit the number of different render targets between
295 * flushes. Helps avoid blowing out our surface cache in EXA.
296 */
297 int nr_fbs;
298
299 struct pipe_poly_stipple poly_stipple;
300 struct pipe_scissor_state scissor[SVGA3D_DX_MAX_VIEWPORTS];
301 struct pipe_blend_color blend_color;
302 struct pipe_stencil_ref stencil_ref;
303 struct pipe_clip_state clip;
304 struct pipe_viewport_state viewport[SVGA3D_DX_MAX_VIEWPORTS];
305
306 unsigned num_samplers[PIPE_SHADER_TYPES];
307 unsigned num_sampler_views[PIPE_SHADER_TYPES];
308 unsigned num_vertex_buffers;
309 enum pipe_prim_type reduced_prim;
310
311 struct {
312 unsigned flag_1d;
313 unsigned flag_srgb;
314 } tex_flags;
315
316 unsigned sample_mask;
317 unsigned vertices_per_patch;
318 float default_tesslevels[6]; /* tessellation (outer[4] + inner[2]) levels */
319 struct {
320 /* Determine the layout of the grid (in block units) to be used. */
321 unsigned size[3];
322 /* If DispatchIndirect is used, this will has grid size info*/
323 struct pipe_resource *indirect;
324 } grid_info;
325 };
326
327 struct svga_prescale {
328 float translate[4];
329 float scale[4];
330 boolean enabled;
331 };
332
333 struct svga_depthrange {
334 float zmin;
335 float zmax;
336 };
337
338 /* Updated by calling svga_update_state( SVGA_STATE_HW_CLEAR )
339 */
340 struct svga_hw_clear_state
341 {
342 struct pipe_framebuffer_state framebuffer;
343
344 /* VGPU9 only */
345 SVGA3dRect viewport;
346 struct svga_depthrange depthrange;
347
348 /* VGPU10 state */
349 SVGA3dViewport viewports[SVGA3D_DX_MAX_VIEWPORTS];
350 struct svga_prescale prescale[SVGA3D_DX_MAX_VIEWPORTS];
351 struct pipe_scissor_state scissors[SVGA3D_DX_MAX_VIEWPORTS];
352 unsigned num_prescale;
353
354 unsigned num_rendertargets;
355 struct pipe_surface *rtv[SVGA3D_MAX_RENDER_TARGETS];
356 struct pipe_surface *dsv;
357 };
358
359 struct svga_hw_view_state
360 {
361 struct pipe_resource *texture;
362 struct svga_sampler_view *v;
363 unsigned min_lod;
364 unsigned max_lod;
365 boolean dirty;
366 };
367
368 /* Updated by calling svga_update_state( SVGA_STATE_HW_DRAW )
369 */
370 struct svga_hw_draw_state
371 {
372 /** VGPU9 rasterization state */
373 unsigned rs[SVGA3D_RS_MAX];
374 /** VGPU9 texture sampler and bindings state */
375 unsigned ts[SVGA3D_PIXEL_SAMPLERREG_MAX][SVGA3D_TS_MAX];
376
377 /** VGPU9 texture views */
378 unsigned num_views;
379 unsigned num_backed_views; /* views with backing copy of texture */
380 struct svga_hw_view_state views[PIPE_MAX_SAMPLERS];
381
382 /** VGPU9 constant buffer values */
383 float cb[PIPE_SHADER_TYPES][SVGA3D_CONSTREG_MAX][4];
384
385 /** Currently bound shaders */
386 struct svga_shader_variant *fs;
387 struct svga_shader_variant *vs;
388 struct svga_shader_variant *gs;
389 struct svga_shader_variant *tcs;
390 struct svga_shader_variant *tes;
391 struct svga_shader_variant *cs;
392
393 /** Currently bound constant buffer, per shader stage */
394 struct pipe_resource *constbuf[PIPE_SHADER_TYPES];
395
396 /** Bitmask of enabled constant buffers */
397 unsigned enabled_constbufs[PIPE_SHADER_TYPES];
398
399 /**
400 * These are used to reduce the number of times we call u_upload_unmap()
401 * while updating the zero-th/default VGPU10 constant buffer.
402 */
403 struct pipe_resource *const0_buffer;
404 struct svga_winsys_surface *const0_handle;
405
406 /** VGPU10 HW state (used to prevent emitting redundant state) */
407 SVGA3dDepthStencilStateId depth_stencil_id;
408 unsigned stencil_ref;
409 SVGA3dBlendStateId blend_id;
410 float blend_factor[4];
411 unsigned blend_sample_mask;
412 SVGA3dRasterizerStateId rasterizer_id;
413 SVGA3dElementLayoutId layout_id;
414 SVGA3dPrimitiveType topology;
415
416 /** Vertex buffer state */
417 SVGA3dVertexBuffer vbuffer_attrs[PIPE_MAX_ATTRIBS];
418 struct pipe_resource *vbuffers[PIPE_MAX_ATTRIBS];
419 unsigned num_vbuffers;
420
421 struct pipe_resource *ib; /**< index buffer for drawing */
422 SVGA3dSurfaceFormat ib_format;
423 unsigned ib_offset;
424
425 unsigned num_samplers[PIPE_SHADER_TYPES];
426 SVGA3dSamplerId samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
427
428 unsigned num_sampler_views[PIPE_SHADER_TYPES];
429 struct pipe_sampler_view
430 *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
431
432 /* used for rebinding */
433 unsigned default_constbuf_size[PIPE_SHADER_TYPES];
434
435 boolean rasterizer_discard; /* set if rasterization is disabled */
436 boolean has_backed_views; /* set if any of the rtv/dsv is a backed surface view */
437 };
438
439
440 /* Updated by calling svga_update_state( SVGA_STATE_NEED_SWTNL )
441 */
442 struct svga_sw_state
443 {
444 /* which parts we need */
445 boolean need_swvfetch;
446 boolean need_pipeline;
447 boolean need_swtnl;
448
449 /* Flag to make sure that need sw is on while
450 * updating state within a swtnl call.
451 */
452 boolean in_swtnl_draw;
453 };
454
455
456 /* Queue some state updates (like rss) and submit them to hardware in
457 * a single packet.
458 */
459 struct svga_hw_queue;
460
461 struct svga_query;
462 struct svga_qmem_alloc_entry;
463
464 struct svga_context
465 {
466 struct pipe_context pipe;
467 struct svga_winsys_context *swc;
468 struct blitter_context *blitter;
469 struct u_upload_mgr *const0_upload;
470 struct u_upload_mgr *tex_upload;
471
472 struct {
473 boolean no_swtnl;
474 boolean force_swtnl;
475 boolean use_min_mipmap;
476
477 /* incremented for each shader */
478 unsigned shader_id;
479
480 boolean no_line_width;
481 boolean force_hw_line_stipple;
482
483 /** To report perf/conformance/etc issues to the gallium frontend */
484 struct pipe_debug_callback callback;
485 } debug;
486
487 struct {
488 struct draw_context *draw;
489 struct vbuf_render *backend;
490 unsigned hw_prim;
491 boolean new_vbuf;
492 boolean new_vdecl;
493 } swtnl;
494
495 /* Bitmask of blend state objects IDs */
496 struct util_bitmask *blend_object_id_bm;
497
498 /* Bitmask of depth/stencil state objects IDs */
499 struct util_bitmask *ds_object_id_bm;
500
501 /* Bitmaks of input element object IDs */
502 struct util_bitmask *input_element_object_id_bm;
503
504 /* Bitmask of rasterizer object IDs */
505 struct util_bitmask *rast_object_id_bm;
506
507 /* Bitmask of sampler state objects IDs */
508 struct util_bitmask *sampler_object_id_bm;
509
510 /* Bitmask of sampler view IDs */
511 struct util_bitmask *sampler_view_id_bm;
512
513 /* Bitmask of used shader IDs */
514 struct util_bitmask *shader_id_bm;
515
516 /* Bitmask of used surface view IDs */
517 struct util_bitmask *surface_view_id_bm;
518
519 /* Bitmask of used stream output IDs */
520 struct util_bitmask *stream_output_id_bm;
521
522 /* Bitmask of used query IDs */
523 struct util_bitmask *query_id_bm;
524
525 struct {
526 uint64_t dirty[SVGA_STATE_MAX];
527
528 /** bitmasks of which const buffers are changed */
529 unsigned dirty_constbufs[PIPE_SHADER_TYPES];
530
531 unsigned texture_timestamp;
532
533 struct svga_sw_state sw;
534 struct svga_hw_draw_state hw_draw;
535 struct svga_hw_clear_state hw_clear;
536 } state;
537
538 struct svga_state curr; /* state from the gallium frontend */
539 uint64_t dirty; /* statechanges since last update_state() */
540
541 union {
542 struct {
543 unsigned rendertargets:1;
544 unsigned texture_samplers:1;
545 unsigned constbufs:1;
546 unsigned vs:1;
547 unsigned fs:1;
548 unsigned gs:1;
549 unsigned tcs:1;
550 unsigned tes:1;
551 unsigned cs:1;
552 unsigned query:1;
553 } flags;
554 unsigned val;
555 } rebind;
556
557 struct svga_hwtnl *hwtnl;
558
559 /** Queries states */
560 struct svga_winsys_gb_query *gb_query; /**< gb query object, one per context */
561 unsigned gb_query_len; /**< gb query object size */
562 struct util_bitmask *gb_query_alloc_mask; /**< gb query object allocation mask */
563 struct svga_qmem_alloc_entry *gb_query_map[SVGA_QUERY_MAX];
564 /**< query mem block mapping */
565 struct svga_query *sq[SVGA_QUERY_MAX+12]; /**< queries currently in progress */
566 /* The last 12 entries are for streamout
567 * queries for stream 0..3
568 */
569
570 /** List of buffers with queued transfers */
571 struct list_head dirty_buffers;
572
573 /** performance / info queries for HUD */
574 struct {
575 uint64_t num_draw_calls; /**< SVGA_QUERY_DRAW_CALLS */
576 uint64_t num_fallbacks; /**< SVGA_QUERY_NUM_FALLBACKS */
577 uint64_t num_flushes; /**< SVGA_QUERY_NUM_FLUSHES */
578 uint64_t num_validations; /**< SVGA_QUERY_NUM_VALIDATIONS */
579 uint64_t map_buffer_time; /**< SVGA_QUERY_MAP_BUFFER_TIME */
580 uint64_t num_buffers_mapped; /**< SVGA_QUERY_NUM_BUFFERS_MAPPED */
581 uint64_t num_textures_mapped; /**< SVGA_QUERY_NUM_TEXTURES_MAPPED */
582 uint64_t num_command_buffers; /**< SVGA_QUERY_NUM_COMMAND_BUFFERS */
583 uint64_t command_buffer_size; /**< SVGA_QUERY_COMMAND_BUFFER_SIZE */
584 uint64_t flush_time; /**< SVGA_QUERY_FLUSH_TIME */
585 uint64_t surface_write_flushes; /**< SVGA_QUERY_SURFACE_WRITE_FLUSHES */
586 uint64_t num_readbacks; /**< SVGA_QUERY_NUM_READBACKS */
587 uint64_t num_resource_updates; /**< SVGA_QUERY_NUM_RESOURCE_UPDATES */
588 uint64_t num_buffer_uploads; /**< SVGA_QUERY_NUM_BUFFER_UPLOADS */
589 uint64_t num_const_buf_updates; /**< SVGA_QUERY_NUM_CONST_BUF_UPDATES */
590 uint64_t num_const_updates; /**< SVGA_QUERY_NUM_CONST_UPDATES */
591 uint64_t num_shaders; /**< SVGA_QUERY_NUM_SHADERS */
592
593 /** The following are summed for SVGA_QUERY_NUM_STATE_OBJECTS */
594 uint64_t num_blend_objects;
595 uint64_t num_depthstencil_objects;
596 uint64_t num_rasterizer_objects;
597 uint64_t num_sampler_objects;
598 uint64_t num_samplerview_objects;
599 uint64_t num_vertexelement_objects;
600
601 uint64_t num_surface_views; /**< SVGA_QUERY_NUM_SURFACE_VIEWS */
602 uint64_t num_bytes_uploaded; /**< SVGA_QUERY_NUM_BYTES_UPLOADED */
603 uint64_t num_generate_mipmap; /**< SVGA_QUERY_NUM_GENERATE_MIPMAP */
604 uint64_t shader_mem_used; /**< SVGA_QUERY_SHADER_MEM_USED */
605
606 boolean uses_time; /**< os_time_get() calls needed? */
607 } hud;
608
609 /** The currently bound stream output targets */
610 boolean in_streamout; /* Set if streamout is active */
611 unsigned num_so_targets;
612 struct svga_winsys_surface *so_surfaces[SVGA3D_DX_MAX_SOTARGETS];
613 struct pipe_stream_output_target *so_targets[SVGA3D_DX_MAX_SOTARGETS];
614 struct svga_stream_output *current_so;
615
616 /**
617 * The following states are used in the workaround for auto draw with
618 * stream instancing.
619 */
620
621 /* Last bound SO targets that can be used to get vertex count */
622 struct pipe_stream_output_target *vcount_so_targets[SVGA3D_DX_MAX_SOTARGETS];
623 unsigned vcount_buffer_stream; /* SO buffer to stream index mask */
624 struct pipe_query *so_queries[4]; /* SO stat queries for each stream */
625
626 /** A blend state with blending disabled, for falling back to when blending
627 * is illegal (e.g. an integer texture is bound)
628 */
629 struct svga_blend_state *noop_blend;
630
631 struct {
632 struct pipe_resource *texture;
633 struct svga_pipe_sampler_view *sampler_view;
634 void *sampler;
635 } polygon_stipple;
636
637 /** Alternate rasterizer states created for point sprite */
638 struct svga_rasterizer_state *rasterizer_no_cull[2];
639
640 /** Depth stencil state created to disable depth stencil test */
641 struct svga_depth_stencil_state *depthstencil_disable;
642
643 /** Current conditional rendering predicate */
644 struct {
645 SVGA3dQueryId query_id;
646 boolean cond;
647 } pred;
648
649 boolean render_condition;
650 boolean disable_rasterizer; /* Set if to disable rasterization */
651
652 struct {
653 struct svga_tcs_shader *passthrough_tcs;
654 struct svga_vertex_shader *vs;
655 struct svga_tes_shader *tes;
656 unsigned vertices_per_patch;
657 boolean passthrough;
658 } tcs;
659
660 };
661
662 /* A flag for each frontend state object:
663 */
664 #define SVGA_NEW_BLEND ((uint64_t) 0x1)
665 #define SVGA_NEW_DEPTH_STENCIL_ALPHA ((uint64_t) 0x2)
666 #define SVGA_NEW_RAST ((uint64_t) 0x4)
667 #define SVGA_NEW_SAMPLER ((uint64_t) 0x8)
668 #define SVGA_NEW_TEXTURE ((uint64_t) 0x10)
669 #define SVGA_NEW_VBUFFER ((uint64_t) 0x20)
670 #define SVGA_NEW_VELEMENT ((uint64_t) 0x40)
671 #define SVGA_NEW_FS ((uint64_t) 0x80)
672 #define SVGA_NEW_VS ((uint64_t) 0x100)
673 #define SVGA_NEW_FS_CONST_BUFFER ((uint64_t) 0x200)
674 #define SVGA_NEW_VS_CONST_BUFFER ((uint64_t) 0x400)
675 #define SVGA_NEW_FRAME_BUFFER ((uint64_t) 0x800)
676 #define SVGA_NEW_STIPPLE ((uint64_t) 0x1000)
677 #define SVGA_NEW_SCISSOR ((uint64_t) 0x2000)
678 #define SVGA_NEW_BLEND_COLOR ((uint64_t) 0x4000)
679 #define SVGA_NEW_CLIP ((uint64_t) 0x8000)
680 #define SVGA_NEW_VIEWPORT ((uint64_t) 0x10000)
681 #define SVGA_NEW_PRESCALE ((uint64_t) 0x20000)
682 #define SVGA_NEW_REDUCED_PRIMITIVE ((uint64_t) 0x40000)
683 #define SVGA_NEW_TEXTURE_BINDING ((uint64_t) 0x80000)
684 #define SVGA_NEW_NEED_PIPELINE ((uint64_t) 0x100000)
685 #define SVGA_NEW_NEED_SWVFETCH ((uint64_t) 0x200000)
686 #define SVGA_NEW_NEED_SWTNL ((uint64_t) 0x400000)
687 #define SVGA_NEW_FS_VARIANT ((uint64_t) 0x800000)
688 #define SVGA_NEW_VS_VARIANT ((uint64_t) 0x1000000)
689 #define SVGA_NEW_TEXTURE_FLAGS ((uint64_t) 0x4000000)
690 #define SVGA_NEW_STENCIL_REF ((uint64_t) 0x8000000)
691 #define SVGA_NEW_GS ((uint64_t) 0x10000000)
692 #define SVGA_NEW_GS_CONST_BUFFER ((uint64_t) 0x20000000)
693 #define SVGA_NEW_GS_VARIANT ((uint64_t) 0x40000000)
694 #define SVGA_NEW_TEXTURE_CONSTS ((uint64_t) 0x80000000)
695 #define SVGA_NEW_TCS ((uint64_t) 0x100000000)
696 #define SVGA_NEW_TES ((uint64_t) 0x200000000)
697 #define SVGA_NEW_TCS_VARIANT ((uint64_t) 0x400000000)
698 #define SVGA_NEW_TES_VARIANT ((uint64_t) 0x800000000)
699 #define SVGA_NEW_TCS_CONST_BUFFER ((uint64_t) 0x1000000000)
700 #define SVGA_NEW_TES_CONST_BUFFER ((uint64_t) 0x2000000000)
701 #define SVGA_NEW_TCS_PARAM ((uint64_t) 0x4000000000)
702 #define SVGA_NEW_ALL ((uint64_t) 0xFFFFFFFFFFFFFFFF)
703
704
705 void svga_init_state_functions( struct svga_context *svga );
706 void svga_init_flush_functions( struct svga_context *svga );
707 void svga_init_string_functions( struct svga_context *svga );
708 void svga_init_blit_functions(struct svga_context *svga);
709
710 void svga_init_blend_functions( struct svga_context *svga );
711 void svga_init_depth_stencil_functions( struct svga_context *svga );
712 void svga_init_misc_functions( struct svga_context *svga );
713 void svga_init_rasterizer_functions( struct svga_context *svga );
714 void svga_init_sampler_functions( struct svga_context *svga );
715 void svga_init_cs_functions( struct svga_context *svga );
716 void svga_init_fs_functions( struct svga_context *svga );
717 void svga_init_vs_functions( struct svga_context *svga );
718 void svga_init_gs_functions( struct svga_context *svga );
719 void svga_init_ts_functions( struct svga_context *svga );
720 void svga_init_vertex_functions( struct svga_context *svga );
721 void svga_init_constbuffer_functions( struct svga_context *svga );
722 void svga_init_draw_functions( struct svga_context *svga );
723 void svga_init_query_functions( struct svga_context *svga );
724 void svga_init_surface_functions(struct svga_context *svga);
725 void svga_init_stream_output_functions( struct svga_context *svga );
726 void svga_init_clear_functions( struct svga_context *svga );
727
728 void svga_cleanup_vertex_state( struct svga_context *svga );
729 void svga_cleanup_sampler_state( struct svga_context *svga );
730 void svga_cleanup_tss_binding( struct svga_context *svga );
731 void svga_cleanup_framebuffer( struct svga_context *svga );
732 void svga_cleanup_tcs_state( struct svga_context *svga );
733
734 void svga_context_flush( struct svga_context *svga,
735 struct pipe_fence_handle **pfence );
736
737 void svga_context_finish(struct svga_context *svga);
738
739 void svga_hwtnl_flush_retry( struct svga_context *svga );
740 void svga_hwtnl_flush_buffer( struct svga_context *svga,
741 struct pipe_resource *buffer );
742
743 void svga_surfaces_flush(struct svga_context *svga);
744
745 struct pipe_context *
746 svga_context_create(struct pipe_screen *screen,
747 void *priv, unsigned flags);
748
749 void svga_toggle_render_condition(struct svga_context *svga,
750 boolean render_condition_enabled,
751 boolean on);
752
753 /***********************************************************************
754 * Inline conversion functions. These are better-typed than the
755 * macros used previously:
756 */
757 static inline struct svga_context *
758 svga_context( struct pipe_context *pipe )
759 {
760 return (struct svga_context *)pipe;
761 }
762
763 static inline struct svga_winsys_screen *
764 svga_sws(struct svga_context *svga)
765 {
766 return svga_screen(svga->pipe.screen)->sws;
767 }
768
769 static inline boolean
770 svga_have_gb_objects(const struct svga_context *svga)
771 {
772 return svga_screen(svga->pipe.screen)->sws->have_gb_objects;
773 }
774
775 static inline boolean
776 svga_have_gb_dma(const struct svga_context *svga)
777 {
778 return svga_screen(svga->pipe.screen)->sws->have_gb_dma;
779 }
780
781 static inline boolean
782 svga_have_vgpu10(const struct svga_context *svga)
783 {
784 return svga_screen(svga->pipe.screen)->sws->have_vgpu10;
785 }
786
787 static inline boolean
788 svga_have_sm4_1(const struct svga_context *svga)
789 {
790 return svga_screen(svga->pipe.screen)->sws->have_sm4_1;
791 }
792
793 static inline boolean
794 svga_have_sm5(const struct svga_context *svga)
795 {
796 return svga_screen(svga->pipe.screen)->sws->have_sm5;
797 }
798
799 static inline boolean
800 svga_need_to_rebind_resources(const struct svga_context *svga)
801 {
802 return svga_screen(svga->pipe.screen)->sws->need_to_rebind_resources;
803 }
804
805 static inline boolean
806 svga_rects_equal(const SVGA3dRect *r1, const SVGA3dRect *r2)
807 {
808 return memcmp(r1, r2, sizeof(*r1)) == 0;
809 }
810
811 /**
812 * If the Gallium HUD is enabled, this will return the current time.
813 * Otherwise, just return zero.
814 */
815 static inline int64_t
816 svga_get_time(struct svga_context *svga)
817 {
818 return svga->hud.uses_time ? os_time_get() : 0;
819 }
820
821 /*
822 * The SVGA_TRY_XX family of macros can be used to optionally replace a
823 * function call with an error value, the purpose is to trigger and test
824 * retry path handling.
825 */
826 #ifdef DEBUG
827
828 /*
829 * Optionally replace a function call with a PIPE_ERROR_OUT_OF_MEMORY
830 * return value
831 */
832 #define SVGA_TRY(_func) \
833 ((SVGA_DEBUG & DEBUG_RETRY) ? PIPE_ERROR_OUT_OF_MEMORY : (_func))
834
835 /* Optionally replace a function call with a NULL return value */
836 #define SVGA_TRY_PTR(_func) \
837 ((SVGA_DEBUG & DEBUG_RETRY) ? NULL : (_func))
838
839 /*
840 * Optionally replace a function call with a NULL return value, and set
841 * the _retry parameter to TRUE.
842 */
843 #define SVGA_TRY_MAP(_func, _retry) \
844 ((SVGA_DEBUG & DEBUG_RETRY) ? (_retry) = TRUE, NULL : (_func))
845 #else
846
847 #define SVGA_TRY(_func) (_func)
848
849 #define SVGA_TRY_PTR(_func) (_func)
850
851 #define SVGA_TRY_MAP(_func, _retry) (_func)
852 #endif
853
854 /**
855 * Enter retry processing after hitting out-of-command space
856 */
857 static inline void
858 svga_retry_enter(struct svga_context *svga)
859 {
860 /* We shouldn't nest retries, but currently we do. */
861 if ((SVGA_DEBUG & DEBUG_RETRY) && svga->swc->in_retry) {
862 debug_printf("WARNING: Recursive retry. Level: %u.\n",
863 svga->swc->in_retry);
864 }
865 svga->swc->in_retry++;
866 }
867
868 /**
869 * Exit retry processing after hitting out-of-command space
870 */
871 static inline void
872 svga_retry_exit(struct svga_context *svga)
873 {
874 assert(svga->swc->in_retry > 0);
875 svga->swc->in_retry--;
876 }
877
878 /**
879 * Perform a function call, and on failure flush the context and retry,
880 * asserting that the retry succeeded. On return, the boolean argument
881 * _retried indicates whether the function call was retried or not.
882 */
883 #define SVGA_RETRY_CHECK(_svga, _func, _retried) \
884 do { \
885 enum pipe_error ret; \
886 \
887 ret = SVGA_TRY(_func); \
888 (_retried) = (ret != PIPE_OK); \
889 if (_retried) { \
890 svga_retry_enter(_svga); \
891 svga_context_flush(_svga, NULL); \
892 ret = (_func); \
893 assert(ret == PIPE_OK); \
894 svga_retry_exit(_svga); \
895 } \
896 } while(0)
897
898 /**
899 * Perform a function call, and on failure flush the context and retry,
900 * asserting that the retry succeeded.
901 */
902 #define SVGA_RETRY(_svga, _func) \
903 do { \
904 UNUSED boolean retried; \
905 \
906 SVGA_RETRY_CHECK(_svga, _func, retried); \
907 } while(0)
908
909 /**
910 * Perform a function call, and on out-of-memory, flush the context and
911 * retry. The retry return value is stored in _ret for reuse.
912 */
913 #define SVGA_RETRY_OOM(_svga, _ret, _func) \
914 do { \
915 (_ret) = SVGA_TRY(_func); \
916 if ((_ret) == PIPE_ERROR_OUT_OF_MEMORY) { \
917 svga_retry_enter(_svga); \
918 svga_context_flush(_svga, NULL); \
919 (_ret) = (_func); \
920 svga_retry_exit(_svga); \
921 } \
922 } while (0);
923
924 #endif