metaforce/hecl/test/main.cpp

262 lines
8.3 KiB
C++
Raw Normal View History

2015-11-12 04:32:53 +00:00
#include <boo/boo.hpp>
#include <LogVisor/LogVisor.hpp>
2015-11-16 04:30:06 +00:00
#include <Athena/MemoryWriter.hpp>
2015-11-13 02:12:09 +00:00
#include "HECL/Runtime.hpp"
2015-11-16 04:30:06 +00:00
#include "HECL/HMDLMeta.hpp"
#include <math.h>
2015-11-17 06:42:42 +00:00
#include <thread>
#include <mutex>
#include <condition_variable>
2015-11-12 04:32:53 +00:00
struct HECLWindowCallback : boo::IWindowCallback
{
bool m_sizeDirty = false;
boo::SWindowRect m_latestSize;
void resized(const boo::SWindowRect& rect)
{
m_sizeDirty = true;
m_latestSize = rect;
}
2015-11-13 02:12:09 +00:00
bool m_destroyed = false;
void destroyed()
{
m_destroyed = true;
}
2015-11-12 04:32:53 +00:00
};
struct HECLApplicationCallback : boo::IApplicationCallback
{
HECLWindowCallback m_windowCb;
boo::IWindow* m_mainWindow = nullptr;
bool m_running = true;
2015-11-17 06:42:42 +00:00
2015-11-12 04:32:53 +00:00
int appMain(boo::IApplication* app)
{
2015-11-16 04:30:06 +00:00
/* Setup boo window */
2016-02-24 03:18:19 +00:00
m_mainWindow = app->newWindow(_S("HECL Test"), 1);
2015-11-12 04:32:53 +00:00
m_mainWindow->setCallback(&m_windowCb);
2015-11-16 04:30:06 +00:00
2015-11-17 06:42:42 +00:00
boo::ITextureR* renderTex = nullptr;
boo::IGraphicsBufferD* vubo = nullptr;
boo::IShaderDataBinding* binding = nullptr;
2015-11-16 04:30:06 +00:00
struct VertexUBO
{
float modelview[4][4] = {};
float modelviewInv[4][4] = {};
float projection[4][4] = {};
VertexUBO()
{
modelview[0][0] = 1.0;
modelview[1][1] = 1.0;
modelview[2][2] = 1.0;
modelview[3][3] = 1.0;
modelviewInv[0][0] = 1.0;
modelviewInv[1][1] = 1.0;
modelviewInv[2][2] = 1.0;
modelviewInv[3][3] = 1.0;
projection[0][0] = 1.0;
projection[1][1] = 1.0;
projection[2][2] = 1.0;
projection[3][3] = 1.0;
}
} vuboData;
2015-11-17 06:42:42 +00:00
std::mutex initmt;
std::condition_variable initcv;
std::mutex loadmt;
std::condition_variable loadcv;
std::unique_lock<std::mutex> outerLk(initmt);
std::thread loaderThr([&]()
{
std::unique_lock<std::mutex> innerLk(initmt);
boo::IGraphicsDataFactory* gfxF = m_mainWindow->getLoadContextDataFactory();
boo::SWindowRect mainWindowRect = m_mainWindow->getWindowFrame();
2016-02-25 02:54:33 +00:00
renderTex = gfxF->newRenderTexture(mainWindowRect.size[0], mainWindowRect.size[1], false, false);
2015-11-17 06:42:42 +00:00
/* HECL managers */
HECL::Runtime::FileStoreManager fileMgr(app->getUniqueName());
HECL::Runtime::ShaderCacheManager shaderMgr(fileMgr, gfxF);
/* Compile HECL shader */
static std::string testShader = "HECLOpaque(Texture(0, UV(0)))";
HECL::Runtime::ShaderTag testShaderTag(testShader, 0, 1, 0, 0, 0, false, false, false);
boo::IShaderPipeline* testShaderObj =
shaderMgr.buildShader(testShaderTag, testShader, "testShader");
/* Generate meta structure (usually statically serialized) */
HECL::HMDLMeta testMeta;
2015-11-21 01:13:06 +00:00
testMeta.topology = HECL::HMDLTopology::TriStrips;
2015-11-17 06:42:42 +00:00
testMeta.vertStride = 32;
testMeta.vertCount = 4;
testMeta.indexCount = 4;
testMeta.colorCount = 0;
testMeta.uvCount = 1;
testMeta.weightCount = 0;
/* Binary form of meta structure */
atUint8 testMetaBuf[HECL_HMDL_META_SZ];
Athena::io::MemoryWriter testMetaWriter(testMetaBuf, HECL_HMDL_META_SZ);
testMeta.write(testMetaWriter);
/* Make Tri-strip VBO */
struct Vert
{
float pos[3];
float norm[3];
float uv[2];
};
static const Vert quad[4] =
{
{{0.5,0.5},{},{1.0,1.0}},
{{-0.5,0.5},{},{0.0,1.0}},
{{0.5,-0.5},{},{1.0,0.0}},
{{-0.5,-0.5},{},{0.0,0.0}}
};
/* Now simple IBO */
static const uint32_t ibo[4] = {0,1,2,3};
/* Construct quad mesh against boo factory */
HECL::Runtime::HMDLData testData(gfxF, testMetaBuf, quad, ibo);
/* Make ramp texture */
using Pixel = uint8_t[4];
static Pixel tex[256][256];
for (int i=0 ; i<256 ; ++i)
for (int j=0 ; j<256 ; ++j)
{
tex[i][j][0] = i;
tex[i][j][1] = j;
tex[i][j][2] = 0;
tex[i][j][3] = 0xff;
}
boo::ITexture* texture =
2015-11-21 01:13:06 +00:00
gfxF->newStaticTexture(256, 256, 1, boo::TextureFormat::RGBA8, tex, 256*256*4);
2015-11-17 06:42:42 +00:00
/* Make vertex uniform buffer */
2015-11-21 01:13:06 +00:00
vubo = gfxF->newDynamicBuffer(boo::BufferUse::Uniform, sizeof(VertexUBO), 1);
2015-11-17 06:42:42 +00:00
/* Assemble data binding */
binding = testData.newShaderDataBindng(gfxF, testShaderObj, 1, (boo::IGraphicsBuffer**)&vubo, 1, &texture);
2015-12-18 03:25:53 +00:00
boo::GraphicsDataToken data = gfxF->commit();
2015-11-17 06:42:42 +00:00
/* Return control to main thread */
innerLk.unlock();
initcv.notify_one();
/* Wait for exit */
std::unique_lock<std::mutex> lk(loadmt);
while (m_running)
{
loadcv.wait(lk);
}
});
initcv.wait(outerLk);
2015-11-16 04:30:06 +00:00
2015-11-17 06:42:42 +00:00
m_mainWindow->showWindow();
m_windowCb.m_latestSize = m_mainWindow->getWindowFrame();
boo::IGraphicsCommandQueue* gfxQ = m_mainWindow->getCommandQueue();
m_mainWindow->getMainContextDataFactory();
2015-11-16 04:30:06 +00:00
size_t frameIdx = 0;
2015-11-12 04:32:53 +00:00
while (m_running)
{
m_mainWindow->waitForRetrace();
2015-11-13 02:12:09 +00:00
if (m_windowCb.m_destroyed)
{
m_running = false;
break;
}
2015-11-12 04:32:53 +00:00
if (m_windowCb.m_sizeDirty)
{
gfxQ->resizeRenderTexture(renderTex,
m_windowCb.m_latestSize.size[0],
m_windowCb.m_latestSize.size[1]);
m_windowCb.m_sizeDirty = false;
}
gfxQ->setRenderTarget(renderTex);
2015-11-16 04:30:06 +00:00
boo::SWindowRect r = m_windowCb.m_latestSize;
r.location[0] = 0;
r.location[1] = 0;
gfxQ->setViewport(r);
2015-11-27 22:17:09 +00:00
gfxQ->setScissor(r);
2015-11-16 04:30:06 +00:00
float rgba[] = {sinf(frameIdx / 60.0), cosf(frameIdx / 60.0), 0.0, 1.0};
gfxQ->setClearColor(rgba);
2015-11-12 04:32:53 +00:00
gfxQ->clearTarget();
2015-11-16 04:30:06 +00:00
2015-11-16 20:43:53 +00:00
vuboData.modelview[3][0] = sinf(frameIdx / 60.0) * 0.5;
vuboData.modelview[3][1] = cosf(frameIdx / 60.0) * 0.5;
2015-11-16 04:30:06 +00:00
vubo->load(&vuboData, sizeof(vuboData));
gfxQ->setShaderDataBinding(binding);
gfxQ->draw(0, 4);
2015-11-12 04:32:53 +00:00
gfxQ->resolveDisplay(renderTex);
gfxQ->execute();
2015-11-16 04:30:06 +00:00
++frameIdx;
2015-11-12 04:32:53 +00:00
}
2015-11-17 06:42:42 +00:00
std::unique_lock<std::mutex> finallk(loadmt);
finallk.unlock();
2015-12-05 00:42:21 +00:00
gfxQ->stopRenderer();
2015-11-17 06:42:42 +00:00
loadcv.notify_one();
loaderThr.join();
2015-11-12 04:32:53 +00:00
return 0;
}
void appQuitting(boo::IApplication* app)
{
m_running = false;
}
};
2015-11-23 03:09:46 +00:00
void AthenaExcHandler(Athena::error::Level level,
2015-11-16 04:30:06 +00:00
const char* file, const char* function,
int line, const char* fmt, ...)
2015-11-23 03:09:46 +00:00
{
static LogVisor::LogModule Log("heclTest::AthenaExcHandler");
va_list ap;
va_start(ap, fmt);
Log.reportSource(LogVisor::Level(level), file, line, fmt, ap);
va_end(ap);
}
2015-11-16 04:30:06 +00:00
2015-11-12 04:32:53 +00:00
#if _WIN32
int wmain(int argc, const boo::SystemChar** argv)
#else
int main(int argc, const boo::SystemChar** argv)
#endif
{
2015-11-16 04:30:06 +00:00
atSetExceptionHandler(AthenaExcHandler);
2015-11-12 04:32:53 +00:00
LogVisor::RegisterConsoleLogger();
HECLApplicationCallback appCb;
2015-11-21 01:13:06 +00:00
int ret = boo::ApplicationRun(boo::IApplication::EPlatformType::Auto,
2015-11-16 04:30:06 +00:00
appCb, _S("heclTest"), _S("HECL Test"), argc, argv);
2015-11-12 04:32:53 +00:00
printf("IM DYING!!\n");
return ret;
}
#if _WIN32
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpCmdLine, int)
{
int argc = 0;
const boo::SystemChar** argv = (const wchar_t**)(CommandLineToArgvW(lpCmdLine, &argc));
static boo::SystemChar selfPath[1024];
GetModuleFileNameW(nullptr, selfPath, 1024);
static const boo::SystemChar* booArgv[32] = {};
booArgv[0] = selfPath;
for (int i=0 ; i<argc ; ++i)
booArgv[i+1] = argv[i];
LogVisor::CreateWin32Console();
return wmain(argc+1, booArgv);
}
#endif