Merge commit 'origin/perrtblend'
[mesa.git] / src / gallium / include / pipe / p_inlines.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 P_INLINES_H
29 #define P_INLINES_H
30
31 #include "p_context.h"
32 #include "p_defines.h"
33 #include "p_screen.h"
34
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40
41 /**
42 * Convenience wrappers for screen buffer functions.
43 */
44
45 static INLINE struct pipe_buffer *
46 pipe_buffer_create( struct pipe_screen *screen,
47 unsigned alignment, unsigned usage, unsigned size )
48 {
49 return screen->buffer_create(screen, alignment, usage, size);
50 }
51
52 static INLINE struct pipe_buffer *
53 pipe_user_buffer_create( struct pipe_screen *screen, void *ptr, unsigned size )
54 {
55 return screen->user_buffer_create(screen, ptr, size);
56 }
57
58 static INLINE void *
59 pipe_buffer_map(struct pipe_screen *screen,
60 struct pipe_buffer *buf,
61 unsigned usage)
62 {
63 if(screen->buffer_map_range) {
64 unsigned offset = 0;
65 unsigned length = buf->size;
66 return screen->buffer_map_range(screen, buf, offset, length, usage);
67 }
68 else
69 return screen->buffer_map(screen, buf, usage);
70 }
71
72 static INLINE void
73 pipe_buffer_unmap(struct pipe_screen *screen,
74 struct pipe_buffer *buf)
75 {
76 screen->buffer_unmap(screen, buf);
77 }
78
79 static INLINE void *
80 pipe_buffer_map_range(struct pipe_screen *screen,
81 struct pipe_buffer *buf,
82 unsigned offset,
83 unsigned length,
84 unsigned usage)
85 {
86 assert(offset < buf->size);
87 assert(offset + length <= buf->size);
88 assert(length);
89 if(screen->buffer_map_range)
90 return screen->buffer_map_range(screen, buf, offset, length, usage);
91 else
92 return screen->buffer_map(screen, buf, usage);
93 }
94
95 static INLINE void
96 pipe_buffer_flush_mapped_range(struct pipe_screen *screen,
97 struct pipe_buffer *buf,
98 unsigned offset,
99 unsigned length)
100 {
101 assert(offset < buf->size);
102 assert(offset + length <= buf->size);
103 assert(length);
104 if(screen->buffer_flush_mapped_range)
105 screen->buffer_flush_mapped_range(screen, buf, offset, length);
106 }
107
108 static INLINE void
109 pipe_buffer_write(struct pipe_screen *screen,
110 struct pipe_buffer *buf,
111 unsigned offset, unsigned size,
112 const void *data)
113 {
114 void *map;
115
116 assert(offset < buf->size);
117 assert(offset + size <= buf->size);
118 assert(size);
119
120 map = pipe_buffer_map_range(screen, buf, offset, size,
121 PIPE_BUFFER_USAGE_CPU_WRITE |
122 PIPE_BUFFER_USAGE_FLUSH_EXPLICIT |
123 PIPE_BUFFER_USAGE_DISCARD);
124 assert(map);
125 if(map) {
126 memcpy((uint8_t *)map + offset, data, size);
127 pipe_buffer_flush_mapped_range(screen, buf, offset, size);
128 pipe_buffer_unmap(screen, buf);
129 }
130 }
131
132 /**
133 * Special case for writing non-overlapping ranges.
134 *
135 * We can avoid GPU/CPU synchronization when writing range that has never
136 * been written before.
137 */
138 static INLINE void
139 pipe_buffer_write_nooverlap(struct pipe_screen *screen,
140 struct pipe_buffer *buf,
141 unsigned offset, unsigned size,
142 const void *data)
143 {
144 void *map;
145
146 assert(offset < buf->size);
147 assert(offset + size <= buf->size);
148 assert(size);
149
150 map = pipe_buffer_map_range(screen, buf, offset, size,
151 PIPE_BUFFER_USAGE_CPU_WRITE |
152 PIPE_BUFFER_USAGE_FLUSH_EXPLICIT |
153 PIPE_BUFFER_USAGE_DISCARD |
154 PIPE_BUFFER_USAGE_UNSYNCHRONIZED);
155 assert(map);
156 if(map) {
157 memcpy((uint8_t *)map + offset, data, size);
158 pipe_buffer_flush_mapped_range(screen, buf, offset, size);
159 pipe_buffer_unmap(screen, buf);
160 }
161 }
162
163 static INLINE void
164 pipe_buffer_read(struct pipe_screen *screen,
165 struct pipe_buffer *buf,
166 unsigned offset, unsigned size,
167 void *data)
168 {
169 void *map;
170
171 assert(offset < buf->size);
172 assert(offset + size <= buf->size);
173 assert(size);
174
175 map = pipe_buffer_map_range(screen, buf, offset, size, PIPE_BUFFER_USAGE_CPU_READ);
176 assert(map);
177 if(map) {
178 memcpy(data, (const uint8_t *)map + offset, size);
179 pipe_buffer_unmap(screen, buf);
180 }
181 }
182
183 static INLINE void *
184 pipe_transfer_map( struct pipe_transfer *transf )
185 {
186 struct pipe_screen *screen = transf->texture->screen;
187 return screen->transfer_map(screen, transf);
188 }
189
190 static INLINE void
191 pipe_transfer_unmap( struct pipe_transfer *transf )
192 {
193 struct pipe_screen *screen = transf->texture->screen;
194 screen->transfer_unmap(screen, transf);
195 }
196
197 static INLINE void
198 pipe_transfer_destroy( struct pipe_transfer *transf )
199 {
200 struct pipe_screen *screen = transf->texture->screen;
201 screen->tex_transfer_destroy(transf);
202 }
203
204 static INLINE unsigned
205 pipe_transfer_buffer_flags( struct pipe_transfer *transf )
206 {
207 switch (transf->usage & PIPE_TRANSFER_READ_WRITE) {
208 case PIPE_TRANSFER_READ_WRITE:
209 return PIPE_BUFFER_USAGE_CPU_READ | PIPE_BUFFER_USAGE_CPU_WRITE;
210 case PIPE_TRANSFER_READ:
211 return PIPE_BUFFER_USAGE_CPU_READ;
212 case PIPE_TRANSFER_WRITE:
213 return PIPE_BUFFER_USAGE_CPU_WRITE;
214 default:
215 debug_assert(0);
216 return 0;
217 }
218 }
219
220 #ifdef __cplusplus
221 }
222 #endif
223
224 #endif /* P_INLINES_H */