Introduce .editorconfig
[mesa.git] / src / gallium / drivers / vc4 / vc4_cl.h
1 /*
2 * Copyright © 2014 Broadcom
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * 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 NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef VC4_CL_H
25 #define VC4_CL_H
26
27 #include <stdint.h>
28
29 #include "util/u_math.h"
30 #include "util/macros.h"
31
32 #include "kernel/vc4_packet.h"
33
34 struct vc4_bo;
35
36 /**
37 * Undefined structure, used for typechecking that you're passing the pointers
38 * to these functions correctly.
39 */
40 struct vc4_cl_out;
41
42 struct vc4_cl {
43 void *base;
44 struct vc4_cl_out *next;
45 struct vc4_cl_out *reloc_next;
46 uint32_t size;
47 #ifdef DEBUG
48 uint32_t reloc_count;
49 #endif
50 };
51
52 void vc4_init_cl(struct vc4_context *vc4, struct vc4_cl *cl);
53 void vc4_reset_cl(struct vc4_cl *cl);
54 void vc4_dump_cl(void *cl, uint32_t size, bool is_render);
55 uint32_t vc4_gem_hindex(struct vc4_context *vc4, struct vc4_bo *bo);
56
57 struct PACKED unaligned_16 { uint16_t x; };
58 struct PACKED unaligned_32 { uint32_t x; };
59
60 static inline uint32_t cl_offset(struct vc4_cl *cl)
61 {
62 return (char *)cl->next - (char *)cl->base;
63 }
64
65 static inline void
66 cl_advance(struct vc4_cl_out **cl, uint32_t n)
67 {
68 (*cl) = (struct vc4_cl_out *)((char *)(*cl) + n);
69 }
70
71 static inline struct vc4_cl_out *
72 cl_start(struct vc4_cl *cl)
73 {
74 return cl->next;
75 }
76
77 static inline void
78 cl_end(struct vc4_cl *cl, struct vc4_cl_out *next)
79 {
80 cl->next = next;
81 assert(cl_offset(cl) <= cl->size);
82 }
83
84
85 static inline void
86 put_unaligned_32(struct vc4_cl_out *ptr, uint32_t val)
87 {
88 struct unaligned_32 *p = (void *)ptr;
89 p->x = val;
90 }
91
92 static inline void
93 put_unaligned_16(struct vc4_cl_out *ptr, uint16_t val)
94 {
95 struct unaligned_16 *p = (void *)ptr;
96 p->x = val;
97 }
98
99 static inline void
100 cl_u8(struct vc4_cl_out **cl, uint8_t n)
101 {
102 *(uint8_t *)(*cl) = n;
103 cl_advance(cl, 1);
104 }
105
106 static inline void
107 cl_u16(struct vc4_cl_out **cl, uint16_t n)
108 {
109 put_unaligned_16(*cl, n);
110 cl_advance(cl, 2);
111 }
112
113 static inline void
114 cl_u32(struct vc4_cl_out **cl, uint32_t n)
115 {
116 put_unaligned_32(*cl, n);
117 cl_advance(cl, 4);
118 }
119
120 static inline void
121 cl_aligned_u32(struct vc4_cl_out **cl, uint32_t n)
122 {
123 *(uint32_t *)(*cl) = n;
124 cl_advance(cl, 4);
125 }
126
127 static inline void
128 cl_ptr(struct vc4_cl_out **cl, void *ptr)
129 {
130 *(struct vc4_cl_out **)(*cl) = ptr;
131 cl_advance(cl, sizeof(void *));
132 }
133
134 static inline void
135 cl_f(struct vc4_cl_out **cl, float f)
136 {
137 cl_u32(cl, fui(f));
138 }
139
140 static inline void
141 cl_aligned_f(struct vc4_cl_out **cl, float f)
142 {
143 cl_aligned_u32(cl, fui(f));
144 }
145
146 static inline void
147 cl_start_reloc(struct vc4_cl *cl, struct vc4_cl_out **out, uint32_t n)
148 {
149 assert(n == 1 || n == 2);
150 #ifdef DEBUG
151 assert(cl->reloc_count == 0);
152 cl->reloc_count = n;
153 #endif
154
155 cl_u8(out, VC4_PACKET_GEM_HANDLES);
156 cl->reloc_next = *out;
157 cl_u32(out, 0); /* Space where hindex will be written. */
158 cl_u32(out, 0); /* Space where hindex will be written. */
159 }
160
161 static inline struct vc4_cl_out *
162 cl_start_shader_reloc(struct vc4_cl *cl, uint32_t n)
163 {
164 #ifdef DEBUG
165 assert(cl->reloc_count == 0);
166 cl->reloc_count = n;
167 #endif
168 cl->reloc_next = cl->next;
169
170 /* Reserve the space where hindex will be written. */
171 cl_advance(&cl->next, n * 4);
172
173 return cl->next;
174 }
175
176 static inline void
177 cl_reloc(struct vc4_context *vc4, struct vc4_cl *cl, struct vc4_cl_out **cl_out,
178 struct vc4_bo *bo, uint32_t offset)
179 {
180 *(uint32_t *)cl->reloc_next = vc4_gem_hindex(vc4, bo);
181 cl_advance(&cl->reloc_next, 4);
182
183 #ifdef DEBUG
184 cl->reloc_count--;
185 #endif
186
187 cl_u32(cl_out, offset);
188 }
189
190 static inline void
191 cl_aligned_reloc(struct vc4_context *vc4, struct vc4_cl *cl,
192 struct vc4_cl_out **cl_out,
193 struct vc4_bo *bo, uint32_t offset)
194 {
195 *(uint32_t *)cl->reloc_next = vc4_gem_hindex(vc4, bo);
196 cl_advance(&cl->reloc_next, 4);
197
198 #ifdef DEBUG
199 cl->reloc_count--;
200 #endif
201
202 cl_aligned_u32(cl_out, offset);
203 }
204
205 void cl_ensure_space(struct vc4_cl *cl, uint32_t size);
206
207 #endif /* VC4_CL_H */