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
31#include "roughness_limiter.h"
32#include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
33#include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"
34
35using namespace RendererRD;
36
37RoughnessLimiter::RoughnessLimiter() {
38 // Initialize roughness limiter
39 Vector<String> shader_modes;
40 shader_modes.push_back("");
41
42 shader.initialize(shader_modes);
43
44 shader_version = shader.version_create();
45
46 pipeline = RD::get_singleton()->compute_pipeline_create(shader.version_get_shader(shader_version, 0));
47}
48
49RoughnessLimiter::~RoughnessLimiter() {
50 shader.version_free(shader_version);
51}
52
53void RoughnessLimiter::roughness_limit(RID p_source_normal, RID p_roughness, const Size2i &p_size, float p_curve) {
54 UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
55 ERR_FAIL_NULL(uniform_set_cache);
56 MaterialStorage *material_storage = MaterialStorage::get_singleton();
57 ERR_FAIL_NULL(material_storage);
58
59 push_constant.screen_size[0] = p_size.x;
60 push_constant.screen_size[1] = p_size.y;
61 push_constant.curve = p_curve;
62
63 RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
64 RID rl_shader = shader.version_get_shader(shader_version, 0);
65
66 RD::Uniform u_source_normal(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_normal }));
67 RD::Uniform u_roughness(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_roughness }));
68
69 RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
70 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, pipeline);
71 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(rl_shader, 0, u_source_normal), 0);
72 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(rl_shader, 1, u_roughness), 1);
73
74 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(RoughnessLimiterPushConstant)); //not used but set anyway
75
76 RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.x, p_size.y, 1);
77
78 RD::get_singleton()->compute_list_end();
79}
80