2012-11-01 15:19:01 +00:00
// Copyright (c) 2012- PPSSPP Project.
// 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
2012-11-04 22:01:49 +00:00
// the Free Software Foundation, version 2.0 or later versions.
2012-11-01 15:19:01 +00:00
// 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 git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
# include "gfx_es2/glsl_program.h"
2012-11-24 14:19:29 +00:00
# include "gfx_es2/gl_state.h"
2013-01-30 20:09:53 +00:00
# include "gfx_es2/fbo.h"
2012-11-01 15:19:01 +00:00
# include "math/lin/matrix4x4.h"
2013-01-30 20:09:53 +00:00
# include "Core/Host.h"
# include "Core/MemMap.h"
# include "Core/Config.h"
# include "Core/System.h"
2013-09-11 20:21:15 +00:00
# include "Core/Reporting.h"
2013-01-30 20:09:53 +00:00
# include "GPU/ge_constants.h"
# include "GPU/GPUState.h"
2012-11-01 15:19:01 +00:00
2013-01-30 20:09:53 +00:00
# include "GPU/GLES/Framebuffer.h"
2013-01-31 23:18:23 +00:00
# include "GPU/GLES/TextureCache.h"
2013-03-15 23:40:37 +00:00
# include "GPU/GLES/ShaderManager.h"
2012-11-01 15:19:01 +00:00
2013-09-17 04:48:14 +00:00
# include <algorithm>
2013-06-26 21:23:16 +00:00
# if defined(USING_GLES2)
# define GL_READ_FRAMEBUFFER GL_FRAMEBUFFER
# define GL_DRAW_FRAMEBUFFER GL_FRAMEBUFFER
# define GL_RGBA8 GL_RGBA
# ifndef GL_DEPTH_COMPONENT24
# define GL_DEPTH_COMPONENT24 GL_DEPTH_COMPONENT24_OES
# endif
# ifndef GL_DEPTH24_STENCIL8_OES
# define GL_DEPTH24_STENCIL8_OES 0x88F0
# endif
# endif
2013-06-29 19:35:28 +00:00
extern int g_iNumVideos ;
2013-06-26 21:23:16 +00:00
2013-01-30 20:09:53 +00:00
static const char tex_fs [ ] =
2012-11-01 15:19:01 +00:00
" #ifdef GL_ES \n "
" precision mediump float; \n "
" #endif \n "
" uniform sampler2D sampler0; \n "
" varying vec2 v_texcoord0; \n "
" void main() { \n "
2013-07-01 12:22:19 +00:00
" gl_FragColor.rgb = texture2D(sampler0, v_texcoord0).rgb; \n "
2013-06-16 00:32:35 +00:00
" gl_FragColor.a = 1.0; \n "
2012-11-01 15:19:01 +00:00
" } \n " ;
2013-01-30 20:09:53 +00:00
static const char basic_vs [ ] =
2012-12-21 10:24:38 +00:00
# ifndef USING_GLES2
" #version 120 \n "
# endif
2012-11-01 15:19:01 +00:00
" attribute vec4 a_position; \n "
" attribute vec2 a_texcoord0; \n "
" uniform mat4 u_viewproj; \n "
" varying vec2 v_texcoord0; \n "
" void main() { \n "
2012-12-21 10:24:38 +00:00
" v_texcoord0 = a_texcoord0; \n "
" gl_Position = u_viewproj * a_position; \n "
2012-11-01 15:19:01 +00:00
" } \n " ;
2013-01-30 20:09:53 +00:00
// Aggressively delete unused FBO:s to save gpu memory.
enum {
2013-04-12 14:00:59 +00:00
FBO_OLD_AGE = 5 ,
2013-01-30 20:09:53 +00:00
} ;
static bool MaskedEqual ( u32 addr1 , u32 addr2 ) {
2013-08-23 06:23:48 +00:00
return ( addr1 & 0x03FFFFFF ) = = ( addr2 & 0x03FFFFFF ) ;
2013-01-30 20:09:53 +00:00
}
2013-07-04 14:24:37 +00:00
inline u16 RGBA8888toRGB565 ( u32 px ) {
2013-06-28 13:48:36 +00:00
return ( ( px > > 3 ) & 0x001F ) | ( ( px > > 5 ) & 0x07E0 ) | ( ( px > > 8 ) & 0xF800 ) ;
}
2013-07-04 14:24:37 +00:00
inline u16 RGBA8888toRGBA4444 ( u32 px ) {
2013-06-28 13:48:36 +00:00
return ( ( px > > 4 ) & 0x000F ) | ( ( px > > 8 ) & 0x00F0 ) | ( ( px > > 12 ) & 0x0F00 ) | ( ( px > > 16 ) & 0xF000 ) ;
}
2013-07-04 14:24:37 +00:00
inline u16 RGBA8888toRGBA5551 ( u32 px ) {
2013-06-28 13:48:36 +00:00
return ( ( px > > 3 ) & 0x001F ) | ( ( px > > 6 ) & 0x03E0 ) | ( ( px > > 9 ) & 0x7C00 ) | ( ( px > > 16 ) & 0x8000 ) ;
}
2013-07-30 06:05:59 +00:00
void ConvertFromRGBA8888 ( u8 * dst , u8 * src , u32 stride , u32 height , GEBufferFormat format ) ;
2013-06-28 13:48:36 +00:00
2013-03-15 20:22:17 +00:00
void CenterRect ( float * x , float * y , float * w , float * h ,
float origW , float origH , float frameW , float frameH )
2013-02-12 21:09:14 +00:00
{
2013-02-13 17:21:21 +00:00
if ( g_Config . bStretchToDisplay )
{
* x = 0 ;
* y = 0 ;
* w = frameW ;
* h = frameH ;
return ;
}
2013-02-12 21:09:14 +00:00
float origRatio = origW / origH ;
float frameRatio = frameW / frameH ;
if ( origRatio > frameRatio )
{
// Image is wider than frame. Center vertically.
float scale = origW / frameW ;
* x = 0.0f ;
* w = frameW ;
* h = frameW / origRatio ;
2013-06-21 05:40:52 +00:00
# ifdef BLACKBERRY
2013-05-08 13:22:45 +00:00
// Stretch a little bit
if ( g_Config . bPartialStretch )
* h = ( frameH + * h ) / 2.0f ; // (408 + 720) / 2 = 564
# endif
2013-02-12 21:09:14 +00:00
* y = ( frameH - * h ) / 2.0f ;
}
else
{
// Image is taller than frame. Center horizontally.
float scale = origH / frameH ;
* y = 0.0f ;
* h = frameH ;
* w = frameH * origRatio ;
* x = ( frameW - * w ) / 2.0f ;
}
}
2013-08-14 19:32:43 +00:00
void ClearBuffer ( ) {
2013-08-14 19:20:06 +00:00
glstate . depthWrite . set ( GL_TRUE ) ;
glstate . colorMask . set ( GL_TRUE , GL_TRUE , GL_TRUE , GL_TRUE ) ;
glClearColor ( 0 , 0 , 0 , 1 ) ;
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT ) ;
}
2013-08-14 19:32:43 +00:00
void DisableState ( ) {
glstate . blend . disable ( ) ;
glstate . cullFace . disable ( ) ;
glstate . depthTest . disable ( ) ;
glstate . scissorTest . disable ( ) ;
glstate . stencilTest . disable ( ) ;
2013-08-23 09:32:10 +00:00
# if !defined(USING_GLES2)
glstate . colorLogicOp . disable ( ) ;
# endif
2013-08-14 19:32:43 +00:00
}
2013-09-28 20:42:13 +00:00
void FramebufferManager : : SetNumExtraFBOs ( int num ) {
2013-09-30 03:28:19 +00:00
for ( size_t i = 0 ; i < extraFBOs_ . size ( ) ; i + + ) {
2013-09-28 20:42:13 +00:00
fbo_destroy ( extraFBOs_ [ i ] ) ;
}
extraFBOs_ . clear ( ) ;
for ( int i = 0 ; i < num ; i + + ) {
// No depth/stencil for post processing
FBO * fbo = fbo_create ( PSP_CoreParameter ( ) . renderWidth , PSP_CoreParameter ( ) . renderHeight , 1 , false , FBO_8888 ) ;
extraFBOs_ . push_back ( fbo ) ;
}
}
2013-07-16 20:50:53 +00:00
void FramebufferManager : : CompileDraw2DProgram ( ) {
2013-09-26 10:41:07 +00:00
if ( ! draw2dprogram_ ) {
2013-09-28 20:42:13 +00:00
SetNumExtraFBOs ( 0 ) ;
draw2dprogram_ = glsl_create_source ( basic_vs , tex_fs ) ;
2013-09-26 10:41:07 +00:00
glsl_bind ( draw2dprogram_ ) ;
glUniform1i ( draw2dprogram_ - > sampler0 , 0 ) ;
if ( g_Config . bFXAA ) {
2013-09-28 20:42:13 +00:00
useFXAA_ = true ;
fxaaProgram_ = glsl_create ( " shaders/fxaa.vsh " , " shaders/fxaa.fsh " ) ;
glsl_bind ( fxaaProgram_ ) ;
glUniform1i ( fxaaProgram_ - > sampler0 , 0 ) ;
SetNumExtraFBOs ( 1 ) ;
float u_delta = 1.0f / PSP_CoreParameter ( ) . renderWidth ;
float v_delta = 1.0f / PSP_CoreParameter ( ) . renderHeight ;
glUniform2f ( glsl_uniform_loc ( fxaaProgram_ , " u_texcoordDelta " ) , u_delta , v_delta ) ;
} else {
fxaaProgram_ = 0 ;
useFXAA_ = false ;
2013-09-26 10:41:07 +00:00
}
2013-07-16 20:50:53 +00:00
glsl_unbind ( ) ;
}
}
2013-09-26 10:41:07 +00:00
void FramebufferManager : : DestroyDraw2DProgram ( ) {
if ( draw2dprogram_ ) {
glsl_destroy ( draw2dprogram_ ) ;
draw2dprogram_ = 0 ;
2013-09-28 20:42:13 +00:00
glsl_destroy ( fxaaProgram_ ) ;
fxaaProgram_ = 0 ;
2013-09-26 10:41:07 +00:00
}
}
2013-01-30 20:09:53 +00:00
FramebufferManager : : FramebufferManager ( ) :
displayFramebufPtr_ ( 0 ) ,
2013-07-06 09:09:08 +00:00
displayStride_ ( 0 ) ,
2013-07-30 06:05:59 +00:00
displayFormat_ ( GE_FORMAT_565 ) ,
2013-07-06 09:09:08 +00:00
displayFramebuf_ ( 0 ) ,
2013-01-30 20:09:53 +00:00
prevDisplayFramebuf_ ( 0 ) ,
2013-01-31 23:18:23 +00:00
prevPrevDisplayFramebuf_ ( 0 ) ,
2013-04-12 14:00:59 +00:00
frameLastFramebufUsed ( 0 ) ,
2013-06-05 21:03:23 +00:00
currentRenderVfb_ ( 0 ) ,
drawPixelsTex_ ( 0 ) ,
2013-07-30 06:05:59 +00:00
drawPixelsTexFormat_ ( GE_FORMAT_INVALID ) ,
2013-07-16 20:50:53 +00:00
convBuf ( 0 ) ,
2013-09-28 20:42:13 +00:00
draw2dprogram_ ( 0 ) ,
fxaaProgram_ ( 0 ) ,
textureCache_ ( 0 ) ,
shaderManager_ ( 0 ) ,
useFXAA_ ( false )
2013-07-05 22:40:01 +00:00
# ifndef USING_GLES2
,
2013-06-28 13:48:36 +00:00
pixelBufObj_ ( 0 ) ,
currentPBO_ ( 0 )
2013-07-05 22:40:01 +00:00
# endif
2013-01-30 20:09:53 +00:00
{
2013-07-16 20:50:53 +00:00
CompileDraw2DProgram ( ) ;
2012-11-01 15:19:01 +00:00
2012-11-18 12:04:49 +00:00
// And an initial clear. We don't clear per frame as the games are supposed to handle that
// by themselves.
2013-08-14 19:32:43 +00:00
ClearBuffer ( ) ;
2013-01-14 18:26:10 +00:00
2013-08-14 19:20:06 +00:00
useBufferedRendering_ = g_Config . iRenderingMode ! = FB_NON_BUFFERED_MODE ;
2013-07-05 01:31:31 +00:00
// Check vendor string to try and guess GPU
const char * cvendor = ( char * ) glGetString ( GL_VENDOR ) ;
if ( cvendor ) {
const std : : string vendor ( cvendor ) ;
if ( vendor = = " NVIDIA Corporation "
| | vendor = = " Nouveau "
| | vendor = = " nouveau " ) {
gpuVendor = GPU_VENDOR_NVIDIA ;
} else if ( vendor = = " Advanced Micro Devices, Inc. "
| | vendor = = " ATI Technologies Inc. " ) {
gpuVendor = GPU_VENDOR_AMD ;
} else if ( vendor = = " Intel "
| | vendor = = " Intel Inc. "
| | vendor = = " Intel Corporation "
| | vendor = = " Tungsten Graphics, Inc " ) { // We'll assume this last one means Intel
gpuVendor = GPU_VENDOR_INTEL ;
2013-07-20 17:14:36 +00:00
} else if ( vendor = = " ARM " )
2013-07-05 01:31:31 +00:00
gpuVendor = GPU_VENDOR_ARM ;
2013-07-20 17:14:36 +00:00
else if ( vendor = = " Imagination Technologies " )
gpuVendor = GPU_VENDOR_POWERVR ;
else if ( vendor = = " Qualcomm " )
gpuVendor = GPU_VENDOR_ADRENO ;
else
2013-07-05 01:31:31 +00:00
gpuVendor = GPU_VENDOR_UNKNOWN ;
2013-07-20 17:14:36 +00:00
} else
2013-07-05 01:31:31 +00:00
gpuVendor = GPU_VENDOR_UNKNOWN ;
2013-07-20 17:14:36 +00:00
gstate_c . gpuVendor = gpuVendor ;
2013-09-07 20:02:55 +00:00
NOTICE_LOG ( SCEGE , " GPU Vendor : %s " , cvendor ) ;
2012-11-01 15:19:01 +00:00
}
2012-12-01 01:15:46 +00:00
FramebufferManager : : ~ FramebufferManager ( ) {
2013-06-05 21:03:23 +00:00
if ( drawPixelsTex_ )
glDeleteTextures ( 1 , & drawPixelsTex_ ) ;
2013-09-26 10:41:07 +00:00
if ( draw2dprogram_ ) {
glsl_destroy ( draw2dprogram_ ) ;
2013-07-16 20:50:53 +00:00
}
2013-09-28 20:42:13 +00:00
SetNumExtraFBOs ( 0 ) ;
2013-06-28 13:48:36 +00:00
2013-07-05 22:40:01 +00:00
# ifndef USING_GLES2
2013-06-28 13:48:36 +00:00
delete [ ] pixelBufObj_ ;
2013-07-05 22:40:01 +00:00
# endif
2012-12-01 01:15:46 +00:00
delete [ ] convBuf ;
2012-11-01 15:19:01 +00:00
}
2013-07-30 06:05:59 +00:00
void FramebufferManager : : DrawPixels ( const u8 * framebuf , GEBufferFormat pixelFormat , int linesize ) {
2013-06-05 21:03:23 +00:00
if ( drawPixelsTex_ & & drawPixelsTexFormat_ ! = pixelFormat ) {
glDeleteTextures ( 1 , & drawPixelsTex_ ) ;
drawPixelsTex_ = 0 ;
}
if ( ! drawPixelsTex_ ) {
glGenTextures ( 1 , & drawPixelsTex_ ) ;
// Initialize backbuffer texture for DrawPixels
glBindTexture ( GL_TEXTURE_2D , drawPixelsTex_ ) ;
glPixelStorei ( GL_UNPACK_ALIGNMENT , 1 ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_CLAMP_TO_EDGE ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_CLAMP_TO_EDGE ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_NEAREST ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST ) ;
2013-06-05 21:26:51 +00:00
glTexImage2D ( GL_TEXTURE_2D , 0 , GL_RGBA , 512 , 272 , 0 , GL_RGBA , GL_UNSIGNED_BYTE , 0 ) ;
2013-06-05 21:03:23 +00:00
glBindTexture ( GL_TEXTURE_2D , 0 ) ;
drawPixelsTexFormat_ = pixelFormat ;
}
2013-06-05 21:26:51 +00:00
// TODO: We can just change the texture format and flip some bits around instead of this.
2013-07-30 05:58:37 +00:00
if ( pixelFormat ! = GE_FORMAT_8888 | | linesize ! = 512 ) {
2013-06-05 21:26:51 +00:00
if ( ! convBuf ) {
convBuf = new u8 [ 512 * 272 * 4 ] ;
}
for ( int y = 0 ; y < 272 ; y + + ) {
switch ( pixelFormat ) {
2013-07-30 05:58:37 +00:00
case GE_FORMAT_565 :
2012-11-01 15:19:01 +00:00
{
2013-06-05 21:26:51 +00:00
const u16 * src = ( const u16 * ) framebuf + linesize * y ;
u8 * dst = convBuf + 4 * 512 * y ;
for ( int x = 0 ; x < 480 ; x + + )
{
u16 col = src [ x ] ;
dst [ x * 4 ] = ( ( col ) & 0x1f ) < < 3 ;
dst [ x * 4 + 1 ] = ( ( col > > 5 ) & 0x3f ) < < 2 ;
dst [ x * 4 + 2 ] = ( ( col > > 11 ) & 0x1f ) < < 3 ;
dst [ x * 4 + 3 ] = 255 ;
}
2012-11-01 15:19:01 +00:00
}
2013-06-05 21:26:51 +00:00
break ;
2012-11-01 15:19:01 +00:00
2013-07-30 05:58:37 +00:00
case GE_FORMAT_5551 :
2012-11-01 15:19:01 +00:00
{
2013-06-05 21:26:51 +00:00
const u16 * src = ( const u16 * ) framebuf + linesize * y ;
u8 * dst = convBuf + 4 * 512 * y ;
for ( int x = 0 ; x < 480 ; x + + )
{
u16 col = src [ x ] ;
dst [ x * 4 ] = ( ( col ) & 0x1f ) < < 3 ;
dst [ x * 4 + 1 ] = ( ( col > > 5 ) & 0x1f ) < < 3 ;
dst [ x * 4 + 2 ] = ( ( col > > 10 ) & 0x1f ) < < 3 ;
dst [ x * 4 + 3 ] = ( col > > 15 ) ? 255 : 0 ;
}
2012-11-01 15:19:01 +00:00
}
2013-06-05 21:26:51 +00:00
break ;
2012-11-01 15:19:01 +00:00
2013-07-30 05:58:37 +00:00
case GE_FORMAT_4444 :
2012-11-01 15:19:01 +00:00
{
2013-06-05 21:26:51 +00:00
const u16 * src = ( const u16 * ) framebuf + linesize * y ;
u8 * dst = convBuf + 4 * 512 * y ;
for ( int x = 0 ; x < 480 ; x + + )
{
u16 col = src [ x ] ;
dst [ x * 4 ] = ( ( col > > 8 ) & 0xf ) < < 4 ;
dst [ x * 4 + 1 ] = ( ( col > > 4 ) & 0xf ) < < 4 ;
dst [ x * 4 + 2 ] = ( col & 0xf ) < < 4 ;
dst [ x * 4 + 3 ] = ( col > > 12 ) < < 4 ;
}
2012-11-01 15:19:01 +00:00
}
2013-06-05 21:26:51 +00:00
break ;
2012-11-01 15:19:01 +00:00
2013-07-30 05:58:37 +00:00
case GE_FORMAT_8888 :
2012-11-01 15:19:01 +00:00
{
2013-06-05 21:26:51 +00:00
const u8 * src = framebuf + linesize * 4 * y ;
u8 * dst = convBuf + 4 * 512 * y ;
memcpy ( dst , src , 4 * 480 ) ;
2012-11-01 15:19:01 +00:00
}
2013-06-05 21:26:51 +00:00
break ;
2013-08-13 06:40:22 +00:00
case GE_FORMAT_INVALID :
_dbg_assert_msg_ ( G3D , false , " Invalid pixelFormat passed to DrawPixels(). " ) ;
break ;
2012-11-01 15:19:01 +00:00
}
}
}
2013-10-01 14:20:09 +00:00
float x , y , w , h ;
CenterRect ( & x , & y , & w , & h , 480.0f , 272.0f , ( float ) PSP_CoreParameter ( ) . pixelWidth , ( float ) PSP_CoreParameter ( ) . pixelHeight ) ;
2013-06-05 21:03:23 +00:00
glBindTexture ( GL_TEXTURE_2D , drawPixelsTex_ ) ;
2013-07-23 11:30:01 +00:00
if ( g_Config . iTexFiltering = = LINEAR | | ( g_Config . iTexFiltering = = LINEARFMV & & g_iNumVideos ) )
2013-06-01 17:28:23 +00:00
{
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR ) ;
}
2013-07-30 05:58:37 +00:00
glTexSubImage2D ( GL_TEXTURE_2D , 0 , 0 , 0 , 512 , 272 , GL_RGBA , GL_UNSIGNED_BYTE , pixelFormat = = GE_FORMAT_8888 ? framebuf : convBuf ) ;
2013-02-12 21:09:14 +00:00
2013-09-28 20:42:13 +00:00
DrawActiveTexture ( x , y , w , h , ( float ) PSP_CoreParameter ( ) . pixelWidth , ( float ) PSP_CoreParameter ( ) . pixelHeight , false , 480.0f / 512.0f ) ;
2012-11-19 22:29:14 +00:00
}
2013-09-28 20:42:13 +00:00
void FramebufferManager : : DrawActiveTexture ( float x , float y , float w , float h , float destW , float destH , bool flip , float uscale , float vscale , GLSLProgram * program ) {
2013-06-05 21:26:51 +00:00
float u2 = uscale ;
2013-07-01 02:32:27 +00:00
// Since we're flipping, 0 is down. That's where the scale goes.
float v1 = flip ? 1.0f : 1.0f - vscale ;
float v2 = flip ? 1.0f - vscale : 1.0f ;
2012-11-19 22:29:14 +00:00
2013-07-01 20:51:24 +00:00
const float pos [ 12 ] = { x , y , 0 , x + w , y , 0 , x + w , y + h , 0 , x , y + h , 0 } ;
const float texCoords [ 8 ] = { 0 , v1 , u2 , v1 , u2 , v2 , 0 , v2 } ;
const GLubyte indices [ 4 ] = { 0 , 1 , 3 , 2 } ;
2013-07-16 20:50:53 +00:00
2013-09-28 20:42:13 +00:00
if ( ! draw2dprogram_ ) {
2013-07-16 20:50:53 +00:00
CompileDraw2DProgram ( ) ;
2013-09-28 20:42:13 +00:00
}
if ( ! program ) {
2013-09-26 10:41:07 +00:00
program = draw2dprogram_ ;
2013-06-28 13:48:36 +00:00
}
glsl_bind ( program ) ;
2012-11-19 22:29:14 +00:00
Matrix4x4 ortho ;
2013-09-28 20:42:13 +00:00
ortho . setOrtho ( 0 , destW , destH , 0 , - 1 , 1 ) ;
2013-06-28 13:48:36 +00:00
glUniformMatrix4fv ( program - > u_viewproj , 1 , GL_FALSE , ortho . getReadPtr ( ) ) ;
2013-01-10 22:49:33 +00:00
glBindBuffer ( GL_ARRAY_BUFFER , 0 ) ;
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , 0 ) ;
2013-06-28 13:48:36 +00:00
glEnableVertexAttribArray ( program - > a_position ) ;
glEnableVertexAttribArray ( program - > a_texcoord0 ) ;
glVertexAttribPointer ( program - > a_position , 3 , GL_FLOAT , GL_FALSE , 12 , pos ) ;
2013-07-01 20:51:24 +00:00
glVertexAttribPointer ( program - > a_texcoord0 , 2 , GL_FLOAT , GL_FALSE , 8 , texCoords ) ;
//glDrawArrays(GL_TRIANGLE_FAN, 0, 4); // glDrawElements tested slightly faster on OpenGL atleast
glDrawElements ( GL_TRIANGLE_STRIP , 4 , GL_UNSIGNED_BYTE , indices ) ;
2013-06-28 13:48:36 +00:00
glDisableVertexAttribArray ( program - > a_position ) ;
glDisableVertexAttribArray ( program - > a_texcoord0 ) ;
2012-11-01 15:19:01 +00:00
glsl_unbind ( ) ;
}
2013-01-30 20:09:53 +00:00
2013-09-21 16:52:30 +00:00
VirtualFramebuffer * FramebufferManager : : GetVFBAt ( u32 addr ) {
2013-05-04 22:28:57 +00:00
VirtualFramebuffer * match = NULL ;
2013-06-23 15:51:35 +00:00
for ( size_t i = 0 ; i < vfbs_ . size ( ) ; + + i ) {
VirtualFramebuffer * v = vfbs_ [ i ] ;
2013-09-21 16:52:30 +00:00
if ( MaskedEqual ( v - > fb_address , addr ) & & v - > format = = displayFormat_ & & v - > width > = 480 ) {
2013-01-30 20:09:53 +00:00
// Could check w too but whatever
2013-09-01 18:55:03 +00:00
if ( match = = NULL | | match - > last_frame_render < v - > last_frame_render ) {
2013-05-04 22:28:57 +00:00
match = v ;
}
2013-01-30 20:09:53 +00:00
}
}
2013-05-04 22:28:57 +00:00
if ( match ! = NULL ) {
return match ;
}
2013-09-21 16:52:30 +00:00
DEBUG_LOG ( SCEGE , " Finding no FBO matching address %08x " , addr ) ;
2013-01-30 20:09:53 +00:00
#if 0 // defined(_DEBUG)
std : : string debug = " FBOs: " ;
2013-06-23 15:51:35 +00:00
for ( size_t i = 0 ; i < vfbs_ . size ( ) ; + + i ) {
2013-01-30 20:09:53 +00:00
char temp [ 256 ] ;
2013-06-23 15:51:35 +00:00
sprintf ( temp , " %08x %i %i " , vfbs_ [ i ] - > fb_address , vfbs_ [ i ] - > width , vfbs_ [ i ] - > height ) ;
2013-01-30 20:09:53 +00:00
debug + = std : : string ( temp ) ;
}
2013-09-07 20:02:55 +00:00
ERROR_LOG ( SCEGE , " FBOs: %s " , debug . c_str ( ) ) ;
2013-01-30 20:09:53 +00:00
# endif
return 0 ;
}
2013-03-15 23:40:37 +00:00
// Heuristics to figure out the size of FBO to create.
2013-09-15 10:46:14 +00:00
static void DrawingSize ( int & drawing_width , int & drawing_height ) {
2013-07-15 13:49:43 +00:00
int default_width = 480 ;
int default_height = 272 ;
2013-08-10 09:22:22 +00:00
int viewport_width = ( int ) gstate . getViewportX1 ( ) ;
int viewport_height = ( int ) gstate . getViewportY1 ( ) ;
2013-08-24 07:59:50 +00:00
int region_width = gstate . getRegionX2 ( ) + 1 ;
int region_height = gstate . getRegionY2 ( ) + 1 ;
int scissor_width = gstate . getScissorX2 ( ) + 1 ;
int scissor_height = gstate . getScissorY2 ( ) + 1 ;
2013-09-11 21:29:26 +00:00
int fb_stride = gstate . fbwidth & 0x3C0 ;
2013-08-10 09:22:22 +00:00
2013-09-11 21:29:26 +00:00
DEBUG_LOG ( SCEGE , " viewport : %ix%i, region : %ix%i , scissor: %ix%i, stride: %i, %i " , viewport_width , viewport_height , region_width , region_height , scissor_width , scissor_height , fb_stride , gstate . isModeThrough ( ) ) ;
2013-08-10 09:22:22 +00:00
2013-08-24 07:59:50 +00:00
// Viewport may return 0x0 for example FF Type-0 and we set it to 480x272
2013-08-10 09:22:22 +00:00
if ( viewport_width < = 1 & & viewport_height < = 1 ) {
2013-08-24 07:59:50 +00:00
viewport_width = default_width ;
viewport_height = default_height ;
}
2013-09-11 21:29:26 +00:00
if ( fb_stride > 0 & & fb_stride < 512 ) {
2013-08-24 07:59:50 +00:00
// Correct scissor size has to be used to render like character shadow in Mortal Kombat .
2013-09-11 21:29:26 +00:00
if ( fb_stride = = scissor_width & & region_width ! = scissor_width ) {
2013-08-24 07:59:50 +00:00
drawing_width = scissor_width ;
drawing_height = scissor_height ;
} else {
2013-08-10 09:22:22 +00:00
drawing_width = viewport_width ;
drawing_height = viewport_height ;
}
2013-07-07 02:39:22 +00:00
} else {
2013-08-24 07:59:50 +00:00
// Correct region size has to be used when fb_width equals to region_width for exmaple GTA/Midnight Club/MSG Peace Maker .
2013-09-14 13:03:24 +00:00
if ( fb_stride = = region_width & & region_width = = viewport_width ) {
2013-08-10 09:22:22 +00:00
drawing_width = region_width ;
drawing_height = region_height ;
2013-09-14 13:03:24 +00:00
} else if ( fb_stride = = viewport_width ) {
drawing_width = viewport_width ;
drawing_height = viewport_height ;
2013-08-24 07:59:50 +00:00
} else {
drawing_width = default_width ;
drawing_height = default_height ;
2013-08-10 09:22:22 +00:00
}
2013-07-15 13:49:43 +00:00
}
2013-01-31 23:18:23 +00:00
}
2013-06-23 15:16:22 +00:00
void FramebufferManager : : DestroyFramebuf ( VirtualFramebuffer * v ) {
2013-08-01 06:19:15 +00:00
textureCache_ - > NotifyFramebuffer ( v - > fb_address , v , NOTIFY_FB_DESTROYED ) ;
2013-06-23 15:16:22 +00:00
if ( v - > fbo ) {
fbo_destroy ( v - > fbo ) ;
v - > fbo = 0 ;
}
2013-06-23 15:05:59 +00:00
// Wipe some pointers
if ( currentRenderVfb_ = = v )
currentRenderVfb_ = 0 ;
if ( displayFramebuf_ = = v )
displayFramebuf_ = 0 ;
if ( prevDisplayFramebuf_ = = v )
prevDisplayFramebuf_ = 0 ;
if ( prevPrevDisplayFramebuf_ = = v )
prevPrevDisplayFramebuf_ = 0 ;
2013-06-23 15:16:22 +00:00
delete v ;
2013-06-23 15:05:59 +00:00
}
2013-01-30 20:09:53 +00:00
void FramebufferManager : : SetRenderFrameBuffer ( ) {
2013-04-21 22:08:47 +00:00
if ( ! gstate_c . framebufChanged & & currentRenderVfb_ ) {
2013-09-01 18:55:03 +00:00
currentRenderVfb_ - > last_frame_render = gpuStats . numFlips ;
2013-08-15 23:00:26 +00:00
currentRenderVfb_ - > dirtyAfterDisplay = true ;
if ( ! gstate_c . skipDrawReason )
currentRenderVfb_ - > reallyDirtyAfterDisplay = true ;
2013-04-21 22:08:47 +00:00
return ;
}
gstate_c . framebufChanged = false ;
2013-01-30 20:09:53 +00:00
// Get parameters
2013-09-07 09:01:01 +00:00
u32 fb_address = ( gstate . fbptr & 0xFFFFFF ) | ( ( gstate . fbwidth & 0xFF0000 ) < < 8 ) ;
2013-01-30 20:09:53 +00:00
int fb_stride = gstate . fbwidth & 0x3C0 ;
2013-09-07 09:01:01 +00:00
u32 z_address = ( gstate . zbptr & 0xFFFFFF ) | ( ( gstate . zbwidth & 0xFF0000 ) < < 8 ) ;
2013-01-30 20:09:53 +00:00
int z_stride = gstate . zbwidth & 0x3C0 ;
// Yeah this is not completely right. but it'll do for now.
2013-07-07 02:39:22 +00:00
//int drawing_width = ((gstate.region2) & 0x3FF) + 1;
//int drawing_height = ((gstate.region2 >> 10) & 0x3FF) + 1;
2013-07-06 16:47:25 +00:00
2013-01-31 23:18:23 +00:00
// As there are no clear "framebuffer width" and "framebuffer height" registers,
// we need to infer the size of the current framebuffer somehow. Let's try the viewport.
2013-08-24 17:20:07 +00:00
GEBufferFormat fmt = gstate . FrameBufFormat ( ) ;
2013-01-30 20:09:53 +00:00
2013-07-07 02:39:22 +00:00
int drawing_width , drawing_height ;
2013-08-24 07:59:50 +00:00
DrawingSize ( drawing_width , drawing_height ) ;
2013-03-15 23:40:37 +00:00
2013-06-20 20:18:44 +00:00
int buffer_width = drawing_width ;
int buffer_height = drawing_height ;
2013-09-14 13:03:24 +00:00
// Find a matching framebuffer, same size or bigger
2013-01-30 20:09:53 +00:00
VirtualFramebuffer * vfb = 0 ;
2013-06-23 15:51:35 +00:00
for ( size_t i = 0 ; i < vfbs_ . size ( ) ; + + i ) {
VirtualFramebuffer * v = vfbs_ [ i ] ;
2013-09-14 13:03:24 +00:00
if ( MaskedEqual ( v - > fb_address , fb_address ) ) {
// Okay, let's check the sizes. If the new one is bigger than the old one, recreate.
// If the opposite, just use it and hope that the game sets scissors accordingly.
if ( v - > bufferWidth > = drawing_width & & v - > bufferHeight > = drawing_height ) {
// Let's not be so picky for now. Let's say this is the one.
vfb = v ;
// Update fb stride in case it changed
vfb - > fb_stride = fb_stride ;
v - > format = fmt ;
// Just hack the width/height and we should be fine. also hack renderwidth/renderheight?
2013-09-07 06:35:10 +00:00
v - > width = drawing_width ;
v - > height = drawing_height ;
2013-09-14 13:03:24 +00:00
break ;
} else {
INFO_LOG ( HLE , " Enlarging framebuffer from (%i, %i) to (%i, %i) " , ( int ) v - > width , ( int ) v - > height , drawing_width , drawing_height ) ;
// drawing_width or drawing_height is bigger. Let's recreate with the max.
// To do this right we should copy the data over too, but meh.
if ( ( int ) v - > width > = drawing_width & & ( int ) v - > height > = drawing_height ) {
buffer_width = ( int ) v - > width ;
buffer_height = ( int ) v - > height ;
} else {
buffer_width = drawing_width ;
buffer_height = drawing_height ;
}
DestroyFramebuf ( v ) ;
vfbs_ . erase ( vfbs_ . begin ( ) + i - - ) ;
break ;
}
}
2013-01-30 20:09:53 +00:00
}
2013-01-31 23:18:23 +00:00
float renderWidthFactor = ( float ) PSP_CoreParameter ( ) . renderWidth / 480.0f ;
float renderHeightFactor = ( float ) PSP_CoreParameter ( ) . renderHeight / 272.0f ;
2013-01-30 20:09:53 +00:00
// None found? Create one.
if ( ! vfb ) {
gstate_c . textureChanged = true ;
vfb = new VirtualFramebuffer ( ) ;
2013-04-09 22:06:29 +00:00
vfb - > fbo = 0 ;
2013-01-30 20:09:53 +00:00
vfb - > fb_address = fb_address ;
vfb - > fb_stride = fb_stride ;
vfb - > z_address = z_address ;
vfb - > z_stride = z_stride ;
vfb - > width = drawing_width ;
vfb - > height = drawing_height ;
2013-02-10 11:13:35 +00:00
vfb - > renderWidth = ( u16 ) ( drawing_width * renderWidthFactor ) ;
vfb - > renderHeight = ( u16 ) ( drawing_height * renderHeightFactor ) ;
2013-06-20 20:18:44 +00:00
vfb - > bufferWidth = buffer_width ;
vfb - > bufferHeight = buffer_height ;
2013-01-30 20:09:53 +00:00
vfb - > format = fmt ;
2013-02-21 20:37:19 +00:00
vfb - > usageFlags = FB_USAGE_RENDERTARGET ;
2013-03-03 12:00:21 +00:00
vfb - > dirtyAfterDisplay = true ;
2013-08-15 23:00:26 +00:00
if ( ( gstate_c . skipDrawReason & SKIPDRAW_SKIPFRAME ) = = 0 )
vfb - > reallyDirtyAfterDisplay = true ;
2013-08-11 18:30:17 +00:00
vfb - > memoryUpdated = false ;
2013-01-30 20:09:53 +00:00
2013-03-09 05:46:11 +00:00
if ( g_Config . bTrueColor ) {
2013-02-19 18:07:42 +00:00
vfb - > colorDepth = FBO_8888 ;
2013-03-09 05:46:11 +00:00
} else {
switch ( fmt ) {
2013-07-30 06:05:59 +00:00
case GE_FORMAT_4444 :
vfb - > colorDepth = FBO_4444 ;
2013-03-09 05:46:11 +00:00
break ;
2013-07-30 06:05:59 +00:00
case GE_FORMAT_5551 :
vfb - > colorDepth = FBO_5551 ;
2013-03-09 05:46:11 +00:00
break ;
2013-07-30 06:05:59 +00:00
case GE_FORMAT_565 :
vfb - > colorDepth = FBO_565 ;
2013-03-09 05:46:11 +00:00
break ;
2013-07-30 06:05:59 +00:00
case GE_FORMAT_8888 :
vfb - > colorDepth = FBO_8888 ;
2013-03-09 05:46:11 +00:00
break ;
2013-07-30 06:05:59 +00:00
default :
vfb - > colorDepth = FBO_8888 ;
2013-03-09 05:46:11 +00:00
break ;
}
}
2013-02-19 18:07:42 +00:00
2013-04-27 18:06:31 +00:00
if ( useBufferedRendering_ ) {
2013-03-03 12:00:21 +00:00
vfb - > fbo = fbo_create ( vfb - > renderWidth , vfb - > renderHeight , 1 , true , vfb - > colorDepth ) ;
2013-06-23 15:05:59 +00:00
if ( vfb - > fbo ) {
fbo_bind_as_render_target ( vfb - > fbo ) ;
} else {
2013-09-07 20:02:55 +00:00
ERROR_LOG ( SCEGE , " Error creating FBO! %i x %i " , vfb - > renderWidth , vfb - > renderHeight ) ;
2013-06-23 15:05:59 +00:00
}
2013-03-09 05:46:11 +00:00
} else {
fbo_unbind ( ) ;
// Let's ignore rendering to targets that have not (yet) been displayed.
gstate_c . skipDrawReason | = SKIPDRAW_NON_DISPLAYED_FB ;
2013-03-03 12:00:21 +00:00
}
2013-08-01 06:19:15 +00:00
textureCache_ - > NotifyFramebuffer ( vfb - > fb_address , vfb , NOTIFY_FB_CREATED ) ;
2013-01-30 20:09:53 +00:00
2013-09-01 18:55:03 +00:00
vfb - > last_frame_render = gpuStats . numFlips ;
2013-08-07 20:32:04 +00:00
frameLastFramebufUsed = gpuStats . numFlips ;
2013-01-30 20:09:53 +00:00
vfbs_ . push_back ( vfb ) ;
2013-08-14 19:32:43 +00:00
ClearBuffer ( ) ;
2013-01-30 20:09:53 +00:00
glEnable ( GL_DITHER ) ;
currentRenderVfb_ = vfb ;
2013-03-09 05:46:11 +00:00
2013-09-07 20:02:55 +00:00
INFO_LOG ( SCEGE , " Creating FBO for %08x : %i x %i x %i " , vfb - > fb_address , vfb - > width , vfb - > height , vfb - > format ) ;
2013-01-30 20:09:53 +00:00
2013-09-23 04:44:11 +00:00
// Let's check for depth buffer overlap. Might be interesting.
bool sharingReported = false ;
for ( size_t i = 0 , end = vfbs_ . size ( ) ; i < end ; + + i ) {
if ( MaskedEqual ( fb_address , vfbs_ [ i ] - > z_address ) ) {
WARN_LOG_REPORT ( SCEGE , " FBO created from existing depthbuffer (unsupported), %08x/%08x and %08x/%08x " , fb_address , z_address , vfbs_ [ i ] - > fb_address , vfbs_ [ i ] - > z_address ) ;
} else if ( MaskedEqual ( z_address , vfbs_ [ i ] - > fb_address ) ) {
WARN_LOG_REPORT ( SCEGE , " FBO using other buffer as depthbuffer (unsupported), %08x/%08x and %08x/%08x " , fb_address , z_address , vfbs_ [ i ] - > fb_address , vfbs_ [ i ] - > z_address ) ;
} else if ( MaskedEqual ( z_address , vfbs_ [ i ] - > z_address ) & & fb_address ! = vfbs_ [ i ] - > fb_address & & ! sharingReported ) {
WARN_LOG_REPORT ( SCEGE , " FBO sharing existing depthbuffer (unsupported), %08x/%08x and %08x/%08x " , fb_address , z_address , vfbs_ [ i ] - > fb_address , vfbs_ [ i ] - > z_address ) ;
sharingReported = true ;
}
}
2013-03-03 12:00:21 +00:00
// We already have it!
} else if ( vfb ! = currentRenderVfb_ ) {
2013-09-22 09:03:39 +00:00
bool updateVRAM = ! ( g_Config . iRenderingMode = = FB_NON_BUFFERED_MODE | | g_Config . iRenderingMode = = FB_BUFFERED_MODE ) ;
if ( updateVRAM & & ! vfb - > memoryUpdated ) {
2013-08-11 18:30:17 +00:00
ReadFramebufferToMemory ( vfb , true ) ;
}
2013-01-30 20:09:53 +00:00
// Use it as a render target.
2013-09-07 20:02:55 +00:00
DEBUG_LOG ( SCEGE , " Switching render target to FBO for %08x: %i x %i x %i " , vfb - > fb_address , vfb - > width , vfb - > height , vfb - > format ) ;
2013-03-03 12:00:21 +00:00
vfb - > usageFlags | = FB_USAGE_RENDERTARGET ;
2013-01-30 20:09:53 +00:00
gstate_c . textureChanged = true ;
2013-09-01 18:55:03 +00:00
vfb - > last_frame_render = gpuStats . numFlips ;
2013-08-07 20:32:04 +00:00
frameLastFramebufUsed = gpuStats . numFlips ;
2013-03-03 12:00:21 +00:00
vfb - > dirtyAfterDisplay = true ;
2013-08-15 23:00:26 +00:00
if ( ( gstate_c . skipDrawReason & SKIPDRAW_SKIPFRAME ) = = 0 )
vfb - > reallyDirtyAfterDisplay = true ;
2013-08-11 18:30:17 +00:00
vfb - > memoryUpdated = false ;
2013-03-03 12:00:21 +00:00
2013-04-27 18:06:31 +00:00
if ( useBufferedRendering_ ) {
2013-04-09 22:06:29 +00:00
if ( vfb - > fbo ) {
fbo_bind_as_render_target ( vfb - > fbo ) ;
} else {
// wtf? This should only happen very briefly when toggling bBufferedRendering
fbo_unbind ( ) ;
}
2013-03-03 12:00:21 +00:00
} else {
2013-04-09 22:06:29 +00:00
if ( vfb - > fbo ) {
// wtf? This should only happen very briefly when toggling bBufferedRendering
2013-08-01 06:19:15 +00:00
textureCache_ - > NotifyFramebuffer ( vfb - > fb_address , vfb , NOTIFY_FB_DESTROYED ) ;
2013-04-09 22:06:29 +00:00
fbo_destroy ( vfb - > fbo ) ;
vfb - > fbo = 0 ;
}
2013-03-03 12:00:21 +00:00
fbo_unbind ( ) ;
// Let's ignore rendering to targets that have not (yet) been displayed.
if ( vfb - > usageFlags & FB_USAGE_DISPLAYED_FRAMEBUFFER )
gstate_c . skipDrawReason & = ~ SKIPDRAW_NON_DISPLAYED_FB ;
else
gstate_c . skipDrawReason | = SKIPDRAW_NON_DISPLAYED_FB ;
/*
if ( drawing_width = = 480 & & drawing_height = = 272 ) {
gstate_c . skipDrawReason & = ~ SKIPDRAW_SKIPNONFB ;
// OK!
} else {
gstate_c . skipDrawReason | = ~ SKIPDRAW_SKIPNONFB ;
} */
}
2013-08-01 06:19:15 +00:00
textureCache_ - > NotifyFramebuffer ( vfb - > fb_address , vfb , NOTIFY_FB_UPDATED ) ;
2013-02-28 22:47:57 +00:00
2013-01-30 20:09:53 +00:00
# ifdef USING_GLES2
2013-02-01 23:24:23 +00:00
// Some tiled mobile GPUs benefit IMMENSELY from clearing an FBO before rendering
// to it. This broke stuff before, so now it only clears on the first use of an
// FBO in a frame. This means that some games won't be able to avoid the on-some-GPUs
// performance-crushing framebuffer reloads from RAM, but we'll have to live with that.
2013-09-01 18:55:03 +00:00
if ( vfb - > last_frame_render ! = gpuStats . numFlips ) {
2013-08-14 20:14:18 +00:00
ClearBuffer ( ) ;
2013-02-22 21:14:17 +00:00
}
2013-01-30 20:09:53 +00:00
# endif
currentRenderVfb_ = vfb ;
2013-03-15 23:40:37 +00:00
} else {
2013-09-01 18:55:03 +00:00
vfb - > last_frame_render = gpuStats . numFlips ;
2013-08-07 20:32:04 +00:00
frameLastFramebufUsed = gpuStats . numFlips ;
2013-08-15 23:00:26 +00:00
vfb - > dirtyAfterDisplay = true ;
if ( ( gstate_c . skipDrawReason & SKIPDRAW_SKIPFRAME ) = = 0 )
vfb - > reallyDirtyAfterDisplay = true ;
2013-03-15 23:40:37 +00:00
}
// ugly...
if ( gstate_c . curRTWidth ! = vfb - > width | | gstate_c . curRTHeight ! = vfb - > height ) {
shaderManager_ - > DirtyUniform ( DIRTY_PROJTHROUGHMATRIX ) ;
gstate_c . curRTWidth = vfb - > width ;
gstate_c . curRTHeight = vfb - > height ;
2013-01-30 20:09:53 +00:00
}
}
void FramebufferManager : : CopyDisplayToOutput ( ) {
fbo_unbind ( ) ;
2013-06-09 09:11:16 +00:00
currentRenderVfb_ = 0 ;
2013-01-30 20:09:53 +00:00
2013-09-21 16:52:30 +00:00
VirtualFramebuffer * vfb = GetVFBAt ( displayFramebufPtr_ ) ;
2013-01-30 20:09:53 +00:00
if ( ! vfb ) {
2013-09-21 16:52:30 +00:00
if ( Memory : : IsValidAddress ( displayFramebufPtr_ ) ) {
2013-03-15 21:50:35 +00:00
// The game is displaying something directly from RAM. In GTA, it's decoded video.
2013-09-21 16:52:30 +00:00
// First check that it's not a known RAM copy of a VRAM framebuffer though, as in MotoGP
for ( auto iter = knownFramebufferCopies_ . begin ( ) ; iter ! = knownFramebufferCopies_ . end ( ) ; + + iter ) {
if ( iter - > second = = displayFramebufPtr_ ) {
vfb = GetVFBAt ( iter - > first ) ;
}
}
if ( ! vfb ) {
// Just a pointer to plain memory to draw. Draw it.
DrawPixels ( Memory : : GetPointer ( displayFramebufPtr_ ) , displayFormat_ , displayStride_ ) ;
return ;
}
2013-03-15 21:50:35 +00:00
} else {
2013-09-07 20:02:55 +00:00
DEBUG_LOG ( SCEGE , " Found no FBO to display! displayFBPtr = %08x " , displayFramebufPtr_ ) ;
2013-03-15 21:50:35 +00:00
// No framebuffer to display! Clear to black.
2013-08-14 19:32:43 +00:00
ClearBuffer ( ) ;
2013-09-21 16:52:30 +00:00
return ;
2013-03-15 21:50:35 +00:00
}
2013-01-30 20:09:53 +00:00
}
2013-03-03 12:00:21 +00:00
vfb - > usageFlags | = FB_USAGE_DISPLAYED_FRAMEBUFFER ;
vfb - > dirtyAfterDisplay = false ;
2013-08-15 23:00:26 +00:00
vfb - > reallyDirtyAfterDisplay = false ;
2013-03-03 12:00:21 +00:00
2013-04-12 14:09:45 +00:00
if ( prevDisplayFramebuf_ ! = displayFramebuf_ ) {
prevPrevDisplayFramebuf_ = prevDisplayFramebuf_ ;
}
if ( displayFramebuf_ ! = vfb ) {
prevDisplayFramebuf_ = displayFramebuf_ ;
}
2013-01-30 20:09:53 +00:00
displayFramebuf_ = vfb ;
2013-08-15 23:00:26 +00:00
if ( resized_ ) {
ClearBuffer ( ) ;
2013-09-26 10:41:07 +00:00
DestroyDraw2DProgram ( ) ;
2013-08-15 23:00:26 +00:00
}
2013-03-09 05:46:11 +00:00
if ( vfb - > fbo ) {
2013-09-07 20:02:55 +00:00
DEBUG_LOG ( SCEGE , " Displaying FBO %08x " , vfb - > fb_address ) ;
2013-08-14 19:32:43 +00:00
DisableState ( ) ;
2013-03-03 12:00:21 +00:00
2013-10-01 14:20:09 +00:00
GLuint colorTexture = fbo_get_color_texture ( vfb - > fbo ) ;
2013-09-28 20:42:13 +00:00
if ( useFXAA_ & & extraFBOs_ . size ( ) = = 1 ) {
2013-10-01 14:20:09 +00:00
glBindTexture ( GL_TEXTURE_2D , colorTexture ) ;
2013-09-28 20:42:13 +00:00
// An additional pass, FXAA to the extra FBO.
fbo_bind_as_render_target ( extraFBOs_ [ 0 ] ) ;
int fbo_w , fbo_h ;
fbo_get_dimensions ( extraFBOs_ [ 0 ] , & fbo_w , & fbo_h ) ;
glstate . viewport . set ( 0 , 0 , fbo_w , fbo_h ) ;
DrawActiveTexture ( 0 , 0 , fbo_w , fbo_h , fbo_w , fbo_h , true , 1.0f , 1.0f , fxaaProgram_ ) ;
fbo_unbind ( ) ;
// Use the extra FBO, with applied FXAA, as a texture.
2013-10-01 14:20:09 +00:00
// fbo_bind_color_as_texture(extraFBOs_[0], 0);
colorTexture = fbo_get_color_texture ( extraFBOs_ [ 0 ] ) ;
2013-09-28 20:42:13 +00:00
}
glstate . viewport . set ( 0 , 0 , PSP_CoreParameter ( ) . pixelWidth , PSP_CoreParameter ( ) . pixelHeight ) ;
2013-08-15 23:00:26 +00:00
// These are in the output display coordinates
2013-03-03 12:00:21 +00:00
float x , y , w , h ;
CenterRect ( & x , & y , & w , & h , 480.0f , 272.0f , ( float ) PSP_CoreParameter ( ) . pixelWidth , ( float ) PSP_CoreParameter ( ) . pixelHeight ) ;
2013-10-01 14:20:09 +00:00
2013-10-04 16:51:56 +00:00
# if defined(USING_GLES2) && !defined(__SYMBIAN32__) && !defined(MEEGO_EDITION_HARMATTAN) && !defined(IOS)
2013-10-01 14:20:09 +00:00
if ( gl_extensions . NV_draw_texture ) {
// Fast path for Tegra. TODO: Make this path work on desktop nvidia, seems glew doesn't have a clue.
glDrawTextureNV ( colorTexture , 0 ,
x , y , w , h , 0.0f ,
0 , 0 , 480.0f / ( float ) vfb - > width , 272.0f / ( float ) vfb - > height ) ;
return ;
}
# endif
glBindTexture ( GL_TEXTURE_2D , colorTexture ) ;
2013-09-28 20:42:13 +00:00
DrawActiveTexture ( x , y , w , h , ( float ) PSP_CoreParameter ( ) . pixelWidth , ( float ) PSP_CoreParameter ( ) . pixelHeight , true , 480.0f / ( float ) vfb - > width , 272.0f / ( float ) vfb - > height ) ;
2013-03-11 21:22:22 +00:00
glBindTexture ( GL_TEXTURE_2D , 0 ) ;
2013-03-03 12:00:21 +00:00
}
}
2013-01-30 20:09:53 +00:00
2013-08-11 18:30:17 +00:00
void FramebufferManager : : ReadFramebufferToMemory ( VirtualFramebuffer * vfb , bool sync ) {
# ifndef USING_GLES2
if ( sync ) {
PackFramebufferAsync_ ( NULL ) ; // flush async just in case when we go for synchronous update
}
# endif
2013-06-25 12:50:35 +00:00
if ( vfb ) {
2013-06-28 13:48:36 +00:00
// We'll pseudo-blit framebuffers here to get a resized and flipped version of vfb.
// For now we'll keep these on the same struct as the ones that can get displayed
// (and blatantly copy work already done above while at it).
VirtualFramebuffer * nvfb = 0 ;
// We maintain a separate vector of framebuffer objects for blitting.
for ( size_t i = 0 ; i < bvfbs_ . size ( ) ; + + i ) {
VirtualFramebuffer * v = bvfbs_ [ i ] ;
if ( MaskedEqual ( v - > fb_address , vfb - > fb_address ) & & v - > format = = vfb - > format ) {
if ( v - > bufferWidth = = vfb - > bufferWidth & & v - > bufferHeight = = vfb - > bufferHeight ) {
nvfb = v ;
v - > fb_stride = vfb - > fb_stride ;
v - > width = vfb - > width ;
v - > height = vfb - > height ;
break ;
2013-06-25 12:50:35 +00:00
}
}
2013-06-28 13:48:36 +00:00
}
2013-06-25 12:50:35 +00:00
2013-06-28 13:48:36 +00:00
// Create a new fbo if none was found for the size
if ( ! nvfb ) {
nvfb = new VirtualFramebuffer ( ) ;
nvfb - > fbo = 0 ;
nvfb - > fb_address = vfb - > fb_address ;
nvfb - > fb_stride = vfb - > fb_stride ;
nvfb - > z_address = vfb - > z_address ;
nvfb - > z_stride = vfb - > z_stride ;
nvfb - > width = vfb - > width ;
nvfb - > height = vfb - > height ;
nvfb - > renderWidth = vfb - > width ;
nvfb - > renderHeight = vfb - > height ;
nvfb - > bufferWidth = vfb - > bufferWidth ;
nvfb - > bufferHeight = vfb - > bufferHeight ;
nvfb - > format = vfb - > format ;
nvfb - > usageFlags = FB_USAGE_RENDERTARGET ;
nvfb - > dirtyAfterDisplay = true ;
2013-09-22 09:03:39 +00:00
// When updating VRAM, it need to be exact format.
switch ( vfb - > format ) {
case GE_FORMAT_4444 :
nvfb - > colorDepth = FBO_4444 ;
break ;
case GE_FORMAT_5551 :
nvfb - > colorDepth = FBO_5551 ;
break ;
case GE_FORMAT_565 :
nvfb - > colorDepth = FBO_565 ;
break ;
case GE_FORMAT_8888 :
default :
nvfb - > colorDepth = FBO_8888 ;
break ;
2013-06-28 13:48:36 +00:00
}
2013-06-25 12:50:35 +00:00
2013-06-28 13:48:36 +00:00
nvfb - > fbo = fbo_create ( nvfb - > width , nvfb - > height , 1 , true , nvfb - > colorDepth ) ;
if ( ! ( nvfb - > fbo ) ) {
2013-09-07 20:02:55 +00:00
ERROR_LOG ( SCEGE , " Error creating FBO! %i x %i " , nvfb - > renderWidth , nvfb - > renderHeight ) ;
2013-08-11 18:30:17 +00:00
return ;
2013-06-25 12:50:35 +00:00
}
2013-09-01 18:55:03 +00:00
nvfb - > last_frame_render = gpuStats . numFlips ;
2013-06-28 13:48:36 +00:00
bvfbs_ . push_back ( nvfb ) ;
2013-08-11 18:30:17 +00:00
fbo_bind_as_render_target ( nvfb - > fbo ) ;
2013-08-14 19:32:43 +00:00
ClearBuffer ( ) ;
2013-07-04 14:24:37 +00:00
glEnable ( GL_DITHER ) ;
2013-06-28 13:48:36 +00:00
} else {
nvfb - > usageFlags | = FB_USAGE_RENDERTARGET ;
2013-09-11 21:29:26 +00:00
gstate_c . textureChanged = true ;
2013-09-01 18:55:03 +00:00
nvfb - > last_frame_render = gpuStats . numFlips ;
2013-06-28 13:48:36 +00:00
nvfb - > dirtyAfterDisplay = true ;
# ifdef USING_GLES2
2013-09-11 20:59:36 +00:00
if ( nvfb - > fbo ) {
fbo_bind_as_render_target ( nvfb - > fbo ) ;
}
2013-08-11 18:30:17 +00:00
// Some tiled mobile GPUs benefit IMMENSELY from clearing an FBO before rendering
// to it. This broke stuff before, so now it only clears on the first use of an
// FBO in a frame. This means that some games won't be able to avoid the on-some-GPUs
// performance-crushing framebuffer reloads from RAM, but we'll have to live with that.
2013-09-01 18:55:03 +00:00
if ( nvfb - > last_frame_render ! = gpuStats . numFlips ) {
2013-08-14 20:14:18 +00:00
ClearBuffer ( ) ;
2013-06-25 12:50:35 +00:00
}
2013-08-11 18:30:17 +00:00
# endif
2013-06-28 13:48:36 +00:00
}
2013-06-25 12:50:35 +00:00
2013-08-11 18:30:17 +00:00
vfb - > memoryUpdated = true ;
2013-07-01 20:51:24 +00:00
BlitFramebuffer_ ( vfb , nvfb , false ) ;
2013-06-25 12:50:35 +00:00
2013-07-01 17:59:44 +00:00
# ifdef USING_GLES2
2013-08-11 18:30:17 +00:00
PackFramebufferSync_ ( nvfb ) ; // synchronous glReadPixels
2013-06-26 21:23:16 +00:00
# else
2013-09-29 02:30:25 +00:00
if ( gl_extensions . PBO_ARB | | ! gl_extensions . ATIClampBug ) {
if ( ! sync ) {
PackFramebufferAsync_ ( nvfb ) ; // asynchronous glReadPixels using PBOs
} else {
PackFramebufferSync_ ( nvfb ) ; // synchronous glReadPixels
}
2013-08-11 18:30:17 +00:00
}
2013-06-26 21:23:16 +00:00
# endif
2013-06-28 13:48:36 +00:00
}
}
2013-07-01 17:59:44 +00:00
void FramebufferManager : : BlitFramebuffer_ ( VirtualFramebuffer * src , VirtualFramebuffer * dst , bool flip , float upscale , float vscale ) {
2013-06-28 13:48:36 +00:00
2013-09-11 20:59:36 +00:00
if ( dst - > fbo ) {
fbo_bind_as_render_target ( dst - > fbo ) ;
2013-09-11 21:29:26 +00:00
} else {
ERROR_LOG_REPORT_ONCE ( dstfbozero , SCEGE , " BlitFramebuffer_: dst->fbo == 0 " ) ;
2013-09-22 09:03:39 +00:00
fbo_unbind ( ) ;
return ;
2013-09-11 20:59:36 +00:00
}
2013-06-28 13:48:36 +00:00
if ( glCheckFramebufferStatus ( GL_DRAW_FRAMEBUFFER ) ! = GL_FRAMEBUFFER_COMPLETE ) {
2013-09-07 20:02:55 +00:00
ERROR_LOG ( SCEGE , " Incomplete target framebuffer, aborting blit " ) ;
2013-06-28 13:48:36 +00:00
fbo_unbind ( ) ;
return ;
}
2013-07-01 20:51:24 +00:00
2013-06-28 13:48:36 +00:00
glstate . viewport . set ( 0 , 0 , dst - > width , dst - > height ) ;
2013-08-14 19:32:43 +00:00
DisableState ( ) ;
2013-06-28 13:48:36 +00:00
2013-09-11 20:21:15 +00:00
if ( src - > fbo ) {
fbo_bind_color_as_texture ( src - > fbo , 0 ) ;
} else {
ERROR_LOG_REPORT_ONCE ( srcfbozero , SCEGE , " BlitFramebuffer_: src->fbo == 0 " ) ;
2013-09-22 09:03:39 +00:00
fbo_unbind ( ) ;
return ;
2013-09-11 20:21:15 +00:00
}
2013-06-28 13:48:36 +00:00
float x , y , w , h ;
CenterRect ( & x , & y , & w , & h , 480.0f , 272.0f , ( float ) PSP_CoreParameter ( ) . pixelWidth , ( float ) PSP_CoreParameter ( ) . pixelHeight ) ;
2013-07-16 20:50:53 +00:00
CompileDraw2DProgram ( ) ;
2013-09-28 20:42:13 +00:00
DrawActiveTexture ( x , y , w , h , ( float ) PSP_CoreParameter ( ) . pixelWidth , ( float ) PSP_CoreParameter ( ) . pixelHeight , flip , upscale , vscale , draw2dprogram_ ) ;
2013-06-28 13:48:36 +00:00
glBindTexture ( GL_TEXTURE_2D , 0 ) ;
fbo_unbind ( ) ;
}
2013-07-06 09:09:08 +00:00
// TODO: SSE/NEON
2013-07-30 06:05:59 +00:00
void ConvertFromRGBA8888 ( u8 * dst , u8 * src , u32 stride , u32 height , GEBufferFormat format ) {
2013-07-04 14:24:37 +00:00
if ( format = = GE_FORMAT_8888 ) {
2013-07-02 13:08:59 +00:00
if ( src = = dst ) {
return ;
2013-07-04 14:24:37 +00:00
} else { // Here lets assume they don't intersect
2013-07-02 13:08:59 +00:00
memcpy ( dst , src , stride * height * 4 ) ;
}
} else { // But here it shouldn't matter if they do
2013-07-06 17:08:59 +00:00
int size = height * stride ;
2013-07-06 09:09:08 +00:00
const u32 * src32 = ( const u32 * ) src ;
2013-07-05 01:31:31 +00:00
u16 * dst16 = ( u16 * ) dst ;
switch ( format ) {
case GE_FORMAT_565 : // BGR 565
for ( int i = 0 ; i < size ; i + + ) {
dst16 [ i ] = RGBA8888toRGB565 ( src32 [ i ] ) ;
}
break ;
case GE_FORMAT_5551 : // ABGR 1555
for ( int i = 0 ; i < size ; i + + ) {
dst16 [ i ] = RGBA8888toRGBA5551 ( src32 [ i ] ) ;
}
2013-06-28 13:48:36 +00:00
2013-07-05 01:31:31 +00:00
break ;
case GE_FORMAT_4444 : // ABGR 4444
for ( int i = 0 ; i < size ; i + + ) {
dst16 [ i ] = RGBA8888toRGBA4444 ( src32 [ i ] ) ;
}
break ;
2013-07-30 06:05:59 +00:00
case GE_FORMAT_8888 :
// Not possible.
break ;
2013-07-05 01:31:31 +00:00
default :
break ;
2013-06-25 12:50:35 +00:00
}
2013-06-28 13:48:36 +00:00
}
}
2013-07-05 22:40:01 +00:00
# ifndef USING_GLES2
2013-08-11 18:30:17 +00:00
void FramebufferManager : : PackFramebufferAsync_ ( VirtualFramebuffer * vfb ) {
const int MAX_PBO = 2 ;
2013-06-28 13:48:36 +00:00
GLubyte * packed = 0 ;
2013-07-02 13:08:59 +00:00
bool unbind = false ;
2013-08-11 18:30:17 +00:00
u8 nextPBO = ( currentPBO_ + 1 ) % MAX_PBO ;
2013-08-14 19:20:06 +00:00
bool useCPU = g_Config . iRenderingMode = = FB_READFBOMEMORY_CPU ;
2013-08-11 18:30:17 +00:00
2013-07-04 14:24:37 +00:00
// We'll prepare two PBOs to switch between readying and reading
2013-07-02 13:08:59 +00:00
if ( ! pixelBufObj_ ) {
2013-08-11 18:30:17 +00:00
GLuint pbos [ MAX_PBO ] ;
glGenBuffers ( MAX_PBO , pbos ) ;
pixelBufObj_ = new AsyncPBO [ MAX_PBO ] ;
for ( int i = 0 ; i < MAX_PBO ; i + + ) {
pixelBufObj_ [ i ] . handle = pbos [ i ] ;
pixelBufObj_ [ i ] . maxSize = 0 ;
pixelBufObj_ [ i ] . reading = false ;
}
}
// Receive previously requested data from a PBO
if ( pixelBufObj_ [ nextPBO ] . reading ) {
glBindBuffer ( GL_PIXEL_PACK_BUFFER , pixelBufObj_ [ nextPBO ] . handle ) ;
packed = ( GLubyte * ) glMapBuffer ( GL_PIXEL_PACK_BUFFER , GL_READ_ONLY ) ;
2013-07-02 13:08:59 +00:00
2013-08-11 18:30:17 +00:00
if ( packed ) {
2013-09-22 09:03:39 +00:00
DEBUG_LOG ( SCEGE , " Reading PBO to memory , bufSize = %u, packed = %08x, fb_address = %08x, stride = %u, pbo = %u " ,
pixelBufObj_ [ nextPBO ] . size , packed , pixelBufObj_ [ nextPBO ] . fb_address , pixelBufObj_ [ nextPBO ] . stride , nextPBO ) ;
2013-08-11 18:30:17 +00:00
if ( useCPU ) {
ConvertFromRGBA8888 ( Memory : : GetPointer ( pixelBufObj_ [ nextPBO ] . fb_address ) , packed ,
pixelBufObj_ [ nextPBO ] . stride , pixelBufObj_ [ nextPBO ] . height ,
pixelBufObj_ [ nextPBO ] . format ) ;
2013-09-22 09:03:39 +00:00
} else {
// We don't need to convert, GPU already did (or should have)
2013-08-11 18:30:17 +00:00
Memory : : Memcpy ( pixelBufObj_ [ nextPBO ] . fb_address , packed , pixelBufObj_ [ nextPBO ] . size ) ;
}
2013-07-02 13:08:59 +00:00
2013-08-11 18:30:17 +00:00
pixelBufObj_ [ nextPBO ] . reading = false ;
}
2013-07-02 13:08:59 +00:00
2013-08-11 18:30:17 +00:00
glUnmapBuffer ( GL_PIXEL_PACK_BUFFER ) ;
2013-07-02 13:08:59 +00:00
2013-08-11 18:30:17 +00:00
unbind = true ;
2013-07-02 13:08:59 +00:00
}
2013-06-28 13:48:36 +00:00
// Order packing/readback of the framebuffer
if ( vfb ) {
2013-06-25 12:50:35 +00:00
int pixelType , pixelSize , pixelFormat , align ;
2013-08-19 18:18:32 +00:00
bool reverseOrder = ( gpuVendor = = GPU_VENDOR_NVIDIA ) | | ( gpuVendor = = GPU_VENDOR_AMD ) ;
2013-06-25 12:50:35 +00:00
switch ( vfb - > format ) {
2013-07-04 14:24:37 +00:00
// GL_UNSIGNED_INT_8_8_8_8 returns A B G R (little-endian, tested in Nvidia card/x86 PC)
// GL_UNSIGNED_BYTE returns R G B A in consecutive bytes ("big-endian"/not treated as 32-bit value)
// We want R G B A, so we use *_REV for 16-bit formats and GL_UNSIGNED_BYTE for 32-bit
case GE_FORMAT_4444 : // 16 bit RGBA
2013-08-19 18:18:32 +00:00
pixelType = ( reverseOrder ? GL_UNSIGNED_SHORT_4_4_4_4_REV : GL_UNSIGNED_SHORT_4_4_4_4 ) ;
2013-07-04 14:24:37 +00:00
pixelFormat = GL_RGBA ;
2013-06-25 18:14:10 +00:00
pixelSize = 2 ;
align = 8 ;
break ;
2013-07-04 14:24:37 +00:00
case GE_FORMAT_5551 : // 16 bit RGBA
2013-08-19 18:18:32 +00:00
pixelType = ( reverseOrder ? GL_UNSIGNED_SHORT_1_5_5_5_REV : GL_UNSIGNED_SHORT_5_5_5_1 ) ;
2013-07-04 14:24:37 +00:00
pixelFormat = GL_RGBA ;
2013-06-25 18:14:10 +00:00
pixelSize = 2 ;
align = 8 ;
break ;
2013-07-04 14:24:37 +00:00
case GE_FORMAT_565 : // 16 bit RGB
2013-08-19 18:18:32 +00:00
pixelType = ( reverseOrder ? GL_UNSIGNED_SHORT_5_6_5_REV : GL_UNSIGNED_SHORT_5_6_5 ) ;
2013-07-04 14:24:37 +00:00
pixelFormat = GL_RGB ;
2013-06-25 18:14:10 +00:00
pixelSize = 2 ;
align = 8 ;
break ;
2013-07-04 14:24:37 +00:00
case GE_FORMAT_8888 : // 32 bit RGBA
2013-06-28 13:48:36 +00:00
default :
pixelType = GL_UNSIGNED_BYTE ;
2013-07-04 14:24:37 +00:00
pixelFormat = GL_RGBA ;
2013-06-25 18:14:10 +00:00
pixelSize = 4 ;
align = 4 ;
break ;
2013-06-25 12:50:35 +00:00
}
2013-07-01 20:51:24 +00:00
u32 bufSize = vfb - > fb_stride * vfb - > height * pixelSize ;
2013-09-19 17:28:14 +00:00
u32 fb_address = ( 0x04000000 ) | vfb - > fb_address ;
2013-06-28 13:48:36 +00:00
2013-08-11 18:30:17 +00:00
if ( vfb - > fbo ) {
fbo_bind_for_read ( vfb - > fbo ) ;
} else {
fbo_unbind ( ) ;
if ( gl_extensions . FBO_ARB ) {
glBindFramebuffer ( GL_READ_FRAMEBUFFER , 0 ) ;
2013-06-25 12:50:35 +00:00
}
2013-08-11 18:30:17 +00:00
return ;
}
if ( glCheckFramebufferStatus ( GL_READ_FRAMEBUFFER ) ! = GL_FRAMEBUFFER_COMPLETE ) {
2013-09-07 20:02:55 +00:00
ERROR_LOG ( SCEGE , " Incomplete source framebuffer, aborting read " ) ;
2013-08-11 18:30:17 +00:00
fbo_unbind ( ) ;
if ( gl_extensions . FBO_ARB ) {
glBindFramebuffer ( GL_READ_FRAMEBUFFER , 0 ) ;
}
return ;
2013-06-25 12:50:35 +00:00
}
2013-06-28 13:48:36 +00:00
glBindBuffer ( GL_PIXEL_PACK_BUFFER , pixelBufObj_ [ currentPBO_ ] . handle ) ;
if ( pixelBufObj_ [ currentPBO_ ] . maxSize < bufSize ) {
2013-07-04 14:24:37 +00:00
// We reserve a buffer big enough to fit all those pixels
2013-07-21 20:15:18 +00:00
if ( useCPU & & pixelType ! = GL_UNSIGNED_BYTE ) {
2013-09-22 09:03:39 +00:00
// Wnd result may be 16-bit but we are reading 32-bit, so we need double the space on the buffer
2013-06-28 13:48:36 +00:00
glBufferData ( GL_PIXEL_PACK_BUFFER , bufSize * 2 , NULL , GL_DYNAMIC_READ ) ;
} else {
glBufferData ( GL_PIXEL_PACK_BUFFER , bufSize , NULL , GL_DYNAMIC_READ ) ;
2013-06-25 12:50:35 +00:00
}
2013-06-28 13:48:36 +00:00
pixelBufObj_ [ currentPBO_ ] . maxSize = bufSize ;
2013-06-25 12:50:35 +00:00
}
2013-07-21 20:15:18 +00:00
if ( useCPU ) {
2013-07-04 14:24:37 +00:00
// If converting pixel formats on the CPU we'll always request RGBA8888
2013-06-28 13:48:36 +00:00
glPixelStorei ( GL_PACK_ALIGNMENT , 4 ) ;
2013-07-04 14:24:37 +00:00
glReadPixels ( 0 , 0 , vfb - > fb_stride , vfb - > height , GL_RGBA , GL_UNSIGNED_BYTE , 0 ) ;
2013-06-28 13:48:36 +00:00
} else {
2013-07-04 14:24:37 +00:00
// Otherwise we'll directly request the format we need and let the GPU sort it out
2013-06-28 13:48:36 +00:00
glPixelStorei ( GL_PACK_ALIGNMENT , align ) ;
glReadPixels ( 0 , 0 , vfb - > fb_stride , vfb - > height , pixelFormat , pixelType , 0 ) ;
}
2013-07-02 13:08:59 +00:00
2013-08-11 18:30:17 +00:00
GLenum error = glGetError ( ) ;
switch ( error ) {
case 0 :
break ;
case GL_INVALID_ENUM :
2013-09-07 20:02:55 +00:00
ERROR_LOG ( SCEGE , " glReadPixels: GL_INVALID_ENUM " ) ;
2013-08-11 18:30:17 +00:00
break ;
case GL_INVALID_VALUE :
2013-09-07 20:02:55 +00:00
ERROR_LOG ( SCEGE , " glReadPixels: GL_INVALID_VALUE " ) ;
2013-08-11 18:30:17 +00:00
break ;
case GL_INVALID_OPERATION :
// GL_INVALID_OPERATION will happen sometimes midframe but everything
// seems to work out when actually mapping buffers?
// GL_SAMPLE_BUFFERS, GL_READ_BUFFER, GL_BUFFER_SIZE/MAPPED,
// GL_PIXEL_PACK_BUFFER_BINDING, all have the expected values.
2013-09-07 20:02:55 +00:00
ERROR_LOG ( SCEGE , " glReadPixels: GL_INVALID_OPERATION " ) ;
2013-08-11 18:30:17 +00:00
break ;
case GL_INVALID_FRAMEBUFFER_OPERATION :
2013-09-07 20:02:55 +00:00
ERROR_LOG ( SCEGE , " glReadPixels: GL_INVALID_FRAMEBUFFER_OPERATION " ) ;
2013-08-11 18:30:17 +00:00
break ;
default :
2013-09-07 20:02:55 +00:00
ERROR_LOG ( SCEGE , " glReadPixels: UNKNOWN OPENGL ERROR %u " , error ) ;
2013-08-11 18:30:17 +00:00
break ;
}
2013-07-04 14:24:37 +00:00
fbo_unbind ( ) ;
if ( gl_extensions . FBO_ARB ) {
glBindFramebuffer ( GL_READ_FRAMEBUFFER , 0 ) ;
}
2013-07-02 13:08:59 +00:00
unbind = true ;
2013-06-28 13:48:36 +00:00
2013-08-11 18:30:17 +00:00
pixelBufObj_ [ currentPBO_ ] . fb_address = fb_address ;
pixelBufObj_ [ currentPBO_ ] . size = bufSize ;
pixelBufObj_ [ currentPBO_ ] . stride = vfb - > fb_stride ;
pixelBufObj_ [ currentPBO_ ] . height = vfb - > height ;
pixelBufObj_ [ currentPBO_ ] . format = vfb - > format ;
pixelBufObj_ [ currentPBO_ ] . reading = true ;
2013-06-28 13:48:36 +00:00
}
currentPBO_ = nextPBO ;
2013-07-02 13:08:59 +00:00
if ( unbind ) {
glBindBuffer ( GL_PIXEL_PACK_BUFFER , 0 ) ;
2013-06-28 13:48:36 +00:00
}
}
2013-07-05 22:40:01 +00:00
# endif
2013-08-11 18:30:17 +00:00
void FramebufferManager : : PackFramebufferSync_ ( VirtualFramebuffer * vfb ) {
2013-09-22 09:03:39 +00:00
if ( vfb - > fbo ) {
2013-07-02 13:08:59 +00:00
fbo_bind_for_read ( vfb - > fbo ) ;
} else {
2013-09-22 09:03:39 +00:00
ERROR_LOG_REPORT_ONCE ( vfbfbozero , SCEGE , " PackFramebufferSync_: vfb->fbo == 0 " ) ;
2013-07-02 13:08:59 +00:00
fbo_unbind ( ) ;
return ;
}
2013-07-04 14:24:37 +00:00
// Pixel size always 4 here because we always request RGBA8888
size_t bufSize = vfb - > fb_stride * vfb - > height * 4 ;
2013-08-11 18:30:17 +00:00
u32 fb_address = ( 0x04000000 ) | vfb - > fb_address ;
2013-06-28 13:48:36 +00:00
2013-07-02 13:08:59 +00:00
GLubyte * packed = 0 ;
if ( vfb - > format = = GE_FORMAT_8888 ) {
packed = ( GLubyte * ) Memory : : GetPointer ( fb_address ) ;
2013-07-04 14:24:37 +00:00
} else { // End result may be 16-bit but we are reading 32-bit, so there may not be enough space at fb_address
2013-07-02 13:08:59 +00:00
packed = ( GLubyte * ) malloc ( bufSize * sizeof ( GLubyte ) ) ;
2013-06-28 13:48:36 +00:00
}
if ( packed ) {
2013-09-07 20:02:55 +00:00
DEBUG_LOG ( SCEGE , " Reading framebuffer to mem, bufSize = %u, packed = %p, fb_address = %08x " ,
2013-07-06 17:08:59 +00:00
( u32 ) bufSize , packed , fb_address ) ;
2013-06-28 13:48:36 +00:00
glPixelStorei ( GL_PACK_ALIGNMENT , 4 ) ;
glReadPixels ( 0 , 0 , vfb - > fb_stride , vfb - > height , GL_RGBA , GL_UNSIGNED_BYTE , packed ) ;
GLenum error = glGetError ( ) ;
switch ( error ) {
case 0 :
break ;
case GL_INVALID_ENUM :
2013-09-07 20:02:55 +00:00
ERROR_LOG ( SCEGE , " glReadPixels: GL_INVALID_ENUM " ) ;
2013-06-28 13:48:36 +00:00
break ;
case GL_INVALID_VALUE :
2013-09-07 20:02:55 +00:00
ERROR_LOG ( SCEGE , " glReadPixels: GL_INVALID_VALUE " ) ;
2013-06-28 13:48:36 +00:00
break ;
2013-08-11 18:30:17 +00:00
case GL_INVALID_OPERATION :
// GL_INVALID_OPERATION will happen sometimes midframe but everything
// seems to work out when actually reading?
2013-09-07 20:02:55 +00:00
ERROR_LOG ( SCEGE , " glReadPixels: GL_INVALID_OPERATION " ) ;
2013-06-28 13:48:36 +00:00
break ;
case GL_INVALID_FRAMEBUFFER_OPERATION :
2013-09-07 20:02:55 +00:00
ERROR_LOG ( SCEGE , " glReadPixels: GL_INVALID_FRAMEBUFFER_OPERATION " ) ;
2013-06-28 13:48:36 +00:00
break ;
default :
2013-09-07 20:02:55 +00:00
ERROR_LOG ( SCEGE , " glReadPixels: UNKNOWN OPENGL ERROR %u " , error ) ;
2013-06-28 13:48:36 +00:00
break ;
2013-06-25 12:50:35 +00:00
}
2013-06-28 13:48:36 +00:00
2013-07-04 14:24:37 +00:00
if ( vfb - > format ! = GE_FORMAT_8888 ) { // If not RGBA 8888 we need to convert
2013-07-02 13:08:59 +00:00
ConvertFromRGBA8888 ( Memory : : GetPointer ( fb_address ) , packed , vfb - > fb_stride , vfb - > height , vfb - > format ) ;
free ( packed ) ;
2013-06-28 13:48:36 +00:00
}
}
fbo_unbind ( ) ;
2013-06-25 12:50:35 +00:00
}
2013-03-03 12:00:21 +00:00
void FramebufferManager : : EndFrame ( ) {
2013-01-30 20:09:53 +00:00
if ( resized_ ) {
DestroyAllFBOs ( ) ;
glstate . viewport . set ( 0 , 0 , PSP_CoreParameter ( ) . pixelWidth , PSP_CoreParameter ( ) . pixelHeight ) ;
resized_ = false ;
}
2013-07-02 13:10:20 +00:00
# ifndef USING_GLES2
2013-07-04 14:24:37 +00:00
// We flush to memory last requested framebuffer, if any
2013-08-11 18:30:17 +00:00
PackFramebufferAsync_ ( NULL ) ;
2013-07-02 13:10:20 +00:00
# endif
2013-01-30 20:09:53 +00:00
}
2013-06-11 09:28:41 +00:00
void FramebufferManager : : DeviceLost ( ) {
DestroyAllFBOs ( ) ;
2013-09-26 10:41:07 +00:00
DestroyDraw2DProgram ( ) ;
2013-06-11 09:28:41 +00:00
resized_ = false ;
}
2013-01-30 20:09:53 +00:00
void FramebufferManager : : BeginFrame ( ) {
DecimateFBOs ( ) ;
currentRenderVfb_ = 0 ;
2013-08-14 19:20:06 +00:00
useBufferedRendering_ = g_Config . iRenderingMode ! = FB_NON_BUFFERED_MODE ;
2013-01-30 20:09:53 +00:00
}
2013-07-30 06:05:59 +00:00
void FramebufferManager : : SetDisplayFramebuffer ( u32 framebuf , u32 stride , GEBufferFormat format ) {
2013-09-21 16:52:30 +00:00
displayFramebufPtr_ = framebuf ;
displayStride_ = stride ;
displayFormat_ = format ;
2013-01-30 20:09:53 +00:00
}
2013-03-03 12:00:21 +00:00
std : : vector < FramebufferInfo > FramebufferManager : : GetFramebufferList ( ) {
2013-02-17 00:06:06 +00:00
std : : vector < FramebufferInfo > list ;
2013-06-23 15:51:35 +00:00
for ( size_t i = 0 ; i < vfbs_ . size ( ) ; + + i ) {
VirtualFramebuffer * vfb = vfbs_ [ i ] ;
2013-02-17 00:06:06 +00:00
FramebufferInfo info ;
info . fb_address = vfb - > fb_address ;
info . z_address = vfb - > z_address ;
info . format = vfb - > format ;
info . width = vfb - > width ;
info . height = vfb - > height ;
info . fbo = vfb - > fbo ;
list . push_back ( info ) ;
}
return list ;
}
2013-09-21 16:52:30 +00:00
// MotoGP workaround
void FramebufferManager : : NotifyFramebufferCopy ( u32 src , u32 dest , int size ) {
for ( size_t i = 0 ; i < vfbs_ . size ( ) ; i + + ) {
// This size fits for MotoGP. Might want to make this more flexible for other games if they do the same.
if ( ( vfbs_ [ i ] - > fb_address | 0x04000000 ) = = src & & size = = 512 * 272 * 2 ) {
// A framebuffer matched!
knownFramebufferCopies_ . insert ( std : : pair < u32 , u32 > ( src , dest ) ) ;
}
}
}
2013-04-12 14:00:59 +00:00
void FramebufferManager : : DecimateFBOs ( ) {
2013-03-11 21:22:22 +00:00
fbo_unbind ( ) ;
2013-06-09 09:11:16 +00:00
currentRenderVfb_ = 0 ;
2013-09-22 09:03:39 +00:00
bool updateVram = ! ( g_Config . iRenderingMode = = FB_NON_BUFFERED_MODE | | g_Config . iRenderingMode = = FB_BUFFERED_MODE ) ;
2013-06-23 15:51:35 +00:00
for ( size_t i = 0 ; i < vfbs_ . size ( ) ; + + i ) {
VirtualFramebuffer * vfb = vfbs_ [ i ] ;
2013-09-01 18:55:03 +00:00
int age = frameLastFramebufUsed - std : : max ( vfb - > last_frame_render , vfb - > last_frame_used ) ;
2013-07-02 13:10:20 +00:00
2013-09-29 02:30:25 +00:00
if ( updateVram & & age = = 0 & & ! vfb - > memoryUpdated & & vfb = = displayFramebuf_ )
2013-09-22 09:03:39 +00:00
ReadFramebufferToMemory ( vfb ) ;
2013-09-29 02:30:25 +00:00
if ( vfb = = displayFramebuf_ | | vfb = = prevDisplayFramebuf_ | | vfb = = prevPrevDisplayFramebuf_ ) {
2013-01-30 20:09:53 +00:00
continue ;
}
2013-07-01 17:35:38 +00:00
2013-07-02 13:10:20 +00:00
if ( age > FBO_OLD_AGE ) {
2013-09-07 20:02:55 +00:00
INFO_LOG ( SCEGE , " Decimating FBO for %08x (%i x %i x %i), age %i " , vfb - > fb_address , vfb - > width , vfb - > height , vfb - > format , age )
2013-06-23 15:16:22 +00:00
DestroyFramebuf ( vfb ) ;
2013-06-23 15:51:35 +00:00
vfbs_ . erase ( vfbs_ . begin ( ) + i - - ) ;
2013-01-30 20:09:53 +00:00
}
}
2013-06-28 13:48:36 +00:00
// Do the same for ReadFramebuffersToMemory's VFBs
for ( size_t i = 0 ; i < bvfbs_ . size ( ) ; + + i ) {
VirtualFramebuffer * vfb = bvfbs_ [ i ] ;
2013-09-01 18:55:03 +00:00
int age = frameLastFramebufUsed - vfb - > last_frame_render ;
2013-06-28 13:48:36 +00:00
if ( age > FBO_OLD_AGE ) {
2013-09-07 20:02:55 +00:00
INFO_LOG ( SCEGE , " Decimating FBO for %08x (%i x %i x %i), age %i " , vfb - > fb_address , vfb - > width , vfb - > height , vfb - > format , age )
2013-06-28 13:48:36 +00:00
DestroyFramebuf ( vfb ) ;
bvfbs_ . erase ( bvfbs_ . begin ( ) + i - - ) ;
}
}
2013-01-30 20:09:53 +00:00
}
void FramebufferManager : : DestroyAllFBOs ( ) {
2013-03-11 21:22:22 +00:00
fbo_unbind ( ) ;
2013-06-09 09:11:16 +00:00
currentRenderVfb_ = 0 ;
2013-06-10 20:07:48 +00:00
displayFramebuf_ = 0 ;
prevDisplayFramebuf_ = 0 ;
prevPrevDisplayFramebuf_ = 0 ;
2013-06-09 09:11:16 +00:00
2013-06-23 15:51:35 +00:00
for ( size_t i = 0 ; i < vfbs_ . size ( ) ; + + i ) {
VirtualFramebuffer * vfb = vfbs_ [ i ] ;
2013-09-07 20:02:55 +00:00
INFO_LOG ( SCEGE , " Destroying FBO for %08x : %i x %i x %i " , vfb - > fb_address , vfb - > width , vfb - > height , vfb - > format ) ;
2013-06-23 15:16:22 +00:00
DestroyFramebuf ( vfb ) ;
2013-01-30 20:09:53 +00:00
}
vfbs_ . clear ( ) ;
}
2013-01-31 11:14:57 +00:00
2013-06-08 11:37:40 +00:00
void FramebufferManager : : UpdateFromMemory ( u32 addr , int size ) {
addr & = ~ 0x40000000 ;
// TODO: Could go through all FBOs, but probably not important?
// TODO: Could also check for inner changes, but video is most important.
if ( addr = = DisplayFramebufAddr ( ) | | addr = = PrevDisplayFramebufAddr ( ) ) {
// TODO: Deleting the FBO is a heavy hammer solution, so let's only do it if it'd help.
if ( ! Memory : : IsValidAddress ( displayFramebufPtr_ ) )
return ;
fbo_unbind ( ) ;
2013-06-09 09:11:16 +00:00
currentRenderVfb_ = 0 ;
2013-06-10 07:10:39 +00:00
bool needUnbind = false ;
2013-06-23 15:51:35 +00:00
for ( size_t i = 0 ; i < vfbs_ . size ( ) ; + + i ) {
VirtualFramebuffer * vfb = vfbs_ [ i ] ;
2013-06-08 11:37:40 +00:00
if ( MaskedEqual ( vfb - > fb_address , addr ) ) {
2013-07-18 18:00:32 +00:00
vfb - > dirtyAfterDisplay = true ;
2013-08-16 07:27:49 +00:00
vfb - > reallyDirtyAfterDisplay = true ;
2013-06-08 11:37:40 +00:00
// TODO: This without the fbo_unbind() above would be better than destroying the FBO.
// However, it doesn't seem to work for Star Ocean, at least
2013-09-11 20:59:36 +00:00
if ( useBufferedRendering_ & & vfb - > fbo ) {
2013-06-10 07:10:39 +00:00
fbo_bind_as_render_target ( vfb - > fbo ) ;
needUnbind = true ;
2013-10-06 00:19:52 +00:00
DrawPixels ( Memory : : GetPointer ( addr | 0x04000000 ) , vfb - > format , vfb - > fb_stride ) ;
2013-06-10 07:10:39 +00:00
} else {
2013-09-07 20:02:55 +00:00
INFO_LOG ( SCEGE , " Invalidating FBO for %08x (%i x %i x %i) " , vfb - > fb_address , vfb - > width , vfb - > height , vfb - > format )
2013-06-23 15:16:22 +00:00
DestroyFramebuf ( vfb ) ;
2013-06-23 15:51:35 +00:00
vfbs_ . erase ( vfbs_ . begin ( ) + i - - ) ;
2013-06-08 11:37:40 +00:00
}
}
}
2013-06-10 07:10:39 +00:00
if ( needUnbind )
fbo_unbind ( ) ;
2013-06-08 11:37:40 +00:00
}
}
2013-01-31 11:14:57 +00:00
void FramebufferManager : : Resized ( ) {
resized_ = true ;
}
2013-09-23 02:03:31 +00:00
2013-09-28 09:14:27 +00:00
bool FramebufferManager : : GetCurrentFramebuffer ( GPUDebugBuffer & buffer ) {
2013-09-23 02:03:31 +00:00
u32 fb_address = ( gstate . fbptr & 0xFFFFFF ) | ( ( gstate . fbwidth & 0xFF0000 ) < < 8 ) ;
int fb_stride = gstate . fbwidth & 0x3C0 ;
VirtualFramebuffer * vfb = currentRenderVfb_ ;
if ( ! vfb ) {
vfb = GetVFBAt ( fb_address ) ;
}
if ( ! vfb ) {
// If there's no vfb and we're drawing there, must be memory?
2013-10-06 00:19:17 +00:00
buffer = GPUDebugBuffer ( Memory : : GetPointer ( fb_address | 0x04000000 ) , fb_stride , 512 , gstate . FrameBufFormat ( ) ) ;
2013-09-23 02:03:31 +00:00
return true ;
}
2013-09-23 05:37:37 +00:00
buffer . Allocate ( vfb - > renderWidth , vfb - > renderHeight , GE_FORMAT_8888 , true ) ;
2013-09-29 05:03:36 +00:00
if ( vfb - > fbo )
fbo_bind_for_read ( vfb - > fbo ) ;
2013-10-06 00:19:17 +00:00
# ifndef USING_GLES2
glReadBuffer ( GL_COLOR_ATTACHMENT0 ) ;
# endif
2013-09-23 02:03:31 +00:00
glPixelStorei ( GL_PACK_ALIGNMENT , 4 ) ;
2013-09-23 03:03:47 +00:00
glReadPixels ( 0 , 0 , vfb - > renderWidth , vfb - > renderHeight , GL_RGBA , GL_UNSIGNED_BYTE , buffer . GetData ( ) ) ;
2013-09-23 02:03:31 +00:00
return true ;
}
2013-09-28 09:14:27 +00:00
bool FramebufferManager : : GetCurrentDepthbuffer ( GPUDebugBuffer & buffer ) {
u32 fb_address = ( gstate . fbptr & 0xFFFFFF ) | ( ( gstate . fbwidth & 0xFF0000 ) < < 8 ) ;
int fb_stride = gstate . fbwidth & 0x3C0 ;
u32 z_address = ( gstate . zbptr & 0xFFFFFF ) | ( ( gstate . zbwidth & 0xFF0000 ) < < 8 ) ;
int z_stride = gstate . zbwidth & 0x3C0 ;
VirtualFramebuffer * vfb = currentRenderVfb_ ;
if ( ! vfb ) {
vfb = GetVFBAt ( fb_address ) ;
}
if ( ! vfb ) {
// If there's no vfb and we're drawing there, must be memory?
2013-09-28 09:38:05 +00:00
// TODO: Is the value 16-bit? It seems to be.
2013-10-06 00:19:17 +00:00
buffer = GPUDebugBuffer ( Memory : : GetPointer ( z_address | 0x04000000 ) , z_stride , 512 , GPU_DBG_FORMAT_16BIT ) ;
2013-09-28 09:14:27 +00:00
return true ;
}
# ifndef USING_GLES2
2013-10-06 00:19:17 +00:00
buffer . Allocate ( vfb - > renderWidth , vfb - > renderHeight , GPU_DBG_FORMAT_16BIT , true ) ;
2013-09-29 05:03:36 +00:00
if ( vfb - > fbo )
fbo_bind_for_read ( vfb - > fbo ) ;
2013-10-06 00:19:17 +00:00
glReadBuffer ( GL_DEPTH_ATTACHMENT ) ;
2013-09-28 09:14:27 +00:00
glPixelStorei ( GL_PACK_ALIGNMENT , 4 ) ;
2013-10-06 00:19:17 +00:00
glReadPixels ( 0 , 0 , vfb - > renderWidth , vfb - > renderHeight , GL_DEPTH_COMPONENT , GL_UNSIGNED_SHORT , buffer . GetData ( ) ) ;
2013-09-28 09:14:27 +00:00
return true ;
# else
return false ;
# endif
}
bool FramebufferManager : : GetCurrentStencilbuffer ( GPUDebugBuffer & buffer ) {
u32 fb_address = ( gstate . fbptr & 0xFFFFFF ) | ( ( gstate . fbwidth & 0xFF0000 ) < < 8 ) ;
int fb_stride = gstate . fbwidth & 0x3C0 ;
VirtualFramebuffer * vfb = currentRenderVfb_ ;
if ( ! vfb ) {
vfb = GetVFBAt ( fb_address ) ;
}
if ( ! vfb ) {
// If there's no vfb and we're drawing there, must be memory?
2013-10-06 00:19:17 +00:00
// TODO: Actually get the stencil.
buffer = GPUDebugBuffer ( Memory : : GetPointer ( fb_address | 0x04000000 ) , fb_stride , 512 , GPU_DBG_FORMAT_8888 ) ;
2013-09-28 09:14:27 +00:00
return true ;
}
# ifndef USING_GLES2
buffer . Allocate ( vfb - > renderWidth , vfb - > renderHeight , GPU_DBG_FORMAT_16BIT , true ) ;
2013-09-29 05:03:36 +00:00
if ( vfb - > fbo )
fbo_bind_for_read ( vfb - > fbo ) ;
2013-10-06 00:19:17 +00:00
glReadBuffer ( GL_STENCIL_ATTACHMENT ) ;
2013-09-28 09:14:27 +00:00
glPixelStorei ( GL_PACK_ALIGNMENT , 2 ) ;
2013-10-06 00:19:17 +00:00
glReadPixels ( 0 , 0 , vfb - > renderWidth , vfb - > renderHeight , GL_STENCIL_INDEX , GL_UNSIGNED_BYTE , buffer . GetData ( ) ) ;
2013-09-28 09:14:27 +00:00
return true ;
# else
return false ;
# endif
}