i915: Implement and use the reworked batchbuffer code
[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 #include "i915_debug.h"
33
34 struct i915_batchbuffer
35 {
36 struct pipe_buffer *buffer;
37 struct i915_winsys *winsys;
38
39 unsigned char *map;
40 unsigned char *ptr;
41
42 size_t size;
43 size_t actual_size;
44
45 size_t relocs;
46 size_t max_relocs;
47 };
48
49 static INLINE boolean
50 i915_batchbuffer_check( struct i915_batchbuffer *batch,
51 size_t dwords,
52 size_t relocs )
53 {
54 /** TODO JB: Check relocs */
55 return dwords * 4 <= batch->size - (batch->ptr - batch->map);
56 }
57
58 static INLINE size_t
59 i915_batchbuffer_space( struct i915_batchbuffer *batch )
60 {
61 return batch->size - (batch->ptr - batch->map);
62 }
63
64 static INLINE void
65 i915_batchbuffer_dword( struct i915_batchbuffer *batch,
66 unsigned dword )
67 {
68 if (i915_batchbuffer_space(batch) < 4)
69 return;
70
71 *(unsigned *)batch->ptr = dword;
72 batch->ptr += 4;
73 }
74
75 static INLINE void
76 i915_batchbuffer_write( struct i915_batchbuffer *batch,
77 void *data,
78 size_t size )
79 {
80 if (i915_batchbuffer_space(batch) < size)
81 return;
82
83 memcpy(data, batch->ptr, size);
84 batch->ptr += size;
85 }
86
87 static INLINE void
88 i915_batchbuffer_reloc( struct i915_batchbuffer *batch,
89 struct pipe_buffer *buffer,
90 size_t flags,
91 size_t offset )
92 {
93 batch->winsys->batch_reloc( batch->winsys, buffer, flags, offset );
94 }
95
96 static INLINE void
97 i915_batchbuffer_flush( struct i915_batchbuffer *batch,
98 struct pipe_fence_handle **fence )
99 {
100 batch->winsys->batch_flush( batch->winsys, fence );
101 }
102
103 #define BATCH_LOCALS
104
105 #define BEGIN_BATCH( dwords, relocs ) \
106 (i915_batchbuffer_check( i915->batch, dwords, relocs ))
107
108 #define OUT_BATCH( dword ) \
109 i915_batchbuffer_dword( i915->batch, dword )
110
111 #define OUT_RELOC( buf, flags, delta ) \
112 i915_batchbuffer_reloc( i915->batch, buf, flags, delta )
113
114 #define ADVANCE_BATCH()
115
116 #define FLUSH_BATCH(fence) do { \
117 if (0) i915_dump_batchbuffer( i915 ); \
118 i915->winsys->batch_flush( i915->winsys, fence ); \
119 i915->hardware_dirty = ~0; \
120 } while (0)
121
122 #endif