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 21 additions and 71 deletions
Showing only changes of commit d7b1e8d41e - Show all commits

View File

@ -12,7 +12,6 @@
#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_batch.h" #include "GPU_batch.h"
// clang-format on // clang-format on
@ -133,20 +132,20 @@ pxr::GfCamera ViewSettings::gf_camera()
(float)border[3] / screen_height)); (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() GLTexture::~GLTexture()
{ {
if (gpu_texture) { if (texture) {
free(); free();
} }
} }
void GLTexture::set_buffer(pxr::HdRenderBuffer *buffer) void GLTexture::set_buffer(pxr::HdRenderBuffer *buffer)
{ {
if (!gpu_texture) { if (!texture) {
create(buffer); create(buffer);
return; return;
} }
@ -158,7 +157,7 @@ void GLTexture::set_buffer(pxr::HdRenderBuffer *buffer)
} }
void *data = buffer->Map(); void *data = buffer->Map();
GPU_texture_update(gpu_texture, GPU_DATA_FLOAT, data); GPU_texture_update(texture, GPU_DATA_FLOAT, data);
buffer->Unmap(); buffer->Unmap();
} }
@ -168,7 +167,7 @@ void GLTexture::create(pxr::HdRenderBuffer *buffer)
height = buffer->GetHeight(); height = buffer->GetHeight();
channels = pxr::HdGetComponentCount(buffer->GetFormat()); channels = pxr::HdGetComponentCount(buffer->GetFormat());
void *data = buffer->Map(); void *data = buffer->Map();
gpu_texture = GPU_texture_create_2d("HydraRendeViewport", texture = GPU_texture_create_2d("HydraRendeViewport",
width, width,
height, height,
1, 1,
@ -177,88 +176,38 @@ void GLTexture::create(pxr::HdRenderBuffer *buffer)
(float *)data); (float *)data);
buffer->Unmap(); buffer->Unmap();
GPU_texture_filter_mode(gpu_texture, true); GPU_texture_filter_mode(texture, true);
GPU_texture_wrap_mode(gpu_texture, true, true); GPU_texture_wrap_mode(texture, true, true);
} }
void GLTexture::free() void GLTexture::free()
{ {
GPU_texture_free(gpu_texture); GPU_texture_free(texture);
gpu_texture = nullptr; 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 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}; float texcoord[8] = {0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0};
GPUVertFormat format = {0}; GPUVertFormat format = {0};
GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 2, 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); GPU_vertformat_attr_add(&format, "texCoord", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
GPUVertBuf *vertex_buffer = GPU_vertbuf_create_with_format(&format); GPUVertBuf *vertex_buffer = GPU_vertbuf_create_with_format(&format);
GPU_vertbuf_data_alloc(vertex_buffer, 4); GPU_vertbuf_data_alloc(vertex_buffer, 4);
GPU_vertbuf_attr_fill(vertex_buffer, 0, position); GPU_vertbuf_attr_fill(vertex_buffer, 0, position);
GPU_vertbuf_attr_fill(vertex_buffer, 1, texcoord); GPU_vertbuf_attr_fill(vertex_buffer, 1, texcoord);
//GLuint vertex_buffer[2]; int slot = GPU_shader_get_sampler_binding(shader, "image");
//glGenBuffers(2, vertex_buffer); GPU_texture_bind(texture, slot);
//glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]); GPU_shader_uniform_1i(shader, "image", slot);
//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
GPUBatch *gpu_batch = GPU_batch_create(GPU_PRIM_TRI_FAN, vertex_buffer, nullptr); GPUBatch *gpu_batch = GPU_batch_create(GPU_PRIM_TRI_FAN, vertex_buffer, nullptr);
GPU_batch_set_shader(gpu_batch, shader);
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); GPU_batch_draw(gpu_batch);
//glDrawArrays(GL_TRIANGLE_FAN, 0, 4); GPU_BATCH_DISCARD_SAFE(gpu_batch);
//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,
@ -319,8 +268,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) {
gpu_texture.set_buffer(render_task_delegate->get_renderer_aov(pxr::HdAovTokens->color)); texture.set_buffer(render_task_delegate->get_renderer_aov(pxr::HdAovTokens->color));
gpu_texture.draw(view_settings.border[0], view_settings.border[1]); texture.draw(shader, view_settings.border[0], view_settings.border[1]);
} }
} }

View File

@ -8,6 +8,7 @@
#include <pxr/imaging/hd/renderBuffer.h> #include <pxr/imaging/hd/renderBuffer.h>
#include "GPU_texture.h" #include "GPU_texture.h"
#include "GPU_shader.h"
#include "engine.h" #include "engine.h"
@ -18,13 +19,13 @@ class GLTexture {
GLTexture(); GLTexture();
~GLTexture(); ~GLTexture();
void set_buffer(pxr::HdRenderBuffer *buffer); void set_buffer(pxr::HdRenderBuffer *buffer);
void draw(float x, float 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();
GPUTexture *gpu_texture; GPUTexture *texture;
int width, height, channels; int width, height, channels;
}; };
@ -43,7 +44,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 gpu_texture; GLTexture texture;
}; };
} // namespace blender::render::hydra } // namespace blender::render::hydra