shader.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "shader.h"
  24. #include "filesystem.h"
  25. #include "json_parser.h"
  26. #include "os.h"
  27. #include "reader_writer.h"
  28. #include "config.h"
  29. namespace crown
  30. {
  31. namespace shader_resource
  32. {
  33. static const char* _scplatform[Platform::COUNT] =
  34. {
  35. "linux",
  36. "windows",
  37. "android"
  38. };
  39. void compile(const char* path, CompileOptions& opts)
  40. {
  41. Buffer buf = opts.read(path);
  42. JSONParser json(array::begin(buf));
  43. JSONElement root = json.root();
  44. DynamicString vs_code2;
  45. DynamicString fs_code2;
  46. DynamicString varying_def;
  47. DynamicString common_code;
  48. DynamicString vs_in_out;
  49. DynamicString fs_in_out;
  50. root.key("vs_code").to_string(vs_code2);
  51. root.key("fs_code").to_string(fs_code2);
  52. root.key("varying_def").to_string(varying_def);
  53. root.key("common").to_string(common_code);
  54. root.key("vs_in_out").to_string(vs_in_out);
  55. root.key("fs_in_out").to_string(fs_in_out);
  56. DynamicString vs_code;
  57. DynamicString fs_code;
  58. vs_code += vs_in_out;
  59. vs_code += common_code;
  60. vs_code += vs_code2;
  61. fs_code += fs_in_out;
  62. fs_code += common_code;
  63. fs_code += fs_code2;
  64. DynamicString vs_code_path;
  65. DynamicString fs_code_path;
  66. DynamicString varying_def_path;
  67. DynamicString tmpvs_path;
  68. DynamicString tmpfs_path;
  69. opts.get_absolute_path("vs_code.tmp", vs_code_path);
  70. opts.get_absolute_path("fs_code.tmp", fs_code_path);
  71. opts.get_absolute_path("varying.tmp", varying_def_path);
  72. opts.get_absolute_path("tmpvs", tmpvs_path);
  73. opts.get_absolute_path("tmpfs", tmpfs_path);
  74. File* vs_file = opts._fs.open(vs_code_path.c_str(), FOM_WRITE);
  75. vs_file->write(vs_code.c_str(), vs_code.length());
  76. opts._fs.close(vs_file);
  77. File* fs_file = opts._fs.open(fs_code_path.c_str(), FOM_WRITE);
  78. fs_file->write(fs_code.c_str(), fs_code.length());
  79. opts._fs.close(fs_file);
  80. File* varying_file = opts._fs.open(varying_def_path.c_str(), FOM_WRITE);
  81. varying_file->write(varying_def.c_str(), varying_def.length());
  82. opts._fs.close(varying_file);
  83. const char* compile_vs[] =
  84. {
  85. "shaderc",
  86. "-f", vs_code_path.c_str(),
  87. "-o", tmpvs_path.c_str(),
  88. "--varyingdef", varying_def_path.c_str(),
  89. "--type", "vertex",
  90. "--platform", _scplatform[opts.platform()],
  91. #if CROWN_PLATFORM_WINDOWS
  92. "--profile", "vs_3_0",
  93. #endif
  94. NULL
  95. };
  96. int exitcode = os::execute_process(compile_vs);
  97. CE_ASSERT(exitcode == 0, "Failed to compile vertex shader");
  98. const char* compile_fs[] =
  99. {
  100. "shaderc",
  101. "-f", fs_code_path.c_str(),
  102. "-o", tmpfs_path.c_str(),
  103. "--varyingdef", varying_def_path.c_str(),
  104. "--type", "fragment",
  105. "--platform", _scplatform[opts.platform()],
  106. #if CROWN_PLATFORM_WINDOWS
  107. "--profile", "ps_3_0",
  108. #endif
  109. NULL
  110. };
  111. exitcode = os::execute_process(compile_fs);
  112. if (exitcode)
  113. {
  114. opts.delete_file(tmpvs_path.c_str());
  115. CE_ASSERT(exitcode == 0, "Failed to compile fragment shader");
  116. }
  117. Buffer tmpvs = opts.read(tmpvs_path.c_str());
  118. Buffer tmpfs = opts.read(tmpfs_path.c_str());
  119. opts.write(uint32_t(1)); // version
  120. opts.write(uint32_t(array::size(tmpvs)));
  121. opts.write(array::begin(tmpvs), array::size(tmpvs));
  122. opts.write(uint32_t(array::size(tmpfs)));
  123. opts.write(array::begin(tmpfs), array::size(tmpfs));
  124. opts.delete_file(vs_code_path.c_str());
  125. opts.delete_file(fs_code_path.c_str());
  126. opts.delete_file(varying_def_path.c_str());
  127. opts.delete_file(tmpvs_path.c_str());
  128. opts.delete_file(tmpfs_path.c_str());
  129. }
  130. void* load(File& file, Allocator& a)
  131. {
  132. BinaryReader br(file);
  133. uint32_t version;
  134. br.read(version);
  135. uint32_t vs_code_size;
  136. br.read(vs_code_size);
  137. const bgfx::Memory* vsmem = bgfx::alloc(vs_code_size);
  138. br.read(vsmem->data, vs_code_size);
  139. uint32_t fs_code_size;
  140. br.read(fs_code_size);
  141. const bgfx::Memory* fsmem = bgfx::alloc(fs_code_size);
  142. br.read(fsmem->data, fs_code_size);
  143. Shader* shader = (Shader*) a.allocate(sizeof(Shader));
  144. shader->vs = vsmem;
  145. shader->fs = fsmem;
  146. shader->program.idx = bgfx::invalidHandle;
  147. return shader;
  148. }
  149. void online(StringId64 id, ResourceManager& rm)
  150. {
  151. Shader* shader = (Shader*) rm.get(SHADER_TYPE, id);
  152. bgfx::ShaderHandle vs = bgfx::createShader(shader->vs);
  153. CE_ASSERT(bgfx::isValid(vs), "Failed to create vertex shader");
  154. bgfx::ShaderHandle fs = bgfx::createShader(shader->fs);
  155. CE_ASSERT(bgfx::isValid(fs), "Failed to create fragment shader");
  156. shader->program = bgfx::createProgram(vs, fs, true);
  157. CE_ASSERT(bgfx::isValid(shader->program), "Failed to create GPU program");
  158. }
  159. void offline(StringId64 id, ResourceManager& rm)
  160. {
  161. Shader* shader = (Shader*) rm.get(SHADER_TYPE, id);
  162. bgfx::destroyProgram(shader->program);
  163. }
  164. void unload(Allocator& a, void* res)
  165. {
  166. a.deallocate(res);
  167. }
  168. } // namespace shader_resource
  169. } // namespace crown