2 * © Copyright 2018 Alyssa Rosenzweig
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:
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
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
28 #include "pan_context.h"
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"
35 #include "tgsi/tgsi_dump.h"
38 panfrost_shader_compile(struct panfrost_context
*ctx
, struct mali_shader_meta
*meta
, const char *src
, int type
, struct panfrost_shader_state
*state
)
44 struct pipe_shader_state
*cso
= state
->base
;
46 if (cso
->type
== PIPE_SHADER_IR_NIR
) {
47 s
= nir_shader_clone(NULL
, cso
->ir
.nir
);
49 assert (cso
->type
== PIPE_SHADER_IR_TGSI
);
50 //tgsi_dump(cso->tokens, 0);
51 s
= tgsi_to_nir(cso
->tokens
, &midgard_nir_options
);
54 s
->info
.stage
= type
== JOB_TYPE_VERTEX
? MESA_SHADER_VERTEX
: MESA_SHADER_FRAGMENT
;
56 if (s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
57 /* Inject the alpha test now if we need to */
59 if (state
->alpha_state
.enabled
) {
60 NIR_PASS_V(s
, nir_lower_alpha_test
, state
->alpha_state
.func
, false);
64 /* Call out to Midgard compiler given the above NIR */
66 midgard_program program
= {
67 .alpha_ref
= state
->alpha_state
.ref_value
70 midgard_compile_shader_nir(s
, &program
, false);
72 /* Prepare the compiled binary for upload */
73 int size
= program
.compiled
.size
;
74 dst
= program
.compiled
.data
;
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. */
80 meta
->shader
= panfrost_upload(&ctx
->shaders
, dst
, size
, true) | program
.first_tag
;
82 util_dynarray_fini(&program
.compiled
);
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
;
89 state
->can_discard
= program
.can_discard
;
90 state
->writes_point_size
= program
.writes_point_size
;
92 /* Separate as primary uniform count is truncated */
93 state
->uniform_count
= program
.uniform_count
;
95 /* gl_Position eats up an extra spot */
96 if (type
== JOB_TYPE_VERTEX
)
97 meta
->varying_count
+= 1;
99 /* Note: gl_FragCoord does -not- eat an extra spot; it will be included
100 * in our count if we need it */
102 meta
->midgard1
.unknown2
= 8; /* XXX */
104 /* Varyings are known only through the shader. We choose to upload this
105 * information with the vertex shader, though the choice is perhaps
108 if (type
== JOB_TYPE_VERTEX
) {
109 struct panfrost_varyings
*varyings
= &state
->varyings
;
111 /* Measured in vec4 words. Don't include gl_Position */
112 int varying_count
= program
.varying_count
;
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? */
118 varyings
->varying_buffer_count
= 2;
120 /* mediump vec4s sequentially */
121 varyings
->varyings_stride
[0] = (2 * sizeof(float)) * varying_count
;
123 /* highp gl_Position */
124 varyings
->varyings_stride
[1] = 4 * sizeof(float);
126 /* mediump gl_PointSize */
127 if (program
.writes_point_size
) {
128 ++varyings
->varying_buffer_count
;
129 varyings
->varyings_stride
[2] = 2; /* sizeof(fp16) */
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);
136 struct mali_attr_meta vertex_special_varyings
[] = {
139 .format
= MALI_VARYING_POS
,
141 .swizzle
= default_vec4_swizzle
,
146 .format
= MALI_RGBA16F
,
148 /* TODO: Wat? yyyy swizzle? */
155 .swizzle
= default_vec1_swizzle
,
160 /* How many special vertex varyings are actually required? */
161 int vertex_special_count
= 2 + (program
.writes_point_size
? 1 : 0);
163 /* Setup actual varyings. XXX: Don't assume vec4 */
165 struct mali_attr_meta mali_varyings
[PIPE_MAX_ATTRIBS
];
167 for (int i
= 0; i
< varying_count
; ++i
) {
168 struct mali_attr_meta vec4_varying_meta
= {
170 .format
= MALI_RGBA16F
,
171 .swizzle
= default_vec4_swizzle
,
174 /* Set offset to keep everything back-to-back in
179 mali_varyings
[i
] = vec4_varying_meta
;
182 /* We don't count the weirdo gl_Position in our varying count */
183 varyings
->varying_count
= varying_count
- 1;
185 /* In this context, position_meta represents the implicit
186 * gl_FragCoord varying. So, upload all the varyings */
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
);
193 struct panfrost_transfer transfer
= panfrost_allocate_chunk(ctx
, vertex_size
+ fragment_size
, HEAP_DESCRIPTOR
);
195 /* Copy varyings in the follow order:
197 * - Varyings 1, 2, ..., n
198 * - Varyings 1, 2, ..., n (duplicate)
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
));
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
;