1 // Copyright 2021 The Tint Authors.
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 "src/transform/external_texture_transform.h"
16
17 #include "src/transform/test_helper.h"
18
19 namespace tint {
20 namespace transform {
21 namespace {
22
23 using ExternalTextureTransformTest = TransformTest;
24
TEST_F(ExternalTextureTransformTest,SampleLevelSinglePlane)25 TEST_F(ExternalTextureTransformTest, SampleLevelSinglePlane) {
26 auto* src = R"(
27 [[group(0), binding(0)]] var s : sampler;
28
29 [[group(0), binding(1)]] var t : texture_external;
30
31 [[stage(fragment)]]
32 fn main([[builtin(position)]] coord : vec4<f32>) -> [[location(0)]] vec4<f32> {
33 return textureSampleLevel(t, s, (coord.xy / vec2<f32>(4.0, 4.0)));
34 }
35 )";
36
37 auto* expect = R"(
38 [[group(0), binding(0)]] var s : sampler;
39
40 [[group(0), binding(1)]] var t : texture_2d<f32>;
41
42 [[stage(fragment)]]
43 fn main([[builtin(position)]] coord : vec4<f32>) -> [[location(0)]] vec4<f32> {
44 return textureSampleLevel(t, s, (coord.xy / vec2<f32>(4.0, 4.0)), 0.0);
45 }
46 )";
47
48 auto got = Run<ExternalTextureTransform>(src);
49
50 EXPECT_EQ(expect, str(got));
51 }
52
TEST_F(ExternalTextureTransformTest,LoadSinglePlane)53 TEST_F(ExternalTextureTransformTest, LoadSinglePlane) {
54 auto* src = R"(
55 [[group(0), binding(0)]] var t : texture_external;
56
57 [[stage(fragment)]]
58 fn main([[builtin(position)]] coord : vec4<f32>) -> [[location(0)]] vec4<f32> {
59 return textureLoad(t, vec2<i32>(1, 1));
60 }
61 )";
62
63 auto* expect = R"(
64 [[group(0), binding(0)]] var t : texture_2d<f32>;
65
66 [[stage(fragment)]]
67 fn main([[builtin(position)]] coord : vec4<f32>) -> [[location(0)]] vec4<f32> {
68 return textureLoad(t, vec2<i32>(1, 1), 0);
69 }
70 )";
71
72 auto got = Run<ExternalTextureTransform>(src);
73
74 EXPECT_EQ(expect, str(got));
75 }
76
TEST_F(ExternalTextureTransformTest,DimensionsSinglePlane)77 TEST_F(ExternalTextureTransformTest, DimensionsSinglePlane) {
78 auto* src = R"(
79 [[group(0), binding(0)]] var t : texture_external;
80
81 [[stage(fragment)]]
82 fn main([[builtin(position)]] coord : vec4<f32>) -> [[location(0)]] vec4<f32> {
83 var dim : vec2<i32>;
84 dim = textureDimensions(t);
85 return vec4<f32>(0.0, 0.0, 0.0, 0.0);
86 }
87 )";
88
89 auto* expect = R"(
90 [[group(0), binding(0)]] var t : texture_2d<f32>;
91
92 [[stage(fragment)]]
93 fn main([[builtin(position)]] coord : vec4<f32>) -> [[location(0)]] vec4<f32> {
94 var dim : vec2<i32>;
95 dim = textureDimensions(t);
96 return vec4<f32>(0.0, 0.0, 0.0, 0.0);
97 }
98 )";
99
100 auto got = Run<ExternalTextureTransform>(src);
101
102 EXPECT_EQ(expect, str(got));
103 }
104
105 } // namespace
106 } // namespace transform
107 } // namespace tint
108