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_atomic.h"
37 #include "util/u_box.h"
38 #include "util/u_math.h"
47 * Reference counting helper functions.
52 pipe_reference_init(struct pipe_reference
*reference
, unsigned count
)
54 p_atomic_set(&reference
->count
, count
);
58 pipe_is_referenced(struct pipe_reference
*reference
)
60 return p_atomic_read(&reference
->count
) != 0;
64 * Update reference counting.
65 * The old thing pointed to, if any, will be unreferenced.
66 * Both 'ptr' and 'reference' may be NULL.
67 * \return TRUE if the object's refcount hits zero and should be destroyed.
70 pipe_reference(struct pipe_reference
*ptr
, struct pipe_reference
*reference
)
72 boolean destroy
= FALSE
;
74 if(ptr
!= reference
) {
75 /* bump the reference.count first */
77 assert(pipe_is_referenced(reference
));
78 p_atomic_inc(&reference
->count
);
82 assert(pipe_is_referenced(ptr
));
83 if (p_atomic_dec_zero(&ptr
->count
)) {
94 pipe_surface_reference(struct pipe_surface
**ptr
, struct pipe_surface
*surf
)
96 struct pipe_surface
*old_surf
= *ptr
;
98 if (pipe_reference(&(*ptr
)->reference
, &surf
->reference
))
99 old_surf
->texture
->screen
->tex_surface_destroy(old_surf
);
105 pipe_resource_reference(struct pipe_resource
**ptr
, struct pipe_resource
*tex
)
107 struct pipe_resource
*old_tex
= *ptr
;
109 if (pipe_reference(&(*ptr
)->reference
, &tex
->reference
))
110 old_tex
->screen
->resource_destroy(old_tex
->screen
, old_tex
);
116 pipe_sampler_view_reference(struct pipe_sampler_view
**ptr
, struct pipe_sampler_view
*view
)
118 struct pipe_sampler_view
*old_view
= *ptr
;
120 if (pipe_reference(&(*ptr
)->reference
, &view
->reference
))
121 old_view
->context
->sampler_view_destroy(old_view
->context
, old_view
);
126 pipe_surface_reset(struct pipe_surface
* ps
, struct pipe_resource
*pt
,
127 unsigned face
, unsigned level
, unsigned zslice
, unsigned flags
)
129 pipe_resource_reference(&ps
->texture
, pt
);
130 ps
->format
= pt
->format
;
131 ps
->width
= u_minify(pt
->width0
, level
);
132 ps
->height
= u_minify(pt
->height0
, level
);
140 pipe_surface_init(struct pipe_surface
* ps
, struct pipe_resource
*pt
,
141 unsigned face
, unsigned level
, unsigned zslice
, unsigned flags
)
144 pipe_reference_init(&ps
->reference
, 1);
145 pipe_surface_reset(ps
, pt
, face
, level
, zslice
, flags
);
149 * Convenience wrappers for screen buffer functions.
152 static INLINE
struct pipe_resource
*
153 pipe_buffer_create( struct pipe_screen
*screen
,
157 struct pipe_resource buffer
;
158 memset(&buffer
, 0, sizeof buffer
);
159 buffer
.target
= PIPE_BUFFER
;
160 buffer
.format
= PIPE_FORMAT_R8_UNORM
; /* want TYPELESS or similar */
162 buffer
.usage
= PIPE_USAGE_DEFAULT
;
164 buffer
.width0
= size
;
167 return screen
->resource_create(screen
, &buffer
);
171 static INLINE
struct pipe_resource
*
172 pipe_user_buffer_create( struct pipe_screen
*screen
, void *ptr
, unsigned size
,
175 return screen
->user_buffer_create(screen
, ptr
, size
, usage
);
179 pipe_buffer_map_range(struct pipe_context
*pipe
,
180 struct pipe_resource
*buffer
,
184 struct pipe_transfer
**transfer
)
189 assert(offset
< buffer
->width0
);
190 assert(offset
+ length
<= buffer
->width0
);
193 u_box_1d(offset
, length
, &box
);
195 *transfer
= pipe
->get_transfer( pipe
,
201 if (*transfer
== NULL
)
204 map
= pipe
->transfer_map( pipe
, *transfer
);
206 pipe
->transfer_destroy( pipe
, *transfer
);
210 /* Match old screen->buffer_map_range() behaviour, return pointer
211 * to where the beginning of the buffer would be:
213 return (void *)((char *)map
- offset
);
218 pipe_buffer_map(struct pipe_context
*pipe
,
219 struct pipe_resource
*buffer
,
221 struct pipe_transfer
**transfer
)
223 return pipe_buffer_map_range(pipe
, buffer
, 0, buffer
->width0
, usage
, transfer
);
228 pipe_buffer_unmap(struct pipe_context
*pipe
,
229 struct pipe_resource
*buf
,
230 struct pipe_transfer
*transfer
)
233 pipe
->transfer_unmap(pipe
, transfer
);
234 pipe
->transfer_destroy(pipe
, transfer
);
239 pipe_buffer_flush_mapped_range(struct pipe_context
*pipe
,
240 struct pipe_transfer
*transfer
,
248 assert(transfer
->box
.x
<= offset
);
249 assert(offset
+ length
<= transfer
->box
.x
+ transfer
->box
.width
);
251 /* Match old screen->buffer_flush_mapped_range() behaviour, where
252 * offset parameter is relative to the start of the buffer, not the
255 transfer_offset
= offset
- transfer
->box
.x
;
257 u_box_1d(transfer_offset
, length
, &box
);
259 pipe
->transfer_flush_region(pipe
, transfer
, &box
);
263 pipe_buffer_write(struct pipe_context
*pipe
,
264 struct pipe_resource
*buf
,
271 u_box_1d(offset
, size
, &box
);
273 pipe
->transfer_inline_write( pipe
,
284 * Special case for writing non-overlapping ranges.
286 * We can avoid GPU/CPU synchronization when writing range that has never
287 * been written before.
290 pipe_buffer_write_nooverlap(struct pipe_context
*pipe
,
291 struct pipe_resource
*buf
,
292 unsigned offset
, unsigned size
,
297 u_box_1d(offset
, size
, &box
);
299 pipe
->transfer_inline_write(pipe
,
302 (PIPE_TRANSFER_WRITE
|
303 PIPE_TRANSFER_NOOVERWRITE
),
310 pipe_buffer_read(struct pipe_context
*pipe
,
311 struct pipe_resource
*buf
,
316 struct pipe_transfer
*src_transfer
;
319 map
= (ubyte
*) pipe_buffer_map_range(pipe
,
326 memcpy(data
, map
+ offset
, size
);
328 pipe_buffer_unmap(pipe
, buf
, src_transfer
);
331 static INLINE
struct pipe_transfer
*
332 pipe_get_transfer( struct pipe_context
*context
,
333 struct pipe_resource
*resource
,
334 unsigned face
, unsigned level
,
336 enum pipe_transfer_usage usage
,
337 unsigned x
, unsigned y
,
338 unsigned w
, unsigned h
)
341 u_box_2d_zslice( x
, y
, zslice
, w
, h
, &box
);
342 return context
->get_transfer( context
,
344 u_subresource(face
, level
),
350 pipe_transfer_map( struct pipe_context
*context
,
351 struct pipe_transfer
*transfer
)
353 return context
->transfer_map( context
, transfer
);
357 pipe_transfer_unmap( struct pipe_context
*context
,
358 struct pipe_transfer
*transfer
)
360 context
->transfer_unmap( context
, transfer
);
365 pipe_transfer_destroy( struct pipe_context
*context
,
366 struct pipe_transfer
*transfer
)
368 context
->transfer_destroy(context
, transfer
);
372 static INLINE boolean
util_get_offset(
373 const struct pipe_rasterizer_state
*templ
,
377 case PIPE_POLYGON_MODE_POINT
:
378 return templ
->offset_point
;
379 case PIPE_POLYGON_MODE_LINE
:
380 return templ
->offset_line
;
381 case PIPE_POLYGON_MODE_FILL
:
382 return templ
->offset_tri
;
394 #endif /* U_INLINES_H */