Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WGSL] Add overloads for textureSample #11934

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion Source/WebGPU/WGSL/Overload.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ struct TypeVariable {

I32 = 1 << 3,
U32 = 1 << 4,
ConcreteInteger = I32 | U32,
AbstractInt = 1 << 5,
Integer = I32 | U32 | AbstractInt,
Integer = ConcreteInteger | AbstractInt,

Number = Float | Integer,
};
Expand Down
15 changes: 15 additions & 0 deletions Source/WebGPU/WGSL/TypeDeclarations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,18 @@
[T < Float, C, R].(Matrix[T, C, R], Vector[T, C]) => Vector[T, R],
[T < Float, C, R].(Vector[T, R], Matrix[T, C, R]) => Vector[T, C],
}

operator :textureSample, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we document the missing textureSample functions with a FIXME?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a link to a bugzilla bug that lists all the missing overloads

[].(Texture[F32, Texture1d], Sampler, F32) => Vector[F32, 4],
[].(Texture[F32, Texture2d], Sampler, Vector[F32, 2]) => Vector[F32, 4],
[].(Texture[F32, Texture2d], Sampler, Vector[F32, 2], Vector[I32, 2]) => Vector[F32, 4],
[T < ConcreteInteger].(Texture[F32, Texture2dArray], Sampler, Vector[F32, 2], T) => Vector[F32, 4],
[T < ConcreteInteger].(Texture[F32, Texture2dArray], Sampler, Vector[F32, 2], T, Vector[I32, 2]) => Vector[F32, 4],
[].(Texture[F32, Texture3d], Sampler, Vector[F32, 3]) => Vector[F32, 4],
[].(Texture[F32, TextureCube], Sampler, Vector[F32, 3]) => Vector[F32, 4],
[].(Texture[F32, Texture3d], Sampler, Vector[F32, 3], Vector[I32, 3]) => Vector[F32, 4],
[T < ConcreteInteger].(Texture[F32, TextureCubeArray], Sampler, Vector[F32, 3], T) => Vector[F32, 4],

# FIXME: add overloads for texture_depth
# https://bugs.webkit.org/show_bug.cgi?id=254515
}
52 changes: 49 additions & 3 deletions Source/WebGPU/WGSL/generator/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def initialize(value)
@value = value
end

def to_s
value.to_s
end

def to_cpp
"AbstractValue { #{value}u }"
end
Expand All @@ -88,14 +92,45 @@ def initialize(name)
end

def [](*arguments)
ParameterizedType.new(self, arguments)
# This is a bit hacky, but the idea is that types such as Vector can
# either become:
# 1) An AbstractType, if it receives a variable. This AbstractType will
# later be promoted to a concrete type once an overload is chosen.
# 2) A concrete type if it's given a concrete type. Since there are no
# variables involved, we can immediately construct a concrete type.
if arguments[0].is_a? Variable
ParameterizedAbstractType.new(self, arguments)
else
Constructor.new(@name.downcase)[*arguments]
end
end

def to_s
@name.to_s
end
end

class Constructor
attr_reader :name, :arguments

def initialize(name, arguments = nil)
@name = name
@arguments = arguments
end

def [](*arguments)
return Constructor.new(@name, arguments)
end

def to_s
"#{name.to_s}[#{arguments.map(&:to_s).join(", ")}]"
end

def to_cpp
"AbstractType { m_types.#{name}Type(#{arguments.map { |a| a.respond_to? :to_cpp and a.to_cpp or a}.join ", "}) }"
end
end

class PrimitiveType
attr_reader :name

Expand All @@ -108,11 +143,11 @@ def to_s
end

def to_cpp
"AbstractType { m_types.#{name.downcase}Type() }"
"m_types.#{name.downcase}Type()"
end
end

class ParameterizedType
class ParameterizedAbstractType
attr_reader :base, :arguments

def initialize(base, arguments)
Expand Down Expand Up @@ -229,10 +264,20 @@ def self.prologue
Matrix = AbstractType.new(:Matrix)
Array = AbstractType.new(:Array)

Texture = Constructor.new(:texture)

Texture1d = Variable.new(:"Types::Texture::Kind::Texture1d", nil)
Texture2d = Variable.new(:"Types::Texture::Kind::Texture2d", nil)
Texture2dArray = Variable.new(:"Types::Texture::Kind::Texture2dArray", nil)
Texture3d = Variable.new(:"Types::Texture::Kind::Texture3d", nil)
TextureCube = Variable.new(:"Types::Texture::Kind::TextureCube", nil)
TextureCubeArray = Variable.new(:"Types::Texture::Kind::TextureCubeArray", nil)

Bool = PrimitiveType.new(:Bool)
I32 = PrimitiveType.new(:I32)
U32 = PrimitiveType.new(:U32)
F32 = PrimitiveType.new(:F32)
Sampler = PrimitiveType.new(:Sampler)
AbstractInt = PrimitiveType.new(:AbstractInt)


Expand All @@ -243,6 +288,7 @@ def self.prologue

Number = Constraint.new(:Number)
Float = Constraint.new(:Float)
ConcreteInteger = Constraint.new(:ConcreteInteger)
EOS
end

Expand Down
56 changes: 56 additions & 0 deletions Source/WebGPU/WGSL/tests/valid/overload.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,59 @@ fn testMultiply() {
let r1 = m * v2;
let r2 = v4 * m;
}

fn testTextureSample() {
{
let t: texture_1d<f32>;
let s: sampler;
let r = textureSample(t, s, 1);
}

{
let t: texture_2d<f32>;
let s: sampler;
let r = textureSample(t, s, vec2<f32>(0, 0));
}

{
let t: texture_2d<f32>;
let s: sampler;
let r = textureSample(t, s, vec2<f32>(0, 0), vec2<i32>(1, 1));
}

{
let t: texture_2d_array<f32>;
let s: sampler;
let r = textureSample(t, s, vec2<f32>(0, 0), 0);
}

{
let t: texture_2d_array<f32>;
let s: sampler;
let r = textureSample(t, s, vec2<f32>(0, 0), 0, vec2<i32>(1, 1));
}

{
let t: texture_3d<f32>;
let s: sampler;
let r = textureSample(t, s, vec3<f32>(0, 0, 0));
}

{
let t: texture_cube<f32>;
let s: sampler;
let r = textureSample(t, s, vec3<f32>(0, 0, 0));
}

{
let t: texture_3d<f32>;
let s: sampler;
let r = textureSample(t, s, vec3<f32>(0, 0, 0), vec3<i32>(0, 0, 0));
}

{
let t: texture_cube_array<f32>;
let s: sampler;
let r = textureSample(t, s, vec3<f32>(0, 0, 0), 0);
}
}