Skip to content

Commit

Permalink
OpSampledImage extra validation
Browse files Browse the repository at this point in the history
* Validate that the type of Image operand matches the result type's
  Image operand
  • Loading branch information
alan-baker committed May 31, 2024
1 parent 3d24089 commit 7909c14
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
8 changes: 7 additions & 1 deletion source/val/validate_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,8 @@ bool IsAllowedSampledImageOperand(spv::Op opcode, ValidationState_t& _) {

spv_result_t ValidateSampledImage(ValidationState_t& _,
const Instruction* inst) {
if (_.GetIdOpcode(inst->type_id()) != spv::Op::OpTypeSampledImage) {
auto type_inst = _.FindDef(inst->type_id());
if (type_inst->opcode() != spv::Op::OpTypeSampledImage) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Result Type to be OpTypeSampledImage.";
}
Expand All @@ -1016,6 +1017,11 @@ spv_result_t ValidateSampledImage(ValidationState_t& _,
<< "Expected Image to be of type OpTypeImage.";
}

if (type_inst->GetOperandAs<uint32_t>(1) != image_type) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Image to have the same type as Result Type Image";
}

ImageTypeInfo info;
if (!GetImageTypeInfo(_, image_type, &info)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
Expand Down
42 changes: 40 additions & 2 deletions test/val/val_image_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4451,7 +4451,7 @@ TEST_F(ValidateImage, QuerySizeNotImage) {
const std::string body = R"(
%img = OpLoad %type_image_f32_2d_0011 %uniform_image_f32_2d_0011
%sampler = OpLoad %type_sampler %uniform_sampler
%simg = OpSampledImage %type_sampled_image_f32_2d_0001 %img %sampler
%simg = OpSampledImage %type_sampled_image_f32_2d_0011 %img %sampler
%res1 = OpImageQuerySize %u32vec2 %sampler
)";

Expand All @@ -4465,7 +4465,7 @@ TEST_F(ValidateImage, QuerySizeSampledImageDirectly) {
const std::string body = R"(
%img = OpLoad %type_image_f32_2d_0011 %uniform_image_f32_2d_0011
%sampler = OpLoad %type_sampler %uniform_sampler
%simg = OpSampledImage %type_sampled_image_f32_2d_0001 %img %sampler
%simg = OpSampledImage %type_sampled_image_f32_2d_0011 %img %sampler
%res1 = OpImageQuerySize %u32vec2 %simg
)";

Expand Down Expand Up @@ -10647,6 +10647,44 @@ TEST_F(ValidateImage, ImageMSArray_ArrayedTypeDoesNotRequireCapability) {
EXPECT_THAT(getDiagnosticString(), Eq(""));
}

TEST_F(ValidateImage, SampledImageTypeMismatch) {
const std::string code = R"(
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpDecorate %im_var DescriptorSet 0
OpDecorate %im_var Binding 0
OpDecorate %s_var DescriptorSet 1
OpDecorate %s_var Binding 0
%void = OpTypeVoid
%float = OpTypeFloat 32
%im1_ty = OpTypeImage %float 2D 0 0 0 1 Unknown
%im2_ty = OpTypeImage %float 2D 1 0 0 1 Unknown
%s_ty = OpTypeSampler
%s_im_ty = OpTypeSampledImage %im2_ty
%ptr_im = OpTypePointer UniformConstant %im1_ty
%ptr_s = OpTypePointer UniformConstant %s_ty
%im_var = OpVariable %ptr_im UniformConstant
%s_var = OpVariable %ptr_s UniformConstant
%void_fn = OpTypeFunction %void
%main = OpFunction %void None %void_fn
%entry = OpLabel
%im_ld = OpLoad %im1_ty %im_var
%s_ld = OpLoad %s_ty %s_var
%sampled_image = OpSampledImage %s_im_ty %im_ld %s_ld
OpReturn
OpFunctionEnd
)";

const spv_target_env env = SPV_ENV_VULKAN_1_0;
CompileSuccessfully(code, env);
ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(env));
EXPECT_THAT(
getDiagnosticString(),
HasSubstr("Expected Image to have the same type as Result Type Image"));
}

} // namespace
} // namespace val
} // namespace spvtools

0 comments on commit 7909c14

Please sign in to comment.