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
);
45 batch
->buf
= dri_bo_alloc(intel
->bufmgr
, "batchbuffer",
46 intel
->maxBatchSize
, 4096);
47 batch
->map
= batch
->buffer
;
48 batch
->size
= intel
->maxBatchSize
;
49 batch
->ptr
= batch
->map
;
50 batch
->reserved_space
= BATCH_RESERVED
;
51 batch
->dirty_state
= ~0;
54 struct intel_batchbuffer
*
55 intel_batchbuffer_alloc(struct intel_context
*intel
)
57 struct intel_batchbuffer
*batch
= calloc(sizeof(*batch
), 1);
60 batch
->buffer
= malloc(intel
->maxBatchSize
);
61 intel_batchbuffer_reset(batch
);
67 intel_batchbuffer_free(struct intel_batchbuffer
*batch
)
70 dri_bo_unreference(batch
->buf
);
77 /* TODO: Push this whole function into bufmgr.
80 do_flush_locked(struct intel_batchbuffer
*batch
, GLuint used
)
82 struct intel_context
*intel
= batch
->intel
;
84 int x_off
= 0, y_off
= 0;
86 dri_bo_subdata (batch
->buf
, 0, used
, batch
->buffer
);
91 dri_bo_exec(batch
->buf
, used
, NULL
, 0, (x_off
& 0xffff) | (y_off
<< 16));
93 if (INTEL_DEBUG
& DEBUG_BATCH
) {
94 dri_bo_map(batch
->buf
, GL_FALSE
);
95 intel_decode(batch
->buf
->virtual, used
/ 4, batch
->buf
->offset
,
96 intel
->intelScreen
->deviceID
);
97 dri_bo_unmap(batch
->buf
);
99 if (intel
->vtbl
.debug_batch
!= NULL
)
100 intel
->vtbl
.debug_batch(intel
);
106 intel
->vtbl
.new_batch(intel
);
110 _intel_batchbuffer_flush(struct intel_batchbuffer
*batch
, const char *file
,
113 struct intel_context
*intel
= batch
->intel
;
114 GLuint used
= batch
->ptr
- batch
->map
;
116 if (intel
->first_post_swapbuffers_batch
== NULL
) {
117 intel
->first_post_swapbuffers_batch
= intel
->batch
->buf
;
118 drm_intel_bo_reference(intel
->first_post_swapbuffers_batch
);
124 if (INTEL_DEBUG
& DEBUG_BATCH
)
125 fprintf(stderr
, "%s:%d: Batchbuffer flush with %db used\n", file
, line
,
128 batch
->reserved_space
= 0;
129 /* Emit a flush if the bufmgr doesn't do it for us. */
130 if (intel
->always_flush_cache
) {
131 intel_batchbuffer_emit_mi_flush(batch
);
132 used
= batch
->ptr
- batch
->map
;
135 /* Round batchbuffer usage to 2 DWORDs. */
137 if ((used
& 4) == 0) {
138 *(GLuint
*) (batch
->ptr
) = 0; /* noop */
140 used
= batch
->ptr
- batch
->map
;
143 /* Mark the end of the buffer. */
144 *(GLuint
*) (batch
->ptr
) = MI_BATCH_BUFFER_END
;
146 used
= batch
->ptr
- batch
->map
;
147 assert (used
<= batch
->buf
->size
);
149 /* Workaround for recursive batchbuffer flushing: If the window is
150 * moved, we can get into a case where we try to flush during a
151 * flush. What happens is that when we try to grab the lock for
152 * the first flush, we detect that the window moved which then
153 * causes another flush (from the intel_draw_buffer() call in
154 * intelUpdatePageFlipping()). To work around this we reset the
155 * batchbuffer tail pointer before trying to get the lock. This
156 * prevent the nested buffer flush, but a better fix would be to
157 * avoid that in the first place. */
158 batch
->ptr
= batch
->map
;
160 if (intel
->vtbl
.finish_batch
)
161 intel
->vtbl
.finish_batch(intel
);
163 /* Check that we didn't just wrap our batchbuffer at a bad time. */
164 assert(!intel
->no_batch_wrap
);
166 /* TODO: Just pass the relocation list and dma buffer up to the
169 do_flush_locked(batch
, used
);
171 if (INTEL_DEBUG
& DEBUG_SYNC
) {
172 fprintf(stderr
, "waiting for idle\n");
173 dri_bo_map(batch
->buf
, GL_TRUE
);
174 dri_bo_unmap(batch
->buf
);
179 intel_batchbuffer_reset(batch
);
183 /* This is the only way buffers get added to the validate list.
186 intel_batchbuffer_emit_reloc(struct intel_batchbuffer
*batch
,
188 uint32_t read_domains
, uint32_t write_domain
,
193 assert(delta
< buffer
->size
);
195 if (batch
->ptr
- batch
->map
> batch
->buf
->size
)
196 printf ("bad relocation ptr %p map %p offset %d size %lu\n",
197 batch
->ptr
, batch
->map
, batch
->ptr
- batch
->map
, batch
->buf
->size
);
198 ret
= dri_bo_emit_reloc(batch
->buf
, read_domains
, write_domain
,
199 delta
, batch
->ptr
- batch
->map
, buffer
);
202 * Using the old buffer offset, write in what the right data would be, in case
203 * the buffer doesn't move and we can short-circuit the relocation processing
206 intel_batchbuffer_emit_dword (batch
, buffer
->offset
+ delta
);
212 intel_batchbuffer_emit_reloc_fenced(struct intel_batchbuffer
*batch
,
213 drm_intel_bo
*buffer
,
214 uint32_t read_domains
, uint32_t write_domain
,
219 assert(delta
< buffer
->size
);
221 if (batch
->ptr
- batch
->map
> batch
->buf
->size
)
222 printf ("bad relocation ptr %p map %p offset %d size %lu\n",
223 batch
->ptr
, batch
->map
, batch
->ptr
- batch
->map
, batch
->buf
->size
);
224 ret
= drm_intel_bo_emit_reloc_fence(batch
->buf
, batch
->ptr
- batch
->map
,
226 read_domains
, write_domain
);
229 * Using the old buffer offset, write in what the right data would
230 * be, in case the buffer doesn't move and we can short-circuit the
231 * relocation processing in the kernel
233 intel_batchbuffer_emit_dword (batch
, buffer
->offset
+ delta
);
239 intel_batchbuffer_data(struct intel_batchbuffer
*batch
,
240 const void *data
, GLuint bytes
)
242 assert((bytes
& 3) == 0);
243 intel_batchbuffer_require_space(batch
, bytes
);
244 __memcpy(batch
->ptr
, data
, bytes
);
248 /* Emit a pipelined flush to either flush render and texture cache for
249 * reading from a FBO-drawn texture, or flush so that frontbuffer
250 * render appears on the screen in DRI1.
252 * This is also used for the always_flush_cache driconf debug option.
255 intel_batchbuffer_emit_mi_flush(struct intel_batchbuffer
*batch
)
257 struct intel_context
*intel
= batch
->intel
;
259 if (intel
->gen
>= 6) {
261 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
262 OUT_BATCH(PIPE_CONTROL_INSTRUCTION_FLUSH
|
263 PIPE_CONTROL_WRITE_FLUSH
|
264 PIPE_CONTROL_NO_WRITE
);
265 OUT_BATCH(0); /* write address */
266 OUT_BATCH(0); /* write data */
268 } else if (intel
->gen
>= 4) {
270 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
271 PIPE_CONTROL_WRITE_FLUSH
|
272 PIPE_CONTROL_NO_WRITE
);
273 OUT_BATCH(0); /* write address */
274 OUT_BATCH(0); /* write data */
275 OUT_BATCH(0); /* write data */