intel: Move the assertions about reloc delta from the macros to the function.
[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 /** Tracking of BEGIN_BATCH()/OUT_BATCH()/ADVANCE_BATCH() debugging */
28 struct {
29 GLuint total;
30 GLubyte *start_ptr;
31 } emit;
32
33 GLuint dirty_state;
34 GLuint reserved_space;
35 };
36
37 struct intel_batchbuffer *intel_batchbuffer_alloc(struct intel_context
38 *intel);
39
40 void intel_batchbuffer_free(struct intel_batchbuffer *batch);
41
42
43 void _intel_batchbuffer_flush(struct intel_batchbuffer *batch,
44 const char *file, int line);
45
46 #define intel_batchbuffer_flush(batch) \
47 _intel_batchbuffer_flush(batch, __FILE__, __LINE__)
48
49 void intel_batchbuffer_reset(struct intel_batchbuffer *batch);
50
51
52 /* Unlike bmBufferData, this currently requires the buffer be mapped.
53 * Consider it a convenience function wrapping multple
54 * intel_buffer_dword() calls.
55 */
56 void intel_batchbuffer_data(struct intel_batchbuffer *batch,
57 const void *data, GLuint bytes);
58
59 void intel_batchbuffer_release_space(struct intel_batchbuffer *batch,
60 GLuint bytes);
61
62 GLboolean intel_batchbuffer_emit_reloc(struct intel_batchbuffer *batch,
63 dri_bo *buffer,
64 uint32_t read_domains,
65 uint32_t write_domain,
66 uint32_t offset);
67 GLboolean intel_batchbuffer_emit_reloc_fenced(struct intel_batchbuffer *batch,
68 drm_intel_bo *buffer,
69 uint32_t read_domains,
70 uint32_t write_domain,
71 uint32_t offset);
72 void intel_batchbuffer_emit_mi_flush(struct intel_batchbuffer *batch);
73
74 /* Inline functions - might actually be better off with these
75 * non-inlined. Certainly better off switching all command packets to
76 * be passed as structs rather than dwords, but that's a little bit of
77 * work...
78 */
79 static INLINE GLint
80 intel_batchbuffer_space(struct intel_batchbuffer *batch)
81 {
82 return (batch->size - batch->reserved_space) - (batch->ptr - batch->map);
83 }
84
85
86 static INLINE void
87 intel_batchbuffer_emit_dword(struct intel_batchbuffer *batch, GLuint dword)
88 {
89 assert(intel_batchbuffer_space(batch) >= 4);
90 *(GLuint *) (batch->ptr) = dword;
91 batch->ptr += 4;
92 }
93
94 static INLINE void
95 intel_batchbuffer_require_space(struct intel_batchbuffer *batch,
96 GLuint sz)
97 {
98 assert(sz < batch->size - 8);
99 if (intel_batchbuffer_space(batch) < sz)
100 intel_batchbuffer_flush(batch);
101 }
102
103 static INLINE uint32_t float_as_int(float f)
104 {
105 union {
106 float f;
107 uint32_t d;
108 } fi;
109
110 fi.f = f;
111 return fi.d;
112 }
113
114 /* Here are the crusty old macros, to be removed:
115 */
116 #define BATCH_LOCALS
117
118 #define BEGIN_BATCH(n) do { \
119 intel_batchbuffer_require_space(intel->batch, (n)*4); \
120 assert(intel->batch->map); \
121 assert(intel->batch->emit.start_ptr == NULL); \
122 intel->batch->emit.total = (n) * 4; \
123 intel->batch->emit.start_ptr = intel->batch->ptr; \
124 } while (0)
125
126 #define OUT_BATCH(d) intel_batchbuffer_emit_dword(intel->batch, d)
127 #define OUT_BATCH_F(f) intel_batchbuffer_emit_dword(intel->batch, \
128 float_as_int(f))
129
130 #define OUT_RELOC(buf, read_domains, write_domain, delta) do { \
131 intel_batchbuffer_emit_reloc(intel->batch, buf, \
132 read_domains, write_domain, delta); \
133 } while (0)
134 #define OUT_RELOC_FENCED(buf, read_domains, write_domain, delta) do { \
135 intel_batchbuffer_emit_reloc_fenced(intel->batch, buf, \
136 read_domains, write_domain, delta); \
137 } while (0)
138
139 #define ADVANCE_BATCH() do { \
140 unsigned int _n = intel->batch->ptr - intel->batch->emit.start_ptr; \
141 assert(intel->batch->emit.start_ptr != NULL); \
142 if (_n != intel->batch->emit.total) { \
143 fprintf(stderr, "ADVANCE_BATCH: %d of %d dwords emitted\n", \
144 _n, intel->batch->emit.total); \
145 abort(); \
146 } \
147 intel->batch->emit.start_ptr = NULL; \
148 } while(0)
149
150 #endif