methods.py 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  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. def build_legacygl_header( filename, include, class_suffix, output_attribs ):
  512. fs = open(filename,"r")
  513. line=fs.readline()
  514. vertex_lines=[]
  515. fragment_lines=[]
  516. uniforms=[]
  517. attributes=[]
  518. fbos=[]
  519. conditionals=[]
  520. enums={}
  521. enum_constants=[]
  522. texunits=[]
  523. texunit_names=[]
  524. ubos=[]
  525. ubo_names=[]
  526. reading=""
  527. line_offset=0
  528. vertex_offset=0
  529. fragment_offset=0
  530. while(line):
  531. if (line.find("[vertex]")!=-1):
  532. reading="vertex"
  533. line=fs.readline()
  534. line_offset+=1
  535. vertex_offset=line_offset
  536. continue
  537. if (line.find("[fragment]")!=-1):
  538. reading="fragment"
  539. line=fs.readline()
  540. line_offset+=1
  541. fragment_offset=line_offset
  542. continue
  543. if (line.find("#ifdef ")!=-1 or line.find("#elif defined(")!=-1):
  544. if (line.find("#ifdef ")!=-1):
  545. ifdefline = line.replace("#ifdef ","").strip()
  546. else:
  547. ifdefline = line.replace("#elif defined(","").strip()
  548. ifdefline = ifdefline.replace(")","").strip()
  549. if (line.find("_EN_")!=-1):
  550. enumbase = ifdefline[:ifdefline.find("_EN_")];
  551. ifdefline = ifdefline.replace("_EN_","_")
  552. line = line.replace("_EN_","_")
  553. # print(enumbase+":"+ifdefline);
  554. if (enumbase not in enums):
  555. enums[enumbase]=[]
  556. if (ifdefline not in enums[enumbase]):
  557. enums[enumbase].append(ifdefline);
  558. elif (not ifdefline in conditionals):
  559. conditionals+=[ifdefline]
  560. if (line.find("uniform")!=-1 and line.lower().find("texunit:")!=-1):
  561. #texture unit
  562. texunitstr = line[line.find(":")+1:].strip()
  563. if (texunitstr=="auto"):
  564. texunit="-1"
  565. else:
  566. texunit = str(int(texunitstr ))
  567. uline=line[:line.lower().find("//")]
  568. uline = uline.replace("uniform","");
  569. uline = uline.replace("highp","");
  570. uline = uline.replace(";","");
  571. lines = uline.split(",")
  572. for x in lines:
  573. x = x.strip()
  574. x = x[ x.rfind(" ")+1: ]
  575. if (x.find("[")!=-1):
  576. #unfiorm array
  577. x = x[ :x.find("[") ]
  578. if (not x in texunit_names):
  579. texunits+=[(x,texunit)]
  580. texunit_names+=[x]
  581. elif (line.find("uniform")!=-1):
  582. uline = line.replace("uniform","");
  583. uline = uline.replace(";","");
  584. lines = uline.split(",")
  585. for x in lines:
  586. x = x.strip()
  587. x = x[ x.rfind(" ")+1: ]
  588. if (x.find("[")!=-1):
  589. #unfiorm array
  590. x = x[ :x.find("[") ]
  591. if (not x in uniforms):
  592. uniforms+=[x]
  593. if ((line.strip().find("in ")==0 or line.strip().find("attribute ")==0) and line.find("attrib:")!=-1):
  594. uline = line.replace("in ","");
  595. uline = uline.replace("attribute ","");
  596. uline = uline.replace("highp ","");
  597. uline = uline.replace(";","");
  598. uline = uline[ uline.find(" "): ].strip()
  599. if (uline.find("//")!=-1):
  600. name,bind = uline.split("//")
  601. if (bind.find("attrib:")!=-1):
  602. name=name.strip()
  603. bind=bind.replace("attrib:","").strip()
  604. attributes+=[(name,bind)]
  605. line=line.replace("\r","")
  606. line=line.replace("\n","")
  607. #line=line.replace("\\","\\\\")
  608. #line=line.replace("\"","\\\"")
  609. #line=line+"\\n\\"
  610. if (reading=="vertex"):
  611. vertex_lines+=[line]
  612. if (reading=="fragment"):
  613. fragment_lines+=[line]
  614. line=fs.readline()
  615. line_offset+=1
  616. fs.close();
  617. out_file = filename+".h"
  618. fd = open(out_file,"w")
  619. fd.write("/* WARNING, THIS FILE WAS GENERATED, DO NOT EDIT */\n");
  620. out_file_base = out_file
  621. out_file_base = out_file_base[ out_file_base.rfind("/")+1: ]
  622. out_file_base = out_file_base[ out_file_base.rfind("\\")+1: ]
  623. # print("out file "+out_file+" base " +out_file_base)
  624. out_file_ifdef = out_file_base.replace(".","_").upper()
  625. fd.write("#ifndef "+out_file_ifdef+class_suffix+"_120\n")
  626. fd.write("#define "+out_file_ifdef+class_suffix+"_120\n")
  627. out_file_class = out_file_base.replace(".glsl.h","").title().replace("_","").replace(".","")+"Shader"+class_suffix;
  628. fd.write("\n\n");
  629. fd.write("#include \"" + include + "\"\n\n\n");
  630. fd.write("class "+out_file_class+" : public Shader"+class_suffix+" {\n\n");
  631. fd.write("\t virtual String get_shader_name() const { return \""+out_file_class+"\"; }\n");
  632. fd.write("public:\n\n");
  633. if (len(conditionals)):
  634. fd.write("\tenum Conditionals {\n");
  635. for x in conditionals:
  636. fd.write("\t\t"+x.upper()+",\n");
  637. fd.write("\t};\n\n");
  638. if (len(uniforms)):
  639. fd.write("\tenum Uniforms {\n");
  640. for x in uniforms:
  641. fd.write("\t\t"+x.upper()+",\n");
  642. fd.write("\t};\n\n");
  643. fd.write("\t_FORCE_INLINE_ int get_uniform(Uniforms p_uniform) const { return _get_uniform(p_uniform); }\n\n");
  644. if (len(conditionals)):
  645. fd.write("\t_FORCE_INLINE_ void set_conditional(Conditionals p_conditional,bool p_enable) { _set_conditional(p_conditional,p_enable); }\n\n");
  646. fd.write("\t#define _FU if (get_uniform(p_uniform)<0) return; ERR_FAIL_COND( get_active()!=this );\n\n ");
  647. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, float p_value) { _FU glUniform1f(get_uniform(p_uniform),p_value); }\n\n");
  648. fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, double p_value) { _FU glUniform1f(get_uniform(p_uniform),p_value); }\n\n");
  649. 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");
  650. 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");
  651. 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");
  652. 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");
  653. 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");
  654. 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");
  655. #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");
  656. #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");
  657. #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");
  658. #fd.write("\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, long p_value) { _FU glUniform1i(get_uniform(p_uniform),p_value); }\n\n");
  659. 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");
  660. 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");
  661. 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");
  662. 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");
  663. 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");
  664. 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");
  665. fd.write("""\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Transform& p_transform) { _FU
  666. const Transform &tr = p_transform;
  667. GLfloat matrix[16]={ /* build a 16x16 matrix */
  668. tr.basis.elements[0][0],
  669. tr.basis.elements[1][0],
  670. tr.basis.elements[2][0],
  671. 0,
  672. tr.basis.elements[0][1],
  673. tr.basis.elements[1][1],
  674. tr.basis.elements[2][1],
  675. 0,
  676. tr.basis.elements[0][2],
  677. tr.basis.elements[1][2],
  678. tr.basis.elements[2][2],
  679. 0,
  680. tr.origin.x,
  681. tr.origin.y,
  682. tr.origin.z,
  683. 1
  684. };
  685. glUniformMatrix4fv(get_uniform(p_uniform),1,false,matrix);
  686. }
  687. """);
  688. fd.write("""\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const Matrix32& p_transform) { _FU
  689. const Matrix32 &tr = p_transform;
  690. GLfloat matrix[16]={ /* build a 16x16 matrix */
  691. tr.elements[0][0],
  692. tr.elements[0][1],
  693. 0,
  694. 0,
  695. tr.elements[1][0],
  696. tr.elements[1][1],
  697. 0,
  698. 0,
  699. 0,
  700. 0,
  701. 1,
  702. 0,
  703. tr.elements[2][0],
  704. tr.elements[2][1],
  705. 0,
  706. 1
  707. };
  708. glUniformMatrix4fv(get_uniform(p_uniform),1,false,matrix);
  709. }
  710. """);
  711. fd.write("""\t_FORCE_INLINE_ void set_uniform(Uniforms p_uniform, const CameraMatrix& p_matrix) { _FU
  712. GLfloat matrix[16];
  713. for (int i=0;i<4;i++) {
  714. for (int j=0;j<4;j++) {
  715. matrix[i*4+j]=p_matrix.matrix[i][j];
  716. }
  717. }
  718. glUniformMatrix4fv(get_uniform(p_uniform),1,false,matrix);
  719. }; """);
  720. fd.write("\n\n#undef _FU\n\n\n");
  721. fd.write("\tvirtual void init() {\n\n");
  722. enum_value_count=0;
  723. if (len(enums)):
  724. fd.write("\t\t//Written using math, given nonstandarity of 64 bits integer constants..\n");
  725. fd.write("\t\tstatic const Enum _enums[]={\n")
  726. bitofs=len(conditionals)
  727. enum_vals=[]
  728. for xv in enums:
  729. x=enums[xv]
  730. bits=1
  731. amt = len(x);
  732. # print(x)
  733. while(2**bits < amt):
  734. bits+=1
  735. # print("amount: "+str(amt)+" bits "+str(bits));
  736. strs="{"
  737. for i in range(amt):
  738. strs+="\"#define "+x[i]+"\\n\","
  739. v={}
  740. v["set_mask"]="uint64_t("+str(i)+")<<"+str(bitofs)
  741. v["clear_mask"]="((uint64_t(1)<<40)-1) ^ (((uint64_t(1)<<"+str(bits)+") - 1)<<"+str(bitofs)+")"
  742. enum_vals.append(v)
  743. enum_constants.append(x[i])
  744. strs+="NULL}"
  745. fd.write("\t\t\t{(uint64_t(1<<"+str(bits)+")-1)<<"+str(bitofs)+","+str(bitofs)+","+strs+"},\n");
  746. bitofs+=bits
  747. fd.write("\t\t};\n\n");
  748. fd.write("\t\tstatic const EnumValue _enum_values[]={\n")
  749. enum_value_count=len(enum_vals);
  750. for x in enum_vals:
  751. fd.write("\t\t\t{"+x["set_mask"]+","+x["clear_mask"]+"},\n");
  752. fd.write("\t\t};\n\n");
  753. else:
  754. fd.write("\t\tstatic const Enum *_enums=NULL;\n")
  755. fd.write("\t\tstatic const EnumValue *_enum_values=NULL;\n")
  756. if (len(conditionals)):
  757. fd.write("\t\tstatic const char* _conditional_strings[]={\n")
  758. if (len(conditionals)):
  759. for x in conditionals:
  760. fd.write("\t\t\t\"#define "+x+"\\n\",\n");
  761. fd.write("\t\t};\n\n");
  762. else:
  763. fd.write("\t\tstatic const char **_conditional_strings=NULL;\n")
  764. if (len(uniforms)):
  765. fd.write("\t\tstatic const char* _uniform_strings[]={\n")
  766. if (len(uniforms)):
  767. for x in uniforms:
  768. fd.write("\t\t\t\""+x+"\",\n");
  769. fd.write("\t\t};\n\n");
  770. else:
  771. fd.write("\t\tstatic const char **_uniform_strings=NULL;\n")
  772. if output_attribs:
  773. if (len(attributes)):
  774. fd.write("\t\tstatic AttributePair _attribute_pairs[]={\n")
  775. for x in attributes:
  776. fd.write("\t\t\t{\""+x[0]+"\","+x[1]+"},\n");
  777. fd.write("\t\t};\n\n");
  778. else:
  779. fd.write("\t\tstatic AttributePair *_attribute_pairs=NULL;\n")
  780. if (len(texunits)):
  781. fd.write("\t\tstatic TexUnitPair _texunit_pairs[]={\n")
  782. for x in texunits:
  783. fd.write("\t\t\t{\""+x[0]+"\","+x[1]+"},\n");
  784. fd.write("\t\t};\n\n");
  785. else:
  786. fd.write("\t\tstatic TexUnitPair *_texunit_pairs=NULL;\n")
  787. fd.write("\t\tstatic const char _vertex_code[]={\n")
  788. for x in vertex_lines:
  789. for i in range(len(x)):
  790. fd.write(str(ord(x[i]))+",");
  791. fd.write(str(ord('\n'))+",");
  792. fd.write("\t\t0};\n\n");
  793. fd.write("\t\tstatic const int _vertex_code_start="+str(vertex_offset)+";\n")
  794. fd.write("\t\tstatic const char _fragment_code[]={\n")
  795. for x in fragment_lines:
  796. for i in range(len(x)):
  797. fd.write(str(ord(x[i]))+",");
  798. fd.write(str(ord('\n'))+",");
  799. fd.write("\t\t0};\n\n");
  800. fd.write("\t\tstatic const int _fragment_code_start="+str(fragment_offset)+";\n")
  801. if output_attribs:
  802. fd.write("\t\tsetup(_conditional_strings,"+str(len(conditionals))+",_uniform_strings,"+str(len(uniforms))+",_attribute_pairs,"+str(len(attributes))+", _texunit_pairs,"+str(len(texunits))+",_vertex_code,_fragment_code,_vertex_code_start,_fragment_code_start);\n")
  803. else:
  804. fd.write("\t\tsetup(_conditional_strings,"+str(len(conditionals))+",_uniform_strings,"+str(len(uniforms))+",_texunit_pairs,"+str(len(texunits))+",_enums,"+str(len(enums))+",_enum_values,"+str(enum_value_count)+",_vertex_code,_fragment_code,_vertex_code_start,_fragment_code_start);\n")
  805. fd.write("\t};\n\n")
  806. if (len(enum_constants)):
  807. fd.write("\tenum EnumConditionals {\n")
  808. for x in enum_constants:
  809. fd.write("\t\t"+x.upper()+",\n");
  810. fd.write("\t};\n\n");
  811. fd.write("\tvoid set_enum_conditional(EnumConditionals p_cond) { _set_enum_conditional(p_cond); }\n")
  812. fd.write("};\n\n");
  813. fd.write("#endif\n\n");
  814. fd.close();
  815. def build_legacygl_headers( target, source, env ):
  816. for x in source:
  817. build_legacygl_header(str(x), include = "drivers/legacygl/shader_lgl.h", class_suffix = "LGL", output_attribs = False);
  818. return 0
  819. def build_gles2_headers( target, source, env ):
  820. for x in source:
  821. build_legacygl_header(str(x), include="drivers/gles2/shader_gles2.h", class_suffix = "GLES2", output_attribs = True)
  822. def update_version():
  823. rev = "custom_build"
  824. if (os.getenv("BUILD_REVISION")!=None):
  825. rev=os.getenv("BUILD_REVISION")
  826. print("Using custom revision: "+rev)
  827. import version
  828. f=open("core/version.h","wb")
  829. f.write("#define VERSION_SHORT_NAME "+str(version.short_name)+"\n")
  830. f.write("#define VERSION_NAME "+str(version.name)+"\n")
  831. f.write("#define VERSION_MAJOR "+str(version.major)+"\n")
  832. f.write("#define VERSION_MINOR "+str(version.minor)+"\n")
  833. if (hasattr(version, 'patch')):
  834. f.write("#define VERSION_PATCH "+str(version.patch)+"\n")
  835. f.write("#define VERSION_REVISION "+str(rev)+"\n")
  836. f.write("#define VERSION_STATUS "+str(version.status)+"\n")
  837. import datetime
  838. f.write("#define VERSION_YEAR "+str(datetime.datetime.now().year)+"\n")
  839. def parse_cg_file(fname, uniforms, sizes, conditionals):
  840. import re
  841. fs = open(fname, "r")
  842. line=fs.readline()
  843. while line:
  844. if re.match(r"^\s*uniform", line):
  845. res = re.match(r"uniform ([\d\w]*) ([\d\w]*)")
  846. type = res.groups(1)
  847. name = res.groups(2)
  848. uniforms.append(name);
  849. if (type.find("texobj") != -1):
  850. sizes.append(1);
  851. else:
  852. t = re.match(r"float(\d)x(\d)", type);
  853. if t:
  854. sizes.append(int(t.groups(1)) * int(t.groups(2)))
  855. else:
  856. t = re.match(r"float(\d)", type);
  857. sizes.append(int(t.groups(1)))
  858. if line.find("[branch]") != -1:
  859. conditionals.append(name);
  860. line = fs.readline();
  861. def build_cg_shader(sname):
  862. vp_uniforms = []
  863. vp_uniform_sizes = []
  864. vp_conditionals = []
  865. parse_cg_file("vp_"+sname+".cg", vp_uniforms, vp_uniform_sizes, vp_conditionals);
  866. fp_uniforms = []
  867. fp_uniform_sizes = []
  868. fp_conditionals = []
  869. parse_cg_file("fp_"+sname+".cg", fp_uniforms, fp_uniform_sizes, fp_conditionals);
  870. fd = open("shader_"+sname+".cg.h", "w");
  871. fd.write('\n#include "shader_cell.h"\n');
  872. fd.write("\nclass Shader_" + sname + " : public ShaderCell {\n");
  873. fd.write("\n\tstatic struct VertexUniforms[] = {\n");
  874. offset = 0;
  875. for i in range(0, len(vp_uniforms)):
  876. fd.write('\t\t{ "%s", %d, %d },\n' % (vp_uniforms[i], offset, vp_uniform_sizes[i]))
  877. offset = offset + vp_uniform_sizes[i];
  878. fd.write("\t};\n\n");
  879. fd.write("public:\n\n");
  880. fd.write("\tenum {\n");
  881. for i in range(0, len(vp_uniforms)):
  882. fd.write('\t\tVP_%s,\n' % vp_uniforms[i].upper())
  883. fd.write("\t};\n");
  884. import glob
  885. def detect_modules():
  886. module_list=[]
  887. includes_cpp=""
  888. register_cpp=""
  889. unregister_cpp=""
  890. for x in glob.glob("modules/*"):
  891. if (not os.path.isdir(x)):
  892. continue
  893. x=x.replace("modules/","") # rest of world
  894. x=x.replace("modules\\","") # win32
  895. module_list.append(x)
  896. try:
  897. with open("modules/"+x+"/register_types.h"):
  898. includes_cpp+='#include "modules/'+x+'/register_types.h"\n'
  899. register_cpp+='#ifdef MODULE_'+x.upper()+'_ENABLED\n'
  900. register_cpp+='\tregister_'+x+'_types();\n'
  901. register_cpp+='#endif\n'
  902. unregister_cpp+='#ifdef MODULE_'+x.upper()+'_ENABLED\n'
  903. unregister_cpp+='\tunregister_'+x+'_types();\n'
  904. unregister_cpp+='#endif\n'
  905. except IOError:
  906. pass
  907. modules_cpp="""
  908. // modules.cpp - THIS FILE IS GENERATED, DO NOT EDIT!!!!!!!
  909. #include "register_module_types.h"
  910. """+includes_cpp+"""
  911. void register_module_types() {
  912. """+register_cpp+"""
  913. }
  914. void unregister_module_types() {
  915. """+unregister_cpp+"""
  916. }
  917. """
  918. f=open("modules/register_module_types.cpp","wb")
  919. f.write(modules_cpp)
  920. return module_list
  921. def win32_spawn(sh, escape, cmd, args, env):
  922. import subprocess
  923. newargs = ' '.join(args[1:])
  924. cmdline = cmd + " " + newargs
  925. startupinfo = subprocess.STARTUPINFO()
  926. #startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  927. for e in env:
  928. if type(env[e]) != type(""):
  929. env[e] = str(env[e])
  930. proc = subprocess.Popen(cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  931. stderr=subprocess.PIPE, startupinfo=startupinfo, shell = False, env = env)
  932. data, err = proc.communicate()
  933. rv = proc.wait()
  934. if rv:
  935. print "====="
  936. print err
  937. print "====="
  938. return rv
  939. """
  940. def win32_spawn(sh, escape, cmd, args, spawnenv):
  941. import win32file
  942. import win32event
  943. import win32process
  944. import win32security
  945. for var in spawnenv:
  946. spawnenv[var] = spawnenv[var].encode('ascii', 'replace')
  947. sAttrs = win32security.SECURITY_ATTRIBUTES()
  948. StartupInfo = win32process.STARTUPINFO()
  949. newargs = ' '.join(map(escape, args[1:]))
  950. cmdline = cmd + " " + newargs
  951. # check for any special operating system commands
  952. if cmd == 'del':
  953. for arg in args[1:]:
  954. win32file.DeleteFile(arg)
  955. exit_code = 0
  956. else:
  957. # otherwise execute the command.
  958. hProcess, hThread, dwPid, dwTid = win32process.CreateProcess(None, cmdline, None, None, 1, 0, spawnenv, None, StartupInfo)
  959. win32event.WaitForSingleObject(hProcess, win32event.INFINITE)
  960. exit_code = win32process.GetExitCodeProcess(hProcess)
  961. win32file.CloseHandle(hProcess);
  962. win32file.CloseHandle(hThread);
  963. return exit_code
  964. """
  965. def android_add_maven_repository(self,url):
  966. self.android_maven_repos.append(url)
  967. def android_add_dependency(self,depline):
  968. self.android_dependencies.append(depline)
  969. def android_add_java_dir(self,subpath):
  970. base_path = self.Dir(".").abspath+"/modules/"+self.current_module+"/"+subpath
  971. self.android_java_dirs.append(base_path)
  972. def android_add_res_dir(self,subpath):
  973. base_path = self.Dir(".").abspath+"/modules/"+self.current_module+"/"+subpath
  974. self.android_res_dirs.append(base_path)
  975. def android_add_aidl_dir(self,subpath):
  976. base_path = self.Dir(".").abspath+"/modules/"+self.current_module+"/"+subpath
  977. self.android_aidl_dirs.append(base_path)
  978. def android_add_jni_dir(self,subpath):
  979. base_path = self.Dir(".").abspath+"/modules/"+self.current_module+"/"+subpath
  980. self.android_jni_dirs.append(base_path)
  981. def android_add_to_manifest(self,file):
  982. base_path = self.Dir(".").abspath+"/modules/"+self.current_module+"/"+file
  983. f = open(base_path,"rb")
  984. self.android_manifest_chunk+=f.read()
  985. def android_add_to_permissions(self,file):
  986. base_path = self.Dir(".").abspath+"/modules/"+self.current_module+"/"+file
  987. f = open(base_path,"rb")
  988. self.android_permission_chunk+=f.read()
  989. def android_add_to_attributes(self,file):
  990. base_path = self.Dir(".").abspath+"/modules/"+self.current_module+"/"+file
  991. f = open(base_path,"rb")
  992. self.android_appattributes_chunk+=f.read()
  993. def disable_module(self):
  994. self.disabled_modules.append(self.current_module)
  995. def use_windows_spawn_fix(self, platform=None):
  996. if (os.name!="nt"):
  997. return #not needed, only for windows
  998. self.split_drivers=True
  999. import subprocess
  1000. def mySubProcess(cmdline,env):
  1001. prefix = ""
  1002. if(platform == 'javascript'):
  1003. prefix = "python.exe "
  1004. startupinfo = subprocess.STARTUPINFO()
  1005. startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  1006. proc = subprocess.Popen(prefix + cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  1007. stderr=subprocess.PIPE, startupinfo=startupinfo, shell = False, env = env)
  1008. data, err = proc.communicate()
  1009. rv = proc.wait()
  1010. if rv:
  1011. print "====="
  1012. print err
  1013. print "====="
  1014. return rv
  1015. def mySpawn(sh, escape, cmd, args, env):
  1016. newargs = ' '.join(args[1:])
  1017. cmdline = cmd + " " + newargs
  1018. rv=0
  1019. if len(cmdline) > 32000 and cmd.endswith("ar") :
  1020. cmdline = cmd + " " + args[1] + " " + args[2] + " "
  1021. for i in range(3,len(args)) :
  1022. rv = mySubProcess( cmdline + args[i], env )
  1023. if rv :
  1024. break
  1025. else:
  1026. rv = mySubProcess( cmdline, env )
  1027. return rv
  1028. self['SPAWN'] = mySpawn
  1029. def save_active_platforms(apnames,ap):
  1030. for x in ap:
  1031. pth = x+"/logo.png"
  1032. # print("open path: "+pth)
  1033. pngf=open(pth,"rb");
  1034. b=pngf.read(1);
  1035. str=" /* AUTOGENERATED FILE, DO NOT EDIT */ \n"
  1036. str+=" static const unsigned char _"+x[9:]+"_logo[]={"
  1037. while(len(b)==1):
  1038. str+=hex(ord(b))
  1039. b=pngf.read(1);
  1040. if (len(b)==1):
  1041. str+=","
  1042. str+="};\n"
  1043. wf = x+"/logo.h"
  1044. logow = open(wf,"wb")
  1045. logow.write(str)
  1046. def colored(sys,env):
  1047. #If the output is not a terminal, do nothing
  1048. if not sys.stdout.isatty():
  1049. return
  1050. colors = {}
  1051. colors['cyan'] = '\033[96m'
  1052. colors['purple'] = '\033[95m'
  1053. colors['blue'] = '\033[94m'
  1054. colors['green'] = '\033[92m'
  1055. colors['yellow'] = '\033[93m'
  1056. colors['red'] = '\033[91m'
  1057. colors['end'] = '\033[0m'
  1058. compile_source_message = '%sCompiling %s==> %s$SOURCE%s' % (colors['blue'], colors['purple'], colors['yellow'], colors['end'])
  1059. java_compile_source_message = '%sCompiling %s==> %s$SOURCE%s' % (colors['blue'], colors['purple'], colors['yellow'], colors['end'])
  1060. compile_shared_source_message = '%sCompiling shared %s==> %s$SOURCE%s' % (colors['blue'], colors['purple'], colors['yellow'], colors['end'])
  1061. link_program_message = '%sLinking Program %s==> %s$TARGET%s' % (colors['red'], colors['purple'], colors['yellow'], colors['end'])
  1062. link_library_message = '%sLinking Static Library %s==> %s$TARGET%s' % (colors['red'], colors['purple'], colors['yellow'], colors['end'])
  1063. ranlib_library_message = '%sRanlib Library %s==> %s$TARGET%s' % (colors['red'], colors['purple'], colors['yellow'], colors['end'])
  1064. link_shared_library_message = '%sLinking Shared Library %s==> %s$TARGET%s' % (colors['red'], colors['purple'], colors['yellow'], colors['end'])
  1065. java_library_message = '%sCreating Java Archive %s==> %s$TARGET%s' % (colors['red'], colors['purple'], colors['yellow'], colors['end'])
  1066. env.Append( CXXCOMSTR=[compile_source_message] )
  1067. env.Append( CCCOMSTR=[compile_source_message] )
  1068. env.Append( SHCCCOMSTR=[compile_shared_source_message] )
  1069. env.Append( SHCXXCOMSTR=[compile_shared_source_message] )
  1070. env.Append( ARCOMSTR=[link_library_message] )
  1071. env.Append( RANLIBCOMSTR=[ranlib_library_message] )
  1072. env.Append( SHLINKCOMSTR=[link_shared_library_message] )
  1073. env.Append( LINKCOMSTR=[link_program_message] )
  1074. env.Append( JARCOMSTR=[java_library_message] )
  1075. env.Append( JAVACCOMSTR=[java_compile_source_message] )