panfrost: Remove if 0'd dead 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_context.h"
29
30 #include "compiler/nir/nir.h"
31 #include "nir/tgsi_to_nir.h"
32 #include "midgard/midgard_compile.h"
33 #include "util/u_dynarray.h"
34
35 #include "tgsi/tgsi_dump.h"
36
37 void
38 panfrost_shader_compile(struct panfrost_context *ctx, struct mali_shader_meta *meta, const char *src, int type, struct panfrost_shader_state *state)
39 {
40 uint8_t *dst;
41
42 nir_shader *s;
43
44 struct pipe_shader_state *cso = state->base;
45
46 if (cso->type == PIPE_SHADER_IR_NIR) {
47 s = nir_shader_clone(NULL, cso->ir.nir);
48 } else {
49 assert (cso->type == PIPE_SHADER_IR_TGSI);
50 //tgsi_dump(cso->tokens, 0);
51 s = tgsi_to_nir(cso->tokens, &midgard_nir_options);
52 }
53
54 s->info.stage = type == JOB_TYPE_VERTEX ? MESA_SHADER_VERTEX : MESA_SHADER_FRAGMENT;
55
56 if (s->info.stage == MESA_SHADER_FRAGMENT) {
57 /* Inject the alpha test now if we need to */
58
59 if (state->alpha_state.enabled) {
60 NIR_PASS_V(s, nir_lower_alpha_test, state->alpha_state.func, false);
61 }
62 }
63
64 /* Call out to Midgard compiler given the above NIR */
65
66 midgard_program program = {
67 .alpha_ref = state->alpha_state.ref_value
68 };
69
70 midgard_compile_shader_nir(s, &program, false);
71
72 /* Prepare the compiled binary for upload */
73 int size = program.compiled.size;
74 dst = program.compiled.data;
75
76 /* Upload the shader. The lookahead tag is ORed on as a tagged pointer.
77 * I bet someone just thought that would be a cute pun. At least,
78 * that's how I'd do it. */
79
80 meta->shader = panfrost_upload(&ctx->shaders, dst, size, true) | program.first_tag;
81
82 util_dynarray_fini(&program.compiled);
83
84 meta->midgard1.uniform_count = MIN2(program.uniform_count, program.uniform_cutoff);
85 meta->attribute_count = program.attribute_count;
86 meta->varying_count = program.varying_count;
87 meta->midgard1.work_count = program.work_register_count;
88
89 state->can_discard = program.can_discard;
90 state->writes_point_size = program.writes_point_size;
91
92 /* Separate as primary uniform count is truncated */
93 state->uniform_count = program.uniform_count;
94
95 /* gl_Position eats up an extra spot */
96 if (type == JOB_TYPE_VERTEX)
97 meta->varying_count += 1;
98
99 /* gl_FragCoord does -not- eat an extra spot; it will be included in our count if we need it */
100
101
102 meta->midgard1.unknown2 = 8; /* XXX */
103
104 /* Varyings are known only through the shader. We choose to upload this
105 * information with the vertex shader, though the choice is perhaps
106 * arbitrary */
107
108 if (type == JOB_TYPE_VERTEX) {
109 struct panfrost_varyings *varyings = &state->varyings;
110
111 /* Measured in vec4 words. Don't include gl_Position */
112 int varying_count = program.varying_count;
113
114 /* Setup two buffers, one for position, the other for normal
115 * varyings, as seen in traces. TODO: Are there other
116 * configurations we might use? */
117
118 varyings->varying_buffer_count = 2;
119
120 /* mediump vec4s sequentially */
121 varyings->varyings_stride[0] = (2 * sizeof(float)) * varying_count;
122
123 /* highp gl_Position */
124 varyings->varyings_stride[1] = 4 * sizeof(float);
125
126 /* mediump gl_PointSize */
127 if (program.writes_point_size) {
128 ++varyings->varying_buffer_count;
129 varyings->varyings_stride[2] = 2; /* sizeof(fp16) */
130 }
131
132 /* Setup gl_Position, its weirdo analogue, and gl_PointSize (optionally) */
133 unsigned default_vec1_swizzle = panfrost_get_default_swizzle(1);
134 unsigned default_vec4_swizzle = panfrost_get_default_swizzle(4);
135
136 struct mali_attr_meta vertex_special_varyings[] = {
137 {
138 .index = 1,
139 .format = MALI_VARYING_POS,
140
141 .swizzle = default_vec4_swizzle,
142 .unknown1 = 0x2,
143 },
144 {
145 .index = 1,
146 .format = MALI_RGBA16F,
147
148 /* TODO: Wat? yyyy swizzle? */
149 .swizzle = 0x249,
150 .unknown1 = 0x0,
151 },
152 {
153 .index = 2,
154 .format = MALI_R16F,
155 .swizzle = default_vec1_swizzle,
156 .unknown1 = 0x2
157 }
158 };
159
160 /* How many special vertex varyings are actually required? */
161 int vertex_special_count = 2 + (program.writes_point_size ? 1 : 0);
162
163 /* Setup actual varyings. XXX: Don't assume vec4 */
164
165 struct mali_attr_meta mali_varyings[PIPE_MAX_ATTRIBS];
166
167 for (int i = 0; i < varying_count; ++i) {
168 struct mali_attr_meta vec4_varying_meta = {
169 .index = 0,
170 .format = MALI_RGBA16F,
171 .swizzle = default_vec4_swizzle,
172 .unknown1 = 0x2,
173
174 /* Set offset to keep everything back-to-back in
175 * the same buffer */
176 .src_offset = 8 * i,
177 };
178
179 mali_varyings[i] = vec4_varying_meta;
180 }
181
182 /* We don't count the weirdo gl_Position in our varying count */
183 varyings->varying_count = varying_count - 1;
184
185 /* In this context, position_meta represents the implicit
186 * gl_FragCoord varying. So, upload all the varyings */
187
188 unsigned varyings_size = sizeof(struct mali_attr_meta) * varyings->varying_count;
189 unsigned vertex_special_size = sizeof(struct mali_attr_meta) * vertex_special_count;
190 unsigned vertex_size = vertex_special_size + varyings_size;
191 unsigned fragment_size = varyings_size + sizeof(struct mali_attr_meta);
192
193 struct panfrost_transfer transfer = panfrost_allocate_chunk(ctx, vertex_size + fragment_size, HEAP_DESCRIPTOR);
194
195 /* Copy varyings in the follow order:
196 * - Position 1, 2
197 * - Varyings 1, 2, ..., n
198 * - Varyings 1, 2, ..., n (duplicate)
199 * - Position 1
200 */
201
202 memcpy(transfer.cpu, vertex_special_varyings, vertex_special_size);
203 memcpy(transfer.cpu + vertex_special_size, mali_varyings, varyings_size);
204 memcpy(transfer.cpu + vertex_size, mali_varyings, varyings_size);
205 memcpy(transfer.cpu + vertex_size + varyings_size, &vertex_special_varyings[0], sizeof(struct mali_attr_meta));
206
207 /* Point to the descriptor */
208 varyings->varyings_buffer_cpu = transfer.cpu;
209 varyings->varyings_descriptor = transfer.gpu;
210 varyings->varyings_descriptor_fragment = transfer.gpu + vertex_size;
211 }
212 }