From ef68df0911acb484e6151139c3d615e0a036644d Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 6 Apr 2023 17:19:31 +0300 Subject: [PATCH 1/9] Don't use OpenGL functions, only use the GPU API. Initial changes --- .../blender/render/hydra/viewport_engine.cc | 135 ++++++++++-------- source/blender/render/hydra/viewport_engine.h | 10 +- 2 files changed, 83 insertions(+), 62 deletions(-) diff --git a/source/blender/render/hydra/viewport_engine.cc b/source/blender/render/hydra/viewport_engine.cc index 94f8ab8dad24..1053cbe30e73 100644 --- a/source/blender/render/hydra/viewport_engine.cc +++ b/source/blender/render/hydra/viewport_engine.cc @@ -1,8 +1,6 @@ /* SPDX-License-Identifier: Apache-2.0 * Copyright 2011-2022 Blender Foundation */ -#include - #include #include #include @@ -15,6 +13,7 @@ #include "BLI_math_matrix.h" #include "DEG_depsgraph_query.h" #include "GPU_shader.h" +#include "GPU_batch.h" // clang-format on #include "camera.h" @@ -134,20 +133,20 @@ pxr::GfCamera ViewSettings::gf_camera() (float)border[3] / screen_height)); } -GLTexture::GLTexture() : texture_id(0), width(0), height(0), channels(4) +GLTexture::GLTexture() : gpu_texture(nullptr), width(0), height(0), channels(4) { } GLTexture::~GLTexture() { - if (texture_id) { + if (gpu_texture) { free(); } } void GLTexture::set_buffer(pxr::HdRenderBuffer *buffer) { - if (!texture_id) { + if (!gpu_texture) { create(buffer); return; } @@ -158,10 +157,8 @@ void GLTexture::set_buffer(pxr::HdRenderBuffer *buffer) return; } - glBindTexture(GL_TEXTURE_2D, texture_id); - void *data = buffer->Map(); - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_FLOAT, data); + GPU_texture_update(gpu_texture, GPU_DATA_FLOAT, data); buffer->Unmap(); } @@ -170,74 +167,98 @@ void GLTexture::create(pxr::HdRenderBuffer *buffer) width = buffer->GetWidth(); height = buffer->GetHeight(); channels = pxr::HdGetComponentCount(buffer->GetFormat()); - - glGenTextures(1, &texture_id); - glBindTexture(GL_TEXTURE_2D, texture_id); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - void *data = buffer->Map(); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, data); + gpu_texture = GPU_texture_create_2d("HydraRendeViewport", + width, + height, + 1, + GPU_RGBA16F, + GPU_TEXTURE_USAGE_GENERAL, + (float *)data); buffer->Unmap(); + + GPU_texture_filter_mode(gpu_texture, true); + GPU_texture_wrap_mode(gpu_texture, true, true); } void GLTexture::free() { - glDeleteTextures(1, &texture_id); - texture_id = 0; + GPU_texture_free(gpu_texture); + gpu_texture = nullptr; } -void GLTexture::draw(GLfloat x, GLfloat y) +void GLTexture::draw(float x, float y) { - // INITIALIZATION + /* Getting shader program */ + GPUShader *shader_program = GPU_shader_get_builtin_shader(GPU_SHADER_3D_IMAGE); + //GLint shader_program; + //glGetIntegerv(GL_CURRENT_PROGRAM, &shader_program); - // Getting shader program - GLint shader_program; - glGetIntegerv(GL_CURRENT_PROGRAM, &shader_program); + //// Generate vertex array + //GLuint vertex_array; + //glGenVertexArrays(1, &vertex_array); - // Generate vertex array - GLuint vertex_array; - glGenVertexArrays(1, &vertex_array); + //int position_location = GPU_shader_get_attribute(shader_program, "pos"); + //int texcoord_location = GPU_shader_get_attribute(shader_program, "texCoord"); - GLint texturecoord_location = glGetAttribLocation(shader_program, "texCoord"); - GLint position_location = glGetAttribLocation(shader_program, "pos"); + //GLint texturecoord_location = glGetAttribLocation(shader_program, "texCoord"); + //GLint position_location = glGetAttribLocation(shader_program, "pos"); // Generate geometry buffers for drawing textured quad - GLfloat position[8] = {x, y, x + width, y, x + width, y + height, x, y + height}; - GLfloat texcoord[8] = {0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0}; + float position[8] = {x, y, x + width, y, x + width, y + height, x, y + height}; + float texcoord[8] = {0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0}; - GLuint vertex_buffer[2]; - glGenBuffers(2, vertex_buffer); - glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]); - glBufferData(GL_ARRAY_BUFFER, 32, position, GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]); - glBufferData(GL_ARRAY_BUFFER, 32, texcoord, GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); + GPUVertFormat format = {0}; + GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 8, GPU_FETCH_FLOAT); + GPU_vertformat_attr_add(&format, "texCoord", GPU_COMP_F32, 8, GPU_FETCH_FLOAT); - // DRAWING - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texture_id); + GPUVertBuf *vertex_buffer = GPU_vertbuf_create_with_format(&format); + GPU_vertbuf_data_alloc(vertex_buffer, 2); + GPU_vertbuf_attr_fill(vertex_buffer, 0, position); + GPU_vertbuf_attr_fill(vertex_buffer, 1, texcoord); - glBindVertexArray(vertex_array); - glEnableVertexAttribArray(texturecoord_location); - glEnableVertexAttribArray(position_location); + //GLuint vertex_buffer[2]; + //glGenBuffers(2, vertex_buffer); + //glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]); + //glBufferData(GL_ARRAY_BUFFER, 32, position, GL_STATIC_DRAW); + //glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]); + //glBufferData(GL_ARRAY_BUFFER, 32, texcoord, GL_STATIC_DRAW); + //glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]); - glVertexAttribPointer(position_location, 2, GL_FLOAT, GL_FALSE, 0, nullptr); - glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]); - glVertexAttribPointer(texturecoord_location, 2, GL_FLOAT, GL_FALSE, 0, nullptr); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + //// DRAWING + GPUBatch *gpu_batch = GPU_batch_create(GPU_PRIM_TRI_FAN, vertex_buffer, nullptr); - glBindVertexArray(0); - glBindTexture(GL_TEXTURE_2D, 0); - // DELETING - glDeleteBuffers(2, vertex_buffer); - glDeleteVertexArrays(1, &vertex_array); + GPU_shader_bind(shader_program); + int slot = GPU_shader_get_sampler_binding(shader_program, "image"); + GPU_texture_bind(gpu_texture, slot); + GPU_shader_uniform_1i(shader_program, "image", slot); + + //glActiveTexture(GL_TEXTURE0); + //glBindTexture(GL_TEXTURE_2D, texture_id); + + //glBindVertexArray(vertex_array); + //glEnableVertexAttribArray(texturecoord_location); + //glEnableVertexAttribArray(position_location); + + //glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]); + //glVertexAttribPointer(position_location, 2, GL_FLOAT, GL_FALSE, 0, nullptr); + //glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]); + //glVertexAttribPointer(texturecoord_location, 2, GL_FLOAT, GL_FALSE, 0, nullptr); + //glBindBuffer(GL_ARRAY_BUFFER, 0); + + GPU_batch_set_shader(gpu_batch, shader_program); + GPU_batch_draw(gpu_batch); + + //glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + + //glBindVertexArray(0); + //glBindTexture(GL_TEXTURE_2D, 0); + + //// DELETING + //glDeleteBuffers(2, vertex_buffer); + //glDeleteVertexArrays(1, &vertex_array); } void ViewportEngine::sync(Depsgraph *depsgraph, @@ -298,8 +319,8 @@ void ViewportEngine::render(Depsgraph *depsgraph, bContext *context) engine->Execute(render_index.get(), &tasks); if ((bl_engine->type->flag & RE_USE_GPU_CONTEXT) == 0) { - texture.set_buffer(render_task_delegate->get_renderer_aov(pxr::HdAovTokens->color)); - texture.draw((GLfloat)view_settings.border[0], (GLfloat)view_settings.border[1]); + gpu_texture.set_buffer(render_task_delegate->get_renderer_aov(pxr::HdAovTokens->color)); + gpu_texture.draw(view_settings.border[0], view_settings.border[1]); } } diff --git a/source/blender/render/hydra/viewport_engine.h b/source/blender/render/hydra/viewport_engine.h index 3e44d2bb5acd..0a091e4e0e77 100644 --- a/source/blender/render/hydra/viewport_engine.h +++ b/source/blender/render/hydra/viewport_engine.h @@ -5,10 +5,10 @@ #include -#include - #include +#include "GPU_texture.h" + #include "engine.h" namespace blender::render::hydra { @@ -18,13 +18,13 @@ class GLTexture { GLTexture(); ~GLTexture(); void set_buffer(pxr::HdRenderBuffer *buffer); - void draw(GLfloat x, GLfloat y); + void draw(float x, float y); private: void create(pxr::HdRenderBuffer *buffer); void free(); - GLuint texture_id; + GPUTexture *gpu_texture; int width, height, channels; }; @@ -43,7 +43,7 @@ class ViewportEngine : public Engine { private: std::chrono::time_point time_begin; - GLTexture texture; + GLTexture gpu_texture; }; } // namespace blender::render::hydra -- 2.30.2 From df16bbff50b59a5b12636154e249321897396529 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 6 Apr 2023 18:50:26 +0300 Subject: [PATCH 2/9] Fixed drawing with GPU --- source/blender/render/hydra/viewport_engine.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/blender/render/hydra/viewport_engine.cc b/source/blender/render/hydra/viewport_engine.cc index 1053cbe30e73..cc397f64f77a 100644 --- a/source/blender/render/hydra/viewport_engine.cc +++ b/source/blender/render/hydra/viewport_engine.cc @@ -209,11 +209,11 @@ void GLTexture::draw(float x, float y) float texcoord[8] = {0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0}; GPUVertFormat format = {0}; - GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 8, GPU_FETCH_FLOAT); - GPU_vertformat_attr_add(&format, "texCoord", GPU_COMP_F32, 8, GPU_FETCH_FLOAT); + GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); + GPU_vertformat_attr_add(&format, "texCoord", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); GPUVertBuf *vertex_buffer = GPU_vertbuf_create_with_format(&format); - GPU_vertbuf_data_alloc(vertex_buffer, 2); + GPU_vertbuf_data_alloc(vertex_buffer, 4); GPU_vertbuf_attr_fill(vertex_buffer, 0, position); GPU_vertbuf_attr_fill(vertex_buffer, 1, texcoord); -- 2.30.2 From d7b1e8d41e5e922952b744c5a1e4c3920ba30a2e Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 6 Apr 2023 19:02:29 +0300 Subject: [PATCH 3/9] Code cleanup --- .../blender/render/hydra/viewport_engine.cc | 85 ++++--------------- source/blender/render/hydra/viewport_engine.h | 7 +- 2 files changed, 21 insertions(+), 71 deletions(-) diff --git a/source/blender/render/hydra/viewport_engine.cc b/source/blender/render/hydra/viewport_engine.cc index cc397f64f77a..8a6469b1b68b 100644 --- a/source/blender/render/hydra/viewport_engine.cc +++ b/source/blender/render/hydra/viewport_engine.cc @@ -12,7 +12,6 @@ #include "BKE_camera.h" #include "BLI_math_matrix.h" #include "DEG_depsgraph_query.h" -#include "GPU_shader.h" #include "GPU_batch.h" // clang-format on @@ -133,20 +132,20 @@ pxr::GfCamera ViewSettings::gf_camera() (float)border[3] / screen_height)); } -GLTexture::GLTexture() : gpu_texture(nullptr), width(0), height(0), channels(4) +GLTexture::GLTexture() : texture(nullptr), width(0), height(0), channels(4) { } GLTexture::~GLTexture() { - if (gpu_texture) { + if (texture) { free(); } } void GLTexture::set_buffer(pxr::HdRenderBuffer *buffer) { - if (!gpu_texture) { + if (!texture) { create(buffer); return; } @@ -158,7 +157,7 @@ void GLTexture::set_buffer(pxr::HdRenderBuffer *buffer) } void *data = buffer->Map(); - GPU_texture_update(gpu_texture, GPU_DATA_FLOAT, data); + GPU_texture_update(texture, GPU_DATA_FLOAT, data); buffer->Unmap(); } @@ -168,7 +167,7 @@ void GLTexture::create(pxr::HdRenderBuffer *buffer) height = buffer->GetHeight(); channels = pxr::HdGetComponentCount(buffer->GetFormat()); void *data = buffer->Map(); - gpu_texture = GPU_texture_create_2d("HydraRendeViewport", + texture = GPU_texture_create_2d("HydraRendeViewport", width, height, 1, @@ -177,88 +176,38 @@ void GLTexture::create(pxr::HdRenderBuffer *buffer) (float *)data); buffer->Unmap(); - GPU_texture_filter_mode(gpu_texture, true); - GPU_texture_wrap_mode(gpu_texture, true, true); + GPU_texture_filter_mode(texture, true); + GPU_texture_wrap_mode(texture, true, true); } void GLTexture::free() { - GPU_texture_free(gpu_texture); - gpu_texture = nullptr; + GPU_texture_free(texture); + texture = nullptr; } -void GLTexture::draw(float x, float y) +void GLTexture::draw(GPUShader *shader, float x, float y) { - /* Getting shader program */ - GPUShader *shader_program = GPU_shader_get_builtin_shader(GPU_SHADER_3D_IMAGE); - //GLint shader_program; - //glGetIntegerv(GL_CURRENT_PROGRAM, &shader_program); - - //// Generate vertex array - //GLuint vertex_array; - //glGenVertexArrays(1, &vertex_array); - - //int position_location = GPU_shader_get_attribute(shader_program, "pos"); - //int texcoord_location = GPU_shader_get_attribute(shader_program, "texCoord"); - - //GLint texturecoord_location = glGetAttribLocation(shader_program, "texCoord"); - //GLint position_location = glGetAttribLocation(shader_program, "pos"); - - // Generate geometry buffers for drawing textured quad float position[8] = {x, y, x + width, y, x + width, y + height, x, y + height}; float texcoord[8] = {0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0}; GPUVertFormat format = {0}; GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); GPU_vertformat_attr_add(&format, "texCoord", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); - GPUVertBuf *vertex_buffer = GPU_vertbuf_create_with_format(&format); GPU_vertbuf_data_alloc(vertex_buffer, 4); GPU_vertbuf_attr_fill(vertex_buffer, 0, position); GPU_vertbuf_attr_fill(vertex_buffer, 1, texcoord); - //GLuint vertex_buffer[2]; - //glGenBuffers(2, vertex_buffer); - //glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]); - //glBufferData(GL_ARRAY_BUFFER, 32, position, GL_STATIC_DRAW); - //glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]); - //glBufferData(GL_ARRAY_BUFFER, 32, texcoord, GL_STATIC_DRAW); - //glBindBuffer(GL_ARRAY_BUFFER, 0); + int slot = GPU_shader_get_sampler_binding(shader, "image"); + GPU_texture_bind(texture, slot); + GPU_shader_uniform_1i(shader, "image", slot); - - //// DRAWING GPUBatch *gpu_batch = GPU_batch_create(GPU_PRIM_TRI_FAN, vertex_buffer, nullptr); - - - GPU_shader_bind(shader_program); - int slot = GPU_shader_get_sampler_binding(shader_program, "image"); - GPU_texture_bind(gpu_texture, slot); - GPU_shader_uniform_1i(shader_program, "image", slot); - - //glActiveTexture(GL_TEXTURE0); - //glBindTexture(GL_TEXTURE_2D, texture_id); - - //glBindVertexArray(vertex_array); - //glEnableVertexAttribArray(texturecoord_location); - //glEnableVertexAttribArray(position_location); - - //glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]); - //glVertexAttribPointer(position_location, 2, GL_FLOAT, GL_FALSE, 0, nullptr); - //glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]); - //glVertexAttribPointer(texturecoord_location, 2, GL_FLOAT, GL_FALSE, 0, nullptr); - //glBindBuffer(GL_ARRAY_BUFFER, 0); - - GPU_batch_set_shader(gpu_batch, shader_program); + GPU_batch_set_shader(gpu_batch, shader); GPU_batch_draw(gpu_batch); - //glDrawArrays(GL_TRIANGLE_FAN, 0, 4); - - //glBindVertexArray(0); - //glBindTexture(GL_TEXTURE_2D, 0); - - //// DELETING - //glDeleteBuffers(2, vertex_buffer); - //glDeleteVertexArrays(1, &vertex_array); + GPU_BATCH_DISCARD_SAFE(gpu_batch); } void ViewportEngine::sync(Depsgraph *depsgraph, @@ -319,8 +268,8 @@ void ViewportEngine::render(Depsgraph *depsgraph, bContext *context) engine->Execute(render_index.get(), &tasks); if ((bl_engine->type->flag & RE_USE_GPU_CONTEXT) == 0) { - gpu_texture.set_buffer(render_task_delegate->get_renderer_aov(pxr::HdAovTokens->color)); - gpu_texture.draw(view_settings.border[0], view_settings.border[1]); + texture.set_buffer(render_task_delegate->get_renderer_aov(pxr::HdAovTokens->color)); + texture.draw(shader, view_settings.border[0], view_settings.border[1]); } } diff --git a/source/blender/render/hydra/viewport_engine.h b/source/blender/render/hydra/viewport_engine.h index 0a091e4e0e77..f4f774a916b4 100644 --- a/source/blender/render/hydra/viewport_engine.h +++ b/source/blender/render/hydra/viewport_engine.h @@ -8,6 +8,7 @@ #include #include "GPU_texture.h" +#include "GPU_shader.h" #include "engine.h" @@ -18,13 +19,13 @@ class GLTexture { GLTexture(); ~GLTexture(); void set_buffer(pxr::HdRenderBuffer *buffer); - void draw(float x, float y); + void draw(GPUShader *shader, float x, float y); private: void create(pxr::HdRenderBuffer *buffer); void free(); - GPUTexture *gpu_texture; + GPUTexture *texture; int width, height, channels; }; @@ -43,7 +44,7 @@ class ViewportEngine : public Engine { private: std::chrono::time_point time_begin; - GLTexture gpu_texture; + GLTexture texture; }; } // namespace blender::render::hydra -- 2.30.2 From f379d12a23f2fa62e89d5055349f003f9cc06970 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 6 Apr 2023 19:33:13 +0300 Subject: [PATCH 4/9] Fixed memory leak --- source/blender/render/hydra/viewport_engine.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/source/blender/render/hydra/viewport_engine.cc b/source/blender/render/hydra/viewport_engine.cc index 8a6469b1b68b..a35a9adc73f5 100644 --- a/source/blender/render/hydra/viewport_engine.cc +++ b/source/blender/render/hydra/viewport_engine.cc @@ -194,20 +194,20 @@ void GLTexture::draw(GPUShader *shader, float x, float y) GPUVertFormat format = {0}; GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); GPU_vertformat_attr_add(&format, "texCoord", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); - GPUVertBuf *vertex_buffer = GPU_vertbuf_create_with_format(&format); - GPU_vertbuf_data_alloc(vertex_buffer, 4); - GPU_vertbuf_attr_fill(vertex_buffer, 0, position); - GPU_vertbuf_attr_fill(vertex_buffer, 1, texcoord); + GPUVertBuf *vbo = GPU_vertbuf_create_with_format(&format); + GPU_vertbuf_data_alloc(vbo, 4); + GPU_vertbuf_attr_fill(vbo, 0, position); + GPU_vertbuf_attr_fill(vbo, 1, texcoord); int slot = GPU_shader_get_sampler_binding(shader, "image"); GPU_texture_bind(texture, slot); GPU_shader_uniform_1i(shader, "image", slot); - GPUBatch *gpu_batch = GPU_batch_create(GPU_PRIM_TRI_FAN, vertex_buffer, nullptr); + GPUBatch *gpu_batch = GPU_batch_create_ex(GPU_PRIM_TRI_FAN, vbo, nullptr, GPU_BATCH_OWNS_VBO); GPU_batch_set_shader(gpu_batch, shader); GPU_batch_draw(gpu_batch); - GPU_BATCH_DISCARD_SAFE(gpu_batch); + GPU_batch_discard(gpu_batch); } void ViewportEngine::sync(Depsgraph *depsgraph, -- 2.30.2 From 175465b8e611860777201dffbebe9bea2724d30e Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 7 Apr 2023 03:21:10 +0300 Subject: [PATCH 5/9] Improving working with batch, renaming. --- .../blender/render/hydra/viewport_engine.cc | 65 ++++++++++--------- source/blender/render/hydra/viewport_engine.h | 12 ++-- 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/source/blender/render/hydra/viewport_engine.cc b/source/blender/render/hydra/viewport_engine.cc index a35a9adc73f5..71561fba5713 100644 --- a/source/blender/render/hydra/viewport_engine.cc +++ b/source/blender/render/hydra/viewport_engine.cc @@ -12,7 +12,7 @@ #include "BKE_camera.h" #include "BLI_math_matrix.h" #include "DEG_depsgraph_query.h" -#include "GPU_batch.h" +#include "GPU_matrix.h" // clang-format on #include "camera.h" @@ -132,18 +132,30 @@ pxr::GfCamera ViewSettings::gf_camera() (float)border[3] / screen_height)); } -GLTexture::GLTexture() : texture(nullptr), width(0), height(0), channels(4) +DrawTexture::DrawTexture() : texture(nullptr), width(0), height(0), channels(4) { + float coords[8] = {0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0}; + + GPUVertFormat format = {0}; + GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); + GPU_vertformat_attr_add(&format, "texCoord", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); + GPUVertBuf *vbo = GPU_vertbuf_create_with_format(&format); + GPU_vertbuf_data_alloc(vbo, 4); + GPU_vertbuf_attr_fill(vbo, 0, coords); + GPU_vertbuf_attr_fill(vbo, 1, coords); + + batch = GPU_batch_create_ex(GPU_PRIM_TRI_FAN, vbo, nullptr, GPU_BATCH_OWNS_VBO); } -GLTexture::~GLTexture() +DrawTexture::~DrawTexture() { if (texture) { free(); } + GPU_batch_discard(batch); } -void GLTexture::set_buffer(pxr::HdRenderBuffer *buffer) +void DrawTexture::set_buffer(pxr::HdRenderBuffer *buffer) { if (!texture) { create(buffer); @@ -161,53 +173,44 @@ void GLTexture::set_buffer(pxr::HdRenderBuffer *buffer) buffer->Unmap(); } -void GLTexture::create(pxr::HdRenderBuffer *buffer) +void DrawTexture::create(pxr::HdRenderBuffer *buffer) { width = buffer->GetWidth(); height = buffer->GetHeight(); channels = pxr::HdGetComponentCount(buffer->GetFormat()); + void *data = buffer->Map(); texture = GPU_texture_create_2d("HydraRendeViewport", - width, - height, - 1, - GPU_RGBA16F, - GPU_TEXTURE_USAGE_GENERAL, - (float *)data); + width, + height, + 1, + GPU_RGBA16F, + GPU_TEXTURE_USAGE_GENERAL, + (float *)data); buffer->Unmap(); GPU_texture_filter_mode(texture, true); GPU_texture_wrap_mode(texture, true, true); } -void GLTexture::free() +void DrawTexture::free() { GPU_texture_free(texture); texture = nullptr; } -void GLTexture::draw(GPUShader *shader, float x, float y) +void DrawTexture::draw(GPUShader *shader, float x, float y) { - float position[8] = {x, y, x + width, y, x + width, y + height, x, y + height}; - float texcoord[8] = {0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0}; - - GPUVertFormat format = {0}; - GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); - GPU_vertformat_attr_add(&format, "texCoord", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); - GPUVertBuf *vbo = GPU_vertbuf_create_with_format(&format); - GPU_vertbuf_data_alloc(vbo, 4); - GPU_vertbuf_attr_fill(vbo, 0, position); - GPU_vertbuf_attr_fill(vbo, 1, texcoord); - int slot = GPU_shader_get_sampler_binding(shader, "image"); GPU_texture_bind(texture, slot); GPU_shader_uniform_1i(shader, "image", slot); - GPUBatch *gpu_batch = GPU_batch_create_ex(GPU_PRIM_TRI_FAN, vbo, nullptr, GPU_BATCH_OWNS_VBO); - GPU_batch_set_shader(gpu_batch, shader); - GPU_batch_draw(gpu_batch); - - GPU_batch_discard(gpu_batch); + GPU_matrix_push(); + GPU_matrix_translate_2f(x, y); + GPU_matrix_scale_2f(width, height); + GPU_batch_set_shader(batch, shader); + GPU_batch_draw(batch); + GPU_matrix_pop(); } void ViewportEngine::sync(Depsgraph *depsgraph, @@ -268,8 +271,8 @@ void ViewportEngine::render(Depsgraph *depsgraph, bContext *context) engine->Execute(render_index.get(), &tasks); if ((bl_engine->type->flag & RE_USE_GPU_CONTEXT) == 0) { - texture.set_buffer(render_task_delegate->get_renderer_aov(pxr::HdAovTokens->color)); - texture.draw(shader, view_settings.border[0], view_settings.border[1]); + 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]); } } diff --git a/source/blender/render/hydra/viewport_engine.h b/source/blender/render/hydra/viewport_engine.h index f4f774a916b4..154842fc7021 100644 --- a/source/blender/render/hydra/viewport_engine.h +++ b/source/blender/render/hydra/viewport_engine.h @@ -7,17 +7,18 @@ #include -#include "GPU_texture.h" +#include "GPU_batch.h" #include "GPU_shader.h" +#include "GPU_texture.h" #include "engine.h" namespace blender::render::hydra { -class GLTexture { +class DrawTexture { public: - GLTexture(); - ~GLTexture(); + DrawTexture(); + ~DrawTexture(); void set_buffer(pxr::HdRenderBuffer *buffer); void draw(GPUShader *shader, float x, float y); @@ -26,6 +27,7 @@ class GLTexture { void free(); GPUTexture *texture; + GPUBatch *batch; int width, height, channels; }; @@ -44,7 +46,7 @@ class ViewportEngine : public Engine { private: std::chrono::time_point time_begin; - GLTexture texture; + DrawTexture draw_texture; }; } // namespace blender::render::hydra -- 2.30.2 From a48ab8785342b8ea16a191a0544a5815686712da Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 7 Apr 2023 03:50:20 +0300 Subject: [PATCH 6/9] Adjusting CMakeLists.txt --- source/blender/render/hydra/CMakeLists.txt | 3 --- 1 file changed, 3 deletions(-) diff --git a/source/blender/render/hydra/CMakeLists.txt b/source/blender/render/hydra/CMakeLists.txt index fa3c6861fb17..97a0c98eb159 100644 --- a/source/blender/render/hydra/CMakeLists.txt +++ b/source/blender/render/hydra/CMakeLists.txt @@ -9,7 +9,6 @@ endif() add_definitions(-DTBB_SUPPRESS_DEPRECATED_MESSAGES=1) add_definitions(${GFLAGS_DEFINES}) -add_definitions(-DGLOG_NO_ABBREVIATED_SEVERITIES) add_definitions(-DBLENDER_GFLAGS_NAMESPACE=${GFLAGS_NAMESPACE}) add_definitions(-DBOOST_ALL_NO_LIB) @@ -31,7 +30,6 @@ set(INC ../../depsgraph ../../blenkernel ../../gpu - ../../gpu/opengl ../../gpu/intern ../../python/intern .. @@ -39,7 +37,6 @@ set(INC ) set(INC_SYS - ${Epoxy_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS} ${USD_INCLUDE_DIRS} ${BOOST_INCLUDE_DIR} -- 2.30.2 From ba5b42e4a7c92303890cf0d2474c2afd13dfb366 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 7 Apr 2023 04:24:51 +0300 Subject: [PATCH 7/9] Preparing FinalEngineGL --- source/blender/render/hydra/final_engine.cc | 16 ++++++++++++++++ source/blender/render/hydra/viewport_engine.cc | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 4052b81fe26e..1422f5b418b6 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -5,6 +5,8 @@ #include "BKE_lib_id.h" #include "DEG_depsgraph_query.h" +#include "GPU_texture.h" +#include "GPU_framebuffer.h" #include "camera.h" #include "final_engine.h" @@ -195,6 +197,20 @@ void FinalEngineGL::render(Depsgraph *depsgraph) {"Combined", std::vector(res[0] * res[1] * 4)}}; // 4 - number of channels std::vector &pixels = render_images["Combined"]; + //GPUFrameBuffer *framebuffer = GPU_framebuffer_create("fbHydraRenderFinal"); + //GPUTexture *texture = GPU_texture_create_2d("texHydraRenderViewport", + // res[0], + // res[1], + // 1, + // GPU_RGBA32F, + // GPU_TEXTURE_USAGE_GENERAL, + // nullptr); + //GPU_texture_filter_mode(texture, true); + //GPU_texture_wrap_mode(texture, true, true); + //GPU_framebuffer_texture_attach(framebuffer, texture, 0, 0); + + //GPU_framebuffer_bind(framebuffer); + GLuint framebuffer_name = 0; glGenFramebuffers(1, &framebuffer_name); glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_name); diff --git a/source/blender/render/hydra/viewport_engine.cc b/source/blender/render/hydra/viewport_engine.cc index 71561fba5713..1cc947e61c3b 100644 --- a/source/blender/render/hydra/viewport_engine.cc +++ b/source/blender/render/hydra/viewport_engine.cc @@ -180,7 +180,7 @@ void DrawTexture::create(pxr::HdRenderBuffer *buffer) channels = pxr::HdGetComponentCount(buffer->GetFormat()); void *data = buffer->Map(); - texture = GPU_texture_create_2d("HydraRendeViewport", + texture = GPU_texture_create_2d("texHydraRenderViewport", width, height, 1, -- 2.30.2 From dbaad025749c73a41414dd91cf1b3f96597e979c Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 7 Apr 2023 05:20:33 +0300 Subject: [PATCH 8/9] Adjusted final_engine.cc --- source/blender/render/hydra/final_engine.cc | 67 ++++++++------------- 1 file changed, 24 insertions(+), 43 deletions(-) diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 1422f5b418b6..97dc2d61976c 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -1,12 +1,10 @@ /* SPDX-License-Identifier: Apache-2.0 * Copyright 2011-2022 Blender Foundation */ -#include - #include "BKE_lib_id.h" #include "DEG_depsgraph_query.h" -#include "GPU_texture.h" #include "GPU_framebuffer.h" +#include "GPU_texture.h" #include "camera.h" #include "final_engine.h" @@ -197,45 +195,21 @@ void FinalEngineGL::render(Depsgraph *depsgraph) {"Combined", std::vector(res[0] * res[1] * 4)}}; // 4 - number of channels std::vector &pixels = render_images["Combined"]; - //GPUFrameBuffer *framebuffer = GPU_framebuffer_create("fbHydraRenderFinal"); - //GPUTexture *texture = GPU_texture_create_2d("texHydraRenderViewport", - // res[0], - // res[1], - // 1, - // GPU_RGBA32F, - // GPU_TEXTURE_USAGE_GENERAL, - // nullptr); - //GPU_texture_filter_mode(texture, true); - //GPU_texture_wrap_mode(texture, true, true); - //GPU_framebuffer_texture_attach(framebuffer, texture, 0, 0); + GPUFrameBuffer *framebuffer = GPU_framebuffer_create("fbHydraRenderFinal"); + GPUTexture *texture = GPU_texture_create_2d("texHydraRenderViewport", + res[0], + res[1], + 1, + GPU_RGBA32F, + GPU_TEXTURE_USAGE_GENERAL, + nullptr); + GPU_texture_filter_mode(texture, true); + GPU_texture_wrap_mode(texture, true, true); + GPU_framebuffer_texture_attach(framebuffer, texture, 0, 0); - //GPU_framebuffer_bind(framebuffer); - - 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, res[0], res[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); + GPU_framebuffer_bind(framebuffer); + float clear_color[4] = {0.0, 0.0, 0.0, 0.0}; + GPU_framebuffer_clear_color_depth(framebuffer, clear_color, 0.0); { // Release the GIL before calling into hydra, in case any hydra plugins call into python. @@ -262,12 +236,19 @@ void FinalEngineGL::render(Depsgraph *depsgraph) break; } - glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels.data()); + void *data = GPU_texture_read(texture, GPU_DATA_FLOAT, 0); + memcpy(pixels.data(), data, pixels.size() * sizeof(float)); + MEM_freeN(data); update_render_result(render_images, layer_name, res[0], res[1]); } - glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels.data()); + void *data = GPU_texture_read(texture, GPU_DATA_FLOAT, 0); + memcpy(pixels.data(), data, pixels.size() * sizeof(float)); + MEM_freeN(data); update_render_result(render_images, layer_name, res[0], res[1]); + + GPU_framebuffer_free(framebuffer); + GPU_texture_free(texture); } } // namespace blender::render::hydra -- 2.30.2 From 1138a9621fa431f33901f03389533fef211399ba Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 7 Apr 2023 05:23:30 +0300 Subject: [PATCH 9/9] Code cleanup --- source/blender/render/hydra/final_engine.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 97dc2d61976c..70287dc9f8ce 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -196,7 +196,7 @@ void FinalEngineGL::render(Depsgraph *depsgraph) std::vector &pixels = render_images["Combined"]; GPUFrameBuffer *framebuffer = GPU_framebuffer_create("fbHydraRenderFinal"); - GPUTexture *texture = GPU_texture_create_2d("texHydraRenderViewport", + GPUTexture *texture = GPU_texture_create_2d("texHydraRenderFinal", res[0], res[1], 1, @@ -209,7 +209,7 @@ void FinalEngineGL::render(Depsgraph *depsgraph) GPU_framebuffer_bind(framebuffer); float clear_color[4] = {0.0, 0.0, 0.0, 0.0}; - GPU_framebuffer_clear_color_depth(framebuffer, clear_color, 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. -- 2.30.2