• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //* Copyright 2017 The Dawn 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 {% if 'dawn' in enabled_tags %}
15     #include "dawn/webgpu_cpp.h"
16 {% else %}
17     #include "webgpu/webgpu_cpp.h"
18 {% endif %}
19 
20 #ifdef __GNUC__
21 // error: 'offsetof' within non-standard-layout type 'wgpu::XXX' is conditionally-supported
22 #pragma GCC diagnostic ignored "-Winvalid-offsetof"
23 #endif
24 
25 namespace wgpu {
26     {% for type in by_category["enum"] %}
27         {% set CppType = as_cppType(type.name) %}
28         {% set CType = as_cType(type.name) %}
29 
30         // {{CppType}}
31 
32         static_assert(sizeof({{CppType}}) == sizeof({{CType}}), "sizeof mismatch for {{CppType}}");
33         static_assert(alignof({{CppType}}) == alignof({{CType}}), "alignof mismatch for {{CppType}}");
34 
35         {% for value in type.values %}
36             static_assert(static_cast<uint32_t>({{CppType}}::{{as_cppEnum(value.name)}}) == {{as_cEnum(type.name, value.name)}}, "value mismatch for {{CppType}}::{{as_cppEnum(value.name)}}");
37         {% endfor %}
38     {% endfor -%}
39 
40     {% for type in by_category["bitmask"] %}
41         {% set CppType = as_cppType(type.name) %}
42         {% set CType = as_cType(type.name) + "Flags" %}
43 
44         // {{CppType}}
45 
46         static_assert(sizeof({{CppType}}) == sizeof({{CType}}), "sizeof mismatch for {{CppType}}");
47         static_assert(alignof({{CppType}}) == alignof({{CType}}), "alignof mismatch for {{CppType}}");
48 
49         {% for value in type.values %}
50             static_assert(static_cast<uint32_t>({{CppType}}::{{as_cppEnum(value.name)}}) == {{as_cEnum(type.name, value.name)}}, "value mismatch for {{CppType}}::{{as_cppEnum(value.name)}}");
51         {% endfor %}
52     {% endfor %}
53 
54     // ChainedStruct
55 
56     static_assert(sizeof(ChainedStruct) == sizeof(WGPUChainedStruct),
57             "sizeof mismatch for ChainedStruct");
58     static_assert(alignof(ChainedStruct) == alignof(WGPUChainedStruct),
59             "alignof mismatch for ChainedStruct");
60     static_assert(offsetof(ChainedStruct, nextInChain) == offsetof(WGPUChainedStruct, next),
61             "offsetof mismatch for ChainedStruct::nextInChain");
62     static_assert(offsetof(ChainedStruct, sType) == offsetof(WGPUChainedStruct, sType),
63             "offsetof mismatch for ChainedStruct::sType");
64     {% for type in by_category["structure"] %}
65         {% set CppType = as_cppType(type.name) %}
66         {% set CType = as_cType(type.name) %}
67 
68         // {{CppType}}
69 
70         static_assert(sizeof({{CppType}}) == sizeof({{CType}}), "sizeof mismatch for {{CppType}}");
71         static_assert(alignof({{CppType}}) == alignof({{CType}}), "alignof mismatch for {{CppType}}");
72 
73         {% if type.extensible %}
74             static_assert(offsetof({{CppType}}, nextInChain) == offsetof({{CType}}, nextInChain),
75                     "offsetof mismatch for {{CppType}}::nextInChain");
76         {% endif %}
77         {% for member in type.members %}
78             {% set memberName = member.name.camelCase() %}
79             static_assert(offsetof({{CppType}}, {{memberName}}) == offsetof({{CType}}, {{memberName}}),
80                     "offsetof mismatch for {{CppType}}::{{memberName}}");
81         {% endfor %}
82     {% endfor -%}
83 
84     {% for type in by_category["object"] %}
85         {% set CppType = as_cppType(type.name) %}
86         {% set CType = as_cType(type.name) %}
87 
88         // {{CppType}}
89 
90         static_assert(sizeof({{CppType}}) == sizeof({{CType}}), "sizeof mismatch for {{CppType}}");
91         static_assert(alignof({{CppType}}) == alignof({{CType}}), "alignof mismatch for {{CppType}}");
92 
93         {% macro render_cpp_method_declaration(type, method) -%}
94             {% set CppType = as_cppType(type.name) %}
95             {{as_cppType(method.return_type.name)}} {{CppType}}::{{method.name.CamelCase()}}(
96                 {%- for arg in method.arguments -%}
97                     {%- if not loop.first %}, {% endif -%}
98                     {%- if arg.type.category == "object" and arg.annotation == "value" -%}
99                         {{as_cppType(arg.type.name)}} const& {{as_varName(arg.name)}}
100                     {%- else -%}
101                         {{as_annotated_cppType(arg)}}
102                     {%- endif -%}
103                 {%- endfor -%}
104             ) const
105         {%- endmacro -%}
106 
107         {%- macro render_cpp_to_c_method_call(type, method) -%}
108             {{as_cMethod(type.name, method.name)}}(Get()
109                 {%- for arg in method.arguments -%},{{" "}}
110                     {%- if arg.annotation == "value" -%}
111                         {%- if arg.type.category == "object" -%}
112                             {{as_varName(arg.name)}}.Get()
113                         {%- elif arg.type.category == "enum" or arg.type.category == "bitmask" -%}
114                             static_cast<{{as_cType(arg.type.name)}}>({{as_varName(arg.name)}})
115                         {%- elif arg.type.category in ["function pointer", "native"] -%}
116                             {{as_varName(arg.name)}}
117                         {%- else -%}
118                             UNHANDLED
119                         {%- endif -%}
120                     {%- else -%}
121                         reinterpret_cast<{{decorate("", as_cType(arg.type.name), arg)}}>({{as_varName(arg.name)}})
122                     {%- endif -%}
123                 {%- endfor -%}
124             )
125         {%- endmacro -%}
126 
127         {% for method in type.methods -%}
128             {{render_cpp_method_declaration(type, method)}} {
129                 {% if method.return_type.name.concatcase() == "void" %}
130                     {{render_cpp_to_c_method_call(type, method)}};
131                 {% else %}
132                     auto result = {{render_cpp_to_c_method_call(type, method)}};
133                     return {{convert_cType_to_cppType(method.return_type, 'value', 'result') | indent(8)}};
134                 {% endif %}
135             }
136         {% endfor %}
137         void {{CppType}}::WGPUReference({{CType}} handle) {
138             if (handle != nullptr) {
139                 {{as_cMethod(type.name, Name("reference"))}}(handle);
140             }
141         }
142         void {{CppType}}::WGPURelease({{CType}} handle) {
143             if (handle != nullptr) {
144                 {{as_cMethod(type.name, Name("release"))}}(handle);
145             }
146         }
147     {% endfor %}
148 
149     // Instance
150 
151     Instance CreateInstance(const InstanceDescriptor* descriptor) {
152         const WGPUInstanceDescriptor* cDescriptor =
153             reinterpret_cast<const WGPUInstanceDescriptor*>(descriptor);
154         return Instance::Acquire(wgpuCreateInstance(cDescriptor));
155     }
156 
157     Proc GetProcAddress(Device const& device, const char* procName) {
158         return reinterpret_cast<Proc>(wgpuGetProcAddress(device.Get(), procName));
159     }
160 
161 }
162