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