spirv: fix using OpSampledImage with OpUndef instead of OpType{Image,Sampler}
authorSamuel Pitoiset <samuel.pitoiset@gmail.com>
Tue, 26 May 2020 11:54:34 +0000 (13:54 +0200)
committerSamuel Pitoiset <samuel.pitoiset@gmail.com>
Mon, 15 Jun 2020 15:02:53 +0000 (17:02 +0200)
This seems valid per the SPIR-V spec to use OpSampledImage with
OpUndef instead of OpTypeImage or OpTypeSampler. When the image
operand is undefined, SPIRV->NIR emits an undef instruction that
can be removed later by the compiler.

This fixes shader compilation crashes with Red Dead Redemption II.

Cc: mesa-stable@lists.freedesktop.org>
Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/5230>

src/compiler/spirv/spirv_to_nir.c

index cfe180c048912a4c2af711e4ab4f2f6ecae4e3a5..10c5de552164318204510eeae250c564533c5cab 100644 (file)
@@ -2259,10 +2259,23 @@ vtn_handle_texture(struct vtn_builder *b, SpvOp opcode,
       struct vtn_value *val =
          vtn_push_value(b, w[2], vtn_value_type_sampled_image);
       val->sampled_image = ralloc(b, struct vtn_sampled_image);
-      val->sampled_image->image =
-         vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
-      val->sampled_image->sampler =
-         vtn_value(b, w[4], vtn_value_type_pointer)->pointer;
+
+      /* It seems valid to use OpSampledImage with OpUndef instead of
+       * OpTypeImage or OpTypeSampler.
+       */
+      if (vtn_untyped_value(b, w[3])->value_type == vtn_value_type_undef) {
+         val->sampled_image->image = NULL;
+      } else {
+         val->sampled_image->image =
+            vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
+      }
+
+      if (vtn_untyped_value(b, w[4])->value_type == vtn_value_type_undef) {
+         val->sampled_image->sampler = NULL;
+      } else {
+         val->sampled_image->sampler =
+            vtn_value(b, w[4], vtn_value_type_pointer)->pointer;
+      }
       return;
    } else if (opcode == SpvOpImage) {
       struct vtn_value *src_val = vtn_untyped_value(b, w[3]);
@@ -2287,6 +2300,11 @@ vtn_handle_texture(struct vtn_builder *b, SpvOp opcode,
       image = sampled_val->pointer;
    }
 
+   if (!image) {
+      vtn_push_value(b, w[2], vtn_value_type_undef);
+      return;
+   }
+
    nir_deref_instr *image_deref = vtn_pointer_to_deref(b, image);
    nir_deref_instr *sampler_deref =
       sampler ? vtn_pointer_to_deref(b, sampler) : NULL;