[Mesa-dev] [PATCH 12/23] mesa: Add Mesa ARB_get_program_binary helper functions

Tapani Pälli tapani.palli at intel.com
Thu Dec 7 13:53:04 UTC 2017


This looks good as well, I've spent also some quality debugger time 
today with these bits and my EGL_ANDROID_blob_cache branch. I'll be 
using these functions and probably do some minor refactors so that 
glGetProgramBinary() errors won't get printed when Android cache calls this.

Reviewed-by: Tapani Pälli <tapani.palli at intel.com>

On 29.11.2017 03:24, Timothy Arceri wrote:
> From: Jordan Justen <jordan.l.justen at intel.com>
> 
> V2 (Timothy Arceri):
>   - add extra code comment
>   - stop passing around void *binary and just pass
>     program_binary_header *hdr instead.
>   - move to src/mesa/main rather than src/util
> 
> V3 (Timothy Arceri):
>   - Move more code out of the backend and into the common
>     helpers.
> 
> Signed-off-by: Jordan Justen <jordan.l.justen at intel.com>
> ---
>   src/mesa/Makefile.sources      |   2 +
>   src/mesa/main/program_binary.c | 291 +++++++++++++++++++++++++++++++++++++++++
>   src/mesa/main/program_binary.h |  56 ++++++++
>   src/mesa/meson.build           |   2 +
>   4 files changed, 351 insertions(+)
>   create mode 100644 src/mesa/main/program_binary.c
>   create mode 100644 src/mesa/main/program_binary.h
> 
> diff --git a/src/mesa/Makefile.sources b/src/mesa/Makefile.sources
> index 6da1e3fef9d..d8b1eb1f995 100644
> --- a/src/mesa/Makefile.sources
> +++ b/src/mesa/Makefile.sources
> @@ -166,20 +166,22 @@ MAIN_FILES = \
>   	main/pixel.c \
>   	main/pixel.h \
>   	main/pixelstore.c \
>   	main/pixelstore.h \
>   	main/pixeltransfer.c \
>   	main/pixeltransfer.h \
>   	main/points.c \
>   	main/points.h \
>   	main/polygon.c \
>   	main/polygon.h \
> +	main/program_binary.c \
> +	main/program_binary.h \
>   	main/program_resource.c \
>   	main/program_resource.h \
>   	main/querymatrix.c \
>   	main/querymatrix.h \
>   	main/queryobj.c \
>   	main/queryobj.h \
>   	main/rastpos.c \
>   	main/rastpos.h \
>   	main/readpix.c \
>   	main/readpix.h \
> diff --git a/src/mesa/main/program_binary.c b/src/mesa/main/program_binary.c
> new file mode 100644
> index 00000000000..2786487362f
> --- /dev/null
> +++ b/src/mesa/main/program_binary.c
> @@ -0,0 +1,291 @@
> +/*
> + * Mesa 3-D graphics library
> + *
> + * Copyright (c) 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.
> + */
> +
> +/**
> + * \file program_binary.c
> + *
> + * Helper functions for serializing a binary program.
> + */
> +
> +
> +#include "compiler/blob.h"
> +#include "compiler/glsl/serialize.h"
> +#include "main/errors.h"
> +#include "main/mtypes.h"
> +#include "util/crc32.h"
> +#include "program_binary.h"
> +#include "program/prog_parameter.h"
> +
> +/**
> + * Mesa supports one binary format, but it must differentiate between formats
> + * produced by different drivers and different Mesa versions.
> + *
> + * Mesa uses a uint32_t value to specify an internal format. The only format
> + * defined has one uint32_t value of 0, followed by 20 bytes specifying a sha1
> + * that uniquely identifies the Mesa driver type and version.
> + */
> +
> +struct program_binary_header {
> +   /* If internal_format is 0, it must be followed by the 20 byte sha1 that
> +    * identifies the Mesa driver and version supported. If we want to support
> +    * something besides a sha1, then a new internal_format value can be added.
> +    */
> +   uint32_t internal_format;
> +   uint8_t sha1[20];
> +   /* Fields following sha1 can be changed since the sha1 will guarantee that
> +    * the binary only works with the same Mesa version.
> +    */
> +   uint32_t size;
> +   uint32_t crc32;
> +};
> +
> +/**
> + * Returns the header size needed for a binary
> + */
> +static unsigned
> +get_program_binary_header_size(void)
> +{
> +   return sizeof(struct program_binary_header);
> +}
> +
> +static bool
> +write_program_binary(const void *payload, unsigned payload_size,
> +                     const void *sha1, void *binary, unsigned binary_size,
> +                     GLenum *binary_format)
> +{
> +   struct program_binary_header *hdr = binary;
> +
> +   if (binary_size < sizeof(*hdr))
> +      return false;
> +
> +   /* binary_size is the size of the buffer provided by the application.
> +    * Make sure our program (payload) will fit in the buffer.
> +    */
> +   if (payload_size > binary_size - sizeof(*hdr))
> +      return false;
> +
> +   hdr->internal_format = 0;
> +   memcpy(hdr->sha1, sha1, sizeof(hdr->sha1));
> +   memcpy(hdr + 1, payload, payload_size);
> +   hdr->size = payload_size;
> +
> +   hdr->crc32 = util_hash_crc32(hdr + 1, payload_size);
> +   *binary_format = GL_PROGRAM_BINARY_FORMAT_MESA;
> +
> +   return true;
> +}
> +
> +static bool
> +simple_header_checks(const struct program_binary_header *hdr, unsigned length)
> +{
> +   if (hdr == NULL || length < sizeof(*hdr))
> +      return false;
> +
> +   if (hdr->internal_format != 0)
> +      return false;
> +
> +   return true;
> +}
> +
> +static bool
> +check_crc32(const struct program_binary_header *hdr, unsigned length)
> +{
> +   uint32_t crc32;
> +   unsigned crc32_len;
> +
> +   crc32_len = hdr->size;
> +   if (crc32_len > length - sizeof(*hdr))
> +      return false;
> +
> +   crc32 = util_hash_crc32(hdr + 1, crc32_len);
> +   if (hdr->crc32 != crc32)
> +      return false;
> +
> +   return true;
> +}
> +
> +static bool
> +is_program_binary_valid(GLenum binary_format, const void *sha1,
> +                        const struct program_binary_header *hdr,
> +                        unsigned length)
> +{
> +   if (binary_format != GL_PROGRAM_BINARY_FORMAT_MESA)
> +      return false;
> +
> +   if (!simple_header_checks(hdr, length))
> +      return false;
> +
> +   if (memcmp(hdr->sha1, sha1, sizeof(hdr->sha1)) != 0)
> +      return false;
> +
> +   if (!check_crc32(hdr, length))
> +      return false;
> +
> +   return true;
> +}
> +
> +/**
> + * Returns the payload within the binary.
> + *
> + * If NULL is returned, then the binary not supported. If non-NULL is
> + * returned, it will be a pointer contained within the specified `binary`
> + * buffer.
> + *
> + * This can be used to access the payload of `binary` during the
> + * glProgramBinary call.
> + */
> +static const void*
> +get_program_binary_payload(GLenum binary_format, const void *sha1,
> +                           const void *binary, unsigned length)
> +{
> +   const struct program_binary_header *hdr = binary;
> +   if (!is_program_binary_valid(binary_format, sha1, hdr, length))
> +      return NULL;
> +   return (const uint8_t*)binary + sizeof(*hdr);
> +}
> +
> +static void
> +write_program_payload(struct gl_context *ctx, struct blob *blob,
> +                      struct gl_shader_program *sh_prog)
> +{
> +   bool serialize[MESA_SHADER_STAGES];
> +   for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
> +      struct gl_linked_shader *shader = sh_prog->_LinkedShaders[stage];
> +      serialize[stage] = shader && shader->Program->driver_cache_blob == NULL;
> +      if (serialize[stage])
> +         ctx->Driver.ProgramBinarySerializeDriverBlob(ctx, shader->Program);
> +   }
> +
> +   serialize_glsl_program(blob, ctx, sh_prog);
> +
> +   for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
> +      if (!serialize[stage])
> +         continue;
> +
> +      struct gl_program *prog = sh_prog->_LinkedShaders[stage]->Program;
> +      ralloc_free(prog->driver_cache_blob);
> +      prog->driver_cache_blob = NULL;
> +      prog->driver_cache_blob_size = 0;
> +   }
> +}
> +
> +static bool
> +read_program_payload(struct gl_context *ctx, struct blob_reader *blob,
> +                     GLenum binary_format, struct gl_shader_program *sh_prog)
> +{
> +   if (!deserialize_glsl_program(blob, ctx, sh_prog))
> +      return false;
> +
> +   unsigned int stage;
> +   for (stage = 0; stage < ARRAY_SIZE(sh_prog->_LinkedShaders); stage++) {
> +      struct gl_linked_shader *shader = sh_prog->_LinkedShaders[stage];
> +      if (!shader)
> +         continue;
> +
> +      ctx->Driver.ProgramBinaryDeserializeDriverBlob(ctx, sh_prog,
> +                                                     shader->Program);
> +   }
> +
> +   return true;
> +}
> +
> +void
> +_mesa_get_program_binary_length(struct gl_context *ctx,
> +                                struct gl_shader_program *sh_prog,
> +                                GLint *length)
> +{
> +   struct blob blob;
> +   blob_init_fixed(&blob, NULL, SIZE_MAX);
> +   write_program_payload(ctx, &blob, sh_prog);
> +   *length = get_program_binary_header_size() + blob.size;
> +   blob_finish(&blob);
> +}
> +
> +void
> +_mesa_get_program_binary(struct gl_context *ctx,
> +                       struct gl_shader_program *sh_prog,
> +                       GLsizei buf_size, GLsizei *length,
> +                       GLenum *binary_format, GLvoid *binary)
> +{
> +   struct blob blob;
> +   uint8_t driver_sha1[20];
> +   unsigned header_size = get_program_binary_header_size();
> +
> +   ctx->Driver.GetProgramBinaryDriverSHA1(ctx, driver_sha1);
> +
> +   blob_init(&blob);
> +
> +   if (buf_size < header_size)
> +      goto fail;
> +
> +   write_program_payload(ctx, &blob, sh_prog);
> +   if (blob.size + header_size > buf_size ||
> +       blob.out_of_memory)
> +      goto fail;
> +
> +   bool written = write_program_binary(blob.data, blob.size, driver_sha1,
> +                                      binary, buf_size, binary_format);
> +   if (!written || blob.out_of_memory)
> +      goto fail;
> +
> +   *length = header_size + blob.size;
> +
> +   blob_finish(&blob);
> +   return;
> +
> +fail:
> +   _mesa_error(ctx, GL_INVALID_OPERATION,
> +               "glGetProgramBinary(buffer too small)");
> +   *length = 0;
> +   blob_finish(&blob);
> +}
> +
> +void
> +_mesa_program_binary(struct gl_context *ctx, struct gl_shader_program *sh_prog,
> +                     GLenum binary_format, const GLvoid *binary,
> +                     GLsizei length)
> +{
> +   uint8_t driver_sha1[20];
> +   unsigned header_size = get_program_binary_header_size();
> +
> +   ctx->Driver.GetProgramBinaryDriverSHA1(ctx, driver_sha1);
> +
> +   const void *payload = get_program_binary_payload(binary_format, driver_sha1,
> +                                                    binary, length);
> +
> +   if (payload == NULL) {
> +      sh_prog->data->LinkStatus = linking_failure;
> +      return;
> +   }
> +
> +   struct blob_reader blob;
> +   blob_reader_init(&blob, payload, length - header_size);
> +
> +   if (!read_program_payload(ctx, &blob, binary_format, sh_prog)) {
> +      sh_prog->data->LinkStatus = linking_failure;
> +      return;
> +   }
> +
> +   sh_prog->data->LinkStatus = linking_success;
> +}
> diff --git a/src/mesa/main/program_binary.h b/src/mesa/main/program_binary.h
> new file mode 100644
> index 00000000000..9e16b9ec54d
> --- /dev/null
> +++ b/src/mesa/main/program_binary.h
> @@ -0,0 +1,56 @@
> +/*
> + * Mesa 3-D graphics library
> + *
> + * Copyright (C) 2004-2007  Brian Paul   All Rights Reserved.
> + * Copyright (C) 2010  VMware, Inc.  All Rights Reserved.
> + *
> + * 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.
> + */
> +
> +
> +#ifndef PROGRAM_BINARY_H
> +#define PROGRAM_BINARY_H
> +
> +#include <stdbool.h>
> +
> +#ifdef __cplusplus
> +extern "C" {
> +#endif
> +
> +void
> +_mesa_get_program_binary_length(struct gl_context *ctx,
> +                                struct gl_shader_program *sh_prog,
> +                                GLint *length);
> +
> +void
> +_mesa_get_program_binary(struct gl_context *ctx,
> +                       struct gl_shader_program *sh_prog,
> +                       GLsizei buf_size, GLsizei *length,
> +                       GLenum *binary_format, GLvoid *binary);
> +
> +void
> +_mesa_program_binary(struct gl_context *ctx, struct gl_shader_program *sh_prog,
> +                     GLenum binary_format, const GLvoid *binary,
> +                     GLsizei length);
> +
> +#ifdef __cplusplus
> +}
> +#endif
> +
> +#endif /* PROGRAM_BINARY_H */
> diff --git a/src/mesa/meson.build b/src/mesa/meson.build
> index 05a3a9ac55d..670c3d22d24 100644
> --- a/src/mesa/meson.build
> +++ b/src/mesa/meson.build
> @@ -208,20 +208,22 @@ files_libmesa_common = files(
>     'main/pixel.c',
>     'main/pixel.h',
>     'main/pixelstore.c',
>     'main/pixelstore.h',
>     'main/pixeltransfer.c',
>     'main/pixeltransfer.h',
>     'main/points.c',
>     'main/points.h',
>     'main/polygon.c',
>     'main/polygon.h',
> +  'main/program_binary.c',
> +  'main/program_binary.h',
>     'main/program_resource.c',
>     'main/program_resource.h',
>     'main/querymatrix.c',
>     'main/querymatrix.h',
>     'main/queryobj.c',
>     'main/queryobj.h',
>     'main/rastpos.c',
>     'main/rastpos.h',
>     'main/readpix.c',
>     'main/readpix.h',
> 


More information about the mesa-dev mailing list