2010-06-09 01:37:08 +00:00
|
|
|
// Copyright (C) 2003-2009 Dolphin 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
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
|
|
|
#include "Common.h"
|
2012-12-17 20:54:20 +00:00
|
|
|
#include <math.h>
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2011-02-07 22:01:08 +00:00
|
|
|
#include "../../Plugin_VideoOGL/Src/GLUtil.h"
|
2012-12-17 20:54:20 +00:00
|
|
|
#include "../../Plugin_VideoOGL/Src/RasterFont.h"
|
2011-02-03 19:55:30 +00:00
|
|
|
#include "SWRenderer.h"
|
|
|
|
#include "SWStatistics.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;
|
|
|
|
|
2012-12-24 15:37:12 +00:00
|
|
|
// Rasterfont isn't compatible with GLES
|
2012-12-17 20:54:20 +00:00
|
|
|
#ifndef USE_GLES
|
2010-06-09 01:37:08 +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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
"uniform " TEXTYPE " Texture;\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" " PREC " vec4 tmpcolor;\n"
|
|
|
|
" tmpcolor = " TEXFUNC "(Texture, TexCoordOut);\n"
|
|
|
|
" gl_FragColor = tmpcolor;\n"
|
|
|
|
"}\n";
|
|
|
|
static const char *vertShaderText =
|
|
|
|
"attribute vec4 pos;\n"
|
|
|
|
"attribute vec2 TexCoordIn;\n "
|
|
|
|
"varying vec2 TexCoordOut;\n "
|
|
|
|
"void main() {\n"
|
|
|
|
" gl_Position = pos;\n"
|
|
|
|
" 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
|
|
|
{
|
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
|
2012-12-26 16:33:45 +00:00
|
|
|
s_pfont = new RasterFont();
|
2010-06-09 01:37:08 +00:00
|
|
|
glEnable(GL_TEXTURE_RECTANGLE_ARB);
|
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();
|
2012-12-11 11:37:08 +00:00
|
|
|
|
2010-06-09 01:37:08 +00:00
|
|
|
s_pfont->printMultilineText(pstr,
|
|
|
|
left * 2.0f / (float)nBackbufferWidth - 1,
|
|
|
|
1 - top * 2.0f / (float)nBackbufferHeight,
|
2012-12-11 11:37:08 +00:00
|
|
|
0, nBackbufferWidth, nBackbufferHeight, color);
|
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
|
|
|
{
|
2011-02-03 19:55:30 +00:00
|
|
|
p+=sprintf(p,"Objects: %i\n",swstats.thisFrame.numDrawnObjects);
|
|
|
|
p+=sprintf(p,"Primatives: %i\n",swstats.thisFrame.numPrimatives);
|
|
|
|
p+=sprintf(p,"Vertices Loaded: %i\n",swstats.thisFrame.numVerticesLoaded);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
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
|
|
|
|
|
|
|
// 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
|
|
|
|
2012-12-26 16:33:45 +00:00
|
|
|
glBindTexture(TEX2D, s_RenderTarget);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2012-12-26 16:33:45 +00:00
|
|
|
glTexImage2D(TEX2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
|
|
|
|
glTexParameteri(TEX2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
2012-12-17 20:54:20 +00:00
|
|
|
glTexParameteri(TEX2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2012-12-26 16:33:45 +00:00
|
|
|
GLfloat u_max = (GLfloat)width;
|
2012-12-30 08:12:31 +00:00
|
|
|
GLfloat v_max = (GLfloat)height;
|
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
|
|
|
|
};
|
|
|
|
//Texture rectangle uses pixel coordinates
|
|
|
|
#ifndef USE_GLES
|
|
|
|
static const GLfloat texverts[4][2] = {
|
|
|
|
{0, v_max},
|
|
|
|
{0, 0},
|
|
|
|
{u_max, 0},
|
|
|
|
{u_max, v_max}
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
static const GLfloat texverts[4][2] = {
|
|
|
|
{0, 1},
|
|
|
|
{0, 0},
|
|
|
|
{1, 0},
|
|
|
|
{1, 1}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
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);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glUniform1i(uni_tex, 0);
|
|
|
|
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);
|
|
|
|
|
2012-12-26 16:33:45 +00:00
|
|
|
glBindTexture(TEX2D, 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
|
|
|
{
|
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();
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2012-12-26 16:33:45 +00:00
|
|
|
swstats.ResetFrame();
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2012-12-17 20:54:20 +00:00
|
|
|
#ifndef USE_GLES
|
2012-12-26 16:33:45 +00:00
|
|
|
glClearDepth(1.0f);
|
2012-12-17 20:54:20 +00:00
|
|
|
#endif
|
2010-06-09 01:37:08 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
GL_REPORT_ERRORD();
|
|
|
|
}
|