8e9742fe82f22524f0f9e65298d344a6b7ac853b
[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_blend_state {
65 uint32_t cb[8];
66 uint32_t cb_no_readwrite[8];
67 };
68
69 struct r300_blend_color_state {
70 uint32_t cb[3];
71 };
72
73 struct r300_clip_state {
74 struct pipe_clip_state clip;
75
76 uint32_t cb[29];
77 };
78
79 struct r300_dsa_state {
80 struct pipe_depth_stencil_alpha_state dsa;
81
82 /* This is actually a command buffer with named dwords. */
83 uint32_t cb_begin;
84 uint32_t alpha_function; /* R300_FG_ALPHA_FUNC: 0x4bd4 */
85 uint32_t cb_reg_seq;
86 uint32_t z_buffer_control; /* R300_ZB_CNTL: 0x4f00 */
87 uint32_t z_stencil_control; /* R300_ZB_ZSTENCILCNTL: 0x4f04 */
88 uint32_t stencil_ref_mask; /* R300_ZB_STENCILREFMASK: 0x4f08 */
89 uint32_t cb_reg;
90 uint32_t stencil_ref_bf; /* R500_ZB_STENCILREFMASK_BF: 0x4fd4 */
91
92 /* The second command buffer disables zbuffer reads and writes. */
93 uint32_t cb_no_readwrite[8];
94
95 /* Whether a two-sided stencil is enabled. */
96 boolean two_sided;
97 /* Whether a fallback should be used for a two-sided stencil ref value. */
98 boolean two_sided_stencil_ref;
99 };
100
101 struct r300_rs_state {
102 /* Original rasterizer state. */
103 struct pipe_rasterizer_state rs;
104 /* Draw-specific rasterizer state. */
105 struct pipe_rasterizer_state rs_draw;
106
107 uint32_t vap_control_status; /* R300_VAP_CNTL_STATUS: 0x2140 */
108 uint32_t multisample_position_0;/* R300_GB_MSPOS0: 0x4010 */
109 uint32_t multisample_position_1;/* R300_GB_MSPOS1: 0x4014 */
110 uint32_t antialiasing_config; /* R300_GB_AA_CONFIG: 0x4020 */
111 uint32_t point_size; /* R300_GA_POINT_SIZE: 0x421c */
112 uint32_t point_minmax; /* R300_GA_POINT_MINMAX: 0x4230 */
113 uint32_t line_control; /* R300_GA_LINE_CNTL: 0x4234 */
114 float depth_scale; /* R300_SU_POLY_OFFSET_FRONT_SCALE: 0x42a4 */
115 /* R300_SU_POLY_OFFSET_BACK_SCALE: 0x42ac */
116 float depth_offset; /* R300_SU_POLY_OFFSET_FRONT_OFFSET: 0x42a8 */
117 /* R300_SU_POLY_OFFSET_BACK_OFFSET: 0x42b0 */
118 uint32_t polygon_offset_enable; /* R300_SU_POLY_OFFSET_ENABLE: 0x42b4 */
119 uint32_t cull_mode; /* R300_SU_CULL_MODE: 0x42b8 */
120 uint32_t line_stipple_config; /* R300_GA_LINE_STIPPLE_CONFIG: 0x4328 */
121 uint32_t line_stipple_value; /* R300_GA_LINE_STIPPLE_VALUE: 0x4260 */
122 uint32_t color_control; /* R300_GA_COLOR_CONTROL: 0x4278 */
123 uint32_t polygon_mode; /* R300_GA_POLY_MODE: 0x4288 */
124 uint32_t clip_rule; /* R300_SC_CLIP_RULE: 0x43D0 */
125
126 /* Specifies top of Raster pipe specific enable controls,
127 * i.e. texture coordinates stuffing for points, lines, triangles */
128 uint32_t stuffing_enable; /* R300_GB_ENABLE: 0x4008 */
129
130 /* Point sprites texture coordinates, 0: lower left, 1: upper right */
131 float point_texcoord_left; /* R300_GA_POINT_S0: 0x4200 */
132 float point_texcoord_bottom; /* R300_GA_POINT_T0: 0x4204 */
133 float point_texcoord_right; /* R300_GA_POINT_S1: 0x4208 */
134 float point_texcoord_top; /* R300_GA_POINT_T1: 0x420c */
135 };
136
137 struct r300_rs_block {
138 uint32_t vap_vtx_state_cntl; /* R300_VAP_VTX_STATE_CNTL: 0x2180 */
139 uint32_t vap_vsm_vtx_assm; /* R300_VAP_VSM_VTX_ASSM: 0x2184 */
140 uint32_t vap_out_vtx_fmt[2]; /* R300_VAP_OUTPUT_VTX_FMT_[0-1]: 0x2090 */
141
142 uint32_t ip[8]; /* R300_RS_IP_[0-7], R500_RS_IP_[0-7] */
143 uint32_t count; /* R300_RS_COUNT */
144 uint32_t inst_count; /* R300_RS_INST_COUNT */
145 uint32_t inst[8]; /* R300_RS_INST_[0-7] */
146 };
147
148 struct r300_sampler_state {
149 struct pipe_sampler_state state;
150
151 uint32_t filter0; /* R300_TX_FILTER0: 0x4400 */
152 uint32_t filter1; /* R300_TX_FILTER1: 0x4440 */
153 uint32_t border_color; /* R300_TX_BORDER_COLOR: 0x45c0 */
154
155 /* Min/max LOD must be clamped to [0, last_level], thus
156 * it's dependent on a currently bound texture */
157 unsigned min_lod, max_lod;
158 };
159
160 struct r300_texture_format_state {
161 uint32_t format0; /* R300_TX_FORMAT0: 0x4480 */
162 uint32_t format1; /* R300_TX_FORMAT1: 0x44c0 */
163 uint32_t format2; /* R300_TX_FORMAT2: 0x4500 */
164 uint32_t tile_config; /* R300_TX_OFFSET (subset thereof) */
165 };
166
167 struct r300_sampler_view {
168 struct pipe_sampler_view base;
169
170 /* Swizzles in the UTIL_FORMAT_SWIZZLE_* representation,
171 * derived from base. */
172 unsigned char swizzle[4];
173
174 /* Copy of r300_texture::texture_format_state with format-specific bits
175 * added. */
176 struct r300_texture_format_state format;
177 };
178
179 struct r300_texture_fb_state {
180 uint32_t pitch[R300_MAX_TEXTURE_LEVELS]; /* COLORPITCH or DEPTHPITCH. */
181 uint32_t format; /* US_OUT_FMT or R300_ZB_FORMAT */
182 };
183
184 struct r300_texture_sampler_state {
185 struct r300_texture_format_state format;
186 uint32_t filter0; /* R300_TX_FILTER0: 0x4400 */
187 uint32_t filter1; /* R300_TX_FILTER1: 0x4440 */
188 uint32_t border_color; /* R300_TX_BORDER_COLOR: 0x45c0 */
189 };
190
191 struct r300_textures_state {
192 /* Textures. */
193 struct r300_sampler_view *sampler_views[16];
194 int sampler_view_count;
195 /* Sampler states. */
196 struct r300_sampler_state *sampler_states[16];
197 int sampler_state_count;
198
199 /* This is the merge of the texture and sampler states. */
200 unsigned count;
201 uint32_t tx_enable; /* R300_TX_ENABLE: 0x4101 */
202 struct r300_texture_sampler_state regs[16];
203 };
204
205 struct r300_vertex_stream_state {
206 /* R300_VAP_PROG_STREAK_CNTL_[0-7] */
207 uint32_t vap_prog_stream_cntl[8];
208 /* R300_VAP_PROG_STREAK_CNTL_EXT_[0-7] */
209 uint32_t vap_prog_stream_cntl_ext[8];
210
211 unsigned count;
212 };
213
214 struct r300_viewport_state {
215 float xscale; /* R300_VAP_VPORT_XSCALE: 0x2098 */
216 float xoffset; /* R300_VAP_VPORT_XOFFSET: 0x209c */
217 float yscale; /* R300_VAP_VPORT_YSCALE: 0x20a0 */
218 float yoffset; /* R300_VAP_VPORT_YOFFSET: 0x20a4 */
219 float zscale; /* R300_VAP_VPORT_ZSCALE: 0x20a8 */
220 float zoffset; /* R300_VAP_VPORT_ZOFFSET: 0x20ac */
221 uint32_t vte_control; /* R300_VAP_VTE_CNTL: 0x20b0 */
222 };
223
224 struct r300_ztop_state {
225 uint32_t z_buffer_top; /* R300_ZB_ZTOP: 0x4f14 */
226 };
227
228 /* The next several objects are not pure Radeon state; they inherit from
229 * various Gallium classes. */
230
231 struct r300_constant_buffer {
232 /* Buffer of constants */
233 uint32_t constants[256][4];
234 /* Total number of constants */
235 unsigned count;
236 };
237
238 /* Query object.
239 *
240 * This is not a subclass of pipe_query because pipe_query is never
241 * actually fully defined. So, rather than have it as a member, and do
242 * subclass-style casting, we treat pipe_query as an opaque, and just
243 * trust that our state tracker does not ever mess up query objects.
244 */
245 struct r300_query {
246 /* The kind of query. Currently only OQ is supported. */
247 unsigned type;
248 /* The number of pipes where query results are stored. */
249 unsigned num_pipes;
250 /* How many results have been written, in dwords. It's incremented
251 * after end_query and flush. */
252 unsigned num_results;
253 /* if we've flushed the query */
254 boolean flushed;
255 /* if begin has been emitted */
256 boolean begin_emitted;
257
258 /* The buffer where query results are stored. */
259 struct r300_winsys_buffer *buffer;
260 /* The size of the buffer. */
261 unsigned buffer_size;
262 /* The domain of the buffer. */
263 enum r300_buffer_domain domain;
264
265 /* Linked list members. */
266 struct r300_query* prev;
267 struct r300_query* next;
268 };
269
270 /* Fence object.
271 *
272 * This is a fake fence. Instead of syncing with the fence, we sync
273 * with the context, which is inefficient but compliant.
274 *
275 * This is not a subclass of pipe_fence_handle because pipe_fence_handle is
276 * never actually fully defined. So, rather than have it as a member, and do
277 * subclass-style casting, we treat pipe_fence_handle as an opaque, and just
278 * trust that our state tracker does not ever mess up fence objects.
279 */
280 struct r300_fence {
281 struct pipe_reference reference;
282 struct r300_context *ctx;
283 boolean signalled;
284 };
285
286 struct r300_surface {
287 struct pipe_surface base;
288
289 /* Winsys buffer backing the texture. */
290 struct r300_winsys_buffer *buffer;
291
292 enum r300_buffer_domain domain;
293
294 uint32_t offset;
295 uint32_t pitch; /* COLORPITCH or DEPTHPITCH. */
296 uint32_t format; /* US_OUT_FMT or R300_ZB_FORMAT. */
297 };
298
299 struct r300_texture {
300 /* Parent class */
301 struct u_resource b;
302
303 enum r300_buffer_domain domain;
304
305 /* Offsets into the buffer. */
306 unsigned offset[R300_MAX_TEXTURE_LEVELS];
307
308 /* A pitch for each mip-level */
309 unsigned pitch[R300_MAX_TEXTURE_LEVELS];
310
311 /* A pitch multiplied by blockwidth as hardware wants
312 * the number of pixels instead of the number of blocks. */
313 unsigned hwpitch[R300_MAX_TEXTURE_LEVELS];
314
315 /* Size of one zslice or face based on the texture target */
316 unsigned layer_size[R300_MAX_TEXTURE_LEVELS];
317
318 /* Whether the mipmap level is macrotiled. */
319 enum r300_buffer_tiling mip_macrotile[R300_MAX_TEXTURE_LEVELS];
320
321 /**
322 * If non-zero, override the natural texture layout with
323 * a custom stride (in bytes).
324 *
325 * \note Mipmapping fails for textures with a non-natural layout!
326 *
327 * \sa r300_texture_get_stride
328 */
329 unsigned stride_override;
330
331 /* Total size of this texture, in bytes. */
332 unsigned size;
333
334 /* Whether this texture has non-power-of-two dimensions
335 * or a user-specified pitch.
336 * It can be either a regular texture or a rectangle one.
337 */
338 boolean uses_pitch;
339
340 /* Pipe buffer backing this texture. */
341 struct r300_winsys_buffer *buffer;
342
343 /* Registers carrying texture format data. */
344 /* Only format-independent bits should be filled in. */
345 struct r300_texture_format_state tx_format;
346 /* All bits should be filled in. */
347 struct r300_texture_fb_state fb_state;
348
349 /* Buffer tiling */
350 enum r300_buffer_tiling microtile, macrotile;
351 };
352
353 struct r300_vertex_element_state {
354 unsigned count;
355 struct pipe_vertex_element velem[PIPE_MAX_ATTRIBS];
356
357 /* If (velem[i].src_format != hw_format[i]), the vertex buffer
358 * referenced by this vertex element cannot be used for rendering and
359 * its vertex data must be translated to hw_format[i]. */
360 enum pipe_format hw_format[PIPE_MAX_ATTRIBS];
361 unsigned hw_format_size[PIPE_MAX_ATTRIBS];
362
363 /* This might mean two things:
364 * - src_format != hw_format, as discussed above.
365 * - src_offset % 4 != 0. */
366 boolean incompatible_layout;
367
368 struct r300_vertex_stream_state vertex_stream;
369 };
370
371 struct r300_translate_context {
372 /* Translate cache for incompatible vertex offset/stride/format fallback. */
373 struct translate_cache *translate_cache;
374
375 /* The vertex buffer slot containing the translated buffer. */
376 unsigned vb_slot;
377
378 /* Saved and new vertex element state. */
379 void *saved_velems, *new_velems;
380 };
381
382 struct r300_context {
383 /* Parent class */
384 struct pipe_context context;
385
386 /* The interface to the windowing system, etc. */
387 struct r300_winsys_screen *rws;
388 /* Screen. */
389 struct r300_screen *screen;
390 /* Draw module. Used mostly for SW TCL. */
391 struct draw_context* draw;
392 /* Accelerated blit support. */
393 struct blitter_context* blitter;
394 /* Stencil two-sided reference value fallback. */
395 struct r300_stencilref_context *stencilref_fallback;
396 /* For translating vertex buffers having incompatible vertex layout. */
397 struct r300_translate_context tran;
398
399 /* Vertex buffer for rendering. */
400 struct pipe_resource* vbo;
401 /* Offset into the VBO. */
402 size_t vbo_offset;
403
404 /* The currently active query. */
405 struct r300_query *query_current;
406 /* The saved query for blitter operations. */
407 struct r300_query *blitter_saved_query;
408 /* Query list. */
409 struct r300_query query_list;
410
411 /* Various CSO state objects. */
412 /* Beginning of atom list. */
413 struct r300_atom atom_list;
414 /* Blend state. */
415 struct r300_atom blend_state;
416 /* Blend color state. */
417 struct r300_atom blend_color_state;
418 /* User clip planes. */
419 struct r300_atom clip_state;
420 /* Depth, stencil, and alpha state. */
421 struct r300_atom dsa_state;
422 /* Fragment shader. */
423 struct r300_atom fs;
424 /* Fragment shader RC_CONSTANT_STATE variables. */
425 struct r300_atom fs_rc_constant_state;
426 /* Fragment shader constant buffer. */
427 struct r300_atom fs_constants;
428 /* Framebuffer state. */
429 struct r300_atom fb_state;
430 /* Occlusion query. */
431 struct r300_atom query_start;
432 /* Rasterizer state. */
433 struct r300_atom rs_state;
434 /* RS block state + VAP (vertex shader) output mapping state. */
435 struct r300_atom rs_block_state;
436 /* Scissor state. */
437 struct r300_atom scissor_state;
438 /* Textures state. */
439 struct r300_atom textures_state;
440 /* Vertex stream formatting state. */
441 struct r300_atom vertex_stream_state;
442 /* Vertex shader. */
443 struct r300_atom vs_state;
444 /* Vertex shader constant buffer. */
445 struct r300_atom vs_constants;
446 /* Viewport state. */
447 struct r300_atom viewport_state;
448 /* ZTOP state. */
449 struct r300_atom ztop_state;
450 /* PVS flush. */
451 struct r300_atom pvs_flush;
452 /* Texture cache invalidate. */
453 struct r300_atom texture_cache_inval;
454
455 /* Invariant state. This must be emitted to get the engine started. */
456 struct r300_atom invariant_state;
457
458 /* Vertex buffers for Gallium. */
459 struct pipe_vertex_buffer vertex_buffer[PIPE_MAX_ATTRIBS];
460 int vertex_buffer_count;
461 int vertex_buffer_max_index;
462 /* Vertex elements for Gallium. */
463 struct r300_vertex_element_state *velems;
464 bool any_user_vbs;
465
466 /* Vertex info for Draw. */
467 struct vertex_info vertex_info;
468
469 struct pipe_stencil_ref stencil_ref;
470 struct pipe_viewport_state viewport;
471
472 /* Stream locations for SWTCL. */
473 int stream_loc_notcl[16];
474
475 /* Flag indicating whether or not the HW is dirty. */
476 uint32_t dirty_hw;
477 /* Whether polygon offset is enabled. */
478 boolean polygon_offset_enabled;
479 /* Z buffer bit depth. */
480 uint32_t zbuffer_bpp;
481 /* Whether rendering is conditional and should be skipped. */
482 boolean skip_rendering;
483 /* Point sprites texcoord index, 1 bit per texcoord */
484 int sprite_coord_enable;
485 /* Whether two-sided color selection is enabled (AKA light_twoside). */
486 boolean two_sided_color;
487 /* Incompatible vertex buffer layout? (misaligned stride or buffer_offset) */
488 boolean incompatible_vb_layout;
489
490 /* upload managers */
491 struct u_upload_mgr *upload_vb;
492 struct u_upload_mgr *upload_ib;
493
494 /* Stat counter. */
495 uint64_t flush_counter;
496 };
497
498 /* Convenience cast wrappers. */
499 static INLINE struct r300_query* r300_query(struct pipe_query* q)
500 {
501 return (struct r300_query*)q;
502 }
503
504 static INLINE struct r300_surface* r300_surface(struct pipe_surface* surf)
505 {
506 return (struct r300_surface*)surf;
507 }
508
509 static INLINE struct r300_texture* r300_texture(struct pipe_resource* tex)
510 {
511 return (struct r300_texture*)tex;
512 }
513
514 static INLINE struct r300_context* r300_context(struct pipe_context* context)
515 {
516 return (struct r300_context*)context;
517 }
518
519 static INLINE struct r300_fragment_shader *r300_fs(struct r300_context *r300)
520 {
521 return (struct r300_fragment_shader*)r300->fs.state;
522 }
523
524 struct pipe_context* r300_create_context(struct pipe_screen* screen,
525 void *priv);
526
527 boolean r300_check_cs(struct r300_context *r300, unsigned size);
528 void r300_finish(struct r300_context *r300);
529
530 /* Context initialization. */
531 struct draw_stage* r300_draw_stage(struct r300_context* r300);
532 void r300_init_blit_functions(struct r300_context *r300);
533 void r300_init_flush_functions(struct r300_context* r300);
534 void r300_init_query_functions(struct r300_context* r300);
535 void r300_init_render_functions(struct r300_context *r300);
536 void r300_init_state_functions(struct r300_context* r300);
537 void r300_init_resource_functions(struct r300_context* r300);
538
539 /* r300_query.c */
540 void r300_resume_query(struct r300_context *r300,
541 struct r300_query *query);
542 void r300_stop_query(struct r300_context *r300);
543
544 /* r300_render_translate.c */
545 void r300_begin_vertex_translate(struct r300_context *r300);
546 void r300_end_vertex_translate(struct r300_context *r300);
547 void r300_translate_index_buffer(struct r300_context *r300,
548 struct pipe_resource **index_buffer,
549 unsigned *index_size, unsigned index_offset,
550 unsigned *start, unsigned count);
551
552 /* r300_render_stencilref.c */
553 void r300_plug_in_stencil_ref_fallback(struct r300_context *r300);
554
555 /* r300_state.c */
556 void r300_mark_fs_code_dirty(struct r300_context *r300);
557
558 /* r300_debug.c */
559 void r500_dump_rs_block(struct r300_rs_block *rs);
560
561
562 static INLINE boolean CTX_DBG_ON(struct r300_context * ctx, unsigned flags)
563 {
564 return SCREEN_DBG_ON(ctx->screen, flags);
565 }
566
567 static INLINE void CTX_DBG(struct r300_context * ctx, unsigned flags,
568 const char * fmt, ...)
569 {
570 if (CTX_DBG_ON(ctx, flags)) {
571 va_list va;
572 va_start(va, fmt);
573 vfprintf(stderr, fmt, va);
574 va_end(va);
575 }
576 }
577
578 #define DBG_ON CTX_DBG_ON
579 #define DBG CTX_DBG
580
581 #endif /* R300_CONTEXT_H */