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
,
68 struct brw_chipset chipset
)
70 struct brw_batchbuffer
*batch
= CALLOC_STRUCT(brw_batchbuffer
);
73 batch
->chipset
= chipset
;
74 brw_batchbuffer_reset(batch
);
80 brw_batchbuffer_free(struct brw_batchbuffer
*batch
)
83 batch
->sws
->bo_unmap(batch
->buf
);
87 bo_reference(&batch
->buf
, NULL
);
93 _brw_batchbuffer_flush(struct brw_batchbuffer
*batch
,
97 GLuint used
= batch
->ptr
- batch
->map
;
102 /* Post-swap throttling done by the state tracker.
105 if (BRW_DEBUG
& DEBUG_BATCH
)
106 debug_printf("%s:%d: Batchbuffer flush with %db used\n",
109 if (ALWAYS_EMIT_MI_FLUSH
) {
110 *(GLuint
*) (batch
->ptr
) = MI_FLUSH
| BRW_FLUSH_STATE_CACHE
;
112 used
= batch
->ptr
- batch
->map
;
115 /* Round batchbuffer usage to 2 DWORDs.
117 if ((used
& 4) == 0) {
118 *(GLuint
*) (batch
->ptr
) = 0; /* noop */
120 used
= batch
->ptr
- batch
->map
;
123 /* Mark the end of the buffer.
125 *(GLuint
*) (batch
->ptr
) = MI_BATCH_BUFFER_END
;
127 used
= batch
->ptr
- batch
->map
;
129 batch
->sws
->bo_flush_range(batch
->buf
, 0, used
);
130 batch
->sws
->bo_unmap(batch
->buf
);
134 batch
->sws
->bo_exec(batch
->buf
, used
);
136 if (BRW_DEBUG
& DEBUG_SYNC
) {
137 /* Abuse map/unmap to achieve wait-for-fence.
139 * XXX: hide this inside the winsys and export a fence
142 debug_printf("waiting for idle\n");
143 batch
->sws
->bo_wait_idle(batch
->buf
);
148 brw_batchbuffer_reset(batch
);
152 /* The OUT_RELOC() macro ends up here, generating a relocation within
156 brw_batchbuffer_emit_reloc(struct brw_batchbuffer
*batch
,
157 struct brw_winsys_buffer
*buffer
,
158 enum brw_buffer_usage usage
,
163 if (batch
->ptr
- batch
->map
> batch
->buf
->size
) {
164 debug_printf("bad relocation ptr %p map %p offset %li size %i\n",
165 batch
->ptr
, batch
->map
, batch
->ptr
- batch
->map
, batch
->buf
->size
);
167 return PIPE_ERROR_OUT_OF_MEMORY
;
170 ret
= batch
->sws
->bo_emit_reloc(batch
->buf
,
173 batch
->ptr
- batch
->map
,
178 /* bo_emit_reloc was resposible for writing a zero into the
179 * batchbuffer if necessary. Just need to update our pointer.
187 brw_batchbuffer_data(struct brw_batchbuffer
*batch
,
188 const void *data
, GLuint bytes
,
189 enum cliprect_mode cliprect_mode
)
193 assert((bytes
& 3) == 0);
195 ret
= brw_batchbuffer_require_space(batch
, bytes
);
199 memcpy(batch
->ptr
, data
, bytes
);