2014-01-19 11:43:16 +04:00
|
|
|
/*
|
2017-01-01 11:18:41 +03:00
|
|
|
* Copyright 2011-2017 Branimir Karadzic. All rights reserved.
|
2016-01-01 11:11:04 +03:00
|
|
|
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
|
2014-01-19 11:43:16 +04:00
|
|
|
*/
|
|
|
|
|
2014-05-04 02:18:28 +04:00
|
|
|
#include "common.h"
|
2015-10-24 06:52:22 +03:00
|
|
|
#include "bgfx_utils.h"
|
|
|
|
|
|
|
|
#include <bx/uint32_t.h>
|
2014-05-04 02:18:28 +04:00
|
|
|
#include "imgui/imgui.h"
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2016-12-06 07:12:31 +03:00
|
|
|
#include <bgfx/embedded_shader.h>
|
|
|
|
|
2014-01-20 10:34:58 +04:00
|
|
|
// embedded shaders
|
|
|
|
#include "vs_drawstress.bin.h"
|
2015-01-23 08:01:09 +03:00
|
|
|
#include "fs_drawstress.bin.h"
|
2014-01-20 10:34:58 +04:00
|
|
|
|
2016-12-06 07:12:31 +03:00
|
|
|
static const bgfx::EmbeddedShader s_embeddedShaders[] =
|
|
|
|
{
|
|
|
|
BGFX_EMBEDDED_SHADER(vs_drawstress),
|
|
|
|
BGFX_EMBEDDED_SHADER(fs_drawstress),
|
|
|
|
|
|
|
|
BGFX_EMBEDDED_SHADER_END()
|
|
|
|
};
|
|
|
|
|
2014-01-19 11:43:16 +04:00
|
|
|
struct PosColorVertex
|
|
|
|
{
|
|
|
|
float m_x;
|
|
|
|
float m_y;
|
|
|
|
float m_z;
|
|
|
|
uint32_t m_abgr;
|
2014-05-04 02:18:28 +04:00
|
|
|
|
|
|
|
static void init()
|
|
|
|
{
|
2014-05-11 07:51:44 +04:00
|
|
|
ms_decl
|
|
|
|
.begin()
|
|
|
|
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
|
|
|
|
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
|
|
|
|
.end();
|
2014-05-04 02:18:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static bgfx::VertexDecl ms_decl;
|
2014-01-19 11:43:16 +04:00
|
|
|
};
|
|
|
|
|
2014-05-04 02:18:28 +04:00
|
|
|
bgfx::VertexDecl PosColorVertex::ms_decl;
|
2014-01-19 11:43:16 +04:00
|
|
|
|
|
|
|
static PosColorVertex s_cubeVertices[8] =
|
|
|
|
{
|
|
|
|
{-1.0f, 1.0f, 1.0f, 0xff000000 },
|
|
|
|
{ 1.0f, 1.0f, 1.0f, 0xff0000ff },
|
|
|
|
{-1.0f, -1.0f, 1.0f, 0xff00ff00 },
|
|
|
|
{ 1.0f, -1.0f, 1.0f, 0xff00ffff },
|
|
|
|
{-1.0f, 1.0f, -1.0f, 0xffff0000 },
|
|
|
|
{ 1.0f, 1.0f, -1.0f, 0xffff00ff },
|
|
|
|
{-1.0f, -1.0f, -1.0f, 0xffffff00 },
|
|
|
|
{ 1.0f, -1.0f, -1.0f, 0xffffffff },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint16_t s_cubeIndices[36] =
|
|
|
|
{
|
|
|
|
0, 1, 2, // 0
|
|
|
|
1, 3, 2,
|
|
|
|
4, 6, 5, // 2
|
|
|
|
5, 6, 7,
|
|
|
|
0, 2, 4, // 4
|
|
|
|
4, 2, 6,
|
|
|
|
1, 5, 3, // 6
|
|
|
|
5, 7, 3,
|
|
|
|
0, 4, 1, // 8
|
|
|
|
4, 5, 1,
|
|
|
|
2, 3, 6, // 10
|
|
|
|
6, 3, 7,
|
|
|
|
};
|
|
|
|
|
2014-03-18 07:16:35 +04:00
|
|
|
#if BX_PLATFORM_EMSCRIPTEN || BX_PLATFORM_NACL
|
|
|
|
static const int64_t highwm = 1000000/35;
|
|
|
|
static const int64_t lowwm = 1000000/27;
|
|
|
|
#else
|
|
|
|
static const int64_t highwm = 1000000/65;
|
|
|
|
static const int64_t lowwm = 1000000/57;
|
|
|
|
#endif // BX_PLATFORM_EMSCRIPTEN || BX_PLATFORM_NACL
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2016-03-07 02:29:22 +03:00
|
|
|
class ExampleDrawStress : public entry::AppI
|
2014-03-18 07:16:35 +04:00
|
|
|
{
|
2015-10-24 06:52:22 +03:00
|
|
|
void init(int _argc, char** _argv) BX_OVERRIDE
|
2014-01-19 11:43:16 +04:00
|
|
|
{
|
2015-10-24 06:52:22 +03:00
|
|
|
Args args(_argc, _argv);
|
2016-05-15 19:02:25 +03:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
m_width = 1280;
|
|
|
|
m_height = 720;
|
|
|
|
m_debug = BGFX_DEBUG_TEXT;
|
|
|
|
m_reset = BGFX_RESET_NONE;
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
m_autoAdjust = true;
|
|
|
|
m_scrollArea = 0;
|
|
|
|
m_dim = 16;
|
|
|
|
m_maxDim = 40;
|
|
|
|
m_transform = 0;
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
m_timeOffset = bx::getHPCounter();
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
m_deltaTimeNs = 0;
|
|
|
|
m_deltaTimeAvgNs = 0;
|
|
|
|
m_numFrames = 0;
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-10-24 06:52:22 +03:00
|
|
|
bgfx::init(args.m_type, args.m_pciId);
|
2015-08-22 06:08:03 +03:00
|
|
|
bgfx::reset(m_width, m_height, m_reset);
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
const bgfx::Caps* caps = bgfx::getCaps();
|
2016-09-24 10:29:59 +03:00
|
|
|
m_maxDim = (int32_t)bx::fpow(float(caps->limits.maxDrawCalls), 1.0f/3.0f);
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
// Enable debug text.
|
|
|
|
bgfx::setDebug(m_debug);
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
// Set view 0 clear state.
|
|
|
|
bgfx::setViewClear(0
|
|
|
|
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
|
|
|
|
, 0x303030ff
|
|
|
|
, 1.0f
|
|
|
|
, 0
|
2014-03-18 07:16:35 +04:00
|
|
|
);
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
// Create vertex stream declaration.
|
|
|
|
PosColorVertex::init();
|
|
|
|
|
2016-12-06 07:12:31 +03:00
|
|
|
bgfx::RendererType::Enum type = bgfx::getRendererType();
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
// Create program from shaders.
|
|
|
|
m_program = bgfx::createProgram(
|
2016-12-06 07:12:31 +03:00
|
|
|
bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_drawstress")
|
|
|
|
, bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_drawstress")
|
2015-08-22 06:08:03 +03:00
|
|
|
, true /* destroy shaders when program is destroyed */
|
|
|
|
);
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
// Create static vertex buffer.
|
|
|
|
m_vbh = bgfx::createVertexBuffer(
|
|
|
|
bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
|
|
|
|
, PosColorVertex::ms_decl
|
|
|
|
);
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
// Create static index buffer.
|
|
|
|
m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
// Imgui.
|
|
|
|
imguiCreate();
|
|
|
|
}
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
int shutdown() BX_OVERRIDE
|
|
|
|
{
|
|
|
|
// Cleanup.
|
|
|
|
imguiDestroy();
|
|
|
|
bgfx::destroyIndexBuffer(m_ibh);
|
|
|
|
bgfx::destroyVertexBuffer(m_vbh);
|
|
|
|
bgfx::destroyProgram(m_program);
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
// Shutdown bgfx.
|
|
|
|
bgfx::shutdown();
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
bool update() BX_OVERRIDE
|
|
|
|
{
|
2015-08-22 07:06:46 +03:00
|
|
|
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
|
2015-08-22 06:08:03 +03:00
|
|
|
{
|
|
|
|
int64_t now = bx::getHPCounter();
|
|
|
|
static int64_t last = now;
|
|
|
|
const int64_t hpFreq = bx::getHPFrequency();
|
|
|
|
const int64_t frameTime = now - last;
|
|
|
|
last = now;
|
|
|
|
const double freq = double(hpFreq);
|
|
|
|
const double toMs = 1000.0/freq;
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
m_deltaTimeNs += frameTime*1000000/hpFreq;
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
if (m_deltaTimeNs > 1000000)
|
2014-01-19 11:43:16 +04:00
|
|
|
{
|
2015-08-22 06:08:03 +03:00
|
|
|
m_deltaTimeAvgNs = m_deltaTimeNs / bx::int64_max(1, m_numFrames);
|
|
|
|
|
|
|
|
if (m_autoAdjust)
|
2014-01-19 11:43:16 +04:00
|
|
|
{
|
2015-08-22 06:08:03 +03:00
|
|
|
if (m_deltaTimeAvgNs < highwm)
|
|
|
|
{
|
|
|
|
m_dim = bx::uint32_min(m_dim + 2, m_maxDim);
|
|
|
|
}
|
|
|
|
else if (m_deltaTimeAvgNs > lowwm)
|
|
|
|
{
|
|
|
|
m_dim = bx::uint32_max(m_dim - 1, 2);
|
|
|
|
}
|
|
|
|
}
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
m_deltaTimeNs = 0;
|
|
|
|
m_numFrames = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++m_numFrames;
|
|
|
|
}
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
float time = (float)( (now-m_timeOffset)/freq);
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 07:06:46 +03:00
|
|
|
imguiBeginFrame(m_mouseState.m_mx
|
|
|
|
, m_mouseState.m_my
|
2015-08-26 08:59:20 +03:00
|
|
|
, (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
|
|
|
|
| (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
|
|
|
|
| (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
|
2015-08-22 07:06:46 +03:00
|
|
|
, m_mouseState.m_mz
|
2015-08-22 06:08:03 +03:00
|
|
|
, m_width
|
|
|
|
, m_height
|
|
|
|
);
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2016-05-15 19:02:25 +03:00
|
|
|
imguiBeginScrollArea("Settings", m_width - m_width / 4 - 10, 10, m_width / 4, m_height / 2, &m_scrollArea);
|
2015-08-22 06:08:03 +03:00
|
|
|
imguiSeparatorLine();
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
m_transform = imguiChoose(m_transform
|
|
|
|
, "Rotate"
|
|
|
|
, "No fragments"
|
|
|
|
);
|
|
|
|
imguiSeparatorLine();
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
if (imguiCheck("Auto adjust", m_autoAdjust) )
|
|
|
|
{
|
|
|
|
m_autoAdjust ^= true;
|
2014-01-19 11:43:16 +04:00
|
|
|
}
|
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
imguiSlider("Dim", m_dim, 5, m_maxDim);
|
|
|
|
imguiLabel("Draw calls: %d", m_dim*m_dim*m_dim);
|
|
|
|
imguiLabel("Avg Delta Time (1 second) [ms]: %0.4f", m_deltaTimeAvgNs/1000.0f);
|
2014-03-18 07:16:35 +04:00
|
|
|
|
2015-11-10 04:56:12 +03:00
|
|
|
imguiSeparatorLine();
|
|
|
|
const bgfx::Stats* stats = bgfx::getStats();
|
|
|
|
imguiLabel("GPU %0.6f [ms]", double(stats->gpuTimeEnd - stats->gpuTimeBegin)*1000.0/stats->gpuTimerFreq);
|
|
|
|
imguiLabel("CPU %0.6f [ms]", double(stats->cpuTimeEnd - stats->cpuTimeBegin)*1000.0/stats->cpuTimerFreq);
|
2016-05-15 18:27:13 +03:00
|
|
|
imguiLabel("Waiting for render thread %0.6f [ms]", double(stats->waitRender) * toMs);
|
|
|
|
imguiLabel("Waiting for submit thread %0.6f [ms]", double(stats->waitSubmit) * toMs);
|
2016-05-15 19:02:25 +03:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
imguiEndScrollArea();
|
|
|
|
imguiEndFrame();
|
2014-03-18 07:16:35 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
float at[3] = { 0.0f, 0.0f, 0.0f };
|
|
|
|
float eye[3] = { 0.0f, 0.0f, -35.0f };
|
2014-03-18 07:16:35 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
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);
|
2014-03-18 07:16:35 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
// Set view and projection matrix for view 0.
|
|
|
|
bgfx::setViewTransform(0, view, proj);
|
2014-03-18 07:16:35 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
// Set view 0 default viewport.
|
|
|
|
bgfx::setViewRect(0, 0, 0, m_width, m_height);
|
2015-04-10 02:58:44 +03:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
// 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);
|
2014-03-18 07:16:35 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
// Use debug font to print information about this example.
|
|
|
|
bgfx::dbgTextClear();
|
|
|
|
bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/17-drawstress");
|
|
|
|
bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Draw stress, maximizing number of draw calls.");
|
|
|
|
bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: %7.3f[ms]", double(frameTime)*toMs);
|
2014-03-18 07:16:35 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
float mtxS[16];
|
|
|
|
const float scale = 0 == m_transform ? 0.25f : 0.0f;
|
|
|
|
bx::mtxScale(mtxS, scale, scale, scale);
|
2014-03-18 07:16:35 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
const float step = 0.6f;
|
|
|
|
float pos[3];
|
|
|
|
pos[0] = -step*m_dim / 2.0f;
|
|
|
|
pos[1] = -step*m_dim / 2.0f;
|
|
|
|
pos[2] = -15.0;
|
2014-03-18 07:16:35 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
for (uint32_t zz = 0; zz < uint32_t(m_dim); ++zz)
|
|
|
|
{
|
|
|
|
for (uint32_t yy = 0; yy < uint32_t(m_dim); ++yy)
|
|
|
|
{
|
|
|
|
for (uint32_t xx = 0; xx < uint32_t(m_dim); ++xx)
|
|
|
|
{
|
|
|
|
float mtxR[16];
|
|
|
|
bx::mtxRotateXYZ(mtxR, time + xx*0.21f, time + yy*0.37f, time + yy*0.13f);
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
float mtx[16];
|
|
|
|
bx::mtxMul(mtx, mtxS, mtxR);
|
2014-03-18 07:16:35 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
mtx[12] = pos[0] + float(xx)*step;
|
|
|
|
mtx[13] = pos[1] + float(yy)*step;
|
|
|
|
mtx[14] = pos[2] + float(zz)*step;
|
2014-03-18 07:16:35 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
// Set model matrix for rendering.
|
|
|
|
bgfx::setTransform(mtx);
|
2014-03-18 07:16:35 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
// Set vertex and index buffer.
|
|
|
|
bgfx::setVertexBuffer(m_vbh);
|
|
|
|
bgfx::setIndexBuffer(m_ibh);
|
2014-03-18 07:16:35 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
// Set render states.
|
|
|
|
bgfx::setState(BGFX_STATE_DEFAULT);
|
2014-03-18 07:16:35 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
// Submit primitive for rendering to view 0.
|
|
|
|
bgfx::submit(0, m_program);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance to next frame. Rendering thread will be kicked to
|
|
|
|
// process submitted rendering primitives.
|
|
|
|
bgfx::frame();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-03-18 07:16:35 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-22 07:06:46 +03:00
|
|
|
entry::MouseState m_mouseState;
|
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
uint32_t m_width;
|
|
|
|
uint32_t m_height;
|
|
|
|
uint32_t m_debug;
|
|
|
|
uint32_t m_reset;
|
|
|
|
|
|
|
|
bool m_autoAdjust;
|
|
|
|
int32_t m_scrollArea;
|
|
|
|
int32_t m_dim;
|
|
|
|
int32_t m_maxDim;
|
|
|
|
uint32_t m_transform;
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2015-08-22 06:08:03 +03:00
|
|
|
int64_t m_timeOffset;
|
|
|
|
|
|
|
|
int64_t m_deltaTimeNs;
|
|
|
|
int64_t m_deltaTimeAvgNs;
|
|
|
|
int64_t m_numFrames;
|
|
|
|
|
|
|
|
bgfx::ProgramHandle m_program;
|
|
|
|
bgfx::VertexBufferHandle m_vbh;
|
|
|
|
bgfx::IndexBufferHandle m_ibh;
|
|
|
|
};
|
2014-01-19 11:43:16 +04:00
|
|
|
|
2016-03-07 02:29:22 +03:00
|
|
|
ENTRY_IMPLEMENT_MAIN(ExampleDrawStress);
|