blob: 8ddd7cb1d6e8cb69ca3ad3206dbfbc8b6faa8447 [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001// Copyright 2020 The Dawn & Tint Authors
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002//
Austin Engcc2516a2023-10-17 20:57:54 +00003// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
Ryan Harrisondbc13af2022-02-21 15:19:07 +00005//
Austin Engcc2516a2023-10-17 20:57:54 +00006// 1. Redistributions of source code must retain the above copyright notice, this
7// list of conditions and the following disclaimer.
Ryan Harrisondbc13af2022-02-21 15:19:07 +00008//
Austin Engcc2516a2023-10-17 20:57:54 +00009// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12//
13// 3. Neither the name of the copyright holder nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Ryan Harrisondbc13af2022-02-21 15:19:07 +000027
dan sinclair99181d82023-07-20 01:14:15 +000028#ifndef SRC_TINT_LANG_WGSL_AST_BUILTIN_TEXTURE_HELPER_TEST_H_
29#define SRC_TINT_LANG_WGSL_AST_BUILTIN_TEXTURE_HELPER_TEST_H_
Ryan Harrisondbc13af2022-02-21 15:19:07 +000030
31#include <vector>
32
Ben Claytoncd52f382023-08-07 13:11:08 +000033#include "src/tint/lang/core/access.h"
34#include "src/tint/lang/core/texel_format.h"
dan sinclair352f8c82023-07-21 00:40:07 +000035#include "src/tint/lang/core/type/storage_texture.h"
36#include "src/tint/lang/core/type/texture_dimension.h"
Ben Claytonae18c412023-07-29 13:00:40 +000037#include "src/tint/lang/wgsl/ast/builder.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000038
James Pricef7a4d9f2023-05-11 13:03:38 +000039namespace tint::ast::test {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000040
Ben Claytoned3389f2023-02-09 23:56:42 +000041/// The name of the texture global variable used by the tests.
42static constexpr const char* kTextureName = "Texture";
43
44/// The name of the sampler global variable used by the tests.
45static constexpr const char* kSamplerName = "Sampler";
46
dan sinclair41e4d9a2022-05-01 14:40:55 +000047enum class TextureKind { kRegular, kDepth, kDepthMultisampled, kMultisampled, kStorage };
Ryan Harrisondbc13af2022-02-21 15:19:07 +000048enum class TextureDataType { kF32, kU32, kI32 };
49
50std::ostream& operator<<(std::ostream& out, const TextureKind& kind);
51std::ostream& operator<<(std::ostream& out, const TextureDataType& ty);
52
53/// Non-exhaustive list of valid texture overloads
54enum class ValidTextureOverload {
dan sinclair41e4d9a2022-05-01 14:40:55 +000055 kDimensions1d,
56 kDimensions2d,
57 kDimensions2dLevel,
58 kDimensions2dArray,
59 kDimensions2dArrayLevel,
60 kDimensions3d,
61 kDimensions3dLevel,
62 kDimensionsCube,
63 kDimensionsCubeLevel,
64 kDimensionsCubeArray,
65 kDimensionsCubeArrayLevel,
66 kDimensionsMultisampled2d,
67 kDimensionsDepth2d,
68 kDimensionsDepth2dLevel,
69 kDimensionsDepth2dArray,
70 kDimensionsDepth2dArrayLevel,
71 kDimensionsDepthCube,
72 kDimensionsDepthCubeLevel,
73 kDimensionsDepthCubeArray,
74 kDimensionsDepthCubeArrayLevel,
75 kDimensionsDepthMultisampled2d,
76 kDimensionsStorageWO1d,
77 kDimensionsStorageWO2d,
78 kDimensionsStorageWO2dArray,
79 kDimensionsStorageWO3d,
80 kGather2dF32,
81 kGather2dOffsetF32,
82 kGather2dArrayF32,
83 kGather2dArrayOffsetF32,
84 kGatherCubeF32,
85 kGatherCubeArrayF32,
86 kGatherDepth2dF32,
87 kGatherDepth2dOffsetF32,
88 kGatherDepth2dArrayF32,
89 kGatherDepth2dArrayOffsetF32,
90 kGatherDepthCubeF32,
91 kGatherDepthCubeArrayF32,
92 kGatherCompareDepth2dF32,
93 kGatherCompareDepth2dOffsetF32,
94 kGatherCompareDepth2dArrayF32,
95 kGatherCompareDepth2dArrayOffsetF32,
96 kGatherCompareDepthCubeF32,
97 kGatherCompareDepthCubeArrayF32,
98 kNumLayers2dArray,
Ben Clayton068eb3e2023-03-08 02:48:34 +000099 kNumLayersCubeArray,
dan sinclair41e4d9a2022-05-01 14:40:55 +0000100 kNumLayersDepth2dArray,
Ben Clayton068eb3e2023-03-08 02:48:34 +0000101 kNumLayersDepthCubeArray,
dan sinclair41e4d9a2022-05-01 14:40:55 +0000102 kNumLayersStorageWO2dArray,
103 kNumLevels2d,
104 kNumLevels2dArray,
105 kNumLevels3d,
106 kNumLevelsCube,
107 kNumLevelsCubeArray,
108 kNumLevelsDepth2d,
109 kNumLevelsDepth2dArray,
110 kNumLevelsDepthCube,
111 kNumLevelsDepthCubeArray,
112 kNumSamplesMultisampled2d,
113 kNumSamplesDepthMultisampled2d,
114 kSample1dF32,
115 kSample2dF32,
116 kSample2dOffsetF32,
117 kSample2dArrayF32,
118 kSample2dArrayOffsetF32,
119 kSample3dF32,
120 kSample3dOffsetF32,
121 kSampleCubeF32,
122 kSampleCubeArrayF32,
123 kSampleDepth2dF32,
124 kSampleDepth2dOffsetF32,
125 kSampleDepth2dArrayF32,
126 kSampleDepth2dArrayOffsetF32,
127 kSampleDepthCubeF32,
128 kSampleDepthCubeArrayF32,
129 kSampleBias2dF32,
130 kSampleBias2dOffsetF32,
131 kSampleBias2dArrayF32,
132 kSampleBias2dArrayOffsetF32,
133 kSampleBias3dF32,
134 kSampleBias3dOffsetF32,
135 kSampleBiasCubeF32,
136 kSampleBiasCubeArrayF32,
137 kSampleLevel2dF32,
138 kSampleLevel2dOffsetF32,
139 kSampleLevel2dArrayF32,
140 kSampleLevel2dArrayOffsetF32,
141 kSampleLevel3dF32,
142 kSampleLevel3dOffsetF32,
143 kSampleLevelCubeF32,
144 kSampleLevelCubeArrayF32,
145 kSampleLevelDepth2dF32,
146 kSampleLevelDepth2dOffsetF32,
147 kSampleLevelDepth2dArrayF32,
148 kSampleLevelDepth2dArrayOffsetF32,
149 kSampleLevelDepthCubeF32,
150 kSampleLevelDepthCubeArrayF32,
151 kSampleGrad2dF32,
152 kSampleGrad2dOffsetF32,
153 kSampleGrad2dArrayF32,
154 kSampleGrad2dArrayOffsetF32,
155 kSampleGrad3dF32,
156 kSampleGrad3dOffsetF32,
157 kSampleGradCubeF32,
158 kSampleGradCubeArrayF32,
159 kSampleCompareDepth2dF32,
160 kSampleCompareDepth2dOffsetF32,
161 kSampleCompareDepth2dArrayF32,
162 kSampleCompareDepth2dArrayOffsetF32,
163 kSampleCompareDepthCubeF32,
164 kSampleCompareDepthCubeArrayF32,
165 kSampleCompareLevelDepth2dF32,
166 kSampleCompareLevelDepth2dOffsetF32,
167 kSampleCompareLevelDepth2dArrayF32,
168 kSampleCompareLevelDepth2dArrayOffsetF32,
169 kSampleCompareLevelDepthCubeF32,
170 kSampleCompareLevelDepthCubeArrayF32,
171 kLoad1dLevelF32,
172 kLoad1dLevelU32,
173 kLoad1dLevelI32,
174 kLoad2dLevelF32,
175 kLoad2dLevelU32,
176 kLoad2dLevelI32,
177 kLoad2dArrayLevelF32,
178 kLoad2dArrayLevelU32,
179 kLoad2dArrayLevelI32,
180 kLoad3dLevelF32,
181 kLoad3dLevelU32,
182 kLoad3dLevelI32,
183 kLoadMultisampled2dF32,
184 kLoadMultisampled2dU32,
185 kLoadMultisampled2dI32,
186 kLoadDepth2dLevelF32,
187 kLoadDepth2dArrayLevelF32,
188 kLoadDepthMultisampled2dF32,
189 kStoreWO1dRgba32float, // Not permutated for all texel formats
190 kStoreWO2dRgba32float, // Not permutated for all texel formats
191 kStoreWO2dArrayRgba32float, // Not permutated for all texel formats
192 kStoreWO3dRgba32float, // Not permutated for all texel formats
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000193};
194
195/// @param texture_overload the ValidTextureOverload
196/// @returns true if the ValidTextureOverload builtin returns no value.
197bool ReturnsVoid(ValidTextureOverload texture_overload);
198
199/// Describes a texture builtin overload
200struct TextureOverloadCase {
Ben Clayton5662f792023-02-22 00:08:55 +0000201 /// Args is a list of Expression used as arguments to the texture overload case.
dan sinclairbae54e72023-07-28 15:01:54 +0000202 using Args = tint::Vector<const Expression*, 8>;
Ben Clayton783b1692022-08-02 17:03:35 +0000203
dan sinclair41e4d9a2022-05-01 14:40:55 +0000204 /// Constructor for textureSample...() functions
205 TextureOverloadCase(ValidTextureOverload,
206 const char*,
207 TextureKind,
dan sinclaircedcdf32023-08-10 02:39:48 +0000208 core::type::SamplerKind,
209 core::type::TextureDimension,
dan sinclair41e4d9a2022-05-01 14:40:55 +0000210 TextureDataType,
211 const char*,
Ben Claytonb549b302023-02-22 16:18:22 +0000212 std::function<Args(ProgramBuilder*)>,
213 bool /* returns_value */);
dan sinclair41e4d9a2022-05-01 14:40:55 +0000214 /// Constructor for textureLoad() functions with non-storage textures
215 TextureOverloadCase(ValidTextureOverload,
216 const char*,
217 TextureKind,
dan sinclaircedcdf32023-08-10 02:39:48 +0000218 core::type::TextureDimension,
dan sinclair41e4d9a2022-05-01 14:40:55 +0000219 TextureDataType,
220 const char*,
Ben Claytonb549b302023-02-22 16:18:22 +0000221 std::function<Args(ProgramBuilder*)>,
222 bool /* returns_value */);
dan sinclair41e4d9a2022-05-01 14:40:55 +0000223 /// Constructor for textureLoad() with storage textures
224 TextureOverloadCase(ValidTextureOverload,
225 const char*,
Ben Claytoncd52f382023-08-07 13:11:08 +0000226 tint::core::Access,
227 tint::core::TexelFormat,
dan sinclaircedcdf32023-08-10 02:39:48 +0000228 core::type::TextureDimension,
dan sinclair41e4d9a2022-05-01 14:40:55 +0000229 TextureDataType,
230 const char*,
Ben Claytonb549b302023-02-22 16:18:22 +0000231 std::function<Args(ProgramBuilder*)>,
232 bool /* returns_value */);
dan sinclair41e4d9a2022-05-01 14:40:55 +0000233 /// Copy constructor
234 TextureOverloadCase(const TextureOverloadCase&);
Ben Clayton9a56b252023-03-15 14:09:40 +0000235
dan sinclair41e4d9a2022-05-01 14:40:55 +0000236 /// Destructor
237 ~TextureOverloadCase();
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000238
dan sinclair41e4d9a2022-05-01 14:40:55 +0000239 /// @return a vector containing a large number (non-exhaustive) of valid
240 /// texture overloads.
241 static std::vector<TextureOverloadCase> ValidCases();
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000242
dan sinclair41e4d9a2022-05-01 14:40:55 +0000243 /// @param builder the AST builder used for the test
244 /// @returns the vector component type of the texture function return value
Ben Clayton5662f792023-02-22 00:08:55 +0000245 Type BuildResultVectorComponentType(ProgramBuilder* builder) const;
dan sinclair41e4d9a2022-05-01 14:40:55 +0000246 /// @param builder the AST builder used for the test
247 /// @returns a variable holding the test texture, automatically registered as
248 /// a global variable.
Ben Clayton5662f792023-02-22 00:08:55 +0000249 const Variable* BuildTextureVariable(ProgramBuilder* builder) const;
dan sinclair41e4d9a2022-05-01 14:40:55 +0000250 /// @param builder the AST builder used for the test
251 /// @returns a Variable holding the test sampler, automatically registered as
252 /// a global variable.
Ben Clayton5662f792023-02-22 00:08:55 +0000253 const Variable* BuildSamplerVariable(ProgramBuilder* builder) const;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000254
dan sinclair41e4d9a2022-05-01 14:40:55 +0000255 /// The enumerator for this overload
256 const ValidTextureOverload overload;
257 /// A human readable description of the overload
258 const char* const description;
259 /// The texture kind for the texture parameter
260 const TextureKind texture_kind;
261 /// The sampler kind for the sampler parameter
262 /// Used only when texture_kind is not kStorage
dan sinclaircedcdf32023-08-10 02:39:48 +0000263 core::type::SamplerKind const sampler_kind = core::type::SamplerKind::kSampler;
dan sinclair41e4d9a2022-05-01 14:40:55 +0000264 /// The access control for the storage texture
265 /// Used only when texture_kind is kStorage
Ben Claytoncd52f382023-08-07 13:11:08 +0000266 tint::core::Access const access = tint::core::Access::kReadWrite;
dan sinclair41e4d9a2022-05-01 14:40:55 +0000267 /// The image format for the storage texture
268 /// Used only when texture_kind is kStorage
Ben Claytoncd52f382023-08-07 13:11:08 +0000269 tint::core::TexelFormat const texel_format = tint::core::TexelFormat::kUndefined;
dan sinclair41e4d9a2022-05-01 14:40:55 +0000270 /// The dimensions of the texture parameter
dan sinclaircedcdf32023-08-10 02:39:48 +0000271 core::type::TextureDimension const texture_dimension;
dan sinclair41e4d9a2022-05-01 14:40:55 +0000272 /// The data type of the texture parameter
273 const TextureDataType texture_data_type;
274 /// Name of the function. e.g. `textureSample`, `textureSampleGrad`, etc
275 const char* const function;
276 /// A function that builds the AST arguments for the overload
Ben Clayton783b1692022-08-02 17:03:35 +0000277 std::function<Args(ProgramBuilder*)> const args;
Ben Claytonb549b302023-02-22 16:18:22 +0000278 /// True if the function returns a value
279 const bool returns_value;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000280};
281
282std::ostream& operator<<(std::ostream& out, const TextureOverloadCase& data);
283
James Pricef7a4d9f2023-05-11 13:03:38 +0000284} // namespace tint::ast::test
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000285
dan sinclair99181d82023-07-20 01:14:15 +0000286#endif // SRC_TINT_LANG_WGSL_AST_BUILTIN_TEXTURE_HELPER_TEST_H_