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
2 changed files with 83 additions and 62 deletions
Showing only changes of commit ef68df0911 - Show all commits

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>
@ -15,6 +13,7 @@
#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_shader.h"
#include "GPU_batch.h"
// clang-format on // clang-format on
#include "camera.h" #include "camera.h"
@ -134,20 +133,20 @@ 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) GLTexture::GLTexture() : gpu_texture(nullptr), width(0), height(0), channels(4)
{ {
} }
GLTexture::~GLTexture() GLTexture::~GLTexture()
{ {
if (texture_id) { if (gpu_texture) {
free(); free();
} }
} }
void GLTexture::set_buffer(pxr::HdRenderBuffer *buffer) void GLTexture::set_buffer(pxr::HdRenderBuffer *buffer)
{ {
if (!texture_id) { if (!gpu_texture) {
create(buffer); create(buffer);
return; return;
} }
@ -158,10 +157,8 @@ 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(gpu_texture, GPU_DATA_FLOAT, data);
buffer->Unmap(); buffer->Unmap();
} }
@ -170,74 +167,98 @@ void GLTexture::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); gpu_texture = GPU_texture_create_2d("HydraRendeViewport",
width,
height,
1,
GPU_RGBA16F,
GPU_TEXTURE_USAGE_GENERAL,
(float *)data);
buffer->Unmap(); buffer->Unmap();
GPU_texture_filter_mode(gpu_texture, true);
GPU_texture_wrap_mode(gpu_texture, true, true);
} }
void GLTexture::free() void GLTexture::free()
{ {
glDeleteTextures(1, &texture_id); GPU_texture_free(gpu_texture);
texture_id = 0; 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 //// Generate vertex array
GLint shader_program; //GLuint vertex_array;
glGetIntegerv(GL_CURRENT_PROGRAM, &shader_program); //glGenVertexArrays(1, &vertex_array);
// Generate vertex array //int position_location = GPU_shader_get_attribute(shader_program, "pos");
GLuint vertex_array; //int texcoord_location = GPU_shader_get_attribute(shader_program, "texCoord");
glGenVertexArrays(1, &vertex_array);
GLint texturecoord_location = glGetAttribLocation(shader_program, "texCoord"); //GLint texturecoord_location = glGetAttribLocation(shader_program, "texCoord");
GLint position_location = glGetAttribLocation(shader_program, "pos"); //GLint position_location = glGetAttribLocation(shader_program, "pos");
// Generate geometry buffers for drawing textured quad // Generate geometry buffers for drawing textured quad
GLfloat position[8] = {x, y, x + width, y, x + width, y + height, x, y + height}; float 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 texcoord[8] = {0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0};
GLuint vertex_buffer[2]; GPUVertFormat format = {0};
glGenBuffers(2, vertex_buffer); GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 8, GPU_FETCH_FLOAT);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]); GPU_vertformat_attr_add(&format, "texCoord", GPU_COMP_F32, 8, GPU_FETCH_FLOAT);
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 GPUVertBuf *vertex_buffer = GPU_vertbuf_create_with_format(&format);
glActiveTexture(GL_TEXTURE0); GPU_vertbuf_data_alloc(vertex_buffer, 2);
glBindTexture(GL_TEXTURE_2D, texture_id); GPU_vertbuf_attr_fill(vertex_buffer, 0, position);
GPU_vertbuf_attr_fill(vertex_buffer, 1, texcoord);
glBindVertexArray(vertex_array); //GLuint vertex_buffer[2];
glEnableVertexAttribArray(texturecoord_location); //glGenBuffers(2, vertex_buffer);
glEnableVertexAttribArray(position_location); //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 GPU_shader_bind(shader_program);
glDeleteBuffers(2, vertex_buffer); int slot = GPU_shader_get_sampler_binding(shader_program, "image");
glDeleteVertexArrays(1, &vertex_array); 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, void ViewportEngine::sync(Depsgraph *depsgraph,
@ -298,8 +319,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)); gpu_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.draw(view_settings.border[0], view_settings.border[1]);
} }
} }

View File

@ -5,10 +5,10 @@
#include <chrono> #include <chrono>
#include <epoxy/gl.h>
#include <pxr/imaging/hd/renderBuffer.h> #include <pxr/imaging/hd/renderBuffer.h>
#include "GPU_texture.h"
#include "engine.h" #include "engine.h"
namespace blender::render::hydra { namespace blender::render::hydra {
@ -18,13 +18,13 @@ class GLTexture {
GLTexture(); GLTexture();
~GLTexture(); ~GLTexture();
void set_buffer(pxr::HdRenderBuffer *buffer); void set_buffer(pxr::HdRenderBuffer *buffer);
void draw(GLfloat x, GLfloat y); void draw(float x, float y);
private: private:
void create(pxr::HdRenderBuffer *buffer); void create(pxr::HdRenderBuffer *buffer);
void free(); void free();
GLuint texture_id; GPUTexture *gpu_texture;
int width, height, channels; int width, height, channels;
}; };
@ -43,7 +43,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; GLTexture gpu_texture;
}; };
} // namespace blender::render::hydra } // namespace blender::render::hydra