2013-04-18 03:29:41 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
#include "Common.h"
|
2012-12-17 20:54:20 +00:00
|
|
|
#include <math.h>
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-08-20 11:51:39 +00:00
|
|
|
#include "Core.h"
|
2011-02-07 22:01:08 +00:00
|
|
|
#include "../../Plugin_VideoOGL/Src/GLUtil.h"
|
2013-03-25 23:05:46 +00:00
|
|
|
#include "RasterFont.h"
|
2011-02-03 19:55:30 +00:00
|
|
|
#include "SWRenderer.h"
|
|
|
|
#include "SWStatistics.h"
|
2013-08-20 11:51:39 +00:00
|
|
|
#include "SWCommandProcessor.h"
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-04-13 05:48:53 +00:00
|
|
|
#include "OnScreenDisplay.h"
|
|
|
|
|
2010-06-09 01:37:08 +00:00
|
|
|
static GLuint s_RenderTarget = 0;
|
|
|
|
|
2012-12-17 20:54:20 +00:00
|
|
|
static GLint attr_pos = -1, attr_tex = -1;
|
|
|
|
static GLint uni_tex = -1;
|
|
|
|
static GLuint program;
|
|
|
|
|
2013-08-20 11:51:39 +00:00
|
|
|
static u8 s_xfbColorTexture[2][EFB_WIDTH*EFB_HEIGHT*4];
|
|
|
|
static int s_currentColorTexture = 0;
|
|
|
|
|
2012-12-24 15:37:12 +00:00
|
|
|
// Rasterfont isn't compatible with GLES
|
2013-03-17 11:37:37 +00:00
|
|
|
// degasus: I think it does, but I can't test it
|
2012-12-17 20:54:20 +00:00
|
|
|
#ifndef USE_GLES
|
2013-03-25 23:05:46 +00:00
|
|
|
RasterFont* s_pfont = NULL;
|
2012-12-17 20:54:20 +00:00
|
|
|
#endif
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2011-02-03 19:55:30 +00:00
|
|
|
void SWRenderer::Init()
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-08-20 11:51:39 +00:00
|
|
|
GLInterface->SetBackBufferDimensions(EFB_WIDTH, EFB_HEIGHT);
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2011-02-03 19:55:30 +00:00
|
|
|
void SWRenderer::Shutdown()
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2012-12-17 20:54:20 +00:00
|
|
|
glDeleteProgram(program);
|
2012-12-26 16:33:45 +00:00
|
|
|
glDeleteTextures(1, &s_RenderTarget);
|
2012-12-17 20:54:20 +00:00
|
|
|
#ifndef USE_GLES
|
|
|
|
delete s_pfont;
|
|
|
|
s_pfont = 0;
|
|
|
|
#endif
|
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2012-12-17 20:54:20 +00:00
|
|
|
void CreateShaders()
|
|
|
|
{
|
|
|
|
static const char *fragShaderText =
|
|
|
|
"varying " PREC " vec2 TexCoordOut;\n"
|
2013-01-19 06:51:00 +00:00
|
|
|
"uniform sampler2D Texture;\n"
|
2012-12-17 20:54:20 +00:00
|
|
|
"void main() {\n"
|
|
|
|
" " PREC " vec4 tmpcolor;\n"
|
2013-01-19 06:51:00 +00:00
|
|
|
" tmpcolor = texture2D(Texture, TexCoordOut);\n"
|
2012-12-17 20:54:20 +00:00
|
|
|
" gl_FragColor = tmpcolor;\n"
|
|
|
|
"}\n";
|
|
|
|
static const char *vertShaderText =
|
|
|
|
"attribute vec4 pos;\n"
|
|
|
|
"attribute vec2 TexCoordIn;\n "
|
|
|
|
"varying vec2 TexCoordOut;\n "
|
|
|
|
"void main() {\n"
|
2013-01-19 06:51:00 +00:00
|
|
|
" gl_Position = pos;\n"
|
2012-12-17 20:54:20 +00:00
|
|
|
" TexCoordOut = TexCoordIn;\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
program = OpenGL_CompileProgram(vertShaderText, fragShaderText);
|
|
|
|
|
|
|
|
glUseProgram(program);
|
|
|
|
|
|
|
|
uni_tex = glGetUniformLocation(program, "Texture");
|
|
|
|
attr_pos = glGetAttribLocation(program, "pos");
|
|
|
|
attr_tex = glGetAttribLocation(program, "TexCoordIn");
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2011-02-03 19:55:30 +00:00
|
|
|
void SWRenderer::Prepare()
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-08-20 11:51:39 +00:00
|
|
|
memset(s_xfbColorTexture, 0, sizeof(s_xfbColorTexture));
|
|
|
|
s_currentColorTexture = 0;
|
|
|
|
|
2012-12-17 20:54:20 +00:00
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
2012-12-26 16:33:45 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 4); // 4-byte pixel alignment
|
|
|
|
glGenTextures(1, &s_RenderTarget);
|
2012-12-17 20:54:20 +00:00
|
|
|
|
|
|
|
CreateShaders();
|
2012-12-26 18:54:58 +00:00
|
|
|
// TODO: Enable for GLES once RasterFont supports GLES
|
2012-12-17 20:54:20 +00:00
|
|
|
#ifndef USE_GLES
|
2013-03-25 23:05:46 +00:00
|
|
|
s_pfont = new RasterFont();
|
2013-01-19 06:51:00 +00:00
|
|
|
glEnable(GL_TEXTURE_2D);
|
2012-12-17 20:54:20 +00:00
|
|
|
#endif
|
|
|
|
GL_REPORT_ERRORD();
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2011-02-03 19:55:30 +00:00
|
|
|
void SWRenderer::RenderText(const char* pstr, int left, int top, u32 color)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2012-12-24 15:37:12 +00:00
|
|
|
#ifndef USE_GLES
|
2012-12-26 19:15:19 +00:00
|
|
|
int nBackbufferWidth = (int)GLInterface->GetBackBufferWidth();
|
|
|
|
int nBackbufferHeight = (int)GLInterface->GetBackBufferHeight();
|
2013-03-25 23:05:46 +00:00
|
|
|
glColor4f(((color>>16) & 0xff)/255.0f, ((color>> 8) & 0xff)/255.0f,
|
|
|
|
((color>> 0) & 0xff)/255.0f, ((color>>24) & 0xFF)/255.0f);
|
2010-06-09 01:37:08 +00:00
|
|
|
s_pfont->printMultilineText(pstr,
|
2013-03-25 23:05:46 +00:00
|
|
|
left * 2.0f / (float)nBackbufferWidth - 1,
|
|
|
|
1 - top * 2.0f / (float)nBackbufferHeight,
|
|
|
|
0, nBackbufferWidth, nBackbufferHeight);
|
2012-12-17 20:54:20 +00:00
|
|
|
#endif
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2011-02-03 19:55:30 +00:00
|
|
|
void SWRenderer::DrawDebugText()
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2012-12-26 16:33:45 +00:00
|
|
|
char debugtext_buffer[8192];
|
2010-06-09 01:37:08 +00:00
|
|
|
char *p = debugtext_buffer;
|
|
|
|
p[0] = 0;
|
|
|
|
|
2011-01-29 04:52:19 +00:00
|
|
|
if (g_SWVideoConfig.bShowStats)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
p+=sprintf(p,"Objects: %i\n",swstats.thisFrame.numDrawnObjects);
|
|
|
|
p+=sprintf(p,"Primitives: %i\n",swstats.thisFrame.numPrimatives);
|
|
|
|
p+=sprintf(p,"Vertices Loaded: %i\n",swstats.thisFrame.numVerticesLoaded);
|
2011-02-03 19:55:30 +00:00
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
p+=sprintf(p,"Triangles Input: %i\n",swstats.thisFrame.numTrianglesIn);
|
|
|
|
p+=sprintf(p,"Triangles Rejected: %i\n",swstats.thisFrame.numTrianglesRejected);
|
|
|
|
p+=sprintf(p,"Triangles Culled: %i\n",swstats.thisFrame.numTrianglesCulled);
|
|
|
|
p+=sprintf(p,"Triangles Clipped: %i\n",swstats.thisFrame.numTrianglesClipped);
|
|
|
|
p+=sprintf(p,"Triangles Drawn: %i\n",swstats.thisFrame.numTrianglesDrawn);
|
2011-02-03 19:55:30 +00:00
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
p+=sprintf(p,"Rasterized Pix: %i\n",swstats.thisFrame.rasterizedPixels);
|
|
|
|
p+=sprintf(p,"TEV Pix In: %i\n",swstats.thisFrame.tevPixelsIn);
|
|
|
|
p+=sprintf(p,"TEV Pix Out: %i\n",swstats.thisFrame.tevPixelsOut);
|
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
// Render a shadow, and then the text.
|
2011-02-03 19:55:30 +00:00
|
|
|
SWRenderer::RenderText(debugtext_buffer, 21, 21, 0xDD000000);
|
|
|
|
SWRenderer::RenderText(debugtext_buffer, 20, 20, 0xFFFFFF00);
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
2013-03-25 02:06:34 +00:00
|
|
|
#ifdef ANDROID
|
2013-04-15 04:36:00 +00:00
|
|
|
// XXX: This /really/ shouldn't be here
|
|
|
|
// But for now, we don't have a generic way for all backends to draw the buttons on screen.
|
|
|
|
// Once that is implemented, we can remove this
|
2013-04-15 01:39:56 +00:00
|
|
|
void DrawButton(GLuint tex, float *coords)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-03-25 02:06:34 +00:00
|
|
|
//Texture rectangle uses pixel coordinates
|
2013-03-20 02:53:09 +00:00
|
|
|
#ifndef USE_GLES
|
2013-03-25 02:06:34 +00:00
|
|
|
GLfloat u_max = (GLfloat)width;
|
|
|
|
GLfloat v_max = (GLfloat)height;
|
|
|
|
|
|
|
|
static const GLfloat texverts[4][2] = {
|
|
|
|
{0, v_max},
|
|
|
|
{0, 0},
|
|
|
|
{u_max, 0},
|
|
|
|
{u_max, v_max}
|
|
|
|
};
|
2013-03-20 02:53:09 +00:00
|
|
|
#else
|
2013-03-25 02:06:34 +00:00
|
|
|
static const GLfloat texverts[4][2] = {
|
|
|
|
{0, 1},
|
|
|
|
{0, 0},
|
|
|
|
{1, 0},
|
|
|
|
{1, 1}
|
|
|
|
};
|
2013-03-20 02:53:09 +00:00
|
|
|
#endif
|
2013-03-25 02:06:34 +00:00
|
|
|
glBindTexture(TEX2D, tex);
|
|
|
|
|
2013-04-15 01:39:56 +00:00
|
|
|
glVertexAttribPointer(attr_pos, 2, GL_FLOAT, GL_FALSE, 0, coords);
|
2013-03-25 02:06:34 +00:00
|
|
|
glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, texverts);
|
|
|
|
glEnableVertexAttribArray(attr_pos);
|
|
|
|
glEnableVertexAttribArray(attr_tex);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glUniform1i(uni_tex, 0);
|
|
|
|
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
|
|
|
glDisableVertexAttribArray(attr_pos);
|
|
|
|
glDisableVertexAttribArray(attr_tex);
|
|
|
|
|
|
|
|
glBindTexture(TEX2D, 0);
|
2013-03-20 02:53:09 +00:00
|
|
|
}
|
2013-03-25 02:06:34 +00:00
|
|
|
#endif
|
2013-08-20 11:51:39 +00:00
|
|
|
|
2013-11-23 07:04:37 +00:00
|
|
|
u8* SWRenderer::getColorTexture() {
|
|
|
|
return s_xfbColorTexture[!s_currentColorTexture];
|
|
|
|
}
|
|
|
|
|
|
|
|
void SWRenderer::swapColorTexture() {
|
|
|
|
s_currentColorTexture = !s_currentColorTexture;
|
|
|
|
}
|
|
|
|
|
2013-11-23 05:38:15 +00:00
|
|
|
void SWRenderer::UpdateColorTexture(EfbInterface::yuv422_packed *xfb, u32 fbWidth, u32 fbHeight)
|
2013-08-20 11:51:39 +00:00
|
|
|
{
|
|
|
|
u32 offset = 0;
|
2013-11-23 07:04:37 +00:00
|
|
|
u8 *TexturePointer = getColorTexture();
|
2013-08-20 11:51:39 +00:00
|
|
|
|
2013-11-23 05:38:15 +00:00
|
|
|
for (u16 y = 0; y < fbHeight; y++)
|
2013-08-20 11:51:39 +00:00
|
|
|
{
|
2013-11-23 05:38:15 +00:00
|
|
|
for (u16 x = 0; x < fbWidth; x+=2)
|
2013-08-20 11:51:39 +00:00
|
|
|
{
|
|
|
|
// We do this one color sample (aka 2 RGB pixles) at a time
|
|
|
|
int Y1 = xfb[x].Y - 16;
|
|
|
|
int Y2 = xfb[x+1].Y - 16;
|
|
|
|
int U = int(xfb[x].UV) - 128;
|
|
|
|
int V = int(xfb[x+1].UV) - 128;
|
|
|
|
|
|
|
|
// We do the inverse BT.601 conversion for YCbCr to RGB
|
|
|
|
// http://www.equasys.de/colorconversion.html#YCbCr-RGBColorFormatConversion
|
|
|
|
TexturePointer[offset++] = min(255.0f, max(0.0f, 1.164f * Y1 + 1.596f * V));
|
|
|
|
TexturePointer[offset++] = min(255.0f, max(0.0f, 1.164f * Y1 - 0.392f * U - 0.813f * V));
|
|
|
|
TexturePointer[offset++] = min(255.0f, max(0.0f, 1.164f * Y1 + 2.017f * U ));
|
|
|
|
TexturePointer[offset++] = 255;
|
|
|
|
|
|
|
|
TexturePointer[offset++] = min(255.0f, max(0.0f, 1.164f * Y2 + 1.596f * V));
|
|
|
|
TexturePointer[offset++] = min(255.0f, max(0.0f, 1.164f * Y2 - 0.392f * U - 0.813f * V));
|
|
|
|
TexturePointer[offset++] = min(255.0f, max(0.0f, 1.164f * Y2 + 2.017f * U ));
|
|
|
|
TexturePointer[offset++] = 255;
|
|
|
|
}
|
2013-11-23 05:38:15 +00:00
|
|
|
xfb += fbWidth;
|
2013-08-20 11:51:39 +00:00
|
|
|
}
|
2013-11-23 07:04:37 +00:00
|
|
|
swapColorTexture();
|
2013-08-20 11:51:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called on the GPU thread
|
|
|
|
void SWRenderer::Swap(u32 fbWidth, u32 fbHeight)
|
|
|
|
{
|
|
|
|
GLInterface->Update(); // just updates the render window position and the backbuffer size
|
|
|
|
if (!g_SWVideoConfig.bHwRasterizer)
|
|
|
|
SWRenderer::DrawTexture(s_xfbColorTexture[s_currentColorTexture], fbWidth, fbHeight);
|
|
|
|
|
|
|
|
swstats.frameCount++;
|
|
|
|
SWRenderer::SwapBuffer();
|
|
|
|
Core::Callback_VideoCopiedToXFB(true); // FIXME: should this function be called FrameRendered?
|
|
|
|
}
|
|
|
|
|
2011-02-03 19:55:30 +00:00
|
|
|
void SWRenderer::DrawTexture(u8 *texture, int width, int height)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-08-20 11:51:39 +00:00
|
|
|
// FIXME: This should add black bars when the game has set the VI to render less than the full xfb.
|
|
|
|
|
2012-12-26 16:33:45 +00:00
|
|
|
GLsizei glWidth = (GLsizei)GLInterface->GetBackBufferWidth();
|
2012-12-17 20:54:20 +00:00
|
|
|
GLsizei glHeight = (GLsizei)GLInterface->GetBackBufferHeight();
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-08-20 11:51:39 +00:00
|
|
|
|
2010-06-09 01:37:08 +00:00
|
|
|
// Update GLViewPort
|
|
|
|
glViewport(0, 0, glWidth, glHeight);
|
2012-12-26 16:33:45 +00:00
|
|
|
glScissor(0, 0, glWidth, glHeight);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-01-19 06:51:00 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, s_RenderTarget);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-01-19 06:51:00 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
2012-12-17 20:54:20 +00:00
|
|
|
|
2013-01-19 08:18:39 +00:00
|
|
|
glUseProgram(program);
|
2012-12-17 20:54:20 +00:00
|
|
|
static const GLfloat verts[4][2] = {
|
|
|
|
{ -1, -1}, // Left top
|
|
|
|
{ -1, 1}, // left bottom
|
|
|
|
{ 1, 1}, // right bottom
|
|
|
|
{ 1, -1} // right top
|
|
|
|
};
|
|
|
|
static const GLfloat texverts[4][2] = {
|
|
|
|
{0, 1},
|
|
|
|
{0, 0},
|
|
|
|
{1, 0},
|
|
|
|
{1, 1}
|
|
|
|
};
|
2013-01-19 08:18:39 +00:00
|
|
|
|
2012-12-17 20:54:20 +00:00
|
|
|
glVertexAttribPointer(attr_pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
|
|
|
|
glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, texverts);
|
2012-12-26 16:33:45 +00:00
|
|
|
glEnableVertexAttribArray(attr_pos);
|
2012-12-17 20:54:20 +00:00
|
|
|
glEnableVertexAttribArray(attr_tex);
|
2013-04-14 03:54:02 +00:00
|
|
|
glUniform1i(uni_tex, 0);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
2012-12-26 16:33:45 +00:00
|
|
|
glDisableVertexAttribArray(attr_pos);
|
2012-12-17 20:54:20 +00:00
|
|
|
glDisableVertexAttribArray(attr_tex);
|
|
|
|
|
2013-01-19 06:51:00 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2010-06-09 01:37:08 +00:00
|
|
|
GL_REPORT_ERRORD();
|
|
|
|
}
|
|
|
|
|
2011-02-03 19:55:30 +00:00
|
|
|
void SWRenderer::SwapBuffer()
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-04-13 05:48:53 +00:00
|
|
|
// Do our OSD callbacks
|
|
|
|
OSD::DoCallbacks(OSD::OSD_ONFRAME);
|
|
|
|
|
2012-12-26 16:33:45 +00:00
|
|
|
DrawDebugText();
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2012-12-26 16:33:45 +00:00
|
|
|
glFlush();
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2012-12-26 06:34:09 +00:00
|
|
|
GLInterface->Swap();
|
2013-04-14 03:54:02 +00:00
|
|
|
|
2013-01-19 06:51:00 +00:00
|
|
|
swstats.ResetFrame();
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
GL_REPORT_ERRORD();
|
|
|
|
}
|