vkd3d-shader: Align the start offset instead of the size in bytecode_put_bytes().

The practical effect this has is that we avoid potential trailing padding at
the end of DXBC blobs. Unfortunately this also means we need to be more
careful about using bytecode_get_size() to find the offset where subsequent
data would get written, although in many cases this follows a put_u32() call.
This commit is contained in:
Henri Verbeet 2023-04-04 18:27:59 +02:00 committed by Alexandre Julliard
parent a0a3fb0e5f
commit d6d9aab31c
Notes: Alexandre Julliard 2023-04-04 22:36:34 +02:00
Approved-by: Alexandre Julliard (@julliard)
Merge-Request: https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/144
5 changed files with 23 additions and 19 deletions

View File

@ -57,7 +57,7 @@ int vkd3d_shader_serialize_dxbc(size_t section_count, const struct vkd3d_shader_
put_u32(&buffer, TAG_DXBC);
checksum_position = bytecode_get_size(&buffer);
checksum_position = bytecode_get_next_offset(&buffer);
for (i = 0; i < 4; ++i)
put_u32(&buffer, 0);
@ -65,13 +65,13 @@ int vkd3d_shader_serialize_dxbc(size_t section_count, const struct vkd3d_shader_
size_position = put_u32(&buffer, 0);
put_u32(&buffer, section_count);
offsets_position = bytecode_get_size(&buffer);
offsets_position = bytecode_get_next_offset(&buffer);
for (i = 0; i < section_count; ++i)
put_u32(&buffer, 0);
for (i = 0; i < section_count; ++i)
{
set_u32(&buffer, offsets_position + i * sizeof(uint32_t), bytecode_get_size(&buffer));
set_u32(&buffer, offsets_position + i * sizeof(uint32_t), bytecode_get_next_offset(&buffer));
put_u32(&buffer, sections[i].tag);
put_u32(&buffer, sections[i].data.size);
bytecode_put_bytes(&buffer, sections[i].data.code, sections[i].data.size);
@ -2754,7 +2754,7 @@ struct root_signature_writer_context
static size_t get_chunk_offset(struct root_signature_writer_context *context)
{
return bytecode_get_size(&context->buffer) - context->chunk_position;
return bytecode_get_next_offset(&context->buffer) - context->chunk_position;
}
static void shader_write_root_signature_header(struct root_signature_writer_context *context)
@ -2770,10 +2770,10 @@ static void shader_write_root_signature_header(struct root_signature_writer_cont
put_u32(buffer, 1);
context->total_size_position = put_u32(buffer, 0xffffffff);
put_u32(buffer, 1); /* chunk count */
put_u32(buffer, bytecode_get_size(buffer) + sizeof(uint32_t)); /* chunk offset */
put_u32(buffer, bytecode_get_next_offset(buffer) + sizeof(uint32_t)); /* chunk offset */
put_u32(buffer, TAG_RTS0);
put_u32(buffer, 0xffffffff);
context->chunk_position = bytecode_get_size(buffer);
context->chunk_position = bytecode_get_next_offset(buffer);
}
static void shader_write_descriptor_ranges(struct vkd3d_bytecode_buffer *buffer,
@ -2862,7 +2862,7 @@ static int shader_write_root_parameters(struct root_signature_writer_context *co
size_t parameters_position;
unsigned int i;
parameters_position = bytecode_get_size(buffer);
parameters_position = bytecode_get_next_offset(buffer);
for (i = 0; i < parameter_count; ++i)
{
put_u32(buffer, versioned_root_signature_get_parameter_type(desc, i));

View File

@ -261,7 +261,7 @@ static void write_sm1_type(struct vkd3d_bytecode_buffer *buffer, struct hlsl_typ
write_sm1_type(buffer, field->type, ctab_start);
}
fields_offset = bytecode_get_size(buffer) - ctab_start;
fields_offset = bytecode_get_next_offset(buffer) - ctab_start;
for (i = 0; i < field_count; ++i)
{
@ -351,7 +351,7 @@ static void write_sm1_uniforms(struct hlsl_ctx *ctx, struct vkd3d_bytecode_buffe
put_u32(buffer, 0); /* FIXME: flags */
put_u32(buffer, 0); /* FIXME: target string */
vars_start = bytecode_get_size(buffer);
vars_start = bytecode_get_next_offset(buffer);
LIST_FOR_EACH_ENTRY(var, &ctx->extern_vars, struct hlsl_ir_var, extern_entry)
{
@ -402,7 +402,7 @@ static void write_sm1_uniforms(struct hlsl_ctx *ctx, struct vkd3d_bytecode_buffe
offset = put_string(buffer, vkd3d_shader_get_version(NULL, NULL));
set_u32(buffer, creator_offset, offset - ctab_start);
ctab_end = bytecode_get_size(buffer);
ctab_end = bytecode_get_next_offset(buffer);
set_u32(buffer, size_offset, vkd3d_make_u32(D3DSIO_COMMENT, (ctab_end - ctab_offset) / sizeof(uint32_t)));
}

View File

@ -387,7 +387,7 @@ static void write_sm4_type(struct hlsl_ctx *ctx, struct vkd3d_bytecode_buffer *b
write_sm4_type(ctx, buffer, field->type);
}
fields_offset = bytecode_get_size(buffer);
fields_offset = bytecode_get_next_offset(buffer);
for (i = 0; i < field_count; ++i)
{
@ -583,7 +583,7 @@ static void write_sm4_rdef(struct hlsl_ctx *ctx, struct dxbc_writer *dxbc)
/* Bound resources. */
resources_offset = bytecode_get_size(&buffer);
resources_offset = bytecode_get_next_offset(&buffer);
set_u32(&buffer, resource_position, resources_offset);
for (i = 0; i < extern_resources_count; ++i)
@ -656,7 +656,7 @@ static void write_sm4_rdef(struct hlsl_ctx *ctx, struct dxbc_writer *dxbc)
/* Buffers. */
cbuffers_offset = bytecode_get_size(&buffer);
cbuffers_offset = bytecode_get_next_offset(&buffer);
set_u32(&buffer, cbuffer_position, cbuffers_offset);
LIST_FOR_EACH_ENTRY(cbuffer, &ctx->buffers, struct hlsl_buffer, entry)
{
@ -692,7 +692,7 @@ static void write_sm4_rdef(struct hlsl_ctx *ctx, struct dxbc_writer *dxbc)
i = 0;
LIST_FOR_EACH_ENTRY(cbuffer, &ctx->buffers, struct hlsl_buffer, entry)
{
size_t vars_start = bytecode_get_size(&buffer);
size_t vars_start = bytecode_get_next_offset(&buffer);
if (!cbuffer->reg.allocated)
continue;

View File

@ -342,20 +342,19 @@ void vkd3d_shader_error(struct vkd3d_shader_message_context *context, const stru
size_t bytecode_put_bytes(struct vkd3d_bytecode_buffer *buffer, const void *bytes, size_t size)
{
size_t aligned_size = align(size, 4);
size_t offset = buffer->size;
size_t offset = bytecode_get_next_offset(buffer);
if (buffer->status)
return offset;
if (!vkd3d_array_reserve((void **)&buffer->data, &buffer->capacity, offset + aligned_size, 1))
if (!vkd3d_array_reserve((void **)&buffer->data, &buffer->capacity, offset + size, 1))
{
buffer->status = VKD3D_ERROR_OUT_OF_MEMORY;
return offset;
}
memset(buffer->data + buffer->size, 0xab, offset - buffer->size);
memcpy(buffer->data + offset, bytes, size);
memset(buffer->data + offset + size, 0xab, aligned_size - size);
buffer->size = offset + aligned_size;
buffer->size = offset + size;
return offset;
}

View File

@ -1094,6 +1094,11 @@ static inline size_t bytecode_get_size(struct vkd3d_bytecode_buffer *buffer)
return buffer->size;
}
static inline size_t bytecode_get_next_offset(struct vkd3d_bytecode_buffer *buffer)
{
return align(buffer->size, 4);
}
uint32_t vkd3d_parse_integer(const char *s);
struct vkd3d_shader_message_context