blob: c4c1a3d632b0a7314a2af4264c4157ed9fbb91f8 [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' %}
Shrek Shao71a363b2022-02-09 19:42:51 +000030{% if 'dawn' in enabled_tags %}
31 #ifdef __EMSCRIPTEN__
32 #error "Do not include this header. Emscripten already provides headers needed for {{metadata.api}}."
33 #endif
34{% endif %}
fujunwei76bda372021-11-23 08:47:35 +000035#ifndef {{metadata.api.upper()}}_H_
36#define {{metadata.api.upper()}}_H_
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000037
Corentin Wallez8d45d442023-05-23 08:16:55 +000038{% set API = metadata.c_prefix %}
39#if defined({{API}}_SHARED_LIBRARY)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000040# if defined(_WIN32)
Corentin Wallez8d45d442023-05-23 08:16:55 +000041# if defined({{API}}_IMPLEMENTATION)
42# define {{API}}_EXPORT __declspec(dllexport)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000043# else
Corentin Wallez8d45d442023-05-23 08:16:55 +000044# define {{API}}_EXPORT __declspec(dllimport)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000045# endif
46# else // defined(_WIN32)
Corentin Wallez8d45d442023-05-23 08:16:55 +000047# if defined({{API}}_IMPLEMENTATION)
48# define {{API}}_EXPORT __attribute__((visibility("default")))
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000049# else
Corentin Wallez8d45d442023-05-23 08:16:55 +000050# define {{API}}_EXPORT
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000051# endif
52# endif // defined(_WIN32)
Corentin Wallez8d45d442023-05-23 08:16:55 +000053#else // defined({{API}}_SHARED_LIBRARY)
54# define {{API}}_EXPORT
55#endif // defined({{API}}_SHARED_LIBRARY)
56
57#if !defined({{API}}_OBJECT_ATTRIBUTE)
58#define {{API}}_OBJECT_ATTRIBUTE
59#endif
60#if !defined({{API}}_ENUM_ATTRIBUTE)
61#define {{API}}_ENUM_ATTRIBUTE
62#endif
63#if !defined({{API}}_STRUCTURE_ATTRIBUTE)
64#define {{API}}_STRUCTURE_ATTRIBUTE
65#endif
66#if !defined({{API}}_FUNCTION_ATTRIBUTE)
67#define {{API}}_FUNCTION_ATTRIBUTE
68#endif
69#if !defined({{API}}_NULLABLE)
70#define {{API}}_NULLABLE
71#endif
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000072
73#include <stdint.h>
74#include <stddef.h>
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000075
Corentin Wallez4910ae72024-04-18 16:55:19 +000076#define WGPU_BREAKING_REFERENCE_ADDREF
77
Austin Eng58213ae2024-04-30 06:15:26 +000078#if defined(__cplusplus)
79# if __cplusplus >= 201103L
80# define {{API}}_MAKE_INIT_STRUCT(type, value) (type value)
81# else
82# define {{API}}_MAKE_INIT_STRUCT(type, value) value
83# endif
84#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
85# define {{API}}_MAKE_INIT_STRUCT(type, value) ((type) value)
86#else
87# define {{API}}_MAKE_INIT_STRUCT(type, value) value
88#endif
89
fujunwei4e876902021-11-25 08:44:01 +000090{% for constant in by_category["constant"] %}
Corentin Wallez8d45d442023-05-23 08:16:55 +000091 #define {{API}}_{{constant.name.SNAKE_CASE()}} {{constant.value}}
fujunwei4e876902021-11-25 08:44:01 +000092{% endfor %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000093
Corentin Wallez8d45d442023-05-23 08:16:55 +000094typedef uint32_t {{API}}Flags;
Loko Kung440a30c2023-08-04 23:41:21 +000095typedef uint32_t {{API}}Bool;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000096
97{% for type in by_category["object"] %}
Corentin Wallez8d45d442023-05-23 08:16:55 +000098 typedef struct {{as_cType(type.name)}}Impl* {{as_cType(type.name)}} {{API}}_OBJECT_ATTRIBUTE;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +000099{% endfor %}
100
Brandon Jones112b7fd2023-05-17 01:52:30 +0000101// Structure forward declarations
102{% for type in by_category["structure"] %}
103 struct {{as_cType(type.name)}};
104{% endfor %}
105
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000106{% for type in by_category["enum"] + by_category["bitmask"] %}
107 typedef enum {{as_cType(type.name)}} {
108 {% for value in type.values %}
109 {{as_cEnum(type.name, value.name)}} = 0x{{format(value.value, "08X")}},
110 {% endfor %}
111 {{as_cEnum(type.name, Name("force32"))}} = 0x7FFFFFFF
Corentin Wallez8d45d442023-05-23 08:16:55 +0000112 } {{as_cType(type.name)}} {{API}}_ENUM_ATTRIBUTE;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000113 {% if type.category == "bitmask" %}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000114 typedef {{API}}Flags {{as_cType(type.name)}}Flags {{API}}_ENUM_ATTRIBUTE;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000115 {% endif %}
116
Kai Ninomiya930e9182021-09-17 19:44:43 +0000117{% endfor -%}
Brandon Jones112b7fd2023-05-17 01:52:30 +0000118{% for type in by_category["function pointer"] %}
119 typedef {{as_cType(type.return_type.name)}} (*{{as_cType(type.name)}})(
120 {%- if type.arguments == [] -%}
121 void
122 {%- else -%}
123 {%- for arg in type.arguments -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000124 {% if not loop.first %}, {% endif %}
125 {% if arg.type.category == "structure" %}struct {% endif %}{{as_annotated_cType(arg)}}
Brandon Jones112b7fd2023-05-17 01:52:30 +0000126 {%- endfor -%}
127 {%- endif -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000128 ) {{API}}_FUNCTION_ATTRIBUTE;
Brandon Jones112b7fd2023-05-17 01:52:30 +0000129{% endfor %}
130
Corentin Wallez8d45d442023-05-23 08:16:55 +0000131typedef struct {{API}}ChainedStruct {
132 struct {{API}}ChainedStruct const * next;
133 {{API}}SType sType;
134} {{API}}ChainedStruct {{API}}_STRUCTURE_ATTRIBUTE;
Corentin Wallez2b24c3d2020-01-15 09:54:42 +0000135
Corentin Wallez8d45d442023-05-23 08:16:55 +0000136typedef struct {{API}}ChainedStructOut {
137 struct {{API}}ChainedStructOut * next;
138 {{API}}SType sType;
139} {{API}}ChainedStructOut {{API}}_STRUCTURE_ATTRIBUTE;
Austin Engbffc9662021-09-17 15:36:00 +0000140
Austin Eng58213ae2024-04-30 06:15:26 +0000141{% macro render_c_default_value(member) -%}
142 {%- if member.annotation in ["*", "const*"] and member.optional or member.default_value == "nullptr" -%}
143 nullptr
144 {%- elif member.type.category == "object" and member.optional -%}
145 nullptr
146 {%- elif member.type.category in ["enum", "bitmask"] and member.default_value != None -%}
147 {{as_cEnum(member.type.name, Name(member.default_value))}}
148 {%- elif member.default_value != None -%}
149 {{member.default_value}}
150 {%- elif member.type.category == "structure" and member.annotation == "value" -%}
151 {{API}}_{{member.type.name.SNAKE_CASE()}}_INIT
152 {%- else -%}
153 {{- assert(member.json_data.get("no_default", false) == false) -}}
154 {{- assert(member.default_value == None) -}}
155 {}
156 {%- endif -%}
157{% endmacro %}
158
159#define {{API}}_COMMA ,
160
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000161{% for type in by_category["structure"] %}
Corentin Walleza45561b2022-07-14 12:58:25 +0000162 {% for root in type.chain_roots %}
163 // Can be chained in {{as_cType(root.name)}}
164 {% endfor %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000165 typedef struct {{as_cType(type.name)}} {
Austin Engbffc9662021-09-17 15:36:00 +0000166 {% set Out = "Out" if type.output else "" %}
Kai Ninomiya7d174a12021-09-21 17:36:27 +0000167 {% set const = "const " if not type.output else "" %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000168 {% if type.extensible %}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000169 {{API}}ChainedStruct{{Out}} {{const}}* nextInChain;
Corentin Wallez2b24c3d2020-01-15 09:54:42 +0000170 {% endif %}
171 {% if type.chained %}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000172 {{API}}ChainedStruct{{Out}} chain;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000173 {% endif %}
174 {% for member in type.members %}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000175 {% if member.optional %}
176 {{API}}_NULLABLE {{as_annotated_cType(member)}};
177 {% else %}
178 {{as_annotated_cType(member)}};
179 {% endif-%}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000180 {% endfor %}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000181 } {{as_cType(type.name)}} {{API}}_STRUCTURE_ATTRIBUTE;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000182
Austin Eng58213ae2024-04-30 06:15:26 +0000183 #define {{API}}_{{type.name.SNAKE_CASE()}}_INIT {{API}}_MAKE_INIT_STRUCT({{as_cType(type.name)}}, { \
184 {% if type.extensible %}
185 /*.nextInChain=*/nullptr {{API}}_COMMA \
186 {% endif %}
187 {% if type.chained %}
188 /*.chain=*/{} {{API}}_COMMA \
189 {% endif %}
190 {% for member in type.members %}
191 /*.{{as_varName(member.name)}}=*/{{render_c_default_value(member)}} {{API}}_COMMA \
192 {% endfor %}
193 })
194
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000195{% endfor %}
Brandon Jones58a471a2021-02-08 19:48:06 +0000196{% for typeDef in by_category["typedef"] %}
197 // {{as_cType(typeDef.name)}} is deprecated.
198 // Use {{as_cType(typeDef.type.name)}} instead.
199 typedef {{as_cType(typeDef.type.name)}} {{as_cType(typeDef.name)}};
200
201{% endfor %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000202#ifdef __cplusplus
203extern "C" {
204#endif
205
Corentin Wallez8d45d442023-05-23 08:16:55 +0000206#if !defined({{API}}_SKIP_PROCS)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000207
fujunwei23f71622021-12-02 07:41:21 +0000208{% for function in by_category["function"] %}
209 typedef {{as_cType(function.return_type.name)}} (*{{as_cProc(None, function.name)}})(
210 {%- for arg in function.arguments -%}
211 {% if not loop.first %}, {% endif %}{{as_annotated_cType(arg)}}
212 {%- endfor -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000213 ) {{API}}_FUNCTION_ATTRIBUTE;
fujunwei23f71622021-12-02 07:41:21 +0000214{% endfor %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000215
Corentin Wallezaca8c4a2019-11-22 14:02:52 +0000216{% for type in by_category["object"] if len(c_methods(type)) > 0 %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000217 // Procs of {{type.name.CamelCase()}}
Corentin Wallezaca8c4a2019-11-22 14:02:52 +0000218 {% for method in c_methods(type) %}
Loko Kungcd162942023-06-01 19:03:05 +0000219 typedef {{as_cReturnType(method.return_type)}} (*{{as_cProc(type.name, method.name)}})(
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000220 {{-as_cType(type.name)}} {{as_varName(type.name)}}
221 {%- for arg in method.arguments -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000222 ,{{" "}}
223 {%- if arg.optional %}{{API}}_NULLABLE {% endif -%}
224 {{as_annotated_cType(arg)}}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000225 {%- endfor -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000226 ) {{API}}_FUNCTION_ATTRIBUTE;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000227 {% endfor %}
228
229{% endfor %}
Austin Eng643625a2023-08-09 00:59:33 +0000230
Corentin Wallez8d45d442023-05-23 08:16:55 +0000231#endif // !defined({{API}}_SKIP_PROCS)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000232
Corentin Wallez8d45d442023-05-23 08:16:55 +0000233#if !defined({{API}}_SKIP_DECLARATIONS)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000234
fujunwei23f71622021-12-02 07:41:21 +0000235{% for function in by_category["function"] %}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000236 {{API}}_EXPORT {{as_cType(function.return_type.name)}} {{as_cMethod(None, function.name)}}(
fujunwei23f71622021-12-02 07:41:21 +0000237 {%- for arg in function.arguments -%}
Kai Ninomiyabe98f272023-09-11 20:47:26 +0000238 {% if not loop.first %}, {% endif -%}
239 {%- if arg.optional %}{{API}}_NULLABLE {% endif -%}
240 {{as_annotated_cType(arg)}}
fujunwei23f71622021-12-02 07:41:21 +0000241 {%- endfor -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000242 ) {{API}}_FUNCTION_ATTRIBUTE;
fujunwei23f71622021-12-02 07:41:21 +0000243{% endfor %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000244
Corentin Wallezaca8c4a2019-11-22 14:02:52 +0000245{% for type in by_category["object"] if len(c_methods(type)) > 0 %}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000246 // Methods of {{type.name.CamelCase()}}
Corentin Wallezaca8c4a2019-11-22 14:02:52 +0000247 {% for method in c_methods(type) %}
Loko Kungcd162942023-06-01 19:03:05 +0000248 {{API}}_EXPORT {{as_cReturnType(method.return_type)}} {{as_cMethod(type.name, method.name)}}(
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000249 {{-as_cType(type.name)}} {{as_varName(type.name)}}
250 {%- for arg in method.arguments -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000251 ,{{" "}}
252 {%- if arg.optional %}{{API}}_NULLABLE {% endif -%}
253 {{as_annotated_cType(arg)}}
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000254 {%- endfor -%}
Corentin Wallez8d45d442023-05-23 08:16:55 +0000255 ) {{API}}_FUNCTION_ATTRIBUTE;
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000256 {% endfor %}
257
258{% endfor %}
Austin Eng643625a2023-08-09 00:59:33 +0000259
Corentin Wallez8d45d442023-05-23 08:16:55 +0000260#endif // !defined({{API}}_SKIP_DECLARATIONS)
Corentin Wallez2c8b5c62019-10-21 20:04:10 +0000261
262#ifdef __cplusplus
263} // extern "C"
264#endif
265
fujunwei76bda372021-11-23 08:47:35 +0000266#endif // {{metadata.api.upper()}}_H_