-
Notifications
You must be signed in to change notification settings - Fork 67
/
dxbc_reader.lua
213 lines (177 loc) · 5 KB
/
dxbc_reader.lua
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
local DataDump = require 'table_dumper'
local argparse = require 'argparse'
local arg_parse = argparse('dxbc_reader')
arg_parse:argument('input', 'input file')
arg_parse:option('-o --output', 'output file', false)
arg_parse:option('-d --debug', 'print debug info', false)
arg_parse:option('-p --print', 'std print', true)
local args = arg_parse:parse()
if not args.input then
args.input = 'fragment4.txt'
end
if not args.output then
args.output = args.input .. '.hlsl'
end
if args.print == 'false' then
args.print = false
end
local DEBUG=args.debug
local parser = require 'dxbc_parse'
local dxbc_def = require 'dxbc_def'
--local file_name = 'fragment.dxbc'
local file_name = args.input
local _format = string.format
local file = io.open(file_name, 'r')
local str = file:read('*a')
local parse_data = parser(str)
dxbc_def:init(parse_data)
--print(DataDump(parse_data))
local function get_op(op)
if not op then return end
local capture
local target_op
for op_def in pairs(dxbc_def.shader_def) do
if op:gsub('^' .. op_def .. '$', function(...) capture = {...} end) and capture then
target_op = op_def
break
end
end
return target_op, capture
end
local function arr2dic(list)
local dic = {}
for idx, v in pairs(list) do
dic[v] = true
dic[idx] = v
end
return dic
end
local BLOCK_DEF = {
['if'] = {
start = 'if',
close = {['else']=true, endif=true},
},
['else'] = {
start = 'else',
close = {endif=true},
},
['loop'] = {
start = 'loop',
close = {endloop=true},
},
['switch'] = {
start = 'switch',
close = {endswitch=true},
},
['case'] = {
--[[
case can closed by self
switch a
case a
case b
break
endswitch
]]--
start = 'case',
close = {case=true, ['break']=true},
}
}
local function pre_process_command(command)
if command.args then
for _, reg in pairs(command.args) do
if reg.idx then
if tonumber(reg.idx) then
reg.idx = tonumber(reg.idx)
end
end
end
end
end
local translate = {}
local idx = 2
local line_id = 1
local blocks = {}
local res_def = parse_data[1]
local function append(msg)
translate[#translate+1] = msg
print(msg)
end
if DEBUG == 't' then
append(DataDump(res_def.binding_data))
end
------------ CBUFFER DEFINE
for _, cbuff in pairs(res_def.cbuff_data) do
append('class ' .. cbuff.cbuffer_name .. '{')
for _, var in pairs(cbuff.vars) do
append(_format('\t%s\t%s;', var.type, var.name))
end
append('}')
end
local _tex_reg_cnt = 1
append('class INPUT {')
for _, var in pairs(res_def.input_data) do
if var.name == 'TEXCOORD' then
append('\t' .. var.name .. _tex_reg_cnt .. ';')
_tex_reg_cnt = _tex_reg_cnt+1
else
append('\t' .. var.name.. ';')
end
end
append('}')
_tex_reg_cnt=1
append('class OUT {')
for _, var in pairs(res_def.output_data) do
if var.name == 'TEXCOORD' then
append('\t' .. var.name .. _tex_reg_cnt.. ';')
_tex_reg_cnt = _tex_reg_cnt+1
else
append('\t' .. var.name .. ';')
end
end
append('}')
------------ CBUFFER DEFINE END
append("void main(INPUT in) {")
blocks[1] = {close = {}}
while idx <= #parse_data do
local command = parse_data[idx]
if command.op then
local op_name, op_param = get_op(command.op)
if op_name then
local op_func = dxbc_def.shader_def[op_name]
if op_func then
pre_process_command(command)
op_param = op_param and arr2dic( op_param) or {}
if DEBUG then
append('')
if DEBUG == 't' then
append(string.rep('\t', #blocks) .. DataDump(command))
end
end
local op_str, block_tag = op_func(op_param, table.unpack(command.args))
local last_block = blocks[#blocks]
if last_block and last_block.close[block_tag] then
table.remove(blocks, #blocks)
end
if DEBUG then
append(string.rep('\t', #blocks) .. command.src)
end
local last_gram = op_str:sub(#op_str)
local end_block = (last_gram == '}' or last_gram == '{' ) and '' or ';'
append(string.format('%s%s%s', string.rep('\t', #blocks), op_str, end_block))
if BLOCK_DEF[block_tag] then
table.insert(blocks, BLOCK_DEF[block_tag])
end
line_id = line_id+1
end
else
assert(false, 'not implement op ' .. command.op)
end
end
idx = idx+1
end
append("}")
local ret = table.concat(translate, '\n')
if args.print then
print(ret)
end
io.open(args.output, 'w'):write(ret)