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