1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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_shader_tokens.h"
34 #include "pipe/p_state.h"
35 #include "pipe/p_screen.h"
36 #include "util/u_debug.h"
37 #include "util/u_debug_describe.h"
38 #include "util/u_debug_refcnt.h"
39 #include "util/u_atomic.h"
40 #include "util/u_box.h"
41 #include "util/u_math.h"
50 * Reference counting helper functions.
55 pipe_reference_init(struct pipe_reference
*dst
, unsigned count
)
57 p_atomic_set(&dst
->count
, count
);
61 pipe_is_referenced(struct pipe_reference
*src
)
63 return p_atomic_read(&src
->count
) != 0;
67 * Update reference counting.
68 * The old thing pointed to, if any, will be unreferenced.
69 * Both 'dst' and 'src' may be NULL.
70 * \return TRUE if the object's refcount hits zero and should be destroyed.
73 pipe_reference_described(struct pipe_reference
*dst
,
74 struct pipe_reference
*src
,
75 debug_reference_descriptor get_desc
)
78 /* bump the src.count first */
80 MAYBE_UNUSED
int count
= p_atomic_inc_return(&src
->count
);
81 assert(count
!= 1); /* src had to be referenced */
82 debug_reference(src
, get_desc
, 1);
86 int count
= p_atomic_dec_return(&dst
->count
);
87 assert(count
!= -1); /* dst had to be referenced */
88 debug_reference(dst
, get_desc
, -1);
98 pipe_reference(struct pipe_reference
*dst
, struct pipe_reference
*src
)
100 return pipe_reference_described(dst
, src
,
101 (debug_reference_descriptor
)
102 debug_describe_reference
);
106 pipe_surface_reference(struct pipe_surface
**dst
, struct pipe_surface
*src
)
108 struct pipe_surface
*old_dst
= *dst
;
110 if (pipe_reference_described(&old_dst
->reference
, &src
->reference
,
111 (debug_reference_descriptor
)
112 debug_describe_surface
))
113 old_dst
->context
->surface_destroy(old_dst
->context
, old_dst
);
118 * Similar to pipe_surface_reference() but always set the pointer to NULL
119 * and pass in an explicit context. The explicit context avoids the problem
120 * of using a deleted context's surface_destroy() method when freeing a surface
121 * that's shared by multiple contexts.
124 pipe_surface_release(struct pipe_context
*pipe
, struct pipe_surface
**ptr
)
126 struct pipe_surface
*old
= *ptr
;
128 if (pipe_reference_described(&old
->reference
, NULL
,
129 (debug_reference_descriptor
)
130 debug_describe_surface
))
131 pipe
->surface_destroy(pipe
, old
);
137 pipe_resource_reference(struct pipe_resource
**dst
, struct pipe_resource
*src
)
139 struct pipe_resource
*old_dst
= *dst
;
141 if (pipe_reference_described(&old_dst
->reference
, &src
->reference
,
142 (debug_reference_descriptor
)
143 debug_describe_resource
)) {
144 /* Avoid recursion, which would prevent inlining this function */
146 struct pipe_resource
*next
= old_dst
->next
;
148 old_dst
->screen
->resource_destroy(old_dst
->screen
, old_dst
);
150 } while (pipe_reference_described(&old_dst
->reference
, NULL
,
151 (debug_reference_descriptor
)
152 debug_describe_resource
));
158 * Same as pipe_surface_release, but used when pipe_context doesn't exist
162 pipe_surface_release_no_context(struct pipe_surface
**ptr
)
164 struct pipe_surface
*surf
= *ptr
;
166 if (pipe_reference_described(&surf
->reference
, NULL
,
167 (debug_reference_descriptor
)
168 debug_describe_surface
)) {
169 /* trivially destroy pipe_surface */
170 pipe_resource_reference(&surf
->texture
, NULL
);
177 * Set *dst to \p src with proper reference counting.
179 * The caller must guarantee that \p src and *dst were created in
180 * the same context (if they exist), and that this must be the current context.
183 pipe_sampler_view_reference(struct pipe_sampler_view
**dst
,
184 struct pipe_sampler_view
*src
)
186 struct pipe_sampler_view
*old_dst
= *dst
;
188 if (pipe_reference_described(&old_dst
->reference
, &src
->reference
,
189 (debug_reference_descriptor
)
190 debug_describe_sampler_view
))
191 old_dst
->context
->sampler_view_destroy(old_dst
->context
, old_dst
);
196 pipe_so_target_reference(struct pipe_stream_output_target
**dst
,
197 struct pipe_stream_output_target
*src
)
199 struct pipe_stream_output_target
*old_dst
= *dst
;
201 if (pipe_reference_described(&old_dst
->reference
, &src
->reference
,
202 (debug_reference_descriptor
)debug_describe_so_target
))
203 old_dst
->context
->stream_output_target_destroy(old_dst
->context
, old_dst
);
208 pipe_vertex_buffer_unreference(struct pipe_vertex_buffer
*dst
)
210 if (dst
->is_user_buffer
)
211 dst
->buffer
.user
= NULL
;
213 pipe_resource_reference(&dst
->buffer
.resource
, NULL
);
217 pipe_vertex_buffer_reference(struct pipe_vertex_buffer
*dst
,
218 const struct pipe_vertex_buffer
*src
)
220 pipe_vertex_buffer_unreference(dst
);
221 if (!src
->is_user_buffer
)
222 pipe_resource_reference(&dst
->buffer
.resource
, src
->buffer
.resource
);
223 memcpy(dst
, src
, sizeof(*src
));
227 pipe_surface_reset(struct pipe_context
*ctx
, struct pipe_surface
* ps
,
228 struct pipe_resource
*pt
, unsigned level
, unsigned layer
)
230 pipe_resource_reference(&ps
->texture
, pt
);
231 ps
->format
= pt
->format
;
232 ps
->width
= u_minify(pt
->width0
, level
);
233 ps
->height
= u_minify(pt
->height0
, level
);
234 ps
->u
.tex
.level
= level
;
235 ps
->u
.tex
.first_layer
= ps
->u
.tex
.last_layer
= layer
;
240 pipe_surface_init(struct pipe_context
*ctx
, struct pipe_surface
* ps
,
241 struct pipe_resource
*pt
, unsigned level
, unsigned layer
)
244 pipe_reference_init(&ps
->reference
, 1);
245 pipe_surface_reset(ctx
, ps
, pt
, level
, layer
);
248 /* Return true if the surfaces are equal. */
249 static inline boolean
250 pipe_surface_equal(struct pipe_surface
*s1
, struct pipe_surface
*s2
)
252 return s1
->texture
== s2
->texture
&&
253 s1
->format
== s2
->format
&&
254 (s1
->texture
->target
!= PIPE_BUFFER
||
255 (s1
->u
.buf
.first_element
== s2
->u
.buf
.first_element
&&
256 s1
->u
.buf
.last_element
== s2
->u
.buf
.last_element
)) &&
257 (s1
->texture
->target
== PIPE_BUFFER
||
258 (s1
->u
.tex
.level
== s2
->u
.tex
.level
&&
259 s1
->u
.tex
.first_layer
== s2
->u
.tex
.first_layer
&&
260 s1
->u
.tex
.last_layer
== s2
->u
.tex
.last_layer
));
264 * Convenience wrappers for screen buffer functions.
269 * Create a new resource.
270 * \param bind bitmask of PIPE_BIND_x flags
271 * \param usage a PIPE_USAGE_x value
273 static inline struct pipe_resource
*
274 pipe_buffer_create(struct pipe_screen
*screen
,
276 enum pipe_resource_usage usage
,
279 struct pipe_resource buffer
;
280 memset(&buffer
, 0, sizeof buffer
);
281 buffer
.target
= PIPE_BUFFER
;
282 buffer
.format
= PIPE_FORMAT_R8_UNORM
; /* want TYPELESS or similar */
284 buffer
.usage
= usage
;
286 buffer
.width0
= size
;
289 buffer
.array_size
= 1;
290 return screen
->resource_create(screen
, &buffer
);
294 static inline struct pipe_resource
*
295 pipe_buffer_create_const0(struct pipe_screen
*screen
,
297 enum pipe_resource_usage usage
,
300 struct pipe_resource buffer
;
301 memset(&buffer
, 0, sizeof buffer
);
302 buffer
.target
= PIPE_BUFFER
;
303 buffer
.format
= PIPE_FORMAT_R8_UNORM
;
305 buffer
.usage
= usage
;
306 buffer
.flags
= screen
->get_param(screen
, PIPE_CAP_CONSTBUF0_FLAGS
);
307 buffer
.width0
= size
;
310 buffer
.array_size
= 1;
311 return screen
->resource_create(screen
, &buffer
);
316 * Map a range of a resource.
317 * \param offset start of region, in bytes
318 * \param length size of region, in bytes
319 * \param access bitmask of PIPE_TRANSFER_x flags
320 * \param transfer returns a transfer object
323 pipe_buffer_map_range(struct pipe_context
*pipe
,
324 struct pipe_resource
*buffer
,
328 struct pipe_transfer
**transfer
)
333 assert(offset
< buffer
->width0
);
334 assert(offset
+ length
<= buffer
->width0
);
337 u_box_1d(offset
, length
, &box
);
339 map
= pipe
->transfer_map(pipe
, buffer
, 0, access
, &box
, transfer
);
349 * Map whole resource.
350 * \param access bitmask of PIPE_TRANSFER_x flags
351 * \param transfer returns a transfer object
354 pipe_buffer_map(struct pipe_context
*pipe
,
355 struct pipe_resource
*buffer
,
357 struct pipe_transfer
**transfer
)
359 return pipe_buffer_map_range(pipe
, buffer
, 0, buffer
->width0
,
365 pipe_buffer_unmap(struct pipe_context
*pipe
,
366 struct pipe_transfer
*transfer
)
368 pipe
->transfer_unmap(pipe
, transfer
);
372 pipe_buffer_flush_mapped_range(struct pipe_context
*pipe
,
373 struct pipe_transfer
*transfer
,
381 assert(transfer
->box
.x
<= (int) offset
);
382 assert((int) (offset
+ length
) <= transfer
->box
.x
+ transfer
->box
.width
);
384 /* Match old screen->buffer_flush_mapped_range() behaviour, where
385 * offset parameter is relative to the start of the buffer, not the
388 transfer_offset
= offset
- transfer
->box
.x
;
390 u_box_1d(transfer_offset
, length
, &box
);
392 pipe
->transfer_flush_region(pipe
, transfer
, &box
);
396 pipe_buffer_write(struct pipe_context
*pipe
,
397 struct pipe_resource
*buf
,
402 /* Don't set any other usage bits. Drivers should derive them. */
403 pipe
->buffer_subdata(pipe
, buf
, PIPE_TRANSFER_WRITE
, offset
, size
, data
);
407 * Special case for writing non-overlapping ranges.
409 * We can avoid GPU/CPU synchronization when writing range that has never
410 * been written before.
413 pipe_buffer_write_nooverlap(struct pipe_context
*pipe
,
414 struct pipe_resource
*buf
,
415 unsigned offset
, unsigned size
,
418 pipe
->buffer_subdata(pipe
, buf
,
419 (PIPE_TRANSFER_WRITE
|
420 PIPE_TRANSFER_UNSYNCHRONIZED
),
426 * Create a new resource and immediately put data into it
427 * \param bind bitmask of PIPE_BIND_x flags
428 * \param usage bitmask of PIPE_USAGE_x flags
430 static inline struct pipe_resource
*
431 pipe_buffer_create_with_data(struct pipe_context
*pipe
,
433 enum pipe_resource_usage usage
,
437 struct pipe_resource
*res
= pipe_buffer_create(pipe
->screen
,
439 pipe_buffer_write_nooverlap(pipe
, res
, 0, size
, ptr
);
444 pipe_buffer_read(struct pipe_context
*pipe
,
445 struct pipe_resource
*buf
,
450 struct pipe_transfer
*src_transfer
;
453 map
= (ubyte
*) pipe_buffer_map_range(pipe
,
461 memcpy(data
, map
, size
);
462 pipe_buffer_unmap(pipe
, src_transfer
);
467 * Map a resource for reading/writing.
468 * \param access bitmask of PIPE_TRANSFER_x flags
471 pipe_transfer_map(struct pipe_context
*context
,
472 struct pipe_resource
*resource
,
473 unsigned level
, unsigned layer
,
475 unsigned x
, unsigned y
,
476 unsigned w
, unsigned h
,
477 struct pipe_transfer
**transfer
)
480 u_box_2d_zslice(x
, y
, layer
, w
, h
, &box
);
481 return context
->transfer_map(context
,
490 * Map a 3D (texture) resource for reading/writing.
491 * \param access bitmask of PIPE_TRANSFER_x flags
494 pipe_transfer_map_3d(struct pipe_context
*context
,
495 struct pipe_resource
*resource
,
498 unsigned x
, unsigned y
, unsigned z
,
499 unsigned w
, unsigned h
, unsigned d
,
500 struct pipe_transfer
**transfer
)
503 u_box_3d(x
, y
, z
, w
, h
, d
, &box
);
504 return context
->transfer_map(context
,
512 pipe_transfer_unmap(struct pipe_context
*context
,
513 struct pipe_transfer
*transfer
)
515 context
->transfer_unmap(context
, transfer
);
519 pipe_set_constant_buffer(struct pipe_context
*pipe
,
520 enum pipe_shader_type shader
, uint index
,
521 struct pipe_resource
*buf
)
524 struct pipe_constant_buffer cb
;
526 cb
.buffer_offset
= 0;
527 cb
.buffer_size
= buf
->width0
;
528 cb
.user_buffer
= NULL
;
529 pipe
->set_constant_buffer(pipe
, shader
, index
, &cb
);
531 pipe
->set_constant_buffer(pipe
, shader
, index
, NULL
);
537 * Get the polygon offset enable/disable flag for the given polygon fill mode.
538 * \param fill_mode one of PIPE_POLYGON_MODE_POINT/LINE/FILL
540 static inline boolean
541 util_get_offset(const struct pipe_rasterizer_state
*templ
,
545 case PIPE_POLYGON_MODE_POINT
:
546 return templ
->offset_point
;
547 case PIPE_POLYGON_MODE_LINE
:
548 return templ
->offset_line
;
549 case PIPE_POLYGON_MODE_FILL
:
550 return templ
->offset_tri
;
558 util_get_min_point_size(const struct pipe_rasterizer_state
*state
)
560 /* The point size should be clamped to this value at the rasterizer stage.
562 return !state
->point_quad_rasterization
&&
563 !state
->point_smooth
&&
564 !state
->multisample
? 1.0f
: 0.0f
;
568 util_query_clear_result(union pipe_query_result
*result
, unsigned type
)
571 case PIPE_QUERY_OCCLUSION_PREDICATE
:
572 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
573 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
574 case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE
:
575 case PIPE_QUERY_GPU_FINISHED
:
578 case PIPE_QUERY_OCCLUSION_COUNTER
:
579 case PIPE_QUERY_TIMESTAMP
:
580 case PIPE_QUERY_TIME_ELAPSED
:
581 case PIPE_QUERY_PRIMITIVES_GENERATED
:
582 case PIPE_QUERY_PRIMITIVES_EMITTED
:
585 case PIPE_QUERY_SO_STATISTICS
:
586 memset(&result
->so_statistics
, 0, sizeof(result
->so_statistics
));
588 case PIPE_QUERY_TIMESTAMP_DISJOINT
:
589 memset(&result
->timestamp_disjoint
, 0, sizeof(result
->timestamp_disjoint
));
591 case PIPE_QUERY_PIPELINE_STATISTICS
:
592 memset(&result
->pipeline_statistics
, 0, sizeof(result
->pipeline_statistics
));
595 memset(result
, 0, sizeof(*result
));
599 /** Convert PIPE_TEXTURE_x to TGSI_TEXTURE_x */
600 static inline enum tgsi_texture_type
601 util_pipe_tex_to_tgsi_tex(enum pipe_texture_target pipe_tex_target
,
604 switch (pipe_tex_target
) {
606 return TGSI_TEXTURE_BUFFER
;
608 case PIPE_TEXTURE_1D
:
609 assert(nr_samples
<= 1);
610 return TGSI_TEXTURE_1D
;
612 case PIPE_TEXTURE_2D
:
613 return nr_samples
> 1 ? TGSI_TEXTURE_2D_MSAA
: TGSI_TEXTURE_2D
;
615 case PIPE_TEXTURE_RECT
:
616 assert(nr_samples
<= 1);
617 return TGSI_TEXTURE_RECT
;
619 case PIPE_TEXTURE_3D
:
620 assert(nr_samples
<= 1);
621 return TGSI_TEXTURE_3D
;
623 case PIPE_TEXTURE_CUBE
:
624 assert(nr_samples
<= 1);
625 return TGSI_TEXTURE_CUBE
;
627 case PIPE_TEXTURE_1D_ARRAY
:
628 assert(nr_samples
<= 1);
629 return TGSI_TEXTURE_1D_ARRAY
;
631 case PIPE_TEXTURE_2D_ARRAY
:
632 return nr_samples
> 1 ? TGSI_TEXTURE_2D_ARRAY_MSAA
:
633 TGSI_TEXTURE_2D_ARRAY
;
635 case PIPE_TEXTURE_CUBE_ARRAY
:
636 return TGSI_TEXTURE_CUBE_ARRAY
;
639 assert(0 && "unexpected texture target");
640 return TGSI_TEXTURE_UNKNOWN
;
646 util_copy_constant_buffer(struct pipe_constant_buffer
*dst
,
647 const struct pipe_constant_buffer
*src
)
650 pipe_resource_reference(&dst
->buffer
, src
->buffer
);
651 dst
->buffer_offset
= src
->buffer_offset
;
652 dst
->buffer_size
= src
->buffer_size
;
653 dst
->user_buffer
= src
->user_buffer
;
656 pipe_resource_reference(&dst
->buffer
, NULL
);
657 dst
->buffer_offset
= 0;
658 dst
->buffer_size
= 0;
659 dst
->user_buffer
= NULL
;
664 util_copy_image_view(struct pipe_image_view
*dst
,
665 const struct pipe_image_view
*src
)
668 pipe_resource_reference(&dst
->resource
, src
->resource
);
669 dst
->format
= src
->format
;
670 dst
->access
= src
->access
;
673 pipe_resource_reference(&dst
->resource
, NULL
);
674 dst
->format
= PIPE_FORMAT_NONE
;
676 memset(&dst
->u
, 0, sizeof(dst
->u
));
680 static inline unsigned
681 util_max_layer(const struct pipe_resource
*r
, unsigned level
)
684 case PIPE_TEXTURE_3D
:
685 return u_minify(r
->depth0
, level
) - 1;
686 case PIPE_TEXTURE_CUBE
:
687 assert(r
->array_size
== 6);
689 case PIPE_TEXTURE_1D_ARRAY
:
690 case PIPE_TEXTURE_2D_ARRAY
:
691 case PIPE_TEXTURE_CUBE_ARRAY
:
692 return r
->array_size
- 1;
698 static inline unsigned
699 util_num_layers(const struct pipe_resource
*r
, unsigned level
)
701 return util_max_layer(r
, level
) + 1;
705 util_texrange_covers_whole_level(const struct pipe_resource
*tex
,
706 unsigned level
, unsigned x
, unsigned y
,
707 unsigned z
, unsigned width
,
708 unsigned height
, unsigned depth
)
710 return x
== 0 && y
== 0 && z
== 0 &&
711 width
== u_minify(tex
->width0
, level
) &&
712 height
== u_minify(tex
->height0
, level
) &&
713 depth
== util_num_layers(tex
, level
);
720 #endif /* U_INLINES_H */