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 15 {% set Prefix = metadata.proc_table_prefix %} 16 {% set prefix = Prefix.lower() %} 17 #include "dawn_native/{{prefix}}_platform.h" 18 #include "dawn_native/{{Prefix}}Native.h" 19 20 #include <algorithm> 21 #include <vector> 22 23 {% for type in by_category["object"] %} 24 {% if type.name.canonical_case() not in ["texture view"] %} 25 #include "dawn_native/{{type.name.CamelCase()}}.h" 26 {% endif %} 27 {% endfor %} 28 29 namespace dawn_native { 30 31 {% for type in by_category["object"] %} 32 {% for method in c_methods(type) %} 33 {% set suffix = as_MethodSuffix(type.name, method.name) %} 34 35 {{as_cType(method.return_type.name)}} Native{{suffix}}( 36 {{-as_cType(type.name)}} cSelf 37 {%- for arg in method.arguments -%} 38 , {{as_annotated_cType(arg)}} 39 {%- endfor -%} 40 ) { 41 //* Perform conversion between C types and frontend types 42 auto self = FromAPI(cSelf); 43 44 {% for arg in method.arguments %} 45 {% set varName = as_varName(arg.name) %} 46 {% if arg.type.category in ["enum", "bitmask"] %} 47 auto {{varName}}_ = static_cast<{{as_frontendType(arg.type)}}>({{varName}}); 48 {% elif arg.annotation != "value" or arg.type.category == "object" %} 49 auto {{varName}}_ = reinterpret_cast<{{decorate("", as_frontendType(arg.type), arg)}}>({{varName}}); 50 {% else %} 51 auto {{varName}}_ = {{as_varName(arg.name)}}; 52 {% endif %} 53 {%- endfor-%} 54 55 {% if method.return_type.name.canonical_case() != "void" %} 56 auto result = 57 {%- endif %} 58 self->API{{method.name.CamelCase()}}( 59 {%- for arg in method.arguments -%} 60 {%- if not loop.first %}, {% endif -%} 61 {{as_varName(arg.name)}}_ 62 {%- endfor -%} 63 ); 64 {% if method.return_type.name.canonical_case() != "void" %} 65 {% if method.return_type.category == "object" %} 66 return ToAPI(result); 67 {% else %} 68 return result; 69 {% endif %} 70 {% endif %} 71 } 72 {% endfor %} 73 {% endfor %} 74 75 namespace { 76 77 struct ProcEntry { 78 WGPUProc proc; 79 const char* name; 80 }; 81 static const ProcEntry sProcMap[] = { 82 {% for (type, method) in c_methods_sorted_by_name %} 83 { reinterpret_cast<WGPUProc>(Native{{as_MethodSuffix(type.name, method.name)}}), "{{as_cMethod(type.name, method.name)}}" }, 84 {% endfor %} 85 }; 86 static constexpr size_t sProcMapSize = sizeof(sProcMap) / sizeof(sProcMap[0]); 87 88 } // anonymous namespace 89 90 WGPUInstance NativeCreateInstance(WGPUInstanceDescriptor const* descriptor) { 91 return ToAPI(InstanceBase::Create(FromAPI(descriptor))); 92 } 93 94 WGPUProc NativeGetProcAddress(WGPUDevice, const char* procName) { 95 if (procName == nullptr) { 96 return nullptr; 97 } 98 99 const ProcEntry* entry = std::lower_bound(&sProcMap[0], &sProcMap[sProcMapSize], procName, 100 [](const ProcEntry &a, const char *b) -> bool { 101 return strcmp(a.name, b) < 0; 102 } 103 ); 104 105 if (entry != &sProcMap[sProcMapSize] && strcmp(entry->name, procName) == 0) { 106 return entry->proc; 107 } 108 109 // Special case the two free-standing functions of the API. 110 if (strcmp(procName, "wgpuGetProcAddress") == 0) { 111 return reinterpret_cast<WGPUProc>(NativeGetProcAddress); 112 } 113 114 if (strcmp(procName, "wgpuCreateInstance") == 0) { 115 return reinterpret_cast<WGPUProc>(NativeCreateInstance); 116 } 117 118 return nullptr; 119 } 120 121 std::vector<const char*> GetProcMapNamesForTestingInternal() { 122 std::vector<const char*> result; 123 result.reserve(sProcMapSize); 124 for (const ProcEntry& entry : sProcMap) { 125 result.push_back(entry.name); 126 } 127 return result; 128 } 129 130 static {{Prefix}}ProcTable gProcTable = { 131 {% for function in by_category["function"] %} 132 Native{{as_cppType(function.name)}}, 133 {% endfor %} 134 {% for type in by_category["object"] %} 135 {% for method in c_methods(type) %} 136 Native{{as_MethodSuffix(type.name, method.name)}}, 137 {% endfor %} 138 {% endfor %} 139 }; 140 141 const {{Prefix}}ProcTable& GetProcsAutogen() { 142 return gProcTable; 143 } 144 } 145