1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "util/u_memory.h"
30 #include "brw_batchbuffer.h"
32 #include "brw_winsys.h"
33 #include "brw_debug.h"
34 #include "brw_structs.h"
36 #define ALWAYS_EMIT_MI_FLUSH 1
39 brw_batchbuffer_reset(struct brw_batchbuffer *batch)
43 ret = batch->sws->bo_alloc( batch->sws,
44 BRW_BUFFER_TYPE_BATCH,
50 batch->size = BRW_BATCH_SIZE;
52 /* With map_range semantics, the winsys can decide whether to
53 * inject a malloc'ed bounce buffer instead of mapping directly.
55 batch->map = batch->sws->bo_map(batch->buf,
56 BRW_DATA_BATCH_BUFFER,
62 batch->ptr = batch->map;
66 struct brw_batchbuffer *
67 brw_batchbuffer_alloc(struct brw_winsys_screen *sws)
69 struct brw_batchbuffer *batch = CALLOC_STRUCT(brw_batchbuffer);
72 brw_batchbuffer_reset(batch);
78 brw_batchbuffer_free(struct brw_batchbuffer *batch)
81 batch->sws->bo_unmap(batch->buf);
85 bo_reference(&batch->buf, NULL);
91 _brw_batchbuffer_flush(struct brw_batchbuffer *batch,
95 GLuint used = batch->ptr - batch->map;
100 /* Post-swap throttling done by the state tracker.
103 if (BRW_DEBUG & DEBUG_BATCH)
104 debug_printf("%s:%d: Batchbuffer flush with %db used\n",
107 if (ALWAYS_EMIT_MI_FLUSH) {
108 *(GLuint *) (batch->ptr) = MI_FLUSH | BRW_FLUSH_STATE_CACHE;
110 used = batch->ptr - batch->map;
113 /* Round batchbuffer usage to 2 DWORDs.
115 if ((used & 4) == 0) {
116 *(GLuint *) (batch->ptr) = 0; /* noop */
118 used = batch->ptr - batch->map;
121 /* Mark the end of the buffer.
123 *(GLuint *) (batch->ptr) = MI_BATCH_BUFFER_END;
125 used = batch->ptr - batch->map;
127 batch->sws->bo_flush_range(batch->buf, 0, used);
128 batch->sws->bo_unmap(batch->buf);
132 batch->sws->bo_exec(batch->buf, used );
134 if (BRW_DEBUG & DEBUG_SYNC) {
135 /* Abuse map/unmap to achieve wait-for-fence.
137 * XXX: hide this inside the winsys and export a fence
140 debug_printf("waiting for idle\n");
141 batch->sws->bo_wait_idle(batch->buf);
146 brw_batchbuffer_reset(batch);
150 /* The OUT_RELOC() macro ends up here, generating a relocation within
154 brw_batchbuffer_emit_reloc(struct brw_batchbuffer *batch,
155 struct brw_winsys_buffer *buffer,
156 enum brw_buffer_usage usage,
161 if (batch->ptr - batch->map > batch->buf->size) {
162 debug_printf("bad relocation ptr %p map %p offset %li size %i\n",
163 batch->ptr, batch->map, (long) (batch->ptr - batch->map), batch->buf->size);
165 return PIPE_ERROR_OUT_OF_MEMORY;
168 ret = batch->sws->bo_emit_reloc(batch->buf,
171 batch->ptr - batch->map,
176 /* bo_emit_reloc was resposible for writing a zero into the
177 * batchbuffer if necessary. Just need to update our pointer.
185 brw_batchbuffer_data(struct brw_batchbuffer *batch,
186 const void *data, GLuint bytes,
187 enum cliprect_mode cliprect_mode)
191 assert((bytes & 3) == 0);
193 ret = brw_batchbuffer_require_space(batch, bytes);
197 memcpy(batch->ptr, data, bytes);