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