i965g: more work on compiling, particularly the brw_draw files
[mesa.git] / src / gallium / drivers / i965 / brw_batchbuffer.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 #include "brw_batchbuffer.h"
29 #include "brw_decode.h"
30 #include "brw_reg.h"
31 #include "brw_winsys.h"
32
33
34 void
35 brw_batchbuffer_reset(struct brw_batchbuffer *batch)
36 {
37 struct intel_context *intel = batch->intel;
38
39 if (batch->buf != NULL) {
40 brw->sws->bo_unreference(batch->buf);
41 batch->buf = NULL;
42 }
43
44 if (!batch->buffer && intel->ttm == GL_TRUE)
45 batch->buffer = malloc (intel->maxBatchSize);
46
47 batch->buf = batch->sws->bo_alloc(batch->sws,
48 BRW_BUFFER_TYPE_BATCH,
49 intel->maxBatchSize, 4096);
50 if (batch->buffer)
51 batch->map = batch->buffer;
52 else {
53 batch->sws->bo_map(batch->buf, GL_TRUE);
54 batch->map = batch->buf->virtual;
55 }
56 batch->size = intel->maxBatchSize;
57 batch->ptr = batch->map;
58 batch->dirty_state = ~0;
59 batch->cliprect_mode = IGNORE_CLIPRECTS;
60 }
61
62 struct brw_batchbuffer *
63 brw_batchbuffer_alloc(struct brw_winsys_screen *sws)
64 {
65 struct brw_batchbuffer *batch = CALLOC_STRUCT(brw_batchbuffer);
66
67 batch->sws = sws;
68 brw_batchbuffer_reset(batch);
69
70 return batch;
71 }
72
73 void
74 brw_batchbuffer_free(struct brw_batchbuffer *batch)
75 {
76 if (batch->map) {
77 dri_bo_unmap(batch->buf);
78 batch->map = NULL;
79 }
80
81 brw->sws->bo_unreference(batch->buf);
82 batch->buf = NULL;
83 FREE(batch);
84 }
85
86
87 void
88 _brw_batchbuffer_flush(struct brw_batchbuffer *batch, const char *file,
89 int line)
90 {
91 struct intel_context *intel = batch->intel;
92 GLuint used = batch->ptr - batch->map;
93
94 if (used == 0)
95 return;
96
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);
100 }
101
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);
105 }
106
107
108 if (INTEL_DEBUG & DEBUG_BATCH)
109 fprintf(stderr, "%s:%d: Batchbuffer flush with %db used\n", file, line,
110 used);
111
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();
115 batch->ptr += 4;
116 used = batch->ptr - batch->map;
117 }
118
119 /* Round batchbuffer usage to 2 DWORDs. */
120
121 if ((used & 4) == 0) {
122 *(GLuint *) (batch->ptr) = 0; /* noop */
123 batch->ptr += 4;
124 used = batch->ptr - batch->map;
125 }
126
127 /* Mark the end of the buffer. */
128 *(GLuint *) (batch->ptr) = MI_BATCH_BUFFER_END; /* noop */
129 batch->ptr += 4;
130 used = batch->ptr - batch->map;
131
132 batch->sws->bo_unmap(batch->buf);
133
134 batch->map = NULL;
135 batch->ptr = NULL;
136
137 batch->sws->bo_exec(batch->buf, used, NULL, 0, 0 );
138
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);
144 }
145
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);
150 }
151
152 /* Reset the buffer:
153 */
154 brw_batchbuffer_reset(batch);
155 }
156
157
158 /* This is the only way buffers get added to the validate list.
159 */
160 GLboolean
161 brw_batchbuffer_emit_reloc(struct brw_batchbuffer *batch,
162 struct brw_winsys_buffer *buffer,
163 uint32_t read_domains, uint32_t write_domain,
164 uint32_t delta)
165 {
166 int ret;
167
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);
171
172 ret = batch->sws->bo_emit_reloc(batch->buf,
173 read_domains,
174 write_domain,
175 delta,
176 batch->ptr - batch->map,
177 buffer);
178
179 /*
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
182 * in the kernel
183 */
184 brw_batchbuffer_emit_dword (batch, buffer->offset + delta);
185
186 return GL_TRUE;
187 }
188
189 void
190 brw_batchbuffer_data(struct brw_batchbuffer *batch,
191 const void *data, GLuint bytes,
192 enum cliprect_mode cliprect_mode)
193 {
194 assert((bytes & 3) == 0);
195 brw_batchbuffer_require_space(batch, bytes);
196 __memcpy(batch->ptr, data, bytes);
197 batch->ptr += bytes;
198 }