material_resource.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #pragma once
  24. #include "types.h"
  25. #include "resource.h"
  26. #include "vector3.h"
  27. #include "color4.h"
  28. #include "bundle.h"
  29. #include "allocator.h"
  30. #include "blob.h"
  31. #include "device.h"
  32. #include "file.h"
  33. #include "resource_manager.h"
  34. #include "material_manager.h"
  35. namespace crown
  36. {
  37. const uint32_t MATERIAL_VERSION = 1;
  38. struct MaterialHeader
  39. {
  40. uint32_t version;
  41. StringId64 shader;
  42. uint32_t num_textures;
  43. uint32_t texture_data_offset;
  44. uint32_t num_uniforms;
  45. uint32_t uniform_data_offset;
  46. uint32_t dynamic_data_size;
  47. uint32_t dynamic_data_offset;
  48. };
  49. struct TextureData
  50. {
  51. char sampler_name[256]; // Sampler uniform name
  52. StringId64 id; // Resource name
  53. uint32_t data_offset; // Offset into dynamic blob
  54. };
  55. struct TextureHandle
  56. {
  57. uint32_t sampler_handle;
  58. uint32_t texture_handle;
  59. };
  60. struct UniformType
  61. {
  62. enum Enum
  63. {
  64. FLOAT,
  65. VECTOR2,
  66. VECTOR3,
  67. VECTOR4,
  68. COUNT
  69. };
  70. };
  71. struct UniformData
  72. {
  73. char name[256]; // Uniform name
  74. uint32_t type; // UniformType::Enum
  75. uint32_t data_offset; // Offset into dynamic blob
  76. };
  77. struct UniformHandle
  78. {
  79. uint32_t uniform_handle;
  80. // data
  81. };
  82. struct MaterialResource
  83. {
  84. public:
  85. //-----------------------------------------------------------------------------
  86. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  87. {
  88. File* file = bundle.open(id);
  89. const size_t file_size = file->size();
  90. void* res = allocator.allocate(file_size);
  91. file->read(res, file_size);
  92. bundle.close(file);
  93. return res;
  94. }
  95. //-----------------------------------------------------------------------------
  96. static void online(StringId64 id, ResourceManager& rm)
  97. {
  98. ResourceId res_id;
  99. res_id.type = MATERIAL_TYPE;
  100. res_id.name = id;
  101. MaterialResource* mr = (MaterialResource*) rm.get(res_id);
  102. char* base = (char*) mr + mr->dynamic_data_offset();
  103. for (uint32_t i = 0; i < mr->num_textures(); i++)
  104. {
  105. TextureData* ud = mr->get_texture_data(i);
  106. TextureHandle* th = mr->get_texture_handle(i, base);
  107. th->sampler_handle = bgfx::createUniform(ud->sampler_name, bgfx::UniformType::Uniform1iv).idx;
  108. }
  109. for (uint32_t i = 0; i < mr->num_uniforms(); i++)
  110. {
  111. UniformData* ud = mr->get_uniform_data(i);
  112. UniformHandle* uh = mr->get_uniform_handle(i, base);
  113. uh->uniform_handle = bgfx::createUniform(ud->name, bgfx::UniformType::Uniform4fv).idx;
  114. }
  115. }
  116. //-----------------------------------------------------------------------------
  117. static void offline(StringId64 id, ResourceManager& rm)
  118. {
  119. ResourceId res_id;
  120. res_id.type = MATERIAL_TYPE;
  121. res_id.name = id;
  122. MaterialResource* mr = (MaterialResource*) rm.get(res_id);
  123. char* base = (char*) mr + mr->dynamic_data_offset();
  124. for (uint32_t i = 0; i < mr->num_textures(); i++)
  125. {
  126. TextureHandle* th = mr->get_texture_handle(i, base);
  127. bgfx::UniformHandle sh;
  128. sh.idx = th->sampler_handle;
  129. bgfx::destroyUniform(sh);
  130. }
  131. for (uint32_t i = 0; i < mr->num_uniforms(); i++)
  132. {
  133. UniformHandle* uh = mr->get_uniform_handle(i, base);
  134. bgfx::UniformHandle bgfx_uh;
  135. bgfx_uh.idx = uh->uniform_handle;
  136. bgfx::destroyUniform(bgfx_uh);
  137. }
  138. }
  139. //-----------------------------------------------------------------------------
  140. static void unload(Allocator& a, void* res)
  141. {
  142. a.deallocate(res);
  143. }
  144. uint32_t dynamic_data_size() const
  145. {
  146. MaterialHeader* mh = (MaterialHeader*) this;
  147. return mh->dynamic_data_size;
  148. }
  149. uint32_t dynamic_data_offset() const
  150. {
  151. MaterialHeader* mh = (MaterialHeader*) this;
  152. return mh->dynamic_data_offset;
  153. }
  154. StringId64 shader() const
  155. {
  156. MaterialHeader* mh = (MaterialHeader*) this;
  157. return mh->shader;
  158. }
  159. uint32_t num_textures() const
  160. {
  161. MaterialHeader* mh = (MaterialHeader*) this;
  162. return mh->num_textures;
  163. }
  164. uint32_t num_uniforms() const
  165. {
  166. MaterialHeader* mh = (MaterialHeader*) this;
  167. return mh->num_uniforms;
  168. }
  169. UniformData* get_uniform_data(uint32_t i) const
  170. {
  171. MaterialHeader* mh = (MaterialHeader*) this;
  172. UniformData* base = (UniformData*) ((char*) mh + mh->uniform_data_offset);
  173. return &base[i];
  174. }
  175. UniformData* get_uniform_data_by_string(const char* str) const
  176. {
  177. MaterialHeader* mh = (MaterialHeader*) this;
  178. UniformData* base = (UniformData*) ((char*) mh + mh->uniform_data_offset);
  179. uint32_t num = num_uniforms();
  180. for (uint32_t i = 0; i < num; i++)
  181. {
  182. if (string::strncmp(base->name, str, 256) == 0)
  183. return base;
  184. base++;
  185. }
  186. CE_FATAL("Oops, bad uniform name");
  187. return NULL;
  188. }
  189. TextureData* get_texture_data(uint32_t i) const
  190. {
  191. MaterialHeader* mh = (MaterialHeader*) this;
  192. TextureData* base = (TextureData*) ((char*) mh + mh->texture_data_offset);
  193. return &base[i];
  194. }
  195. UniformHandle* get_uniform_handle(uint32_t i, char* dynamic) const
  196. {
  197. UniformData* ud = get_uniform_data(i);
  198. return (UniformHandle*) (dynamic + ud->data_offset);
  199. }
  200. UniformHandle* get_uniform_handle_by_string(const char* str, char* dynamic) const
  201. {
  202. UniformData* ud = get_uniform_data_by_string(str);
  203. return (UniformHandle*) (dynamic + ud->data_offset);
  204. }
  205. TextureHandle* get_texture_handle(uint32_t i, char* dynamic) const
  206. {
  207. TextureData* td = get_texture_data(i);
  208. return (TextureHandle*) (dynamic + td->data_offset);
  209. }
  210. private:
  211. // Disable construction
  212. MaterialResource();
  213. };
  214. } // namespace crown