| 1 |
/* |
| 2 |
* Copyright (C) 2019 Collabora, Ltd. |
| 3 |
* Copyright (C) 2019-2020 Collabora, Ltd. |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "compiler.h" |
| 8 |
|
| 9 |
/* Derivatives in Midgard are implemented on the texture pipe, rather than the |
| 10 |
* ALU pipe as suggested by NIR. The rationale is that normal texture |
| 11 |
* instructions require (implicit) derivatives to be calculated anyway, so it |
| 12 |
* makes sense to reuse the derivative logic. Thus, in addition to the usual |
| 13 |
* texturing ops that calculate derivatives, there are two explicit texture ops |
| 14 |
* dFdx/dFdy that perform differencing across helper invocations in either |
| 15 |
* horizontal or vertical directions. |
| 16 |
* |
| 17 |
* One major caveat is that derivatives can only be calculated on up to a vec2 |
| 18 |
* at a time. This restriction presumably is to save some silicon, as 99% of |
| 19 |
* derivatives will be vec2 (autocalculating mip levels of 2D texture |
| 20 |
* coordinates). Admittedly I'm not sure why 3D textures can have their levels |
| 21 |
* calculated automatically, umm... Pressing on. |
| 22 |
* |
| 23 |
* This caveat is handled in two steps. During the first pass (code |
| 24 |
* generation), we generate texture ops 1:1 to the incoming NIR derivatives. |
| 25 |
* This works for float/vec2 but not for vec3/vec4. A later lowering pass will |
| 26 |
* scan for vec3/vec4 derivatives and lower (split) to multiple instructions. |
| 27 |
* This pass is separated as we'll have to rewrite th e destination into a |
| 28 |
* register (rather than SSA) and we'd rather do this after we have the whole |
| 29 |
* IR in front of us to do it at once. |
| 30 |
*/ |
| 31 |
|
| 32 |
static unsigned |
| 33 |
mir_derivative_mode(nir_intrinsic_op op) |
| 34 |
{ |
| 35 |
switch (op) { |
| 36 |
case nir_intrinsic_ddx: |
| 37 |
case nir_intrinsic_ddx_fine: |
| 38 |
case nir_intrinsic_ddx_coarse: |
| 39 |
return TEXTURE_DFDX; |
| 40 |
|
| 41 |
case nir_intrinsic_ddy: |
| 42 |
case nir_intrinsic_ddy_fine: |
| 43 |
case nir_intrinsic_ddy_coarse: |
| 44 |
return TEXTURE_DFDY; |
| 45 |
|
| 46 |
default: |
| 47 |
UNREACHABLE("Invalid derivative op"); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/* Returns true if a texturing op computes derivatives either explicitly or |
| 52 |
* implicitly */ |
| 53 |
|
| 54 |
bool |
| 55 |
mir_op_computes_derivatives(mesa_shader_stage stage, unsigned op) |
| 56 |
{ |
| 57 |
/* Only fragment shaders may compute derivatives, but the sense of |
| 58 |
* "normal" changes in vertex shaders on certain GPUs */ |
| 59 |
|
| 60 |
if (op == midgard_tex_op_normal && stage != MESA_SHADER_FRAGMENT) |
| 61 |
return false; |
| 62 |
|
| 63 |
switch (op) { |
| 64 |
case midgard_tex_op_normal: |
| 65 |
case midgard_tex_op_derivative: |
| 66 |
assert(stage == MESA_SHADER_FRAGMENT); |
| 67 |
return true; |
| 68 |
default: |
| 69 |
return false; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
void |
| 74 |
midgard_emit_derivatives(compiler_context *ctx, nir_intrinsic_instr *instr) |
| 75 |
{ |
| 76 |
/* Create texture instructions */ |
| 77 |
midgard_instruction ins = { |
| 78 |
.type = TAG_TEXTURE_4, |
| 79 |
.dest_type = nir_type_float32, |
| 80 |
.src = |
| 81 |
{ |
| 82 |
~0, |
| 83 |
nir_src_index(ctx, &instr->src[0]), |
| 84 |
~0, |
| 85 |
~0, |
| 86 |
}, |
| 87 |
.swizzle = SWIZZLE_IDENTITY_4, |
| 88 |
.src_types = |
| 89 |
{ |
| 90 |
nir_type_float32, |
| 91 |
nir_type_float32, |
| 92 |
}, |
| 93 |
.op = midgard_tex_op_derivative, |
| 94 |
.texture = |
| 95 |
{ |
| 96 |
.mode = mir_derivative_mode(instr->intrinsic), |
| 97 |
.format = 2, |
| 98 |
.in_reg_full = 1, |
| 99 |
.out_full = 1, |
| 100 |
.sampler_type = MALI_SAMPLER_FLOAT, |
| 101 |
}, |
| 102 |
}; |
| 103 |
|
| 104 |
ins.dest = nir_def_index_with_mask(&instr->def, &ins.mask); |
| 105 |
emit_mir_instruction(ctx, &ins); |
| 106 |
} |
| 107 |
|
| 108 |
void |
| 109 |
midgard_lower_derivatives(compiler_context *ctx, midgard_block *block) |
| 110 |
{ |
| 111 |
mir_foreach_instr_in_block_safe(block, ins) { |
| 112 |
if (ins->type != TAG_TEXTURE_4) |
| 113 |
continue; |
| 114 |
if (ins->op != midgard_tex_op_derivative) |
| 115 |
continue; |
| 116 |
|
| 117 |
/* Check if we need to split */ |
| 118 |
|
| 119 |
bool upper = ins->mask & 0b1100; |
| 120 |
bool lower = ins->mask & 0b0011; |
| 121 |
|
| 122 |
if (!(upper && lower)) |
| 123 |
continue; |
| 124 |
|
| 125 |
/* Duplicate for dedicated upper instruction */ |
| 126 |
|
| 127 |
midgard_instruction dup; |
| 128 |
memcpy(&dup, ins, sizeof(dup)); |
| 129 |
|
| 130 |
/* Fixup masks. Make original just lower and dupe just upper */ |
| 131 |
|
| 132 |
ins->mask &= 0b0011; |
| 133 |
dup.mask &= 0b1100; |
| 134 |
|
| 135 |
/* Fixup swizzles */ |
| 136 |
dup.swizzle[0][0] = dup.swizzle[0][1] = dup.swizzle[0][2] = COMPONENT_X; |
| 137 |
dup.swizzle[0][3] = COMPONENT_Y; |
| 138 |
|
| 139 |
dup.swizzle[1][0] = COMPONENT_Z; |
| 140 |
dup.swizzle[1][1] = dup.swizzle[1][2] = dup.swizzle[1][3] = COMPONENT_W; |
| 141 |
|
| 142 |
/* Insert the new instruction */ |
| 143 |
mir_insert_instruction_before(ctx, mir_next_op(ins), &dup); |
| 144 |
|
| 145 |
/* We'll need both instructions to write to the same index, so |
| 146 |
* rewrite to use a register */ |
| 147 |
|
| 148 |
unsigned new = make_compiler_temp_reg(ctx); |
| 149 |
mir_rewrite_index(ctx, ins->dest, new); |
| 150 |
} |
| 151 |
} |