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 "brw_batchbuffer.h"
29 #include "brw_decode.h"
31 #include "brw_winsys.h"
35 brw_batchbuffer_reset(struct brw_batchbuffer
*batch
)
37 struct intel_context
*intel
= batch
->intel
;
39 if (batch
->buf
!= NULL
) {
40 brw
->sws
->bo_unreference(batch
->buf
);
44 if (!batch
->buffer
&& intel
->ttm
== GL_TRUE
)
45 batch
->buffer
= malloc (intel
->maxBatchSize
);
47 batch
->buf
= batch
->sws
->bo_alloc(batch
->sws
,
48 BRW_BUFFER_TYPE_BATCH
,
49 intel
->maxBatchSize
, 4096);
51 batch
->map
= batch
->buffer
;
53 batch
->sws
->bo_map(batch
->buf
, GL_TRUE
);
54 batch
->map
= batch
->buf
->virtual;
56 batch
->size
= intel
->maxBatchSize
;
57 batch
->ptr
= batch
->map
;
58 batch
->dirty_state
= ~0;
59 batch
->cliprect_mode
= IGNORE_CLIPRECTS
;
62 struct brw_batchbuffer
*
63 brw_batchbuffer_alloc(struct brw_winsys_screen
*sws
)
65 struct brw_batchbuffer
*batch
= CALLOC_STRUCT(brw_batchbuffer
);
68 brw_batchbuffer_reset(batch
);
74 brw_batchbuffer_free(struct brw_batchbuffer
*batch
)
77 dri_bo_unmap(batch
->buf
);
81 brw
->sws
->bo_unreference(batch
->buf
);
88 _brw_batchbuffer_flush(struct brw_batchbuffer
*batch
, const char *file
,
91 struct intel_context
*intel
= batch
->intel
;
92 GLuint used
= batch
->ptr
- batch
->map
;
97 if (intel
->first_post_swapbuffers_batch
== NULL
) {
98 intel
->first_post_swapbuffers_batch
= intel
->batch
->buf
;
99 batch
->sws
->bo_reference(intel
->first_post_swapbuffers_batch
);
102 if (intel
->first_post_swapbuffers_batch
== NULL
) {
103 intel
->first_post_swapbuffers_batch
= intel
->batch
->buf
;
104 batch
->sws
->bo_reference(intel
->first_post_swapbuffers_batch
);
108 if (INTEL_DEBUG
& DEBUG_BATCH
)
109 fprintf(stderr
, "%s:%d: Batchbuffer flush with %db used\n", file
, line
,
112 /* Emit a flush if the bufmgr doesn't do it for us. */
113 if (intel
->always_flush_cache
|| !intel
->ttm
) {
114 *(GLuint
*) (batch
->ptr
) = intel
->vtbl
.flush_cmd();
116 used
= batch
->ptr
- batch
->map
;
119 /* Round batchbuffer usage to 2 DWORDs. */
121 if ((used
& 4) == 0) {
122 *(GLuint
*) (batch
->ptr
) = 0; /* noop */
124 used
= batch
->ptr
- batch
->map
;
127 /* Mark the end of the buffer. */
128 *(GLuint
*) (batch
->ptr
) = MI_BATCH_BUFFER_END
; /* noop */
130 used
= batch
->ptr
- batch
->map
;
132 batch
->sws
->bo_unmap(batch
->buf
);
137 batch
->sws
->bo_exec(batch
->buf
, used
, NULL
, 0, 0 );
139 if (INTEL_DEBUG
& DEBUG_BATCH
) {
140 dri_bo_map(batch
->buf
, GL_FALSE
);
141 intel_decode(batch
->buf
->virtual, used
/ 4, batch
->buf
->offset
,
142 brw
->brw_screen
->pci_id
);
143 dri_bo_unmap(batch
->buf
);
146 if (INTEL_DEBUG
& DEBUG_SYNC
) {
147 fprintf(stderr
, "waiting for idle\n");
148 dri_bo_map(batch
->buf
, GL_TRUE
);
149 dri_bo_unmap(batch
->buf
);
154 brw_batchbuffer_reset(batch
);
158 /* This is the only way buffers get added to the validate list.
161 brw_batchbuffer_emit_reloc(struct brw_batchbuffer
*batch
,
162 struct brw_winsys_buffer
*buffer
,
163 uint32_t read_domains
, uint32_t write_domain
,
168 if (batch
->ptr
- batch
->map
> batch
->buf
->size
)
169 _mesa_printf ("bad relocation ptr %p map %p offset %d size %d\n",
170 batch
->ptr
, batch
->map
, batch
->ptr
- batch
->map
, batch
->buf
->size
);
172 ret
= batch
->sws
->bo_emit_reloc(batch
->buf
,
176 batch
->ptr
- batch
->map
,
180 * Using the old buffer offset, write in what the right data would be, in case
181 * the buffer doesn't move and we can short-circuit the relocation processing
184 brw_batchbuffer_emit_dword (batch
, buffer
->offset
+ delta
);
190 brw_batchbuffer_data(struct brw_batchbuffer
*batch
,
191 const void *data
, GLuint bytes
,
192 enum cliprect_mode cliprect_mode
)
194 assert((bytes
& 3) == 0);
195 brw_batchbuffer_require_space(batch
, bytes
);
196 __memcpy(batch
->ptr
, data
, bytes
);