gallium: change comments to remove 'state tracker'
[mesa.git] / src / gallium / auxiliary / util / u_helpers.c
1 /**************************************************************************
2 *
3 * Copyright 2012 Marek Olšák <maraeo@gmail.com>
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 #include "util/u_cpu_detect.h"
29 #include "util/u_helpers.h"
30 #include "util/u_inlines.h"
31 #include "util/u_upload_mgr.h"
32 #include "util/u_thread.h"
33 #include "util/os_time.h"
34 #include <inttypes.h>
35
36 /**
37 * This function is used to copy an array of pipe_vertex_buffer structures,
38 * while properly referencing the pipe_vertex_buffer::buffer member.
39 *
40 * enabled_buffers is updated such that the bits corresponding to the indices
41 * of disabled buffers are set to 0 and the enabled ones are set to 1.
42 *
43 * \sa util_copy_framebuffer_state
44 */
45 void util_set_vertex_buffers_mask(struct pipe_vertex_buffer *dst,
46 uint32_t *enabled_buffers,
47 const struct pipe_vertex_buffer *src,
48 unsigned start_slot, unsigned count)
49 {
50 unsigned i;
51 uint32_t bitmask = 0;
52
53 dst += start_slot;
54
55 *enabled_buffers &= ~u_bit_consecutive(start_slot, count);
56
57 if (src) {
58 for (i = 0; i < count; i++) {
59 if (src[i].buffer.resource)
60 bitmask |= 1 << i;
61
62 pipe_vertex_buffer_unreference(&dst[i]);
63
64 if (!src[i].is_user_buffer)
65 pipe_resource_reference(&dst[i].buffer.resource, src[i].buffer.resource);
66 }
67
68 /* Copy over the other members of pipe_vertex_buffer. */
69 memcpy(dst, src, count * sizeof(struct pipe_vertex_buffer));
70
71 *enabled_buffers |= bitmask << start_slot;
72 }
73 else {
74 /* Unreference the buffers. */
75 for (i = 0; i < count; i++)
76 pipe_vertex_buffer_unreference(&dst[i]);
77 }
78 }
79
80 /**
81 * Same as util_set_vertex_buffers_mask, but it only returns the number
82 * of bound buffers.
83 */
84 void util_set_vertex_buffers_count(struct pipe_vertex_buffer *dst,
85 unsigned *dst_count,
86 const struct pipe_vertex_buffer *src,
87 unsigned start_slot, unsigned count)
88 {
89 unsigned i;
90 uint32_t enabled_buffers = 0;
91
92 for (i = 0; i < *dst_count; i++) {
93 if (dst[i].buffer.resource)
94 enabled_buffers |= (1ull << i);
95 }
96
97 util_set_vertex_buffers_mask(dst, &enabled_buffers, src, start_slot,
98 count);
99
100 *dst_count = util_last_bit(enabled_buffers);
101 }
102
103 /**
104 * This function is used to copy an array of pipe_shader_buffer structures,
105 * while properly referencing the pipe_shader_buffer::buffer member.
106 *
107 * \sa util_set_vertex_buffer_mask
108 */
109 void util_set_shader_buffers_mask(struct pipe_shader_buffer *dst,
110 uint32_t *enabled_buffers,
111 const struct pipe_shader_buffer *src,
112 unsigned start_slot, unsigned count)
113 {
114 unsigned i;
115
116 dst += start_slot;
117
118 if (src) {
119 for (i = 0; i < count; i++) {
120 pipe_resource_reference(&dst[i].buffer, src[i].buffer);
121
122 if (src[i].buffer)
123 *enabled_buffers |= (1ull << (start_slot + i));
124 else
125 *enabled_buffers &= ~(1ull << (start_slot + i));
126 }
127
128 /* Copy over the other members of pipe_shader_buffer. */
129 memcpy(dst, src, count * sizeof(struct pipe_shader_buffer));
130 }
131 else {
132 /* Unreference the buffers. */
133 for (i = 0; i < count; i++)
134 pipe_resource_reference(&dst[i].buffer, NULL);
135
136 *enabled_buffers &= ~(((1ull << count) - 1) << start_slot);
137 }
138 }
139
140 /**
141 * Given a user index buffer, save the structure to "saved", and upload it.
142 */
143 bool
144 util_upload_index_buffer(struct pipe_context *pipe,
145 const struct pipe_draw_info *info,
146 struct pipe_resource **out_buffer,
147 unsigned *out_offset, unsigned alignment)
148 {
149 unsigned start_offset = info->start * info->index_size;
150
151 u_upload_data(pipe->stream_uploader, start_offset,
152 info->count * info->index_size, alignment,
153 (char*)info->index.user + start_offset,
154 out_offset, out_buffer);
155 u_upload_unmap(pipe->stream_uploader);
156 *out_offset -= start_offset;
157 return *out_buffer != NULL;
158 }
159
160 /**
161 * Called by MakeCurrent. Used to notify the driver that the application
162 * thread may have been changed.
163 *
164 * The function pins the current thread and driver threads to a group of
165 * CPU cores that share the same L3 cache. This is needed for good multi-
166 * threading performance on AMD Zen CPUs.
167 *
168 * \param upper_thread thread in gallium frontends that also needs to be
169 * pinned.
170 */
171 void
172 util_pin_driver_threads_to_random_L3(struct pipe_context *ctx,
173 thrd_t *upper_thread)
174 {
175 /* If pinning has no effect, don't do anything. */
176 if (util_cpu_caps.nr_cpus == util_cpu_caps.cores_per_L3)
177 return;
178
179 unsigned num_L3_caches = util_cpu_caps.nr_cpus /
180 util_cpu_caps.cores_per_L3;
181
182 /* Get a semi-random number. */
183 int64_t t = os_time_get_nano();
184 unsigned cache = (t ^ (t >> 8) ^ (t >> 16)) % num_L3_caches;
185
186 /* Tell the driver to pin its threads to the selected L3 cache. */
187 if (ctx->set_context_param) {
188 ctx->set_context_param(ctx, PIPE_CONTEXT_PARAM_PIN_THREADS_TO_L3_CACHE,
189 cache);
190 }
191
192 /* Do the same for the upper level thread if there is any (e.g. glthread) */
193 if (upper_thread)
194 util_pin_thread_to_L3(*upper_thread, cache, util_cpu_caps.cores_per_L3);
195
196 /* Optionally pin the application thread to the same L3 to get maximum
197 * performance with glthread on AMD Zen. (this function is only called
198 * with glthread) This is used to estimate and remove the overhead of
199 * Infinity Fabric between L3 caches.
200 */
201 #if defined(HAVE_PTHREAD)
202 if (debug_get_bool_option("pin_app_thread", false))
203 util_pin_thread_to_L3(pthread_self(), cache, util_cpu_caps.cores_per_L3);
204 #endif
205 }
206
207 /* This is a helper for hardware bring-up. Don't remove. */
208 struct pipe_query *
209 util_begin_pipestat_query(struct pipe_context *ctx)
210 {
211 struct pipe_query *q =
212 ctx->create_query(ctx, PIPE_QUERY_PIPELINE_STATISTICS, 0);
213 if (!q)
214 return NULL;
215
216 ctx->begin_query(ctx, q);
217 return q;
218 }
219
220 /* This is a helper for hardware bring-up. Don't remove. */
221 void
222 util_end_pipestat_query(struct pipe_context *ctx, struct pipe_query *q,
223 FILE *f)
224 {
225 static unsigned counter;
226 struct pipe_query_data_pipeline_statistics stats;
227
228 ctx->end_query(ctx, q);
229 ctx->get_query_result(ctx, q, true, (void*)&stats);
230 ctx->destroy_query(ctx, q);
231
232 fprintf(f,
233 "Draw call %u:\n"
234 " ia_vertices = %"PRIu64"\n"
235 " ia_primitives = %"PRIu64"\n"
236 " vs_invocations = %"PRIu64"\n"
237 " gs_invocations = %"PRIu64"\n"
238 " gs_primitives = %"PRIu64"\n"
239 " c_invocations = %"PRIu64"\n"
240 " c_primitives = %"PRIu64"\n"
241 " ps_invocations = %"PRIu64"\n"
242 " hs_invocations = %"PRIu64"\n"
243 " ds_invocations = %"PRIu64"\n"
244 " cs_invocations = %"PRIu64"\n",
245 (unsigned)p_atomic_inc_return(&counter),
246 stats.ia_vertices,
247 stats.ia_primitives,
248 stats.vs_invocations,
249 stats.gs_invocations,
250 stats.gs_primitives,
251 stats.c_invocations,
252 stats.c_primitives,
253 stats.ps_invocations,
254 stats.hs_invocations,
255 stats.ds_invocations,
256 stats.cs_invocations);
257 }
258
259 /* This is a helper for hardware bring-up. Don't remove. */
260 void
261 util_wait_for_idle(struct pipe_context *ctx)
262 {
263 struct pipe_fence_handle *fence = NULL;
264
265 ctx->flush(ctx, &fence, 0);
266 ctx->screen->fence_finish(ctx->screen, NULL, fence, PIPE_TIMEOUT_INFINITE);
267 }
268
269 void
270 util_throttle_init(struct util_throttle *t, uint64_t max_mem_usage)
271 {
272 t->max_mem_usage = max_mem_usage;
273 }
274
275 void
276 util_throttle_deinit(struct pipe_screen *screen, struct util_throttle *t)
277 {
278 for (unsigned i = 0; i < ARRAY_SIZE(t->ring); i++)
279 screen->fence_reference(screen, &t->ring[i].fence, NULL);
280 }
281
282 static uint64_t
283 util_get_throttle_total_memory_usage(struct util_throttle *t)
284 {
285 uint64_t total_usage = 0;
286
287 for (unsigned i = 0; i < ARRAY_SIZE(t->ring); i++)
288 total_usage += t->ring[i].mem_usage;
289 return total_usage;
290 }
291
292 static void util_dump_throttle_ring(struct util_throttle *t)
293 {
294 printf("Throttle:\n");
295 for (unsigned i = 0; i < ARRAY_SIZE(t->ring); i++) {
296 printf(" ring[%u]: fence = %s, mem_usage = %"PRIu64"%s%s\n",
297 i, t->ring[i].fence ? "yes" : " no",
298 t->ring[i].mem_usage,
299 t->flush_index == i ? " [flush]" : "",
300 t->wait_index == i ? " [wait]" : "");
301 }
302 }
303
304 /**
305 * Notify util_throttle that the next operation allocates memory.
306 * util_throttle tracks memory usage and waits for fences until its tracked
307 * memory usage decreases.
308 *
309 * Example:
310 * util_throttle_memory_usage(..., w*h*d*Bpp);
311 * TexSubImage(..., w, h, d, ...);
312 *
313 * This means that TexSubImage can't allocate more memory its maximum limit
314 * set during initialization.
315 */
316 void
317 util_throttle_memory_usage(struct pipe_context *pipe,
318 struct util_throttle *t, uint64_t memory_size)
319 {
320 (void)util_dump_throttle_ring; /* silence warning */
321
322 if (!t->max_mem_usage)
323 return;
324
325 struct pipe_screen *screen = pipe->screen;
326 struct pipe_fence_handle **fence = NULL;
327 unsigned ring_size = ARRAY_SIZE(t->ring);
328 uint64_t total = util_get_throttle_total_memory_usage(t);
329
330 /* If there is not enough memory, walk the list of fences and find
331 * the latest one that we need to wait for.
332 */
333 while (t->wait_index != t->flush_index &&
334 total && total + memory_size > t->max_mem_usage) {
335 assert(t->ring[t->wait_index].fence);
336
337 /* Release an older fence if we need to wait for a newer one. */
338 if (fence)
339 screen->fence_reference(screen, fence, NULL);
340
341 fence = &t->ring[t->wait_index].fence;
342 t->ring[t->wait_index].mem_usage = 0;
343 t->wait_index = (t->wait_index + 1) % ring_size;
344
345 total = util_get_throttle_total_memory_usage(t);
346 }
347
348 /* Wait for the fence to decrease memory usage. */
349 if (fence) {
350 screen->fence_finish(screen, pipe, *fence, PIPE_TIMEOUT_INFINITE);
351 screen->fence_reference(screen, fence, NULL);
352 }
353
354 /* Flush and get a fence if we've exhausted memory usage for the current
355 * slot.
356 */
357 if (t->ring[t->flush_index].mem_usage &&
358 t->ring[t->flush_index].mem_usage + memory_size >
359 t->max_mem_usage / (ring_size / 2)) {
360 struct pipe_fence_handle **fence =
361 &t->ring[t->flush_index].fence;
362
363 /* Expect that the current flush slot doesn't have a fence yet. */
364 assert(!*fence);
365
366 pipe->flush(pipe, fence, PIPE_FLUSH_ASYNC);
367 t->flush_index = (t->flush_index + 1) % ring_size;
368
369 /* Vacate the next slot if it's occupied. This should be rare. */
370 if (t->flush_index == t->wait_index) {
371 struct pipe_fence_handle **fence =
372 &t->ring[t->wait_index].fence;
373
374 t->ring[t->wait_index].mem_usage = 0;
375 t->wait_index = (t->wait_index + 1) % ring_size;
376
377 assert(*fence);
378 screen->fence_finish(screen, pipe, *fence, PIPE_TIMEOUT_INFINITE);
379 screen->fence_reference(screen, fence, NULL);
380 }
381
382 assert(!t->ring[t->flush_index].mem_usage);
383 assert(!t->ring[t->flush_index].fence);
384 }
385
386 t->ring[t->flush_index].mem_usage += memory_size;
387 }