Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / gallium / drivers / i915simple / i915_batch.h
1 /**************************************************************************
2 *
3 * Copyright 2007 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 #ifndef I915_BATCH_H
29 #define I915_BATCH_H
30
31 #include "i915_winsys.h"
32
33 struct i915_batchbuffer
34 {
35 struct pipe_buffer *buffer;
36 struct i915_winsys *winsys;
37
38 unsigned char *map;
39 unsigned char *ptr;
40
41 size_t size;
42 size_t actual_size;
43
44 size_t relocs;
45 size_t max_relocs;
46 };
47
48 static INLINE boolean
49 i915_batchbuffer_check( struct i915_batchbuffer *batch,
50 size_t dwords,
51 size_t relocs )
52 {
53 /** TODO JB: Check relocs */
54 return dwords * 4 <= batch->size - (batch->ptr - batch->map);
55 }
56
57 static INLINE size_t
58 i915_batchbuffer_space( struct i915_batchbuffer *batch )
59 {
60 return batch->size - (batch->ptr - batch->map);
61 }
62
63 static INLINE void
64 i915_batchbuffer_dword( struct i915_batchbuffer *batch,
65 unsigned dword )
66 {
67 if (i915_batchbuffer_space(batch) < 4)
68 return;
69
70 *(unsigned *)batch->ptr = dword;
71 batch->ptr += 4;
72 }
73
74 static INLINE void
75 i915_batchbuffer_write( struct i915_batchbuffer *batch,
76 void *data,
77 size_t size )
78 {
79 if (i915_batchbuffer_space(batch) < size)
80 return;
81
82 memcpy(data, batch->ptr, size);
83 batch->ptr += size;
84 }
85
86 static INLINE void
87 i915_batchbuffer_reloc( struct i915_batchbuffer *batch,
88 struct pipe_buffer *buffer,
89 size_t flags,
90 size_t offset )
91 {
92 batch->winsys->batch_reloc( batch->winsys, buffer, flags, offset );
93 }
94
95 static INLINE void
96 i915_batchbuffer_flush( struct i915_batchbuffer *batch,
97 struct pipe_fence_handle **fence )
98 {
99 batch->winsys->batch_flush( batch->winsys, fence );
100 }
101
102 #define BEGIN_BATCH( dwords, relocs ) \
103 (i915_batchbuffer_check( i915->batch, dwords, relocs ))
104
105 #define OUT_BATCH( dword ) \
106 i915_batchbuffer_dword( i915->batch, dword )
107
108 #define OUT_RELOC( buf, flags, delta ) \
109 i915_batchbuffer_reloc( i915->batch, buf, flags, delta )
110
111 #define FLUSH_BATCH(fence) do { \
112 i915->winsys->batch_flush( i915->winsys, fence ); \
113 i915->hardware_dirty = ~0; \
114 } while (0)
115
116 #endif