bgfx/examples/17-drawstress/drawstress.cpp

351 lines
8.3 KiB
C++
Raw Normal View History

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>
// embedded shaders
#include "vs_drawstress.bin.h"
2015-01-23 08:01:09 +03:00
#include "fs_drawstress.bin.h"
2017-06-26 07:44:04 +03:00
namespace
{
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()
{
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,
};
#if BX_PLATFORM_EMSCRIPTEN
2014-03-18 07:16:35 +04:00
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
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
{
2017-06-26 07:44:04 +03:00
public:
ExampleDrawStress(const char* _name, const char* _description)
: entry::AppI(_name, _description)
{
}
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
m_width = 1280;
m_height = 720;
2017-06-26 07:44:04 +03:00
m_debug = BGFX_DEBUG_NONE;
m_reset = BGFX_RESET_NONE;
2014-01-19 11:43:16 +04:00
m_autoAdjust = true;
m_scrollArea = 0;
m_dim = 16;
m_maxDim = 40;
m_transform = 0;
2014-01-19 11:43:16 +04:00
m_timeOffset = bx::getHPCounter();
2014-01-19 11:43:16 +04: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);
bgfx::reset(m_width, m_height, m_reset);
2014-01-19 11:43:16 +04: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
// Enable debug text.
bgfx::setDebug(m_debug);
2014-01-19 11:43:16 +04:00
// Set view 0 clear state.
bgfx::setViewClear(0
2017-06-21 19:24:14 +03:00
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
, 0x303030ff
, 1.0f
, 0
);
2014-01-19 11:43:16 +04: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
// Create program from shaders.
m_program = bgfx::createProgram(
2017-06-21 19:24:14 +03:00
bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_drawstress")
, bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_drawstress")
, true /* destroy shaders when program is destroyed */
);
2014-01-19 11:43:16 +04:00
// Create static vertex buffer.
m_vbh = bgfx::createVertexBuffer(
2017-06-21 19:24:14 +03:00
bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
, PosColorVertex::ms_decl
);
2014-01-19 11:43:16 +04:00
// Create static index buffer.
m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
2014-01-19 11:43:16 +04:00
// Imgui.
imguiCreate();
}
2014-01-19 11:43:16 +04: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
// Shutdown bgfx.
bgfx::shutdown();
2014-01-19 11:43:16 +04:00
return 0;
}
2014-01-19 11:43:16 +04: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) )
{
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
m_deltaTimeNs += frameTime*1000000/hpFreq;
2014-01-19 11:43:16 +04:00
if (m_deltaTimeNs > 1000000)
2014-01-19 11:43:16 +04:00
{
m_deltaTimeAvgNs = m_deltaTimeNs / bx::int64_max(1, m_numFrames);
if (m_autoAdjust)
2014-01-19 11:43:16 +04: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
m_deltaTimeNs = 0;
m_numFrames = 0;
}
else
{
++m_numFrames;
}
2014-01-19 11:43:16 +04: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
2017-06-21 19:24:14 +03:00
, m_mouseState.m_my
, (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)
, m_mouseState.m_mz
, uint16_t(m_width)
, uint16_t(m_height)
);
2014-01-19 11:43:16 +04:00
2017-06-26 07:44:04 +03:00
bool restart = showExampleDialog(this);
ImGui::SetNextWindowPos(ImVec2((float)m_width - (float)m_width / 4.0f - 10.0f, 10.0f) );
ImGui::SetNextWindowSize(ImVec2((float)m_width / 4.0f, (float)m_height / 2.0f) );
ImGui::Begin("Settings"
2017-06-21 19:24:14 +03:00
, NULL
, ImVec2((float)m_width / 4.0f, (float)m_height / 2.0f)
, ImGuiWindowFlags_AlwaysAutoResize
);
ImGui::RadioButton("Rotate",&m_transform,0);
ImGui::RadioButton("No fragments",&m_transform,1);
ImGui::Separator();
ImGui::Checkbox("Auto adjust", &m_autoAdjust);
ImGui::SliderInt("Dim", &m_dim, 5, m_maxDim);
ImGui::Text("Draw calls: %d", m_dim*m_dim*m_dim);
ImGui::Text("Avg Delta Time (1 second) [ms]: %0.4f", m_deltaTimeAvgNs/1000.0f);
ImGui::Separator();
const bgfx::Stats* stats = bgfx::getStats();
ImGui::Text("GPU %0.6f [ms]", double(stats->gpuTimeEnd - stats->gpuTimeBegin)*1000.0/stats->gpuTimerFreq);
ImGui::Text("CPU %0.6f [ms]", double(stats->cpuTimeEnd - stats->cpuTimeBegin)*1000.0/stats->cpuTimerFreq);
ImGui::Text("Waiting for render thread %0.6f [ms]", double(stats->waitRender) * toMs);
ImGui::Text("Waiting for submit thread %0.6f [ms]", double(stats->waitSubmit) * toMs);
2016-05-15 19:02:25 +03:00
ImGui::End();
2017-06-21 19:24:14 +03:00
imguiEndFrame();
2014-03-18 07:16:35 +04: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
float view[16];
bx::mtxLookAt(view, eye, at);
2017-06-13 08:43:07 +03:00
const bgfx::Caps* caps = bgfx::getCaps();
float proj[16];
bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, caps->homogeneousDepth);
2014-03-18 07:16:35 +04:00
// Set view and projection matrix for view 0.
bgfx::setViewTransform(0, view, proj);
2014-03-18 07:16:35 +04:00
// Set view 0 default viewport.
2017-03-13 04:25:23 +03:00
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
2015-04-10 02:58:44 +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
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
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
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
float mtx[16];
bx::mtxMul(mtx, mtxS, mtxR);
2014-03-18 07:16:35 +04: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
// Set model matrix for rendering.
bgfx::setTransform(mtx);
2014-03-18 07:16:35 +04:00
// Set vertex and index buffer.
2017-05-14 21:48:59 +03:00
bgfx::setVertexBuffer(0, m_vbh);
bgfx::setIndexBuffer(m_ibh);
2014-03-18 07:16:35 +04:00
// Set render states.
bgfx::setState(BGFX_STATE_DEFAULT);
2014-03-18 07:16:35 +04: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();
2017-06-26 07:44:04 +03:00
return !restart;
}
2014-03-18 07:16:35 +04:00
return false;
}
2015-08-22 07:06:46 +03:00
entry::MouseState m_mouseState;
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;
int32_t m_transform;
2014-01-19 11:43:16 +04: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
2017-06-26 07:44:04 +03:00
} // namespace
ENTRY_IMPLEMENT_MAIN(ExampleDrawStress, "17-drawstress", "Draw stress, maximizing number of draw calls.");