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