r300g: separate num_cs_end_dwords out from prepare_for_rendering
[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 END_CB do { \
93 CB_DEBUG(if (cs_count != 0) \
94 debug_printf("r300: Warning: cs_count off by %d at (%s, %s:%i)\n", \
95 cs_count, __FUNCTION__, __FILE__, __LINE__);) \
96 } while (0)
97
98
99 /**
100 * Storing pure DWORDs.
101 */
102
103 #define OUT_CB(value) do { \
104 *cs_ptr = (value); \
105 cs_ptr++; \
106 CB_DEBUG(cs_count--;) \
107 } while (0)
108
109 #define OUT_CB_TABLE(values, count) do { \
110 memcpy(cs_ptr, values, count * sizeof(uint32_t)); \
111 cs_ptr += count; \
112 CB_DEBUG(cs_count -= count;) \
113 } while (0)
114
115 #define OUT_CB_32F(value) \
116 OUT_CB(fui(value));
117
118 #define OUT_CB_REG(register, value) do { \
119 assert(register); \
120 OUT_CB(CP_PACKET0(register, 0)); \
121 OUT_CB(value); \
122 } while (0)
123
124 /* Note: This expects count to be the number of registers,
125 * not the actual packet0 count! */
126 #define OUT_CB_REG_SEQ(register, count) do { \
127 assert(register); \
128 OUT_CB(CP_PACKET0(register, (count) - 1)); \
129 } while (0)
130
131 #define OUT_CB_ONE_REG(register, count) do { \
132 assert(register); \
133 OUT_CB(CP_PACKET0(register, (count) - 1) | RADEON_ONE_REG_WR); \
134 } while (0)
135
136 #define OUT_CB_PKT3(op, count) \
137 OUT_CB(CP_PACKET3(op, count))
138
139 #endif /* R300_CB_H */