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_buffer_objects.h"
31 #include "intel_decode.h"
32 #include "intel_reg.h"
33 #include "intel_bufmgr.h"
34 #include "intel_buffers.h"
37 intel_batchbuffer_reset(struct intel_context
*intel
)
39 if (intel
->batch
.bo
!= NULL
) {
40 drm_intel_bo_unreference(intel
->batch
.bo
);
41 intel
->batch
.bo
= NULL
;
44 intel
->batch
.bo
= drm_intel_bo_alloc(intel
->bufmgr
, "batchbuffer",
45 intel
->maxBatchSize
, 4096);
47 intel
->batch
.reserved_space
= BATCH_RESERVED
;
48 intel
->batch
.state_batch_offset
= intel
->batch
.bo
->size
;
49 intel
->batch
.used
= 0;
53 intel_batchbuffer_free(struct intel_context
*intel
)
55 drm_intel_bo_unreference(intel
->batch
.bo
);
59 /* TODO: Push this whole function into bufmgr.
62 do_flush_locked(struct intel_context
*intel
)
64 struct intel_batchbuffer
*batch
= &intel
->batch
;
67 if (!intel
->intelScreen
->no_hw
) {
70 if (intel
->gen
< 6 || !batch
->is_blit
) {
71 ring
= I915_EXEC_RENDER
;
76 ret
= drm_intel_bo_subdata(batch
->bo
, 0, 4*batch
->used
, batch
->map
);
77 if (ret
== 0 && batch
->state_batch_offset
!= batch
->bo
->size
) {
78 ret
= drm_intel_bo_subdata(batch
->bo
,
79 batch
->state_batch_offset
,
80 batch
->bo
->size
- batch
->state_batch_offset
,
81 (char *)batch
->map
+ batch
->state_batch_offset
);
85 ret
= drm_intel_bo_mrb_exec(batch
->bo
, 4*batch
->used
, NULL
, 0, 0, ring
);
88 if (unlikely(INTEL_DEBUG
& DEBUG_BATCH
)) {
89 intel_decode(batch
->map
, batch
->used
,
91 intel
->intelScreen
->deviceID
, GL_TRUE
);
93 if (intel
->vtbl
.debug_batch
!= NULL
)
94 intel
->vtbl
.debug_batch(intel
);
100 intel
->vtbl
.new_batch(intel
);
104 _intel_batchbuffer_flush(struct intel_context
*intel
,
105 const char *file
, int line
)
107 if (intel
->batch
.used
== 0)
110 if (intel
->first_post_swapbuffers_batch
== NULL
) {
111 intel
->first_post_swapbuffers_batch
= intel
->batch
.bo
;
112 drm_intel_bo_reference(intel
->first_post_swapbuffers_batch
);
115 if (unlikely(INTEL_DEBUG
& DEBUG_BATCH
))
116 fprintf(stderr
, "%s:%d: Batchbuffer flush with %db used\n", file
, line
,
117 4*intel
->batch
.used
);
119 intel
->batch
.reserved_space
= 0;
121 if (intel
->always_flush_cache
) {
122 intel_batchbuffer_emit_mi_flush(intel
);
125 /* Mark the end of the buffer. */
126 intel_batchbuffer_emit_dword(intel
, MI_BATCH_BUFFER_END
);
127 if (intel
->batch
.used
& 1) {
128 /* Round batchbuffer usage to 2 DWORDs. */
129 intel_batchbuffer_emit_dword(intel
, MI_NOOP
);
132 if (intel
->vtbl
.finish_batch
)
133 intel
->vtbl
.finish_batch(intel
);
135 intel_upload_finish(intel
);
137 /* Check that we didn't just wrap our batchbuffer at a bad time. */
138 assert(!intel
->no_batch_wrap
);
140 do_flush_locked(intel
);
142 if (unlikely(INTEL_DEBUG
& DEBUG_SYNC
)) {
143 fprintf(stderr
, "waiting for idle\n");
144 drm_intel_bo_wait_rendering(intel
->batch
.bo
);
149 intel_batchbuffer_reset(intel
);
153 /* This is the only way buffers get added to the validate list.
156 intel_batchbuffer_emit_reloc(struct intel_context
*intel
,
157 drm_intel_bo
*buffer
,
158 uint32_t read_domains
, uint32_t write_domain
,
163 assert(delta
< buffer
->size
);
165 ret
= drm_intel_bo_emit_reloc(intel
->batch
.bo
, 4*intel
->batch
.used
,
167 read_domains
, write_domain
);
171 * Using the old buffer offset, write in what the right data would be, in case
172 * the buffer doesn't move and we can short-circuit the relocation processing
175 intel_batchbuffer_emit_dword(intel
, buffer
->offset
+ delta
);
181 intel_batchbuffer_emit_reloc_fenced(struct intel_context
*intel
,
182 drm_intel_bo
*buffer
,
183 uint32_t read_domains
,
184 uint32_t write_domain
,
189 assert(delta
< buffer
->size
);
191 ret
= drm_intel_bo_emit_reloc_fence(intel
->batch
.bo
, 4*intel
->batch
.used
,
193 read_domains
, write_domain
);
197 * Using the old buffer offset, write in what the right data would
198 * be, in case the buffer doesn't move and we can short-circuit the
199 * relocation processing in the kernel
201 intel_batchbuffer_emit_dword(intel
, buffer
->offset
+ delta
);
207 intel_batchbuffer_data(struct intel_context
*intel
,
208 const void *data
, GLuint bytes
, bool is_blit
)
210 assert((bytes
& 3) == 0);
211 intel_batchbuffer_require_space(intel
, bytes
, is_blit
);
212 __memcpy(intel
->batch
.map
+ intel
->batch
.used
, data
, bytes
);
213 intel
->batch
.used
+= bytes
>> 2;
216 /* Emit a pipelined flush to either flush render and texture cache for
217 * reading from a FBO-drawn texture, or flush so that frontbuffer
218 * render appears on the screen in DRI1.
220 * This is also used for the always_flush_cache driconf debug option.
223 intel_batchbuffer_emit_mi_flush(struct intel_context
*intel
)
225 if (intel
->gen
>= 6) {
226 if (intel
->batch
.is_blit
) {
228 OUT_BATCH(MI_FLUSH_DW
);
235 /* XXX workaround: issue any post sync != 0 before write
238 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
239 OUT_BATCH(PIPE_CONTROL_WRITE_IMMEDIATE
);
240 OUT_BATCH(0); /* write address */
241 OUT_BATCH(0); /* write data */
243 OUT_BATCH(_3DSTATE_PIPE_CONTROL
);
244 OUT_BATCH(PIPE_CONTROL_INSTRUCTION_FLUSH
|
245 PIPE_CONTROL_WRITE_FLUSH
|
246 PIPE_CONTROL_DEPTH_CACHE_FLUSH
|
247 PIPE_CONTROL_NO_WRITE
);
248 OUT_BATCH(0); /* write address */
249 OUT_BATCH(0); /* write data */
252 } else if (intel
->gen
>= 4) {
254 OUT_BATCH(_3DSTATE_PIPE_CONTROL
|
255 PIPE_CONTROL_WRITE_FLUSH
|
256 PIPE_CONTROL_NO_WRITE
);
257 OUT_BATCH(0); /* write address */
258 OUT_BATCH(0); /* write data */
259 OUT_BATCH(0); /* write data */