Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/src/vkcom/shader/spirv_generator.py
16345 views
1
# Iterate all GLSL shaders (with suffix '.comp') in current directory.
2
#
3
# Use glslangValidator to compile them to SPIR-V shaders and write them
4
# into .cpp files as unsigned int array.
5
#
6
# Also generate a header file 'spv_shader.hpp' to extern declare these shaders.
7
8
import re
9
import os
10
import sys
11
12
dir = "./"
13
license_decl = \
14
'// This file is part of OpenCV project.\n'\
15
'// It is subject to the license terms in the LICENSE file found in the top-level directory\n'\
16
'// of this distribution and at http://opencv.org/license.html.\n'\
17
'//\n'\
18
'// Copyright (C) 2018, Intel Corporation, all rights reserved.\n'\
19
'// Third party copyrights are property of their respective owners.\n\n'
20
precomp = '#include \"../../precomp.hpp\"\n'
21
ns_head = '\nnamespace cv { namespace dnn { namespace vkcom {\n\n'
22
ns_tail = '\n}}} // namespace cv::dnn::vkcom\n'
23
24
headfile = open('spv_shader.hpp', 'w')
25
headfile.write(license_decl)
26
headfile.write('#ifndef OPENCV_DNN_SPV_SHADER_HPP\n')
27
headfile.write('#define OPENCV_DNN_SPV_SHADER_HPP\n\n')
28
headfile.write(ns_head)
29
30
cmd_remove = ''
31
null_out = ''
32
if sys.platform.find('win32') != -1:
33
cmd_remove = 'del'
34
null_out = ' >>nul 2>nul'
35
elif sys.platform.find('linux') != -1:
36
cmd_remove = 'rm'
37
null_out = ' > /dev/null 2>&1'
38
39
list = os.listdir(dir)
40
for i in range(0, len(list)):
41
if (os.path.splitext(list[i])[-1] != '.comp'):
42
continue
43
prefix = os.path.splitext(list[i])[0];
44
path = os.path.join(dir, list[i])
45
46
47
bin_file = prefix + '.tmp'
48
cmd = ' glslangValidator -V ' + path + ' -S comp -o ' + bin_file
49
print('compiling')
50
if os.system(cmd) != 0:
51
continue;
52
size = os.path.getsize(bin_file)
53
54
spv_txt_file = prefix + '.spv'
55
cmd = 'glslangValidator -V ' + path + ' -S comp -o ' + spv_txt_file + ' -x' + null_out
56
os.system(cmd)
57
58
infile_name = spv_txt_file
59
outfile_name = prefix + '_spv.cpp'
60
array_name = prefix + '_spv'
61
infile = open(infile_name, 'r')
62
outfile = open(outfile_name, 'w')
63
64
outfile.write(license_decl)
65
outfile.write(precomp)
66
outfile.write(ns_head)
67
# xxx.spv ==> xxx_spv.cpp
68
fmt = 'extern const unsigned int %s[%d] = {\n' % (array_name, size/4)
69
outfile.write(fmt)
70
for eachLine in infile:
71
if(re.match(r'^.*\/\/', eachLine)):
72
continue
73
newline = ' ' + eachLine.replace('\t','')
74
outfile.write(newline)
75
infile.close()
76
outfile.write("};\n")
77
outfile.write(ns_tail)
78
79
# write a line into header file
80
fmt = 'extern const unsigned int %s[%d];\n' % (array_name, size/4)
81
headfile.write(fmt)
82
83
os.system(cmd_remove + ' ' + bin_file)
84
os.system(cmd_remove + ' ' + spv_txt_file)
85
86
headfile.write(ns_tail)
87
headfile.write('\n#endif /* OPENCV_DNN_SPV_SHADER_HPP */\n')
88
headfile.close();
89
90