Merge branch '7.8'
[mesa.git] / src / gallium / drivers / i965 / brw_batchbuffer.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "util/u_memory.h"
29
30 #include "brw_batchbuffer.h"
31 #include "brw_reg.h"
32 #include "brw_winsys.h"
33 #include "brw_debug.h"
34 #include "brw_structs.h"
35
36 #define ALWAYS_EMIT_MI_FLUSH 1
37
38 enum pipe_error
39 brw_batchbuffer_reset(struct brw_batchbuffer *batch)
40 {
41 enum pipe_error ret;
42
43 ret = batch->sws->bo_alloc( batch->sws,
44 BRW_BUFFER_TYPE_BATCH,
45 BRW_BATCH_SIZE, 4096,
46 &batch->buf );
47 if (ret)
48 return ret;
49
50 batch->size = BRW_BATCH_SIZE;
51
52 /* With map_range semantics, the winsys can decide whether to
53 * inject a malloc'ed bounce buffer instead of mapping directly.
54 */
55 batch->map = batch->sws->bo_map(batch->buf,
56 BRW_DATA_BATCH_BUFFER,
57 0, batch->size,
58 GL_TRUE,
59 GL_TRUE,
60 GL_TRUE);
61
62 batch->ptr = batch->map;
63 return PIPE_OK;
64 }
65
66 struct brw_batchbuffer *
67 brw_batchbuffer_alloc(struct brw_winsys_screen *sws,
68 struct brw_chipset chipset)
69 {
70 struct brw_batchbuffer *batch = CALLOC_STRUCT(brw_batchbuffer);
71
72 batch->sws = sws;
73 batch->chipset = chipset;
74 brw_batchbuffer_reset(batch);
75
76 return batch;
77 }
78
79 void
80 brw_batchbuffer_free(struct brw_batchbuffer *batch)
81 {
82 if (batch->map) {
83 batch->sws->bo_unmap(batch->buf);
84 batch->map = NULL;
85 }
86
87 bo_reference(&batch->buf, NULL);
88 FREE(batch);
89 }
90
91
92 void
93 _brw_batchbuffer_flush(struct brw_batchbuffer *batch,
94 const char *file,
95 int line)
96 {
97 GLuint used = batch->ptr - batch->map;
98
99 if (used == 0)
100 return;
101
102 /* Post-swap throttling done by the state tracker.
103 */
104
105 if (BRW_DEBUG & DEBUG_BATCH)
106 debug_printf("%s:%d: Batchbuffer flush with %db used\n",
107 file, line, used);
108
109 if (ALWAYS_EMIT_MI_FLUSH) {
110 *(GLuint *) (batch->ptr) = MI_FLUSH | BRW_FLUSH_STATE_CACHE;
111 batch->ptr += 4;
112 used = batch->ptr - batch->map;
113 }
114
115 /* Round batchbuffer usage to 2 DWORDs.
116 */
117 if ((used & 4) == 0) {
118 *(GLuint *) (batch->ptr) = 0; /* noop */
119 batch->ptr += 4;
120 used = batch->ptr - batch->map;
121 }
122
123 /* Mark the end of the buffer.
124 */
125 *(GLuint *) (batch->ptr) = MI_BATCH_BUFFER_END;
126 batch->ptr += 4;
127 used = batch->ptr - batch->map;
128
129 batch->sws->bo_flush_range(batch->buf, 0, used);
130 batch->sws->bo_unmap(batch->buf);
131 batch->map = NULL;
132 batch->ptr = NULL;
133
134 batch->sws->bo_exec(batch->buf, used );
135
136 if (BRW_DEBUG & DEBUG_SYNC) {
137 /* Abuse map/unmap to achieve wait-for-fence.
138 *
139 * XXX: hide this inside the winsys and export a fence
140 * interface.
141 */
142 debug_printf("waiting for idle\n");
143 batch->sws->bo_wait_idle(batch->buf);
144 }
145
146 /* Reset the buffer:
147 */
148 brw_batchbuffer_reset(batch);
149 }
150
151
152 /* The OUT_RELOC() macro ends up here, generating a relocation within
153 * the batch buffer.
154 */
155 enum pipe_error
156 brw_batchbuffer_emit_reloc(struct brw_batchbuffer *batch,
157 struct brw_winsys_buffer *buffer,
158 enum brw_buffer_usage usage,
159 uint32_t delta)
160 {
161 int ret;
162
163 if (batch->ptr - batch->map > batch->buf->size) {
164 debug_printf("bad relocation ptr %p map %p offset %d size %d\n",
165 batch->ptr, batch->map, batch->ptr - batch->map, batch->buf->size);
166
167 return PIPE_ERROR_OUT_OF_MEMORY;
168 }
169
170 ret = batch->sws->bo_emit_reloc(batch->buf,
171 usage,
172 delta,
173 batch->ptr - batch->map,
174 buffer);
175 if (ret != 0)
176 return ret;
177
178 /* bo_emit_reloc was resposible for writing a zero into the
179 * batchbuffer if necessary. Just need to update our pointer.
180 */
181 batch->ptr += 4;
182
183 return 0;
184 }
185
186 enum pipe_error
187 brw_batchbuffer_data(struct brw_batchbuffer *batch,
188 const void *data, GLuint bytes,
189 enum cliprect_mode cliprect_mode)
190 {
191 enum pipe_error ret;
192
193 assert((bytes & 3) == 0);
194
195 ret = brw_batchbuffer_require_space(batch, bytes);
196 if (ret)
197 return ret;
198
199 memcpy(batch->ptr, data, bytes);
200 batch->ptr += bytes;
201 return 0;
202 }