material.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2012-2024 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #include "core/strings/string_id.inl"
  6. #include "resource/material_resource.h"
  7. #include "resource/resource_manager.h"
  8. #include "resource/texture_resource.h"
  9. #include "world/material.h"
  10. #include "world/shader_manager.h"
  11. #include <bgfx/bgfx.h>
  12. namespace crown
  13. {
  14. void Material::bind(ShaderManager &sm, u8 view, s32 depth) const
  15. {
  16. using namespace material_resource;
  17. // Set samplers.
  18. const TextureData *td = texture_data_array(_resource);
  19. for (u32 i = 0; i < _resource->num_textures; ++i) {
  20. const TextureHandle *th = texture_handle(td, i, _data);
  21. bgfx::UniformHandle sampler;
  22. bgfx::TextureHandle texture;
  23. sampler.idx = th->sampler_handle;
  24. texture.idx = th->texture_handle;
  25. bgfx::setTexture(i
  26. , sampler
  27. , texture
  28. , sm.sampler_state(_resource->shader, td->name)
  29. );
  30. }
  31. // Set uniforms.
  32. const UniformData *ud = uniform_data_array(_resource);
  33. for (u32 i = 0; i < _resource->num_uniforms; ++i) {
  34. const UniformHandle *uh = uniform_handle(ud, i, _data);
  35. bgfx::UniformHandle buh;
  36. buh.idx = uh->uniform_handle;
  37. bgfx::setUniform(buh, (char *)uh + sizeof(uh->uniform_handle));
  38. }
  39. sm.submit(_resource->shader, view, depth);
  40. }
  41. template<typename T>
  42. static void set_uniform_value(StringId32 name, const T &value, const MaterialResource *mr, char *dynamic_data)
  43. {
  44. UniformData *ud = material_resource::uniform_data_array(mr);
  45. u32 idx = material_resource::uniform_data_index(mr, ud, name);
  46. char *p = (char *)material_resource::uniform_handle(ud, idx, dynamic_data);
  47. *(T *)(p + sizeof(u32)) = value;
  48. }
  49. void Material::set_float(StringId32 name, f32 value)
  50. {
  51. set_uniform_value(name, value, _resource, _data);
  52. }
  53. void Material::set_vector2(StringId32 name, const Vector2 &value)
  54. {
  55. set_uniform_value(name, value, _resource, _data);
  56. }
  57. void Material::set_vector3(StringId32 name, const Vector3 &value)
  58. {
  59. set_uniform_value(name, value, _resource, _data);
  60. }
  61. void Material::set_vector4(StringId32 name, const Vector4 &value)
  62. {
  63. set_uniform_value(name, value, _resource, _data);
  64. }
  65. void Material::set_matrix4x4(StringId32 name, const Matrix4x4 &value)
  66. {
  67. set_uniform_value(name, value, _resource, _data);
  68. }
  69. void Material::set_texture(StringId32 sampler_name, ResourceId texture_resource)
  70. {
  71. const TextureResource *tr = (TextureResource *)_resource_manager->get(RESOURCE_TYPE_TEXTURE, texture_resource);
  72. TextureData *td = material_resource::texture_data_array(_resource);
  73. u32 idx = material_resource::texture_data_index(_resource, td, sampler_name);
  74. TextureHandle *th = (TextureHandle *)material_resource::texture_handle(td, idx, _data);
  75. th->texture_handle = tr->handle.idx;
  76. }
  77. } // namespace crown