summaryrefslogtreecommitdiff
path: root/src/gallium/state_trackers/d3d1x/d3d1xshader/src/sm4_analyze.cpp
blob: 7903d547f145de370acf6ca79c612b81e0db8ba3 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**************************************************************************
 *
 * 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 <vector>
#include <set>
#include "sm4.h"

#define check(x) do {if(!(x)) return false;} while(0)

bool sm4_link_cf_insns(sm4_program& program)
{
	if(program.cf_insn_linked.size())
		return true;

	std::vector<int> cf_insn_linked;
	cf_insn_linked.resize(program.insns.size());
	memset(&cf_insn_linked[0], 0xff, cf_insn_linked.size() * sizeof(int));
	std::vector<unsigned> cf_stack;
	for(unsigned insn_num = 0; insn_num < program.insns.size(); ++insn_num)
	{
		unsigned v;
		switch(program.insns[insn_num]->opcode)
		{
		case SM4_OPCODE_LOOP:
			cf_stack.push_back(insn_num);
			break;
		case SM4_OPCODE_ENDLOOP:
			check(!cf_stack.empty());
			v = cf_stack.back();
			check(program.insns[v]->opcode == SM4_OPCODE_LOOP);
			cf_insn_linked[v] = insn_num;
			cf_insn_linked[insn_num] = v;
			cf_stack.pop_back();
			break;
		case SM4_OPCODE_IF:
		case SM4_OPCODE_SWITCH:
			cf_insn_linked[insn_num] = insn_num; // later changed
			cf_stack.push_back(insn_num);
			break;
		case SM4_OPCODE_ELSE:
		case SM4_OPCODE_CASE:
			check(!cf_stack.empty());
			v = cf_stack.back();
			if(program.insns[insn_num]->opcode == SM4_OPCODE_ELSE)
				check(program.insns[v]->opcode == SM4_OPCODE_IF);
			else
				check(program.insns[v]->opcode == SM4_OPCODE_SWITCH || program.insns[v]->opcode == SM4_OPCODE_CASE);
			cf_insn_linked[insn_num] = cf_insn_linked[v]; // later changed
			cf_insn_linked[v] = insn_num;
			cf_stack.back() = insn_num;
			break;
		case SM4_OPCODE_ENDSWITCH:
		case SM4_OPCODE_ENDIF:
			check(!cf_stack.empty());
			v = cf_stack.back();
			if(program.insns[insn_num]->opcode == SM4_OPCODE_ENDIF)
				check(program.insns[v]->opcode == SM4_OPCODE_IF || program.insns[v]->opcode == SM4_OPCODE_ELSE);
			else
				check(program.insns[v]->opcode == SM4_OPCODE_SWITCH || program.insns[v]->opcode == SM4_OPCODE_CASE);
			cf_insn_linked[insn_num] = cf_insn_linked[v];
			cf_insn_linked[v] = insn_num;
			cf_stack.pop_back();
			break;
		}
	}
	check(cf_stack.empty());
	program.cf_insn_linked.swap(cf_insn_linked);
	return true;
}

bool sm4_find_labels(sm4_program& program)
{
	if(program.labels_found)
		return true;

	std::vector<int> labels;
	for(unsigned insn_num = 0; insn_num < program.insns.size(); ++insn_num)
	{
		switch(program.insns[insn_num]->opcode)
		{
		case SM4_OPCODE_LABEL:
			if(program.insns[insn_num]->num_ops > 0)
			{
				sm4_op& op = *program.insns[insn_num]->ops[0];
				if(op.file == SM4_FILE_LABEL && op.has_simple_index())
				{
					unsigned idx = (unsigned)op.indices[0].disp;
					if(idx >= labels.size())
						labels.resize(idx + 1);
					labels[idx] = insn_num;
				}
			}
			break;
		}
	}
	program.label_to_insn_num.swap(labels);
	program.labels_found = true;
	return true;
}

bool sm4_allocate_resource_sampler_pairs(sm4_program& program)
{
	if(program.resource_sampler_slots_assigned)
		return true;

	std::set<std::pair<int, int> > pairs;
	std::set<int> resinfos;

	for(unsigned insn_num = 0; insn_num < program.insns.size(); ++insn_num)
	{
		int resource = -1;
		int sampler = -2;
		for(unsigned i = 0; i < program.insns[insn_num]->num_ops; ++i)
		{
			sm4_op* op = program.insns[insn_num]->ops[i].get();
			if(op)
			{
				if(op->file == SM4_FILE_RESOURCE)
				{
					if(!op->has_simple_index() || resource >= 0)
						return false;
					resource = (int)op->indices[0].disp;
				}
				if(op->file == SM4_FILE_SAMPLER)
				{
					if(!op->has_simple_index() || sampler >= 0)
						return false;
					sampler = (int)op->indices[0].disp;
				}
			}
		}

		unsigned opcode = program.insns[insn_num]->opcode;
		if(opcode == SM4_OPCODE_LD || opcode == SM4_OPCODE_LD_MS)
			sampler = -1;
		if(sampler >= -1 && resource >= 0)
			pairs.insert(std::make_pair(resource, sampler));
		if(opcode == SM4_OPCODE_RESINFO)
			resinfos.insert(resource);
	}

	for(std::set<std::pair<int, int> >::iterator i = pairs.begin(); i != pairs.end(); ++i)
	{
		program.resource_sampler_to_slot[*i] = program.slot_to_resource.size();
		if(!program.resource_to_slot.count(i->first))
		{
			program.resource_to_slot[i->first] = program.slot_to_resource.size();
			resinfos.erase(i->first);
		}
		program.slot_to_resource.push_back(i->first);
		program.slot_to_sampler.push_back(i->second);
	}

	for(std::set<int>::iterator i = resinfos.begin(); i != resinfos.end(); ++i)
	{
		program.resource_sampler_to_slot[std::make_pair(*i, -1)] = program.slot_to_resource.size();
		program.resource_to_slot[*i] = program.slot_to_resource.size();
		program.slot_to_resource.push_back(*i);
		program.slot_to_sampler.push_back(-1);
	}
	program.resource_sampler_slots_assigned = true;
	return true;
}