2009-07-28 21:32:10 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
2009-02-21 13:53:26 +00:00
|
|
|
// Fast image conversion using OpenGL shaders.
|
|
|
|
// This kind of stuff would be a LOT nicer with OpenCL.
|
|
|
|
|
2011-01-31 01:28:32 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
#include "TextureConverter.h"
|
2009-01-11 22:25:57 +00:00
|
|
|
#include "TextureConversionShader.h"
|
2010-09-28 02:15:02 +00:00
|
|
|
#include "TextureCache.h"
|
2008-12-26 11:23:59 +00:00
|
|
|
#include "PixelShaderCache.h"
|
2011-12-08 05:20:55 +00:00
|
|
|
#include "ProgramShaderCache.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
#include "VertexShaderManager.h"
|
2009-09-03 20:37:35 +00:00
|
|
|
#include "FramebufferManager.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
#include "Globals.h"
|
2009-09-13 09:23:30 +00:00
|
|
|
#include "VideoConfig.h"
|
2009-01-11 22:25:57 +00:00
|
|
|
#include "ImageWrite.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
#include "Render.h"
|
2010-02-02 21:56:29 +00:00
|
|
|
#include "FileUtil.h"
|
2011-01-31 01:28:32 +00:00
|
|
|
#include "HW/Memmap.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2011-01-29 20:16:51 +00:00
|
|
|
namespace OGL
|
|
|
|
{
|
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
namespace TextureConverter
|
|
|
|
{
|
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
using OGL::TextureCache;
|
|
|
|
|
2009-02-21 13:53:26 +00:00
|
|
|
static GLuint s_texConvFrameBuffer = 0;
|
2008-12-08 05:25:12 +00:00
|
|
|
static GLuint s_srcTexture = 0; // for decoding from RAM
|
2009-09-26 12:39:12 +00:00
|
|
|
static GLuint s_srcTextureWidth = 0;
|
|
|
|
static GLuint s_srcTextureHeight = 0;
|
2008-12-08 05:25:12 +00:00
|
|
|
static GLuint s_dstRenderBuffer = 0; // for encoding to RAM
|
|
|
|
|
|
|
|
const int renderBufferWidth = 1024;
|
|
|
|
const int renderBufferHeight = 1024;
|
|
|
|
|
|
|
|
static FRAGMENTSHADER s_rgbToYuyvProgram;
|
|
|
|
static FRAGMENTSHADER s_yuyvToRgbProgram;
|
|
|
|
|
2009-06-08 18:34:24 +00:00
|
|
|
// Not all slots are taken - but who cares.
|
2009-01-11 22:25:57 +00:00
|
|
|
const u32 NUM_ENCODING_PROGRAMS = 64;
|
|
|
|
static FRAGMENTSHADER s_encodingPrograms[NUM_ENCODING_PROGRAMS];
|
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
void CreateRgbToYuyvProgram()
|
|
|
|
{
|
2009-06-08 18:34:24 +00:00
|
|
|
// Output is BGRA because that is slightly faster than RGBA.
|
2011-12-03 02:17:26 +00:00
|
|
|
if(g_ActiveConfig.bUseGLSL)
|
|
|
|
{
|
2011-12-10 13:38:30 +00:00
|
|
|
if(g_ActiveConfig.backend_info.bSupportsGLSLBinding)
|
|
|
|
{
|
|
|
|
const char *FProgram =
|
|
|
|
"#version 330 compatibility\n"
|
|
|
|
"#extension GL_ARB_texture_rectangle : enable\n"
|
|
|
|
"#extension GL_ARB_shading_language_420pack : enable\n"
|
|
|
|
"layout(binding = 0) uniform sampler2DRect samp0;\n"
|
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
|
|
|
" vec2 uv1 = vec2(gl_TexCoord[0].x + 1.0f, gl_TexCoord[0].y);\n"
|
|
|
|
" vec3 c0 = texture2DRect(samp0, gl_TexCoord[0].xy).rgb;\n"
|
|
|
|
" vec3 c1 = texture2DRect(samp0, uv1).rgb;\n"
|
|
|
|
" vec3 y_const = vec3(0.257f,0.504f,0.098f);\n"
|
|
|
|
" vec3 u_const = vec3(-0.148f,-0.291f,0.439f);\n"
|
|
|
|
" vec3 v_const = vec3(0.439f,-0.368f,-0.071f);\n"
|
|
|
|
" vec4 const3 = vec4(0.0625f,0.5f,0.0625f,0.5f);\n"
|
|
|
|
" vec3 c01 = (c0 + c1) * 0.5f;\n"
|
|
|
|
" gl_FragData[0] = vec4(dot(c1,y_const),dot(c01,u_const),dot(c0,y_const),dot(c01, v_const)) + const3;\n"
|
|
|
|
"}\n";
|
|
|
|
if (!PixelShaderCache::CompilePixelShader(s_rgbToYuyvProgram, FProgram))
|
|
|
|
ERROR_LOG(VIDEO, "Failed to create RGB to YUYV fragment program.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char *FProgram =
|
|
|
|
"#version 120\n"
|
|
|
|
"#extension GL_ARB_texture_rectangle : enable\n"
|
|
|
|
"uniform sampler2DRect samp0;\n"
|
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
|
|
|
" vec2 uv1 = vec2(gl_TexCoord[0].x + 1.0f, gl_TexCoord[0].y);\n"
|
|
|
|
" vec3 c0 = texture2DRect(samp0, gl_TexCoord[0].xy).rgb;\n"
|
|
|
|
" vec3 c1 = texture2DRect(samp0, uv1).rgb;\n"
|
|
|
|
" vec3 y_const = vec3(0.257f,0.504f,0.098f);\n"
|
|
|
|
" vec3 u_const = vec3(-0.148f,-0.291f,0.439f);\n"
|
|
|
|
" vec3 v_const = vec3(0.439f,-0.368f,-0.071f);\n"
|
|
|
|
" vec4 const3 = vec4(0.0625f,0.5f,0.0625f,0.5f);\n"
|
|
|
|
" vec3 c01 = (c0 + c1) * 0.5f;\n"
|
|
|
|
" gl_FragData[0] = vec4(dot(c1,y_const),dot(c01,u_const),dot(c0,y_const),dot(c01, v_const)) + const3;\n"
|
|
|
|
"}\n";
|
|
|
|
if (!PixelShaderCache::CompilePixelShader(s_rgbToYuyvProgram, FProgram))
|
|
|
|
ERROR_LOG(VIDEO, "Failed to create RGB to YUYV fragment program.");
|
|
|
|
}
|
2011-12-03 02:17:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char *FProgram =
|
|
|
|
"uniform samplerRECT samp0 : register(s0);\n"
|
|
|
|
"void main(\n"
|
|
|
|
" out float4 ocol0 : COLOR0,\n"
|
|
|
|
" in float2 uv0 : TEXCOORD0)\n"
|
|
|
|
"{\n"
|
|
|
|
" float2 uv1 = float2(uv0.x + 1.0f, uv0.y);\n"
|
|
|
|
" float3 c0 = texRECT(samp0, uv0).rgb;\n"
|
|
|
|
" float3 c1 = texRECT(samp0, uv1).rgb;\n"
|
|
|
|
" float3 y_const = float3(0.257f,0.504f,0.098f);\n"
|
|
|
|
" float3 u_const = float3(-0.148f,-0.291f,0.439f);\n"
|
|
|
|
" float3 v_const = float3(0.439f,-0.368f,-0.071f);\n"
|
|
|
|
" float4 const3 = float4(0.0625f,0.5f,0.0625f,0.5f);\n"
|
|
|
|
" float3 c01 = (c0 + c1) * 0.5f;\n"
|
|
|
|
" ocol0 = float4(dot(c1,y_const),dot(c01,u_const),dot(c0,y_const),dot(c01, v_const)) + const3;\n"
|
|
|
|
"}\n";
|
|
|
|
if (!PixelShaderCache::CompilePixelShader(s_rgbToYuyvProgram, FProgram))
|
|
|
|
ERROR_LOG(VIDEO, "Failed to create RGB to YUYV fragment program.");
|
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CreateYuyvToRgbProgram()
|
|
|
|
{
|
2011-12-03 02:17:26 +00:00
|
|
|
if(g_ActiveConfig.bUseGLSL)
|
|
|
|
{
|
2011-12-10 13:38:30 +00:00
|
|
|
if(g_ActiveConfig.backend_info.bSupportsGLSLBinding)
|
|
|
|
{
|
|
|
|
const char *FProgram =
|
|
|
|
"#version 330 compatibility\n"
|
|
|
|
"#extension GL_ARB_texture_rectangle : enable\n"
|
|
|
|
"#extension GL_ARB_shading_language_420pack : enable\n"
|
|
|
|
"layout(binding = 0) uniform sampler2DRect samp0;\n"
|
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
|
|
|
" vec4 c0 = texture2DRect(samp0, gl_TexCoord[0].xy).rgba;\n"
|
|
|
|
|
|
|
|
" float f = step(0.5, fract(gl_TexCoord[0].x));\n"
|
|
|
|
" float y = mix(c0.b, c0.r, f);\n"
|
|
|
|
" float yComp = 1.164f * (y - 0.0625f);\n"
|
|
|
|
" float uComp = c0.g - 0.5f;\n"
|
|
|
|
" float vComp = c0.a - 0.5f;\n"
|
|
|
|
|
|
|
|
" gl_FragData[0] = vec4(yComp + (1.596f * vComp),\n"
|
|
|
|
" yComp - (0.813f * vComp) - (0.391f * uComp),\n"
|
|
|
|
" yComp + (2.018f * uComp),\n"
|
|
|
|
" 1.0f);\n"
|
|
|
|
"}\n";
|
|
|
|
if (!PixelShaderCache::CompilePixelShader(s_yuyvToRgbProgram, FProgram))
|
|
|
|
ERROR_LOG(VIDEO, "Failed to create YUYV to RGB fragment program.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char *FProgram =
|
|
|
|
"#version 120\n"
|
|
|
|
"#ifdef GL_ARB_texture_rectangle\n"
|
|
|
|
"#extension GL_ARB_texture_rectangle : require\n"
|
|
|
|
"#endif\n"
|
|
|
|
"uniform sampler2DRect samp0;\n"
|
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
|
|
|
" vec4 c0 = texture2DRect(samp0, gl_TexCoord[0].xy).rgba;\n"
|
|
|
|
|
|
|
|
" float f = step(0.5, fract(gl_TexCoord[0].x));\n"
|
|
|
|
" float y = mix(c0.b, c0.r, f);\n"
|
|
|
|
" float yComp = 1.164f * (y - 0.0625f);\n"
|
|
|
|
" float uComp = c0.g - 0.5f;\n"
|
|
|
|
" float vComp = c0.a - 0.5f;\n"
|
|
|
|
|
|
|
|
" gl_FragData[0] = vec4(yComp + (1.596f * vComp),\n"
|
|
|
|
" yComp - (0.813f * vComp) - (0.391f * uComp),\n"
|
|
|
|
" yComp + (2.018f * uComp),\n"
|
|
|
|
" 1.0f);\n"
|
|
|
|
"}\n";
|
|
|
|
if (!PixelShaderCache::CompilePixelShader(s_yuyvToRgbProgram, FProgram))
|
|
|
|
ERROR_LOG(VIDEO, "Failed to create YUYV to RGB fragment program.");
|
|
|
|
}
|
2011-12-03 02:17:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char *FProgram =
|
|
|
|
"uniform samplerRECT samp0 : register(s0);\n"
|
|
|
|
"void main(\n"
|
|
|
|
" out float4 ocol0 : COLOR0,\n"
|
|
|
|
" in float2 uv0 : TEXCOORD0)\n"
|
|
|
|
"{\n"
|
|
|
|
" float4 c0 = texRECT(samp0, uv0).rgba;\n"
|
|
|
|
|
|
|
|
" float f = step(0.5, frac(uv0.x));\n"
|
|
|
|
" float y = lerp(c0.b, c0.r, f);\n"
|
|
|
|
" float yComp = 1.164f * (y - 0.0625f);\n"
|
|
|
|
" float uComp = c0.g - 0.5f;\n"
|
|
|
|
" float vComp = c0.a - 0.5f;\n"
|
|
|
|
|
|
|
|
" ocol0 = float4(yComp + (1.596f * vComp),\n"
|
|
|
|
" yComp - (0.813f * vComp) - (0.391f * uComp),\n"
|
|
|
|
" yComp + (2.018f * uComp),\n"
|
|
|
|
" 1.0f);\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
if (!PixelShaderCache::CompilePixelShader(s_yuyvToRgbProgram, FProgram))
|
|
|
|
ERROR_LOG(VIDEO, "Failed to create YUYV to RGB fragment program.");
|
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2009-03-08 19:19:51 +00:00
|
|
|
FRAGMENTSHADER &GetOrCreateEncodingShader(u32 format)
|
2009-01-11 22:25:57 +00:00
|
|
|
{
|
2009-02-21 13:53:26 +00:00
|
|
|
if (format > NUM_ENCODING_PROGRAMS)
|
2009-01-11 22:25:57 +00:00
|
|
|
{
|
|
|
|
PanicAlert("Unknown texture copy format: 0x%x\n", format);
|
|
|
|
return s_encodingPrograms[0];
|
|
|
|
}
|
|
|
|
|
2009-02-21 13:53:26 +00:00
|
|
|
if (s_encodingPrograms[format].glprogid == 0)
|
2009-01-11 22:25:57 +00:00
|
|
|
{
|
2011-12-03 02:17:26 +00:00
|
|
|
const char* shader = TextureConversionShader::GenerateEncodingShader(format, g_ActiveConfig.bUseGLSL ? API_GLSL : API_OPENGL);
|
2009-01-11 22:25:57 +00:00
|
|
|
|
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
2011-07-15 02:17:14 +00:00
|
|
|
if (g_ActiveConfig.iLog & CONF_SAVESHADERS && shader)
|
|
|
|
{
|
2009-02-21 13:53:26 +00:00
|
|
|
static int counter = 0;
|
|
|
|
char szTemp[MAX_PATH];
|
2011-02-28 20:40:15 +00:00
|
|
|
sprintf(szTemp, "%senc_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++);
|
2009-02-21 13:53:26 +00:00
|
|
|
|
|
|
|
SaveData(szTemp, shader);
|
|
|
|
}
|
2009-01-11 22:25:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!PixelShaderCache::CompilePixelShader(s_encodingPrograms[format], shader)) {
|
2010-07-24 10:24:16 +00:00
|
|
|
ERROR_LOG(VIDEO, "Failed to create encoding fragment program");
|
2009-01-11 22:25:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return s_encodingPrograms[format];
|
|
|
|
}
|
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
void Init()
|
|
|
|
{
|
2009-02-22 22:49:42 +00:00
|
|
|
glGenFramebuffersEXT(1, &s_texConvFrameBuffer);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
glGenRenderbuffersEXT(1, &s_dstRenderBuffer);
|
|
|
|
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, s_dstRenderBuffer);
|
|
|
|
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA, renderBufferWidth, renderBufferHeight);
|
|
|
|
|
2011-02-13 19:05:24 +00:00
|
|
|
s_srcTextureWidth = 0;
|
|
|
|
s_srcTextureHeight = 0;
|
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
glGenTextures(1, &s_srcTexture);
|
|
|
|
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, s_srcTexture);
|
|
|
|
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
2011-02-13 19:05:24 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
CreateRgbToYuyvProgram();
|
|
|
|
CreateYuyvToRgbProgram();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown()
|
|
|
|
{
|
2011-07-15 02:17:14 +00:00
|
|
|
glDeleteTextures(1, &s_srcTexture);
|
2008-12-08 05:25:12 +00:00
|
|
|
glDeleteRenderbuffersEXT(1, &s_dstRenderBuffer);
|
2009-02-22 22:49:42 +00:00
|
|
|
glDeleteFramebuffersEXT(1, &s_texConvFrameBuffer);
|
2009-06-08 18:34:24 +00:00
|
|
|
|
|
|
|
s_rgbToYuyvProgram.Destroy();
|
|
|
|
s_yuyvToRgbProgram.Destroy();
|
|
|
|
|
2009-07-03 23:53:20 +00:00
|
|
|
for (unsigned int i = 0; i < NUM_ENCODING_PROGRAMS; i++)
|
2009-06-08 18:34:24 +00:00
|
|
|
s_encodingPrograms[i].Destroy();
|
|
|
|
|
2009-02-22 22:49:42 +00:00
|
|
|
s_srcTexture = 0;
|
|
|
|
s_dstRenderBuffer = 0;
|
|
|
|
s_texConvFrameBuffer = 0;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2011-12-08 05:20:55 +00:00
|
|
|
void EncodeToRamUsingShader(GLuint srcTexture, const TargetRectangle& sourceRc,
|
2011-12-11 10:14:02 +00:00
|
|
|
u8* destAddr, int dstWidth, int dstHeight, int readStride,
|
2011-07-15 02:17:14 +00:00
|
|
|
bool toTexture, bool linearFilter)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2010-07-12 19:30:25 +00:00
|
|
|
|
2011-07-15 02:17:14 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
// switch to texture converter frame buffer
|
|
|
|
// attach render buffer as color destination
|
2010-11-14 23:31:53 +00:00
|
|
|
FramebufferManager::SetFramebuffer(s_texConvFrameBuffer);
|
2009-09-03 20:37:35 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, s_dstRenderBuffer);
|
2011-07-15 02:17:14 +00:00
|
|
|
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, s_dstRenderBuffer);
|
2008-12-08 05:25:12 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2011-07-15 02:17:14 +00:00
|
|
|
|
2009-03-22 11:21:44 +00:00
|
|
|
for (int i = 1; i < 8; ++i)
|
2010-09-28 02:15:02 +00:00
|
|
|
TextureCache::DisableStage(i);
|
2009-03-22 11:21:44 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
// set source texture
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
2009-03-22 11:21:44 +00:00
|
|
|
glEnable(GL_TEXTURE_RECTANGLE_ARB);
|
2008-12-08 05:25:12 +00:00
|
|
|
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, srcTexture);
|
2009-01-11 22:25:57 +00:00
|
|
|
|
2009-02-28 16:33:59 +00:00
|
|
|
if (linearFilter)
|
2009-01-11 22:25:57 +00:00
|
|
|
{
|
|
|
|
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
}
|
2009-09-04 06:09:21 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2011-12-08 05:23:00 +00:00
|
|
|
if(g_ActiveConfig.bUseGLSL)
|
|
|
|
PixelShaderCache::SetPSSampler("samp0", 0);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2009-01-11 22:25:57 +00:00
|
|
|
glViewport(0, 0, (GLsizei)dstWidth, (GLsizei)dstHeight);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2009-02-28 16:33:59 +00:00
|
|
|
// Draw...
|
2008-12-08 05:25:12 +00:00
|
|
|
glBegin(GL_QUADS);
|
2011-07-15 02:17:14 +00:00
|
|
|
glTexCoord2f((float)sourceRc.left, (float)sourceRc.top); glVertex2f(-1,-1);
|
2008-12-08 05:25:12 +00:00
|
|
|
glTexCoord2f((float)sourceRc.left, (float)sourceRc.bottom); glVertex2f(-1,1);
|
2011-07-15 02:17:14 +00:00
|
|
|
glTexCoord2f((float)sourceRc.right, (float)sourceRc.bottom); glVertex2f(1,1);
|
|
|
|
glTexCoord2f((float)sourceRc.right, (float)sourceRc.top); glVertex2f(1,-1);
|
|
|
|
glEnd();
|
2008-12-08 05:25:12 +00:00
|
|
|
GL_REPORT_ERRORD();
|
|
|
|
|
2010-11-07 04:28:33 +00:00
|
|
|
// .. and then read back the results.
|
2009-01-11 22:25:57 +00:00
|
|
|
// TODO: make this less slow.
|
2009-11-15 20:14:03 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
int writeStride = bpmem.copyMipMapStrideChannels * 32;
|
2009-11-15 20:14:03 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
if (writeStride != readStride && toTexture)
|
|
|
|
{
|
|
|
|
// writing to a texture of a different size
|
|
|
|
|
|
|
|
int readHeight = readStride / dstWidth;
|
|
|
|
readHeight /= 4; // 4 bytes per pixel
|
|
|
|
|
|
|
|
int readStart = 0;
|
|
|
|
int readLoops = dstHeight / readHeight;
|
|
|
|
for (int i = 0; i < readLoops; i++)
|
|
|
|
{
|
|
|
|
glReadPixels(0, readStart, (GLsizei)dstWidth, (GLsizei)readHeight, GL_BGRA, GL_UNSIGNED_BYTE, destAddr);
|
|
|
|
readStart += readHeight;
|
|
|
|
destAddr += writeStride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
glReadPixels(0, 0, (GLsizei)dstWidth, (GLsizei)dstHeight, GL_BGRA, GL_UNSIGNED_BYTE, destAddr);
|
2009-11-15 20:14:03 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2011-07-15 02:17:14 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2011-12-30 00:00:34 +00:00
|
|
|
int EncodeToRamFromTexture(u32 address,GLuint source_texture, bool bFromZBuffer, bool bIsIntensityFmt, u32 copyfmt, int bScaleByHalf, const EFBRectangle& source)
|
2010-07-12 19:30:25 +00:00
|
|
|
{
|
|
|
|
u32 format = copyfmt;
|
|
|
|
|
|
|
|
if (bFromZBuffer)
|
|
|
|
{
|
|
|
|
format |= _GX_TF_ZTF;
|
|
|
|
if (copyfmt == 11)
|
|
|
|
format = GX_TF_Z16;
|
|
|
|
else if (format < GX_TF_Z8 || format > GX_TF_Z24X8)
|
|
|
|
format |= _GX_TF_CTF;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (copyfmt > GX_TF_RGBA8 || (copyfmt < GX_TF_RGB565 && !bIsIntensityFmt))
|
|
|
|
format |= _GX_TF_CTF;
|
|
|
|
|
|
|
|
FRAGMENTSHADER& texconv_shader = GetOrCreateEncodingShader(format);
|
|
|
|
if (texconv_shader.glprogid == 0)
|
|
|
|
return 0;
|
|
|
|
|
2011-01-31 01:28:32 +00:00
|
|
|
u8 *dest_ptr = Memory::GetPointer(address);
|
2010-07-12 19:30:25 +00:00
|
|
|
|
|
|
|
int width = (source.right - source.left) >> bScaleByHalf;
|
|
|
|
int height = (source.bottom - source.top) >> bScaleByHalf;
|
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
int size_in_bytes = TexDecoder_GetTextureSizeInBytes(width, height, format);
|
|
|
|
|
2010-07-12 19:30:25 +00:00
|
|
|
u16 blkW = TexDecoder_GetBlockWidthInTexels(format) - 1;
|
|
|
|
u16 blkH = TexDecoder_GetBlockHeightInTexels(format) - 1;
|
|
|
|
u16 samples = TextureConversionShader::GetEncodedSampleCount(format);
|
|
|
|
|
|
|
|
// only copy on cache line boundaries
|
|
|
|
// extra pixels are copied but not displayed in the resulting texture
|
|
|
|
s32 expandedWidth = (width + blkW) & (~blkW);
|
|
|
|
s32 expandedHeight = (height + blkH) & (~blkH);
|
|
|
|
|
2011-12-08 05:20:55 +00:00
|
|
|
if(g_ActiveConfig.bUseGLSL)
|
|
|
|
ProgramShaderCache::SetBothShaders(texconv_shader.glprogid, 0);
|
|
|
|
else
|
|
|
|
PixelShaderCache::SetCurrentShader(texconv_shader.glprogid);
|
|
|
|
|
2010-12-20 17:06:39 +00:00
|
|
|
float sampleStride = bScaleByHalf ? 2.f : 1.f;
|
2010-12-10 15:54:14 +00:00
|
|
|
TextureConversionShader::SetShaderParameters((float)expandedWidth,
|
|
|
|
(float)Renderer::EFBToScaledY(expandedHeight), // TODO: Why do we scale this?
|
|
|
|
(float)Renderer::EFBToScaledX(source.left),
|
|
|
|
(float)Renderer::EFBToScaledY(EFB_HEIGHT - source.top - expandedHeight),
|
|
|
|
Renderer::EFBToScaledXf(sampleStride),
|
|
|
|
Renderer::EFBToScaledYf(sampleStride));
|
2010-07-12 19:30:25 +00:00
|
|
|
|
|
|
|
TargetRectangle scaledSource;
|
|
|
|
scaledSource.top = 0;
|
|
|
|
scaledSource.bottom = expandedHeight;
|
|
|
|
scaledSource.left = 0;
|
|
|
|
scaledSource.right = expandedWidth / samples;
|
2010-09-28 02:15:02 +00:00
|
|
|
int cacheBytes = 32;
|
2010-12-20 17:06:39 +00:00
|
|
|
if ((format & 0x0f) == 6)
|
|
|
|
cacheBytes = 64;
|
|
|
|
|
|
|
|
int readStride = (expandedWidth * cacheBytes) /
|
|
|
|
TexDecoder_GetBlockWidthInTexels(format);
|
2011-12-08 05:20:55 +00:00
|
|
|
EncodeToRamUsingShader(source_texture, scaledSource,
|
2010-12-20 17:06:39 +00:00
|
|
|
dest_ptr, expandedWidth / samples, expandedHeight, readStride,
|
|
|
|
true, bScaleByHalf > 0 && !bFromZBuffer);
|
2011-12-30 00:00:34 +00:00
|
|
|
return size_in_bytes; // TODO: D3D11 is calculating this value differently!
|
2009-11-15 20:14:03 +00:00
|
|
|
|
2009-01-11 22:25:57 +00:00
|
|
|
}
|
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
void EncodeToRamYUYV(GLuint srcTexture, const TargetRectangle& sourceRc, u8* destAddr, int dstWidth, int dstHeight)
|
2009-01-11 22:25:57 +00:00
|
|
|
{
|
2010-11-18 02:21:26 +00:00
|
|
|
g_renderer->ResetAPIState();
|
2011-12-08 05:20:55 +00:00
|
|
|
|
|
|
|
if(g_ActiveConfig.bUseGLSL)
|
|
|
|
ProgramShaderCache::SetBothShaders(s_rgbToYuyvProgram.glprogid, 0);
|
|
|
|
else
|
|
|
|
PixelShaderCache::SetCurrentShader(s_rgbToYuyvProgram.glprogid);
|
|
|
|
|
|
|
|
EncodeToRamUsingShader(srcTexture, sourceRc, destAddr, dstWidth / 2, dstHeight, 0, false, false);
|
2010-11-14 23:31:53 +00:00
|
|
|
FramebufferManager::SetFramebuffer(0);
|
2011-07-15 02:17:14 +00:00
|
|
|
VertexShaderManager::SetViewportChanged();
|
2010-07-12 19:30:25 +00:00
|
|
|
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
|
2010-09-28 02:15:02 +00:00
|
|
|
TextureCache::DisableStage(0);
|
2010-11-18 02:21:26 +00:00
|
|
|
g_renderer->RestoreAPIState();
|
2011-07-15 02:17:14 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2009-01-11 22:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-28 19:02:37 +00:00
|
|
|
// Should be scale free.
|
2009-06-29 07:30:48 +00:00
|
|
|
void DecodeToTexture(u32 xfbAddr, int srcWidth, int srcHeight, GLuint destTexture)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2011-01-31 01:28:32 +00:00
|
|
|
u8* srcAddr = Memory::GetPointer(xfbAddr);
|
2009-06-29 07:30:48 +00:00
|
|
|
if (!srcAddr)
|
|
|
|
{
|
|
|
|
WARN_LOG(VIDEO, "Tried to decode from invalid memory address");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-05 00:01:18 +00:00
|
|
|
int srcFmtWidth = srcWidth / 2;
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2010-11-18 02:21:26 +00:00
|
|
|
g_renderer->ResetAPIState(); // reset any game specific settings
|
2010-09-30 15:24:34 +00:00
|
|
|
|
2010-11-07 04:28:33 +00:00
|
|
|
// switch to texture converter frame buffer
|
2008-12-08 05:25:12 +00:00
|
|
|
// attach destTexture as color destination
|
2010-11-14 23:31:53 +00:00
|
|
|
FramebufferManager::SetFramebuffer(s_texConvFrameBuffer);
|
2011-07-15 02:17:14 +00:00
|
|
|
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, destTexture);
|
2008-12-08 05:25:12 +00:00
|
|
|
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_ARB, destTexture, 0);
|
|
|
|
|
2009-06-26 08:57:53 +00:00
|
|
|
GL_REPORT_FBO_ERROR();
|
|
|
|
|
2009-03-22 11:21:44 +00:00
|
|
|
for (int i = 1; i < 8; ++i)
|
2010-09-28 02:15:02 +00:00
|
|
|
TextureCache::DisableStage(i);
|
2009-03-22 11:21:44 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
// activate source texture
|
|
|
|
// set srcAddr as data for source texture
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
2009-03-22 11:21:44 +00:00
|
|
|
glEnable(GL_TEXTURE_RECTANGLE_ARB);
|
2008-12-08 05:25:12 +00:00
|
|
|
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, s_srcTexture);
|
|
|
|
|
2009-02-21 13:53:26 +00:00
|
|
|
// TODO: make this less slow. (How?)
|
2010-02-20 04:18:19 +00:00
|
|
|
if((GLsizei)s_srcTextureWidth == (GLsizei)srcFmtWidth && (GLsizei)s_srcTextureHeight == (GLsizei)srcHeight)
|
2009-09-26 12:39:12 +00:00
|
|
|
{
|
2011-07-15 02:17:14 +00:00
|
|
|
glTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB, 0,0,0,s_srcTextureWidth, s_srcTextureHeight,
|
|
|
|
GL_BGRA, GL_UNSIGNED_BYTE, srcAddr);
|
2009-09-26 12:39:12 +00:00
|
|
|
}
|
|
|
|
else
|
2011-07-15 02:17:14 +00:00
|
|
|
{
|
|
|
|
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, (GLsizei)srcFmtWidth, (GLsizei)srcHeight,
|
|
|
|
0, GL_BGRA, GL_UNSIGNED_BYTE, srcAddr);
|
2009-09-26 12:39:12 +00:00
|
|
|
s_srcTextureWidth = (GLsizei)srcFmtWidth;
|
|
|
|
s_srcTextureHeight = (GLsizei)srcHeight;
|
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
glViewport(0, 0, srcWidth, srcHeight);
|
2011-12-10 21:40:10 +00:00
|
|
|
if(g_ActiveConfig.bUseGLSL)
|
|
|
|
ProgramShaderCache::SetBothShaders(s_yuyvToRgbProgram.glprogid, 0);
|
|
|
|
else
|
|
|
|
PixelShaderCache::SetCurrentShader(s_yuyvToRgbProgram.glprogid);
|
2011-07-15 02:17:14 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
GL_REPORT_ERRORD();
|
|
|
|
|
2011-07-15 02:17:14 +00:00
|
|
|
glBegin(GL_QUADS);
|
2010-12-10 15:54:14 +00:00
|
|
|
glTexCoord2f((float)srcFmtWidth, (float)srcHeight); glVertex2f(1,-1);
|
|
|
|
glTexCoord2f((float)srcFmtWidth, 0); glVertex2f(1,1);
|
2008-12-08 05:25:12 +00:00
|
|
|
glTexCoord2f(0, 0); glVertex2f(-1,1);
|
2008-12-26 12:47:32 +00:00
|
|
|
glTexCoord2f(0, (float)srcHeight); glVertex2f(-1,-1);
|
2011-07-15 02:17:14 +00:00
|
|
|
glEnd();
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
// reset state
|
|
|
|
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
|
2009-06-26 08:57:53 +00:00
|
|
|
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_ARB, 0, 0);
|
2010-09-28 02:15:02 +00:00
|
|
|
TextureCache::DisableStage(0);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2008-12-26 10:43:18 +00:00
|
|
|
VertexShaderManager::SetViewportChanged();
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
FramebufferManager::SetFramebuffer(0);
|
2009-06-26 08:57:53 +00:00
|
|
|
|
2010-11-18 02:21:26 +00:00
|
|
|
g_renderer->RestoreAPIState();
|
2011-07-15 02:17:14 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2009-02-21 13:53:26 +00:00
|
|
|
} // namespace
|
2011-01-29 20:16:51 +00:00
|
|
|
|
2011-01-29 22:48:33 +00:00
|
|
|
} // namespace OGL
|