dxbc.h revision 6c598c78bd17642d731cf57b8369cc794f64ba2f
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#ifndef DXBC_H_
28#define DXBC_H_
29
30#include <stdint.h>
31#include <vector>
32#include <map>
33#include <iostream>
34#include "le32.h"
35
36#define FOURCC(a, b, c, d) ((uint32_t)(uint8_t)(a) | ((uint32_t)(uint8_t)(b) << 8) | ((uint32_t)(uint8_t)(c) << 16) | ((uint32_t)(uint8_t)(d) << 24 ))
37#define FOURCC_DXBC FOURCC('D', 'X', 'B', 'C')
38#define FOURCC_RDEF FOURCC('R', 'D', 'E', 'F')
39#define FOURCC_ISGN FOURCC('I', 'S', 'G', 'N')
40#define FOURCC_OSGN FOURCC('O', 'S', 'G', 'N')
41#define FOURCC_SHDR FOURCC('S', 'H', 'D', 'R')
42#define FOURCC_SHEX FOURCC('S', 'H', 'E', 'X')
43#define FOURCC_STAT FOURCC('S', 'T', 'A', 'T')
44
45/* this is always little-endian! */
46struct dxbc_chunk_header
47{
48	unsigned fourcc;
49	unsigned size;
50};
51
52/* this is always little-endian! */
53struct dxbc_chunk_signature : public dxbc_chunk_header
54{
55	uint32_t count;
56	uint32_t unk;
57	struct
58	{
59		uint32_t name_offset;
60		uint32_t semantic_index;
61		uint32_t system_value_type;
62		uint32_t component_type;
63		uint32_t register_num;
64		uint8_t mask;
65		uint8_t read_write_mask;
66		uint8_t stream; /* TODO: guess! */
67		uint8_t unused;
68	} elements[];
69};
70
71struct dxbc_container
72{
73	const void* data;
74	std::vector<dxbc_chunk_header*> chunks;
75	std::map<unsigned, unsigned> chunk_map;
76};
77
78struct dxbc_container_header
79{
80	unsigned fourcc;
81	uint32_t unk[4];
82	uint32_t one;
83	uint32_t total_size;
84	uint32_t chunk_count;
85};
86
87dxbc_container* dxbc_parse(const void* data, int size);
88std::ostream& operator <<(std::ostream& out, const dxbc_container& container);
89
90dxbc_chunk_header* dxbc_find_chunk(const void* data, int size, unsigned fourcc);
91
92static inline dxbc_chunk_header* dxbc_find_shader_bytecode(const void* data, int size)
93{
94	dxbc_chunk_header* chunk;
95	chunk = dxbc_find_chunk(data, size, FOURCC_SHDR);
96	if(!chunk)
97		chunk = dxbc_find_chunk(data, size, FOURCC_SHEX);
98	return chunk;
99}
100
101static inline dxbc_chunk_signature* dxbc_find_signature(const void* data, int size, bool output)
102{
103	return (dxbc_chunk_signature*)dxbc_find_chunk(data, size, output ? FOURCC_OSGN : FOURCC_ISGN);
104}
105
106struct _D3D11_SIGNATURE_PARAMETER_DESC;
107typedef struct _D3D11_SIGNATURE_PARAMETER_DESC D3D11_SIGNATURE_PARAMETER_DESC;
108int dxbc_parse_signature(dxbc_chunk_signature* sig, D3D11_SIGNATURE_PARAMETER_DESC** params);
109
110std::pair<void*, size_t> dxbc_assemble(struct dxbc_chunk_header** chunks, unsigned num_chunks);
111
112#endif /* DXBC_H_ */
113