[glsl][ir] Support sampled texture types
This CL adds support for the `SampledTexture` type.
Bug: 42251044
Change-Id: Ia5d380720dc0dcbe6b855befe503c5411977bebe
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/204515
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
diff --git a/src/tint/lang/glsl/writer/printer/printer.cc b/src/tint/lang/glsl/writer/printer/printer.cc
index 84fe10a..4db5cd0 100644
--- a/src/tint/lang/glsl/writer/printer/printer.cc
+++ b/src/tint/lang/glsl/writer/printer/printer.cc
@@ -59,6 +59,7 @@
#include "src/tint/lang/core/type/i32.h"
#include "src/tint/lang/core/type/matrix.h"
#include "src/tint/lang/core/type/pointer.h"
+#include "src/tint/lang/core/type/sampled_texture.h"
#include "src/tint/lang/core/type/storage_texture.h"
#include "src/tint/lang/core/type/u32.h"
#include "src/tint/lang/core/type/vector.h"
@@ -447,6 +448,7 @@
TINT_ASSERT(!t->Is<core::type::ExternalTexture>());
auto* storage = t->As<core::type::StorageTexture>();
+ auto* sampled = t->As<core::type::SampledTexture>();
out << "highp ";
@@ -477,7 +479,7 @@
TINT_UNREACHABLE();
}
}
- auto* subtype = storage ? storage->Type() : nullptr;
+ auto* subtype = sampled ? sampled->Type() : storage ? storage->Type() : nullptr;
tint::Switch(
subtype, //
@@ -486,7 +488,7 @@
[&](const core::type::U32*) { out << "u"; }, //
TINT_ICE_ON_NO_MATCH);
- out << "image";
+ out << (storage ? "image" : "sampler");
switch (t->Dim()) {
case core::type::TextureDimension::k1d:
diff --git a/src/tint/lang/glsl/writer/type_test.cc b/src/tint/lang/glsl/writer/type_test.cc
index fb90160..844c1dd 100644
--- a/src/tint/lang/glsl/writer/type_test.cc
+++ b/src/tint/lang/glsl/writer/type_test.cc
@@ -562,9 +562,8 @@
out << str.str();
return out;
}
-using GlslWriterSampledTexturesTest = GlslWriterTestWithParam<GlslTextureData>;
-// TODO(dsinclair): Add sampled texture support
-TEST_P(GlslWriterSampledTexturesTest, DISABLED_Emit) {
+using GlslWriterSampledTextureESTest = GlslWriterTestWithParam<GlslTextureData>;
+TEST_P(GlslWriterSampledTextureESTest, Emit) {
auto params = GetParam();
const core::type::Type* subtype = nullptr;
@@ -573,7 +572,7 @@
subtype = ty.f32();
break;
case TextureDataType::kI32:
- subtype = ty.u32();
+ subtype = ty.i32();
break;
case TextureDataType::kU32:
subtype = ty.u32();
@@ -581,23 +580,83 @@
}
auto* t = ty.Get<core::type::SampledTexture>(params.dim, subtype);
- auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute);
+ auto* func = b.Function("foo", ty.void_());
auto* param = b.FunctionParam("a", t);
func->SetParams({param});
- func->SetWorkgroupSize(1, 1, 1);
b.Append(func->Block(), [&] { b.Return(func); });
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
- EXPECT_EQ(output_.glsl, R"(
-layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
-void main()" + params.result +
+ EXPECT_EQ(output_.glsl, GlslHeader() + R"(
+void foo(highp )" + params.result +
R"( a) {
}
+layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
+void main() {
+}
)");
}
INSTANTIATE_TEST_SUITE_P(
GlslWriterTest,
- GlslWriterSampledTexturesTest,
+ GlslWriterSampledTextureESTest,
+ testing::Values(
+ GlslTextureData{core::type::TextureDimension::k2d, TextureDataType::kF32, "sampler2D"},
+ GlslTextureData{core::type::TextureDimension::k2dArray, TextureDataType::kF32,
+ "sampler2DArray"},
+ GlslTextureData{core::type::TextureDimension::k3d, TextureDataType::kF32, "sampler3D"},
+ GlslTextureData{core::type::TextureDimension::kCube, TextureDataType::kF32, "samplerCube"},
+
+ GlslTextureData{core::type::TextureDimension::k2d, TextureDataType::kI32, "isampler2D"},
+ GlslTextureData{core::type::TextureDimension::k2dArray, TextureDataType::kI32,
+ "isampler2DArray"},
+ GlslTextureData{core::type::TextureDimension::k3d, TextureDataType::kI32, "isampler3D"},
+ GlslTextureData{core::type::TextureDimension::kCube, TextureDataType::kI32, "isamplerCube"},
+
+ GlslTextureData{core::type::TextureDimension::k2d, TextureDataType::kU32, "usampler2D"},
+ GlslTextureData{core::type::TextureDimension::k2dArray, TextureDataType::kU32,
+ "usampler2DArray"},
+ GlslTextureData{core::type::TextureDimension::k3d, TextureDataType::kU32, "usampler3D"},
+ GlslTextureData{core::type::TextureDimension::kCube, TextureDataType::kU32,
+ "usamplerCube"}));
+
+using GlslWriterSampledTextureNonESTest = GlslWriterTestWithParam<GlslTextureData>;
+TEST_P(GlslWriterSampledTextureNonESTest, Emit) {
+ auto params = GetParam();
+
+ const core::type::Type* subtype = nullptr;
+ switch (params.datatype) {
+ case TextureDataType::kF32:
+ subtype = ty.f32();
+ break;
+ case TextureDataType::kI32:
+ subtype = ty.i32();
+ break;
+ case TextureDataType::kU32:
+ subtype = ty.u32();
+ break;
+ }
+
+ auto* t = ty.Get<core::type::SampledTexture>(params.dim, subtype);
+ auto* func = b.Function("foo", ty.void_());
+ auto* param = b.FunctionParam("a", t);
+ func->SetParams({param});
+ b.Append(func->Block(), [&] { b.Return(func); });
+
+ Options opts{};
+ opts.version = Version(Version::Standard::kDesktop, 4, 6);
+ ASSERT_TRUE(Generate(opts)) << err_ << output_.glsl;
+ EXPECT_EQ(output_.glsl, R"(#version 460
+
+void foo(highp )" + params.result +
+ R"( a) {
+}
+layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
+void main() {
+}
+)");
+}
+INSTANTIATE_TEST_SUITE_P(
+ GlslWriterTest,
+ GlslWriterSampledTextureNonESTest,
testing::Values(
GlslTextureData{core::type::TextureDimension::k1d, TextureDataType::kF32, "sampler1D"},
GlslTextureData{core::type::TextureDimension::k2d, TextureDataType::kF32, "sampler2D"},
@@ -613,7 +672,7 @@
GlslTextureData{core::type::TextureDimension::k2dArray, TextureDataType::kI32,
"isampler2DArray"},
GlslTextureData{core::type::TextureDimension::k3d, TextureDataType::kI32, "isampler3D"},
- GlslTextureData{core::type::TextureDimension::kCube, TextureDataType::kI32, "samplerCube"},
+ GlslTextureData{core::type::TextureDimension::kCube, TextureDataType::kI32, "isamplerCube"},
GlslTextureData{core::type::TextureDimension::kCubeArray, TextureDataType::kI32,
"isamplerCubeArray"},