20a9a0904a624257fb0f2277edff0ac8706f22f2
[mesa.git] / src / gallium / drivers / panfrost / pan_compute.c
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 "util/u_memory.h"
31 #include "nir_serialize.h"
32
33 /* Compute CSOs are tracked like graphics shader CSOs, but are
34 * considerably simpler. We do not implement multiple
35 * variants/keying. So the CSO create function just goes ahead and
36 * compiles the thing. */
37
38 static void *
39 panfrost_create_compute_state(
40 struct pipe_context *pctx,
41 const struct pipe_compute_state *cso)
42 {
43 struct panfrost_context *ctx = pan_context(pctx);
44
45 struct panfrost_shader_variants *so = CALLOC_STRUCT(panfrost_shader_variants);
46 so->cbase = *cso;
47 so->is_compute = true;
48
49 struct panfrost_shader_state *v = &so->variants[0];
50
51 so->variant_count = 1;
52 so->active_variant = 0;
53
54 v->tripipe = malloc(sizeof(struct mali_shader_meta));
55
56 if (cso->ir_type == PIPE_SHADER_IR_NIR_SERIALIZED) {
57 struct blob_reader reader;
58 const struct pipe_binary_program_header *hdr = cso->prog;
59
60 blob_reader_init(&reader, hdr->blob, hdr->num_bytes);
61 so->cbase.prog = nir_deserialize(NULL, &midgard_nir_options, &reader);
62 so->cbase.ir_type = PIPE_SHADER_IR_NIR;
63 }
64
65 panfrost_shader_compile(ctx, v->tripipe,
66 so->cbase.ir_type, so->cbase.prog,
67 MESA_SHADER_COMPUTE, v, NULL);
68
69 return so;
70 }
71
72 static void
73 panfrost_bind_compute_state(struct pipe_context *pipe, void *cso)
74 {
75 struct panfrost_context *ctx = pan_context(pipe);
76
77 struct panfrost_shader_variants *variants =
78 (struct panfrost_shader_variants *) cso;
79
80 ctx->shader[PIPE_SHADER_COMPUTE] = variants;
81 }
82
83 static void
84 panfrost_delete_compute_state(struct pipe_context *pipe, void *cso)
85 {
86 free(cso);
87 }
88
89 /* Launch grid is the compute equivalent of draw_vbo, so in this routine, we
90 * construct the COMPUTE job and some of its payload.
91 */
92
93 static void
94 panfrost_launch_grid(struct pipe_context *pipe,
95 const struct pipe_grid_info *info)
96 {
97 struct panfrost_context *ctx = pan_context(pipe);
98
99 /* TODO: Do we want a special compute-only batch? */
100 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
101
102 ctx->compute_grid = info;
103
104 struct mali_job_descriptor_header job = {
105 .job_type = JOB_TYPE_COMPUTE,
106 .job_descriptor_size = 1,
107 .job_barrier = 1
108 };
109
110 /* TODO: Stub */
111 struct midgard_payload_vertex_tiler *payload = &ctx->payloads[PIPE_SHADER_COMPUTE];
112
113 panfrost_emit_for_draw(ctx, false);
114
115 /* Compute jobs have a "compute FBD". It's not a real framebuffer
116 * descriptor - there is no framebuffer - but it takes the place of
117 * one. As far as I can tell, it's actually the beginning of a
118 * single-render-target framebuffer descriptor with almost everything
119 * zeroed out.
120 */
121 struct mali_compute_fbd compute_fbd = {
122 .unknown1 = {
123 0, 0x1F, 0, 0, 0, 0, 0, 0
124 }
125 };
126
127 payload->postfix.framebuffer =
128 panfrost_upload_transient(batch, &compute_fbd, sizeof(compute_fbd));
129
130 /* Invoke according to the grid info */
131
132 panfrost_pack_work_groups_compute(&payload->prefix,
133 info->grid[0], info->grid[1], info->grid[2],
134 info->block[0], info->block[1], info->block[2], false);
135
136 /* Upload the payload */
137
138 struct panfrost_transfer transfer = panfrost_allocate_transient(batch, sizeof(job) + sizeof(*payload));
139 memcpy(transfer.cpu, &job, sizeof(job));
140 memcpy(transfer.cpu + sizeof(job), payload, sizeof(*payload));
141
142 /* Queue the job */
143 panfrost_scoreboard_queue_compute_job(batch, transfer);
144
145 panfrost_flush_all_batches(ctx, true);
146 }
147
148 void
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->launch_grid = panfrost_launch_grid;
156 }
157
158