| 1 |
// Copyright (c) 2017 Valve Corporation |
| 2 |
// Copyright (c) 2017 LunarG Inc. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
|
| 16 |
#include <string> |
| 17 |
|
| 18 |
#include "test/opt/pass_fixture.h" |
| 19 |
#include "test/opt/pass_utils.h" |
| 20 |
|
| 21 |
namespace spvtools { |
| 22 |
namespace opt { |
| 23 |
namespace { |
| 24 |
|
| 25 |
using InlineOpaqueTest = PassTest<::testing::Test>; |
| 26 |
|
| 27 |
TEST_F(InlineOpaqueTest, InlineCallWithStructArgContainingSampledImage) { |
| 28 |
// Function with opaque argument is inlined. |
| 29 |
// TODO(greg-lunarg): Add HLSL code |
| 30 |
|
| 31 |
const std::string predefs = |
| 32 |
R"(OpCapability Shader |
| 33 |
%1 = OpExtInstImport "GLSL.std.450" |
| 34 |
OpMemoryModel Logical GLSL450 |
| 35 |
OpEntryPoint Fragment %main "main" %outColor %texCoords |
| 36 |
OpExecutionMode %main OriginUpperLeft |
| 37 |
OpSource GLSL 140 |
| 38 |
OpName %main "main" |
| 39 |
OpName %S_t "S_t" |
| 40 |
OpMemberName %S_t 0 "v0" |
| 41 |
OpMemberName %S_t 1 "v1" |
| 42 |
OpMemberName %S_t 2 "smp" |
| 43 |
OpName %foo_struct_S_t_vf2_vf21_ "foo(struct-S_t-vf2-vf21;" |
| 44 |
OpName %s "s" |
| 45 |
OpName %outColor "outColor" |
| 46 |
OpName %sampler15 "sampler15" |
| 47 |
OpName %s0 "s0" |
| 48 |
OpName %texCoords "texCoords" |
| 49 |
OpName %param "param" |
| 50 |
OpDecorate %sampler15 DescriptorSet 0 |
| 51 |
%void = OpTypeVoid |
| 52 |
%12 = OpTypeFunction %void |
| 53 |
%float = OpTypeFloat 32 |
| 54 |
%v2float = OpTypeVector %float 2 |
| 55 |
%v4float = OpTypeVector %float 4 |
| 56 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 57 |
%outColor = OpVariable %_ptr_Output_v4float Output |
| 58 |
%17 = OpTypeImage %float 2D 0 0 0 1 Unknown |
| 59 |
%18 = OpTypeSampledImage %17 |
| 60 |
%S_t = OpTypeStruct %v2float %v2float %18 |
| 61 |
%_ptr_Function_S_t = OpTypePointer Function %S_t |
| 62 |
%20 = OpTypeFunction %void %_ptr_Function_S_t |
| 63 |
%_ptr_UniformConstant_18 = OpTypePointer UniformConstant %18 |
| 64 |
%_ptr_Function_18 = OpTypePointer Function %18 |
| 65 |
%sampler15 = OpVariable %_ptr_UniformConstant_18 UniformConstant |
| 66 |
%int = OpTypeInt 32 1 |
| 67 |
%int_0 = OpConstant %int 0 |
| 68 |
%int_2 = OpConstant %int 2 |
| 69 |
%_ptr_Function_v2float = OpTypePointer Function %v2float |
| 70 |
%_ptr_Input_v2float = OpTypePointer Input %v2float |
| 71 |
%texCoords = OpVariable %_ptr_Input_v2float Input |
| 72 |
)"; |
| 73 |
|
| 74 |
const std::string before = |
| 75 |
R"(%main = OpFunction %void None %12 |
| 76 |
%28 = OpLabel |
| 77 |
%s0 = OpVariable %_ptr_Function_S_t Function |
| 78 |
%param = OpVariable %_ptr_Function_S_t Function |
| 79 |
%29 = OpLoad %v2float %texCoords |
| 80 |
%30 = OpAccessChain %_ptr_Function_v2float %s0 %int_0 |
| 81 |
OpStore %30 %29 |
| 82 |
%31 = OpLoad %18 %sampler15 |
| 83 |
%32 = OpAccessChain %_ptr_Function_18 %s0 %int_2 |
| 84 |
OpStore %32 %31 |
| 85 |
%33 = OpLoad %S_t %s0 |
| 86 |
OpStore %param %33 |
| 87 |
%34 = OpFunctionCall %void %foo_struct_S_t_vf2_vf21_ %param |
| 88 |
OpReturn |
| 89 |
OpFunctionEnd |
| 90 |
)"; |
| 91 |
|
| 92 |
const std::string after = |
| 93 |
R"(%34 = OpUndef %void |
| 94 |
%main = OpFunction %void None %12 |
| 95 |
%28 = OpLabel |
| 96 |
%s0 = OpVariable %_ptr_Function_S_t Function |
| 97 |
%param = OpVariable %_ptr_Function_S_t Function |
| 98 |
%29 = OpLoad %v2float %texCoords |
| 99 |
%30 = OpAccessChain %_ptr_Function_v2float %s0 %int_0 |
| 100 |
OpStore %30 %29 |
| 101 |
%31 = OpLoad %18 %sampler15 |
| 102 |
%32 = OpAccessChain %_ptr_Function_18 %s0 %int_2 |
| 103 |
OpStore %32 %31 |
| 104 |
%33 = OpLoad %S_t %s0 |
| 105 |
OpStore %param %33 |
| 106 |
%42 = OpAccessChain %_ptr_Function_18 %param %int_2 |
| 107 |
%43 = OpLoad %18 %42 |
| 108 |
%44 = OpAccessChain %_ptr_Function_v2float %param %int_0 |
| 109 |
%45 = OpLoad %v2float %44 |
| 110 |
%46 = OpImageSampleImplicitLod %v4float %43 %45 |
| 111 |
OpStore %outColor %46 |
| 112 |
OpReturn |
| 113 |
OpFunctionEnd |
| 114 |
)"; |
| 115 |
|
| 116 |
const std::string post_defs = |
| 117 |
R"(%foo_struct_S_t_vf2_vf21_ = OpFunction %void None %20 |
| 118 |
%s = OpFunctionParameter %_ptr_Function_S_t |
| 119 |
%35 = OpLabel |
| 120 |
%36 = OpAccessChain %_ptr_Function_18 %s %int_2 |
| 121 |
%37 = OpLoad %18 %36 |
| 122 |
%38 = OpAccessChain %_ptr_Function_v2float %s %int_0 |
| 123 |
%39 = OpLoad %v2float %38 |
| 124 |
%40 = OpImageSampleImplicitLod %v4float %37 %39 |
| 125 |
OpStore %outColor %40 |
| 126 |
OpReturn |
| 127 |
OpFunctionEnd |
| 128 |
)"; |
| 129 |
|
| 130 |
SinglePassRunAndCheck<InlineOpaquePass>( |
| 131 |
predefs + before + post_defs, predefs + after + post_defs, true, true); |
| 132 |
} |
| 133 |
|
| 134 |
TEST_F(InlineOpaqueTest, InlineOpaqueReturn) { |
| 135 |
// Function with opaque return value is inlined. |
| 136 |
// TODO(greg-lunarg): Add HLSL code |
| 137 |
|
| 138 |
const std::string predefs = |
| 139 |
R"(OpCapability Shader |
| 140 |
%1 = OpExtInstImport "GLSL.std.450" |
| 141 |
OpMemoryModel Logical GLSL450 |
| 142 |
OpEntryPoint Fragment %main "main" %texCoords %outColor |
| 143 |
OpExecutionMode %main OriginUpperLeft |
| 144 |
OpSource GLSL 140 |
| 145 |
OpName %main "main" |
| 146 |
OpName %foo_ "foo(" |
| 147 |
OpName %texCoords "texCoords" |
| 148 |
OpName %outColor "outColor" |
| 149 |
OpName %sampler15 "sampler15" |
| 150 |
OpName %sampler16 "sampler16" |
| 151 |
OpDecorate %sampler15 DescriptorSet 0 |
| 152 |
OpDecorate %sampler16 DescriptorSet 0 |
| 153 |
%void = OpTypeVoid |
| 154 |
%9 = OpTypeFunction %void |
| 155 |
%float = OpTypeFloat 32 |
| 156 |
%v2float = OpTypeVector %float 2 |
| 157 |
%bool = OpTypeBool |
| 158 |
%false = OpConstantFalse %bool |
| 159 |
%_ptr_Input_v2float = OpTypePointer Input %v2float |
| 160 |
%texCoords = OpVariable %_ptr_Input_v2float Input |
| 161 |
%float_0 = OpConstant %float 0 |
| 162 |
%16 = OpConstantComposite %v2float %float_0 %float_0 |
| 163 |
%v4float = OpTypeVector %float 4 |
| 164 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 165 |
%outColor = OpVariable %_ptr_Output_v4float Output |
| 166 |
%19 = OpTypeImage %float 2D 0 0 0 1 Unknown |
| 167 |
%20 = OpTypeSampledImage %19 |
| 168 |
%21 = OpTypeFunction %20 |
| 169 |
%_ptr_UniformConstant_20 = OpTypePointer UniformConstant %20 |
| 170 |
%_ptr_Function_20 = OpTypePointer Function %20 |
| 171 |
%sampler15 = OpVariable %_ptr_UniformConstant_20 UniformConstant |
| 172 |
%sampler16 = OpVariable %_ptr_UniformConstant_20 UniformConstant |
| 173 |
)"; |
| 174 |
|
| 175 |
const std::string before = |
| 176 |
R"(%main = OpFunction %void None %9 |
| 177 |
%24 = OpLabel |
| 178 |
%25 = OpVariable %_ptr_Function_20 Function |
| 179 |
%26 = OpFunctionCall %20 %foo_ |
| 180 |
OpStore %25 %26 |
| 181 |
%27 = OpLoad %20 %25 |
| 182 |
%28 = OpLoad %v2float %texCoords |
| 183 |
%29 = OpImageSampleImplicitLod %v4float %27 %28 |
| 184 |
OpStore %outColor %29 |
| 185 |
OpReturn |
| 186 |
OpFunctionEnd |
| 187 |
)"; |
| 188 |
|
| 189 |
const std::string after = |
| 190 |
R"(%main = OpFunction %void None %9 |
| 191 |
%24 = OpLabel |
| 192 |
%34 = OpVariable %_ptr_Function_20 Function |
| 193 |
%35 = OpVariable %_ptr_Function_20 Function |
| 194 |
%25 = OpVariable %_ptr_Function_20 Function |
| 195 |
%37 = OpLoad %20 %sampler16 |
| 196 |
OpStore %34 %37 |
| 197 |
%38 = OpLoad %20 %34 |
| 198 |
OpStore %35 %38 |
| 199 |
%26 = OpLoad %20 %35 |
| 200 |
OpStore %25 %26 |
| 201 |
%27 = OpLoad %20 %25 |
| 202 |
%28 = OpLoad %v2float %texCoords |
| 203 |
%29 = OpImageSampleImplicitLod %v4float %27 %28 |
| 204 |
OpStore %outColor %29 |
| 205 |
OpReturn |
| 206 |
OpFunctionEnd |
| 207 |
)"; |
| 208 |
|
| 209 |
const std::string post_defs = |
| 210 |
R"(%foo_ = OpFunction %20 None %21 |
| 211 |
%30 = OpLabel |
| 212 |
%31 = OpVariable %_ptr_Function_20 Function |
| 213 |
%32 = OpLoad %20 %sampler16 |
| 214 |
OpStore %31 %32 |
| 215 |
%33 = OpLoad %20 %31 |
| 216 |
OpReturnValue %33 |
| 217 |
OpFunctionEnd |
| 218 |
)"; |
| 219 |
|
| 220 |
SinglePassRunAndCheck<InlineOpaquePass>( |
| 221 |
predefs + before + post_defs, predefs + after + post_defs, true, true); |
| 222 |
} |
| 223 |
|
| 224 |
TEST_F(InlineOpaqueTest, InlineInNonEntryPointFunction) { |
| 225 |
// This demonstrates opaque inlining in a function that is not |
| 226 |
// an entry point function (main2) but is in the call tree of an |
| 227 |
// entry point function (main). |
| 228 |
// TODO(greg-lunarg): Add HLSL code |
| 229 |
|
| 230 |
const std::string predefs = |
| 231 |
R"(OpCapability Shader |
| 232 |
%1 = OpExtInstImport "GLSL.std.450" |
| 233 |
OpMemoryModel Logical GLSL450 |
| 234 |
OpEntryPoint Fragment %main "main" %outColor %texCoords |
| 235 |
OpExecutionMode %main OriginUpperLeft |
| 236 |
OpSource GLSL 140 |
| 237 |
OpName %main "main" |
| 238 |
OpName %main2 "main2" |
| 239 |
OpName %S_t "S_t" |
| 240 |
OpMemberName %S_t 0 "v0" |
| 241 |
OpMemberName %S_t 1 "v1" |
| 242 |
OpMemberName %S_t 2 "smp" |
| 243 |
OpName %foo_struct_S_t_vf2_vf21_ "foo(struct-S_t-vf2-vf21;" |
| 244 |
OpName %s "s" |
| 245 |
OpName %outColor "outColor" |
| 246 |
OpName %sampler15 "sampler15" |
| 247 |
OpName %s0 "s0" |
| 248 |
OpName %texCoords "texCoords" |
| 249 |
OpName %param "param" |
| 250 |
OpDecorate %sampler15 DescriptorSet 0 |
| 251 |
%void = OpTypeVoid |
| 252 |
%13 = OpTypeFunction %void |
| 253 |
%float = OpTypeFloat 32 |
| 254 |
%v2float = OpTypeVector %float 2 |
| 255 |
%v4float = OpTypeVector %float 4 |
| 256 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 257 |
%outColor = OpVariable %_ptr_Output_v4float Output |
| 258 |
%18 = OpTypeImage %float 2D 0 0 0 1 Unknown |
| 259 |
%19 = OpTypeSampledImage %18 |
| 260 |
%S_t = OpTypeStruct %v2float %v2float %19 |
| 261 |
%_ptr_Function_S_t = OpTypePointer Function %S_t |
| 262 |
%21 = OpTypeFunction %void %_ptr_Function_S_t |
| 263 |
%_ptr_UniformConstant_19 = OpTypePointer UniformConstant %19 |
| 264 |
%_ptr_Function_19 = OpTypePointer Function %19 |
| 265 |
%sampler15 = OpVariable %_ptr_UniformConstant_19 UniformConstant |
| 266 |
%int = OpTypeInt 32 1 |
| 267 |
%int_0 = OpConstant %int 0 |
| 268 |
%int_2 = OpConstant %int 2 |
| 269 |
%_ptr_Function_v2float = OpTypePointer Function %v2float |
| 270 |
%_ptr_Input_v2float = OpTypePointer Input %v2float |
| 271 |
%texCoords = OpVariable %_ptr_Input_v2float Input |
| 272 |
)"; |
| 273 |
|
| 274 |
const std::string before = |
| 275 |
R"(%main2 = OpFunction %void None %13 |
| 276 |
%29 = OpLabel |
| 277 |
%s0 = OpVariable %_ptr_Function_S_t Function |
| 278 |
%param = OpVariable %_ptr_Function_S_t Function |
| 279 |
%30 = OpLoad %v2float %texCoords |
| 280 |
%31 = OpAccessChain %_ptr_Function_v2float %s0 %int_0 |
| 281 |
OpStore %31 %30 |
| 282 |
%32 = OpLoad %19 %sampler15 |
| 283 |
%33 = OpAccessChain %_ptr_Function_19 %s0 %int_2 |
| 284 |
OpStore %33 %32 |
| 285 |
%34 = OpLoad %S_t %s0 |
| 286 |
OpStore %param %34 |
| 287 |
%35 = OpFunctionCall %void %foo_struct_S_t_vf2_vf21_ %param |
| 288 |
OpReturn |
| 289 |
OpFunctionEnd |
| 290 |
)"; |
| 291 |
|
| 292 |
const std::string after = |
| 293 |
R"(%35 = OpUndef %void |
| 294 |
%main2 = OpFunction %void None %13 |
| 295 |
%29 = OpLabel |
| 296 |
%s0 = OpVariable %_ptr_Function_S_t Function |
| 297 |
%param = OpVariable %_ptr_Function_S_t Function |
| 298 |
%30 = OpLoad %v2float %texCoords |
| 299 |
%31 = OpAccessChain %_ptr_Function_v2float %s0 %int_0 |
| 300 |
OpStore %31 %30 |
| 301 |
%32 = OpLoad %19 %sampler15 |
| 302 |
%33 = OpAccessChain %_ptr_Function_19 %s0 %int_2 |
| 303 |
OpStore %33 %32 |
| 304 |
%34 = OpLoad %S_t %s0 |
| 305 |
OpStore %param %34 |
| 306 |
%45 = OpAccessChain %_ptr_Function_19 %param %int_2 |
| 307 |
%46 = OpLoad %19 %45 |
| 308 |
%47 = OpAccessChain %_ptr_Function_v2float %param %int_0 |
| 309 |
%48 = OpLoad %v2float %47 |
| 310 |
%49 = OpImageSampleImplicitLod %v4float %46 %48 |
| 311 |
OpStore %outColor %49 |
| 312 |
OpReturn |
| 313 |
OpFunctionEnd |
| 314 |
)"; |
| 315 |
|
| 316 |
const std::string post_defs = |
| 317 |
R"(%main = OpFunction %void None %13 |
| 318 |
%36 = OpLabel |
| 319 |
%37 = OpFunctionCall %void %main2 |
| 320 |
OpReturn |
| 321 |
OpFunctionEnd |
| 322 |
%foo_struct_S_t_vf2_vf21_ = OpFunction %void None %21 |
| 323 |
%s = OpFunctionParameter %_ptr_Function_S_t |
| 324 |
%38 = OpLabel |
| 325 |
%39 = OpAccessChain %_ptr_Function_19 %s %int_2 |
| 326 |
%40 = OpLoad %19 %39 |
| 327 |
%41 = OpAccessChain %_ptr_Function_v2float %s %int_0 |
| 328 |
%42 = OpLoad %v2float %41 |
| 329 |
%43 = OpImageSampleImplicitLod %v4float %40 %42 |
| 330 |
OpStore %outColor %43 |
| 331 |
OpReturn |
| 332 |
OpFunctionEnd |
| 333 |
)"; |
| 334 |
|
| 335 |
SinglePassRunAndCheck<InlineOpaquePass>( |
| 336 |
predefs + before + post_defs, predefs + after + post_defs, true, true); |
| 337 |
} |
| 338 |
|
| 339 |
TEST_F(InlineOpaqueTest, NoInlineNoOpaque) { |
| 340 |
// Function without opaque interface is not inlined. |
| 341 |
// #version 140 |
| 342 |
// |
| 343 |
// in vec4 BaseColor; |
| 344 |
// |
| 345 |
// float foo(vec4 bar) |
| 346 |
// { |
| 347 |
// return bar.x + bar.y; |
| 348 |
// } |
| 349 |
// |
| 350 |
// void main() |
| 351 |
// { |
| 352 |
// vec4 color = vec4(foo(BaseColor)); |
| 353 |
// gl_FragColor = color; |
| 354 |
// } |
| 355 |
|
| 356 |
const std::string assembly = |
| 357 |
R"(OpCapability Shader |
| 358 |
%1 = OpExtInstImport "GLSL.std.450" |
| 359 |
OpMemoryModel Logical GLSL450 |
| 360 |
OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor |
| 361 |
OpExecutionMode %main OriginUpperLeft |
| 362 |
OpSource GLSL 140 |
| 363 |
OpName %main "main" |
| 364 |
OpName %foo_vf4_ "foo(vf4;" |
| 365 |
OpName %bar "bar" |
| 366 |
OpName %color "color" |
| 367 |
OpName %BaseColor "BaseColor" |
| 368 |
OpName %param "param" |
| 369 |
OpName %gl_FragColor "gl_FragColor" |
| 370 |
%void = OpTypeVoid |
| 371 |
%10 = OpTypeFunction %void |
| 372 |
%float = OpTypeFloat 32 |
| 373 |
%v4float = OpTypeVector %float 4 |
| 374 |
%_ptr_Function_v4float = OpTypePointer Function %v4float |
| 375 |
%14 = OpTypeFunction %float %_ptr_Function_v4float |
| 376 |
%uint = OpTypeInt 32 0 |
| 377 |
%uint_0 = OpConstant %uint 0 |
| 378 |
%_ptr_Function_float = OpTypePointer Function %float |
| 379 |
%uint_1 = OpConstant %uint 1 |
| 380 |
%_ptr_Input_v4float = OpTypePointer Input %v4float |
| 381 |
%BaseColor = OpVariable %_ptr_Input_v4float Input |
| 382 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 383 |
%gl_FragColor = OpVariable %_ptr_Output_v4float Output |
| 384 |
%main = OpFunction %void None %10 |
| 385 |
%21 = OpLabel |
| 386 |
%color = OpVariable %_ptr_Function_v4float Function |
| 387 |
%param = OpVariable %_ptr_Function_v4float Function |
| 388 |
%22 = OpLoad %v4float %BaseColor |
| 389 |
OpStore %param %22 |
| 390 |
%23 = OpFunctionCall %float %foo_vf4_ %param |
| 391 |
%24 = OpCompositeConstruct %v4float %23 %23 %23 %23 |
| 392 |
OpStore %color %24 |
| 393 |
%25 = OpLoad %v4float %color |
| 394 |
OpStore %gl_FragColor %25 |
| 395 |
OpReturn |
| 396 |
OpFunctionEnd |
| 397 |
%foo_vf4_ = OpFunction %float None %14 |
| 398 |
%bar = OpFunctionParameter %_ptr_Function_v4float |
| 399 |
%26 = OpLabel |
| 400 |
%27 = OpAccessChain %_ptr_Function_float %bar %uint_0 |
| 401 |
%28 = OpLoad %float %27 |
| 402 |
%29 = OpAccessChain %_ptr_Function_float %bar %uint_1 |
| 403 |
%30 = OpLoad %float %29 |
| 404 |
%31 = OpFAdd %float %28 %30 |
| 405 |
OpReturnValue %31 |
| 406 |
OpFunctionEnd |
| 407 |
)"; |
| 408 |
|
| 409 |
SinglePassRunAndCheck<InlineOpaquePass>(assembly, assembly, true, true); |
| 410 |
} |
| 411 |
|
| 412 |
} // namespace |
| 413 |
} // namespace opt |
| 414 |
} // namespace spvtools |