Path: blob/21.2-virgl/src/gallium/drivers/panfrost/pan_compute.c
4570 views
/*1* Copyright (C) 2019 Collabora, Ltd.2* Copyright (C) 2019 Red Hat Inc.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*23* Authors (Collabora):24* Alyssa Rosenzweig <[email protected]>25*26*/2728#include "pan_context.h"29#include "panfrost-quirks.h"30#include "pan_bo.h"31#include "pan_shader.h"32#include "util/u_memory.h"33#include "nir_serialize.h"3435/* Compute CSOs are tracked like graphics shader CSOs, but are36* considerably simpler. We do not implement multiple37* variants/keying. So the CSO create function just goes ahead and38* compiles the thing. */3940static void *41panfrost_create_compute_state(42struct pipe_context *pctx,43const struct pipe_compute_state *cso)44{45struct panfrost_context *ctx = pan_context(pctx);46struct panfrost_device *dev = pan_device(pctx->screen);4748struct panfrost_shader_variants *so = CALLOC_STRUCT(panfrost_shader_variants);49so->cbase = *cso;50so->is_compute = true;5152struct panfrost_shader_state *v = calloc(1, sizeof(*v));53so->variants = v;5455so->variant_count = 1;56so->active_variant = 0;5758if (cso->ir_type == PIPE_SHADER_IR_NIR_SERIALIZED) {59struct blob_reader reader;60const struct pipe_binary_program_header *hdr = cso->prog;6162blob_reader_init(&reader, hdr->blob, hdr->num_bytes);6364const struct nir_shader_compiler_options *options =65pan_shader_get_compiler_options(dev);6667so->cbase.prog = nir_deserialize(NULL, options, &reader);68so->cbase.ir_type = PIPE_SHADER_IR_NIR;69}7071panfrost_shader_compile(pctx->screen, &ctx->shaders, &ctx->descs,72so->cbase.ir_type, so->cbase.prog, MESA_SHADER_COMPUTE,73v);7475return so;76}7778static void79panfrost_bind_compute_state(struct pipe_context *pipe, void *cso)80{81struct panfrost_context *ctx = pan_context(pipe);82ctx->shader[PIPE_SHADER_COMPUTE] = cso;83}8485static void86panfrost_delete_compute_state(struct pipe_context *pipe, void *cso)87{88free(cso);89}9091static void92panfrost_set_compute_resources(struct pipe_context *pctx,93unsigned start, unsigned count,94struct pipe_surface **resources)95{96/* TODO */97}9899static void100panfrost_set_global_binding(struct pipe_context *pctx,101unsigned first, unsigned count,102struct pipe_resource **resources,103uint32_t **handles)104{105if (!resources)106return;107108struct panfrost_context *ctx = pan_context(pctx);109struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);110111for (unsigned i = first; i < first + count; ++i) {112struct panfrost_resource *rsrc = pan_resource(resources[i]);113panfrost_batch_write_rsrc(batch, rsrc, PIPE_SHADER_COMPUTE);114115util_range_add(&rsrc->base, &rsrc->valid_buffer_range,1160, rsrc->base.width0);117118/* The handle points to uint32_t, but space is allocated for 64 bits */119memcpy(handles[i], &rsrc->image.data.bo->ptr.gpu, sizeof(mali_ptr));120}121}122123static void124panfrost_memory_barrier(struct pipe_context *pctx, unsigned flags)125{126/* TODO: Be smart and only flush the minimum needed, maybe emitting a127* cache flush job if that would help */128panfrost_flush_all_batches(pan_context(pctx));129}130131void132panfrost_compute_context_init(struct pipe_context *pctx)133{134pctx->create_compute_state = panfrost_create_compute_state;135pctx->bind_compute_state = panfrost_bind_compute_state;136pctx->delete_compute_state = panfrost_delete_compute_state;137138pctx->set_compute_resources = panfrost_set_compute_resources;139pctx->set_global_binding = panfrost_set_global_binding;140141pctx->memory_barrier = panfrost_memory_barrier;142}143144145