bgfx/examples/08-update/update.cpp

680 lines
18 KiB
C++
Raw Normal View History

2013-01-07 05:53:45 +04:00
/*
2018-01-01 22:16:06 +03:00
* Copyright 2011-2018 Branimir Karadzic. All rights reserved.
2016-01-01 11:11:04 +03:00
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
2013-01-07 05:53:45 +04:00
*/
#include "common.h"
2014-05-04 02:18:28 +04:00
#include "bgfx_utils.h"
#include "packrect.h"
2017-06-26 07:44:04 +03:00
#include "imgui/imgui.h"
2013-01-07 05:53:45 +04:00
2018-06-16 18:34:06 +03:00
#include <bx/rng.h>
#include <list>
2013-01-07 05:53:45 +04:00
2017-06-26 07:44:04 +03:00
namespace
{
2014-05-04 02:18:28 +04:00
struct PosTexcoordVertex
2013-01-07 05:53:45 +04:00
{
float m_x;
float m_y;
float m_z;
float m_u;
float m_v;
float m_w;
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::TexCoord0, 3, bgfx::AttribType::Float)
.end();
2014-05-04 02:18:28 +04:00
};
static bgfx::VertexDecl ms_decl;
2013-01-07 05:53:45 +04:00
};
2014-05-04 02:18:28 +04:00
bgfx::VertexDecl PosTexcoordVertex::ms_decl;
2013-01-07 05:53:45 +04:00
2018-05-07 04:39:44 +03:00
static PosTexcoordVertex s_cubeVertices[] =
2013-01-07 05:53:45 +04:00
{
{-1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f },
{-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f },
{ 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f },
{-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f },
{ 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f },
{-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f },
{ 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f },
{-1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f },
{-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f },
{-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f },
{-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f },
{ 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f },
{ 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f },
{ 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f },
{-1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f },
{-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f },
{ 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f },
{-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f },
{-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f },
{ 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f },
{ 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f },
{-1.0f, 1.0f, 1.0f, -2.0f, 2.0f, 2.0f },
{ 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f },
{-1.0f, -1.0f, 1.0f, -2.0f, -2.0f, 2.0f },
{ 1.0f, -1.0f, 1.0f, 2.0f, -2.0f, 2.0f },
2013-01-07 05:53:45 +04:00
};
2018-05-07 04:39:44 +03:00
BX_STATIC_ASSERT(BX_COUNTOF(s_cubeVertices) == 28);
2013-01-07 05:53:45 +04:00
2018-05-07 04:39:44 +03:00
static const uint16_t s_cubeIndices[] =
2013-01-07 05:53:45 +04:00
{
2013-02-22 10:05:33 +04:00
0, 1, 2, // 0
1, 3, 2,
2013-02-22 10:05:33 +04:00
4, 6, 5, // 2
5, 6, 7,
2013-02-22 10:05:33 +04:00
8, 10, 9, // 4
9, 10, 11,
2013-02-22 10:05:33 +04:00
12, 14, 13, // 6
14, 15, 13,
2013-02-22 10:05:33 +04:00
16, 18, 17, // 8
18, 19, 17,
2013-02-22 10:05:33 +04:00
20, 22, 21, // 10
21, 22, 23,
2013-01-07 05:53:45 +04:00
};
2018-05-07 04:39:44 +03:00
BX_STATIC_ASSERT(BX_COUNTOF(s_cubeIndices) == 36);
static void updateTextureCubeRectBgra8(
bgfx::TextureHandle _handle
, uint8_t _side
, uint16_t _x
, uint16_t _y
, uint16_t _width
, uint16_t _height
, uint8_t _r
, uint8_t _g
, uint8_t _b
, uint8_t _a = 0xff
)
{
bgfx::TextureInfo ti;
2016-08-20 07:05:37 +03:00
bgfx::calcTextureSize(ti, _width, _height, 1, false, false, 1, bgfx::TextureFormat::BGRA8);
const bgfx::Memory* mem = bgfx::alloc(ti.storageSize);
uint8_t* data = (uint8_t*)mem->data;
for (uint32_t ii = 0, num = ti.storageSize*8/ti.bitsPerPixel; ii < num; ++ii)
{
data[0] = _b;
data[1] = _g;
data[2] = _r;
data[3] = _a;
data += 4;
}
2016-08-22 00:03:16 +03:00
bgfx::updateTextureCube(_handle, 0, _side, 0, _x, _y, _width, _height, mem);
}
2018-05-07 04:39:44 +03:00
static const uint16_t kTextureSide = 512;
static const uint32_t kTexture2dSize = 256;
2013-02-22 09:07:31 +04:00
2016-03-07 02:29:22 +03:00
class ExampleUpdate : public entry::AppI
2015-10-25 05:25:03 +03:00
{
public:
2017-06-26 07:44:04 +03:00
ExampleUpdate(const char* _name, const char* _description)
: entry::AppI(_name, _description)
2018-05-07 04:39:44 +03:00
, m_cube(kTextureSide)
2015-10-25 05:25:03 +03:00
{
}
2013-02-22 09:07:31 +04:00
2017-07-15 10:17:29 +03:00
void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
2015-10-25 05:25:03 +03:00
{
Args args(_argc, _argv);
2013-01-07 05:53:45 +04:00
2017-06-30 08:23:18 +03:00
m_width = _width;
m_height = _height;
2017-06-26 07:44:04 +03:00
m_debug = BGFX_DEBUG_NONE;
2015-10-25 05:25:03 +03:00
m_reset = BGFX_RESET_VSYNC;
2013-01-07 05:53:45 +04:00
bgfx::Init init;
init.type = args.m_type;
init.vendorId = args.m_pciId;
init.resolution.width = m_width;
init.resolution.height = m_height;
init.resolution.reset = m_reset;
bgfx::init(init);
2013-09-10 10:17:17 +04:00
2015-10-25 05:25:03 +03:00
// Enable debug text.
bgfx::setDebug(m_debug);
2015-10-25 05:25:03 +03:00
// Set view 0 clear state.
bgfx::setViewClear(0
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
, 0x303030ff
, 1.0f
, 0
);
2015-10-25 05:25:03 +03:00
// Create vertex stream declaration.
PosTexcoordVertex::init();
m_textures[ 0] = loadTexture("textures/texture_compression_bc1.ktx", BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP);
m_textures[ 1] = loadTexture("textures/texture_compression_bc2.ktx", BGFX_SAMPLER_U_CLAMP);
m_textures[ 2] = loadTexture("textures/texture_compression_bc3.ktx", BGFX_SAMPLER_V_CLAMP);
m_textures[ 3] = loadTexture("textures/texture_compression_etc1.ktx", BGFX_SAMPLER_U_BORDER | BGFX_SAMPLER_V_BORDER | BGFX_SAMPLER_BORDER_COLOR(1));
m_textures[ 4] = loadTexture("textures/texture_compression_etc2.ktx");
m_textures[ 5] = loadTexture("textures/texture_compression_ptc12.pvr");
m_textures[ 6] = loadTexture("textures/texture_compression_ptc14.pvr");
m_textures[ 7] = loadTexture("textures/texture_compression_ptc22.pvr");
m_textures[ 8] = loadTexture("textures/texture_compression_ptc24.pvr");
m_textures[ 9] = loadTexture("textures/texture_compression_atc.dds");
m_textures[10] = loadTexture("textures/texture_compression_atci.dds");
m_textures[11] = loadTexture("textures/texture_compression_atce.dds");
BX_STATIC_ASSERT(12 == BX_COUNTOF(m_textures));
2015-10-25 05:25:03 +03:00
const bgfx::Caps* caps = bgfx::getCaps();
m_texture3DSupported = !!(caps->supported & BGFX_CAPS_TEXTURE_3D);
m_blitSupported = !!(caps->supported & BGFX_CAPS_TEXTURE_BLIT);
m_computeSupported = !!(caps->supported & BGFX_CAPS_COMPUTE);
2016-10-23 00:26:58 +03:00
m_numTextures3d = 0;
2015-10-25 05:25:03 +03:00
if (m_texture3DSupported)
2015-10-24 06:57:04 +03:00
{
2015-10-25 05:25:03 +03:00
const bgfx::Memory* mem8 = bgfx::alloc(32*32*32);
const bgfx::Memory* mem16f = bgfx::alloc(32*32*32*2);
const bgfx::Memory* mem32f = bgfx::alloc(32*32*32*4);
for (uint8_t zz = 0; zz < 32; ++zz)
2015-10-24 06:57:04 +03:00
{
2015-10-25 05:25:03 +03:00
for (uint8_t yy = 0; yy < 32; ++yy)
2015-10-24 06:57:04 +03:00
{
2015-10-25 05:25:03 +03:00
for (uint8_t xx = 0; xx < 32; ++xx)
{
const uint32_t offset = ( (zz*32+yy)*32+xx);
const uint32_t val = xx ^ yy ^ zz;
2017-03-12 09:17:34 +03:00
mem8->data[offset] = uint8_t(val<<3);
2015-10-25 05:25:03 +03:00
*(uint16_t*)&mem16f->data[offset*2] = bx::halfFromFloat( (float)val/32.0f);
*(float*)&mem32f->data[offset*4] = (float)val/32.0f;
}
2015-10-24 06:57:04 +03:00
}
}
2017-12-20 22:11:02 +03:00
if (0 != (BGFX_CAPS_FORMAT_TEXTURE_3D & caps->formats[bgfx::TextureFormat::R8]) )
2015-10-25 05:25:03 +03:00
{
m_textures3d[m_numTextures3d++] = bgfx::createTexture3D(32, 32, 32, false, bgfx::TextureFormat::R8, BGFX_SAMPLER_U_CLAMP|BGFX_SAMPLER_V_CLAMP|BGFX_SAMPLER_W_CLAMP, mem8);
2015-10-25 05:25:03 +03:00
}
2017-12-20 22:11:02 +03:00
if (0 != (BGFX_CAPS_FORMAT_TEXTURE_3D & caps->formats[bgfx::TextureFormat::R16F]) )
2015-10-25 05:25:03 +03:00
{
m_textures3d[m_numTextures3d++] = bgfx::createTexture3D(32, 32, 32, false, bgfx::TextureFormat::R16F, BGFX_SAMPLER_U_CLAMP|BGFX_SAMPLER_V_CLAMP|BGFX_SAMPLER_W_CLAMP, mem16f);
2015-10-25 05:25:03 +03:00
}
2017-12-20 22:11:02 +03:00
if (0 != (BGFX_CAPS_FORMAT_TEXTURE_3D & caps->formats[bgfx::TextureFormat::R32F]) )
2015-10-25 05:25:03 +03:00
{
m_textures3d[m_numTextures3d++] = bgfx::createTexture3D(32, 32, 32, false, bgfx::TextureFormat::R32F, BGFX_SAMPLER_U_CLAMP|BGFX_SAMPLER_V_CLAMP|BGFX_SAMPLER_W_CLAMP, mem32f);
2015-10-25 05:25:03 +03:00
}
}
2015-10-25 05:25:03 +03:00
// Create static vertex buffer.
2018-05-07 04:39:44 +03:00
m_vbh = bgfx::createVertexBuffer(bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) ), PosTexcoordVertex::ms_decl);
2015-10-25 05:25:03 +03:00
// Create static index buffer.
2018-05-07 04:39:44 +03:00
m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
2015-10-25 05:25:03 +03:00
// Create programs.
m_program = loadProgram("vs_update", "fs_update");
m_programCmp = loadProgram("vs_update", "fs_update_cmp");
2017-06-10 07:57:08 +03:00
m_program3d.idx = bgfx::kInvalidHandle;
2015-10-25 05:25:03 +03:00
if (m_texture3DSupported)
{
2015-10-25 05:25:03 +03:00
m_program3d = loadProgram("vs_update", "fs_update_3d");
}
2017-06-10 07:57:08 +03:00
m_programCompute.idx = bgfx::kInvalidHandle;
if (m_computeSupported)
{
m_programCompute = bgfx::createProgram( loadShader( "cs_update" ), true );
}
2015-10-25 05:25:03 +03:00
// Create texture sampler uniforms.
s_texCube = bgfx::createUniform("s_texCube", bgfx::UniformType::Sampler);
s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
2013-01-07 05:53:45 +04:00
2015-10-25 05:25:03 +03:00
// Create time uniform.
u_time = bgfx::createUniform("u_time", bgfx::UniformType::Vec4);
2013-01-07 05:53:45 +04:00
for(uint32_t ii = 0; ii<BX_COUNTOF( m_textureCube ); ++ii)
{
2017-06-10 07:57:08 +03:00
m_textureCube[ii].idx = bgfx::kInvalidHandle;
}
2016-08-20 07:05:37 +03:00
m_textureCube[0] = bgfx::createTextureCube(
2018-05-07 04:39:44 +03:00
kTextureSide
2018-04-24 02:23:38 +03:00
, false
, 1
, bgfx::TextureFormat::BGRA8
, BGFX_SAMPLER_MIN_POINT|BGFX_SAMPLER_MAG_POINT|BGFX_SAMPLER_MIP_POINT
2018-04-24 02:23:38 +03:00
);
if (m_blitSupported)
{
m_textureCube[1] = bgfx::createTextureCube(
2018-05-07 04:39:44 +03:00
kTextureSide
2016-08-20 07:05:37 +03:00
, false
, 1
2015-10-25 05:25:03 +03:00
, bgfx::TextureFormat::BGRA8
, BGFX_SAMPLER_MIN_POINT|BGFX_SAMPLER_MAG_POINT|BGFX_SAMPLER_MIP_POINT|BGFX_TEXTURE_BLIT_DST
2015-10-25 05:25:03 +03:00
);
}
2013-01-07 05:53:45 +04:00
2016-10-23 00:26:58 +03:00
if (m_computeSupported)
{
m_textureCube[2] = bgfx::createTextureCube(
2018-05-07 04:39:44 +03:00
kTextureSide
, false
, 1
, bgfx::TextureFormat::RGBA8
, BGFX_TEXTURE_COMPUTE_WRITE
);
}
2016-08-20 07:05:37 +03:00
m_texture2d = bgfx::createTexture2D(
2018-05-07 04:39:44 +03:00
kTexture2dSize
, kTexture2dSize
2018-04-24 02:23:38 +03:00
, false
, 1
, bgfx::TextureFormat::BGRA8
, BGFX_SAMPLER_MIN_POINT|BGFX_SAMPLER_MAG_POINT|BGFX_SAMPLER_MIP_POINT
2018-04-24 02:23:38 +03:00
);
2018-05-07 04:39:44 +03:00
m_texture2dData = (uint8_t*)malloc(kTexture2dSize*kTexture2dSize*4);
2013-01-07 05:53:45 +04:00
2018-06-16 18:34:06 +03:00
m_rr = m_rng.gen()%255;
m_gg = m_rng.gen()%255;
m_bb = m_rng.gen()%255;
2015-10-18 03:39:44 +03:00
2015-10-25 05:25:03 +03:00
m_hit = 0;
m_miss = 0;
2013-11-09 10:10:31 +04:00
2015-10-25 05:25:03 +03:00
m_updateTime = 0;
m_timeOffset = bx::getHPCounter();
2017-06-26 07:44:04 +03:00
imguiCreate();
2015-10-18 03:39:44 +03:00
}
2017-07-15 10:17:29 +03:00
virtual int shutdown() override
2015-10-25 05:25:03 +03:00
{
2017-06-26 07:44:04 +03:00
imguiDestroy();
2016-02-02 07:20:17 +03:00
// m_texture2dData is managed from main thread, and it's passed to renderer
2015-10-25 05:25:03 +03:00
// just as MemoryRef. At this point render might be using it. We must wait
// previous frame to finish before we can free it.
bgfx::frame();
2013-11-09 10:10:31 +04:00
2015-10-25 05:25:03 +03:00
// Cleanup.
2016-02-02 07:20:17 +03:00
free(m_texture2dData);
2013-01-07 05:53:45 +04:00
2015-10-25 05:25:03 +03:00
for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
{
bgfx::destroy(m_textures[ii]);
2015-10-25 05:25:03 +03:00
}
2013-01-07 05:53:45 +04:00
2016-10-23 00:26:58 +03:00
for (uint32_t ii = 0; ii < m_numTextures3d; ++ii)
2015-10-25 05:25:03 +03:00
{
bgfx::destroy(m_textures3d[ii]);
2015-10-25 05:25:03 +03:00
}
2013-01-07 05:53:45 +04:00
bgfx::destroy(m_texture2d);
for (uint32_t ii = 0; ii<BX_COUNTOF(m_textureCube); ++ii)
2015-10-25 05:25:03 +03:00
{
if (bgfx::isValid(m_textureCube[ii]))
{
bgfx::destroy(m_textureCube[ii]);
}
2015-10-25 05:25:03 +03:00
}
bgfx::destroy(m_ibh);
bgfx::destroy(m_vbh);
2015-10-25 05:25:03 +03:00
if (bgfx::isValid(m_program3d) )
{
bgfx::destroy(m_program3d);
2015-10-25 05:25:03 +03:00
}
bgfx::destroy(m_programCmp);
if (bgfx::isValid(m_programCompute) )
{
bgfx::destroy(m_programCompute);
}
bgfx::destroy(m_program);
bgfx::destroy(u_time);
bgfx::destroy(s_texColor);
bgfx::destroy(s_texCube);
2015-10-25 05:25:03 +03:00
// Shutdown bgfx.
bgfx::shutdown();
2015-10-25 05:25:03 +03:00
return 0;
}
2013-04-27 23:43:18 +04:00
2017-07-15 10:17:29 +03:00
bool update() override
2013-02-22 09:07:31 +04:00
{
2017-06-26 07:44:04 +03:00
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
2013-01-07 05:53:45 +04:00
{
2017-06-26 07:44:04 +03:00
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)
);
2017-06-30 09:19:20 +03:00
showExampleDialog(this);
2017-06-26 07:44:04 +03:00
imguiEndFrame();
2018-06-16 18:34:06 +03:00
float borderColor[4] =
{
float(m_rng.gen()%255)/255.0f,
float(m_rng.gen()%255)/255.0f,
float(m_rng.gen()%255)/255.0f,
float(m_rng.gen()%255)/255.0f,
};
2015-10-25 05:25:03 +03:00
bgfx::setPaletteColor(1, borderColor);
// Set view 0 and 1 viewport.
2017-03-12 09:17:34 +03:00
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
2015-10-25 05:25: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);
int64_t now = bx::getHPCounter();
float time = (float)( (now - m_timeOffset)/double(bx::getHPFrequency() ) );
bgfx::setUniform(u_time, &time);
if (now > m_updateTime)
2013-01-07 05:53:45 +04:00
{
2015-10-25 05:25:03 +03:00
PackCube face;
2018-06-16 18:34:06 +03:00
uint16_t bw = bx::max<uint16_t>(1, m_rng.gen()%(kTextureSide/4) );
uint16_t bh = bx::max<uint16_t>(1, m_rng.gen()%(kTextureSide/4) );
2015-10-25 05:25:03 +03:00
if (m_cube.find(bw, bh, face) )
2015-10-18 03:39:44 +03:00
{
2015-10-25 05:25:03 +03:00
m_quads.push_back(face);
2013-01-07 05:53:45 +04:00
2015-10-25 05:25:03 +03:00
++m_hit;
2015-10-24 04:13:04 +03:00
const Pack2D& rect = face.m_rect;
2015-10-25 05:25:03 +03:00
updateTextureCubeRectBgra8(m_textureCube[0], face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, m_rr, m_gg, m_bb);
if (m_blitSupported)
2015-10-24 04:13:04 +03:00
{
2015-10-25 05:25:03 +03:00
bgfx::blit(0, m_textureCube[1], 0, rect.m_x, rect.m_y, face.m_side, m_textureCube[0], 0, rect.m_x, rect.m_y, face.m_side, rect.m_width, rect.m_height);
2015-10-24 04:13:04 +03:00
}
2018-06-16 18:34:06 +03:00
m_rr = m_rng.gen()%255;
m_gg = m_rng.gen()%255;
m_bb = m_rng.gen()%255;
2013-01-07 05:53:45 +04:00
}
2015-10-25 05:25:03 +03:00
else
{
++m_miss;
2013-11-09 10:10:31 +04:00
2015-10-25 05:25:03 +03:00
for (uint32_t ii = 0, num = bx::uint32_min(10, (uint32_t)m_quads.size() ); ii < num; ++ii)
{
face = m_quads.front();
const Pack2D& rect = face.m_rect;
2013-11-09 10:10:31 +04:00
2015-10-25 05:25:03 +03:00
updateTextureCubeRectBgra8(m_textureCube[0], face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, 0, 0, 0);
if (m_blitSupported)
{
bgfx::blit(0, m_textureCube[1], 0, rect.m_x, rect.m_y, face.m_side, m_textureCube[0], 0, rect.m_x, rect.m_y, face.m_side, rect.m_width, rect.m_height);
}
2013-11-09 10:10:31 +04:00
2015-10-25 05:25:03 +03:00
m_cube.clear(face);
m_quads.pop_front();
2013-11-09 10:10:31 +04:00
}
}
2015-10-25 05:25:03 +03:00
{
// Fill rect.
2018-05-07 04:39:44 +03:00
const uint32_t pitch = kTexture2dSize*4;
2018-06-16 18:34:06 +03:00
const uint16_t tw = m_rng.gen()% kTexture2dSize;
const uint16_t th = m_rng.gen()% kTexture2dSize;
const uint16_t tx = m_rng.gen()%(kTexture2dSize-tw);
const uint16_t ty = m_rng.gen()%(kTexture2dSize-th);
2013-01-07 05:53:45 +04:00
2018-05-07 04:39:44 +03:00
uint8_t* dst = &m_texture2dData[(ty*kTexture2dSize+tx)*4];
2015-10-25 05:25:03 +03:00
uint8_t* next = dst + pitch;
2013-01-07 05:53:45 +04:00
2015-10-25 05:25:03 +03:00
// Using makeRef to pass texture memory without copying.
const bgfx::Memory* mem = bgfx::makeRef(dst, tw*th*4);
2013-01-07 05:53:45 +04:00
2015-10-25 05:25:03 +03:00
for (uint32_t yy = 0; yy < th; ++yy, dst = next, next += pitch)
{
for (uint32_t xx = 0; xx < tw; ++xx, dst += 4)
{
dst[0] = m_bb;
dst[1] = m_gg;
dst[2] = m_rr;
dst[3] = 255;
}
}
2013-01-07 05:53:45 +04:00
2015-10-25 05:25:03 +03:00
// Pitch here makes possible to pass data from source to destination
// without need for m_textures and allocated memory to be the same size.
2016-08-22 00:03:16 +03:00
bgfx::updateTexture2D(m_texture2d, 0, 0, tx, ty, tw, th, mem, pitch);
2015-10-25 05:25:03 +03:00
}
}
2013-01-07 05:53:45 +04:00
2018-11-17 08:54:20 +03:00
const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
const bx::Vec3 eye = { 0.0f, 0.0f, -5.0f };
2013-01-07 05:53:45 +04:00
2015-10-25 05:25:03 +03:00
float view[16];
bx::mtxLookAt(view, eye, at);
2018-01-20 04:07:03 +03:00
float proj[16];
2017-02-23 09:26:39 +03:00
bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
2013-09-10 10:17:17 +04:00
2015-10-25 05:25:03 +03:00
// Set view and projection matrix for view 0.
bgfx::setViewTransform(0, view, proj);
2013-09-10 10:17:17 +04:00
// Update texturecube using compute shader
2016-10-23 00:26:58 +03:00
if (bgfx::isValid(m_programCompute) )
2015-10-25 05:25:03 +03:00
{
2018-01-20 04:07:03 +03:00
bgfx::setImage(0, m_textureCube[2], 0, bgfx::Access::Write);
2018-05-07 04:39:44 +03:00
bgfx::dispatch(0, m_programCompute, kTextureSide/16, kTextureSide/16);
}
2013-11-09 10:10:31 +04:00
for (uint32_t ii = 0; ii < BX_COUNTOF(m_textureCube); ++ii)
{
2016-10-23 00:26:58 +03:00
if (bgfx::isValid(m_textureCube[ii]))
{
float mtx[16];
2018-01-20 04:07:03 +03:00
bx::mtxSRT(mtx, 0.7f, 0.7f, 0.7f, time, time*0.37f, 0.0f, -2.0f +ii*2.0f, 0.0f, 0.0f);
2013-11-09 10:10:31 +04:00
// Set model matrix for rendering.
2018-01-20 04:07:03 +03:00
bgfx::setTransform(mtx);
2013-11-09 10:10:31 +04:00
// Set vertex and index buffer.
2018-05-07 04:39:44 +03:00
bgfx::setVertexBuffer(0, m_vbh);
2018-01-20 04:07:03 +03:00
bgfx::setIndexBuffer(m_ibh);
2013-11-09 10:10:31 +04:00
// Bind texture.
2018-01-20 04:07:03 +03:00
bgfx::setTexture(0, s_texCube, m_textureCube[ii]);
// Set render states.
2018-01-20 04:07:03 +03:00
bgfx::setState(BGFX_STATE_DEFAULT);
2013-11-09 10:10:31 +04:00
// Submit primitive for rendering to view 0.
2018-01-20 04:07:03 +03:00
bgfx::submit(0, m_program);
}
2015-10-25 05:25:03 +03:00
}
2013-11-09 10:10:31 +04:00
2015-10-25 05:25:03 +03:00
// Set view and projection matrix for view 1.
const float aspectRatio = float(m_height)/float(m_width);
const float margin = 0.7f;
const float sizeX = 0.5f * BX_COUNTOF(m_textures) * 2.1f + margin;
const float sizeY = sizeX * aspectRatio;
2017-06-13 08:43:07 +03:00
const bgfx::Caps* caps = bgfx::getCaps();
bx::mtxOrtho(proj, -sizeX, sizeX, sizeY, -sizeY, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth);
2015-10-25 05:25:03 +03:00
bgfx::setViewTransform(1, NULL, proj);
2013-11-09 10:10:31 +04:00
2015-10-25 05:25:03 +03:00
float mtx[16];
bx::mtxTranslate(mtx, -sizeX + margin + 1.0f, 1.9f, 0.0f);
2013-09-10 10:17:17 +04:00
// Set model matrix for rendering.
bgfx::setTransform(mtx);
// Set vertex and index buffer.
2017-05-14 21:48:59 +03:00
bgfx::setVertexBuffer(0, m_vbh);
2015-10-25 05:25:03 +03:00
bgfx::setIndexBuffer(m_ibh);
// Bind texture.
2015-10-25 05:25:03 +03:00
bgfx::setTexture(0, s_texColor, m_texture2d);
// Set render states.
bgfx::setState(BGFX_STATE_DEFAULT);
2014-10-05 11:18:07 +04:00
// Submit primitive for rendering to view 1.
2015-10-25 05:25:03 +03:00
bgfx::submit(1, m_programCmp);
const float xpos = -sizeX + margin + 1.0f;
2015-10-25 05:25:03 +03:00
for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
{
bx::mtxTranslate(mtx, xpos + ii*2.1f, sizeY - margin - 1.0f, 0.0f);
2015-10-25 05:25:03 +03:00
// Set model matrix for rendering.
bgfx::setTransform(mtx);
2015-10-25 05:25:03 +03:00
// Set vertex and index buffer.
2017-05-14 21:48:59 +03:00
bgfx::setVertexBuffer(0, m_vbh);
2015-10-25 05:25:03 +03:00
bgfx::setIndexBuffer(m_ibh, 0, 6);
2015-10-25 05:25:03 +03:00
// Bind texture.
bgfx::setTexture(0, s_texColor, m_textures[ii]);
2015-10-25 05:25:03 +03:00
// Set render states.
bgfx::setState(BGFX_STATE_DEFAULT);
2015-10-25 05:25:03 +03:00
// Submit primitive for rendering to view 1.
bgfx::submit(1, m_programCmp);
}
2016-10-23 00:26:58 +03:00
for (uint32_t ii = 0; ii < m_numTextures3d; ++ii)
2015-10-25 05:25:03 +03:00
{
bx::mtxTranslate(mtx, xpos + (ii+(BX_COUNTOF(m_textures) - m_numTextures3d)*0.5f)*2.1f, -sizeY + margin + 1.0f, 0.0f);
2015-10-25 05:25:03 +03:00
// Set model matrix for rendering.
bgfx::setTransform(mtx);
2013-09-10 10:17:17 +04:00
2015-10-25 05:25:03 +03:00
// Set vertex and index buffer.
2017-05-14 21:48:59 +03:00
bgfx::setVertexBuffer(0, m_vbh);
2015-10-25 05:25:03 +03:00
bgfx::setIndexBuffer(m_ibh, 0, 6);
2013-09-10 10:17:17 +04:00
2015-10-25 05:25:03 +03:00
// Bind texture.
bgfx::setTexture(0, s_texColor, m_textures3d[ii]);
2013-09-10 10:17:17 +04:00
2015-10-25 05:25:03 +03:00
// Set render states.
bgfx::setState(BGFX_STATE_DEFAULT);
2013-09-10 10:17:17 +04:00
2015-10-25 05:25:03 +03:00
// Submit primitive for rendering to view 1.
bgfx::submit(1, m_program3d);
}
for (uint32_t ii = 0; ii < 4; ++ii)
{
bx::mtxTranslate(mtx, sizeX - margin - 1.0f, -sizeY + margin + 1.0f + ii*2.1f, 0.0f);
2013-01-07 05:53:45 +04:00
2015-10-25 05:25:03 +03:00
// Set model matrix for rendering.
bgfx::setTransform(mtx);
2014-02-21 10:04:50 +04:00
2015-10-25 05:25:03 +03:00
// Set vertex and index buffer.
2017-05-14 21:48:59 +03:00
bgfx::setVertexBuffer(0, m_vbh, 24, 4);
2015-10-25 05:25:03 +03:00
bgfx::setIndexBuffer(m_ibh, 0, 6);
2013-11-09 10:10:31 +04:00
2015-10-25 05:25:03 +03:00
// Bind texture.
bgfx::setTexture(0, s_texColor, m_textures[ii]);
2013-11-18 08:57:38 +04:00
2015-10-25 05:25:03 +03:00
// Set render states.
bgfx::setState(BGFX_STATE_DEFAULT);
2015-10-25 05:25:03 +03:00
// Submit primitive for rendering to view 1.
bgfx::submit(1, m_programCmp);
}
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
2017-06-26 07:44:04 +03:00
2017-06-30 09:19:20 +03:00
return true;
2015-10-25 05:25:03 +03:00
}
return false;
2015-03-30 04:18:06 +03:00
}
2013-01-07 05:53:45 +04:00
2017-06-26 07:44:04 +03:00
entry::MouseState m_mouseState;
2015-10-25 05:25:03 +03:00
uint32_t m_width;
uint32_t m_height;
uint32_t m_debug;
uint32_t m_reset;
2017-06-26 07:44:04 +03:00
uint8_t* m_texture2dData;
2016-10-23 00:26:58 +03:00
uint32_t m_numTextures3d;
2015-10-25 05:25:03 +03:00
bool m_texture3DSupported;
bool m_blitSupported;
bool m_computeSupported;
2015-10-25 05:25:03 +03:00
std::list<PackCube> m_quads;
RectPackCubeT<256> m_cube;
int64_t m_updateTime;
int64_t m_timeOffset;
2018-06-16 18:34:06 +03:00
bx::RngMwc m_rng;
2015-10-25 05:25:03 +03:00
uint32_t m_hit;
uint32_t m_miss;
uint8_t m_rr;
uint8_t m_gg;
uint8_t m_bb;
bgfx::TextureHandle m_textures[12];
2015-10-25 05:25:03 +03:00
bgfx::TextureHandle m_textures3d[3];
bgfx::TextureHandle m_texture2d;
bgfx::TextureHandle m_textureCube[3];
2015-10-25 05:25:03 +03:00
bgfx::IndexBufferHandle m_ibh;
bgfx::VertexBufferHandle m_vbh;
bgfx::ProgramHandle m_program3d;
bgfx::ProgramHandle m_programCmp;
bgfx::ProgramHandle m_programCompute;
2015-10-25 05:25:03 +03:00
bgfx::ProgramHandle m_program;
bgfx::UniformHandle u_time;
bgfx::UniformHandle s_texColor;
bgfx::UniformHandle s_texCube;
2013-01-07 05:53:45 +04:00
2015-10-25 05:25:03 +03:00
};
2017-06-26 07:44:04 +03:00
} // namespace
2017-07-01 09:36:37 +03:00
ENTRY_IMPLEMENT_MAIN(ExampleUpdate, "08-update", "Updating textures.");