i915g: Change state code in vbuf code
[mesa.git] / src / gallium / drivers / r300 / r300_cb.h
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2010 Marek Olšák <maraeo@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
24 /**
25 * This file contains macros for building command buffers in memory.
26 *
27 * Use NEW_CB for buffers with a varying size and it will also allocate
28 * the buffer.
29 * Use BEGIN_CB for arrays with a static size.
30 *
31 * Example:
32 *
33 * uint32_t cb[3];
34 * CB_LOCALS;
35 *
36 * BEGIN_CB(cb, 3);
37 * OUT_CB_REG_SEQ(R500_RB3D_CONSTANT_COLOR_AR, 2);
38 * OUT_CB(blend_color_red_alpha);
39 * OUT_CB(blend_color_green_blue);
40 * END_CB;
41 *
42 * And later:
43 *
44 * CS_LOCALS;
45 * WRITE_CS_TABLE(cb, 3);
46 *
47 * Or using a little slower variant:
48 *
49 * CS_LOCALS;
50 * BEGIN_CS(cb, 3);
51 * OUT_CS_TABLE(cb, 3);
52 * END_CS;
53 */
54
55 #ifndef R300_CB_H
56 #define R300_CB_H
57
58 #include "r300_reg.h"
59
60 /* Yes, I know macros are ugly. However, they are much prettier than the code
61 * that they neatly hide away, and don't have the cost of function setup, so
62 * we're going to use them. */
63
64 #ifdef DEBUG
65 #define CB_DEBUG(x) x
66 #else
67 #define CB_DEBUG(x)
68 #endif
69
70
71 /**
72 * Command buffer setup.
73 */
74
75 #define CB_LOCALS \
76 CB_DEBUG(int cs_count = 0;) \
77 uint32_t *cs_ptr = NULL; \
78 CB_DEBUG((void) cs_count;) (void) cs_ptr;
79
80 #define NEW_CB(ptr, size) do { \
81 assert(sizeof(*ptr) == sizeof(uint32_t)); \
82 cs_ptr = (ptr) = (uint32_t*)malloc((size) * sizeof(uint32_t)); \
83 CB_DEBUG(cs_count = size;) \
84 } while (0)
85
86 #define BEGIN_CB(ptr, size) do { \
87 assert(sizeof(*ptr) == sizeof(uint32_t)); \
88 cs_ptr = ptr; \
89 CB_DEBUG(cs_count = size;) \
90 } while (0)
91
92 #define BEGIN_CS_AS_CB(r300, size) \
93 BEGIN_CB(r300->rws->get_cs_pointer(r300->rws, dwords), dwords)
94
95 #define END_CB do { \
96 CB_DEBUG(if (cs_count != 0) \
97 debug_printf("r300: Warning: cs_count off by %d at (%s, %s:%i)\n", \
98 cs_count, __FUNCTION__, __FILE__, __LINE__);) \
99 } while (0)
100
101
102 /**
103 * Storing pure DWORDs.
104 */
105
106 #define OUT_CB(value) do { \
107 *cs_ptr = (value); \
108 cs_ptr++; \
109 CB_DEBUG(cs_count--;) \
110 } while (0)
111
112 #define OUT_CB_TABLE(values, count) do { \
113 memcpy(cs_ptr, values, count * sizeof(uint32_t)); \
114 cs_ptr += count; \
115 CB_DEBUG(cs_count -= count;) \
116 } while (0)
117
118 #define OUT_CB_32F(value) \
119 OUT_CB(fui(value));
120
121 #define OUT_CB_REG(register, value) do { \
122 assert(register); \
123 OUT_CB(CP_PACKET0(register, 0)); \
124 OUT_CB(value); \
125 } while (0)
126
127 /* Note: This expects count to be the number of registers,
128 * not the actual packet0 count! */
129 #define OUT_CB_REG_SEQ(register, count) do { \
130 assert(register); \
131 OUT_CB(CP_PACKET0(register, (count) - 1)); \
132 } while (0)
133
134 #define OUT_CB_ONE_REG(register, count) do { \
135 assert(register); \
136 OUT_CB(CP_PACKET0(register, (count) - 1) | RADEON_ONE_REG_WR); \
137 } while (0)
138
139 #define OUT_CB_PKT3(op, count) \
140 OUT_CB(CP_PACKET3(op, count))
141
142 #endif /* R300_CB_H */