2019-09-12 02:53:04 +00:00
|
|
|
#include "gpu_hw_opengl.h"
|
2020-01-10 03:31:12 +00:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/log.h"
|
2021-01-23 09:00:54 +00:00
|
|
|
#include "common/state_wrapper.h"
|
2020-09-11 14:23:13 +00:00
|
|
|
#include "common/timer.h"
|
2019-11-03 03:36:54 +00:00
|
|
|
#include "gpu_hw_shadergen.h"
|
2019-11-03 14:39:48 +00:00
|
|
|
#include "host_display.h"
|
2021-01-11 04:50:25 +00:00
|
|
|
#include "shader_cache_version.h"
|
2019-09-12 14:18:13 +00:00
|
|
|
#include "system.h"
|
2020-12-25 08:02:38 +00:00
|
|
|
#include "texture_replacements.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()
|
|
|
|
{
|
2019-12-10 13:36:59 +00:00
|
|
|
// Destroy objects which don't have destructors to clean them up
|
2020-05-03 07:11:28 +00:00
|
|
|
if (m_vram_fbo_id != 0)
|
|
|
|
glDeleteFramebuffers(1, &m_vram_fbo_id);
|
2019-12-10 13:36:59 +00:00
|
|
|
if (m_vao_id != 0)
|
|
|
|
glDeleteVertexArrays(1, &m_vao_id);
|
|
|
|
if (m_attributeless_vao_id != 0)
|
|
|
|
glDeleteVertexArrays(1, &m_attributeless_vao_id);
|
|
|
|
if (m_texture_buffer_r16ui_texture != 0)
|
|
|
|
glDeleteTextures(1, &m_texture_buffer_r16ui_texture);
|
|
|
|
|
2019-11-05 09:51:08 +00:00
|
|
|
if (m_host_display)
|
|
|
|
{
|
2020-02-28 07:01:01 +00:00
|
|
|
m_host_display->ClearDisplayTexture();
|
2019-11-05 09:51:08 +00:00
|
|
|
ResetGraphicsAPIState();
|
|
|
|
}
|
2020-08-02 07:16:51 +00:00
|
|
|
|
|
|
|
// One of our programs might've been bound.
|
|
|
|
GL::Program::ResetLastProgram();
|
|
|
|
glUseProgram(0);
|
2019-09-12 02:53:04 +00:00
|
|
|
}
|
|
|
|
|
2021-03-28 03:47:10 +00:00
|
|
|
GPURenderer GPU_HW_OpenGL::GetRendererType() const
|
|
|
|
{
|
|
|
|
return GPURenderer::HardwareOpenGL;
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
bool GPU_HW_OpenGL::Initialize(HostDisplay* host_display)
|
2019-09-12 02:53:04 +00:00
|
|
|
{
|
2019-11-05 15:33:09 +00:00
|
|
|
if (host_display->GetRenderAPI() != HostDisplay::RenderAPI::OpenGL &&
|
|
|
|
host_display->GetRenderAPI() != HostDisplay::RenderAPI::OpenGLES)
|
2019-11-03 14:39:48 +00:00
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Host render API type is incompatible");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-21 08:17:00 +00:00
|
|
|
const bool opengl_is_available =
|
|
|
|
((host_display->GetRenderAPI() == HostDisplay::RenderAPI::OpenGL &&
|
|
|
|
(GLAD_GL_VERSION_3_0 || GLAD_GL_ARB_uniform_buffer_object)) ||
|
|
|
|
(host_display->GetRenderAPI() == HostDisplay::RenderAPI::OpenGLES && GLAD_GL_ES_VERSION_3_0));
|
|
|
|
if (!opengl_is_available)
|
|
|
|
{
|
|
|
|
g_host_interface->AddOSDMessage(
|
|
|
|
g_host_interface->TranslateStdString("OSDMessage", "OpenGL renderer unavailable, your driver or hardware is not "
|
|
|
|
"recent enough. OpenGL 3.1 or OpenGL ES 3.0 is required."),
|
|
|
|
20.0f);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-05 15:33:09 +00:00
|
|
|
SetCapabilities(host_display);
|
2019-11-03 15:57:08 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (!GPU_HW::Initialize(host_display))
|
2019-11-03 15:57:08 +00:00
|
|
|
return false;
|
|
|
|
|
2019-12-10 14:01:29 +00:00
|
|
|
if (!CreateFramebuffer())
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to create framebuffer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CreateVertexBuffer())
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to create vertex buffer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CreateUniformBuffer())
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to create uniform buffer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CreateTextureBuffer())
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to create texture buffer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-12 14:18:13 +00:00
|
|
|
if (!CompilePrograms())
|
2019-12-10 14:01:29 +00:00
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to compile programs");
|
2019-09-12 14:18:13 +00:00
|
|
|
return false;
|
2019-12-10 14:01:29 +00:00
|
|
|
}
|
2019-09-12 14:18:13 +00:00
|
|
|
|
2019-10-04 12:10:43 +00:00
|
|
|
RestoreGraphicsAPIState();
|
2019-09-12 02:53:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-23 09:00:54 +00:00
|
|
|
void GPU_HW_OpenGL::Reset(bool clear_vram)
|
2019-09-12 02:53:04 +00:00
|
|
|
{
|
2021-01-23 09:00:54 +00:00
|
|
|
GPU_HW::Reset(clear_vram);
|
2019-09-12 02:53:04 +00:00
|
|
|
|
2021-01-23 09:00:54 +00:00
|
|
|
if (clear_vram)
|
|
|
|
ClearFramebuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GPU_HW_OpenGL::DoState(StateWrapper& sw, HostDisplayTexture** host_texture, bool update_display)
|
|
|
|
{
|
|
|
|
if (host_texture)
|
|
|
|
{
|
2021-01-24 11:12:28 +00:00
|
|
|
HostDisplayTexture* tex = *host_texture;
|
2021-01-23 09:00:54 +00:00
|
|
|
if (sw.IsReading())
|
|
|
|
{
|
|
|
|
if (tex->GetWidth() != m_vram_texture.GetWidth() || tex->GetHeight() != m_vram_texture.GetHeight() ||
|
|
|
|
tex->GetSamples() != m_vram_texture.GetSamples())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyFramebufferForState(
|
|
|
|
m_vram_texture.GetGLTarget(), static_cast<GLuint>(reinterpret_cast<uintptr_t>(tex->GetHandle())), 0, 0, 0,
|
|
|
|
m_vram_texture.GetGLId(), m_vram_fbo_id, 0, 0, m_vram_texture.GetWidth(), m_vram_texture.GetHeight());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-24 11:12:28 +00:00
|
|
|
if (!tex || tex->GetWidth() != m_vram_texture.GetWidth() || tex->GetHeight() != m_vram_texture.GetHeight() ||
|
|
|
|
tex->GetSamples() != m_vram_texture.GetSamples())
|
|
|
|
{
|
|
|
|
delete tex;
|
|
|
|
|
|
|
|
tex = m_host_display
|
|
|
|
->CreateTexture(m_vram_texture.GetWidth(), m_vram_texture.GetHeight(), 1, 1,
|
|
|
|
m_vram_texture.GetSamples(), HostDisplayPixelFormat::RGBA8, nullptr, 0, false)
|
|
|
|
.release();
|
|
|
|
*host_texture = tex;
|
|
|
|
if (!tex)
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-23 09:00:54 +00:00
|
|
|
|
|
|
|
CopyFramebufferForState(m_vram_texture.GetGLTarget(), m_vram_texture.GetGLId(), m_vram_fbo_id, 0, 0,
|
|
|
|
static_cast<GLuint>(reinterpret_cast<uintptr_t>(tex->GetHandle())), 0, 0, 0,
|
|
|
|
m_vram_texture.GetWidth(), m_vram_texture.GetHeight());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return GPU_HW::DoState(sw, host_texture, update_display);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_HW_OpenGL::CopyFramebufferForState(GLenum target, GLuint src_texture, u32 src_fbo, u32 src_x, u32 src_y,
|
|
|
|
GLuint dst_texture, u32 dst_fbo, u32 dst_x, u32 dst_y, u32 width,
|
|
|
|
u32 height)
|
|
|
|
{
|
|
|
|
if (target != GL_TEXTURE_2D && GLAD_GL_VERSION_4_3)
|
|
|
|
{
|
|
|
|
glCopyImageSubData(src_texture, target, 0, src_x, src_y, 0, dst_texture, target, 0, dst_x, dst_y, 0, width, height,
|
|
|
|
1);
|
|
|
|
}
|
|
|
|
else if (target != GL_TEXTURE_2D && GLAD_GL_EXT_copy_image)
|
|
|
|
{
|
|
|
|
glCopyImageSubDataEXT(src_texture, target, 0, src_x, src_y, 0, dst_texture, target, 0, dst_x, dst_y, 0, width,
|
|
|
|
height, 1);
|
|
|
|
}
|
|
|
|
else if (target != GL_TEXTURE_2D && GLAD_GL_OES_copy_image)
|
|
|
|
{
|
|
|
|
glCopyImageSubDataOES(src_texture, target, 0, src_x, src_y, 0, dst_texture, target, 0, dst_x, dst_y, 0, width,
|
|
|
|
height, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (src_fbo == 0)
|
|
|
|
{
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, m_state_copy_fbo_id);
|
|
|
|
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, src_texture, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, src_fbo);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dst_fbo == 0)
|
|
|
|
{
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_state_copy_fbo_id);
|
|
|
|
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, dst_texture, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst_fbo);
|
|
|
|
}
|
|
|
|
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
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);
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
|
|
|
|
|
|
|
if (src_fbo == 0)
|
|
|
|
{
|
|
|
|
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
|
|
|
|
}
|
|
|
|
else if (dst_fbo == 0)
|
|
|
|
{
|
|
|
|
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_vram_fbo_id);
|
|
|
|
}
|
2019-09-12 02:53:04 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 12:10:43 +00:00
|
|
|
void GPU_HW_OpenGL::ResetGraphicsAPIState()
|
|
|
|
{
|
|
|
|
GPU_HW::ResetGraphicsAPIState();
|
|
|
|
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
glBindVertexArray(0);
|
2020-09-12 15:19:57 +00:00
|
|
|
m_uniform_stream_buffer->Unbind();
|
2019-10-04 12:10:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_HW_OpenGL::RestoreGraphicsAPIState()
|
|
|
|
{
|
2020-05-03 07:11:28 +00:00
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_vram_fbo_id);
|
2019-12-10 14:01:29 +00:00
|
|
|
glViewport(0, 0, m_vram_texture.GetWidth(), m_vram_texture.GetHeight());
|
2019-10-04 12:10:43 +00:00
|
|
|
|
|
|
|
glDisable(GL_CULL_FACE);
|
2020-05-03 07:11:28 +00:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
2019-10-04 12:10:43 +00:00
|
|
|
glEnable(GL_SCISSOR_TEST);
|
2020-05-03 07:11:28 +00:00
|
|
|
glDepthMask(GL_TRUE);
|
2019-10-04 12:10:43 +00:00
|
|
|
glBindVertexArray(m_vao_id);
|
2020-09-12 15:19:57 +00:00
|
|
|
m_uniform_stream_buffer->Bind();
|
2020-10-31 02:12:23 +00:00
|
|
|
m_vram_read_texture.Bind();
|
|
|
|
SetBlendMode();
|
2020-12-22 15:10:49 +00:00
|
|
|
m_current_depth_test = 0;
|
2020-10-31 02:12:23 +00:00
|
|
|
SetDepthFunc();
|
2019-11-05 09:19:49 +00:00
|
|
|
SetScissorFromDrawingArea();
|
|
|
|
m_batch_ubo_dirty = true;
|
2019-10-04 12:10:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 14:31:25 +00:00
|
|
|
void GPU_HW_OpenGL::UpdateSettings()
|
2019-09-26 04:03:32 +00:00
|
|
|
{
|
2019-11-01 14:31:25 +00:00
|
|
|
GPU_HW::UpdateSettings();
|
2019-09-26 04:03:32 +00:00
|
|
|
|
2020-08-10 12:37:30 +00:00
|
|
|
bool framebuffer_changed, shaders_changed;
|
|
|
|
UpdateHWSettings(&framebuffer_changed, &shaders_changed);
|
|
|
|
|
|
|
|
if (framebuffer_changed)
|
2020-08-10 18:03:11 +00:00
|
|
|
{
|
2020-10-30 14:38:06 +00:00
|
|
|
RestoreGraphicsAPIState();
|
|
|
|
ReadVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT);
|
|
|
|
ResetGraphicsAPIState();
|
2020-08-10 18:03:11 +00:00
|
|
|
m_host_display->ClearDisplayTexture();
|
2020-08-10 12:37:30 +00:00
|
|
|
CreateFramebuffer();
|
2020-08-10 18:03:11 +00:00
|
|
|
}
|
2020-08-10 12:37:30 +00:00
|
|
|
if (shaders_changed)
|
|
|
|
CompilePrograms();
|
|
|
|
|
2020-08-11 16:27:07 +00:00
|
|
|
if (framebuffer_changed)
|
|
|
|
{
|
|
|
|
RestoreGraphicsAPIState();
|
2020-12-14 16:19:28 +00:00
|
|
|
UpdateVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT, m_vram_ptr, false, false);
|
2020-08-11 16:27:07 +00:00
|
|
|
UpdateDepthBufferFromMaskBit();
|
|
|
|
UpdateDisplay();
|
|
|
|
ResetGraphicsAPIState();
|
|
|
|
}
|
2019-10-12 12:15:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 12:21:56 +00:00
|
|
|
void GPU_HW_OpenGL::MapBatchVertexPointer(u32 required_vertices)
|
|
|
|
{
|
2020-04-18 05:30:46 +00:00
|
|
|
DebugAssert(!m_batch_start_vertex_ptr);
|
2019-11-02 12:21:56 +00:00
|
|
|
|
|
|
|
const GL::StreamBuffer::MappingResult res =
|
2019-11-03 03:36:54 +00:00
|
|
|
m_vertex_stream_buffer->Map(sizeof(BatchVertex), required_vertices * sizeof(BatchVertex));
|
2019-11-02 12:21:56 +00:00
|
|
|
|
2019-11-03 03:36:54 +00:00
|
|
|
m_batch_start_vertex_ptr = static_cast<BatchVertex*>(res.pointer);
|
2019-11-02 12:21:56 +00:00
|
|
|
m_batch_current_vertex_ptr = m_batch_start_vertex_ptr;
|
|
|
|
m_batch_end_vertex_ptr = m_batch_start_vertex_ptr + res.space_aligned;
|
|
|
|
m_batch_base_vertex = res.index_aligned;
|
|
|
|
}
|
|
|
|
|
2020-04-18 05:30:46 +00:00
|
|
|
void GPU_HW_OpenGL::UnmapBatchVertexPointer(u32 used_vertices)
|
|
|
|
{
|
|
|
|
DebugAssert(m_batch_start_vertex_ptr);
|
|
|
|
|
|
|
|
m_vertex_stream_buffer->Unmap(used_vertices * sizeof(BatchVertex));
|
|
|
|
m_vertex_stream_buffer->Bind();
|
|
|
|
m_batch_start_vertex_ptr = nullptr;
|
|
|
|
m_batch_end_vertex_ptr = nullptr;
|
|
|
|
m_batch_current_vertex_ptr = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-09-13 16:07:31 +00:00
|
|
|
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-11-05 15:33:09 +00:00
|
|
|
void GPU_HW_OpenGL::SetCapabilities(HostDisplay* host_display)
|
2019-10-04 11:08:53 +00:00
|
|
|
{
|
|
|
|
GLint max_texture_size = VRAM_WIDTH;
|
|
|
|
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size);
|
|
|
|
Log_InfoPrintf("Max texture size: %dx%d", max_texture_size, max_texture_size);
|
2020-04-16 11:29:11 +00:00
|
|
|
m_max_resolution_scale = static_cast<u32>(max_texture_size / VRAM_WIDTH);
|
2019-11-03 03:15:17 +00:00
|
|
|
|
2020-10-30 14:38:06 +00:00
|
|
|
m_max_multisamples = 1;
|
2021-06-20 03:10:15 +00:00
|
|
|
if (GLAD_GL_ARB_texture_storage || GLAD_GL_ES_VERSION_3_2)
|
2020-10-31 05:20:06 +00:00
|
|
|
{
|
|
|
|
glGetIntegerv(GL_MAX_SAMPLES, reinterpret_cast<GLint*>(&m_max_multisamples));
|
|
|
|
if (m_max_multisamples == 0)
|
|
|
|
m_max_multisamples = 1;
|
|
|
|
}
|
2020-10-30 14:38:06 +00:00
|
|
|
|
2021-06-20 03:10:15 +00:00
|
|
|
m_supports_per_sample_shading = GLAD_GL_VERSION_4_0 || GLAD_GL_ES_VERSION_3_2 || GLAD_GL_ARB_sample_shading;
|
2020-10-30 14:38:06 +00:00
|
|
|
Log_InfoPrintf("Per-sample shading: %s", m_supports_per_sample_shading ? "supported" : "not supported");
|
|
|
|
Log_InfoPrintf("Max multisamples: %u", m_max_multisamples);
|
|
|
|
|
2019-11-03 03:15:17 +00:00
|
|
|
glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, reinterpret_cast<GLint*>(&m_uniform_buffer_alignment));
|
|
|
|
Log_InfoPrintf("Uniform buffer offset alignment: %u", m_uniform_buffer_alignment);
|
2019-11-05 13:05:57 +00:00
|
|
|
|
2020-11-30 14:08:26 +00:00
|
|
|
if (!GLAD_GL_VERSION_4_3 && !GLAD_GL_EXT_copy_image && !GLAD_GL_ES_VERSION_3_2 && !GLAD_GL_OES_copy_image)
|
|
|
|
Log_WarningPrintf("GL_EXT/OES_copy_image missing, this may affect performance.");
|
2019-11-05 13:15:54 +00:00
|
|
|
|
2020-06-26 11:39:37 +00:00
|
|
|
#ifdef __APPLE__
|
2020-05-25 10:26:13 +00:00
|
|
|
// Partial texture buffer uploads appear to be broken in macOS's OpenGL driver.
|
2021-03-18 02:44:45 +00:00
|
|
|
m_use_texture_buffer_for_vram_writes = false;
|
2020-05-25 10:26:13 +00:00
|
|
|
#else
|
2021-03-18 02:44:45 +00:00
|
|
|
m_use_texture_buffer_for_vram_writes = (GLAD_GL_VERSION_3_1 || GLAD_GL_ES_VERSION_3_2);
|
2020-05-25 10:26:13 +00:00
|
|
|
#endif
|
2021-03-18 02:44:45 +00:00
|
|
|
m_texture_stream_buffer_size = VRAM_UPDATE_TEXTURE_BUFFER_SIZE;
|
|
|
|
if (m_use_texture_buffer_for_vram_writes)
|
2019-11-05 15:33:09 +00:00
|
|
|
{
|
2021-03-18 02:44:45 +00:00
|
|
|
GLint max_texel_buffer_size;
|
|
|
|
glGetIntegerv(GL_MAX_TEXTURE_BUFFER_SIZE, reinterpret_cast<GLint*>(&max_texel_buffer_size));
|
|
|
|
Log_InfoPrintf("Max texel buffer size: %u", max_texel_buffer_size);
|
2021-06-03 13:03:06 +00:00
|
|
|
if (max_texel_buffer_size < static_cast<int>(VRAM_WIDTH * VRAM_HEIGHT))
|
2020-10-10 08:27:18 +00:00
|
|
|
{
|
|
|
|
Log_WarningPrintf("Maximum texture buffer size is less than VRAM size, not using texel buffers.");
|
2021-03-18 02:44:45 +00:00
|
|
|
m_use_texture_buffer_for_vram_writes = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_texture_stream_buffer_size =
|
|
|
|
std::min<u32>(VRAM_UPDATE_TEXTURE_BUFFER_SIZE, static_cast<u32>(max_texel_buffer_size) * sizeof(u16));
|
2020-10-10 08:27:18 +00:00
|
|
|
}
|
2019-11-05 15:33:09 +00:00
|
|
|
}
|
2020-07-07 15:57:51 +00:00
|
|
|
|
2021-03-18 02:44:45 +00:00
|
|
|
if (!m_use_texture_buffer_for_vram_writes)
|
2019-11-05 15:33:09 +00:00
|
|
|
{
|
2020-06-23 15:39:53 +00:00
|
|
|
// Try SSBOs.
|
2021-03-08 01:40:34 +00:00
|
|
|
GLint max_fragment_storage_blocks = 0;
|
2020-06-23 15:39:53 +00:00
|
|
|
GLint64 max_ssbo_size = 0;
|
|
|
|
if (GLAD_GL_VERSION_4_3 || GLAD_GL_ES_VERSION_3_1 || GLAD_GL_ARB_shader_storage_buffer_object)
|
2021-03-08 01:40:34 +00:00
|
|
|
{
|
|
|
|
glGetIntegerv(GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS, &max_fragment_storage_blocks);
|
2020-06-23 15:39:53 +00:00
|
|
|
glGetInteger64v(GL_MAX_SHADER_STORAGE_BLOCK_SIZE, &max_ssbo_size);
|
2021-03-08 01:40:34 +00:00
|
|
|
}
|
2020-06-23 15:39:53 +00:00
|
|
|
|
2021-03-08 01:40:34 +00:00
|
|
|
Log_InfoPrintf("Max fragment shader storage blocks: %d", max_fragment_storage_blocks);
|
2021-02-06 09:19:55 +00:00
|
|
|
Log_InfoPrintf("Max shader storage buffer size: %" PRId64, max_ssbo_size);
|
2021-03-08 01:40:34 +00:00
|
|
|
m_use_ssbo_for_vram_writes = (max_fragment_storage_blocks > 0 &&
|
|
|
|
max_ssbo_size >= static_cast<GLint64>(VRAM_WIDTH * VRAM_HEIGHT * sizeof(u16)));
|
2020-06-23 15:39:53 +00:00
|
|
|
if (m_use_ssbo_for_vram_writes)
|
2020-10-30 14:38:06 +00:00
|
|
|
{
|
2020-06-23 15:39:53 +00:00
|
|
|
Log_InfoPrintf("Using shader storage buffers for VRAM writes.");
|
2021-03-18 02:44:45 +00:00
|
|
|
m_texture_stream_buffer_size =
|
|
|
|
static_cast<u32>(std::min<u64>(VRAM_UPDATE_TEXTURE_BUFFER_SIZE, static_cast<u64>(max_ssbo_size)));
|
2020-10-30 14:38:06 +00:00
|
|
|
}
|
2020-06-23 15:39:53 +00:00
|
|
|
else
|
2020-10-30 14:38:06 +00:00
|
|
|
{
|
2021-04-09 08:06:42 +00:00
|
|
|
Log_WarningPrintf("Texture buffers and SSBOs are not supported, VRAM writes will be slower and multisampling "
|
|
|
|
"will be unavailable.");
|
|
|
|
m_max_multisamples = 1;
|
|
|
|
m_supports_per_sample_shading = false;
|
2020-10-30 14:38:06 +00:00
|
|
|
}
|
2019-11-05 15:33:09 +00:00
|
|
|
}
|
2019-11-24 13:23:33 +00:00
|
|
|
|
|
|
|
int max_dual_source_draw_buffers = 0;
|
|
|
|
glGetIntegerv(GL_MAX_DUAL_SOURCE_DRAW_BUFFERS, &max_dual_source_draw_buffers);
|
2020-10-14 08:49:49 +00:00
|
|
|
m_supports_dual_source_blend =
|
2020-11-10 10:36:17 +00:00
|
|
|
(max_dual_source_draw_buffers > 0) &&
|
|
|
|
(GLAD_GL_VERSION_3_3 || GLAD_GL_ARB_blend_func_extended || GLAD_GL_EXT_blend_func_extended);
|
2020-04-16 11:29:11 +00:00
|
|
|
|
2020-12-30 06:26:20 +00:00
|
|
|
// adaptive smoothing would require texture views, which aren't in GLES.
|
|
|
|
m_supports_adaptive_downsampling = false;
|
2019-10-04 11:08:53 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 14:01:29 +00:00
|
|
|
bool GPU_HW_OpenGL::CreateFramebuffer()
|
2019-09-12 02:53:04 +00:00
|
|
|
{
|
2019-10-03 06:46:13 +00:00
|
|
|
// scale vram size to internal resolution
|
|
|
|
const u32 texture_width = VRAM_WIDTH * m_resolution_scale;
|
|
|
|
const u32 texture_height = VRAM_HEIGHT * m_resolution_scale;
|
2020-10-30 14:38:06 +00:00
|
|
|
const u32 multisamples = m_multisamples;
|
2019-09-12 02:53:04 +00:00
|
|
|
|
2020-10-30 14:38:06 +00:00
|
|
|
if (!m_vram_texture.Create(texture_width, texture_height, multisamples, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, nullptr,
|
|
|
|
false, true) ||
|
|
|
|
!m_vram_depth_texture.Create(texture_width, texture_height, multisamples, GL_DEPTH_COMPONENT16,
|
|
|
|
GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, nullptr, false) ||
|
|
|
|
!m_vram_read_texture.Create(texture_width, texture_height, 1, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, nullptr, false,
|
2020-08-04 03:19:42 +00:00
|
|
|
true) ||
|
2020-05-03 07:11:28 +00:00
|
|
|
!m_vram_read_texture.CreateFramebuffer() ||
|
2020-10-30 14:38:06 +00:00
|
|
|
!m_vram_encoding_texture.Create(VRAM_WIDTH, VRAM_HEIGHT, 1, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, nullptr,
|
|
|
|
false) ||
|
2020-05-03 07:11:28 +00:00
|
|
|
!m_vram_encoding_texture.CreateFramebuffer() ||
|
2021-02-06 09:19:55 +00:00
|
|
|
!m_display_texture.Create(GPU_MAX_DISPLAY_WIDTH * m_resolution_scale, GPU_MAX_DISPLAY_HEIGHT * m_resolution_scale,
|
|
|
|
1, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, nullptr, false) ||
|
2020-05-03 07:11:28 +00:00
|
|
|
!m_display_texture.CreateFramebuffer())
|
2019-12-10 14:01:29 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-13 16:07:31 +00:00
|
|
|
|
2021-01-23 09:00:54 +00:00
|
|
|
if (m_vram_fbo_id == 0)
|
|
|
|
glGenFramebuffers(1, &m_vram_fbo_id);
|
|
|
|
|
2020-05-03 07:11:28 +00:00
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_vram_fbo_id);
|
2020-10-30 14:38:06 +00:00
|
|
|
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_vram_texture.GetGLTarget(),
|
|
|
|
m_vram_texture.GetGLId(), 0);
|
|
|
|
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_vram_depth_texture.GetGLTarget(),
|
|
|
|
m_vram_depth_texture.GetGLId(), 0);
|
2020-05-03 07:11:28 +00:00
|
|
|
Assert(glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
|
|
|
|
|
2020-12-30 06:26:20 +00:00
|
|
|
if (m_downsample_mode == GPUDownsampleMode::Box)
|
|
|
|
{
|
|
|
|
if (!m_downsample_texture.Create(VRAM_WIDTH, VRAM_HEIGHT, 1, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE) ||
|
|
|
|
!m_downsample_texture.CreateFramebuffer())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-23 09:00:54 +00:00
|
|
|
if (m_state_copy_fbo_id == 0)
|
|
|
|
glGenFramebuffers(1, &m_state_copy_fbo_id);
|
|
|
|
|
2019-11-05 12:34:27 +00:00
|
|
|
SetFullVRAMDirtyRectangle();
|
2019-12-10 14:01:29 +00:00
|
|
|
return true;
|
2019-09-12 02:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_HW_OpenGL::ClearFramebuffer()
|
|
|
|
{
|
2020-12-22 15:10:49 +00:00
|
|
|
const float depth_clear_value = m_pgxp_depth_buffer ? 1.0f : 0.0f;
|
|
|
|
|
2019-10-04 12:10:43 +00:00
|
|
|
glDisable(GL_SCISSOR_TEST);
|
2019-09-12 02:53:04 +00:00
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
2020-12-22 15:10:49 +00:00
|
|
|
IsGLES() ? glClearDepthf(depth_clear_value) : glClearDepth(depth_clear_value);
|
2020-05-03 07:11:28 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2019-10-04 12:10:43 +00:00
|
|
|
glEnable(GL_SCISSOR_TEST);
|
2020-12-22 15:10:49 +00:00
|
|
|
m_last_depth_z = 1.0f;
|
2019-11-05 12:34:27 +00:00
|
|
|
SetFullVRAMDirtyRectangle();
|
2019-09-12 02:53:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 14:01:29 +00:00
|
|
|
bool GPU_HW_OpenGL::CreateVertexBuffer()
|
2019-09-12 02:53:04 +00:00
|
|
|
{
|
2019-11-02 12:21:56 +00:00
|
|
|
m_vertex_stream_buffer = GL::StreamBuffer::Create(GL_ARRAY_BUFFER, VERTEX_BUFFER_SIZE);
|
|
|
|
if (!m_vertex_stream_buffer)
|
2019-12-10 14:01:29 +00:00
|
|
|
return false;
|
2019-11-02 12:21:56 +00:00
|
|
|
|
|
|
|
m_vertex_stream_buffer->Bind();
|
2019-09-12 14:18:13 +00:00
|
|
|
|
|
|
|
glGenVertexArrays(1, &m_vao_id);
|
|
|
|
glBindVertexArray(m_vao_id);
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glEnableVertexAttribArray(1);
|
|
|
|
glEnableVertexAttribArray(2);
|
2019-09-27 11:20:35 +00:00
|
|
|
glEnableVertexAttribArray(3);
|
2020-08-10 12:37:07 +00:00
|
|
|
glEnableVertexAttribArray(4);
|
2020-08-01 14:25:07 +00:00
|
|
|
glVertexAttribPointer(0, 4, GL_FLOAT, false, sizeof(BatchVertex), reinterpret_cast<void*>(offsetof(BatchVertex, x)));
|
2019-11-03 03:36:54 +00:00
|
|
|
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, true, sizeof(BatchVertex),
|
|
|
|
reinterpret_cast<void*>(offsetof(BatchVertex, color)));
|
2020-04-23 05:55:49 +00:00
|
|
|
glVertexAttribIPointer(2, 1, GL_UNSIGNED_INT, sizeof(BatchVertex), reinterpret_cast<void*>(offsetof(BatchVertex, u)));
|
|
|
|
glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, sizeof(BatchVertex),
|
|
|
|
reinterpret_cast<void*>(offsetof(BatchVertex, texpage)));
|
2020-08-10 12:37:07 +00:00
|
|
|
glVertexAttribPointer(4, 4, GL_UNSIGNED_BYTE, true, sizeof(BatchVertex),
|
|
|
|
reinterpret_cast<void*>(offsetof(BatchVertex, uv_limits)));
|
2019-09-12 14:18:13 +00:00
|
|
|
glBindVertexArray(0);
|
2019-09-13 16:07:31 +00:00
|
|
|
|
|
|
|
glGenVertexArrays(1, &m_attributeless_vao_id);
|
2019-12-10 14:01:29 +00:00
|
|
|
return true;
|
2019-09-12 02:53:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 14:01:29 +00:00
|
|
|
bool GPU_HW_OpenGL::CreateUniformBuffer()
|
2019-11-03 03:15:17 +00:00
|
|
|
{
|
|
|
|
m_uniform_stream_buffer = GL::StreamBuffer::Create(GL_UNIFORM_BUFFER, UNIFORM_BUFFER_SIZE);
|
|
|
|
if (!m_uniform_stream_buffer)
|
2019-12-10 14:01:29 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2019-11-03 03:15:17 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 14:01:29 +00:00
|
|
|
bool GPU_HW_OpenGL::CreateTextureBuffer()
|
2019-11-02 12:31:15 +00:00
|
|
|
{
|
2020-10-10 08:27:39 +00:00
|
|
|
const GLenum target =
|
|
|
|
(m_use_ssbo_for_vram_writes ? GL_SHADER_STORAGE_BUFFER :
|
2021-03-18 02:44:45 +00:00
|
|
|
(m_use_texture_buffer_for_vram_writes ? GL_TEXTURE_BUFFER : GL_PIXEL_UNPACK_BUFFER));
|
|
|
|
m_texture_stream_buffer = GL::StreamBuffer::Create(target, m_texture_stream_buffer_size);
|
2019-11-02 12:31:15 +00:00
|
|
|
if (!m_texture_stream_buffer)
|
2019-12-10 14:01:29 +00:00
|
|
|
return false;
|
2019-11-02 13:43:20 +00:00
|
|
|
|
2021-03-18 02:44:45 +00:00
|
|
|
if (m_use_texture_buffer_for_vram_writes)
|
2019-11-05 15:33:09 +00:00
|
|
|
{
|
|
|
|
glGenTextures(1, &m_texture_buffer_r16ui_texture);
|
|
|
|
glBindTexture(GL_TEXTURE_BUFFER, m_texture_buffer_r16ui_texture);
|
|
|
|
glTexBuffer(GL_TEXTURE_BUFFER, GL_R16UI, m_texture_stream_buffer->GetGLBufferId());
|
|
|
|
}
|
2019-11-02 13:43:20 +00:00
|
|
|
|
|
|
|
m_texture_stream_buffer->Unbind();
|
2019-12-10 14:01:29 +00:00
|
|
|
return true;
|
2019-11-02 12:31:15 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 14:18:13 +00:00
|
|
|
bool GPU_HW_OpenGL::CompilePrograms()
|
2019-09-12 02:53:04 +00:00
|
|
|
{
|
2020-10-09 07:59:55 +00:00
|
|
|
GL::ShaderCache shader_cache;
|
2021-01-11 04:50:25 +00:00
|
|
|
shader_cache.Open(IsGLES(), g_host_interface->GetShaderCacheBasePath(), SHADER_CACHE_VERSION);
|
2020-10-09 07:59:55 +00:00
|
|
|
|
2020-05-02 06:01:16 +00:00
|
|
|
const bool use_binding_layout = GPU_HW_ShaderGen::UseGLSLBindingLayout();
|
2020-10-30 14:38:06 +00:00
|
|
|
GPU_HW_ShaderGen shadergen(m_host_display->GetRenderAPI(), m_resolution_scale, m_multisamples, m_per_sample_shading,
|
|
|
|
m_true_color, m_scaled_dithering, m_texture_filtering, m_using_uv_limits,
|
2020-12-22 15:10:49 +00:00
|
|
|
m_pgxp_depth_buffer, m_supports_dual_source_blend);
|
2019-11-03 03:36:54 +00:00
|
|
|
|
2021-07-21 09:22:04 +00:00
|
|
|
ShaderCompileProgressTracker progress("Compiling Programs", (4 * 9 * 2 * 2) + (2 * 3) + (2 * 2) + 1 + 1 + 1 + 1 + 1);
|
2020-02-15 15:15:16 +00:00
|
|
|
|
2019-11-01 11:47:45 +00:00
|
|
|
for (u32 render_mode = 0; render_mode < 4; render_mode++)
|
2019-09-12 14:18:13 +00:00
|
|
|
{
|
2019-11-01 11:47:45 +00:00
|
|
|
for (u32 texture_mode = 0; texture_mode < 9; texture_mode++)
|
2019-09-26 13:33:20 +00:00
|
|
|
{
|
2019-11-01 12:47:32 +00:00
|
|
|
for (u8 dithering = 0; dithering < 2; dithering++)
|
2019-09-26 13:33:20 +00:00
|
|
|
{
|
2020-04-03 14:11:33 +00:00
|
|
|
for (u8 interlacing = 0; interlacing < 2; interlacing++)
|
|
|
|
{
|
2020-10-22 09:31:28 +00:00
|
|
|
const bool textured = (static_cast<GPUTextureMode>(texture_mode) != GPUTextureMode::Disabled);
|
2020-08-10 12:37:30 +00:00
|
|
|
const std::string batch_vs = shadergen.GenerateBatchVertexShader(textured);
|
2020-04-03 14:11:33 +00:00
|
|
|
const std::string fs = shadergen.GenerateBatchFragmentShader(
|
2020-10-22 09:31:28 +00:00
|
|
|
static_cast<BatchRenderMode>(render_mode), static_cast<GPUTextureMode>(texture_mode),
|
2020-04-03 14:11:33 +00:00
|
|
|
ConvertToBoolUnchecked(dithering), ConvertToBoolUnchecked(interlacing));
|
|
|
|
|
2020-05-02 06:01:16 +00:00
|
|
|
const auto link_callback = [this, textured, use_binding_layout](GL::Program& prog) {
|
|
|
|
if (!use_binding_layout)
|
2020-04-03 14:11:33 +00:00
|
|
|
{
|
2020-05-02 06:01:16 +00:00
|
|
|
prog.BindAttribute(0, "a_pos");
|
|
|
|
prog.BindAttribute(1, "a_col0");
|
|
|
|
if (textured)
|
2020-05-02 05:13:17 +00:00
|
|
|
{
|
2020-05-02 06:01:16 +00:00
|
|
|
prog.BindAttribute(2, "a_texcoord");
|
|
|
|
prog.BindAttribute(3, "a_texpage");
|
2020-08-10 12:37:07 +00:00
|
|
|
prog.BindAttribute(4, "a_uv_limits");
|
2020-05-02 05:13:17 +00:00
|
|
|
}
|
2020-05-02 06:01:16 +00:00
|
|
|
|
2020-05-03 09:38:27 +00:00
|
|
|
if (!IsGLES() || m_supports_dual_source_blend)
|
2020-05-02 05:13:17 +00:00
|
|
|
{
|
2020-05-02 06:01:16 +00:00
|
|
|
if (m_supports_dual_source_blend)
|
|
|
|
{
|
|
|
|
prog.BindFragDataIndexed(0, "o_col0");
|
|
|
|
prog.BindFragDataIndexed(1, "o_col1");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prog.BindFragData(0, "o_col0");
|
|
|
|
}
|
2020-05-02 05:13:17 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-16 11:29:11 +00:00
|
|
|
};
|
|
|
|
|
2020-10-09 07:59:55 +00:00
|
|
|
std::optional<GL::Program> prog = shader_cache.GetProgram(batch_vs, {}, fs, link_callback);
|
2020-04-03 14:11:33 +00:00
|
|
|
if (!prog)
|
|
|
|
return false;
|
|
|
|
|
2020-05-02 06:01:16 +00:00
|
|
|
if (!use_binding_layout)
|
2020-04-03 14:11:20 +00:00
|
|
|
{
|
2020-05-02 06:01:16 +00:00
|
|
|
prog->BindUniformBlock("UBOBlock", 1);
|
|
|
|
if (textured)
|
|
|
|
{
|
|
|
|
prog->Bind();
|
|
|
|
prog->Uniform1i("samp0", 0);
|
|
|
|
}
|
2020-04-03 14:11:20 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 14:11:33 +00:00
|
|
|
m_render_programs[render_mode][texture_mode][dithering][interlacing] = std::move(*prog);
|
2020-09-11 14:23:13 +00:00
|
|
|
|
2021-07-11 03:21:41 +00:00
|
|
|
progress.Increment();
|
2019-11-03 03:36:54 +00:00
|
|
|
}
|
2019-09-26 13:33:20 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-12 14:18:13 +00:00
|
|
|
}
|
2019-09-12 02:53:04 +00:00
|
|
|
|
2019-10-23 05:36:37 +00:00
|
|
|
for (u8 depth_24bit = 0; depth_24bit < 2; depth_24bit++)
|
2019-10-05 02:31:48 +00:00
|
|
|
{
|
2020-05-25 17:18:04 +00:00
|
|
|
for (u8 interlaced = 0; interlaced < 3; interlaced++)
|
2019-10-23 05:36:37 +00:00
|
|
|
{
|
2019-11-03 03:36:54 +00:00
|
|
|
const std::string vs = shadergen.GenerateScreenQuadVertexShader();
|
2020-11-03 04:17:51 +00:00
|
|
|
const std::string fs = shadergen.GenerateDisplayFragmentShader(
|
|
|
|
ConvertToBoolUnchecked(depth_24bit), static_cast<InterlacedRenderMode>(interlaced), m_chroma_smoothing);
|
2019-10-23 05:36:37 +00:00
|
|
|
|
2020-05-02 06:01:16 +00:00
|
|
|
std::optional<GL::Program> prog =
|
2020-10-09 07:59:55 +00:00
|
|
|
shader_cache.GetProgram(vs, {}, fs, [this, use_binding_layout](GL::Program& prog) {
|
2020-05-03 09:38:27 +00:00
|
|
|
if (!IsGLES() && !use_binding_layout)
|
2020-05-02 06:01:16 +00:00
|
|
|
prog.BindFragData(0, "o_col0");
|
|
|
|
});
|
2020-04-03 14:11:20 +00:00
|
|
|
if (!prog)
|
2019-10-23 05:36:37 +00:00
|
|
|
return false;
|
|
|
|
|
2020-05-02 06:01:16 +00:00
|
|
|
if (!use_binding_layout)
|
|
|
|
{
|
|
|
|
prog->BindUniformBlock("UBOBlock", 1);
|
|
|
|
prog->Bind();
|
|
|
|
prog->Uniform1i("samp0", 0);
|
|
|
|
}
|
2020-04-03 14:11:20 +00:00
|
|
|
m_display_programs[depth_24bit][interlaced] = std::move(*prog);
|
2021-07-11 03:21:41 +00:00
|
|
|
progress.Increment();
|
2020-04-03 14:11:20 +00:00
|
|
|
}
|
2019-11-14 06:58:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 09:22:04 +00:00
|
|
|
for (u8 wrapped = 0; wrapped < 2; wrapped++)
|
|
|
|
{
|
|
|
|
for (u8 interlaced = 0; interlaced < 2; interlaced++)
|
|
|
|
{
|
|
|
|
std::optional<GL::Program> prog = shader_cache.GetProgram(
|
|
|
|
shadergen.GenerateScreenQuadVertexShader(), {},
|
|
|
|
shadergen.GenerateVRAMFillFragmentShader(ConvertToBoolUnchecked(wrapped), ConvertToBoolUnchecked(interlaced)),
|
|
|
|
[this, use_binding_layout](GL::Program& prog) {
|
|
|
|
if (!IsGLES() && !use_binding_layout)
|
|
|
|
prog.BindFragData(0, "o_col0");
|
|
|
|
});
|
|
|
|
if (!prog)
|
|
|
|
return false;
|
2019-11-14 06:58:27 +00:00
|
|
|
|
2021-07-21 09:22:04 +00:00
|
|
|
if (!use_binding_layout)
|
|
|
|
prog->BindUniformBlock("UBOBlock", 1);
|
2020-05-02 06:01:16 +00:00
|
|
|
|
2021-07-21 09:22:04 +00:00
|
|
|
m_vram_fill_programs[wrapped][interlaced] = std::move(*prog);
|
|
|
|
progress.Increment();
|
|
|
|
}
|
|
|
|
}
|
2020-04-03 14:11:39 +00:00
|
|
|
|
2021-07-21 09:22:04 +00:00
|
|
|
std::optional<GL::Program> prog =
|
2020-10-09 07:59:55 +00:00
|
|
|
shader_cache.GetProgram(shadergen.GenerateScreenQuadVertexShader(), {}, shadergen.GenerateVRAMReadFragmentShader(),
|
|
|
|
[this, use_binding_layout](GL::Program& prog) {
|
|
|
|
if (!IsGLES() && !use_binding_layout)
|
|
|
|
prog.BindFragData(0, "o_col0");
|
|
|
|
});
|
2020-04-03 14:11:39 +00:00
|
|
|
if (!prog)
|
|
|
|
return false;
|
2019-11-14 06:58:27 +00:00
|
|
|
|
2020-05-02 06:01:16 +00:00
|
|
|
if (!use_binding_layout)
|
|
|
|
{
|
|
|
|
prog->BindUniformBlock("UBOBlock", 1);
|
|
|
|
prog->Bind();
|
|
|
|
prog->Uniform1i("samp0", 0);
|
|
|
|
}
|
2020-04-03 14:11:20 +00:00
|
|
|
m_vram_read_program = std::move(*prog);
|
2021-07-11 03:21:41 +00:00
|
|
|
progress.Increment();
|
2019-11-14 06:58:27 +00:00
|
|
|
|
2020-10-09 07:59:55 +00:00
|
|
|
prog =
|
|
|
|
shader_cache.GetProgram(shadergen.GenerateScreenQuadVertexShader(), {}, shadergen.GenerateVRAMCopyFragmentShader(),
|
|
|
|
[this, use_binding_layout](GL::Program& prog) {
|
|
|
|
if (!IsGLES() && !use_binding_layout)
|
|
|
|
prog.BindFragData(0, "o_col0");
|
|
|
|
});
|
2020-04-19 12:30:54 +00:00
|
|
|
if (!prog)
|
|
|
|
return false;
|
|
|
|
|
2020-05-02 06:01:16 +00:00
|
|
|
if (!use_binding_layout)
|
|
|
|
{
|
|
|
|
prog->BindUniformBlock("UBOBlock", 1);
|
|
|
|
prog->Bind();
|
|
|
|
prog->Uniform1i("samp0", 0);
|
|
|
|
}
|
2020-04-19 12:30:54 +00:00
|
|
|
m_vram_copy_program = std::move(*prog);
|
2021-07-11 03:21:41 +00:00
|
|
|
progress.Increment();
|
2020-04-19 12:30:54 +00:00
|
|
|
|
2020-10-09 07:59:55 +00:00
|
|
|
prog = shader_cache.GetProgram(shadergen.GenerateScreenQuadVertexShader(), {},
|
|
|
|
shadergen.GenerateVRAMUpdateDepthFragmentShader());
|
2020-05-03 07:11:28 +00:00
|
|
|
if (!prog)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
prog->Bind();
|
|
|
|
prog->Uniform1i("samp0", 0);
|
|
|
|
m_vram_update_depth_program = std::move(*prog);
|
2021-07-11 03:21:41 +00:00
|
|
|
progress.Increment();
|
2020-05-03 07:11:28 +00:00
|
|
|
|
2021-03-18 02:44:45 +00:00
|
|
|
if (m_use_texture_buffer_for_vram_writes || m_use_ssbo_for_vram_writes)
|
2019-11-03 03:36:54 +00:00
|
|
|
{
|
2020-10-09 07:59:55 +00:00
|
|
|
prog = shader_cache.GetProgram(shadergen.GenerateScreenQuadVertexShader(), {},
|
|
|
|
shadergen.GenerateVRAMWriteFragmentShader(m_use_ssbo_for_vram_writes),
|
|
|
|
[this, use_binding_layout](GL::Program& prog) {
|
|
|
|
if (!IsGLES() && !use_binding_layout)
|
|
|
|
prog.BindFragData(0, "o_col0");
|
|
|
|
});
|
2020-04-03 14:11:20 +00:00
|
|
|
if (!prog)
|
2019-11-05 15:33:09 +00:00
|
|
|
return false;
|
2019-11-03 05:18:51 +00:00
|
|
|
|
2020-05-02 06:01:16 +00:00
|
|
|
if (!use_binding_layout)
|
|
|
|
{
|
|
|
|
prog->BindUniformBlock("UBOBlock", 1);
|
|
|
|
prog->Bind();
|
|
|
|
prog->Uniform1i("samp0", 0);
|
|
|
|
}
|
2020-04-03 14:11:20 +00:00
|
|
|
m_vram_write_program = std::move(*prog);
|
2019-11-05 15:33:09 +00:00
|
|
|
}
|
2019-11-02 13:43:20 +00:00
|
|
|
|
2021-07-11 03:21:41 +00:00
|
|
|
progress.Increment();
|
2020-12-30 06:26:20 +00:00
|
|
|
|
|
|
|
if (m_downsample_mode == GPUDownsampleMode::Box)
|
|
|
|
{
|
|
|
|
prog = shader_cache.GetProgram(shadergen.GenerateScreenQuadVertexShader(), {},
|
|
|
|
shadergen.GenerateBoxSampleDownsampleFragmentShader(),
|
|
|
|
[this, use_binding_layout](GL::Program& prog) {
|
|
|
|
if (!IsGLES() && !use_binding_layout)
|
|
|
|
prog.BindFragData(0, "o_col0");
|
|
|
|
});
|
|
|
|
if (!prog)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!use_binding_layout)
|
|
|
|
{
|
|
|
|
prog->Bind();
|
|
|
|
prog->Uniform1i("samp0", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_downsample_program = std::move(*prog);
|
|
|
|
}
|
|
|
|
|
2021-07-11 03:21:41 +00:00
|
|
|
progress.Increment();
|
2020-09-11 14:23:13 +00:00
|
|
|
#undef UPDATE_PROGRESS
|
|
|
|
|
2019-09-12 14:18:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-09-12 02:53:04 +00:00
|
|
|
|
2020-04-18 05:30:46 +00:00
|
|
|
void GPU_HW_OpenGL::DrawBatchVertices(BatchRenderMode render_mode, u32 base_vertex, u32 num_vertices)
|
2019-09-13 16:07:31 +00:00
|
|
|
{
|
2020-08-02 07:17:05 +00:00
|
|
|
const GL::Program& prog = m_render_programs[static_cast<u8>(render_mode)][static_cast<u8>(m_batch.texture_mode)]
|
|
|
|
[BoolToUInt8(m_batch.dithering)][BoolToUInt8(m_batch.interlacing)];
|
2019-09-13 16:07:31 +00:00
|
|
|
prog.Bind();
|
2019-09-14 12:47:20 +00:00
|
|
|
|
2020-10-31 02:12:23 +00:00
|
|
|
if (m_current_transparency_mode != m_batch.transparency_mode || m_current_render_mode != render_mode)
|
|
|
|
{
|
|
|
|
m_current_transparency_mode = m_batch.transparency_mode;
|
|
|
|
m_current_render_mode = render_mode;
|
|
|
|
SetBlendMode();
|
|
|
|
}
|
2019-09-13 16:07:31 +00:00
|
|
|
|
2020-12-22 15:10:49 +00:00
|
|
|
SetDepthFunc();
|
2020-10-31 02:12:23 +00:00
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLES, m_batch_base_vertex, num_vertices);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_HW_OpenGL::SetBlendMode()
|
|
|
|
{
|
2020-11-10 10:36:17 +00:00
|
|
|
if (UseAlphaBlending(m_current_transparency_mode, m_current_render_mode))
|
2019-10-06 03:09:03 +00:00
|
|
|
{
|
|
|
|
glEnable(GL_BLEND);
|
2020-10-22 09:31:28 +00:00
|
|
|
glBlendEquationSeparate(m_current_transparency_mode == GPUTransparencyMode::BackgroundMinusForeground ?
|
2020-10-31 02:12:23 +00:00
|
|
|
GL_FUNC_REVERSE_SUBTRACT :
|
|
|
|
GL_FUNC_ADD,
|
|
|
|
GL_FUNC_ADD);
|
2021-02-06 10:05:57 +00:00
|
|
|
if (m_supports_dual_source_blend)
|
|
|
|
{
|
|
|
|
glBlendFuncSeparate(GL_ONE, m_supports_dual_source_blend ? GL_SRC1_ALPHA : GL_SRC_ALPHA, GL_ONE, GL_ZERO);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const float factor =
|
|
|
|
(m_current_transparency_mode == GPUTransparencyMode::HalfBackgroundPlusHalfForeground) ? 0.5f : 1.0f;
|
|
|
|
glBlendFuncSeparate(GL_ONE, GL_CONSTANT_ALPHA, GL_ONE, GL_ZERO);
|
|
|
|
glBlendColor(0.0f, 0.0f, 0.0f, factor);
|
|
|
|
}
|
2019-10-04 12:10:43 +00:00
|
|
|
}
|
2020-11-10 10:36:17 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
}
|
2020-10-31 02:12:23 +00:00
|
|
|
}
|
2019-10-05 04:36:48 +00:00
|
|
|
|
2020-12-25 08:02:38 +00:00
|
|
|
bool GPU_HW_OpenGL::BlitVRAMReplacementTexture(const TextureReplacementTexture* tex, u32 dst_x, u32 dst_y, u32 width,
|
|
|
|
u32 height)
|
|
|
|
{
|
|
|
|
if (!m_vram_write_replacement_texture.IsValid())
|
|
|
|
{
|
|
|
|
if (!m_vram_write_replacement_texture.Create(tex->GetWidth(), tex->GetHeight(), 1, GL_RGBA, GL_RGBA,
|
2021-07-13 04:16:38 +00:00
|
|
|
GL_UNSIGNED_BYTE, tex->GetPixels(), true) ||
|
2020-12-25 08:02:38 +00:00
|
|
|
!m_vram_write_replacement_texture.CreateFramebuffer())
|
|
|
|
{
|
|
|
|
m_vram_write_replacement_texture.Destroy();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_vram_write_replacement_texture.Replace(tex->GetWidth(), tex->GetHeight(), GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE,
|
|
|
|
tex->GetPixels());
|
|
|
|
}
|
|
|
|
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
m_vram_write_replacement_texture.BindFramebuffer(GL_READ_FRAMEBUFFER);
|
|
|
|
|
|
|
|
dst_y = m_vram_texture.GetHeight() - dst_y - height;
|
|
|
|
glBlitFramebuffer(0, tex->GetHeight(), tex->GetWidth(), 0, dst_x, dst_y, dst_x + width, dst_y + height,
|
|
|
|
GL_COLOR_BUFFER_BIT, GL_LINEAR);
|
|
|
|
|
|
|
|
m_vram_read_texture.Bind();
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-31 02:12:23 +00:00
|
|
|
void GPU_HW_OpenGL::SetDepthFunc()
|
|
|
|
{
|
2020-12-22 15:10:49 +00:00
|
|
|
SetDepthFunc(m_batch.use_depth_buffer ? GL_LEQUAL : (m_batch.check_mask_before_draw ? GL_GEQUAL : GL_ALWAYS));
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_HW_OpenGL::SetDepthFunc(GLenum func)
|
|
|
|
{
|
|
|
|
if (m_current_depth_test == func)
|
|
|
|
return;
|
|
|
|
|
|
|
|
glDepthFunc(func);
|
|
|
|
m_current_depth_test = func;
|
2019-11-05 09:19:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_HW_OpenGL::SetScissorFromDrawingArea()
|
|
|
|
{
|
|
|
|
int left, top, right, bottom;
|
|
|
|
CalcScissorRect(&left, &top, &right, &bottom);
|
|
|
|
|
|
|
|
const int width = right - left;
|
|
|
|
const int height = bottom - top;
|
|
|
|
const int x = left;
|
2019-12-10 14:01:29 +00:00
|
|
|
const int y = m_vram_texture.GetHeight() - bottom;
|
2019-11-05 09:19:49 +00:00
|
|
|
|
|
|
|
Log_DebugPrintf("SetScissor: (%d-%d, %d-%d)", x, x + width, y, y + height);
|
|
|
|
glScissor(x, y, width, height);
|
2019-11-03 03:15:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 05:30:46 +00:00
|
|
|
void GPU_HW_OpenGL::UploadUniformBuffer(const void* data, u32 data_size)
|
2019-11-03 03:15:17 +00:00
|
|
|
{
|
|
|
|
const GL::StreamBuffer::MappingResult res = m_uniform_stream_buffer->Map(m_uniform_buffer_alignment, data_size);
|
|
|
|
std::memcpy(res.pointer, data, data_size);
|
|
|
|
m_uniform_stream_buffer->Unmap(data_size);
|
|
|
|
|
|
|
|
glBindBufferRange(GL_UNIFORM_BUFFER, 1, m_uniform_stream_buffer->GetGLBufferId(), res.buffer_offset, data_size);
|
|
|
|
|
2019-11-05 09:44:17 +00:00
|
|
|
m_renderer_stats.num_uniform_buffer_updates++;
|
2019-09-12 02:53:04 +00:00
|
|
|
}
|
|
|
|
|
2020-08-02 17:26:11 +00:00
|
|
|
void GPU_HW_OpenGL::ClearDisplay()
|
|
|
|
{
|
|
|
|
GPU_HW::ClearDisplay();
|
|
|
|
|
2021-07-11 04:09:11 +00:00
|
|
|
m_host_display->ClearDisplayTexture();
|
|
|
|
|
2020-08-02 17:26:11 +00:00
|
|
|
m_display_texture.BindFramebuffer(GL_DRAW_FRAMEBUFFER);
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
2020-08-18 14:41:25 +00:00
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_vram_fbo_id);
|
2020-08-02 17:26:11 +00:00
|
|
|
}
|
|
|
|
|
2019-09-13 16:07:31 +00:00
|
|
|
void GPU_HW_OpenGL::UpdateDisplay()
|
|
|
|
{
|
|
|
|
GPU_HW::UpdateDisplay();
|
2019-09-27 12:02:13 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (g_settings.debugging.show_vram)
|
2019-09-27 12:02:13 +00:00
|
|
|
{
|
2020-10-30 14:38:06 +00:00
|
|
|
if (IsUsingMultisampling())
|
|
|
|
{
|
|
|
|
UpdateVRAMReadTexture();
|
|
|
|
|
2020-10-21 15:25:33 +00:00
|
|
|
m_host_display->SetDisplayTexture(reinterpret_cast<void*>(static_cast<uintptr_t>(m_vram_read_texture.GetGLId())),
|
|
|
|
HostDisplayPixelFormat::RGBA8, m_vram_read_texture.GetWidth(),
|
|
|
|
static_cast<s32>(m_vram_read_texture.GetHeight()), 0,
|
|
|
|
m_vram_read_texture.GetHeight(), m_vram_read_texture.GetWidth(),
|
|
|
|
-static_cast<s32>(m_vram_read_texture.GetHeight()));
|
2020-10-30 14:38:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_host_display->SetDisplayTexture(reinterpret_cast<void*>(static_cast<uintptr_t>(m_vram_texture.GetGLId())),
|
2020-10-21 15:25:33 +00:00
|
|
|
HostDisplayPixelFormat::RGBA8, m_vram_texture.GetWidth(),
|
|
|
|
static_cast<s32>(m_vram_texture.GetHeight()), 0, m_vram_texture.GetHeight(),
|
|
|
|
m_vram_texture.GetWidth(), -static_cast<s32>(m_vram_texture.GetHeight()));
|
2020-10-30 14:38:06 +00:00
|
|
|
}
|
2020-03-28 15:14:37 +00:00
|
|
|
m_host_display->SetDisplayParameters(VRAM_WIDTH, VRAM_HEIGHT, 0, 0, VRAM_WIDTH, VRAM_HEIGHT,
|
2020-03-28 15:14:33 +00:00
|
|
|
static_cast<float>(VRAM_WIDTH) / static_cast<float>(VRAM_HEIGHT));
|
2019-09-27 12:02:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-24 01:57:14 +00:00
|
|
|
m_host_display->SetDisplayParameters(m_crtc_state.display_width, m_crtc_state.display_height,
|
|
|
|
m_crtc_state.display_origin_left, m_crtc_state.display_origin_top,
|
|
|
|
m_crtc_state.display_vram_width, m_crtc_state.display_vram_height,
|
|
|
|
GetDisplayAspectRatio());
|
|
|
|
|
2020-11-03 04:17:51 +00:00
|
|
|
const u32 resolution_scale = m_GPUSTAT.display_area_color_depth_24 ? 1 : m_resolution_scale;
|
2020-03-28 15:14:37 +00:00
|
|
|
const u32 vram_offset_x = m_crtc_state.display_vram_left;
|
2020-05-26 14:27:25 +00:00
|
|
|
const u32 vram_offset_y = m_crtc_state.display_vram_top;
|
2020-11-03 04:17:51 +00:00
|
|
|
const u32 scaled_vram_offset_x = vram_offset_x * resolution_scale;
|
|
|
|
const u32 scaled_vram_offset_y = vram_offset_y * resolution_scale;
|
2020-03-28 15:14:37 +00:00
|
|
|
const u32 display_width = m_crtc_state.display_vram_width;
|
|
|
|
const u32 display_height = m_crtc_state.display_vram_height;
|
2020-11-03 04:17:51 +00:00
|
|
|
const u32 scaled_display_width = display_width * resolution_scale;
|
|
|
|
const u32 scaled_display_height = display_height * resolution_scale;
|
2020-05-25 17:18:04 +00:00
|
|
|
const InterlacedRenderMode interlaced = GetInterlacedRenderMode();
|
2019-10-05 02:31:48 +00:00
|
|
|
|
2020-04-27 11:49:34 +00:00
|
|
|
if (IsDisplayDisabled())
|
2019-10-23 05:36:37 +00:00
|
|
|
{
|
2020-02-28 07:01:01 +00:00
|
|
|
m_host_display->ClearDisplayTexture();
|
2019-10-28 07:43:34 +00:00
|
|
|
}
|
2020-05-25 17:18:04 +00:00
|
|
|
else if (!m_GPUSTAT.display_area_color_depth_24 && interlaced == GPU_HW::InterlacedRenderMode::None &&
|
2020-10-30 14:38:06 +00:00
|
|
|
!IsUsingMultisampling() && (scaled_vram_offset_x + scaled_display_width) <= m_vram_texture.GetWidth() &&
|
2020-05-11 15:35:02 +00:00
|
|
|
(scaled_vram_offset_y + scaled_display_height) <= m_vram_texture.GetHeight())
|
2019-10-28 07:43:34 +00:00
|
|
|
{
|
2020-12-30 06:26:20 +00:00
|
|
|
if (IsUsingDownsampling())
|
|
|
|
{
|
|
|
|
DownsampleFramebuffer(m_vram_texture, scaled_vram_offset_x, scaled_vram_offset_y, scaled_display_width,
|
|
|
|
scaled_display_height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_host_display->SetDisplayTexture(reinterpret_cast<void*>(static_cast<uintptr_t>(m_vram_texture.GetGLId())),
|
|
|
|
HostDisplayPixelFormat::RGBA8, m_vram_texture.GetWidth(),
|
|
|
|
m_vram_texture.GetHeight(), scaled_vram_offset_x,
|
|
|
|
m_vram_texture.GetHeight() - scaled_vram_offset_y, scaled_display_width,
|
|
|
|
-static_cast<s32>(scaled_display_height));
|
|
|
|
}
|
2019-10-23 05:36:37 +00:00
|
|
|
}
|
|
|
|
else
|
2019-10-05 02:31:48 +00:00
|
|
|
{
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
2020-05-03 07:11:28 +00:00
|
|
|
glDisable(GL_DEPTH_TEST);
|
2019-10-20 12:59:40 +00:00
|
|
|
|
2020-05-25 17:18:04 +00:00
|
|
|
m_display_programs[BoolToUInt8(m_GPUSTAT.display_area_color_depth_24)][static_cast<u8>(interlaced)].Bind();
|
2020-04-01 14:17:25 +00:00
|
|
|
m_display_texture.BindFramebuffer(GL_DRAW_FRAMEBUFFER);
|
|
|
|
m_vram_texture.Bind();
|
2019-11-03 05:18:51 +00:00
|
|
|
|
2021-06-11 01:50:03 +00:00
|
|
|
if (interlaced == InterlacedRenderMode::None && (GLAD_GL_VERSION_4_3 || GLAD_GL_ES_VERSION_3_0))
|
|
|
|
{
|
|
|
|
static constexpr std::array<GLenum, 1> attachments = {GL_COLOR_ATTACHMENT0};
|
|
|
|
glInvalidateFramebuffer(GL_DRAW_FRAMEBUFFER, static_cast<GLsizei>(attachments.size()), attachments.data());
|
|
|
|
}
|
|
|
|
|
2020-05-26 11:30:57 +00:00
|
|
|
const u8 height_div2 = BoolToUInt8(interlaced == GPU_HW::InterlacedRenderMode::SeparateFields);
|
2020-05-31 17:26:33 +00:00
|
|
|
const u32 reinterpret_field_offset = (interlaced != InterlacedRenderMode::None) ? GetInterlacedDisplayField() : 0;
|
2020-05-26 14:27:25 +00:00
|
|
|
const u32 scaled_flipped_vram_offset_y = m_vram_texture.GetHeight() - scaled_vram_offset_y -
|
|
|
|
reinterpret_field_offset - (scaled_display_height >> height_div2);
|
2020-11-03 04:17:51 +00:00
|
|
|
const u32 reinterpret_start_x = m_crtc_state.regs.X * resolution_scale;
|
|
|
|
const u32 reinterpret_crop_left = (m_crtc_state.display_vram_left - m_crtc_state.regs.X) * resolution_scale;
|
2020-05-11 15:35:02 +00:00
|
|
|
const u32 uniforms[4] = {reinterpret_start_x, scaled_flipped_vram_offset_y, reinterpret_crop_left,
|
|
|
|
reinterpret_field_offset};
|
2020-04-18 05:30:46 +00:00
|
|
|
UploadUniformBuffer(uniforms, sizeof(uniforms));
|
2020-04-01 14:17:25 +00:00
|
|
|
m_batch_ubo_dirty = true;
|
|
|
|
|
2021-01-31 14:51:15 +00:00
|
|
|
Assert(scaled_display_width <= m_display_texture.GetWidth() &&
|
|
|
|
scaled_display_height <= m_display_texture.GetHeight());
|
|
|
|
|
2020-05-26 03:44:05 +00:00
|
|
|
glViewport(0, 0, scaled_display_width, scaled_display_height);
|
2020-05-03 07:11:28 +00:00
|
|
|
glBindVertexArray(m_attributeless_vao_id);
|
2020-04-01 14:17:25 +00:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
|
|
|
|
2020-12-30 06:26:20 +00:00
|
|
|
if (IsUsingDownsampling())
|
|
|
|
{
|
|
|
|
DownsampleFramebuffer(m_display_texture, 0, 0, scaled_display_width, scaled_display_height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_host_display->SetDisplayTexture(reinterpret_cast<void*>(static_cast<uintptr_t>(m_display_texture.GetGLId())),
|
|
|
|
HostDisplayPixelFormat::RGBA8, m_display_texture.GetWidth(),
|
|
|
|
m_display_texture.GetHeight(), 0, scaled_display_height, scaled_display_width,
|
|
|
|
-static_cast<s32>(scaled_display_height));
|
|
|
|
}
|
2019-10-05 02:31:48 +00:00
|
|
|
|
|
|
|
// restore state
|
2020-05-03 07:11:28 +00:00
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_vram_fbo_id);
|
|
|
|
glBindVertexArray(m_vao_id);
|
2019-12-10 14:01:29 +00:00
|
|
|
glViewport(0, 0, m_vram_texture.GetWidth(), m_vram_texture.GetHeight());
|
2020-05-03 07:11:28 +00:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
2019-10-05 02:31:48 +00:00
|
|
|
glEnable(GL_SCISSOR_TEST);
|
2020-10-31 02:12:23 +00:00
|
|
|
m_vram_read_texture.Bind();
|
|
|
|
SetBlendMode();
|
|
|
|
SetDepthFunc();
|
2019-10-05 02:31:48 +00:00
|
|
|
}
|
2019-09-27 12:02:13 +00:00
|
|
|
}
|
2019-09-13 16:07:31 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 07:16:59 +00:00
|
|
|
void GPU_HW_OpenGL::ReadVRAM(u32 x, u32 y, u32 width, u32 height)
|
2019-09-14 10:45:26 +00:00
|
|
|
{
|
2021-05-19 03:43:49 +00:00
|
|
|
if (IsUsingSoftwareRendererForReadbacks())
|
|
|
|
{
|
2021-05-20 04:01:47 +00:00
|
|
|
ReadSoftwareRendererVRAM(x, y, width, height);
|
2021-05-19 03:43:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-14 06:58:27 +00:00
|
|
|
// Get bounds with wrap-around handled.
|
|
|
|
const Common::Rectangle<u32> copy_rect = GetVRAMTransferBounds(x, y, width, height);
|
2019-12-28 05:50:35 +00:00
|
|
|
const u32 encoded_width = (copy_rect.GetWidth() + 1) / 2;
|
2019-11-14 06:58:27 +00:00
|
|
|
const u32 encoded_height = copy_rect.GetHeight();
|
|
|
|
|
|
|
|
// Encode the 24-bit texture as 16-bit.
|
|
|
|
const u32 uniforms[4] = {copy_rect.left, VRAM_HEIGHT - copy_rect.top - copy_rect.GetHeight(), copy_rect.GetWidth(),
|
|
|
|
copy_rect.GetHeight()};
|
2019-12-10 14:01:29 +00:00
|
|
|
m_vram_encoding_texture.BindFramebuffer(GL_DRAW_FRAMEBUFFER);
|
|
|
|
m_vram_texture.Bind();
|
2019-11-14 06:58:27 +00:00
|
|
|
m_vram_read_program.Bind();
|
2020-04-18 05:30:46 +00:00
|
|
|
UploadUniformBuffer(uniforms, sizeof(uniforms));
|
2019-11-14 06:58:27 +00:00
|
|
|
glDisable(GL_BLEND);
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
glViewport(0, 0, encoded_width, encoded_height);
|
2020-05-03 07:11:28 +00:00
|
|
|
glBindVertexArray(m_attributeless_vao_id);
|
2019-11-14 06:58:27 +00:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
|
|
|
|
|
|
|
// Readback encoded texture.
|
2019-12-10 14:01:29 +00:00
|
|
|
m_vram_encoding_texture.BindFramebuffer(GL_READ_FRAMEBUFFER);
|
2019-11-14 06:58:27 +00:00
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT, 2);
|
|
|
|
glPixelStorei(GL_PACK_ROW_LENGTH, VRAM_WIDTH / 2);
|
|
|
|
glReadPixels(0, 0, encoded_width, encoded_height, GL_RGBA, GL_UNSIGNED_BYTE,
|
|
|
|
&m_vram_shadow[copy_rect.top * VRAM_WIDTH + copy_rect.left]);
|
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT, 4);
|
|
|
|
glPixelStorei(GL_PACK_ROW_LENGTH, 0);
|
|
|
|
RestoreGraphicsAPIState();
|
2019-09-14 10:45:26 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 15:05:37 +00:00
|
|
|
void GPU_HW_OpenGL::FillVRAM(u32 x, u32 y, u32 width, u32 height, u32 color)
|
2019-09-14 06:43:39 +00:00
|
|
|
{
|
2021-05-20 04:01:47 +00:00
|
|
|
if (IsUsingSoftwareRendererForReadbacks())
|
|
|
|
FillSoftwareRendererVRAM(x, y, width, height, color);
|
|
|
|
|
2019-11-05 12:12:37 +00:00
|
|
|
GPU_HW::FillVRAM(x, y, width, height, color);
|
|
|
|
|
2021-07-21 09:22:04 +00:00
|
|
|
const Common::Rectangle<u32> bounds(GetVRAMTransferBounds(x, y, width, height));
|
|
|
|
glScissor(bounds.left * m_resolution_scale,
|
|
|
|
m_vram_texture.GetHeight() - (bounds.top * m_resolution_scale) - (height * m_resolution_scale),
|
|
|
|
width * m_resolution_scale, height * m_resolution_scale);
|
2019-09-14 06:43:39 +00:00
|
|
|
|
2020-04-03 14:11:39 +00:00
|
|
|
// fast path when not using interlaced rendering
|
2021-07-21 09:22:04 +00:00
|
|
|
const bool wrapped = IsVRAMFillOversized(x, y, width, height);
|
|
|
|
const bool interlaced = IsInterlacedRenderingEnabled();
|
|
|
|
if (!wrapped && !interlaced)
|
2020-04-03 14:11:39 +00:00
|
|
|
{
|
2021-03-18 02:55:02 +00:00
|
|
|
const auto [r, g, b, a] =
|
|
|
|
RGBA8ToFloat(m_true_color ? color : VRAMRGBA5551ToRGBA8888(VRAMRGBA8888ToRGBA5551(color)));
|
2020-04-03 14:11:39 +00:00
|
|
|
glClearColor(r, g, b, a);
|
2020-05-03 09:38:27 +00:00
|
|
|
IsGLES() ? glClearDepthf(a) : glClearDepth(a);
|
2020-05-03 07:11:28 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2020-04-03 14:11:39 +00:00
|
|
|
SetScissorFromDrawingArea();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-11 10:18:07 +00:00
|
|
|
const VRAMFillUBOData uniforms = GetVRAMFillUBOData(x, y, width, height, color);
|
2020-04-03 14:11:39 +00:00
|
|
|
|
2021-07-21 09:22:04 +00:00
|
|
|
m_vram_fill_programs[BoolToUInt8(wrapped)][BoolToUInt8(interlaced)].Bind();
|
2020-04-18 05:30:46 +00:00
|
|
|
UploadUniformBuffer(&uniforms, sizeof(uniforms));
|
2020-05-03 07:11:28 +00:00
|
|
|
glDisable(GL_BLEND);
|
2020-12-22 15:10:49 +00:00
|
|
|
SetDepthFunc(GL_ALWAYS);
|
2020-05-03 07:11:28 +00:00
|
|
|
glBindVertexArray(m_attributeless_vao_id);
|
2020-04-03 14:11:39 +00:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
|
|
|
|
|
|
|
RestoreGraphicsAPIState();
|
|
|
|
}
|
2019-09-14 06:43:39 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 16:19:28 +00:00
|
|
|
void GPU_HW_OpenGL::UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data, bool set_mask, bool check_mask)
|
2019-09-12 15:10:08 +00:00
|
|
|
{
|
2021-05-20 04:01:47 +00:00
|
|
|
if (IsUsingSoftwareRendererForReadbacks())
|
|
|
|
UpdateSoftwareRendererVRAM(x, y, width, height, data, set_mask, check_mask);
|
|
|
|
|
2020-12-25 08:02:38 +00:00
|
|
|
const Common::Rectangle<u32> bounds = GetVRAMTransferBounds(x, y, width, height);
|
|
|
|
GPU_HW::UpdateVRAM(bounds.left, bounds.top, bounds.GetWidth(), bounds.GetHeight(), data, set_mask, check_mask);
|
|
|
|
|
|
|
|
if (!check_mask)
|
|
|
|
{
|
|
|
|
const TextureReplacementTexture* rtex = g_texture_replacements.GetVRAMWriteReplacement(width, height, data);
|
|
|
|
if (rtex && BlitVRAMReplacementTexture(rtex, x * m_resolution_scale, y * m_resolution_scale,
|
|
|
|
width * m_resolution_scale, height * m_resolution_scale))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-02 12:31:15 +00:00
|
|
|
const u32 num_pixels = width * height;
|
2021-03-18 02:44:45 +00:00
|
|
|
if (m_use_texture_buffer_for_vram_writes || m_use_ssbo_for_vram_writes)
|
2019-09-12 15:10:08 +00:00
|
|
|
{
|
2019-11-05 13:15:54 +00:00
|
|
|
const auto map_result = m_texture_stream_buffer->Map(sizeof(u16), num_pixels * sizeof(u16));
|
|
|
|
std::memcpy(map_result.pointer, data, num_pixels * sizeof(u16));
|
|
|
|
m_texture_stream_buffer->Unmap(num_pixels * sizeof(u16));
|
|
|
|
m_texture_stream_buffer->Unbind();
|
2019-09-13 16:07:31 +00:00
|
|
|
|
2019-11-13 12:48:37 +00:00
|
|
|
glDisable(GL_BLEND);
|
2020-12-22 15:10:49 +00:00
|
|
|
SetDepthFunc((check_mask && !m_pgxp_depth_buffer) ? GL_GEQUAL : GL_ALWAYS);
|
2019-09-13 16:07:31 +00:00
|
|
|
|
2019-11-05 13:15:54 +00:00
|
|
|
m_vram_write_program.Bind();
|
2020-06-23 15:39:53 +00:00
|
|
|
if (m_use_ssbo_for_vram_writes)
|
|
|
|
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_texture_stream_buffer->GetGLBufferId());
|
|
|
|
else
|
|
|
|
glBindTexture(GL_TEXTURE_BUFFER, m_texture_buffer_r16ui_texture);
|
2019-09-12 15:10:08 +00:00
|
|
|
|
2020-12-14 16:19:28 +00:00
|
|
|
const VRAMWriteUBOData uniforms =
|
|
|
|
GetVRAMWriteUBOData(x, y, width, height, map_result.index_aligned, set_mask, check_mask);
|
2020-05-03 07:11:28 +00:00
|
|
|
UploadUniformBuffer(&uniforms, sizeof(uniforms));
|
2019-09-12 15:10:08 +00:00
|
|
|
|
2020-07-11 10:18:07 +00:00
|
|
|
// the viewport should already be set to the full vram, so just adjust the scissor
|
|
|
|
const Common::Rectangle<u32> scaled_bounds = bounds * m_resolution_scale;
|
|
|
|
glScissor(scaled_bounds.left, m_vram_texture.GetHeight() - scaled_bounds.top - scaled_bounds.GetHeight(),
|
|
|
|
scaled_bounds.GetWidth(), scaled_bounds.GetHeight());
|
|
|
|
|
2020-05-03 07:11:28 +00:00
|
|
|
glBindVertexArray(m_attributeless_vao_id);
|
2019-11-05 13:15:54 +00:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
2019-11-02 12:31:15 +00:00
|
|
|
|
2019-11-05 13:15:54 +00:00
|
|
|
RestoreGraphicsAPIState();
|
|
|
|
}
|
2019-10-03 06:46:13 +00:00
|
|
|
else
|
2019-11-05 13:15:54 +00:00
|
|
|
{
|
2021-03-18 02:44:45 +00:00
|
|
|
if ((x + width) > VRAM_WIDTH || (y + height) > VRAM_HEIGHT || check_mask)
|
2020-07-11 10:18:07 +00:00
|
|
|
{
|
|
|
|
// CPU round trip if oversized for now.
|
2021-03-18 02:44:45 +00:00
|
|
|
Log_WarningPrintf("Oversized/masked VRAM update (%u-%u, %u-%u), CPU round trip", x, x + width, y, y + height);
|
2020-07-11 10:18:07 +00:00
|
|
|
ReadVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT);
|
2020-12-14 16:19:28 +00:00
|
|
|
GPU::UpdateVRAM(x, y, width, height, data, set_mask, check_mask);
|
|
|
|
UpdateVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT, m_vram_shadow.data(), false, false);
|
2020-07-11 10:18:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-14 16:19:28 +00:00
|
|
|
GPU_HW::UpdateVRAM(x, y, width, height, data, set_mask, check_mask);
|
2020-07-11 10:18:07 +00:00
|
|
|
|
2019-11-05 13:15:54 +00:00
|
|
|
const auto map_result = m_texture_stream_buffer->Map(sizeof(u32), num_pixels * sizeof(u32));
|
2019-09-13 16:07:31 +00:00
|
|
|
|
2019-11-05 13:15:54 +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));
|
2021-03-18 02:44:45 +00:00
|
|
|
const u16 mask_or = set_mask ? 0x8000 : 0x0000;
|
2019-11-05 13:15:54 +00:00
|
|
|
u32* dest_ptr = static_cast<u32*>(map_result.pointer);
|
|
|
|
for (u32 row = 0; row < height; row++)
|
|
|
|
{
|
|
|
|
const u8* source_row_ptr = source_ptr;
|
2019-09-26 13:33:20 +00:00
|
|
|
|
2019-11-05 13:15:54 +00:00
|
|
|
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);
|
2021-03-18 02:55:02 +00:00
|
|
|
*(dest_ptr++) = VRAMRGBA5551ToRGBA8888(src_col | mask_or);
|
2019-11-05 13:15:54 +00:00
|
|
|
}
|
2019-11-02 13:43:20 +00:00
|
|
|
|
2019-11-05 13:15:54 +00:00
|
|
|
source_ptr -= source_stride;
|
|
|
|
}
|
2019-11-02 13:43:20 +00:00
|
|
|
|
2019-11-05 13:15:54 +00:00
|
|
|
m_texture_stream_buffer->Unmap(num_pixels * sizeof(u32));
|
|
|
|
m_texture_stream_buffer->Bind();
|
2019-11-03 05:18:51 +00:00
|
|
|
|
2021-04-09 08:06:42 +00:00
|
|
|
// have to write to the 1x texture first
|
|
|
|
if (m_resolution_scale > 1)
|
|
|
|
m_vram_encoding_texture.Bind();
|
|
|
|
else
|
|
|
|
m_vram_texture.Bind();
|
|
|
|
|
2019-11-05 13:15:54 +00:00
|
|
|
// lower-left origin flip happens here
|
|
|
|
const u32 flipped_y = VRAM_HEIGHT - y - height;
|
2019-11-01 12:06:19 +00:00
|
|
|
|
2019-11-05 13:15:54 +00:00
|
|
|
// update texture data
|
2021-04-09 08:06:42 +00:00
|
|
|
glTexSubImage2D(m_vram_texture.GetGLTarget(), 0, x, flipped_y, width, height, GL_RGBA, GL_UNSIGNED_BYTE,
|
2020-07-09 03:29:58 +00:00
|
|
|
reinterpret_cast<void*>(static_cast<uintptr_t>(map_result.buffer_offset)));
|
2019-11-05 13:15:54 +00:00
|
|
|
m_texture_stream_buffer->Unbind();
|
|
|
|
|
2021-04-09 08:06:42 +00:00
|
|
|
if (m_resolution_scale > 1)
|
2019-11-05 13:15:54 +00:00
|
|
|
{
|
|
|
|
// scale to internal resolution
|
|
|
|
const u32 scaled_width = width * m_resolution_scale;
|
|
|
|
const u32 scaled_height = height * m_resolution_scale;
|
|
|
|
const u32 scaled_x = x * m_resolution_scale;
|
|
|
|
const u32 scaled_y = y * m_resolution_scale;
|
2019-12-10 14:01:29 +00:00
|
|
|
const u32 scaled_flipped_y = m_vram_texture.GetHeight() - scaled_y - scaled_height;
|
2021-04-09 08:06:42 +00:00
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
m_vram_encoding_texture.BindFramebuffer(GL_READ_FRAMEBUFFER);
|
|
|
|
glBlitFramebuffer(x, flipped_y, x + width, flipped_y + height, scaled_x, scaled_flipped_y,
|
|
|
|
scaled_x + scaled_width, scaled_flipped_y + scaled_height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
2019-11-05 13:15:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-12 15:10:08 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2021-05-20 04:01:47 +00:00
|
|
|
if (IsUsingSoftwareRendererForReadbacks())
|
|
|
|
CopySoftwareRendererVRAM(src_x, src_y, dst_x, dst_y, width, height);
|
|
|
|
|
2021-02-07 09:56:38 +00:00
|
|
|
const Common::Rectangle<u32> dst_bounds = GetVRAMTransferBounds(dst_x, dst_y, width, height);
|
|
|
|
const Common::Rectangle<u32> src_bounds = GetVRAMTransferBounds(src_x, src_y, width, height);
|
|
|
|
const bool src_dirty = m_vram_dirty_rect.Intersects(src_bounds);
|
|
|
|
|
2020-04-19 12:30:54 +00:00
|
|
|
if (UseVRAMCopyShader(src_x, src_y, dst_x, dst_y, width, height))
|
2020-01-22 14:01:05 +00:00
|
|
|
{
|
2021-02-07 09:56:38 +00:00
|
|
|
if (src_dirty)
|
2020-04-19 12:30:54 +00:00
|
|
|
UpdateVRAMReadTexture();
|
2021-02-07 10:00:16 +00:00
|
|
|
IncludeVRAMDirtyRectangle(dst_bounds);
|
2020-04-19 12:30:54 +00:00
|
|
|
|
2020-07-22 04:47:09 +00:00
|
|
|
VRAMCopyUBOData uniforms = GetVRAMCopyUBOData(src_x, src_y, dst_x, dst_y, width, height);
|
2020-04-19 12:30:54 +00:00
|
|
|
uniforms.u_src_y = m_vram_texture.GetHeight() - uniforms.u_src_y - uniforms.u_height;
|
|
|
|
uniforms.u_dst_y = m_vram_texture.GetHeight() - uniforms.u_dst_y - uniforms.u_height;
|
|
|
|
UploadUniformBuffer(&uniforms, sizeof(uniforms));
|
|
|
|
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
glDisable(GL_BLEND);
|
2020-12-22 15:10:49 +00:00
|
|
|
SetDepthFunc((m_GPUSTAT.check_mask_before_draw && !m_pgxp_depth_buffer) ? GL_GEQUAL : GL_ALWAYS);
|
2020-04-19 12:30:54 +00:00
|
|
|
|
|
|
|
const Common::Rectangle<u32> dst_bounds_scaled(dst_bounds * m_resolution_scale);
|
|
|
|
glViewport(dst_bounds_scaled.left,
|
|
|
|
m_vram_texture.GetHeight() - dst_bounds_scaled.top - dst_bounds_scaled.GetHeight(),
|
|
|
|
dst_bounds_scaled.GetWidth(), dst_bounds_scaled.GetHeight());
|
|
|
|
m_vram_read_texture.Bind();
|
|
|
|
m_vram_copy_program.Bind();
|
2021-04-05 16:19:56 +00:00
|
|
|
glBindVertexArray(m_attributeless_vao_id);
|
2020-04-19 12:30:54 +00:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
|
|
|
|
|
|
|
RestoreGraphicsAPIState();
|
2020-06-26 16:23:12 +00:00
|
|
|
|
|
|
|
if (m_GPUSTAT.check_mask_before_draw)
|
|
|
|
m_current_depth++;
|
|
|
|
|
2020-01-22 14:01:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:12:37 +00:00
|
|
|
GPU_HW::CopyVRAM(src_x, src_y, dst_x, dst_y, width, height);
|
|
|
|
|
2019-10-03 06:46:13 +00:00
|
|
|
src_x *= m_resolution_scale;
|
|
|
|
src_y *= m_resolution_scale;
|
|
|
|
dst_x *= m_resolution_scale;
|
|
|
|
dst_y *= m_resolution_scale;
|
|
|
|
width *= m_resolution_scale;
|
|
|
|
height *= m_resolution_scale;
|
2019-09-17 14:58:30 +00:00
|
|
|
|
|
|
|
// lower-left origin flip
|
2019-12-10 14:01:29 +00:00
|
|
|
src_y = m_vram_texture.GetHeight() - src_y - height;
|
|
|
|
dst_y = m_vram_texture.GetHeight() - dst_y - height;
|
2019-09-17 14:58:30 +00:00
|
|
|
|
2020-04-16 11:27:01 +00:00
|
|
|
if (GLAD_GL_VERSION_4_3)
|
2019-11-24 14:26:18 +00:00
|
|
|
{
|
2020-10-30 14:38:06 +00:00
|
|
|
glCopyImageSubData(m_vram_texture.GetGLId(), m_vram_texture.GetGLTarget(), 0, src_x, src_y, 0,
|
|
|
|
m_vram_texture.GetGLId(), m_vram_texture.GetGLTarget(), 0, dst_x, dst_y, 0, width, height, 1);
|
2019-11-24 14:26:18 +00:00
|
|
|
}
|
|
|
|
else if (GLAD_GL_EXT_copy_image)
|
|
|
|
{
|
2020-10-30 14:38:06 +00:00
|
|
|
glCopyImageSubDataEXT(m_vram_texture.GetGLId(), m_vram_texture.GetGLTarget(), 0, src_x, src_y, 0,
|
|
|
|
m_vram_texture.GetGLId(), m_vram_texture.GetGLTarget(), 0, dst_x, dst_y, 0, width, height, 1);
|
2019-11-24 14:26:18 +00:00
|
|
|
}
|
2020-11-30 14:08:26 +00:00
|
|
|
else if (GLAD_GL_OES_copy_image)
|
|
|
|
{
|
|
|
|
glCopyImageSubDataOES(m_vram_texture.GetGLId(), m_vram_texture.GetGLTarget(), 0, src_x, src_y, 0,
|
|
|
|
m_vram_texture.GetGLId(), m_vram_texture.GetGLTarget(), 0, dst_x, dst_y, 0, width, height, 1);
|
|
|
|
}
|
2020-04-16 11:27:01 +00:00
|
|
|
else
|
2019-11-24 14:26:18 +00:00
|
|
|
{
|
2021-04-09 08:06:42 +00:00
|
|
|
// glBlitFramebufer with same source/destination should be legal, but on Mali (at least Bifrost) it breaks.
|
|
|
|
// So, blit from the shadow texture, like in the other renderers.
|
2021-02-07 09:56:38 +00:00
|
|
|
if (src_dirty)
|
|
|
|
UpdateVRAMReadTexture();
|
|
|
|
|
2021-04-05 16:19:56 +00:00
|
|
|
glDisable(GL_SCISSOR_TEST);
|
2021-04-09 08:06:42 +00:00
|
|
|
m_vram_read_texture.BindFramebuffer(GL_READ_FRAMEBUFFER);
|
2021-04-05 16:19:56 +00:00
|
|
|
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);
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
2019-11-05 13:05:57 +00:00
|
|
|
}
|
2021-04-05 16:19:56 +00:00
|
|
|
|
2021-04-09 08:06:42 +00:00
|
|
|
IncludeVRAMDirtyRectangle(dst_bounds);
|
2021-04-05 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_HW_OpenGL::UpdateVRAMReadTexture()
|
|
|
|
{
|
|
|
|
const auto scaled_rect = m_vram_dirty_rect * m_resolution_scale;
|
|
|
|
const u32 width = scaled_rect.GetWidth();
|
|
|
|
const u32 height = scaled_rect.GetHeight();
|
|
|
|
const u32 x = scaled_rect.left;
|
|
|
|
const u32 y = m_vram_texture.GetHeight() - scaled_rect.top - height;
|
2021-04-09 08:06:42 +00:00
|
|
|
const bool multisampled = m_vram_texture.IsMultisampled();
|
2021-04-05 16:19:56 +00:00
|
|
|
|
2021-04-09 08:06:42 +00:00
|
|
|
if (!multisampled && GLAD_GL_VERSION_4_3)
|
|
|
|
{
|
|
|
|
glCopyImageSubData(m_vram_texture.GetGLId(), m_vram_texture.GetGLTarget(), 0, x, y, 0,
|
|
|
|
m_vram_read_texture.GetGLId(), m_vram_texture.GetGLTarget(), 0, x, y, 0, width, height, 1);
|
|
|
|
}
|
|
|
|
else if (!multisampled && GLAD_GL_EXT_copy_image)
|
|
|
|
{
|
|
|
|
glCopyImageSubDataEXT(m_vram_texture.GetGLId(), m_vram_texture.GetGLTarget(), 0, x, y, 0,
|
|
|
|
m_vram_read_texture.GetGLId(), m_vram_texture.GetGLTarget(), 0, x, y, 0, width, height, 1);
|
|
|
|
}
|
|
|
|
else if (!multisampled && GLAD_GL_OES_copy_image)
|
|
|
|
{
|
|
|
|
glCopyImageSubDataOES(m_vram_texture.GetGLId(), m_vram_texture.GetGLTarget(), 0, x, y, 0,
|
|
|
|
m_vram_read_texture.GetGLId(), m_vram_texture.GetGLTarget(), 0, x, y, 0, width, height, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_vram_read_texture.BindFramebuffer(GL_DRAW_FRAMEBUFFER);
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, m_vram_fbo_id);
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
glBlitFramebuffer(x, y, x + width, y + height, x, y, x + width, y + height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_vram_fbo_id);
|
|
|
|
}
|
2020-06-02 11:45:36 +00:00
|
|
|
|
|
|
|
GPU_HW::UpdateVRAMReadTexture();
|
2019-09-13 16:07:31 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 07:11:28 +00:00
|
|
|
void GPU_HW_OpenGL::UpdateDepthBufferFromMaskBit()
|
|
|
|
{
|
2020-12-22 15:10:49 +00:00
|
|
|
if (m_pgxp_depth_buffer)
|
|
|
|
return;
|
|
|
|
|
2020-05-03 07:11:28 +00:00
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
|
|
|
glDepthFunc(GL_ALWAYS);
|
|
|
|
|
|
|
|
m_vram_texture.Bind();
|
|
|
|
m_vram_update_depth_program.Bind();
|
|
|
|
glBindVertexArray(m_attributeless_vao_id);
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
|
|
|
|
|
|
|
glBindVertexArray(m_vao_id);
|
|
|
|
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
2020-10-31 02:12:23 +00:00
|
|
|
|
|
|
|
m_vram_read_texture.Bind();
|
2020-05-03 07:11:28 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 15:10:49 +00:00
|
|
|
void GPU_HW_OpenGL::ClearDepthBuffer()
|
|
|
|
{
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
IsGLES() ? glClearDepthf(1.0f) : glClearDepth(1.0f);
|
|
|
|
glClear(GL_DEPTH_BUFFER_BIT);
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
|
|
|
m_last_depth_z = 1.0f;
|
|
|
|
}
|
|
|
|
|
2020-12-30 06:26:20 +00:00
|
|
|
void GPU_HW_OpenGL::DownsampleFramebuffer(GL::Texture& source, u32 left, u32 top, u32 width, u32 height)
|
|
|
|
{
|
|
|
|
DebugAssert(m_downsample_mode != GPUDownsampleMode::Adaptive);
|
|
|
|
DownsampleFramebufferBoxFilter(source, left, top, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_HW_OpenGL::DownsampleFramebufferBoxFilter(GL::Texture& source, u32 left, u32 top, u32 width, u32 height)
|
|
|
|
{
|
|
|
|
const u32 ds_left = left / m_resolution_scale;
|
|
|
|
const u32 ds_top = top / m_resolution_scale;
|
|
|
|
const u32 ds_width = width / m_resolution_scale;
|
|
|
|
const u32 ds_height = height / m_resolution_scale;
|
|
|
|
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
glViewport(ds_left, m_downsample_texture.GetHeight() - ds_top - ds_height, ds_width, ds_height);
|
|
|
|
glBindVertexArray(m_attributeless_vao_id);
|
|
|
|
source.Bind();
|
|
|
|
m_downsample_texture.BindFramebuffer(GL_DRAW_FRAMEBUFFER);
|
|
|
|
m_downsample_program.Bind();
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
|
|
|
|
|
|
|
RestoreGraphicsAPIState();
|
|
|
|
|
|
|
|
m_host_display->SetDisplayTexture(reinterpret_cast<void*>(static_cast<uintptr_t>(m_downsample_texture.GetGLId())),
|
|
|
|
HostDisplayPixelFormat::RGBA8, m_downsample_texture.GetWidth(),
|
|
|
|
m_downsample_texture.GetHeight(), ds_left,
|
|
|
|
m_downsample_texture.GetHeight() - ds_top, ds_width, -static_cast<s32>(ds_height));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|