2 * Copyright (C) 2019 Collabora, Ltd.
3 * Copyright (C) 2019 Red Hat Inc.
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:
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
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
24 * Authors (Collabora):
25 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
29 #include "pan_context.h"
30 #include "util/u_memory.h"
31 #include "nir_serialize.h"
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. */
39 panfrost_create_compute_state(
40 struct pipe_context
*pctx
,
41 const struct pipe_compute_state
*cso
)
43 struct panfrost_context
*ctx
= pan_context(pctx
);
45 struct panfrost_shader_variants
*so
= CALLOC_STRUCT(panfrost_shader_variants
);
47 so
->is_compute
= true;
49 struct panfrost_shader_state
*v
= &so
->variants
[0];
51 so
->variant_count
= 1;
52 so
->active_variant
= 0;
54 v
->tripipe
= malloc(sizeof(struct mali_shader_meta
));
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
;
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
;
65 panfrost_shader_compile(ctx
, v
->tripipe
,
66 so
->cbase
.ir_type
, so
->cbase
.prog
,
67 MESA_SHADER_COMPUTE
, v
, NULL
);
73 panfrost_bind_compute_state(struct pipe_context
*pipe
, void *cso
)
75 struct panfrost_context
*ctx
= pan_context(pipe
);
77 struct panfrost_shader_variants
*variants
=
78 (struct panfrost_shader_variants
*) cso
;
80 ctx
->shader
[PIPE_SHADER_COMPUTE
] = variants
;
84 panfrost_delete_compute_state(struct pipe_context
*pipe
, void *cso
)
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.
94 panfrost_launch_grid(struct pipe_context
*pipe
,
95 const struct pipe_grid_info
*info
)
97 struct panfrost_context
*ctx
= pan_context(pipe
);
99 /* TODO: Do we want a special compute-only batch? */
100 struct panfrost_batch
*batch
= panfrost_get_batch_for_fbo(ctx
);
102 ctx
->compute_grid
= info
;
104 struct mali_job_descriptor_header job
= {
105 .job_type
= JOB_TYPE_COMPUTE
,
106 .job_descriptor_size
= 1,
111 struct midgard_payload_vertex_tiler
*payload
= &ctx
->payloads
[PIPE_SHADER_COMPUTE
];
113 panfrost_emit_for_draw(ctx
, false);
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
121 struct mali_compute_fbd compute_fbd
= {
123 0, 0x1F, 0, 0, 0, 0, 0, 0
127 payload
->postfix
.framebuffer
=
128 panfrost_upload_transient(batch
, &compute_fbd
, sizeof(compute_fbd
));
130 /* Invoke according to the grid info */
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);
136 /* Upload the payload */
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
));
143 panfrost_scoreboard_queue_compute_job(batch
, transfer
);
145 panfrost_flush_all_batches(ctx
, true);
149 panfrost_compute_context_init(struct pipe_context
*pctx
)
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
;
155 pctx
->launch_grid
= panfrost_launch_grid
;