2019-06-22 18:34:26 +02:00
|
|
|
/*************************************************************************/
|
2020-12-04 19:26:24 +01:00
|
|
|
/* renderer_compositor_rd.cpp */
|
2019-06-22 18:34:26 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2019-06-22 18:34:26 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2020-12-04 19:26:24 +01:00
|
|
|
#include "renderer_compositor_rd.h"
|
2019-06-11 20:43:37 +02:00
|
|
|
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/config/project_settings.h"
|
2020-04-29 22:34:09 +02:00
|
|
|
|
2020-12-04 19:26:24 +01:00
|
|
|
void RendererCompositorRD::prepare_for_blitting_render_targets() {
|
2019-06-24 21:13:06 +02:00
|
|
|
RD::get_singleton()->prepare_screen_for_drawing();
|
|
|
|
}
|
|
|
|
|
2020-12-04 19:26:24 +01:00
|
|
|
void RendererCompositorRD::blit_render_targets_to_screen(DisplayServer::WindowID p_screen, const BlitToScreen *p_render_targets, int p_amount) {
|
2019-06-16 04:45:24 +02:00
|
|
|
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin_for_screen(p_screen);
|
2019-06-19 22:03:19 +02:00
|
|
|
|
2019-06-16 04:45:24 +02:00
|
|
|
for (int i = 0; i < p_amount; i++) {
|
|
|
|
RID texture = storage->render_target_get_texture(p_render_targets[i].render_target);
|
|
|
|
ERR_CONTINUE(texture.is_null());
|
|
|
|
RID rd_texture = storage->texture_get_rd_texture(texture);
|
|
|
|
ERR_CONTINUE(rd_texture.is_null());
|
|
|
|
if (!render_target_descriptors.has(rd_texture) || !RD::get_singleton()->uniform_set_is_valid(render_target_descriptors[rd_texture])) {
|
|
|
|
Vector<RD::Uniform> uniforms;
|
|
|
|
RD::Uniform u;
|
2020-10-17 03:19:21 +02:00
|
|
|
u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
|
2019-06-16 04:45:24 +02:00
|
|
|
u.binding = 0;
|
|
|
|
u.ids.push_back(copy_viewports_sampler);
|
|
|
|
u.ids.push_back(rd_texture);
|
2019-06-19 22:03:19 +02:00
|
|
|
uniforms.push_back(u);
|
2019-06-16 04:45:24 +02:00
|
|
|
RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, copy_viewports_rd_shader, 0);
|
|
|
|
|
|
|
|
render_target_descriptors[rd_texture] = uniform_set;
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2 screen_size(RD::get_singleton()->screen_get_width(p_screen), RD::get_singleton()->screen_get_height(p_screen));
|
|
|
|
|
|
|
|
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, copy_viewports_rd_pipeline);
|
|
|
|
RD::get_singleton()->draw_list_bind_index_array(draw_list, copy_viewports_rd_array);
|
|
|
|
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, render_target_descriptors[rd_texture], 0);
|
|
|
|
|
|
|
|
float push_constant[4] = {
|
|
|
|
p_render_targets[i].rect.position.x / screen_size.width,
|
|
|
|
p_render_targets[i].rect.position.y / screen_size.height,
|
|
|
|
p_render_targets[i].rect.size.width / screen_size.width,
|
|
|
|
p_render_targets[i].rect.size.height / screen_size.height,
|
|
|
|
};
|
|
|
|
RD::get_singleton()->draw_list_set_push_constant(draw_list, push_constant, 4 * sizeof(float));
|
|
|
|
RD::get_singleton()->draw_list_draw(draw_list, true);
|
|
|
|
}
|
2019-06-19 22:03:19 +02:00
|
|
|
|
|
|
|
RD::get_singleton()->draw_list_end();
|
2019-06-16 04:45:24 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 19:26:24 +01:00
|
|
|
void RendererCompositorRD::begin_frame(double frame_step) {
|
2019-07-29 20:29:43 +02:00
|
|
|
frame++;
|
2020-05-01 14:34:23 +02:00
|
|
|
delta = frame_step;
|
2019-07-21 16:31:30 +02:00
|
|
|
time += frame_step;
|
2020-04-29 22:34:09 +02:00
|
|
|
|
|
|
|
double time_roll_over = GLOBAL_GET("rendering/limits/time/time_rollover_secs");
|
|
|
|
time = Math::fmod(time, time_roll_over);
|
|
|
|
|
2019-07-21 16:31:30 +02:00
|
|
|
canvas->set_time(time);
|
2020-01-12 02:26:52 +01:00
|
|
|
scene->set_time(time, frame_step);
|
2019-06-16 04:45:24 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 19:26:24 +01:00
|
|
|
void RendererCompositorRD::end_frame(bool p_swap_buffers) {
|
2019-06-22 18:34:26 +02:00
|
|
|
#ifndef _MSC_VER
|
2020-08-27 11:41:49 +02:00
|
|
|
#warning TODO: likely pass a bool to swap buffers to avoid display?
|
2019-06-22 18:34:26 +02:00
|
|
|
#endif
|
2019-10-05 15:27:43 +02:00
|
|
|
RD::get_singleton()->swap_buffers(); //probably should pass some bool to avoid display?
|
2019-06-16 04:45:24 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 19:26:24 +01:00
|
|
|
void RendererCompositorRD::initialize() {
|
2019-06-16 04:45:24 +02:00
|
|
|
{ //create framebuffer copy shader
|
2019-07-29 00:58:32 +02:00
|
|
|
RenderingDevice::ShaderStageData vert;
|
2019-06-16 04:45:24 +02:00
|
|
|
vert.shader_stage = RenderingDevice::SHADER_STAGE_VERTEX;
|
2019-07-29 00:58:32 +02:00
|
|
|
vert.spir_v = RenderingDevice::get_singleton()->shader_compile_from_source(RenderingDevice::SHADER_STAGE_VERTEX,
|
2019-06-16 04:45:24 +02:00
|
|
|
"#version 450\n"
|
|
|
|
"layout(push_constant, binding = 0, std140) uniform Pos { vec4 dst_rect; } pos;\n"
|
|
|
|
"layout(location =0) out vec2 uv;\n"
|
|
|
|
"void main() { \n"
|
2019-06-19 22:03:19 +02:00
|
|
|
" vec2 base_arr[4] = vec2[](vec2(0.0,0.0),vec2(0.0,1.0),vec2(1.0,1.0),vec2(1.0,0.0));\n"
|
2019-06-16 04:45:24 +02:00
|
|
|
" uv = base_arr[gl_VertexIndex];\n"
|
2019-06-19 22:03:19 +02:00
|
|
|
" vec2 vtx = pos.dst_rect.xy+uv*pos.dst_rect.zw;\n"
|
|
|
|
" gl_Position = vec4(vtx * 2.0 - 1.0,0.0,1.0);\n"
|
2019-07-29 00:58:32 +02:00
|
|
|
"}\n");
|
2019-06-16 04:45:24 +02:00
|
|
|
|
2019-07-29 00:58:32 +02:00
|
|
|
RenderingDevice::ShaderStageData frag;
|
2019-06-16 04:45:24 +02:00
|
|
|
frag.shader_stage = RenderingDevice::SHADER_STAGE_FRAGMENT;
|
2019-07-29 17:59:18 +02:00
|
|
|
frag.spir_v = RenderingDevice::get_singleton()->shader_compile_from_source(RenderingDevice::SHADER_STAGE_FRAGMENT,
|
2019-06-16 04:45:24 +02:00
|
|
|
"#version 450\n"
|
|
|
|
"layout (location = 0) in vec2 uv;\n"
|
|
|
|
"layout (location = 0) out vec4 color;\n"
|
|
|
|
"layout (binding = 0) uniform sampler2D src_rt;\n"
|
2019-07-29 00:58:32 +02:00
|
|
|
"void main() { color=texture(src_rt,uv); }\n");
|
2019-06-16 04:45:24 +02:00
|
|
|
|
2019-07-29 00:58:32 +02:00
|
|
|
Vector<RenderingDevice::ShaderStageData> source;
|
2019-06-16 04:45:24 +02:00
|
|
|
source.push_back(vert);
|
|
|
|
source.push_back(frag);
|
|
|
|
String error;
|
2019-07-29 00:58:32 +02:00
|
|
|
copy_viewports_rd_shader = RD::get_singleton()->shader_create(source);
|
2019-06-16 04:45:24 +02:00
|
|
|
if (!copy_viewports_rd_shader.is_valid()) {
|
2020-02-13 18:50:07 +01:00
|
|
|
print_line("Failed compilation: " + error);
|
2019-06-16 04:45:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{ //create index array for copy shader
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector<uint8_t> pv;
|
2019-06-16 04:45:24 +02:00
|
|
|
pv.resize(6 * 4);
|
|
|
|
{
|
2020-02-17 22:06:54 +01:00
|
|
|
uint8_t *w = pv.ptrw();
|
|
|
|
int *p32 = (int *)w;
|
2019-06-16 04:45:24 +02:00
|
|
|
p32[0] = 0;
|
|
|
|
p32[1] = 1;
|
|
|
|
p32[2] = 2;
|
|
|
|
p32[3] = 0;
|
|
|
|
p32[4] = 2;
|
|
|
|
p32[5] = 3;
|
|
|
|
}
|
|
|
|
copy_viewports_rd_index_buffer = RD::get_singleton()->index_buffer_create(6, RenderingDevice::INDEX_BUFFER_FORMAT_UINT32, pv);
|
|
|
|
copy_viewports_rd_array = RD::get_singleton()->index_array_create(copy_viewports_rd_index_buffer, 0, 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
{ //pipeline
|
|
|
|
copy_viewports_rd_pipeline = RD::get_singleton()->render_pipeline_create(copy_viewports_rd_shader, RD::get_singleton()->screen_get_framebuffer_format(), RD::INVALID_ID, RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RenderingDevice::PipelineColorBlendState::create_disabled(), 0);
|
|
|
|
}
|
|
|
|
{ // sampler
|
|
|
|
copy_viewports_sampler = RD::get_singleton()->sampler_create(RD::SamplerState());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 19:26:24 +01:00
|
|
|
ThreadWorkPool RendererCompositorRD::thread_work_pool;
|
|
|
|
uint64_t RendererCompositorRD::frame = 1;
|
2019-07-29 17:59:18 +02:00
|
|
|
|
2020-12-04 19:26:24 +01:00
|
|
|
void RendererCompositorRD::finalize() {
|
2019-07-29 17:59:18 +02:00
|
|
|
thread_work_pool.finish();
|
|
|
|
|
2019-06-16 04:45:24 +02:00
|
|
|
memdelete(scene);
|
|
|
|
memdelete(canvas);
|
|
|
|
memdelete(storage);
|
|
|
|
|
|
|
|
//only need to erase these, the rest are erased by cascade
|
|
|
|
RD::get_singleton()->free(copy_viewports_rd_index_buffer);
|
|
|
|
RD::get_singleton()->free(copy_viewports_rd_shader);
|
|
|
|
RD::get_singleton()->free(copy_viewports_sampler);
|
|
|
|
}
|
|
|
|
|
2020-12-04 19:26:24 +01:00
|
|
|
RendererCompositorRD *RendererCompositorRD::singleton = nullptr;
|
2020-05-01 14:34:23 +02:00
|
|
|
|
2020-12-04 19:26:24 +01:00
|
|
|
RendererCompositorRD::RendererCompositorRD() {
|
2020-05-01 14:34:23 +02:00
|
|
|
singleton = this;
|
2019-07-29 17:59:18 +02:00
|
|
|
thread_work_pool.init();
|
2019-07-21 16:31:30 +02:00
|
|
|
time = 0;
|
2019-07-29 20:29:43 +02:00
|
|
|
|
2020-12-04 19:26:24 +01:00
|
|
|
storage = memnew(RendererStorageRD);
|
|
|
|
canvas = memnew(RendererCanvasRenderRD(storage));
|
|
|
|
scene = memnew(RendererSceneRenderForward(storage));
|
2019-06-11 20:43:37 +02:00
|
|
|
}
|