| 1 |
// Copyright (c) 2021 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include <string> |
| 16 |
|
| 17 |
#include "test/opt/pass_fixture.h" |
| 18 |
#include "test/opt/pass_utils.h" |
| 19 |
|
| 20 |
namespace spvtools { |
| 21 |
namespace opt { |
| 22 |
namespace { |
| 23 |
|
| 24 |
using ReplaceDescArrayAccessUsingVarIndexTest = PassTest<::testing::Test>; |
| 25 |
|
| 26 |
TEST_F(ReplaceDescArrayAccessUsingVarIndexTest, |
| 27 |
ReplaceAccessChainToTextureArray) { |
| 28 |
const std::string text = R"( |
| 29 |
OpCapability Shader |
| 30 |
OpMemoryModel Logical GLSL450 |
| 31 |
OpEntryPoint Fragment %psmain "psmain" %gl_FragCoord %in_var_INSTANCEID %out_var_SV_TARGET |
| 32 |
OpExecutionMode %psmain OriginUpperLeft |
| 33 |
OpSource HLSL 600 |
| 34 |
OpName %type_sampler "type.sampler" |
| 35 |
OpName %Sampler0 "Sampler0" |
| 36 |
OpName %type_2d_image "type.2d.image" |
| 37 |
OpName %Tex0 "Tex0" |
| 38 |
OpName %in_var_INSTANCEID "in.var.INSTANCEID" |
| 39 |
OpName %out_var_SV_TARGET "out.var.SV_TARGET" |
| 40 |
OpName %psmain "psmain" |
| 41 |
OpName %type_sampled_image "type.sampled.image" |
| 42 |
OpDecorate %gl_FragCoord BuiltIn FragCoord |
| 43 |
OpDecorate %in_var_INSTANCEID Flat |
| 44 |
OpDecorate %in_var_INSTANCEID Location 0 |
| 45 |
OpDecorate %out_var_SV_TARGET Location 0 |
| 46 |
OpDecorate %Sampler0 DescriptorSet 0 |
| 47 |
OpDecorate %Sampler0 Binding 1 |
| 48 |
OpDecorate %Tex0 DescriptorSet 0 |
| 49 |
OpDecorate %Tex0 Binding 2 |
| 50 |
%bool = OpTypeBool |
| 51 |
%type_sampler = OpTypeSampler |
| 52 |
%_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler |
| 53 |
%uint = OpTypeInt 32 0 |
| 54 |
%uint_3 = OpConstant %uint 3 |
| 55 |
%float = OpTypeFloat 32 |
| 56 |
%type_2d_image = OpTypeImage %float 2D 2 0 0 0 Unknown |
| 57 |
%_arr_type_2d_image_uint_3 = OpTypeArray %type_2d_image %uint_3 |
| 58 |
%_ptr_UniformConstant__arr_type_2d_image_uint_3 = OpTypePointer UniformConstant %_arr_type_2d_image_uint_3 |
| 59 |
%v4float = OpTypeVector %float 4 |
| 60 |
%_ptr_Input_v4float = OpTypePointer Input %v4float |
| 61 |
%_ptr_Input_uint = OpTypePointer Input %uint |
| 62 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 63 |
%void = OpTypeVoid |
| 64 |
%21 = OpTypeFunction %void |
| 65 |
%_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image |
| 66 |
%v2float = OpTypeVector %float 2 |
| 67 |
%v2uint = OpTypeVector %uint 2 |
| 68 |
%uint_0 = OpConstant %uint 0 |
| 69 |
%uint_1 = OpConstant %uint 1 |
| 70 |
%27 = OpConstantComposite %v2uint %uint_0 %uint_1 |
| 71 |
%type_sampled_image = OpTypeSampledImage %type_2d_image |
| 72 |
%Sampler0 = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant |
| 73 |
%Tex0 = OpVariable %_ptr_UniformConstant__arr_type_2d_image_uint_3 UniformConstant |
| 74 |
%gl_FragCoord = OpVariable %_ptr_Input_v4float Input |
| 75 |
%in_var_INSTANCEID = OpVariable %_ptr_Input_uint Input |
| 76 |
%out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output |
| 77 |
%uint_2 = OpConstant %uint 2 |
| 78 |
%66 = OpConstantNull %v4float |
| 79 |
|
| 80 |
; CHECK: [[null_value:%\w+]] = OpConstantNull %v4float |
| 81 |
|
| 82 |
%psmain = OpFunction %void None %21 |
| 83 |
%39 = OpLabel |
| 84 |
%29 = OpLoad %v4float %gl_FragCoord |
| 85 |
%30 = OpLoad %uint %in_var_INSTANCEID |
| 86 |
%37 = OpIEqual %bool %30 %uint_2 |
| 87 |
OpSelectionMerge %38 None |
| 88 |
OpBranchConditional %37 %28 %40 |
| 89 |
|
| 90 |
; CHECK: [[var_index:%\w+]] = OpLoad %uint %in_var_INSTANCEID |
| 91 |
; CHECK: OpSelectionMerge [[cond_branch_merge:%\w+]] None |
| 92 |
; CHECK: OpBranchConditional {{%\w+}} {{%\w+}} [[bb_cond_br:%\w+]] |
| 93 |
|
| 94 |
%28 = OpLabel |
| 95 |
%31 = OpAccessChain %_ptr_UniformConstant_type_2d_image %Tex0 %30 |
| 96 |
%32 = OpLoad %type_2d_image %31 |
| 97 |
OpImageWrite %32 %27 %29 |
| 98 |
|
| 99 |
; CHECK: OpSelectionMerge [[merge:%\w+]] None |
| 100 |
; CHECK: OpSwitch [[var_index]] [[default:%\w+]] 0 [[case0:%\w+]] 1 [[case1:%\w+]] 2 [[case2:%\w+]] |
| 101 |
; CHECK: [[case0]] = OpLabel |
| 102 |
; CHECK: OpAccessChain |
| 103 |
; CHECK: OpLoad |
| 104 |
; CHECK: OpImageWrite |
| 105 |
; CHECK: OpBranch [[merge]] |
| 106 |
; CHECK: [[case1]] = OpLabel |
| 107 |
; CHECK: OpAccessChain |
| 108 |
; CHECK: OpLoad |
| 109 |
; CHECK: OpImageWrite |
| 110 |
; CHECK: OpBranch [[merge]] |
| 111 |
; CHECK: [[case2]] = OpLabel |
| 112 |
; CHECK: OpAccessChain |
| 113 |
; CHECK: OpLoad |
| 114 |
; CHECK: OpImageWrite |
| 115 |
; CHECK: OpBranch [[merge]] |
| 116 |
; CHECK: [[default]] = OpLabel |
| 117 |
; CHECK: OpBranch [[merge]] |
| 118 |
; CHECK: [[merge]] = OpLabel |
| 119 |
|
| 120 |
%33 = OpLoad %type_sampler %Sampler0 |
| 121 |
%34 = OpVectorShuffle %v2float %29 %29 0 1 |
| 122 |
%35 = OpSampledImage %type_sampled_image %32 %33 |
| 123 |
%36 = OpImageSampleImplicitLod %v4float %35 %34 None |
| 124 |
|
| 125 |
; CHECK: OpSelectionMerge [[merge:%\w+]] None |
| 126 |
; CHECK: OpSwitch [[var_index]] [[default:%\w+]] 0 [[case0:%\w+]] 1 [[case1:%\w+]] 2 [[case2:%\w+]] |
| 127 |
; CHECK: [[case0]] = OpLabel |
| 128 |
; CHECK: [[ac:%\w+]] = OpAccessChain %_ptr_UniformConstant_type_2d_image %Tex0 %uint_0 |
| 129 |
; CHECK: [[sam:%\w+]] = OpLoad %type_sampler %Sampler0 |
| 130 |
; CHECK: [[img:%\w+]] = OpLoad %type_2d_image [[ac]] |
| 131 |
; CHECK: [[sampledImg:%\w+]] = OpSampledImage %type_sampled_image [[img]] [[sam]] |
| 132 |
; CHECK: [[value0:%\w+]] = OpImageSampleImplicitLod %v4float [[sampledImg]] |
| 133 |
; CHECK: OpBranch [[merge]] |
| 134 |
; CHECK: [[case1]] = OpLabel |
| 135 |
; CHECK: [[ac:%\w+]] = OpAccessChain %_ptr_UniformConstant_type_2d_image %Tex0 %uint_1 |
| 136 |
; CHECK: [[sam:%\w+]] = OpLoad %type_sampler %Sampler0 |
| 137 |
; CHECK: [[img:%\w+]] = OpLoad %type_2d_image [[ac]] |
| 138 |
; CHECK: [[sampledImg:%\w+]] = OpSampledImage %type_sampled_image [[img]] [[sam]] |
| 139 |
; CHECK: [[value1:%\w+]] = OpImageSampleImplicitLod %v4float [[sampledImg]] |
| 140 |
; CHECK: OpBranch [[merge]] |
| 141 |
; CHECK: [[case2]] = OpLabel |
| 142 |
; CHECK: [[ac:%\w+]] = OpAccessChain %_ptr_UniformConstant_type_2d_image %Tex0 %uint_2 |
| 143 |
; CHECK: [[sam:%\w+]] = OpLoad %type_sampler %Sampler0 |
| 144 |
; CHECK: [[img:%\w+]] = OpLoad %type_2d_image [[ac]] |
| 145 |
; CHECK: [[sampledImg:%\w+]] = OpSampledImage %type_sampled_image [[img]] [[sam]] |
| 146 |
; CHECK: [[value2:%\w+]] = OpImageSampleImplicitLod %v4float [[sampledImg]] |
| 147 |
; CHECK: OpBranch [[merge]] |
| 148 |
; CHECK: [[default]] = OpLabel |
| 149 |
; CHECK: OpBranch [[merge]] |
| 150 |
; CHECK: [[merge]] = OpLabel |
| 151 |
; CHECK: [[phi0:%\w+]] = OpPhi %v4float [[value0]] [[case0]] [[value1]] [[case1]] [[value2]] [[case2]] [[null_value]] [[default]] |
| 152 |
|
| 153 |
OpBranch %38 |
| 154 |
%40 = OpLabel |
| 155 |
OpBranch %38 |
| 156 |
%38 = OpLabel |
| 157 |
%41 = OpPhi %v4float %36 %28 %29 %40 |
| 158 |
|
| 159 |
; CHECK: OpBranch [[cond_branch_merge]] |
| 160 |
; CHECK: [[bb_cond_br]] = OpLabel |
| 161 |
; CHECK: OpBranch [[cond_branch_merge]] |
| 162 |
; CHECK: [[cond_branch_merge]] = OpLabel |
| 163 |
; CHECK: [[phi1:%\w+]] = OpPhi %v4float [[phi0]] [[merge]] {{%\w+}} [[bb_cond_br]] |
| 164 |
; CHECK: OpStore {{%\w+}} [[phi1]] |
| 165 |
|
| 166 |
OpStore %out_var_SV_TARGET %41 |
| 167 |
OpReturn |
| 168 |
OpFunctionEnd |
| 169 |
)"; |
| 170 |
|
| 171 |
SinglePassRunAndMatch<ReplaceDescArrayAccessUsingVarIndex>(text, true); |
| 172 |
} |
| 173 |
|
| 174 |
TEST_F(ReplaceDescArrayAccessUsingVarIndexTest, |
| 175 |
ReplaceAccessChainToTextureArrayAndSamplerArray) { |
| 176 |
const std::string text = R"( |
| 177 |
OpCapability Shader |
| 178 |
OpMemoryModel Logical GLSL450 |
| 179 |
OpEntryPoint Fragment %psmain "psmain" %gl_FragCoord %in_var_INSTANCEID %out_var_SV_TARGET |
| 180 |
OpExecutionMode %psmain OriginUpperLeft |
| 181 |
OpSource HLSL 600 |
| 182 |
OpName %type_sampler "type.sampler" |
| 183 |
OpName %Sampler0 "Sampler0" |
| 184 |
OpName %type_2d_image "type.2d.image" |
| 185 |
OpName %Tex0 "Tex0" |
| 186 |
OpName %in_var_INSTANCEID "in.var.INSTANCEID" |
| 187 |
OpName %out_var_SV_TARGET "out.var.SV_TARGET" |
| 188 |
OpName %psmain "psmain" |
| 189 |
OpName %type_sampled_image "type.sampled.image" |
| 190 |
OpDecorate %gl_FragCoord BuiltIn FragCoord |
| 191 |
OpDecorate %in_var_INSTANCEID Flat |
| 192 |
OpDecorate %in_var_INSTANCEID Location 0 |
| 193 |
OpDecorate %out_var_SV_TARGET Location 0 |
| 194 |
OpDecorate %Sampler0 DescriptorSet 0 |
| 195 |
OpDecorate %Sampler0 Binding 1 |
| 196 |
OpDecorate %Tex0 DescriptorSet 0 |
| 197 |
OpDecorate %Tex0 Binding 2 |
| 198 |
%type_sampler = OpTypeSampler |
| 199 |
%uint = OpTypeInt 32 0 |
| 200 |
%uint_2 = OpConstant %uint 2 |
| 201 |
%_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler |
| 202 |
%_arr_type_sampler_uint_2 = OpTypeArray %type_sampler %uint_2 |
| 203 |
%_ptr_UniformConstant__arr_type_sampler_uint_2 = OpTypePointer UniformConstant %_arr_type_sampler_uint_2 |
| 204 |
%float = OpTypeFloat 32 |
| 205 |
%type_2d_image = OpTypeImage %float 2D 2 0 0 0 Unknown |
| 206 |
%_arr_type_2d_image_uint_2 = OpTypeArray %type_2d_image %uint_2 |
| 207 |
%_ptr_UniformConstant__arr_type_2d_image_uint_2 = OpTypePointer UniformConstant %_arr_type_2d_image_uint_2 |
| 208 |
%v4float = OpTypeVector %float 4 |
| 209 |
%_ptr_Input_v4float = OpTypePointer Input %v4float |
| 210 |
%_ptr_Input_uint = OpTypePointer Input %uint |
| 211 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 212 |
%void = OpTypeVoid |
| 213 |
%21 = OpTypeFunction %void |
| 214 |
%_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image |
| 215 |
%v2float = OpTypeVector %float 2 |
| 216 |
%v2uint = OpTypeVector %uint 2 |
| 217 |
%uint_0 = OpConstant %uint 0 |
| 218 |
%uint_1 = OpConstant %uint 1 |
| 219 |
%27 = OpConstantComposite %v2uint %uint_0 %uint_1 |
| 220 |
%type_sampled_image = OpTypeSampledImage %type_2d_image |
| 221 |
%Sampler0 = OpVariable %_ptr_UniformConstant__arr_type_sampler_uint_2 UniformConstant |
| 222 |
%Tex0 = OpVariable %_ptr_UniformConstant__arr_type_2d_image_uint_2 UniformConstant |
| 223 |
%gl_FragCoord = OpVariable %_ptr_Input_v4float Input |
| 224 |
%in_var_INSTANCEID = OpVariable %_ptr_Input_uint Input |
| 225 |
%out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output |
| 226 |
%66 = OpConstantNull %v4float |
| 227 |
%psmain = OpFunction %void None %21 |
| 228 |
%28 = OpLabel |
| 229 |
%29 = OpLoad %v4float %gl_FragCoord |
| 230 |
%30 = OpLoad %uint %in_var_INSTANCEID |
| 231 |
%31 = OpAccessChain %_ptr_UniformConstant_type_2d_image %Tex0 %30 |
| 232 |
%32 = OpLoad %type_2d_image %31 |
| 233 |
OpImageWrite %32 %27 %29 |
| 234 |
|
| 235 |
; CHECK: [[null_value:%\w+]] = OpConstantNull %v4float |
| 236 |
|
| 237 |
; CHECK: [[var_index:%\w+]] = OpLoad %uint %in_var_INSTANCEID |
| 238 |
; CHECK: OpSelectionMerge [[merge:%\w+]] None |
| 239 |
; CHECK: OpSwitch [[var_index]] [[default:%\w+]] 0 [[case0:%\w+]] 1 [[case1:%\w+]] |
| 240 |
; CHECK: [[case0]] = OpLabel |
| 241 |
; CHECK: OpAccessChain |
| 242 |
; CHECK: OpLoad |
| 243 |
; CHECK: OpImageWrite |
| 244 |
; CHECK: OpBranch [[merge]] |
| 245 |
; CHECK: [[case1]] = OpLabel |
| 246 |
; CHECK: OpAccessChain |
| 247 |
; CHECK: OpLoad |
| 248 |
; CHECK: OpImageWrite |
| 249 |
; CHECK: OpBranch [[merge]] |
| 250 |
; CHECK: [[default]] = OpLabel |
| 251 |
; CHECK: OpBranch [[merge]] |
| 252 |
; CHECK: [[merge]] = OpLabel |
| 253 |
|
| 254 |
%33 = OpAccessChain %_ptr_UniformConstant_type_sampler %Sampler0 %30 |
| 255 |
%37 = OpLoad %type_sampler %33 |
| 256 |
%34 = OpVectorShuffle %v2float %29 %29 0 1 |
| 257 |
%35 = OpSampledImage %type_sampled_image %32 %37 |
| 258 |
%36 = OpImageSampleImplicitLod %v4float %35 %34 None |
| 259 |
|
| 260 |
; SPIR-V instructions to be replaced (will be killed by ADCE) |
| 261 |
; CHECK: OpSelectionMerge |
| 262 |
; CHECK: OpSwitch |
| 263 |
|
| 264 |
; CHECK: OpSelectionMerge [[merge_sampler:%\w+]] None |
| 265 |
; CHECK: OpSwitch [[var_index]] [[default_sampler:%\w+]] 0 [[case_sampler0:%\w+]] 1 [[case_sampler1:%\w+]] |
| 266 |
|
| 267 |
; CHECK: [[case_sampler0]] = OpLabel |
| 268 |
; CHECK: OpSelectionMerge [[merge_texture0:%\w+]] None |
| 269 |
; CHECK: OpSwitch [[var_index]] [[default_texture:%\w+]] 0 [[case_texture0:%\w+]] 1 [[case_texture1:%\w+]] |
| 270 |
; CHECK: [[case_texture0]] = OpLabel |
| 271 |
; CHECK: [[pt0:%\w+]] = OpAccessChain %_ptr_UniformConstant_type_2d_image %Tex0 %uint_0 |
| 272 |
; CHECK: [[ps0:%\w+]] = OpAccessChain %_ptr_UniformConstant_type_sampler %Sampler0 %uint_0 |
| 273 |
; CHECK: [[s0:%\w+]] = OpLoad %type_sampler [[ps0]] |
| 274 |
; CHECK: [[t0:%\w+]] = OpLoad %type_2d_image [[pt0]] |
| 275 |
; CHECK: [[sampledImg0:%\w+]] = OpSampledImage %type_sampled_image [[t0]] [[s0]] |
| 276 |
; CHECK: [[value0:%\w+]] = OpImageSampleImplicitLod %v4float [[sampledImg0]] |
| 277 |
; CHECK: OpBranch [[merge_texture0]] |
| 278 |
; CHECK: [[case_texture1]] = OpLabel |
| 279 |
; CHECK: [[pt1:%\w+]] = OpAccessChain %_ptr_UniformConstant_type_2d_image %Tex0 %uint_1 |
| 280 |
; CHECK: [[ps0:%\w+]] = OpAccessChain %_ptr_UniformConstant_type_sampler %Sampler0 %uint_0 |
| 281 |
; CHECK: [[s0:%\w+]] = OpLoad %type_sampler [[ps0]] |
| 282 |
; CHECK: [[t1:%\w+]] = OpLoad %type_2d_image [[pt1]] |
| 283 |
; CHECK: [[sampledImg1:%\w+]] = OpSampledImage %type_sampled_image [[t1]] [[s0]] |
| 284 |
; CHECK: [[value1:%\w+]] = OpImageSampleImplicitLod %v4float [[sampledImg1]] |
| 285 |
; CHECK: OpBranch [[merge_texture0]] |
| 286 |
; CHECK: [[default_texture]] = OpLabel |
| 287 |
; CHECK: OpBranch [[merge_texture0]] |
| 288 |
; CHECK: [[merge_texture0]] = OpLabel |
| 289 |
; CHECK: [[phi0:%\w+]] = OpPhi %v4float [[value0]] [[case_texture0]] [[value1]] [[case_texture1]] [[null_value]] [[default_texture]] |
| 290 |
; CHECK: OpBranch [[merge_sampler]] |
| 291 |
|
| 292 |
; CHECK: [[case_sampler1]] = OpLabel |
| 293 |
; CHECK: OpSelectionMerge [[merge_texture1:%\w+]] None |
| 294 |
; CHECK: OpSwitch [[var_index]] [[default_texture:%\w+]] 0 [[case_texture0:%\w+]] 1 [[case_texture1:%\w+]] |
| 295 |
; CHECK: [[case_texture0]] = OpLabel |
| 296 |
; CHECK: [[pt0:%\w+]] = OpAccessChain %_ptr_UniformConstant_type_2d_image %Tex0 %uint_0 |
| 297 |
; CHECK: [[ps1:%\w+]] = OpAccessChain %_ptr_UniformConstant_type_sampler %Sampler0 %uint_1 |
| 298 |
; CHECK: [[s1:%\w+]] = OpLoad %type_sampler [[ps1]] |
| 299 |
; CHECK: [[t0:%\w+]] = OpLoad %type_2d_image [[pt0]] |
| 300 |
; CHECK: [[sampledImg0:%\w+]] = OpSampledImage %type_sampled_image [[t0]] [[s1]] |
| 301 |
; CHECK: [[value0:%\w+]] = OpImageSampleImplicitLod %v4float [[sampledImg0]] |
| 302 |
; CHECK: OpBranch [[merge_texture1]] |
| 303 |
; CHECK: [[case_texture1]] = OpLabel |
| 304 |
; CHECK: [[pt1:%\w+]] = OpAccessChain %_ptr_UniformConstant_type_2d_image %Tex0 %uint_1 |
| 305 |
; CHECK: [[ps1:%\w+]] = OpAccessChain %_ptr_UniformConstant_type_sampler %Sampler0 %uint_1 |
| 306 |
; CHECK: [[s1:%\w+]] = OpLoad %type_sampler [[ps1]] |
| 307 |
; CHECK: [[t1:%\w+]] = OpLoad %type_2d_image [[pt1]] |
| 308 |
; CHECK: [[sampledImg1:%\w+]] = OpSampledImage %type_sampled_image [[t1]] [[s1]] |
| 309 |
; CHECK: [[value1:%\w+]] = OpImageSampleImplicitLod %v4float [[sampledImg1]] |
| 310 |
; CHECK: OpBranch [[merge_texture1]] |
| 311 |
; CHECK: [[default_texture]] = OpLabel |
| 312 |
; CHECK: OpBranch [[merge_texture1]] |
| 313 |
; CHECK: [[merge_texture1]] = OpLabel |
| 314 |
; CHECK: [[phi1:%\w+]] = OpPhi %v4float [[value0]] [[case_texture0]] [[value1]] [[case_texture1]] [[null_value]] [[default_texture]] |
| 315 |
|
| 316 |
; CHECK: [[default_sampler]] = OpLabel |
| 317 |
; CHECK: OpBranch [[merge_sampler]] |
| 318 |
; CHECK: [[merge_sampler]] = OpLabel |
| 319 |
; CHECK: OpPhi %v4float [[phi0]] [[merge_texture0]] [[phi1]] [[merge_texture1]] [[null_value]] [[default_sampler]] |
| 320 |
; CHECK: OpStore |
| 321 |
|
| 322 |
OpStore %out_var_SV_TARGET %36 |
| 323 |
OpReturn |
| 324 |
OpFunctionEnd |
| 325 |
)"; |
| 326 |
|
| 327 |
SinglePassRunAndMatch<ReplaceDescArrayAccessUsingVarIndex>(text, true); |
| 328 |
} |
| 329 |
|
| 330 |
TEST_F(ReplaceDescArrayAccessUsingVarIndexTest, |
| 331 |
ReplaceAccessChainToTextureArrayWithSingleElement) { |
| 332 |
const std::string text = R"( |
| 333 |
OpCapability Shader |
| 334 |
OpMemoryModel Logical GLSL450 |
| 335 |
OpEntryPoint Fragment %psmain "psmain" %gl_FragCoord %in_var_INSTANCEID %out_var_SV_TARGET |
| 336 |
OpExecutionMode %psmain OriginUpperLeft |
| 337 |
OpSource HLSL 600 |
| 338 |
OpName %type_sampler "type.sampler" |
| 339 |
OpName %Sampler0 "Sampler0" |
| 340 |
OpName %type_2d_image "type.2d.image" |
| 341 |
OpName %Tex0 "Tex0" |
| 342 |
OpName %in_var_INSTANCEID "in.var.INSTANCEID" |
| 343 |
OpName %out_var_SV_TARGET "out.var.SV_TARGET" |
| 344 |
OpName %psmain "psmain" |
| 345 |
OpName %type_sampled_image "type.sampled.image" |
| 346 |
OpDecorate %gl_FragCoord BuiltIn FragCoord |
| 347 |
OpDecorate %in_var_INSTANCEID Flat |
| 348 |
OpDecorate %in_var_INSTANCEID Location 0 |
| 349 |
OpDecorate %out_var_SV_TARGET Location 0 |
| 350 |
OpDecorate %Sampler0 DescriptorSet 0 |
| 351 |
OpDecorate %Sampler0 Binding 1 |
| 352 |
OpDecorate %Tex0 DescriptorSet 0 |
| 353 |
OpDecorate %Tex0 Binding 2 |
| 354 |
%type_sampler = OpTypeSampler |
| 355 |
%_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler |
| 356 |
%uint = OpTypeInt 32 0 |
| 357 |
%uint_1 = OpConstant %uint 1 |
| 358 |
%float = OpTypeFloat 32 |
| 359 |
%type_2d_image = OpTypeImage %float 2D 2 0 0 0 Unknown |
| 360 |
%_arr_type_2d_image_uint_1 = OpTypeArray %type_2d_image %uint_1 |
| 361 |
%_ptr_UniformConstant__arr_type_2d_image_uint_1 = OpTypePointer UniformConstant %_arr_type_2d_image_uint_1 |
| 362 |
%v4float = OpTypeVector %float 4 |
| 363 |
%_ptr_Input_v4float = OpTypePointer Input %v4float |
| 364 |
%_ptr_Input_uint = OpTypePointer Input %uint |
| 365 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 366 |
%void = OpTypeVoid |
| 367 |
%21 = OpTypeFunction %void |
| 368 |
%_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image |
| 369 |
%v2float = OpTypeVector %float 2 |
| 370 |
%v2uint = OpTypeVector %uint 2 |
| 371 |
%uint_0 = OpConstant %uint 0 |
| 372 |
%27 = OpConstantComposite %v2uint %uint_0 %uint_1 |
| 373 |
%type_sampled_image = OpTypeSampledImage %type_2d_image |
| 374 |
%Sampler0 = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant |
| 375 |
%Tex0 = OpVariable %_ptr_UniformConstant__arr_type_2d_image_uint_1 UniformConstant |
| 376 |
%gl_FragCoord = OpVariable %_ptr_Input_v4float Input |
| 377 |
%in_var_INSTANCEID = OpVariable %_ptr_Input_uint Input |
| 378 |
%out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output |
| 379 |
%uint_2 = OpConstant %uint 2 |
| 380 |
%66 = OpConstantNull %v4float |
| 381 |
%psmain = OpFunction %void None %21 |
| 382 |
%28 = OpLabel |
| 383 |
%29 = OpLoad %v4float %gl_FragCoord |
| 384 |
%30 = OpLoad %uint %in_var_INSTANCEID |
| 385 |
%31 = OpAccessChain %_ptr_UniformConstant_type_2d_image %Tex0 %30 |
| 386 |
%32 = OpLoad %type_2d_image %31 |
| 387 |
OpImageWrite %32 %27 %29 |
| 388 |
|
| 389 |
; CHECK: [[ac:%\w+]] = OpAccessChain %_ptr_UniformConstant_type_2d_image %Tex0 %uint_0 |
| 390 |
; CHECK-NOT: OpAccessChain |
| 391 |
; CHECK-NOT: OpSwitch |
| 392 |
; CHECK-NOT: OpPhi |
| 393 |
|
| 394 |
%33 = OpLoad %type_sampler %Sampler0 |
| 395 |
%34 = OpVectorShuffle %v2float %29 %29 0 1 |
| 396 |
%35 = OpSampledImage %type_sampled_image %32 %33 |
| 397 |
%36 = OpImageSampleImplicitLod %v4float %35 %34 None |
| 398 |
|
| 399 |
OpStore %out_var_SV_TARGET %36 |
| 400 |
OpReturn |
| 401 |
OpFunctionEnd |
| 402 |
)"; |
| 403 |
|
| 404 |
SinglePassRunAndMatch<ReplaceDescArrayAccessUsingVarIndex>(text, true); |
| 405 |
} |
| 406 |
|
| 407 |
TEST_F(ReplaceDescArrayAccessUsingVarIndexTest, ReplaceMultipleAccessChains) { |
| 408 |
const std::string text = R"( |
| 409 |
OpCapability Shader |
| 410 |
OpMemoryModel Logical GLSL450 |
| 411 |
OpEntryPoint Fragment %1 "TestFragment" %2 |
| 412 |
OpExecutionMode %1 OriginUpperLeft |
| 413 |
OpName %11 "type.ConstantBuffer.TestStruct" |
| 414 |
OpMemberName %11 0 "val1" |
| 415 |
OpMemberName %11 1 "val2" |
| 416 |
OpName %3 "TestResources" |
| 417 |
OpName %13 "type.2d.image" |
| 418 |
OpName %4 "OutBuffer" |
| 419 |
OpName %2 "in.var.SV_INSTANCEID" |
| 420 |
OpName %1 "TestFragment" |
| 421 |
OpDecorate %2 Flat |
| 422 |
OpDecorate %2 Location 0 |
| 423 |
OpDecorate %3 DescriptorSet 0 |
| 424 |
OpDecorate %3 Binding 0 |
| 425 |
OpDecorate %4 DescriptorSet 0 |
| 426 |
OpDecorate %4 Binding 1 |
| 427 |
OpMemberDecorate %11 0 Offset 0 |
| 428 |
OpMemberDecorate %11 1 Offset 4 |
| 429 |
OpDecorate %11 Block |
| 430 |
%9 = OpTypeInt 32 0 |
| 431 |
%10 = OpConstant %9 2 |
| 432 |
%11 = OpTypeStruct %9 %9 |
| 433 |
%8 = OpTypeArray %11 %10 |
| 434 |
%7 = OpTypePointer Uniform %8 |
| 435 |
%13 = OpTypeImage %9 2D 2 0 0 2 R32ui |
| 436 |
%12 = OpTypePointer UniformConstant %13 |
| 437 |
%14 = OpTypePointer Input %9 |
| 438 |
%15 = OpTypeVoid |
| 439 |
%16 = OpTypeFunction %15 |
| 440 |
%40 = OpTypeVector %9 2 |
| 441 |
%3 = OpVariable %7 Uniform |
| 442 |
%4 = OpVariable %12 UniformConstant |
| 443 |
%2 = OpVariable %14 Input |
| 444 |
%57 = OpTypePointer Uniform %11 |
| 445 |
%61 = OpTypePointer Uniform %9 |
| 446 |
%62 = OpConstant %9 0 |
| 447 |
%1 = OpFunction %15 None %16 |
| 448 |
%17 = OpLabel |
| 449 |
%20 = OpLoad %9 %2 |
| 450 |
%47 = OpAccessChain %57 %3 %20 |
| 451 |
%63 = OpAccessChain %61 %47 %62 |
| 452 |
%64 = OpLoad %9 %63 |
| 453 |
|
| 454 |
; CHECK: [[null_value:%\w+]] = OpConstantNull %uint |
| 455 |
|
| 456 |
; CHECK: [[var_index:%\w+]] = OpLoad %uint %in_var_SV_INSTANCEID |
| 457 |
; CHECK: OpSelectionMerge [[merge:%\w+]] None |
| 458 |
; CHECK: OpSwitch [[var_index]] [[default:%\w+]] 0 [[case0:%\w+]] 1 [[case1:%\w+]] |
| 459 |
; CHECK: [[case0]] = OpLabel |
| 460 |
; CHECK: OpAccessChain |
| 461 |
; CHECK: OpAccessChain |
| 462 |
; CHECK: [[result0:%\w+]] = OpLoad |
| 463 |
; CHECK: OpBranch [[merge]] |
| 464 |
; CHECK: [[case1]] = OpLabel |
| 465 |
; CHECK: OpAccessChain |
| 466 |
; CHECK: OpAccessChain |
| 467 |
; CHECK: [[result1:%\w+]] = OpLoad |
| 468 |
; CHECK: OpBranch [[merge]] |
| 469 |
; CHECK: [[default]] = OpLabel |
| 470 |
; CHECK: OpBranch [[merge]] |
| 471 |
; CHECK: [[merge]] = OpLabel |
| 472 |
; CHECK: OpPhi %uint [[result0]] [[case0]] [[result1]] [[case1]] [[null_value]] [[default]] |
| 473 |
|
| 474 |
%55 = OpCompositeConstruct %40 %20 %20 |
| 475 |
%56 = OpLoad %13 %4 |
| 476 |
OpImageWrite %56 %55 %64 None |
| 477 |
OpReturn |
| 478 |
OpFunctionEnd |
| 479 |
)"; |
| 480 |
|
| 481 |
SinglePassRunAndMatch<ReplaceDescArrayAccessUsingVarIndex>(text, true); |
| 482 |
} |
| 483 |
|
| 484 |
TEST_F(ReplaceDescArrayAccessUsingVarIndexTest, |
| 485 |
ReplaceAccessChainToTextureArrayWithNonUniformIndex) { |
| 486 |
const std::string text = R"( |
| 487 |
OpCapability Shader |
| 488 |
OpCapability ShaderNonUniform |
| 489 |
OpCapability SampledImageArrayNonUniformIndexing |
| 490 |
OpExtension "SPV_EXT_descriptor_indexing" |
| 491 |
OpMemoryModel Logical GLSL450 |
| 492 |
OpEntryPoint Fragment %PSMain "PSMain" %in_var_TEXCOORD0 %in_var_MATERIAL_ID %out_var_SV_TARGET |
| 493 |
OpExecutionMode %PSMain OriginUpperLeft |
| 494 |
OpSource HLSL 610 |
| 495 |
OpName %type_sampler "type.sampler" |
| 496 |
OpName %sampler_ "sampler_" |
| 497 |
OpName %type_2d_image "type.2d.image" |
| 498 |
OpName %texture_2d "texture_2d" |
| 499 |
OpName %in_var_TEXCOORD0 "in.var.TEXCOORD0" |
| 500 |
OpName %in_var_MATERIAL_ID "in.var.MATERIAL_ID" |
| 501 |
OpName %out_var_SV_TARGET "out.var.SV_TARGET" |
| 502 |
OpName %PSMain "PSMain" |
| 503 |
OpName %type_sampled_image "type.sampled.image" |
| 504 |
OpDecorate %in_var_MATERIAL_ID Flat |
| 505 |
OpDecorate %in_var_TEXCOORD0 Location 0 |
| 506 |
OpDecorate %in_var_MATERIAL_ID Location 1 |
| 507 |
OpDecorate %out_var_SV_TARGET Location 0 |
| 508 |
OpDecorate %sampler_ DescriptorSet 1 |
| 509 |
OpDecorate %sampler_ Binding 1 |
| 510 |
OpDecorate %texture_2d DescriptorSet 0 |
| 511 |
OpDecorate %texture_2d Binding 0 |
| 512 |
|
| 513 |
; CHECK: OpDecorate [[v0:%\w+]] NonUniform |
| 514 |
; CHECK: OpDecorate [[v1:%\w+]] NonUniform |
| 515 |
; CHECK: OpDecorate [[v2:%\w+]] NonUniform |
| 516 |
; CHECK: OpDecorate [[v3:%\w+]] NonUniform |
| 517 |
|
| 518 |
OpDecorate %10 NonUniform |
| 519 |
OpDecorate %11 NonUniform |
| 520 |
OpDecorate %12 NonUniform |
| 521 |
OpDecorate %13 NonUniform |
| 522 |
%type_sampler = OpTypeSampler |
| 523 |
%_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler |
| 524 |
%uint = OpTypeInt 32 0 |
| 525 |
%uint_4 = OpConstant %uint 4 |
| 526 |
%float = OpTypeFloat 32 |
| 527 |
%type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown |
| 528 |
%_arr_type_2d_image_uint_4 = OpTypeArray %type_2d_image %uint_4 |
| 529 |
%_ptr_UniformConstant__arr_type_2d_image_uint_4 = OpTypePointer UniformConstant %_arr_type_2d_image_uint_4 |
| 530 |
%v2float = OpTypeVector %float 2 |
| 531 |
%_ptr_Input_v2float = OpTypePointer Input %v2float |
| 532 |
%_ptr_Input_uint = OpTypePointer Input %uint |
| 533 |
%v4float = OpTypeVector %float 4 |
| 534 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 535 |
%void = OpTypeVoid |
| 536 |
%26 = OpTypeFunction %void |
| 537 |
%_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image |
| 538 |
%type_sampled_image = OpTypeSampledImage %type_2d_image |
| 539 |
%sampler_ = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant |
| 540 |
%texture_2d = OpVariable %_ptr_UniformConstant__arr_type_2d_image_uint_4 UniformConstant |
| 541 |
%in_var_TEXCOORD0 = OpVariable %_ptr_Input_v2float Input |
| 542 |
%in_var_MATERIAL_ID = OpVariable %_ptr_Input_uint Input |
| 543 |
%out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output |
| 544 |
|
| 545 |
; CHECK: %uint_0 = OpConstant %uint 0 |
| 546 |
; CHECK: %uint_1 = OpConstant %uint 1 |
| 547 |
; CHECK: %uint_2 = OpConstant %uint 2 |
| 548 |
; CHECK: %uint_3 = OpConstant %uint 3 |
| 549 |
|
| 550 |
%PSMain = OpFunction %void None %26 |
| 551 |
%28 = OpLabel |
| 552 |
%29 = OpLoad %v2float %in_var_TEXCOORD0 |
| 553 |
%30 = OpLoad %uint %in_var_MATERIAL_ID |
| 554 |
; CHECK: [[v0]] = OpCopyObject %uint {{%\w+}} |
| 555 |
%10 = OpCopyObject %uint %30 |
| 556 |
; CHECK: [[v1]] = OpAccessChain %_ptr_UniformConstant_type_2d_image %texture_2d [[v0]] |
| 557 |
%11 = OpAccessChain %_ptr_UniformConstant_type_2d_image %texture_2d %10 |
| 558 |
; CHECK: [[v2]] = OpLoad %type_2d_image [[v1]] |
| 559 |
%12 = OpLoad %type_2d_image %11 |
| 560 |
%31 = OpLoad %type_sampler %sampler_ |
| 561 |
; CHECK: [[v3]] = OpSampledImage %type_sampled_image [[v2]] {{%\w+}} |
| 562 |
%13 = OpSampledImage %type_sampled_image %12 %31 |
| 563 |
%32 = OpImageSampleImplicitLod %v4float %13 %29 None |
| 564 |
OpStore %out_var_SV_TARGET %32 |
| 565 |
OpReturn |
| 566 |
OpFunctionEnd |
| 567 |
)"; |
| 568 |
|
| 569 |
SinglePassRunAndMatch<ReplaceDescArrayAccessUsingVarIndex>(text, true); |
| 570 |
} |
| 571 |
|
| 572 |
} // namespace |
| 573 |
} // namespace opt |
| 574 |
} // namespace spvtools |