4639c1b03c38e90afe90a131172048c2ecfa4b63
[mesa.git] / src / gallium / drivers / panfrost / pan_compute.c
1 /*
2 * Copyright (C) 2019 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * 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 THE
21 * SOFTWARE.
22 *
23 * Authors (Collabora):
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25 *
26 */
27
28 #include "pan_context.h"
29 #include "util/u_memory.h"
30
31 /* Compute CSOs are tracked like graphics shader CSOs, but are
32 * considerably simpler. We do not implement multiple
33 * variants/keying. So the CSO create function just goes ahead and
34 * compiles the thing. */
35
36 static void *
37 panfrost_create_compute_state(
38 struct pipe_context *pctx,
39 const struct pipe_compute_state *cso)
40 {
41 struct panfrost_context *ctx = pan_context(pctx);
42
43 struct panfrost_shader_variants *so = CALLOC_STRUCT(panfrost_shader_variants);
44 so->cbase = *cso;
45 so->is_compute = true;
46
47 struct panfrost_shader_state *v = &so->variants[0];
48
49 so->variant_count = 1;
50 so->active_variant = 0;
51
52 v->tripipe = malloc(sizeof(struct mali_shader_meta));
53
54 panfrost_shader_compile(ctx, v->tripipe,
55 cso->ir_type, cso->prog,
56 MESA_SHADER_COMPUTE, v, NULL);
57
58
59
60 return so;
61 }
62
63 static void
64 panfrost_bind_compute_state(struct pipe_context *pipe, void *cso)
65 {
66 struct panfrost_context *ctx = pan_context(pipe);
67
68 struct panfrost_shader_variants *variants =
69 (struct panfrost_shader_variants *) cso;
70
71 ctx->shader[PIPE_SHADER_COMPUTE] = variants;
72 }
73
74 static void
75 panfrost_delete_compute_state(struct pipe_context *pipe, void *cso)
76 {
77 free(cso);
78 }
79
80 /* Launch grid is the compute equivalent of draw_vbo, so in this routine, we
81 * construct the COMPUTE job and some of its payload.
82 */
83
84 static void
85 panfrost_launch_grid(struct pipe_context *pipe,
86 const struct pipe_grid_info *info)
87 {
88 struct panfrost_context *ctx = pan_context(pipe);
89
90 /* TODO: Do we want a special compute-only batch? */
91 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
92
93 ctx->compute_grid = info;
94
95 struct mali_job_descriptor_header job = {
96 .job_type = JOB_TYPE_COMPUTE,
97 .job_descriptor_size = 1,
98 .job_barrier = 1
99 };
100
101 /* TODO: Stub */
102 struct midgard_payload_vertex_tiler *payload = &ctx->payloads[PIPE_SHADER_COMPUTE];
103
104 panfrost_emit_for_draw(ctx, false);
105
106 /* Compute jobs have a "compute FBD". It's not a real framebuffer
107 * descriptor - there is no framebuffer - but it takes the place of
108 * one. As far as I can tell, it's actually the beginning of a
109 * single-render-target framebuffer descriptor with almost everything
110 * zeroed out.
111 */
112 struct mali_compute_fbd compute_fbd = {
113 .unknown1 = {
114 0, 0x1F, 0, 0, 0, 0, 0, 0
115 }
116 };
117
118 payload->postfix.framebuffer =
119 panfrost_upload_transient(batch, &compute_fbd, sizeof(compute_fbd));
120
121 /* Invoke according to the grid info */
122
123 panfrost_pack_work_groups_compute(&payload->prefix,
124 info->grid[0], info->grid[1], info->grid[2],
125 info->block[0], info->block[1], info->block[2], false);
126
127 /* Upload the payload */
128
129 struct panfrost_transfer transfer = panfrost_allocate_transient(batch, sizeof(job) + sizeof(*payload));
130 memcpy(transfer.cpu, &job, sizeof(job));
131 memcpy(transfer.cpu + sizeof(job), payload, sizeof(*payload));
132
133 /* Queue the job */
134 panfrost_scoreboard_queue_compute_job(batch, transfer);
135
136 panfrost_flush(pipe, NULL, PIPE_FLUSH_END_OF_FRAME);
137 }
138
139 void
140 panfrost_compute_context_init(struct pipe_context *pctx)
141 {
142 pctx->create_compute_state = panfrost_create_compute_state;
143 pctx->bind_compute_state = panfrost_bind_compute_state;
144 pctx->delete_compute_state = panfrost_delete_compute_state;
145
146 pctx->launch_grid = panfrost_launch_grid;
147 }
148
149