2018-07-18 09:40:26 +00:00
//* Copyright 2017 The Dawn Authors
2017-04-20 18:38:20 +00:00
//*
//* Licensed under the Apache License, Version 2.0 (the "License");
//* you may not use this file except in compliance with the License.
//* You may obtain a copy of the License at
//*
//* http://www.apache.org/licenses/LICENSE-2.0
//*
//* Unless required by applicable law or agreed to in writing, software
//* distributed under the License is distributed on an "AS IS" BASIS,
//* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//* See the License for the specific language governing permissions and
//* limitations under the License.
2021-09-17 19:44:43 +00:00
{ % if ' dawn ' in enabled_tags % }
# include "dawn/webgpu_cpp.h"
{ % else % }
# include "webgpu/webgpu_cpp.h"
{ % endif % }
2017-04-20 18:38:20 +00:00
2021-09-23 17:34:53 +00:00
# ifdef __GNUC__
// error: 'offsetof' within non-standard-layout type 'wgpu::XXX' is conditionally-supported
# pragma GCC diagnostic ignored "-Winvalid-offsetof"
# endif
2019-10-21 20:04:10 +00:00
namespace wgpu {
2019-10-17 08:46:07 +00:00
{ % for type in by_category [ " enum " ] % }
2017-04-20 18:38:20 +00:00
{ % set CppType = as_cppType ( type . name ) % }
{ % set CType = as_cType ( type . name ) % }
2020-07-30 19:02:43 +00:00
// {{CppType}}
2017-04-20 18:38:20 +00:00
static_assert ( sizeof ( { { CppType } } ) = = sizeof ( { { CType } } ) , " sizeof mismatch for {{CppType}} " ) ;
static_assert ( alignof ( { { CppType } } ) = = alignof ( { { CType } } ) , " alignof mismatch for {{CppType}} " ) ;
{ % for value in type . values % }
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)}} " ) ;
{ % endfor % }
2020-07-30 19:02:43 +00:00
{ % endfor - % }
2019-10-17 08:46:07 +00:00
{ % for type in by_category [ " bitmask " ] % }
{ % set CppType = as_cppType ( type . name ) % }
{ % set CType = as_cType ( type . name ) + " Flags " % }
2020-07-30 19:02:43 +00:00
// {{CppType}}
2019-10-17 08:46:07 +00:00
static_assert ( sizeof ( { { CppType } } ) = = sizeof ( { { CType } } ) , " sizeof mismatch for {{CppType}} " ) ;
static_assert ( alignof ( { { CppType } } ) = = alignof ( { { CType } } ) , " alignof mismatch for {{CppType}} " ) ;
{ % for value in type . values % }
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)}} " ) ;
{ % endfor % }
{ % endfor % }
2017-04-20 18:38:20 +00:00
2020-07-30 19:02:43 +00:00
// ChainedStruct
2020-01-15 09:54:42 +00:00
static_assert ( sizeof ( ChainedStruct ) = = sizeof ( WGPUChainedStruct ) ,
" sizeof mismatch for ChainedStruct " ) ;
static_assert ( alignof ( ChainedStruct ) = = alignof ( WGPUChainedStruct ) ,
" alignof mismatch for ChainedStruct " ) ;
2020-01-25 09:30:40 +00:00
static_assert ( offsetof ( ChainedStruct , nextInChain ) = = offsetof ( WGPUChainedStruct , next ) ,
2020-01-15 09:54:42 +00:00
" offsetof mismatch for ChainedStruct::nextInChain " ) ;
static_assert ( offsetof ( ChainedStruct , sType ) = = offsetof ( WGPUChainedStruct , sType ) ,
" offsetof mismatch for ChainedStruct::sType " ) ;
2018-05-17 20:55:53 +00:00
{ % for type in by_category [ " structure " ] % }
{ % set CppType = as_cppType ( type . name ) % }
{ % set CType = as_cType ( type . name ) % }
2020-07-30 19:02:43 +00:00
// {{CppType}}
2018-05-17 20:55:53 +00:00
static_assert ( sizeof ( { { CppType } } ) = = sizeof ( { { CType } } ) , " sizeof mismatch for {{CppType}} " ) ;
static_assert ( alignof ( { { CppType } } ) = = alignof ( { { CType } } ) , " alignof mismatch for {{CppType}} " ) ;
{ % if type . extensible % }
static_assert ( offsetof ( { { CppType } } , nextInChain ) = = offsetof ( { { CType } } , nextInChain ) ,
" offsetof mismatch for {{CppType}}::nextInChain " ) ;
{ % endif % }
{ % for member in type . members % }
{ % set memberName = member . name . camelCase ( ) % }
static_assert ( offsetof ( { { CppType } } , { { memberName } } ) = = offsetof ( { { CType } } , { { memberName } } ) ,
" offsetof mismatch for {{CppType}}::{{memberName}} " ) ;
{ % endfor % }
2020-07-30 19:02:43 +00:00
{ % endfor - % }
2018-05-17 20:55:53 +00:00
2017-04-20 18:38:20 +00:00
{ % for type in by_category [ " object " ] % }
{ % set CppType = as_cppType ( type . name ) % }
{ % set CType = as_cType ( type . name ) % }
2020-07-30 19:02:43 +00:00
// {{CppType}}
2017-04-20 18:38:20 +00:00
static_assert ( sizeof ( { { CppType } } ) = = sizeof ( { { CType } } ) , " sizeof mismatch for {{CppType}} " ) ;
static_assert ( alignof ( { { CppType } } ) = = alignof ( { { CType } } ) , " alignof mismatch for {{CppType}} " ) ;
2020-07-30 19:02:43 +00:00
{ % macro render_cpp_method_declaration ( type , method ) - % }
2017-04-20 18:38:20 +00:00
{ % set CppType = as_cppType ( type . name ) % }
2019-04-01 21:48:38 +00:00
{ { as_cppType ( method . return_type . name ) } } { { CppType } } : : { { method . name . CamelCase ( ) } } (
2017-04-20 18:38:20 +00:00
{ % - for arg in method . arguments - % }
{ % - if not loop . first % } , { % endif - % }
{ % - if arg . type . category = = " object " and arg . annotation = = " value " - % }
{ { as_cppType ( arg . type . name ) } } const & { { as_varName ( arg . name ) } }
{ % - else - % }
{ { as_annotated_cppType ( arg ) } }
{ % - endif - % }
{ % - endfor - % }
) const
2020-07-30 19:02:43 +00:00
{ % - endmacro - % }
2017-04-20 18:38:20 +00:00
2020-07-30 19:02:43 +00:00
{ % - macro render_cpp_to_c_method_call ( type , method ) - % }
2017-04-20 18:38:20 +00:00
{ { as_cMethod ( type . name , method . name ) } } ( Get ( )
{ % - for arg in method . arguments - % } , { { " " } }
{ % - if arg . annotation = = " value " - % }
{ % - if arg . type . category = = " object " - % }
{ { as_varName ( arg . name ) } } . Get ( )
{ % - elif arg . type . category = = " enum " or arg . type . category = = " bitmask " - % }
static_cast < { { as_cType ( arg . type . name ) } } > ( { { as_varName ( arg . name ) } } )
2019-11-22 13:18:22 +00:00
{ % - elif arg . type . category in [ " callback " , " native " ] - % }
2017-04-20 18:38:20 +00:00
{ { as_varName ( arg . name ) } }
{ % - else - % }
UNHANDLED
{ % - endif - % }
{ % - else - % }
reinterpret_cast < { { decorate ( " " , as_cType ( arg . type . name ) , arg ) } } > ( { { as_varName ( arg . name ) } } )
{ % - endif - % }
{ % - endfor - % }
)
2020-07-30 19:02:43 +00:00
{ % - endmacro - % }
2017-04-20 18:38:20 +00:00
2020-07-30 19:02:43 +00:00
{ % for method in type . methods - % }
2017-04-20 18:38:20 +00:00
{ { render_cpp_method_declaration ( type , method ) } } {
{ % if method . return_type . name . concatcase ( ) = = " void " % }
{ { render_cpp_to_c_method_call ( type , method ) } } ;
{ % else % }
auto result = { { render_cpp_to_c_method_call ( type , method ) } } ;
2019-05-15 18:55:22 +00:00
return { { convert_cType_to_cppType ( method . return_type , ' value ' , ' result ' ) | indent ( 8 ) } } ;
2017-04-20 18:38:20 +00:00
{ % endif % }
}
{ % endfor % }
2019-10-21 20:04:10 +00:00
void { { CppType } } : : WGPUReference ( { { CType } } handle ) {
2017-04-20 18:38:20 +00:00
if ( handle ! = nullptr ) {
{ { as_cMethod ( type . name , Name ( " reference " ) ) } } ( handle ) ;
}
}
2019-10-21 20:04:10 +00:00
void { { CppType } } : : WGPURelease ( { { CType } } handle ) {
2017-04-20 18:38:20 +00:00
if ( handle ! = nullptr ) {
{ { as_cMethod ( type . name , Name ( " release " ) ) } } ( handle ) ;
}
}
{ % endfor % }
2020-07-30 19:02:43 +00:00
// Instance
2020-01-10 13:06:48 +00:00
Instance CreateInstance ( const InstanceDescriptor * descriptor ) {
const WGPUInstanceDescriptor * cDescriptor =
reinterpret_cast < const WGPUInstanceDescriptor * > ( descriptor ) ;
return Instance : : Acquire ( wgpuCreateInstance ( cDescriptor ) ) ;
}
2019-10-15 12:08:48 +00:00
Proc GetProcAddress ( Device const & device , const char * procName ) {
2019-11-26 17:28:11 +00:00
return reinterpret_cast < Proc > ( wgpuGetProcAddress ( device . Get ( ) , procName ) ) ;
2019-10-15 12:08:48 +00:00
}
2017-04-20 18:38:20 +00:00
}