blob: 132be07a17b25b7884d79fa18e5aadd3961f01ab [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001//* Copyright 2020 The Dawn & Tint Authors
Shrek Shaod7304d12021-12-14 01:06:15 +00002//*
Austin Engcc2516a2023-10-17 20:57:54 +00003//* Redistribution and use in source and binary forms, with or without
4//* modification, are permitted provided that the following conditions are met:
Shrek Shaod7304d12021-12-14 01:06:15 +00005//*
Austin Engcc2516a2023-10-17 20:57:54 +00006//* 1. Redistributions of source code must retain the above copyright notice, this
7//* list of conditions and the following disclaimer.
Shrek Shaod7304d12021-12-14 01:06:15 +00008//*
Austin Engcc2516a2023-10-17 20:57:54 +00009//* 2. Redistributions in binary form must reproduce the above copyright notice,
10//* this list of conditions and the following disclaimer in the documentation
11//* and/or other materials provided with the distribution.
Corentin Wallez1bf31672020-01-15 15:39:12 +000012//*
Austin Engcc2516a2023-10-17 20:57:54 +000013//* 3. Neither the name of the copyright holder nor the names of its
14//* contributors may be used to endorse or promote products derived from
15//* this software without specific prior written permission.
Corentin Wallez1bf31672020-01-15 15:39:12 +000016//*
Austin Engcc2516a2023-10-17 20:57:54 +000017//* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18//* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19//* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20//* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21//* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22//* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23//* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24//* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25//* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26//* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Corentin Wallez1bf31672020-01-15 15:39:12 +000027//*
28//*
Shrek Shaod7304d12021-12-14 01:06:15 +000029{% include 'BSD_LICENSE' %}
Loko Kung90fdaa82024-07-24 00:59:58 +000030
Shrek Shao71a363b2022-02-09 19:42:51 +000031{% if 'dawn' in enabled_tags %}
32 #ifdef __EMSCRIPTEN__
Loko Kungc2aeab72024-07-25 20:08:34 +000033 #error "Do not include this header. Emscripten already provides headers needed for {{metadata.api}}."
34 #endif
Shrek Shao71a363b2022-02-09 19:42:51 +000035{% endif %}
Loko Kung90fdaa82024-07-24 00:59:58 +000036
fujunwei76bda372021-11-23 08:47:35 +000037#ifndef {{metadata.api.upper()}}_H_
38#define {{metadata.api.upper()}}_H_
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000039
Corentin Wallezefcc72e2024-09-26 11:51:53 +000040#define WGPU_BREAKING_CHANGE_STRING_VIEW_LABELS
Corentin Wallez03d6e0e2024-10-11 12:53:30 +000041#define WGPU_BREAKING_CHANGE_STRING_VIEW_OUTPUT_STRUCTS
Corentin Walleze4ce1292024-10-14 15:58:25 +000042#define WGPU_BREAKING_CHANGE_STRING_VIEW_CALLBACKS
Corentin Wallezefcc72e2024-09-26 11:51:53 +000043
Corentin Wallez8d45d442023-05-23 08:16:55 +000044{% set API = metadata.c_prefix %}
45#if defined({{API}}_SHARED_LIBRARY)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000046# if defined(_WIN32)
Corentin Wallez8d45d442023-05-23 08:16:55 +000047# if defined({{API}}_IMPLEMENTATION)
48# define {{API}}_EXPORT __declspec(dllexport)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000049# else
Corentin Wallez8d45d442023-05-23 08:16:55 +000050# define {{API}}_EXPORT __declspec(dllimport)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000051# endif
52# else // defined(_WIN32)
Corentin Wallez8d45d442023-05-23 08:16:55 +000053# if defined({{API}}_IMPLEMENTATION)
54# define {{API}}_EXPORT __attribute__((visibility("default")))
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000055# else
Corentin Wallez8d45d442023-05-23 08:16:55 +000056# define {{API}}_EXPORT
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000057# endif
58# endif // defined(_WIN32)
Corentin Wallez8d45d442023-05-23 08:16:55 +000059#else // defined({{API}}_SHARED_LIBRARY)
60# define {{API}}_EXPORT
61#endif // defined({{API}}_SHARED_LIBRARY)
62
63#if !defined({{API}}_OBJECT_ATTRIBUTE)
64#define {{API}}_OBJECT_ATTRIBUTE
65#endif
66#if !defined({{API}}_ENUM_ATTRIBUTE)
67#define {{API}}_ENUM_ATTRIBUTE
68#endif
69#if !defined({{API}}_STRUCTURE_ATTRIBUTE)
70#define {{API}}_STRUCTURE_ATTRIBUTE
71#endif
72#if !defined({{API}}_FUNCTION_ATTRIBUTE)
73#define {{API}}_FUNCTION_ATTRIBUTE
74#endif
75#if !defined({{API}}_NULLABLE)
76#define {{API}}_NULLABLE
77#endif
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000078
François Beaufort37e6b5c2024-09-05 20:01:49 +000079#define WGPU_BREAKING_CHANGE_DROP_DESCRIPTOR
80
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000081#include <stdint.h>
82#include <stddef.h>
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000083
Austin Eng58213ae2024-04-30 06:15:26 +000084#if defined(__cplusplus)
85# if __cplusplus >= 201103L
86# define {{API}}_MAKE_INIT_STRUCT(type, value) (type value)
87# else
88# define {{API}}_MAKE_INIT_STRUCT(type, value) value
89# endif
90#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
91# define {{API}}_MAKE_INIT_STRUCT(type, value) ((type) value)
92#else
93# define {{API}}_MAKE_INIT_STRUCT(type, value) value
94#endif
95
fujunwei4e876902021-11-25 08:44:01 +000096{% for constant in by_category["constant"] %}
Corentin Wallez8d45d442023-05-23 08:16:55 +000097 #define {{API}}_{{constant.name.SNAKE_CASE()}} {{constant.value}}
fujunwei4e876902021-11-25 08:44:01 +000098{% endfor %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000099
Austin Eng83d3e7f2024-06-28 19:48:10 +0000100typedef uint64_t {{API}}Flags;
Loko Kung440a30c2023-08-04 23:41:21 +0000101typedef uint32_t {{API}}Bool;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000102
103{% for type in by_category["object"] %}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000104 typedef struct {{as_cType(type.name)}}Impl* {{as_cType(type.name)}} {{API}}_OBJECT_ATTRIBUTE;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000105{% endfor %}
106
Brandon Jones112b7fd2023-05-17 01:52:30 +0000107// Structure forward declarations
Corentin Walleza1e1e572024-09-26 09:33:21 +0000108{% for type in by_category["structure"] %}
Brandon Jones112b7fd2023-05-17 01:52:30 +0000109 struct {{as_cType(type.name)}};
110{% endfor %}
111
Austin Eng83d3e7f2024-06-28 19:48:10 +0000112{% for type in by_category["enum"] %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000113 typedef enum {{as_cType(type.name)}} {
114 {% for value in type.values %}
115 {{as_cEnum(type.name, value.name)}} = 0x{{format(value.value, "08X")}},
116 {% endfor %}
117 {{as_cEnum(type.name, Name("force32"))}} = 0x7FFFFFFF
Corentin Wallez8d45d442023-05-23 08:16:55 +0000118 } {{as_cType(type.name)}} {{API}}_ENUM_ATTRIBUTE;
Austin Eng83d3e7f2024-06-28 19:48:10 +0000119{% endfor %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000120
Austin Eng83d3e7f2024-06-28 19:48:10 +0000121{% for type in by_category["bitmask"] %}
Austin Eng83d3e7f2024-06-28 19:48:10 +0000122 typedef {{API}}Flags {{as_cType(type.name)}};
Austin Eng83d3e7f2024-06-28 19:48:10 +0000123 {% for value in type.values %}
124 static const {{as_cType(type.name)}} {{as_cEnum(type.name, value.name)}} = 0x{{format(value.value, "016X")}};
125 {% endfor %}
Kai Ninomiya930e9182021-09-17 19:44:43 +0000126{% endfor -%}
Austin Eng83d3e7f2024-06-28 19:48:10 +0000127
Brandon Jones112b7fd2023-05-17 01:52:30 +0000128{% for type in by_category["function pointer"] %}
129 typedef {{as_cType(type.return_type.name)}} (*{{as_cType(type.name)}})(
130 {%- if type.arguments == [] -%}
131 void
132 {%- else -%}
133 {%- for arg in type.arguments -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000134 {% if not loop.first %}, {% endif %}
135 {% if arg.type.category == "structure" %}struct {% endif %}{{as_annotated_cType(arg)}}
Brandon Jones112b7fd2023-05-17 01:52:30 +0000136 {%- endfor -%}
137 {%- endif -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000138 ) {{API}}_FUNCTION_ATTRIBUTE;
Brandon Jones112b7fd2023-05-17 01:52:30 +0000139{% endfor %}
140
Loko Kung4e635252024-05-08 04:33:44 +0000141// Callback function pointers
142{% for type in by_category["callback function"] %}
143 typedef {{as_cType(type.return_type.name)}} (*{{as_cType(type.name)}})(
144 {%- for arg in type.arguments -%}
Jaswant Panchumarti6f5358202024-06-11 21:45:14 +0000145 {% if arg.type.category == "structure" %}struct {% endif %}{{as_annotated_cType(arg)}}{{", "}}
Loko Kung4e635252024-05-08 04:33:44 +0000146 {%- endfor -%}
147 void* userdata1, void* userdata2) {{API}}_FUNCTION_ATTRIBUTE;
148{% endfor %}
149
Corentin Wallez8d45d442023-05-23 08:16:55 +0000150typedef struct {{API}}ChainedStruct {
151 struct {{API}}ChainedStruct const * next;
152 {{API}}SType sType;
153} {{API}}ChainedStruct {{API}}_STRUCTURE_ATTRIBUTE;
Corentin Wallez2b24c3d2020-01-15 09:54:42 +0000154
Corentin Wallez8d45d442023-05-23 08:16:55 +0000155typedef struct {{API}}ChainedStructOut {
156 struct {{API}}ChainedStructOut * next;
157 {{API}}SType sType;
158} {{API}}ChainedStructOut {{API}}_STRUCTURE_ATTRIBUTE;
Austin Engbffc9662021-09-17 15:36:00 +0000159
Austin Eng58213ae2024-04-30 06:15:26 +0000160{% macro render_c_default_value(member) -%}
161 {%- if member.annotation in ["*", "const*"] and member.optional or member.default_value == "nullptr" -%}
162 nullptr
163 {%- elif member.type.category == "object" and member.optional -%}
164 nullptr
Loko Kung4e635252024-05-08 04:33:44 +0000165 {%- elif member.type.category == "callback function" -%}
166 nullptr
Austin Eng58213ae2024-04-30 06:15:26 +0000167 {%- elif member.type.category in ["enum", "bitmask"] and member.default_value != None -%}
168 {{as_cEnum(member.type.name, Name(member.default_value))}}
169 {%- elif member.default_value != None -%}
170 {{member.default_value}}
171 {%- elif member.type.category == "structure" and member.annotation == "value" -%}
172 {{API}}_{{member.type.name.SNAKE_CASE()}}_INIT
173 {%- else -%}
174 {{- assert(member.json_data.get("no_default", false) == false) -}}
175 {{- assert(member.default_value == None) -}}
176 {}
177 {%- endif -%}
178{% endmacro %}
Corentin Wallezd5c71302024-09-26 09:44:03 +0000179{% macro nullable_annotation(record) -%}
180 {% if record.optional and (record.type.category == "object" or record.annotation != "value") -%}
181 {{API}}_NULLABLE{{" "}}
182 {%- endif %}
183{%- endmacro %}
Austin Eng58213ae2024-04-30 06:15:26 +0000184
185#define {{API}}_COMMA ,
186
Loko Kungf2a5e572024-06-20 21:56:42 +0000187{% for type in by_category["callback info"] %}
188 typedef struct {{as_cType(type.name)}} {
189 {{API}}ChainedStruct const* nextInChain;
190 {% for member in type.members %}
191 {{as_annotated_cType(member)}};
192 {% endfor %}
193 void* userdata1;
194 void* userdata2;
195 } {{as_cType(type.name)}} {{API}}_STRUCTURE_ATTRIBUTE;
196
197 #define {{API}}_{{type.name.SNAKE_CASE()}}_INIT {{API}}_MAKE_INIT_STRUCT({{as_cType(type.name)}}, { \
198 /*.nextInChain=*/nullptr {{API}}_COMMA \
199 {% for member in type.members %}
200 /*.{{as_varName(member.name)}}=*/{{render_c_default_value(member)}} {{API}}_COMMA \
201 {% endfor %}
202 /*.userdata1=*/nullptr {{API}}_COMMA \
203 /*.userdata2=*/nullptr {{API}}_COMMA \
204 })
205
206{% endfor %}
Austin Eng622dbaf2024-07-23 00:09:52 +0000207
Corentin Walleza1e1e572024-09-26 09:33:21 +0000208{% for type in by_category["structure"] %}
Corentin Walleza45561b2022-07-14 12:58:25 +0000209 {% for root in type.chain_roots %}
210 // Can be chained in {{as_cType(root.name)}}
211 {% endfor %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000212 typedef struct {{as_cType(type.name)}} {
Austin Engbffc9662021-09-17 15:36:00 +0000213 {% set Out = "Out" if type.output else "" %}
Kai Ninomiya7d174a12021-09-21 17:36:27 +0000214 {% set const = "const " if not type.output else "" %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000215 {% if type.extensible %}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000216 {{API}}ChainedStruct{{Out}} {{const}}* nextInChain;
Corentin Wallez2b24c3d2020-01-15 09:54:42 +0000217 {% endif %}
218 {% if type.chained %}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000219 {{API}}ChainedStruct{{Out}} chain;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000220 {% endif %}
221 {% for member in type.members %}
Corentin Wallezd5c71302024-09-26 09:44:03 +0000222 {{nullable_annotation(member)}}{{as_annotated_cType(member)}};
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000223 {% endfor %}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000224 } {{as_cType(type.name)}} {{API}}_STRUCTURE_ATTRIBUTE;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000225
Austin Eng58213ae2024-04-30 06:15:26 +0000226 #define {{API}}_{{type.name.SNAKE_CASE()}}_INIT {{API}}_MAKE_INIT_STRUCT({{as_cType(type.name)}}, { \
227 {% if type.extensible %}
228 /*.nextInChain=*/nullptr {{API}}_COMMA \
229 {% endif %}
230 {% if type.chained %}
Corentin Wallez075e51a2024-09-20 08:06:07 +0000231 /*.chain=*/{/*.nextInChain*/nullptr {{API}}_COMMA /*.sType*/{{API}}SType_{{type.name.CamelCase()}}} {{API}}_COMMA \
Austin Eng58213ae2024-04-30 06:15:26 +0000232 {% endif %}
233 {% for member in type.members %}
234 /*.{{as_varName(member.name)}}=*/{{render_c_default_value(member)}} {{API}}_COMMA \
235 {% endfor %}
236 })
237
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000238{% endfor %}
Brandon Jones58a471a2021-02-08 19:48:06 +0000239{% for typeDef in by_category["typedef"] %}
240 // {{as_cType(typeDef.name)}} is deprecated.
241 // Use {{as_cType(typeDef.type.name)}} instead.
242 typedef {{as_cType(typeDef.type.name)}} {{as_cType(typeDef.name)}};
243
244{% endfor %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000245#ifdef __cplusplus
246extern "C" {
247#endif
248
Corentin Wallez8d45d442023-05-23 08:16:55 +0000249#if !defined({{API}}_SKIP_PROCS)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000250
Lokbondo Kungf2588232024-10-22 21:06:21 +0000251// TODO(374150686): Remove these Emscripten specific declarations from the
252// header once they are fully deprecated.
253#ifdef __EMSCRIPTEN__
254{{API}}_EXPORT WGPUDevice emscripten_webgpu_get_device(void);
255#endif
256
fujunwei23f71622021-12-02 07:41:21 +0000257{% for function in by_category["function"] %}
258 typedef {{as_cType(function.return_type.name)}} (*{{as_cProc(None, function.name)}})(
259 {%- for arg in function.arguments -%}
Corentin Wallezd5c71302024-09-26 09:44:03 +0000260 {% if not loop.first %}, {% endif %}
261 {{nullable_annotation(arg)}}{{as_annotated_cType(arg)}}
fujunwei23f71622021-12-02 07:41:21 +0000262 {%- endfor -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000263 ) {{API}}_FUNCTION_ATTRIBUTE;
fujunwei23f71622021-12-02 07:41:21 +0000264{% endfor %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000265
Corentin Wallezaca8c4a2019-11-22 14:02:52 +0000266{% for type in by_category["object"] if len(c_methods(type)) > 0 %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000267 // Procs of {{type.name.CamelCase()}}
Corentin Wallezaca8c4a2019-11-22 14:02:52 +0000268 {% for method in c_methods(type) %}
Austin Eng83d3e7f2024-06-28 19:48:10 +0000269 typedef {{as_cType(method.return_type.name)}} (*{{as_cProc(type.name, method.name)}})(
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000270 {{-as_cType(type.name)}} {{as_varName(type.name)}}
271 {%- for arg in method.arguments -%}
Corentin Wallezd5c71302024-09-26 09:44:03 +0000272 , {{nullable_annotation(arg)}}{{as_annotated_cType(arg)}}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000273 {%- endfor -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000274 ) {{API}}_FUNCTION_ATTRIBUTE;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000275 {% endfor %}
276
277{% endfor %}
Austin Eng643625a2023-08-09 00:59:33 +0000278
Corentin Wallez8d45d442023-05-23 08:16:55 +0000279#endif // !defined({{API}}_SKIP_PROCS)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000280
Corentin Wallez8d45d442023-05-23 08:16:55 +0000281#if !defined({{API}}_SKIP_DECLARATIONS)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000282
fujunwei23f71622021-12-02 07:41:21 +0000283{% for function in by_category["function"] %}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000284 {{API}}_EXPORT {{as_cType(function.return_type.name)}} {{as_cMethod(None, function.name)}}(
fujunwei23f71622021-12-02 07:41:21 +0000285 {%- for arg in function.arguments -%}
Kai Ninomiyabe98f272023-09-11 20:47:26 +0000286 {% if not loop.first %}, {% endif -%}
Corentin Wallezd5c71302024-09-26 09:44:03 +0000287 {{nullable_annotation(arg)}}{{as_annotated_cType(arg)}}
fujunwei23f71622021-12-02 07:41:21 +0000288 {%- endfor -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000289 ) {{API}}_FUNCTION_ATTRIBUTE;
fujunwei23f71622021-12-02 07:41:21 +0000290{% endfor %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000291
Corentin Wallezaca8c4a2019-11-22 14:02:52 +0000292{% for type in by_category["object"] if len(c_methods(type)) > 0 %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000293 // Methods of {{type.name.CamelCase()}}
Corentin Wallezaca8c4a2019-11-22 14:02:52 +0000294 {% for method in c_methods(type) %}
Austin Eng83d3e7f2024-06-28 19:48:10 +0000295 {{API}}_EXPORT {{as_cType(method.return_type.name)}} {{as_cMethod(type.name, method.name)}}(
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000296 {{-as_cType(type.name)}} {{as_varName(type.name)}}
297 {%- for arg in method.arguments -%}
Corentin Wallezd5c71302024-09-26 09:44:03 +0000298 , {{nullable_annotation(arg)}}{{as_annotated_cType(arg)}}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000299 {%- endfor -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000300 ) {{API}}_FUNCTION_ATTRIBUTE;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000301 {% endfor %}
302
303{% endfor %}
Austin Eng643625a2023-08-09 00:59:33 +0000304
Corentin Wallez8d45d442023-05-23 08:16:55 +0000305#endif // !defined({{API}}_SKIP_DECLARATIONS)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000306
307#ifdef __cplusplus
308} // extern "C"
309#endif
310
fujunwei76bda372021-11-23 08:47:35 +0000311#endif // {{metadata.api.upper()}}_H_