[Mesa-dev] [PATCH] vulkan/util: Add generator for enum_to_str functions

Dylan Baker dylan at pnwbakers.com
Fri Feb 17 20:03:07 UTC 2017


I'll send out a v2 soon.

Quoting Matt Turner (2017-02-17 11:38:17)
> On Fri, Feb 17, 2017 at 10:49 AM, Dylan Baker <dylan at pnwbakers.com> wrote:
> > This adds a python generator to produce enum_to_str functions for
> > Vulkan from the vk.xml API description. It supports extensions as well
> > as core API features, and the generator works with both python2 and
> > python3.
> >
> > CC: Jason Ekstrand <jason at jlekstrand.net>
> > Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
> > ---
> >  configure.ac                       |   1 +
> >  src/Makefile.am                    |   1 +
> >  src/intel/vulkan/Makefile.am       |   2 +
> >  src/intel/vulkan/anv_util.c        |  36 +-------
> >  src/vulkan/util/.gitignore         |   1 +
> >  src/vulkan/util/Makefile.am        |  22 +++++
> >  src/vulkan/util/gen_enum_to_str.py | 172 +++++++++++++++++++++++++++++++++++++
> >  7 files changed, 201 insertions(+), 34 deletions(-)
> >  create mode 100644 src/vulkan/util/.gitignore
> >  create mode 100644 src/vulkan/util/Makefile.am
> >  create mode 100644 src/vulkan/util/gen_enum_to_str.py
> >
> > diff --git a/configure.ac b/configure.ac
> > index 7e4544f5bf..c83a5234da 100644
> > --- a/configure.ac
> > +++ b/configure.ac
> > @@ -2691,6 +2691,7 @@ AC_CONFIG_FILES([Makefile
> >                 src/mesa/main/tests/Makefile
> >                 src/util/Makefile
> >                 src/util/tests/hash_table/Makefile
> > +               src/vulkan/util/Makefile
> >                 src/vulkan/wsi/Makefile])
> 
> These two really don't need to be separate Makefiles.
> 
> Can you try, as a follow-on, to combine them as a unified
> src/vulkan/Makefile.am?

Okay, I'll do that

> 
> >
> >  AC_OUTPUT
> > diff --git a/src/Makefile.am b/src/Makefile.am
> > index 12e5dcdb12..90f95b2265 100644
> > --- a/src/Makefile.am
> > +++ b/src/Makefile.am
> > @@ -117,6 +117,7 @@ SUBDIRS += intel/tools
> >  endif
> >
> >  if HAVE_VULKAN_COMMON
> > +SUBDIRS += vulkan/util
> >  SUBDIRS += vulkan/wsi
> 
> List both on the same line.

done

