1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
// Copyright (c) 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <vector>
#include "gmock/gmock.h"
#include "source/opt/convert_to_sampled_image_pass.h"
#include "test/opt/pass_fixture.h"
#include "test/opt/pass_utils.h"
namespace spvtools {
namespace opt {
namespace {
using testing::Eq;
using VectorOfDescriptorSetAndBindingPairs =
std::vector<DescriptorSetAndBinding>;
struct DescriptorSetAndBindingStringParsingTestCase {
const char* descriptor_set_binding_str;
bool expect_success;
VectorOfDescriptorSetAndBindingPairs expected_descriptor_set_binding_pairs;
};
using DescriptorSetAndBindingStringParsingTest =
::testing::TestWithParam<DescriptorSetAndBindingStringParsingTestCase>;
TEST_P(DescriptorSetAndBindingStringParsingTest, TestCase) {
const auto& tc = GetParam();
auto actual_descriptor_set_binding_pairs =
ConvertToSampledImagePass::ParseDescriptorSetBindingPairsString(
tc.descriptor_set_binding_str);
if (tc.expect_success) {
EXPECT_NE(nullptr, actual_descriptor_set_binding_pairs);
if (actual_descriptor_set_binding_pairs) {
EXPECT_THAT(*actual_descriptor_set_binding_pairs,
Eq(tc.expected_descriptor_set_binding_pairs));
}
} else {
EXPECT_EQ(nullptr, actual_descriptor_set_binding_pairs);
}
}
INSTANTIATE_TEST_SUITE_P(
ValidString, DescriptorSetAndBindingStringParsingTest,
::testing::ValuesIn(std::vector<
DescriptorSetAndBindingStringParsingTestCase>{
// 0. empty vector
{"", true, VectorOfDescriptorSetAndBindingPairs({})},
// 1. one pair
{"100:1024", true,
VectorOfDescriptorSetAndBindingPairs({DescriptorSetAndBinding{100,
1024}})},
// 2. two pairs
{"100:1024 200:2048", true,
VectorOfDescriptorSetAndBindingPairs(
{DescriptorSetAndBinding{100, 1024},
DescriptorSetAndBinding{200, 2048}})},
// 3. spaces between entries
{"100:1024 \n \r \t \v \f 200:2048", true,
VectorOfDescriptorSetAndBindingPairs(
{DescriptorSetAndBinding{100, 1024},
DescriptorSetAndBinding{200, 2048}})},
// 4. \t, \n, \r and spaces before spec id
{" \n \r\t \t \v \f 100:1024", true,
VectorOfDescriptorSetAndBindingPairs({DescriptorSetAndBinding{100,
1024}})},
// 5. \t, \n, \r and spaces after value string
{"100:1024 \n \r\t \t \v \f ", true,
VectorOfDescriptorSetAndBindingPairs({DescriptorSetAndBinding{100,
1024}})},
// 6. maximum spec id
{"4294967295:0", true,
VectorOfDescriptorSetAndBindingPairs({DescriptorSetAndBinding{
4294967295, 0}})},
// 7. minimum spec id
{"0:100", true,
VectorOfDescriptorSetAndBindingPairs({DescriptorSetAndBinding{0,
100}})},
// 8. multiple entries
{"101:1 102:2 103:3 104:4 200:201 9999:1000", true,
VectorOfDescriptorSetAndBindingPairs(
{DescriptorSetAndBinding{101, 1}, DescriptorSetAndBinding{102, 2},
DescriptorSetAndBinding{103, 3}, DescriptorSetAndBinding{104, 4},
DescriptorSetAndBinding{200, 201},
DescriptorSetAndBinding{9999, 1000}})},
}));
INSTANTIATE_TEST_SUITE_P(
InvalidString, DescriptorSetAndBindingStringParsingTest,
::testing::ValuesIn(
std::vector<DescriptorSetAndBindingStringParsingTestCase>{
// 0. missing default value
{"100:", false, VectorOfDescriptorSetAndBindingPairs{}},
// 1. descriptor set is not an integer
{"100.0:200", false, VectorOfDescriptorSetAndBindingPairs{}},
// 2. descriptor set is not a number
{"something_not_a_number:1", false,
VectorOfDescriptorSetAndBindingPairs{}},
// 3. only descriptor set number
{"100", false, VectorOfDescriptorSetAndBindingPairs{}},
// 4. empty descriptor set
{":3", false, VectorOfDescriptorSetAndBindingPairs{}},
// 5. only colon
{":", false, VectorOfDescriptorSetAndBindingPairs{}},
// 6. descriptor set overflow
{"4294967296:200", false, VectorOfDescriptorSetAndBindingPairs{}},
// 7. descriptor set less than 0
{"-1:200", false, VectorOfDescriptorSetAndBindingPairs{}},
// 8. nullptr
{nullptr, false, VectorOfDescriptorSetAndBindingPairs{}},
// 9. only a number is invalid
{"1234", false, VectorOfDescriptorSetAndBindingPairs{}},
// 10. invalid entry separator
{"12:34;23:14", false, VectorOfDescriptorSetAndBindingPairs{}},
// 11. invalid descriptor set and default value separator
{"12@34", false, VectorOfDescriptorSetAndBindingPairs{}},
// 12. spaces before colon
{"100 :1024", false, VectorOfDescriptorSetAndBindingPairs{}},
// 13. spaces after colon
{"100: 1024", false, VectorOfDescriptorSetAndBindingPairs{}},
// 14. descriptor set represented in hex float format is invalid
{"0x3p10:200", false, VectorOfDescriptorSetAndBindingPairs{}},
}));
std::string BuildShader(const char* shader_decorate_instructions,
const char* shader_image_and_sampler_variables,
const char* shader_body) {
// Base HLSL code:
//
// SamplerState sam : register(s2);
// Texture2D <float4> texture : register(t5);
//
// float4 main() : SV_TARGET {
// return texture.SampleLevel(sam, float2(1, 2), 10, 2);
// }
std::stringstream ss;
ss << R"(
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %out_var_SV_TARGET
OpExecutionMode %main OriginUpperLeft
OpSource HLSL 600
OpName %type_sampler "type.sampler"
OpName %type_2d_image "type.2d.image"
OpName %out_var_SV_TARGET "out.var.SV_TARGET"
OpName %main "main"
OpName %type_sampled_image "type.sampled.image"
OpDecorate %out_var_SV_TARGET Location 0
)";
ss << shader_decorate_instructions;
ss << R"(
%float = OpTypeFloat 32
%float_1 = OpConstant %float 1
%float_2 = OpConstant %float 2
%v2float = OpTypeVector %float 2
%12 = OpConstantComposite %v2float %float_1 %float_2
%float_10 = OpConstant %float 10
%int = OpTypeInt 32 1
%int_2 = OpConstant %int 2
%v2int = OpTypeVector %int 2
%17 = OpConstantComposite %v2int %int_2 %int_2
%type_sampler = OpTypeSampler
%_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
%type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
%_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
%v4float = OpTypeVector %float 4
%_ptr_Output_v4float = OpTypePointer Output %v4float
%void = OpTypeVoid
%23 = OpTypeFunction %void
%type_sampled_image = OpTypeSampledImage %type_2d_image
)";
ss << shader_image_and_sampler_variables;
ss << R"(
%out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
%main = OpFunction %void None %23
%24 = OpLabel
)";
ss << shader_body;
ss << R"(
OpReturn
OpFunctionEnd
)";
return ss.str();
}
using ConvertToSampledImageTest = PassTest<::testing::Test>;
TEST_F(ConvertToSampledImageTest, Texture2DAndSamplerToSampledImage) {
const std::string shader = BuildShader(
R"(
OpDecorate %sam DescriptorSet 0
OpDecorate %sam Binding 5
OpDecorate %texture DescriptorSet 0
OpDecorate %texture Binding 5
)",
R"(
; CHECK-NOT: OpVariable %_ptr_UniformConstant_type_2d_image
; CHECK: [[tex:%\w+]] = OpVariable %_ptr_UniformConstant_type_sampled_image UniformConstant
%sam = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
%texture = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
)",
R"(
; CHECK: [[load:%\w+]] = OpLoad %type_sampled_image [[tex]]
; CHECK: OpImageSampleExplicitLod %v4float [[load]]
%25 = OpLoad %type_2d_image %texture
%26 = OpLoad %type_sampler %sam
%27 = OpSampledImage %type_sampled_image %25 %26
%28 = OpImageSampleExplicitLod %v4float %27 %12 Lod|ConstOffset %float_10 %17
OpStore %out_var_SV_TARGET %28
)");
auto result = SinglePassRunAndMatch<ConvertToSampledImagePass>(
shader, /* do_validate = */ true,
VectorOfDescriptorSetAndBindingPairs{DescriptorSetAndBinding{0, 5}});
EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
}
TEST_F(ConvertToSampledImageTest, Texture2DToSampledImage) {
const std::string shader = BuildShader(
R"(
OpDecorate %sam DescriptorSet 0
OpDecorate %sam Binding 2
OpDecorate %texture DescriptorSet 0
OpDecorate %texture Binding 5
)",
R"(
; CHECK: [[tex:%\w+]] = OpVariable %_ptr_UniformConstant_type_sampled_image UniformConstant
%sam = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
%texture = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
)",
R"(
; CHECK: [[load:%\w+]] = OpLoad %type_sampled_image [[tex]]
; CHECK: [[image_extraction:%\w+]] = OpImage %type_2d_image [[load]]
; CHECK: OpSampledImage %type_sampled_image [[image_extraction]]
%25 = OpLoad %type_2d_image %texture
%26 = OpLoad %type_sampler %sam
%27 = OpSampledImage %type_sampled_image %25 %26
%28 = OpImageSampleExplicitLod %v4float %27 %12 Lod|ConstOffset %float_10 %17
OpStore %out_var_SV_TARGET %28
)");
auto result = SinglePassRunAndMatch<ConvertToSampledImagePass>(
shader, /* do_validate = */ true,
VectorOfDescriptorSetAndBindingPairs{DescriptorSetAndBinding{0, 5}});
EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
}
TEST_F(ConvertToSampledImageTest, SamplerToSampledImage) {
const std::string shader = BuildShader(
R"(
OpDecorate %sam DescriptorSet 0
OpDecorate %sam Binding 2
OpDecorate %texture DescriptorSet 0
OpDecorate %texture Binding 5
)",
R"(
%sam = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
%texture = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
)",
R"(
%25 = OpLoad %type_2d_image %texture
%26 = OpLoad %type_sampler %sam
%27 = OpSampledImage %type_sampled_image %25 %26
%28 = OpImageSampleExplicitLod %v4float %27 %12 Lod|ConstOffset %float_10 %17
OpStore %out_var_SV_TARGET %28
)");
auto result = SinglePassRunToBinary<ConvertToSampledImagePass>(
shader, /* skip_nop = */ false,
VectorOfDescriptorSetAndBindingPairs{DescriptorSetAndBinding{0, 2}});
EXPECT_EQ(std::get<1>(result), Pass::Status::Failure);
}
TEST_F(ConvertToSampledImageTest, TwoImagesWithDuplicatedDescriptorSetBinding) {
const std::string shader = BuildShader(
R"(
OpDecorate %sam DescriptorSet 0
OpDecorate %sam Binding 2
OpDecorate %texture0 DescriptorSet 0
OpDecorate %texture0 Binding 5
OpDecorate %texture1 DescriptorSet 0
OpDecorate %texture1 Binding 5
)",
R"(
%sam = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
%texture0 = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
%texture1 = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
)",
R"(
%25 = OpLoad %type_2d_image %texture0
%26 = OpLoad %type_sampler %sam
%27 = OpSampledImage %type_sampled_image %25 %26
%28 = OpImageSampleExplicitLod %v4float %27 %12 Lod|ConstOffset %float_10 %17
OpStore %out_var_SV_TARGET %28
)");
auto result = SinglePassRunToBinary<ConvertToSampledImagePass>(
shader, /* skip_nop = */ false,
VectorOfDescriptorSetAndBindingPairs{DescriptorSetAndBinding{0, 5}});
EXPECT_EQ(std::get<1>(result), Pass::Status::Failure);
}
TEST_F(ConvertToSampledImageTest,
TwoSamplersWithDuplicatedDescriptorSetBinding) {
const std::string shader = BuildShader(
R"(
OpDecorate %sam0 DescriptorSet 0
OpDecorate %sam0 Binding 2
OpDecorate %sam1 DescriptorSet 0
OpDecorate %sam1 Binding 2
OpDecorate %texture DescriptorSet 0
OpDecorate %texture Binding 5
)",
R"(
%sam0 = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
%sam1 = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
%texture = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
)",
R"(
%25 = OpLoad %type_2d_image %texture
%26 = OpLoad %type_sampler %sam0
%27 = OpSampledImage %type_sampled_image %25 %26
%28 = OpImageSampleExplicitLod %v4float %27 %12 Lod|ConstOffset %float_10 %17
OpStore %out_var_SV_TARGET %28
)");
auto result = SinglePassRunToBinary<ConvertToSampledImagePass>(
shader, /* skip_nop = */ false,
VectorOfDescriptorSetAndBindingPairs{DescriptorSetAndBinding{0, 2}});
EXPECT_EQ(std::get<1>(result), Pass::Status::Failure);
}
} // namespace
} // namespace opt
} // namespace spvtools
|