Skip to content

Commit ac3c958

Browse files
committed
[WGSL] Add suport for depth textures
https://bugs.webkit.org/show_bug.cgi?id=261881 rdar://115842732 Reviewed by Dan Glastonbury. Add a new TextureDepth type to the type system and expose all the kinds of depth textures. Spec reference: https://www.w3.org/TR/WGSL/#texture-depth * Source/WebGPU/WGSL/ConstantRewriter.cpp: (WGSL::ConstantRewriter::materialize): * Source/WebGPU/WGSL/Constraints.cpp: (WGSL::concretize): * Source/WebGPU/WGSL/GlobalVariableRewriter.cpp: (WGSL::bindingMemberForGlobal): * Source/WebGPU/WGSL/Metal/MetalFunctionWriter.cpp: (WGSL::Metal::FunctionDefinitionWriter::visit): * Source/WebGPU/WGSL/TypeCheck.cpp: (WGSL::TypeChecker::TypeChecker): * Source/WebGPU/WGSL/TypeStore.cpp: (WGSL::TypeStore::TypeStore): * Source/WebGPU/WGSL/TypeStore.h: (WGSL::TypeStore::textureDepth2dType const): (WGSL::TypeStore::textureDepth2dArrayType const): (WGSL::TypeStore::textureDepthCubeType const): (WGSL::TypeStore::textureDepthCubeArrayType const): (WGSL::TypeStore::textureDepthMultisampled2dType const): * Source/WebGPU/WGSL/Types.cpp: (WGSL::Type::dump const): (WGSL::Type::size const): (WGSL::Type::alignment const): * Source/WebGPU/WGSL/Types.h: Canonical link: https://commits.webkit.org/268308@main
1 parent a55b02b commit ac3c958

File tree

9 files changed

+115
-0
lines changed

9 files changed

+115
-0
lines changed

Source/WebGPU/WGSL/ConstantRewriter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ void ConstantRewriter::materialize(Node& expression, const ConstantValue& value)
314314
[&](const TextureStorage&) {
315315
RELEASE_ASSERT_NOT_REACHED();
316316
},
317+
[&](const TextureDepth&) {
318+
RELEASE_ASSERT_NOT_REACHED();
319+
},
317320
[&](const TypeConstructor&) {
318321
RELEASE_ASSERT_NOT_REACHED();
319322
},

Source/WebGPU/WGSL/Constraints.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ const Type* concretize(const Type* type, TypeStore& types)
177177
[&](const TextureStorage&) -> const Type* {
178178
RELEASE_ASSERT_NOT_REACHED();
179179
},
180+
[&](const TextureDepth&) -> const Type* {
181+
RELEASE_ASSERT_NOT_REACHED();
182+
},
180183
[&](const Reference&) -> const Type* {
181184
RELEASE_ASSERT_NOT_REACHED();
182185
},

