r300g: turn rasterizer state into a command buffer
[mesa.git] / src / gallium / drivers / r300 / r300_context.h
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #ifndef R300_CONTEXT_H
24 #define R300_CONTEXT_H
25
26 #include "draw/draw_vertex.h"
27
28 #include "util/u_blitter.h"
29
30 #include "pipe/p_context.h"
31 #include "util/u_inlines.h"
32 #include "util/u_transfer.h"
33
34 #include "translate/translate_cache.h"
35
36 #include "r300_defines.h"
37 #include "r300_screen.h"
38
39 struct u_upload_mgr;
40 struct r300_context;
41 struct r300_fragment_shader;
42 struct r300_vertex_shader;
43 struct r300_stencilref_context;
44
45 struct r300_atom {
46 /* List pointers. */
47 struct r300_atom *prev, *next;
48 /* Name, for debugging. */
49 const char* name;
50 /* Stat counter. */
51 uint64_t counter;
52 /* Opaque state. */
53 void* state;
54 /* Emit the state to the context. */
55 void (*emit)(struct r300_context*, unsigned, void*);
56 /* Upper bound on number of dwords to emit. */
57 unsigned size;
58 /* Whether this atom should be emitted. */
59 boolean dirty;
60 /* Whether this atom may be emitted with state == NULL. */
61 boolean allow_null_state;
62 };
63
64 struct r300_aa_state {
65 struct r300_surface *dest;
66
67 uint32_t aa_config;
68 uint32_t aaresolve_ctl;
69 };
70
71 struct r300_blend_state {
72 uint32_t cb[8];
73 uint32_t cb_no_readwrite[8];
74 };
75
76 struct r300_blend_color_state {
77 uint32_t cb[3];
78 };
79
80 struct r300_clip_state {
81 struct pipe_clip_state clip;
82
83 uint32_t cb[29];
84 };
85
86 struct r300_dsa_state {
87 struct pipe_depth_stencil_alpha_state dsa;
88
89 /* This is actually a command buffer with named dwords. */
90 uint32_t cb_begin;
91 uint32_t alpha_function; /* R300_FG_ALPHA_FUNC: 0x4bd4 */
92 uint32_t cb_reg_seq;
93 uint32_t z_buffer_control; /* R300_ZB_CNTL: 0x4f00 */
94 uint32_t z_stencil_control; /* R300_ZB_ZSTENCILCNTL: 0x4f04 */
95 uint32_t stencil_ref_mask; /* R300_ZB_STENCILREFMASK: 0x4f08 */
96 uint32_t cb_reg;
97 uint32_t stencil_ref_bf; /* R500_ZB_STENCILREFMASK_BF: 0x4fd4 */
98
99 /* The second command buffer disables zbuffer reads and writes. */
100 uint32_t cb_no_readwrite[8];
101
102 /* Whether a two-sided stencil is enabled. */
103 boolean two_sided;
104 /* Whether a fallback should be used for a two-sided stencil ref value. */
105 boolean two_sided_stencil_ref;
106 };
107
108 struct r300_gpu_flush {
109 uint32_t cb_flush_clean[6];
110 };
111
112 struct r300_rs_state {
113 /* Original rasterizer state. */
114 struct pipe_rasterizer_state rs;
115 /* Draw-specific rasterizer state. */
116 struct pipe_rasterizer_state rs_draw;
117
118 /* Command buffers. */
119 uint32_t cb_main[25];
120 uint32_t cb_poly_offset_zb16[5];
121 uint32_t cb_poly_offset_zb24[5];
122
123 /* The index to cb_main where the cull_mode register value resides. */
124 unsigned cull_mode_index;
125
126 /* Whether polygon offset is enabled. */
127 boolean polygon_offset_enable;
128
129 /* This is emitted in the draw function. */
130 uint32_t color_control; /* R300_GA_COLOR_CONTROL: 0x4278 */
131 };
132
133 struct r300_rs_block {
134 uint32_t vap_vtx_state_cntl; /* R300_VAP_VTX_STATE_CNTL: 0x2180 */
135 uint32_t vap_vsm_vtx_assm; /* R300_VAP_VSM_VTX_ASSM: 0x2184 */
136 uint32_t vap_out_vtx_fmt[2]; /* R300_VAP_OUTPUT_VTX_FMT_[0-1]: 0x2090 */
137
138 uint32_t ip[8]; /* R300_RS_IP_[0-7], R500_RS_IP_[0-7] */
139 uint32_t count; /* R300_RS_COUNT */
140 uint32_t inst_count; /* R300_RS_INST_COUNT */
141 uint32_t inst[8]; /* R300_RS_INST_[0-7] */
142 };
143
144 struct r300_sampler_state {
145 struct pipe_sampler_state state;
146
147 uint32_t filter0; /* R300_TX_FILTER0: 0x4400 */
148 uint32_t filter1; /* R300_TX_FILTER1: 0x4440 */
149 uint32_t border_color; /* R300_TX_BORDER_COLOR: 0x45c0 */
150
151 /* Min/max LOD must be clamped to [0, last_level], thus
152 * it's dependent on a currently bound texture */
153 unsigned min_lod, max_lod;
154 };
155
156 struct r300_texture_format_state {
157 uint32_t format0; /* R300_TX_FORMAT0: 0x4480 */
158 uint32_t format1; /* R300_TX_FORMAT1: 0x44c0 */
159 uint32_t format2; /* R300_TX_FORMAT2: 0x4500 */
160 uint32_t tile_config; /* R300_TX_OFFSET (subset thereof) */
161 };
162
163 struct r300_sampler_view {
164 struct pipe_sampler_view base;
165
166 /* Swizzles in the UTIL_FORMAT_SWIZZLE_* representation,
167 * derived from base. */
168 unsigned char swizzle[4];
169
170 /* Copy of r300_texture::texture_format_state with format-specific bits
171 * added. */
172 struct r300_texture_format_state format;
173
174 /* The texture cache region for this texture. */
175 uint32_t texcache_region;
176 };
177
178 struct r300_texture_fb_state {
179 uint32_t pitch[R300_MAX_TEXTURE_LEVELS]; /* COLORPITCH or DEPTHPITCH. */
180 uint32_t format; /* US_OUT_FMT or R300_ZB_FORMAT */
181 };
182
183 struct r300_texture_sampler_state {
184 struct r300_texture_format_state format;
185 uint32_t filter0; /* R300_TX_FILTER0: 0x4400 */
186 uint32_t filter1; /* R300_TX_FILTER1: 0x4440 */
187 uint32_t border_color; /* R300_TX_BORDER_COLOR: 0x45c0 */
188 };
189
190 struct r300_textures_state {
191 /* Textures. */
192 struct r300_sampler_view *sampler_views[16];
193 int sampler_view_count;
194 /* Sampler states. */
195 struct r300_sampler_state *sampler_states[16];
196 int sampler_state_count;
197
198 /* This is the merge of the texture and sampler states. */
199 unsigned count;
200 uint32_t tx_enable; /* R300_TX_ENABLE: 0x4101 */
201 struct r300_texture_sampler_state regs[16];
202 };
203
204 struct r300_vertex_stream_state {
205 /* R300_VAP_PROG_STREAK_CNTL_[0-7] */
206 uint32_t vap_prog_stream_cntl[8];
207 /* R300_VAP_PROG_STREAK_CNTL_EXT_[0-7] */
208 uint32_t vap_prog_stream_cntl_ext[8];
209
210 unsigned count;
211 };
212
213 struct r300_invariant_state {
214 uint32_t cb[22];
215 };
216
217 struct r300_vap_invariant_state {
218 uint32_t cb[9];
219 };
220
221 struct r300_viewport_state {
222 float xscale; /* R300_VAP_VPORT_XSCALE: 0x2098 */
223 float xoffset; /* R300_VAP_VPORT_XOFFSET: 0x209c */
224 float yscale; /* R300_VAP_VPORT_YSCALE: 0x20a0 */
225 float yoffset; /* R300_VAP_VPORT_YOFFSET: 0x20a4 */
226 float zscale; /* R300_VAP_VPORT_ZSCALE: 0x20a8 */
227 float zoffset; /* R300_VAP_VPORT_ZOFFSET: 0x20ac */
228 uint32_t vte_control; /* R300_VAP_VTE_CNTL: 0x20b0 */
229 };
230
231 struct r300_ztop_state {
232 uint32_t z_buffer_top; /* R300_ZB_ZTOP: 0x4f14 */
233 };
234
235 /* The next several objects are not pure Radeon state; they inherit from
236 * various Gallium classes. */
237
238 struct r300_constant_buffer {
239 /* Buffer of constants */
240 uint32_t constants[256][4];
241 /* Total number of constants */
242 unsigned count;
243 };
244
245 /* Query object.
246 *
247 * This is not a subclass of pipe_query because pipe_query is never
248 * actually fully defined. So, rather than have it as a member, and do
249 * subclass-style casting, we treat pipe_query as an opaque, and just
250 * trust that our state tracker does not ever mess up query objects.
251 */
252 struct r300_query {
253 /* The kind of query. Currently only OQ is supported. */
254 unsigned type;
255 /* The number of pipes where query results are stored. */
256 unsigned num_pipes;
257 /* How many results have been written, in dwords. It's incremented
258 * after end_query and flush. */
259 unsigned num_results;
260 /* if we've flushed the query */
261 boolean flushed;
262 /* if begin has been emitted */
263 boolean begin_emitted;
264
265 /* The buffer where query results are stored. */
266 struct r300_winsys_buffer *buffer;
267 /* The size of the buffer. */
268 unsigned buffer_size;
269 /* The domain of the buffer. */
270 enum r300_buffer_domain domain;
271
272 /* Linked list members. */
273 struct r300_query* prev;
274 struct r300_query* next;
275 };
276
277 /* Fence object.
278 *
279 * This is a fake fence. Instead of syncing with the fence, we sync
280 * with the context, which is inefficient but compliant.
281 *
282 * This is not a subclass of pipe_fence_handle because pipe_fence_handle is
283 * never actually fully defined. So, rather than have it as a member, and do
284 * subclass-style casting, we treat pipe_fence_handle as an opaque, and just
285 * trust that our state tracker does not ever mess up fence objects.
286 */
287 struct r300_fence {
288 struct pipe_reference reference;
289 struct r300_context *ctx;
290 boolean signalled;
291 };
292
293 struct r300_surface {
294 struct pipe_surface base;
295
296 /* Winsys buffer backing the texture. */
297 struct r300_winsys_buffer *buffer;
298
299 enum r300_buffer_domain domain;
300
301 uint32_t offset;
302 uint32_t pitch; /* COLORPITCH or DEPTHPITCH. */
303 uint32_t format; /* US_OUT_FMT or R300_ZB_FORMAT. */
304 };
305
306 struct r300_texture {
307 /* Parent class */
308 struct u_resource b;
309
310 enum r300_buffer_domain domain;
311
312 /* Offsets into the buffer. */
313 unsigned offset[R300_MAX_TEXTURE_LEVELS];
314
315 /* A pitch for each mip-level */
316 unsigned pitch[R300_MAX_TEXTURE_LEVELS];
317
318 /* A pitch multiplied by blockwidth as hardware wants
319 * the number of pixels instead of the number of blocks. */
320 unsigned hwpitch[R300_MAX_TEXTURE_LEVELS];
321
322 /* Size of one zslice or face based on the texture target */
323 unsigned layer_size[R300_MAX_TEXTURE_LEVELS];
324
325 /* Whether the mipmap level is macrotiled. */
326 enum r300_buffer_tiling mip_macrotile[R300_MAX_TEXTURE_LEVELS];
327
328 /**
329 * If non-zero, override the natural texture layout with
330 * a custom stride (in bytes).
331 *
332 * \note Mipmapping fails for textures with a non-natural layout!
333 *
334 * \sa r300_texture_get_stride
335 */
336 unsigned stride_override;
337
338 /* Total size of this texture, in bytes. */
339 unsigned size;
340
341 /* Whether this texture has non-power-of-two dimensions
342 * or a user-specified pitch.
343 * It can be either a regular texture or a rectangle one.
344 */
345 boolean uses_pitch;
346
347 /* Pipe buffer backing this texture. */
348 struct r300_winsys_buffer *buffer;
349
350 /* Registers carrying texture format data. */
351 /* Only format-independent bits should be filled in. */
352 struct r300_texture_format_state tx_format;
353 /* All bits should be filled in. */
354 struct r300_texture_fb_state fb_state;
355
356 /* Buffer tiling */
357 enum r300_buffer_tiling microtile, macrotile;
358 };
359
360 struct r300_vertex_element_state {
361 unsigned count;
362 struct pipe_vertex_element velem[PIPE_MAX_ATTRIBS];
363
364 /* If (velem[i].src_format != hw_format[i]), the vertex buffer
365 * referenced by this vertex element cannot be used for rendering and
366 * its vertex data must be translated to hw_format[i]. */
367 enum pipe_format hw_format[PIPE_MAX_ATTRIBS];
368 unsigned hw_format_size[PIPE_MAX_ATTRIBS];
369
370 /* The size of the vertex, in dwords. */
371 unsigned vertex_size_dwords;
372
373 /* This might mean two things:
374 * - src_format != hw_format, as discussed above.
375 * - src_offset % 4 != 0. */
376 boolean incompatible_layout;
377
378 struct r300_vertex_stream_state vertex_stream;
379 };
380
381 struct r300_translate_context {
382 /* Translate cache for incompatible vertex offset/stride/format fallback. */
383 struct translate_cache *translate_cache;
384
385 /* The vertex buffer slot containing the translated buffer. */
386 unsigned vb_slot;
387
388 /* Saved and new vertex element state. */
389 void *saved_velems, *new_velems;
390 };
391
392 struct r300_context {
393 /* Parent class */
394 struct pipe_context context;
395
396 /* The interface to the windowing system, etc. */
397 struct r300_winsys_screen *rws;
398 /* Screen. */
399 struct r300_screen *screen;
400 /* Draw module. Used mostly for SW TCL. */
401 struct draw_context* draw;
402 /* Accelerated blit support. */
403 struct blitter_context* blitter;
404 /* Stencil two-sided reference value fallback. */
405 struct r300_stencilref_context *stencilref_fallback;
406 /* For translating vertex buffers having incompatible vertex layout. */
407 struct r300_translate_context tran;
408
409 /* Vertex buffer for rendering. */
410 struct pipe_resource* vbo;
411 /* The KIL opcode needs the first texture unit to be enabled
412 * on r3xx-r4xx. In order to calm down the CS checker, we bind this
413 * dummy texture there. */
414 struct r300_sampler_view *texkill_sampler;
415 /* Offset into the VBO. */
416 size_t vbo_offset;
417
418 /* The currently active query. */
419 struct r300_query *query_current;
420 /* The saved query for blitter operations. */
421 struct r300_query *blitter_saved_query;
422 /* Query list. */
423 struct r300_query query_list;
424
425 /* Various CSO state objects. */
426 /* Beginning of atom list. */
427 struct r300_atom atom_list;
428 /* Anti-aliasing (MSAA) state. */
429 struct r300_atom aa_state;
430 /* Blend state. */
431 struct r300_atom blend_state;
432 /* Blend color state. */
433 struct r300_atom blend_color_state;
434 /* User clip planes. */
435 struct r300_atom clip_state;
436 /* Depth, stencil, and alpha state. */
437 struct r300_atom dsa_state;
438 /* Fragment shader. */
439 struct r300_atom fs;
440 /* Fragment shader RC_CONSTANT_STATE variables. */
441 struct r300_atom fs_rc_constant_state;
442 /* Fragment shader constant buffer. */
443 struct r300_atom fs_constants;
444 /* Framebuffer state. */
445 struct r300_atom fb_state;
446 /* Occlusion query. */
447 struct r300_atom query_start;
448 /* Rasterizer state. */
449 struct r300_atom rs_state;
450 /* RS block state + VAP (vertex shader) output mapping state. */
451 struct r300_atom rs_block_state;
452 /* Scissor state. */
453 struct r300_atom scissor_state;
454 /* Textures state. */
455 struct r300_atom textures_state;
456 /* Vertex stream formatting state. */
457 struct r300_atom vertex_stream_state;
458 /* Vertex shader. */
459 struct r300_atom vs_state;
460 /* Vertex shader constant buffer. */
461 struct r300_atom vs_constants;
462 /* Viewport state. */
463 struct r300_atom viewport_state;
464 /* ZTOP state. */
465 struct r300_atom ztop_state;
466 /* PVS flush. */
467 struct r300_atom pvs_flush;
468 /* VAP invariant state. */
469 struct r300_atom vap_invariant_state;
470 /* Texture cache invalidate. */
471 struct r300_atom texture_cache_inval;
472 /* GPU flush. */
473 struct r300_atom gpu_flush;
474
475 /* Invariant state. This must be emitted to get the engine started. */
476 struct r300_atom invariant_state;
477
478 /* Vertex buffers for Gallium. */
479 struct pipe_vertex_buffer vertex_buffer[PIPE_MAX_ATTRIBS];
480 int vertex_buffer_count;
481 int vertex_buffer_max_index;
482 /* Vertex elements for Gallium. */
483 struct r300_vertex_element_state *velems;
484 bool any_user_vbs;
485
486 /* Vertex info for Draw. */
487 struct vertex_info vertex_info;
488
489 struct pipe_stencil_ref stencil_ref;
490 struct pipe_viewport_state viewport;
491
492 /* Stream locations for SWTCL. */
493 int stream_loc_notcl[16];
494
495 /* Flag indicating whether or not the HW is dirty. */
496 uint32_t dirty_hw;
497 /* Whether polygon offset is enabled. */
498 boolean polygon_offset_enabled;
499 /* Z buffer bit depth. */
500 uint32_t zbuffer_bpp;
501 /* Whether rendering is conditional and should be skipped. */
502 boolean skip_rendering;
503 /* Point sprites texcoord index, 1 bit per texcoord */
504 int sprite_coord_enable;
505 /* Whether two-sided color selection is enabled (AKA light_twoside). */
506 boolean two_sided_color;
507 /* Incompatible vertex buffer layout? (misaligned stride or buffer_offset) */
508 boolean incompatible_vb_layout;
509
510 /* upload managers */
511 struct u_upload_mgr *upload_vb;
512 struct u_upload_mgr *upload_ib;
513
514 /* Stat counter. */
515 uint64_t flush_counter;
516 };
517
518 /* Convenience cast wrappers. */
519 static INLINE struct r300_query* r300_query(struct pipe_query* q)
520 {
521 return (struct r300_query*)q;
522 }
523
524 static INLINE struct r300_surface* r300_surface(struct pipe_surface* surf)
525 {
526 return (struct r300_surface*)surf;
527 }
528
529 static INLINE struct r300_texture* r300_texture(struct pipe_resource* tex)
530 {
531 return (struct r300_texture*)tex;
532 }
533
534 static INLINE struct r300_context* r300_context(struct pipe_context* context)
535 {
536 return (struct r300_context*)context;
537 }
538
539 static INLINE struct r300_fragment_shader *r300_fs(struct r300_context *r300)
540 {
541 return (struct r300_fragment_shader*)r300->fs.state;
542 }
543
544 struct pipe_context* r300_create_context(struct pipe_screen* screen,
545 void *priv);
546
547 boolean r300_check_cs(struct r300_context *r300, unsigned size);
548 void r300_finish(struct r300_context *r300);
549
550 /* Context initialization. */
551 struct draw_stage* r300_draw_stage(struct r300_context* r300);
552 void r300_init_blit_functions(struct r300_context *r300);
553 void r300_init_flush_functions(struct r300_context* r300);
554 void r300_init_query_functions(struct r300_context* r300);
555 void r300_init_render_functions(struct r300_context *r300);
556 void r300_init_state_functions(struct r300_context* r300);
557 void r300_init_resource_functions(struct r300_context* r300);
558
559 /* r300_query.c */
560 void r300_resume_query(struct r300_context *r300,
561 struct r300_query *query);
562 void r300_stop_query(struct r300_context *r300);
563
564 /* r300_render_translate.c */
565 void r300_begin_vertex_translate(struct r300_context *r300);
566 void r300_end_vertex_translate(struct r300_context *r300);
567 void r300_translate_index_buffer(struct r300_context *r300,
568 struct pipe_resource **index_buffer,
569 unsigned *index_size, unsigned index_offset,
570 unsigned *start, unsigned count);
571
572 /* r300_render_stencilref.c */
573 void r300_plug_in_stencil_ref_fallback(struct r300_context *r300);
574
575 /* r300_state.c */
576 void r300_mark_fs_code_dirty(struct r300_context *r300);
577
578 /* r300_debug.c */
579 void r500_dump_rs_block(struct r300_rs_block *rs);
580
581
582 static INLINE boolean CTX_DBG_ON(struct r300_context * ctx, unsigned flags)
583 {
584 return SCREEN_DBG_ON(ctx->screen, flags);
585 }
586
587 static INLINE void CTX_DBG(struct r300_context * ctx, unsigned flags,
588 const char * fmt, ...)
589 {
590 if (CTX_DBG_ON(ctx, flags)) {
591 va_list va;
592 va_start(va, fmt);
593 vfprintf(stderr, fmt, va);
594 va_end(va);
595 }
596 }
597
598 #define DBG_ON CTX_DBG_ON
599 #define DBG CTX_DBG
600
601 #endif /* R300_CONTEXT_H */