GLSL: emit UBO and SSBO interface blocks.

Do not emit block-decorated structs.
Combine them with the variable into a uniform or buffer interface block.

Bug: tint:1223
Change-Id: I16263ea449ecab705319dc440275b7d169021bc9
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/67243
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Stephen White <senorblanco@chromium.org>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
Stephen White 2021-10-21 21:24:14 +00:00 committed by Tint LUCI CQ
parent 1e896001e6
commit 092326894e
4 changed files with 95 additions and 104 deletions

View File

@ -130,9 +130,11 @@ bool GeneratorImpl::Generate() {
return false; return false;
} }
} else if (auto* str = decl->As<ast::Struct>()) { } else if (auto* str = decl->As<ast::Struct>()) {
if (!str->IsBlockDecorated()) {
if (!EmitStructType(current_buffer_, builder_.Sem().Get(str))) { if (!EmitStructType(current_buffer_, builder_.Sem().Get(str))) {
return false; return false;
} }
}
} else if (auto* func = decl->As<ast::Function>()) { } else if (auto* func = decl->As<ast::Function>()) {
if (func->IsEntryPoint()) { if (func->IsEntryPoint()) {
if (!EmitEntryPointFunction(func)) { if (!EmitEntryPointFunction(func)) {
@ -1544,12 +1546,19 @@ bool GeneratorImpl::EmitGlobalVariable(const ast::Variable* global) {
bool GeneratorImpl::EmitUniformVariable(const sem::Variable* var) { bool GeneratorImpl::EmitUniformVariable(const sem::Variable* var) {
auto* decl = var->Declaration(); auto* decl = var->Declaration();
auto* type = var->Type()->UnwrapRef(); auto* type = var->Type()->UnwrapRef();
auto out = line(); auto* str = type->As<sem::Struct>();
if (!EmitTypeAndName(out, type, ast::StorageClass::kUniform, var->Access(), if (!str) {
builder_.Symbols().NameFor(decl->symbol))) { TINT_ICE(Writer, builder_.Diagnostics())
<< "storage variable must be of struct type";
return false; return false;
} }
out << ";"; ast::VariableBindingPoint bp = decl->BindingPoint();
line() << "layout (set = " << bp.group->value
<< ", binding = " << bp.binding->value << ") uniform "
<< StructName(str) << " {";
EmitStructMembers(current_buffer_, str);
auto name = builder_.Symbols().NameFor(decl->symbol);
line() << "} " << name << ";";
return true; return true;
} }
@ -1557,14 +1566,19 @@ bool GeneratorImpl::EmitUniformVariable(const sem::Variable* var) {
bool GeneratorImpl::EmitStorageVariable(const sem::Variable* var) { bool GeneratorImpl::EmitStorageVariable(const sem::Variable* var) {
auto* decl = var->Declaration(); auto* decl = var->Declaration();
auto* type = var->Type()->UnwrapRef(); auto* type = var->Type()->UnwrapRef();
auto out = line(); auto* str = type->As<sem::Struct>();
if (!EmitTypeAndName(out, type, ast::StorageClass::kStorage, var->Access(), if (!str) {
builder_.Symbols().NameFor(decl->symbol))) { TINT_ICE(Writer, builder_.Diagnostics())
<< "storage variable must be of struct type";
return false; return false;
} }
ast::VariableBindingPoint bp = decl->BindingPoint();
out << ";"; line() << "layout (set = " << bp.group->value
<< ", binding = " << bp.binding->value << ") buffer "
<< StructName(str) << " {";
EmitStructMembers(current_buffer_, str);
auto name = builder_.Symbols().NameFor(decl->symbol);
line() << "} " << name << ";";
return true; return true;
} }

View File

@ -433,11 +433,10 @@ TEST_F(GlslGeneratorImplTest_Function,
EXPECT_EQ(gen.result(), R"(#version 310 es EXPECT_EQ(gen.result(), R"(#version 310 es
precision mediump float; precision mediump float;
struct UBO {
vec4 coord;
};
uniform UBO ubo; layout (set = 1, binding = 0) uniform UBO {
vec4 coord;
} ubo;
float sub_func(float param) { float sub_func(float param) {
return ubo.coord.x; return ubo.coord.x;
@ -484,11 +483,10 @@ TEST_F(GlslGeneratorImplTest_Function,
EXPECT_EQ(gen.result(), R"(#version 310 es EXPECT_EQ(gen.result(), R"(#version 310 es
precision mediump float; precision mediump float;
struct Uniforms {
vec4 coord;
};
uniform Uniforms uniforms; layout (set = 1, binding = 0) uniform Uniforms {
vec4 coord;
} uniforms;
void frag_main() { void frag_main() {
float v = uniforms.coord.x; float v = uniforms.coord.x;
@ -536,12 +534,11 @@ TEST_F(GlslGeneratorImplTest_Function,
EXPECT_EQ(gen.result(), R"(#version 310 es EXPECT_EQ(gen.result(), R"(#version 310 es
precision mediump float; precision mediump float;
struct Data {
layout (set = 1, binding = 0) buffer Data {
int a; int a;
float b; float b;
}; } coord;
Data coord;
void frag_main() { void frag_main() {
float v = coord.b; float v = coord.b;
@ -589,12 +586,11 @@ TEST_F(GlslGeneratorImplTest_Function,
R"(#version 310 es R"(#version 310 es
precision mediump float; precision mediump float;
struct Data {
layout (set = 1, binding = 0) buffer Data {
int a; int a;
float b; float b;
}; } coord;
Data coord;
void frag_main() { void frag_main() {
float v = coord.b; float v = coord.b;
@ -638,12 +634,11 @@ TEST_F(GlslGeneratorImplTest_Function,
EXPECT_EQ(gen.result(), R"(#version 310 es EXPECT_EQ(gen.result(), R"(#version 310 es
precision mediump float; precision mediump float;
struct Data {
layout (set = 1, binding = 0) buffer Data {
int a; int a;
float b; float b;
}; } coord;
Data coord;
void frag_main() { void frag_main() {
coord.b = 2.0f; coord.b = 2.0f;
@ -688,12 +683,11 @@ TEST_F(GlslGeneratorImplTest_Function,
EXPECT_EQ(gen.result(), R"(#version 310 es EXPECT_EQ(gen.result(), R"(#version 310 es
precision mediump float; precision mediump float;
struct Data {
layout (set = 1, binding = 0) buffer Data {
int a; int a;
float b; float b;
}; } coord;
Data coord;
void frag_main() { void frag_main() {
coord.b = 2.0f; coord.b = 2.0f;
@ -740,11 +734,10 @@ TEST_F(GlslGeneratorImplTest_Function,
EXPECT_EQ(gen.result(), R"(#version 310 es EXPECT_EQ(gen.result(), R"(#version 310 es
precision mediump float; precision mediump float;
struct S {
float x;
};
uniform S coord; layout (set = 1, binding = 0) uniform S {
float x;
} coord;
float sub_func(float param) { float sub_func(float param) {
return coord.x; return coord.x;
@ -797,11 +790,10 @@ TEST_F(GlslGeneratorImplTest_Function,
R"(#version 310 es R"(#version 310 es
precision mediump float; precision mediump float;
struct S {
float x;
};
S coord; layout (set = 1, binding = 0) buffer S {
float x;
} coord;
float sub_func(float param) { float sub_func(float param) {
return coord.x; return coord.x;
@ -1064,11 +1056,10 @@ TEST_F(GlslGeneratorImplTest_Function,
EXPECT_EQ(gen.result(), R"(#version 310 es EXPECT_EQ(gen.result(), R"(#version 310 es
precision mediump float; precision mediump float;
struct Data {
float d;
};
Data data; layout (set = 0, binding = 0) buffer Data {
float d;
} data;
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void a() { void a() {

View File

@ -296,12 +296,11 @@ TEST_F(GlslGeneratorImplTest_MemberAccessor, StorageBuffer_Store_Matrix_Empty) {
R"(#version 310 es R"(#version 310 es
precision mediump float; precision mediump float;
struct Data {
layout (set = 1, binding = 0) buffer Data {
int a; int a;
mat2x3 b; mat2x3 b;
}; } data;
Data data;
void tint_symbol() { void tint_symbol() {
data.b = mat2x3(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); data.b = mat2x3(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
@ -343,12 +342,11 @@ TEST_F(GlslGeneratorImplTest_MemberAccessor,
R"(#version 310 es R"(#version 310 es
precision mediump float; precision mediump float;
struct Data {
layout (set = 1, binding = 0) buffer Data {
float z; float z;
mat4x3 a; mat4x3 a;
}; } data;
Data data;
void tint_symbol() { void tint_symbol() {
float x = data.a[2][1]; float x = data.a[2][1];
@ -388,12 +386,11 @@ TEST_F(GlslGeneratorImplTest_MemberAccessor,
R"(#version 310 es R"(#version 310 es
precision mediump float; precision mediump float;
struct Data {
layout (set = 1, binding = 0) buffer Data {
float z; float z;
int a[5]; int a[5];
}; } data;
Data data;
void tint_symbol() { void tint_symbol() {
int x = data.a[2]; int x = data.a[2];
@ -434,12 +431,11 @@ TEST_F(GlslGeneratorImplTest_MemberAccessor,
R"(#version 310 es R"(#version 310 es
precision mediump float; precision mediump float;
struct Data {
layout (set = 1, binding = 0) buffer Data {
float z; float z;
int a[5]; int a[5];
}; } data;
Data data;
void tint_symbol() { void tint_symbol() {
int x = data.a[((2 + 4) - 3)]; int x = data.a[((2 + 4) - 3)];
@ -477,12 +473,11 @@ TEST_F(GlslGeneratorImplTest_MemberAccessor, StorageBuffer_Store_ToArray) {
R"(#version 310 es R"(#version 310 es
precision mediump float; precision mediump float;
struct Data {
layout (set = 1, binding = 0) buffer Data {
float z; float z;
int a[5]; int a[5];
}; } data;
Data data;
void tint_symbol() { void tint_symbol() {
data.a[2] = 2; data.a[2] = 2;
@ -535,11 +530,10 @@ struct Inner {
vec3 a; vec3 a;
vec3 b; vec3 b;
}; };
struct Data {
Inner c[4];
};
Data data; layout (set = 1, binding = 0) buffer Data {
Inner c[4];
} data;
void tint_symbol() { void tint_symbol() {
vec3 x = data.c[2].b; vec3 x = data.c[2].b;
@ -595,11 +589,10 @@ struct Inner {
vec3 a; vec3 a;
vec3 b; vec3 b;
}; };
struct Data {
Inner c[4];
};
Data data; layout (set = 1, binding = 0) buffer Data {
Inner c[4];
} data;
void tint_symbol() { void tint_symbol() {
vec2 x = data.c[2].b.xy; vec2 x = data.c[2].b.xy;
@ -655,11 +648,10 @@ struct Inner {
vec3 a; vec3 a;
vec3 b; vec3 b;
}; };
struct Data {
Inner c[4];
};
Data data; layout (set = 1, binding = 0) buffer Data {
Inner c[4];
} data;
void tint_symbol() { void tint_symbol() {
float x = data.c[2].b.g; float x = data.c[2].b.g;
@ -715,11 +707,10 @@ struct Inner {
vec3 a; vec3 a;
vec3 b; vec3 b;
}; };
struct Data {
Inner c[4];
};
Data data; layout (set = 1, binding = 0) buffer Data {
Inner c[4];
} data;
void tint_symbol() { void tint_symbol() {
float x = data.c[2].b[1]; float x = data.c[2].b[1];
@ -771,11 +762,10 @@ struct Inner {
vec3 a; vec3 a;
vec3 b; vec3 b;
}; };
struct Data {
Inner c[4];
};
Data data; layout (set = 1, binding = 0) buffer Data {
Inner c[4];
} data;
void tint_symbol() { void tint_symbol() {
data.c[2].b = vec3(1.0f, 2.0f, 3.0f); data.c[2].b = vec3(1.0f, 2.0f, 3.0f);
@ -831,11 +821,10 @@ struct Inner {
ivec3 a; ivec3 a;
vec3 b; vec3 b;
}; };
struct Data {
Inner c[4];
};
Data data; layout (set = 1, binding = 0) buffer Data {
Inner c[4];
} data;
void tint_symbol() { void tint_symbol() {
data.c[2].b.y = 1.0f; data.c[2].b.y = 1.0f;

View File

@ -51,11 +51,10 @@ TEST_F(GlslSanitizerTest, Call_ArrayLength) {
auto* expect = R"(#version 310 es auto* expect = R"(#version 310 es
precision mediump float; precision mediump float;
struct my_struct {
float a[0];
};
my_struct b; layout (set = 2, binding = 1) buffer my_struct {
float a[0];
} b;
void a_func() { void a_func() {
uint tint_symbol_1 = 0u; uint tint_symbol_1 = 0u;
@ -103,12 +102,11 @@ TEST_F(GlslSanitizerTest, Call_ArrayLength_OtherMembersInStruct) {
auto* expect = R"(#version 310 es auto* expect = R"(#version 310 es
precision mediump float; precision mediump float;
struct my_struct {
layout (set = 2, binding = 1) buffer my_struct {
float z; float z;
float a[0]; float a[0];
}; } b;
my_struct b;
void a_func() { void a_func() {
uint tint_symbol_1 = 0u; uint tint_symbol_1 = 0u;
@ -158,11 +156,10 @@ TEST_F(GlslSanitizerTest, Call_ArrayLength_ViaLets) {
auto* expect = R"(#version 310 es auto* expect = R"(#version 310 es
precision mediump float; precision mediump float;
struct my_struct {
float a[0];
};
my_struct b; layout (set = 2, binding = 1) buffer my_struct {
float a[0];
} b;
void a_func() { void a_func() {
uint tint_symbol_1 = 0u; uint tint_symbol_1 = 0u;