shader.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*************************************************************************/
  2. /* shader.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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. void Shader::set_mode(Mode p_mode) {
  35. ERR_FAIL_INDEX(p_mode,2);
  36. VisualServer::get_singleton()->shader_set_mode(shader,VisualServer::ShaderMode(p_mode));
  37. emit_signal(SceneStringNames::get_singleton()->changed);
  38. }
  39. Shader::Mode Shader::get_mode() const {
  40. return (Mode)VisualServer::get_singleton()->shader_get_mode(shader);
  41. }
  42. void Shader::set_code( const String& p_vertex, const String& p_fragment, const String& p_light,int p_fragment_ofs,int p_light_ofs) {
  43. VisualServer::get_singleton()->shader_set_code(shader,p_vertex,p_fragment,p_light,0,p_fragment_ofs,p_light_ofs);
  44. params_cache_dirty=true;
  45. emit_signal(SceneStringNames::get_singleton()->changed);
  46. }
  47. String Shader::get_vertex_code() const {
  48. return VisualServer::get_singleton()->shader_get_vertex_code(shader);
  49. }
  50. String Shader::get_fragment_code() const {
  51. return VisualServer::get_singleton()->shader_get_fragment_code(shader);
  52. }
  53. String Shader::get_light_code() const {
  54. return VisualServer::get_singleton()->shader_get_light_code(shader);
  55. }
  56. bool Shader::has_param(const StringName& p_param) const {
  57. if (params_cache_dirty)
  58. get_param_list(NULL);
  59. return (params_cache.has(p_param));
  60. }
  61. void Shader::get_param_list(List<PropertyInfo> *p_params) const {
  62. List<PropertyInfo> local;
  63. VisualServer::get_singleton()->shader_get_param_list(shader,&local);
  64. params_cache.clear();
  65. params_cache_dirty=false;
  66. for(List<PropertyInfo>::Element *E=local.front();E;E=E->next()) {
  67. PropertyInfo pi=E->get();
  68. pi.name="param/"+pi.name;
  69. params_cache[pi.name]=E->get().name;
  70. if (p_params) {
  71. //small little hack
  72. if (pi.type==Variant::_RID)
  73. pi.type=Variant::OBJECT;
  74. p_params->push_back(pi);
  75. }
  76. }
  77. }
  78. RID Shader::get_rid() const {
  79. return shader;
  80. }
  81. Dictionary Shader::_get_code() {
  82. String fs = VisualServer::get_singleton()->shader_get_fragment_code(shader);
  83. String vs = VisualServer::get_singleton()->shader_get_vertex_code(shader);
  84. String ls = VisualServer::get_singleton()->shader_get_light_code(shader);
  85. Dictionary c;
  86. c["fragment"]=fs;
  87. c["fragment_ofs"]=0;
  88. c["vertex"]=vs;
  89. c["vertex_ofs"]=0;
  90. c["light"]=ls;
  91. c["light_ofs"]=0;
  92. return c;
  93. }
  94. void Shader::_set_code(const Dictionary& p_string) {
  95. ERR_FAIL_COND(!p_string.has("fragment"));
  96. ERR_FAIL_COND(!p_string.has("vertex"));
  97. String light;
  98. if (p_string.has("light"))
  99. light=p_string["light"];
  100. set_code(p_string["vertex"],p_string["fragment"],light);
  101. }
  102. void Shader::_bind_methods() {
  103. ObjectTypeDB::bind_method(_MD("set_mode","mode"),&Shader::set_mode);
  104. ObjectTypeDB::bind_method(_MD("get_mode"),&Shader::get_mode);
  105. ObjectTypeDB::bind_method(_MD("set_code","vcode","fcode","lcode","fofs","lofs"),&Shader::set_code,DEFVAL(0),DEFVAL(0));
  106. ObjectTypeDB::bind_method(_MD("get_vertex_code"),&Shader::get_vertex_code);
  107. ObjectTypeDB::bind_method(_MD("get_fragment_code"),&Shader::get_fragment_code);
  108. ObjectTypeDB::bind_method(_MD("get_light_code"),&Shader::get_light_code);
  109. ObjectTypeDB::bind_method(_MD("has_param","name"),&Shader::has_param);
  110. ObjectTypeDB::bind_method(_MD("_set_code","code"),&Shader::_set_code);
  111. ObjectTypeDB::bind_method(_MD("_get_code"),&Shader::_get_code);
  112. //ObjectTypeDB::bind_method(_MD("get_param_list"),&Shader::get_fragment_code);
  113. ADD_PROPERTY( PropertyInfo(Variant::STRING, "_code",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR), _SCS("_set_code"), _SCS("_get_code") );
  114. BIND_CONSTANT( MODE_MATERIAL );
  115. BIND_CONSTANT( MODE_CANVAS_ITEM );
  116. BIND_CONSTANT( MODE_POST_PROCESS );
  117. }
  118. Shader::Shader() {
  119. shader = VisualServer::get_singleton()->shader_create();
  120. params_cache_dirty=true;
  121. }
  122. Shader::~Shader() {
  123. VisualServer::get_singleton()->free(shader);
  124. }
  125. /************ Loader from text ***************/
  126. RES ResourceFormatLoaderShader::load(const String &p_path,const String& p_original_path) {
  127. String fragment_code;
  128. String vertex_code;
  129. String light_code;
  130. int mode=-1;
  131. Error err;
  132. FileAccess *f = FileAccess::open(p_path,FileAccess::READ,&err);
  133. ERR_EXPLAIN("Unable to open shader file: "+p_path);
  134. ERR_FAIL_COND_V(err,RES());
  135. String base_path = p_path.get_base_dir();
  136. Ref<Shader> shader( memnew( Shader ) );
  137. int line=0;
  138. while(!f->eof_reached()) {
  139. String l = f->get_line();
  140. line++;
  141. if (mode<=0) {
  142. l = l.strip_edges();
  143. int comment = l.find(";");
  144. if (comment!=-1)
  145. l=l.substr(0,comment);
  146. }
  147. if (mode<1)
  148. vertex_code+="\n";
  149. if (mode<2)
  150. fragment_code+="\n";
  151. if (mode < 1 && l=="")
  152. continue;
  153. if (l.begins_with("[")) {
  154. l=l.strip_edges();
  155. if (l=="[params]") {
  156. if (mode>=0) {
  157. memdelete(f);
  158. ERR_EXPLAIN(p_path+":"+itos(line)+": Misplaced [params] section.");
  159. ERR_FAIL_V(RES());
  160. }
  161. mode=0;
  162. } else if (l=="[vertex]") {
  163. if (mode>=1) {
  164. memdelete(f);
  165. ERR_EXPLAIN(p_path+":"+itos(line)+": Misplaced [vertex] section.");
  166. ERR_FAIL_V(RES());
  167. }
  168. mode=1;
  169. } else if (l=="[fragment]") {
  170. if (mode>=2) {
  171. memdelete(f);
  172. ERR_EXPLAIN(p_path+":"+itos(line)+": Misplaced [fragment] section.");
  173. ERR_FAIL_V(RES());
  174. }
  175. mode=1;
  176. } else {
  177. memdelete(f);
  178. ERR_EXPLAIN(p_path+":"+itos(line)+": Unknown section type: '"+l+"'.");
  179. ERR_FAIL_V(RES());
  180. }
  181. continue;
  182. }
  183. if (mode==0) {
  184. int eqpos = l.find("=");
  185. if (eqpos==-1) {
  186. memdelete(f);
  187. ERR_EXPLAIN(p_path+":"+itos(line)+": Expected '='.");
  188. ERR_FAIL_V(RES());
  189. }
  190. String right=l.substr(eqpos+1,l.length()).strip_edges();
  191. if (right=="") {
  192. memdelete(f);
  193. ERR_EXPLAIN(p_path+":"+itos(line)+": Expected value after '='.");
  194. ERR_FAIL_V(RES());
  195. }
  196. Variant value;
  197. if (right=="true") {
  198. value = true;
  199. } else if (right=="false") {
  200. value = false;
  201. } else if (right.is_valid_float()) {
  202. //is number
  203. value = right.to_double();
  204. } else if (right.is_valid_html_color()) {
  205. //is html color
  206. value = Color::html(right);
  207. } else {
  208. //attempt to parse a constructor
  209. int popenpos = right.find("(");
  210. if (popenpos==-1) {
  211. memdelete(f);
  212. ERR_EXPLAIN(p_path+":"+itos(line)+": Invalid constructor syntax: "+right);
  213. ERR_FAIL_V(RES());
  214. }
  215. int pclosepos = right.find_last(")");
  216. if (pclosepos==-1) {
  217. ERR_EXPLAIN(p_path+":"+itos(line)+": Invalid constructor parameter syntax: "+right);
  218. ERR_FAIL_V(RES());
  219. }
  220. String type = right.substr(0,popenpos);
  221. String param = right.substr(popenpos+1,pclosepos-popenpos-1).strip_edges();
  222. print_line("type: "+type+" param: "+param);
  223. if (type=="tex") {
  224. if (param=="") {
  225. value=RID();
  226. } else {
  227. String path;
  228. if (param.is_abs_path())
  229. path=param;
  230. else
  231. path=base_path+"/"+param;
  232. Ref<Texture> texture = ResourceLoader::load(path);
  233. if (!texture.is_valid()) {
  234. memdelete(f);
  235. ERR_EXPLAIN(p_path+":"+itos(line)+": Couldn't find icon at path: "+path);
  236. ERR_FAIL_V(RES());
  237. }
  238. value=texture;
  239. }
  240. } else if (type=="vec3") {
  241. if (param=="") {
  242. value=Vector3();
  243. } else {
  244. Vector<String> params = param.split(",");
  245. if (params.size()!=3) {
  246. memdelete(f);
  247. ERR_EXPLAIN(p_path+":"+itos(line)+": Invalid param count for vec3(): '"+right+"'.");
  248. ERR_FAIL_V(RES());
  249. }
  250. Vector3 v;
  251. for(int i=0;i<3;i++)
  252. v[i]=params[i].to_double();
  253. value=v;
  254. }
  255. } else if (type=="xform") {
  256. if (param=="") {
  257. value=Transform();
  258. } else {
  259. Vector<String> params = param.split(",");
  260. if (params.size()!=12) {
  261. memdelete(f);
  262. ERR_EXPLAIN(p_path+":"+itos(line)+": Invalid param count for xform(): '"+right+"'.");
  263. ERR_FAIL_V(RES());
  264. }
  265. Transform t;
  266. for(int i=0;i<9;i++)
  267. t.basis[i%3][i/3]=params[i].to_double();
  268. for(int i=0;i<3;i++)
  269. t.origin[i]=params[i-9].to_double();
  270. value=t;
  271. }
  272. } else {
  273. memdelete(f);
  274. ERR_EXPLAIN(p_path+":"+itos(line)+": Invalid constructor type: '"+type+"'.");
  275. ERR_FAIL_V(RES());
  276. }
  277. }
  278. String left= l.substr(0,eqpos);
  279. // shader->set_param(left,value);
  280. } else if (mode==1) {
  281. vertex_code+=l;
  282. } else if (mode==2) {
  283. fragment_code+=l;
  284. }
  285. }
  286. shader->set_code(vertex_code,fragment_code,light_code);
  287. f->close();
  288. memdelete(f);
  289. return shader;
  290. }
  291. void ResourceFormatLoaderShader::get_recognized_extensions(List<String> *p_extensions) const {
  292. p_extensions->push_back("shader");
  293. }
  294. bool ResourceFormatLoaderShader::handles_type(const String& p_type) const {
  295. return p_type=="Shader";
  296. }
  297. String ResourceFormatLoaderShader::get_resource_type(const String &p_path) const {
  298. if (p_path.extension().to_lower()=="shader")
  299. return "Shader";
  300. return "";
  301. }