Configure the namespace of native implementation in api.json

Make ValidationUtils flexible for other generation.

BUG=dawn:1201
Change-Id: I42ccbd3d9c2fe37abec4b8f7eb395583dbe1dc8d
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/72980
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Junwei Fu <junwei.fu@intel.com>
diff --git a/dawn.json b/dawn.json
index e9eea72..5c3e5de 100644
--- a/dawn.json
+++ b/dawn.json
@@ -22,6 +22,7 @@
         "c_prefix": "WGPU",
         "namespace": "wgpu",
         "proc_table_prefix": "Dawn",
+        "native_namespace": "dawn native",
         "copyright_year": "2019"
     },
 
diff --git a/docs/codegen.md b/docs/codegen.md
index e0cfd55..d4054ea 100644
--- a/docs/codegen.md
+++ b/docs/codegen.md
@@ -28,8 +28,10 @@
  - `"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.
+ - `"proc_table_prefix"` a string, the prefix of proc table.
+ - `"impl_dir"` a string, the directory of API implementation
+ - `"native_namespace"` a string, the namespace of native implementation
+ - `"copyright_year"` (optional) a string, templates will use 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 ffee315..086f9cb 100644
--- a/generator/dawn_json_generator.py
+++ b/generator/dawn_json_generator.py
@@ -29,6 +29,8 @@
         self.namespace = metadata['namespace']
         self.c_prefix = metadata.get('c_prefix', self.namespace.upper())
         self.proc_table_prefix = metadata['proc_table_prefix']
+        self.impl_dir = metadata.get('impl_dir', '')
+        self.native_namespace = metadata['native_namespace']
         self.copyright_year = metadata.get('copyright_year', None)
 
 
@@ -856,13 +858,15 @@
                 }
             ]
 
+            impl_dir = metadata.impl_dir + '/' if metadata.impl_dir else ''
+            native_dir = impl_dir + Name(metadata.native_namespace).snake_case()
             renders.append(
                 FileRender('dawn_native/ValidationUtils.h',
-                           'src/dawn_native/ValidationUtils_autogen.h',
+                           'src/' + native_dir + '/ValidationUtils_autogen.h',
                            frontend_params))
             renders.append(
                 FileRender('dawn_native/ValidationUtils.cpp',
-                           'src/dawn_native/ValidationUtils_autogen.cpp',
+                           'src/' + native_dir + '/ValidationUtils_autogen.cpp',
                            frontend_params))
             renders.append(
                 FileRender('dawn_native/dawn_platform.h',
diff --git a/generator/templates/dawn_native/ValidationUtils.cpp b/generator/templates/dawn_native/ValidationUtils.cpp
index 199371e..abe0726 100644
--- a/generator/templates/dawn_native/ValidationUtils.cpp
+++ b/generator/templates/dawn_native/ValidationUtils.cpp
@@ -12,15 +12,19 @@
 //* See the License for the specific language governing permissions and
 //* limitations under the License.
 
-#include "dawn_native/ValidationUtils_autogen.h"
+{% set native_namespace = Name(metadata.native_namespace).snake_case() %}
+{% set impl_dir = metadata.impl_dir + "/" if metadata.impl_dir else "" %}
+{% set native_dir = impl_dir + native_namespace %}
+#include "{{native_dir}}/ValidationUtils_autogen.h"
 
-namespace dawn_native {
+namespace {{native_namespace}} {
 
+    {% set namespace = metadata.namespace %}
     {% for type in by_category["enum"] %}
-        MaybeError Validate{{type.name.CamelCase()}}(wgpu::{{as_cppType(type.name)}} value) {
+        MaybeError Validate{{type.name.CamelCase()}}({{namespace}}::{{as_cppType(type.name)}} value) {
             switch (value) {
                 {% for value in type.values if value.valid %}
-                    case wgpu::{{as_cppType(type.name)}}::{{as_cppEnum(value.name)}}:
+                    case {{namespace}}::{{as_cppType(type.name)}}::{{as_cppEnum(value.name)}}:
                         return {};
                 {% endfor %}
                 default:
@@ -31,8 +35,8 @@
     {% endfor %}
 
     {% for type in by_category["bitmask"] %}
-        MaybeError Validate{{type.name.CamelCase()}}(wgpu::{{as_cppType(type.name)}} value) {
-            if ((value & static_cast<wgpu::{{as_cppType(type.name)}}>(~{{type.full_mask}})) == 0) {
+        MaybeError Validate{{type.name.CamelCase()}}({{namespace}}::{{as_cppType(type.name)}} value) {
+            if ((value & static_cast<{{namespace}}::{{as_cppType(type.name)}}>(~{{type.full_mask}})) == 0) {
                 return {};
             }
             return DAWN_VALIDATION_ERROR("Invalid value for {{as_cType(type.name)}}");
@@ -40,4 +44,4 @@
 
     {% endfor %}
 
-} // namespace dawn_native
+} // namespace {{native_namespace}}
diff --git a/generator/templates/dawn_native/ValidationUtils.h b/generator/templates/dawn_native/ValidationUtils.h
index 1983249..68d7176 100644
--- a/generator/templates/dawn_native/ValidationUtils.h
+++ b/generator/templates/dawn_native/ValidationUtils.h
@@ -15,17 +15,22 @@
 #ifndef BACKEND_VALIDATIONUTILS_H_
 #define BACKEND_VALIDATIONUTILS_H_
 
-#include "dawn/webgpu_cpp.h"
+{% set api = metadata.api.lower() %}
+#include "dawn/{{api}}_cpp.h"
 
-#include "dawn_native/Error.h"
+{% set native_namespace = Name(metadata.native_namespace).snake_case() %}
+{% set impl_dir = metadata.impl_dir + "/" if metadata.impl_dir else "" %}
+{% set native_dir = impl_dir + native_namespace %}
+#include "{{native_dir}}/Error.h"
 
-namespace dawn_native {
+namespace {{native_namespace}} {
 
     // Helper functions to check the value of enums and bitmasks
     {% for type in by_category["enum"] + by_category["bitmask"] %}
-        MaybeError Validate{{type.name.CamelCase()}}(wgpu::{{as_cppType(type.name)}} value);
+        {% set namespace = metadata.namespace %}
+        MaybeError Validate{{type.name.CamelCase()}}({{namespace}}::{{as_cppType(type.name)}} value);
     {% endfor %}
 
-} // namespace dawn_native
+} // namespace {{native_namespace}}
 
 #endif  // BACKEND_VALIDATIONUTILS_H_