> 
> >  endif
> >  EXTRA_DIST += vulkan/registry/vk.xml
> > diff --git a/src/intel/vulkan/Makefile.am b/src/intel/vulkan/Makefile.am
> > index 4197b0e77c..54bf0f5de1 100644
> > --- a/src/intel/vulkan/Makefile.am
> > +++ b/src/intel/vulkan/Makefile.am
> > @@ -49,6 +49,7 @@ AM_CPPFLAGS = \
> >         -I$(top_builddir)/src \
> >         -I$(top_srcdir)/src \
> >         -I$(top_srcdir)/src/vulkan/wsi \
> > +       -I$(top_builddir)/src/vulkan/util \
> >         -I$(top_builddir)/src/compiler \
> >         -I$(top_srcdir)/src/compiler \
> >         -I$(top_builddir)/src/compiler/nir \
> > @@ -125,6 +126,7 @@ libvulkan_common_la_SOURCES = $(VULKAN_SOURCES)
> >
> >  VULKAN_LIB_DEPS += \
> >         libvulkan_common.la \
> > +       $(top_builddir)/src/vulkan/util/libvulkan_util.la \
> >         $(top_builddir)/src/vulkan/wsi/libvulkan_wsi.la \
> >         $(top_builddir)/src/mesa/drivers/dri/i965/libi965_compiler.la \
> >         $(top_builddir)/src/compiler/nir/libnir.la \
> > diff --git a/src/intel/vulkan/anv_util.c b/src/intel/vulkan/anv_util.c
> > index 6d75187065..ec5c9486d8 100644
> > --- a/src/intel/vulkan/anv_util.c
> > +++ b/src/intel/vulkan/anv_util.c
> > @@ -29,6 +29,7 @@
> >  #include <assert.h>
> >
> >  #include "anv_private.h"
> > +#include "vk_enum_to_str.h"
> >
> >  /** Log an error message.  */
> >  void anv_printflike(1, 2)
> > @@ -69,40 +70,7 @@ __vk_errorf(VkResult error, const char *file, int line, const char *format, ...)
> >     va_list ap;
> >     char buffer[256];
> >
> > -#define ERROR_CASE(error) case error: error_str = #error; break;
> > -
> > -   const char *error_str;
> > -   switch ((int32_t)error) {
> > -
> > -   /* Core errors */
> > -   ERROR_CASE(VK_ERROR_OUT_OF_HOST_MEMORY)
> > -   ERROR_CASE(VK_ERROR_OUT_OF_DEVICE_MEMORY)
> > -   ERROR_CASE(VK_ERROR_INITIALIZATION_FAILED)
> > -   ERROR_CASE(VK_ERROR_DEVICE_LOST)
> > -   ERROR_CASE(VK_ERROR_MEMORY_MAP_FAILED)
> > -   ERROR_CASE(VK_ERROR_LAYER_NOT_PRESENT)
> > -   ERROR_CASE(VK_ERROR_EXTENSION_NOT_PRESENT)
> > -   ERROR_CASE(VK_ERROR_FEATURE_NOT_PRESENT)
> > -   ERROR_CASE(VK_ERROR_INCOMPATIBLE_DRIVER)
> > -   ERROR_CASE(VK_ERROR_TOO_MANY_OBJECTS)
> > -   ERROR_CASE(VK_ERROR_FORMAT_NOT_SUPPORTED)
> > -   ERROR_CASE(VK_ERROR_FRAGMENTED_POOL)
> > -
> > -   /* Extension errors */
> > -   ERROR_CASE(VK_ERROR_SURFACE_LOST_KHR)
> > -   ERROR_CASE(VK_ERROR_NATIVE_WINDOW_IN_USE_KHR)
> > -   ERROR_CASE(VK_ERROR_OUT_OF_DATE_KHR)
> > -   ERROR_CASE(VK_ERROR_INCOMPATIBLE_DISPLAY_KHR)
> > -   ERROR_CASE(VK_ERROR_VALIDATION_FAILED_EXT)
> > -   ERROR_CASE(VK_ERROR_INVALID_SHADER_NV)
> > -   ERROR_CASE(VK_ERROR_OUT_OF_POOL_MEMORY_KHR)
> > -
> > -   default:
> > -      assert(!"Unknown error");
> > -      error_str = "unknown error";
> > -   }
> > -
> > -#undef ERROR_CASE
> > +   const char *error_str = vk_Result_to_str(error);
> >
> >     if (format) {
> >        va_start(ap, format);
> > diff --git a/src/vulkan/util/.gitignore b/src/vulkan/util/.gitignore
> > new file mode 100644
> > index 0000000000..5c79217982
> > --- /dev/null
> > +++ b/src/vulkan/util/.gitignore
> > @@ -0,0 +1 @@
> > +vk_enum_to_str.*
> > diff --git a/src/vulkan/util/Makefile.am b/src/vulkan/util/Makefile.am
> > new file mode 100644
> > index 0000000000..ced83e8873
> > --- /dev/null
> > +++ b/src/vulkan/util/Makefile.am
> > @@ -0,0 +1,22 @@
> > +vulkan_api_xml = $(top_srcdir)/src/vulkan/registry/vk.xml
> > +
> > +AM_CPPFLAGS = \
> > +       -I$(top_srcdir)/include \
> > +       -I$(top_srcdir)/src
> > +
> > +EXTRA_DIST= \
> 
> Space before =

done

> 
> > +       gen_enum_to_str.py
> > +
> > +BUILT_SOURCES= \
> 
> Space before =

done

> 
> > +       vk_enum_to_str.c \
> > +       vk_enum_to_str.h
> > +
> > +vk_enum_to_str.c vk_enum_to_str.h: gen_enum_to_str.py $(vulkan_api_xml)
> > +       $(AM_V_GEN)$(PYTHON2) $(srcdir)/gen_enum_to_str.py
> > +
> > +noinst_LTLIBRARIES = libvulkan_util.la
> > +
> > +libvulkan_util_la_SOURCES = \
> > +       vk_enum_to_str.c \
> > +       vk_enum_to_str.h
> > +
> > diff --git a/src/vulkan/util/gen_enum_to_str.py b/src/vulkan/util/gen_enum_to_str.py
> > new file mode 100644
> > index 0000000000..0564b8e028
> > --- /dev/null
> > +++ b/src/vulkan/util/gen_enum_to_str.py
> > @@ -0,0 +1,172 @@
> > +# encoding=utf-8
> > +# Copyright © 2017 Intel Corporation
> > +
> > +# Permission is hereby granted, free of charge, to any person obtaining a copy
> > +# of this software and associated documentation files (the "Software"), to deal
> > +# in the Software without restriction, including without limitation the rights
> > +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> > +# copies of the Software, and to permit persons to whom the Software is
> > +# furnished to do so, subject to the following conditions:
> > +
> > +# The above copyright notice and this permission notice shall be included in
> > +# all copies or substantial portions of the Software.
> > +
> > +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> > +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> > +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> > +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> > +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> > +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
> > +# SOFTWARE.
> > +
> > +"""Create enum to string functions for vulking using vk.xml."""
> > +
> > +from __future__ import print_function
> > +import os
> > +import textwrap
> > +import xml.etree.cElementTree as et
> > +
> > +from mako.template import Template
> > +
> > +VK_XML = os.path.join(os.path.dirname(__file__), '..', 'registry', 'vk.xml')
> > +
> > +COPYRIGHT = textwrap.dedent(u"""\
> > +    * Copyright © 2017 Intel Corporation
> > +    *
> > +    * Permission is hereby granted, free of charge, to any person obtaining a copy
> > +    * of this software and associated documentation files (the "Software"), to deal
> > +    * in the Software without restriction, including without limitation the rights
> > +    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> > +    * copies of the Software, and to permit persons to whom the Software is
> > +    * furnished to do so, subject to the following conditions:
> > +    *
> > +    * The above copyright notice and this permission notice shall be included in
> > +    * all copies or substantial portions of the Software.
> > +    *
> > +    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> > +    * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> > +    * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> > +    * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> > +    * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> > +    * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
> > +    * SOFTWARE.""")
> > +
> > +C_TEMPLATE = Template(textwrap.dedent(u"""\
> > +    /* Autogenerated file -- do not edit
> > +     * generated by ${file}
> > +     *
> > +     ${copyright}
> > +     */
> > +
> > +    #include <vulkan/vulkan.h>
> > +    #include "util/macros.h"
> > +    #include "vk_enum_to_str.h"
> > +
> > +    % for enum in enums:
> > +
> > +        const char *
> > +        vk_${enum.name[2:]}_to_str(${enum.name} input)
> > +        {
> > +            switch(input) {
> > +            % for v in enum.values:
> > +                case ${v}:
> > +                    return "${v}";
> 
> "case", like "default", shouldn't be indented.

I know that it's not good C style to indent the case statements, but I think it
makes the for loop of the template more readable.

> 
> Acked-by: Matt Turner <mattst88 at gmail.com>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: signature
URL: <https://lists.freedesktop.org/archives/mesa-dev/attachments/20170217/a48326da/attachment-0001.sig>


More information about the mesa-dev mailing list