material.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.h"
  24. #include "material_resource.h"
  25. #include "memory.h"
  26. #include "random.h"
  27. #include "device.h"
  28. #include "resource_manager.h"
  29. #include "texture_resource.h"
  30. #include "material_manager.h"
  31. #include "log.h"
  32. #include "shader.h"
  33. #include <bgfx.h>
  34. namespace crown
  35. {
  36. void Material::create(const MaterialResource* mr, MaterialManager& mm)
  37. {
  38. uint32_t size = mr->dynamic_data_size();
  39. uint32_t offt = mr->dynamic_data_offset();
  40. char* base = (char*) mr + offt;
  41. data = (char*) default_allocator().allocate(size);
  42. memcpy(data, base, size);
  43. for (uint32_t i = 0; i < mr->num_textures(); i++)
  44. {
  45. TextureData* ud = mr->get_texture_data(i);
  46. TextureHandle* th = mr->get_texture_handle(i, data);
  47. th->sampler_handle = bgfx::createUniform(ud->sampler_name, bgfx::UniformType::Uniform1iv).idx;
  48. ResourceId texid;
  49. texid.type = TEXTURE_TYPE;
  50. texid.name = ud->id;
  51. TextureImage* teximg = (TextureImage*) device()->resource_manager()->get(texid);
  52. th->texture_handle = teximg->handle.idx;
  53. }
  54. for (uint32_t i = 0; i < mr->num_uniforms(); i++)
  55. {
  56. UniformData* ud = mr->get_uniform_data(i);
  57. UniformHandle* uh = mr->get_uniform_handle(i, data);
  58. uh->uniform_handle = bgfx::createUniform(ud->name, bgfx::UniformType::Uniform4fv).idx;
  59. }
  60. resource = mr;
  61. }
  62. void Material::clone(const Material& m)
  63. {
  64. const MaterialResource* mr = m.resource;
  65. uint32_t size = mr->dynamic_data_size();
  66. char* base = m.data;
  67. data = (char*) default_allocator().allocate(size);
  68. memcpy(data, base, size);
  69. resource = mr;
  70. }
  71. void Material::destroy() const
  72. {
  73. for (uint32_t i = 0; i < resource->num_textures(); i++)
  74. {
  75. TextureHandle* th = resource->get_texture_handle(i, data);
  76. bgfx::UniformHandle sh;
  77. sh.idx = th->sampler_handle;
  78. bgfx::destroyUniform(sh);
  79. }
  80. for (uint32_t i = 0; i < resource->num_uniforms(); i++)
  81. {
  82. UniformHandle* uh = resource->get_uniform_handle(i, data);
  83. bgfx::UniformHandle bgfx_uh;
  84. bgfx_uh.idx = uh->uniform_handle;
  85. bgfx::destroyUniform(bgfx_uh);
  86. }
  87. default_allocator().deallocate(data);
  88. }
  89. void Material::bind() const
  90. {
  91. // bgfx::setProgram(program);
  92. ResourceId shader_id;
  93. shader_id.type = SHADER_TYPE;
  94. shader_id.name = resource->shader();
  95. Shader* shader = (Shader*) device()->resource_manager()->get(shader_id);
  96. bgfx::setProgram(shader->program);
  97. // Set samplers
  98. for (uint32_t i = 0; i < resource->num_textures(); i++)
  99. {
  100. TextureHandle* th = resource->get_texture_handle(i, data);
  101. bgfx::UniformHandle sampler;
  102. bgfx::TextureHandle texture;
  103. sampler.idx = th->sampler_handle;
  104. texture.idx = th->texture_handle;
  105. bgfx::setTexture(i, sampler, texture);
  106. }
  107. // Set uniforms
  108. for (uint32_t i = 0; i < resource->num_uniforms(); i++)
  109. {
  110. UniformHandle* uh = resource->get_uniform_handle(i, data);
  111. bgfx::UniformHandle buh;
  112. buh.idx = uh->uniform_handle;
  113. bgfx::setUniform(buh, (char*) uh + sizeof(uh->uniform_handle));
  114. }
  115. }
  116. void Material::set_float(const char* name, float val)
  117. {
  118. char* p = (char*) resource->get_uniform_handle_by_string(name, data);
  119. *((float*)(p + sizeof(uint32_t))) = val;
  120. }
  121. void Material::set_vector2(const char* name, const Vector2& val)
  122. {
  123. char* p = (char*) resource->get_uniform_handle_by_string(name, data);
  124. *((Vector2*)(p + sizeof(uint32_t))) = val;
  125. }
  126. void Material::set_vector3(const char* name, const Vector3& val)
  127. {
  128. char* p = (char*) resource->get_uniform_handle_by_string(name, data);
  129. *((Vector3*)(p + sizeof(uint32_t))) = val;
  130. }
  131. } // namespace crown