nir: allow passing alpha-ref state to lowering-code
[mesa.git] / src / gallium / drivers / panfrost / pan_assemble.c
1 /*
2 * © Copyright 2018 Alyssa Rosenzweig
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 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "pan_bo.h"
29 #include "pan_context.h"
30
31 #include "compiler/nir/nir.h"
32 #include "nir/tgsi_to_nir.h"
33 #include "midgard/midgard_compile.h"
34 #include "util/u_dynarray.h"
35
36 #include "tgsi/tgsi_dump.h"
37
38 void
39 panfrost_shader_compile(
40 struct panfrost_context *ctx,
41 struct mali_shader_meta *meta,
42 enum pipe_shader_ir ir_type,
43 const void *ir,
44 gl_shader_stage stage,
45 struct panfrost_shader_state *state,
46 uint64_t *outputs_written)
47 {
48 struct panfrost_screen *screen = pan_screen(ctx->base.screen);
49 uint8_t *dst;
50
51 nir_shader *s;
52
53 if (ir_type == PIPE_SHADER_IR_NIR) {
54 s = nir_shader_clone(NULL, ir);
55 } else {
56 assert (ir_type == PIPE_SHADER_IR_TGSI);
57 s = tgsi_to_nir(ir, ctx->base.screen);
58 }
59
60 s->info.stage = stage;
61
62 if (stage == MESA_SHADER_FRAGMENT) {
63 /* Inject the alpha test now if we need to */
64
65 if (state->alpha_state.enabled) {
66 NIR_PASS_V(s, nir_lower_alpha_test,
67 state->alpha_state.func, false, NULL);
68 }
69 }
70
71 /* Call out to Midgard compiler given the above NIR */
72
73 midgard_program program = {
74 .alpha_ref = state->alpha_state.ref_value
75 };
76
77 midgard_compile_shader_nir(&ctx->compiler, s, &program, false);
78
79 /* Prepare the compiled binary for upload */
80 int size = program.compiled.size;
81 dst = program.compiled.data;
82
83 /* Upload the shader. The lookahead tag is ORed on as a tagged pointer.
84 * I bet someone just thought that would be a cute pun. At least,
85 * that's how I'd do it. */
86
87 state->bo = panfrost_bo_create(screen, size, PAN_BO_EXECUTE);
88 memcpy(state->bo->cpu, dst, size);
89 meta->shader = state->bo->gpu | program.first_tag;
90
91 util_dynarray_fini(&program.compiled);
92
93 /* Sysvals are prepended */
94 program.uniform_count += program.sysval_count;
95 state->sysval_count = program.sysval_count;
96 memcpy(state->sysval, program.sysvals, sizeof(state->sysval[0]) * state->sysval_count);
97
98 meta->midgard1.uniform_count = MIN2(program.uniform_count, program.uniform_cutoff);
99 meta->midgard1.work_count = program.work_register_count;
100
101 switch (stage) {
102 case MESA_SHADER_VERTEX:
103 meta->attribute_count = util_bitcount64(s->info.inputs_read);
104 meta->varying_count = util_bitcount64(s->info.outputs_written);
105 break;
106 case MESA_SHADER_FRAGMENT:
107 meta->attribute_count = 0;
108 meta->varying_count = util_bitcount64(s->info.inputs_read);
109 break;
110 case MESA_SHADER_COMPUTE:
111 /* TODO: images */
112 meta->attribute_count = 0;
113 meta->varying_count = 0;
114 break;
115 default:
116 unreachable("Unknown shader state");
117 }
118
119 state->can_discard = s->info.fs.uses_discard;
120 state->writes_point_size = program.writes_point_size;
121 state->reads_point_coord = false;
122 state->helper_invocations = s->info.fs.needs_helper_invocations;
123
124 if (outputs_written)
125 *outputs_written = s->info.outputs_written;
126
127 /* Separate as primary uniform count is truncated */
128 state->uniform_count = program.uniform_count;
129
130 meta->midgard1.unknown2 = 8; /* XXX */
131
132 unsigned default_vec1_swizzle = panfrost_get_default_swizzle(1);
133 unsigned default_vec2_swizzle = panfrost_get_default_swizzle(2);
134 unsigned default_vec4_swizzle = panfrost_get_default_swizzle(4);
135
136 /* Iterate the varyings and emit the corresponding descriptor */
137 for (unsigned i = 0; i < meta->varying_count; ++i) {
138 unsigned location = program.varyings[i];
139
140 /* Default to a vec4 varying */
141 struct mali_attr_meta v = {
142 .format = MALI_RGBA32F,
143 .swizzle = default_vec4_swizzle,
144 .unknown1 = 0x2,
145 };
146
147 /* Check for special cases, otherwise assume general varying */
148
149 if (location == VARYING_SLOT_POS) {
150 if (stage == MESA_SHADER_FRAGMENT)
151 state->reads_frag_coord = true;
152 else
153 v.format = MALI_VARYING_POS;
154 } else if (location == VARYING_SLOT_PSIZ) {
155 v.format = MALI_R16F;
156 v.swizzle = default_vec1_swizzle;
157
158 state->writes_point_size = true;
159 } else if (location == VARYING_SLOT_PNTC) {
160 v.format = MALI_RG16F;
161 v.swizzle = default_vec2_swizzle;
162
163 state->reads_point_coord = true;
164 } else if (location == VARYING_SLOT_FACE) {
165 v.format = MALI_R32I;
166 v.swizzle = default_vec1_swizzle;
167
168 state->reads_face = true;
169 }
170
171 state->varyings[i] = v;
172 state->varyings_loc[i] = location;
173 }
174 }