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