Merge commit 'origin/gallium-master-merge'
[platform/upstream/mesa.git] / src / gallium / drivers / r300 / r300_cs.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_CS_H
24 #define R300_CS_H
25
26 #include "r300_reg.h"
27 #include "r300_winsys.h"
28
29 /* Pack a 32-bit float into a dword. */
30 static uint32_t pack_float_32(float f)
31 {
32     union {
33         float f;
34         uint32_t u;
35     } u;
36
37     u.f = f;
38     return u.u;
39 }
40
41 /* Yes, I know macros are ugly. However, they are much prettier than the code
42  * that they neatly hide away, and don't have the cost of function setup,so
43  * we're going to use them. */
44
45 #define MAX_CS_SIZE 64 * 1024 / 4
46
47 /* XXX stolen from radeon_drm.h */
48 #define RADEON_GEM_DOMAIN_CPU  0x1
49 #define RADEON_GEM_DOMAIN_GTT  0x2
50 #define RADEON_GEM_DOMAIN_VRAM 0x4
51
52 /* XXX stolen from radeon_reg.h */
53 #define RADEON_CP_PACKET0 0x0
54
55 #define CP_PACKET0(register, count) \
56     (RADEON_CP_PACKET0 | ((count) << 16) | ((register) >> 2))
57
58 #define CP_PACKET3(op, count) \
59     (RADEON_CP_PACKET3 | (op) | ((count) << 16))
60
61 #define CS_LOCALS(context) \
62     struct r300_winsys* cs_winsys = context->winsys; \
63     struct radeon_cs* cs = cs_winsys->cs; \
64     int cs_count = 0;
65
66 #define CHECK_CS(size) \
67     cs_winsys->check_cs(cs, (size))
68
69 #define BEGIN_CS(size) do { \
70     CHECK_CS(size); \
71     debug_printf("r300: BEGIN_CS, count %d, in %s (%s:%d)\n", \
72         size, __FUNCTION__, __FILE__, __LINE__); \
73     cs_winsys->begin_cs(cs, (size), __FILE__, __FUNCTION__, __LINE__); \
74     cs_count = size; \
75 } while (0)
76
77 #define OUT_CS(value) do { \
78     cs_winsys->write_cs_dword(cs, (value)); \
79     cs_count--; \
80 } while (0)
81
82 #define OUT_CS_32F(value) do { \
83     cs_winsys->write_cs_dword(cs, pack_float_32(value)); \
84     cs_count--; \
85 } while (0)
86
87 #define OUT_CS_REG(register, value) do { \
88     debug_printf("r300: writing 0x%08X to register 0x%04X\n", \
89         value, register); \
90     assert(register); \
91     OUT_CS(CP_PACKET0(register, 0)); \
92     OUT_CS(value); \
93 } while (0)
94
95 /* Note: This expects count to be the number of registers,
96  * not the actual packet0 count! */
97 #define OUT_CS_REG_SEQ(register, count) do { \
98     debug_printf("r300: writing register sequence of %d to 0x%04X\n", \
99         count, register); \
100     assert(register); \
101     OUT_CS(CP_PACKET0(register, ((count) - 1))); \
102 } while (0)
103
104 #define OUT_CS_RELOC(bo, offset, rd, wd, flags) do { \
105     debug_printf("r300: writing relocation for buffer %p, offset %d\n", \
106         bo, offset); \
107     assert(bo); \
108     OUT_CS(offset); \
109     cs_winsys->write_cs_reloc(cs, bo, rd, wd, flags); \
110     cs_count -= 2; \
111 } while (0)
112
113 #define END_CS do { \
114     debug_printf("r300: END_CS in %s (%s:%d)\n", __FUNCTION__, __FILE__, \
115         __LINE__); \
116     if (cs_count != 0) \
117         debug_printf("r300: Warning: cs_count off by %d\n", cs_count); \
118     cs_winsys->end_cs(cs, __FILE__, __FUNCTION__, __LINE__); \
119 } while (0)
120
121 #define FLUSH_CS do { \
122     debug_printf("r300: FLUSH_CS in %s (%s:%d)\n", __FUNCTION__, __FILE__, \
123         __LINE__); \
124     cs_winsys->flush_cs(cs); \
125 } while (0)
126
127 #include "r300_cs_inlines.h"
128
129 #endif /* R300_CS_H */