0.2.0-dev Download ZIP
173 lines ·9.6 KB Raw
| 1 | /**************************************************************************/ |
| 2 | /* resolve.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 "resolve.h" |
| 32 | |
| 33 | #include "servers/rendering/renderer_rd/storage_rd/material_storage.h" |
| 34 | #include "servers/rendering/renderer_rd/uniform_set_cache_rd.h" |
| 35 | |
| 36 | using namespace RendererRD; |
| 37 | |
| 38 | Resolve::Resolve(bool p_prefer_raster_effects) { |
| 39 | prefer_raster_effects = p_prefer_raster_effects; |
| 40 | |
| 41 | if (prefer_raster_effects) { |
| 42 | Vector<String> resolve_modes; |
| 43 | resolve_modes.push_back(""); |
| 44 | |
| 45 | resolve_raster.shader.initialize(resolve_modes); |
| 46 | resolve_raster.shader_version = resolve_raster.shader.version_create(); |
| 47 | resolve_raster.pipeline.setup(resolve_raster.shader.version_get_shader(resolve_raster.shader_version, 0), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0); |
| 48 | } else { |
| 49 | Vector<String> resolve_modes; |
| 50 | resolve_modes.push_back("\n#define MODE_RESOLVE_GI\n"); |
| 51 | resolve_modes.push_back("\n#define MODE_RESOLVE_GI\n#define VOXEL_GI_RESOLVE\n"); |
| 52 | resolve_modes.push_back("\n#define MODE_RESOLVE_DEPTH\n"); |
| 53 | |
| 54 | resolve.shader.initialize(resolve_modes); |
| 55 | |
| 56 | resolve.shader_version = resolve.shader.version_create(); |
| 57 | |
| 58 | for (int i = 0; i < RESOLVE_MODE_MAX; i++) { |
| 59 | resolve.pipelines[i] = RD::get_singleton()->compute_pipeline_create(resolve.shader.version_get_shader(resolve.shader_version, i)); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | Resolve::~Resolve() { |
| 65 | if (prefer_raster_effects) { |
| 66 | resolve_raster.shader.version_free(resolve_raster.shader_version); |
| 67 | } else { |
| 68 | resolve.shader.version_free(resolve.shader_version); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void Resolve::resolve_gi(RID p_source_depth, RID p_source_normal_roughness, RID p_source_voxel_gi, RID p_dest_depth, RID p_dest_normal_roughness, RID p_dest_voxel_gi, Vector2i p_screen_size, int p_samples) { |
| 73 | ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute shader resolve with the mobile renderer."); |
| 74 | |
| 75 | UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton(); |
| 76 | ERR_FAIL_NULL(uniform_set_cache); |
| 77 | MaterialStorage *material_storage = MaterialStorage::get_singleton(); |
| 78 | ERR_FAIL_NULL(material_storage); |
| 79 | |
| 80 | ResolvePushConstant push_constant; |
| 81 | push_constant.screen_size[0] = p_screen_size.x; |
| 82 | push_constant.screen_size[1] = p_screen_size.y; |
| 83 | push_constant.samples = p_samples; |
| 84 | |
| 85 | // setup our uniforms |
| 86 | RID default_sampler = material_storage->sampler_rd_get_default(RSE::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RSE::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 87 | |
| 88 | RD::Uniform u_source_depth(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_depth })); |
| 89 | RD::Uniform u_source_normal_roughness(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 1, Vector<RID>({ default_sampler, p_source_normal_roughness })); |
| 90 | RD::Uniform u_dest_depth(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_dest_depth })); |
| 91 | RD::Uniform u_dest_normal_roughness(RD::UNIFORM_TYPE_IMAGE, 1, Vector<RID>({ p_dest_normal_roughness })); |
| 92 | |
| 93 | ResolveMode mode = p_source_voxel_gi.is_valid() ? RESOLVE_MODE_GI_VOXEL_GI : RESOLVE_MODE_GI; |
| 94 | RID shader = resolve.shader.version_get_shader(resolve.shader_version, mode); |
| 95 | ERR_FAIL_COND(shader.is_null()); |
| 96 | |
| 97 | RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin(); |
| 98 | RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, resolve.pipelines[mode]); |
| 99 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_depth, u_source_normal_roughness), 0); |
| 100 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_dest_depth, u_dest_normal_roughness), 1); |
| 101 | if (p_source_voxel_gi.is_valid()) { |
| 102 | RD::Uniform u_source_voxel_gi(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_voxel_gi })); |
| 103 | RD::Uniform u_dest_voxel_gi(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_voxel_gi); |
| 104 | |
| 105 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 2, u_source_voxel_gi), 2); |
| 106 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_voxel_gi), 3); |
| 107 | } |
| 108 | |
| 109 | RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ResolvePushConstant)); |
| 110 | |
| 111 | RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.x, p_screen_size.y, 1); |
| 112 | |
| 113 | RD::get_singleton()->compute_list_end(); |
| 114 | } |
| 115 | |
| 116 | void Resolve::resolve_depth(RID p_source_depth, RID p_dest_depth, Vector2i p_screen_size, int p_samples) { |
| 117 | ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute shader resolve with the mobile renderer."); |
| 118 | |
| 119 | UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton(); |
| 120 | ERR_FAIL_NULL(uniform_set_cache); |
| 121 | MaterialStorage *material_storage = MaterialStorage::get_singleton(); |
| 122 | ERR_FAIL_NULL(material_storage); |
| 123 | |
| 124 | ResolvePushConstant push_constant; |
| 125 | push_constant.screen_size[0] = p_screen_size.x; |
| 126 | push_constant.screen_size[1] = p_screen_size.y; |
| 127 | push_constant.samples = p_samples; |
| 128 | |
| 129 | // setup our uniforms |
| 130 | RID default_sampler = material_storage->sampler_rd_get_default(RSE::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RSE::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 131 | |
| 132 | RD::Uniform u_source_depth(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_depth })); |
| 133 | RD::Uniform u_dest_depth(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_depth); |
| 134 | |
| 135 | ResolveMode mode = RESOLVE_MODE_DEPTH; |
| 136 | RID shader = resolve.shader.version_get_shader(resolve.shader_version, mode); |
| 137 | ERR_FAIL_COND(shader.is_null()); |
| 138 | |
| 139 | RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin(); |
| 140 | RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, resolve.pipelines[mode]); |
| 141 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_depth), 0); |
| 142 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_dest_depth), 1); |
| 143 | |
| 144 | RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ResolvePushConstant)); |
| 145 | |
| 146 | RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.x, p_screen_size.y, 1); |
| 147 | |
| 148 | RD::get_singleton()->compute_list_end(); |
| 149 | } |
| 150 | |
| 151 | void Resolve::resolve_depth_raster(RID p_source_rd_texture, RID p_dest_framebuffer, int p_samples) { |
| 152 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 153 | UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton(); |
| 154 | |
| 155 | memset(&resolve_raster.push_constant, 0, sizeof(ResolvePushConstant)); |
| 156 | resolve_raster.push_constant.samples = p_samples; |
| 157 | |
| 158 | RID default_sampler = material_storage->sampler_rd_get_default(RSE::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RSE::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 159 | |
| 160 | RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture })); |
| 161 | |
| 162 | RID shader = resolve_raster.shader.version_get_shader(resolve_raster.shader_version, 0); |
| 163 | ERR_FAIL_COND(shader.is_null()); |
| 164 | |
| 165 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer); |
| 166 | RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, resolve_raster.pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer))); |
| 167 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0); |
| 168 | |
| 169 | RD::get_singleton()->draw_list_set_push_constant(draw_list, &resolve_raster.push_constant, sizeof(ResolvePushConstant)); |
| 170 | |
| 171 | RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u); |
| 172 | RD::get_singleton()->draw_list_end(); |
| 173 | } |