BLEN-365: Improve creation algorithm of PreviewEngine #21

Merged
Bogdan Nagirniak merged 11 commits from BLEN-365 into hydra-render 2023-04-10 12:44:48 +02:00
4 changed files with 81 additions and 111 deletions
Showing only changes of commit a853a7abee - Show all commits

View File

@ -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}

View File

@ -1,10 +1,10 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#include <pxr/imaging/glf/drawTarget.h>
#include "BKE_lib_id.h"
#include "DEG_depsgraph_query.h"
#include "GPU_framebuffer.h"
#include "GPU_texture.h"
#include "camera.h"
#include "final_engine.h"
@ -195,31 +195,21 @@ void FinalEngineGL::render(Depsgraph *depsgraph)
{"Combined", std::vector<float>(res[0] * res[1] * 4)}}; // 4 - number of channels
std::vector<float> &pixels = render_images["Combined"];
GLuint framebuffer_name = 0;
glGenFramebuffers(1, &framebuffer_name);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_name);
GPUFrameBuffer *framebuffer = GPU_framebuffer_create("fbHydraRenderFinal");
GPUTexture *texture = GPU_texture_create_2d("texHydraRenderFinal",
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);
// 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, 1.0);
{
// Release the GIL before calling into hydra, in case any hydra plugins call into python.
@ -246,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

View File

@ -1,8 +1,6 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#include <epoxy/gl.h>
#include <pxr/base/gf/camera.h>
#include <pxr/imaging/glf/drawTarget.h>
#include <pxr/usd/usdGeom/camera.h>
@ -14,7 +12,7 @@
#include "BKE_camera.h"
#include "BLI_math_matrix.h"
#include "DEG_depsgraph_query.h"
#include "GPU_shader.h"
#include "GPU_matrix.h"
// clang-format on
#include "camera.h"
@ -134,20 +132,32 @@ pxr::GfCamera ViewSettings::gf_camera()
(float)border[3] / screen_height));
}
GLTexture::GLTexture() : texture_id(0), 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_id) {
if (texture) {
free();
}
GPU_batch_discard(batch);
}
void GLTexture::set_buffer(pxr::HdRenderBuffer *buffer)
void DrawTexture::set_buffer(pxr::HdRenderBuffer *buffer)
{
if (!texture_id) {
if (!texture) {
create(buffer);
return;
}
@ -158,86 +168,49 @@ 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(texture, GPU_DATA_FLOAT, data);
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());
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);
texture = GPU_texture_create_2d("texHydraRenderViewport",
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()
{
glDeleteTextures(1, &texture_id);
texture_id = 0;
GPU_texture_free(texture);
texture = nullptr;
}
void GLTexture::draw(GLfloat x, GLfloat y)
void DrawTexture::draw(GPUShader *shader, float x, float y)
{
// INITIALIZATION
int slot = GPU_shader_get_sampler_binding(shader, "image");
GPU_texture_bind(texture, slot);
GPU_shader_uniform_1i(shader, "image", slot);
// Getting shader program
GLint shader_program;
glGetIntegerv(GL_CURRENT_PROGRAM, &shader_program);
// Generate vertex array
GLuint vertex_array;
glGenVertexArrays(1, &vertex_array);
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};
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);
// DRAWING
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);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
// DELETING
glDeleteBuffers(2, vertex_buffer);
glDeleteVertexArrays(1, &vertex_array);
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,
@ -298,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((GLfloat)view_settings.border[0], (GLfloat)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]);
}
}

View File

@ -5,26 +5,29 @@
#include <chrono>
#include <epoxy/gl.h>
#include <pxr/imaging/hd/renderBuffer.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(GLfloat x, GLfloat y);
void draw(GPUShader *shader, float x, float y);
private:
void create(pxr::HdRenderBuffer *buffer);
void free();
GLuint texture_id;
GPUTexture *texture;
GPUBatch *batch;
int width, height, channels;
};
@ -43,7 +46,7 @@ class ViewportEngine : public Engine {
private:
std::chrono::time_point<std::chrono::steady_clock> time_begin;
GLTexture texture;
DrawTexture draw_texture;
};
} // namespace blender::render::hydra