vkd3d-shader/spirv: Do not reallocate the SPIR-V program.

I ran the compilation of ~1000 DXBC-TPF shaders randomly taken from
my collection and measured the performance using callgrind and the
kcachegrind "cycle count" estimation.

BEFORE:
 * 1,846,641,596 cycles
 * 1,845,635,336 cycles
 * 1,841,335,225 cycles

AFTER:
 * 1,764,035,136 cycles
 * 1,767,948,767 cycles
 * 1,773,927,734 cycles

So callgrind would estimate a 3.6% improvement at least.

The counterpoint is that the caller might get an allocation that
is potentially bigger than necessary. I would expect that allocation
to be rather short-lived anyway, so that's probably not a problem.
This commit is contained in:
Giovanni Mascellani 2024-09-19 14:31:00 +02:00 committed by Henri Verbeet
parent 35d3161f9d
commit 9777c8bc65
Notes: Henri Verbeet 2024-09-20 17:31:54 +02:00
Approved-by: Giovanni Mascellani (@giomasce)
Approved-by: Henri Verbeet (@hverbeet)
Merge-Request: https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/1090

View File

@ -299,6 +299,16 @@ static void vkd3d_spirv_stream_free(struct vkd3d_spirv_stream *stream)
vkd3d_spirv_stream_clear(stream); vkd3d_spirv_stream_clear(stream);
} }
static void vkd3d_shader_code_from_spirv_stream(struct vkd3d_shader_code *code, struct vkd3d_spirv_stream *stream)
{
code->code = stream->words;
code->size = stream->word_count * sizeof(*stream->words);
stream->words = NULL;
stream->capacity = 0;
stream->word_count = 0;
}
static size_t vkd3d_spirv_stream_current_location(struct vkd3d_spirv_stream *stream) static size_t vkd3d_spirv_stream_current_location(struct vkd3d_spirv_stream *stream)
{ {
return stream->word_count; return stream->word_count;
@ -2018,9 +2028,7 @@ static bool vkd3d_spirv_compile_module(struct vkd3d_spirv_builder *builder,
{ {
uint64_t capability_mask = builder->capability_mask; uint64_t capability_mask = builder->capability_mask;
struct vkd3d_spirv_stream stream; struct vkd3d_spirv_stream stream;
uint32_t *code;
unsigned int i; unsigned int i;
size_t size;
vkd3d_spirv_stream_init(&stream); vkd3d_spirv_stream_init(&stream);
@ -2086,18 +2094,8 @@ static bool vkd3d_spirv_compile_module(struct vkd3d_spirv_builder *builder,
return false; return false;
} }
if (!(code = vkd3d_calloc(stream.word_count, sizeof(*code)))) vkd3d_shader_code_from_spirv_stream(spirv, &stream);
{
vkd3d_spirv_stream_free(&stream); vkd3d_spirv_stream_free(&stream);
return false;
}
size = stream.word_count * sizeof(*code);
memcpy(code, stream.words, size);
vkd3d_spirv_stream_free(&stream);
spirv->code = code;
spirv->size = size;
return true; return true;
} }