| 1 |
/* |
| 2 |
* Copyright (C) 2024 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "compiler/nir/nir.h" |
| 7 |
#include "compiler/nir/nir_builder.h" |
| 8 |
#include "pan_nir.h" |
| 9 |
|
| 10 |
static bool |
| 11 |
nir_lower_image_ms(nir_builder *b, nir_intrinsic_instr *intr, |
| 12 |
UNUSED void *data) |
| 13 |
{ |
| 14 |
bool img_deref = false; |
| 15 |
|
| 16 |
switch (intr->intrinsic) { |
| 17 |
case nir_intrinsic_image_deref_load: |
| 18 |
case nir_intrinsic_image_deref_store: |
| 19 |
img_deref = true; |
| 20 |
break; |
| 21 |
case nir_intrinsic_image_texel_address: |
| 22 |
case nir_intrinsic_image_load: |
| 23 |
case nir_intrinsic_image_store: |
| 24 |
break; |
| 25 |
default: |
| 26 |
return false; |
| 27 |
} |
| 28 |
|
| 29 |
if (nir_intrinsic_image_dim(intr) != GLSL_SAMPLER_DIM_MS) |
| 30 |
return false; |
| 31 |
|
| 32 |
b->cursor = nir_before_instr(&intr->instr); |
| 33 |
|
| 34 |
nir_def *coord = intr->src[1].ssa; |
| 35 |
nir_def *sample = nir_channel(b, intr->src[2].ssa, 0); |
| 36 |
bool is_array = nir_intrinsic_image_array(intr); |
| 37 |
|
| 38 |
nir_def *img_samples = |
| 39 |
img_deref ? |
| 40 |
nir_image_deref_samples(b, 32, intr->src[0].ssa, .image_array = is_array, |
| 41 |
.image_dim = GLSL_SAMPLER_DIM_MS) : |
| 42 |
nir_image_samples(b, 32, intr->src[0].ssa, .image_array = is_array, |
| 43 |
.image_dim = GLSL_SAMPLER_DIM_MS); |
| 44 |
|
| 45 |
nir_def *z_coord = nir_channel(b, coord, 2); |
| 46 |
|
| 47 |
/* image2DMS is treated by panfrost as if it were a 3D image, so |
| 48 |
* the sample index is in src[2]. We need to put this into the coordinates |
| 49 |
* in the Z component. If there already was a Z component (i.e. an |
| 50 |
* array index) then scale that by the number of samples and add it to |
| 51 |
* the sample number. We've lowered image2DMSArray images to be 3D images |
| 52 |
* with a larger Z. |
| 53 |
*/ |
| 54 |
z_coord = nir_iadd(b, sample, nir_imul(b, z_coord, img_samples)); |
| 55 |
nir_src_rewrite(&intr->src[1], |
| 56 |
nir_vector_insert_imm(b, coord, z_coord, 2)); |
| 57 |
|
| 58 |
nir_intrinsic_set_image_dim(intr, GLSL_SAMPLER_DIM_3D); |
| 59 |
nir_intrinsic_set_image_array(intr, false); |
| 60 |
return true; |
| 61 |
} |
| 62 |
|
| 63 |
bool |
| 64 |
pan_nir_lower_image_ms(nir_shader *shader) |
| 65 |
{ |
| 66 |
return nir_shader_intrinsics_pass( |
| 67 |
shader, nir_lower_image_ms, |
| 68 |
nir_metadata_control_flow, NULL); |
| 69 |
} |