summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/examples/05-instancing/instancing.cpp
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/bgfx/examples/05-instancing/instancing.cpp')
-rw-r--r--3rdparty/bgfx/examples/05-instancing/instancing.cpp91
1 files changed, 59 insertions, 32 deletions
diff --git a/3rdparty/bgfx/examples/05-instancing/instancing.cpp b/3rdparty/bgfx/examples/05-instancing/instancing.cpp
index 834969ea02e..c295a9813c5 100644
--- a/3rdparty/bgfx/examples/05-instancing/instancing.cpp
+++ b/3rdparty/bgfx/examples/05-instancing/instancing.cpp
@@ -5,6 +5,10 @@
#include "common.h"
#include "bgfx_utils.h"
+#include "imgui/imgui.h"
+
+namespace
+{
struct PosColorVertex
{
@@ -57,13 +61,19 @@ static const uint16_t s_cubeIndices[36] =
class ExampleInstancing : public entry::AppI
{
- void init(int _argc, char** _argv) BX_OVERRIDE
+public:
+ ExampleInstancing(const char* _name, const char* _description)
+ : entry::AppI(_name, _description)
+ {
+ }
+
+ void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
{
Args args(_argc, _argv);
- m_width = 1280;
- m_height = 720;
- m_debug = BGFX_DEBUG_TEXT;
+ m_width = _width;
+ m_height = _height;
+ m_debug = BGFX_DEBUG_NONE;
m_reset = BGFX_RESET_VSYNC;
bgfx::init(args.m_type, args.m_pciId);
@@ -98,14 +108,18 @@ class ExampleInstancing : public entry::AppI
m_program = loadProgram("vs_instancing", "fs_instancing");
m_timeOffset = bx::getHPCounter();
+
+ imguiCreate();
}
- int shutdown() BX_OVERRIDE
+ int shutdown() override
{
+ imguiDestroy();
+
// Cleanup.
- bgfx::destroyIndexBuffer(m_ibh);
- bgfx::destroyVertexBuffer(m_vbh);
- bgfx::destroyProgram(m_program);
+ bgfx::destroy(m_ibh);
+ bgfx::destroy(m_vbh);
+ bgfx::destroy(m_program);
// Shutdown bgfx.
bgfx::shutdown();
@@ -113,10 +127,24 @@ class ExampleInstancing : public entry::AppI
return 0;
}
- bool update() BX_OVERRIDE
+ bool update() override
{
- if (!entry::processEvents(m_width, m_height, m_debug, m_reset) )
+ if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
{
+ imguiBeginFrame(m_mouseState.m_mx
+ , 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)
+ );
+
+ showExampleDialog(this);
+
+ imguiEndFrame();
+
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
@@ -124,19 +152,7 @@ class ExampleInstancing : public entry::AppI
// 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;
- float time = (float)( (now - m_timeOffset)/double(bx::getHPFrequency() ) );
-
- // Use debug font to print information about this example.
- bgfx::dbgTextClear();
- bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/05-instancing");
- bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Geometry instancing.");
- bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
+ float time = (float)( (bx::getHPCounter() - m_timeOffset)/double(bx::getHPFrequency() ) );
// Get renderer capabilities info.
const bgfx::Caps* caps = bgfx::getCaps();
@@ -147,7 +163,7 @@ class ExampleInstancing : public entry::AppI
// When instancing is not supported by GPU, implement alternative
// code path that doesn't use instancing.
bool blink = uint32_t(time*3.0f)&1;
- bgfx::dbgTextPrintf(0, 5, blink ? 0x1f : 0x01, " Instancing is not supported by GPU. ");
+ bgfx::dbgTextPrintf(0, 0, blink ? 0x1f : 0x01, " Instancing is not supported by GPU. ");
}
else
{
@@ -181,16 +197,22 @@ class ExampleInstancing : public entry::AppI
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
}
+ // 80 bytes stride = 64 bytes for 4x4 matrix + 16 bytes for RGBA color.
const uint16_t instanceStride = 80;
- const bgfx::InstanceDataBuffer* idb = bgfx::allocInstanceDataBuffer(121, instanceStride);
- if (NULL != idb)
+ // 11x11 cubes
+ const uint32_t numInstances = 121;
+
+ if (numInstances == bgfx::getAvailInstanceDataBuffer(numInstances, instanceStride) )
{
- uint8_t* data = idb->data;
+ bgfx::InstanceDataBuffer idb;
+ bgfx::allocInstanceDataBuffer(&idb, numInstances, instanceStride);
+
+ uint8_t* data = idb.data;
// Write instance data for 11x11 cubes.
- for (uint32_t yy = 0, numInstances = 0; yy < 11 && numInstances < idb->num; ++yy)
+ for (uint32_t yy = 0; yy < 11; ++yy)
{
- for (uint32_t xx = 0; xx < 11 && numInstances < idb->num; ++xx, ++numInstances)
+ for (uint32_t xx = 0; xx < 11; ++xx)
{
float* mtx = (float*)data;
bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
@@ -209,11 +231,11 @@ class ExampleInstancing : public entry::AppI
}
// Set vertex and index buffer.
- bgfx::setVertexBuffer(m_vbh);
+ bgfx::setVertexBuffer(0, m_vbh);
bgfx::setIndexBuffer(m_ibh);
// Set instance data buffer.
- bgfx::setInstanceDataBuffer(idb);
+ bgfx::setInstanceDataBuffer(&idb);
// Set render states.
bgfx::setState(BGFX_STATE_DEFAULT);
@@ -226,12 +248,15 @@ class ExampleInstancing : public entry::AppI
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
+
return true;
}
return false;
}
+ entry::MouseState m_mouseState;
+
uint32_t m_width;
uint32_t m_height;
uint32_t m_debug;
@@ -243,4 +268,6 @@ class ExampleInstancing : public entry::AppI
int64_t m_timeOffset;
};
-ENTRY_IMPLEMENT_MAIN(ExampleInstancing);
+} // namespace
+
+ENTRY_IMPLEMENT_MAIN(ExampleInstancing, "05-instancing", "Geometry instancing.");