dxbc_parse.cpp revision 92617aeac109481258f0c3863d09c1b8903d438b
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
32struct dxbc_container_header
33{
34	unsigned fourcc;
35	uint32_t unk[5];
36	uint32_t total_size;
37	uint32_t chunk_count;
38	uint32_t chunk_offsets[];
39};
40
41dxbc_container* dxbc_parse(const void* data, int size)
42{
43	std::auto_ptr<dxbc_container> container(new dxbc_container());
44	container->data = data;
45	dxbc_container_header* header = (dxbc_container_header*)data;
46	if(bswap_le32(header->fourcc) != FOURCC_DXBC)
47		return 0;
48	unsigned num_chunks = bswap_le32(header->chunk_count);
49	for(unsigned i = 0; i < num_chunks; ++i)
50	{
51		unsigned offset = bswap_le32(header->chunk_offsets[i]);
52		dxbc_chunk_header* chunk = (dxbc_chunk_header*)((char*)data + offset);
53		unsigned fourcc = bswap_le32(chunk->fourcc);
54		container->chunk_map[fourcc] = i;
55		container->chunks.push_back(chunk);
56	}
57	return container.release();
58}
59
60dxbc_chunk_header* dxbc_find_chunk(const void* data, int size, unsigned fourcc)
61{
62	dxbc_container_header* header = (dxbc_container_header*)data;
63	if(bswap_le32(header->fourcc) != FOURCC_DXBC)
64		return 0;
65	unsigned num_chunks = bswap_le32(header->chunk_count);
66	for(unsigned i = 0; i < num_chunks; ++i)
67	{
68		unsigned offset = bswap_le32(header->chunk_offsets[i]);
69		dxbc_chunk_header* chunk = (dxbc_chunk_header*)((char*)data + offset);
70		if(bswap_le32(chunk->fourcc) == fourcc)
71			return chunk;
72	}
73	return 0;
74}
75
76int dxbc_parse_signature(dxbc_chunk_signature* sig, D3D11_SIGNATURE_PARAMETER_DESC** params)
77{
78	unsigned count = bswap_le32(sig->count);
79	*params = (D3D11_SIGNATURE_PARAMETER_DESC*)malloc(sizeof(D3D11_SIGNATURE_PARAMETER_DESC) * count);
80
81	for (unsigned i = 0; i < count; ++i)
82	{
83		D3D11_SIGNATURE_PARAMETER_DESC& param = (*params)[i];
84		param.SemanticName = (char*)&sig->count + bswap_le32(sig->elements[i].name_offset);
85		param.SemanticIndex = bswap_le32(sig->elements[i].semantic_index);
86		param.SystemValueType = (D3D_NAME)bswap_le32(sig->elements[i].system_value_type);
87		param.ComponentType = (D3D_REGISTER_COMPONENT_TYPE)bswap_le32(sig->elements[i].component_type);
88		param.Mask = sig->elements[i].mask;
89		param.ReadWriteMask = sig->elements[i].read_write_mask;
90		param.Stream = sig->elements[i].stream;
91	}
92	return count;
93}
94