summaryrefslogtreecommitdiff
path: root/src/gallium/state_trackers/d3d1x/d3d1xshader/src/dxbc_parse.cpp
blob: 6b696ae1afec805fe0e1b5a738ff6d0ea7441c6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**************************************************************************
 *
 * Copyright 2010 Luca Barbieri
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#include <memory>
#include "dxbc.h"
#include <d3d11shader.h>
#include <d3dcommon.h>

struct dxbc_container_header
{
	unsigned fourcc;
	uint32_t unk[5];
	uint32_t total_size;
	uint32_t chunk_count;
	uint32_t chunk_offsets[];
};

dxbc_container* dxbc_parse(const void* data, int size)
{
	std::auto_ptr<dxbc_container> container(new dxbc_container());
	container->data = data;
	dxbc_container_header* header = (dxbc_container_header*)data;
	if(bswap_le32(header->fourcc) != FOURCC_DXBC)
		return 0;
	unsigned num_chunks = bswap_le32(header->chunk_count);
	for(unsigned i = 0; i < num_chunks; ++i)
	{
		unsigned offset = bswap_le32(header->chunk_offsets[i]);
		dxbc_chunk_header* chunk = (dxbc_chunk_header*)((char*)data + offset);
		unsigned fourcc = bswap_le32(chunk->fourcc);
		container->chunk_map[fourcc] = i;
		container->chunks.push_back(chunk);
	}
	return container.release();
}

dxbc_chunk_header* dxbc_find_chunk(const void* data, int size, unsigned fourcc)
{
	dxbc_container_header* header = (dxbc_container_header*)data;
	if(bswap_le32(header->fourcc) != FOURCC_DXBC)
		return 0;
	unsigned num_chunks = bswap_le32(header->chunk_count);
	for(unsigned i = 0; i < num_chunks; ++i)
	{
		unsigned offset = bswap_le32(header->chunk_offsets[i]);
		dxbc_chunk_header* chunk = (dxbc_chunk_header*)((char*)data + offset);
		if(bswap_le32(chunk->fourcc) == fourcc)
			return chunk;
	}
	return 0;
}

int dxbc_parse_signature(dxbc_chunk_signature* sig, D3D11_SIGNATURE_PARAMETER_DESC** params)
{
	unsigned count = bswap_le32(sig->count);
	*params = (D3D11_SIGNATURE_PARAMETER_DESC*)malloc(sizeof(D3D11_SIGNATURE_PARAMETER_DESC) * count);

	for (unsigned i = 0; i < count; ++i)
	{
		D3D11_SIGNATURE_PARAMETER_DESC& param = (*params)[i];
		param.SemanticName = (char*)&sig->count + bswap_le32(sig->elements[i].name_offset);
		param.SemanticIndex = bswap_le32(sig->elements[i].semantic_index);
		param.SystemValueType = (D3D_NAME)bswap_le32(sig->elements[i].system_value_type);
		param.ComponentType = (D3D_REGISTER_COMPONENT_TYPE)bswap_le32(sig->elements[i].component_type);
		param.Mask = sig->elements[i].mask;
		param.ReadWriteMask = sig->elements[i].read_write_mask;
		param.Stream = sig->elements[i].stream;
	}
	return count;
}