vkd3d-shader: Implement DXBC checksum for root signatures.

Signed-off-by: Józef Kucia <jkucia@codeweavers.com>
Signed-off-by: Henri Verbeet <hverbeet@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Józef Kucia 2018-12-17 21:25:54 +01:00 committed by Alexandre Julliard
parent 3795add878
commit 49e55dd639
4 changed files with 56 additions and 38 deletions

View File

@ -35,11 +35,15 @@
#include "vkd3d_shader_private.h"
#define DXBC_CHECKSUM_BLOCK_SIZE 64
STATIC_ASSERT(sizeof(unsigned int) == 4);
struct md5_ctx
{
unsigned int i[2];
unsigned int buf[4];
unsigned char in[64];
unsigned char in[DXBC_CHECKSUM_BLOCK_SIZE];
unsigned char digest[16];
};
@ -163,7 +167,7 @@ static void byte_reverse(unsigned char *buf, unsigned longs)
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
* initialization constants.
*/
void md5_init(struct md5_ctx *ctx)
static void md5_init(struct md5_ctx *ctx)
{
ctx->buf[0] = 0x67452301;
ctx->buf[1] = 0xefcdab89;
@ -177,7 +181,7 @@ void md5_init(struct md5_ctx *ctx)
* Update context to reflect the concatenation of another buffer full
* of bytes.
*/
void md5_update(struct md5_ctx *ctx, const unsigned char *buf, unsigned int len)
static void md5_update(struct md5_ctx *ctx, const unsigned char *buf, unsigned int len)
{
unsigned int t;
@ -194,7 +198,7 @@ void md5_update(struct md5_ctx *ctx, const unsigned char *buf, unsigned int len)
if (t)
{
unsigned char *p = (unsigned char *)ctx->in + t;
t = 64 - t;
t = DXBC_CHECKSUM_BLOCK_SIZE - t;
if (len < t)
{
@ -212,27 +216,25 @@ void md5_update(struct md5_ctx *ctx, const unsigned char *buf, unsigned int len)
}
/* Process data in 64-byte chunks */
while (len >= 64)
while (len >= DXBC_CHECKSUM_BLOCK_SIZE)
{
memcpy(ctx->in, buf, 64);
memcpy(ctx->in, buf, DXBC_CHECKSUM_BLOCK_SIZE);
byte_reverse(ctx->in, 16);
md5_transform(ctx->buf, (unsigned int *)ctx->in);
buf += 64;
len -= 64;
buf += DXBC_CHECKSUM_BLOCK_SIZE;
len -= DXBC_CHECKSUM_BLOCK_SIZE;
}
/* Handle any remaining bytes of data. */
memcpy(ctx->in, buf, len);
}
/*
* Final wrapup - pad to 64-byte boundary with the bit pattern
* 1 0* (64-bit count of bits processed, MSB-first)
*/
void md5_final(struct md5_ctx *ctx)
static void dxbc_checksum_final(struct md5_ctx *ctx)
{
unsigned int padding;
unsigned int length;
unsigned int count;
unsigned char *p;
@ -243,34 +245,57 @@ void md5_final(struct md5_ctx *ctx)
always at least one byte free */
p = ctx->in + count;
*p++ = 0x80;
++count;
/* Bytes of padding needed to make 64 bytes */
count = 64 - 1 - count;
padding = DXBC_CHECKSUM_BLOCK_SIZE - count;
/* Pad out to 56 mod 64 */
if (count < 8)
if (padding < 8)
{
/* Two lots of padding: Pad the first block to 64 bytes */
memset(p, 0, count);
memset(p, 0, padding);
byte_reverse(ctx->in, 16);
md5_transform(ctx->buf, (unsigned int *)ctx->in);
/* Now fill the next block with 56 bytes */
memset(ctx->in, 0, 56);
/* Now fill the next block */
memset(ctx->in, 0, DXBC_CHECKSUM_BLOCK_SIZE);
}
else
{
/* Pad block to 56 bytes */
memset(p, 0, count - 8);
/* Make place for bitcount at the beginning of the block */
memmove(&ctx->in[4], ctx->in, count);
/* Pad block to 60 bytes */
memset(p + 4, 0, padding - 4);
}
byte_reverse(ctx->in, 14);
/* Append length in bits and transform */
((unsigned int *)ctx->in)[14] = ctx->i[0];
((unsigned int *)ctx->in)[15] = ctx->i[1];
length = ctx->i[0];
memcpy(&ctx->in[0], &length, sizeof(length));
byte_reverse(&ctx->in[4], 14);
length = ctx->i[0] >> 2 | 0x1;
memcpy(&ctx->in[DXBC_CHECKSUM_BLOCK_SIZE - 4], &length, sizeof(length));
md5_transform(ctx->buf, (unsigned int *)ctx->in);
byte_reverse((unsigned char *)ctx->buf, 4);
memcpy(ctx->digest, ctx->buf, 16);
}
#define DXBC_CHECKSUM_SKIP_BYTE_COUNT 20
void vkd3d_compute_dxbc_checksum(const void *dxbc, size_t size, uint32_t checksum[4])
{
const uint8_t *ptr = dxbc;
struct md5_ctx ctx;
assert(size > DXBC_CHECKSUM_SKIP_BYTE_COUNT);
ptr += DXBC_CHECKSUM_SKIP_BYTE_COUNT;
size -= DXBC_CHECKSUM_SKIP_BYTE_COUNT;
md5_init(&ctx);
md5_update(&ctx, ptr, size);
dxbc_checksum_final(&ctx);
memcpy(checksum, ctx.digest, sizeof(ctx.digest));
}

View File

@ -2389,7 +2389,7 @@ static int shader_write_root_signature_header(struct root_signature_writer_conte
if (!write_dword(context, TAG_DXBC))
return VKD3D_ERROR_OUT_OF_MEMORY;
WARN("DXBC checksum is not implemented.\n");
/* The checksum is computed when all data is generated. */
if (!write_dwords(context, 4, 0x00000000))
return VKD3D_ERROR_OUT_OF_MEMORY;
@ -2596,6 +2596,7 @@ int vkd3d_shader_serialize_root_signature(const struct vkd3d_root_signature_desc
{
struct root_signature_writer_context context;
size_t total_size, chunk_size;
uint32_t checksum[4];
int ret;
TRACE("root_signature %p, version %#x, dxbc %p.\n", root_signature, version, dxbc);
@ -2628,5 +2629,8 @@ int vkd3d_shader_serialize_root_signature(const struct vkd3d_root_signature_desc
dxbc->code = context.data;
dxbc->size = total_size;
vkd3d_compute_dxbc_checksum(dxbc->code, dxbc->size, checksum);
memcpy((uint32_t *)dxbc->code + 1, checksum, sizeof(checksum));
return VKD3D_OK;
}

View File

@ -815,11 +815,7 @@ int vkd3d_dxbc_compiler_generate_spirv(struct vkd3d_dxbc_compiler *compiler,
struct vkd3d_shader_code *spirv) DECLSPEC_HIDDEN;
void vkd3d_dxbc_compiler_destroy(struct vkd3d_dxbc_compiler *compiler) DECLSPEC_HIDDEN;
struct md5_ctx;
void md5_init(struct md5_ctx *ctx) DECLSPEC_HIDDEN;
void md5_update(struct md5_ctx *ctx, const unsigned char *buf, unsigned int len) DECLSPEC_HIDDEN;
void md5_final(struct md5_ctx *ctx) DECLSPEC_HIDDEN;
void vkd3d_compute_dxbc_checksum(const void *dxbc, size_t size, uint32_t checksum[4]) DECLSPEC_HIDDEN;
static inline enum vkd3d_component_type vkd3d_component_type_from_data_type(
enum vkd3d_data_type data_type)

View File

@ -9107,14 +9107,7 @@ static void test_root_signature_serialization_(unsigned int line, const DWORD *c
ok_(line)(blob_size == code_size, "Got size %u, expected %u.\n",
(unsigned int)blob_size, (unsigned int)code_size);
ok_(line)(blob_buffer[0] == code[0], "Got magic %#x, expected %#x.\n",
(unsigned int)blob_buffer[0], (unsigned int)code[0]);
for (i = 1; i < 5; ++i)
{
todo ok_(line)(blob_buffer[i] == code[i], "Got checksum %#x, expected %#x at %u.\n",
(unsigned int)blob_buffer[i], (unsigned int)code[i], i - 1);
}
for (; i < code_size / sizeof(DWORD); ++i)
for (i = 0; i < code_size / sizeof(DWORD); ++i)
{
ok_(line)(blob_buffer[i] == code[i], "Got dword %#x, expected %#x at %u.\n",
(unsigned int)blob_buffer[i], (unsigned int)code[i], i);