2013-04-18 03:29:41 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/HW/Memmap.h"
|
|
|
|
|
|
|
|
#include "VideoBackends/OGL/FramebufferManager.h"
|
|
|
|
#include "VideoBackends/OGL/Render.h"
|
|
|
|
#include "VideoBackends/OGL/TextureConverter.h"
|
|
|
|
|
|
|
|
#include "VideoCommon/DriverDetails.h"
|
|
|
|
#include "VideoCommon/OnScreenDisplay.h"
|
|
|
|
#include "VideoCommon/VertexShaderGen.h"
|
2011-01-29 20:16:51 +00:00
|
|
|
|
|
|
|
namespace OGL
|
|
|
|
{
|
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
int FramebufferManager::m_targetWidth;
|
|
|
|
int FramebufferManager::m_targetHeight;
|
|
|
|
int FramebufferManager::m_msaaSamples;
|
2009-09-03 20:37:35 +00:00
|
|
|
|
2014-05-01 10:45:05 +00:00
|
|
|
GLenum FramebufferManager::m_textureType;
|
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
GLuint FramebufferManager::m_efbFramebuffer;
|
2014-05-01 10:45:05 +00:00
|
|
|
GLuint FramebufferManager::m_xfbFramebuffer;
|
|
|
|
GLuint FramebufferManager::m_efbColor;
|
|
|
|
GLuint FramebufferManager::m_efbDepth;
|
|
|
|
GLuint FramebufferManager::m_efbColorSwap; // for hot swap when reinterpreting EFB pixel formats
|
2010-11-14 23:31:53 +00:00
|
|
|
|
|
|
|
// Only used in MSAA mode.
|
|
|
|
GLuint FramebufferManager::m_resolvedFramebuffer;
|
|
|
|
GLuint FramebufferManager::m_resolvedColorTexture;
|
|
|
|
GLuint FramebufferManager::m_resolvedDepthTexture;
|
|
|
|
|
2013-07-22 13:41:10 +00:00
|
|
|
// reinterpret pixel format
|
|
|
|
SHADER FramebufferManager::m_pixel_format_shaders[2];
|
|
|
|
|
|
|
|
|
2014-04-30 16:13:31 +00:00
|
|
|
FramebufferManager::FramebufferManager(int targetWidth, int targetHeight, int msaaSamples)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2013-04-15 20:28:55 +00:00
|
|
|
m_efbFramebuffer = 0;
|
2014-05-01 10:45:05 +00:00
|
|
|
m_xfbFramebuffer = 0;
|
2013-04-15 20:28:55 +00:00
|
|
|
m_efbColor = 0;
|
|
|
|
m_efbDepth = 0;
|
2014-05-01 10:45:05 +00:00
|
|
|
m_efbColorSwap = 0;
|
2013-04-15 20:28:55 +00:00
|
|
|
m_resolvedFramebuffer = 0;
|
|
|
|
m_resolvedColorTexture = 0;
|
|
|
|
m_resolvedDepthTexture = 0;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2009-07-06 02:10:26 +00:00
|
|
|
m_targetWidth = targetWidth;
|
|
|
|
m_targetHeight = targetHeight;
|
2010-11-14 23:31:53 +00:00
|
|
|
|
2009-07-06 02:10:26 +00:00
|
|
|
m_msaaSamples = msaaSamples;
|
|
|
|
|
|
|
|
// The EFB can be set to different pixel formats by the game through the
|
|
|
|
// BPMEM_ZCOMPARE register (which should probably have a different name).
|
|
|
|
// They are:
|
|
|
|
// - 24-bit RGB (8-bit components) with 24-bit Z
|
|
|
|
// - 24-bit RGBA (6-bit components) with 24-bit Z
|
|
|
|
// - Multisampled 16-bit RGB (5-6-5 format) with 16-bit Z
|
|
|
|
// We only use one EFB format here: 32-bit ARGB with 24-bit Z.
|
|
|
|
// Multisampling depends on user settings.
|
|
|
|
// The distinction becomes important for certain operations, i.e. the
|
|
|
|
// alpha channel should be ignored if the EFB does not have one.
|
|
|
|
|
2013-07-13 22:24:23 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0 + 9);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-05-01 10:45:05 +00:00
|
|
|
GLuint glObj[3];
|
|
|
|
glGenTextures(3, glObj);
|
|
|
|
m_efbColor = glObj[0];
|
|
|
|
m_efbDepth = glObj[1];
|
|
|
|
m_efbColorSwap = glObj[2];
|
|
|
|
|
|
|
|
// OpenGL MSAA textures are a different kind of texture type and must be allocated
|
|
|
|
// with a different function, so we create them separately.
|
2009-07-06 02:10:26 +00:00
|
|
|
if (m_msaaSamples <= 1)
|
|
|
|
{
|
2014-05-01 10:45:05 +00:00
|
|
|
m_textureType = GL_TEXTURE_2D;
|
|
|
|
|
|
|
|
glBindTexture(m_textureType, m_efbColor);
|
|
|
|
glTexParameteri(m_textureType, GL_TEXTURE_MAX_LEVEL, 0);
|
|
|
|
glTexParameteri(m_textureType, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(m_textureType, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexImage2D(m_textureType, 0, GL_RGBA, m_targetWidth, m_targetHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
|
|
|
|
|
|
|
glBindTexture(m_textureType, m_efbDepth);
|
|
|
|
glTexParameteri(m_textureType, GL_TEXTURE_MAX_LEVEL, 0);
|
|
|
|
glTexParameteri(m_textureType, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(m_textureType, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexImage2D(m_textureType, 0, GL_DEPTH_COMPONENT24, m_targetWidth, m_targetHeight, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
|
|
|
|
|
|
|
|
glBindTexture(m_textureType, m_efbColorSwap);
|
|
|
|
glTexParameteri(m_textureType, GL_TEXTURE_MAX_LEVEL, 0);
|
|
|
|
glTexParameteri(m_textureType, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(m_textureType, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexImage2D(m_textureType, 0, GL_RGBA, m_targetWidth, m_targetHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-01 10:45:05 +00:00
|
|
|
m_textureType = GL_TEXTURE_2D_MULTISAMPLE;
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-05-01 10:45:05 +00:00
|
|
|
glBindTexture(m_textureType, m_efbColor);
|
|
|
|
glTexImage2DMultisample(m_textureType, m_msaaSamples, GL_RGBA, m_targetWidth, m_targetHeight, false);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-05-01 10:45:05 +00:00
|
|
|
glBindTexture(m_textureType, m_efbDepth);
|
|
|
|
glTexImage2DMultisample(m_textureType, m_msaaSamples, GL_DEPTH_COMPONENT24, m_targetWidth, m_targetHeight, false);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-05-01 10:45:05 +00:00
|
|
|
glBindTexture(m_textureType, m_efbColorSwap);
|
|
|
|
glTexImage2DMultisample(m_textureType, m_msaaSamples, GL_RGBA, m_targetWidth, m_targetHeight, false);
|
|
|
|
glBindTexture(m_textureType, 0);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-05-01 10:45:05 +00:00
|
|
|
// Although we are able to access the multisampled texture directly, we don't do it everywhere.
|
|
|
|
// The old way is to "resolve" this multisampled texture by copying it into a non-sampled texture.
|
|
|
|
// This would lead to an unneeded copy of the EFB, so we are going to avoid it.
|
|
|
|
// But as this job isn't done right now, we do need that texture for resolving:
|
2009-07-06 02:10:26 +00:00
|
|
|
glGenTextures(2, glObj);
|
|
|
|
m_resolvedColorTexture = glObj[0];
|
|
|
|
m_resolvedDepthTexture = glObj[1];
|
|
|
|
|
2013-11-25 07:32:41 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_resolvedColorTexture);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
|
2014-01-03 07:15:19 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
2014-03-09 20:14:26 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_targetWidth, m_targetHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2013-11-25 07:32:41 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_resolvedDepthTexture);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
|
2014-01-03 07:15:19 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
2014-03-09 20:14:26 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, m_targetWidth, m_targetHeight, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
// Bind resolved textures to resolved framebuffer.
|
2014-05-01 10:45:05 +00:00
|
|
|
glGenFramebuffers(1, &m_resolvedFramebuffer);
|
2013-01-03 11:06:47 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_resolvedFramebuffer);
|
2013-11-25 07:32:41 +00:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_resolvedColorTexture, 0);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_resolvedDepthTexture, 0);
|
2009-07-06 02:10:26 +00:00
|
|
|
GL_REPORT_FBO_ERROR();
|
|
|
|
}
|
|
|
|
|
2014-05-01 10:45:05 +00:00
|
|
|
// Create XFB framebuffer; targets will be created elsewhere.
|
2013-01-03 11:06:47 +00:00
|
|
|
glGenFramebuffers(1, &m_xfbFramebuffer);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-05-01 10:45:05 +00:00
|
|
|
// Bind target textures to EFB framebuffer.
|
|
|
|
glGenFramebuffers(1, &m_efbFramebuffer);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_efbFramebuffer);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_textureType, m_efbColor, 0);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_textureType, m_efbDepth, 0);
|
|
|
|
GL_REPORT_FBO_ERROR();
|
|
|
|
|
2010-12-28 16:16:27 +00:00
|
|
|
// EFB framebuffer is currently bound, make sure to clear its alpha value to 1.f
|
|
|
|
glViewport(0, 0, m_targetWidth, m_targetHeight);
|
|
|
|
glScissor(0, 0, m_targetWidth, m_targetHeight);
|
|
|
|
glClearColor(0.f, 0.f, 0.f, 1.f);
|
2013-05-06 04:20:46 +00:00
|
|
|
glClearDepthf(1.0f);
|
2010-12-28 16:16:27 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-07-22 13:41:10 +00:00
|
|
|
// reinterpret pixel format
|
2013-10-29 05:23:17 +00:00
|
|
|
char vs[] =
|
2013-07-22 13:41:10 +00:00
|
|
|
"void main(void) {\n"
|
2013-11-25 11:27:54 +00:00
|
|
|
" vec2 rawpos = vec2(gl_VertexID&1, gl_VertexID&2);\n"
|
|
|
|
" gl_Position = vec4(rawpos*2.0-1.0, 0.0, 1.0);\n"
|
2013-07-22 13:41:10 +00:00
|
|
|
"}\n";
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-05-01 10:45:05 +00:00
|
|
|
// The way to sample the EFB is based on the on the current configuration.
|
|
|
|
// As we use the same sampling way for both interpreting shaders, the sampling
|
|
|
|
// shader are generated first:
|
|
|
|
std::string sampler;
|
|
|
|
if (m_msaaSamples <= 1)
|
|
|
|
{
|
|
|
|
// non-msaa, so just fetch the pixel
|
|
|
|
sampler =
|
|
|
|
"uniform sampler2D samp9;\n"
|
|
|
|
"vec4 sampleEFB(ivec2 pos) {\n"
|
|
|
|
" return texelFetch(samp9, pos, 0);\n"
|
|
|
|
"}\n";
|
|
|
|
}
|
|
|
|
else if (g_ogl_config.bSupportSampleShading)
|
|
|
|
{
|
|
|
|
// msaa + sample shading available, so just fetch the sample
|
|
|
|
// This will lead to sample shading, but it's the only way to not loose
|
|
|
|
// the values of each sample.
|
|
|
|
sampler =
|
|
|
|
"uniform sampler2DMS samp9;\n"
|
|
|
|
"vec4 sampleEFB(ivec2 pos) {\n"
|
|
|
|
" return texelFetch(samp9, pos, gl_SampleID);\n"
|
|
|
|
"}\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// msaa without sample shading: calculate the mean value of the pixel
|
|
|
|
std::stringstream samples;
|
|
|
|
samples << m_msaaSamples;
|
|
|
|
sampler =
|
|
|
|
"uniform sampler2DMS samp9;\n"
|
|
|
|
"vec4 sampleEFB(ivec2 pos) {\n"
|
|
|
|
" vec4 color = vec4(0.0, 0.0, 0.0, 0.0);\n"
|
|
|
|
" for(int i=0; i<" + samples.str() + "; i++)\n"
|
|
|
|
" color += texelFetch(samp9, pos, i);\n"
|
|
|
|
" return color / " + samples.str() + ";\n"
|
|
|
|
"}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ps_rgba6_to_rgb8 = sampler +
|
2013-11-25 00:06:29 +00:00
|
|
|
"out vec4 ocol0;\n"
|
2013-07-22 13:41:10 +00:00
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
2014-05-01 10:45:05 +00:00
|
|
|
" ivec4 src6 = ivec4(round(sampleEFB(ivec2(gl_FragCoord.xy)) * 63.f));\n"
|
2013-07-22 13:41:10 +00:00
|
|
|
" ivec4 dst8;\n"
|
|
|
|
" dst8.r = (src6.r << 2) | (src6.g >> 4);\n"
|
|
|
|
" dst8.g = ((src6.g & 0xF) << 4) | (src6.b >> 2);\n"
|
|
|
|
" dst8.b = ((src6.b & 0x3) << 6) | src6.a;\n"
|
|
|
|
" dst8.a = 255;\n"
|
|
|
|
" ocol0 = float4(dst8) / 255.f;\n"
|
|
|
|
"}";
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-05-01 10:45:05 +00:00
|
|
|
std::string ps_rgb8_to_rgba6 = sampler +
|
2013-11-25 00:06:29 +00:00
|
|
|
"out vec4 ocol0;\n"
|
2013-07-22 13:41:10 +00:00
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
2014-05-01 10:45:05 +00:00
|
|
|
" ivec4 src8 = ivec4(round(sampleEFB(ivec2(gl_FragCoord.xy)) * 255.f));\n"
|
2013-07-22 13:41:10 +00:00
|
|
|
" ivec4 dst6;\n"
|
|
|
|
" dst6.r = src8.r >> 2;\n"
|
|
|
|
" dst6.g = ((src8.r & 0x3) << 4) | (src8.g >> 4);\n"
|
|
|
|
" dst6.b = ((src8.g & 0xF) << 2) | (src8.b >> 6);\n"
|
|
|
|
" dst6.a = src8.b & 0x3F;\n"
|
|
|
|
" ocol0 = float4(dst6) / 63.f;\n"
|
|
|
|
"}";
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-05-01 10:45:05 +00:00
|
|
|
ProgramShaderCache::CompileShader(m_pixel_format_shaders[0], vs, ps_rgb8_to_rgba6.c_str());
|
|
|
|
ProgramShaderCache::CompileShader(m_pixel_format_shaders[1], vs, ps_rgba6_to_rgb8.c_str());
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
FramebufferManager::~FramebufferManager()
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2013-01-03 11:06:47 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
GLuint glObj[3];
|
|
|
|
|
|
|
|
// Note: OpenGL deletion functions silently ignore parameters of "0".
|
|
|
|
|
|
|
|
glObj[0] = m_efbFramebuffer;
|
2014-05-01 10:45:05 +00:00
|
|
|
glObj[1] = m_xfbFramebuffer;
|
|
|
|
glObj[2] = m_resolvedFramebuffer;
|
2013-01-03 11:06:47 +00:00
|
|
|
glDeleteFramebuffers(3, glObj);
|
2009-07-06 02:10:26 +00:00
|
|
|
m_efbFramebuffer = 0;
|
|
|
|
m_xfbFramebuffer = 0;
|
2014-05-01 10:45:05 +00:00
|
|
|
m_resolvedFramebuffer = 0;
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
glObj[0] = m_resolvedColorTexture;
|
|
|
|
glObj[1] = m_resolvedDepthTexture;
|
2010-11-14 23:31:53 +00:00
|
|
|
glDeleteTextures(2, glObj);
|
2009-07-06 02:10:26 +00:00
|
|
|
m_resolvedColorTexture = 0;
|
|
|
|
m_resolvedDepthTexture = 0;
|
|
|
|
|
|
|
|
glObj[0] = m_efbColor;
|
|
|
|
glObj[1] = m_efbDepth;
|
2014-05-01 10:45:05 +00:00
|
|
|
glObj[2] = m_efbColorSwap;
|
|
|
|
glDeleteTextures(3, glObj);
|
2009-07-06 02:10:26 +00:00
|
|
|
m_efbColor = 0;
|
|
|
|
m_efbDepth = 0;
|
2014-05-01 10:45:05 +00:00
|
|
|
m_efbColorSwap = 0;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-07-22 13:41:10 +00:00
|
|
|
// reinterpret pixel format
|
|
|
|
m_pixel_format_shaders[0].Destroy();
|
|
|
|
m_pixel_format_shaders[1].Destroy();
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
GLuint FramebufferManager::GetEFBColorTexture(const EFBRectangle& sourceRc)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
|
|
|
if (m_msaaSamples <= 1)
|
|
|
|
{
|
|
|
|
return m_efbColor;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Transfer the EFB to a resolved texture. EXT_framebuffer_blit is
|
|
|
|
// required.
|
|
|
|
|
2010-11-18 02:21:26 +00:00
|
|
|
TargetRectangle targetRc = g_renderer->ConvertEFBRectangle(sourceRc);
|
2009-07-15 00:51:24 +00:00
|
|
|
targetRc.ClampLL(0, 0, m_targetWidth, m_targetHeight);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
// Resolve.
|
2013-01-03 11:06:47 +00:00
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, m_efbFramebuffer);
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_resolvedFramebuffer);
|
|
|
|
glBlitFramebuffer(
|
2009-07-15 00:51:24 +00:00
|
|
|
targetRc.left, targetRc.top, targetRc.right, targetRc.bottom,
|
|
|
|
targetRc.left, targetRc.top, targetRc.right, targetRc.bottom,
|
2009-07-06 02:10:26 +00:00
|
|
|
GL_COLOR_BUFFER_BIT, GL_NEAREST
|
|
|
|
);
|
|
|
|
|
|
|
|
// Return to EFB.
|
2013-01-03 11:06:47 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_efbFramebuffer);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
return m_resolvedColorTexture;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
GLuint FramebufferManager::GetEFBDepthTexture(const EFBRectangle& sourceRc)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
|
|
|
if (m_msaaSamples <= 1)
|
|
|
|
{
|
|
|
|
return m_efbDepth;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-01 10:45:05 +00:00
|
|
|
// Transfer the EFB to a resolved texture.
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2010-11-18 02:21:26 +00:00
|
|
|
TargetRectangle targetRc = g_renderer->ConvertEFBRectangle(sourceRc);
|
2009-07-15 00:51:24 +00:00
|
|
|
targetRc.ClampLL(0, 0, m_targetWidth, m_targetHeight);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
// Resolve.
|
2013-01-03 11:06:47 +00:00
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, m_efbFramebuffer);
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_resolvedFramebuffer);
|
|
|
|
glBlitFramebuffer(
|
2009-07-15 00:51:24 +00:00
|
|
|
targetRc.left, targetRc.top, targetRc.right, targetRc.bottom,
|
|
|
|
targetRc.left, targetRc.top, targetRc.right, targetRc.bottom,
|
2009-07-06 02:10:26 +00:00
|
|
|
GL_DEPTH_BUFFER_BIT, GL_NEAREST
|
|
|
|
);
|
|
|
|
|
|
|
|
// Return to EFB.
|
2013-01-03 11:06:47 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_efbFramebuffer);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
return m_resolvedDepthTexture;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-27 03:18:01 +00:00
|
|
|
void FramebufferManager::CopyToRealXFB(u32 xfbAddr, u32 fbWidth, u32 fbHeight, const EFBRectangle& sourceRc,float Gamma)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2011-01-31 01:28:32 +00:00
|
|
|
u8* xfb_in_ram = Memory::GetPointer(xfbAddr);
|
2010-09-28 02:15:02 +00:00
|
|
|
if (!xfb_in_ram)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
|
|
|
WARN_LOG(VIDEO, "Tried to copy to invalid XFB address");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-18 02:21:26 +00:00
|
|
|
TargetRectangle targetRc = g_renderer->ConvertEFBRectangle(sourceRc);
|
2010-11-14 23:31:53 +00:00
|
|
|
TextureConverter::EncodeToRamYUYV(ResolveAndGetRenderTarget(sourceRc), targetRc, xfb_in_ram, fbWidth, fbHeight);
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
void FramebufferManager::SetFramebuffer(GLuint fb)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2013-01-03 11:06:47 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, fb != 0 ? fb : GetEFBFramebuffer());
|
2010-11-14 23:31:53 +00:00
|
|
|
}
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
// Apply AA if enabled
|
|
|
|
GLuint FramebufferManager::ResolveAndGetRenderTarget(const EFBRectangle &source_rect)
|
|
|
|
{
|
|
|
|
return GetEFBColorTexture(source_rect);
|
|
|
|
}
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
GLuint FramebufferManager::ResolveAndGetDepthTarget(const EFBRectangle &source_rect)
|
|
|
|
{
|
|
|
|
return GetEFBDepthTexture(source_rect);
|
|
|
|
}
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2013-07-22 13:41:10 +00:00
|
|
|
void FramebufferManager::ReinterpretPixelData(unsigned int convtype)
|
|
|
|
{
|
|
|
|
g_renderer->ResetAPIState();
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-07-22 13:41:10 +00:00
|
|
|
GLuint src_texture = 0;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-05-01 10:45:05 +00:00
|
|
|
// We aren't allowed to render and sample the same texture in one draw call,
|
|
|
|
// so we have to create a new texture and overwrite it completely.
|
|
|
|
// To not allocate one big texture every time, we've allocated two on
|
|
|
|
// initialization and just swap them here:
|
|
|
|
src_texture = m_efbColor;
|
|
|
|
m_efbColor = m_efbColorSwap;
|
|
|
|
m_efbColorSwap = src_texture;
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_textureType, m_efbColor, 0);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-07-22 13:41:10 +00:00
|
|
|
glViewport(0,0, m_targetWidth, m_targetHeight);
|
|
|
|
glActiveTexture(GL_TEXTURE0 + 9);
|
2014-05-01 10:45:05 +00:00
|
|
|
glBindTexture(m_textureType, src_texture);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-07-22 13:41:10 +00:00
|
|
|
m_pixel_format_shaders[convtype ? 1 : 0].Bind();
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
2014-05-01 10:45:05 +00:00
|
|
|
glBindTexture(m_textureType, 0);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-07-22 13:41:10 +00:00
|
|
|
g_renderer->RestoreAPIState();
|
|
|
|
}
|
|
|
|
|
2013-01-16 00:37:00 +00:00
|
|
|
XFBSource::~XFBSource()
|
|
|
|
{
|
2013-08-20 13:11:03 +00:00
|
|
|
glDeleteTextures(1, &texture);
|
2013-01-16 00:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-26 13:21:22 +00:00
|
|
|
void XFBSource::Draw(const MathUtil::Rectangle<int> &sourcerc,
|
2013-11-23 22:33:43 +00:00
|
|
|
const MathUtil::Rectangle<float> &drawrc) const
|
2010-11-14 23:31:53 +00:00
|
|
|
{
|
|
|
|
// Texture map xfbSource->texture onto the main buffer
|
2013-08-20 13:11:03 +00:00
|
|
|
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
|
2013-01-03 19:44:47 +00:00
|
|
|
glBlitFramebuffer(sourcerc.left, sourcerc.bottom, sourcerc.right, sourcerc.top,
|
2014-01-26 13:21:22 +00:00
|
|
|
(int)drawrc.left, (int)drawrc.bottom, (int)drawrc.right, (int)drawrc.top,
|
2013-01-03 19:44:47 +00:00
|
|
|
GL_COLOR_BUFFER_BIT, GL_LINEAR);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
GL_REPORT_ERRORD();
|
|
|
|
}
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
void XFBSource::DecodeToTexture(u32 xfbAddr, u32 fbWidth, u32 fbHeight)
|
|
|
|
{
|
2013-08-20 13:11:03 +00:00
|
|
|
TextureConverter::DecodeToTexture(xfbAddr, fbWidth, fbHeight, texture);
|
2010-11-14 23:31:53 +00:00
|
|
|
}
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2010-12-27 03:18:01 +00:00
|
|
|
void XFBSource::CopyEFB(float Gamma)
|
2010-11-14 23:31:53 +00:00
|
|
|
{
|
2013-08-26 20:18:00 +00:00
|
|
|
g_renderer->ResetAPIState();
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
// Copy EFB data to XFB and restore render target again
|
2013-01-03 17:36:19 +00:00
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, FramebufferManager::GetEFBFramebuffer());
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, FramebufferManager::GetXFBFramebuffer());
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2013-01-03 17:36:19 +00:00
|
|
|
// Bind texture.
|
2013-08-20 13:11:03 +00:00
|
|
|
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
|
2013-01-03 17:36:19 +00:00
|
|
|
GL_REPORT_FBO_ERROR();
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2013-01-03 17:36:19 +00:00
|
|
|
glBlitFramebuffer(
|
|
|
|
0, 0, texWidth, texHeight,
|
|
|
|
0, 0, texWidth, texHeight,
|
|
|
|
GL_COLOR_BUFFER_BIT, GL_NEAREST
|
|
|
|
);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2013-01-03 17:36:19 +00:00
|
|
|
// Return to EFB.
|
2013-01-16 00:37:00 +00:00
|
|
|
FramebufferManager::SetFramebuffer(0);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-08-26 20:18:00 +00:00
|
|
|
g_renderer->RestoreAPIState();
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
XFBSourceBase* FramebufferManager::CreateXFBSource(unsigned int target_width, unsigned int target_height)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2013-08-20 13:11:03 +00:00
|
|
|
GLuint texture;
|
2010-03-10 06:45:13 +00:00
|
|
|
|
2013-08-20 13:11:03 +00:00
|
|
|
glGenTextures(1, &texture);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-08-20 13:11:03 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0 + 9);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
|
2014-03-09 20:14:26 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, target_width, target_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2013-08-20 13:11:03 +00:00
|
|
|
return new XFBSource(texture);
|
2009-09-03 20:37:35 +00:00
|
|
|
}
|
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
void FramebufferManager::GetTargetSize(unsigned int *width, unsigned int *height, const EFBRectangle& sourceRc)
|
2009-09-03 20:37:35 +00:00
|
|
|
{
|
2010-11-14 23:31:53 +00:00
|
|
|
*width = m_targetWidth;
|
|
|
|
*height = m_targetHeight;
|
2009-09-03 20:37:35 +00:00
|
|
|
}
|
2011-01-29 20:16:51 +00:00
|
|
|
|
2011-01-29 22:48:33 +00:00
|
|
|
} // namespace OGL
|