callback to entry::AppI (#1165)

This commit is contained in:
attilaz 2017-06-16 22:10:37 +02:00 committed by Branimir Karadžić
parent ab2d8a7bcc
commit 034a91162d

View File

@ -310,153 +310,184 @@ private:
uint32_t m_maxBlocks;
};
int _main_(int _argc, char** _argv)
class ExampleCallback : public entry::AppI
{
Args args(_argc, _argv);
BgfxCallback callback;
BgfxAllocator allocator;
uint32_t width = 1280;
uint32_t height = 720;
// Enumerate supported backend renderers.
bgfx::RendererType::Enum renderers[bgfx::RendererType::Count];
uint8_t numRenderers = bgfx::getSupportedRenderers(BX_COUNTOF(renderers), renderers);
bgfx::init(bgfx::RendererType::Count == args.m_type
? renderers[bx::getHPCounter() % numRenderers] /* randomize renderer */
: args.m_type
, args.m_pciId
, 0
, &callback // custom callback handler
, &allocator // custom allocator
);
bgfx::reset(width, height, BGFX_RESET_CAPTURE|BGFX_RESET_MSAA_X16);
// Enable debug text.
bgfx::setDebug(BGFX_DEBUG_TEXT);
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, 1280, 720);
// Set view 0 clear state.
bgfx::setViewClear(0
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
, 0x303030ff
, 1.0f
, 0
);
// Create vertex stream declaration.
PosColorVertex::init();
// Create static vertex buffer.
bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(
bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
, PosColorVertex::ms_decl
);
// Create static index buffer.
bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
// Create program from shaders.
bgfx::ProgramHandle program = loadProgram("vs_callback", "fs_callback");
float time = 0.0f;
const bgfx::RendererType::Enum rendererType = bgfx::getRendererType();
// 5 second 60Hz video
for (uint32_t frame = 0; frame < 300; ++frame)
void init(int _argc, char** _argv) BX_OVERRIDE
{
// This dummy draw call is here to make sure that view 0 is cleared
// if no other draw calls are submitted to view 0.
bgfx::touch(0);
int64_t now = bx::getHPCounter();
static int64_t last = now;
const int64_t frameTime = now - last;
last = now;
const double freq = double(bx::getHPFrequency() );
const double toMs = 1000.0/freq;
// Use debug font to print information about this example.
bgfx::dbgTextClear();
bgfx::dbgTextPrintf( 0, 1, 0x4f, "bgfx/examples/07-callback");
bgfx::dbgTextPrintf( 0, 2, 0x6f, "Description: Implementing application specific callbacks for taking screen shots,");
bgfx::dbgTextPrintf(13, 3, 0x6f, "caching OpenGL binary shaders, and video capture.");
bgfx::dbgTextPrintf( 0, 4, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
bgfx::dbgTextPrintf( 2, 6, 0x0e, "Supported renderers:");
for (uint8_t ii = 0; ii < numRenderers; ++ii)
{
bgfx::dbgTextPrintf( 2, 7+ii, 0x0c, "[%c] %s"
, renderers[ii] == rendererType ? '\xfe' : ' '
, bgfx::getRendererName(renderers[ii])
);
}
float at[3] = { 0.0f, 0.0f, 0.0f };
float eye[3] = { 0.0f, 0.0f, -35.0f };
float view[16];
float proj[16];
bx::mtxLookAt(view, eye, at);
bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
// Set view and projection matrix for view 0.
bgfx::setViewTransform(0, view, proj);
time += 1.0f/60.0f;
// Submit 11x11 cubes.
for (uint32_t yy = 0; yy < 11; ++yy)
{
for (uint32_t xx = 0; xx < 11-yy; ++xx)
{
float mtx[16];
bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
mtx[12] = -15.0f + float(xx)*3.0f;
mtx[13] = -15.0f + float(yy)*3.0f;
mtx[14] = 0.0f;
// Set model matrix for rendering.
bgfx::setTransform(mtx);
// Set vertex and index buffer.
bgfx::setVertexBuffer(0, vbh);
bgfx::setIndexBuffer(ibh);
// Set render states.
bgfx::setState(BGFX_STATE_DEFAULT);
// Submit primitive for rendering to view 0.
bgfx::submit(0, program);
}
}
// Take screen shot at frame 150.
if (150 == frame)
{
bgfx::FrameBufferHandle fbh = BGFX_INVALID_HANDLE;
bgfx::requestScreenShot(fbh, "temp/frame150");
}
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
Args args(_argc, _argv);
m_width = 1280;
m_height = 720;
// Enumerate supported backend renderers.
m_numRenderers = bgfx::getSupportedRenderers(BX_COUNTOF(m_renderers), m_renderers);
bgfx::init(bgfx::RendererType::Count == args.m_type
? m_renderers[bx::getHPCounter() % m_numRenderers] /* randomize renderer */
: args.m_type
, args.m_pciId
, 0
, &m_callback // custom callback handler
, &m_allocator // custom allocator
);
bgfx::reset(m_width, m_height, BGFX_RESET_CAPTURE|BGFX_RESET_MSAA_X16);
// Enable debug text.
bgfx::setDebug(BGFX_DEBUG_TEXT);
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, 1280, 720);
// Set view 0 clear state.
bgfx::setViewClear(0
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
, 0x303030ff
, 1.0f
, 0
);
// Create vertex stream declaration.
PosColorVertex::init();
// Create static vertex buffer.
m_vbh = bgfx::createVertexBuffer(
bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
, PosColorVertex::ms_decl
);
// Create static index buffer.
m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
// Create program from shaders.
m_program = loadProgram("vs_callback", "fs_callback");
m_time = 0.0f;
m_frame = 0;
}
// Cleanup.
bgfx::destroyIndexBuffer(ibh);
bgfx::destroyVertexBuffer(vbh);
bgfx::destroyProgram(program);
virtual int shutdown() BX_OVERRIDE
{
// Cleanup.
bgfx::destroyIndexBuffer(m_ibh);
bgfx::destroyVertexBuffer(m_vbh);
bgfx::destroyProgram(m_program);
// Shutdown bgfx.
bgfx::shutdown();
m_allocator.dumpStats();
return 0;
}
bool update() BX_OVERRIDE
{
// 5 second 60Hz video
if ( m_frame < 300)
{
++m_frame;
const bgfx::RendererType::Enum rendererType = bgfx::getRendererType();
// This dummy draw call is here to make sure that view 0 is cleared
// if no other draw calls are submitted to view 0.
bgfx::touch(0);
int64_t now = bx::getHPCounter();
static int64_t last = now;
const int64_t frameTime = now - last;
last = now;
const double freq = double(bx::getHPFrequency() );
const double toMs = 1000.0/freq;
// Use debug font to print information about this example.
bgfx::dbgTextClear();
bgfx::dbgTextPrintf( 0, 1, 0x4f, "bgfx/examples/07-callback");
bgfx::dbgTextPrintf( 0, 2, 0x6f, "Description: Implementing application specific callbacks for taking screen shots,");
bgfx::dbgTextPrintf(13, 3, 0x6f, "caching OpenGL binary shaders, and video capture.");
bgfx::dbgTextPrintf( 0, 4, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
bgfx::dbgTextPrintf( 2, 6, 0x0e, "Supported renderers:");
for (uint8_t ii = 0; ii < m_numRenderers; ++ii)
{
bgfx::dbgTextPrintf( 2, 7+ii, 0x0c, "[%c] %s"
, m_renderers[ii] == rendererType ? '\xfe' : ' '
, bgfx::getRendererName(m_renderers[ii])
);
}
float at[3] = { 0.0f, 0.0f, 0.0f };
float eye[3] = { 0.0f, 0.0f, -35.0f };
float view[16];
float proj[16];
bx::mtxLookAt(view, eye, at);
bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
// Set view and projection matrix for view 0.
bgfx::setViewTransform(0, view, proj);
m_time += 1.0f/60.0f;
// Submit 11x11 cubes.
for (uint32_t yy = 0; yy < 11; ++yy)
{
for (uint32_t xx = 0; xx < 11-yy; ++xx)
{
float mtx[16];
bx::mtxRotateXY(mtx, m_time + xx*0.21f, m_time + yy*0.37f);
mtx[12] = -15.0f + float(xx)*3.0f;
mtx[13] = -15.0f + float(yy)*3.0f;
mtx[14] = 0.0f;
// Set model matrix for rendering.
bgfx::setTransform(mtx);
// Set vertex and index buffer.
bgfx::setVertexBuffer(0, m_vbh);
bgfx::setIndexBuffer(m_ibh);
// Set render states.
bgfx::setState(BGFX_STATE_DEFAULT);
// Submit primitive for rendering to view 0.
bgfx::submit(0, m_program);
}
}
// Take screen shot at frame 150.
if (150 == m_frame)
{
bgfx::FrameBufferHandle fbh = BGFX_INVALID_HANDLE;
bgfx::requestScreenShot(fbh, "temp/frame150");
}
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
return true;
}
return false;
}
// Shutdown bgfx.
bgfx::shutdown();
BgfxCallback m_callback;
BgfxAllocator m_allocator;
uint32_t m_width;
uint32_t m_height;
bgfx::RendererType::Enum m_renderers[bgfx::RendererType::Count];
uint8_t m_numRenderers;
bgfx::VertexBufferHandle m_vbh;
bgfx::IndexBufferHandle m_ibh;
bgfx::ProgramHandle m_program;
float m_time;
uint32_t m_frame;
};
allocator.dumpStats();
ENTRY_IMPLEMENT_MAIN(ExampleCallback);
return 0;
}