i915: Tell the kernel when we actually need fence registers on our BOs.
[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(batch->map);
90 assert(intel_batchbuffer_space(batch) >= 4);
91 *(GLuint *) (batch->ptr) = dword;
92 batch->ptr += 4;
93 }
94
95 static INLINE void
96 intel_batchbuffer_require_space(struct intel_batchbuffer *batch,
97 GLuint sz)
98 {
99 assert(sz < batch->size - 8);
100 if (intel_batchbuffer_space(batch) < sz)
101 intel_batchbuffer_flush(batch);
102 }
103
104 static INLINE uint32_t float_as_int(float f)
105 {
106 union {
107 float f;
108 uint32_t d;
109 } fi;
110
111 fi.f = f;
112 return fi.d;
113 }
114
115 /* Here are the crusty old macros, to be removed:
116 */
117 #define BATCH_LOCALS
118
119 #define BEGIN_BATCH(n) do { \
120 intel_batchbuffer_require_space(intel->batch, (n)*4); \
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 assert((unsigned) (delta) < buf->size); \
132 intel_batchbuffer_emit_reloc(intel->batch, buf, \
133 read_domains, write_domain, delta); \
134 } while (0)
135 #define OUT_RELOC_FENCED(buf, read_domains, write_domain, delta) do { \
136 assert((unsigned) (delta) < buf->size); \
137 intel_batchbuffer_emit_reloc_fenced(intel->batch, buf, \
138 read_domains, write_domain, delta); \
139 } while (0)
140
141 #define ADVANCE_BATCH() do { \
142 unsigned int _n = intel->batch->ptr - intel->batch->emit.start_ptr; \
143 assert(intel->batch->emit.start_ptr != NULL); \
144 if (_n != intel->batch->emit.total) { \
145 fprintf(stderr, "ADVANCE_BATCH: %d of %d dwords emitted\n", \
146 _n, intel->batch->emit.total); \
147 abort(); \
148 } \
149 intel->batch->emit.start_ptr = NULL; \
150 } while(0)
151
152 #endif