gallium: split CAP_INSTANCE_DRAWING into INSTANCEID and INSTANCE_DIVISOR
[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 "util/u_memory.h"
29
30 #include "brw_batchbuffer.h"
31 #include "brw_reg.h"
32 #include "brw_winsys.h"
33 #include "brw_debug.h"
34 #include "brw_structs.h"
35
36 #define ALWAYS_EMIT_MI_FLUSH 1
37
38 enum pipe_error
39 brw_batchbuffer_reset(struct brw_batchbuffer *batch)
40 {
41 enum pipe_error ret;
42
43 ret = batch->sws->bo_alloc( batch->sws,
44 BRW_BUFFER_TYPE_BATCH,
45 BRW_BATCH_SIZE, 4096,
46 &batch->buf );
47 if (ret)
48 return ret;
49
50 batch->size = BRW_BATCH_SIZE;
51
52 /* With map_range semantics, the winsys can decide whether to
53 * inject a malloc'ed bounce buffer instead of mapping directly.
54 */
55 batch->map = batch->sws->bo_map(batch->buf,
56 BRW_DATA_BATCH_BUFFER,
57 0, batch->size,
58 GL_TRUE,
59 GL_TRUE,
60 GL_TRUE);
61
62 batch->ptr = batch->map;
63 return PIPE_OK;
64 }
65
66 struct brw_batchbuffer *
67 brw_batchbuffer_alloc(struct brw_winsys_screen *sws)
68 {
69 struct brw_batchbuffer *batch = CALLOC_STRUCT(brw_batchbuffer);
70
71 batch->sws = sws;
72 brw_batchbuffer_reset(batch);
73
74 return batch;
75 }
76
77 void
78 brw_batchbuffer_free(struct brw_batchbuffer *batch)
79 {
80 if (batch->map) {
81 batch->sws->bo_unmap(batch->buf);
82 batch->map = NULL;
83 }
84
85 bo_reference(&batch->buf, NULL);
86 FREE(batch);
87 }
88
89
90 void
91 _brw_batchbuffer_flush(struct brw_batchbuffer *batch,
92 const char *file,
93 int line)
94 {
95 GLuint used = batch->ptr - batch->map;
96
97 if (used == 0)
98 return;
99
100 /* Post-swap throttling done by the state tracker.
101 */
102
103 if (BRW_DEBUG & DEBUG_BATCH)
104 debug_printf("%s:%d: Batchbuffer flush with %db used\n",
105 file, line, used);
106
107 if (ALWAYS_EMIT_MI_FLUSH) {
108 *(GLuint *) (batch->ptr) = MI_FLUSH | BRW_FLUSH_STATE_CACHE;
109 batch->ptr += 4;
110 used = batch->ptr - batch->map;
111 }
112
113 /* Round batchbuffer usage to 2 DWORDs.
114 */
115 if ((used & 4) == 0) {
116 *(GLuint *) (batch->ptr) = 0; /* noop */
117 batch->ptr += 4;
118 used = batch->ptr - batch->map;
119 }
120
121 /* Mark the end of the buffer.
122 */
123 *(GLuint *) (batch->ptr) = MI_BATCH_BUFFER_END;
124 batch->ptr += 4;
125 used = batch->ptr - batch->map;
126
127 batch->sws->bo_flush_range(batch->buf, 0, used);
128 batch->sws->bo_unmap(batch->buf);
129 batch->map = NULL;
130 batch->ptr = NULL;
131
132 batch->sws->bo_exec(batch->buf, used );
133
134 if (BRW_DEBUG & DEBUG_SYNC) {
135 /* Abuse map/unmap to achieve wait-for-fence.
136 *
137 * XXX: hide this inside the winsys and export a fence
138 * interface.
139 */
140 debug_printf("waiting for idle\n");
141 batch->sws->bo_wait_idle(batch->buf);
142 }
143
144 /* Reset the buffer:
145 */
146 brw_batchbuffer_reset(batch);
147 }
148
149
150 /* The OUT_RELOC() macro ends up here, generating a relocation within
151 * the batch buffer.
152 */
153 enum pipe_error
154 brw_batchbuffer_emit_reloc(struct brw_batchbuffer *batch,
155 struct brw_winsys_buffer *buffer,
156 enum brw_buffer_usage usage,
157 uint32_t delta)
158 {
159 int ret;
160
161 if (batch->ptr - batch->map > batch->buf->size) {
162 debug_printf("bad relocation ptr %p map %p offset %li size %i\n",
163 batch->ptr, batch->map, (long) (batch->ptr - batch->map), batch->buf->size);
164
165 return PIPE_ERROR_OUT_OF_MEMORY;
166 }
167
168 ret = batch->sws->bo_emit_reloc(batch->buf,
169 usage,
170 delta,
171 batch->ptr - batch->map,
172 buffer);
173 if (ret != 0)
174 return ret;
175
176 /* bo_emit_reloc was resposible for writing a zero into the
177 * batchbuffer if necessary. Just need to update our pointer.
178 */
179 batch->ptr += 4;
180
181 return 0;
182 }
183
184 enum pipe_error
185 brw_batchbuffer_data(struct brw_batchbuffer *batch,
186 const void *data, GLuint bytes,
187 enum cliprect_mode cliprect_mode)
188 {
189 enum pipe_error ret;
190
191 assert((bytes & 3) == 0);
192
193 ret = brw_batchbuffer_require_space(batch, bytes);
194 if (ret)
195 return ret;
196
197 memcpy(batch->ptr, data, bytes);
198 batch->ptr += bytes;
199 return 0;
200 }