blob: 631614b43493096c610fc78eee959b09936924c9 [file]
// Copyright 2025 The Dawn & Tint Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "src/tint/lang/spirv/reader/parser/helper_test.h"
namespace tint::spirv::reader {
namespace {
TEST_F(SpirvParserTest, Type_Sampler) {
EXPECT_IR(R"(
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %1 "main"
OpExecutionMode %1 LocalSize 1 1 1
OpDecorate %6 DescriptorSet 1
OpDecorate %6 Binding 2
%void = OpTypeVoid
%samp = OpTypeSampler
%_ptr_Uniform = OpTypePointer UniformConstant %samp
%5 = OpTypeFunction %void
%6 = OpVariable %_ptr_Uniform UniformConstant
%1 = OpFunction %void None %5
%7 = OpLabel
OpReturn
OpFunctionEnd
)",
R"(
$B1: { # root
%1:ptr<handle, sampler, read> = var @binding_point(1, 2)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
ret
}
}
)");
}
TEST_F(SpirvParserTest, Type_Image_1d) {
EXPECT_IR(R"(
OpCapability Shader
OpCapability Sampled1D
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpDecorate %6 DescriptorSet 1
OpDecorate %6 Binding 2
%void = OpTypeVoid
%f32 = OpTypeFloat 32
%img = OpTypeImage %f32 1D 0 0 0 1 Unknown
%_ptr_Uniform = OpTypePointer UniformConstant %img
%6 = OpVariable %_ptr_Uniform UniformConstant
%ep_type = OpTypeFunction %void
%main = OpFunction %void None %ep_type
%main_start = OpLabel
OpReturn
OpFunctionEnd
)",
R"(
$B1: { # root
%1:ptr<handle, spirv.image<f32, 1d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read_write>, read> = var @binding_point(1, 2)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
ret
}
}
)");
}
TEST_F(SpirvParserTest, Type_Image_2d) {
EXPECT_IR(R"(
OpCapability Shader
OpCapability Sampled1D
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpDecorate %6 DescriptorSet 1
OpDecorate %6 Binding 2
%void = OpTypeVoid
%f32 = OpTypeFloat 32
%img = OpTypeImage %f32 2D 0 0 0 1 Unknown
%_ptr_Uniform = OpTypePointer UniformConstant %img
%6 = OpVariable %_ptr_Uniform UniformConstant
%ep_type = OpTypeFunction %void
%main = OpFunction %void None %ep_type
%main_start = OpLabel
OpReturn
OpFunctionEnd
)",
R"(
$B1: { # root
%1:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read_write>, read> = var @binding_point(1, 2)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
ret
}
}
)");
}
TEST_F(SpirvParserTest, Type_Image_3d) {
EXPECT_IR(R"(
OpCapability Shader
OpCapability Sampled1D
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpDecorate %6 DescriptorSet 1
OpDecorate %6 Binding 2
%void = OpTypeVoid
%f32 = OpTypeFloat 32
%img = OpTypeImage %f32 3D 0 0 0 1 Unknown
%_ptr_Uniform = OpTypePointer UniformConstant %img
%6 = OpVariable %_ptr_Uniform UniformConstant
%ep_type = OpTypeFunction %void
%main = OpFunction %void None %ep_type
%main_start = OpLabel
OpReturn
OpFunctionEnd
)",
R"(
$B1: { # root
%1:ptr<handle, spirv.image<f32, 3d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read_write>, read> = var @binding_point(1, 2)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
ret
}
}
)");
}
TEST_F(SpirvParserTest, Type_Image_Cube) {
EXPECT_IR(R"(
OpCapability Shader
OpCapability Sampled1D
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpDecorate %6 DescriptorSet 1
OpDecorate %6 Binding 2
%void = OpTypeVoid
%f32 = OpTypeFloat 32
%img = OpTypeImage %f32 Cube 0 0 0 1 Unknown
%_ptr_Uniform = OpTypePointer UniformConstant %img
%6 = OpVariable %_ptr_Uniform UniformConstant
%ep_type = OpTypeFunction %void
%main = OpFunction %void None %ep_type
%main_start = OpLabel
OpReturn
OpFunctionEnd
)",
R"(
$B1: { # root
%1:ptr<handle, spirv.image<f32, cube, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read_write>, read> = var @binding_point(1, 2)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
ret
}
}
)");
}
TEST_F(SpirvParserTest, Type_Image_SubpassData) {
EXPECT_IR(R"(
OpCapability Shader
OpCapability InputAttachment
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpDecorate %6 DescriptorSet 1
OpDecorate %6 Binding 2
%void = OpTypeVoid
%f32 = OpTypeFloat 32
%img = OpTypeImage %f32 SubpassData 0 0 0 2 Unknown
%_ptr_Uniform = OpTypePointer UniformConstant %img
%6 = OpVariable %_ptr_Uniform UniformConstant
%ep_type = OpTypeFunction %void
%main = OpFunction %void None %ep_type
%main_start = OpLabel
OpReturn
OpFunctionEnd
)",
R"(
$B1: { # root
%1:ptr<handle, spirv.image<f32, subpass_data, not_depth, non_arrayed, single_sampled, rw_op_compatible, undefined, read_write>, read> = var @binding_point(1, 2)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
ret
}
}
)");
}
TEST_F(SpirvParserTest, Type_Image_Depth) {
EXPECT_IR(R"(
OpCapability Shader
OpCapability Sampled1D
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpDecorate %6 DescriptorSet 1
OpDecorate %6 Binding 2
%void = OpTypeVoid
%f32 = OpTypeFloat 32
%img = OpTypeImage %f32 1D 1 0 0 1 Unknown
%_ptr_Uniform = OpTypePointer UniformConstant %img
%6 = OpVariable %_ptr_Uniform UniformConstant
%ep_type = OpTypeFunction %void
%main = OpFunction %void None %ep_type
%main_start = OpLabel
OpReturn
OpFunctionEnd
)",
R"(
$B1: { # root
%1:ptr<handle, spirv.image<f32, 1d, depth, non_arrayed, single_sampled, sampling_compatible, undefined, read_write>, read> = var @binding_point(1, 2)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
ret
}
}
)");
}
TEST_F(SpirvParserTest, Type_Image_DepthUnknown) {
EXPECT_IR(R"(
OpCapability Shader
OpCapability Sampled1D
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpDecorate %6 DescriptorSet 1
OpDecorate %6 Binding 2
%void = OpTypeVoid
%f32 = OpTypeFloat 32
%img = OpTypeImage %f32 2D 2 0 0 1 Unknown
%_ptr_Uniform = OpTypePointer UniformConstant %img
%6 = OpVariable %_ptr_Uniform UniformConstant
%ep_type = OpTypeFunction %void
%main = OpFunction %void None %ep_type
%main_start = OpLabel
OpReturn
OpFunctionEnd
)",
R"(
$B1: { # root
%1:ptr<handle, spirv.image<f32, 2d, depth_unknown, non_arrayed, single_sampled, sampling_compatible, undefined, read_write>, read> = var @binding_point(1, 2)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
ret
}
}
)");
}
TEST_F(SpirvParserTest, Type_Image_Arrayed) {
EXPECT_IR(R"(
OpCapability Shader
OpCapability Sampled1D
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpDecorate %6 DescriptorSet 1
OpDecorate %6 Binding 2
%void = OpTypeVoid
%f32 = OpTypeFloat 32
%img = OpTypeImage %f32 2D 0 1 0 1 Unknown
%_ptr_Uniform = OpTypePointer UniformConstant %img
%6 = OpVariable %_ptr_Uniform UniformConstant
%ep_type = OpTypeFunction %void
%main = OpFunction %void None %ep_type
%main_start = OpLabel
OpReturn
OpFunctionEnd
)",
R"(
$B1: { # root
%1:ptr<handle, spirv.image<f32, 2d, not_depth, arrayed, single_sampled, sampling_compatible, undefined, read_write>, read> = var @binding_point(1, 2)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
ret
}
}
)");
}
TEST_F(SpirvParserTest, Type_Image_RW_Ops) {
EXPECT_IR(R"(
OpCapability Shader
OpCapability Sampled1D
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpDecorate %6 DescriptorSet 1
OpDecorate %6 Binding 2
%void = OpTypeVoid
%f32 = OpTypeFloat 32
%img = OpTypeImage %f32 1D 0 0 0 2 Unknown
%_ptr_Uniform = OpTypePointer UniformConstant %img
%6 = OpVariable %_ptr_Uniform UniformConstant
%ep_type = OpTypeFunction %void
%main = OpFunction %void None %ep_type
%main_start = OpLabel
OpReturn
OpFunctionEnd
)",
R"(
$B1: { # root
%1:ptr<handle, spirv.image<f32, 1d, not_depth, non_arrayed, single_sampled, rw_op_compatible, undefined, read_write>, read> = var @binding_point(1, 2)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
ret
}
}
)");
}
TEST_F(SpirvParserTest, Type_Image_TexelFormat) {
EXPECT_IR(R"(
OpCapability Shader
OpCapability Sampled1D
OpCapability StorageImageExtendedFormats
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpDecorate %6 DescriptorSet 1
OpDecorate %6 Binding 2
%void = OpTypeVoid
%f32 = OpTypeFloat 32
%img = OpTypeImage %f32 1D 0 0 0 1 Rg32f
%_ptr_Uniform = OpTypePointer UniformConstant %img
%6 = OpVariable %_ptr_Uniform UniformConstant
%ep_type = OpTypeFunction %void
%main = OpFunction %void None %ep_type
%main_start = OpLabel
OpReturn
OpFunctionEnd
)",
R"(
$B1: { # root
%1:ptr<handle, spirv.image<f32, 1d, not_depth, non_arrayed, single_sampled, sampling_compatible, rg32float, read_write>, read> = var @binding_point(1, 2)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
ret
}
}
)");
}
TEST_F(SpirvParserTest, Type_Image_TexelFormat_R8) {
EXPECT_IR(R"(
OpCapability Shader
OpCapability Sampled1D
OpCapability StorageImageExtendedFormats
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpDecorate %6 DescriptorSet 1
OpDecorate %6 Binding 2
%void = OpTypeVoid
%f32 = OpTypeFloat 32
%img = OpTypeImage %f32 1D 0 0 0 1 R8
%_ptr_Uniform = OpTypePointer UniformConstant %img
%6 = OpVariable %_ptr_Uniform UniformConstant
%ep_type = OpTypeFunction %void
%main = OpFunction %void None %ep_type
%main_start = OpLabel
OpReturn
OpFunctionEnd
)",
R"(
$B1: { # root
%1:ptr<handle, spirv.image<f32, 1d, not_depth, non_arrayed, single_sampled, sampling_compatible, r8unorm, read_write>, read> = var @binding_point(1, 2)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
ret
}
}
)");
}
TEST_F(SpirvParserTest, Type_SampledImage) {
options.sampler_mappings.insert({BindingPoint{1, 2}, BindingPoint{5, 6}});
EXPECT_IR(R"(
OpCapability Shader
OpCapability Sampled1D
OpCapability StorageImageExtendedFormats
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpDecorate %6 DescriptorSet 1
OpDecorate %6 Binding 2
%void = OpTypeVoid
%int = OpTypeInt 32 1
%f32 = OpTypeFloat 32
%inner = OpTypeImage %f32 2D 0 0 0 1 Rg32f
%img = OpTypeSampledImage %inner
%v2float = OpTypeVector %f32 2
%v4float = OpTypeVector %f32 4
%int_1 = OpConstant %int 1
%float_1 = OpConstant %f32 1
%float_2 = OpConstant %f32 2
%coords2 = OpConstantComposite %v2float %float_1 %float_2
%_ptr_Uniform = OpTypePointer UniformConstant %img
%6 = OpVariable %_ptr_Uniform UniformConstant
%ep_type = OpTypeFunction %void
%main = OpFunction %void None %ep_type
%main_start = OpLabel
%10 = OpLoad %img %6
%result = OpImageGather %v4float %10 %coords2 %int_1
OpReturn
OpFunctionEnd
)",
R"(
$B1: { # root
%1:ptr<handle, sampler, read> = var @binding_point(5, 6)
%2:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, rg32float, read_write>, read> = var @binding_point(1, 2)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
%4:spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, rg32float, read_write> = load %2
%5:sampler = load %1
%6:spirv.sampled_image<spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, rg32float, read_write>> = spirv.op_sampled_image<spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, rg32float, read_write>> %4, %5
%7:vec4<f32> = spirv.image_gather %6, vec2<f32>(1.0f, 2.0f), 1i, 0u
ret
}
}
)");
}
TEST_F(SpirvParserTest, Type_OpSampledImage) {
options.sampler_mappings.insert({BindingPoint{1, 2}, BindingPoint{5, 6}});
EXPECT_IR(R"(
OpCapability Shader
OpCapability Sampled1D
OpCapability StorageImageExtendedFormats
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpDecorate %var_tex DescriptorSet 1
OpDecorate %var_tex Binding 2
OpDecorate %var_sampler DescriptorSet 1
OpDecorate %var_sampler Binding 1
%void = OpTypeVoid
%float = OpTypeFloat 32
%int = OpTypeInt 32 1
%sampler = OpTypeSampler
%ptr_sampler = OpTypePointer UniformConstant %sampler
%tex = OpTypeImage %float 2D 0 0 0 1 Unknown
%ptr_tex = OpTypePointer UniformConstant %tex
%sampled_img = OpTypeSampledImage %tex
%v2float = OpTypeVector %float 2
%v4float = OpTypeVector %float 4
%int_1 = OpConstant %int 1
%float_1 = OpConstant %float 1
%float_2 = OpConstant %float 2
%coords2 = OpConstantComposite %v2float %float_1 %float_2
%var_tex = OpVariable %ptr_tex UniformConstant
%var_sampler = OpVariable %ptr_sampler UniformConstant
%ep_type = OpTypeFunction %void
%main = OpFunction %void None %ep_type
%main_start = OpLabel
%sam = OpLoad %sampler %var_sampler
%im = OpLoad %tex %var_tex
%sampled_image = OpSampledImage %sampled_img %im %sam
%result = OpImageGather %v4float %sampled_image %coords2 %int_1
OpReturn
OpFunctionEnd
)",
R"(
$B1: { # root
%1:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read_write>, read> = var @binding_point(1, 2)
%2:ptr<handle, sampler, read> = var @binding_point(1, 1)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
%4:sampler = load %2
%5:spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read_write> = load %1
%6:spirv.sampled_image<spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read_write>> = spirv.op_sampled_image<spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read_write>> %5, %4
%7:vec4<f32> = spirv.image_gather %6, vec2<f32>(1.0f, 2.0f), 1i, 0u
ret
}
}
)");
}
TEST_F(SpirvParserTest, Bug448679392) {
EXPECT_IR(R"(
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpName %main "main"
OpName %tex "tex"
OpName %sampler "sampler"
OpDecorate %tex Binding 0
OpDecorate %tex DescriptorSet 1
OpDecorate %sampler Binding 0
OpDecorate %sampler DescriptorSet 2
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%29 = OpTypeSampler
%25 = OpTypeImage %float Cube 0 0 0 1 Unknown
%33 = OpTypeSampledImage %25
%ptr_tex = OpTypePointer UniformConstant %25
%ptr_sampler = OpTypePointer UniformConstant %29
%tex = OpVariable %ptr_tex UniformConstant
%sampler = OpVariable %ptr_sampler UniformConstant
%main = OpFunction %void None %3
%5 = OpLabel
%28 = OpLoad %25 %tex
%32 = OpLoad %29 %sampler
%34 = OpSampledImage %33 %28 %32
%36 = OpImage %25 %34
OpReturn
OpFunctionEnd
)",
R"(
$B1: { # root
%tex:ptr<handle, spirv.image<f32, cube, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read_write>, read> = var @binding_point(1, 0)
%sampler:ptr<handle, sampler, read> = var @binding_point(2, 0)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
%4:spirv.image<f32, cube, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read_write> = load %tex
%5:sampler = load %sampler
%6:spirv.sampled_image<spirv.image<f32, cube, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read_write>> = spirv.op_sampled_image<spirv.image<f32, cube, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read_write>> %4, %5
%7:spirv.image<f32, cube, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read_write> = spirv.op_image<spirv.image<f32, cube, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read_write>> %6
ret
}
}
)");
}
// OpImageFetch keeps every image operand after the mask, in mask bit order. ConstOffset:
// https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Image_Operands
TEST_F(SpirvParserTest, ImageFetch_LodAndConstOffset) {
EXPECT_IR(R"(
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpDecorate %var_tex DescriptorSet 0
OpDecorate %var_tex Binding 0
%void = OpTypeVoid
%float = OpTypeFloat 32
%int = OpTypeInt 32 1
%tex = OpTypeImage %float 2D 0 0 0 1 Unknown
%ptr_tex = OpTypePointer UniformConstant %tex
%v2int = OpTypeVector %int 2
%v4float = OpTypeVector %float 4
%int_1 = OpConstant %int 1
%int_n1 = OpConstant %int -1
%coords = OpConstantComposite %v2int %int_1 %int_1
%offset = OpConstantComposite %v2int %int_n1 %int_1
%var_tex = OpVariable %ptr_tex UniformConstant
%ep_type = OpTypeFunction %void
%main = OpFunction %void None %ep_type
%main_start = OpLabel
%im = OpLoad %tex %var_tex
%result = OpImageFetch %v4float %im %coords Lod|ConstOffset %int_1 %offset
OpReturn
OpFunctionEnd
)",
R"(
$B1: { # root
%1:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read_write>, read> = var @binding_point(0, 0)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
%3:spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read_write> = load %1
%4:vec4<f32> = spirv.image_fetch %3, vec2<i32>(1i), 10u, 1i, vec2<i32>(-1i, 1i)
ret
}
}
)");
}
// The ConstOffset operand comes before the Sample operand.
TEST_F(SpirvParserTest, ImageFetch_ConstOffsetAndSample) {
EXPECT_IR(R"(
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpDecorate %var_tex DescriptorSet 0
OpDecorate %var_tex Binding 0
%void = OpTypeVoid
%float = OpTypeFloat 32
%int = OpTypeInt 32 1
%tex = OpTypeImage %float 2D 0 0 1 1 Unknown
%ptr_tex = OpTypePointer UniformConstant %tex
%v2int = OpTypeVector %int 2
%v4float = OpTypeVector %float 4
%int_1 = OpConstant %int 1
%int_2 = OpConstant %int 2
%int_n1 = OpConstant %int -1
%coords = OpConstantComposite %v2int %int_1 %int_1
%offset = OpConstantComposite %v2int %int_n1 %int_1
%var_tex = OpVariable %ptr_tex UniformConstant
%ep_type = OpTypeFunction %void
%main = OpFunction %void None %ep_type
%main_start = OpLabel
%im = OpLoad %tex %var_tex
%result = OpImageFetch %v4float %im %coords ConstOffset|Sample %offset %int_2
OpReturn
OpFunctionEnd
)",
R"(
$B1: { # root
%1:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, multi_sampled, sampling_compatible, undefined, read_write>, read> = var @binding_point(0, 0)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
%3:spirv.image<f32, 2d, not_depth, non_arrayed, multi_sampled, sampling_compatible, undefined, read_write> = load %1
%4:vec4<f32> = spirv.image_fetch %3, vec2<i32>(1i), 72u, vec2<i32>(-1i, 1i), 2i
ret
}
}
)");
}
// WGSL only samples f32 textures (https://www.w3.org/TR/WGSL/#texturesample), while SPIR-V allows
// OpImageSample* on integer textures. These tests check that such samples are rejected.
struct IntegerSampleCase {
std::string sampled_type;
std::string result_type;
std::string sample;
std::string opcode;
};
std::ostream& operator<<(std::ostream& out, const IntegerSampleCase& c) {
return out << c.sampled_type << "_" << c.opcode;
}
using SpirvParserIntegerSampleTest = SpirvParserTestWithParam<IntegerSampleCase>;
// Sampling an i32 or u32 texture fails with a message naming the instruction.
TEST_P(SpirvParserIntegerSampleTest, Rejected) {
auto& params = GetParam();
auto res = Run(R"(
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main"
OpExecutionMode %main OriginUpperLeft
OpDecorate %tex DescriptorSet 0
OpDecorate %tex Binding 0
OpDecorate %smp DescriptorSet 0
OpDecorate %smp Binding 1
%void = OpTypeVoid
%fn = OpTypeFunction %void
%f32 = OpTypeFloat 32
%i32 = OpTypeInt 32 1
%u32 = OpTypeInt 32 0
%vec2f = OpTypeVector %f32 2
%vec3f = OpTypeVector %f32 3
%vec4i = OpTypeVector %i32 4
%vec4u = OpTypeVector %u32 4
%f32_0 = OpConstant %f32 0
%coord2 = OpConstantComposite %vec2f %f32_0 %f32_0
%coord3 = OpConstantComposite %vec3f %f32_0 %f32_0 %f32_0
%img = OpTypeImage )" +
params.sampled_type + R"( 2D 0 0 0 1 Unknown
%si = OpTypeSampledImage %img
%img_ptr = OpTypePointer UniformConstant %img
%sampler = OpTypeSampler
%smp_ptr = OpTypePointer UniformConstant %sampler
%tex = OpVariable %img_ptr UniformConstant
%smp = OpVariable %smp_ptr UniformConstant
%main = OpFunction %void None %fn
%start = OpLabel
%t = OpLoad %img %tex
%s = OpLoad %sampler %smp
%ts = OpSampledImage %si %t %s
%r = )" + params.opcode +
" " + params.result_type + " %ts " + params.sample + R"(
OpReturn
OpFunctionEnd
)");
ASSERT_NE(res, Success);
EXPECT_THAT(res.Failure().reason,
testing::HasSubstr(params.opcode + " on a texture with sampled type " +
params.sampled_type.substr(1) + " is not supported"));
}
INSTANTIATE_TEST_SUITE_P(
SpirvParser,
SpirvParserIntegerSampleTest,
testing::Values(
IntegerSampleCase{"%i32", "%vec4i", "%coord2", "OpImageSampleImplicitLod"},
IntegerSampleCase{"%i32", "%vec4i", "%coord2 Lod %f32_0", "OpImageSampleExplicitLod"},
IntegerSampleCase{"%i32", "%vec4i", "%coord3", "OpImageSampleProjImplicitLod"},
IntegerSampleCase{"%i32", "%vec4i", "%coord3 Lod %f32_0", "OpImageSampleProjExplicitLod"},
IntegerSampleCase{"%u32", "%vec4u", "%coord2", "OpImageSampleImplicitLod"},
IntegerSampleCase{"%u32", "%vec4u", "%coord2 Lod %f32_0", "OpImageSampleExplicitLod"},
IntegerSampleCase{"%u32", "%vec4u", "%coord3", "OpImageSampleProjImplicitLod"},
IntegerSampleCase{"%u32", "%vec4u", "%coord3 Lod %f32_0", "OpImageSampleProjExplicitLod"}));
// Vulkan memory model image operands are passed through: the mask literal is followed by the
// scope ids. https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Image_Operands
std::string MemoryModelImageModule(std::string body) {
return R"(
OpCapability Shader
OpCapability VulkanMemoryModel
OpExtension "SPV_KHR_vulkan_memory_model"
OpMemoryModel Logical Vulkan
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpDecorate %img DescriptorSet 0
OpDecorate %img Binding 0
%void = OpTypeVoid
%fn = OpTypeFunction %void
%u32 = OpTypeInt 32 0
%i32 = OpTypeInt 32 1
%f32 = OpTypeFloat 32
%vec2i = OpTypeVector %i32 2
%vec4f = OpTypeVector %f32 4
%vec4f_0 = OpConstantNull %vec4f
%coord = OpConstantNull %vec2i
%workgroup = OpConstant %u32 2
%img_ty = OpTypeImage %f32 2D 0 0 0 2 Rgba32f
%img_ptr = OpTypePointer UniformConstant %img_ty
%img = OpVariable %img_ptr UniformConstant
%main = OpFunction %void None %fn
%start = OpLabel
%i = OpLoad %img_ty %img
)" + body +
R"(
OpReturn
OpFunctionEnd
)";
}
// OpImageRead emits the mask followed by the MakeTexelVisible scope id.
TEST_F(SpirvParserTest, ImageRead_MakeTexelVisible) {
EXPECT_IR_SPV(MemoryModelImageModule(R"(
%1 = OpImageRead %vec4f %i %coord MakeTexelVisible|NonPrivateTexel %workgroup
)"),
R"(
$B1: { # root
%1:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, rw_op_compatible, rgba32float, read_write>, read> = var @binding_point(0, 0)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
%3:spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, rw_op_compatible, rgba32float, read_write> = load %1
%4:vec4<f32> = spirv.image_read %3, vec2<i32>(0i), 1536u, 2u
ret
}
}
)",
SPV_ENV_VULKAN_1_1);
}
// OpImageWrite emits the mask followed by the MakeTexelAvailable scope id.
TEST_F(SpirvParserTest, ImageWrite_MakeTexelAvailable) {
EXPECT_IR_SPV(MemoryModelImageModule(R"(
OpImageWrite %i %coord %vec4f_0 MakeTexelAvailable|NonPrivateTexel %workgroup
)"),
R"(
$B1: { # root
%1:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, rw_op_compatible, rgba32float, read_write>, read> = var @binding_point(0, 0)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
%3:spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, rw_op_compatible, rgba32float, read_write> = load %1
%4:void = spirv.image_write %3, vec2<i32>(0i), vec4<f32>(0.0f), 1280u, 2u
ret
}
}
)",
SPV_ENV_VULKAN_1_1);
}
// Operands with no id, such as VolatileTexel, only change the mask.
TEST_F(SpirvParserTest, ImageWrite_VolatileTexel) {
EXPECT_IR_SPV(MemoryModelImageModule(R"(
OpImageWrite %i %coord %vec4f_0 VolatileTexel
)"),
R"(
$B1: { # root
%1:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, rw_op_compatible, rgba32float, read_write>, read> = var @binding_point(0, 0)
}
%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
$B2: {
%3:spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, rw_op_compatible, rgba32float, read_write> = load %1
%4:void = spirv.image_write %3, vec2<i32>(0i), vec4<f32>(0.0f), 2048u
ret
}
}
)",
SPV_ENV_VULKAN_1_1);
}
// OpSampledImage normalizes the result's image type to the input image's type when their Depth
// operands differ, and OpImage uses that same image type:
// https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpSampledImage
TEST_F(SpirvParserTest, SampledImage_DepthMismatch) {
EXPECT_IR(R"(
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main"
OpExecutionMode %main OriginUpperLeft
OpDecorate %tex DescriptorSet 0
OpDecorate %tex Binding 0
OpDecorate %sam DescriptorSet 0
OpDecorate %sam Binding 1
%void = OpTypeVoid
%fn = OpTypeFunction %void
%float = OpTypeFloat 32
%v3 = OpTypeVector %float 3
%image = OpTypeImage %float 2D 0 1 0 1 Unknown
%depth = OpTypeImage %float 2D 1 1 0 1 Unknown
%sampled = OpTypeSampledImage %depth
%samtype = OpTypeSampler
%ptrimage = OpTypePointer UniformConstant %image
%ptrsam = OpTypePointer UniformConstant %samtype
%tex = OpVariable %ptrimage UniformConstant
%sam = OpVariable %ptrsam UniformConstant
%zero = OpConstant %float 0
%half = OpConstant %float 0.5
%coord = OpConstantComposite %v3 %half %half %zero
%main = OpFunction %void None %fn
%entry = OpLabel
%im = OpLoad %image %tex
%sa = OpLoad %samtype %sam
%si = OpSampledImage %sampled %im %sa
%val = OpImageSampleDrefExplicitLod %float %si %coord %half Lod %zero
%ex = OpImage %depth %si
OpReturn
OpFunctionEnd
)",
R"(
$B1: { # root
%1:ptr<handle, spirv.image<f32, 2d, not_depth, arrayed, single_sampled, sampling_compatible, undefined, read_write>, read> = var @binding_point(0, 0)
%2:ptr<handle, sampler, read> = var @binding_point(0, 1)
}
%main = @fragment func():void {
$B2: {
%4:spirv.image<f32, 2d, not_depth, arrayed, single_sampled, sampling_compatible, undefined, read_write> = load %1
%5:sampler = load %2
%6:spirv.sampled_image<spirv.image<f32, 2d, not_depth, arrayed, single_sampled, sampling_compatible, undefined, read_write>> = spirv.op_sampled_image<spirv.image<f32, 2d, not_depth, arrayed, single_sampled, sampling_compatible, undefined, read_write>> %4, %5
%7:f32 = spirv.image_sample_dref_explicit_lod %6, vec3<f32>(0.5f, 0.5f, 0.0f), 0.5f, 2u, 0.0f
%8:spirv.image<f32, 2d, not_depth, arrayed, single_sampled, sampling_compatible, undefined, read_write> = spirv.op_image<spirv.image<f32, 2d, not_depth, arrayed, single_sampled, sampling_compatible, undefined, read_write>> %6
ret
}
}
)");
}
} // namespace
} // namespace tint::spirv::reader