u_inlines.h revision 3c9aa3a7b12cfe178c14fea93cfb64a32db0b8ad
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 U_INLINES_H
29#define U_INLINES_H
30
31#include "pipe/p_context.h"
32#include "pipe/p_defines.h"
33#include "pipe/p_state.h"
34#include "pipe/p_screen.h"
35#include "util/u_debug.h"
36#include "util/u_debug_describe.h"
37#include "util/u_debug_refcnt.h"
38#include "util/u_atomic.h"
39#include "util/u_box.h"
40#include "util/u_math.h"
41
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47
48/*
49 * Reference counting helper functions.
50 */
51
52
53static INLINE void
54pipe_reference_init(struct pipe_reference *reference, unsigned count)
55{
56   p_atomic_set(&reference->count, count);
57}
58
59static INLINE boolean
60pipe_is_referenced(struct pipe_reference *reference)
61{
62   return p_atomic_read(&reference->count) != 0;
63}
64
65/**
66 * Update reference counting.
67 * The old thing pointed to, if any, will be unreferenced.
68 * Both 'ptr' and 'reference' may be NULL.
69 * \return TRUE if the object's refcount hits zero and should be destroyed.
70 */
71static INLINE boolean
72pipe_reference_described(struct pipe_reference *ptr,
73                         struct pipe_reference *reference,
74                         debug_reference_descriptor get_desc)
75{
76   boolean destroy = FALSE;
77
78   if(ptr != reference) {
79      /* bump the reference.count first */
80      if (reference) {
81         assert(pipe_is_referenced(reference));
82         p_atomic_inc(&reference->count);
83         debug_reference(reference, get_desc, 1);
84      }
85
86      if (ptr) {
87         assert(pipe_is_referenced(ptr));
88         if (p_atomic_dec_zero(&ptr->count)) {
89            destroy = TRUE;
90         }
91         debug_reference(ptr, get_desc, -1);
92      }
93   }
94
95   return destroy;
96}
97
98static INLINE boolean
99pipe_reference(struct pipe_reference *ptr, struct pipe_reference *reference)
100{
101   return pipe_reference_described(ptr, reference,
102                                   (debug_reference_descriptor)debug_describe_reference);
103}
104
105static INLINE void
106pipe_surface_reference(struct pipe_surface **ptr, struct pipe_surface *surf)
107{
108   struct pipe_surface *old_surf = *ptr;
109
110   if (pipe_reference_described(&(*ptr)->reference, &surf->reference,
111                                (debug_reference_descriptor)debug_describe_surface))
112      old_surf->context->surface_destroy(old_surf->context, old_surf);
113   *ptr = surf;
114}
115
116static INLINE void
117pipe_resource_reference(struct pipe_resource **ptr, struct pipe_resource *tex)
118{
119   struct pipe_resource *old_tex = *ptr;
120
121   if (pipe_reference_described(&(*ptr)->reference, &tex->reference,
122                                (debug_reference_descriptor)debug_describe_resource))
123      old_tex->screen->resource_destroy(old_tex->screen, old_tex);
124   *ptr = tex;
125}
126
127static INLINE void
128pipe_sampler_view_reference(struct pipe_sampler_view **ptr, struct pipe_sampler_view *view)
129{
130   struct pipe_sampler_view *old_view = *ptr;
131
132   if (pipe_reference_described(&(*ptr)->reference, &view->reference,
133                                (debug_reference_descriptor)debug_describe_sampler_view))
134      old_view->context->sampler_view_destroy(old_view->context, old_view);
135   *ptr = view;
136}
137
138static INLINE void
139pipe_surface_reset(struct pipe_context *ctx, struct pipe_surface* ps,
140                   struct pipe_resource *pt, unsigned level, unsigned layer,
141                   unsigned flags)
142{
143   pipe_resource_reference(&ps->texture, pt);
144   ps->format = pt->format;
145   ps->width = u_minify(pt->width0, level);
146   ps->height = u_minify(pt->height0, level);
147   ps->usage = flags;
148   ps->u.tex.level = level;
149   ps->u.tex.first_layer = ps->u.tex.last_layer = layer;
150   ps->context = ctx;
151}
152
153static INLINE void
154pipe_surface_init(struct pipe_context *ctx, struct pipe_surface* ps,
155                  struct pipe_resource *pt, unsigned level, unsigned layer,
156                  unsigned flags)
157{
158   ps->texture = 0;
159   pipe_reference_init(&ps->reference, 1);
160   pipe_surface_reset(ctx, ps, pt, level, layer, flags);
161}
162
163/*
164 * Convenience wrappers for screen buffer functions.
165 */
166
167static INLINE struct pipe_resource *
168pipe_buffer_create( struct pipe_screen *screen,
169		    unsigned bind,
170		    unsigned size )
171{
172   struct pipe_resource buffer;
173   memset(&buffer, 0, sizeof buffer);
174   buffer.target = PIPE_BUFFER;
175   buffer.format = PIPE_FORMAT_R8_UNORM; /* want TYPELESS or similar */
176   buffer.bind = bind;
177   buffer.usage = PIPE_USAGE_DEFAULT;
178   buffer.flags = 0;
179   buffer.width0 = size;
180   buffer.height0 = 1;
181   buffer.depth0 = 1;
182   buffer.array_size = 1;
183   return screen->resource_create(screen, &buffer);
184}
185
186
187static INLINE struct pipe_resource *
188pipe_user_buffer_create( struct pipe_screen *screen, void *ptr, unsigned size,
189			 unsigned usage )
190{
191   return screen->user_buffer_create(screen, ptr, size, usage);
192}
193
194static INLINE void *
195pipe_buffer_map_range(struct pipe_context *pipe,
196		      struct pipe_resource *buffer,
197		      unsigned offset,
198		      unsigned length,
199		      unsigned usage,
200		      struct pipe_transfer **transfer)
201{
202   struct pipe_box box;
203   void *map;
204
205   assert(offset < buffer->width0);
206   assert(offset + length <= buffer->width0);
207   assert(length);
208
209   u_box_1d(offset, length, &box);
210
211   *transfer = pipe->get_transfer( pipe,
212                                   buffer,
213                                   0,
214                                   usage,
215                                   &box);
216
217   if (*transfer == NULL)
218      return NULL;
219
220   map = pipe->transfer_map( pipe, *transfer );
221   if (map == NULL) {
222      pipe->transfer_destroy( pipe, *transfer );
223      return NULL;
224   }
225
226   /* Match old screen->buffer_map_range() behaviour, return pointer
227    * to where the beginning of the buffer would be:
228    */
229   return (void *)((char *)map - offset);
230}
231
232
233static INLINE void *
234pipe_buffer_map(struct pipe_context *pipe,
235                struct pipe_resource *buffer,
236                unsigned usage,
237                struct pipe_transfer **transfer)
238{
239   return pipe_buffer_map_range(pipe, buffer, 0, buffer->width0, usage, transfer);
240}
241
242
243static INLINE void
244pipe_buffer_unmap(struct pipe_context *pipe,
245                  struct pipe_transfer *transfer)
246{
247   if (transfer) {
248      pipe->transfer_unmap(pipe, transfer);
249      pipe->transfer_destroy(pipe, transfer);
250   }
251}
252
253static INLINE void
254pipe_buffer_flush_mapped_range(struct pipe_context *pipe,
255                               struct pipe_transfer *transfer,
256                               unsigned offset,
257                               unsigned length)
258{
259   struct pipe_box box;
260   int transfer_offset;
261
262   assert(length);
263   assert(transfer->box.x <= offset);
264   assert(offset + length <= transfer->box.x + transfer->box.width);
265
266   /* Match old screen->buffer_flush_mapped_range() behaviour, where
267    * offset parameter is relative to the start of the buffer, not the
268    * mapped range.
269    */
270   transfer_offset = offset - transfer->box.x;
271
272   u_box_1d(transfer_offset, length, &box);
273
274   pipe->transfer_flush_region(pipe, transfer, &box);
275}
276
277static INLINE void
278pipe_buffer_write(struct pipe_context *pipe,
279                  struct pipe_resource *buf,
280                  unsigned offset,
281                  unsigned size,
282                  const void *data)
283{
284   struct pipe_box box;
285
286   u_box_1d(offset, size, &box);
287
288   pipe->transfer_inline_write( pipe,
289                                buf,
290                                0,
291                                PIPE_TRANSFER_WRITE,
292                                &box,
293                                data,
294                                size,
295                                0);
296}
297
298/**
299 * Special case for writing non-overlapping ranges.
300 *
301 * We can avoid GPU/CPU synchronization when writing range that has never
302 * been written before.
303 */
304static INLINE void
305pipe_buffer_write_nooverlap(struct pipe_context *pipe,
306                            struct pipe_resource *buf,
307                            unsigned offset, unsigned size,
308                            const void *data)
309{
310   struct pipe_box box;
311
312   u_box_1d(offset, size, &box);
313
314   pipe->transfer_inline_write(pipe,
315                               buf,
316                               0,
317                               (PIPE_TRANSFER_WRITE |
318                                PIPE_TRANSFER_NOOVERWRITE),
319                               &box,
320                               data,
321                               0, 0);
322}
323
324static INLINE void
325pipe_buffer_read(struct pipe_context *pipe,
326                 struct pipe_resource *buf,
327                 unsigned offset,
328                 unsigned size,
329                 void *data)
330{
331   struct pipe_transfer *src_transfer;
332   ubyte *map;
333
334   map = (ubyte *) pipe_buffer_map_range(pipe,
335					 buf,
336					 offset, size,
337					 PIPE_TRANSFER_READ,
338					 &src_transfer);
339
340   if (map)
341      memcpy(data, map + offset, size);
342
343   pipe_buffer_unmap(pipe, src_transfer);
344}
345
346static INLINE struct pipe_transfer *
347pipe_get_transfer( struct pipe_context *context,
348                   struct pipe_resource *resource,
349                   unsigned level, unsigned layer,
350                   enum pipe_transfer_usage usage,
351                   unsigned x, unsigned y,
352                   unsigned w, unsigned h)
353{
354   struct pipe_box box;
355   u_box_2d_zslice( x, y, layer, w, h, &box );
356   return context->get_transfer( context,
357                                 resource,
358                                 level,
359                                 usage,
360                                 &box );
361}
362
363static INLINE void *
364pipe_transfer_map( struct pipe_context *context,
365                   struct pipe_transfer *transfer )
366{
367   return context->transfer_map( context, transfer );
368}
369
370static INLINE void
371pipe_transfer_unmap( struct pipe_context *context,
372                     struct pipe_transfer *transfer )
373{
374   context->transfer_unmap( context, transfer );
375}
376
377
378static INLINE void
379pipe_transfer_destroy( struct pipe_context *context,
380                       struct pipe_transfer *transfer )
381{
382   context->transfer_destroy(context, transfer);
383}
384
385
386static INLINE boolean util_get_offset(
387   const struct pipe_rasterizer_state *templ,
388   unsigned fill_mode)
389{
390   switch(fill_mode) {
391   case PIPE_POLYGON_MODE_POINT:
392      return templ->offset_point;
393   case PIPE_POLYGON_MODE_LINE:
394      return templ->offset_line;
395   case PIPE_POLYGON_MODE_FILL:
396      return templ->offset_tri;
397   default:
398      assert(0);
399      return FALSE;
400   }
401}
402
403static INLINE void util_copy_vertex_buffers(struct pipe_vertex_buffer *dst,
404                                            unsigned *dst_count,
405                                            const struct pipe_vertex_buffer *src,
406                                            unsigned src_count)
407{
408   unsigned i;
409
410   for (i = 0; i < src_count; i++) {
411      pipe_resource_reference(&dst[i].buffer, src[i].buffer);
412   }
413   for (; i < *dst_count; i++) {
414      pipe_resource_reference(&dst[i].buffer, NULL);
415   }
416
417   *dst_count = src_count;
418   memcpy(dst, src, src_count * sizeof(struct pipe_vertex_buffer));
419}
420
421#ifdef __cplusplus
422}
423#endif
424
425#endif /* U_INLINES_H */
426