shader.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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* s_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_code;
  45. DynamicString fs_code;
  46. DynamicString varying_def;
  47. root.key("vs_code").to_string(vs_code);
  48. root.key("fs_code").to_string(fs_code);
  49. root.key("varying_def").to_string(varying_def);
  50. DynamicString vs_code_path;
  51. DynamicString fs_code_path;
  52. DynamicString varying_def_path;
  53. DynamicString tmpvs_path;
  54. DynamicString tmpfs_path;
  55. opts.get_absolute_path(vs_code.c_str(), vs_code_path);
  56. opts.get_absolute_path(fs_code.c_str(), fs_code_path);
  57. opts.get_absolute_path(varying_def.c_str(), varying_def_path);
  58. opts.get_absolute_path("tmpvs", tmpvs_path);
  59. opts.get_absolute_path("tmpfs", tmpfs_path);
  60. const char* compile_vs[] =
  61. {
  62. "shaderc",
  63. "-f", vs_code_path.c_str(),
  64. "-o", tmpvs_path.c_str(),
  65. "--varyingdef", varying_def_path.c_str(),
  66. "--type", "vertex",
  67. "--platform", s_scplatform[opts.platform()],
  68. #if CROWN_PLATFORM_WINDOWS
  69. "--profile", "vs_3_0",
  70. #endif
  71. NULL
  72. };
  73. os::execute_process(compile_vs);
  74. const char* compile_fs[] =
  75. {
  76. "shaderc",
  77. "-f", fs_code_path.c_str(),
  78. "-o", tmpfs_path.c_str(),
  79. "--varyingdef", varying_def_path.c_str(),
  80. "--type", "fragment",
  81. "--platform", s_scplatform[opts.platform()],
  82. #if CROWN_PLATFORM_WINDOWS
  83. "--profile", "ps_3_0",
  84. #endif
  85. NULL
  86. };
  87. os::execute_process(compile_fs);
  88. Buffer tmpvs = opts.read(tmpvs_path.c_str());
  89. Buffer tmpfs = opts.read(tmpfs_path.c_str());
  90. opts.write(uint32_t(1)); // version
  91. opts.write(uint32_t(array::size(tmpvs)));
  92. opts.write(array::begin(tmpvs), array::size(tmpvs));
  93. opts.write(uint32_t(array::size(tmpfs)));
  94. opts.write(array::begin(tmpfs), array::size(tmpfs));
  95. opts.delete_file(tmpvs_path.c_str());
  96. opts.delete_file(tmpfs_path.c_str());
  97. }
  98. void* load(File& file, Allocator& a)
  99. {
  100. BinaryReader br(file);
  101. uint32_t version;
  102. br.read(version);
  103. uint32_t vs_code_size;
  104. br.read(vs_code_size);
  105. const bgfx::Memory* vsmem = bgfx::alloc(vs_code_size);
  106. br.read(vsmem->data, vs_code_size);
  107. uint32_t fs_code_size;
  108. br.read(fs_code_size);
  109. const bgfx::Memory* fsmem = bgfx::alloc(fs_code_size);
  110. br.read(fsmem->data, fs_code_size);
  111. Shader* shader = (Shader*) a.allocate(sizeof(Shader));
  112. shader->vs = vsmem;
  113. shader->fs = fsmem;
  114. shader->program.idx = bgfx::invalidHandle;
  115. return shader;
  116. }
  117. void online(StringId64 id, ResourceManager& rm)
  118. {
  119. Shader* shader = (Shader*) rm.get(SHADER_TYPE, id);
  120. bgfx::ShaderHandle vs = bgfx::createShader(shader->vs);
  121. bgfx::ShaderHandle fs = bgfx::createShader(shader->fs);
  122. shader->program = bgfx::createProgram(vs, fs, true);
  123. }
  124. void offline(StringId64 id, ResourceManager& rm)
  125. {
  126. Shader* shader = (Shader*) rm.get(SHADER_TYPE, id);
  127. bgfx::destroyProgram(shader->program);
  128. }
  129. void unload(Allocator& a, void* res)
  130. {
  131. a.deallocate(res);
  132. }
  133. } // namespace shader_resource
  134. } // namespace crown