From 8ab42153cd49e386a62be615a8b1088ebaeb265c Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 5 Jul 2023 17:59:38 +0300 Subject: [PATCH 1/9] Fix rendering of Final render for Storm delegate. Initial fixes --- source/blender/render/hydra/final_engine.cc | 137 +++++++++++++++--- source/blender/render/hydra/final_engine.h | 2 + .../render/hydra/scene_delegate/mesh.h | 2 +- 3 files changed, 123 insertions(+), 18 deletions(-) diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 301ce224d018..0ad026634523 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: Apache-2.0 * Copyright 2011-2022 Blender Foundation */ +#include + #include "BKE_lib_id.h" #include "BLI_timecode.h" #include "DEG_depsgraph_query.h" @@ -16,14 +18,11 @@ namespace blender::render::hydra { void FinalEngine::render(Depsgraph *depsgraph) { prepare_for_render(depsgraph); + render_task_delegate_->set_renderer_aov(pxr::HdAovTokens->color); + + engine_->Execute(render_index_.get(), &tasks_); std::vector &pixels = render_images_["Combined"]; - - { - /* Release the GIL before calling into hydra, in case any hydra plugins call into python. */ - engine_->Execute(render_index_.get(), &tasks_); - } - char elapsed_time[32]; double time_begin = PIL_check_seconds_timer(); float percent_done = 0.0; @@ -34,10 +33,8 @@ void FinalEngine::render(Depsgraph *depsgraph) } percent_done = renderer_percent_done(); - BLI_timecode_string_from_time_simple( elapsed_time, sizeof(elapsed_time), PIL_check_seconds_timer() - time_begin); - notify_status(percent_done / 100.0, scene_name_ + ": " + layer_name_, std::string("Render Time: ") + elapsed_time + @@ -105,7 +102,6 @@ void FinalEngine::prepare_for_render(Depsgraph *depsgraph) free_camera_delegate_->SetCamera(camera); render_task_delegate_->set_camera_and_viewport( free_camera_delegate_->GetCameraId(), pxr::GfVec4d(0, 0, resolution_[0], resolution_[1])); - render_task_delegate_->set_renderer_aov(pxr::HdAovTokens->color); if (simple_light_task_delegate_) { simple_light_task_delegate_->set_camera_path(free_camera_delegate_->GetCameraId()); @@ -118,13 +114,11 @@ void FinalEngine::prepare_for_render(Depsgraph *depsgraph) std::vector(resolution_[0] * resolution_[1] * 4)); /* 4 - number of channels. */ } -void FinalEngineGL::render(Depsgraph *depsgraph) +void FinalEngineGL::render0(Depsgraph *depsgraph) { prepare_for_render(depsgraph); - std::vector &pixels = render_images_["Combined"]; - - GPUFrameBuffer *framebuffer = GPU_framebuffer_create("fb_hdyra_render_final"); + GPUFrameBuffer *framebuffer = GPU_framebuffer_create("fb_hydra_render_final"); GPUTexture *texture = GPU_texture_create_2d("tex_hydra_render_final", resolution_[0], resolution_[1], @@ -140,11 +134,9 @@ void FinalEngineGL::render(Depsgraph *depsgraph) float clear_color[4] = {0.0, 0.0, 0.0, 0.0}; GPU_framebuffer_clear_color_depth(framebuffer, clear_color, 1.0); - { - /* Release the GIL before calling into hydra, in case any hydra plugins call into python. */ - engine_->Execute(render_index_.get(), &tasks_); - } + engine_->Execute(render_index_.get(), &tasks_); + std::vector &pixels = render_images_["Combined"]; char elapsed_time[32]; double time_begin = PIL_check_seconds_timer(); float percent_done = 0.0; @@ -183,4 +175,115 @@ void FinalEngineGL::render(Depsgraph *depsgraph) GPU_texture_free(texture); } +void FinalEngineGL::render(Depsgraph *depsgraph) +{ + prepare_for_render(depsgraph); + + GLuint framebuffer_name = 0; + glGenFramebuffers(1, &framebuffer_name); + glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_name); + + // The texture we're going to render to + GLuint rendered_texture; + glGenTextures(1, &rendered_texture); + + // "Bind" the newly created texture : all future texture functions will modify this texture + glBindTexture(GL_TEXTURE_2D, rendered_texture); + + // Give an empty image to OpenGL ( the last "0" ) + glTexImage2D( + GL_TEXTURE_2D, 0, GL_RGBA32F, resolution_[0], resolution_[1], 0, GL_RGBA, GL_FLOAT, 0); + + // Poor filtering. Needed ! + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + + // Set "rendered_texture" as our colour attachement #0 + glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, rendered_texture, 0); + + // Generate vertex array + GLuint vao; + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + + engine_->Execute(render_index_.get(), &tasks_); + + std::vector &pixels = render_images_["Combined"]; + char elapsed_time[32]; + double time_begin = PIL_check_seconds_timer(); + float percent_done = 0.0; + + while (true) { + if (RE_engine_test_break(bl_engine_)) { + break; + } + + percent_done = renderer_percent_done(); + + BLI_timecode_string_from_time_simple( + elapsed_time, sizeof(elapsed_time), PIL_check_seconds_timer() - time_begin); + + notify_status(percent_done / 100.0, + scene_name_ + ": " + layer_name_, + std::string("Render Time: ") + elapsed_time + + " | Done: " + std::to_string(int(percent_done)) + "%"); + + if (render_task_delegate_->is_converged()) { + break; + } + + glBindTexture(GL_TEXTURE_2D, rendered_texture); + glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels.data()); + update_render_result(); + } + + glBindTexture(GL_TEXTURE_2D, rendered_texture); + glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels.data()); + update_render_result(); +} + +void FinalEngineGL::render1(Depsgraph *depsgraph) +{ + prepare_for_render(depsgraph); + + //pxr::TraceCollector::GetInstance().SetEnabled(true); + + auto d = pxr::GlfDrawTarget::New(resolution_); + d->Bind(); + d->AddAttachment("color", GL_RGBA, GL_FLOAT, GL_RGBA); + engine_->Execute(render_index_.get(), &tasks_); + //d->Unbind(); + + std::vector &pixels = render_images_["Combined"]; + char elapsed_time[32]; + double time_begin = PIL_check_seconds_timer(); + float percent_done = 0.0; + + while (true) { + if (RE_engine_test_break(bl_engine_)) { + break; + } + + percent_done = renderer_percent_done(); + + BLI_timecode_string_from_time_simple( + elapsed_time, sizeof(elapsed_time), PIL_check_seconds_timer() - time_begin); + + notify_status(percent_done / 100.0, + scene_name_ + ": " + layer_name_, + std::string("Render Time: ") + elapsed_time + + " | Done: " + std::to_string(int(percent_done)) + "%"); + + if (render_task_delegate_->is_converged()) { + break; + } + } + + GLuint rendered_texture; + + //glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels.data()); + //update_render_result(); + pxr::TraceCollector::GetInstance().SetEnabled(false); +} + } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/final_engine.h b/source/blender/render/hydra/final_engine.h index 25e44e8a288a..e0878e40b0b9 100644 --- a/source/blender/render/hydra/final_engine.h +++ b/source/blender/render/hydra/final_engine.h @@ -30,6 +30,8 @@ class FinalEngineGL : public FinalEngine { public: using FinalEngine::FinalEngine; + void render0(Depsgraph *depsgraph); + void render1(Depsgraph *depsgraph); void render(Depsgraph *depsgraph) override; }; diff --git a/source/blender/render/hydra/scene_delegate/mesh.h b/source/blender/render/hydra/scene_delegate/mesh.h index 95f0ba04ed2a..33ff498b6dfa 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.h +++ b/source/blender/render/hydra/scene_delegate/mesh.h @@ -43,7 +43,7 @@ class MeshData : public ObjectData { void available_materials(Set &paths) const; pxr::SdfPathVector submesh_paths() const; - pxr::HdCullStyle cull_style = pxr::HdCullStyleBackUnlessDoubleSided; + pxr::HdCullStyle cull_style = pxr::HdCullStyleBack; private: pxr::SdfPath submesh_prim_id(int index) const; -- 2.30.2 From fd68f0ca34c31e2971eab1277f9e7716bf96a651 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 5 Jul 2023 18:35:03 +0300 Subject: [PATCH 2/9] Moved cull_style to MaterialData --- source/blender/render/hydra/final_engine.cc | 8 ++++---- .../hydra/scene_delegate/blender_scene_delegate.cc | 2 +- source/blender/render/hydra/scene_delegate/image.cc | 2 +- .../blender/render/hydra/scene_delegate/material.cc | 5 +++++ .../blender/render/hydra/scene_delegate/material.h | 3 ++- source/blender/render/hydra/scene_delegate/mesh.cc | 12 +++++++++++- source/blender/render/hydra/scene_delegate/mesh.h | 3 +-- source/blender/render/hydra/scene_delegate/world.cc | 7 ++----- 8 files changed, 27 insertions(+), 15 deletions(-) diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 0ad026634523..1c84fef57262 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -246,13 +246,13 @@ void FinalEngineGL::render1(Depsgraph *depsgraph) { prepare_for_render(depsgraph); - //pxr::TraceCollector::GetInstance().SetEnabled(true); + // pxr::TraceCollector::GetInstance().SetEnabled(true); auto d = pxr::GlfDrawTarget::New(resolution_); d->Bind(); d->AddAttachment("color", GL_RGBA, GL_FLOAT, GL_RGBA); engine_->Execute(render_index_.get(), &tasks_); - //d->Unbind(); + // d->Unbind(); std::vector &pixels = render_images_["Combined"]; char elapsed_time[32]; @@ -281,8 +281,8 @@ void FinalEngineGL::render1(Depsgraph *depsgraph) GLuint rendered_texture; - //glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels.data()); - //update_render_result(); + // glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels.data()); + // update_render_result(); pxr::TraceCollector::GetInstance().SetEnabled(false); } diff --git a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc index c6f29f6aaa64..36577d7abb7b 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -178,7 +178,7 @@ bool BlenderSceneDelegate::GetDoubleSided(pxr::SdfPath const &id) pxr::HdCullStyle BlenderSceneDelegate::GetCullStyle(pxr::SdfPath const &id) { CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 3, "%s", id.GetText()); - return mesh_data(id)->cull_style; + return mesh_data(id)->cull_style(id); } pxr::SdfPath BlenderSceneDelegate::GetInstancerId(pxr::SdfPath const &prim_id) diff --git a/source/blender/render/hydra/scene_delegate/image.cc b/source/blender/render/hydra/scene_delegate/image.cc index c737cf333cc5..97dcf9a728ca 100644 --- a/source/blender/render/hydra/scene_delegate/image.cc +++ b/source/blender/render/hydra/scene_delegate/image.cc @@ -45,7 +45,7 @@ static std::string cache_image_file(Image *image, BKE_image_path_ext_from_imformat(&scene->r.im_format, &r_ext); opts.im_format = scene->r.im_format; } - + snprintf(file_name, sizeof(file_name), "img_%016llx%s", (uintptr_t)image, r_ext); file_path = get_cache_file(file_name); diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index f2a65531e5c0..0068e8234146 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -89,6 +89,11 @@ pxr::VtValue MaterialData::get_material_resource() const return material_network_map_; } +pxr::HdCullStyle MaterialData::cull_style() const +{ + return double_sided ? pxr::HdCullStyle::HdCullStyleNothing : pxr::HdCullStyle::HdCullStyleBack; +} + void MaterialData::export_mtlx() { /* Call of python function hydra.export_mtlx() */ diff --git a/source/blender/render/hydra/scene_delegate/material.h b/source/blender/render/hydra/scene_delegate/material.h index fd87cd332414..b366ed548c42 100644 --- a/source/blender/render/hydra/scene_delegate/material.h +++ b/source/blender/render/hydra/scene_delegate/material.h @@ -3,7 +3,7 @@ #pragma once -#include "pxr/base/tf/hashmap.h" +#include #include #include @@ -27,6 +27,7 @@ class MaterialData : public IdData { pxr::VtValue get_data(pxr::TfToken const &key) const override; pxr::VtValue get_material_resource() const; + pxr::HdCullStyle cull_style() const; bool double_sided = true; diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index f2bf1235b2de..bd12116f6f49 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -149,6 +149,15 @@ pxr::SdfPath MeshData::material_id(pxr::SdfPath const &id) const return sm.mat_data->prim_id; } +pxr::HdCullStyle MeshData::cull_style(pxr::SdfPath const &id) const +{ + const SubMesh &sm = submesh(id); + if (sm.mat_data) { + return sm.mat_data->cull_style(); + } + return pxr::HdCullStyle::HdCullStyleDontCare; +} + bool MeshData::double_sided(pxr::SdfPath const &id) const { const SubMesh &sm = submesh(id); @@ -163,7 +172,8 @@ void MeshData::update_double_sided(MaterialData *mat_data) for (int i = 0; i < submeshes_.size(); ++i) { if (submeshes_[i].mat_data == mat_data) { scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty( - submesh_prim_id(i), pxr::HdChangeTracker::DirtyDoubleSided); + submesh_prim_id(i), + pxr::HdChangeTracker::DirtyDoubleSided | pxr::HdChangeTracker::DirtyCullStyle); ID_LOG(1, "%d", i); } } diff --git a/source/blender/render/hydra/scene_delegate/mesh.h b/source/blender/render/hydra/scene_delegate/mesh.h index 33ff498b6dfa..649056adac0c 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.h +++ b/source/blender/render/hydra/scene_delegate/mesh.h @@ -38,13 +38,12 @@ class MeshData : public ObjectData { pxr::HdMeshTopology mesh_topology(pxr::SdfPath const &id) const; pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const; pxr::SdfPath material_id(pxr::SdfPath const &id) const; + pxr::HdCullStyle cull_style(pxr::SdfPath const &id) const; bool double_sided(pxr::SdfPath const &id) const; void update_double_sided(MaterialData *mat_data); void available_materials(Set &paths) const; pxr::SdfPathVector submesh_paths() const; - pxr::HdCullStyle cull_style = pxr::HdCullStyleBack; - private: pxr::SdfPath submesh_prim_id(int index) const; const SubMesh &submesh(pxr::SdfPath const &id) const; diff --git a/source/blender/render/hydra/scene_delegate/world.cc b/source/blender/render/hydra/scene_delegate/world.cc index 511756cd3936..c7f7aa746286 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -176,11 +176,8 @@ void WorldData::write_transform() { transform = pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -90), pxr::GfVec3d()); - transform *= pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -180), - pxr::GfVec3d()); - transform *= pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(0.0, 0.0, 1.0), 90.0), - pxr::GfVec3d()); - + transform *= pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -180), pxr::GfVec3d()); + transform *= pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(0.0, 0.0, 1.0), 90.0), pxr::GfVec3d()); } } // namespace blender::render::hydra -- 2.30.2 From 2e68ca68d0f96688ffc23b9c69e38acc3ef39435 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 5 Jul 2023 19:10:11 +0300 Subject: [PATCH 3/9] Adding depth component --- source/blender/render/hydra/final_engine.cc | 44 +++++++++++++-------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 1c84fef57262..0278a8469b34 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -179,27 +179,39 @@ void FinalEngineGL::render(Depsgraph *depsgraph) { prepare_for_render(depsgraph); - GLuint framebuffer_name = 0; - glGenFramebuffers(1, &framebuffer_name); - glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_name); + GLuint framebuffer = 0, tex_color = 0, tex_depth = 0; + glGenFramebuffers(1, &framebuffer); + glGenTextures(1, &tex_color); + glGenTextures(1, &tex_depth); - // The texture we're going to render to - GLuint rendered_texture; - glGenTextures(1, &rendered_texture); + glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); - // "Bind" the newly created texture : all future texture functions will modify this texture - glBindTexture(GL_TEXTURE_2D, rendered_texture); - - // Give an empty image to OpenGL ( the last "0" ) + // Set tex_color as GL_COLOR_ATTACHMENT0 + glBindTexture(GL_TEXTURE_2D, tex_color); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA32F, resolution_[0], resolution_[1], 0, GL_RGBA, GL_FLOAT, 0); - - // Poor filtering. Needed ! glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex_color, 0); - // Set "rendered_texture" as our colour attachement #0 - glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, rendered_texture, 0); + // Set tex_depth as GL_DEPTH_ATTACHMENT + glBindTexture(GL_TEXTURE_2D, tex_depth); + glTexImage2D(GL_TEXTURE_2D, + 0, + GL_DEPTH_COMPONENT32, + resolution_[0], + resolution_[1], + 0, + GL_DEPTH_COMPONENT, + GL_FLOAT, + 0); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + //glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, tex_depth, 0); // Generate vertex array GLuint vao; @@ -232,12 +244,12 @@ void FinalEngineGL::render(Depsgraph *depsgraph) break; } - glBindTexture(GL_TEXTURE_2D, rendered_texture); + glBindTexture(GL_TEXTURE_2D, tex_color); glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels.data()); update_render_result(); } - glBindTexture(GL_TEXTURE_2D, rendered_texture); + glBindTexture(GL_TEXTURE_2D, tex_color); glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels.data()); update_render_result(); } -- 2.30.2 From 6ee0f4793d47e2d2850ee3aec2249440cd5facc8 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 5 Jul 2023 21:09:30 +0300 Subject: [PATCH 4/9] Continuing --- source/blender/render/hydra/final_engine.cc | 74 ++++++++++++++------- source/blender/render/hydra/python.cc | 16 +++-- 2 files changed, 59 insertions(+), 31 deletions(-) diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 0278a8469b34..6e8207d0fc9e 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -114,7 +114,7 @@ void FinalEngine::prepare_for_render(Depsgraph *depsgraph) std::vector(resolution_[0] * resolution_[1] * 4)); /* 4 - number of channels. */ } -void FinalEngineGL::render0(Depsgraph *depsgraph) +void FinalEngineGL::render(Depsgraph *depsgraph) { prepare_for_render(depsgraph); @@ -124,14 +124,15 @@ void FinalEngineGL::render0(Depsgraph *depsgraph) resolution_[1], 1, GPU_RGBA32F, - GPU_TEXTURE_USAGE_GENERAL, + GPU_TEXTURE_USAGE_ATTACHMENT, nullptr); GPU_texture_filter_mode(texture, true); GPU_texture_mipmap_mode(texture, true, true); - GPU_framebuffer_texture_attach(framebuffer, texture, 0, 0); GPU_framebuffer_bind(framebuffer); - float clear_color[4] = {0.0, 0.0, 0.0, 0.0}; + GPU_framebuffer_texture_attach(framebuffer, texture, 0, 0); + + float clear_color[4] = {0.1f, 0.1f, 0.1f, 1.0f}; GPU_framebuffer_clear_color_depth(framebuffer, clear_color, 1.0); engine_->Execute(render_index_.get(), &tasks_); @@ -175,16 +176,16 @@ void FinalEngineGL::render0(Depsgraph *depsgraph) GPU_texture_free(texture); } -void FinalEngineGL::render(Depsgraph *depsgraph) +void FinalEngineGL::render0(Depsgraph *depsgraph) { prepare_for_render(depsgraph); - GLuint framebuffer = 0, tex_color = 0, tex_depth = 0; - glGenFramebuffers(1, &framebuffer); + GLuint fbo = 0, tex_color = 0, tex_depth = 0; + glGenFramebuffers(1, &fbo); glGenTextures(1, &tex_color); glGenTextures(1, &tex_depth); - glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); + glBindFramebuffer(GL_FRAMEBUFFER, fbo); // Set tex_color as GL_COLOR_ATTACHMENT0 glBindTexture(GL_TEXTURE_2D, tex_color); @@ -194,30 +195,53 @@ void FinalEngineGL::render(Depsgraph *depsgraph) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex_color, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex_color, 0); - // Set tex_depth as GL_DEPTH_ATTACHMENT - glBindTexture(GL_TEXTURE_2D, tex_depth); - glTexImage2D(GL_TEXTURE_2D, - 0, - GL_DEPTH_COMPONENT32, - resolution_[0], - resolution_[1], - 0, - GL_DEPTH_COMPONENT, - GL_FLOAT, - 0); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - //glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, tex_depth, 0); + + GLuint rbo = 0; + glGenRenderbuffers(1, &rbo); + glBindRenderbuffer(GL_RENDERBUFFER, rbo); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, resolution_[0], resolution_[1]); + glBindRenderbuffer(GL_RENDERBUFFER, 0); + + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo); + + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { + printf("ERROR::FRAMEBUFFER:: Framebuffer is not complete!\n"); + } + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + //// Set tex_depth as GL_DEPTH_ATTACHMENT + //glBindTexture(GL_TEXTURE_2D, tex_depth); + //glTexImage2D(GL_TEXTURE_2D, + // 0, + // GL_DEPTH_COMPONENT32, + // resolution_[0], + // resolution_[1], + // 0, + // GL_DEPTH_COMPONENT, + // GL_FLOAT, + // 0); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + ////glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, tex_depth, 0); // Generate vertex array GLuint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + + + glEnable(GL_DEPTH_TEST); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); + //glClearDepthf(1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + //glDepthFunc(GL_LESS); + engine_->Execute(render_index_.get(), &tasks_); std::vector &pixels = render_images_["Combined"]; diff --git a/source/blender/render/hydra/python.cc b/source/blender/render/hydra/python.cc index 3aff44b38b36..93ce10fd7f3e 100644 --- a/source/blender/render/hydra/python.cc +++ b/source/blender/render/hydra/python.cc @@ -117,9 +117,9 @@ static PyObject *engine_sync_func(PyObject * /*self*/, PyObject *args) Depsgraph *depsgraph = (Depsgraph *)PyLong_AsVoidPtr(pydepsgraph); bContext *context = (bContext *)PyLong_AsVoidPtr(pycontext); + CLOG_INFO(LOG_RENDER_HYDRA, 2, "Engine %016llx", engine); engine->sync(depsgraph, context); - CLOG_INFO(LOG_RENDER_HYDRA, 2, "Engine %016llx", engine); Py_RETURN_NONE; } @@ -135,9 +135,9 @@ static PyObject *engine_sync_usd_func(PyObject * /*self*/, PyObject *args) boost::python::extract extract(pystage); pxr::UsdStagePtr stage = extract(); + CLOG_INFO(LOG_RENDER_HYDRA, 2, "Engine %016llx", engine); engine->sync_usd(stage); - CLOG_INFO(LOG_RENDER_HYDRA, 2, "Engine %016llx", engine); Py_RETURN_NONE; } @@ -152,12 +152,13 @@ static PyObject *engine_render_func(PyObject * /*self*/, PyObject *args) Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine); Depsgraph *depsgraph = (Depsgraph *)PyLong_AsVoidPtr(pydepsgraph); + CLOG_INFO(LOG_RENDER_HYDRA, 2, "Engine %016llx", engine); + /* Allow Blender to execute other Python scripts. */ Py_BEGIN_ALLOW_THREADS; engine->render(depsgraph); Py_END_ALLOW_THREADS; - CLOG_INFO(LOG_RENDER_HYDRA, 3, "Engine %016llx", engine); Py_RETURN_NONE; } @@ -172,12 +173,13 @@ static PyObject *engine_view_draw_func(PyObject * /*self*/, PyObject *args) Depsgraph *depsgraph = (Depsgraph *)PyLong_AsVoidPtr(pydepsgraph); bContext *context = (bContext *)PyLong_AsVoidPtr(pycontext); + CLOG_INFO(LOG_RENDER_HYDRA, 3, "Engine %016llx", engine); + /* Allow Blender to execute other Python scripts. */ Py_BEGIN_ALLOW_THREADS; engine->render(depsgraph, context); Py_END_ALLOW_THREADS; - CLOG_INFO(LOG_RENDER_HYDRA, 3, "Engine %016llx", engine); Py_RETURN_NONE; } @@ -208,9 +210,10 @@ static PyObject *engine_set_sync_setting_func(PyObject * /*self*/, PyObject *arg } Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine); - engine->set_sync_setting(key, get_setting_val(pyval)); CLOG_INFO(LOG_RENDER_HYDRA, 3, "Engine %016llx: %s", engine, key); + engine->set_sync_setting(key, get_setting_val(pyval)); + Py_RETURN_NONE; } @@ -223,9 +226,10 @@ static PyObject *engine_set_render_setting_func(PyObject * /*self*/, PyObject *a } Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine); - engine->set_render_setting(key, get_setting_val(pyval)); CLOG_INFO(LOG_RENDER_HYDRA, 3, "Engine %016llx: %s", engine, key); + engine->set_render_setting(key, get_setting_val(pyval)); + Py_RETURN_NONE; } -- 2.30.2 From 9da35493ac4e00d8d07ec5cb9da1c7aac5674318 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 6 Jul 2023 01:53:57 +0300 Subject: [PATCH 5/9] Fixing depth --- source/blender/render/hydra/final_engine.cc | 66 ++++++++++++++------- 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 6e8207d0fc9e..84bbaa45723b 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -118,23 +118,39 @@ void FinalEngineGL::render(Depsgraph *depsgraph) { prepare_for_render(depsgraph); - GPUFrameBuffer *framebuffer = GPU_framebuffer_create("fb_hydra_render_final"); - GPUTexture *texture = GPU_texture_create_2d("tex_hydra_render_final", - resolution_[0], - resolution_[1], - 1, - GPU_RGBA32F, - GPU_TEXTURE_USAGE_ATTACHMENT, - nullptr); - GPU_texture_filter_mode(texture, true); - GPU_texture_mipmap_mode(texture, true, true); + GPUFrameBuffer *framebuffer = GPU_framebuffer_create("fb_render_hydra"); + GPUTexture *tex_color = GPU_texture_create_2d("tex_render_hydra_color", + resolution_[0], + resolution_[1], + 1, + GPU_RGBA32F, + GPU_TEXTURE_USAGE_GENERAL, + nullptr); + GPUTexture *tex_depth = GPU_texture_create_2d("tex_render_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); - GPU_framebuffer_texture_attach(framebuffer, texture, 0, 0); float clear_color[4] = {0.1f, 0.1f, 0.1f, 1.0f}; GPU_framebuffer_clear_color_depth(framebuffer, clear_color, 1.0); + // Generate vertex array + GLuint vao; + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + engine_->Execute(render_index_.get(), &tasks_); std::vector &pixels = render_images_["Combined"]; @@ -161,19 +177,20 @@ void FinalEngineGL::render(Depsgraph *depsgraph) break; } - void *data = GPU_texture_read(texture, GPU_DATA_FLOAT, 0); + 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(texture, GPU_DATA_FLOAT, 0); + 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(); GPU_framebuffer_free(framebuffer); - GPU_texture_free(texture); + GPU_texture_free(tex_color); + GPU_texture_free(tex_depth); } void FinalEngineGL::render0(Depsgraph *depsgraph) @@ -282,13 +299,17 @@ void FinalEngineGL::render1(Depsgraph *depsgraph) { prepare_for_render(depsgraph); - // pxr::TraceCollector::GetInstance().SetEnabled(true); - auto d = pxr::GlfDrawTarget::New(resolution_); d->Bind(); - d->AddAttachment("color", GL_RGBA, GL_FLOAT, GL_RGBA); + d->AddAttachment("color", GL_RGBA, GL_FLOAT, GL_RGBA32F); + d->AddAttachment("depth", GL_DEPTH_COMPONENT, GL_FLOAT, GL_DEPTH_COMPONENT32); + + // Generate vertex array + GLuint vao; + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + engine_->Execute(render_index_.get(), &tasks_); - // d->Unbind(); std::vector &pixels = render_images_["Combined"]; char elapsed_time[32]; @@ -315,11 +336,10 @@ void FinalEngineGL::render1(Depsgraph *depsgraph) } } - GLuint rendered_texture; - - // glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels.data()); - // update_render_result(); - pxr::TraceCollector::GetInstance().SetEnabled(false); + GLuint t = d->GetAttachment("color")->GetGlTextureName(); + glBindTexture(GL_TEXTURE_2D, t); + glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels.data()); + update_render_result(); } } // namespace blender::render::hydra -- 2.30.2 From 1d6cd374d98c7eafdcdaaa27c2bbadacb4b64d5c Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 6 Jul 2023 02:50:41 +0300 Subject: [PATCH 6/9] Removed unused code --- source/blender/render/hydra/final_engine.cc | 162 ++------------------ source/blender/render/hydra/final_engine.h | 2 - 2 files changed, 11 insertions(+), 153 deletions(-) diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 84bbaa45723b..9673e4e0fd66 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -2,6 +2,7 @@ * Copyright 2011-2022 Blender Foundation */ #include +#include #include "BKE_lib_id.h" #include "BLI_timecode.h" @@ -143,8 +144,16 @@ void FinalEngineGL::render(Depsgraph *depsgraph) GPU_framebuffer_bind(framebuffer); - float clear_color[4] = {0.1f, 0.1f, 0.1f, 1.0f}; - GPU_framebuffer_clear_color_depth(framebuffer, clear_color, 1.0); + float clear_color[4] = {0.0f, 0.0f, 0.0f, 1.0f}; + pxr::VtValue world_color = scene_delegate_->GetLightParamValue( + scene_delegate_->GetDelegateID().AppendElementString("World"), pxr::HdLightTokens->color); + if (!world_color.IsEmpty()) { + auto &c = world_color.Get(); + clear_color[0] = c[0]; + clear_color[1] = c[1]; + clear_color[2] = c[2]; + } + GPU_framebuffer_clear_color_depth(framebuffer, clear_color, 1.0f); // Generate vertex array GLuint vao; @@ -193,153 +202,4 @@ void FinalEngineGL::render(Depsgraph *depsgraph) GPU_texture_free(tex_depth); } -void FinalEngineGL::render0(Depsgraph *depsgraph) -{ - prepare_for_render(depsgraph); - - GLuint fbo = 0, tex_color = 0, tex_depth = 0; - glGenFramebuffers(1, &fbo); - glGenTextures(1, &tex_color); - glGenTextures(1, &tex_depth); - - glBindFramebuffer(GL_FRAMEBUFFER, fbo); - - // Set tex_color as GL_COLOR_ATTACHMENT0 - glBindTexture(GL_TEXTURE_2D, tex_color); - glTexImage2D( - GL_TEXTURE_2D, 0, GL_RGBA32F, resolution_[0], resolution_[1], 0, GL_RGBA, GL_FLOAT, 0); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex_color, 0); - - - GLuint rbo = 0; - glGenRenderbuffers(1, &rbo); - glBindRenderbuffer(GL_RENDERBUFFER, rbo); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, resolution_[0], resolution_[1]); - glBindRenderbuffer(GL_RENDERBUFFER, 0); - - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo); - - if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { - printf("ERROR::FRAMEBUFFER:: Framebuffer is not complete!\n"); - } - glBindFramebuffer(GL_FRAMEBUFFER, 0); - - //// Set tex_depth as GL_DEPTH_ATTACHMENT - //glBindTexture(GL_TEXTURE_2D, tex_depth); - //glTexImage2D(GL_TEXTURE_2D, - // 0, - // GL_DEPTH_COMPONENT32, - // resolution_[0], - // resolution_[1], - // 0, - // GL_DEPTH_COMPONENT, - // GL_FLOAT, - // 0); - //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - ////glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, tex_depth, 0); - - // Generate vertex array - GLuint vao; - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - - glBindFramebuffer(GL_FRAMEBUFFER, fbo); - - - glEnable(GL_DEPTH_TEST); - glClearColor(0.1f, 0.1f, 0.1f, 1.0f); - //glClearDepthf(1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - //glDepthFunc(GL_LESS); - - engine_->Execute(render_index_.get(), &tasks_); - - std::vector &pixels = render_images_["Combined"]; - char elapsed_time[32]; - double time_begin = PIL_check_seconds_timer(); - float percent_done = 0.0; - - while (true) { - if (RE_engine_test_break(bl_engine_)) { - break; - } - - percent_done = renderer_percent_done(); - - BLI_timecode_string_from_time_simple( - elapsed_time, sizeof(elapsed_time), PIL_check_seconds_timer() - time_begin); - - notify_status(percent_done / 100.0, - scene_name_ + ": " + layer_name_, - std::string("Render Time: ") + elapsed_time + - " | Done: " + std::to_string(int(percent_done)) + "%"); - - if (render_task_delegate_->is_converged()) { - break; - } - - glBindTexture(GL_TEXTURE_2D, tex_color); - glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels.data()); - update_render_result(); - } - - glBindTexture(GL_TEXTURE_2D, tex_color); - glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels.data()); - update_render_result(); -} - -void FinalEngineGL::render1(Depsgraph *depsgraph) -{ - prepare_for_render(depsgraph); - - auto d = pxr::GlfDrawTarget::New(resolution_); - d->Bind(); - d->AddAttachment("color", GL_RGBA, GL_FLOAT, GL_RGBA32F); - d->AddAttachment("depth", GL_DEPTH_COMPONENT, GL_FLOAT, GL_DEPTH_COMPONENT32); - - // Generate vertex array - GLuint vao; - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - - engine_->Execute(render_index_.get(), &tasks_); - - std::vector &pixels = render_images_["Combined"]; - char elapsed_time[32]; - double time_begin = PIL_check_seconds_timer(); - float percent_done = 0.0; - - while (true) { - if (RE_engine_test_break(bl_engine_)) { - break; - } - - percent_done = renderer_percent_done(); - - BLI_timecode_string_from_time_simple( - elapsed_time, sizeof(elapsed_time), PIL_check_seconds_timer() - time_begin); - - notify_status(percent_done / 100.0, - scene_name_ + ": " + layer_name_, - std::string("Render Time: ") + elapsed_time + - " | Done: " + std::to_string(int(percent_done)) + "%"); - - if (render_task_delegate_->is_converged()) { - break; - } - } - - GLuint t = d->GetAttachment("color")->GetGlTextureName(); - glBindTexture(GL_TEXTURE_2D, t); - glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels.data()); - update_render_result(); -} - } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/final_engine.h b/source/blender/render/hydra/final_engine.h index e0878e40b0b9..25e44e8a288a 100644 --- a/source/blender/render/hydra/final_engine.h +++ b/source/blender/render/hydra/final_engine.h @@ -30,8 +30,6 @@ class FinalEngineGL : public FinalEngine { public: using FinalEngine::FinalEngine; - void render0(Depsgraph *depsgraph); - void render1(Depsgraph *depsgraph); void render(Depsgraph *depsgraph) override; }; -- 2.30.2 From bdacb8d2adaeda0f80ee4e0cd38b67320cff5642 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 6 Jul 2023 14:40:38 +0300 Subject: [PATCH 7/9] Polishing code. Renamed: FinaleEngineGL -> FinalEngineGPU --- scripts/modules/bpy_hydra.py | 3 +++ source/blender/render/hydra/final_engine.cc | 14 ++++++++------ source/blender/render/hydra/final_engine.h | 2 +- source/blender/render/hydra/python.cc | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/scripts/modules/bpy_hydra.py b/scripts/modules/bpy_hydra.py index 5fd60ef1ae2a..74ce7405b9de 100644 --- a/scripts/modules/bpy_hydra.py +++ b/scripts/modules/bpy_hydra.py @@ -92,6 +92,9 @@ class HydraRenderEngine(bpy.types.RenderEngine): # final render def update(self, data, depsgraph): + # Note: if bl_use_gpu_context = True, leave this function empty and move engine creation + # and syncing to render() function + engine_type = 'PREVIEW' if self.is_preview else 'FINAL' self.engine_ptr = _bpy_hydra.engine_create(self.as_pointer(), engine_type, self.delegate_id) if not self.engine_ptr: diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 9673e4e0fd66..8006b8195805 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: Apache-2.0 * Copyright 2011-2022 Blender Foundation */ -#include +#include #include #include "BKE_lib_id.h" @@ -115,7 +115,7 @@ void FinalEngine::prepare_for_render(Depsgraph *depsgraph) std::vector(resolution_[0] * resolution_[1] * 4)); /* 4 - number of channels. */ } -void FinalEngineGL::render(Depsgraph *depsgraph) +void FinalEngineGPU::render(Depsgraph *depsgraph) { prepare_for_render(depsgraph); @@ -155,10 +155,11 @@ void FinalEngineGL::render(Depsgraph *depsgraph) } GPU_framebuffer_clear_color_depth(framebuffer, clear_color, 1.0f); - // Generate vertex array - GLuint vao; - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); + /* 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_); @@ -197,6 +198,7 @@ void FinalEngineGL::render(Depsgraph *depsgraph) MEM_freeN(data); update_render_result(); + glDeleteVertexArrays(1, &VAO); GPU_framebuffer_free(framebuffer); GPU_texture_free(tex_color); GPU_texture_free(tex_depth); diff --git a/source/blender/render/hydra/final_engine.h b/source/blender/render/hydra/final_engine.h index 25e44e8a288a..2ab44bbadaa5 100644 --- a/source/blender/render/hydra/final_engine.h +++ b/source/blender/render/hydra/final_engine.h @@ -26,7 +26,7 @@ class FinalEngine : public Engine { pxr::GfVec2i resolution_; }; -class FinalEngineGL : public FinalEngine { +class FinalEngineGPU : public FinalEngine { public: using FinalEngine::FinalEngine; diff --git a/source/blender/render/hydra/python.cc b/source/blender/render/hydra/python.cc index 93ce10fd7f3e..0cc0d11c21b7 100644 --- a/source/blender/render/hydra/python.cc +++ b/source/blender/render/hydra/python.cc @@ -75,7 +75,7 @@ static PyObject *engine_create_func(PyObject * /*self*/, PyObject *args) } else { if (bl_engine->type->flag & RE_USE_GPU_CONTEXT) { - engine = new FinalEngineGL(bl_engine, render_delegate_id); + engine = new FinalEngineGPU(bl_engine, render_delegate_id); } else { engine = new FinalEngine(bl_engine, render_delegate_id); -- 2.30.2 From 987b4ab9fc5308541185a2989c383bab0afbd040 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Sun, 9 Jul 2023 15:19:20 +0300 Subject: [PATCH 8/9] Fixed review comments --- scripts/modules/bpy_hydra.py | 14 ++++++++++---- source/blender/render/hydra/scene_delegate/mesh.cc | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/scripts/modules/bpy_hydra.py b/scripts/modules/bpy_hydra.py index 74ce7405b9de..649351288d58 100644 --- a/scripts/modules/bpy_hydra.py +++ b/scripts/modules/bpy_hydra.py @@ -91,10 +91,8 @@ class HydraRenderEngine(bpy.types.RenderEngine): return {} # final render - def update(self, data, depsgraph): - # Note: if bl_use_gpu_context = True, leave this function empty and move engine creation - # and syncing to render() function - + def _update(self, depsgraph): + """This function is preferable to override in child classes instead of update()""" engine_type = 'PREVIEW' if self.is_preview else 'FINAL' self.engine_ptr = _bpy_hydra.engine_create(self.as_pointer(), engine_type, self.delegate_id) if not self.engine_ptr: @@ -105,7 +103,15 @@ class HydraRenderEngine(bpy.types.RenderEngine): _bpy_hydra.engine_sync(self.engine_ptr, depsgraph.as_pointer(), bpy.context.as_pointer()) + def update(self, data, depsgraph): + # If bl_use_gpu_context is true, this function is ignored and render() is used + if not self.bl_use_gpu_context: + self._update(depsgraph) + def render(self, depsgraph): + if self.bl_use_gpu_context: + self._update(depsgraph) + if not self.engine_ptr: return diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index 167c5560bba9..81136635efc6 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -154,7 +154,7 @@ pxr::HdCullStyle MeshData::cull_style(pxr::SdfPath const &id) const if (sm.mat_data) { return sm.mat_data->cull_style(); } - return pxr::HdCullStyle::HdCullStyleDontCare; + return pxr::HdCullStyle::HdCullStyleNothing; } bool MeshData::double_sided(pxr::SdfPath const &id) const -- 2.30.2 From 23b65fa7c9c6240c4e9ddac658974f141e4f3f96 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Mon, 10 Jul 2023 14:18:05 +0300 Subject: [PATCH 9/9] Some code cleanups --- source/blender/render/hydra/CMakeLists.txt | 1 + source/blender/render/hydra/final_engine.cc | 5 +++-- source/blender/render/hydra/viewport_engine.cc | 11 ++++------- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/source/blender/render/hydra/CMakeLists.txt b/source/blender/render/hydra/CMakeLists.txt index bee162760ba9..52b6b175c983 100644 --- a/source/blender/render/hydra/CMakeLists.txt +++ b/source/blender/render/hydra/CMakeLists.txt @@ -39,6 +39,7 @@ set(INC set(INC_SYS ${PYTHON_INCLUDE_DIRS} + ${Epoxy_INCLUDE_DIRS} ${USD_INCLUDE_DIRS} ${BOOST_INCLUDE_DIR} ${TBB_INCLUDE_DIR} diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 8006b8195805..cf7a79dcb0b9 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -1,7 +1,8 @@ /* SPDX-License-Identifier: Apache-2.0 * Copyright 2011-2022 Blender Foundation */ -#include +#include + #include #include "BKE_lib_id.h" @@ -155,7 +156,7 @@ void FinalEngineGPU::render(Depsgraph *depsgraph) } 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 + /* 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); diff --git a/source/blender/render/hydra/viewport_engine.cc b/source/blender/render/hydra/viewport_engine.cc index 14e68ad5fe38..35fa618080c0 100644 --- a/source/blender/render/hydra/viewport_engine.cc +++ b/source/blender/render/hydra/viewport_engine.cc @@ -253,14 +253,11 @@ void ViewportEngine::render(Depsgraph *depsgraph, bContext *context) } tasks.push_back(render_task_delegate_->get_task()); - { - /* Release the GIL before calling into hydra, in case any hydra plugins call into python. */ - engine_->Execute(render_index_.get(), &tasks); + engine_->Execute(render_index_.get(), &tasks); - if ((bl_engine_->type->flag & RE_USE_GPU_CONTEXT) == 0) { - draw_texture_.set_buffer(render_task_delegate_->get_renderer_aov(pxr::HdAovTokens->color)); - draw_texture_.draw(shader, view_settings.border[0], view_settings.border[1]); - } + if ((bl_engine_->type->flag & RE_USE_GPU_CONTEXT) == 0) { + draw_texture_.set_buffer(render_task_delegate_->get_renderer_aov(pxr::HdAovTokens->color)); + draw_texture_.draw(shader, view_settings.border[0], view_settings.border[1]); } GPU_shader_unbind(); -- 2.30.2