blob: eef0dcb7e2f8def89162addf1c6fd9ed0a299e9b [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 Wallez8d45d442023-05-23 08:16:55 +000040{% set API = metadata.c_prefix %}
41#if defined({{API}}_SHARED_LIBRARY)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000042# if defined(_WIN32)
Corentin Wallez8d45d442023-05-23 08:16:55 +000043# if defined({{API}}_IMPLEMENTATION)
44# define {{API}}_EXPORT __declspec(dllexport)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000045# else
Corentin Wallez8d45d442023-05-23 08:16:55 +000046# define {{API}}_EXPORT __declspec(dllimport)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000047# endif
48# else // defined(_WIN32)
Corentin Wallez8d45d442023-05-23 08:16:55 +000049# if defined({{API}}_IMPLEMENTATION)
50# define {{API}}_EXPORT __attribute__((visibility("default")))
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000051# else
Corentin Wallez8d45d442023-05-23 08:16:55 +000052# define {{API}}_EXPORT
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000053# endif
54# endif // defined(_WIN32)
Corentin Wallez8d45d442023-05-23 08:16:55 +000055#else // defined({{API}}_SHARED_LIBRARY)
56# define {{API}}_EXPORT
57#endif // defined({{API}}_SHARED_LIBRARY)
58
59#if !defined({{API}}_OBJECT_ATTRIBUTE)
60#define {{API}}_OBJECT_ATTRIBUTE
61#endif
62#if !defined({{API}}_ENUM_ATTRIBUTE)
63#define {{API}}_ENUM_ATTRIBUTE
64#endif
65#if !defined({{API}}_STRUCTURE_ATTRIBUTE)
66#define {{API}}_STRUCTURE_ATTRIBUTE
67#endif
68#if !defined({{API}}_FUNCTION_ATTRIBUTE)
69#define {{API}}_FUNCTION_ATTRIBUTE
70#endif
71#if !defined({{API}}_NULLABLE)
72#define {{API}}_NULLABLE
73#endif
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000074
François Beaufort37e6b5c2024-09-05 20:01:49 +000075#define WGPU_BREAKING_CHANGE_DROP_DESCRIPTOR
76
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000077#include <stdint.h>
78#include <stddef.h>
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000079
Austin Eng58213ae2024-04-30 06:15:26 +000080#if defined(__cplusplus)
81# if __cplusplus >= 201103L
82# define {{API}}_MAKE_INIT_STRUCT(type, value) (type value)
83# else
84# define {{API}}_MAKE_INIT_STRUCT(type, value) value
85# endif
86#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
87# define {{API}}_MAKE_INIT_STRUCT(type, value) ((type) value)
88#else
89# define {{API}}_MAKE_INIT_STRUCT(type, value) value
90#endif
91
fujunwei4e876902021-11-25 08:44:01 +000092{% for constant in by_category["constant"] %}
Corentin Wallez8d45d442023-05-23 08:16:55 +000093 #define {{API}}_{{constant.name.SNAKE_CASE()}} {{constant.value}}
fujunwei4e876902021-11-25 08:44:01 +000094{% endfor %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000095
Austin Eng83d3e7f2024-06-28 19:48:10 +000096typedef uint64_t {{API}}Flags;
Loko Kung440a30c2023-08-04 23:41:21 +000097typedef uint32_t {{API}}Bool;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000098
99{% for type in by_category["object"] %}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000100 typedef struct {{as_cType(type.name)}}Impl* {{as_cType(type.name)}} {{API}}_OBJECT_ATTRIBUTE;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000101{% endfor %}
102
Brandon Jones112b7fd2023-05-17 01:52:30 +0000103// Structure forward declarations
Austin Eng622dbaf2024-07-23 00:09:52 +0000104{% for type in by_category["structure"] if type.name.get() != "nullable string view" %}
Brandon Jones112b7fd2023-05-17 01:52:30 +0000105 struct {{as_cType(type.name)}};
106{% endfor %}
107
Austin Eng83d3e7f2024-06-28 19:48:10 +0000108{% for type in by_category["enum"] %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000109 typedef enum {{as_cType(type.name)}} {
110 {% for value in type.values %}
111 {{as_cEnum(type.name, value.name)}} = 0x{{format(value.value, "08X")}},
112 {% endfor %}
113 {{as_cEnum(type.name, Name("force32"))}} = 0x7FFFFFFF
Corentin Wallez8d45d442023-05-23 08:16:55 +0000114 } {{as_cType(type.name)}} {{API}}_ENUM_ATTRIBUTE;
Austin Eng83d3e7f2024-06-28 19:48:10 +0000115{% endfor %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000116
Austin Eng83d3e7f2024-06-28 19:48:10 +0000117{% for type in by_category["bitmask"] %}
Austin Eng83d3e7f2024-06-28 19:48:10 +0000118 typedef {{API}}Flags {{as_cType(type.name)}};
Austin Eng83d3e7f2024-06-28 19:48:10 +0000119 {% for value in type.values %}
120 static const {{as_cType(type.name)}} {{as_cEnum(type.name, value.name)}} = 0x{{format(value.value, "016X")}};
121 {% endfor %}
Kai Ninomiya930e9182021-09-17 19:44:43 +0000122{% endfor -%}
Austin Eng83d3e7f2024-06-28 19:48:10 +0000123
Brandon Jones112b7fd2023-05-17 01:52:30 +0000124{% for type in by_category["function pointer"] %}
125 typedef {{as_cType(type.return_type.name)}} (*{{as_cType(type.name)}})(
126 {%- if type.arguments == [] -%}
127 void
128 {%- else -%}
129 {%- for arg in type.arguments -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000130 {% if not loop.first %}, {% endif %}
131 {% if arg.type.category == "structure" %}struct {% endif %}{{as_annotated_cType(arg)}}
Brandon Jones112b7fd2023-05-17 01:52:30 +0000132 {%- endfor -%}
133 {%- endif -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000134 ) {{API}}_FUNCTION_ATTRIBUTE;
Brandon Jones112b7fd2023-05-17 01:52:30 +0000135{% endfor %}
136
Loko Kung4e635252024-05-08 04:33:44 +0000137// Callback function pointers
138{% for type in by_category["callback function"] %}
139 typedef {{as_cType(type.return_type.name)}} (*{{as_cType(type.name)}})(
140 {%- for arg in type.arguments -%}
Jaswant Panchumarti6f5358202024-06-11 21:45:14 +0000141 {% if arg.type.category == "structure" %}struct {% endif %}{{as_annotated_cType(arg)}}{{", "}}
Loko Kung4e635252024-05-08 04:33:44 +0000142 {%- endfor -%}
143 void* userdata1, void* userdata2) {{API}}_FUNCTION_ATTRIBUTE;
144{% endfor %}
145
Corentin Wallez8d45d442023-05-23 08:16:55 +0000146typedef struct {{API}}ChainedStruct {
147 struct {{API}}ChainedStruct const * next;
148 {{API}}SType sType;
149} {{API}}ChainedStruct {{API}}_STRUCTURE_ATTRIBUTE;
Corentin Wallez2b24c3d2020-01-15 09:54:42 +0000150
Corentin Wallez8d45d442023-05-23 08:16:55 +0000151typedef struct {{API}}ChainedStructOut {
152 struct {{API}}ChainedStructOut * next;
153 {{API}}SType sType;
154} {{API}}ChainedStructOut {{API}}_STRUCTURE_ATTRIBUTE;
Austin Engbffc9662021-09-17 15:36:00 +0000155
Austin Eng58213ae2024-04-30 06:15:26 +0000156{% macro render_c_default_value(member) -%}
157 {%- if member.annotation in ["*", "const*"] and member.optional or member.default_value == "nullptr" -%}
158 nullptr
159 {%- elif member.type.category == "object" and member.optional -%}
160 nullptr
Loko Kung4e635252024-05-08 04:33:44 +0000161 {%- elif member.type.category == "callback function" -%}
162 nullptr
Austin Eng58213ae2024-04-30 06:15:26 +0000163 {%- elif member.type.category in ["enum", "bitmask"] and member.default_value != None -%}
164 {{as_cEnum(member.type.name, Name(member.default_value))}}
165 {%- elif member.default_value != None -%}
166 {{member.default_value}}
167 {%- elif member.type.category == "structure" and member.annotation == "value" -%}
168 {{API}}_{{member.type.name.SNAKE_CASE()}}_INIT
169 {%- else -%}
170 {{- assert(member.json_data.get("no_default", false) == false) -}}
171 {{- assert(member.default_value == None) -}}
172 {}
173 {%- endif -%}
174{% endmacro %}
175
176#define {{API}}_COMMA ,
177
Loko Kungf2a5e572024-06-20 21:56:42 +0000178{% for type in by_category["callback info"] %}
179 typedef struct {{as_cType(type.name)}} {
180 {{API}}ChainedStruct const* nextInChain;
181 {% for member in type.members %}
182 {{as_annotated_cType(member)}};
183 {% endfor %}
184 void* userdata1;
185 void* userdata2;
186 } {{as_cType(type.name)}} {{API}}_STRUCTURE_ATTRIBUTE;
187
188 #define {{API}}_{{type.name.SNAKE_CASE()}}_INIT {{API}}_MAKE_INIT_STRUCT({{as_cType(type.name)}}, { \
189 /*.nextInChain=*/nullptr {{API}}_COMMA \
190 {% for member in type.members %}
191 /*.{{as_varName(member.name)}}=*/{{render_c_default_value(member)}} {{API}}_COMMA \
192 {% endfor %}
193 /*.userdata1=*/nullptr {{API}}_COMMA \
194 /*.userdata2=*/nullptr {{API}}_COMMA \
195 })
196
197{% endfor %}
Austin Eng622dbaf2024-07-23 00:09:52 +0000198
199{% for type in by_category["structure"] if type.name.get() != "nullable string view" %}
Corentin Walleza45561b2022-07-14 12:58:25 +0000200 {% for root in type.chain_roots %}
201 // Can be chained in {{as_cType(root.name)}}
202 {% endfor %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000203 typedef struct {{as_cType(type.name)}} {
Austin Engbffc9662021-09-17 15:36:00 +0000204 {% set Out = "Out" if type.output else "" %}
Kai Ninomiya7d174a12021-09-21 17:36:27 +0000205 {% set const = "const " if not type.output else "" %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000206 {% if type.extensible %}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000207 {{API}}ChainedStruct{{Out}} {{const}}* nextInChain;
Corentin Wallez2b24c3d2020-01-15 09:54:42 +0000208 {% endif %}
209 {% if type.chained %}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000210 {{API}}ChainedStruct{{Out}} chain;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000211 {% endif %}
212 {% for member in type.members %}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000213 {% if member.optional %}
214 {{API}}_NULLABLE {{as_annotated_cType(member)}};
215 {% else %}
216 {{as_annotated_cType(member)}};
217 {% endif-%}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000218 {% endfor %}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000219 } {{as_cType(type.name)}} {{API}}_STRUCTURE_ATTRIBUTE;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000220
Austin Eng58213ae2024-04-30 06:15:26 +0000221 #define {{API}}_{{type.name.SNAKE_CASE()}}_INIT {{API}}_MAKE_INIT_STRUCT({{as_cType(type.name)}}, { \
222 {% if type.extensible %}
223 /*.nextInChain=*/nullptr {{API}}_COMMA \
224 {% endif %}
225 {% if type.chained %}
Corentin Wallez075e51a2024-09-20 08:06:07 +0000226 /*.chain=*/{/*.nextInChain*/nullptr {{API}}_COMMA /*.sType*/{{API}}SType_{{type.name.CamelCase()}}} {{API}}_COMMA \
Austin Eng58213ae2024-04-30 06:15:26 +0000227 {% endif %}
228 {% for member in type.members %}
229 /*.{{as_varName(member.name)}}=*/{{render_c_default_value(member)}} {{API}}_COMMA \
230 {% endfor %}
231 })
232
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000233{% endfor %}
Brandon Jones58a471a2021-02-08 19:48:06 +0000234{% for typeDef in by_category["typedef"] %}
235 // {{as_cType(typeDef.name)}} is deprecated.
236 // Use {{as_cType(typeDef.type.name)}} instead.
237 typedef {{as_cType(typeDef.type.name)}} {{as_cType(typeDef.name)}};
238
239{% endfor %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000240#ifdef __cplusplus
241extern "C" {
242#endif
243
Corentin Wallez8d45d442023-05-23 08:16:55 +0000244#if !defined({{API}}_SKIP_PROCS)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000245
fujunwei23f71622021-12-02 07:41:21 +0000246{% for function in by_category["function"] %}
247 typedef {{as_cType(function.return_type.name)}} (*{{as_cProc(None, function.name)}})(
248 {%- for arg in function.arguments -%}
249 {% if not loop.first %}, {% endif %}{{as_annotated_cType(arg)}}
250 {%- endfor -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000251 ) {{API}}_FUNCTION_ATTRIBUTE;
fujunwei23f71622021-12-02 07:41:21 +0000252{% endfor %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000253
Corentin Wallezaca8c4a2019-11-22 14:02:52 +0000254{% for type in by_category["object"] if len(c_methods(type)) > 0 %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000255 // Procs of {{type.name.CamelCase()}}
Corentin Wallezaca8c4a2019-11-22 14:02:52 +0000256 {% for method in c_methods(type) %}
Austin Eng83d3e7f2024-06-28 19:48:10 +0000257 typedef {{as_cType(method.return_type.name)}} (*{{as_cProc(type.name, method.name)}})(
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000258 {{-as_cType(type.name)}} {{as_varName(type.name)}}
259 {%- for arg in method.arguments -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000260 ,{{" "}}
261 {%- if arg.optional %}{{API}}_NULLABLE {% endif -%}
262 {{as_annotated_cType(arg)}}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000263 {%- endfor -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000264 ) {{API}}_FUNCTION_ATTRIBUTE;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000265 {% endfor %}
266
267{% endfor %}
Austin Eng643625a2023-08-09 00:59:33 +0000268
Corentin Wallez8d45d442023-05-23 08:16:55 +0000269#endif // !defined({{API}}_SKIP_PROCS)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000270
Corentin Wallez8d45d442023-05-23 08:16:55 +0000271#if !defined({{API}}_SKIP_DECLARATIONS)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000272
fujunwei23f71622021-12-02 07:41:21 +0000273{% for function in by_category["function"] %}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000274 {{API}}_EXPORT {{as_cType(function.return_type.name)}} {{as_cMethod(None, function.name)}}(
fujunwei23f71622021-12-02 07:41:21 +0000275 {%- for arg in function.arguments -%}
Kai Ninomiyabe98f272023-09-11 20:47:26 +0000276 {% if not loop.first %}, {% endif -%}
277 {%- if arg.optional %}{{API}}_NULLABLE {% endif -%}
278 {{as_annotated_cType(arg)}}
fujunwei23f71622021-12-02 07:41:21 +0000279 {%- endfor -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000280 ) {{API}}_FUNCTION_ATTRIBUTE;
fujunwei23f71622021-12-02 07:41:21 +0000281{% endfor %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000282
Corentin Wallezaca8c4a2019-11-22 14:02:52 +0000283{% for type in by_category["object"] if len(c_methods(type)) > 0 %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000284 // Methods of {{type.name.CamelCase()}}
Corentin Wallezaca8c4a2019-11-22 14:02:52 +0000285 {% for method in c_methods(type) %}
Austin Eng83d3e7f2024-06-28 19:48:10 +0000286 {{API}}_EXPORT {{as_cType(method.return_type.name)}} {{as_cMethod(type.name, method.name)}}(
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000287 {{-as_cType(type.name)}} {{as_varName(type.name)}}
288 {%- for arg in method.arguments -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000289 ,{{" "}}
290 {%- if arg.optional %}{{API}}_NULLABLE {% endif -%}
291 {{as_annotated_cType(arg)}}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000292 {%- endfor -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000293 ) {{API}}_FUNCTION_ATTRIBUTE;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000294 {% endfor %}
295
296{% endfor %}
Austin Eng643625a2023-08-09 00:59:33 +0000297
Corentin Wallez8d45d442023-05-23 08:16:55 +0000298#endif // !defined({{API}}_SKIP_DECLARATIONS)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000299
300#ifdef __cplusplus
301} // extern "C"
302#endif
303
fujunwei76bda372021-11-23 08:47:35 +0000304#endif // {{metadata.api.upper()}}_H_