[wgsl-in] [wgsl-out] Implement bitcast()

This commit is contained in:
Zicklag authored and Dzmitry Malyshau committed 2021-08-20 17:19:23 -04:00
1 parent 778049f97a
commit 897afbd710
8 files changed
+156 -94

No files matched your search

+12 -2
View File
@@ -1145,7 +1145,11 @@ impl<W: Write> Writer<W> {
let name = &self.names[&NameKey::GlobalVariable(handle)];
write!(self.out, "{}", name)?;
}
Expression::As { expr, kind, .. } => {
Expression::As {
expr,
kind,
convert,
} => {
let inner = func_ctx.info[expr].ty.inner_with(&module.types);
match *inner {
TypeInner::Matrix { columns, rows, .. } => {
@@ -1164,7 +1168,13 @@ impl<W: Write> Writer<W> {
scalar_kind_str(kind)
)?;
}
TypeInner::Scalar { .. } => write!(self.out, "{}", scalar_kind_str(kind))?,
TypeInner::Scalar { .. } => {
if convert.is_some() {
write!(self.out, "{}", scalar_kind_str(kind))?
} else {
write!(self.out, "bitcast<{}>", scalar_kind_str(kind))?
}
}
_ => {
return Err(Error::Unimplemented(format!(
"write_expr expression::as {:?}",
+30
View File
@@ -1274,6 +1274,36 @@ impl Parser {
}
} else {
match name {
"bitcast" => {
let _ = lexer.next();
lexer.expect_generic_paren('<')?;
let ((ty, _access), type_span) = lexer.capture_span(|lexer| {
self.parse_type_decl(lexer, None, ctx.types, ctx.constants)
})?;
lexer.expect_generic_paren('>')?;
lexer.open_arguments()?;
let expr = self.parse_general_expression(lexer, ctx.reborrow())?;
lexer.close_arguments()?;
let kind = match ctx.types[ty].inner {
crate::TypeInner::Scalar { kind, .. } => kind,
crate::TypeInner::Vector { kind, .. } => kind,
_ => {
return Err(Error::BadTypeCast {
from_type: format!("{:?}", ctx.resolve_type(expr)?),
span: type_span,
to_type: format!("{:?}", ctx.types[ty].inner),
})
}
};
crate::Expression::As {
expr,
kind,
convert: None,
}
}
"select" => {
let _ = lexer.next();
lexer.open_arguments()?;
+5 -1
View File
@@ -2,6 +2,7 @@
let v_f32_one: vec4<f32> = vec4<f32>(1.0, 1.0, 1.0, 1.0);
let v_f32_zero: vec4<f32> = vec4<f32>(0.0, 0.0, 0.0, 0.0);
let v_f32_half: vec4<f32> = vec4<f32>(0.5, 0.5, 0.5, 0.5);
let v_i32_one: vec4<i32> = vec4<i32>(1, 1, 1, 1);
fn builtins() -> vec4<f32> {
// select()
@@ -12,8 +13,11 @@ fn builtins() -> vec4<f32> {
// mix()
let m1 = mix(v_f32_zero, v_f32_one, v_f32_half);
let m2 = mix(v_f32_zero, v_f32_one, 0.1);
// bitcast()
let b1 = bitcast<f32>(v_i32_one.x);
let b2 = bitcast<vec4<f32>>(v_i32_one);
// done
return vec4<f32>(vec4<i32>(s1)) + s2 + m1 + m2;
return vec4<f32>(vec4<i32>(s1)) + s2 + m1 + m2 + b1 + b2;
}
fn splat() -> vec4<f32> {
+6 -4
View File
@@ -12,7 +12,9 @@ vec4 builtins() {
vec4 s3_ = mix(vec4(0.0, 0.0, 0.0, 0.0), vec4(1.0, 1.0, 1.0, 1.0), bvec4(false, false, false, false));
vec4 m1_ = mix(vec4(0.0, 0.0, 0.0, 0.0), vec4(1.0, 1.0, 1.0, 1.0), vec4(0.5, 0.5, 0.5, 0.5));
vec4 m2_ = mix(vec4(0.0, 0.0, 0.0, 0.0), vec4(1.0, 1.0, 1.0, 1.0), 0.1);
return (((vec4(ivec4(s1_)) + s2_) + m1_) + m2_);
float b1_ = intBitsToFloat(ivec4(1, 1, 1, 1).x);
vec4 b2_ = intBitsToFloat(ivec4(1, 1, 1, 1));
return (((((vec4(ivec4(s1_)) + s2_) + m1_) + m2_) + vec4(b1_)) + b2_);
}
vec4 splat() {
@@ -30,9 +32,9 @@ int unary() {
}
void main() {
vec4 _e3 = builtins();
vec4 _e4 = splat();
int _e5 = unary();
vec4 _e4 = builtins();
vec4 _e5 = splat();
int _e6 = unary();
return;
}
+7 -4
View File
@@ -1,6 +1,7 @@
static const float4 v_f32_one = float4(1.0, 1.0, 1.0, 1.0);
static const float4 v_f32_zero = float4(0.0, 0.0, 0.0, 0.0);
static const float4 v_f32_half = float4(0.5, 0.5, 0.5, 0.5);
static const int4 v_i32_one = int4(1, 1, 1, 1);
float4 builtins()
{
@@ -9,7 +10,9 @@ float4 builtins()
float4 s3_ = (bool4(false, false, false, false) ? float4(0.0, 0.0, 0.0, 0.0) : float4(1.0, 1.0, 1.0, 1.0));
float4 m1_ = lerp(float4(0.0, 0.0, 0.0, 0.0), float4(1.0, 1.0, 1.0, 1.0), float4(0.5, 0.5, 0.5, 0.5));
float4 m2_ = lerp(float4(0.0, 0.0, 0.0, 0.0), float4(1.0, 1.0, 1.0, 1.0), 0.1);
return (((float4(int4(s1_.xxxx)) + s2_) + m1_) + m2_);
float b1_ = float(int4(1, 1, 1, 1).x);
float4 b2_ = float4(int4(1, 1, 1, 1));
return (((((float4(int4(s1_.xxxx)) + s2_) + m1_) + m2_) + float4(b1_.xxxx)) + b2_);
}
float4 splat()
@@ -31,8 +34,8 @@ int unary()
[numthreads(1, 1, 1)]
void main()
{
const float4 _e3 = builtins();
const float4 _e4 = splat();
const int _e5 = unary();
const float4 _e4 = builtins();
const float4 _e5 = splat();
const int _e6 = unary();
return;
}
+7 -4
View File
@@ -5,6 +5,7 @@
constant metal::float4 v_f32_one = {1.0, 1.0, 1.0, 1.0};
constant metal::float4 v_f32_zero = {0.0, 0.0, 0.0, 0.0};
constant metal::float4 v_f32_half = {0.5, 0.5, 0.5, 0.5};
constant metal::int4 v_i32_one = {1, 1, 1, 1};
metal::float4 builtins(
) {
@@ -13,7 +14,9 @@ metal::float4 builtins(
metal::float4 s3_ = metal::select(v_f32_one, v_f32_zero, metal::bool4(false, false, false, false));
metal::float4 m1_ = metal::mix(v_f32_zero, v_f32_one, v_f32_half);
metal::float4 m2_ = metal::mix(v_f32_zero, v_f32_one, 0.1);
return ((static_cast<float4>(metal::int4(s1_)) + s2_) + m1_) + m2_;
float b1_ = as_type<float>(v_i32_one.x);
metal::float4 b2_ = as_type<float4>(v_i32_one);
return ((((static_cast<float4>(metal::int4(s1_)) + s2_) + m1_) + m2_) + metal::float4(b1_)) + b2_;
}
metal::float4 splat(
@@ -34,8 +37,8 @@ int unary(
kernel void main1(
) {
metal::float4 _e3 = builtins();
metal::float4 _e4 = splat();
int _e5 = unary();
metal::float4 _e4 = builtins();
metal::float4 _e5 = splat();
int _e6 = unary();
return;
}
+82 -75
View File
@@ -1,99 +1,106 @@
; SPIR-V
; Version: 1.0
; Generator: rspirv
; Bound: 76
; Bound: 83
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %70 "main"
OpExecutionMode %70 LocalSize 1 1 1
OpEntryPoint GLCompute %77 "main"
OpExecutionMode %77 LocalSize 1 1 1
%2 = OpTypeVoid
%4 = OpTypeFloat 32
%3 = OpConstant %4 1.0
%5 = OpConstant %4 0.0
%6 = OpConstant %4 0.5
%8 = OpTypeBool
%7 = OpConstantTrue %8
%10 = OpTypeInt 32 1
%9 = OpConstant %10 0
%11 = OpConstant %10 1
%12 = OpConstantFalse %8
%8 = OpTypeInt 32 1
%7 = OpConstant %8 1
%10 = OpTypeBool
%9 = OpConstantTrue %10
%11 = OpConstant %8 0
%12 = OpConstantFalse %10
%13 = OpConstant %4 0.1
%14 = OpConstant %4 2.0
%15 = OpConstant %4 3.0
%16 = OpConstant %4 4.0
%17 = OpConstant %10 5
%18 = OpConstant %10 2
%17 = OpConstant %8 5
%18 = OpConstant %8 2
%19 = OpTypeVector %4 4
%20 = OpTypeVector %8 4
%21 = OpConstantComposite %19 %3 %3 %3 %3
%22 = OpConstantComposite %19 %5 %5 %5 %5
%23 = OpConstantComposite %19 %6 %6 %6 %6
%26 = OpTypeFunction %19
%36 = OpTypeVector %10 4
%45 = OpTypeVector %4 2
%61 = OpTypeFunction %10
%68 = OpConstantNull %10
%71 = OpTypeFunction %2
%25 = OpFunction %19 None %26
%24 = OpLabel
OpBranch %27
%27 = OpLabel
%28 = OpSelect %10 %7 %11 %9
%30 = OpCompositeConstruct %20 %7 %7 %7 %7
%29 = OpSelect %19 %30 %21 %22
%31 = OpCompositeConstruct %20 %12 %12 %12 %12
%32 = OpSelect %19 %31 %22 %21
%33 = OpExtInst %19 %1 FMix %22 %21 %23
%35 = OpCompositeConstruct %19 %13 %13 %13 %13
%34 = OpExtInst %19 %1 FMix %22 %21 %35
%37 = OpCompositeConstruct %36 %28 %28 %28 %28
%38 = OpConvertSToF %19 %37
%39 = OpFAdd %19 %38 %29
%40 = OpFAdd %19 %39 %33
%41 = OpFAdd %19 %40 %34
OpReturnValue %41
%21 = OpTypeVector %10 4
%22 = OpConstantComposite %19 %3 %3 %3 %3
%23 = OpConstantComposite %19 %5 %5 %5 %5
%24 = OpConstantComposite %19 %6 %6 %6 %6
%25 = OpConstantComposite %20 %7 %7 %7 %7
%28 = OpTypeFunction %19
%52 = OpTypeVector %4 2
%68 = OpTypeFunction %8
%75 = OpConstantNull %8
%78 = OpTypeFunction %2
%27 = OpFunction %19 None %28
%26 = OpLabel
OpBranch %29
%29 = OpLabel
%30 = OpSelect %8 %9 %7 %11
%32 = OpCompositeConstruct %21 %9 %9 %9 %9
%31 = OpSelect %19 %32 %22 %23
%33 = OpCompositeConstruct %21 %12 %12 %12 %12
%34 = OpSelect %19 %33 %23 %22
%35 = OpExtInst %19 %1 FMix %23 %22 %24
%37 = OpCompositeConstruct %19 %13 %13 %13 %13
%36 = OpExtInst %19 %1 FMix %23 %22 %37
%38 = OpCompositeExtract %8 %25 0
%39 = OpBitcast %4 %38
%40 = OpBitcast %19 %25
%41 = OpCompositeConstruct %20 %30 %30 %30 %30
%42 = OpConvertSToF %19 %41
%43 = OpFAdd %19 %42 %31
%44 = OpFAdd %19 %43 %35
%45 = OpFAdd %19 %44 %36
%46 = OpCompositeConstruct %19 %39 %39 %39 %39
%47 = OpFAdd %19 %45 %46
%48 = OpFAdd %19 %47 %40
OpReturnValue %48
OpFunctionEnd
%43 = OpFunction %19 None %26
%42 = OpLabel
OpBranch %44
%44 = OpLabel
%46 = OpCompositeConstruct %45 %14 %14
%47 = OpCompositeConstruct %45 %3 %3
%48 = OpFAdd %45 %47 %46
%49 = OpCompositeConstruct %45 %15 %15
%50 = OpFSub %45 %48 %49
%51 = OpCompositeConstruct %45 %16 %16
%52 = OpFDiv %45 %50 %51
%53 = OpCompositeConstruct %36 %17 %17 %17 %17
%54 = OpCompositeConstruct %36 %18 %18 %18 %18
%55 = OpSMod %36 %53 %54
%56 = OpVectorShuffle %19 %52 %52 0 1 0 1
%57 = OpConvertSToF %19 %55
%58 = OpFAdd %19 %56 %57
OpReturnValue %58
%50 = OpFunction %19 None %28
%49 = OpLabel
OpBranch %51
%51 = OpLabel
%53 = OpCompositeConstruct %52 %14 %14
%54 = OpCompositeConstruct %52 %3 %3
%55 = OpFAdd %52 %54 %53
%56 = OpCompositeConstruct %52 %15 %15
%57 = OpFSub %52 %55 %56
%58 = OpCompositeConstruct %52 %16 %16
%59 = OpFDiv %52 %57 %58
%60 = OpCompositeConstruct %20 %17 %17 %17 %17
%61 = OpCompositeConstruct %20 %18 %18 %18 %18
%62 = OpSMod %20 %60 %61
%63 = OpVectorShuffle %19 %59 %59 0 1 0 1
%64 = OpConvertSToF %19 %62
%65 = OpFAdd %19 %63 %64
OpReturnValue %65
OpFunctionEnd
%60 = OpFunction %10 None %61
%59 = OpLabel
OpBranch %62
%62 = OpLabel
%63 = OpLogicalNot %8 %7
OpSelectionMerge %64 None
OpBranchConditional %63 %65 %66
%65 = OpLabel
OpReturnValue %11
%67 = OpFunction %8 None %68
%66 = OpLabel
%67 = OpNot %10 %11
OpReturnValue %67
%64 = OpLabel
OpReturnValue %68
OpFunctionEnd
%70 = OpFunction %2 None %71
OpBranch %69
%69 = OpLabel
OpBranch %72
%70 = OpLogicalNot %10 %9
OpSelectionMerge %71 None
OpBranchConditional %70 %72 %73
%72 = OpLabel
%73 = OpFunctionCall %19 %25
%74 = OpFunctionCall %19 %43
%75 = OpFunctionCall %10 %60
OpReturnValue %7
%73 = OpLabel
%74 = OpNot %8 %7
OpReturnValue %74
%71 = OpLabel
OpReturnValue %75
OpFunctionEnd
%77 = OpFunction %2 None %78
%76 = OpLabel
OpBranch %79
%79 = OpLabel
%80 = OpFunctionCall %19 %27
%81 = OpFunctionCall %19 %50
%82 = OpFunctionCall %8 %67
OpReturn
OpFunctionEnd
+7 -4
View File
@@ -1,13 +1,16 @@
let v_f32_one: vec4<f32> = vec4<f32>(1.0, 1.0, 1.0, 1.0);
let v_f32_zero: vec4<f32> = vec4<f32>(0.0, 0.0, 0.0, 0.0);
let v_f32_half: vec4<f32> = vec4<f32>(0.5, 0.5, 0.5, 0.5);
let v_i32_one: vec4<i32> = vec4<i32>(1, 1, 1, 1);
fn builtins() -> vec4<f32> {
let s1_: i32 = select(0, 1, true);
let s2_: vec4<f32> = select(vec4<f32>(0.0, 0.0, 0.0, 0.0), vec4<f32>(1.0, 1.0, 1.0, 1.0), true);
let s3_: vec4<f32> = select(vec4<f32>(1.0, 1.0, 1.0, 1.0), vec4<f32>(0.0, 0.0, 0.0, 0.0), vec4<bool>(false, false, false, false));
let m1_: vec4<f32> = mix(vec4<f32>(0.0, 0.0, 0.0, 0.0), vec4<f32>(1.0, 1.0, 1.0, 1.0), vec4<f32>(0.5, 0.5, 0.5, 0.5));
let m2_: vec4<f32> = mix(vec4<f32>(0.0, 0.0, 0.0, 0.0), vec4<f32>(1.0, 1.0, 1.0, 1.0), 0.1);
return (((vec4<f32>(vec4<i32>(s1_)) + s2_) + m1_) + m2_);
let b1_: f32 = bitcast<f32>(vec4<i32>(1, 1, 1, 1).x);
let b2_: vec4<f32> = vec4<f32>(vec4<i32>(1, 1, 1, 1));
return (((((vec4<f32>(vec4<i32>(s1_)) + s2_) + m1_) + m2_) + vec4<f32>(b1_)) + b2_);
}
fn splat() -> vec4<f32> {
@@ -26,8 +29,8 @@ fn unary() -> i32 {
[[stage(compute), workgroup_size(1, 1, 1)]]
fn main() {
let _e3: vec4<f32> = builtins();
let _e4: vec4<f32> = splat();
let _e5: i32 = unary();
let _e4: vec4<f32> = builtins();
let _e5: vec4<f32> = splat();
let _e6: i32 = unary();
return;
}