Make the prefix of proc table configurable in api.json

Add a metadata to configure the prefix of proc table and Make proc table
flexiable with the prefix and declared functions.

BUG=dawn:1201
Change-Id: Id28e5521506fa5dc8efca90a7883fbd3dd548e8d
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/71526
Commit-Queue: Junwei Fu <junwei.fu@intel.com>
Reviewed-by: Austin Eng <enga@chromium.org>
diff --git a/dawn.json b/dawn.json
index 1e15ac8..a2a0a89 100644
--- a/dawn.json
+++ b/dawn.json
@@ -21,6 +21,7 @@
         "api": "WebGPU",
         "c_prefix": "WGPU",
         "namespace": "wgpu",
+        "proc_table_prefix": "Dawn",
         "copyright_year": "2019"
     },
 
diff --git a/docs/codegen.md b/docs/codegen.md
index 3166d37..d7caf37 100644
--- a/docs/codegen.md
+++ b/docs/codegen.md
@@ -28,6 +28,7 @@
  - `"api"` a string, the name of the Web API
  - `"namespace"` a string, the namespace of C++ wrapper
  - `"c_prefix"` (optional) a string, the prefix of C function and data type, it will default to upper-case of `"namespace"` if it's not provided.
+  - `"proc_table_prefix"` a string, the prefix of proc table.
  - `"copyright_year"` (optional) a string, templates will use the the year of copyright.
 
 The basic schema is that every entry is a thing with a `"category"` key what determines the sub-schema to apply to that thing. Categories and their sub-shema are defined below. Several parts of the schema use the concept of "record" which is a list of "record members" which are a combination of a type, a name and other metadata. For example the list of arguments of a function is a record. The list of structure members is a record. This combined concept is useful for the dawn_wire generator to generate code for structure and function calls in a very similar way.
diff --git a/generator/dawn_json_generator.py b/generator/dawn_json_generator.py
index 03fe667..89ed11f 100644
--- a/generator/dawn_json_generator.py
+++ b/generator/dawn_json_generator.py
@@ -28,6 +28,7 @@
         self.api = metadata['api']
         self.namespace = metadata['namespace']
         self.c_prefix = metadata.get('c_prefix', self.namespace.upper())
+        self.proc_table_prefix = metadata['proc_table_prefix']
         self.copyright_year = metadata.get('copyright_year', None)
 
 
@@ -759,12 +760,13 @@
 
         api_file_name = metadata.api.lower()
         if 'dawn_headers' in targets:
+            prefix = metadata.proc_table_prefix.lower()
             renders.append(
                 FileRender('api.h', 'src/include/dawn/' + api_file_name + '.h',
                            [RENDER_PARAMS_BASE, params_dawn]))
             renders.append(
                 FileRender('dawn_proc_table.h',
-                           'src/include/dawn/dawn_proc_table.h',
+                           'src/include/dawn/' + prefix + '_proc_table.h',
                            [RENDER_PARAMS_BASE, params_dawn]))
 
         if 'dawncpp_headers' in targets:
diff --git a/generator/templates/dawn_native/ProcTable.cpp b/generator/templates/dawn_native/ProcTable.cpp
index f2a4c55..3b6d9ff 100644
--- a/generator/templates/dawn_native/ProcTable.cpp
+++ b/generator/templates/dawn_native/ProcTable.cpp
@@ -12,8 +12,10 @@
 //* See the License for the specific language governing permissions and
 //* limitations under the License.
 
-#include "dawn_native/dawn_platform.h"
-#include "dawn_native/DawnNative.h"
+{% set Prefix = metadata.proc_table_prefix %}
+{% set prefix = Prefix.lower() %}
+#include "dawn_native/{{prefix}}_platform.h"
+#include "dawn_native/{{Prefix}}Native.h"
 
 #include <algorithm>
 #include <vector>
@@ -124,9 +126,10 @@
         return result;
     }
 
