Refactor example to use new entry.

This commit is contained in:
Branimir Karadžić 2015-10-24 19:25:03 -07:00
parent 0cfc61b8ac
commit e3c97d8dc7

View file

@ -34,7 +34,7 @@ struct PosTexcoordVertex
bgfx::VertexDecl PosTexcoordVertex::ms_decl; bgfx::VertexDecl PosTexcoordVertex::ms_decl;
static PosTexcoordVertex s_cubeVertices[28] = static PosTexcoordVertex s_m_cubeVertices[28] =
{ {
{-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 },
@ -72,7 +72,7 @@ static PosTexcoordVertex s_cubeVertices[28] =
{ 1.0f, -1.0f, 1.0f, 2.0f, -2.0f, 2.0f }, { 1.0f, -1.0f, 1.0f, 2.0f, -2.0f, 2.0f },
}; };
static const uint16_t s_cubeIndices[36] = static const uint16_t s_m_cubeIndices[36] =
{ {
0, 1, 2, // 0 0, 1, 2, // 0
1, 3, 2, 1, 3, 2,
@ -112,20 +112,31 @@ static void updateTextureCubeRectBgra8(bgfx::TextureHandle _handle, uint8_t _sid
bgfx::updateTextureCube(_handle, _side, 0, _x, _y, _width, _height, mem); bgfx::updateTextureCube(_handle, _side, 0, _x, _y, _width, _height, mem);
} }
int _main_(int _argc, char** _argv) static const uint32_t m_textureside = 512;
static const uint32_t m_texture2dSize = 256;
class Update : public entry::AppI
{ {
public:
Update()
: m_cube(m_textureside)
{
}
void init(int _argc, char** _argv) BX_OVERRIDE
{
Args args(_argc, _argv); Args args(_argc, _argv);
uint32_t width = 1280; m_width = 1280;
uint32_t height = 720; m_height = 720;
uint32_t debug = BGFX_DEBUG_TEXT; m_debug = BGFX_DEBUG_TEXT;
uint32_t reset = BGFX_RESET_VSYNC; m_reset = BGFX_RESET_VSYNC;
bgfx::init(args.m_type, args.m_pciId); bgfx::init(args.m_type, args.m_pciId);
bgfx::reset(width, height, reset); bgfx::reset(m_width, m_height, m_reset);
// Enable debug text. // Enable debug text.
bgfx::setDebug(debug); bgfx::setDebug(m_debug);
// Set view 0 clear state. // Set view 0 clear state.
bgfx::setViewClear(0 bgfx::setViewClear(0
@ -138,27 +149,22 @@ int _main_(int _argc, char** _argv)
// Create vertex stream declaration. // Create vertex stream declaration.
PosTexcoordVertex::init(); PosTexcoordVertex::init();
bgfx::TextureHandle textures[] = m_textures[0] = loadTexture("texture_compression_bc1.dds", BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP);
{ m_textures[1] = loadTexture("texture_compression_bc2.dds", BGFX_TEXTURE_U_CLAMP);
loadTexture("texture_compression_bc1.dds", BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP), m_textures[2] = loadTexture("texture_compression_bc3.dds", BGFX_TEXTURE_V_CLAMP);
loadTexture("texture_compression_bc2.dds", BGFX_TEXTURE_U_CLAMP), m_textures[3] = loadTexture("texture_compression_etc1.ktx", BGFX_TEXTURE_U_BORDER|BGFX_TEXTURE_V_BORDER|BGFX_TEXTURE_BORDER_COLOR(1) );
loadTexture("texture_compression_bc3.dds", BGFX_TEXTURE_V_CLAMP), m_textures[4] = loadTexture("texture_compression_etc2.ktx");
loadTexture("texture_compression_etc1.ktx", BGFX_TEXTURE_U_BORDER|BGFX_TEXTURE_V_BORDER|BGFX_TEXTURE_BORDER_COLOR(1) ), m_textures[5] = loadTexture("texture_compression_ptc12.pvr");
loadTexture("texture_compression_etc2.ktx"), m_textures[6] = loadTexture("texture_compression_ptc14.pvr");
loadTexture("texture_compression_ptc12.pvr"), m_textures[7] = loadTexture("texture_compression_ptc22.pvr");
loadTexture("texture_compression_ptc14.pvr"), m_textures[8] = loadTexture("texture_compression_ptc24.pvr");
loadTexture("texture_compression_ptc22.pvr"),
loadTexture("texture_compression_ptc24.pvr"),
};
const bgfx::Caps* caps = bgfx::getCaps(); const bgfx::Caps* caps = bgfx::getCaps();
const bool texture3DSupported = !!(caps->supported & BGFX_CAPS_TEXTURE_3D); m_texture3DSupported = !!(caps->supported & BGFX_CAPS_TEXTURE_3D);
const bool blitSupported = !!(caps->supported & BGFX_CAPS_TEXTURE_BLIT); m_blitSupported = !!(caps->supported & BGFX_CAPS_TEXTURE_BLIT);
m_numm_textures3d = 0;
uint32_t numTextures3d = 0; if (m_texture3DSupported)
bgfx::TextureHandle textures3d[3] = {};
if (texture3DSupported)
{ {
const bgfx::Memory* mem8 = bgfx::alloc(32*32*32); const bgfx::Memory* mem8 = bgfx::alloc(32*32*32);
const bgfx::Memory* mem16f = bgfx::alloc(32*32*32*2); const bgfx::Memory* mem16f = bgfx::alloc(32*32*32*2);
@ -180,90 +186,127 @@ int _main_(int _argc, char** _argv)
if (0 != (BGFX_CAPS_FORMAT_TEXTURE_2D & caps->formats[bgfx::TextureFormat::R8]) ) if (0 != (BGFX_CAPS_FORMAT_TEXTURE_2D & caps->formats[bgfx::TextureFormat::R8]) )
{ {
textures3d[numTextures3d++] = bgfx::createTexture3D(32, 32, 32, 0, bgfx::TextureFormat::R8, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP, mem8); m_textures3d[m_numm_textures3d++] = bgfx::createTexture3D(32, 32, 32, 0, bgfx::TextureFormat::R8, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP, mem8);
} }
if (0 != (BGFX_CAPS_FORMAT_TEXTURE_2D & caps->formats[bgfx::TextureFormat::R16F]) ) if (0 != (BGFX_CAPS_FORMAT_TEXTURE_2D & caps->formats[bgfx::TextureFormat::R16F]) )
{ {
textures3d[numTextures3d++] = bgfx::createTexture3D(32, 32, 32, 0, bgfx::TextureFormat::R16F, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP, mem16f); m_textures3d[m_numm_textures3d++] = bgfx::createTexture3D(32, 32, 32, 0, bgfx::TextureFormat::R16F, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP, mem16f);
} }
if (0 != (BGFX_CAPS_FORMAT_TEXTURE_2D & caps->formats[bgfx::TextureFormat::R32F]) ) if (0 != (BGFX_CAPS_FORMAT_TEXTURE_2D & caps->formats[bgfx::TextureFormat::R32F]) )
{ {
textures3d[numTextures3d++] = bgfx::createTexture3D(32, 32, 32, 0, bgfx::TextureFormat::R32F, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP, mem32f); m_textures3d[m_numm_textures3d++] = bgfx::createTexture3D(32, 32, 32, 0, bgfx::TextureFormat::R32F, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP, mem32f);
} }
} }
// Create static vertex buffer. // Create static vertex buffer.
bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) ), PosTexcoordVertex::ms_decl); m_vbh = bgfx::createVertexBuffer(bgfx::makeRef(s_m_cubeVertices, sizeof(s_m_cubeVertices) ), PosTexcoordVertex::ms_decl);
// Create static index buffer. // Create static index buffer.
bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) ); m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_m_cubeIndices, sizeof(s_m_cubeIndices) ) );
// Create programs. // Create programs.
bgfx::ProgramHandle program = loadProgram("vs_update", "fs_update"); m_program = loadProgram("vs_update", "fs_update");
bgfx::ProgramHandle programCmp = loadProgram("vs_update", "fs_update_cmp"); m_programCmp = loadProgram("vs_update", "fs_update_cmp");
bgfx::ProgramHandle program3d = BGFX_INVALID_HANDLE; m_program3d = BGFX_INVALID_HANDLE;
if (texture3DSupported) if (m_texture3DSupported)
{ {
program3d = loadProgram("vs_update", "fs_update_3d"); m_program3d = loadProgram("vs_update", "fs_update_3d");
} }
// Create texture sampler uniforms. // Create texture sampler uniforms.
bgfx::UniformHandle s_texCube = bgfx::createUniform("s_texCube", bgfx::UniformType::Int1); s_texCube = bgfx::createUniform("s_texCube", bgfx::UniformType::Int1);
bgfx::UniformHandle s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1); s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
// Create time uniform. // Create time uniform.
bgfx::UniformHandle u_time = bgfx::createUniform("u_time", bgfx::UniformType::Vec4); u_time = bgfx::createUniform("u_time", bgfx::UniformType::Vec4);
const uint32_t textureSide = 2048; m_textureCube[0] = bgfx::createTextureCube(m_textureside, 1
bgfx::TextureHandle textureCube[2];
textureCube[0] = bgfx::createTextureCube(textureSide, 1
, bgfx::TextureFormat::BGRA8 , bgfx::TextureFormat::BGRA8
, BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
); );
if (blitSupported) if (m_blitSupported)
{ {
textureCube[1] = bgfx::createTextureCube(textureSide, 1 m_textureCube[1] = bgfx::createTextureCube(m_textureside, 1
, bgfx::TextureFormat::BGRA8 , bgfx::TextureFormat::BGRA8
, BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT|BGFX_TEXTURE_BLIT_DST , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT|BGFX_TEXTURE_BLIT_DST
); );
} }
const uint32_t texture2dSize = 256; m_texture2d = bgfx::createTexture2D(m_texture2dSize, m_texture2dSize, 1
bgfx::TextureHandle texture2d = bgfx::createTexture2D(texture2dSize, texture2dSize, 1
, bgfx::TextureFormat::BGRA8 , bgfx::TextureFormat::BGRA8
, BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
); );
uint8_t* texture2dData = (uint8_t*)malloc(texture2dSize*texture2dSize*4); m_m_texture2dData = (uint8_t*)malloc(m_texture2dSize*m_texture2dSize*4);
uint8_t rr = rand()%255; m_rr = rand()%255;
uint8_t gg = rand()%255; m_gg = rand()%255;
uint8_t bb = rand()%255; m_bb = rand()%255;
int64_t updateTime = 0; m_hit = 0;
m_miss = 0;
RectPackCubeT<256> cube(textureSide); m_updateTime = 0;
m_timeOffset = bx::getHPCounter();
}
uint32_t hit = 0; virtual int shutdown() BX_OVERRIDE
uint32_t miss = 0; {
std::list<PackCube> quads; // m_m_texture2dData is managed from main thread, and it's passed to renderer
// 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();
int64_t timeOffset = bx::getHPCounter(); // Cleanup.
free(m_m_texture2dData);
while (!entry::processEvents(width, height, debug, reset) ) for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
{
bgfx::destroyTexture(m_textures[ii]);
}
for (uint32_t ii = 0; ii < m_numm_textures3d; ++ii)
{
bgfx::destroyTexture(m_textures3d[ii]);
}
bgfx::destroyTexture(m_texture2d);
bgfx::destroyTexture(m_textureCube[0]);
if (m_blitSupported)
{
bgfx::destroyTexture(m_textureCube[1]);
}
bgfx::destroyIndexBuffer(m_ibh);
bgfx::destroyVertexBuffer(m_vbh);
if (bgfx::isValid(m_program3d) )
{
bgfx::destroyProgram(m_program3d);
}
bgfx::destroyProgram(m_programCmp);
bgfx::destroyProgram(m_program);
bgfx::destroyUniform(u_time);
bgfx::destroyUniform(s_texColor);
bgfx::destroyUniform(s_texCube);
// Shutdown bgfx.
bgfx::shutdown();
return 0;
}
bool update() BX_OVERRIDE
{
if (!entry::processEvents(m_width, m_height, m_debug, m_reset) )
{ {
float borderColor[4] = { float(rand()%255)/255.0f, float(rand()%255)/255.0f, float(rand()%255)/255.0f, float(rand()%255)/255.0f }; float borderColor[4] = { float(rand()%255)/255.0f, float(rand()%255)/255.0f, float(rand()%255)/255.0f, float(rand()%255)/255.0f };
bgfx::setPaletteColor(1, borderColor); bgfx::setPaletteColor(1, borderColor);
// Set view 0 and 1 viewport. // Set view 0 and 1 viewport.
bgfx::setViewRect(0, 0, 0, width, height); bgfx::setViewRect(0, 0, 0, m_width, m_height);
bgfx::setViewRect(1, 0, 0, width, height); bgfx::setViewRect(1, 0, 0, m_width, m_height);
// This dummy draw call is here to make sure that view 0 is cleared // This dummy draw call is here to make sure that view 0 is cleared
// if no other draw calls are submitted to view 0. // if no other draw calls are submitted to view 0.
@ -275,69 +318,69 @@ int _main_(int _argc, char** _argv)
last = now; last = now;
const int64_t freq = bx::getHPFrequency(); const int64_t freq = bx::getHPFrequency();
const double toMs = 1000.0/double(freq); const double toMs = 1000.0/double(freq);
float time = (float)( (now - timeOffset)/double(bx::getHPFrequency() ) ); float time = (float)( (now - m_timeOffset)/double(bx::getHPFrequency() ) );
bgfx::setUniform(u_time, &time); bgfx::setUniform(u_time, &time);
// Use debug font to print information about this example. // Use debug font to print information about this example.
bgfx::dbgTextClear(); bgfx::dbgTextClear();
bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/08-update"); bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/08-update");
bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating textures."); bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating m_textures.");
bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs); bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
if (now > updateTime) if (now > m_updateTime)
{ {
PackCube face; PackCube face;
uint32_t bw = bx::uint16_max(1, rand()%(textureSide/4) ); uint32_t bw = bx::uint16_max(1, rand()%(m_textureside/4) );
uint32_t bh = bx::uint16_max(1, rand()%(textureSide/4) ); uint32_t bh = bx::uint16_max(1, rand()%(m_textureside/4) );
if (cube.find(bw, bh, face) ) if (m_cube.find(bw, bh, face) )
{ {
quads.push_back(face); m_quads.push_back(face);
++hit; ++m_hit;
const Pack2D& rect = face.m_rect; const Pack2D& rect = face.m_rect;
updateTextureCubeRectBgra8(textureCube[0], face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, rr, gg, bb); 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 (blitSupported) if (m_blitSupported)
{ {
bgfx::blit(0, textureCube[1], 0, rect.m_x, rect.m_y, face.m_side, textureCube[0], 0, rect.m_x, rect.m_y, face.m_side, rect.m_width, rect.m_height); 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);
} }
rr = rand()%255; m_rr = rand()%255;
gg = rand()%255; m_gg = rand()%255;
bb = rand()%255; m_bb = rand()%255;
} }
else else
{ {
++miss; ++m_miss;
for (uint32_t ii = 0, num = bx::uint32_min(10, (uint32_t)quads.size() ); ii < num; ++ii) for (uint32_t ii = 0, num = bx::uint32_min(10, (uint32_t)m_quads.size() ); ii < num; ++ii)
{ {
face = quads.front(); face = m_quads.front();
const Pack2D& rect = face.m_rect; const Pack2D& rect = face.m_rect;
updateTextureCubeRectBgra8(textureCube[0], face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, 0, 0, 0); updateTextureCubeRectBgra8(m_textureCube[0], face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, 0, 0, 0);
if (blitSupported) if (m_blitSupported)
{ {
bgfx::blit(0, textureCube[1], 0, rect.m_x, rect.m_y, face.m_side, textureCube[0], 0, rect.m_x, rect.m_y, face.m_side, rect.m_width, rect.m_height); 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);
} }
cube.clear(face); m_cube.clear(face);
quads.pop_front(); m_quads.pop_front();
} }
} }
{ {
// Fill rect. // Fill rect.
const uint32_t pitch = texture2dSize*4; const uint32_t pitch = m_texture2dSize*4;
const uint16_t tw = rand()%texture2dSize; const uint16_t tw = rand()%m_texture2dSize;
const uint16_t th = rand()%texture2dSize; const uint16_t th = rand()%m_texture2dSize;
const uint16_t tx = rand()%(texture2dSize-tw); const uint16_t tx = rand()%(m_texture2dSize-tw);
const uint16_t ty = rand()%(texture2dSize-th); const uint16_t ty = rand()%(m_texture2dSize-th);
uint8_t* dst = &texture2dData[(ty*texture2dSize+tx)*4]; uint8_t* dst = &m_m_texture2dData[(ty*m_texture2dSize+tx)*4];
uint8_t* next = dst + pitch; uint8_t* next = dst + pitch;
// Using makeRef to pass texture memory without copying. // Using makeRef to pass texture memory without copying.
@ -347,20 +390,20 @@ int _main_(int _argc, char** _argv)
{ {
for (uint32_t xx = 0; xx < tw; ++xx, dst += 4) for (uint32_t xx = 0; xx < tw; ++xx, dst += 4)
{ {
dst[0] = bb; dst[0] = m_bb;
dst[1] = gg; dst[1] = m_gg;
dst[2] = rr; dst[2] = m_rr;
dst[3] = 255; dst[3] = 255;
} }
} }
// Pitch here makes possible to pass data from source to destination // Pitch here makes possible to pass data from source to destination
// without need for textures and allocated memory to be the same size. // without need for m_textures and allocated memory to be the same size.
bgfx::updateTexture2D(texture2d, 0, tx, ty, tw, th, mem, pitch); bgfx::updateTexture2D(m_texture2d, 0, tx, ty, tw, th, mem, pitch);
} }
} }
bgfx::dbgTextPrintf(0, 4, 0x0f, "hit: %d, miss %d", hit, miss); bgfx::dbgTextPrintf(0, 4, 0x0f, "m_hit: %d, m_miss %d", m_hit, m_miss);
float at[3] = { 0.0f, 0.0f, 0.0f }; float at[3] = { 0.0f, 0.0f, 0.0f };
float eye[3] = { 0.0f, 0.0f, -5.0f }; float eye[3] = { 0.0f, 0.0f, -5.0f };
@ -368,61 +411,61 @@ int _main_(int _argc, char** _argv)
float view[16]; float view[16];
float proj[16]; float proj[16];
bx::mtxLookAt(view, eye, at); bx::mtxLookAt(view, eye, at);
bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f); bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f);
// Set view and projection matrix for view 0. // Set view and projection matrix for view 0.
bgfx::setViewTransform(0, view, proj); bgfx::setViewTransform(0, view, proj);
for (uint32_t ii = 0; ii < 1 + uint32_t(blitSupported); ++ii) for (uint32_t ii = 0; ii < 1 + uint32_t(m_blitSupported); ++ii)
{ {
float mtx[16]; float mtx[16];
bx::mtxSRT(mtx, 1.0f, 1.0f, 1.0f, time, time*0.37f, 0.0f, -1.5f*blitSupported + ii*3.0f, 0.0f, 0.0f); bx::mtxSRT(mtx, 1.0f, 1.0f, 1.0f, time, time*0.37f, 0.0f, -1.5f*m_blitSupported + ii*3.0f, 0.0f, 0.0f);
// Set model matrix for rendering. // Set model matrix for rendering.
bgfx::setTransform(mtx); bgfx::setTransform(mtx);
// Set vertex and index buffer. // Set vertex and index buffer.
bgfx::setVertexBuffer(vbh); bgfx::setVertexBuffer(m_vbh);
bgfx::setIndexBuffer(ibh); bgfx::setIndexBuffer(m_ibh);
// Bind texture. // Bind texture.
bgfx::setTexture(0, s_texCube, textureCube[ii]); bgfx::setTexture(0, s_texCube, m_textureCube[ii]);
// Set render states. // Set render states.
bgfx::setState(BGFX_STATE_DEFAULT); bgfx::setState(BGFX_STATE_DEFAULT);
// Submit primitive for rendering to view 0. // Submit primitive for rendering to view 0.
bgfx::submit(0, program); bgfx::submit(0, m_program);
} }
// Set view and projection matrix for view 1. // Set view and projection matrix for view 1.
const float aspectRatio = float(height)/float(width); const float aspectRatio = float(m_height)/float(m_width);
const float size = 11.0f; const float size = 11.0f;
bx::mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f); bx::mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f);
bgfx::setViewTransform(1, NULL, proj); bgfx::setViewTransform(1, NULL, proj);
float mtx[16]; float mtx[16];
bx::mtxTranslate(mtx, -size+2.0f - BX_COUNTOF(textures)*0.1f*0.5f, 1.9f, 0.0f); bx::mtxTranslate(mtx, -size+2.0f - BX_COUNTOF(m_textures)*0.1f*0.5f, 1.9f, 0.0f);
// Set model matrix for rendering. // Set model matrix for rendering.
bgfx::setTransform(mtx); bgfx::setTransform(mtx);
// Set vertex and index buffer. // Set vertex and index buffer.
bgfx::setVertexBuffer(vbh); bgfx::setVertexBuffer(m_vbh);
bgfx::setIndexBuffer(ibh); bgfx::setIndexBuffer(m_ibh);
// Bind texture. // Bind texture.
bgfx::setTexture(0, s_texColor, texture2d); bgfx::setTexture(0, s_texColor, m_texture2d);
// Set render states. // Set render states.
bgfx::setState(BGFX_STATE_DEFAULT); bgfx::setState(BGFX_STATE_DEFAULT);
// Submit primitive for rendering to view 1. // Submit primitive for rendering to view 1.
bgfx::submit(1, programCmp); bgfx::submit(1, m_programCmp);
const float xpos = -size+2.0f - BX_COUNTOF(textures)*0.1f*0.5f; const float xpos = -size+2.0f - BX_COUNTOF(m_textures)*0.1f*0.5f;
for (uint32_t ii = 0; ii < BX_COUNTOF(textures); ++ii) for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
{ {
bx::mtxTranslate(mtx, xpos + ii*2.1f, size-6.5f, 0.0f); bx::mtxTranslate(mtx, xpos + ii*2.1f, size-6.5f, 0.0f);
@ -430,20 +473,20 @@ int _main_(int _argc, char** _argv)
bgfx::setTransform(mtx); bgfx::setTransform(mtx);
// Set vertex and index buffer. // Set vertex and index buffer.
bgfx::setVertexBuffer(vbh); bgfx::setVertexBuffer(m_vbh);
bgfx::setIndexBuffer(ibh, 0, 6); bgfx::setIndexBuffer(m_ibh, 0, 6);
// Bind texture. // Bind texture.
bgfx::setTexture(0, s_texColor, textures[ii]); bgfx::setTexture(0, s_texColor, m_textures[ii]);
// Set render states. // Set render states.
bgfx::setState(BGFX_STATE_DEFAULT); bgfx::setState(BGFX_STATE_DEFAULT);
// Submit primitive for rendering to view 1. // Submit primitive for rendering to view 1.
bgfx::submit(1, programCmp); bgfx::submit(1, m_programCmp);
} }
for (uint32_t ii = 0; ii < numTextures3d; ++ii) for (uint32_t ii = 0; ii < m_numm_textures3d; ++ii)
{ {
bx::mtxTranslate(mtx, xpos + ii*2.1f, -size+6.5f, 0.0f); bx::mtxTranslate(mtx, xpos + ii*2.1f, -size+6.5f, 0.0f);
@ -451,17 +494,17 @@ int _main_(int _argc, char** _argv)
bgfx::setTransform(mtx); bgfx::setTransform(mtx);
// Set vertex and index buffer. // Set vertex and index buffer.
bgfx::setVertexBuffer(vbh); bgfx::setVertexBuffer(m_vbh);
bgfx::setIndexBuffer(ibh, 0, 6); bgfx::setIndexBuffer(m_ibh, 0, 6);
// Bind texture. // Bind texture.
bgfx::setTexture(0, s_texColor, textures3d[ii]); bgfx::setTexture(0, s_texColor, m_textures3d[ii]);
// Set render states. // Set render states.
bgfx::setState(BGFX_STATE_DEFAULT); bgfx::setState(BGFX_STATE_DEFAULT);
// Submit primitive for rendering to view 1. // Submit primitive for rendering to view 1.
bgfx::submit(1, program3d); bgfx::submit(1, m_program3d);
} }
for (uint32_t ii = 0; ii < 4; ++ii) for (uint32_t ii = 0; ii < 4; ++ii)
@ -472,62 +515,64 @@ int _main_(int _argc, char** _argv)
bgfx::setTransform(mtx); bgfx::setTransform(mtx);
// Set vertex and index buffer. // Set vertex and index buffer.
bgfx::setVertexBuffer(vbh, 24, 4); bgfx::setVertexBuffer(m_vbh, 24, 4);
bgfx::setIndexBuffer(ibh, 0, 6); bgfx::setIndexBuffer(m_ibh, 0, 6);
// Bind texture. // Bind texture.
bgfx::setTexture(0, s_texColor, textures[ii]); bgfx::setTexture(0, s_texColor, m_textures[ii]);
// Set render states. // Set render states.
bgfx::setState(BGFX_STATE_DEFAULT); bgfx::setState(BGFX_STATE_DEFAULT);
// Submit primitive for rendering to view 1. // Submit primitive for rendering to view 1.
bgfx::submit(1, programCmp); bgfx::submit(1, m_programCmp);
} }
// Advance to next frame. Rendering thread will be kicked to // Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives. // process submitted rendering primitives.
bgfx::frame(); bgfx::frame();
return true;
} }
// texture2dData is managed from main thread, and it's passed to renderer return false;
// 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();
// Cleanup.
free(texture2dData);
for (uint32_t ii = 0; ii < BX_COUNTOF(textures); ++ii)
{
bgfx::destroyTexture(textures[ii]);
} }
for (uint32_t ii = 0; ii < numTextures3d; ++ii) uint8_t* m_m_texture2dData;
{
bgfx::destroyTexture(textures3d[ii]);
}
bgfx::destroyTexture(texture2d); uint32_t m_width;
bgfx::destroyTexture(textureCube[0]); uint32_t m_height;
if (blitSupported) uint32_t m_debug;
{ uint32_t m_reset;
bgfx::destroyTexture(textureCube[1]);
}
bgfx::destroyIndexBuffer(ibh);
bgfx::destroyVertexBuffer(vbh);
if (bgfx::isValid(program3d) )
{
bgfx::destroyProgram(program3d);
}
bgfx::destroyProgram(programCmp);
bgfx::destroyProgram(program);
bgfx::destroyUniform(u_time);
bgfx::destroyUniform(s_texColor);
bgfx::destroyUniform(s_texCube);
// Shutdown bgfx. uint32_t m_numm_textures3d;
bgfx::shutdown(); bool m_texture3DSupported;
bool m_blitSupported;
return 0; std::list<PackCube> m_quads;
} RectPackCubeT<256> m_cube;
int64_t m_updateTime;
int64_t m_timeOffset;
uint32_t m_hit;
uint32_t m_miss;
uint8_t m_rr;
uint8_t m_gg;
uint8_t m_bb;
bgfx::TextureHandle m_textures[9];
bgfx::TextureHandle m_textures3d[3];
bgfx::TextureHandle m_texture2d;
bgfx::TextureHandle m_textureCube[2];
bgfx::IndexBufferHandle m_ibh;
bgfx::VertexBufferHandle m_vbh;
bgfx::ProgramHandle m_program3d;
bgfx::ProgramHandle m_programCmp;
bgfx::ProgramHandle m_program;
bgfx::UniformHandle u_time;
bgfx::UniformHandle s_texColor;
bgfx::UniformHandle s_texCube;
};
ENTRY_IMPLEMENT_MAIN(Update);