svga_context.h revision 3464ebd5
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_double_list.h"
35
36#include "tgsi/tgsi_scan.h"
37
38#include "svga_state.h"
39
40
41#define SVGA_TEX_UNITS 8
42#define SVGA_MAX_POINTSIZE 80.0
43
44struct draw_vertex_shader;
45struct svga_shader_result;
46struct SVGACmdMemory;
47struct util_bitmask;
48struct u_upload_mgr;
49
50
51struct svga_shader
52{
53   const struct tgsi_token *tokens;
54
55   struct tgsi_shader_info info;
56
57   struct svga_shader_result *results;
58
59   unsigned id;
60
61   boolean use_sm30;
62};
63
64struct svga_fragment_shader
65{
66   struct svga_shader base;
67};
68
69struct svga_vertex_shader
70{
71   struct svga_shader base;
72
73   struct draw_vertex_shader *draw_shader;
74};
75
76
77struct svga_cache_context;
78struct svga_tracked_state;
79
80struct svga_blend_state {
81
82   boolean need_white_fragments;
83
84   /* Should be per-render-target:
85    */
86   struct {
87      uint8_t writemask;
88
89      boolean blend_enable;
90      uint8_t srcblend;
91      uint8_t dstblend;
92      uint8_t blendeq;
93
94      boolean separate_alpha_blend_enable;
95      uint8_t srcblend_alpha;
96      uint8_t dstblend_alpha;
97      uint8_t blendeq_alpha;
98
99   } rt[1];
100};
101
102struct svga_depth_stencil_state {
103   unsigned zfunc:8;
104   unsigned zenable:1;
105   unsigned zwriteenable:1;
106
107   unsigned alphatestenable:1;
108   unsigned alphafunc:8;
109
110   struct {
111      unsigned enabled:1;
112      unsigned func:8;
113      unsigned fail:8;
114      unsigned zfail:8;
115      unsigned pass:8;
116   } stencil[2];
117
118   /* SVGA3D has one ref/mask/writemask triple shared between front &
119    * back face stencil.  We really need two:
120    */
121   unsigned stencil_mask:8;
122   unsigned stencil_writemask:8;
123
124   float    alpharef;
125};
126
127#define SVGA_UNFILLED_DISABLE 0
128#define SVGA_UNFILLED_LINE    1
129#define SVGA_UNFILLED_POINT   2
130
131#define SVGA_PIPELINE_FLAG_POINTS   (1<<PIPE_PRIM_POINTS)
132#define SVGA_PIPELINE_FLAG_LINES    (1<<PIPE_PRIM_LINES)
133#define SVGA_PIPELINE_FLAG_TRIS     (1<<PIPE_PRIM_TRIANGLES)
134
135struct svga_rasterizer_state {
136   struct pipe_rasterizer_state templ; /* needed for draw module */
137
138   unsigned shademode:8;
139   unsigned cullmode:8;
140   unsigned scissortestenable:1;
141   unsigned multisampleantialias:1;
142   unsigned antialiasedlineenable:1;
143   unsigned lastpixel:1;
144
145   unsigned linepattern;
146
147   float slopescaledepthbias;
148   float depthbias;
149   float pointsize;
150
151   unsigned hw_unfilled:16;         /* 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   /** For debugging: */
157   const char* need_pipeline_tris_str;
158   const char* need_pipeline_lines_str;
159   const char* need_pipeline_points_str;
160};
161
162struct svga_sampler_state {
163   unsigned mipfilter;
164   unsigned magfilter;
165   unsigned minfilter;
166   unsigned aniso_level;
167   float lod_bias;
168   unsigned addressu;
169   unsigned addressv;
170   unsigned addressw;
171   unsigned bordercolor;
172   unsigned normalized_coords:1;
173   unsigned compare_mode:1;
174   unsigned compare_func:3;
175
176   unsigned min_lod;
177   unsigned view_min_lod;
178   unsigned view_max_lod;
179};
180
181struct svga_velems_state {
182   unsigned count;
183   struct pipe_vertex_element velem[PIPE_MAX_ATTRIBS];
184};
185
186/* Use to calculate differences between state emitted to hardware and
187 * current driver-calculated state.
188 */
189struct svga_state
190{
191   const struct svga_blend_state *blend;
192   const struct svga_depth_stencil_state *depth;
193   const struct svga_rasterizer_state *rast;
194   const struct svga_sampler_state *sampler[PIPE_MAX_SAMPLERS];
195   const struct svga_velems_state *velems;
196
197   struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS]; /* or texture ID's? */
198   struct svga_fragment_shader *fs;
199   struct svga_vertex_shader *vs;
200
201   struct pipe_vertex_buffer vb[PIPE_MAX_ATTRIBS];
202   struct pipe_index_buffer ib;
203   struct pipe_resource *cb[PIPE_SHADER_TYPES];
204
205   struct pipe_framebuffer_state framebuffer;
206   float depthscale;
207
208   /* Hack to limit the number of different render targets between
209    * flushes.  Helps avoid blowing out our surface cache in EXA.
210    */
211   int nr_fbs;
212
213   struct pipe_poly_stipple poly_stipple;
214   struct pipe_scissor_state scissor;
215   struct pipe_blend_color blend_color;
216   struct pipe_stencil_ref stencil_ref;
217   struct pipe_clip_state clip;
218   struct pipe_viewport_state viewport;
219
220   unsigned num_samplers;
221   unsigned num_sampler_views;
222   unsigned num_vertex_buffers;
223   unsigned reduced_prim;
224
225   struct {
226      unsigned flag_1d;
227      unsigned flag_srgb;
228   } tex_flags;
229
230   boolean any_user_vertex_buffers;
231
232   unsigned zero_stride_vertex_elements;
233   unsigned num_zero_stride_vertex_elements;
234   /* ### maybe dynamically allocate this */
235   float zero_stride_constants[PIPE_MAX_ATTRIBS*4];
236};
237
238#define RS_MAX 97
239#define TS_MAX 30
240#define CB_MAX 256
241
242struct svga_prescale {
243   float translate[4];
244   float scale[4];
245   boolean enabled;
246};
247
248
249/* Updated by calling svga_update_state( SVGA_STATE_HW_CLEAR )
250 */
251struct svga_hw_clear_state
252{
253   struct {
254      unsigned x,y,w,h;
255   } viewport;
256
257   struct {
258      float zmin, zmax;
259   } depthrange;
260
261   struct pipe_framebuffer_state framebuffer;
262   struct svga_prescale prescale;
263};
264
265struct svga_hw_view_state
266{
267   struct pipe_resource *texture;
268   struct svga_sampler_view *v;
269   unsigned min_lod;
270   unsigned max_lod;
271   int dirty;
272};
273
274/* Updated by calling svga_update_state( SVGA_STATE_HW_DRAW )
275 */
276struct svga_hw_draw_state
277{
278   unsigned rs[RS_MAX];
279   unsigned ts[16][TS_MAX];
280   float cb[PIPE_SHADER_TYPES][CB_MAX][4];
281
282   struct svga_shader_result *fs;
283   struct svga_shader_result *vs;
284   struct svga_hw_view_state views[PIPE_MAX_SAMPLERS];
285
286   unsigned num_views;
287};
288
289
290/* Updated by calling svga_update_state( SVGA_STATE_NEED_SWTNL )
291 */
292struct svga_sw_state
293{
294   unsigned ve_format[PIPE_MAX_ATTRIBS]; /* NEW_VELEMENT */
295
296   /* which parts we need */
297   boolean need_swvfetch;
298   boolean need_pipeline;
299   boolean need_swtnl;
300
301   /* Flag to make sure that need sw is on while
302    * updating state within a swtnl call.
303    */
304   boolean in_swtnl_draw;
305};
306
307
308/* Queue some state updates (like rss) and submit them to hardware in
309 * a single packet.
310 */
311struct svga_hw_queue;
312
313struct svga_query;
314
315struct svga_context
316{
317   struct pipe_context pipe;
318   struct svga_winsys_context *swc;
319
320   struct {
321      boolean no_swtnl;
322      boolean force_swtnl;
323      boolean use_min_mipmap;
324
325      /* incremented for each shader */
326      unsigned shader_id;
327
328      unsigned disable_shader;
329
330      boolean no_line_width;
331      boolean force_hw_line_stipple;
332   } debug;
333
334   struct {
335      struct draw_context *draw;
336      struct vbuf_render *backend;
337      unsigned hw_prim;
338      boolean new_vbuf;
339      boolean new_vdecl;
340   } swtnl;
341
342   /* Bitmask of used shader IDs */
343   struct util_bitmask *fs_bm;
344   struct util_bitmask *vs_bm;
345
346   struct {
347      unsigned dirty[SVGA_STATE_MAX];
348
349      unsigned texture_timestamp;
350
351      /*
352       */
353      struct svga_sw_state          sw;
354      struct svga_hw_draw_state     hw_draw;
355      struct svga_hw_clear_state    hw_clear;
356   } state;
357
358   struct svga_state curr;      /* state from the state tracker */
359   unsigned dirty;              /* statechanges since last update_state() */
360
361   struct {
362      unsigned rendertargets:1;
363      unsigned texture_samplers:1;
364   } rebind;
365
366   struct u_upload_mgr *upload_ib;
367   struct u_upload_mgr *upload_vb;
368   struct svga_hwtnl *hwtnl;
369
370   /** The occlusion query currently in progress */
371   struct svga_query *sq;
372
373   /** List of buffers with queued transfers */
374   struct list_head dirty_buffers;
375
376   /** Was the previous draw done with the SW path? */
377   boolean prev_draw_swtnl;
378};
379
380/* A flag for each state_tracker state object:
381 */
382#define SVGA_NEW_BLEND               0x1
383#define SVGA_NEW_DEPTH_STENCIL       0x2
384#define SVGA_NEW_RAST                0x4
385#define SVGA_NEW_SAMPLER             0x8
386#define SVGA_NEW_TEXTURE             0x10
387#define SVGA_NEW_VBUFFER             0x20
388#define SVGA_NEW_VELEMENT            0x40
389#define SVGA_NEW_FS                  0x80
390#define SVGA_NEW_VS                  0x100
391#define SVGA_NEW_FS_CONST_BUFFER     0x200
392#define SVGA_NEW_VS_CONST_BUFFER     0x400
393#define SVGA_NEW_FRAME_BUFFER        0x800
394#define SVGA_NEW_STIPPLE             0x1000
395#define SVGA_NEW_SCISSOR             0x2000
396#define SVGA_NEW_BLEND_COLOR         0x4000
397#define SVGA_NEW_CLIP                0x8000
398#define SVGA_NEW_VIEWPORT            0x10000
399#define SVGA_NEW_PRESCALE            0x20000
400#define SVGA_NEW_REDUCED_PRIMITIVE   0x40000
401#define SVGA_NEW_TEXTURE_BINDING     0x80000
402#define SVGA_NEW_NEED_PIPELINE       0x100000
403#define SVGA_NEW_NEED_SWVFETCH       0x200000
404#define SVGA_NEW_NEED_SWTNL          0x400000
405#define SVGA_NEW_FS_RESULT           0x800000
406#define SVGA_NEW_VS_RESULT           0x1000000
407#define SVGA_NEW_ZERO_STRIDE         0x2000000
408#define SVGA_NEW_TEXTURE_FLAGS       0x4000000
409#define SVGA_NEW_STENCIL_REF         0x8000000
410
411
412
413
414
415/***********************************************************************
416 * svga_clear.c:
417 */
418void svga_clear(struct pipe_context *pipe,
419                unsigned buffers,
420                const float *rgba,
421                double depth,
422                unsigned stencil);
423
424
425/***********************************************************************
426 * svga_screen_texture.c:
427 */
428void svga_mark_surfaces_dirty(struct svga_context *svga);
429
430
431
432
433void svga_init_state_functions( struct svga_context *svga );
434void svga_init_flush_functions( struct svga_context *svga );
435void svga_init_string_functions( struct svga_context *svga );
436void svga_init_blit_functions(struct svga_context *svga);
437
438void svga_init_blend_functions( struct svga_context *svga );
439void svga_init_depth_stencil_functions( struct svga_context *svga );
440void svga_init_misc_functions( struct svga_context *svga );
441void svga_init_rasterizer_functions( struct svga_context *svga );
442void svga_init_sampler_functions( struct svga_context *svga );
443void svga_init_fs_functions( struct svga_context *svga );
444void svga_init_vs_functions( struct svga_context *svga );
445void svga_init_vertex_functions( struct svga_context *svga );
446void svga_init_constbuffer_functions( struct svga_context *svga );
447void svga_init_draw_functions( struct svga_context *svga );
448void svga_init_query_functions( struct svga_context *svga );
449void svga_init_surface_functions(struct svga_context *svga);
450
451void svga_cleanup_vertex_state( struct svga_context *svga );
452void svga_cleanup_tss_binding( struct svga_context *svga );
453void svga_cleanup_framebuffer( struct svga_context *svga );
454
455void svga_context_flush( struct svga_context *svga,
456                         struct pipe_fence_handle **pfence );
457
458void svga_hwtnl_flush_retry( struct svga_context *svga );
459
460void svga_surfaces_flush(struct svga_context *svga);
461
462struct pipe_context *
463svga_context_create(struct pipe_screen *screen,
464		    void *priv);
465
466
467/***********************************************************************
468 * Inline conversion functions.  These are better-typed than the
469 * macros used previously:
470 */
471static INLINE struct svga_context *
472svga_context( struct pipe_context *pipe )
473{
474   return (struct svga_context *)pipe;
475}
476
477
478
479#endif
480