2020-05-26 23:47:03 +02:00
|
|
|
/* Copyright (C) 2020 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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 for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* Implementation of profile display (containing only display routines,
|
|
|
|
* the data model(s) are implemented elsewhere).
|
2006-04-24 01:14:18 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "ProfileViewer.h"
|
2009-11-06 19:35:32 +01:00
|
|
|
|
2013-10-18 17:53:07 +02:00
|
|
|
#include "graphics/FontMetrics.h"
|
2012-02-12 14:20:49 +01:00
|
|
|
#include "graphics/ShaderManager.h"
|
|
|
|
#include "graphics/TextRenderer.h"
|
2019-08-04 05:52:00 +02:00
|
|
|
#include "gui/GUIMatrix.h"
|
|
|
|
#include "lib/external_libraries/libsdl.h"
|
2006-04-24 01:14:18 +02:00
|
|
|
#include "ps/CLogger.h"
|
2009-11-06 19:35:32 +01:00
|
|
|
#include "ps/Filesystem.h"
|
|
|
|
#include "ps/Hotkey.h"
|
|
|
|
#include "ps/Profile.h"
|
2020-01-02 19:07:40 +01:00
|
|
|
#include "ps/Pyrogenesis.h"
|
2009-11-06 19:35:32 +01:00
|
|
|
#include "renderer/Renderer.h"
|
2011-02-19 04:14:37 +01:00
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2019-08-04 05:52:00 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <ctime>
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
extern int g_xres, g_yres;
|
|
|
|
|
|
|
|
struct CProfileViewerInternals
|
|
|
|
{
|
2015-07-29 03:07:23 +02:00
|
|
|
NONCOPYABLE(CProfileViewerInternals); // because of the ofstream
|
|
|
|
public:
|
2006-04-24 01:14:18 +02:00
|
|
|
CProfileViewerInternals() {}
|
|
|
|
|
|
|
|
/// Whether the profiling display is currently visible
|
|
|
|
bool profileVisible;
|
|
|
|
|
|
|
|
/// List of root tables
|
|
|
|
std::vector<AbstractProfileTable*> rootTables;
|
|
|
|
|
|
|
|
/// Path from a root table (path[0]) to the currently visible table (path[size-1])
|
|
|
|
std::vector<AbstractProfileTable*> path;
|
|
|
|
|
|
|
|
/// Helper functions
|
|
|
|
void TableIsDeleted(AbstractProfileTable* table);
|
|
|
|
void NavigateTree(int id);
|
|
|
|
|
|
|
|
/// File for saved profile output (reset when the game is restarted)
|
|
|
|
std::ofstream outputStream;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// AbstractProfileTable implementation
|
|
|
|
|
|
|
|
AbstractProfileTable::~AbstractProfileTable()
|
|
|
|
{
|
|
|
|
if (CProfileViewer::IsInitialised())
|
|
|
|
{
|
|
|
|
g_ProfileViewer.m->TableIsDeleted(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// CProfileViewer implementation
|
|
|
|
|
|
|
|
|
|
|
|
// AbstractProfileTable got deleted, make sure we have no dangling pointers
|
|
|
|
void CProfileViewerInternals::TableIsDeleted(AbstractProfileTable* table)
|
|
|
|
{
|
|
|
|
for(int idx = (int)rootTables.size()-1; idx >= 0; --idx)
|
|
|
|
{
|
|
|
|
if (rootTables[idx] == table)
|
|
|
|
rootTables.erase(rootTables.begin() + idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(size_t idx = 0; idx < path.size(); ++idx)
|
|
|
|
{
|
|
|
|
if (path[idx] != table)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
path.erase(path.begin() + idx, path.end());
|
|
|
|
if (path.size() == 0)
|
|
|
|
profileVisible = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Move into child tables or return to parent tables based on the given number
|
|
|
|
void CProfileViewerInternals::NavigateTree(int id)
|
|
|
|
{
|
|
|
|
if (id == 0)
|
|
|
|
{
|
|
|
|
if (path.size() > 1)
|
|
|
|
path.pop_back();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AbstractProfileTable* table = path[path.size() - 1];
|
|
|
|
size_t numrows = table->GetNumberRows();
|
|
|
|
|
|
|
|
for(size_t row = 0; row < numrows; ++row)
|
|
|
|
{
|
|
|
|
AbstractProfileTable* child = table->GetChild(row);
|
|
|
|
|
|
|
|
if (!child)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
--id;
|
|
|
|
if (id == 0)
|
|
|
|
{
|
|
|
|
path.push_back(child);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Construction/Destruction
|
|
|
|
CProfileViewer::CProfileViewer()
|
|
|
|
{
|
|
|
|
m = new CProfileViewerInternals;
|
|
|
|
m->profileVisible = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CProfileViewer::~CProfileViewer()
|
|
|
|
{
|
|
|
|
delete m;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Render
|
|
|
|
void CProfileViewer::RenderProfile()
|
|
|
|
{
|
|
|
|
if (!m->profileVisible)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!m->path.size())
|
|
|
|
{
|
|
|
|
m->profileVisible = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-09 14:09:01 +01:00
|
|
|
PROFILE3_GPU("profile viewer");
|
2011-11-04 02:35:50 +01:00
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
|
|
|
|
AbstractProfileTable* table = m->path[m->path.size() - 1];
|
|
|
|
const std::vector<ProfileColumn>& columns = table->GetColumns();
|
|
|
|
size_t numrows = table->GetNumberRows();
|
|
|
|
|
2013-10-18 18:15:42 +02:00
|
|
|
CStrIntern font_name("mono-stroke-10");
|
2013-10-18 17:53:07 +02:00
|
|
|
CFontMetrics font(font_name);
|
2010-05-30 15:42:56 +02:00
|
|
|
int lineSpacing = font.GetLineSpacing();
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
// Render background
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
GLint estimate_height;
|
|
|
|
GLint estimate_width;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
estimate_width = 50;
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
for(size_t i = 0; i < columns.size(); ++i)
|
|
|
|
estimate_width += (GLint)columns[i].width;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
estimate_height = 3 + (GLint)numrows;
|
2006-04-24 01:14:18 +02:00
|
|
|
if (m->path.size() > 1)
|
|
|
|
estimate_height += 2;
|
2010-05-30 15:42:56 +02:00
|
|
|
estimate_height = lineSpacing*estimate_height;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2013-09-29 15:19:52 +02:00
|
|
|
CShaderTechniquePtr solidTech = g_Renderer.GetShaderManager().LoadEffect(str_gui_solid);
|
2012-02-12 21:45:31 +01:00
|
|
|
solidTech->BeginPass();
|
|
|
|
CShaderProgramPtr solidShader = solidTech->GetShader();
|
2012-02-12 14:20:49 +01:00
|
|
|
|
2013-09-29 15:19:52 +02:00
|
|
|
solidShader->Uniform(str_color, 0.0f, 0.0f, 0.0f, 0.5f);
|
2012-02-12 14:20:49 +01:00
|
|
|
|
|
|
|
CMatrix3D transform = GetDefaultGuiMatrix();
|
2013-09-29 15:19:52 +02:00
|
|
|
solidShader->Uniform(str_transform, transform);
|
2012-02-12 14:20:49 +01:00
|
|
|
|
|
|
|
float backgroundVerts[] = {
|
|
|
|
(float)estimate_width, 0.0f,
|
|
|
|
0.0f, 0.0f,
|
|
|
|
0.0f, (float)estimate_height,
|
|
|
|
0.0f, (float)estimate_height,
|
|
|
|
(float)estimate_width, (float)estimate_height,
|
|
|
|
(float)estimate_width, 0.0f
|
|
|
|
};
|
|
|
|
solidShader->VertexPointer(2, GL_FLOAT, 0, backgroundVerts);
|
|
|
|
solidShader->AssertPointersBound();
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
|
|
|
|
|
|
transform.PostTranslate(22.0f, lineSpacing*3.0f, 0.0f);
|
2013-09-29 15:19:52 +02:00
|
|
|
solidShader->Uniform(str_transform, transform);
|
2012-02-12 14:20:49 +01:00
|
|
|
|
|
|
|
// Draw row backgrounds
|
|
|
|
for (size_t row = 0; row < numrows; ++row)
|
|
|
|
{
|
|
|
|
if (row % 2)
|
2013-09-29 15:19:52 +02:00
|
|
|
solidShader->Uniform(str_color, 1.0f, 1.0f, 1.0f, 0.1f);
|
2012-02-12 14:20:49 +01:00
|
|
|
else
|
2013-09-29 15:19:52 +02:00
|
|
|
solidShader->Uniform(str_color, 0.0f, 0.0f, 0.0f, 0.1f);
|
2012-02-12 14:20:49 +01:00
|
|
|
|
|
|
|
float rowVerts[] = {
|
|
|
|
-22.f, 2.f,
|
|
|
|
estimate_width-22.f, 2.f,
|
|
|
|
estimate_width-22.f, 2.f-lineSpacing,
|
|
|
|
|
|
|
|
estimate_width-22.f, 2.f-lineSpacing,
|
|
|
|
-22.f, 2.f-lineSpacing,
|
|
|
|
-22.f, 2.f
|
|
|
|
};
|
|
|
|
solidShader->VertexPointer(2, GL_FLOAT, 0, rowVerts);
|
|
|
|
solidShader->AssertPointersBound();
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
|
|
|
|
|
|
transform.PostTranslate(0.0f, lineSpacing, 0.0f);
|
2013-09-29 15:19:52 +02:00
|
|
|
solidShader->Uniform(str_transform, transform);
|
2012-02-12 14:20:49 +01:00
|
|
|
}
|
|
|
|
|
2012-02-12 21:45:31 +01:00
|
|
|
solidTech->EndPass();
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
// Print table and column titles
|
2012-02-12 14:20:49 +01:00
|
|
|
|
2013-09-29 15:19:52 +02:00
|
|
|
CShaderTechniquePtr textTech = g_Renderer.GetShaderManager().LoadEffect(str_gui_text);
|
2012-02-12 21:45:31 +01:00
|
|
|
textTech->BeginPass();
|
2012-02-12 14:20:49 +01:00
|
|
|
|
2012-02-12 21:45:31 +01:00
|
|
|
CTextRenderer textRenderer(textTech->GetShader());
|
2012-02-12 14:20:49 +01:00
|
|
|
textRenderer.Font(font_name);
|
2012-02-12 21:45:31 +01:00
|
|
|
textRenderer.Color(1.0f, 1.0f, 1.0f);
|
2012-02-12 14:20:49 +01:00
|
|
|
|
|
|
|
textRenderer.PrintfAt(2.0f, lineSpacing, L"%hs", table->GetTitle().c_str());
|
|
|
|
|
|
|
|
textRenderer.Translate(22.0f, lineSpacing*2.0f, 0.0f);
|
|
|
|
|
|
|
|
float colX = 0.0f;
|
|
|
|
for (size_t col = 0; col < columns.size(); ++col)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2012-02-25 18:14:47 +01:00
|
|
|
CStrW text = columns[col].title.FromUTF8();
|
2011-03-03 02:09:19 +01:00
|
|
|
int w, h;
|
2012-02-25 18:14:47 +01:00
|
|
|
font.CalculateStringSize(text.c_str(), w, h);
|
2012-02-12 14:20:49 +01:00
|
|
|
|
|
|
|
float x = colX;
|
2011-03-03 02:09:19 +01:00
|
|
|
if (col > 0) // right-align all but the first column
|
2012-02-12 14:20:49 +01:00
|
|
|
x += columns[col].width - w;
|
2012-02-25 18:14:47 +01:00
|
|
|
textRenderer.Put(x, 0.0f, text.c_str());
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2012-02-12 14:20:49 +01:00
|
|
|
colX += columns[col].width;
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
2012-02-12 14:20:49 +01:00
|
|
|
|
|
|
|
textRenderer.Translate(0.0f, lineSpacing, 0.0f);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
// Print rows
|
|
|
|
int currentExpandId = 1;
|
|
|
|
|
2012-02-12 14:20:49 +01:00
|
|
|
for (size_t row = 0; row < numrows; ++row)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
if (table->IsHighlightRow(row))
|
2012-02-12 21:45:31 +01:00
|
|
|
textRenderer.Color(1.0f, 0.5f, 0.5f);
|
2006-04-24 01:14:18 +02:00
|
|
|
else
|
2012-02-12 21:45:31 +01:00
|
|
|
textRenderer.Color(1.0f, 1.0f, 1.0f);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
if (table->GetChild(row))
|
|
|
|
{
|
2012-02-12 14:20:49 +01:00
|
|
|
textRenderer.PrintfAt(-15.0f, 0.0f, L"%d", currentExpandId);
|
2006-04-24 01:14:18 +02:00
|
|
|
currentExpandId++;
|
|
|
|
}
|
|
|
|
|
2020-11-26 23:28:50 +01:00
|
|
|
float rowColX = 0.0f;
|
2012-02-12 14:20:49 +01:00
|
|
|
for (size_t col = 0; col < columns.size(); ++col)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2012-02-25 18:14:47 +01:00
|
|
|
CStrW text = table->GetCellText(row, col).FromUTF8();
|
2011-03-03 02:09:19 +01:00
|
|
|
int w, h;
|
2012-02-25 18:14:47 +01:00
|
|
|
font.CalculateStringSize(text.c_str(), w, h);
|
2012-02-12 14:20:49 +01:00
|
|
|
|
2020-11-26 23:28:50 +01:00
|
|
|
float x = rowColX;
|
2011-03-03 02:09:19 +01:00
|
|
|
if (col > 0) // right-align all but the first column
|
2012-02-12 14:20:49 +01:00
|
|
|
x += columns[col].width - w;
|
2012-02-25 18:14:47 +01:00
|
|
|
textRenderer.Put(x, 0.0f, text.c_str());
|
2012-02-12 14:20:49 +01:00
|
|
|
|
2020-11-26 23:28:50 +01:00
|
|
|
rowColX += columns[col].width;
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
2012-02-12 14:20:49 +01:00
|
|
|
textRenderer.Translate(0.0f, lineSpacing, 0.0f);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
2012-02-12 14:20:49 +01:00
|
|
|
|
2012-02-12 21:45:31 +01:00
|
|
|
textRenderer.Color(1.0f, 1.0f, 1.0f);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
if (m->path.size() > 1)
|
|
|
|
{
|
2012-02-12 14:20:49 +01:00
|
|
|
textRenderer.Translate(0.0f, lineSpacing, 0.0f);
|
2012-02-25 18:14:47 +01:00
|
|
|
textRenderer.Put(-15.0f, 0.0f, L"0");
|
|
|
|
textRenderer.Put(0.0f, 0.0f, L"back to parent");
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
2012-02-12 14:20:49 +01:00
|
|
|
textRenderer.Render();
|
2012-02-12 21:45:31 +01:00
|
|
|
textTech->EndPass();
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
glDisable(GL_BLEND);
|
2012-02-12 14:20:49 +01:00
|
|
|
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Handle input
|
2006-08-26 23:52:18 +02:00
|
|
|
InReaction CProfileViewer::Input(const SDL_Event_* ev)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2006-08-26 23:52:18 +02:00
|
|
|
switch(ev->ev.type)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
{
|
|
|
|
if (!m->profileVisible)
|
|
|
|
break;
|
|
|
|
|
2012-02-06 23:47:35 +01:00
|
|
|
int k = ev->ev.key.keysym.sym;
|
|
|
|
if (k >= SDLK_0 && k <= SDLK_9)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2012-02-06 23:47:35 +01:00
|
|
|
m->NavigateTree(k - SDLK_0);
|
2006-04-24 01:14:18 +02:00
|
|
|
return IN_HANDLED;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-05-26 23:47:03 +02:00
|
|
|
case SDL_HOTKEYPRESS:
|
2010-10-23 04:37:00 +02:00
|
|
|
std::string hotkey = static_cast<const char*>(ev->ev.user.data1);
|
|
|
|
|
|
|
|
if( hotkey == "profile.toggle" )
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
if (!m->profileVisible)
|
|
|
|
{
|
|
|
|
if (m->rootTables.size())
|
|
|
|
{
|
|
|
|
m->profileVisible = true;
|
|
|
|
m->path.push_back(m->rootTables[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
size_t i;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
for(i = 0; i < m->rootTables.size(); ++i)
|
|
|
|
{
|
|
|
|
if (m->rootTables[i] == m->path[0])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
|
|
|
|
m->path.clear();
|
|
|
|
if (i < m->rootTables.size())
|
|
|
|
{
|
|
|
|
m->path.push_back(m->rootTables[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m->profileVisible = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return( IN_HANDLED );
|
|
|
|
}
|
2010-10-23 04:37:00 +02:00
|
|
|
else if( hotkey == "profile.save" )
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
SaveToFile();
|
|
|
|
return( IN_HANDLED );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return( IN_PASS );
|
|
|
|
}
|
|
|
|
|
2006-08-26 23:52:18 +02:00
|
|
|
InReaction CProfileViewer::InputThunk(const SDL_Event_* ev)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
if (CProfileViewer::IsInitialised())
|
|
|
|
return g_ProfileViewer.Input(ev);
|
|
|
|
|
|
|
|
return IN_PASS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Add a table to the list of roots
|
2010-08-02 21:23:58 +02:00
|
|
|
void CProfileViewer::AddRootTable(AbstractProfileTable* table, bool front)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2010-08-02 21:23:58 +02:00
|
|
|
if (front)
|
|
|
|
m->rootTables.insert(m->rootTables.begin(), table);
|
|
|
|
else
|
|
|
|
m->rootTables.push_back(table);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2020-12-31 15:27:02 +01:00
|
|
|
class WriteTable
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2020-12-31 15:27:02 +01:00
|
|
|
public:
|
|
|
|
WriteTable(std::ofstream& outputStream) : m_OutputStream(outputStream) {}
|
|
|
|
WriteTable(const WriteTable& writeTable) = default;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
void operator() (AbstractProfileTable* table)
|
|
|
|
{
|
|
|
|
std::vector<CStr> data; // 2d array of (rows+head)*columns elements
|
|
|
|
|
2010-10-31 23:00:28 +01:00
|
|
|
const std::vector<ProfileColumn>& columns = table->GetColumns();
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
// Add column headers to 'data'
|
2010-10-31 23:00:28 +01:00
|
|
|
for (std::vector<ProfileColumn>::const_iterator col_it = columns.begin();
|
|
|
|
col_it != columns.end(); ++col_it)
|
2006-04-24 01:14:18 +02:00
|
|
|
data.push_back(col_it->title);
|
|
|
|
|
|
|
|
// Recursively add all profile data to 'data'
|
|
|
|
WriteRows(1, table, data);
|
|
|
|
|
|
|
|
// Calculate the width of each column ( = the maximum width of
|
|
|
|
// any value in that column)
|
|
|
|
std::vector<size_t> columnWidths;
|
2010-10-31 23:00:28 +01:00
|
|
|
size_t cols = columns.size();
|
2006-04-24 01:14:18 +02:00
|
|
|
for (size_t c = 0; c < cols; ++c)
|
|
|
|
{
|
|
|
|
size_t max = 0;
|
|
|
|
for (size_t i = c; i < data.size(); i += cols)
|
|
|
|
max = std::max(max, data[i].length());
|
|
|
|
columnWidths.push_back(max);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output data as a formatted table:
|
|
|
|
|
2020-12-31 15:27:02 +01:00
|
|
|
m_OutputStream << "\n\n" << table->GetTitle() << "\n";
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2013-01-21 21:33:25 +01:00
|
|
|
if (cols == 0) // avoid divide-by-zero
|
|
|
|
return;
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
for (size_t r = 0; r < data.size()/cols; ++r)
|
|
|
|
{
|
|
|
|
for (size_t c = 0; c < cols; ++c)
|
2020-12-31 15:27:02 +01:00
|
|
|
m_OutputStream << (c ? " | " : "\n")
|
2006-04-24 01:14:18 +02:00
|
|
|
<< data[r*cols + c].Pad(PS_TRIM_RIGHT, columnWidths[c]);
|
|
|
|
|
|
|
|
// Add dividers under some rows. (Currently only the first, since
|
|
|
|
// that contains the column headers.)
|
|
|
|
if (r == 0)
|
|
|
|
for (size_t c = 0; c < cols; ++c)
|
2020-12-31 15:27:02 +01:00
|
|
|
m_OutputStream << (c ? "-|-" : "\n")
|
2006-04-24 01:14:18 +02:00
|
|
|
<< CStr::Repeat("-", columnWidths[c]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteRows(int indent, AbstractProfileTable* table, std::vector<CStr>& data)
|
|
|
|
{
|
2010-10-31 23:00:28 +01:00
|
|
|
const std::vector<ProfileColumn>& columns = table->GetColumns();
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
for (size_t r = 0; r < table->GetNumberRows(); ++r)
|
|
|
|
{
|
|
|
|
// Do pretty tree-structure indenting
|
|
|
|
CStr indentation = CStr::Repeat("| ", indent-1);
|
|
|
|
if (r+1 == table->GetNumberRows())
|
|
|
|
indentation += "'-";
|
|
|
|
else
|
|
|
|
indentation += "|-";
|
|
|
|
|
2010-10-31 23:00:28 +01:00
|
|
|
for (size_t c = 0; c < columns.size(); ++c)
|
2006-04-24 01:14:18 +02:00
|
|
|
if (c == 0)
|
|
|
|
data.push_back(indentation + table->GetCellText(r, c));
|
|
|
|
else
|
|
|
|
data.push_back(table->GetCellText(r, c));
|
|
|
|
|
|
|
|
if (table->GetChild(r))
|
|
|
|
WriteRows(indent+1, table->GetChild(r), data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-12-31 15:27:02 +01:00
|
|
|
std::ofstream& m_OutputStream;
|
2006-04-24 01:14:18 +02:00
|
|
|
const WriteTable& operator=(const WriteTable&);
|
|
|
|
};
|
|
|
|
|
2011-02-19 04:14:37 +01:00
|
|
|
struct DumpTable
|
|
|
|
{
|
2017-08-24 02:32:42 +02:00
|
|
|
const ScriptInterface& m_ScriptInterface;
|
2015-01-24 15:46:52 +01:00
|
|
|
JS::PersistentRooted<JS::Value> m_Root;
|
2017-08-24 02:32:42 +02:00
|
|
|
DumpTable(const ScriptInterface& scriptInterface, JS::HandleValue root) :
|
2020-11-18 15:39:04 +01:00
|
|
|
m_ScriptInterface(scriptInterface), m_Root(scriptInterface.GetGeneralJSContext(), root)
|
2015-01-24 15:46:52 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:02:58 +01:00
|
|
|
// std::for_each requires a move constructor and the use of JS::PersistentRooted<T> apparently breaks a requirement for an
|
2015-01-24 15:46:52 +01:00
|
|
|
// automatic move constructor
|
|
|
|
DumpTable(DumpTable && original) :
|
|
|
|
m_ScriptInterface(original.m_ScriptInterface),
|
2020-11-18 15:39:04 +01:00
|
|
|
m_Root(original.m_ScriptInterface.GetGeneralJSContext(), original.m_Root.get())
|
2011-02-19 04:14:37 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator() (AbstractProfileTable* table)
|
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue t(rq.cx);
|
2019-09-13 02:56:51 +02:00
|
|
|
ScriptInterface::CreateObject(
|
2020-11-13 14:18:22 +01:00
|
|
|
rq,
|
2019-09-07 18:51:44 +02:00
|
|
|
&t,
|
|
|
|
"cols", DumpCols(table),
|
|
|
|
"data", DumpRows(table));
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2015-01-24 15:46:52 +01:00
|
|
|
m_ScriptInterface.SetProperty(m_Root, table->GetTitle().c_str(), t);
|
2011-03-03 02:09:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> DumpCols(AbstractProfileTable* table)
|
|
|
|
{
|
|
|
|
std::vector<std::string> titles;
|
|
|
|
|
|
|
|
const std::vector<ProfileColumn>& columns = table->GetColumns();
|
|
|
|
|
|
|
|
for (size_t c = 0; c < columns.size(); ++c)
|
|
|
|
titles.push_back(columns[c].title);
|
|
|
|
|
|
|
|
return titles;
|
2011-02-19 04:14:37 +01:00
|
|
|
}
|
|
|
|
|
2015-01-24 15:46:52 +01:00
|
|
|
JS::Value DumpRows(AbstractProfileTable* table)
|
2011-02-19 04:14:37 +01:00
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue data(rq.cx);
|
|
|
|
ScriptInterface::CreateObject(rq, &data);
|
2011-02-19 04:14:37 +01:00
|
|
|
|
|
|
|
const std::vector<ProfileColumn>& columns = table->GetColumns();
|
|
|
|
|
|
|
|
for (size_t r = 0; r < table->GetNumberRows(); ++r)
|
|
|
|
{
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue row(rq.cx);
|
|
|
|
ScriptInterface::CreateArray(rq, &row);
|
2019-07-22 21:35:14 +02:00
|
|
|
|
2015-01-24 15:46:52 +01:00
|
|
|
m_ScriptInterface.SetProperty(data, table->GetCellText(r, 0).c_str(), row);
|
2011-02-19 04:14:37 +01:00
|
|
|
|
|
|
|
if (table->GetChild(r))
|
2015-01-24 15:46:52 +01:00
|
|
|
{
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue childRows(rq.cx, DumpRows(table->GetChild(r)));
|
2015-01-24 15:46:52 +01:00
|
|
|
m_ScriptInterface.SetPropertyInt(row, 0, childRows);
|
|
|
|
}
|
2011-03-03 02:09:19 +01:00
|
|
|
|
|
|
|
for (size_t c = 1; c < columns.size(); ++c)
|
2015-01-24 15:46:52 +01:00
|
|
|
m_ScriptInterface.SetPropertyInt(row, c, table->GetCellText(r, c));
|
2011-02-19 04:14:37 +01:00
|
|
|
}
|
|
|
|
|
2015-01-24 15:46:52 +01:00
|
|
|
return data;
|
2011-02-19 04:14:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const DumpTable& operator=(const DumpTable&);
|
|
|
|
};
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
bool SortByName(AbstractProfileTable* a, AbstractProfileTable* b)
|
|
|
|
{
|
|
|
|
return (a->GetName() < b->GetName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CProfileViewer::SaveToFile()
|
|
|
|
{
|
|
|
|
// Open the file, if necessary. If this method is called several times,
|
|
|
|
// the profile results will be appended to the previous ones from the same
|
|
|
|
// run.
|
|
|
|
if (! m->outputStream.is_open())
|
|
|
|
{
|
|
|
|
// Open the file. (It will be closed when the CProfileViewer
|
|
|
|
// destructor is called.)
|
2011-03-23 14:36:20 +01:00
|
|
|
OsPath path = psLogDir()/"profile.txt";
|
|
|
|
m->outputStream.open(OsString(path).c_str(), std::ofstream::out | std::ofstream::trunc);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
if (m->outputStream.fail())
|
|
|
|
{
|
2015-01-22 21:31:30 +01:00
|
|
|
LOGERROR("Failed to open profile log file");
|
2006-04-24 01:14:18 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-06-18 01:40:13 +02:00
|
|
|
else
|
|
|
|
{
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGMESSAGERENDER("Profiler snapshot saved to '%s'", path.string8());
|
2011-06-18 01:40:13 +02:00
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
time_t t;
|
|
|
|
time(&t);
|
|
|
|
m->outputStream << "================================================================\n\n";
|
|
|
|
m->outputStream << "PS profiler snapshot - " << asctime(localtime(&t));
|
|
|
|
|
|
|
|
std::vector<AbstractProfileTable*> tables = m->rootTables;
|
2006-09-30 23:36:19 +02:00
|
|
|
sort(tables.begin(), tables.end(), SortByName);
|
|
|
|
for_each(tables.begin(), tables.end(), WriteTable(m->outputStream));
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
m->outputStream << "\n\n================================================================\n";
|
|
|
|
m->outputStream.flush();
|
|
|
|
}
|
2006-09-28 22:41:12 +02:00
|
|
|
|
|
|
|
void CProfileViewer::ShowTable(const CStr& table)
|
|
|
|
{
|
|
|
|
m->path.clear();
|
|
|
|
|
|
|
|
if (table.length() > 0)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m->rootTables.size(); ++i)
|
|
|
|
{
|
|
|
|
if (m->rootTables[i]->GetName() == table)
|
|
|
|
{
|
|
|
|
m->path.push_back(m->rootTables[i]);
|
|
|
|
m->profileVisible = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No matching table found, so don't display anything
|
|
|
|
m->profileVisible = false;
|
|
|
|
}
|