Merge commit 'origin/7.8'
[mesa.git] / src / mesa / drivers / dri / intel / intel_batchbuffer.h
1 #ifndef INTEL_BATCHBUFFER_H
2 #define INTEL_BATCHBUFFER_H
3
4 #include "main/mtypes.h"
5
6 #include "intel_context.h"
7 #include "intel_bufmgr.h"
8 #include "intel_reg.h"
9
10 #define BATCH_SZ 16384
11 #define BATCH_RESERVED 16
12
13
14 struct intel_batchbuffer
15 {
16 struct intel_context *intel;
17
18 dri_bo *buf;
19
20 GLubyte *buffer;
21
22 GLubyte *map;
23 GLubyte *ptr;
24
25 GLuint size;
26
27 #ifdef DEBUG
28 /** Tracking of BEGIN_BATCH()/OUT_BATCH()/ADVANCE_BATCH() debugging */
29 struct {
30 GLuint total;
31 GLubyte *start_ptr;
32 } emit;
33 #endif
34
35 GLuint dirty_state;
36 GLuint reserved_space;
37 };
38
39 struct intel_batchbuffer *intel_batchbuffer_alloc(struct intel_context
40 *intel);
41
42 void intel_batchbuffer_free(struct intel_batchbuffer *batch);
43
44
45 void _intel_batchbuffer_flush(struct intel_batchbuffer *batch,
46 const char *file, int line);
47
48 #define intel_batchbuffer_flush(batch) \
49 _intel_batchbuffer_flush(batch, __FILE__, __LINE__)
50
51 void intel_batchbuffer_reset(struct intel_batchbuffer *batch);
52
53
54 /* Unlike bmBufferData, this currently requires the buffer be mapped.
55 * Consider it a convenience function wrapping multple
56 * intel_buffer_dword() calls.
57 */
58 void intel_batchbuffer_data(struct intel_batchbuffer *batch,
59 const void *data, GLuint bytes);
60
61 void intel_batchbuffer_release_space(struct intel_batchbuffer *batch,
62 GLuint bytes);
63
64 GLboolean intel_batchbuffer_emit_reloc(struct intel_batchbuffer *batch,
65 dri_bo *buffer,
66 uint32_t read_domains,
67 uint32_t write_domain,
68 uint32_t offset);
69 GLboolean intel_batchbuffer_emit_reloc_fenced(struct intel_batchbuffer *batch,
70 drm_intel_bo *buffer,
71 uint32_t read_domains,
72 uint32_t write_domain,
73 uint32_t offset);
74 void intel_batchbuffer_emit_mi_flush(struct intel_batchbuffer *batch);
75
76 static INLINE uint32_t float_as_int(float f)
77 {
78 union {
79 float f;
80 uint32_t d;
81 } fi;
82
83 fi.f = f;
84 return fi.d;
85 }
86
87 /* Inline functions - might actually be better off with these
88 * non-inlined. Certainly better off switching all command packets to
89 * be passed as structs rather than dwords, but that's a little bit of
90 * work...
91 */
92 static INLINE GLint
93 intel_batchbuffer_space(struct intel_batchbuffer *batch)
94 {
95 return (batch->size - batch->reserved_space) - (batch->ptr - batch->map);
96 }
97
98
99 static INLINE void
100 intel_batchbuffer_emit_dword(struct intel_batchbuffer *batch, GLuint dword)
101 {
102 #ifdef DEBUG
103 assert(intel_batchbuffer_space(batch) >= 4);
104 #endif
105 *(GLuint *) (batch->ptr) = dword;
106 batch->ptr += 4;
107 }
108
109 static INLINE void
110 intel_batchbuffer_emit_float(struct intel_batchbuffer *batch, float f)
111 {
112 intel_batchbuffer_emit_dword(batch, float_as_int(f));
113 }
114
115 static INLINE void
116 intel_batchbuffer_require_space(struct intel_batchbuffer *batch,
117 GLuint sz)
118 {
119 #ifdef DEBUG
120 assert(sz < batch->size - 8);
121 #endif
122 if (intel_batchbuffer_space(batch) < sz)
123 intel_batchbuffer_flush(batch);
124 }
125
126 static INLINE void
127 intel_batchbuffer_begin(struct intel_batchbuffer *batch, int n)
128 {
129 intel_batchbuffer_require_space(batch, n * 4);
130 #ifdef DEBUG
131 assert(batch->map);
132 assert(batch->emit.start_ptr == NULL);
133 batch->emit.total = n * 4;
134 batch->emit.start_ptr = batch->ptr;
135 #endif
136 }
137
138 static INLINE void
139 intel_batchbuffer_advance(struct intel_batchbuffer *batch)
140 {
141 #ifdef DEBUG
142 unsigned int _n = batch->ptr - batch->emit.start_ptr;
143 assert(batch->emit.start_ptr != NULL);
144 if (_n != batch->emit.total) {
145 fprintf(stderr, "ADVANCE_BATCH: %d of %d dwords emitted\n",
146 _n, batch->emit.total);
147 abort();
148 }
149 batch->emit.start_ptr = NULL;
150 #endif
151 }
152
153 /* Here are the crusty old macros, to be removed:
154 */
155 #define BATCH_LOCALS
156
157 #define BEGIN_BATCH(n) intel_batchbuffer_begin(intel->batch, n)
158 #define OUT_BATCH(d) intel_batchbuffer_emit_dword(intel->batch, d)
159 #define OUT_BATCH_F(f) intel_batchbuffer_emit_float(intel->batch,f)
160 #define OUT_RELOC(buf, read_domains, write_domain, delta) do { \
161 intel_batchbuffer_emit_reloc(intel->batch, buf, \
162 read_domains, write_domain, delta); \
163 } while (0)
164 #define OUT_RELOC_FENCED(buf, read_domains, write_domain, delta) do { \
165 intel_batchbuffer_emit_reloc_fenced(intel->batch, buf, \
166 read_domains, write_domain, delta); \
167 } while (0)
168
169 #define ADVANCE_BATCH() intel_batchbuffer_advance(intel->batch);
170
171 #endif