forked from blender/blender
Move to use the GPU API instead OpenGL #23
@ -12,7 +12,7 @@
|
||||
#include "BKE_camera.h"
|
||||
#include "BLI_math_matrix.h"
|
||||
#include "DEG_depsgraph_query.h"
|
||||
#include "GPU_batch.h"
|
||||
#include "GPU_matrix.h"
|
||||
// clang-format on
|
||||
|
||||
#include "camera.h"
|
||||
@ -132,18 +132,30 @@ pxr::GfCamera ViewSettings::gf_camera()
|
||||
(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) {
|
||||
free();
|
||||
}
|
||||
GPU_batch_discard(batch);
|
||||
}
|
||||
|
||||
void GLTexture::set_buffer(pxr::HdRenderBuffer *buffer)
|
||||
void DrawTexture::set_buffer(pxr::HdRenderBuffer *buffer)
|
||||
{
|
||||
if (!texture) {
|
||||
create(buffer);
|
||||
@ -161,11 +173,12 @@ void GLTexture::set_buffer(pxr::HdRenderBuffer *buffer)
|
||||
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());
|
||||
|
||||
void *data = buffer->Map();
|
||||
texture = GPU_texture_create_2d("HydraRendeViewport",
|
||||
width,
|
||||
@ -180,34 +193,24 @@ void GLTexture::create(pxr::HdRenderBuffer *buffer)
|
||||
GPU_texture_wrap_mode(texture, true, true);
|
||||
}
|
||||
|
||||
void GLTexture::free()
|
||||
void DrawTexture::free()
|
||||
{
|
||||
GPU_texture_free(texture);
|
||||
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");
|
||||
GPU_texture_bind(texture, 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_batch_set_shader(gpu_batch, shader);
|
||||
GPU_batch_draw(gpu_batch);
|
||||
|
||||
GPU_batch_discard(gpu_batch);
|
||||
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,
|
||||
@ -268,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(shader, view_settings.border[0], 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]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,17 +7,18 @@
|
||||
|
||||
#include <pxr/imaging/hd/renderBuffer.h>
|
||||
|
||||
#include "GPU_texture.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(GPUShader *shader, float x, float y);
|
||||
|
||||
@ -26,6 +27,7 @@ class GLTexture {
|
||||
void free();
|
||||
|
||||
GPUTexture *texture;
|
||||
GPUBatch *batch;
|
||||
int width, height, channels;
|
||||
};
|
||||
|
||||
@ -44,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
|
||||
|
Loading…
Reference in New Issue
Block a user