blob: c63784495b099970aea340f2cd29d62cae91003f [file] [log] [blame]
// Copyright 2024 The Dawn & Tint Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "src/tint/lang/core/fluent_types.h"
#include "src/tint/lang/core/ir/function.h"
#include "src/tint/lang/core/number.h"
#include "src/tint/lang/core/type/builtin_structs.h"
#include "src/tint/lang/core/type/depth_multisampled_texture.h"
#include "src/tint/lang/core/type/depth_texture.h"
#include "src/tint/lang/core/type/multisampled_texture.h"
#include "src/tint/lang/core/type/sampled_texture.h"
#include "src/tint/lang/core/type/sampler.h"
#include "src/tint/lang/core/type/sampler_kind.h"
#include "src/tint/lang/core/type/storage_texture.h"
#include "src/tint/lang/core/type/texture_dimension.h"
#include "src/tint/lang/glsl/writer/helper_test.h"
#include "gtest/gtest.h"
using namespace tint::core::fluent_types; // NOLINT
using namespace tint::core::number_suffixes; // NOLINT
namespace tint::glsl::writer {
namespace {
TEST_F(GlslWriterTest, BuiltinGeneric) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute);
func->SetWorkgroupSize(1, 1, 1);
b.Append(func->Block(), [&] {
auto* x = b.Let("x", 1_i);
auto* c = b.Call(ty.i32(), core::BuiltinFn::kAbs, x);
b.Let("w", c);
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
int x = 1;
int w = abs(x);
}
)");
}
TEST_F(GlslWriterTest, BuiltinSelectScalar) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* x = b.Let("x", 1_i);
auto* y = b.Let("y", 2_i);
auto* c = b.Call(ty.i32(), core::BuiltinFn::kSelect, x, y, true);
b.Let("w", c);
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
void main() {
int x = 1;
int y = 2;
int w = ((true) ? (y) : (x));
}
)");
}
TEST_F(GlslWriterTest, BuiltinSelectVector) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* x = b.Let("x", b.Construct<vec2<i32>>(1_i, 2_i));
auto* y = b.Let("y", b.Construct<vec2<i32>>(3_i, 4_i));
auto* cmp = b.Construct<vec2<bool>>(true, false);
auto* c = b.Call(ty.vec2<i32>(), core::BuiltinFn::kSelect, x, y, cmp);
b.Let("w", c);
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
void main() {
ivec2 x = ivec2(1, 2);
ivec2 y = ivec2(3, 4);
bvec2 v = bvec2(true, false);
int v_1 = ((v.x) ? (y.x) : (x.x));
ivec2 w = ivec2(v_1, ((v.y) ? (y.y) : (x.y)));
}
)");
}
TEST_F(GlslWriterTest, BuiltinStorageBarrier) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute);
func->SetWorkgroupSize(1, 1, 1);
b.Append(func->Block(), [&] {
b.Call(ty.void_(), core::BuiltinFn::kStorageBarrier);
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
barrier();
memoryBarrierBuffer();
}
)");
}
TEST_F(GlslWriterTest, BuiltinTextureBarrier) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute);
func->SetWorkgroupSize(1, 1, 1);
b.Append(func->Block(), [&] {
b.Call(ty.void_(), core::BuiltinFn::kTextureBarrier);
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
barrier();
memoryBarrierImage();
}
)");
}
TEST_F(GlslWriterTest, BuiltinWorkgroupBarrier) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute);
func->SetWorkgroupSize(1, 1, 1);
b.Append(func->Block(), [&] {
b.Call(ty.void_(), core::BuiltinFn::kWorkgroupBarrier);
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
barrier();
}
)");
}
TEST_F(GlslWriterTest, BuiltinStorageAtomicCompareExchangeWeak) {
auto* sb = ty.Struct(mod.symbols.New("SB"), {
{mod.symbols.New("padding"), ty.vec4<f32>()},
{mod.symbols.New("a"), ty.atomic<i32>()},
{mod.symbols.New("b"), ty.atomic<u32>()},
});
auto* var = b.Var("v", storage, sb, core::Access::kReadWrite);
var->SetBindingPoint(0, 0);
b.ir.root_block->Append(var);
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
b.Let("x",
b.Call(core::type::CreateAtomicCompareExchangeResult(ty, mod.symbols, ty.i32()),
core::BuiltinFn::kAtomicCompareExchangeWeak,
b.Access(ty.ptr<storage, atomic<i32>, read_write>(), var, 1_u), 123_i, 345_i));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
struct SB {
vec4 padding;
int a;
uint b;
};
struct atomic_compare_exchange_result_i32 {
int old_value;
bool exchanged;
};
layout(binding = 0, std430)
buffer tint_symbol_1_1_ssbo {
SB tint_symbol;
} v_1;
void main() {
int v_2 = atomicCompSwap(v_1.tint_symbol.a, 123, 345);
atomic_compare_exchange_result_i32 x = atomic_compare_exchange_result_i32(v_2, (v_2 == 123));
}
)");
}
TEST_F(GlslWriterTest, BuiltinStorageAtomicCompareExchangeWeakDirect) {
auto* var = b.Var("v", storage, ty.atomic<i32>(), core::Access::kReadWrite);
var->SetBindingPoint(0, 0);
b.ir.root_block->Append(var);
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
b.Let("x", b.Call(core::type::CreateAtomicCompareExchangeResult(ty, mod.symbols, ty.i32()),
core::BuiltinFn::kAtomicCompareExchangeWeak, var, 123_i, 345_i));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
struct atomic_compare_exchange_result_i32 {
int old_value;
bool exchanged;
};
layout(binding = 0, std430)
buffer tint_symbol_1_1_ssbo {
int tint_symbol;
} v_1;
void main() {
int v_2 = atomicCompSwap(v_1.tint_symbol, 123, 345);
atomic_compare_exchange_result_i32 x = atomic_compare_exchange_result_i32(v_2, (v_2 == 123));
}
)");
}
TEST_F(GlslWriterTest, BuiltinWorkgroupAtomicLoad) {
auto* sb = ty.Struct(mod.symbols.New("SB"), {
{mod.symbols.New("padding"), ty.vec4<f32>()},
{mod.symbols.New("a"), ty.atomic<i32>()},
{mod.symbols.New("b"), ty.atomic<u32>()},
});
auto* var = b.Var("v", workgroup, sb, core::Access::kReadWrite);
b.ir.root_block->Append(var);
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute);
func->SetWorkgroupSize(1, 1, 1);
b.Append(func->Block(), [&] {
b.Let("x", b.Call(ty.i32(), core::BuiltinFn::kAtomicLoad,
b.Access(ty.ptr<workgroup, atomic<i32>, read_write>(), var, 1_u)));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(
struct SB {
vec4 padding;
int a;
uint b;
};
shared SB v;
void foo_inner(uint tint_local_index) {
if ((tint_local_index == 0u)) {
v.padding = vec4(0.0f);
atomicExchange(v.a, 0);
atomicExchange(v.b, 0u);
}
barrier();
int x = atomicOr(v.a, 0);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
foo_inner(gl_LocalInvocationIndex);
}
)");
}
TEST_F(GlslWriterTest, BuiltinWorkgroupAtomicSub) {
auto* sb = ty.Struct(mod.symbols.New("SB"), {
{mod.symbols.New("padding"), ty.vec4<f32>()},
{mod.symbols.New("a"), ty.atomic<i32>()},
{mod.symbols.New("b"), ty.atomic<u32>()},
});
auto* var = b.Var("v", workgroup, sb, core::Access::kReadWrite);
b.ir.root_block->Append(var);
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute);
func->SetWorkgroupSize(1, 1, 1);
b.Append(func->Block(), [&] {
b.Let("x", b.Call(ty.i32(), core::BuiltinFn::kAtomicSub,
b.Access(ty.ptr<workgroup, atomic<i32>, read_write>(), var, 1_u), 123_i));
b.Let("y", b.Call(ty.u32(), core::BuiltinFn::kAtomicSub,
b.Access(ty.ptr<workgroup, atomic<u32>, read_write>(), var, 2_u), 123_u));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(
struct SB {
vec4 padding;
int a;
uint b;
};
shared SB v;
void foo_inner(uint tint_local_index) {
if ((tint_local_index == 0u)) {
v.padding = vec4(0.0f);
atomicExchange(v.a, 0);
atomicExchange(v.b, 0u);
}
barrier();
int x = atomicAdd(v.a, -(123));
uint y = atomicAdd(v.b, -(123u));
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
foo_inner(gl_LocalInvocationIndex);
}
)");
}
TEST_F(GlslWriterTest, BuiltinWorkgroupAtomicCompareExchangeWeak) {
auto* sb = ty.Struct(mod.symbols.New("SB"), {
{mod.symbols.New("padding"), ty.vec4<f32>()},
{mod.symbols.New("a"), ty.atomic<i32>()},
{mod.symbols.New("b"), ty.atomic<u32>()},
});
auto* var = b.Var("v", workgroup, sb, core::Access::kReadWrite);
b.ir.root_block->Append(var);
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute);
func->SetWorkgroupSize(1, 1, 1);
b.Append(func->Block(), [&] {
b.Let("x", b.Call(core::type::CreateAtomicCompareExchangeResult(ty, mod.symbols, ty.i32()),
core::BuiltinFn::kAtomicCompareExchangeWeak,
b.Access(ty.ptr<workgroup, atomic<i32>, read_write>(), var, 1_u), 123_i,
345_i));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(
struct SB {
vec4 padding;
int a;
uint b;
};
struct atomic_compare_exchange_result_i32 {
int old_value;
bool exchanged;
};
shared SB v;
void foo_inner(uint tint_local_index) {
if ((tint_local_index == 0u)) {
v.padding = vec4(0.0f);
atomicExchange(v.a, 0);
atomicExchange(v.b, 0u);
}
barrier();
int v_1 = atomicCompSwap(v.a, 123, 345);
atomic_compare_exchange_result_i32 x = atomic_compare_exchange_result_i32(v_1, (v_1 == 123));
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
foo_inner(gl_LocalInvocationIndex);
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_FloatToFloat) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", 1_f);
b.Let("x", b.Bitcast<f32>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
void main() {
float a = 1.0f;
float x = a;
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_IntToFloat) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", 1_i);
b.Let("x", b.Bitcast<f32>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
void main() {
int a = 1;
float x = intBitsToFloat(a);
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_UintToFloat) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", 1_u);
b.Let("x", b.Bitcast<f32>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
void main() {
uint a = 1u;
float x = uintBitsToFloat(a);
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_Vec3UintToVec3Float) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", b.Splat<vec3<u32>>(1_u));
b.Let("x", b.Bitcast<vec3<f32>>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
void main() {
uvec3 a = uvec3(1u);
vec3 x = uintBitsToFloat(a);
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_FloatToInt) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", 1_f);
b.Let("x", b.Bitcast<i32>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
void main() {
float a = 1.0f;
int x = floatBitsToInt(a);
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_FloatToUint) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", 1_f);
b.Let("x", b.Bitcast<u32>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
void main() {
float a = 1.0f;
uint x = floatBitsToUint(a);
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_UintToInt) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", 1_u);
b.Let("x", b.Bitcast<i32>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
void main() {
uint a = 1u;
int x = int(a);
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_IntToUint) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", 1_i);
b.Let("x", b.Bitcast<u32>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
void main() {
int a = 1;
uint x = uint(a);
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_I32ToVec2F16) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", 1_i);
b.Let("x", b.Bitcast<vec2<f16>>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require
precision highp float;
precision highp int;
f16vec2 tint_bitcast_to_f16(int src) {
return unpackFloat2x16(uint(src));
}
void main() {
int a = 1;
f16vec2 x = tint_bitcast_to_f16(a);
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_Vec2F16ToI32) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", b.Construct<vec2<f16>>(1_h, 2_h));
b.Let("x", b.Bitcast<i32>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require
precision highp float;
precision highp int;
int tint_bitcast_from_f16(f16vec2 src) {
return int(packFloat2x16(src));
}
void main() {
f16vec2 a = f16vec2(1.0hf, 2.0hf);
int x = tint_bitcast_from_f16(a);
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_U32ToVec2F16) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", 1_u);
b.Let("x", b.Bitcast<vec2<f16>>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require
precision highp float;
precision highp int;
f16vec2 tint_bitcast_to_f16(uint src) {
return unpackFloat2x16(uint(src));
}
void main() {
uint a = 1u;
f16vec2 x = tint_bitcast_to_f16(a);
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_Vec2F16ToU32) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", b.Construct<vec2<f16>>(1_h, 2_h));
b.Let("x", b.Bitcast<u32>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require
precision highp float;
precision highp int;
uint tint_bitcast_from_f16(f16vec2 src) {
return uint(packFloat2x16(src));
}
void main() {
f16vec2 a = f16vec2(1.0hf, 2.0hf);
uint x = tint_bitcast_from_f16(a);
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_F32ToVec2F16) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", 1_f);
b.Let("x", b.Bitcast<vec2<f16>>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require
precision highp float;
precision highp int;
f16vec2 tint_bitcast_to_f16(float src) {
return unpackFloat2x16(floatBitsToUint(src));
}
void main() {
float a = 1.0f;
f16vec2 x = tint_bitcast_to_f16(a);
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_Vec2F16ToF32) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", b.Construct<vec2<f16>>(1_h, 2_h));
b.Let("x", b.Bitcast<f32>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require
precision highp float;
precision highp int;
float tint_bitcast_from_f16(f16vec2 src) {
return uintBitsToFloat(packFloat2x16(src));
}
void main() {
f16vec2 a = f16vec2(1.0hf, 2.0hf);
float x = tint_bitcast_from_f16(a);
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_Vec2I32ToVec4F16) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", b.Construct<vec2<i32>>(1_i, 2_i));
b.Let("x", b.Bitcast<vec4<f16>>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require
precision highp float;
precision highp int;
f16vec4 tint_bitcast_to_f16(ivec2 src) {
uvec2 v = uvec2(src);
f16vec2 v_1 = unpackFloat2x16(v.x);
return f16vec4(v_1, unpackFloat2x16(v.y));
}
void main() {
ivec2 a = ivec2(1, 2);
f16vec4 x = tint_bitcast_to_f16(a);
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_Vec4F16ToVec2I32) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", b.Construct<vec4<f16>>(1_h, 2_h, 3_h, 4_h));
b.Let("x", b.Bitcast<vec2<i32>>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require
precision highp float;
precision highp int;
ivec2 tint_bitcast_from_f16(f16vec4 src) {
uint v = packFloat2x16(src.xy);
return ivec2(uvec2(v, packFloat2x16(src.zw)));
}
void main() {
f16vec4 a = f16vec4(1.0hf, 2.0hf, 3.0hf, 4.0hf);
ivec2 x = tint_bitcast_from_f16(a);
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_Vec2U32ToVec4F16) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", b.Construct<vec2<u32>>(1_u, 2_u));
b.Let("x", b.Bitcast<vec4<f16>>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require
precision highp float;
precision highp int;
f16vec4 tint_bitcast_to_f16(uvec2 src) {
uvec2 v = uvec2(src);
f16vec2 v_1 = unpackFloat2x16(v.x);
return f16vec4(v_1, unpackFloat2x16(v.y));
}
void main() {
uvec2 a = uvec2(1u, 2u);
f16vec4 x = tint_bitcast_to_f16(a);
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_Vec4F16ToVec2U32) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", b.Construct<vec4<f16>>(1_h, 2_h, 3_h, 4_h));
b.Let("x", b.Bitcast<vec2<u32>>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require
precision highp float;
precision highp int;
uvec2 tint_bitcast_from_f16(f16vec4 src) {
uint v = packFloat2x16(src.xy);
return uvec2(uvec2(v, packFloat2x16(src.zw)));
}
void main() {
f16vec4 a = f16vec4(1.0hf, 2.0hf, 3.0hf, 4.0hf);
uvec2 x = tint_bitcast_from_f16(a);
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_Vec2F32ToVec4F16) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", b.Construct<vec2<f32>>(1_f, 2_f));
b.Let("x", b.Bitcast<vec4<f16>>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require
precision highp float;
precision highp int;
f16vec4 tint_bitcast_to_f16(vec2 src) {
uvec2 v = floatBitsToUint(src);
f16vec2 v_1 = unpackFloat2x16(v.x);
return f16vec4(v_1, unpackFloat2x16(v.y));
}
void main() {
vec2 a = vec2(1.0f, 2.0f);
f16vec4 x = tint_bitcast_to_f16(a);
}
)");
}
TEST_F(GlslWriterTest, BuiltinBitcast_Vec4F16ToVec2F32) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* a = b.Let("a", b.Construct<vec4<f16>>(1_h, 2_h, 3_h, 4_h));
b.Let("x", b.Bitcast<vec2<f32>>(a));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require
precision highp float;
precision highp int;
vec2 tint_bitcast_from_f16(f16vec4 src) {
uint v = packFloat2x16(src.xy);
return uintBitsToFloat(uvec2(v, packFloat2x16(src.zw)));
}
void main() {
f16vec4 a = f16vec4(1.0hf, 2.0hf, 3.0hf, 4.0hf);
vec2 x = tint_bitcast_from_f16(a);
}
)");
}
TEST_F(GlslWriterTest, BuiltinTextureDimensions_1d) {
auto* t = b.FunctionParam(
"t", ty.Get<core::type::SampledTexture>(core::type::TextureDimension::k1d, ty.f32()));
auto* func = b.Function("foo", ty.u32());
func->SetParams({t});
b.Append(func->Block(), [&] {
auto* result = b.Call<u32>(core::BuiltinFn::kTextureDimensions, t);
b.Return(func, result);
});
Options opts{};
opts.version = Version(Version::Standard::kDesktop, 4, 6);
ASSERT_TRUE(Generate(opts)) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, R"(#version 460
uint foo(highp sampler1D t) {
return uint(textureSize(t, 0));
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
}
)");
}
TEST_F(GlslWriterTest, BuiltinTextureDimensions_2d_WithoutLod) {
auto* t = b.FunctionParam(
"t", ty.Get<core::type::SampledTexture>(core::type::TextureDimension::k2d, ty.f32()));
auto* func = b.Function("foo", ty.vec2<u32>());
func->SetParams({t});
b.Append(func->Block(), [&] {
auto* result = b.Call<vec2<u32>>(core::BuiltinFn::kTextureDimensions, t);
b.Return(func, result);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(
uvec2 foo(highp sampler2D t) {
return uvec2(textureSize(t, 0));
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
}
)");
}
// TODO(dsinclair): Requires textureNumLevels
TEST_F(GlslWriterTest, DISABLED_BuiltinTextureDimensions_2d_WithU32Lod) {
auto* t = b.FunctionParam(
"t", ty.Get<core::type::SampledTexture>(core::type::TextureDimension::k2d, ty.f32()));
auto* func = b.Function("foo", ty.vec2<u32>());
func->SetParams({t});
b.Append(func->Block(), [&] {
auto* result = b.Call<vec2<u32>>(core::BuiltinFn::kTextureDimensions, t, 3_u);
b.Return(func, result);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(
)");
}
TEST_F(GlslWriterTest, BuiltinTextureDimensions_2dArray) {
auto* t = b.FunctionParam(
"t", ty.Get<core::type::SampledTexture>(core::type::TextureDimension::k2dArray, ty.f32()));
auto* func = b.Function("foo", ty.vec2<u32>());
func->SetParams({t});
b.Append(func->Block(), [&] {
auto* result = b.Call<vec2<u32>>(core::BuiltinFn::kTextureDimensions, t);
b.Return(func, result);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(
uvec2 foo(highp sampler2DArray t) {
return uvec2(textureSize(t, 0).xy);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
}
)");
}
TEST_F(GlslWriterTest, BuiltinTextureDimensions_Storage1D) {
auto* t = b.FunctionParam(
"t",
ty.Get<core::type::StorageTexture>(
core::type::TextureDimension::k2d, core::TexelFormat::kRg32Float, core::Access::kRead,
core::type::StorageTexture::SubtypeFor(core::TexelFormat::kRg32Float, ty)));
auto* func = b.Function("foo", ty.vec2<u32>());
func->SetParams({t});
b.Append(func->Block(), [&] {
auto* result = b.Call<vec2<u32>>(core::BuiltinFn::kTextureDimensions, t);
b.Return(func, result);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(
uvec2 foo(highp readonly image2D t) {
return uvec2(imageSize(t));
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
}
)");
}
TEST_F(GlslWriterTest, BuiltinTextureDimensions_DepthMultisampled) {
auto* t = b.FunctionParam(
"t", ty.Get<core::type::DepthMultisampledTexture>(core::type::TextureDimension::k2d));
auto* func = b.Function("foo", ty.vec2<u32>());
func->SetParams({t});
b.Append(func->Block(), [&] {
auto* result = b.Call<vec2<u32>>(core::BuiltinFn::kTextureDimensions, t);
b.Return(func, result);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(
uvec2 foo(highp sampler2DMS t) {
return uvec2(textureSize(t));
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
}
)");
}
TEST_F(GlslWriterTest, CountOneBits) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
b.Let("x", b.Call(ty.u32(), core::BuiltinFn::kCountOneBits, 1_u));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
void main() {
uint x = uint(bitCount(1u));
}
)");
}
TEST_F(GlslWriterTest, ExtractBits) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
b.Let("x", b.Call(ty.u32(), core::BuiltinFn::kExtractBits, 1_u, 2_u, 3_u));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
void main() {
uint v = min(2u, 32u);
uint v_1 = min(3u, (32u - v));
int v_2 = int(v);
uint x = bitfieldExtract(1u, v_2, int(v_1));
}
)");
}
TEST_F(GlslWriterTest, InsertBits) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
b.Let("x", b.Call(ty.u32(), core::BuiltinFn::kInsertBits, 1_u, 2_u, 3_u, 4_u));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
void main() {
uint v = min(3u, 32u);
uint v_1 = min(4u, (32u - v));
int v_2 = int(v);
uint x = bitfieldInsert(1u, 2u, v_2, int(v_1));
}
)");
}
TEST_F(GlslWriterTest, TextureNumLayers_2DArray) {
auto* var =
b.Var("v", handle,
ty.Get<core::type::SampledTexture>(core::type::TextureDimension::k2dArray, ty.f32()),
core::Access::kReadWrite);
var->SetBindingPoint(0, 0);
b.ir.root_block->Append(var);
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
b.Let("x", b.Call(ty.u32(), core::BuiltinFn::kTextureNumLayers, b.Load(var)));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
uniform highp sampler2DArray v;
void main() {
uint x = uint(textureSize(v, 0).z);
}
)");
}
TEST_F(GlslWriterTest, TextureNumLayers_Depth2DArray) {
auto* var =
b.Var("v", handle, ty.Get<core::type::DepthTexture>(core::type::TextureDimension::k2dArray),
core::Access::kReadWrite);
var->SetBindingPoint(0, 0);
b.ir.root_block->Append(var);
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
b.Let("x", b.Call(ty.u32(), core::BuiltinFn::kTextureNumLayers, b.Load(var)));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
uniform highp sampler2DArrayShadow v;
void main() {
uint x = uint(textureSize(v, 0).z);
}
)");
}
TEST_F(GlslWriterTest, TextureNumLayers_CubeArray) {
auto* var = b.Var(
"v", handle,
ty.Get<core::type::SampledTexture>(core::type::TextureDimension::kCubeArray, ty.f32()),
core::Access::kReadWrite);
var->SetBindingPoint(0, 0);
b.ir.root_block->Append(var);
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
b.Let("x", b.Call(ty.u32(), core::BuiltinFn::kTextureNumLayers, b.Load(var)));
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
precision highp float;
precision highp int;
uniform highp samplerCubeArray v;
void main() {
uint x = uint(textureSize(v, 0).z);
}
)");
}
TEST_F(GlslWriterTest, TextureNumLayers_DepthCubeArray) {
auto* var = b.Var("v", handle,
ty.Get<core::type::DepthTexture>(core::type::TextureDimension::kCubeArray),
core::Access::kReadWrite);
var->SetBindingPoint(0, 0);
b.ir.root_block->Append(var);
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
b.Let("x", b.Call(ty.u32(), core::BuiltinFn::kTextureNumLayers, b.Load(var)));
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
precision highp float;
precision highp int;
uniform highp samplerCubeArrayShadow v;
void main() {
uint x = uint(textureSize(v, 0).z);
}
)");
}
TEST_F(GlslWriterTest, TextureNumLayers_Storage2DArray) {
auto* storage_ty = ty.Get<core::type::StorageTexture>(
core::type::TextureDimension::k2dArray, core::TexelFormat::kRg32Float, core::Access::kRead,
core::type::StorageTexture::SubtypeFor(core::TexelFormat::kRg32Float, ty));
auto* var = b.Var("v", handle, storage_ty, core::Access::kReadWrite);
var->SetBindingPoint(0, 0);
b.ir.root_block->Append(var);
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
b.Let("x", b.Call(ty.u32(), core::BuiltinFn::kTextureNumLayers, b.Load(var)));
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
precision highp float;
precision highp int;
layout(binding = 0, rg32f) uniform highp readonly image2DArray v;
void main() {
uint x = uint(imageSize(v).z);
}
)");
}
TEST_F(GlslWriterTest, BuiltinTextureLoad_1DF32) {
auto* t = b.Var(ty.ptr(
handle, ty.Get<core::type::SampledTexture>(core::type::TextureDimension::k1d, ty.f32())));
t->SetBindingPoint(0, 0);
b.ir.root_block->Append(t);
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* coords = b.Value(1_u);
auto* level = b.Value(3_u);
b.Let("x", b.Call<vec4<f32>>(core::BuiltinFn::kTextureLoad, b.Load(t), coords, level));
b.Return(func);
});
Options opts;
opts.disable_robustness = true;
opts.version = Version(Version::Standard::kDesktop, 4, 6);
ASSERT_TRUE(Generate(opts)) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, R"(#version 460
precision highp float;
precision highp int;
uniform highp sampler1D v;
void main() {
int v_1 = int(1u);
vec4 x = texelFetch(v, v_1, int(3u));
}
)");
}
TEST_F(GlslWriterTest, BuiltinTextureLoad_2DLevelI32) {
auto* t = b.Var(ty.ptr(
handle, ty.Get<core::type::SampledTexture>(core::type::TextureDimension::k2d, ty.i32())));
t->SetBindingPoint(0, 0);
b.ir.root_block->Append(t);
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* coords = b.Composite(ty.vec2<u32>(), 1_u, 2_u);
auto* level = b.Value(3_u);
b.Let("x", b.Call<vec4<i32>>(core::BuiltinFn::kTextureLoad, b.Load(t), coords, level));
b.Return(func);
});
Options opts;
opts.disable_robustness = true;
ASSERT_TRUE(Generate(opts)) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
uniform highp isampler2D v;
void main() {
ivec2 v_1 = ivec2(uvec2(1u, 2u));
ivec4 x = texelFetch(v, v_1, int(3u));
}
)");
}
TEST_F(GlslWriterTest, BuiltinTextureLoad_3DLevelU32) {
auto* t = b.Var(ty.ptr(
handle, ty.Get<core::type::SampledTexture>(core::type::TextureDimension::k3d, ty.f32())));
t->SetBindingPoint(0, 0);
b.ir.root_block->Append(t);
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* coords = b.Composite(ty.vec3<i32>(), 1_i, 2_i, 3_i);
auto* level = b.Value(4_u);
b.Let("x", b.Call<vec4<f32>>(core::BuiltinFn::kTextureLoad, b.Load(t), coords, level));
b.Return(func);
});
Options opts;
opts.disable_robustness = true;
ASSERT_TRUE(Generate(opts)) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
uniform highp sampler3D v;
void main() {
ivec3 v_1 = ivec3(ivec3(1, 2, 3));
vec4 x = texelFetch(v, v_1, int(4u));
}
)");
}
TEST_F(GlslWriterTest, BuiltinTextureLoad_Multisampled2DI32) {
auto* t = b.Var(ty.ptr(handle, ty.Get<core::type::MultisampledTexture>(
core::type::TextureDimension::k2d, ty.i32())));
t->SetBindingPoint(0, 0);
b.ir.root_block->Append(t);
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* coords = b.Composite(ty.vec2<i32>(), 1_i, 2_i);
auto* sample_idx = b.Value(3_i);
b.Let("x", b.Call<vec4<i32>>(core::BuiltinFn::kTextureLoad, b.Load(t), coords, sample_idx));
b.Return(func);
});
Options opts;
opts.disable_robustness = true;
ASSERT_TRUE(Generate(opts)) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
uniform highp isampler2DMS v;
void main() {
ivec2 v_1 = ivec2(ivec2(1, 2));
ivec4 x = texelFetch(v, v_1, int(3));
}
)");
}
TEST_F(GlslWriterTest, BuiltinTextureLoad_Storage2D) {
auto* t = b.Var(ty.ptr(
handle,
ty.Get<core::type::StorageTexture>(
core::type::TextureDimension::k2d, core::TexelFormat::kRg32Float, core::Access::kRead,
core::type::StorageTexture::SubtypeFor(core::TexelFormat::kRg32Float, ty))));
t->SetBindingPoint(0, 0);
b.ir.root_block->Append(t);
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* coords = b.Construct(ty.vec2<i32>(), b.Value(1_i), b.Value(2_i));
b.Let("x", b.Call<vec4<f32>>(core::BuiltinFn::kTextureLoad, b.Load(t), coords));
b.Return(func);
});
Options opts;
opts.disable_robustness = true;
opts.version = Version(Version::Standard::kDesktop, 4, 6);
ASSERT_TRUE(Generate(opts)) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, R"(#version 460
precision highp float;
precision highp int;
layout(binding = 0, rg32f) uniform highp readonly image2D v;
void main() {
vec4 x = imageLoad(v, ivec2(ivec2(1, 2)));
}
)");
}
TEST_F(GlslWriterTest, BuiltinTextureStore1D) {
auto* t = b.Var(ty.ptr(
handle,
ty.Get<core::type::StorageTexture>(
core::type::TextureDimension::k1d, core::TexelFormat::kR32Float, core::Access::kWrite,
core::type::StorageTexture::SubtypeFor(core::TexelFormat::kR32Float, ty))));
t->SetBindingPoint(0, 0);
b.ir.root_block->Append(t);
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* coords = b.Value(1_i);
auto* value = b.Composite(ty.vec4<f32>(), .5_f, 0_f, 0_f, 1_f);
b.Call(ty.void_(), core::BuiltinFn::kTextureStore, b.Load(t), coords, value);
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
precision highp float;
precision highp int;
layout(binding = 0, r32f) uniform highp writeonly image1D v;
void main() {
imageStore(v, 1, vec4(0.5f, 0.0f, 0.0f, 1.0f));
}
)");
}
TEST_F(GlslWriterTest, BuiltinTextureStore3D) {
auto* t = b.Var(ty.ptr(
handle,
ty.Get<core::type::StorageTexture>(
core::type::TextureDimension::k3d, core::TexelFormat::kR32Float, core::Access::kWrite,
core::type::StorageTexture::SubtypeFor(core::TexelFormat::kR32Float, ty))));
t->SetBindingPoint(0, 0);
b.ir.root_block->Append(t);
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* coords = b.Composite(ty.vec3<i32>(), 1_i, 2_i, 3_i);
auto* value = b.Composite(ty.vec4<f32>(), .5_f, 0_f, 0_f, 1_f);
b.Call(ty.void_(), core::BuiltinFn::kTextureStore, b.Load(t), coords, value);
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
layout(binding = 0, r32f) uniform highp writeonly image3D v;
void main() {
imageStore(v, ivec3(1, 2, 3), vec4(0.5f, 0.0f, 0.0f, 1.0f));
}
)");
}
TEST_F(GlslWriterTest, BuiltinTextureStoreArray) {
auto* t = b.Var(ty.ptr(
handle, ty.Get<core::type::StorageTexture>(
core::type::TextureDimension::k2dArray, core::TexelFormat::kRgba32Float,
core::Access::kWrite,
core::type::StorageTexture::SubtypeFor(core::TexelFormat::kR32Float, ty))));
t->SetBindingPoint(0, 0);
b.ir.root_block->Append(t);
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* coords = b.Composite(ty.vec2<i32>(), 1_i, 2_i);
auto* value = b.Composite(ty.vec4<f32>(), .5_f, .4_f, .3_f, 1_f);
b.Call(ty.void_(), core::BuiltinFn::kTextureStore, b.Load(t), coords, 3_u, value);
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
layout(binding = 0, rgba32f) uniform highp writeonly image2DArray v;
void main() {
imageStore(v, ivec3(ivec2(1, 2), int(3u)), vec4(0.5f, 0.40000000596046447754f, 0.30000001192092895508f, 1.0f));
}
)");
}
TEST_F(GlslWriterTest, BuiltinFMA_f32) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* x = b.Splat(ty.vec3<f32>(), 1_f);
auto* y = b.Splat(ty.vec3<f32>(), 2_f);
auto* z = b.Splat(ty.vec3<f32>(), 3_f);
b.Let("x", b.Call(ty.vec3<f32>(), core::BuiltinFn::kFma, x, y, z));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
void main() {
vec3 x = ((vec3(1.0f) * vec3(2.0f)) + vec3(3.0f));
}
)");
}
TEST_F(GlslWriterTest, BuiltinFMA_f16) {
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* x = b.Splat(ty.vec3<f16>(), 1_h);
auto* y = b.Splat(ty.vec3<f16>(), 2_h);
auto* z = b.Splat(ty.vec3<f16>(), 3_h);
b.Let("x", b.Call(ty.vec3<f16>(), core::BuiltinFn::kFma, x, y, z));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
#extension GL_AMD_gpu_shader_half_float: require
void main() {
f16vec3 x = ((f16vec3(1.0hf) * f16vec3(2.0hf)) + f16vec3(3.0hf));
}
)");
}
TEST_F(GlslWriterTest, BuiltinArrayLength) {
auto* sb = ty.Struct(mod.symbols.New("SB"), {
{mod.symbols.New("b"), ty.array<u32>()},
});
auto* var = b.Var("v", storage, sb, core::Access::kReadWrite);
var->SetBindingPoint(0, 0);
b.ir.root_block->Append(var);
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
b.Append(func->Block(), [&] {
auto* ary = b.Access(ty.ptr<storage, array<u32>, read_write>(), var, 0_u);
b.Let("x", b.Call(ty.u32(), core::BuiltinFn::kArrayLength, ary));
b.Return(func);
});
ASSERT_TRUE(Generate()) << err_ << output_.glsl;
EXPECT_EQ(output_.glsl, GlslHeader() + R"(precision highp float;
precision highp int;
layout(binding = 0, std430)
buffer SB_1_ssbo {
uint b[];
} v;
void main() {
uint x = uint(v.b.length());
}
)");
}
} // namespace
} // namespace tint::glsl::writer