webgpu/dawn/include/tint/binding_point.h
 1// Copyright 2021 The Tint Authors.
 2//
 3// Licensed under the Apache License, Version 2.0 (the "License");
 4// you may not use this file except in compliance with the License.
 5// You may obtain a copy of the License at
 6//
 7//     http://www.apache.org/licenses/LICENSE-2.0
 8//
 9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef INCLUDE_TINT_BINDING_POINT_H_
16#define INCLUDE_TINT_BINDING_POINT_H_
17
18#include <stdint.h>
19
20#include <functional>
21
22#include "src/tint/utils/math/hash.h"
23#include "src/tint/utils/reflection/reflection.h"
24#include "src/tint/utils/text/string_stream.h"
25#include "src/tint/utils/traits/traits.h"
26
27namespace tint {
28
29/// BindingPoint holds a group and binding index.
30struct BindingPoint {
31    /// The `@group` part of the binding point
32    uint32_t group = 0;
33    /// The `@binding` part of the binding point
34    uint32_t binding = 0;
35
36    /// Reflect the fields of this class so that it can be used by tint::ForeachField()
37    TINT_REFLECT(group, binding);
38
39    /// Equality operator
40    /// @param rhs the BindingPoint to compare against
41    /// @returns true if this BindingPoint is equal to `rhs`
42    inline bool operator==(const BindingPoint& rhs) const {
43        return group == rhs.group && binding == rhs.binding;
44    }
45
46    /// Inequality operator
47    /// @param rhs the BindingPoint to compare against
48    /// @returns true if this BindingPoint is not equal to `rhs`
49    inline bool operator!=(const BindingPoint& rhs) const { return !(*this == rhs); }
50
51    /// Less-than operator
52    /// @param rhs the BindingPoint to compare against
53    /// @returns true if this BindingPoint comes before @p rhs
54    inline bool operator<(const BindingPoint& rhs) const {
55        if (group < rhs.group) {
56            return true;
57        }
58        if (group > rhs.group) {
59            return false;
60        }
61        return binding < rhs.binding;
62    }
63};
64
65/// Prints the BindingPoint @p bp to @p o
66/// @param o the stream to write to
67/// @param bp the BindingPoint
68/// @return the stream so calls can be chained
69template <typename STREAM, typename = traits::EnableIfIsOStream<STREAM>>
70auto& operator<<(STREAM& o, const BindingPoint& bp) {
71    return o << "[group: " << bp.group << ", binding: " << bp.binding << "]";
72}
73
74}  // namespace tint
75
76namespace std {
77
78/// Custom std::hash specialization for tint::BindingPoint so BindingPoints can be used as keys for
79/// std::unordered_map and std::unordered_set.
80template <>
81class hash<tint::BindingPoint> {
82  public:
83    /// @param binding_point the binding point to create a hash for
84    /// @return the hash value
85    inline std::size_t operator()(const tint::BindingPoint& binding_point) const {
86        return tint::Hash(binding_point.group, binding_point.binding);
87    }
88};
89
90}  // namespace std
91
92#endif  // INCLUDE_TINT_BINDING_POINT_H_