1848b8605Smrg/**********************************************************
2848b8605Smrg * Copyright 2008-2009 VMware, Inc.  All rights reserved.
3848b8605Smrg *
4848b8605Smrg * Permission is hereby granted, free of charge, to any person
5848b8605Smrg * obtaining a copy of this software and associated documentation
6848b8605Smrg * files (the "Software"), to deal in the Software without
7848b8605Smrg * restriction, including without limitation the rights to use, copy,
8848b8605Smrg * modify, merge, publish, distribute, sublicense, and/or sell copies
9848b8605Smrg * of the Software, and to permit persons to whom the Software is
10848b8605Smrg * furnished to do so, subject to the following conditions:
11848b8605Smrg *
12848b8605Smrg * The above copyright notice and this permission notice shall be
13848b8605Smrg * included in all copies or substantial portions of the Software.
14848b8605Smrg *
15848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16848b8605Smrg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18848b8605Smrg * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19848b8605Smrg * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20848b8605Smrg * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21848b8605Smrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22848b8605Smrg * SOFTWARE.
23848b8605Smrg *
24848b8605Smrg **********************************************************/
25848b8605Smrg
26848b8605Smrg#ifndef SVGA_CONTEXT_H
27848b8605Smrg#define SVGA_CONTEXT_H
28848b8605Smrg
29848b8605Smrg
30848b8605Smrg#include "pipe/p_context.h"
31848b8605Smrg#include "pipe/p_defines.h"
32848b8605Smrg#include "pipe/p_state.h"
33848b8605Smrg
34b8e80941Smrg#include "util/os_time.h"
35b8e80941Smrg
36848b8605Smrg#include "util/u_blitter.h"
37b8e80941Smrg#include "util/list.h"
38848b8605Smrg
39848b8605Smrg#include "tgsi/tgsi_scan.h"
40848b8605Smrg
41848b8605Smrg#include "svga_screen.h"
42848b8605Smrg#include "svga_state.h"
43848b8605Smrg#include "svga_winsys.h"
44848b8605Smrg#include "svga_hw_reg.h"
45848b8605Smrg#include "svga3d_shaderdefs.h"
46848b8605Smrg
47848b8605Smrg
48848b8605Smrg/** Non-GPU queries for gallium HUD */
49b8e80941Smrgenum svga_hud {
50b8e80941Smrg/* per-frame counters */
51b8e80941Smrg   SVGA_QUERY_NUM_DRAW_CALLS = PIPE_QUERY_DRIVER_SPECIFIC,
52b8e80941Smrg   SVGA_QUERY_NUM_FALLBACKS,
53b8e80941Smrg   SVGA_QUERY_NUM_FLUSHES,
54b8e80941Smrg   SVGA_QUERY_NUM_VALIDATIONS,
55b8e80941Smrg   SVGA_QUERY_MAP_BUFFER_TIME,
56b8e80941Smrg   SVGA_QUERY_NUM_BUFFERS_MAPPED,
57b8e80941Smrg   SVGA_QUERY_NUM_TEXTURES_MAPPED,
58b8e80941Smrg   SVGA_QUERY_NUM_BYTES_UPLOADED,
59b8e80941Smrg   SVGA_QUERY_COMMAND_BUFFER_SIZE,
60b8e80941Smrg   SVGA_QUERY_FLUSH_TIME,
61b8e80941Smrg   SVGA_QUERY_SURFACE_WRITE_FLUSHES,
62b8e80941Smrg   SVGA_QUERY_NUM_READBACKS,
63b8e80941Smrg   SVGA_QUERY_NUM_RESOURCE_UPDATES,
64b8e80941Smrg   SVGA_QUERY_NUM_BUFFER_UPLOADS,
65b8e80941Smrg   SVGA_QUERY_NUM_CONST_BUF_UPDATES,
66b8e80941Smrg   SVGA_QUERY_NUM_CONST_UPDATES,
67b8e80941Smrg
68b8e80941Smrg/* running total counters */
69b8e80941Smrg   SVGA_QUERY_MEMORY_USED,
70b8e80941Smrg   SVGA_QUERY_NUM_SHADERS,
71b8e80941Smrg   SVGA_QUERY_NUM_RESOURCES,
72b8e80941Smrg   SVGA_QUERY_NUM_STATE_OBJECTS,
73b8e80941Smrg   SVGA_QUERY_NUM_SURFACE_VIEWS,
74b8e80941Smrg   SVGA_QUERY_NUM_GENERATE_MIPMAP,
75b8e80941Smrg   SVGA_QUERY_NUM_FAILED_ALLOCATIONS,
76b8e80941Smrg   SVGA_QUERY_NUM_COMMANDS_PER_DRAW,
77b8e80941Smrg
78b8e80941Smrg/*SVGA_QUERY_MAX has to be last because it is size of an array*/
79b8e80941Smrg   SVGA_QUERY_MAX
80b8e80941Smrg};
81b8e80941Smrg
82b8e80941Smrg/**
83b8e80941Smrg * Maximum supported number of constant buffers per shader
84b8e80941Smrg */
85b8e80941Smrg#define SVGA_MAX_CONST_BUFS 14
86848b8605Smrg
87b8e80941Smrg/**
88b8e80941Smrg * Maximum constant buffer size that can be set in the
89b8e80941Smrg * DXSetSingleConstantBuffer command is
90b8e80941Smrg * DX10 constant buffer element count * 4 4-bytes components
91b8e80941Smrg */
92b8e80941Smrg#define SVGA_MAX_CONST_BUF_SIZE (4096 * 4 * sizeof(int))
93b8e80941Smrg
94b8e80941Smrg#define CONST0_UPLOAD_ALIGNMENT 256
95848b8605Smrg
96848b8605Smrgstruct draw_vertex_shader;
97848b8605Smrgstruct draw_fragment_shader;
98848b8605Smrgstruct svga_shader_variant;
99848b8605Smrgstruct SVGACmdMemory;
100848b8605Smrgstruct util_bitmask;
101848b8605Smrg
102848b8605Smrg
103848b8605Smrgstruct svga_cache_context;
104848b8605Smrgstruct svga_tracked_state;
105848b8605Smrg
106848b8605Smrgstruct svga_blend_state {
107b8e80941Smrg   unsigned need_white_fragments:1;
108b8e80941Smrg   unsigned independent_blend_enable:1;
109b8e80941Smrg   unsigned alpha_to_coverage:1;
110b8e80941Smrg   unsigned alpha_to_one:1;
111b8e80941Smrg   unsigned blend_color_alpha:1;  /**< set blend color to alpha value */
112848b8605Smrg
113b8e80941Smrg   /** Per-render target state */
114848b8605Smrg   struct {
115848b8605Smrg      uint8_t writemask;
116848b8605Smrg
117848b8605Smrg      boolean blend_enable;
118848b8605Smrg      uint8_t srcblend;
119848b8605Smrg      uint8_t dstblend;
120848b8605Smrg      uint8_t blendeq;
121b8e80941Smrg
122848b8605Smrg      boolean separate_alpha_blend_enable;
123848b8605Smrg      uint8_t srcblend_alpha;
124848b8605Smrg      uint8_t dstblend_alpha;
125848b8605Smrg      uint8_t blendeq_alpha;
126b8e80941Smrg   } rt[PIPE_MAX_COLOR_BUFS];
127848b8605Smrg
128b8e80941Smrg   SVGA3dBlendStateId id;  /**< vgpu10 */
129848b8605Smrg};
130848b8605Smrg
131848b8605Smrgstruct svga_depth_stencil_state {
132848b8605Smrg   unsigned zfunc:8;
133848b8605Smrg   unsigned zenable:1;
134848b8605Smrg   unsigned zwriteenable:1;
135848b8605Smrg
136848b8605Smrg   unsigned alphatestenable:1;
137848b8605Smrg   unsigned alphafunc:8;
138b8e80941Smrg
139848b8605Smrg   struct {
140848b8605Smrg      unsigned enabled:1;
141848b8605Smrg      unsigned func:8;
142848b8605Smrg      unsigned fail:8;
143848b8605Smrg      unsigned zfail:8;
144848b8605Smrg      unsigned pass:8;
145848b8605Smrg   } stencil[2];
146b8e80941Smrg
147848b8605Smrg   /* SVGA3D has one ref/mask/writemask triple shared between front &
148848b8605Smrg    * back face stencil.  We really need two:
149848b8605Smrg    */
150848b8605Smrg   unsigned stencil_mask:8;
151848b8605Smrg   unsigned stencil_writemask:8;
152848b8605Smrg
153848b8605Smrg   float    alpharef;
154b8e80941Smrg
155b8e80941Smrg   SVGA3dDepthStencilStateId id;  /**< vgpu10 */
156848b8605Smrg};
157848b8605Smrg
158848b8605Smrg#define SVGA_UNFILLED_DISABLE 0
159848b8605Smrg#define SVGA_UNFILLED_LINE    1
160848b8605Smrg#define SVGA_UNFILLED_POINT   2
161848b8605Smrg
162848b8605Smrg#define SVGA_PIPELINE_FLAG_POINTS   (1<<PIPE_PRIM_POINTS)
163848b8605Smrg#define SVGA_PIPELINE_FLAG_LINES    (1<<PIPE_PRIM_LINES)
164848b8605Smrg#define SVGA_PIPELINE_FLAG_TRIS     (1<<PIPE_PRIM_TRIANGLES)
165848b8605Smrg
166848b8605Smrgstruct svga_rasterizer_state {
167848b8605Smrg   struct pipe_rasterizer_state templ; /* needed for draw module */
168848b8605Smrg
169848b8605Smrg   unsigned shademode:8;
170848b8605Smrg   unsigned cullmode:8;
171848b8605Smrg   unsigned scissortestenable:1;
172848b8605Smrg   unsigned multisampleantialias:1;
173848b8605Smrg   unsigned antialiasedlineenable:1;
174848b8605Smrg   unsigned lastpixel:1;
175848b8605Smrg   unsigned pointsprite:1;
176848b8605Smrg
177848b8605Smrg   unsigned linepattern;
178848b8605Smrg
179848b8605Smrg   float slopescaledepthbias;
180848b8605Smrg   float depthbias;
181848b8605Smrg   float pointsize;
182848b8605Smrg   float linewidth;
183b8e80941Smrg
184b8e80941Smrg   unsigned hw_fillmode:2;         /* PIPE_POLYGON_MODE_x */
185848b8605Smrg
186848b8605Smrg   /** Which prims do we need help for?  Bitmask of (1 << PIPE_PRIM_x) flags */
187848b8605Smrg   unsigned need_pipeline:16;
188848b8605Smrg
189b8e80941Smrg   SVGA3dRasterizerStateId id;    /**< vgpu10 */
190b8e80941Smrg
191848b8605Smrg   /** For debugging: */
192848b8605Smrg   const char* need_pipeline_tris_str;
193848b8605Smrg   const char* need_pipeline_lines_str;
194848b8605Smrg   const char* need_pipeline_points_str;
195848b8605Smrg};
196848b8605Smrg
197848b8605Smrgstruct svga_sampler_state {
198848b8605Smrg   unsigned mipfilter;
199848b8605Smrg   unsigned magfilter;
200848b8605Smrg   unsigned minfilter;
201848b8605Smrg   unsigned aniso_level;
202848b8605Smrg   float lod_bias;
203848b8605Smrg   unsigned addressu;
204848b8605Smrg   unsigned addressv;
205848b8605Smrg   unsigned addressw;
206848b8605Smrg   unsigned bordercolor;
207848b8605Smrg   unsigned normalized_coords:1;
208848b8605Smrg   unsigned compare_mode:1;
209848b8605Smrg   unsigned compare_func:3;
210848b8605Smrg
211848b8605Smrg   unsigned min_lod;
212848b8605Smrg   unsigned view_min_lod;
213848b8605Smrg   unsigned view_max_lod;
214b8e80941Smrg
215b8e80941Smrg   SVGA3dSamplerId id[2];
216b8e80941Smrg};
217b8e80941Smrg
218b8e80941Smrg
219b8e80941Smrgstruct svga_pipe_sampler_view
220b8e80941Smrg{
221b8e80941Smrg   struct pipe_sampler_view base;
222b8e80941Smrg
223b8e80941Smrg   SVGA3dShaderResourceViewId id;
224848b8605Smrg};
225848b8605Smrg
226b8e80941Smrg
227b8e80941Smrgstatic inline struct svga_pipe_sampler_view *
228b8e80941Smrgsvga_pipe_sampler_view(struct pipe_sampler_view *v)
229b8e80941Smrg{
230b8e80941Smrg   return (struct svga_pipe_sampler_view *) v;
231b8e80941Smrg}
232b8e80941Smrg
233b8e80941Smrg
234848b8605Smrgstruct svga_velems_state {
235848b8605Smrg   unsigned count;
236848b8605Smrg   struct pipe_vertex_element velem[PIPE_MAX_ATTRIBS];
237848b8605Smrg   SVGA3dDeclType decl_type[PIPE_MAX_ATTRIBS]; /**< vertex attrib formats */
238b8e80941Smrg
239b8e80941Smrg   /** Bitmasks indicating which attributes need format conversion */
240b8e80941Smrg   unsigned adjust_attrib_range;     /**< range adjustment */
241b8e80941Smrg   unsigned attrib_is_pure_int;      /**< pure int */
242b8e80941Smrg   unsigned adjust_attrib_w_1;       /**< set w = 1 */
243b8e80941Smrg   unsigned adjust_attrib_itof;      /**< int->float */
244b8e80941Smrg   unsigned adjust_attrib_utof;      /**< uint->float */
245b8e80941Smrg   unsigned attrib_is_bgra;          /**< R / B swizzling */
246b8e80941Smrg   unsigned attrib_puint_to_snorm;   /**< 10_10_10_2 packed uint -> snorm */
247b8e80941Smrg   unsigned attrib_puint_to_uscaled; /**< 10_10_10_2 packed uint -> uscaled */
248b8e80941Smrg   unsigned attrib_puint_to_sscaled; /**< 10_10_10_2 packed uint -> sscaled */
249b8e80941Smrg
250848b8605Smrg   boolean need_swvfetch;
251b8e80941Smrg
252b8e80941Smrg   SVGA3dElementLayoutId id; /**< VGPU10 */
253848b8605Smrg};
254848b8605Smrg
255b8e80941Smrg
256848b8605Smrg/* Use to calculate differences between state emitted to hardware and
257b8e80941Smrg * current driver-calculated state.
258848b8605Smrg */
259b8e80941Smrgstruct svga_state
260848b8605Smrg{
261848b8605Smrg   const struct svga_blend_state *blend;
262848b8605Smrg   const struct svga_depth_stencil_state *depth;
263848b8605Smrg   const struct svga_rasterizer_state *rast;
264b8e80941Smrg   const struct svga_sampler_state *sampler[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
265848b8605Smrg   const struct svga_velems_state *velems;
266848b8605Smrg
267b8e80941Smrg   struct pipe_sampler_view *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS]; /* or texture ID's? */
268848b8605Smrg   struct svga_fragment_shader *fs;
269848b8605Smrg   struct svga_vertex_shader *vs;
270b8e80941Smrg   struct svga_geometry_shader *user_gs; /* user-specified GS */
271b8e80941Smrg   struct svga_geometry_shader *gs;      /* derived GS */
272848b8605Smrg
273848b8605Smrg   struct pipe_vertex_buffer vb[PIPE_MAX_ATTRIBS];
274b8e80941Smrg   /** Constant buffers for each shader.
275b8e80941Smrg    * The size should probably always match with that of
276b8e80941Smrg    * svga_shader_emitter_v10.num_shader_consts.
277b8e80941Smrg    */
278b8e80941Smrg   struct pipe_constant_buffer constbufs[PIPE_SHADER_TYPES][SVGA_MAX_CONST_BUFS];
279848b8605Smrg
280848b8605Smrg   struct pipe_framebuffer_state framebuffer;
281848b8605Smrg   float depthscale;
282848b8605Smrg
283848b8605Smrg   /* Hack to limit the number of different render targets between
284848b8605Smrg    * flushes.  Helps avoid blowing out our surface cache in EXA.
285848b8605Smrg    */
286848b8605Smrg   int nr_fbs;
287848b8605Smrg
288848b8605Smrg   struct pipe_poly_stipple poly_stipple;
289848b8605Smrg   struct pipe_scissor_state scissor;
290848b8605Smrg   struct pipe_blend_color blend_color;
291848b8605Smrg   struct pipe_stencil_ref stencil_ref;
292848b8605Smrg   struct pipe_clip_state clip;
293848b8605Smrg   struct pipe_viewport_state viewport;
294848b8605Smrg
295b8e80941Smrg   unsigned num_samplers[PIPE_SHADER_TYPES];
296b8e80941Smrg   unsigned num_sampler_views[PIPE_SHADER_TYPES];
297848b8605Smrg   unsigned num_vertex_buffers;
298b8e80941Smrg   enum pipe_prim_type reduced_prim;
299848b8605Smrg
300848b8605Smrg   struct {
301848b8605Smrg      unsigned flag_1d;
302848b8605Smrg      unsigned flag_srgb;
303848b8605Smrg   } tex_flags;
304b8e80941Smrg
305b8e80941Smrg   unsigned sample_mask;
306848b8605Smrg};
307848b8605Smrg
308848b8605Smrgstruct svga_prescale {
309848b8605Smrg   float translate[4];
310848b8605Smrg   float scale[4];
311848b8605Smrg   boolean enabled;
312848b8605Smrg};
313848b8605Smrg
314848b8605Smrg
315848b8605Smrg/* Updated by calling svga_update_state( SVGA_STATE_HW_CLEAR )
316848b8605Smrg */
317848b8605Smrgstruct svga_hw_clear_state
318848b8605Smrg{
319b8e80941Smrg   SVGA3dRect viewport;
320848b8605Smrg
321848b8605Smrg   struct {
322848b8605Smrg      float zmin, zmax;
323848b8605Smrg   } depthrange;
324b8e80941Smrg
325848b8605Smrg   struct pipe_framebuffer_state framebuffer;
326848b8605Smrg   struct svga_prescale prescale;
327b8e80941Smrg
328b8e80941Smrg   /* VGPU10 state */
329b8e80941Smrg   unsigned num_rendertargets;
330b8e80941Smrg   struct pipe_surface *rtv[SVGA3D_MAX_RENDER_TARGETS];
331b8e80941Smrg   struct pipe_surface *dsv;
332848b8605Smrg};
333848b8605Smrg
334848b8605Smrgstruct svga_hw_view_state
335848b8605Smrg{
336848b8605Smrg   struct pipe_resource *texture;
337848b8605Smrg   struct svga_sampler_view *v;
338848b8605Smrg   unsigned min_lod;
339848b8605Smrg   unsigned max_lod;
340b8e80941Smrg   boolean dirty;
341848b8605Smrg};
342848b8605Smrg
343848b8605Smrg/* Updated by calling svga_update_state( SVGA_STATE_HW_DRAW )
344848b8605Smrg */
345848b8605Smrgstruct svga_hw_draw_state
346848b8605Smrg{
347b8e80941Smrg   /** VGPU9 rasterization state */
348848b8605Smrg   unsigned rs[SVGA3D_RS_MAX];
349b8e80941Smrg   /** VGPU9 texture sampler and bindings state */
350848b8605Smrg   unsigned ts[SVGA3D_PIXEL_SAMPLERREG_MAX][SVGA3D_TS_MAX];
351848b8605Smrg
352b8e80941Smrg   /** VGPU9 texture views */
353b8e80941Smrg   unsigned num_views;
354b8e80941Smrg   unsigned num_backed_views; /* views with backing copy of texture */
355b8e80941Smrg   struct svga_hw_view_state views[PIPE_MAX_SAMPLERS];
356848b8605Smrg
357b8e80941Smrg   /** VGPU9 constant buffer values */
358b8e80941Smrg   float cb[PIPE_SHADER_TYPES][SVGA3D_CONSTREG_MAX][4];
359b8e80941Smrg
360b8e80941Smrg   /** Currently bound shaders */
361848b8605Smrg   struct svga_shader_variant *fs;
362848b8605Smrg   struct svga_shader_variant *vs;
363b8e80941Smrg   struct svga_shader_variant *gs;
364848b8605Smrg
365b8e80941Smrg   /** Currently bound constant buffer, per shader stage */
366b8e80941Smrg   struct pipe_resource *constbuf[PIPE_SHADER_TYPES];
367b8e80941Smrg
368b8e80941Smrg   /** Bitmask of enabled constant buffers */
369b8e80941Smrg   unsigned enabled_constbufs[PIPE_SHADER_TYPES];
370b8e80941Smrg
371b8e80941Smrg   /**
372b8e80941Smrg    * These are used to reduce the number of times we call u_upload_unmap()
373b8e80941Smrg    * while updating the zero-th/default VGPU10 constant buffer.
374b8e80941Smrg    */
375b8e80941Smrg   struct pipe_resource *const0_buffer;
376b8e80941Smrg   struct svga_winsys_surface *const0_handle;
377b8e80941Smrg
378b8e80941Smrg   /** VGPU10 HW state (used to prevent emitting redundant state) */
379b8e80941Smrg   SVGA3dDepthStencilStateId depth_stencil_id;
380b8e80941Smrg   unsigned stencil_ref;
381b8e80941Smrg   SVGA3dBlendStateId blend_id;
382b8e80941Smrg   float blend_factor[4];
383b8e80941Smrg   unsigned blend_sample_mask;
384b8e80941Smrg   SVGA3dRasterizerStateId rasterizer_id;
385b8e80941Smrg   SVGA3dElementLayoutId layout_id;
386b8e80941Smrg   SVGA3dPrimitiveType topology;
387b8e80941Smrg
388b8e80941Smrg   /** Vertex buffer state */
389b8e80941Smrg   SVGA3dVertexBuffer vbuffer_attrs[PIPE_MAX_ATTRIBS];
390b8e80941Smrg   struct pipe_resource *vbuffers[PIPE_MAX_ATTRIBS];
391b8e80941Smrg   unsigned num_vbuffers;
392b8e80941Smrg
393b8e80941Smrg   struct pipe_resource *ib;  /**< index buffer for drawing */
394b8e80941Smrg   SVGA3dSurfaceFormat ib_format;
395b8e80941Smrg   unsigned ib_offset;
396b8e80941Smrg
397b8e80941Smrg   unsigned num_samplers[PIPE_SHADER_TYPES];
398b8e80941Smrg   SVGA3dSamplerId samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
399b8e80941Smrg
400b8e80941Smrg   unsigned num_sampler_views[PIPE_SHADER_TYPES];
401b8e80941Smrg   struct pipe_sampler_view
402b8e80941Smrg      *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
403b8e80941Smrg
404b8e80941Smrg   /* used for rebinding */
405b8e80941Smrg   unsigned default_constbuf_size[PIPE_SHADER_TYPES];
406b8e80941Smrg
407b8e80941Smrg   boolean rasterizer_discard; /* set if rasterization is disabled */
408b8e80941Smrg   boolean has_backed_views;   /* set if any of the rtv/dsv is a backed surface view */
409848b8605Smrg};
410848b8605Smrg
411848b8605Smrg
412848b8605Smrg/* Updated by calling svga_update_state( SVGA_STATE_NEED_SWTNL )
413848b8605Smrg */
414848b8605Smrgstruct svga_sw_state
415848b8605Smrg{
416848b8605Smrg   /* which parts we need */
417848b8605Smrg   boolean need_swvfetch;
418848b8605Smrg   boolean need_pipeline;
419848b8605Smrg   boolean need_swtnl;
420848b8605Smrg
421848b8605Smrg   /* Flag to make sure that need sw is on while
422848b8605Smrg    * updating state within a swtnl call.
423848b8605Smrg    */
424848b8605Smrg   boolean in_swtnl_draw;
425848b8605Smrg};
426848b8605Smrg
427848b8605Smrg
428848b8605Smrg/* Queue some state updates (like rss) and submit them to hardware in
429848b8605Smrg * a single packet.
430848b8605Smrg */
431848b8605Smrgstruct svga_hw_queue;
432848b8605Smrg
433848b8605Smrgstruct svga_query;
434b8e80941Smrgstruct svga_qmem_alloc_entry;
435848b8605Smrg
436848b8605Smrgstruct svga_context
437848b8605Smrg{
438848b8605Smrg   struct pipe_context pipe;
439848b8605Smrg   struct svga_winsys_context *swc;
440848b8605Smrg   struct blitter_context *blitter;
441b8e80941Smrg   struct u_upload_mgr *const0_upload;
442b8e80941Smrg   struct u_upload_mgr *tex_upload;
443848b8605Smrg
444848b8605Smrg   struct {
445848b8605Smrg      boolean no_swtnl;
446848b8605Smrg      boolean force_swtnl;
447848b8605Smrg      boolean use_min_mipmap;
448848b8605Smrg
449848b8605Smrg      /* incremented for each shader */
450848b8605Smrg      unsigned shader_id;
451848b8605Smrg
452848b8605Smrg      boolean no_line_width;
453848b8605Smrg      boolean force_hw_line_stipple;
454b8e80941Smrg
455b8e80941Smrg      /** To report perf/conformance/etc issues to the state tracker */
456b8e80941Smrg      struct pipe_debug_callback callback;
457848b8605Smrg   } debug;
458848b8605Smrg
459848b8605Smrg   struct {
460848b8605Smrg      struct draw_context *draw;
461848b8605Smrg      struct vbuf_render *backend;
462848b8605Smrg      unsigned hw_prim;
463848b8605Smrg      boolean new_vbuf;
464848b8605Smrg      boolean new_vdecl;
465848b8605Smrg   } swtnl;
466848b8605Smrg
467b8e80941Smrg   /* Bitmask of blend state objects IDs */
468b8e80941Smrg   struct util_bitmask *blend_object_id_bm;
469b8e80941Smrg
470b8e80941Smrg   /* Bitmask of depth/stencil state objects IDs */
471b8e80941Smrg   struct util_bitmask *ds_object_id_bm;
472b8e80941Smrg
473b8e80941Smrg   /* Bitmaks of input element object IDs */
474b8e80941Smrg   struct util_bitmask *input_element_object_id_bm;
475b8e80941Smrg
476b8e80941Smrg   /* Bitmask of rasterizer object IDs */
477b8e80941Smrg   struct util_bitmask *rast_object_id_bm;
478b8e80941Smrg
479b8e80941Smrg   /* Bitmask of sampler state objects IDs */
480b8e80941Smrg   struct util_bitmask *sampler_object_id_bm;
481b8e80941Smrg
482b8e80941Smrg   /* Bitmask of sampler view IDs */
483b8e80941Smrg   struct util_bitmask *sampler_view_id_bm;
484b8e80941Smrg
485848b8605Smrg   /* Bitmask of used shader IDs */
486848b8605Smrg   struct util_bitmask *shader_id_bm;
487848b8605Smrg
488b8e80941Smrg   /* Bitmask of used surface view IDs */
489b8e80941Smrg   struct util_bitmask *surface_view_id_bm;
490b8e80941Smrg
491b8e80941Smrg   /* Bitmask of used stream output IDs */
492b8e80941Smrg   struct util_bitmask *stream_output_id_bm;
493b8e80941Smrg
494b8e80941Smrg   /* Bitmask of used query IDs */
495b8e80941Smrg   struct util_bitmask *query_id_bm;
496b8e80941Smrg
497848b8605Smrg   struct {
498848b8605Smrg      unsigned dirty[SVGA_STATE_MAX];
499848b8605Smrg
500b8e80941Smrg      /** bitmasks of which const buffers are changed */
501b8e80941Smrg      unsigned dirty_constbufs[PIPE_SHADER_TYPES];
502b8e80941Smrg
503848b8605Smrg      unsigned texture_timestamp;
504848b8605Smrg
505848b8605Smrg      struct svga_sw_state          sw;
506848b8605Smrg      struct svga_hw_draw_state     hw_draw;
507848b8605Smrg      struct svga_hw_clear_state    hw_clear;
508848b8605Smrg   } state;
509848b8605Smrg
510848b8605Smrg   struct svga_state curr;      /* state from the state tracker */
511848b8605Smrg   unsigned dirty;              /* statechanges since last update_state() */
512848b8605Smrg
513b8e80941Smrg   union {
514b8e80941Smrg      struct {
515b8e80941Smrg         unsigned rendertargets:1;
516b8e80941Smrg         unsigned texture_samplers:1;
517b8e80941Smrg         unsigned constbufs:1;
518b8e80941Smrg         unsigned vs:1;
519b8e80941Smrg         unsigned fs:1;
520b8e80941Smrg         unsigned gs:1;
521b8e80941Smrg         unsigned query:1;
522b8e80941Smrg      } flags;
523b8e80941Smrg      unsigned val;
524848b8605Smrg   } rebind;
525848b8605Smrg
526848b8605Smrg   struct svga_hwtnl *hwtnl;
527848b8605Smrg
528b8e80941Smrg   /** Queries states */
529b8e80941Smrg   struct svga_winsys_gb_query *gb_query;     /**< gb query object, one per context */
530b8e80941Smrg   unsigned gb_query_len;                     /**< gb query object size */
531b8e80941Smrg   struct util_bitmask *gb_query_alloc_mask;  /**< gb query object allocation mask */
532b8e80941Smrg   struct svga_qmem_alloc_entry *gb_query_map[SVGA_QUERY_MAX];
533b8e80941Smrg                                              /**< query mem block mapping */
534b8e80941Smrg   struct svga_query *sq[SVGA_QUERY_MAX];     /**< queries currently in progress */
535848b8605Smrg
536848b8605Smrg   /** List of buffers with queued transfers */
537848b8605Smrg   struct list_head dirty_buffers;
538848b8605Smrg
539b8e80941Smrg   /** performance / info queries for HUD */
540b8e80941Smrg   struct {
541b8e80941Smrg      uint64_t num_draw_calls;          /**< SVGA_QUERY_DRAW_CALLS */
542b8e80941Smrg      uint64_t num_fallbacks;           /**< SVGA_QUERY_NUM_FALLBACKS */
543b8e80941Smrg      uint64_t num_flushes;             /**< SVGA_QUERY_NUM_FLUSHES */
544b8e80941Smrg      uint64_t num_validations;         /**< SVGA_QUERY_NUM_VALIDATIONS */
545b8e80941Smrg      uint64_t map_buffer_time;         /**< SVGA_QUERY_MAP_BUFFER_TIME */
546b8e80941Smrg      uint64_t num_buffers_mapped;      /**< SVGA_QUERY_NUM_BUFFERS_MAPPED */
547b8e80941Smrg      uint64_t num_textures_mapped;     /**< SVGA_QUERY_NUM_TEXTURES_MAPPED */
548b8e80941Smrg      uint64_t command_buffer_size;     /**< SVGA_QUERY_COMMAND_BUFFER_SIZE */
549b8e80941Smrg      uint64_t flush_time;              /**< SVGA_QUERY_FLUSH_TIME */
550b8e80941Smrg      uint64_t surface_write_flushes;   /**< SVGA_QUERY_SURFACE_WRITE_FLUSHES */
551b8e80941Smrg      uint64_t num_readbacks;           /**< SVGA_QUERY_NUM_READBACKS */
552b8e80941Smrg      uint64_t num_resource_updates;    /**< SVGA_QUERY_NUM_RESOURCE_UPDATES */
553b8e80941Smrg      uint64_t num_buffer_uploads;      /**< SVGA_QUERY_NUM_BUFFER_UPLOADS */
554b8e80941Smrg      uint64_t num_const_buf_updates;   /**< SVGA_QUERY_NUM_CONST_BUF_UPDATES */
555b8e80941Smrg      uint64_t num_const_updates;       /**< SVGA_QUERY_NUM_CONST_UPDATES */
556b8e80941Smrg      uint64_t num_shaders;             /**< SVGA_QUERY_NUM_SHADERS */
557b8e80941Smrg
558b8e80941Smrg      /** The following are summed for SVGA_QUERY_NUM_STATE_OBJECTS */
559b8e80941Smrg      uint64_t num_blend_objects;
560b8e80941Smrg      uint64_t num_depthstencil_objects;
561b8e80941Smrg      uint64_t num_rasterizer_objects;
562b8e80941Smrg      uint64_t num_sampler_objects;
563b8e80941Smrg      uint64_t num_samplerview_objects;
564b8e80941Smrg      uint64_t num_vertexelement_objects;
565b8e80941Smrg
566b8e80941Smrg      uint64_t num_surface_views;       /**< SVGA_QUERY_NUM_SURFACE_VIEWS */
567b8e80941Smrg      uint64_t num_bytes_uploaded;      /**< SVGA_QUERY_NUM_BYTES_UPLOADED */
568b8e80941Smrg      uint64_t num_generate_mipmap;     /**< SVGA_QUERY_NUM_GENERATE_MIPMAP */
569b8e80941Smrg
570b8e80941Smrg      boolean uses_time;                /**< os_time_get() calls needed? */
571b8e80941Smrg   } hud;
572b8e80941Smrg
573b8e80941Smrg   /** The currently bound stream output targets */
574b8e80941Smrg   unsigned num_so_targets;
575b8e80941Smrg   struct svga_winsys_surface *so_surfaces[SVGA3D_DX_MAX_SOTARGETS];
576b8e80941Smrg   struct pipe_stream_output_target *so_targets[SVGA3D_DX_MAX_SOTARGETS];
577b8e80941Smrg   struct svga_stream_output *current_so;
578b8e80941Smrg
579b8e80941Smrg   /** A blend state with blending disabled, for falling back to when blending
580b8e80941Smrg    * is illegal (e.g. an integer texture is bound)
581b8e80941Smrg    */
582b8e80941Smrg   struct svga_blend_state *noop_blend;
583b8e80941Smrg
584b8e80941Smrg   struct {
585b8e80941Smrg      struct pipe_resource *texture;
586b8e80941Smrg      struct svga_pipe_sampler_view *sampler_view;
587b8e80941Smrg      void *sampler;
588b8e80941Smrg   } polygon_stipple;
589b8e80941Smrg
590b8e80941Smrg   /** Alternate rasterizer states created for point sprite */
591b8e80941Smrg   struct svga_rasterizer_state *rasterizer_no_cull[2];
592b8e80941Smrg
593b8e80941Smrg   /** Depth stencil state created to disable depth stencil test */
594b8e80941Smrg   struct svga_depth_stencil_state *depthstencil_disable;
595b8e80941Smrg
596b8e80941Smrg   /** Current conditional rendering predicate */
597b8e80941Smrg   struct {
598b8e80941Smrg      SVGA3dQueryId query_id;
599b8e80941Smrg      boolean cond;
600b8e80941Smrg   } pred;
601b8e80941Smrg
602b8e80941Smrg   boolean render_condition;
603b8e80941Smrg   boolean disable_rasterizer; /* Set if to disable rasterization */
604848b8605Smrg};
605848b8605Smrg
606848b8605Smrg/* A flag for each state_tracker state object:
607848b8605Smrg */
608848b8605Smrg#define SVGA_NEW_BLEND               0x1
609b8e80941Smrg#define SVGA_NEW_DEPTH_STENCIL_ALPHA 0x2
610848b8605Smrg#define SVGA_NEW_RAST                0x4
611848b8605Smrg#define SVGA_NEW_SAMPLER             0x8
612848b8605Smrg#define SVGA_NEW_TEXTURE             0x10
613848b8605Smrg#define SVGA_NEW_VBUFFER             0x20
614848b8605Smrg#define SVGA_NEW_VELEMENT            0x40
615848b8605Smrg#define SVGA_NEW_FS                  0x80
616848b8605Smrg#define SVGA_NEW_VS                  0x100
617848b8605Smrg#define SVGA_NEW_FS_CONST_BUFFER     0x200
618848b8605Smrg#define SVGA_NEW_VS_CONST_BUFFER     0x400
619848b8605Smrg#define SVGA_NEW_FRAME_BUFFER        0x800
620848b8605Smrg#define SVGA_NEW_STIPPLE             0x1000
621848b8605Smrg#define SVGA_NEW_SCISSOR             0x2000
622848b8605Smrg#define SVGA_NEW_BLEND_COLOR         0x4000
623848b8605Smrg#define SVGA_NEW_CLIP                0x8000
624848b8605Smrg#define SVGA_NEW_VIEWPORT            0x10000
625848b8605Smrg#define SVGA_NEW_PRESCALE            0x20000
626848b8605Smrg#define SVGA_NEW_REDUCED_PRIMITIVE   0x40000
627848b8605Smrg#define SVGA_NEW_TEXTURE_BINDING     0x80000
628848b8605Smrg#define SVGA_NEW_NEED_PIPELINE       0x100000
629848b8605Smrg#define SVGA_NEW_NEED_SWVFETCH       0x200000
630848b8605Smrg#define SVGA_NEW_NEED_SWTNL          0x400000
631848b8605Smrg#define SVGA_NEW_FS_VARIANT          0x800000
632848b8605Smrg#define SVGA_NEW_VS_VARIANT          0x1000000
633848b8605Smrg#define SVGA_NEW_TEXTURE_FLAGS       0x4000000
634848b8605Smrg#define SVGA_NEW_STENCIL_REF         0x8000000
635b8e80941Smrg#define SVGA_NEW_GS                  0x10000000
636b8e80941Smrg#define SVGA_NEW_GS_CONST_BUFFER     0x20000000
637b8e80941Smrg#define SVGA_NEW_GS_VARIANT          0x40000000
638b8e80941Smrg#define SVGA_NEW_TEXTURE_CONSTS      0x80000000
639848b8605Smrg
640848b8605Smrg
641848b8605Smrgvoid svga_init_state_functions( struct svga_context *svga );
642848b8605Smrgvoid svga_init_flush_functions( struct svga_context *svga );
643848b8605Smrgvoid svga_init_string_functions( struct svga_context *svga );
644848b8605Smrgvoid svga_init_blit_functions(struct svga_context *svga);
645848b8605Smrg
646848b8605Smrgvoid svga_init_blend_functions( struct svga_context *svga );
647848b8605Smrgvoid svga_init_depth_stencil_functions( struct svga_context *svga );
648848b8605Smrgvoid svga_init_misc_functions( struct svga_context *svga );
649848b8605Smrgvoid svga_init_rasterizer_functions( struct svga_context *svga );
650848b8605Smrgvoid svga_init_sampler_functions( struct svga_context *svga );
651848b8605Smrgvoid svga_init_fs_functions( struct svga_context *svga );
652848b8605Smrgvoid svga_init_vs_functions( struct svga_context *svga );
653b8e80941Smrgvoid svga_init_gs_functions( struct svga_context *svga );
654848b8605Smrgvoid svga_init_vertex_functions( struct svga_context *svga );
655848b8605Smrgvoid svga_init_constbuffer_functions( struct svga_context *svga );
656848b8605Smrgvoid svga_init_draw_functions( struct svga_context *svga );
657848b8605Smrgvoid svga_init_query_functions( struct svga_context *svga );
658848b8605Smrgvoid svga_init_surface_functions(struct svga_context *svga);
659b8e80941Smrgvoid svga_init_stream_output_functions( struct svga_context *svga );
660b8e80941Smrgvoid svga_init_clear_functions( struct svga_context *svga );
661848b8605Smrg
662848b8605Smrgvoid svga_cleanup_vertex_state( struct svga_context *svga );
663b8e80941Smrgvoid svga_cleanup_sampler_state( struct svga_context *svga );
664848b8605Smrgvoid svga_cleanup_tss_binding( struct svga_context *svga );
665848b8605Smrgvoid svga_cleanup_framebuffer( struct svga_context *svga );
666848b8605Smrg
667848b8605Smrgvoid svga_context_flush( struct svga_context *svga,
668848b8605Smrg                         struct pipe_fence_handle **pfence );
669848b8605Smrg
670b8e80941Smrgvoid svga_context_finish(struct svga_context *svga);
671b8e80941Smrg
672848b8605Smrgvoid svga_hwtnl_flush_retry( struct svga_context *svga );
673848b8605Smrgvoid svga_hwtnl_flush_buffer( struct svga_context *svga,
674848b8605Smrg                              struct pipe_resource *buffer );
675848b8605Smrg
676848b8605Smrgvoid svga_surfaces_flush(struct svga_context *svga);
677848b8605Smrg
678848b8605Smrgstruct pipe_context *
679848b8605Smrgsvga_context_create(struct pipe_screen *screen,
680b8e80941Smrg                    void *priv, unsigned flags);
681848b8605Smrg
682b8e80941Smrgvoid svga_toggle_render_condition(struct svga_context *svga,
683b8e80941Smrg                                  boolean render_condition_enabled,
684b8e80941Smrg                                  boolean on);
685848b8605Smrg
686848b8605Smrg/***********************************************************************
687848b8605Smrg * Inline conversion functions.  These are better-typed than the
688848b8605Smrg * macros used previously:
689848b8605Smrg */
690b8e80941Smrgstatic inline struct svga_context *
691848b8605Smrgsvga_context( struct pipe_context *pipe )
692848b8605Smrg{
693848b8605Smrg   return (struct svga_context *)pipe;
694848b8605Smrg}
695848b8605Smrg
696b8e80941Smrgstatic inline struct svga_winsys_screen *
697b8e80941Smrgsvga_sws(struct svga_context *svga)
698b8e80941Smrg{
699b8e80941Smrg   return svga_screen(svga->pipe.screen)->sws;
700b8e80941Smrg}
701848b8605Smrg
702b8e80941Smrgstatic inline boolean
703848b8605Smrgsvga_have_gb_objects(const struct svga_context *svga)
704848b8605Smrg{
705848b8605Smrg   return svga_screen(svga->pipe.screen)->sws->have_gb_objects;
706848b8605Smrg}
707848b8605Smrg
708b8e80941Smrgstatic inline boolean
709848b8605Smrgsvga_have_gb_dma(const struct svga_context *svga)
710848b8605Smrg{
711848b8605Smrg   return svga_screen(svga->pipe.screen)->sws->have_gb_dma;
712848b8605Smrg}
713848b8605Smrg
714b8e80941Smrgstatic inline boolean
715b8e80941Smrgsvga_have_vgpu10(const struct svga_context *svga)
716b8e80941Smrg{
717b8e80941Smrg   return svga_screen(svga->pipe.screen)->sws->have_vgpu10;
718b8e80941Smrg}
719b8e80941Smrg
720b8e80941Smrgstatic inline boolean
721b8e80941Smrgsvga_have_sm4_1(const struct svga_context *svga)
722b8e80941Smrg{
723b8e80941Smrg   return svga_screen(svga->pipe.screen)->sws->have_sm4_1;
724b8e80941Smrg}
725b8e80941Smrg
726b8e80941Smrgstatic inline boolean
727b8e80941Smrgsvga_need_to_rebind_resources(const struct svga_context *svga)
728b8e80941Smrg{
729b8e80941Smrg   return svga_screen(svga->pipe.screen)->sws->need_to_rebind_resources;
730b8e80941Smrg}
731b8e80941Smrg
732b8e80941Smrgstatic inline boolean
733b8e80941Smrgsvga_rects_equal(const SVGA3dRect *r1, const SVGA3dRect *r2)
734b8e80941Smrg{
735b8e80941Smrg   return memcmp(r1, r2, sizeof(*r1)) == 0;
736b8e80941Smrg}
737b8e80941Smrg
738b8e80941Smrg/**
739b8e80941Smrg * If the Gallium HUD is enabled, this will return the current time.
740b8e80941Smrg * Otherwise, just return zero.
741b8e80941Smrg */
742b8e80941Smrgstatic inline int64_t
743b8e80941Smrgsvga_get_time(struct svga_context *svga)
744b8e80941Smrg{
745b8e80941Smrg   return svga->hud.uses_time ? os_time_get() : 0;
746b8e80941Smrg}
747b8e80941Smrg
748848b8605Smrg
749848b8605Smrg#endif
750