Path: blob/21.2-virgl/src/gallium/drivers/swr/swr_scratch.cpp
4570 views
/****************************************************************************1* Copyright (C) 2015 Intel Corporation. All Rights Reserved.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21***************************************************************************/2223#include "util/u_memory.h"24#include "swr_context.h"25#include "swr_screen.h"26#include "swr_scratch.h"27#include "swr_fence.h"28#include "swr_fence_work.h"29#include "api.h"3031void *32swr_copy_to_scratch_space(struct swr_context *ctx,33struct swr_scratch_space *space,34const void *user_buffer,35unsigned int size)36{37void *ptr;38assert(space);39assert(size);4041/* Allocate enough so that MAX_DRAWS_IN_FLIGHT sets fit. */42uint32_t max_size_in_flight = size * ctx->max_draws_in_flight;4344/* Need to grow space */45if (max_size_in_flight > space->current_size) {46space->current_size = max_size_in_flight;4748if (space->base) {49/* defer delete, use aligned-free, fence finish enforces the defer50* delete will be on the *next* fence */51struct swr_screen *screen = swr_screen(ctx->pipe.screen);52swr_fence_finish(ctx->pipe.screen, NULL, screen->flush_fence, 0);53swr_fence_work_free(screen->flush_fence, space->base, true);54space->base = NULL;55}5657if (!space->base) {58space->base = (uint8_t *)AlignedMalloc(space->current_size,59sizeof(void *));60space->head = (void *)space->base;61}62}6364/* Wrap */65if (((uint8_t *)space->head + size)66>= ((uint8_t *)space->base + space->current_size)) {67space->head = space->base;68}6970ptr = space->head;71space->head = (uint8_t *)space->head + size;7273/* Copy user_buffer to scratch */74if (user_buffer)75memcpy(ptr, user_buffer, size);7677return ptr;78}798081void82swr_init_scratch_buffers(struct swr_context *ctx)83{84struct swr_scratch_buffers *scratch;8586scratch = CALLOC_STRUCT(swr_scratch_buffers);87ctx->scratch = scratch;88}8990void91swr_destroy_scratch_buffers(struct swr_context *ctx)92{93struct swr_scratch_buffers *scratch = ctx->scratch;9495if (scratch) {96AlignedFree(scratch->vs_constants.base);97AlignedFree(scratch->fs_constants.base);98AlignedFree(scratch->gs_constants.base);99AlignedFree(scratch->tcs_constants.base);100AlignedFree(scratch->tes_constants.base);101AlignedFree(scratch->vertex_buffer.base);102AlignedFree(scratch->index_buffer.base);103FREE(scratch);104}105}106107108