material_resource.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 "material_resource.h"
  24. #include "dynamic_string.h"
  25. #include "string_utils.h"
  26. #include "string_utils.h"
  27. #include "json_parser.h"
  28. #include "filesystem.h"
  29. #include "reader_writer.h"
  30. #include "os.h"
  31. namespace crown
  32. {
  33. namespace material_resource
  34. {
  35. struct Data
  36. {
  37. Array<TextureData> textures;
  38. Array<UniformData> uniforms;
  39. Array<char> dynamic;
  40. Data()
  41. : textures(default_allocator())
  42. , uniforms(default_allocator())
  43. , dynamic(default_allocator())
  44. {}
  45. };
  46. // Returns offset to start of data
  47. template <typename T>
  48. static uint32_t reserve_dynamic_data(T data, Array<char>& dynamic)
  49. {
  50. uint32_t offt = array::size(dynamic);
  51. array::push(dynamic, (char*) &data, sizeof(data));
  52. return offt;
  53. }
  54. static void parse_textures(JSONElement root, Array<TextureData>& textures, Array<char>& dynamic)
  55. {
  56. using namespace vector;
  57. Vector<DynamicString> keys(default_allocator());
  58. root.key("textures").to_keys(keys);
  59. for (uint32_t i = 0; i < size(keys); i++)
  60. {
  61. TextureHandle th;
  62. th.sampler_handle = 0;
  63. th.texture_handle = 0;
  64. ResourceId texid = root.key("textures").key(keys[i].c_str()).to_resource_id("texture");
  65. TextureData td;
  66. td.id = texid.name;
  67. td.data_offset = reserve_dynamic_data(th, dynamic);
  68. strncpy(td.sampler_name, keys[i].c_str(), 256);
  69. array::push_back(textures, td);
  70. }
  71. }
  72. struct UniformTypeInfo
  73. {
  74. const char* name;
  75. UniformType::Enum type;
  76. uint8_t size;
  77. };
  78. static const UniformTypeInfo s_uniform_type_info[UniformType::COUNT] =
  79. {
  80. { "float", UniformType::FLOAT, 4 },
  81. { "vector2", UniformType::VECTOR2, 8 },
  82. { "vector3", UniformType::VECTOR3, 12 },
  83. { "vector4", UniformType::VECTOR4, 16 }
  84. };
  85. static UniformType::Enum string_to_uniform_type(const char* str)
  86. {
  87. for (uint32_t i = 0; i < UniformType::COUNT; i++)
  88. {
  89. if (string::strcmp(str, s_uniform_type_info[i].name) == 0)
  90. return s_uniform_type_info[i].type;
  91. }
  92. CE_FATAL("Unknown uniform type");
  93. return UniformType::COUNT;
  94. }
  95. static void parse_uniforms(JSONElement root, Array<UniformData>& uniforms, Array<char>& dynamic)
  96. {
  97. using namespace vector;
  98. Vector<DynamicString> keys(default_allocator());
  99. root.key("uniforms").to_keys(keys);
  100. for (uint32_t i = 0; i < size(keys); i++)
  101. {
  102. UniformHandle uh;
  103. uh.uniform_handle = 0;
  104. DynamicString type;
  105. root.key("uniforms").key(keys[i].c_str()).key("type").to_string(type);
  106. UniformData ud;
  107. ud.type = string_to_uniform_type(type.c_str());
  108. ud.data_offset = reserve_dynamic_data(uh, dynamic);
  109. strncpy(ud.name, keys[i].c_str(), 256);
  110. switch (ud.type)
  111. {
  112. case UniformType::FLOAT:
  113. {
  114. float data = root.key("uniforms").key(keys[i].c_str()).key("value").to_int();
  115. reserve_dynamic_data(data, dynamic);
  116. break;
  117. }
  118. case UniformType::VECTOR2:
  119. {
  120. Vector2 data = root.key("uniforms").key(keys[i].c_str()).key("value").to_vector2();
  121. reserve_dynamic_data(data, dynamic);
  122. break;
  123. }
  124. case UniformType::VECTOR3:
  125. {
  126. Vector3 data = root.key("uniforms").key(keys[i].c_str()).key("value").to_vector3();
  127. reserve_dynamic_data(data, dynamic);
  128. break;
  129. }
  130. case UniformType::VECTOR4:
  131. {
  132. Vector4 data = root.key("uniforms").key(keys[i].c_str()).key("value").to_vector4();
  133. reserve_dynamic_data(data, dynamic);
  134. break;
  135. }
  136. default: CE_FATAL("Oops"); break;
  137. }
  138. array::push_back(uniforms, ud);
  139. }
  140. }
  141. //-----------------------------------------------------------------------------
  142. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  143. {
  144. File* file = fs.open(resource_path, FOM_READ);
  145. JSONParser json(*file);
  146. fs.close(file);
  147. JSONElement root = json.root();
  148. Array<TextureData> texdata(default_allocator());
  149. Array<UniformData> unidata(default_allocator());
  150. Array<char> dynblob(default_allocator());
  151. ResourceId shader = root.key("shader").to_resource_id("shader");
  152. parse_textures(root, texdata, dynblob);
  153. parse_uniforms(root, unidata, dynblob);
  154. MaterialHeader mh;
  155. mh.version = MATERIAL_VERSION;
  156. mh.shader = shader.name;
  157. mh.num_textures = array::size(texdata);
  158. mh.texture_data_offset = sizeof(mh);
  159. mh.num_uniforms = array::size(unidata);
  160. mh.uniform_data_offset = sizeof(mh) + sizeof(TextureData) * array::size(texdata);
  161. mh.dynamic_data_size = array::size(dynblob);
  162. mh.dynamic_data_offset = sizeof(mh) + sizeof(TextureData) * array::size(texdata) + sizeof(UniformData) * array::size(unidata);
  163. out_file->write((char*) &mh, sizeof(mh));
  164. out_file->write((char*) array::begin(texdata), sizeof(TextureData) * array::size(texdata));
  165. out_file->write((char*) array::begin(unidata), sizeof(UniformData) * array::size(unidata));
  166. out_file->write((char*) array::begin(dynblob), sizeof(char) * array::size(dynblob));
  167. }
  168. } // namespace material_resource
  169. } // namespace crown