1/**************************************************************************
2 *
3 * Copyright 2010 Luca Barbieri
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27#include <memory>
28#include "dxbc.h"
29#include <d3d11shader.h>
30#include <d3dcommon.h>
31
32dxbc_container* dxbc_parse(const void* data, int size)
33{
34	std::auto_ptr<dxbc_container> container(new dxbc_container());
35	container->data = data;
36	dxbc_container_header* header = (dxbc_container_header*)data;
37	uint32_t* chunk_offsets = (uint32_t*)(header + 1);
38	if(bswap_le32(header->fourcc) != FOURCC_DXBC)
39		return 0;
40	unsigned num_chunks = bswap_le32(header->chunk_count);
41	for(unsigned i = 0; i < num_chunks; ++i)
42	{
43		unsigned offset = bswap_le32(chunk_offsets[i]);
44		dxbc_chunk_header* chunk = (dxbc_chunk_header*)((char*)data + offset);
45		unsigned fourcc = bswap_le32(chunk->fourcc);
46		container->chunk_map[fourcc] = i;
47		container->chunks.push_back(chunk);
48	}
49	return container.release();
50}
51
52dxbc_chunk_header* dxbc_find_chunk(const void* data, int size, unsigned fourcc)
53{
54	dxbc_container_header* header = (dxbc_container_header*)data;
55	uint32_t* chunk_offsets = (uint32_t*)(header + 1);
56	if(bswap_le32(header->fourcc) != FOURCC_DXBC)
57		return 0;
58	unsigned num_chunks = bswap_le32(header->chunk_count);
59	for(unsigned i = 0; i < num_chunks; ++i)
60	{
61		unsigned offset = bswap_le32(chunk_offsets[i]);
62		dxbc_chunk_header* chunk = (dxbc_chunk_header*)((char*)data + offset);
63		if(bswap_le32(chunk->fourcc) == fourcc)
64			return chunk;
65	}
66	return 0;
67}
68
69int dxbc_parse_signature(dxbc_chunk_signature* sig, D3D11_SIGNATURE_PARAMETER_DESC** params)
70{
71	unsigned count = bswap_le32(sig->count);
72	*params = (D3D11_SIGNATURE_PARAMETER_DESC*)malloc(sizeof(D3D11_SIGNATURE_PARAMETER_DESC) * count);
73
74	for (unsigned i = 0; i < count; ++i)
75	{
76		D3D11_SIGNATURE_PARAMETER_DESC& param = (*params)[i];
77		param.SemanticName = (char*)&sig->count + bswap_le32(sig->elements[i].name_offset);
78		param.SemanticIndex = bswap_le32(sig->elements[i].semantic_index);
79		param.SystemValueType = (D3D_NAME)bswap_le32(sig->elements[i].system_value_type);
80		param.ComponentType = (D3D_REGISTER_COMPONENT_TYPE)bswap_le32(sig->elements[i].component_type);
81		param.Register = bswap_le32(sig->elements[i].register_num);
82		param.Mask = sig->elements[i].mask;
83		param.ReadWriteMask = sig->elements[i].read_write_mask;
84		param.Stream = sig->elements[i].stream;
85	}
86	return count;
87}
88