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