Source/WebGPU/WGSL/GlobalVariableRewriter.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,33 @@ static BindGroupLayoutEntry::BindingMember bindingMemberForGlobal(auto& global)
708708
return StorageTextureBindingLayout {
709709
.viewDimension = viewDimension
710710
};
711+
}, [&](const TextureDepth& texture) -> BindGroupLayoutEntry::BindingMember {
712+
TextureViewDimension viewDimension;
713+
bool multisampled = false;
714+
switch (texture.kind) {
715+
case Types::TextureDepth::Kind::TextureDepth2d:
716+
viewDimension = TextureViewDimension::TwoDimensional;
717+
break;
718+
case Types::TextureDepth::Kind::TextureDepth2dArray:
719+
viewDimension = TextureViewDimension::TwoDimensionalArray;
720+
break;
721+
case Types::TextureDepth::Kind::TextureDepthCube:
722+
viewDimension = TextureViewDimension::Cube;
723+
break;
724+
case Types::TextureDepth::Kind::TextureDepthCubeArray:
725+
viewDimension = TextureViewDimension::CubeArray;
726+
break;
727+
case Types::TextureDepth::Kind::TextureDepthMultisampled2d:
728+
viewDimension = TextureViewDimension::TwoDimensional;
729+
multisampled = true;
730+
break;
731+
}
732+
733+
return TextureBindingLayout {
734+
.sampleType = TextureSampleType::Depth,
735+
.viewDimension = viewDimension,
736+
.multisampled = multisampled
737+
};
711738
}, [&](const Reference&) -> BindGroupLayoutEntry::BindingMember {
712739
RELEASE_ASSERT_NOT_REACHED();
713740
}, [&](const Pointer&) -> BindGroupLayoutEntry::BindingMember {

Source/WebGPU/WGSL/Metal/MetalFunctionWriter.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,27 @@ void FunctionDefinitionWriter::visit(const Type* type)
693693
}
694694
m_stringBuilder.append(base, "<", type, ", access::", mode, ">");
695695
},
696+
[&](const TextureDepth& texture) {
697+
const char* base;
698+
switch (texture.kind) {
699+
case TextureDepth::Kind::TextureDepth2d:
700+
base = "depth2d";
701+
break;
702+
case TextureDepth::Kind::TextureDepth2dArray:
703+
base = "depth2d_array";
704+
break;
705+
case TextureDepth::Kind::TextureDepthCube:
706+
base = "depthcube";
707+
break;
708+
case TextureDepth::Kind::TextureDepthCubeArray:
709+
base = "depthcube_array";
710+
break;
711+
case TextureDepth::Kind::TextureDepthMultisampled2d:
712+
base = "depth2d_ms";
713+
break;
714+
}
715+
m_stringBuilder.append(base, "<float>");
716+
},
696717
[&](const Reference& reference) {
697718
const char* addressSpace = nullptr;
698719
switch (reference.addressSpace) {

Source/WebGPU/WGSL/TypeCheck.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ TypeChecker::TypeChecker(ShaderModule& shaderModule)
162162
introduceType(AST::Identifier::make("sampler"_s), m_types.samplerType());
163163
introduceType(AST::Identifier::make("texture_external"_s), m_types.textureExternalType());
164164

165+
introduceType(AST::Identifier::make("texture_depth_2d"_s), m_types.textureDepth2dType());
166+
introduceType(AST::Identifier::make("texture_depth_2d_array"_s), m_types.textureDepth2dArrayType());
167+
introduceType(AST::Identifier::make("texture_depth_cube"_s), m_types.textureDepthCubeType());
168+
introduceType(AST::Identifier::make("texture_depth_cube_array"_s), m_types.textureDepthCubeArrayType());
169+
introduceType(AST::Identifier::make("texture_depth_multisampled_2d"_s), m_types.textureDepthMultisampled2dType());
170+
165171
introduceType(AST::Identifier::make("ptr"_s), m_types.typeConstructorType(
166172
"ptr"_s,
167173
[this](AST::ElaboratedTypeExpression& type) -> const Type* {

Source/WebGPU/WGSL/TypeStore.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ TypeStore::TypeStore()
117117
m_accessMode = allocateType<Primitive>(Primitive::AccessMode);
118118
m_texelFormat = allocateType<Primitive>(Primitive::TexelFormat);
119119
m_addressSpace = allocateType<Primitive>(Primitive::AddressSpace);
120+
m_textureDepth2d = allocateType<TextureDepth>(TextureDepth::Kind::TextureDepth2d);
121+
m_textureDepthArray2d = allocateType<TextureDepth>(TextureDepth::Kind::TextureDepth2dArray);
122+
m_textureDepthCube = allocateType<TextureDepth>(TextureDepth::Kind::TextureDepthCube);
123+
m_textureDepthArrayCube = allocateType<TextureDepth>(TextureDepth::Kind::TextureDepthCubeArray);
124+
m_textureDepthMultisampled2d = allocateType<TextureDepth>(TextureDepth::Kind::TextureDepthMultisampled2d);
120125
}
121126

122127
const Type* TypeStore::structType(AST::Structure& structure, HashMap<String, const Type*>&& fields)

Source/WebGPU/WGSL/TypeStore.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ class TypeStore {
8383
const Type* texelFormatType() const { return m_texelFormat; }
8484
const Type* addressSpaceType() const { return m_addressSpace; }
8585

86+
const Type* textureDepth2dType() const { return m_textureDepth2d; }
87+
const Type* textureDepth2dArrayType() const { return m_textureDepthArray2d; }
88+
const Type* textureDepthCubeType() const { return m_textureDepthCube; }
89+
const Type* textureDepthCubeArrayType() const { return m_textureDepthArrayCube; }
90+
const Type* textureDepthMultisampled2dType() const { return m_textureDepthMultisampled2d; }
91+
8692
const Type* structType(AST::Structure&, HashMap<String, const Type*>&& = { });
8793
const Type* arrayType(const Type*, std::optional<unsigned>);
8894
const Type* vectorType(const Type*, uint8_t);
@@ -114,6 +120,11 @@ class TypeStore {
114120
const Type* m_accessMode;
115121
const Type* m_texelFormat;
116122
const Type* m_addressSpace;
123+
const Type* m_textureDepth2d;
124+
const Type* m_textureDepthArray2d;
125+
const Type* m_textureDepthCube;
126+
const Type* m_textureDepthArrayCube;
127+
const Type* m_textureDepthMultisampled2d;
117128
};
118129

119130
} // namespace WGSL

Source/WebGPU/WGSL/Types.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,25 @@ void Type::dump(PrintStream& out) const
183183
}
184184
out.print(">");
185185
},
186+
[&](const TextureDepth& texture) {
187+
switch (texture.kind) {
188+
case TextureDepth::Kind::TextureDepth2d:
189+
out.print("texture_depth_2d");
190+
break;
191+
case TextureDepth::Kind::TextureDepth2dArray:
192+
out.print("texture_depth_2d_array");
193+
break;
194+
case TextureDepth::Kind::TextureDepthCube:
195+
out.print("texture_depth_cube");
196+
break;
197+
case TextureDepth::Kind::TextureDepthCubeArray:
198+
out.print("texture_depth_cube_array");
199+
break;
200+
case TextureDepth::Kind::TextureDepthMultisampled2d:
201+
out.print("texture_depth_multisampled_2d");
202+
break;
203+
}
204+
},
186205
[&](const Reference& reference) {
187206
out.print("ref<", reference.addressSpace, ", ", *reference.element, ", ", reference.accessMode, ">");
188207
},
@@ -344,6 +363,9 @@ unsigned Type::size() const
344363
[&](const TextureStorage&) -> unsigned {
345364
RELEASE_ASSERT_NOT_REACHED();
346365
},
366+
[&](const TextureDepth&) -> unsigned {
367+
RELEASE_ASSERT_NOT_REACHED();
368+
},
347369
[&](const Reference&) -> unsigned {
348370
RELEASE_ASSERT_NOT_REACHED();
349371
},
@@ -409,6 +431,9 @@ unsigned Type::alignment() const
409431
[&](const TextureStorage&) -> unsigned {
410432
RELEASE_ASSERT_NOT_REACHED();
411433
},
434+
[&](const TextureDepth&) -> unsigned {
435+
RELEASE_ASSERT_NOT_REACHED();
436+
},
412437
[&](const Reference&) -> unsigned {
413438
RELEASE_ASSERT_NOT_REACHED();
414439
},

Source/WebGPU/WGSL/Types.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ struct TextureStorage {
125125
AccessMode access;
126126
};
127127

128+
struct TextureDepth {
129+
enum class Kind : uint8_t {
130+
TextureDepth2d = 1,
131+
TextureDepth2dArray,
132+
TextureDepthCube,
133+
TextureDepthCubeArray,
134+
TextureDepthMultisampled2d,
135+
};
136+
137+
Kind kind;
138+
};
139+
128140
struct Vector {
129141
const Type* element;
130142
uint8_t size;
@@ -182,6 +194,7 @@ struct Type : public std::variant<
182194
Types::Function,
183195
Types::Texture,
184196
Types::TextureStorage,
197+
Types::TextureDepth,
185198
Types::Reference,
186199
Types::Pointer,
187200
Types::TypeConstructor,
@@ -196,6 +209,7 @@ struct Type : public std::variant<
196209
Types::Function,
197210
Types::Texture,
198211
Types::TextureStorage,
212+
Types::TextureDepth,
199213
Types::Reference,
200214
Types::Pointer,
201215
Types::TypeConstructor,

0 commit comments

Comments
 (0)