vc4: Rename GEM_HANDLES to be in a namespace.
[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
31 #include "vc4_packet.h"
32
33 struct vc4_bo;
34
35 struct vc4_cl {
36 void *base;
37 void *next;
38 void *end;
39 uint32_t reloc_next;
40 uint32_t reloc_count;
41 };
42
43 void vc4_init_cl(struct vc4_context *vc4, struct vc4_cl *cl);
44 void vc4_grow_cl(struct vc4_cl *cl);
45 void vc4_reset_cl(struct vc4_cl *cl);
46 uint32_t vc4_gem_hindex(struct vc4_context *vc4, struct vc4_bo *bo);
47
48 static inline void
49 cl_u8(struct vc4_cl *cl, uint8_t n)
50 {
51 if (cl->next + 1 > cl->end)
52 vc4_grow_cl(cl);
53
54 *(uint8_t *)cl->next = n;
55 cl->next++;
56 }
57
58 static inline void
59 cl_u16(struct vc4_cl *cl, uint32_t n)
60 {
61 if (cl->next + 2 > cl->end)
62 vc4_grow_cl(cl);
63
64 *(uint16_t *)cl->next = n;
65 cl->next += 2;
66 }
67
68 static inline void
69 cl_u32(struct vc4_cl *cl, uint32_t n)
70 {
71 if (cl->next + 4 > cl->end)
72 vc4_grow_cl(cl);
73
74 *(uint32_t *)cl->next = n;
75 cl->next += 4;
76 }
77
78 static inline void
79 cl_ptr(struct vc4_cl *cl, void *ptr)
80 {
81 if (cl->next + sizeof(void *) > cl->end)
82 vc4_grow_cl(cl);
83
84 *(void **)cl->next = ptr;
85 cl->next += sizeof(void *);
86 }
87
88 static inline void
89 cl_f(struct vc4_cl *cl, float f)
90 {
91 cl_u32(cl, fui(f));
92 }
93
94 static inline void
95 cl_start_reloc(struct vc4_cl *cl, uint32_t n)
96 {
97 assert(n == 1 || n == 2);
98 assert(cl->reloc_count == 0);
99 cl->reloc_count = n;
100
101 cl_u8(cl, VC4_PACKET_GEM_HANDLES);
102 cl->reloc_next = cl->next - cl->base;
103 cl_u32(cl, 0); /* Space where hindex will be written. */
104 cl_u32(cl, 0); /* Space where hindex will be written. */
105 }
106
107 static inline void
108 cl_start_shader_reloc(struct vc4_cl *cl, uint32_t n)
109 {
110 assert(cl->reloc_count == 0);
111 cl->reloc_count = n;
112 cl->reloc_next = cl->next - cl->base;
113
114 for (int i = 0; i < n; i++)
115 cl_u32(cl, 0); /* Space where hindex will be written. */
116 }
117
118 static inline void
119 cl_reloc(struct vc4_context *vc4, struct vc4_cl *cl,
120 struct vc4_bo *bo, uint32_t offset)
121 {
122 *(uint32_t *)(cl->base + cl->reloc_next) = vc4_gem_hindex(vc4, bo);
123 cl->reloc_next += 4;
124
125 cl->reloc_count--;
126
127 cl_u32(cl, offset);
128 }
129
130 #endif /* VC4_CL_H */