u_inlines.h revision b6b6b8f8bb56c2e010b9e126797b4e54c6875eb3
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/* Return true if the surfaces are equal. */
164static INLINE boolean
165pipe_surface_equal(struct pipe_surface *s1, struct pipe_surface *s2)
166{
167   return s1->texture == s2->texture &&
168          s1->format == s2->format &&
169          (s1->texture->target != PIPE_BUFFER ||
170           (s1->u.buf.first_element == s2->u.buf.first_element &&
171            s1->u.buf.last_element == s2->u.buf.last_element)) &&
172          (s1->texture->target == PIPE_BUFFER ||
173           (s1->u.tex.level == s2->u.tex.level &&
174            s1->u.tex.first_layer == s2->u.tex.first_layer &&
175            s1->u.tex.last_layer == s2->u.tex.last_layer));
176}
177
178/*
179 * Convenience wrappers for screen buffer functions.
180 */
181
182static INLINE struct pipe_resource *
183pipe_buffer_create( struct pipe_screen *screen,
184		    unsigned bind,
185		    unsigned size )
186{
187   struct pipe_resource buffer;
188   memset(&buffer, 0, sizeof buffer);
189   buffer.target = PIPE_BUFFER;
190   buffer.format = PIPE_FORMAT_R8_UNORM; /* want TYPELESS or similar */
191   buffer.bind = bind;
192   buffer.usage = PIPE_USAGE_DEFAULT;
193   buffer.flags = 0;
194   buffer.width0 = size;
195   buffer.height0 = 1;
196   buffer.depth0 = 1;
197   buffer.array_size = 1;
198   return screen->resource_create(screen, &buffer);
199}
200
201
202static INLINE struct pipe_resource *
203pipe_user_buffer_create( struct pipe_screen *screen, void *ptr, unsigned size,
204			 unsigned usage )
205{
206   return screen->user_buffer_create(screen, ptr, size, usage);
207}
208
209static INLINE void *
210pipe_buffer_map_range(struct pipe_context *pipe,
211		      struct pipe_resource *buffer,
212		      unsigned offset,
213		      unsigned length,
214		      unsigned usage,
215		      struct pipe_transfer **transfer)
216{
217   struct pipe_box box;
218   void *map;
219
220   assert(offset < buffer->width0);
221   assert(offset + length <= buffer->width0);
222   assert(length);
223
224   u_box_1d(offset, length, &box);
225
226   *transfer = pipe->get_transfer( pipe,
227                                   buffer,
228                                   0,
229                                   usage,
230                                   &box);
231
232   if (*transfer == NULL)
233      return NULL;
234
235   map = pipe->transfer_map( pipe, *transfer );
236   if (map == NULL) {
237      pipe->transfer_destroy( pipe, *transfer );
238      *transfer = NULL;
239      return NULL;
240   }
241
242   /* Match old screen->buffer_map_range() behaviour, return pointer
243    * to where the beginning of the buffer would be:
244    */
245   return (void *)((char *)map - offset);
246}
247
248
249static INLINE void *
250pipe_buffer_map(struct pipe_context *pipe,
251                struct pipe_resource *buffer,
252                unsigned usage,
253                struct pipe_transfer **transfer)
254{
255   return pipe_buffer_map_range(pipe, buffer, 0, buffer->width0, usage, transfer);
256}
257
258
259static INLINE void
260pipe_buffer_unmap(struct pipe_context *pipe,
261                  struct pipe_transfer *transfer)
262{
263   if (transfer) {
264      pipe->transfer_unmap(pipe, transfer);
265      pipe->transfer_destroy(pipe, transfer);
266   }
267}
268
269static INLINE void
270pipe_buffer_flush_mapped_range(struct pipe_context *pipe,
271                               struct pipe_transfer *transfer,
272                               unsigned offset,
273                               unsigned length)
274{
275   struct pipe_box box;
276   int transfer_offset;
277
278   assert(length);
279   assert(transfer->box.x <= offset);
280   assert(offset + length <= transfer->box.x + transfer->box.width);
281
282   /* Match old screen->buffer_flush_mapped_range() behaviour, where
283    * offset parameter is relative to the start of the buffer, not the
284    * mapped range.
285    */
286   transfer_offset = offset - transfer->box.x;
287
288   u_box_1d(transfer_offset, length, &box);
289
290   pipe->transfer_flush_region(pipe, transfer, &box);
291}
292
293static INLINE void
294pipe_buffer_write(struct pipe_context *pipe,
295                  struct pipe_resource *buf,
296                  unsigned offset,
297                  unsigned size,
298                  const void *data)
299{
300   struct pipe_box box;
301
302   u_box_1d(offset, size, &box);
303
304   pipe->transfer_inline_write( pipe,
305                                buf,
306                                0,
307                                PIPE_TRANSFER_WRITE,
308                                &box,
309                                data,
310                                size,
311                                0);
312}
313
314/**
315 * Special case for writing non-overlapping ranges.
316 *
317 * We can avoid GPU/CPU synchronization when writing range that has never
318 * been written before.
319 */
320static INLINE void
321pipe_buffer_write_nooverlap(struct pipe_context *pipe,
322                            struct pipe_resource *buf,
323                            unsigned offset, unsigned size,
324                            const void *data)
325{
326   struct pipe_box box;
327
328   u_box_1d(offset, size, &box);
329
330   pipe->transfer_inline_write(pipe,
331                               buf,
332                               0,
333                               (PIPE_TRANSFER_WRITE |
334                                PIPE_TRANSFER_NOOVERWRITE),
335                               &box,
336                               data,
337                               0, 0);
338}
339
340static INLINE void
341pipe_buffer_read(struct pipe_context *pipe,
342                 struct pipe_resource *buf,
343                 unsigned offset,
344                 unsigned size,
345                 void *data)
346{
347   struct pipe_transfer *src_transfer;
348   ubyte *map;
349
350   map = (ubyte *) pipe_buffer_map_range(pipe,
351					 buf,
352					 offset, size,
353					 PIPE_TRANSFER_READ,
354					 &src_transfer);
355
356   if (map)
357      memcpy(data, map + offset, size);
358
359   pipe_buffer_unmap(pipe, src_transfer);
360}
361
362static INLINE struct pipe_transfer *
363pipe_get_transfer( struct pipe_context *context,
364                   struct pipe_resource *resource,
365                   unsigned level, unsigned layer,
366                   enum pipe_transfer_usage usage,
367                   unsigned x, unsigned y,
368                   unsigned w, unsigned h)
369{
370   struct pipe_box box;
371   u_box_2d_zslice( x, y, layer, w, h, &box );
372   return context->get_transfer( context,
373                                 resource,
374                                 level,
375                                 usage,
376                                 &box );
377}
378
379static INLINE void *
380pipe_transfer_map( struct pipe_context *context,
381                   struct pipe_transfer *transfer )
382{
383   return context->transfer_map( context, transfer );
384}
385
386static INLINE void
387pipe_transfer_unmap( struct pipe_context *context,
388                     struct pipe_transfer *transfer )
389{
390   context->transfer_unmap( context, transfer );
391}
392
393
394static INLINE void
395pipe_transfer_destroy( struct pipe_context *context,
396                       struct pipe_transfer *transfer )
397{
398   context->transfer_destroy(context, transfer);
399}
400
401
402static INLINE boolean util_get_offset(
403   const struct pipe_rasterizer_state *templ,
404   unsigned fill_mode)
405{
406   switch(fill_mode) {
407   case PIPE_POLYGON_MODE_POINT:
408      return templ->offset_point;
409   case PIPE_POLYGON_MODE_LINE:
410      return templ->offset_line;
411   case PIPE_POLYGON_MODE_FILL:
412      return templ->offset_tri;
413   default:
414      assert(0);
415      return FALSE;
416   }
417}
418
419/**
420 * This function is used to copy an array of pipe_vertex_buffer structures,
421 * while properly referencing the pipe_vertex_buffer::buffer member.
422 *
423 * \sa util_copy_framebuffer_state
424 */
425static INLINE void util_copy_vertex_buffers(struct pipe_vertex_buffer *dst,
426                                            unsigned *dst_count,
427                                            const struct pipe_vertex_buffer *src,
428                                            unsigned src_count)
429{
430   unsigned i;
431
432   /* Reference the buffers of 'src' in 'dst'. */
433   for (i = 0; i < src_count; i++) {
434      pipe_resource_reference(&dst[i].buffer, src[i].buffer);
435   }
436   /* Unreference the rest of the buffers in 'dst'. */
437   for (; i < *dst_count; i++) {
438      pipe_resource_reference(&dst[i].buffer, NULL);
439   }
440
441   /* Update the size of 'dst' and copy over the other members
442    * of pipe_vertex_buffer. */
443   *dst_count = src_count;
444   memcpy(dst, src, src_count * sizeof(struct pipe_vertex_buffer));
445}
446
447#ifdef __cplusplus
448}
449#endif
450
451#endif /* U_INLINES_H */
452