shader.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*************************************************************************/
  2. /* shader.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "shader.h"
  30. #include "servers/visual_server.h"
  31. #include "texture.h"
  32. #include "os/file_access.h"
  33. #include "scene/scene_string_names.h"
  34. Shader::Mode Shader::get_mode() const {
  35. return mode;
  36. }
  37. void Shader::set_code( const String& p_vertex, const String& p_fragment, const String& p_light,int p_fragment_ofs,int p_light_ofs) {
  38. VisualServer::get_singleton()->shader_set_code(shader,p_vertex,p_fragment,p_light,0,p_fragment_ofs,p_light_ofs);
  39. params_cache_dirty=true;
  40. emit_signal(SceneStringNames::get_singleton()->changed);
  41. }
  42. String Shader::get_vertex_code() const {
  43. return VisualServer::get_singleton()->shader_get_vertex_code(shader);
  44. }
  45. String Shader::get_fragment_code() const {
  46. return VisualServer::get_singleton()->shader_get_fragment_code(shader);
  47. }
  48. String Shader::get_light_code() const {
  49. return VisualServer::get_singleton()->shader_get_light_code(shader);
  50. }
  51. bool Shader::has_param(const StringName& p_param) const {
  52. if (params_cache_dirty)
  53. get_param_list(NULL);
  54. return (params_cache.has(p_param));
  55. }
  56. void Shader::get_param_list(List<PropertyInfo> *p_params) const {
  57. List<PropertyInfo> local;
  58. VisualServer::get_singleton()->shader_get_param_list(shader,&local);
  59. params_cache.clear();
  60. params_cache_dirty=false;
  61. for(List<PropertyInfo>::Element *E=local.front();E;E=E->next()) {
  62. PropertyInfo pi=E->get();
  63. pi.name="shader_param/"+pi.name;
  64. params_cache[pi.name]=E->get().name;
  65. if (p_params) {
  66. //small little hack
  67. if (pi.type==Variant::_RID)
  68. pi.type=Variant::OBJECT;
  69. p_params->push_back(pi);
  70. }
  71. }
  72. }
  73. RID Shader::get_rid() const {
  74. return shader;
  75. }
  76. Dictionary Shader::_get_code() {
  77. String fs = VisualServer::get_singleton()->shader_get_fragment_code(shader);
  78. String vs = VisualServer::get_singleton()->shader_get_vertex_code(shader);
  79. String ls = VisualServer::get_singleton()->shader_get_light_code(shader);
  80. Dictionary c;
  81. c["fragment"]=fs;
  82. c["fragment_ofs"]=0;
  83. c["vertex"]=vs;
  84. c["vertex_ofs"]=0;
  85. c["light"]=ls;
  86. c["light_ofs"]=0;
  87. Array arr;
  88. for(const Map<StringName,Ref<Texture> >::Element *E=default_textures.front();E;E=E->next()) {
  89. arr.push_back(E->key());
  90. arr.push_back(E->get());
  91. }
  92. if (arr.size())
  93. c["default_tex"]=arr;
  94. return c;
  95. }
  96. void Shader::_set_code(const Dictionary& p_string) {
  97. ERR_FAIL_COND(!p_string.has("fragment"));
  98. ERR_FAIL_COND(!p_string.has("vertex"));
  99. String light;
  100. if (p_string.has("light"))
  101. light=p_string["light"];
  102. set_code(p_string["vertex"],p_string["fragment"],light);
  103. if (p_string.has("default_tex")) {
  104. Array arr=p_string["default_tex"];
  105. if ((arr.size()&1)==0) {
  106. for(int i=0;i<arr.size();i+=2) {
  107. set_default_texture_param(arr[i],arr[i+1]);
  108. }
  109. }
  110. }
  111. }
  112. void Shader::set_default_texture_param(const StringName& p_param,const Ref<Texture>& p_texture) {
  113. if (p_texture.is_valid()) {
  114. default_textures[p_param]=p_texture;
  115. VS::get_singleton()->shader_set_default_texture_param(shader,p_param,p_texture->get_rid());
  116. } else {
  117. default_textures.erase(p_param);
  118. VS::get_singleton()->shader_set_default_texture_param(shader,p_param,RID());
  119. }
  120. }
  121. Ref<Texture> Shader::get_default_texture_param(const StringName& p_param) const{
  122. if (default_textures.has(p_param))
  123. return default_textures[p_param];
  124. else
  125. return Ref<Texture>();
  126. }
  127. void Shader::get_default_texture_param_list(List<StringName>* r_textures) const{
  128. for(const Map<StringName,Ref<Texture> >::Element *E=default_textures.front();E;E=E->next()) {
  129. r_textures->push_back(E->key());
  130. }
  131. }
  132. void Shader::_bind_methods() {
  133. ObjectTypeDB::bind_method(_MD("get_mode"),&Shader::get_mode);
  134. ObjectTypeDB::bind_method(_MD("set_code","vcode","fcode","lcode","fofs","lofs"),&Shader::set_code,DEFVAL(0),DEFVAL(0));
  135. ObjectTypeDB::bind_method(_MD("get_vertex_code"),&Shader::get_vertex_code);
  136. ObjectTypeDB::bind_method(_MD("get_fragment_code"),&Shader::get_fragment_code);
  137. ObjectTypeDB::bind_method(_MD("get_light_code"),&Shader::get_light_code);
  138. ObjectTypeDB::bind_method(_MD("set_default_texture_param","param","texture:Texture"),&Shader::set_default_texture_param);
  139. ObjectTypeDB::bind_method(_MD("get_default_texture_param:Texture","param"),&Shader::get_default_texture_param);
  140. ObjectTypeDB::bind_method(_MD("has_param","name"),&Shader::has_param);
  141. ObjectTypeDB::bind_method(_MD("_set_code","code"),&Shader::_set_code);
  142. ObjectTypeDB::bind_method(_MD("_get_code"),&Shader::_get_code);
  143. //ObjectTypeDB::bind_method(_MD("get_param_list"),&Shader::get_fragment_code);
  144. ADD_PROPERTY( PropertyInfo(Variant::STRING, "_code",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR), _SCS("_set_code"), _SCS("_get_code") );
  145. BIND_CONSTANT( MODE_MATERIAL );
  146. BIND_CONSTANT( MODE_CANVAS_ITEM );
  147. BIND_CONSTANT( MODE_POST_PROCESS );
  148. }
  149. Shader::Shader(Mode p_mode) {
  150. mode=p_mode;
  151. shader = VisualServer::get_singleton()->shader_create(VS::ShaderMode(p_mode));
  152. params_cache_dirty=true;
  153. }
  154. Shader::~Shader() {
  155. VisualServer::get_singleton()->free(shader);
  156. }
  157. /************ Loader from text ***************/
  158. RES ResourceFormatLoaderShader::load(const String &p_path, const String& p_original_path, Error *r_error) {
  159. if (r_error)
  160. *r_error=ERR_FILE_CANT_OPEN;
  161. String fragment_code;
  162. String vertex_code;
  163. String light_code;
  164. int mode=-1;
  165. Error err;
  166. FileAccess *f = FileAccess::open(p_path,FileAccess::READ,&err);
  167. ERR_EXPLAIN("Unable to open shader file: "+p_path);
  168. ERR_FAIL_COND_V(err,RES());
  169. String base_path = p_path.get_base_dir();
  170. if (r_error)
  171. *r_error=ERR_FILE_CORRUPT;
  172. Ref<Shader> shader;//( memnew( Shader ) );
  173. int line=0;
  174. while(!f->eof_reached()) {
  175. String l = f->get_line();
  176. line++;
  177. if (mode<=0) {
  178. l = l.strip_edges();
  179. int comment = l.find(";");
  180. if (comment!=-1)
  181. l=l.substr(0,comment);
  182. }
  183. if (mode<1)
  184. vertex_code+="\n";
  185. if (mode<2)
  186. fragment_code+="\n";
  187. if (mode < 1 && l=="")
  188. continue;
  189. if (l.begins_with("[")) {
  190. l=l.strip_edges();
  191. if (l=="[params]") {
  192. if (mode>=0) {
  193. memdelete(f);
  194. ERR_EXPLAIN(p_path+":"+itos(line)+": Misplaced [params] section.");
  195. ERR_FAIL_V(RES());
  196. }
  197. mode=0;
  198. } else if (l=="[vertex]") {
  199. if (mode>=1) {
  200. memdelete(f);
  201. ERR_EXPLAIN(p_path+":"+itos(line)+": Misplaced [vertex] section.");
  202. ERR_FAIL_V(RES());
  203. }
  204. mode=1;
  205. } else if (l=="[fragment]") {
  206. if (mode>=2) {
  207. memdelete(f);
  208. ERR_EXPLAIN(p_path+":"+itos(line)+": Misplaced [fragment] section.");
  209. ERR_FAIL_V(RES());
  210. }
  211. mode=1;
  212. } else {
  213. memdelete(f);
  214. ERR_EXPLAIN(p_path+":"+itos(line)+": Unknown section type: '"+l+"'.");
  215. ERR_FAIL_V(RES());
  216. }
  217. continue;
  218. }
  219. if (mode==0) {
  220. int eqpos = l.find("=");
  221. if (eqpos==-1) {
  222. memdelete(f);
  223. ERR_EXPLAIN(p_path+":"+itos(line)+": Expected '='.");
  224. ERR_FAIL_V(RES());
  225. }
  226. String right=l.substr(eqpos+1,l.length()).strip_edges();
  227. if (right=="") {
  228. memdelete(f);
  229. ERR_EXPLAIN(p_path+":"+itos(line)+": Expected value after '='.");
  230. ERR_FAIL_V(RES());
  231. }
  232. Variant value;
  233. if (right=="true") {
  234. value = true;
  235. } else if (right=="false") {
  236. value = false;
  237. } else if (right.is_valid_float()) {
  238. //is number
  239. value = right.to_double();
  240. } else if (right.is_valid_html_color()) {
  241. //is html color
  242. value = Color::html(right);
  243. } else {
  244. //attempt to parse a constructor
  245. int popenpos = right.find("(");
  246. if (popenpos==-1) {
  247. memdelete(f);
  248. ERR_EXPLAIN(p_path+":"+itos(line)+": Invalid constructor syntax: "+right);
  249. ERR_FAIL_V(RES());
  250. }
  251. int pclosepos = right.find_last(")");
  252. if (pclosepos==-1) {
  253. ERR_EXPLAIN(p_path+":"+itos(line)+": Invalid constructor parameter syntax: "+right);
  254. ERR_FAIL_V(RES());
  255. }
  256. String type = right.substr(0,popenpos);
  257. String param = right.substr(popenpos+1,pclosepos-popenpos-1).strip_edges();
  258. if (type=="tex") {
  259. if (param=="") {
  260. value=RID();
  261. } else {
  262. String path;
  263. if (param.is_abs_path())
  264. path=param;
  265. else
  266. path=base_path+"/"+param;
  267. Ref<Texture> texture = ResourceLoader::load(path);
  268. if (!texture.is_valid()) {
  269. memdelete(f);
  270. ERR_EXPLAIN(p_path+":"+itos(line)+": Couldn't find icon at path: "+path);
  271. ERR_FAIL_V(RES());
  272. }
  273. value=texture;
  274. }
  275. } else if (type=="vec3") {
  276. if (param=="") {
  277. value=Vector3();
  278. } else {
  279. Vector<String> params = param.split(",");
  280. if (params.size()!=3) {
  281. memdelete(f);
  282. ERR_EXPLAIN(p_path+":"+itos(line)+": Invalid param count for vec3(): '"+right+"'.");
  283. ERR_FAIL_V(RES());
  284. }
  285. Vector3 v;
  286. for(int i=0;i<3;i++)
  287. v[i]=params[i].to_double();
  288. value=v;
  289. }
  290. } else if (type=="xform") {
  291. if (param=="") {
  292. value=Transform();
  293. } else {
  294. Vector<String> params = param.split(",");
  295. if (params.size()!=12) {
  296. memdelete(f);
  297. ERR_EXPLAIN(p_path+":"+itos(line)+": Invalid param count for xform(): '"+right+"'.");
  298. ERR_FAIL_V(RES());
  299. }
  300. Transform t;
  301. for(int i=0;i<9;i++)
  302. t.basis[i%3][i/3]=params[i].to_double();
  303. for(int i=0;i<3;i++)
  304. t.origin[i]=params[i-9].to_double();
  305. value=t;
  306. }
  307. } else {
  308. memdelete(f);
  309. ERR_EXPLAIN(p_path+":"+itos(line)+": Invalid constructor type: '"+type+"'.");
  310. ERR_FAIL_V(RES());
  311. }
  312. }
  313. String left= l.substr(0,eqpos);
  314. // shader->set_param(left,value);
  315. } else if (mode==1) {
  316. vertex_code+=l;
  317. } else if (mode==2) {
  318. fragment_code+=l;
  319. }
  320. }
  321. shader->set_code(vertex_code,fragment_code,light_code);
  322. f->close();
  323. memdelete(f);
  324. if (r_error)
  325. *r_error=OK;
  326. return shader;
  327. }
  328. void ResourceFormatLoaderShader::get_recognized_extensions(List<String> *p_extensions) const {
  329. p_extensions->push_back("shader");
  330. }
  331. bool ResourceFormatLoaderShader::handles_type(const String& p_type) const {
  332. return p_type=="Shader";
  333. }
  334. String ResourceFormatLoaderShader::get_resource_type(const String &p_path) const {
  335. if (p_path.extension().to_lower()=="shader")
  336. return "Shader";
  337. return "";
  338. }