From 7fbc99b35d4f58a51528d6c9b863a29764098554 Mon Sep 17 00:00:00 2001 From: Vasyl-Pidhirskyi Date: Thu, 20 Jul 2023 17:39:26 +0300 Subject: [PATCH] Implemented PreviewEngineGPU::render. Adjusted python.cc to use PreviewEngineGPU. --- source/blender/render/hydra/preview_engine.cc | 74 +++++++++++++++++++ source/blender/render/hydra/preview_engine.h | 7 ++ source/blender/render/hydra/python.cc | 7 +- 3 files changed, 87 insertions(+), 1 deletion(-) diff --git a/source/blender/render/hydra/preview_engine.cc b/source/blender/render/hydra/preview_engine.cc index bfe3572df3f2..cc9ced197865 100644 --- a/source/blender/render/hydra/preview_engine.cc +++ b/source/blender/render/hydra/preview_engine.cc @@ -1,6 +1,11 @@ /* SPDX-License-Identifier: Apache-2.0 * Copyright 2011-2022 Blender Foundation */ +#include + +#include "GPU_framebuffer.h" +#include "GPU_texture.h" + #include "IMB_imbuf_types.h" #include "preview_engine.h" @@ -34,6 +39,75 @@ void PreviewEngine::render(Depsgraph *depsgraph) update_render_result(pixels); } +void PreviewEngineGPU::render(Depsgraph *depsgraph) +{ + prepare_for_render(depsgraph); + + GPUFrameBuffer *framebuffer = GPU_framebuffer_create("fb_render_mat_prev_hydra"); + GPUTexture *tex_color = GPU_texture_create_2d("tex_render_mat_prev_hydra_color", + resolution_[0], + resolution_[1], + 1, + GPU_RGBA32F, + GPU_TEXTURE_USAGE_GENERAL, + nullptr); + GPUTexture *tex_depth = GPU_texture_create_2d("tex_render_mat_prev_hydra_depth", + resolution_[0], + resolution_[1], + 1, + GPU_DEPTH32F_STENCIL8, + GPU_TEXTURE_USAGE_GENERAL, + nullptr); + GPU_texture_filter_mode(tex_color, true); + GPU_texture_mipmap_mode(tex_color, true, true); + GPU_texture_filter_mode(tex_depth, true); + GPU_texture_mipmap_mode(tex_depth, true, true); + + GPU_framebuffer_ensure_config( + &framebuffer, {GPU_ATTACHMENT_TEXTURE(tex_depth), GPU_ATTACHMENT_TEXTURE(tex_color)}); + + GPU_framebuffer_bind(framebuffer); + + float clear_color[4] = {0.0f, 0.0f, 0.0f, 1.0f}; + + GPU_framebuffer_clear_color_depth(framebuffer, clear_color, 1.0f); + + /* Important: we have to create and bind at least one Vertex Array Object (VAO) before render + execution: More info at https://open.gl/drawing */ + GLuint VAO; + glGenVertexArrays(1, &VAO); + glBindVertexArray(VAO); + + engine_->Execute(render_index_.get(), &tasks_); + + std::vector &pixels = render_images_["Combined"]; + + while (true) { + if (RE_engine_test_break(bl_engine_)) { + break; + } + + if (render_task_delegate_->is_converged()) { + break; + } + + void *data = GPU_texture_read(tex_color, GPU_DATA_FLOAT, 0); + memcpy(pixels.data(), data, pixels.size() * sizeof(float)); + MEM_freeN(data); + update_render_result(); + } + + void *data = GPU_texture_read(tex_color, GPU_DATA_FLOAT, 0); + memcpy(pixels.data(), data, pixels.size() * sizeof(float)); + MEM_freeN(data); + update_render_result(); + + glDeleteVertexArrays(1, &VAO); + GPU_framebuffer_free(framebuffer); + GPU_texture_free(tex_color); + GPU_texture_free(tex_depth); +} + void PreviewEngine::update_render_result(std::vector &pixels) { RenderResult *result = RE_engine_begin_result( diff --git a/source/blender/render/hydra/preview_engine.h b/source/blender/render/hydra/preview_engine.h index 4dfc88d80028..a3fa9e045603 100644 --- a/source/blender/render/hydra/preview_engine.h +++ b/source/blender/render/hydra/preview_engine.h @@ -17,4 +17,11 @@ class PreviewEngine : public FinalEngine { void update_render_result(std::vector &pixels); }; +class PreviewEngineGPU : public FinalEngineGPU { + public: + using FinalEngineGPU::FinalEngineGPU; + + void render(Depsgraph *depsgraph) override; +}; + } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/python.cc b/source/blender/render/hydra/python.cc index 7f4799399c08..da8b5b96dce4 100644 --- a/source/blender/render/hydra/python.cc +++ b/source/blender/render/hydra/python.cc @@ -71,7 +71,12 @@ static PyObject *engine_create_func(PyObject * /*self*/, PyObject *args) engine = new ViewportEngine(bl_engine, render_delegate_id); } else if (STREQ(engine_type, "PREVIEW")) { - engine = new PreviewEngine(bl_engine, render_delegate_id); + if (bl_engine->type->flag & RE_USE_GPU_CONTEXT) { + engine = new PreviewEngineGPU(bl_engine, render_delegate_id); + } + else { + engine = new PreviewEngine(bl_engine, render_delegate_id); + } } else { if (bl_engine->type->flag & RE_USE_GPU_CONTEXT) { -- 2.30.2