f1a5c1fd51b93c3389178f9cfaadc6e6181ab64c
[mesa.git] / src / mesa / drivers / dri / i965 / intel_batchbuffer.h
1 #ifndef INTEL_BATCHBUFFER_H
2 #define INTEL_BATCHBUFFER_H
3
4 #include "main/mtypes.h"
5
6 #include "brw_context.h"
7 #include "brw_bufmgr.h"
8
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12
13 /**
14 * Number of bytes to reserve for commands necessary to complete a batch.
15 *
16 * This includes:
17 * - MI_BATCHBUFFER_END (4 bytes)
18 * - Optional MI_NOOP for ensuring the batch length is qword aligned (4 bytes)
19 * - Any state emitted by vtbl->finish_batch():
20 * - Gen4-5 record ending occlusion query values (4 * 4 = 16 bytes)
21 * - Disabling OA counters on Gen6+ (3 DWords = 12 bytes)
22 * - Ending MI_REPORT_PERF_COUNT on Gen5+, plus associated PIPE_CONTROLs:
23 * - Two sets of PIPE_CONTROLs, which become 4 PIPE_CONTROLs each on SNB,
24 * which are 5 DWords each ==> 2 * 4 * 5 * 4 = 160 bytes
25 * - 3 DWords for MI_REPORT_PERF_COUNT itself on Gen6+. ==> 12 bytes.
26 * On Ironlake, it's 6 DWords, but we have some slack due to the lack of
27 * Sandybridge PIPE_CONTROL madness.
28 * - CC_STATE workaround on HSW (17 * 4 = 68 bytes)
29 * - 10 dwords for initial mi_flush
30 * - 2 dwords for CC state setup
31 * - 5 dwords for the required pipe control at the end
32 * - Restoring L3 configuration: (24 dwords = 96 bytes)
33 * - 2*6 dwords for two PIPE_CONTROL flushes.
34 * - 7 dwords for L3 configuration set-up.
35 * - 5 dwords for L3 atomic set-up (on HSW).
36 */
37 #define BATCH_RESERVED 308
38
39 struct intel_batchbuffer;
40
41 void intel_batchbuffer_init(struct intel_batchbuffer *batch,
42 struct brw_bufmgr *bufmgr,
43 bool has_llc);
44 void intel_batchbuffer_free(struct intel_batchbuffer *batch);
45 void intel_batchbuffer_save_state(struct brw_context *brw);
46 void intel_batchbuffer_reset_to_saved(struct brw_context *brw);
47 void intel_batchbuffer_require_space(struct brw_context *brw, GLuint sz,
48 enum brw_gpu_ring ring);
49 int _intel_batchbuffer_flush_fence(struct brw_context *brw,
50 int in_fence_fd, int *out_fence_fd,
51 const char *file, int line);
52
53 #define intel_batchbuffer_flush(brw) \
54 _intel_batchbuffer_flush_fence((brw), -1, NULL, __FILE__, __LINE__)
55
56 #define intel_batchbuffer_flush_fence(brw, in_fence_fd, out_fence_fd) \
57 _intel_batchbuffer_flush_fence((brw), (in_fence_fd), (out_fence_fd), \
58 __FILE__, __LINE__)
59
60 /* Unlike bmBufferData, this currently requires the buffer be mapped.
61 * Consider it a convenience function wrapping multple
62 * intel_buffer_dword() calls.
63 */
64 void intel_batchbuffer_data(struct brw_context *brw,
65 const void *data, GLuint bytes,
66 enum brw_gpu_ring ring);
67
68 bool brw_batch_has_aperture_space(struct brw_context *brw,
69 unsigned extra_space_in_bytes);
70
71 bool brw_batch_references(struct intel_batchbuffer *batch, struct brw_bo *bo);
72
73 uint64_t brw_emit_reloc(struct intel_batchbuffer *batch, uint32_t batch_offset,
74 struct brw_bo *target, uint32_t target_offset,
75 uint32_t read_domains, uint32_t write_domain);
76
77 #define USED_BATCH(batch) ((uintptr_t)((batch).map_next - (batch).map))
78
79 static inline uint32_t float_as_int(float f)
80 {
81 union {
82 float f;
83 uint32_t d;
84 } fi;
85
86 fi.f = f;
87 return fi.d;
88 }
89
90 /* Inline functions - might actually be better off with these
91 * non-inlined. Certainly better off switching all command packets to
92 * be passed as structs rather than dwords, but that's a little bit of
93 * work...
94 */
95 static inline unsigned
96 intel_batchbuffer_space(struct intel_batchbuffer *batch)
97 {
98 return (batch->state_batch_offset - batch->reserved_space)
99 - USED_BATCH(*batch) * 4;
100 }
101
102
103 static inline void
104 intel_batchbuffer_emit_dword(struct intel_batchbuffer *batch, GLuint dword)
105 {
106 #ifdef DEBUG
107 assert(intel_batchbuffer_space(batch) >= 4);
108 #endif
109 *batch->map_next++ = dword;
110 assert(batch->ring != UNKNOWN_RING);
111 }
112
113 static inline void
114 intel_batchbuffer_emit_float(struct intel_batchbuffer *batch, float f)
115 {
116 intel_batchbuffer_emit_dword(batch, float_as_int(f));
117 }
118
119 static inline void
120 intel_batchbuffer_begin(struct brw_context *brw, int n, enum brw_gpu_ring ring)
121 {
122 intel_batchbuffer_require_space(brw, n * 4, ring);
123
124 #ifdef DEBUG
125 brw->batch.emit = USED_BATCH(brw->batch);
126 brw->batch.total = n;
127 #endif
128 }
129
130 static inline void
131 intel_batchbuffer_advance(struct brw_context *brw)
132 {
133 #ifdef DEBUG
134 struct intel_batchbuffer *batch = &brw->batch;
135 unsigned int _n = USED_BATCH(*batch) - batch->emit;
136 assert(batch->total != 0);
137 if (_n != batch->total) {
138 fprintf(stderr, "ADVANCE_BATCH: %d of %d dwords emitted\n",
139 _n, batch->total);
140 abort();
141 }
142 batch->total = 0;
143 #else
144 (void) brw;
145 #endif
146 }
147
148 #define BEGIN_BATCH(n) do { \
149 intel_batchbuffer_begin(brw, (n), RENDER_RING); \
150 uint32_t *__map = brw->batch.map_next; \
151 brw->batch.map_next += (n)
152
153 #define BEGIN_BATCH_BLT(n) do { \
154 intel_batchbuffer_begin(brw, (n), BLT_RING); \
155 uint32_t *__map = brw->batch.map_next; \
156 brw->batch.map_next += (n)
157
158 #define OUT_BATCH(d) *__map++ = (d)
159 #define OUT_BATCH_F(f) OUT_BATCH(float_as_int((f)))
160
161 #define OUT_RELOC(buf, read_domains, write_domain, delta) do { \
162 uint32_t __offset = (__map - brw->batch.map) * 4; \
163 uint32_t reloc = \
164 brw_emit_reloc(&brw->batch, __offset, (buf), (delta), \
165 (read_domains), (write_domain)); \
166 OUT_BATCH(reloc); \
167 } while (0)
168
169 /* Handle 48-bit address relocations for Gen8+ */
170 #define OUT_RELOC64(buf, read_domains, write_domain, delta) do { \
171 uint32_t __offset = (__map - brw->batch.map) * 4; \
172 uint64_t reloc64 = \
173 brw_emit_reloc(&brw->batch, __offset, (buf), (delta), \
174 (read_domains), (write_domain)); \
175 OUT_BATCH(reloc64); \
176 OUT_BATCH(reloc64 >> 32); \
177 } while (0)
178
179 #define ADVANCE_BATCH() \
180 assert(__map == brw->batch.map_next); \
181 intel_batchbuffer_advance(brw); \
182 } while (0)
183
184 #ifdef __cplusplus
185 }
186 #endif
187
188 #endif