Skip to content

Commit

Permalink
Autogenerate Features mappings
Browse files Browse the repository at this point in the history
Makes it much easier to add new features.
A few minor changes:
 - names are changed from hyphen-case to snake_case
 - make the features set a typed bitset

Change-Id: Ia5fff4c96421878952d676c56ab42c8719b27478
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/147820
Reviewed-by: Loko Kung <[email protected]>
Auto-Submit: Austin Eng <[email protected]>
Kokoro: Kokoro <[email protected]>
Commit-Queue: Austin Eng <[email protected]>
  • Loading branch information
austinEng authored and Dawn LUCI CQ committed Aug 21, 2023
1 parent 76130f0 commit d1dcb50
Show file tree
Hide file tree
Showing 24 changed files with 262 additions and 461 deletions.
2 changes: 1 addition & 1 deletion dawn.json
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,7 @@
"feature name": {
"category": "enum",
"values": [
{"value": 0, "name": "undefined", "jsrepr": "undefined"},
{"value": 0, "name": "undefined", "jsrepr": "undefined", "valid": false},
{"value": 1, "name": "depth clip control"},
{"value": 2, "name": "depth32 float stencil8"},
{"value": 3, "name": "timestamp query"},
Expand Down
8 changes: 8 additions & 0 deletions generator/dawn_json_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,14 @@ def get_file_renders(self, args):
FileRender('dawn/native/ChainUtils.cpp',
'src/' + native_dir + '/ChainUtils_autogen.cpp',
frontend_params))
renders.append(
FileRender('dawn/native/Features.h',
'src/' + native_dir + '/Features_autogen.h',
frontend_params))
renders.append(
FileRender('dawn/native/Features.inl',
'src/' + native_dir + '/Features_autogen.inl',
frontend_params))
renders.append(
FileRender('dawn/native/api_absl_format.h',
'src/' + native_dir + '/' + api + '_absl_format_autogen.h',
Expand Down
44 changes: 44 additions & 0 deletions generator/templates/dawn/native/Features.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2023 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

{% set namespace_name = Name(metadata.native_namespace) %}
{% set DIR = namespace_name.concatcase().upper() %}
#ifndef {{DIR}}_FEATURES_AUTOGEN_H_
#define {{DIR}}_FEATURES_AUTOGEN_H_

#include "dawn/native/dawn_platform.h"
#include "dawn/native/DawnNative.h"
#include "dawn/common/ityp_array.h"

namespace dawn::native {

enum class Feature {
{% for enum in types["feature name"].values if enum.valid %}
{{as_cppEnum(enum.name)}},
{% endfor %}
InvalidEnum,
};

template<>
struct EnumCount<Feature> {
{% set counter = namespace(value = 0) %}
{% for enum in types["feature name"].values if enum.valid -%}
{% set counter.value = counter.value + 1 %}
{% endfor %}
static constexpr uint32_t value = {{counter.value}};
};

} // namespace dawn::native

#endif // {{DIR}}_FEATURES_AUTOGEN_H_
76 changes: 76 additions & 0 deletions generator/templates/dawn/native/Features.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2023 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace dawn::native {

wgpu::FeatureName ToAPI(Feature feature) {
switch (feature) {
{% for enum in types["feature name"].values if enum.valid %}
case Feature::{{as_cppEnum(enum.name)}}:
return wgpu::FeatureName::{{as_cppEnum(enum.name)}};
{% endfor %}
case Feature::InvalidEnum:
UNREACHABLE();
}
}

Feature FromAPI(wgpu::FeatureName feature) {
switch (feature) {
{% for enum in types["feature name"].values %}
case wgpu::FeatureName::{{as_cppEnum(enum.name)}}:
{% if enum.valid %}
return Feature::{{as_cppEnum(enum.name)}};
{% else %}
return Feature::InvalidEnum;
{% endif %}
{% endfor %}
default:
return Feature::InvalidEnum;
}
}

static constexpr bool FeatureInfoIsDefined(Feature feature) {
for (const auto& info : kFeatureInfo) {
if (info.feature == feature) {
return true;
}
}
return false;
}

static constexpr ityp::array<Feature, FeatureInfo, kEnumCount<Feature>> InitializeFeatureEnumAndInfoList() {
constexpr size_t kInfoCount = sizeof(kFeatureInfo) / sizeof(kFeatureInfo[0]);
ityp::array<Feature, FeatureInfo, kEnumCount<Feature>> list{};
{% for enum in types["feature name"].values if enum.valid %}
{
static_assert(FeatureInfoIsDefined(Feature::{{as_cppEnum(enum.name)}}),
"Please define feature info for {{as_cppEnum(enum.name)}} in Features.cpp");
for (size_t i = 0; i < kInfoCount; ++i) {
if (kFeatureInfo[i].feature == Feature::{{as_cppEnum(enum.name)}}) {
list[Feature::{{as_cppEnum(enum.name)}}] = {
"{{enum.name.snake_case()}}",
kFeatureInfo[i].info.description,
kFeatureInfo[i].info.url,
kFeatureInfo[i].info.featureState,
};
}
}
}
{% endfor %}
return list;
}

const ityp::array<Feature, FeatureInfo, kEnumCount<Feature>> kFeatureNameAndInfoList = InitializeFeatureEnumAndInfoList();

}
2 changes: 1 addition & 1 deletion src/dawn/common/ityp_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class array : private ::std::array<Value, Size> {
// NOLINTNEXTLINE(runtime/explicit)
constexpr array(Values&&... values) : Base{std::forward<Values>(values)...} {}

Value& operator[](Index i) {
constexpr Value& operator[](Index i) {
I index = static_cast<I>(i);
ASSERT(index >= 0 && index < I(Size));
return Base::operator[](index);
Expand Down
3 changes: 3 additions & 0 deletions src/dawn/common/ityp_bitset.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class bitset : private ::std::bitset<N> {
explicit constexpr bitset(const Base& rhs) : Base(rhs) {}

public:
const Base& AsBase() const { return static_cast<const Base&>(*this); }
Base& AsBase() { return static_cast<Base&>(*this); }

using reference = typename Base::reference;

constexpr bitset() noexcept : Base() {}
Expand Down
2 changes: 2 additions & 0 deletions src/dawn/native/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ dawn_json_generator("utils_gen") {
outputs = [
"src/dawn/native/ChainUtils_autogen.h",
"src/dawn/native/ChainUtils_autogen.cpp",
"src/dawn/native/Features_autogen.h",
"src/dawn/native/Features_autogen.inl",
"src/dawn/native/ProcTable.cpp",
"src/dawn/native/dawn_platform_autogen.h",
"src/dawn/native/wgpu_structs_autogen.h",
Expand Down
2 changes: 1 addition & 1 deletion src/dawn/native/CommandEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ MaybeError ValidateColorAttachmentRenderToSingleSampled(
DAWN_INVALID_IF(
!device->HasFeature(Feature::MSAARenderToSingleSampled),
"The color attachment %s has implicit sample count while the %s feature is not enabled.",
colorAttachment.view, FeatureEnumToAPIFeature(Feature::MSAARenderToSingleSampled));
colorAttachment.view, ToAPI(Feature::MSAARenderToSingleSampled));

DAWN_INVALID_IF(!IsValidSampleCount(msaaRenderToSingleSampledDesc->implicitSampleCount) ||
msaaRenderToSingleSampledDesc->implicitSampleCount <= 1,
Expand Down
10 changes: 4 additions & 6 deletions src/dawn/native/CommandValidation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,10 @@ MaybeError ValidateTimestampQuery(const DeviceBase* device,
Feature requiredFeature) {
DAWN_TRY(device->ValidateObject(querySet));

DAWN_INVALID_IF(!device->HasFeature(requiredFeature),
"Timestamp queries used without the %s feature enabled.",
device->GetPhysicalDevice()
->GetInstance()
->GetFeatureInfo(FeatureEnumToAPIFeature(requiredFeature))
->name);
DAWN_INVALID_IF(
!device->HasFeature(requiredFeature),
"Timestamp queries used without the %s feature enabled.",
device->GetPhysicalDevice()->GetInstance()->GetFeatureInfo(ToAPI(requiredFeature))->name);

DAWN_INVALID_IF(querySet->GetQueryType() != wgpu::QueryType::Timestamp,
"The type of %s is not %s.", querySet, wgpu::QueryType::Timestamp);
Expand Down
Loading

0 comments on commit d1dcb50

Please sign in to comment.