methods.py 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. import os
  2. def add_source_files(self, sources, filetype, lib_env=None, shared=False):
  3. import glob
  4. import string
  5. # if not lib_objects:
  6. if not lib_env:
  7. lib_env = self
  8. if type(filetype) == type(""):
  9. dir = self.Dir('.').abspath
  10. list = glob.glob(dir + "/" + filetype)
  11. for f in list:
  12. sources.append(self.Object(f))
  13. else:
  14. for f in filetype:
  15. sources.append(self.Object(f))
  16. def build_shader_header(target, source, env):
  17. for x in source:
  18. print x
  19. name = str(x)
  20. name = name[name.rfind("/") + 1:]
  21. name = name[name.rfind("\\") + 1:]
  22. name = name.replace(".", "_")
  23. fs = open(str(x), "r")
  24. fd = open(str(x) + ".h", "w")
  25. fd.write("/* this file has been generated by SCons, do not edit! */\n")
  26. fd.write("static const char *" + name + "=\n")
  27. line = fs.readline()
  28. while(line):
  29. line = line.replace("\r", "")
  30. line = line.replace("\n", "")
  31. line = line.replace("\\", "\\\\")
  32. line = line.replace("\"", "\\\"")
  33. fd.write("\"" + line + "\\n\"\n")
  34. line = fs.readline()
  35. fd.write(";\n")
  36. return 0
  37. def build_glsl_header(filename):
  38. fs = open(filename, "r")
  39. line = fs.readline()
  40. vertex_lines = []
  41. fragment_lines = []
  42. uniforms = []
  43. attributes = []
  44. fbos = []
  45. conditionals = []
  46. texunits = []
  47. texunit_names = []
  48. ubos = []
  49. ubo_names = []
  50. reading = ""
  51. line_offset = 0
  52. vertex_offset = 0
  53. fragment_offset = 0
  54. while(line):
  55. if (line.find("[vertex]") != -1):
  56. reading = "vertex"
  57. line = fs.readline()
  58. line_offset += 1
  59. vertex_offset = line_offset
  60. continue
  61. if (line.find("[fragment]") != -1):
  62. reading = "fragment"
  63. line = fs.readline()
  64. line_offset += 1
  65. fragment_offset = line_offset
  66. continue
  67. if (line.find("#ifdef ") != -1):
  68. ifdefline = line.replace("#ifdef ", "").strip()
  69. if (not ifdefline in conditionals):
  70. conditionals += [ifdefline]
  71. if (line.find("#elif defined(") != -1):
  72. ifdefline = line.replace("#elif defined(", "").strip()
  73. ifdefline = ifdefline.replace(")", "").strip()
  74. if (not ifdefline in conditionals):
  75. conditionals += [ifdefline]
  76. import re
  77. if re.search(r"^\s*uniform", line):
  78. if (line.lower().find("texunit:") != -1):
  79. # texture unit
  80. texunit = str(int(line[line.find(":") + 1:].strip()))
  81. uline = line[:line.lower().find("//")]
  82. uline = uline.replace("uniform", "")
  83. uline = uline.replace(";", "")
  84. lines = uline.split(",")
  85. for x in lines:
  86. x = x.strip()
  87. x = x[x.rfind(" ") + 1:]
  88. if (x.find("[") != -1):
  89. # unfiorm array
  90. x = x[:x.find("[")]
  91. if (not x in texunit_names):
  92. texunits += [(x, texunit)]
  93. texunit_names += [x]
  94. elif (line.lower().find("ubo:") != -1):
  95. # ubo
  96. uboidx = str(int(line[line.find(":") + 1:].strip()))
  97. uline = line[:line.lower().find("//")]
  98. uline = uline[uline.find("uniform") + len("uniform"):]
  99. uline = uline.replace(";", "")
  100. uline = uline.replace("{", "")
  101. lines = uline.split(",")
  102. for x in lines:
  103. x = x.strip()
  104. x = x[x.rfind(" ") + 1:]
  105. if (x.find("[") != -1):
  106. # unfiorm array
  107. x = x[:x.find("[")]
  108. if (not x in ubo_names):
  109. ubos += [(x, uboidx)]
  110. ubo_names += [x]
  111. else:
  112. uline = line.replace("uniform", "")
  113. uline = uline.replace(";", "")
  114. lines = uline.split(",")
  115. for x in lines:
  116. x = x.strip()
  117. x = x[x.rfind(" ") + 1:]
  118. if (x.find("[") != -1):
  119. # unfiorm array
  120. x = x[:x.find("[")]
  121. if (not x in uniforms):
  122. uniforms += [x]
  123. if ((line.strip().find("in ") == 0 or line.strip().find("attribute ") == 0) and line.find("attrib:") != -1):
  124. uline = line.replace("in ", "")
  125. uline = uline.replace("attribute ", "")
  126. uline = uline.replace(";", "")
  127. uline = uline[uline.find(" "):].strip()
  128. if (uline.find("//") != -1):
  129. name, bind = uline.split("//")
  130. if (bind.find("attrib:") != -1):
  131. name = name.strip()
  132. bind = bind.replace("attrib:", "").strip()
  133. attributes += [(name, bind)]
  134. if (line.strip().find("out ") == 0):
  135. uline = line.replace("out", "").strip()
  136. uline = uline.replace(";", "")
  137. uline = uline[uline.find(" "):].strip()
  138. if (uline.find("//") != -1):
  139. name, bind = uline.split("//")
  140. if (bind.find("drawbuffer:") != -1):
  141. name = name.strip()
  142. bind = bind.replace("drawbuffer:", "").strip()
  143. fbos += [(name, bind)]
  144. line = line.replace("\r", "")
  145. line = line.replace("\n", "")
  146. line = line.replace("\\", "\\\\")
  147. line = line.replace("\"", "\\\"")
  148. # line=line+"\\n\\" no need to anymore
  149. if (reading == "vertex"):
  150. vertex_lines += [line]
  151. if (reading == "fragment"):
  152. fragment_lines += [line]
  153. line = fs.readline()
  154. line_offset += 1
  155. fs.close()
  156. out_file = filename + ".h"
  157. fd = open(out_file, "w")
  158. fd.write("/* WARNING, THIS FILE WAS GENERATED, DO NOT EDIT */\n")
  159. out_file_base = out_file
  160. out_file_base = out_file_base[out_file_base.rfind("/") + 1:]
  161. out_file_base = out_file_base[out_file_base.rfind("\\") + 1:]
  162. # print("out file "+out_file+" base " +out_file_base)
  163. out_file_ifdef = out_file_base.replace(".", "_").upper()
  164. fd.write("#ifndef " + out_file_ifdef + "\n")
  165. fd.write("#define " + out_file_ifdef + "\n")
  166. out_file_class = out_file_base.replace(".glsl.h", "").title().replace("_", "").replace(".", "") + "ShaderGL"
  167. fd.write("\n\n")
  168. fd.write("#include \"drivers/opengl/shader_gl.h\"\n\n\n")
  169. fd.write("class " + out_file_class + " : public ShaderGL {\n\n")
  170. fd.write("\t virtual String get_shader_name() const { return \"" + out_file_class + "\"; }\n")
  171. fd.write("public:\n\n")
  172. if (len(conditionals)):
  173. fd.write("\tenum Conditionals {\n")
  174. for x in conditionals:
  175. fd.write("\t\t" + x + ",\n")
  176. fd.write("\t};\n\n")
  177. if (len(uniforms)):
  178. fd.write("\tenum Uniforms {\n")
  179. for x in uniforms:
  180. fd.write("\t\t" + x.upper() + ",\n")
  181. fd.write("\t};\n\n")
  182. fd.write("\t_FORCE_INLINE_ int get_uniform(Uniforms p_uniform) const { return _get_uniform(p_uniform); }\n\n")
  183. if (len(conditionals)):
  184. fd.write("\t_FORCE_INLINE_ void set_conditional(Conditionals p_conditional,bool p_enable) { _set_conditional(p_conditional,p_enable); }\n\n")
  185. fd.write("\t#define _FU if (get_uniform(p_uniform)<0) return; ERR_FAIL_COND( get_active()!=this );\n\n ")
  186. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, float p_value) { _FU glUniform1f(get_uniform(p_uniform),p_value); }\n\n")
  187. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, double p_value) { _FU glUniform1f(get_uniform(p_uniform),p_value); }\n\n")
  188. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, uint8_t p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n")
  189. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, int8_t p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n")
  190. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, uint16_t p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n")
  191. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, int16_t p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n")
  192. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, uint32_t p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n")
  193. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, int32_t p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n")
  194. #fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, uint64_t p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n");
  195. #fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, int64_t p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n");
  196. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, unsigned long p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n")
  197. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, long p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n")
  198. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Color& p_color) { _FU GLfloat col[4]={p_color.r,p_color.g,p_color.b,p_color.a}; glUniform4fv(get_uniform(p_uniform),1,col); }\n\n")
  199. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Vector2& p_vec2) { _FU GLfloat vec2[2]={p_vec2.x,p_vec2.y}; glUniform2fv(get_uniform(p_uniform),1,vec2); }\n\n")
  200. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Vector3& p_vec3) { _FU GLfloat vec3[3]={p_vec3.x,p_vec3.y,p_vec3.z}; glUniform3fv(get_uniform(p_uniform),1,vec3); }\n\n")
  201. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, float p_a, float p_b) { _FU glUniform2f(get_uniform(p_uniform),p_a,p_b); }\n\n")
  202. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, float p_a, float p_b, float p_c) { _FU glUniform3f(get_uniform(p_uniform),p_a,p_b,p_c); }\n\n")
  203. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, float p_a, float p_b, float p_c, float p_d) { _FU glUniform4f(get_uniform(p_uniform),p_a,p_b,p_c,p_d); }\n\n")
  204. fd.write("""\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Transform& p_transform) { _FU
  205. const Transform &tr = p_transform;
  206. GLfloat matrix[16]={ /* build a 16x16 matrix */
  207. tr.basis.elements[0][0],
  208. tr.basis.elements[1][0],
  209. tr.basis.elements[2][0],
  210. 0,
  211. tr.basis.elements[0][1],
  212. tr.basis.elements[1][1],
  213. tr.basis.elements[2][1],
  214. 0,
  215. tr.basis.elements[0][2],
  216. tr.basis.elements[1][2],
  217. tr.basis.elements[2][2],
  218. 0,
  219. tr.origin.x,
  220. tr.origin.y,
  221. tr.origin.z,
  222. 1
  223. };
  224. glUniformMatrix4fv(get_uniform(p_uniform),1,false,matrix);
  225. }
  226. """)
  227. fd.write("""\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Matrix32& p_transform) { _FU
  228. const Matrix32 &tr = p_transform;
  229. GLfloat matrix[16]={ /* build a 16x16 matrix */
  230. tr.elements[0][0],
  231. tr.elements[0][1],
  232. 0,
  233. 0,
  234. tr.elements[1][0],
  235. tr.elements[1][1],
  236. 0,
  237. 0,
  238. 0,
  239. 0,
  240. 1,
  241. 0,
  242. tr.elements[2][0],
  243. tr.elements[2][1],
  244. 0,
  245. 1
  246. };
  247. glUniformMatrix4fv(get_uniform(p_uniform),1,false,matrix);
  248. }
  249. """)
  250. fd.write("""\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const CameraMatrix& p_matrix) { _FU
  251. GLfloat matrix[16];
  252. for (int i=0;i<4;i++) {
  253. for (int j=0;j<4;j++) {
  254. matrix[i*4+j]=p_matrix.matrix[i][j];
  255. }
  256. }
  257. glUniformMatrix4fv(get_uniform(p_uniform),1,false,matrix);
  258. }; """)
  259. fd.write("\n\n#undef _FU\n\n\n")
  260. fd.write("\tvirtual void init() {\n\n")
  261. if (len(conditionals)):
  262. fd.write("\t\tstatic const char* _conditional_strings[]={\n")
  263. if (len(conditionals)):
  264. for x in conditionals:
  265. fd.write("\t\t\t\"#define " + x + "\\n\",\n")
  266. fd.write("\t\t};\n\n")
  267. else:
  268. fd.write("\t\tstatic const char **_conditional_strings=NULL;\n")
  269. if (len(uniforms)):
  270. fd.write("\t\tstatic const char* _uniform_strings[]={\n")
  271. if (len(uniforms)):
  272. for x in uniforms:
  273. fd.write("\t\t\t\"" + x + "\",\n")
  274. fd.write("\t\t};\n\n")
  275. else:
  276. fd.write("\t\tstatic const char **_uniform_strings=NULL;\n")
  277. if (len(attributes)):
  278. fd.write("\t\tstatic AttributePair _attribute_pairs[]={\n")
  279. for x in attributes:
  280. fd.write("\t\t\t{\"" + x[0] + "\"," + x[1] + "},\n")
  281. fd.write("\t\t};\n\n")
  282. else:
  283. fd.write("\t\tstatic AttributePair *_attribute_pairs=NULL;\n")
  284. if (len(fbos)):
  285. fd.write("\t\tstatic FBOPair _fbo_pairs[]={\n")
  286. for x in fbos:
  287. fd.write("\t\t\t{\"" + x[0] + "\"," + x[1] + "},\n")
  288. fd.write("\t\t};\n\n")
  289. else:
  290. fd.write("\t\tstatic FBOPair *_fbo_pairs=NULL;\n")
  291. if (len(ubos)):
  292. fd.write("\t\tstatic UBOPair _ubo_pairs[]={\n")
  293. for x in ubos:
  294. fd.write("\t\t\t{\"" + x[0] + "\"," + x[1] + "},\n")
  295. fd.write("\t\t};\n\n")
  296. else:
  297. fd.write("\t\tstatic UBOPair *_ubo_pairs=NULL;\n")
  298. if (len(texunits)):
  299. fd.write("\t\tstatic TexUnitPair _texunit_pairs[]={\n")
  300. for x in texunits:
  301. fd.write("\t\t\t{\"" + x[0] + "\"," + x[1] + "},\n")
  302. fd.write("\t\t};\n\n")
  303. else:
  304. fd.write("\t\tstatic TexUnitPair *_texunit_pairs=NULL;\n")
  305. fd.write("\t\tstatic const char* _vertex_code=\"\\\n")
  306. for x in vertex_lines:
  307. fd.write("\t\t\t" + x + "\n")
  308. fd.write("\t\t\";\n\n")
  309. fd.write("\t\tstatic const int _vertex_code_start=" + str(vertex_offset) + ";\n")
  310. fd.write("\t\tstatic const char* _fragment_code=\"\\\n")
  311. for x in fragment_lines:
  312. fd.write("\t\t\t" + x + "\n")
  313. fd.write("\t\t\";\n\n")
  314. fd.write("\t\tstatic const int _fragment_code_start=" + str(fragment_offset) + ";\n")
  315. fd.write("\t\tsetup(_conditional_strings," + str(len(conditionals)) + ",_uniform_strings," + str(len(uniforms)) + ",_attribute_pairs," + str(len(attributes)) + ",_fbo_pairs," + str(len(fbos)) + ",_ubo_pairs," + str(len(ubos)) + ",_texunit_pairs," + str(len(texunits)) + ",_vertex_code,_fragment_code,_vertex_code_start,_fragment_code_start);\n")
  316. fd.write("\t};\n\n")
  317. fd.write("};\n\n")
  318. fd.write("#endif\n\n")
  319. fd.close()
  320. def build_glsl_headers(target, source, env):
  321. for x in source:
  322. build_glsl_header(str(x))
  323. return 0
  324. def build_hlsl_dx9_header(filename):
  325. fs = open(filename, "r")
  326. line = fs.readline()
  327. vertex_lines = []
  328. fragment_lines = []
  329. uniforms = []
  330. fragment_uniforms = []
  331. attributes = []
  332. fbos = []
  333. conditionals = []
  334. reading = ""
  335. line_offset = 0
  336. vertex_offset = 0
  337. fragment_offset = 0
  338. while(line):
  339. if (line.find("[vertex]") != -1):
  340. reading = "vertex"
  341. line = fs.readline()
  342. line_offset += 1
  343. vertex_offset = line_offset
  344. continue
  345. if (line.find("[fragment]") != -1):
  346. reading = "fragment"
  347. line = fs.readline()
  348. line_offset += 1
  349. fragment_offset = line_offset
  350. continue
  351. if (line.find("#ifdef ") != -1):
  352. ifdefline = line.replace("#ifdef ", "").strip()
  353. if (not ifdefline in conditionals):
  354. conditionals += [ifdefline]
  355. if (line.find("#elif defined(") != -1):
  356. ifdefline = line.replace("#elif defined(", "").strip()
  357. ifdefline = ifdefline.replace(")", "").strip()
  358. if (not ifdefline in conditionals):
  359. conditionals += [ifdefline]
  360. if (line.find("uniform") != -1):
  361. uline = line.replace("uniform", "")
  362. uline = uline.replace(";", "")
  363. lines = uline.split(",")
  364. for x in lines:
  365. x = x.strip()
  366. x = x[x.rfind(" ") + 1:]
  367. if (x.find("[") != -1):
  368. # unfiorm array
  369. x = x[:x.find("[")]
  370. if (not x in uniforms):
  371. uniforms += [x]
  372. fragment_uniforms += [reading == "fragment"]
  373. line = line.replace("\r", "")
  374. line = line.replace("\n", "")
  375. line = line.replace("\\", "\\\\")
  376. line = line.replace("\"", "\\\"")
  377. line = line + "\\n\\"
  378. if (reading == "vertex"):
  379. vertex_lines += [line]
  380. if (reading == "fragment"):
  381. fragment_lines += [line]
  382. line = fs.readline()
  383. line_offset += 1
  384. fs.close()
  385. out_file = filename + ".h"
  386. fd = open(out_file, "w")
  387. fd.write("/* WARNING, THIS FILE WAS GENERATED, DO NOT EDIT */\n")
  388. out_file_base = out_file
  389. out_file_base = out_file_base[out_file_base.rfind("/") + 1:]
  390. out_file_base = out_file_base[out_file_base.rfind("\\") + 1:]
  391. # print("out file "+out_file+" base " +out_file_base)
  392. out_file_ifdef = out_file_base.replace(".", "_").upper()
  393. fd.write("#ifndef " + out_file_ifdef + "\n")
  394. fd.write("#define " + out_file_ifdef + "\n")
  395. out_file_class = out_file_base.replace(".hlsl.h", "").title().replace("_", "").replace(".", "") + "ShaderDX9"
  396. fd.write("\n\n")
  397. fd.write("#include \"drivers/directx9/shader_dx9.h\"\n\n\n")
  398. fd.write("class " + out_file_class + " : public ShaderDX9 {\n\n")
  399. fd.write("\t virtual String get_shader_name() const { return \"" + out_file_class + "\"; }\n")
  400. fd.write("public:\n\n")
  401. if (len(conditionals)):
  402. fd.write("\tenum Conditionals {\n")
  403. for x in conditionals:
  404. fd.write("\t\t" + x + ",\n")
  405. fd.write("\t};\n\n")
  406. if (len(uniforms)):
  407. fd.write("\tenum Uniforms {\n")
  408. for x in uniforms:
  409. fd.write("\t\t" + x.upper() + ",\n")
  410. fd.write("\t};\n\n")
  411. if (len(conditionals)):
  412. fd.write("\t_FORCE_INLINE_ void set_conditional(Conditionals p_conditional,bool p_enable) { _set_conditional(p_conditional,p_enable); }\n\n")
  413. fd.write("\t#define _FU if (!_uniform_valid(p_uniform)) return; ERR_FAIL_COND( get_active()!=this );\n\n ")
  414. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, bool p_value) { _FU set_uniformb(p_uniform,p_value); }\n\n")
  415. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, float p_value) { _FU set_uniformf(p_uniform,p_value); }\n\n")
  416. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, double p_value) { _FU set_uniformf(p_uniform,p_value); }\n\n")
  417. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, uint8_t p_value) { _FU set_uniformi(p_uniform,p_value); }\n\n")
  418. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, int8_t p_value) { _FU set_uniformi(p_uniform,p_value); }\n\n")
  419. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, uint16_t p_value) { _FU set_uniformi(p_uniform,p_value); }\n\n")
  420. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, int16_t p_value) { _FU set_uniformi(p_uniform,p_value); }\n\n")
  421. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, uint32_t p_value) { _FU set_uniformi(p_uniform,p_value); }\n\n")
  422. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, int32_t p_value) { _FU set_uniformi(p_uniform,p_value); }\n\n")
  423. #fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, uint64_t p_value) { _FU set_uniformi(p_uniform,p_value); }\n\n");
  424. #fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, int64_t p_value) { _FU set_uniformi(p_uniform,p_value); }\n\n");
  425. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, unsigned long p_value) { _FU set_uniformi(p_uniform,p_value); }\n\n")
  426. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, long p_value) { _FU set_uniformi(p_uniform,p_value); }\n\n")
  427. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Color& p_color) { _FU float col[4]={p_color.r,p_color.g,p_color.b,p_color.a}; set_uniformfv(p_uniform,col); }\n\n")
  428. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Vector2& p_vec2) { _FU float vec2[4]={p_vec2.x,p_vec2.y,0,0}; set_uniformfv(p_uniform,vec2); }\n\n")
  429. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Vector3& p_vec3) { _FU float vec3[4]={p_vec3.x,p_vec3.y,p_vec3.z,0}; set_uniformfv(p_uniform,vec3); }\n\n")
  430. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, float p_a, float p_b) { _FU float vec2[4]={p_a,p_b,0,0}; set_uniformfv(p_uniform,vec2); }\n\n")
  431. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, float p_a, float p_b, float p_c) { _FU float vec3[4]={p_a,p_b,p_c,0}; set_uniformfv(p_uniform,vec3); }\n\n")
  432. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, float p_a, float p_b, float p_c, float p_d) { _FU float vec4[4]={p_a,p_b,p_c,p_d}; set_uniformfv(p_uniform,vec4); }\n\n")
  433. fd.write("""\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Transform& p_transform) { _FU
  434. const Transform &tr = p_transform;
  435. float matrix[16]={ /* build a 16x16 matrix */
  436. tr.basis.elements[0][0],
  437. tr.basis.elements[0][1],
  438. tr.basis.elements[0][2],
  439. tr.origin.x,
  440. tr.basis.elements[1][0],
  441. tr.basis.elements[1][1],
  442. tr.basis.elements[1][2],
  443. tr.origin.y,
  444. tr.basis.elements[2][0],
  445. tr.basis.elements[2][1],
  446. tr.basis.elements[2][2],
  447. tr.origin.z,
  448. 0,
  449. 0,
  450. 0,
  451. 1
  452. };
  453. set_uniformfv(p_uniform,&matrix[0],4);
  454. }
  455. """)
  456. fd.write("""\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const CameraMatrix& p_matrix) { _FU
  457. float matrix[16];
  458. for (int i=0;i<4;i++) {
  459. for (int j=0;j<4;j++) {
  460. matrix[i*4+j]=p_matrix.matrix[j][i];
  461. }
  462. }
  463. set_uniformfv(p_uniform,&matrix[0],4);
  464. }; """)
  465. fd.write("\n\n#undef _FU\n\n\n")
  466. fd.write("\tvirtual void init(IDirect3DDevice9 *p_device,ShaderSupport p_version) {\n\n")
  467. if (len(conditionals)):
  468. fd.write("\t\tstatic const char* _conditional_strings[]={\n")
  469. if (len(conditionals)):
  470. for x in conditionals:
  471. fd.write("\t\t\t\"" + x + "\",\n")
  472. fd.write("\t\t};\n\n")
  473. else:
  474. fd.write("\t\tstatic const char **_conditional_strings=NULL;\n")
  475. if (len(uniforms)):
  476. fd.write("\t\tstatic const char* _uniform_strings[]={\n")
  477. if (len(uniforms)):
  478. for x in uniforms:
  479. fd.write("\t\t\t\"" + x + "\",\n")
  480. fd.write("\t\t};\n\n")
  481. fd.write("\t\tstatic const bool _fragment_uniforms[]={\n")
  482. if (len(uniforms)):
  483. for x in fragment_uniforms:
  484. if (x):
  485. fd.write("\t\t\ttrue,\n")
  486. else:
  487. fd.write("\t\t\tfalse,\n")
  488. fd.write("\t\t};\n\n")
  489. else:
  490. fd.write("\t\tstatic const char **_uniform_strings=NULL;\n")
  491. fd.write("\t\tstatic const bool *_fragment_uniforms=NULL;\n")
  492. fd.write("\t\tstatic const char* _vertex_code=\"\\\n")
  493. for x in vertex_lines:
  494. fd.write("\t\t\t" + x + "\n")
  495. fd.write("\t\t\";\n\n")
  496. fd.write("\t\tstatic const int _vertex_code_start=" + str(vertex_offset) + ";\n")
  497. fd.write("\t\tstatic const char* _fragment_code=\"\\\n")
  498. for x in fragment_lines:
  499. fd.write("\t\t\t" + x + "\n")
  500. fd.write("\t\t\";\n\n")
  501. fd.write("\t\tstatic const int _fragment_code_start=" + str(fragment_offset) + ";\n")
  502. fd.write("\t\tsetup(p_device,p_version,_conditional_strings," + str(len(conditionals)) + ",_uniform_strings," + str(len(uniforms)) + ",_fragment_uniforms,_vertex_code,_fragment_code,_vertex_code_start,_fragment_code_start);\n")
  503. fd.write("\t};\n\n")
  504. fd.write("};\n\n")
  505. fd.write("#endif\n\n")
  506. fd.close()
  507. def build_hlsl_dx9_headers(target, source, env):
  508. for x in source:
  509. build_hlsl_dx9_header(str(x))
  510. return 0
  511. class LegacyGLHeaderStruct:
  512. def __init__(self):
  513. self.vertex_lines = []
  514. self.fragment_lines = []
  515. self.uniforms = []
  516. self.attributes = []
  517. self.fbos = []
  518. self.conditionals = []
  519. self.enums = {}
  520. self.texunits = []
  521. self.texunit_names = []
  522. self.ubos = []
  523. self.ubo_names = []
  524. self.vertex_included_files = []
  525. self.fragment_included_files = []
  526. self.reading = ""
  527. self.line_offset = 0
  528. self.vertex_offset = 0
  529. self.fragment_offset = 0
  530. def include_file_in_legacygl_header(filename, header_data, depth):
  531. fs = open(filename, "r")
  532. line = fs.readline()
  533. while(line):
  534. if (line.find("[vertex]") != -1):
  535. header_data.reading = "vertex"
  536. line = fs.readline()
  537. header_data.line_offset += 1
  538. header_data.vertex_offset = header_data.line_offset
  539. continue
  540. if (line.find("[fragment]") != -1):
  541. header_data.reading = "fragment"
  542. line = fs.readline()
  543. header_data.line_offset += 1
  544. header_data.fragment_offset = header_data.line_offset
  545. continue
  546. while(line.find("#include ") != -1):
  547. includeline = line.replace("#include ", "").strip()[1:-1]
  548. import os.path
  549. included_file = os.path.relpath(os.path.dirname(filename) + "/" + includeline)
  550. if (not included_file in header_data.vertex_included_files and header_data.reading == "vertex"):
  551. header_data.vertex_included_files += [included_file]
  552. if(include_file_in_legacygl_header(included_file, header_data, depth + 1) == None):
  553. print "Error in file '" + filename + "': #include " + includeline + "could not be found!"
  554. elif (not included_file in header_data.fragment_included_files and header_data.reading == "fragment"):
  555. header_data.fragment_included_files += [included_file]
  556. if(include_file_in_legacygl_header(included_file, header_data, depth + 1) == None):
  557. print "Error in file '" + filename + "': #include " + includeline + "could not be found!"
  558. line = fs.readline()
  559. if (line.find("#ifdef ") != -1 or line.find("#elif defined(") != -1):
  560. if (line.find("#ifdef ") != -1):
  561. ifdefline = line.replace("#ifdef ", "").strip()
  562. else:
  563. ifdefline = line.replace("#elif defined(", "").strip()
  564. ifdefline = ifdefline.replace(")", "").strip()
  565. if (line.find("_EN_") != -1):
  566. enumbase = ifdefline[:ifdefline.find("_EN_")]
  567. ifdefline = ifdefline.replace("_EN_", "_")
  568. line = line.replace("_EN_", "_")
  569. # print(enumbase+":"+ifdefline);
  570. if (enumbase not in header_data.enums):
  571. header_data.enums[enumbase] = []
  572. if (ifdefline not in header_data.enums[enumbase]):
  573. header_data.enums[enumbase].append(ifdefline)
  574. elif (not ifdefline in header_data.conditionals):
  575. header_data.conditionals += [ifdefline]
  576. if (line.find("uniform") != -1 and line.lower().find("texunit:") != -1):
  577. # texture unit
  578. texunitstr = line[line.find(":") + 1:].strip()
  579. if (texunitstr == "auto"):
  580. texunit = "-1"
  581. else:
  582. texunit = str(int(texunitstr))
  583. uline = line[:line.lower().find("//")]
  584. uline = uline.replace("uniform", "")
  585. uline = uline.replace("highp", "")
  586. uline = uline.replace(";", "")
  587. lines = uline.split(",")
  588. for x in lines:
  589. x = x.strip()
  590. x = x[x.rfind(" ") + 1:]
  591. if (x.find("[") != -1):
  592. # unfiorm array
  593. x = x[:x.find("[")]
  594. if (not x in header_data.texunit_names):
  595. header_data.texunits += [(x, texunit)]
  596. header_data.texunit_names += [x]
  597. elif (line.find("uniform") != -1):
  598. uline = line.replace("uniform", "")
  599. uline = uline.replace(";", "")
  600. lines = uline.split(",")
  601. for x in lines:
  602. x = x.strip()
  603. x = x[x.rfind(" ") + 1:]
  604. if (x.find("[") != -1):
  605. # unfiorm array
  606. x = x[:x.find("[")]
  607. if (not x in header_data.uniforms):
  608. header_data.uniforms += [x]
  609. if ((line.strip().find("in ") == 0 or line.strip().find("attribute ") == 0) and line.find("attrib:") != -1):
  610. uline = line.replace("in ", "")
  611. uline = uline.replace("attribute ", "")
  612. uline = uline.replace("highp ", "")
  613. uline = uline.replace(";", "")
  614. uline = uline[uline.find(" "):].strip()
  615. if (uline.find("//") != -1):
  616. name, bind = uline.split("//")
  617. if (bind.find("attrib:") != -1):
  618. name = name.strip()
  619. bind = bind.replace("attrib:", "").strip()
  620. header_data.attributes += [(name, bind)]
  621. line = line.replace("\r", "")
  622. line = line.replace("\n", "")
  623. # line=line.replace("\\","\\\\")
  624. # line=line.replace("\"","\\\"")
  625. # line=line+"\\n\\"
  626. if (header_data.reading == "vertex"):
  627. header_data.vertex_lines += [line]
  628. if (header_data.reading == "fragment"):
  629. header_data.fragment_lines += [line]
  630. line = fs.readline()
  631. header_data.line_offset += 1
  632. fs.close()
  633. return header_data
  634. def build_legacygl_header(filename, include, class_suffix, output_attribs):
  635. header_data = LegacyGLHeaderStruct()
  636. include_file_in_legacygl_header(filename, header_data, 0)
  637. out_file = filename + ".h"
  638. fd = open(out_file, "w")
  639. enum_constants = []
  640. fd.write("/* WARNING, THIS FILE WAS GENERATED, DO NOT EDIT */\n")
  641. out_file_base = out_file
  642. out_file_base = out_file_base[out_file_base.rfind("/") + 1:]
  643. out_file_base = out_file_base[out_file_base.rfind("\\") + 1:]
  644. # print("out file "+out_file+" base " +out_file_base)
  645. out_file_ifdef = out_file_base.replace(".", "_").upper()
  646. fd.write("#ifndef " + out_file_ifdef + class_suffix + "_120\n")
  647. fd.write("#define " + out_file_ifdef + class_suffix + "_120\n")
  648. out_file_class = out_file_base.replace(".glsl.h", "").title().replace("_", "").replace(".", "") + "Shader" + class_suffix
  649. fd.write("\n\n")
  650. fd.write("#include \"" + include + "\"\n\n\n")
  651. fd.write("class " + out_file_class + " : public Shader" + class_suffix + " {\n\n")
  652. fd.write("\t virtual String get_shader_name() const { return \"" + out_file_class + "\"; }\n")
  653. fd.write("public:\n\n")
  654. if (len(header_data.conditionals)):
  655. fd.write("\tenum Conditionals {\n")
  656. for x in header_data.conditionals:
  657. fd.write("\t\t" + x.upper() + ",\n")
  658. fd.write("\t};\n\n")
  659. if (len(header_data.uniforms)):
  660. fd.write("\tenum Uniforms {\n")
  661. for x in header_data.uniforms:
  662. fd.write("\t\t" + x.upper() + ",\n")
  663. fd.write("\t};\n\n")
  664. fd.write("\t_FORCE_INLINE_ int get_uniform(Uniforms p_uniform) const { return _get_uniform(p_uniform); }\n\n")
  665. if (len(header_data.conditionals)):
  666. fd.write("\t_FORCE_INLINE_ void set_conditional(Conditionals p_conditional,bool p_enable) { _set_conditional(p_conditional,p_enable); }\n\n")
  667. fd.write("\t#define _FU if (get_uniform(p_uniform)<0) return; ERR_FAIL_COND( get_active()!=this );\n\n ")
  668. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, float p_value) { _FU glUniform1f(get_uniform(p_uniform),p_value); }\n\n")
  669. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, double p_value) { _FU glUniform1f(get_uniform(p_uniform),p_value); }\n\n")
  670. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, uint8_t p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n")
  671. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, int8_t p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n")
  672. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, uint16_t p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n")
  673. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, int16_t p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n")
  674. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, uint32_t p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n")
  675. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, int32_t p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n")
  676. #fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, uint64_t p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n");
  677. #fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, int64_t p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n");
  678. #fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, unsigned long p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n");
  679. #fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, long p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n");
  680. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Color& p_color) { _FU GLfloat col[4]={p_color.r,p_color.g,p_color.b,p_color.a}; glUniform4fv(get_uniform(p_uniform),1,col); }\n\n")
  681. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Vector2& p_vec2) { _FU GLfloat vec2[2]={p_vec2.x,p_vec2.y}; glUniform2fv(get_uniform(p_uniform),1,vec2); }\n\n")
  682. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Vector3& p_vec3) { _FU GLfloat vec3[3]={p_vec3.x,p_vec3.y,p_vec3.z}; glUniform3fv(get_uniform(p_uniform),1,vec3); }\n\n")
  683. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, float p_a, float p_b) { _FU glUniform2f(get_uniform(p_uniform),p_a,p_b); }\n\n")
  684. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, float p_a, float p_b, float p_c) { _FU glUniform3f(get_uniform(p_uniform),p_a,p_b,p_c); }\n\n")
  685. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, float p_a, float p_b, float p_c, float p_d) { _FU glUniform4f(get_uniform(p_uniform),p_a,p_b,p_c,p_d); }\n\n")
  686. fd.write("""\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Transform& p_transform) { _FU
  687. const Transform &tr = p_transform;
  688. GLfloat matrix[16]={ /* build a 16x16 matrix */
  689. tr.basis.elements[0][0],
  690. tr.basis.elements[1][0],
  691. tr.basis.elements[2][0],
  692. 0,
  693. tr.basis.elements[0][1],
  694. tr.basis.elements[1][1],
  695. tr.basis.elements[2][1],
  696. 0,
  697. tr.basis.elements[0][2],
  698. tr.basis.elements[1][2],
  699. tr.basis.elements[2][2],
  700. 0,
  701. tr.origin.x,
  702. tr.origin.y,
  703. tr.origin.z,
  704. 1
  705. };
  706. glUniformMatrix4fv(get_uniform(p_uniform),1,false,matrix);
  707. }
  708. """)
  709. fd.write("""\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Matrix32& p_transform) { _FU
  710. const Matrix32 &tr = p_transform;
  711. GLfloat matrix[16]={ /* build a 16x16 matrix */
  712. tr.elements[0][0],
  713. tr.elements[0][1],
  714. 0,
  715. 0,
  716. tr.elements[1][0],
  717. tr.elements[1][1],
  718. 0,
  719. 0,
  720. 0,
  721. 0,
  722. 1,
  723. 0,
  724. tr.elements[2][0],
  725. tr.elements[2][1],
  726. 0,
  727. 1
  728. };
  729. glUniformMatrix4fv(get_uniform(p_uniform),1,false,matrix);
  730. }
  731. """)
  732. fd.write("""\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const CameraMatrix& p_matrix) { _FU
  733. GLfloat matrix[16];
  734. for (int i=0;i<4;i++) {
  735. for (int j=0;j<4;j++) {
  736. matrix[i*4+j]=p_matrix.matrix[i][j];
  737. }
  738. }
  739. glUniformMatrix4fv(get_uniform(p_uniform),1,false,matrix);
  740. }; """)
  741. fd.write("\n\n#undef _FU\n\n\n")
  742. fd.write("\tvirtual void init() {\n\n")
  743. enum_value_count = 0
  744. if (len(header_data.enums)):
  745. fd.write("\t\t//Written using math, given nonstandarity of 64 bits integer constants..\n")
  746. fd.write("\t\tstatic const Enum _enums[]={\n")
  747. bitofs = len(header_data.conditionals)
  748. enum_vals = []
  749. for xv in header_data.enums:
  750. x = header_data.enums[xv]
  751. bits = 1
  752. amt = len(x)
  753. # print(x)
  754. while(2**bits < amt):
  755. bits += 1
  756. # print("amount: "+str(amt)+" bits "+str(bits));
  757. strs = "{"
  758. for i in range(amt):
  759. strs += "\"#define " + x[i] + "\\n\","
  760. v = {}
  761. v["set_mask"] = "uint64_t(" + str(i) + ")<<" + str(bitofs)
  762. v["clear_mask"] = "((uint64_t(1)<<40)-1) ^ (((uint64_t(1)<<" + str(bits) + ") - 1)<<" + str(bitofs) + ")"
  763. enum_vals.append(v)
  764. enum_constants.append(x[i])
  765. strs += "NULL}"
  766. fd.write("\t\t\t{(uint64_t(1<<" + str(bits) + ")-1)<<" + str(bitofs) + "," + str(bitofs) + "," + strs + "},\n")
  767. bitofs += bits
  768. fd.write("\t\t};\n\n")
  769. fd.write("\t\tstatic const EnumValue _enum_values[]={\n")
  770. enum_value_count = len(enum_vals)
  771. for x in enum_vals:
  772. fd.write("\t\t\t{" + x["set_mask"] + "," + x["clear_mask"] + "},\n")
  773. fd.write("\t\t};\n\n")
  774. else:
  775. fd.write("\t\tstatic const Enum *_enums=NULL;\n")
  776. fd.write("\t\tstatic const EnumValue *_enum_values=NULL;\n")
  777. if (len(header_data.conditionals)):
  778. fd.write("\t\tstatic const char* _conditional_strings[]={\n")
  779. if (len(header_data.conditionals)):
  780. for x in header_data.conditionals:
  781. fd.write("\t\t\t\"#define " + x + "\\n\",\n")
  782. fd.write("\t\t};\n\n")
  783. else:
  784. fd.write("\t\tstatic const char **_conditional_strings=NULL;\n")
  785. if (len(header_data.uniforms)):
  786. fd.write("\t\tstatic const char* _uniform_strings[]={\n")
  787. if (len(header_data.uniforms)):
  788. for x in header_data.uniforms:
  789. fd.write("\t\t\t\"" + x + "\",\n")
  790. fd.write("\t\t};\n\n")
  791. else:
  792. fd.write("\t\tstatic const char **_uniform_strings=NULL;\n")
  793. if output_attribs:
  794. if (len(header_data.attributes)):
  795. fd.write("\t\tstatic AttributePair _attribute_pairs[]={\n")
  796. for x in header_data.attributes:
  797. fd.write("\t\t\t{\"" + x[0] + "\"," + x[1] + "},\n")
  798. fd.write("\t\t};\n\n")
  799. else:
  800. fd.write("\t\tstatic AttributePair *_attribute_pairs=NULL;\n")
  801. if (len(header_data.texunits)):
  802. fd.write("\t\tstatic TexUnitPair _texunit_pairs[]={\n")
  803. for x in header_data.texunits:
  804. fd.write("\t\t\t{\"" + x[0] + "\"," + x[1] + "},\n")
  805. fd.write("\t\t};\n\n")
  806. else:
  807. fd.write("\t\tstatic TexUnitPair *_texunit_pairs=NULL;\n")
  808. fd.write("\t\tstatic const char _vertex_code[]={\n")
  809. for x in header_data.vertex_lines:
  810. for i in range(len(x)):
  811. fd.write(str(ord(x[i])) + ",")
  812. fd.write(str(ord('\n')) + ",")
  813. fd.write("\t\t0};\n\n")
  814. fd.write("\t\tstatic const int _vertex_code_start=" + str(header_data.vertex_offset) + ";\n")
  815. fd.write("\t\tstatic const char _fragment_code[]={\n")
  816. for x in header_data.fragment_lines:
  817. for i in range(len(x)):
  818. fd.write(str(ord(x[i])) + ",")
  819. fd.write(str(ord('\n')) + ",")
  820. fd.write("\t\t0};\n\n")
  821. fd.write("\t\tstatic const int _fragment_code_start=" + str(header_data.fragment_offset) + ";\n")
  822. if output_attribs:
  823. fd.write("\t\tsetup(_conditional_strings," + str(len(header_data.conditionals)) + ",_uniform_strings," + str(len(header_data.uniforms)) + ",_attribute_pairs," + str(len(header_data.attributes)) + ", _texunit_pairs," + str(len(header_data.texunits)) + ",_vertex_code,_fragment_code,_vertex_code_start,_fragment_code_start);\n")
  824. else:
  825. fd.write("\t\tsetup(_conditional_strings," + str(len(header_data.conditionals)) + ",_uniform_strings," + str(len(header_data.uniforms)) + ",_texunit_pairs," + str(len(header_data.texunits)) + ",_enums," + str(len(header_data.enums)) + ",_enum_values," + str(enum_value_count) + ",_vertex_code,_fragment_code,_vertex_code_start,_fragment_code_start);\n")
  826. fd.write("\t};\n\n")
  827. if (len(enum_constants)):
  828. fd.write("\tenum EnumConditionals {\n")
  829. for x in enum_constants:
  830. fd.write("\t\t" + x.upper() + ",\n")
  831. fd.write("\t};\n\n")
  832. fd.write("\tvoid set_enum_conditional(EnumConditionals p_cond) { _set_enum_conditional(p_cond); }\n")
  833. fd.write("};\n\n")
  834. fd.write("#endif\n\n")
  835. fd.close()
  836. def build_legacygl_headers(target, source, env):
  837. for x in source:
  838. build_legacygl_header(str(x), include="drivers/legacygl/shader_lgl.h", class_suffix="LGL", output_attribs=False)
  839. return 0
  840. def build_gles2_headers(target, source, env):
  841. for x in source:
  842. build_legacygl_header(str(x), include="drivers/gles2/shader_gles2.h", class_suffix="GLES2", output_attribs=True)
  843. def update_version():
  844. rev = "custom_build"
  845. if (os.getenv("BUILD_REVISION") != None):
  846. rev = os.getenv("BUILD_REVISION")
  847. print("Using custom revision: " + rev)
  848. import version
  849. f = open("core/version.h", "wb")
  850. f.write("#define VERSION_SHORT_NAME " + str(version.short_name) + "\n")
  851. f.write("#define VERSION_NAME " + str(version.name) + "\n")
  852. f.write("#define VERSION_MAJOR " + str(version.major) + "\n")
  853. f.write("#define VERSION_MINOR " + str(version.minor) + "\n")
  854. if (hasattr(version, 'patch')):
  855. f.write("#define VERSION_PATCH " + str(version.patch) + "\n")
  856. f.write("#define VERSION_REVISION " + str(rev) + "\n")
  857. f.write("#define VERSION_STATUS " + str(version.status) + "\n")
  858. import datetime
  859. f.write("#define VERSION_YEAR " + str(datetime.datetime.now().year) + "\n")
  860. def parse_cg_file(fname, uniforms, sizes, conditionals):
  861. import re
  862. fs = open(fname, "r")
  863. line = fs.readline()
  864. while line:
  865. if re.match(r"^\s*uniform", line):
  866. res = re.match(r"uniform ([\d\w]*) ([\d\w]*)")
  867. type = res.groups(1)
  868. name = res.groups(2)
  869. uniforms.append(name)
  870. if (type.find("texobj") != -1):
  871. sizes.append(1)
  872. else:
  873. t = re.match(r"float(\d)x(\d)", type)
  874. if t:
  875. sizes.append(int(t.groups(1)) * int(t.groups(2)))
  876. else:
  877. t = re.match(r"float(\d)", type)
  878. sizes.append(int(t.groups(1)))
  879. if line.find("[branch]") != -1:
  880. conditionals.append(name)
  881. line = fs.readline()
  882. def build_cg_shader(sname):
  883. vp_uniforms = []
  884. vp_uniform_sizes = []
  885. vp_conditionals = []
  886. parse_cg_file("vp_" + sname + ".cg", vp_uniforms, vp_uniform_sizes, vp_conditionals)
  887. fp_uniforms = []
  888. fp_uniform_sizes = []
  889. fp_conditionals = []
  890. parse_cg_file("fp_" + sname + ".cg", fp_uniforms, fp_uniform_sizes, fp_conditionals)
  891. fd = open("shader_" + sname + ".cg.h", "w")
  892. fd.write('\n#include "shader_cell.h"\n')
  893. fd.write("\nclass Shader_" + sname + " : public ShaderCell {\n")
  894. fd.write("\n\tstatic struct VertexUniforms[] = {\n")
  895. offset = 0
  896. for i in range(0, len(vp_uniforms)):
  897. fd.write('\t\t{ "%s", %d, %d },\n' % (vp_uniforms[i], offset, vp_uniform_sizes[i]))
  898. offset = offset + vp_uniform_sizes[i]
  899. fd.write("\t};\n\n")
  900. fd.write("public:\n\n")
  901. fd.write("\tenum {\n")
  902. for i in range(0, len(vp_uniforms)):
  903. fd.write('\t\tVP_%s,\n' % vp_uniforms[i].upper())
  904. fd.write("\t};\n")
  905. import glob
  906. def detect_modules():
  907. module_list = []
  908. includes_cpp = ""
  909. register_cpp = ""
  910. unregister_cpp = ""
  911. files = glob.glob("modules/*")
  912. files.sort() # so register_module_types does not change that often, and also plugins are registered in alphabetic order
  913. for x in files:
  914. if (not os.path.isdir(x)):
  915. continue
  916. x = x.replace("modules/", "") # rest of world
  917. x = x.replace("modules\\", "") # win32
  918. module_list.append(x)
  919. try:
  920. with open("modules/" + x + "/register_types.h"):
  921. includes_cpp += '#include "modules/' + x + '/register_types.h"\n'
  922. register_cpp += '#ifdef MODULE_' + x.upper() + '_ENABLED\n'
  923. register_cpp += '\tregister_' + x + '_types();\n'
  924. register_cpp += '#endif\n'
  925. unregister_cpp += '#ifdef MODULE_' + x.upper() + '_ENABLED\n'
  926. unregister_cpp += '\tunregister_' + x + '_types();\n'
  927. unregister_cpp += '#endif\n'
  928. except IOError:
  929. pass
  930. modules_cpp = """
  931. // modules.cpp - THIS FILE IS GENERATED, DO NOT EDIT!!!!!!!
  932. #include "register_module_types.h"
  933. """ + includes_cpp + """
  934. void register_module_types() {
  935. """ + register_cpp + """
  936. }
  937. void unregister_module_types() {
  938. """ + unregister_cpp + """
  939. }
  940. """
  941. f = open("modules/register_module_types.cpp", "wb")
  942. f.write(modules_cpp)
  943. return module_list
  944. def win32_spawn(sh, escape, cmd, args, env):
  945. import subprocess
  946. newargs = ' '.join(args[1:])
  947. cmdline = cmd + " " + newargs
  948. startupinfo = subprocess.STARTUPINFO()
  949. #startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  950. for e in env:
  951. if type(env[e]) != type(""):
  952. env[e] = str(env[e])
  953. proc = subprocess.Popen(cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  954. stderr=subprocess.PIPE, startupinfo=startupinfo, shell=False, env=env)
  955. data, err = proc.communicate()
  956. rv = proc.wait()
  957. if rv:
  958. print "====="
  959. print err
  960. print "====="
  961. return rv
  962. """
  963. def win32_spawn(sh, escape, cmd, args, spawnenv):
  964. import win32file
  965. import win32event
  966. import win32process
  967. import win32security
  968. for var in spawnenv:
  969. spawnenv[var] = spawnenv[var].encode('ascii', 'replace')
  970. sAttrs = win32security.SECURITY_ATTRIBUTES()
  971. StartupInfo = win32process.STARTUPINFO()
  972. newargs = ' '.join(map(escape, args[1:]))
  973. cmdline = cmd + " " + newargs
  974. # check for any special operating system commands
  975. if cmd == 'del':
  976. for arg in args[1:]:
  977. win32file.DeleteFile(arg)
  978. exit_code = 0
  979. else:
  980. # otherwise execute the command.
  981. hProcess, hThread, dwPid, dwTid = win32process.CreateProcess(None, cmdline, None, None, 1, 0, spawnenv, None, StartupInfo)
  982. win32event.WaitForSingleObject(hProcess, win32event.INFINITE)
  983. exit_code = win32process.GetExitCodeProcess(hProcess)
  984. win32file.CloseHandle(hProcess);
  985. win32file.CloseHandle(hThread);
  986. return exit_code
  987. """
  988. def android_add_maven_repository(self, url):
  989. self.android_maven_repos.append(url)
  990. def android_add_dependency(self, depline):
  991. self.android_dependencies.append(depline)
  992. def android_add_java_dir(self, subpath):
  993. base_path = self.Dir(".").abspath + "/modules/" + self.current_module + "/" + subpath
  994. self.android_java_dirs.append(base_path)
  995. def android_add_res_dir(self, subpath):
  996. base_path = self.Dir(".").abspath + "/modules/" + self.current_module + "/" + subpath
  997. self.android_res_dirs.append(base_path)
  998. def android_add_aidl_dir(self, subpath):
  999. base_path = self.Dir(".").abspath + "/modules/" + self.current_module + "/" + subpath
  1000. self.android_aidl_dirs.append(base_path)
  1001. def android_add_jni_dir(self, subpath):
  1002. base_path = self.Dir(".").abspath + "/modules/" + self.current_module + "/" + subpath
  1003. self.android_jni_dirs.append(base_path)
  1004. def android_add_default_config(self, config):
  1005. self.android_default_config.append(config)
  1006. def android_add_to_manifest(self, file):
  1007. base_path = self.Dir(".").abspath + "/modules/" + self.current_module + "/" + file
  1008. f = open(base_path, "rb")
  1009. self.android_manifest_chunk += f.read()
  1010. def android_add_to_permissions(self, file):
  1011. base_path = self.Dir(".").abspath + "/modules/" + self.current_module + "/" + file
  1012. f = open(base_path, "rb")
  1013. self.android_permission_chunk += f.read()
  1014. def android_add_to_attributes(self, file):
  1015. base_path = self.Dir(".").abspath + "/modules/" + self.current_module + "/" + file
  1016. f = open(base_path, "rb")
  1017. self.android_appattributes_chunk += f.read()
  1018. def disable_module(self):
  1019. self.disabled_modules.append(self.current_module)
  1020. def use_windows_spawn_fix(self, platform=None):
  1021. if (os.name != "nt"):
  1022. return # not needed, only for windows
  1023. # On Windows, due to the limited command line length, when creating a static library
  1024. # from a very high number of objects SCons will invoke "ar" once per object file;
  1025. # that makes object files with same names to be overwritten so the last wins and
  1026. # the library looses symbols defined by overwritten objects.
  1027. # By enabling quick append instead of the default mode (replacing), libraries will
  1028. # got built correctly regardless the invokation strategy.
  1029. # Furthermore, since SCons will rebuild the library from scratch when an object file
  1030. # changes, no multiple versions of the same object file will be present.
  1031. self.Replace(ARFLAGS='q')
  1032. import subprocess
  1033. def mySubProcess(cmdline, env):
  1034. prefix = ""
  1035. if(platform == 'javascript'):
  1036. prefix = "python.exe "
  1037. startupinfo = subprocess.STARTUPINFO()
  1038. startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  1039. proc = subprocess.Popen(prefix + cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  1040. stderr=subprocess.PIPE, startupinfo=startupinfo, shell=False, env=env)
  1041. data, err = proc.communicate()
  1042. rv = proc.wait()
  1043. if rv:
  1044. print "====="
  1045. print err
  1046. print "====="
  1047. return rv
  1048. def mySpawn(sh, escape, cmd, args, env):
  1049. newargs = ' '.join(args[1:])
  1050. cmdline = cmd + " " + newargs
  1051. rv = 0
  1052. env = {str(key): str(value) for key, value in env.iteritems()}
  1053. if len(cmdline) > 32000 and cmd.endswith("ar"):
  1054. cmdline = cmd + " " + args[1] + " " + args[2] + " "
  1055. for i in range(3, len(args)):
  1056. rv = mySubProcess(cmdline + args[i], env)
  1057. if rv:
  1058. break
  1059. else:
  1060. rv = mySubProcess(cmdline, env)
  1061. return rv
  1062. self['SPAWN'] = mySpawn
  1063. def split_lib(self, libname):
  1064. import string
  1065. env = self
  1066. num = 0
  1067. cur_base = ""
  1068. max_src = 64
  1069. list = []
  1070. lib_list = []
  1071. for f in getattr(env, libname + "_sources"):
  1072. fname = ""
  1073. if type(f) == type(""):
  1074. fname = env.File(f).path
  1075. else:
  1076. fname = env.File(f)[0].path
  1077. fname = fname.replace("\\", "/")
  1078. base = string.join(fname.split("/")[:2], "/")
  1079. if base != cur_base and len(list) > max_src:
  1080. if num > 0:
  1081. lib = env.Library(libname + str(num), list)
  1082. lib_list.append(lib)
  1083. list = []
  1084. num = num + 1
  1085. cur_base = base
  1086. list.append(f)
  1087. lib = env.Library(libname + str(num), list)
  1088. lib_list.append(lib)
  1089. if len(lib_list) > 0:
  1090. import os, sys
  1091. if os.name == 'posix' and sys.platform == 'msys':
  1092. env.Replace(ARFLAGS=['rcsT'])
  1093. lib = env.Library(libname + "_collated", lib_list)
  1094. lib_list = [lib]
  1095. lib_base = []
  1096. env.add_source_files(lib_base, "*.cpp")
  1097. lib_list.insert(0, env.Library(libname, lib_base))
  1098. env.Prepend(LIBS=lib_list)
  1099. def save_active_platforms(apnames, ap):
  1100. for x in ap:
  1101. pth = x + "/logo.png"
  1102. # print("open path: "+pth)
  1103. pngf = open(pth, "rb")
  1104. b = pngf.read(1)
  1105. str = " /* AUTOGENERATED FILE, DO NOT EDIT */ \n"
  1106. str += " static const unsigned char _" + x[9:] + "_logo[]={"
  1107. while(len(b) == 1):
  1108. str += hex(ord(b))
  1109. b = pngf.read(1)
  1110. if (len(b) == 1):
  1111. str += ","
  1112. str += "};\n"
  1113. wf = x + "/logo.h"
  1114. logow = open(wf, "wb")
  1115. logow.write(str)
  1116. def no_verbose(sys, env):
  1117. # If the output is not a terminal, do nothing
  1118. if not sys.stdout.isatty():
  1119. return
  1120. colors = {}
  1121. colors['cyan'] = '\033[96m'
  1122. colors['purple'] = '\033[95m'
  1123. colors['blue'] = '\033[94m'
  1124. colors['green'] = '\033[92m'
  1125. colors['yellow'] = '\033[93m'
  1126. colors['red'] = '\033[91m'
  1127. colors['end'] = '\033[0m'
  1128. compile_source_message = '%sCompiling %s==> %s$SOURCE%s' % (colors['blue'], colors['purple'], colors['yellow'], colors['end'])
  1129. java_compile_source_message = '%sCompiling %s==> %s$SOURCE%s' % (colors['blue'], colors['purple'], colors['yellow'], colors['end'])
  1130. compile_shared_source_message = '%sCompiling shared %s==> %s$SOURCE%s' % (colors['blue'], colors['purple'], colors['yellow'], colors['end'])
  1131. link_program_message = '%sLinking Program %s==> %s$TARGET%s' % (colors['red'], colors['purple'], colors['yellow'], colors['end'])
  1132. link_library_message = '%sLinking Static Library %s==> %s$TARGET%s' % (colors['red'], colors['purple'], colors['yellow'], colors['end'])
  1133. ranlib_library_message = '%sRanlib Library %s==> %s$TARGET%s' % (colors['red'], colors['purple'], colors['yellow'], colors['end'])
  1134. link_shared_library_message = '%sLinking Shared Library %s==> %s$TARGET%s' % (colors['red'], colors['purple'], colors['yellow'], colors['end'])
  1135. java_library_message = '%sCreating Java Archive %s==> %s$TARGET%s' % (colors['red'], colors['purple'], colors['yellow'], colors['end'])
  1136. env.Append(CXXCOMSTR=[compile_source_message])
  1137. env.Append(CCCOMSTR=[compile_source_message])
  1138. env.Append(SHCCCOMSTR=[compile_shared_source_message])
  1139. env.Append(SHCXXCOMSTR=[compile_shared_source_message])
  1140. env.Append(ARCOMSTR=[link_library_message])
  1141. env.Append(RANLIBCOMSTR=[ranlib_library_message])
  1142. env.Append(SHLINKCOMSTR=[link_shared_library_message])
  1143. env.Append(LINKCOMSTR=[link_program_message])
  1144. env.Append(JARCOMSTR=[java_library_message])
  1145. env.Append(JAVACCOMSTR=[java_compile_source_message])
  1146. def detect_visual_c_compiler_version(tools_env):
  1147. # tools_env is the variable scons uses to call tools that execute tasks, SCons's env['ENV'] that executes tasks...
  1148. # (see the SCons documentation for more information on what it does)...
  1149. # in order for this function to be well encapsulated i choose to force it to recieve SCons's TOOLS env (env['ENV']
  1150. # and not scons setup environment (env)... so make sure you call the right environment on it or it will fail to detect
  1151. # the propper vc version that will be called
  1152. # These is no flag to give to visual c compilers to set the architecture, ie scons bits argument (32,64,ARM etc)
  1153. # There are many different cl.exe files that are run, and each one compiles & links to a different architecture
  1154. # As far as I know, the only way to figure out what compiler will be run when Scons calls cl.exe via Program()
  1155. # is to check the PATH varaible and figure out which one will be called first. Code bellow does that and returns:
  1156. # the following string values:
  1157. # "" Compiler not detected
  1158. # "amd64" Native 64 bit compiler
  1159. # "amd64_x86" 64 bit Cross Compiler for 32 bit
  1160. # "x86" Native 32 bit compiler
  1161. # "x86_amd64" 32 bit Cross Compiler for 64 bit
  1162. # There are other architectures, but Godot does not support them currently, so this function does not detect arm/amd64_arm
  1163. # and similar architectures/compilers
  1164. # Set chosen compiler to "not detected"
  1165. vc_chosen_compiler_index = -1
  1166. vc_chosen_compiler_str = ""
  1167. # find() works with -1 so big ifs bellow are needed... the simplest solution, in fact
  1168. # First test if amd64 and amd64_x86 compilers are present in the path
  1169. vc_amd64_compiler_detection_index = tools_env["PATH"].find(tools_env["VCINSTALLDIR"] + "BIN\\amd64;")
  1170. if(vc_amd64_compiler_detection_index > -1):
  1171. vc_chosen_compiler_index = vc_amd64_compiler_detection_index
  1172. vc_chosen_compiler_str = "amd64"
  1173. vc_amd64_x86_compiler_detection_index = tools_env["PATH"].find(tools_env["VCINSTALLDIR"] + "BIN\\amd64_x86;")
  1174. if(vc_amd64_x86_compiler_detection_index > -1
  1175. and (vc_chosen_compiler_index == -1
  1176. or vc_chosen_compiler_index > vc_amd64_x86_compiler_detection_index)):
  1177. vc_chosen_compiler_index = vc_amd64_x86_compiler_detection_index
  1178. vc_chosen_compiler_str = "amd64_x86"
  1179. # Now check the 32 bit compilers
  1180. vc_x86_compiler_detection_index = tools_env["PATH"].find(tools_env["VCINSTALLDIR"] + "BIN;")
  1181. if(vc_x86_compiler_detection_index > -1
  1182. and (vc_chosen_compiler_index == -1
  1183. or vc_chosen_compiler_index > vc_x86_compiler_detection_index)):
  1184. vc_chosen_compiler_index = vc_x86_compiler_detection_index
  1185. vc_chosen_compiler_str = "x86"
  1186. vc_x86_amd64_compiler_detection_index = tools_env["PATH"].find(tools_env['VCINSTALLDIR'] + "BIN\\x86_amd64;")
  1187. if(vc_x86_amd64_compiler_detection_index > -1
  1188. and (vc_chosen_compiler_index == -1
  1189. or vc_chosen_compiler_index > vc_x86_amd64_compiler_detection_index)):
  1190. vc_chosen_compiler_index = vc_x86_amd64_compiler_detection_index
  1191. vc_chosen_compiler_str = "x86_amd64"
  1192. # debug help
  1193. # print vc_amd64_compiler_detection_index
  1194. # print vc_amd64_x86_compiler_detection_index
  1195. # print vc_x86_compiler_detection_index
  1196. # print vc_x86_amd64_compiler_detection_index
  1197. # print "chosen "+str(vc_chosen_compiler_index)+ " | "+str(vc_chosen_compiler_str)
  1198. return vc_chosen_compiler_str
  1199. def precious_program(env, program, sources, **args):
  1200. program = env.ProgramOriginal(program, sources, **args)
  1201. env.Precious(program)
  1202. return program