2013-01-06 17:53:45 -08:00
/*
2016-01-01 00:11:04 -08:00
* Copyright 2011 - 2016 Branimir Karadzic . All rights reserved .
* License : https : //github.com/bkaradzic/bgfx#license-bsd-2-clause
2013-01-06 17:53:45 -08:00
*/
2013-05-23 22:07:54 -07:00
# include "common.h"
2014-05-03 15:18:28 -07:00
# include "bgfx_utils.h"
2013-05-18 22:12:40 -07:00
2013-01-07 22:01:07 -08:00
# include <bx/uint32_t.h>
2013-05-23 22:07:54 -07:00
# include "packrect.h"
2013-01-06 17:53:45 -08:00
2013-03-02 21:35:09 -08:00
# include <list>
2013-01-06 17:53:45 -08:00
2014-05-03 15:18:28 -07:00
struct PosTexcoordVertex
2013-01-06 17:53:45 -08:00
{
float m_x ;
float m_y ;
float m_z ;
float m_u ;
float m_v ;
float m_w ;
2014-05-03 15:18:28 -07:00
static void init ( )
{
2014-05-10 20:51:44 -07:00
ms_decl
. begin ( )
. add ( bgfx : : Attrib : : Position , 3 , bgfx : : AttribType : : Float )
. add ( bgfx : : Attrib : : TexCoord0 , 3 , bgfx : : AttribType : : Float )
. end ( ) ;
2014-05-03 15:18:28 -07:00
} ;
static bgfx : : VertexDecl ms_decl ;
2013-01-06 17:53:45 -08:00
} ;
2014-05-03 15:18:28 -07:00
bgfx : : VertexDecl PosTexcoordVertex : : ms_decl ;
2013-01-06 17:53:45 -08:00
2015-10-24 19:25:03 -07:00
static PosTexcoordVertex s_m_cubeVertices [ 28 ] =
2013-01-06 17:53:45 -08: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 } ,
2014-04-09 21:23:27 -07:00
{ - 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-06 17:53:45 -08:00
} ;
2015-10-24 19:25:03 -07:00
static const uint16_t s_m_cubeIndices [ 36 ] =
2013-01-06 17:53:45 -08:00
{
2013-02-21 22:05:33 -08:00
0 , 1 , 2 , // 0
1 , 3 , 2 ,
2014-04-09 21:23:27 -07:00
2013-02-21 22:05:33 -08:00
4 , 6 , 5 , // 2
5 , 6 , 7 ,
2014-04-09 21:23:27 -07:00
2013-02-21 22:05:33 -08:00
8 , 10 , 9 , // 4
9 , 10 , 11 ,
2014-04-09 21:23:27 -07:00
2013-02-21 22:05:33 -08:00
12 , 14 , 13 , // 6
2015-02-09 11:24:59 -08:00
14 , 15 , 13 ,
2014-04-09 21:23:27 -07:00
2013-02-21 22:05:33 -08:00
16 , 18 , 17 , // 8
18 , 19 , 17 ,
2014-04-09 21:23:27 -07:00
2013-02-21 22:05:33 -08:00
20 , 22 , 21 , // 10
21 , 22 , 23 ,
2013-01-06 17:53:45 -08:00
} ;
2013-09-08 23:03:14 -07:00
static void updateTextureCubeRectBgra8 ( bgfx : : TextureHandle _handle , uint8_t _side , uint32_t _x , uint32_t _y , uint32_t _width , uint32_t _height , uint8_t _r , uint8_t _g , uint8_t _b , uint8_t _a = 0xff )
{
bgfx : : TextureInfo ti ;
2015-02-09 11:24:59 -08:00
bgfx : : calcTextureSize ( ti , _width , _height , 1 , 1 , false , bgfx : : TextureFormat : : BGRA8 ) ;
2013-09-08 23:03:14 -07:00
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 ;
}
bgfx : : updateTextureCube ( _handle , _side , 0 , _x , _y , _width , _height , mem ) ;
}
2015-10-24 19:25:03 -07:00
static const uint32_t m_textureside = 512 ;
static const uint32_t m_texture2dSize = 256 ;
2013-02-21 21:07:31 -08:00
2015-10-24 19:25:03 -07:00
class Update : public entry : : AppI
{
public :
Update ( )
: m_cube ( m_textureside )
{
}
2013-02-21 21:07:31 -08:00
2015-10-24 19:25:03 -07:00
void init ( int _argc , char * * _argv ) BX_OVERRIDE
{
Args args ( _argc , _argv ) ;
2013-01-06 17:53:45 -08:00
2015-10-24 19:25:03 -07:00
m_width = 1280 ;
m_height = 720 ;
m_debug = BGFX_DEBUG_TEXT ;
m_reset = BGFX_RESET_VSYNC ;
2013-01-06 17:53:45 -08:00
2015-10-24 19:25:03 -07:00
bgfx : : init ( args . m_type , args . m_pciId ) ;
bgfx : : reset ( m_width , m_height , m_reset ) ;
2013-09-09 23:17:17 -07:00
2015-10-24 19:25:03 -07:00
// Enable debug text.
bgfx : : setDebug ( m_debug ) ;
2015-03-28 11:55:49 -07:00
2015-10-24 19:25:03 -07:00
// Set view 0 clear state.
bgfx : : setViewClear ( 0
, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
, 0x303030ff
, 1.0f
, 0
) ;
2015-03-28 11:55:49 -07:00
2015-10-24 19:25:03 -07:00
// Create vertex stream declaration.
PosTexcoordVertex : : init ( ) ;
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 ) ;
m_textures [ 2 ] = loadTexture ( " texture_compression_bc3.dds " , BGFX_TEXTURE_V_CLAMP ) ;
m_textures [ 3 ] = loadTexture ( " texture_compression_etc1.ktx " , BGFX_TEXTURE_U_BORDER | BGFX_TEXTURE_V_BORDER | BGFX_TEXTURE_BORDER_COLOR ( 1 ) ) ;
m_textures [ 4 ] = loadTexture ( " texture_compression_etc2.ktx " ) ;
m_textures [ 5 ] = loadTexture ( " texture_compression_ptc12.pvr " ) ;
m_textures [ 6 ] = loadTexture ( " texture_compression_ptc14.pvr " ) ;
m_textures [ 7 ] = loadTexture ( " texture_compression_ptc22.pvr " ) ;
m_textures [ 8 ] = loadTexture ( " texture_compression_ptc24.pvr " ) ;
const bgfx : : Caps * caps = bgfx : : getCaps ( ) ;
m_texture3DSupported = ! ! ( caps - > supported & BGFX_CAPS_TEXTURE_3D ) ;
m_blitSupported = ! ! ( caps - > supported & BGFX_CAPS_TEXTURE_BLIT ) ;
m_numm_textures3d = 0 ;
if ( m_texture3DSupported )
2015-10-23 20:57:04 -07:00
{
2015-10-24 19:25:03 -07: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-23 20:57:04 -07:00
{
2015-10-24 19:25:03 -07:00
for ( uint8_t yy = 0 ; yy < 32 ; + + yy )
2015-10-23 20:57:04 -07:00
{
2015-10-24 19:25:03 -07: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 ;
mem8 - > data [ offset ] = val < < 3 ;
* ( uint16_t * ) & mem16f - > data [ offset * 2 ] = bx : : halfFromFloat ( ( float ) val / 32.0f ) ;
* ( float * ) & mem32f - > data [ offset * 4 ] = ( float ) val / 32.0f ;
}
2015-10-23 20:57:04 -07:00
}
}
2015-10-24 19:25:03 -07:00
if ( 0 ! = ( BGFX_CAPS_FORMAT_TEXTURE_2D & caps - > formats [ bgfx : : TextureFormat : : R8 ] ) )
{
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 ) ;
}
2015-03-28 21:42:28 -07:00
2015-10-24 19:25:03 -07:00
if ( 0 ! = ( BGFX_CAPS_FORMAT_TEXTURE_2D & caps - > formats [ bgfx : : TextureFormat : : R16F ] ) )
{
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 ] ) )
{
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 ) ;
}
2015-03-28 21:42:28 -07:00
}
2015-10-24 19:25:03 -07:00
// Create static vertex buffer.
m_vbh = bgfx : : createVertexBuffer ( bgfx : : makeRef ( s_m_cubeVertices , sizeof ( s_m_cubeVertices ) ) , PosTexcoordVertex : : ms_decl ) ;
// Create static index buffer.
m_ibh = bgfx : : createIndexBuffer ( bgfx : : makeRef ( s_m_cubeIndices , sizeof ( s_m_cubeIndices ) ) ) ;
// Create programs.
m_program = loadProgram ( " vs_update " , " fs_update " ) ;
m_programCmp = loadProgram ( " vs_update " , " fs_update_cmp " ) ;
2015-10-25 21:31:34 -07:00
m_program3d . idx = bgfx : : invalidHandle ;
2015-10-24 19:25:03 -07:00
if ( m_texture3DSupported )
2015-03-28 21:42:28 -07:00
{
2015-10-24 19:25:03 -07:00
m_program3d = loadProgram ( " vs_update " , " fs_update_3d " ) ;
2015-03-28 21:42:28 -07:00
}
2015-03-16 16:48:46 -07:00
2015-10-24 19:25:03 -07:00
// Create texture sampler uniforms.
s_texCube = bgfx : : createUniform ( " s_texCube " , bgfx : : UniformType : : Int1 ) ;
s_texColor = bgfx : : createUniform ( " s_texColor " , bgfx : : UniformType : : Int1 ) ;
2013-01-06 17:53:45 -08:00
2015-10-24 19:25:03 -07:00
// Create time uniform.
u_time = bgfx : : createUniform ( " u_time " , bgfx : : UniformType : : Vec4 ) ;
2013-01-06 17:53:45 -08:00
2015-10-24 19:25:03 -07:00
m_textureCube [ 0 ] = bgfx : : createTextureCube ( m_textureside , 1
, bgfx : : TextureFormat : : BGRA8
, BGFX_TEXTURE_MIN_POINT | BGFX_TEXTURE_MAG_POINT | BGFX_TEXTURE_MIP_POINT
) ;
2015-09-25 21:32:12 -07:00
2015-10-24 19:25:03 -07:00
if ( m_blitSupported )
{
m_textureCube [ 1 ] = bgfx : : createTextureCube ( m_textureside , 1
, bgfx : : TextureFormat : : BGRA8
, BGFX_TEXTURE_MIN_POINT | BGFX_TEXTURE_MAG_POINT | BGFX_TEXTURE_MIP_POINT | BGFX_TEXTURE_BLIT_DST
) ;
}
2013-01-06 17:53:45 -08:00
2015-10-24 19:25:03 -07:00
m_texture2d = bgfx : : createTexture2D ( m_texture2dSize , m_texture2dSize , 1
, bgfx : : TextureFormat : : BGRA8
, BGFX_TEXTURE_MIN_POINT | BGFX_TEXTURE_MAG_POINT | BGFX_TEXTURE_MIP_POINT
) ;
2015-03-16 16:48:46 -07:00
2016-02-01 20:20:17 -08:00
m_texture2dData = ( uint8_t * ) malloc ( m_texture2dSize * m_texture2dSize * 4 ) ;
2013-01-06 17:53:45 -08:00
2015-10-24 19:25:03 -07:00
m_rr = rand ( ) % 255 ;
m_gg = rand ( ) % 255 ;
m_bb = rand ( ) % 255 ;
2015-10-17 17:39:44 -07:00
2015-10-24 19:25:03 -07:00
m_hit = 0 ;
m_miss = 0 ;
2013-11-08 22:10:31 -08:00
2015-10-24 19:25:03 -07:00
m_updateTime = 0 ;
m_timeOffset = bx : : getHPCounter ( ) ;
2015-10-17 17:39:44 -07:00
}
2015-10-24 19:25:03 -07:00
virtual int shutdown ( ) BX_OVERRIDE
{
2016-02-01 20:20:17 -08:00
// m_texture2dData is managed from main thread, and it's passed to renderer
2015-10-24 19:25:03 -07: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-08 22:10:31 -08:00
2015-10-24 19:25:03 -07:00
// Cleanup.
2016-02-01 20:20:17 -08:00
free ( m_texture2dData ) ;
2013-01-06 17:53:45 -08:00
2015-10-24 19:25:03 -07:00
for ( uint32_t ii = 0 ; ii < BX_COUNTOF ( m_textures ) ; + + ii )
{
bgfx : : destroyTexture ( m_textures [ ii ] ) ;
}
2013-01-06 17:53:45 -08:00
2015-10-24 19:25:03 -07:00
for ( uint32_t ii = 0 ; ii < m_numm_textures3d ; + + ii )
{
bgfx : : destroyTexture ( m_textures3d [ ii ] ) ;
}
2013-01-06 17:53:45 -08:00
2015-10-24 19:25:03 -07:00
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 ) ;
2013-03-02 21:35:09 -08:00
2015-10-24 19:25:03 -07:00
// Shutdown bgfx.
bgfx : : shutdown ( ) ;
2013-03-02 21:35:09 -08:00
2015-10-24 19:25:03 -07:00
return 0 ;
}
2013-04-27 12:43:18 -07:00
2015-10-24 19:25:03 -07:00
bool update ( ) BX_OVERRIDE
2013-02-21 21:07:31 -08:00
{
2015-10-24 19:25:03 -07:00
if ( ! entry : : processEvents ( m_width , m_height , m_debug , m_reset ) )
2013-01-06 17:53:45 -08:00
{
2015-10-24 19:25:03 -07:00
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 ) ;
// Set view 0 and 1 viewport.
bgfx : : setViewRect ( 0 , 0 , 0 , m_width , m_height ) ;
bgfx : : setViewRect ( 1 , 0 , 0 , m_width , m_height ) ;
// 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 ( ) ;
static int64_t last = now ;
const int64_t frameTime = now - last ;
last = now ;
const int64_t freq = bx : : getHPFrequency ( ) ;
const double toMs = 1000.0 / double ( freq ) ;
float time = ( float ) ( ( now - m_timeOffset ) / double ( bx : : getHPFrequency ( ) ) ) ;
bgfx : : setUniform ( u_time , & time ) ;
// Use debug font to print information about this example.
bgfx : : dbgTextClear ( ) ;
bgfx : : dbgTextPrintf ( 0 , 1 , 0x4f , " bgfx/examples/08-update " ) ;
bgfx : : dbgTextPrintf ( 0 , 2 , 0x6f , " Description: Updating m_textures. " ) ;
bgfx : : dbgTextPrintf ( 0 , 3 , 0x0f , " Frame: % 7.3f[ms] " , double ( frameTime ) * toMs ) ;
if ( now > m_updateTime )
2013-01-06 17:53:45 -08:00
{
2015-10-24 19:25:03 -07:00
PackCube face ;
2013-03-02 21:35:09 -08:00
2015-10-24 19:25:03 -07:00
uint32_t bw = bx : : uint16_max ( 1 , rand ( ) % ( m_textureside / 4 ) ) ;
uint32_t bh = bx : : uint16_max ( 1 , rand ( ) % ( m_textureside / 4 ) ) ;
2013-03-02 21:35:09 -08:00
2015-10-24 19:25:03 -07:00
if ( m_cube . find ( bw , bh , face ) )
2015-10-17 17:39:44 -07:00
{
2015-10-24 19:25:03 -07:00
m_quads . push_back ( face ) ;
2013-01-06 17:53:45 -08:00
2015-10-24 19:25:03 -07:00
+ + m_hit ;
2015-10-23 18:13:04 -07:00
const Pack2D & rect = face . m_rect ;
2015-10-24 19:25:03 -07: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-23 18:13:04 -07:00
{
2015-10-24 19:25:03 -07: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-23 18:13:04 -07:00
}
2015-10-24 19:25:03 -07:00
m_rr = rand ( ) % 255 ;
m_gg = rand ( ) % 255 ;
m_bb = rand ( ) % 255 ;
2013-01-06 17:53:45 -08:00
}
2015-10-24 19:25:03 -07:00
else
{
+ + m_miss ;
2013-11-08 22:10:31 -08:00
2015-10-24 19:25:03 -07: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-08 22:10:31 -08:00
2015-10-24 19:25:03 -07: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-08 22:10:31 -08:00
2015-10-24 19:25:03 -07:00
m_cube . clear ( face ) ;
m_quads . pop_front ( ) ;
2013-11-08 22:10:31 -08:00
}
}
2015-10-24 19:25:03 -07:00
{
// Fill rect.
const uint32_t pitch = m_texture2dSize * 4 ;
2015-02-09 11:24:59 -08:00
2015-10-24 19:25:03 -07:00
const uint16_t tw = rand ( ) % m_texture2dSize ;
const uint16_t th = rand ( ) % m_texture2dSize ;
const uint16_t tx = rand ( ) % ( m_texture2dSize - tw ) ;
const uint16_t ty = rand ( ) % ( m_texture2dSize - th ) ;
2013-01-06 17:53:45 -08:00
2016-02-01 20:20:17 -08:00
uint8_t * dst = & m_texture2dData [ ( ty * m_texture2dSize + tx ) * 4 ] ;
2015-10-24 19:25:03 -07:00
uint8_t * next = dst + pitch ;
2013-01-06 17:53:45 -08:00
2015-10-24 19:25:03 -07:00
// Using makeRef to pass texture memory without copying.
const bgfx : : Memory * mem = bgfx : : makeRef ( dst , tw * th * 4 ) ;
2013-01-06 17:53:45 -08:00
2015-10-24 19:25:03 -07: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-06 17:53:45 -08:00
2015-10-24 19:25:03 -07: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.
bgfx : : updateTexture2D ( m_texture2d , 0 , tx , ty , tw , th , mem , pitch ) ;
}
}
2013-01-06 17:53:45 -08:00
2015-10-24 19:25:03 -07:00
bgfx : : dbgTextPrintf ( 0 , 4 , 0x0f , " m_hit: %d, m_miss %d " , m_hit , m_miss ) ;
2013-01-06 17:53:45 -08:00
2015-10-24 19:25:03 -07:00
float at [ 3 ] = { 0.0f , 0.0f , 0.0f } ;
float eye [ 3 ] = { 0.0f , 0.0f , - 5.0f } ;
2013-01-06 17:53:45 -08:00
2015-10-24 19:25:03 -07: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 ) ;
2013-09-09 23:17:17 -07:00
2015-10-24 19:25:03 -07:00
// Set view and projection matrix for view 0.
bgfx : : setViewTransform ( 0 , view , proj ) ;
2013-09-09 23:17:17 -07:00
2015-10-24 19:25:03 -07:00
for ( uint32_t ii = 0 ; ii < 1 + uint32_t ( m_blitSupported ) ; + + ii )
{
float mtx [ 16 ] ;
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 ) ;
2013-11-08 22:10:31 -08:00
2015-10-24 19:25:03 -07:00
// Set model matrix for rendering.
bgfx : : setTransform ( mtx ) ;
2013-11-08 22:10:31 -08:00
2015-10-24 19:25:03 -07:00
// Set vertex and index buffer.
bgfx : : setVertexBuffer ( m_vbh ) ;
bgfx : : setIndexBuffer ( m_ibh ) ;
2013-11-08 22:10:31 -08:00
2015-10-24 19:25:03 -07:00
// Bind texture.
bgfx : : setTexture ( 0 , s_texCube , m_textureCube [ ii ] ) ;
2013-11-08 22:10:31 -08:00
2015-10-24 19:25:03 -07:00
// Set render states.
bgfx : : setState ( BGFX_STATE_DEFAULT ) ;
2013-11-08 22:10:31 -08:00
2015-10-24 19:25:03 -07:00
// Submit primitive for rendering to view 0.
bgfx : : submit ( 0 , m_program ) ;
}
2013-11-08 22:10:31 -08:00
2015-10-24 19:25:03 -07:00
// Set view and projection matrix for view 1.
const float aspectRatio = float ( m_height ) / float ( m_width ) ;
const float size = 11.0f ;
bx : : mtxOrtho ( proj , - size , size , size * aspectRatio , - size * aspectRatio , 0.0f , 1000.0f ) ;
bgfx : : setViewTransform ( 1 , NULL , proj ) ;
2013-11-08 22:10:31 -08:00
2015-10-24 19:25:03 -07:00
float mtx [ 16 ] ;
bx : : mtxTranslate ( mtx , - size + 2.0f - BX_COUNTOF ( m_textures ) * 0.1f * 0.5f , 1.9f , 0.0f ) ;
2013-09-09 23:17:17 -07:00
// Set model matrix for rendering.
bgfx : : setTransform ( mtx ) ;
// Set vertex and index buffer.
2015-10-24 19:25:03 -07:00
bgfx : : setVertexBuffer ( m_vbh ) ;
bgfx : : setIndexBuffer ( m_ibh ) ;
2014-04-09 21:23:27 -07:00
// Bind texture.
2015-10-24 19:25:03 -07:00
bgfx : : setTexture ( 0 , s_texColor , m_texture2d ) ;
2014-04-09 21:23:27 -07:00
// Set render states.
bgfx : : setState ( BGFX_STATE_DEFAULT ) ;
2014-10-05 00:18:07 -07:00
// Submit primitive for rendering to view 1.
2015-10-24 19:25:03 -07:00
bgfx : : submit ( 1 , m_programCmp ) ;
2014-04-09 21:23:27 -07:00
2015-10-24 19:25:03 -07:00
const float xpos = - size + 2.0f - BX_COUNTOF ( m_textures ) * 0.1f * 0.5f ;
2015-03-16 16:48:46 -07:00
2015-10-24 19:25:03 -07:00
for ( uint32_t ii = 0 ; ii < BX_COUNTOF ( m_textures ) ; + + ii )
{
bx : : mtxTranslate ( mtx , xpos + ii * 2.1f , size - 6.5f , 0.0f ) ;
2015-03-16 16:48:46 -07:00
2015-10-24 19:25:03 -07:00
// Set model matrix for rendering.
bgfx : : setTransform ( mtx ) ;
2015-03-16 16:48:46 -07:00
2015-10-24 19:25:03 -07:00
// Set vertex and index buffer.
bgfx : : setVertexBuffer ( m_vbh ) ;
bgfx : : setIndexBuffer ( m_ibh , 0 , 6 ) ;
2015-03-16 16:48:46 -07:00
2015-10-24 19:25:03 -07:00
// Bind texture.
bgfx : : setTexture ( 0 , s_texColor , m_textures [ ii ] ) ;
2015-03-16 16:48:46 -07:00
2015-10-24 19:25:03 -07:00
// Set render states.
bgfx : : setState ( BGFX_STATE_DEFAULT ) ;
2015-03-16 16:48:46 -07:00
2015-10-24 19:25:03 -07:00
// Submit primitive for rendering to view 1.
bgfx : : submit ( 1 , m_programCmp ) ;
}
2014-04-09 21:23:27 -07:00
2015-10-24 19:25:03 -07:00
for ( uint32_t ii = 0 ; ii < m_numm_textures3d ; + + ii )
{
bx : : mtxTranslate ( mtx , xpos + ii * 2.1f , - size + 6.5f , 0.0f ) ;
2014-04-09 21:23:27 -07:00
2015-10-24 19:25:03 -07:00
// Set model matrix for rendering.
bgfx : : setTransform ( mtx ) ;
2013-09-09 23:17:17 -07:00
2015-10-24 19:25:03 -07:00
// Set vertex and index buffer.
bgfx : : setVertexBuffer ( m_vbh ) ;
bgfx : : setIndexBuffer ( m_ibh , 0 , 6 ) ;
2013-09-09 23:17:17 -07:00
2015-10-24 19:25:03 -07:00
// Bind texture.
bgfx : : setTexture ( 0 , s_texColor , m_textures3d [ ii ] ) ;
2013-09-09 23:17:17 -07:00
2015-10-24 19:25:03 -07:00
// Set render states.
bgfx : : setState ( BGFX_STATE_DEFAULT ) ;
2013-09-09 23:17:17 -07:00
2015-10-24 19:25:03 -07:00
// Submit primitive for rendering to view 1.
bgfx : : submit ( 1 , m_program3d ) ;
}
for ( uint32_t ii = 0 ; ii < 4 ; + + ii )
{
bx : : mtxTranslate ( mtx , xpos + ( size - 2.0f ) * 2.1f , - size + 6.5f + ii * 2.1f , 0.0f ) ;
2013-01-06 17:53:45 -08:00
2015-10-24 19:25:03 -07:00
// Set model matrix for rendering.
bgfx : : setTransform ( mtx ) ;
2014-02-20 22:04:50 -08:00
2015-10-24 19:25:03 -07:00
// Set vertex and index buffer.
bgfx : : setVertexBuffer ( m_vbh , 24 , 4 ) ;
bgfx : : setIndexBuffer ( m_ibh , 0 , 6 ) ;
2013-11-08 22:10:31 -08:00
2015-10-24 19:25:03 -07:00
// Bind texture.
bgfx : : setTexture ( 0 , s_texColor , m_textures [ ii ] ) ;
2013-11-17 20:57:38 -08:00
2015-10-24 19:25:03 -07:00
// Set render states.
bgfx : : setState ( BGFX_STATE_DEFAULT ) ;
2015-03-16 18:49:41 -07:00
2015-10-24 19:25:03 -07: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 ( ) ;
return true ;
}
return false ;
2015-03-29 18:18:06 -07:00
}
2013-01-06 17:53:45 -08:00
2016-02-01 20:20:17 -08:00
uint8_t * m_texture2dData ;
2015-10-24 19:25:03 -07:00
uint32_t m_width ;
uint32_t m_height ;
uint32_t m_debug ;
uint32_t m_reset ;
uint32_t m_numm_textures3d ;
bool m_texture3DSupported ;
bool m_blitSupported ;
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 ;
2013-01-06 17:53:45 -08:00
2015-10-24 19:25:03 -07:00
} ;
ENTRY_IMPLEMENT_MAIN ( Update ) ;