[PATCH vkd3d v3 2/5] vkd3d-shader: Track bytecode buffer size in bytes.

Matteo Bruni matteo.mystral at gmail.com
Wed Jul 7 08:56:16 CDT 2021


On Mon, Jul 5, 2021 at 11:02 PM Zebediah Figura <zfigura at codeweavers.com> wrote:
>
> Signed-off-by: Zebediah Figura <zfigura at codeweavers.com>
> ---
> v3: Remove the alignment handling change. This really should have been a
> separate patch anyway, but it's also not quite clear how to structure it nicely,
> and given that the only benefit is tighter string packing it's probably not
> worth spending too much effort on at the moment.
>
>  libs/vkd3d-shader/hlsl.h         |   4 +-
>  libs/vkd3d-shader/hlsl_codegen.c | 101 ++++++++++++++-----------------
>  2 files changed, 47 insertions(+), 58 deletions(-)
>
> diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h
> index e0045acf..54273ac6 100644
> --- a/libs/vkd3d-shader/hlsl.h
> +++ b/libs/vkd3d-shader/hlsl.h
> @@ -126,7 +126,7 @@ struct hlsl_type
>      } e;
>
>      unsigned int reg_size;
> -    unsigned int bytecode_offset;
> +    size_t bytecode_offset;
>  };
>
>  struct hlsl_semantic
> @@ -144,7 +144,7 @@ struct hlsl_struct_field
>      struct hlsl_semantic semantic;
>      unsigned int reg_offset;
>
> -    unsigned int name_bytecode_offset;
> +    size_t name_bytecode_offset;
>  };
>
>  struct hlsl_reg
> diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c
> index 778d9dff..51cb789f 100644
> --- a/libs/vkd3d-shader/hlsl_codegen.c
> +++ b/libs/vkd3d-shader/hlsl_codegen.c
> @@ -1324,72 +1324,57 @@ static struct hlsl_reg hlsl_reg_from_deref(const struct hlsl_deref *deref, const
>  struct bytecode_buffer
>  {
>      struct hlsl_ctx *ctx;
> -    uint32_t *data;
> -    size_t count, size;
> +    uint8_t *data;
> +    size_t size, capacity;
>      int status;
>  };
>
> -/* Returns the token index. */
> -static unsigned int put_dword(struct bytecode_buffer *buffer, uint32_t value)
> +static size_t put_bytes(struct bytecode_buffer *buffer, const void *bytes, size_t size)
>  {
> -    unsigned int index = buffer->count;
> +    size_t aligned_size = align(size, 4);
> +    size_t offset = buffer->size;
>
>      if (buffer->status)
> -        return index;
> +        return offset;
>
> -    if (!hlsl_array_reserve(buffer->ctx, (void **)&buffer->data, &buffer->size,
> -            buffer->count + 1, sizeof(*buffer->data)))
> +    if (!hlsl_array_reserve(buffer->ctx, (void **)&buffer->data, &buffer->capacity, offset + aligned_size, 1))
>      {
>          buffer->status = VKD3D_ERROR_OUT_OF_MEMORY;
> -        return index;
> +        return offset;
>      }
> -    buffer->data[buffer->count++] = value;
> +    memcpy(buffer->data + offset, bytes, size);
> +    memset(buffer->data + offset + size, 0xab, aligned_size - size);
> +    buffer->size = offset + aligned_size;
> +    return offset;
> +}
>
> -    return index;
> +static size_t put_dword(struct bytecode_buffer *buffer, uint32_t value)
> +{
> +    return put_bytes(buffer, &value, sizeof(value));
>  }
>
> -/* Returns the token index. */
> -static unsigned int put_float(struct bytecode_buffer *buffer, float value)
> +static size_t put_float(struct bytecode_buffer *buffer, float value)
>  {
> -    union
> -    {
> -        float f;
> -        uint32_t u;
> -    } u;
> -    u.f = value;
> -    return put_dword(buffer, u.u);
> +    return put_bytes(buffer, &value, sizeof(value));
>  }
>
> -static void set_dword(struct bytecode_buffer *buffer, unsigned int index, uint32_t value)
> +static void set_dword(struct bytecode_buffer *buffer, size_t offset, uint32_t value)
>  {
>      if (buffer->status)
>          return;
>
> -    assert(index < buffer->count);
> -    buffer->data[index] = value;
> +    assert(offset + sizeof(value) <= buffer->size);
> +    memcpy(buffer->data + offset, &value, sizeof(value));
>  }
>
> -/* Returns the token index. */
> -static unsigned int put_string(struct bytecode_buffer *buffer, const char *str)
> +static size_t put_string(struct bytecode_buffer *buffer, const char *string)
>  {
> -    unsigned int index = buffer->count;
> -    size_t len = strlen(str) + 1;
> -    unsigned int token_count = (len + 3) / sizeof(*buffer->data);
> -
> -    if (buffer->status)
> -        return index;
> -
> -    if (!hlsl_array_reserve(buffer->ctx, (void **)&buffer->data, &buffer->size,
> -            buffer->count + token_count, sizeof(*buffer->data)))
> -    {
> -        buffer->status = E_OUTOFMEMORY;
> -        return index;
> -    }
> +    return put_bytes(buffer, string, strlen(string) + 1);
> +}
>
> -    buffer->data[buffer->count + token_count - 1] = 0xabababab;
> -    memcpy(buffer->data + buffer->count, str, len);
> -    buffer->count += token_count;
> -    return index;
> +static size_t get_buffer_size(struct bytecode_buffer *buffer)
> +{
> +    return buffer->size;
>  }

Just a nitpick: what you're really interested in is the current offset
in the buffer. Not that there is any kind of seeking in the middle of
the buffer or such, but the fact that buffer->size matches the current
offset is a bit of an implementation detail. Yeah, I'm complaining
about the function name... Feel free to ignore it altogether.



More information about the wine-devel mailing list