Merge remote branch 'origin/master' into pipe-video
[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 drm_intel_bo *buf;
19
20 GLubyte *map;
21 GLubyte *ptr;
22
23 GLuint size;
24 uint32_t state_batch_offset;
25
26 #ifdef DEBUG
27 /** Tracking of BEGIN_BATCH()/OUT_BATCH()/ADVANCE_BATCH() debugging */
28 struct {
29 GLuint total;
30 GLubyte *start_ptr;
31 } emit;
32 #endif
33
34 GLuint dirty_state;
35 GLuint reserved_space;
36 };
37
38 struct intel_batchbuffer *intel_batchbuffer_alloc(struct intel_context
39 *intel);
40
41 void intel_batchbuffer_free(struct intel_batchbuffer *batch);
42
43
44 void _intel_batchbuffer_flush(struct intel_batchbuffer *batch,
45 const char *file, int line);
46
47 #define intel_batchbuffer_flush(batch) \
48 _intel_batchbuffer_flush(batch, __FILE__, __LINE__)
49
50 void intel_batchbuffer_reset(struct intel_batchbuffer *batch);
51
52
53 /* Unlike bmBufferData, this currently requires the buffer be mapped.
54 * Consider it a convenience function wrapping multple
55 * intel_buffer_dword() calls.
56 */
57 void intel_batchbuffer_data(struct intel_batchbuffer *batch,
58 const void *data, GLuint bytes);
59
60 void intel_batchbuffer_release_space(struct intel_batchbuffer *batch,
61 GLuint bytes);
62
63 GLboolean intel_batchbuffer_emit_reloc(struct intel_batchbuffer *batch,
64 drm_intel_bo *buffer,
65 uint32_t read_domains,
66 uint32_t write_domain,
67 uint32_t offset);
68 GLboolean intel_batchbuffer_emit_reloc_fenced(struct intel_batchbuffer *batch,
69 drm_intel_bo *buffer,
70 uint32_t read_domains,
71 uint32_t write_domain,
72 uint32_t offset);
73 void intel_batchbuffer_emit_mi_flush(struct intel_batchbuffer *batch);
74
75 static INLINE uint32_t float_as_int(float f)
76 {
77 union {
78 float f;
79 uint32_t d;
80 } fi;
81
82 fi.f = f;
83 return fi.d;
84 }
85
86 /* Inline functions - might actually be better off with these
87 * non-inlined. Certainly better off switching all command packets to
88 * be passed as structs rather than dwords, but that's a little bit of
89 * work...
90 */
91 static INLINE GLint
92 intel_batchbuffer_space(struct intel_batchbuffer *batch)
93 {
94 return (batch->state_batch_offset - batch->reserved_space) -
95 (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