roughness_limiter.cpp 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**************************************************************************/
  2. /* roughness_limiter.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "roughness_limiter.h"
  31. #include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
  32. #include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"
  33. using namespace RendererRD;
  34. RoughnessLimiter::RoughnessLimiter() {
  35. // Initialize roughness limiter
  36. Vector<String> shader_modes;
  37. shader_modes.push_back("");
  38. shader.initialize(shader_modes);
  39. shader_version = shader.version_create();
  40. pipeline = RD::get_singleton()->compute_pipeline_create(shader.version_get_shader(shader_version, 0));
  41. }
  42. RoughnessLimiter::~RoughnessLimiter() {
  43. shader.version_free(shader_version);
  44. }
  45. void RoughnessLimiter::roughness_limit(RID p_source_normal, RID p_roughness, const Size2i &p_size, float p_curve) {
  46. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  47. ERR_FAIL_NULL(uniform_set_cache);
  48. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  49. ERR_FAIL_NULL(material_storage);
  50. push_constant.screen_size[0] = p_size.x;
  51. push_constant.screen_size[1] = p_size.y;
  52. push_constant.curve = p_curve;
  53. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  54. RID rl_shader = shader.version_get_shader(shader_version, 0);
  55. RD::Uniform u_source_normal(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_normal }));
  56. RD::Uniform u_roughness(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_roughness }));
  57. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  58. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, pipeline);
  59. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(rl_shader, 0, u_source_normal), 0);
  60. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(rl_shader, 1, u_roughness), 1);
  61. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(RoughnessLimiterPushConstant)); //not used but set anyway
  62. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.x, p_size.y, 1);
  63. RD::get_singleton()->compute_list_end();
  64. }