Merge branch 'master' into glsl-pp-rework-2
[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
67 /* XXX: Actually we should be using/detecting DISCARD
68 * instead of assuming that WRITE implies discard */
69 if((usage & PIPE_BUFFER_USAGE_CPU_WRITE) &&
70 !(usage & PIPE_BUFFER_USAGE_DISCARD))
71 usage |= PIPE_BUFFER_USAGE_CPU_READ;
72
73 return screen->buffer_map_range(screen, buf, offset, length, usage);
74 }
75 else
76 return screen->buffer_map(screen, buf, usage);
77 }
78
79 static INLINE void
80 pipe_buffer_unmap(struct pipe_screen *screen,
81 struct pipe_buffer *buf)
82 {
83 screen->buffer_unmap(screen, buf);
84 }
85
86 static INLINE void *
87 pipe_buffer_map_range(struct pipe_screen *screen,
88 struct pipe_buffer *buf,
89 unsigned offset,
90 unsigned length,
91 unsigned usage)
92 {
93 assert(offset < buf->size);
94 assert(offset + length <= buf->size);
95 assert(length);
96 if(screen->buffer_map_range)
97 return screen->buffer_map_range(screen, buf, offset, length, usage);
98 else
99 return screen->buffer_map(screen, buf, usage);
100 }
101
102 static INLINE void
103 pipe_buffer_flush_mapped_range(struct pipe_screen *screen,
104 struct pipe_buffer *buf,
105 unsigned offset,
106 unsigned length)
107 {
108 assert(offset < buf->size);
109 assert(offset + length <= buf->size);
110 assert(length);
111 if(screen->buffer_flush_mapped_range)
112 screen->buffer_flush_mapped_range(screen, buf, offset, length);
113 }
114
115 static INLINE void
116 pipe_buffer_write(struct pipe_screen *screen,
117 struct pipe_buffer *buf,
118 unsigned offset, unsigned size,
119 const void *data)
120 {
121 void *map;
122
123 assert(offset < buf->size);
124 assert(offset + size <= buf->size);
125 assert(size);
126
127 map = pipe_buffer_map_range(screen, buf, offset, size,
128 PIPE_BUFFER_USAGE_CPU_WRITE |
129 PIPE_BUFFER_USAGE_FLUSH_EXPLICIT);
130 assert(map);
131 if(map) {
132 memcpy((uint8_t *)map + offset, data, size);
133 pipe_buffer_flush_mapped_range(screen, buf, offset, size);
134 pipe_buffer_unmap(screen, buf);
135 }
136 }
137
138 static INLINE void
139 pipe_buffer_read(struct pipe_screen *screen,
140 struct pipe_buffer *buf,
141 unsigned offset, unsigned size,
142 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, PIPE_BUFFER_USAGE_CPU_READ);
151 assert(map);
152 if(map) {
153 memcpy(data, (const uint8_t *)map + offset, size);
154 pipe_buffer_unmap(screen, buf);
155 }
156 }
157
158 static INLINE void *
159 pipe_transfer_map( struct pipe_transfer *transf )
160 {
161 struct pipe_screen *screen = transf->texture->screen;
162 return screen->transfer_map(screen, transf);
163 }
164
165 static INLINE void
166 pipe_transfer_unmap( struct pipe_transfer *transf )
167 {
168 struct pipe_screen *screen = transf->texture->screen;
169 screen->transfer_unmap(screen, transf);
170 }
171
172 static INLINE void
173 pipe_transfer_destroy( struct pipe_transfer *transf )
174 {
175 struct pipe_screen *screen = transf->texture->screen;
176 screen->tex_transfer_destroy(transf);
177 }
178
179 static INLINE unsigned
180 pipe_transfer_buffer_flags( struct pipe_transfer *transf )
181 {
182 switch (transf->usage & PIPE_TRANSFER_READ_WRITE) {
183 case PIPE_TRANSFER_READ_WRITE:
184 return PIPE_BUFFER_USAGE_CPU_READ | PIPE_BUFFER_USAGE_CPU_WRITE;
185 case PIPE_TRANSFER_READ:
186 return PIPE_BUFFER_USAGE_CPU_READ;
187 case PIPE_TRANSFER_WRITE:
188 return PIPE_BUFFER_USAGE_CPU_WRITE;
189 default:
190 debug_assert(0);
191 return 0;
192 }
193 }
194
195 #ifdef __cplusplus
196 }
197 #endif
198
199 #endif /* P_INLINES_H */