Adds strformat code-gen for helping auto-generate readable strings for structs.

- Adds generator infra for absl::StrFormat for bind group structs and types.
- Uses absl::ParsedFormat to avoid multiple parsing for format strings.

Bug: dawn:549
Change-Id: Ida4ca65eb85c4474c492161c8ae34f53bd692a3c
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/81944
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Loko Kung <lokokung@google.com>
This commit is contained in:
Loko Kung
2022-03-19 00:21:48 +00:00
committed by Dawn LUCI CQ
parent 39c2029063
commit 4d8352542a
6 changed files with 115 additions and 55 deletions

View File

@@ -661,11 +661,13 @@ namespace dawn::native {
}
std::string BindGroupLayoutBase::EntriesToString() const {
std::string entries = " [";
std::string entries = "[";
std::string sep = "";
const BindGroupLayoutBase::BindingMap& bindingMap = GetBindingMap();
for (const auto [bindingNumber, bindingIndex] : bindingMap) {
const BindingInfo& bindingInfo = GetBindingInfo(bindingIndex);
entries += absl::StrFormat("%s, ", bindingInfo);
entries += absl::StrFormat("%s%s", sep, bindingInfo);
sep = ", ";
}
entries += "]";
return entries;

View File

@@ -24,19 +24,19 @@ namespace dawn::native {
absl::FormatSink* s) {
switch (value) {
case BindingInfoType::Buffer:
s->Append("Buffer");
s->Append("buffer");
break;
case BindingInfoType::Sampler:
s->Append("Sampler");
s->Append("sampler");
break;
case BindingInfoType::Texture:
s->Append("Texture");
s->Append("texture");
break;
case BindingInfoType::StorageTexture:
s->Append("StorageTexture");
s->Append("storageTexture");
break;
case BindingInfoType::ExternalTexture:
s->Append("ExternalTexture");
s->Append("externalTexture");
break;
default:
UNREACHABLE();
@@ -48,44 +48,29 @@ namespace dawn::native {
const BindingInfo& value,
const absl::FormatConversionSpec& spec,
absl::FormatSink* s) {
s->Append(absl::StrFormat("{\n binding: %u\n visibility: %s\n %s: {\n",
static_cast<uint32_t>(value.binding), value.visibility,
value.bindingType));
static const auto* const fmt =
new absl::ParsedFormat<'u', 's', 's', 's'>("{ binding: %u, visibility: %s, %s: %s }");
switch (value.bindingType) {
case BindingInfoType::Buffer:
s->Append(absl::StrFormat(" type: %s\n", value.buffer.type));
if (value.buffer.hasDynamicOffset) {
s->Append(" hasDynamicOffset: true\n");
}
if (value.buffer.minBindingSize != 0) {
s->Append(
absl::StrFormat(" minBindingSize: %u\n", value.buffer.minBindingSize));
}
s->Append(absl::StrFormat(*fmt, static_cast<uint32_t>(value.binding),
value.visibility, value.bindingType, value.buffer));
break;
case BindingInfoType::Sampler:
s->Append(absl::StrFormat(" type: %s\n", value.sampler.type));
s->Append(absl::StrFormat(*fmt, static_cast<uint32_t>(value.binding),
value.visibility, value.bindingType, value.sampler));
break;
case BindingInfoType::Texture:
s->Append(absl::StrFormat(" sampleType: %s\n", value.texture.sampleType));
s->Append(absl::StrFormat(" viewDimension: %s\n", value.texture.viewDimension));
if (value.texture.multisampled) {
s->Append(" multisampled: true\n");
} else {
s->Append(" multisampled: false\n");
}
s->Append(absl::StrFormat(*fmt, static_cast<uint32_t>(value.binding),
value.visibility, value.bindingType, value.texture));
break;
case BindingInfoType::StorageTexture:
s->Append(absl::StrFormat(" access: %s\n", value.storageTexture.access));
s->Append(absl::StrFormat(" format: %s\n", value.storageTexture.format));
s->Append(
absl::StrFormat(" viewDimension: %s\n", value.storageTexture.viewDimension));
s->Append(absl::StrFormat(*fmt, static_cast<uint32_t>(value.binding),
value.visibility, value.bindingType,
value.storageTexture));
break;
case BindingInfoType::ExternalTexture:
break;
}
s->Append(" }\n}");
return {true};
}