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 41 additions and 36 deletions
Showing only changes of commit 175465b8e6 - Show all commits

View File

@ -12,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_batch.h" #include "GPU_matrix.h"
// clang-format on // clang-format on
#include "camera.h" #include "camera.h"
@ -132,18 +132,30 @@ pxr::GfCamera ViewSettings::gf_camera()
(float)border[3] / screen_height)); (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) { if (texture) {
free(); free();
} }
GPU_batch_discard(batch);
} }
void GLTexture::set_buffer(pxr::HdRenderBuffer *buffer) void DrawTexture::set_buffer(pxr::HdRenderBuffer *buffer)
{ {
if (!texture) { if (!texture) {
create(buffer); create(buffer);
@ -161,53 +173,44 @@ void GLTexture::set_buffer(pxr::HdRenderBuffer *buffer)
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());
void *data = buffer->Map(); void *data = buffer->Map();
texture = GPU_texture_create_2d("HydraRendeViewport", texture = GPU_texture_create_2d("HydraRendeViewport",
width, width,
height, height,
1, 1,
GPU_RGBA16F, GPU_RGBA16F,
GPU_TEXTURE_USAGE_GENERAL, GPU_TEXTURE_USAGE_GENERAL,
(float *)data); (float *)data);
buffer->Unmap(); buffer->Unmap();
GPU_texture_filter_mode(texture, true); GPU_texture_filter_mode(texture, true);
GPU_texture_wrap_mode(texture, true, true); GPU_texture_wrap_mode(texture, true, true);
} }
void GLTexture::free() void DrawTexture::free()
{ {
GPU_texture_free(texture); GPU_texture_free(texture);
texture = nullptr; 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"); int slot = GPU_shader_get_sampler_binding(shader, "image");
GPU_texture_bind(texture, slot); GPU_texture_bind(texture, slot);
GPU_shader_uniform_1i(shader, "image", 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_matrix_push();
GPU_batch_set_shader(gpu_batch, shader); GPU_matrix_translate_2f(x, y);
GPU_batch_draw(gpu_batch); GPU_matrix_scale_2f(width, height);
GPU_batch_set_shader(batch, shader);
GPU_batch_discard(gpu_batch); GPU_batch_draw(batch);
GPU_matrix_pop();
} }
void ViewportEngine::sync(Depsgraph *depsgraph, void ViewportEngine::sync(Depsgraph *depsgraph,
@ -268,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(shader, view_settings.border[0], view_settings.border[1]); draw_texture.draw(shader, view_settings.border[0], view_settings.border[1]);
} }
} }

View File

@ -7,17 +7,18 @@
#include <pxr/imaging/hd/renderBuffer.h> #include <pxr/imaging/hd/renderBuffer.h>
#include "GPU_texture.h" #include "GPU_batch.h"
#include "GPU_shader.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(GPUShader *shader, float x, float y); void draw(GPUShader *shader, float x, float y);
@ -26,6 +27,7 @@ class GLTexture {
void free(); void free();
GPUTexture *texture; GPUTexture *texture;
GPUBatch *batch;
int width, height, channels; int width, height, channels;
}; };
@ -44,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