Path: blob/21.2-virgl/src/gallium/auxiliary/util/u_helpers.c
4561 views
/**************************************************************************1*2* Copyright 2012 Marek Olšák <[email protected]>3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL THE AUTHORS AND/OR THEIR SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include "util/u_cpu_detect.h"28#include "util/u_helpers.h"29#include "util/u_inlines.h"30#include "util/u_upload_mgr.h"31#include "util/u_thread.h"32#include "util/os_time.h"33#include <inttypes.h>3435/**36* This function is used to copy an array of pipe_vertex_buffer structures,37* while properly referencing the pipe_vertex_buffer::buffer member.38*39* enabled_buffers is updated such that the bits corresponding to the indices40* of disabled buffers are set to 0 and the enabled ones are set to 1.41*42* \sa util_copy_framebuffer_state43*/44void util_set_vertex_buffers_mask(struct pipe_vertex_buffer *dst,45uint32_t *enabled_buffers,46const struct pipe_vertex_buffer *src,47unsigned start_slot, unsigned count,48unsigned unbind_num_trailing_slots,49bool take_ownership)50{51unsigned i;52uint32_t bitmask = 0;5354dst += start_slot;5556*enabled_buffers &= ~u_bit_consecutive(start_slot, count);5758if (src) {59for (i = 0; i < count; i++) {60if (src[i].buffer.resource)61bitmask |= 1 << i;6263pipe_vertex_buffer_unreference(&dst[i]);6465if (!take_ownership && !src[i].is_user_buffer)66pipe_resource_reference(&dst[i].buffer.resource, src[i].buffer.resource);67}6869/* Copy over the other members of pipe_vertex_buffer. */70memcpy(dst, src, count * sizeof(struct pipe_vertex_buffer));7172*enabled_buffers |= bitmask << start_slot;73}74else {75/* Unreference the buffers. */76for (i = 0; i < count; i++)77pipe_vertex_buffer_unreference(&dst[i]);78}7980for (i = 0; i < unbind_num_trailing_slots; i++)81pipe_vertex_buffer_unreference(&dst[count + i]);82}8384/**85* Same as util_set_vertex_buffers_mask, but it only returns the number86* of bound buffers.87*/88void util_set_vertex_buffers_count(struct pipe_vertex_buffer *dst,89unsigned *dst_count,90const struct pipe_vertex_buffer *src,91unsigned start_slot, unsigned count,92unsigned unbind_num_trailing_slots,93bool take_ownership)94{95unsigned i;96uint32_t enabled_buffers = 0;9798for (i = 0; i < *dst_count; i++) {99if (dst[i].buffer.resource)100enabled_buffers |= (1ull << i);101}102103util_set_vertex_buffers_mask(dst, &enabled_buffers, src, start_slot,104count, unbind_num_trailing_slots,105take_ownership);106107*dst_count = util_last_bit(enabled_buffers);108}109110/**111* This function is used to copy an array of pipe_shader_buffer structures,112* while properly referencing the pipe_shader_buffer::buffer member.113*114* \sa util_set_vertex_buffer_mask115*/116void util_set_shader_buffers_mask(struct pipe_shader_buffer *dst,117uint32_t *enabled_buffers,118const struct pipe_shader_buffer *src,119unsigned start_slot, unsigned count)120{121unsigned i;122123dst += start_slot;124125if (src) {126for (i = 0; i < count; i++) {127pipe_resource_reference(&dst[i].buffer, src[i].buffer);128129if (src[i].buffer)130*enabled_buffers |= (1ull << (start_slot + i));131else132*enabled_buffers &= ~(1ull << (start_slot + i));133}134135/* Copy over the other members of pipe_shader_buffer. */136memcpy(dst, src, count * sizeof(struct pipe_shader_buffer));137}138else {139/* Unreference the buffers. */140for (i = 0; i < count; i++)141pipe_resource_reference(&dst[i].buffer, NULL);142143*enabled_buffers &= ~(((1ull << count) - 1) << start_slot);144}145}146147/**148* Given a user index buffer, save the structure to "saved", and upload it.149*/150bool151util_upload_index_buffer(struct pipe_context *pipe,152const struct pipe_draw_info *info,153const struct pipe_draw_start_count_bias *draw,154struct pipe_resource **out_buffer,155unsigned *out_offset, unsigned alignment)156{157unsigned start_offset = draw->start * info->index_size;158159u_upload_data(pipe->stream_uploader, start_offset,160draw->count * info->index_size, alignment,161(char*)info->index.user + start_offset,162out_offset, out_buffer);163u_upload_unmap(pipe->stream_uploader);164*out_offset -= start_offset;165return *out_buffer != NULL;166}167168/* This is a helper for hardware bring-up. Don't remove. */169struct pipe_query *170util_begin_pipestat_query(struct pipe_context *ctx)171{172struct pipe_query *q =173ctx->create_query(ctx, PIPE_QUERY_PIPELINE_STATISTICS, 0);174if (!q)175return NULL;176177ctx->begin_query(ctx, q);178return q;179}180181/* This is a helper for hardware bring-up. Don't remove. */182void183util_end_pipestat_query(struct pipe_context *ctx, struct pipe_query *q,184FILE *f)185{186static unsigned counter;187struct pipe_query_data_pipeline_statistics stats;188189ctx->end_query(ctx, q);190ctx->get_query_result(ctx, q, true, (void*)&stats);191ctx->destroy_query(ctx, q);192193fprintf(f,194"Draw call %u:\n"195" ia_vertices = %"PRIu64"\n"196" ia_primitives = %"PRIu64"\n"197" vs_invocations = %"PRIu64"\n"198" gs_invocations = %"PRIu64"\n"199" gs_primitives = %"PRIu64"\n"200" c_invocations = %"PRIu64"\n"201" c_primitives = %"PRIu64"\n"202" ps_invocations = %"PRIu64"\n"203" hs_invocations = %"PRIu64"\n"204" ds_invocations = %"PRIu64"\n"205" cs_invocations = %"PRIu64"\n",206(unsigned)p_atomic_inc_return(&counter),207stats.ia_vertices,208stats.ia_primitives,209stats.vs_invocations,210stats.gs_invocations,211stats.gs_primitives,212stats.c_invocations,213stats.c_primitives,214stats.ps_invocations,215stats.hs_invocations,216stats.ds_invocations,217stats.cs_invocations);218}219220/* This is a helper for profiling. Don't remove. */221struct pipe_query *222util_begin_time_query(struct pipe_context *ctx)223{224struct pipe_query *q =225ctx->create_query(ctx, PIPE_QUERY_TIME_ELAPSED, 0);226if (!q)227return NULL;228229ctx->begin_query(ctx, q);230return q;231}232233/* This is a helper for profiling. Don't remove. */234void235util_end_time_query(struct pipe_context *ctx, struct pipe_query *q, FILE *f,236const char *name)237{238union pipe_query_result result;239240ctx->end_query(ctx, q);241ctx->get_query_result(ctx, q, true, &result);242ctx->destroy_query(ctx, q);243244fprintf(f, "Time elapsed: %s - %"PRIu64".%u us\n", name, result.u64 / 1000, (unsigned)(result.u64 % 1000) / 100);245}246247/* This is a helper for hardware bring-up. Don't remove. */248void249util_wait_for_idle(struct pipe_context *ctx)250{251struct pipe_fence_handle *fence = NULL;252253ctx->flush(ctx, &fence, 0);254ctx->screen->fence_finish(ctx->screen, NULL, fence, PIPE_TIMEOUT_INFINITE);255}256257void258util_throttle_init(struct util_throttle *t, uint64_t max_mem_usage)259{260t->max_mem_usage = max_mem_usage;261}262263void264util_throttle_deinit(struct pipe_screen *screen, struct util_throttle *t)265{266for (unsigned i = 0; i < ARRAY_SIZE(t->ring); i++)267screen->fence_reference(screen, &t->ring[i].fence, NULL);268}269270static uint64_t271util_get_throttle_total_memory_usage(struct util_throttle *t)272{273uint64_t total_usage = 0;274275for (unsigned i = 0; i < ARRAY_SIZE(t->ring); i++)276total_usage += t->ring[i].mem_usage;277return total_usage;278}279280static void util_dump_throttle_ring(struct util_throttle *t)281{282printf("Throttle:\n");283for (unsigned i = 0; i < ARRAY_SIZE(t->ring); i++) {284printf(" ring[%u]: fence = %s, mem_usage = %"PRIu64"%s%s\n",285i, t->ring[i].fence ? "yes" : " no",286t->ring[i].mem_usage,287t->flush_index == i ? " [flush]" : "",288t->wait_index == i ? " [wait]" : "");289}290}291292/**293* Notify util_throttle that the next operation allocates memory.294* util_throttle tracks memory usage and waits for fences until its tracked295* memory usage decreases.296*297* Example:298* util_throttle_memory_usage(..., w*h*d*Bpp);299* TexSubImage(..., w, h, d, ...);300*301* This means that TexSubImage can't allocate more memory its maximum limit302* set during initialization.303*/304void305util_throttle_memory_usage(struct pipe_context *pipe,306struct util_throttle *t, uint64_t memory_size)307{308(void)util_dump_throttle_ring; /* silence warning */309310if (!t->max_mem_usage)311return;312313struct pipe_screen *screen = pipe->screen;314struct pipe_fence_handle **fence = NULL;315unsigned ring_size = ARRAY_SIZE(t->ring);316uint64_t total = util_get_throttle_total_memory_usage(t);317318/* If there is not enough memory, walk the list of fences and find319* the latest one that we need to wait for.320*/321while (t->wait_index != t->flush_index &&322total && total + memory_size > t->max_mem_usage) {323assert(t->ring[t->wait_index].fence);324325/* Release an older fence if we need to wait for a newer one. */326if (fence)327screen->fence_reference(screen, fence, NULL);328329fence = &t->ring[t->wait_index].fence;330t->ring[t->wait_index].mem_usage = 0;331t->wait_index = (t->wait_index + 1) % ring_size;332333total = util_get_throttle_total_memory_usage(t);334}335336/* Wait for the fence to decrease memory usage. */337if (fence) {338screen->fence_finish(screen, pipe, *fence, PIPE_TIMEOUT_INFINITE);339screen->fence_reference(screen, fence, NULL);340}341342/* Flush and get a fence if we've exhausted memory usage for the current343* slot.344*/345if (t->ring[t->flush_index].mem_usage &&346t->ring[t->flush_index].mem_usage + memory_size >347t->max_mem_usage / (ring_size / 2)) {348struct pipe_fence_handle **fence =349&t->ring[t->flush_index].fence;350351/* Expect that the current flush slot doesn't have a fence yet. */352assert(!*fence);353354pipe->flush(pipe, fence, PIPE_FLUSH_ASYNC);355t->flush_index = (t->flush_index + 1) % ring_size;356357/* Vacate the next slot if it's occupied. This should be rare. */358if (t->flush_index == t->wait_index) {359struct pipe_fence_handle **fence =360&t->ring[t->wait_index].fence;361362t->ring[t->wait_index].mem_usage = 0;363t->wait_index = (t->wait_index + 1) % ring_size;364365assert(*fence);366screen->fence_finish(screen, pipe, *fence, PIPE_TIMEOUT_INFINITE);367screen->fence_reference(screen, fence, NULL);368}369370assert(!t->ring[t->flush_index].mem_usage);371assert(!t->ring[t->flush_index].fence);372}373374t->ring[t->flush_index].mem_usage += memory_size;375}376377bool378util_lower_clearsize_to_dword(const void *clearValue, int *clearValueSize, uint32_t *clamped)379{380/* Reduce a large clear value size if possible. */381if (*clearValueSize > 4) {382bool clear_dword_duplicated = true;383const uint32_t *clear_value = clearValue;384385/* See if we can lower large fills to dword fills. */386for (unsigned i = 1; i < *clearValueSize / 4; i++) {387if (clear_value[0] != clear_value[i]) {388clear_dword_duplicated = false;389break;390}391}392if (clear_dword_duplicated) {393*clamped = *clear_value;394*clearValueSize = 4;395}396return clear_dword_duplicated;397}398399/* Expand a small clear value size. */400if (*clearValueSize <= 2) {401if (*clearValueSize == 1) {402*clamped = *(uint8_t *)clearValue;403*clamped |=404(*clamped << 8) | (*clamped << 16) | (*clamped << 24);405} else {406*clamped = *(uint16_t *)clearValue;407*clamped |= *clamped << 16;408}409*clearValueSize = 4;410return true;411}412return false;413}414415416