-    static DawnProcTable gProcTable = {
-        NativeGetProcAddress,
-        NativeCreateInstance,
+    static {{Prefix}}ProcTable gProcTable = {
+        {% for function in by_category["function"] %}
+            Native{{as_cppType(function.name)}},
+        {% endfor %}
         {% for type in by_category["object"] %}
             {% for method in c_methods(type) %}
                 Native{{as_MethodSuffix(type.name, method.name)}},
@@ -134,7 +137,7 @@
         {% endfor %}
     };
 
-    const DawnProcTable& GetProcsAutogen() {
+    const {{Prefix}}ProcTable& GetProcsAutogen() {
         return gProcTable;
     }
 }
diff --git a/generator/templates/dawn_proc_table.h b/generator/templates/dawn_proc_table.h
index 1da1f73..16f3fc2 100644
--- a/generator/templates/dawn_proc_table.h
+++ b/generator/templates/dawn_proc_table.h
@@ -12,15 +12,17 @@
 //* See the License for the specific language governing permissions and
 //* limitations under the License.
 
-#ifndef DAWN_DAWN_PROC_TABLE_H_
-#define DAWN_DAWN_PROC_TABLE_H_
+{% set Prefix = metadata.proc_table_prefix %}
+#ifndef DAWN_{{Prefix.upper()}}_PROC_TABLE_H_
+#define DAWN_{{Prefix.upper()}}_PROC_TABLE_H_
 
-#include "dawn/webgpu.h"
+#include "dawn/{{metadata.api.lower()}}.h"
 
 // Note: Often allocated as a static global. Do not add a complex constructor.
-typedef struct DawnProcTable {
-    WGPUProcGetProcAddress getProcAddress;
-    WGPUProcCreateInstance createInstance;
+typedef struct {{Prefix}}ProcTable {
+    {% for function in by_category["function"] %}
+        {{as_cProc(None, function.name)}} {{as_varName(function.name)}};
+    {% endfor %}
 
     {% for type in by_category["object"] %}
         {% for method in c_methods(type) %}
@@ -28,6 +30,6 @@
         {% endfor %}
 
     {% endfor %}
-} DawnProcTable;
+} {{Prefix}}ProcTable;
 
-#endif  // DAWN_DAWN_PROC_TABLE_H_
+#endif  // DAWN_{{Prefix.upper()}}_PROC_TABLE_H_
diff --git a/generator/templates/dawn_thread_dispatch_proc.cpp b/generator/templates/dawn_thread_dispatch_proc.cpp
index bfc7794..ab6b24f 100644
--- a/generator/templates/dawn_thread_dispatch_proc.cpp
+++ b/generator/templates/dawn_thread_dispatch_proc.cpp
@@ -1,11 +1,13 @@
-#include "dawn/dawn_thread_dispatch_proc.h"
+{% set Prefix = metadata.proc_table_prefix %}
+{% set prefix = Prefix.lower() %}
+#include "dawn/{{prefix}}_thread_dispatch_proc.h"
 
 #include <thread>
 
-static DawnProcTable nullProcs;
-thread_local DawnProcTable perThreadProcs;
+static {{Prefix}}ProcTable nullProcs;
+thread_local {{Prefix}}ProcTable perThreadProcs;
 
-void dawnProcSetPerThreadProcs(const DawnProcTable* procs) {
+void {{prefix}}ProcSetPerThreadProcs(const {{Prefix}}ProcTable* procs) {
     if (procs) {
         perThreadProcs = *procs;
     } else {
@@ -40,13 +42,14 @@
 {% endfor %}
 
 extern "C" {
-    DawnProcTable dawnThreadDispatchProcTable = {
-        ThreadDispatchGetProcAddress,
-        ThreadDispatchCreateInstance,
-{% for type in by_category["object"] %}
-    {% for method in c_methods(type) %}
-        ThreadDispatch{{as_MethodSuffix(type.name, method.name)}},
-    {% endfor %}
-{% endfor %}
+    {{Prefix}}ProcTable {{prefix}}ThreadDispatchProcTable = {
+        {% for function in by_category["function"] %}
+            ThreadDispatch{{as_cppType(function.name)}},
+        {% endfor %}
+        {% for type in by_category["object"] %}
+            {% for method in c_methods(type) %}
+                ThreadDispatch{{as_MethodSuffix(type.name, method.name)}},
+            {% endfor %}
+        {% endfor %}
     };
 }
diff --git a/generator/templates/dawn_wire/client/ApiProcs.cpp b/generator/templates/dawn_wire/client/ApiProcs.cpp
index ded2dda..48b3e3a 100644
--- a/generator/templates/dawn_wire/client/ApiProcs.cpp
+++ b/generator/templates/dawn_wire/client/ApiProcs.cpp
@@ -159,16 +159,18 @@
         return result;
     }
 
-    static DawnProcTable gProcTable = {
-        ClientGetProcAddress,
-        ClientCreateInstance,
+    {% set Prefix = metadata.proc_table_prefix %}
+    static {{Prefix}}ProcTable gProcTable = {
+        {% for function in by_category["function"] %}
+            Client{{as_cppType(function.name)}},
+        {% endfor %}
         {% for type in by_category["object"] %}
             {% for method in c_methods(type) %}
                 Client{{as_MethodSuffix(type.name, method.name)}},
             {% endfor %}
         {% endfor %}
     };
-    const DawnProcTable& GetProcs() {
+    const {{Prefix}}ProcTable& GetProcs() {
         return gProcTable;
     }
 }}  // namespace dawn_wire::client