Skip to content

Commit

Permalink
[WGSL] Add suport for depth textures
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tadeuzagallo committed Sep 22, 2023
1 parent a55b02b commit ac3c958
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Source/WebGPU/WGSL/ConstantRewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ void ConstantRewriter::materialize(Node& expression, const ConstantValue& value)
[&](const TextureStorage&) {
RELEASE_ASSERT_NOT_REACHED();
},
[&](const TextureDepth&) {
RELEASE_ASSERT_NOT_REACHED();
},
[&](const TypeConstructor&) {
RELEASE_ASSERT_NOT_REACHED();
},
Expand Down
3 changes: 3 additions & 0 deletions Source/WebGPU/WGSL/Constraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ const Type* concretize(const Type* type, TypeStore& types)
[&](const TextureStorage&) -> const Type* {
RELEASE_ASSERT_NOT_REACHED();
},
[&](const TextureDepth&) -> const Type* {
RELEASE_ASSERT_NOT_REACHED();
},
[&](const Reference&) -> const Type* {
RELEASE_ASSERT_NOT_REACHED();
},
Expand Down
27 changes: 27 additions & 0 deletions Source/WebGPU/WGSL/GlobalVariableRewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,33 @@ static BindGroupLayoutEntry::BindingMember bindingMemberForGlobal(auto& global)
return StorageTextureBindingLayout {
.viewDimension = viewDimension
};
}, [&](const TextureDepth& texture) -> BindGroupLayoutEntry::BindingMember {
TextureViewDimension viewDimension;
bool multisampled = false;
switch (texture.kind) {
case Types::TextureDepth::Kind::TextureDepth2d:
viewDimension = TextureViewDimension::TwoDimensional;
break;
case Types::TextureDepth::Kind::TextureDepth2dArray:
viewDimension = TextureViewDimension::TwoDimensionalArray;
break;
case Types::TextureDepth::Kind::TextureDepthCube:
viewDimension = TextureViewDimension::Cube;
break;
case Types::TextureDepth::Kind::TextureDepthCubeArray:
viewDimension = TextureViewDimension::CubeArray;
break;
case Types::TextureDepth::Kind::TextureDepthMultisampled2d:
viewDimension = TextureViewDimension::TwoDimensional;
multisampled = true;
break;
}

return TextureBindingLayout {
.sampleType = TextureSampleType::Depth,
.viewDimension = viewDimension,
.multisampled = multisampled
};
}, [&](const Reference&) -> BindGroupLayoutEntry::BindingMember {
RELEASE_ASSERT_NOT_REACHED();
}, [&](const Pointer&) -> BindGroupLayoutEntry::BindingMember {
Expand Down
21 changes: 21 additions & 0 deletions Source/WebGPU/WGSL/Metal/MetalFunctionWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,27 @@ void FunctionDefinitionWriter::visit(const Type* type)
}
m_stringBuilder.append(base, "<", type, ", access::", mode, ">");
},
[&](const TextureDepth& texture) {
const char* base;
switch (texture.kind) {
case TextureDepth::Kind::TextureDepth2d:
base = "depth2d";
break;
case TextureDepth::Kind::TextureDepth2dArray:
base = "depth2d_array";
break;
case TextureDepth::Kind::TextureDepthCube:
base = "depthcube";
break;
case TextureDepth::Kind::TextureDepthCubeArray:
base = "depthcube_array";
break;
case TextureDepth::Kind::TextureDepthMultisampled2d:
base = "depth2d_ms";
break;
}
m_stringBuilder.append(base, "<float>");
},
[&](const Reference& reference) {
const char* addressSpace = nullptr;
switch (reference.addressSpace) {
Expand Down
6 changes: 6 additions & 0 deletions Source/WebGPU/WGSL/TypeCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ TypeChecker::TypeChecker(ShaderModule& shaderModule)
introduceType(AST::Identifier::make("sampler"_s), m_types.samplerType());
introduceType(AST::Identifier::make("texture_external"_s), m_types.textureExternalType());

introduceType(AST::Identifier::make("texture_depth_2d"_s), m_types.textureDepth2dType());
introduceType(AST::Identifier::make("texture_depth_2d_array"_s), m_types.textureDepth2dArrayType());
introduceType(AST::Identifier::make("texture_depth_cube"_s), m_types.textureDepthCubeType());
introduceType(AST::Identifier::make("texture_depth_cube_array"_s), m_types.textureDepthCubeArrayType());
introduceType(AST::Identifier::make("texture_depth_multisampled_2d"_s), m_types.textureDepthMultisampled2dType());

introduceType(AST::Identifier::make("ptr"_s), m_types.typeConstructorType(
"ptr"_s,
[this](AST::ElaboratedTypeExpression& type) -> const Type* {
Expand Down
5 changes: 5 additions & 0 deletions Source/WebGPU/WGSL/TypeStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ TypeStore::TypeStore()
m_accessMode = allocateType<Primitive>(Primitive::AccessMode);
m_texelFormat = allocateType<Primitive>(Primitive::TexelFormat);
m_addressSpace = allocateType<Primitive>(Primitive::AddressSpace);
m_textureDepth2d = allocateType<TextureDepth>(TextureDepth::Kind::TextureDepth2d);
m_textureDepthArray2d = allocateType<TextureDepth>(TextureDepth::Kind::TextureDepth2dArray);
m_textureDepthCube = allocateType<TextureDepth>(TextureDepth::Kind::TextureDepthCube);
m_textureDepthArrayCube = allocateType<TextureDepth>(TextureDepth::Kind::TextureDepthCubeArray);
m_textureDepthMultisampled2d = allocateType<TextureDepth>(TextureDepth::Kind::TextureDepthMultisampled2d);
}

const Type* TypeStore::structType(AST::Structure& structure, HashMap<String, const Type*>&& fields)
Expand Down
11 changes: 11 additions & 0 deletions Source/WebGPU/WGSL/TypeStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ class TypeStore {
const Type* texelFormatType() const { return m_texelFormat; }
const Type* addressSpaceType() const { return m_addressSpace; }

const Type* textureDepth2dType() const { return m_textureDepth2d; }
const Type* textureDepth2dArrayType() const { return m_textureDepthArray2d; }
const Type* textureDepthCubeType() const { return m_textureDepthCube; }
const Type* textureDepthCubeArrayType() const { return m_textureDepthArrayCube; }
const Type* textureDepthMultisampled2dType() const { return m_textureDepthMultisampled2d; }

const Type* structType(AST::Structure&, HashMap<String, const Type*>&& = { });
const Type* arrayType(const Type*, std::optional<unsigned>);
const Type* vectorType(const Type*, uint8_t);
Expand Down Expand Up @@ -114,6 +120,11 @@ class TypeStore {
const Type* m_accessMode;
const Type* m_texelFormat;
const Type* m_addressSpace;
const Type* m_textureDepth2d;
const Type* m_textureDepthArray2d;
const Type* m_textureDepthCube;
const Type* m_textureDepthArrayCube;
const Type* m_textureDepthMultisampled2d;
};

} // namespace WGSL
25 changes: 25 additions & 0 deletions Source/WebGPU/WGSL/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,25 @@ void Type::dump(PrintStream& out) const
}
out.print(">");
},
[&](const TextureDepth& texture) {
switch (texture.kind) {
case TextureDepth::Kind::TextureDepth2d:
out.print("texture_depth_2d");
break;
case TextureDepth::Kind::TextureDepth2dArray:
out.print("texture_depth_2d_array");
break;
case TextureDepth::Kind::TextureDepthCube:
out.print("texture_depth_cube");
break;
case TextureDepth::Kind::TextureDepthCubeArray:
out.print("texture_depth_cube_array");
break;
case TextureDepth::Kind::TextureDepthMultisampled2d:
out.print("texture_depth_multisampled_2d");
break;
}
},
[&](const Reference& reference) {
out.print("ref<", reference.addressSpace, ", ", *reference.element, ", ", reference.accessMode, ">");
},
Expand Down Expand Up @@ -344,6 +363,9 @@ unsigned Type::size() const
[&](const TextureStorage&) -> unsigned {
RELEASE_ASSERT_NOT_REACHED();
},
[&](const TextureDepth&) -> unsigned {
RELEASE_ASSERT_NOT_REACHED();
},
[&](const Reference&) -> unsigned {
RELEASE_ASSERT_NOT_REACHED();
},
Expand Down Expand Up @@ -409,6 +431,9 @@ unsigned Type::alignment() const
[&](const TextureStorage&) -> unsigned {
RELEASE_ASSERT_NOT_REACHED();
},
[&](const TextureDepth&) -> unsigned {
RELEASE_ASSERT_NOT_REACHED();
},
[&](const Reference&) -> unsigned {
RELEASE_ASSERT_NOT_REACHED();
},
Expand Down
14 changes: 14 additions & 0 deletions Source/WebGPU/WGSL/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ struct TextureStorage {
AccessMode access;
};

struct TextureDepth {
enum class Kind : uint8_t {
TextureDepth2d = 1,
TextureDepth2dArray,
TextureDepthCube,
TextureDepthCubeArray,
TextureDepthMultisampled2d,
};

Kind kind;
};

struct Vector {
const Type* element;
uint8_t size;
Expand Down Expand Up @@ -182,6 +194,7 @@ struct Type : public std::variant<
Types::Function,
Types::Texture,
Types::TextureStorage,
Types::TextureDepth,
Types::Reference,
Types::Pointer,
Types::TypeConstructor,
Expand All @@ -196,6 +209,7 @@ struct Type : public std::variant<
Types::Function,
Types::Texture,
Types::TextureStorage,
Types::TextureDepth,
Types::Reference,
Types::Pointer,
Types::TypeConstructor,
Expand Down

0 comments on commit ac3c958

Please sign in to comment.