vc4: Optimize CL emits by doing size checks up front.
[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 uint32_t size;
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_reset_cl(struct vc4_cl *cl);
45 void vc4_dump_cl(void *cl, uint32_t size, bool is_render);
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 assert((cl->next - cl->base) + 1 <= cl->size);
52
53 *(uint8_t *)cl->next = n;
54 cl->next++;
55 }
56
57 static inline void
58 cl_u16(struct vc4_cl *cl, uint32_t n)
59 {
60 assert((cl->next - cl->base) + 2 <= cl->size);
61
62 *(uint16_t *)cl->next = n;
63 cl->next += 2;
64 }
65
66 static inline void
67 cl_u32(struct vc4_cl *cl, uint32_t n)
68 {
69 assert((cl->next - cl->base) + 4 <= cl->size);
70
71 *(uint32_t *)cl->next = n;
72 cl->next += 4;
73 }
74
75 static inline void
76 cl_ptr(struct vc4_cl *cl, void *ptr)
77 {
78 assert((cl->next - cl->base) + sizeof(void *) <= cl->size);
79
80 *(void **)cl->next = ptr;
81 cl->next += sizeof(void *);
82 }
83
84 static inline void
85 cl_f(struct vc4_cl *cl, float f)
86 {
87 cl_u32(cl, fui(f));
88 }
89
90 static inline void
91 cl_start_reloc(struct vc4_cl *cl, uint32_t n)
92 {
93 assert(n == 1 || n == 2);
94 assert(cl->reloc_count == 0);
95 cl->reloc_count = n;
96
97 cl_u8(cl, VC4_PACKET_GEM_HANDLES);
98 cl->reloc_next = cl->next - cl->base;
99 cl_u32(cl, 0); /* Space where hindex will be written. */
100 cl_u32(cl, 0); /* Space where hindex will be written. */
101 }
102
103 static inline void
104 cl_start_shader_reloc(struct vc4_cl *cl, uint32_t n)
105 {
106 assert(cl->reloc_count == 0);
107 cl->reloc_count = n;
108 cl->reloc_next = cl->next - cl->base;
109
110 for (int i = 0; i < n; i++)
111 cl_u32(cl, 0); /* Space where hindex will be written. */
112 }
113
114 static inline void
115 cl_reloc_hindex(struct vc4_cl *cl, uint32_t hindex, uint32_t offset)
116 {
117 *(uint32_t *)(cl->base + cl->reloc_next) = hindex;
118 cl->reloc_next += 4;
119
120 cl->reloc_count--;
121
122 cl_u32(cl, offset);
123 }
124
125 static inline void
126 cl_reloc(struct vc4_context *vc4, struct vc4_cl *cl,
127 struct vc4_bo *bo, uint32_t offset)
128 {
129 cl_reloc_hindex(cl, vc4_gem_hindex(vc4, bo), offset);
130 }
131
132 void cl_ensure_space(struct vc4_cl *cl, uint32_t size);
133
134 #endif /* VC4_CL_H */