1 /**************************************************************************
2 *
3 * Copyright 2012 Marek Olšák <[email protected]>
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 THE AUTHORS AND/OR THEIR 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_HELPERS_H
29 #define U_HELPERS_H
30
31 #include "pipe/p_state.h"
32 #include "c11/threads.h"
33 #include "compiler/shader_enums.h"
34 #include <stdio.h>
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 void util_set_vertex_buffers_mask(struct pipe_vertex_buffer *dst,
41 uint32_t *enabled_buffers,
42 const struct pipe_vertex_buffer *src,
43 unsigned count,
44 bool take_ownership);
45
46 void util_set_vertex_buffers_count(struct pipe_vertex_buffer *dst,
47 unsigned *dst_count,
48 const struct pipe_vertex_buffer *src,
49 unsigned count,
50 bool take_ownership);
51
52 void util_set_shader_buffers_mask(struct pipe_shader_buffer *dst,
53 uint32_t *enabled_buffers,
54 const struct pipe_shader_buffer *src,
55 unsigned start_slot, unsigned count);
56
57 bool util_upload_index_buffer(struct pipe_context *pipe,
58 const struct pipe_draw_info *info,
59 const struct pipe_draw_start_count_bias *draw,
60 struct pipe_resource **out_buffer,
61 unsigned *out_offset, unsigned alignment);
62
63 void
64 util_lower_uint64_vertex_elements(const struct pipe_vertex_element **velems,
65 unsigned *velem_count,
66 struct pipe_vertex_element tmp[PIPE_MAX_ATTRIBS]);
67
68 /* Helper function to determine if the varying should contain the point
69 * coordinates, given the sprite_coord_enable mask. Requires
70 * PIPE_CAP_TGSI_TEXCOORD to be enabled.
71 */
72 static inline bool
util_varying_is_point_coord(gl_varying_slot slot,uint32_t sprite_coord_enable)73 util_varying_is_point_coord(gl_varying_slot slot, uint32_t sprite_coord_enable)
74 {
75 if (slot == VARYING_SLOT_PNTC)
76 return true;
77
78 if (slot >= VARYING_SLOT_TEX0 && slot <= VARYING_SLOT_TEX7 &&
79 (sprite_coord_enable & (1 << (slot - VARYING_SLOT_TEX0)))) {
80 return true;
81 }
82
83 return false;
84 }
85
86 struct pipe_query *
87 util_begin_pipestat_query(struct pipe_context *ctx);
88
89 void
90 util_end_pipestat_query(struct pipe_context *ctx, struct pipe_query *q,
91 FILE *f);
92
93 struct pipe_query *
94 util_begin_time_query(struct pipe_context *ctx);
95 void
96 util_end_time_query(struct pipe_context *ctx, struct pipe_query *q, FILE *f,
97 const char *name);
98
99 void
100 util_wait_for_idle(struct pipe_context *ctx);
101
102 /* A utility for throttling execution based on memory usage. */
103 struct util_throttle {
104 struct {
105 struct pipe_fence_handle *fence;
106 uint64_t mem_usage;
107 } ring[10];
108
109 unsigned flush_index;
110 unsigned wait_index;
111 uint64_t max_mem_usage;
112 };
113
114 void util_throttle_init(struct util_throttle *t, uint64_t max_mem_usage);
115 void util_throttle_deinit(struct pipe_screen *screen, struct util_throttle *t);
116 void util_throttle_memory_usage(struct pipe_context *pipe,
117 struct util_throttle *t, uint64_t memory_size);
118 void util_sw_query_memory_info(struct pipe_screen *pscreen,
119 struct pipe_memory_info *info);
120
121 void
122 util_init_pipe_vertex_state(struct pipe_screen *screen,
123 struct pipe_vertex_buffer *buffer,
124 const struct pipe_vertex_element *elements,
125 unsigned num_elements,
126 struct pipe_resource *indexbuf,
127 uint32_t full_velem_mask,
128 struct pipe_vertex_state *state);
129
130 union pipe_color_union util_clamp_color(enum pipe_format format,
131 const union pipe_color_union *color);
132
133 struct pipe_sampler_view
134 util_image_to_sampler_view(struct pipe_image_view *v);
135
136 #ifdef __cplusplus
137 }
138 #endif
139
140 #endif
141