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"
|
|
|
|
#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-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"
|
|
|
|
" gl_FragColor = texture2D(sampler0, v_texcoord0);\n"
|
|
|
|
"}\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 vec4 v_color;\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) {
|
|
|
|
return (addr1 & 0x3FFFFFF) == (addr2 & 0x3FFFFFF);
|
|
|
|
}
|
|
|
|
|
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-05-08 13:22:45 +00:00
|
|
|
#ifdef BLACKBERRY10
|
|
|
|
// 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-01-30 20:09:53 +00:00
|
|
|
FramebufferManager::FramebufferManager() :
|
|
|
|
displayFramebufPtr_(0),
|
|
|
|
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-06-05 21:26:51 +00:00
|
|
|
drawPixelsTexFormat_(-1),
|
|
|
|
convBuf(0)
|
2013-01-30 20:09:53 +00:00
|
|
|
{
|
2012-11-01 15:19:01 +00:00
|
|
|
draw2dprogram = glsl_create_source(basic_vs, tex_fs);
|
|
|
|
|
|
|
|
glsl_bind(draw2dprogram);
|
|
|
|
glUniform1i(draw2dprogram->sampler0, 0);
|
|
|
|
glsl_unbind();
|
|
|
|
|
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-02-22 21:14:17 +00:00
|
|
|
glstate.depthWrite.set(GL_TRUE);
|
|
|
|
glstate.colorMask.set(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
2013-02-23 08:23:24 +00:00
|
|
|
glClearColor(0,0,0,1);
|
2013-01-14 18:26:10 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
|
|
|
|
2013-04-27 22:46:10 +00:00
|
|
|
useBufferedRendering_ = g_Config.bBufferedRendering;
|
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_);
|
2012-11-01 15:19:01 +00:00
|
|
|
glsl_destroy(draw2dprogram);
|
2012-12-01 01:15:46 +00:00
|
|
|
delete [] convBuf;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-12-01 01:15:46 +00:00
|
|
|
void FramebufferManager::DrawPixels(const u8 *framebuf, int 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);
|
|
|
|
|
|
|
|
switch (pixelFormat) {
|
|
|
|
case PSP_DISPLAY_PIXEL_FORMAT_8888:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
if (pixelFormat != PSP_DISPLAY_PIXEL_FORMAT_8888 || linesize != 512) {
|
|
|
|
if (!convBuf) {
|
|
|
|
convBuf = new u8[512 * 272 * 4];
|
|
|
|
}
|
|
|
|
for (int y = 0; y < 272; y++) {
|
|
|
|
switch (pixelFormat) {
|
|
|
|
case PSP_DISPLAY_PIXEL_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-06-05 21:26:51 +00:00
|
|
|
case PSP_DISPLAY_PIXEL_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-06-05 21:26:51 +00:00
|
|
|
case PSP_DISPLAY_PIXEL_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-06-05 21:26:51 +00:00
|
|
|
case PSP_DISPLAY_PIXEL_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;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-05 21:03:23 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D,drawPixelsTex_);
|
2013-06-01 17:28:23 +00:00
|
|
|
if (g_Config.bLinearFiltering)
|
|
|
|
{
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
}
|
2013-06-05 21:26:51 +00:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,512,272, GL_RGBA, GL_UNSIGNED_BYTE, pixelFormat == PSP_DISPLAY_PIXEL_FORMAT_8888 ? framebuf : convBuf);
|
2013-02-12 21:09:14 +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:26:51 +00:00
|
|
|
DrawActiveTexture(x, y, w, h, false, 480.0f / 512.0f);
|
2012-11-19 22:29:14 +00:00
|
|
|
}
|
|
|
|
|
2013-06-05 21:26:51 +00:00
|
|
|
void FramebufferManager::DrawActiveTexture(float x, float y, float w, float h, bool flip, float uscale) {
|
|
|
|
float u2 = uscale;
|
2012-11-23 11:43:31 +00:00
|
|
|
float v1 = flip ? 1.0f : 0.0f;
|
|
|
|
float v2 = flip ? 0.0f : 1.0f;
|
2012-11-19 22:29:14 +00:00
|
|
|
|
2013-02-12 21:09:14 +00:00
|
|
|
const float pos[12] = {x,y,0, x+w,y,0, x+w,y+h,0, x,y+h,0};
|
2012-11-19 22:29:14 +00:00
|
|
|
const float texCoords[8] = {0, v1, u2, v1, u2, v2, 0, v2};
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
glsl_bind(draw2dprogram);
|
2012-11-19 22:29:14 +00:00
|
|
|
Matrix4x4 ortho;
|
2013-02-12 21:09:14 +00:00
|
|
|
ortho.setOrtho(0, (float)PSP_CoreParameter().pixelWidth, (float)PSP_CoreParameter().pixelHeight, 0, -1, 1);
|
2012-11-19 22:29:14 +00:00
|
|
|
glUniformMatrix4fv(draw2dprogram->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);
|
2012-11-01 15:19:01 +00:00
|
|
|
glEnableVertexAttribArray(draw2dprogram->a_position);
|
|
|
|
glEnableVertexAttribArray(draw2dprogram->a_texcoord0);
|
|
|
|
glVertexAttribPointer(draw2dprogram->a_position, 3, GL_FLOAT, GL_FALSE, 12, pos);
|
|
|
|
glVertexAttribPointer(draw2dprogram->a_texcoord0, 2, GL_FLOAT, GL_FALSE, 8, texCoords);
|
|
|
|
glDrawArrays(GL_TRIANGLE_FAN, 0, 4); // TODO: TRIANGLE_STRIP is more likely to be optimized.
|
|
|
|
glDisableVertexAttribArray(draw2dprogram->a_position);
|
|
|
|
glDisableVertexAttribArray(draw2dprogram->a_texcoord0);
|
|
|
|
glsl_unbind();
|
|
|
|
}
|
2013-01-30 20:09:53 +00:00
|
|
|
|
2013-02-21 20:37:19 +00:00
|
|
|
VirtualFramebuffer *FramebufferManager::GetDisplayFBO() {
|
2013-05-04 22:28:57 +00:00
|
|
|
VirtualFramebuffer *match = NULL;
|
2013-01-30 20:09:53 +00:00
|
|
|
for (auto iter = vfbs_.begin(); iter != vfbs_.end(); ++iter) {
|
|
|
|
VirtualFramebuffer *v = *iter;
|
|
|
|
if (MaskedEqual(v->fb_address, displayFramebufPtr_) && v->format == displayFormat_) {
|
|
|
|
// Could check w too but whatever
|
2013-05-04 22:28:57 +00:00
|
|
|
if (match == NULL || match->last_frame_used < v->last_frame_used) {
|
|
|
|
match = v;
|
|
|
|
}
|
2013-01-30 20:09:53 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-04 22:28:57 +00:00
|
|
|
if (match != NULL) {
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
2013-01-30 20:09:53 +00:00
|
|
|
DEBUG_LOG(HLE, "Finding no FBO matching address %08x", displayFramebufPtr_);
|
|
|
|
#if 0 // defined(_DEBUG)
|
|
|
|
std::string debug = "FBOs: ";
|
|
|
|
for (auto iter = vfbs_.begin(); iter != vfbs_.end(); ++iter) {
|
|
|
|
char temp[256];
|
|
|
|
sprintf(temp, "%08x %i %i", (*iter)->fb_address, (*iter)->width, (*iter)->height);
|
|
|
|
debug += std::string(temp);
|
|
|
|
}
|
|
|
|
ERROR_LOG(HLE, "FBOs: %s", debug.c_str());
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-15 23:40:37 +00:00
|
|
|
void GetViewportDimensions(int &w, int &h) {
|
2013-01-31 23:18:23 +00:00
|
|
|
float vpXa = getFloat24(gstate.viewportx1);
|
|
|
|
float vpYa = getFloat24(gstate.viewporty1);
|
2013-03-15 23:40:37 +00:00
|
|
|
w = (int)fabsf(vpXa * 2);
|
|
|
|
h = (int)fabsf(vpYa * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Heuristics to figure out the size of FBO to create.
|
|
|
|
void GuessDrawingSize(int &drawing_width, int &drawing_height) {
|
|
|
|
GetViewportDimensions(drawing_width, drawing_height);
|
|
|
|
|
|
|
|
// HACK for first frame where some games don't init things right
|
|
|
|
if (drawing_width <= 1 && drawing_height <= 1) {
|
|
|
|
drawing_width = 480;
|
|
|
|
drawing_height = 272;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, cap using scissor. Hm, no, this doesn't work so well.
|
|
|
|
/*
|
|
|
|
if (drawing_width > gstate.getScissorX2() + 1)
|
|
|
|
drawing_width = gstate.getScissorX2() + 1;
|
|
|
|
if (drawing_height > gstate.getScissorY2() + 1)
|
|
|
|
drawing_height = gstate.getScissorY2() + 1;*/
|
2013-03-24 23:45:50 +00:00
|
|
|
|
|
|
|
// Cap at maximum texture size for now. Don't see much point in drawing bigger.
|
|
|
|
drawing_width = std::min(drawing_width, 512);
|
|
|
|
drawing_height = std::min(drawing_height, 512);
|
2013-01-31 23:18:23 +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_) {
|
|
|
|
currentRenderVfb_->last_frame_used = gpuStats.numFrames;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gstate_c.framebufChanged = false;
|
|
|
|
|
2013-01-30 20:09:53 +00:00
|
|
|
// Get parameters
|
|
|
|
u32 fb_address = (gstate.fbptr & 0xFFE000) | ((gstate.fbwidth & 0xFF0000) << 8);
|
|
|
|
int fb_stride = gstate.fbwidth & 0x3C0;
|
|
|
|
|
|
|
|
u32 z_address = (gstate.zbptr & 0xFFE000) | ((gstate.zbwidth & 0xFF0000) << 8);
|
|
|
|
int z_stride = gstate.zbwidth & 0x3C0;
|
|
|
|
|
|
|
|
// Yeah this is not completely right. but it'll do for now.
|
2013-01-31 23:18:23 +00:00
|
|
|
//int drawing_width = ((gstate.region2) & 0x3FF) + 1;
|
|
|
|
//int drawing_height = ((gstate.region2 >> 10) & 0x3FF) + 1;
|
|
|
|
|
|
|
|
// 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-01-30 20:09:53 +00:00
|
|
|
int fmt = gstate.framebufpixformat & 3;
|
|
|
|
|
2013-03-15 23:40:37 +00:00
|
|
|
int drawing_width, drawing_height;
|
|
|
|
GuessDrawingSize(drawing_width, drawing_height);
|
|
|
|
|
2013-01-30 20:09:53 +00:00
|
|
|
// Find a matching framebuffer
|
|
|
|
VirtualFramebuffer *vfb = 0;
|
|
|
|
for (auto iter = vfbs_.begin(); iter != vfbs_.end(); ++iter) {
|
|
|
|
VirtualFramebuffer *v = *iter;
|
|
|
|
if (MaskedEqual(v->fb_address, fb_address) && v->width == drawing_width && v->height == drawing_height && v->format == fmt) {
|
|
|
|
// 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;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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-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) {
|
|
|
|
case GE_FORMAT_4444:
|
|
|
|
vfb->colorDepth = FBO_4444;
|
|
|
|
break;
|
|
|
|
case GE_FORMAT_5551:
|
|
|
|
vfb->colorDepth = FBO_5551;
|
|
|
|
break;
|
|
|
|
case GE_FORMAT_565:
|
|
|
|
vfb->colorDepth = FBO_565;
|
|
|
|
break;
|
|
|
|
case GE_FORMAT_8888:
|
|
|
|
vfb->colorDepth = FBO_8888;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
vfb->colorDepth = FBO_8888;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-02-19 18:07:42 +00:00
|
|
|
|
2013-01-30 20:09:53 +00:00
|
|
|
//#ifdef ANDROID
|
|
|
|
// vfb->colorDepth = FBO_8888;
|
|
|
|
//#endif
|
2013-01-31 23:18:23 +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-03-09 05:46:11 +00:00
|
|
|
fbo_bind_as_render_target(vfb->fbo);
|
|
|
|
} 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-02-21 20:37:19 +00:00
|
|
|
textureCache_->NotifyFramebuffer(vfb->fb_address, vfb);
|
2013-01-30 20:09:53 +00:00
|
|
|
|
|
|
|
vfb->last_frame_used = gpuStats.numFrames;
|
2013-04-12 14:00:59 +00:00
|
|
|
frameLastFramebufUsed = gpuStats.numFrames;
|
2013-01-30 20:09:53 +00:00
|
|
|
vfbs_.push_back(vfb);
|
2013-03-09 13:56:32 +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-01-30 20:09:53 +00:00
|
|
|
glEnable(GL_DITHER);
|
|
|
|
currentRenderVfb_ = vfb;
|
2013-03-09 05:46:11 +00:00
|
|
|
|
2013-01-30 20:09:53 +00:00
|
|
|
INFO_LOG(HLE, "Creating 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
|
|
|
// We already have it!
|
|
|
|
} else if (vfb != currentRenderVfb_) {
|
2013-01-30 20:09:53 +00:00
|
|
|
// Use it as a render target.
|
|
|
|
DEBUG_LOG(HLE, "Switching render target to FBO for %08x", vfb->fb_address);
|
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-01-31 23:18:23 +00:00
|
|
|
vfb->last_frame_used = gpuStats.numFrames;
|
2013-04-12 14:00:59 +00:00
|
|
|
frameLastFramebufUsed = gpuStats.numFrames;
|
2013-03-03 12:00:21 +00:00
|
|
|
vfb->dirtyAfterDisplay = true;
|
|
|
|
|
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
|
|
|
|
textureCache_->NotifyFramebufferDestroyed(vfb->fb_address, vfb);
|
|
|
|
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-02-21 20:37:19 +00:00
|
|
|
textureCache_->NotifyFramebuffer(vfb->fb_address, vfb);
|
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-03-09 05:46:11 +00:00
|
|
|
if (vfb->last_frame_used != gpuStats.numFrames) {
|
2013-02-22 21:14:17 +00:00
|
|
|
glstate.depthWrite.set(GL_TRUE);
|
|
|
|
glstate.colorMask.set(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
2013-02-23 18:17:16 +00:00
|
|
|
glClearColor(0,0,0,1);
|
2013-02-01 23:24:23 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
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 {
|
|
|
|
vfb->last_frame_used = gpuStats.numFrames;
|
2013-04-12 14:00:59 +00:00
|
|
|
frameLastFramebufUsed = gpuStats.numFrames;
|
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();
|
|
|
|
|
|
|
|
VirtualFramebuffer *vfb = GetDisplayFBO();
|
|
|
|
if (!vfb) {
|
2013-03-15 21:50:35 +00:00
|
|
|
if (Memory::IsValidAddress(displayFramebufPtr_)) {
|
|
|
|
// The game is displaying something directly from RAM. In GTA, it's decoded video.
|
|
|
|
DrawPixels(Memory::GetPointer(displayFramebufPtr_), displayFormat_, displayStride_);
|
|
|
|
} else {
|
|
|
|
DEBUG_LOG(HLE, "Found no FBO to display! displayFBPtr = %08x", displayFramebufPtr_);
|
|
|
|
// No framebuffer to display! Clear to black.
|
|
|
|
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-01-30 20:09:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-03 12:00:21 +00:00
|
|
|
vfb->usageFlags |= FB_USAGE_DISPLAYED_FRAMEBUFFER;
|
|
|
|
vfb->dirtyAfterDisplay = false;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
currentRenderVfb_ = 0;
|
|
|
|
|
2013-03-09 05:46:11 +00:00
|
|
|
if (vfb->fbo) {
|
2013-03-03 12:00:21 +00:00
|
|
|
glstate.viewport.set(0, 0, PSP_CoreParameter().pixelWidth, PSP_CoreParameter().pixelHeight);
|
|
|
|
DEBUG_LOG(HLE, "Displaying FBO %08x", vfb->fb_address);
|
|
|
|
glstate.blend.disable();
|
|
|
|
glstate.cullFace.disable();
|
|
|
|
glstate.depthTest.disable();
|
|
|
|
glstate.scissorTest.disable();
|
|
|
|
glstate.stencilTest.disable();
|
|
|
|
|
|
|
|
fbo_bind_color_as_texture(vfb->fbo, 0);
|
2013-02-23 18:44:52 +00:00
|
|
|
|
2013-03-03 12:00:21 +00:00
|
|
|
// These are in the output display coordinates
|
|
|
|
float x, y, w, h;
|
|
|
|
CenterRect(&x, &y, &w, &h, 480.0f, 272.0f, (float)PSP_CoreParameter().pixelWidth, (float)PSP_CoreParameter().pixelHeight);
|
|
|
|
DrawActiveTexture(x, y, w, h, true);
|
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-02-13 17:21:21 +00:00
|
|
|
if (resized_) {
|
2013-02-22 21:14:17 +00:00
|
|
|
glstate.depthWrite.set(GL_TRUE);
|
|
|
|
glstate.colorMask.set(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
2013-02-13 17:21:21 +00:00
|
|
|
glClearColor(0,0,0,1);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
|
|
|
}
|
2013-03-03 12:00:21 +00:00
|
|
|
}
|
2013-01-30 20:09:53 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FramebufferManager::BeginFrame() {
|
|
|
|
DecimateFBOs();
|
|
|
|
// NOTE - this is all wrong. At the beginning of the frame is a TERRIBLE time to draw the fb.
|
|
|
|
if (g_Config.bDisplayFramebuffer && displayFramebufPtr_) {
|
2013-06-05 21:03:23 +00:00
|
|
|
INFO_LOG(HLE, "Drawing the framebuffer (%08x)", displayFramebufPtr_);
|
2013-01-30 20:09:53 +00:00
|
|
|
const u8 *pspframebuf = Memory::GetPointer((0x44000000) | (displayFramebufPtr_ & 0x1FFFFF)); // TODO - check
|
|
|
|
glstate.cullFace.disable();
|
|
|
|
glstate.depthTest.disable();
|
|
|
|
glstate.blend.disable();
|
2013-02-23 18:44:52 +00:00
|
|
|
glstate.scissorTest.disable();
|
|
|
|
glstate.stencilTest.disable();
|
2013-01-30 20:09:53 +00:00
|
|
|
DrawPixels(pspframebuf, displayFormat_, displayStride_);
|
|
|
|
// TODO: restore state?
|
|
|
|
}
|
|
|
|
currentRenderVfb_ = 0;
|
2013-04-27 18:06:31 +00:00
|
|
|
useBufferedRendering_ = g_Config.bBufferedRendering;
|
2013-01-30 20:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FramebufferManager::SetDisplayFramebuffer(u32 framebuf, u32 stride, int format) {
|
2013-03-15 21:50:35 +00:00
|
|
|
|
|
|
|
if ((framebuf & 0x04000000) == 0) {
|
|
|
|
DEBUG_LOG(HLE, "Non-VRAM display framebuffer address set: %08x", framebuf);
|
2013-03-18 15:04:17 +00:00
|
|
|
} else {
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (auto iter = vfbs_.begin(); iter != vfbs_.end(); ++iter) {
|
|
|
|
VirtualFramebuffer *vfb = *iter;
|
|
|
|
|
|
|
|
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-04-12 14:00:59 +00:00
|
|
|
void FramebufferManager::DecimateFBOs() {
|
2013-03-11 21:22:22 +00:00
|
|
|
fbo_unbind();
|
2013-01-30 20:09:53 +00:00
|
|
|
for (auto iter = vfbs_.begin(); iter != vfbs_.end();) {
|
2013-01-31 23:18:23 +00:00
|
|
|
VirtualFramebuffer *vfb = *iter;
|
|
|
|
if (vfb == displayFramebuf_ || vfb == prevDisplayFramebuf_ || vfb == prevPrevDisplayFramebuf_) {
|
2013-01-30 20:09:53 +00:00
|
|
|
++iter;
|
|
|
|
continue;
|
|
|
|
}
|
2013-04-12 14:00:59 +00:00
|
|
|
int age = frameLastFramebufUsed - (*iter)->last_frame_used;
|
|
|
|
if (age > FBO_OLD_AGE) {
|
2013-04-27 18:06:31 +00:00
|
|
|
textureCache_->NotifyFramebufferDestroyed(vfb->fb_address, vfb);
|
2013-03-16 07:54:39 +00:00
|
|
|
INFO_LOG(HLE, "Decimating FBO for %08x (%i x %i x %i), age %i", vfb->fb_address, vfb->width, vfb->height, vfb->format, age)
|
2013-03-03 12:00:21 +00:00
|
|
|
if (vfb->fbo) {
|
|
|
|
fbo_destroy(vfb->fbo);
|
|
|
|
vfb->fbo = 0;
|
|
|
|
}
|
2013-01-31 23:18:23 +00:00
|
|
|
delete vfb;
|
2013-01-30 20:09:53 +00:00
|
|
|
vfbs_.erase(iter++);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FramebufferManager::DestroyAllFBOs() {
|
2013-03-11 21:22:22 +00:00
|
|
|
fbo_unbind();
|
2013-01-30 20:09:53 +00:00
|
|
|
for (auto iter = vfbs_.begin(); iter != vfbs_.end(); ++iter) {
|
2013-01-31 23:18:23 +00:00
|
|
|
VirtualFramebuffer *vfb = *iter;
|
2013-02-21 20:37:19 +00:00
|
|
|
textureCache_->NotifyFramebufferDestroyed(vfb->fb_address, vfb);
|
2013-03-11 18:40:31 +00:00
|
|
|
if (vfb->fbo) {
|
2013-03-15 20:00:03 +00:00
|
|
|
INFO_LOG(HLE, "Destroying 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
|
|
|
fbo_destroy(vfb->fbo);
|
2013-03-11 18:40:31 +00:00
|
|
|
vfb->fbo = 0;
|
|
|
|
}
|
2013-01-31 23:18:23 +00:00
|
|
|
delete vfb;
|
2013-01-30 20:09:53 +00:00
|
|
|
}
|
|
|
|
vfbs_.clear();
|
|
|
|
}
|
2013-01-31 11:14:57 +00:00
|
|
|
|
|
|
|
void FramebufferManager::Resized() {
|
|
|
|
resized_ = true;
|
|
|
|
}
|