writer/hlsl: Zero-initialize var declarations
Don't construct these with undefined values Change-Id: I6225d9be0973ebc1a8594526aa32ec6775b5e865 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/51300 Commit-Queue: Ben Clayton <bclayton@google.com> Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
bb3d963405
commit
6aa5a92a10
|
@ -2092,7 +2092,7 @@ bool GeneratorImpl::EmitLiteral(std::ostream& out, ast::Literal* lit) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GeneratorImpl::EmitZeroValue(std::ostream& out, sem::Type* type) {
|
bool GeneratorImpl::EmitZeroValue(std::ostream& out, const sem::Type* type) {
|
||||||
if (type->Is<sem::Bool>()) {
|
if (type->Is<sem::Bool>()) {
|
||||||
out << "false";
|
out << "false";
|
||||||
} else if (type->Is<sem::F32>()) {
|
} else if (type->Is<sem::F32>()) {
|
||||||
|
@ -2142,6 +2142,18 @@ bool GeneratorImpl::EmitZeroValue(std::ostream& out, sem::Type* type) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out << "}";
|
out << "}";
|
||||||
|
} else if (auto* arr = type->As<sem::Array>()) {
|
||||||
|
out << "{";
|
||||||
|
auto* elem = arr->ElemType();
|
||||||
|
for (size_t i = 0; i < arr->Count(); i++) {
|
||||||
|
if (i > 0) {
|
||||||
|
out << ", ";
|
||||||
|
}
|
||||||
|
if (!EmitZeroValue(out, elem)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out << "}";
|
||||||
} else {
|
} else {
|
||||||
diagnostics_.add_error("Invalid type for zero emission: " +
|
diagnostics_.add_error("Invalid type for zero emission: " +
|
||||||
type->type_name());
|
type->type_name());
|
||||||
|
@ -2626,6 +2638,9 @@ bool GeneratorImpl::EmitVariable(std::ostream& out,
|
||||||
bool skip_constructor) {
|
bool skip_constructor) {
|
||||||
make_indent(out);
|
make_indent(out);
|
||||||
|
|
||||||
|
auto* sem = builder_.Sem().Get(var);
|
||||||
|
auto* type = sem->Type();
|
||||||
|
|
||||||
// TODO(dsinclair): Handle variable decorations
|
// TODO(dsinclair): Handle variable decorations
|
||||||
if (!var->decorations().empty()) {
|
if (!var->decorations().empty()) {
|
||||||
diagnostics_.add_error("Variable decorations are not handled yet");
|
diagnostics_.add_error("Variable decorations are not handled yet");
|
||||||
|
@ -2633,21 +2648,25 @@ bool GeneratorImpl::EmitVariable(std::ostream& out,
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostringstream constructor_out;
|
std::ostringstream constructor_out;
|
||||||
if (!skip_constructor && var->constructor() != nullptr) {
|
if (!skip_constructor) {
|
||||||
constructor_out << " = ";
|
constructor_out << " = ";
|
||||||
|
|
||||||
|
if (var->constructor()) {
|
||||||
std::ostringstream pre;
|
std::ostringstream pre;
|
||||||
if (!EmitExpression(pre, constructor_out, var->constructor())) {
|
if (!EmitExpression(pre, constructor_out, var->constructor())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
out << pre.str();
|
out << pre.str();
|
||||||
|
} else {
|
||||||
|
if (!EmitZeroValue(constructor_out, type)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (var->is_const()) {
|
if (var->is_const()) {
|
||||||
out << "const ";
|
out << "const ";
|
||||||
}
|
}
|
||||||
auto* sem = builder_.Sem().Get(var);
|
|
||||||
auto* type = sem->Type();
|
|
||||||
if (!EmitType(out, type, sem->StorageClass(), sem->AccessControl(),
|
if (!EmitType(out, type, sem->StorageClass(), sem->AccessControl(),
|
||||||
builder_.Symbols().NameFor(var->symbol()))) {
|
builder_.Symbols().NameFor(var->symbol()))) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -315,7 +315,7 @@ class GeneratorImpl : public TextGenerator {
|
||||||
/// @param out the output stream
|
/// @param out the output stream
|
||||||
/// @param type the type to emit the value for
|
/// @param type the type to emit the value for
|
||||||
/// @returns true if the zero value was successfully emitted.
|
/// @returns true if the zero value was successfully emitted.
|
||||||
bool EmitZeroValue(std::ostream& out, sem::Type* type);
|
bool EmitZeroValue(std::ostream& out, const sem::Type* type);
|
||||||
/// Handles generating a variable
|
/// Handles generating a variable
|
||||||
/// @param out the output stream
|
/// @param out the output stream
|
||||||
/// @param var the variable to generate
|
/// @param var the variable to generate
|
||||||
|
|
|
@ -35,7 +35,7 @@ TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement) {
|
||||||
gen.increment_indent();
|
gen.increment_indent();
|
||||||
|
|
||||||
ASSERT_TRUE(gen.EmitStatement(out, stmt)) << gen.error();
|
ASSERT_TRUE(gen.EmitStatement(out, stmt)) << gen.error();
|
||||||
EXPECT_EQ(result(), " float a;\n");
|
EXPECT_EQ(result(), " float a = 0.0f;\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Const) {
|
TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Const) {
|
||||||
|
@ -61,7 +61,8 @@ TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Array) {
|
||||||
gen.increment_indent();
|
gen.increment_indent();
|
||||||
|
|
||||||
ASSERT_TRUE(gen.Generate(out)) << gen.error();
|
ASSERT_TRUE(gen.Generate(out)) << gen.error();
|
||||||
EXPECT_THAT(result(), HasSubstr(" float a[5];\n"));
|
EXPECT_THAT(result(),
|
||||||
|
HasSubstr(" float a[5] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f};\n"));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Private) {
|
TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Private) {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[numthreads(1, 1, 1)]
|
[numthreads(1, 1, 1)]
|
||||||
void main() {
|
void main() {
|
||||||
float3x3 m;
|
float3x3 m = float3x3(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
|
||||||
const float3 v = m[1];
|
const float3 v = m[1];
|
||||||
const float f = v[1];
|
const float f = v[1];
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[numthreads(1, 1, 1)]
|
[numthreads(1, 1, 1)]
|
||||||
void main() {
|
void main() {
|
||||||
float3 v;
|
float3 v = float3(0.0f, 0.0f, 0.0f);
|
||||||
const float scalar = v.y;
|
const float scalar = v.y;
|
||||||
const float2 swizzle2 = v.xz;
|
const float2 swizzle2 = v.xz;
|
||||||
const float3 swizzle3 = v.xzy;
|
const float3 swizzle3 = v.xzy;
|
||||||
|
|
Loading…
Reference in New Issue