aboutsummaryrefslogtreecommitdiffstats
path: root/src/panfrost/midgard/midgard_derivatives.c
blob: ceb3bb96166286ace63dd92a15388256a66745e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (C) 2019 Collabora, Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors (Collabora):
 *   Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
 */

#include "compiler.h"

/* Derivatives in Midgard are implemented on the texture pipe, rather than the
 * ALU pipe as suggested by NIR. The rationale is that normal texture
 * instructions require (implicit) derivatives to be calculated anyway, so it
 * makes sense to reuse the derivative logic. Thus, in addition to the usual
 * texturing ops that calculate derivatives, there are two explicit texture ops
 * dFdx/dFdy that perform differencing across helper invocations in either
 * horizontal or vertical directions.
 *
 * One major caveat is that derivatives can only be calculated on up to a vec2
 * at a time. This restriction presumably is to save some silicon, as 99% of
 * derivatives will be vec2 (autocalculating mip levels of 2D texture
 * coordinates). Admittedly I'm not sure why 3D textures can have their levels
 * calculated automatically, umm... Pressing on.
 *
 * This caveat is handled in two steps. During the first pass (code
 * generation), we generate texture ops 1:1 to the incoming NIR derivatives.
 * This works for float/vec2 but not for vec3/vec4. A later lowering pass will
 * scan for vec3/vec4 derivatives and lower (split) to multiple instructions.
 * This pass is separated as we'll have to rewrite th e destination into a
 * register (rather than SSA) and we'd rather do this after we have the whole
 * IR in front of us to do it at once.
 */

static unsigned
mir_derivative_op(nir_op op)
{
        switch (op) {
        case nir_op_fddx:
        case nir_op_fddx_fine:
        case nir_op_fddx_coarse:
                return TEXTURE_OP_DFDX;

        case nir_op_fddy:
        case nir_op_fddy_fine:
        case nir_op_fddy_coarse:
                return TEXTURE_OP_DFDY;

        default:
                unreachable("Invalid derivative op");
        }
}

/* Returns true if a texturing op computes derivatives either explicitly or
 * implicitly */

bool
mir_op_computes_derivatives(gl_shader_stage stage, unsigned op)
{
        /* Only fragment shaders may compute derivatives, but the sense of
         * "normal" changes in vertex shaders on certain GPUs */

        if (op == TEXTURE_OP_NORMAL && stage != MESA_SHADER_FRAGMENT)
                return false;

        switch (op) {
        case TEXTURE_OP_NORMAL:
        case TEXTURE_OP_DFDX:
        case TEXTURE_OP_DFDY:
                assert(stage == MESA_SHADER_FRAGMENT);
                return true;
        default:
                return false;
        }
}

void
midgard_emit_derivatives(compiler_context *ctx, nir_alu_instr *instr)
{
        /* Create texture instructions */

        unsigned nr_components = nir_dest_num_components(instr->dest.dest);

        midgard_instruction ins = {
                .type = TAG_TEXTURE_4,
                .mask = mask_of(nr_components),
                .dest = nir_dest_index(ctx, &instr->dest.dest),
                .src = { nir_alu_src_index(ctx, &instr->src[0]), ~0, ~0 },
                .texture = {
                        .op = mir_derivative_op(instr->op),
                        .format = MALI_TEX_2D,
                        .in_reg_full = 1,
                        .out_full = 1,
                        .sampler_type = MALI_SAMPLER_FLOAT,
                }
        };

        ins.swizzle[0][2] = ins.swizzle[0][3] = COMPONENT_X;
        ins.swizzle[1][2] = ins.swizzle[1][3] = COMPONENT_X;

        if (!instr->dest.dest.is_ssa)
                ins.mask &= instr->dest.write_mask;

        emit_mir_instruction(ctx, ins);

        /* TODO: Set .cont/.last automatically via dataflow analysis */
        ctx->texture_op_count++;
}

void
midgard_lower_derivatives(compiler_context *ctx, midgard_block *block)
{
        mir_foreach_instr_in_block_safe(block, ins) {
                if (ins->type != TAG_TEXTURE_4) continue;
                if (!OP_IS_DERIVATIVE(ins->texture.op)) continue;

                /* Check if we need to split */

                bool upper = ins->mask & 0b1100;
                bool lower = ins->mask & 0b0011;

                if (!(upper && lower)) continue;

                /* Duplicate for dedicated upper instruction */

                midgard_instruction dup;
                memcpy(&dup, ins, sizeof(dup));

                /* Fixup masks. Make original just lower and dupe just upper */

                ins->mask &= 0b0011;
                dup.mask &= 0b1100;

                /* Fixup swizzles */
                dup.swizzle[0][0] = dup.swizzle[0][1] = dup.swizzle[0][2] = COMPONENT_X;
                dup.swizzle[0][3] = COMPONENT_Y;

                dup.swizzle[1][0] = COMPONENT_Z;
                dup.swizzle[1][1] = dup.swizzle[1][2] = dup.swizzle[1][3] = COMPONENT_W;

                /* Insert the new instruction */
                mir_insert_instruction_before(ctx, mir_next_op(ins), dup);

                /* TODO: Set .cont/.last automatically via dataflow analysis */
                ctx->texture_op_count++;

                /* We'll need both instructions to write to the same index, so
                 * rewrite to use a register */

                unsigned new = make_compiler_temp_reg(ctx);
                mir_rewrite_index(ctx, ins->dest, new);
        }
}