1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2019 Collabora, Ltd.
3bf215546Sopenharmony_ci * Copyright (C) 2019 Red Hat Inc.
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
8bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
10bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
14bf215546Sopenharmony_ci * Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22bf215546Sopenharmony_ci * SOFTWARE.
23bf215546Sopenharmony_ci *
24bf215546Sopenharmony_ci * Authors (Collabora):
25bf215546Sopenharmony_ci *   Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
26bf215546Sopenharmony_ci *
27bf215546Sopenharmony_ci */
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "pan_context.h"
30bf215546Sopenharmony_ci#include "pan_bo.h"
31bf215546Sopenharmony_ci#include "pan_shader.h"
32bf215546Sopenharmony_ci#include "util/u_memory.h"
33bf215546Sopenharmony_ci#include "nir_serialize.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci/* Compute CSOs are tracked like graphics shader CSOs, but are
36bf215546Sopenharmony_ci * considerably simpler. We do not implement multiple
37bf215546Sopenharmony_ci * variants/keying. So the CSO create function just goes ahead and
38bf215546Sopenharmony_ci * compiles the thing. */
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_cistatic void *
41bf215546Sopenharmony_cipanfrost_create_compute_state(
42bf215546Sopenharmony_ci        struct pipe_context *pctx,
43bf215546Sopenharmony_ci        const struct pipe_compute_state *cso)
44bf215546Sopenharmony_ci{
45bf215546Sopenharmony_ci        struct panfrost_context *ctx = pan_context(pctx);
46bf215546Sopenharmony_ci        struct panfrost_screen *screen = pan_screen(pctx->screen);
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci        struct panfrost_shader_variants *so = CALLOC_STRUCT(panfrost_shader_variants);
49bf215546Sopenharmony_ci        so->req_input_mem = cso->req_input_mem;
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci        struct panfrost_shader_state *v = calloc(1, sizeof(*v));
52bf215546Sopenharmony_ci        so->variants = v;
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci        so->variant_count = 1;
55bf215546Sopenharmony_ci        so->active_variant = 0;
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci        nir_shader *deserialized = NULL;
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci        if (cso->ir_type == PIPE_SHADER_IR_NIR_SERIALIZED) {
60bf215546Sopenharmony_ci                struct blob_reader reader;
61bf215546Sopenharmony_ci                const struct pipe_binary_program_header *hdr = cso->prog;
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci                blob_reader_init(&reader, hdr->blob, hdr->num_bytes);
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci                const struct nir_shader_compiler_options *options =
66bf215546Sopenharmony_ci                        screen->vtbl.get_compiler_options();
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci                deserialized = nir_deserialize(NULL, options, &reader);
69bf215546Sopenharmony_ci        } else {
70bf215546Sopenharmony_ci                assert(cso->ir_type == PIPE_SHADER_IR_NIR && "TGSI kernels unsupported");
71bf215546Sopenharmony_ci        }
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci        panfrost_shader_compile(pctx->screen, &ctx->shaders, &ctx->descs,
74bf215546Sopenharmony_ci                                deserialized ?: cso->prog, v);
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci        /* There are no variants so we won't need the NIR again */
77bf215546Sopenharmony_ci        ralloc_free(deserialized);
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci        return so;
80bf215546Sopenharmony_ci}
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_cistatic void
83bf215546Sopenharmony_cipanfrost_bind_compute_state(struct pipe_context *pipe, void *cso)
84bf215546Sopenharmony_ci{
85bf215546Sopenharmony_ci        struct panfrost_context *ctx = pan_context(pipe);
86bf215546Sopenharmony_ci        ctx->shader[PIPE_SHADER_COMPUTE] = cso;
87bf215546Sopenharmony_ci}
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_cistatic void
90bf215546Sopenharmony_cipanfrost_delete_compute_state(struct pipe_context *pipe, void *cso)
91bf215546Sopenharmony_ci{
92bf215546Sopenharmony_ci        struct panfrost_shader_variants *so =
93bf215546Sopenharmony_ci                (struct panfrost_shader_variants *)cso;
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci        free(so->variants);
96bf215546Sopenharmony_ci        free(cso);
97bf215546Sopenharmony_ci}
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_cistatic void
100bf215546Sopenharmony_cipanfrost_set_compute_resources(struct pipe_context *pctx,
101bf215546Sopenharmony_ci                         unsigned start, unsigned count,
102bf215546Sopenharmony_ci                         struct pipe_surface **resources)
103bf215546Sopenharmony_ci{
104bf215546Sopenharmony_ci        /* TODO */
105bf215546Sopenharmony_ci}
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_cistatic void
108bf215546Sopenharmony_cipanfrost_set_global_binding(struct pipe_context *pctx,
109bf215546Sopenharmony_ci                      unsigned first, unsigned count,
110bf215546Sopenharmony_ci                      struct pipe_resource **resources,
111bf215546Sopenharmony_ci                      uint32_t **handles)
112bf215546Sopenharmony_ci{
113bf215546Sopenharmony_ci        if (!resources)
114bf215546Sopenharmony_ci                return;
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci        struct panfrost_context *ctx = pan_context(pctx);
117bf215546Sopenharmony_ci        struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci        for (unsigned i = first; i < first + count; ++i) {
120bf215546Sopenharmony_ci                struct panfrost_resource *rsrc = pan_resource(resources[i]);
121bf215546Sopenharmony_ci                panfrost_batch_write_rsrc(batch, rsrc, PIPE_SHADER_COMPUTE);
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci                util_range_add(&rsrc->base, &rsrc->valid_buffer_range,
124bf215546Sopenharmony_ci                                0, rsrc->base.width0);
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci                /* The handle points to uint32_t, but space is allocated for 64
127bf215546Sopenharmony_ci                 * bits. We need to respect the offset passed in. This interface
128bf215546Sopenharmony_ci                 * is so bad.
129bf215546Sopenharmony_ci                 */
130bf215546Sopenharmony_ci                mali_ptr addr = 0;
131bf215546Sopenharmony_ci                static_assert(sizeof(addr) == 8, "size out of sync");
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci                memcpy(&addr, handles[i], sizeof(addr));
134bf215546Sopenharmony_ci                addr += rsrc->image.data.bo->ptr.gpu;
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci                memcpy(handles[i], &addr, sizeof(addr));
137bf215546Sopenharmony_ci        }
138bf215546Sopenharmony_ci}
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_cistatic void
141bf215546Sopenharmony_cipanfrost_memory_barrier(struct pipe_context *pctx, unsigned flags)
142bf215546Sopenharmony_ci{
143bf215546Sopenharmony_ci        /* TODO: Be smart and only flush the minimum needed, maybe emitting a
144bf215546Sopenharmony_ci         * cache flush job if that would help */
145bf215546Sopenharmony_ci        panfrost_flush_all_batches(pan_context(pctx), "Memory barrier");
146bf215546Sopenharmony_ci}
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_civoid
149bf215546Sopenharmony_cipanfrost_compute_context_init(struct pipe_context *pctx)
150bf215546Sopenharmony_ci{
151bf215546Sopenharmony_ci        pctx->create_compute_state = panfrost_create_compute_state;
152bf215546Sopenharmony_ci        pctx->bind_compute_state = panfrost_bind_compute_state;
153bf215546Sopenharmony_ci        pctx->delete_compute_state = panfrost_delete_compute_state;
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_ci        pctx->set_compute_resources = panfrost_set_compute_resources;
156bf215546Sopenharmony_ci        pctx->set_global_binding = panfrost_set_global_binding;
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ci        pctx->memory_barrier = panfrost_memory_barrier;
159bf215546Sopenharmony_ci}
160