i965: Reorganize batch/state BO fields into a 'brw_growing_bo' struct.
[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 /* The kernel assumes batchbuffers are smaller than 256kB. */
14 #define MAX_BATCH_SIZE (256 * 1024)
15
16 /* 3DSTATE_BINDING_TABLE_POINTERS has a U16 offset from Surface State Base
17 * Address, which means that we can't put binding tables beyond 64kB. This
18 * effectively limits the maximum statebuffer size to 64kB.
19 */
20 #define MAX_STATE_SIZE (64 * 1024)
21
22 struct intel_batchbuffer;
23
24 void intel_batchbuffer_init(struct brw_context *brw);
25 void intel_batchbuffer_free(struct intel_batchbuffer *batch);
26 void intel_batchbuffer_save_state(struct brw_context *brw);
27 void intel_batchbuffer_reset_to_saved(struct brw_context *brw);
28 void intel_batchbuffer_require_space(struct brw_context *brw, GLuint sz,
29 enum brw_gpu_ring ring);
30 int _intel_batchbuffer_flush_fence(struct brw_context *brw,
31 int in_fence_fd, int *out_fence_fd,
32 const char *file, int line);
33
34 #define intel_batchbuffer_flush(brw) \
35 _intel_batchbuffer_flush_fence((brw), -1, NULL, __FILE__, __LINE__)
36
37 #define intel_batchbuffer_flush_fence(brw, in_fence_fd, out_fence_fd) \
38 _intel_batchbuffer_flush_fence((brw), (in_fence_fd), (out_fence_fd), \
39 __FILE__, __LINE__)
40
41 /* Unlike bmBufferData, this currently requires the buffer be mapped.
42 * Consider it a convenience function wrapping multple
43 * intel_buffer_dword() calls.
44 */
45 void intel_batchbuffer_data(struct brw_context *brw,
46 const void *data, GLuint bytes,
47 enum brw_gpu_ring ring);
48
49 bool brw_batch_has_aperture_space(struct brw_context *brw,
50 unsigned extra_space_in_bytes);
51
52 bool brw_batch_references(struct intel_batchbuffer *batch, struct brw_bo *bo);
53
54 #define RELOC_WRITE EXEC_OBJECT_WRITE
55 #define RELOC_NEEDS_GGTT EXEC_OBJECT_NEEDS_GTT
56 uint64_t brw_batch_reloc(struct intel_batchbuffer *batch,
57 uint32_t batch_offset,
58 struct brw_bo *target,
59 uint32_t target_offset,
60 unsigned flags);
61 uint64_t brw_state_reloc(struct intel_batchbuffer *batch,
62 uint32_t batch_offset,
63 struct brw_bo *target,
64 uint32_t target_offset,
65 unsigned flags);
66
67 #define USED_BATCH(_batch) \
68 ((uintptr_t)((_batch).map_next - (_batch).batch.map))
69
70 static inline uint32_t float_as_int(float f)
71 {
72 union {
73 float f;
74 uint32_t d;
75 } fi;
76
77 fi.f = f;
78 return fi.d;
79 }
80
81 static inline void
82 intel_batchbuffer_emit_dword(struct intel_batchbuffer *batch, GLuint dword)
83 {
84 *batch->map_next++ = dword;
85 assert(batch->ring != UNKNOWN_RING);
86 }
87
88 static inline void
89 intel_batchbuffer_emit_float(struct intel_batchbuffer *batch, float f)
90 {
91 intel_batchbuffer_emit_dword(batch, float_as_int(f));
92 }
93
94 static inline void
95 intel_batchbuffer_begin(struct brw_context *brw, int n, enum brw_gpu_ring ring)
96 {
97 intel_batchbuffer_require_space(brw, n * 4, ring);
98
99 #ifdef DEBUG
100 brw->batch.emit = USED_BATCH(brw->batch);
101 brw->batch.total = n;
102 #endif
103 }
104
105 static inline void
106 intel_batchbuffer_advance(struct brw_context *brw)
107 {
108 #ifdef DEBUG
109 struct intel_batchbuffer *batch = &brw->batch;
110 unsigned int _n = USED_BATCH(*batch) - batch->emit;
111 assert(batch->total != 0);
112 if (_n != batch->total) {
113 fprintf(stderr, "ADVANCE_BATCH: %d of %d dwords emitted\n",
114 _n, batch->total);
115 abort();
116 }
117 batch->total = 0;
118 #else
119 (void) brw;
120 #endif
121 }
122
123 static inline bool
124 brw_ptr_in_state_buffer(struct intel_batchbuffer *batch, void *p)
125 {
126 return (char *) p >= (char *) batch->state.map &&
127 (char *) p < (char *) batch->state.map + batch->state.bo->size;
128 }
129
130 #define BEGIN_BATCH(n) do { \
131 intel_batchbuffer_begin(brw, (n), RENDER_RING); \
132 uint32_t *__map = brw->batch.map_next; \
133 brw->batch.map_next += (n)
134
135 #define BEGIN_BATCH_BLT(n) do { \
136 intel_batchbuffer_begin(brw, (n), BLT_RING); \
137 uint32_t *__map = brw->batch.map_next; \
138 brw->batch.map_next += (n)
139
140 #define OUT_BATCH(d) *__map++ = (d)
141 #define OUT_BATCH_F(f) OUT_BATCH(float_as_int((f)))
142
143 #define OUT_RELOC(buf, flags, delta) do { \
144 uint32_t __offset = (__map - brw->batch.batch.map) * 4; \
145 uint32_t reloc = \
146 brw_batch_reloc(&brw->batch, __offset, (buf), (delta), (flags)); \
147 OUT_BATCH(reloc); \
148 } while (0)
149
150 /* Handle 48-bit address relocations for Gen8+ */
151 #define OUT_RELOC64(buf, flags, delta) do { \
152 uint32_t __offset = (__map - brw->batch.batch.map) * 4; \
153 uint64_t reloc64 = \
154 brw_batch_reloc(&brw->batch, __offset, (buf), (delta), (flags)); \
155 OUT_BATCH(reloc64); \
156 OUT_BATCH(reloc64 >> 32); \
157 } while (0)
158
159 #define ADVANCE_BATCH() \
160 assert(__map == brw->batch.map_next); \
161 intel_batchbuffer_advance(brw); \
162 } while (0)
163
164 #ifdef __cplusplus
165 }
166 #endif
167
168 #endif