1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
49 * Reference counting helper functions.
54 pipe_reference_init(struct pipe_reference
*reference
, unsigned count
)
56 p_atomic_set(&reference
->count
, count
);
60 pipe_is_referenced(struct pipe_reference
*reference
)
62 return p_atomic_read(&reference
->count
) != 0;
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.
72 pipe_reference_described(struct pipe_reference
*ptr
,
73 struct pipe_reference
*reference
,
74 debug_reference_descriptor get_desc
)
76 boolean destroy
= FALSE
;
78 if(ptr
!= reference
) {
79 /* bump the reference.count first */
81 assert(pipe_is_referenced(reference
));
82 p_atomic_inc(&reference
->count
);
83 debug_reference(reference
, get_desc
, 1);
87 assert(pipe_is_referenced(ptr
));
88 if (p_atomic_dec_zero(&ptr
->count
)) {
91 debug_reference(ptr
, get_desc
, -1);
99 pipe_reference(struct pipe_reference
*ptr
, struct pipe_reference
*reference
)
101 return pipe_reference_described(ptr
, reference
,
102 (debug_reference_descriptor
)debug_describe_reference
);
106 pipe_surface_reference(struct pipe_surface
**ptr
, struct pipe_surface
*surf
)
108 struct pipe_surface
*old_surf
= *ptr
;
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
);
117 pipe_resource_reference(struct pipe_resource
**ptr
, struct pipe_resource
*tex
)
119 struct pipe_resource
*old_tex
= *ptr
;
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
);
128 pipe_sampler_view_reference(struct pipe_sampler_view
**ptr
, struct pipe_sampler_view
*view
)
130 struct pipe_sampler_view
*old_view
= *ptr
;
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
);
139 pipe_surface_reset(struct pipe_context
*ctx
, struct pipe_surface
* ps
,
140 struct pipe_resource
*pt
, unsigned level
, unsigned layer
,
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
);
148 ps
->u
.tex
.level
= level
;
149 ps
->u
.tex
.first_layer
= ps
->u
.tex
.last_layer
= layer
;
154 pipe_surface_init(struct pipe_context
*ctx
, struct pipe_surface
* ps
,
155 struct pipe_resource
*pt
, unsigned level
, unsigned layer
,
159 pipe_reference_init(&ps
->reference
, 1);
160 pipe_surface_reset(ctx
, ps
, pt
, level
, layer
, flags
);
164 * Convenience wrappers for screen buffer functions.
167 static INLINE
struct pipe_resource
*
168 pipe_buffer_create( struct pipe_screen
*screen
,
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 */
177 buffer
.usage
= PIPE_USAGE_DEFAULT
;
179 buffer
.width0
= size
;
182 buffer
.array_size
= 1;
183 return screen
->resource_create(screen
, &buffer
);
187 static INLINE
struct pipe_resource
*
188 pipe_user_buffer_create( struct pipe_screen
*screen
, void *ptr
, unsigned size
,
191 return screen
->user_buffer_create(screen
, ptr
, size
, usage
);
195 pipe_buffer_map_range(struct pipe_context
*pipe
,
196 struct pipe_resource
*buffer
,
200 struct pipe_transfer
**transfer
)
205 assert(offset
< buffer
->width0
);
206 assert(offset
+ length
<= buffer
->width0
);
209 u_box_1d(offset
, length
, &box
);
211 *transfer
= pipe
->get_transfer( pipe
,
217 if (*transfer
== NULL
)
220 map
= pipe
->transfer_map( pipe
, *transfer
);
222 pipe
->transfer_destroy( pipe
, *transfer
);
226 /* Match old screen->buffer_map_range() behaviour, return pointer
227 * to where the beginning of the buffer would be:
229 return (void *)((char *)map
- offset
);
234 pipe_buffer_map(struct pipe_context
*pipe
,
235 struct pipe_resource
*buffer
,
237 struct pipe_transfer
**transfer
)
239 return pipe_buffer_map_range(pipe
, buffer
, 0, buffer
->width0
, usage
, transfer
);
244 pipe_buffer_unmap(struct pipe_context
*pipe
,
245 struct pipe_resource
*buf
,
246 struct pipe_transfer
*transfer
)
249 pipe
->transfer_unmap(pipe
, transfer
);
250 pipe
->transfer_destroy(pipe
, transfer
);
255 pipe_buffer_flush_mapped_range(struct pipe_context
*pipe
,
256 struct pipe_transfer
*transfer
,
264 assert(transfer
->box
.x
<= offset
);
265 assert(offset
+ length
<= transfer
->box
.x
+ transfer
->box
.width
);
267 /* Match old screen->buffer_flush_mapped_range() behaviour, where
268 * offset parameter is relative to the start of the buffer, not the
271 transfer_offset
= offset
- transfer
->box
.x
;
273 u_box_1d(transfer_offset
, length
, &box
);
275 pipe
->transfer_flush_region(pipe
, transfer
, &box
);
279 pipe_buffer_write(struct pipe_context
*pipe
,
280 struct pipe_resource
*buf
,
287 u_box_1d(offset
, size
, &box
);
289 pipe
->transfer_inline_write( pipe
,
300 * Special case for writing non-overlapping ranges.
302 * We can avoid GPU/CPU synchronization when writing range that has never
303 * been written before.
306 pipe_buffer_write_nooverlap(struct pipe_context
*pipe
,
307 struct pipe_resource
*buf
,
308 unsigned offset
, unsigned size
,
313 u_box_1d(offset
, size
, &box
);
315 pipe
->transfer_inline_write(pipe
,
318 (PIPE_TRANSFER_WRITE
|
319 PIPE_TRANSFER_NOOVERWRITE
),
326 pipe_buffer_read(struct pipe_context
*pipe
,
327 struct pipe_resource
*buf
,
332 struct pipe_transfer
*src_transfer
;
335 map
= (ubyte
*) pipe_buffer_map_range(pipe
,
342 memcpy(data
, map
+ offset
, size
);
344 pipe_buffer_unmap(pipe
, buf
, src_transfer
);
347 static INLINE
struct pipe_transfer
*
348 pipe_get_transfer( struct pipe_context
*context
,
349 struct pipe_resource
*resource
,
350 unsigned level
, unsigned layer
,
351 enum pipe_transfer_usage usage
,
352 unsigned x
, unsigned y
,
353 unsigned w
, unsigned h
)
356 u_box_2d_zslice( x
, y
, layer
, w
, h
, &box
);
357 return context
->get_transfer( context
,
365 pipe_transfer_map( struct pipe_context
*context
,
366 struct pipe_transfer
*transfer
)
368 return context
->transfer_map( context
, transfer
);
372 pipe_transfer_unmap( struct pipe_context
*context
,
373 struct pipe_transfer
*transfer
)
375 context
->transfer_unmap( context
, transfer
);
380 pipe_transfer_destroy( struct pipe_context
*context
,
381 struct pipe_transfer
*transfer
)
383 context
->transfer_destroy(context
, transfer
);
387 static INLINE boolean
util_get_offset(
388 const struct pipe_rasterizer_state
*templ
,
392 case PIPE_POLYGON_MODE_POINT
:
393 return templ
->offset_point
;
394 case PIPE_POLYGON_MODE_LINE
:
395 return templ
->offset_line
;
396 case PIPE_POLYGON_MODE_FILL
:
397 return templ
->offset_tri
;
408 #endif /* U_INLINES_H */