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 "intel_context.h"
29 #include "intel_batchbuffer.h"
30 #include "intel_decode.h"
31 #include "intel_reg.h"
32 #include "intel_bufmgr.h"
33 #include "intel_buffers.h"
36 intel_batchbuffer_reset(struct intel_batchbuffer
*batch
)
38 struct intel_context
*intel
= batch
->intel
;
40 if (batch
->buf
!= NULL
) {
41 dri_bo_unreference(batch
->buf
);
46 batch
->buffer
= malloc (intel
->maxBatchSize
);
48 batch
->buf
= dri_bo_alloc(intel
->bufmgr
, "batchbuffer",
49 intel
->maxBatchSize
, 4096);
51 batch
->map
= batch
->buffer
;
53 dri_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;
61 struct intel_batchbuffer
*
62 intel_batchbuffer_alloc(struct intel_context
*intel
)
64 struct intel_batchbuffer
*batch
= calloc(sizeof(*batch
), 1);
67 intel_batchbuffer_reset(batch
);
73 intel_batchbuffer_free(struct intel_batchbuffer
*batch
)
79 dri_bo_unmap(batch
->buf
);
83 dri_bo_unreference(batch
->buf
);
90 /* TODO: Push this whole function into bufmgr.
93 do_flush_locked(struct intel_batchbuffer
*batch
, GLuint used
)
95 struct intel_context
*intel
= batch
->intel
;
97 int x_off
= 0, y_off
= 0;
100 dri_bo_subdata (batch
->buf
, 0, used
, batch
->buffer
);
102 dri_bo_unmap(batch
->buf
);
108 dri_bo_exec(batch
->buf
, used
, NULL
, 0, (x_off
& 0xffff) | (y_off
<< 16));
110 if (INTEL_DEBUG
& DEBUG_BATCH
) {
111 dri_bo_map(batch
->buf
, GL_FALSE
);
112 intel_decode(batch
->buf
->virtual, used
/ 4, batch
->buf
->offset
,
113 intel
->intelScreen
->deviceID
);
114 dri_bo_unmap(batch
->buf
);
116 if (intel
->vtbl
.debug_batch
!= NULL
)
117 intel
->vtbl
.debug_batch(intel
);
123 intel
->vtbl
.new_batch(intel
);
127 _intel_batchbuffer_flush(struct intel_batchbuffer
*batch
, const char *file
,
130 struct intel_context
*intel
= batch
->intel
;
131 GLuint used
= batch
->ptr
- batch
->map
;
133 if (!intel
->using_dri2_swapbuffers
&&
134 intel
->first_post_swapbuffers_batch
== NULL
) {
135 intel
->first_post_swapbuffers_batch
= intel
->batch
->buf
;
136 drm_intel_bo_reference(intel
->first_post_swapbuffers_batch
);
142 if (INTEL_DEBUG
& DEBUG_BATCH
)
143 fprintf(stderr
, "%s:%d: Batchbuffer flush with %db used\n", file
, line
,
146 batch
->reserved_space
= 0;
147 /* Emit a flush if the bufmgr doesn't do it for us. */
148 if (intel
->always_flush_cache
) {
149 intel_batchbuffer_emit_mi_flush(batch
);
150 used
= batch
->ptr
- batch
->map
;
153 /* Round batchbuffer usage to 2 DWORDs. */
155 if ((used
& 4) == 0) {
156 *(GLuint
*) (batch
->ptr
) = 0; /* noop */
158 used
= batch
->ptr
- batch
->map
;
161 /* Mark the end of the buffer. */
162 *(GLuint
*) (batch
->ptr
) = MI_BATCH_BUFFER_END
;
164 used
= batch
->ptr
- batch
->map
;
165 assert (used
<= batch
->buf
->size
);
167 /* Workaround for recursive batchbuffer flushing: If the window is
168 * moved, we can get into a case where we try to flush during a
169 * flush. What happens is that when we try to grab the lock for
170 * the first flush, we detect that the window moved which then
171 * causes another flush (from the intel_draw_buffer() call in
172 * intelUpdatePageFlipping()). To work around this we reset the
173 * batchbuffer tail pointer before trying to get the lock. This
174 * prevent the nested buffer flush, but a better fix would be to
175 * avoid that in the first place. */
176 batch
->ptr
= batch
->map
;
178 if (intel
->vtbl
.finish_batch
)
179 intel
->vtbl
.finish_batch(intel
);
181 /* Check that we didn't just wrap our batchbuffer at a bad time. */
182 assert(!intel
->no_batch_wrap
);
184 batch
->reserved_space
= BATCH_RESERVED
;
186 /* TODO: Just pass the relocation list and dma buffer up to the
189 do_flush_locked(batch
, used
);
191 if (INTEL_DEBUG
& DEBUG_SYNC
) {
192 fprintf(stderr
, "waiting for idle\n");
193 dri_bo_map(batch
->buf
, GL_TRUE
);
194 dri_bo_unmap(batch
->buf
);
199 intel_batchbuffer_reset(batch
);
203 /* This is the only way buffers get added to the validate list.
206 intel_batchbuffer_emit_reloc(struct intel_batchbuffer
*batch
,
208 uint32_t read_domains
, uint32_t write_domain
,
213 if (batch
->ptr
- batch
->map
> batch
->buf
->size
)
214 printf ("bad relocation ptr %p map %p offset %d size %lu\n",
215 batch
->ptr
, batch
->map
, batch
->ptr
- batch
->map
, batch
->buf
->size
);
216 ret
= dri_bo_emit_reloc(batch
->buf
, read_domains
, write_domain
,
217 delta
, batch
->ptr
- batch
->map
, buffer
);
220 * Using the old buffer offset, write in what the right data would be, in case
221 * the buffer doesn't move and we can short-circuit the relocation processing
224 intel_batchbuffer_emit_dword (batch
, buffer
->offset
+ delta
);
230 intel_batchbuffer_data(struct intel_batchbuffer
*batch
,
231 const void *data
, GLuint bytes
)
233 assert((bytes
& 3) == 0);
234 intel_batchbuffer_require_space(batch
, bytes
);
235 __memcpy(batch
->ptr
, data
, bytes
);
239 /* Emit a pipelined flush to either flush render and texture cache for
240 * reading from a FBO-drawn texture, or flush so that frontbuffer
241 * render appears on the screen in DRI1.
243 * This is also used for the always_flush_cache driconf debug option.
246 intel_batchbuffer_emit_mi_flush(struct intel_batchbuffer
*batch
)
248 struct intel_context
*intel
= batch
->intel
;
250 if (intel
->gen
>= 4) {
252 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
253 PIPE_CONTROL_INSTRUCTION_FLUSH
|
254 PIPE_CONTROL_WRITE_FLUSH
|
255 PIPE_CONTROL_NO_WRITE
);
256 OUT_BATCH(0); /* write address */
257 OUT_BATCH(0); /* write data */
258 OUT_BATCH(0); /* write data */