Duckstation/src/pse/gpu_hw_opengl.cpp

375 lines
11 KiB
C++
Raw Normal View History

2019-09-12 02:53:04 +00:00
#include "gpu_hw_opengl.h"
#include "YBaseLib/Assert.h"
#include "YBaseLib/Log.h"
2019-09-12 14:18:13 +00:00
#include "host_interface.h"
#include "system.h"
2019-09-12 02:53:04 +00:00
Log_SetChannel(GPU_HW_OpenGL);
GPU_HW_OpenGL::GPU_HW_OpenGL() : GPU_HW() {}
GPU_HW_OpenGL::~GPU_HW_OpenGL()
{
DestroyFramebuffer();
}
2019-09-20 13:40:19 +00:00
bool GPU_HW_OpenGL::Initialize(System* system, DMA* dma, InterruptController* interrupt_controller, Timers* timers)
2019-09-12 02:53:04 +00:00
{
2019-09-20 13:40:19 +00:00
if (!GPU_HW::Initialize(system, dma, interrupt_controller, timers))
2019-09-12 02:53:04 +00:00
return false;
CreateFramebuffer();
2019-09-12 14:18:13 +00:00
CreateVertexBuffer();
if (!CompilePrograms())
return false;
2019-09-12 02:53:04 +00:00
return true;
}
void GPU_HW_OpenGL::Reset()
{
GPU_HW::Reset();
ClearFramebuffer();
}
std::tuple<s32, s32> GPU_HW_OpenGL::ConvertToFramebufferCoordinates(s32 x, s32 y)
{
return std::make_tuple(x, static_cast<s32>(static_cast<s32>(VRAM_HEIGHT) - y));
}
2019-09-12 02:53:04 +00:00
void GPU_HW_OpenGL::CreateFramebuffer()
{
2019-09-12 14:18:13 +00:00
m_framebuffer_texture =
std::make_unique<GL::Texture>(VRAM_WIDTH, VRAM_HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, nullptr, false);
2019-09-12 02:53:04 +00:00
glGenFramebuffers(1, &m_framebuffer_fbo_id);
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer_fbo_id);
2019-09-12 14:18:13 +00:00
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_framebuffer_texture->GetGLId(), 0);
2019-09-12 02:53:04 +00:00
Assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
m_texture_page_texture =
std::make_unique<GL::Texture>(TEXTURE_PAGE_WIDTH, TEXTURE_PAGE_HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, nullptr, false);
glGenFramebuffers(1, &m_texture_page_fbo_id);
glBindFramebuffer(GL_FRAMEBUFFER, m_texture_page_fbo_id);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_texture_page_texture->GetGLId(), 0);
Assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
2019-09-12 02:53:04 +00:00
}
void GPU_HW_OpenGL::ClearFramebuffer()
{
// TODO: get rid of the FBO switches
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer_fbo_id);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
2019-09-12 14:18:13 +00:00
m_system->GetHostInterface()->SetDisplayTexture(m_framebuffer_texture.get(), 0, 0, VRAM_WIDTH, VRAM_HEIGHT);
2019-09-12 02:53:04 +00:00
}
void GPU_HW_OpenGL::DestroyFramebuffer()
{
glDeleteFramebuffers(1, &m_texture_page_fbo_id);
m_texture_page_fbo_id = 0;
m_texture_page_texture.reset();
2019-09-12 02:53:04 +00:00
glDeleteFramebuffers(1, &m_framebuffer_fbo_id);
m_framebuffer_fbo_id = 0;
2019-09-12 14:18:13 +00:00
m_framebuffer_texture.reset();
2019-09-12 02:53:04 +00:00
}
2019-09-12 14:18:13 +00:00
void GPU_HW_OpenGL::CreateVertexBuffer()
2019-09-12 02:53:04 +00:00
{
2019-09-12 14:18:13 +00:00
glGenBuffers(1, &m_vertex_buffer);
2019-09-12 02:53:04 +00:00
glBindBuffer(GL_ARRAY_BUFFER, m_vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, VERTEX_BUFFER_SIZE, nullptr, GL_STREAM_DRAW);
2019-09-12 14:18:13 +00:00
glGenVertexArrays(1, &m_vao_id);
glBindVertexArray(m_vao_id);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glVertexAttribIPointer(0, 2, GL_INT, sizeof(HWVertex), reinterpret_cast<void*>(offsetof(HWVertex, x)));
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, true, sizeof(HWVertex),
reinterpret_cast<void*>(offsetof(HWVertex, color)));
glVertexAttribPointer(2, 2, GL_UNSIGNED_BYTE, true, sizeof(HWVertex),
reinterpret_cast<void*>(offsetof(HWVertex, texcoord)));
2019-09-12 14:18:13 +00:00
glBindVertexArray(0);
glGenVertexArrays(1, &m_attributeless_vao_id);
2019-09-12 02:53:04 +00:00
}
2019-09-12 14:18:13 +00:00
bool GPU_HW_OpenGL::CompilePrograms()
2019-09-12 02:53:04 +00:00
{
bool result = true;
result &= CompileProgram(m_color_program, false, false);
result &= CompileProgram(m_texture_program, true, false);
result &= CompileProgram(m_blended_texture_program, true, true);
if (!result)
return false;
const std::string screen_quad_vs = GenerateScreenQuadVertexShader();
for (u32 palette_size = 0; palette_size < static_cast<u32>(m_texture_page_programs.size()); palette_size++)
2019-09-12 14:18:13 +00:00
{
2019-09-14 06:43:39 +00:00
const std::string fs = GenerateTexturePageFragmentShader(static_cast<TextureColorMode>(palette_size));
2019-09-12 02:53:04 +00:00
GL::Program& prog = m_texture_page_programs[palette_size];
if (!prog.Compile(screen_quad_vs.c_str(), fs.c_str()))
2019-09-12 14:18:13 +00:00
return false;
2019-09-12 02:53:04 +00:00
prog.BindFragData(0, "o_col0");
2019-09-12 02:53:04 +00:00
2019-09-12 14:18:13 +00:00
if (!prog.Link())
return false;
prog.RegisterUniform("samp0");
prog.RegisterUniform("base_offset");
prog.RegisterUniform("palette_offset");
prog.Bind();
prog.Uniform1i(0, 0);
2019-09-12 14:18:13 +00:00
}
2019-09-12 02:53:04 +00:00
2019-09-12 14:18:13 +00:00
return true;
}
2019-09-12 02:53:04 +00:00
bool GPU_HW_OpenGL::CompileProgram(GL::Program& prog, bool textured, bool blending)
2019-09-12 14:18:13 +00:00
{
const std::string vs = GenerateVertexShader(textured);
const std::string fs = GenerateFragmentShader(textured, blending);
if (!prog.Compile(vs.c_str(), fs.c_str()))
return false;
prog.BindAttribute(0, "a_pos");
prog.BindAttribute(1, "a_col0");
if (textured)
prog.BindAttribute(2, "a_tex0");
prog.BindFragData(0, "o_col0");
if (!prog.Link())
2019-09-12 14:18:13 +00:00
return false;
2019-09-12 02:53:04 +00:00
prog.Bind();
prog.RegisterUniform("u_pos_offset");
prog.Uniform2i(0, 0, 0);
if (textured)
{
prog.RegisterUniform("samp0");
prog.Uniform1i(1, 0);
}
2019-09-12 14:18:13 +00:00
return true;
}
2019-09-12 02:53:04 +00:00
void GPU_HW_OpenGL::SetProgram(bool textured, bool blending)
{
const GL::Program& prog = textured ? (blending ? m_blended_texture_program : m_texture_program) : m_color_program;
prog.Bind();
if (textured)
m_texture_page_texture->Bind();
prog.Uniform2i(0, m_drawing_offset.x, m_drawing_offset.y);
}
2019-09-12 14:18:13 +00:00
void GPU_HW_OpenGL::SetViewport()
{
glViewport(0, 0, VRAM_WIDTH, VRAM_HEIGHT);
2019-09-12 02:53:04 +00:00
}
2019-09-14 11:54:58 +00:00
void GPU_HW_OpenGL::SetScissor()
{
int left, top, right, bottom;
CalcScissorRect(&left, &top, &right, &bottom);
const int width = right - left;
const int height = bottom - top;
const int x = left;
const int y = VRAM_HEIGHT - bottom;
Log_DebugPrintf("SetScissor: (%d-%d, %d-%d)", x, x + width, y, y + height);
glScissor(x, y, width, height);
}
2019-09-12 14:18:13 +00:00
2019-09-18 14:55:06 +00:00
void GPU_HW_OpenGL::SetBlendState()
{
struct BlendVars
{
GLenum src_factor;
GLenum func;
GLenum dst_factor;
float color;
};
static const std::array<BlendVars, 4> blend_vars = {{
{GL_CONSTANT_COLOR, GL_FUNC_ADD, GL_CONSTANT_COLOR, 0.5f}, // B/2 + F/2
{GL_ONE, GL_FUNC_ADD, GL_ONE, -1.0f}, // B + F
{GL_ONE, GL_FUNC_REVERSE_SUBTRACT, GL_ONE, -1.0f}, // B - F
{GL_CONSTANT_COLOR, GL_FUNC_ADD, GL_ONE, 0.25f} // B + F/4
}};
if (!m_batch.transparency_enable)
2019-09-18 14:55:06 +00:00
{
glDisable(GL_BLEND);
return;
}
const BlendVars& vars = blend_vars[static_cast<u8>(m_batch.transparency_mode)];
2019-09-18 14:55:06 +00:00
glEnable(GL_BLEND);
glBlendFuncSeparate(vars.src_factor, vars.dst_factor, GL_ONE, GL_ZERO);
glBlendEquationSeparate(vars.func, GL_FUNC_ADD);
if (vars.color >= 0.0f)
glBlendColor(vars.color, vars.color, vars.color, 1.0f);
}
void GPU_HW_OpenGL::UpdateDisplay()
{
GPU_HW::UpdateDisplay();
m_system->GetHostInterface()->SetDisplayTexture(m_framebuffer_texture.get(), 0, 0, VRAM_WIDTH, VRAM_HEIGHT);
}
2019-09-14 10:45:26 +00:00
void GPU_HW_OpenGL::ReadVRAM(u32 x, u32 y, u32 width, u32 height, void* buffer)
{
// we need to convert RGBA8 -> RGBA5551
std::vector<u32> temp_buffer(width * height);
glBindFramebuffer(GL_READ_FRAMEBUFFER, m_framebuffer_fbo_id);
2019-09-14 11:34:55 +00:00
glReadPixels(x, VRAM_HEIGHT - y - height, width, height, GL_RGBA, GL_UNSIGNED_BYTE, temp_buffer.data());
2019-09-14 10:45:26 +00:00
// reverse copy because of lower-left origin
const u32 source_stride = width * sizeof(u32);
const u8* source_ptr = reinterpret_cast<const u8*>(temp_buffer.data()) + (source_stride * (height - 1));
const u32 dst_stride = width * sizeof(u16);
u8* dst_ptr = static_cast<u8*>(buffer);
for (u32 row = 0; row < height; row++)
{
const u8* source_row_ptr = source_ptr;
u8* dst_row_ptr = dst_ptr;
for (u32 col = 0; col < width; col++)
{
u32 src_col;
std::memcpy(&src_col, source_row_ptr, sizeof(src_col));
source_row_ptr += sizeof(src_col);
const u16 dst_col = RGBA8888ToRGBA5551(src_col);
2019-09-14 10:45:26 +00:00
std::memcpy(dst_row_ptr, &dst_col, sizeof(dst_col));
dst_row_ptr += sizeof(dst_col);
}
source_ptr -= source_stride;
dst_ptr += dst_stride;
}
}
void GPU_HW_OpenGL::FillVRAM(u32 x, u32 y, u32 width, u32 height, u16 color)
2019-09-14 06:43:39 +00:00
{
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer_fbo_id);
glEnable(GL_SCISSOR_TEST);
glScissor(x, VRAM_HEIGHT - y - height, width, height);
const auto [r, g, b, a] = RGBA8ToFloat(RGBA5551ToRGBA8888(color));
2019-09-14 06:43:39 +00:00
glClearColor(r, g, b, a);
glClear(GL_COLOR_BUFFER_BIT);
}
2019-09-12 15:10:08 +00:00
void GPU_HW_OpenGL::UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data)
{
std::vector<u32> rgba_data;
rgba_data.reserve(width * height);
2019-09-12 15:10:08 +00:00
// reverse copy the rows so it matches opengl's lower-left origin
const u32 source_stride = width * sizeof(u16);
const u8* source_ptr = static_cast<const u8*>(data) + (source_stride * (height - 1));
for (u32 row = 0; row < height; row++)
2019-09-12 15:10:08 +00:00
{
const u8* source_row_ptr = source_ptr;
for (u32 col = 0; col < width; col++)
{
u16 src_col;
std::memcpy(&src_col, source_row_ptr, sizeof(src_col));
source_row_ptr += sizeof(src_col);
const u32 dst_col = RGBA5551ToRGBA8888(src_col);
rgba_data.push_back(dst_col);
}
2019-09-12 15:10:08 +00:00
source_ptr -= source_stride;
2019-09-12 15:10:08 +00:00
}
m_framebuffer_texture->Bind();
// lower-left origin flip happens here
glTexSubImage2D(GL_TEXTURE_2D, 0, x, VRAM_HEIGHT - y - height, width, height, GL_RGBA, GL_UNSIGNED_BYTE,
2019-09-12 15:10:08 +00:00
rgba_data.data());
}
2019-09-17 14:58:30 +00:00
void GPU_HW_OpenGL::CopyVRAM(u32 src_x, u32 src_y, u32 dst_x, u32 dst_y, u32 width, u32 height)
{
glDisable(GL_SCISSOR_TEST);
// lower-left origin flip
src_y = VRAM_HEIGHT - src_y - height;
dst_y = VRAM_HEIGHT - dst_y - height;
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer_fbo_id);
glBlitFramebuffer(src_x, src_y, src_x + width, src_y + height, dst_x, dst_y, dst_x + width, dst_y + height,
GL_COLOR_BUFFER_BIT, GL_NEAREST);
}
void GPU_HW_OpenGL::UpdateTexturePageTexture()
2019-09-12 02:53:04 +00:00
{
glBindFramebuffer(GL_FRAMEBUFFER, m_texture_page_fbo_id);
m_framebuffer_texture->Bind();
2019-09-12 02:53:04 +00:00
glDisable(GL_BLEND);
2019-09-14 11:54:58 +00:00
glDisable(GL_SCISSOR_TEST);
glViewport(0, 0, TEXTURE_PAGE_WIDTH, TEXTURE_PAGE_HEIGHT);
glBindVertexArray(m_attributeless_vao_id);
const GL::Program& prog = m_texture_page_programs[static_cast<u8>(m_render_state.texture_color_mode)];
prog.Bind();
prog.Uniform2i(1, m_render_state.texture_base_x, m_render_state.texture_base_y);
if (m_render_state.texture_color_mode >= GPU::TextureColorMode::Palette4Bit)
prog.Uniform2i(2, m_render_state.texture_palette_x, m_render_state.texture_palette_y);
2019-09-12 02:53:04 +00:00
glDrawArrays(GL_TRIANGLES, 0, 3);
m_framebuffer_texture->Unbind();
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer_fbo_id);
}
void GPU_HW_OpenGL::FlushRender()
{
if (m_batch.vertices.empty())
return;
2019-09-14 11:54:58 +00:00
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glEnable(GL_SCISSOR_TEST);
glDepthMask(GL_FALSE);
SetProgram(m_batch.texture_enable, m_batch.texture_blending_enable);
2019-09-12 14:18:13 +00:00
SetViewport();
2019-09-14 11:54:58 +00:00
SetScissor();
2019-09-18 14:55:06 +00:00
SetBlendState();
2019-09-12 02:53:04 +00:00
2019-09-12 14:18:13 +00:00
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer_fbo_id);
glBindVertexArray(m_vao_id);
2019-09-12 02:53:04 +00:00
Assert((m_batch.vertices.size() * sizeof(HWVertex)) <= VERTEX_BUFFER_SIZE);
2019-09-12 14:18:13 +00:00
glBindBuffer(GL_ARRAY_BUFFER, m_vertex_buffer);
glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast<GLsizei>(sizeof(HWVertex) * m_batch.vertices.size()),
m_batch.vertices.data());
2019-09-12 02:53:04 +00:00
static constexpr std::array<GLenum, 3> gl_primitives = {{GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP}};
glDrawArrays(gl_primitives[static_cast<u8>(m_batch.primitive)], 0, static_cast<GLsizei>(m_batch.vertices.size()));
2019-09-12 02:53:04 +00:00
m_batch.vertices.clear();
2019-09-12 14:18:13 +00:00
}
2019-09-12 02:53:04 +00:00
2019-09-12 14:18:13 +00:00
std::unique_ptr<GPU> GPU::CreateHardwareOpenGLRenderer()
{
return std::make_unique<GPU_HW_OpenGL>();
2019-09-12 02:53:04 +00:00
}