Move to use the GPU API instead OpenGL #23

Merged
Bogdan Nagirniak merged 9 commits from BLEN-371 into hydra-render 2023-04-07 13:28:49 +02:00
4 changed files with 81 additions and 111 deletions

View File

@ -9,7 +9,6 @@ endif()
add_definitions(-DTBB_SUPPRESS_DEPRECATED_MESSAGES=1) add_definitions(-DTBB_SUPPRESS_DEPRECATED_MESSAGES=1)
add_definitions(${GFLAGS_DEFINES}) add_definitions(${GFLAGS_DEFINES})
add_definitions(-DGLOG_NO_ABBREVIATED_SEVERITIES)
add_definitions(-DBLENDER_GFLAGS_NAMESPACE=${GFLAGS_NAMESPACE}) add_definitions(-DBLENDER_GFLAGS_NAMESPACE=${GFLAGS_NAMESPACE})
add_definitions(-DBOOST_ALL_NO_LIB) add_definitions(-DBOOST_ALL_NO_LIB)
@ -31,7 +30,6 @@ set(INC
../../depsgraph ../../depsgraph
../../blenkernel ../../blenkernel
../../gpu ../../gpu
../../gpu/opengl
../../gpu/intern ../../gpu/intern
../../python/intern ../../python/intern
.. ..
@ -39,7 +37,6 @@ set(INC
) )
set(INC_SYS set(INC_SYS
${Epoxy_INCLUDE_DIRS}
${PYTHON_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS}
${USD_INCLUDE_DIRS} ${USD_INCLUDE_DIRS}
${BOOST_INCLUDE_DIR} ${BOOST_INCLUDE_DIR}

View File

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

View File

@ -1,8 +1,6 @@
/* SPDX-License-Identifier: Apache-2.0 /* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */ * Copyright 2011-2022 Blender Foundation */
#include <epoxy/gl.h>
#include <pxr/base/gf/camera.h> #include <pxr/base/gf/camera.h>
#include <pxr/imaging/glf/drawTarget.h> #include <pxr/imaging/glf/drawTarget.h>
#include <pxr/usd/usdGeom/camera.h> #include <pxr/usd/usdGeom/camera.h>
@ -14,7 +12,7 @@
#include "BKE_camera.h" #include "BKE_camera.h"
#include "BLI_math_matrix.h" #include "BLI_math_matrix.h"
#include "DEG_depsgraph_query.h" #include "DEG_depsgraph_query.h"
#include "GPU_shader.h" #include "GPU_matrix.h"
// clang-format on // clang-format on
#include "camera.h" #include "camera.h"
@ -134,20 +132,32 @@ pxr::GfCamera ViewSettings::gf_camera()
(float)border[3] / screen_height)); (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(); 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); create(buffer);
return; return;
} }
@ -158,86 +168,49 @@ void GLTexture::set_buffer(pxr::HdRenderBuffer *buffer)
return; return;
} }
glBindTexture(GL_TEXTURE_2D, texture_id);
void *data = buffer->Map(); 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(); buffer->Unmap();
} }
void GLTexture::create(pxr::HdRenderBuffer *buffer) void DrawTexture::create(pxr::HdRenderBuffer *buffer)
{ {
width = buffer->GetWidth(); width = buffer->GetWidth();
height = buffer->GetHeight(); height = buffer->GetHeight();
channels = pxr::HdGetComponentCount(buffer->GetFormat()); 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(); 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(); 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); GPU_texture_free(texture);
texture_id = 0; 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 GPU_matrix_push();
GLint shader_program; GPU_matrix_translate_2f(x, y);
glGetIntegerv(GL_CURRENT_PROGRAM, &shader_program); GPU_matrix_scale_2f(width, height);
GPU_batch_set_shader(batch, shader);
// Generate vertex array GPU_batch_draw(batch);
GLuint vertex_array; GPU_matrix_pop();
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);
} }
void ViewportEngine::sync(Depsgraph *depsgraph, void ViewportEngine::sync(Depsgraph *depsgraph,
@ -298,8 +271,8 @@ void ViewportEngine::render(Depsgraph *depsgraph, bContext *context)
engine->Execute(render_index.get(), &tasks); engine->Execute(render_index.get(), &tasks);
if ((bl_engine->type->flag & RE_USE_GPU_CONTEXT) == 0) { if ((bl_engine->type->flag & RE_USE_GPU_CONTEXT) == 0) {
texture.set_buffer(render_task_delegate->get_renderer_aov(pxr::HdAovTokens->color)); draw_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.draw(shader, view_settings.border[0], view_settings.border[1]);
} }
} }

View File

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