glsl2: Add and use new variable mode ir_var_temporary
[mesa/nouveau-pmpeg.git] / src / gallium / drivers / i965 / brw_batchbuffer.c
blob8b3f46f2c1632cf4b288d0e2ca4f6a1994464c8d
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:
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.
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 "util/u_memory.h"
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"
36 #define ALWAYS_EMIT_MI_FLUSH 1
38 enum pipe_error
39 brw_batchbuffer_reset(struct brw_batchbuffer *batch)
41 enum pipe_error ret;
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;
50 batch->size = BRW_BATCH_SIZE;
52 /* With map_range semantics, the winsys can decide whether to
53 * inject a malloc'ed bounce buffer instead of mapping directly.
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);
62 batch->ptr = batch->map;
63 return PIPE_OK;
66 struct brw_batchbuffer *
67 brw_batchbuffer_alloc(struct brw_winsys_screen *sws,
68 struct brw_chipset chipset)
70 struct brw_batchbuffer *batch = CALLOC_STRUCT(brw_batchbuffer);
72 batch->sws = sws;
73 batch->chipset = chipset;
74 brw_batchbuffer_reset(batch);
76 return batch;
79 void
80 brw_batchbuffer_free(struct brw_batchbuffer *batch)
82 if (batch->map) {
83 batch->sws->bo_unmap(batch->buf);
84 batch->map = NULL;
87 bo_reference(&batch->buf, NULL);
88 FREE(batch);
92 void
93 _brw_batchbuffer_flush(struct brw_batchbuffer *batch,
94 const char *file,
95 int line)
97 GLuint used = batch->ptr - batch->map;
99 if (used == 0)
100 return;
102 /* Post-swap throttling done by the state tracker.
105 if (BRW_DEBUG & DEBUG_BATCH)
106 debug_printf("%s:%d: Batchbuffer flush with %db used\n",
107 file, line, used);
109 if (ALWAYS_EMIT_MI_FLUSH) {
110 *(GLuint *) (batch->ptr) = MI_FLUSH | BRW_FLUSH_STATE_CACHE;
111 batch->ptr += 4;
112 used = batch->ptr - batch->map;
115 /* Round batchbuffer usage to 2 DWORDs.
117 if ((used & 4) == 0) {
118 *(GLuint *) (batch->ptr) = 0; /* noop */
119 batch->ptr += 4;
120 used = batch->ptr - batch->map;
123 /* Mark the end of the buffer.
125 *(GLuint *) (batch->ptr) = MI_BATCH_BUFFER_END;
126 batch->ptr += 4;
127 used = batch->ptr - batch->map;
129 batch->sws->bo_flush_range(batch->buf, 0, used);
130 batch->sws->bo_unmap(batch->buf);
131 batch->map = NULL;
132 batch->ptr = NULL;
134 batch->sws->bo_exec(batch->buf, used );
136 if (BRW_DEBUG & DEBUG_SYNC) {
137 /* Abuse map/unmap to achieve wait-for-fence.
139 * XXX: hide this inside the winsys and export a fence
140 * interface.
142 debug_printf("waiting for idle\n");
143 batch->sws->bo_wait_idle(batch->buf);
146 /* Reset the buffer:
148 brw_batchbuffer_reset(batch);
152 /* The OUT_RELOC() macro ends up here, generating a relocation within
153 * the batch buffer.
155 enum pipe_error
156 brw_batchbuffer_emit_reloc(struct brw_batchbuffer *batch,
157 struct brw_winsys_buffer *buffer,
158 enum brw_buffer_usage usage,
159 uint32_t delta)
161 int ret;
163 if (batch->ptr - batch->map > batch->buf->size) {
164 debug_printf("bad relocation ptr %p map %p offset %li size %i\n",
165 batch->ptr, batch->map, batch->ptr - batch->map, batch->buf->size);
167 return PIPE_ERROR_OUT_OF_MEMORY;
170 ret = batch->sws->bo_emit_reloc(batch->buf,
171 usage,
172 delta,
173 batch->ptr - batch->map,
174 buffer);
175 if (ret != 0)
176 return ret;
178 /* bo_emit_reloc was resposible for writing a zero into the
179 * batchbuffer if necessary. Just need to update our pointer.
181 batch->ptr += 4;
183 return 0;
186 enum pipe_error
187 brw_batchbuffer_data(struct brw_batchbuffer *batch,
188 const void *data, GLuint bytes,
189 enum cliprect_mode cliprect_mode)
191 enum pipe_error ret;
193 assert((bytes & 3) == 0);
195 ret = brw_batchbuffer_require_space(batch, bytes);
196 if (ret)
197 return ret;
199 memcpy(batch->ptr, data, bytes);
200 batch->ptr += bytes;
201 return 0;