2012-08-05 17:51:49 -04:00
|
|
|
/*
|
2015-01-01 18:04:46 -05:00
|
|
|
* Copyright 2011-2015 Branimir Karadzic. All rights reserved.
|
2012-08-05 17:51:49 -04:00
|
|
|
* License: http://www.opensource.org/licenses/BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2013-11-14 00:54:36 -05:00
|
|
|
#ifndef BGFX_P_H_HEADER_GUARD
|
|
|
|
#define BGFX_P_H_HEADER_GUARD
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2014-10-22 02:38:16 -04:00
|
|
|
#include <bx/platform.h>
|
|
|
|
|
2014-08-23 17:13:23 -04:00
|
|
|
#ifndef BGFX_CONFIG_DEBUG
|
|
|
|
# define BGFX_CONFIG_DEBUG 0
|
|
|
|
#endif // BGFX_CONFIG_DEBUG
|
|
|
|
|
2014-10-22 02:38:16 -04:00
|
|
|
#if BGFX_CONFIG_DEBUG || BX_COMPILER_CLANG_ANALYZER
|
2014-08-23 17:01:52 -04:00
|
|
|
# define BX_TRACE _BX_TRACE
|
|
|
|
# define BX_WARN _BX_WARN
|
|
|
|
# define BX_CHECK _BX_CHECK
|
|
|
|
# define BX_CONFIG_ALLOCATOR_DEBUG 1
|
|
|
|
#endif // BGFX_CONFIG_DEBUG
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
#include "bgfx.h"
|
2013-04-28 15:27:35 -04:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdarg.h> // va_list
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <alloca.h>
|
|
|
|
|
2015-03-26 18:01:09 -04:00
|
|
|
// Check handle, cannot be bgfx::invalidHandle and must be valid.
|
2015-03-26 02:33:35 -04:00
|
|
|
#define BGFX_CHECK_HANDLE(_desc, _handleAlloc, _handle) \
|
|
|
|
BX_CHECK(isValid(_handle) \
|
|
|
|
&& _handleAlloc.isValid(_handle.idx) \
|
|
|
|
, "Invalid handle. %s handle: %d (max %d)" \
|
|
|
|
, _desc \
|
|
|
|
, _handle.idx \
|
|
|
|
, _handleAlloc.getMaxHandles() \
|
2015-03-26 18:01:09 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Check handle, it's ok to be bgfx::invalidHandle or must be valid.
|
|
|
|
#define BGFX_CHECK_HANDLE_INVALID_OK(_desc, _handleAlloc, _handle) \
|
|
|
|
BX_CHECK(!isValid(_handle) \
|
|
|
|
|| _handleAlloc.isValid(_handle.idx) \
|
|
|
|
, "Invalid handle. %s handle: %d (max %d)" \
|
|
|
|
, _desc \
|
|
|
|
, _handle.idx \
|
|
|
|
, _handleAlloc.getMaxHandles() \
|
|
|
|
)
|
2015-03-26 02:33:35 -04:00
|
|
|
|
2013-04-28 15:27:35 -04:00
|
|
|
namespace bgfx
|
|
|
|
{
|
2014-10-22 02:38:16 -04:00
|
|
|
#if BX_COMPILER_CLANG_ANALYZER
|
2014-10-22 01:58:07 -04:00
|
|
|
void __attribute__((analyzer_noreturn)) fatal(Fatal::Enum _code, const char* _format, ...);
|
|
|
|
#else
|
2013-09-09 00:03:03 -04:00
|
|
|
void fatal(Fatal::Enum _code, const char* _format, ...);
|
2014-10-22 02:38:16 -04:00
|
|
|
#endif // BX_COMPILER_CLANG_ANALYZER
|
2014-10-22 01:58:07 -04:00
|
|
|
|
2013-04-28 15:27:35 -04:00
|
|
|
void dbgPrintf(const char* _format, ...);
|
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2013-08-18 03:00:14 -04:00
|
|
|
#define _BX_TRACE(_format, ...) \
|
2014-06-08 23:57:39 -04:00
|
|
|
BX_MACRO_BLOCK_BEGIN \
|
2013-04-28 15:27:35 -04:00
|
|
|
bgfx::dbgPrintf(BX_FILE_LINE_LITERAL "BGFX " _format "\n", ##__VA_ARGS__); \
|
2014-06-08 23:57:39 -04:00
|
|
|
BX_MACRO_BLOCK_END
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2013-08-18 03:00:14 -04:00
|
|
|
#define _BX_WARN(_condition, _format, ...) \
|
2014-06-08 23:57:39 -04:00
|
|
|
BX_MACRO_BLOCK_BEGIN \
|
|
|
|
if (!BX_IGNORE_C4127(_condition) ) \
|
2012-11-25 21:24:50 -05:00
|
|
|
{ \
|
2013-01-15 23:37:07 -05:00
|
|
|
BX_TRACE("WARN " _format, ##__VA_ARGS__); \
|
2012-11-25 21:24:50 -05:00
|
|
|
} \
|
2014-06-08 23:57:39 -04:00
|
|
|
BX_MACRO_BLOCK_END
|
2012-11-25 21:24:50 -05:00
|
|
|
|
2013-08-18 03:00:14 -04:00
|
|
|
#define _BX_CHECK(_condition, _format, ...) \
|
2014-06-08 23:57:39 -04:00
|
|
|
BX_MACRO_BLOCK_BEGIN \
|
|
|
|
if (!BX_IGNORE_C4127(_condition) ) \
|
2012-08-05 17:51:49 -04:00
|
|
|
{ \
|
2013-01-15 23:37:07 -05:00
|
|
|
BX_TRACE("CHECK " _format, ##__VA_ARGS__); \
|
2013-04-28 15:27:35 -04:00
|
|
|
bgfx::fatal(bgfx::Fatal::DebugCheck, _format, ##__VA_ARGS__); \
|
2012-08-05 17:51:49 -04:00
|
|
|
} \
|
2014-06-08 23:57:39 -04:00
|
|
|
BX_MACRO_BLOCK_END
|
2013-08-17 01:32:03 -04:00
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
#define BGFX_FATAL(_condition, _err, _format, ...) \
|
2014-06-08 23:57:39 -04:00
|
|
|
BX_MACRO_BLOCK_BEGIN \
|
|
|
|
if (!BX_IGNORE_C4127(_condition) ) \
|
2012-08-05 17:51:49 -04:00
|
|
|
{ \
|
|
|
|
fatal(_err, _format, ##__VA_ARGS__); \
|
|
|
|
} \
|
2014-06-08 23:57:39 -04:00
|
|
|
BX_MACRO_BLOCK_END
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
#include <bx/bx.h>
|
|
|
|
#include <bx/debug.h>
|
2014-10-29 01:08:55 -04:00
|
|
|
#include <bx/fpumath.h>
|
2014-01-19 02:33:00 -05:00
|
|
|
#include <bx/float4x4_t.h>
|
2012-08-05 17:51:49 -04:00
|
|
|
#include <bx/blockalloc.h>
|
2012-07-29 16:50:23 -04:00
|
|
|
#include <bx/endian.h>
|
2012-08-05 17:51:49 -04:00
|
|
|
#include <bx/handlealloc.h>
|
|
|
|
#include <bx/hash.h>
|
|
|
|
#include <bx/radixsort.h>
|
|
|
|
#include <bx/ringbuffer.h>
|
|
|
|
#include <bx/uint32_t.h>
|
2012-11-25 21:24:50 -05:00
|
|
|
#include <bx/readerwriter.h>
|
2013-08-29 01:55:12 -04:00
|
|
|
#include <bx/allocator.h>
|
2013-04-28 15:27:35 -04:00
|
|
|
#include <bx/string.h>
|
2013-07-27 01:55:13 -04:00
|
|
|
#include <bx/os.h>
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2013-11-18 23:43:17 -05:00
|
|
|
#include "bgfxplatform.h"
|
2013-09-02 19:22:53 -04:00
|
|
|
#include "image.h"
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2014-08-17 20:20:15 -04:00
|
|
|
#define BGFX_CHUNK_MAGIC_CSH BX_MAKEFOURCC('C', 'S', 'H', 0x1)
|
|
|
|
#define BGFX_CHUNK_MAGIC_FSH BX_MAKEFOURCC('F', 'S', 'H', 0x3)
|
2012-11-25 21:24:50 -05:00
|
|
|
#define BGFX_CHUNK_MAGIC_TEX BX_MAKEFOURCC('T', 'E', 'X', 0x0)
|
2014-08-17 20:20:15 -04:00
|
|
|
#define BGFX_CHUNK_MAGIC_VSH BX_MAKEFOURCC('V', 'S', 'H', 0x3)
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2015-01-11 21:00:08 -05:00
|
|
|
#define BGFX_CLEAR_COLOR_USE_PALETTE UINT16_C(0x8000)
|
|
|
|
#define BGFX_CLEAR_MASK (0 \
|
|
|
|
| BGFX_CLEAR_COLOR \
|
|
|
|
| BGFX_CLEAR_DEPTH \
|
|
|
|
| BGFX_CLEAR_STENCIL \
|
|
|
|
| BGFX_CLEAR_COLOR_USE_PALETTE \
|
|
|
|
)
|
2014-09-01 14:24:51 -04:00
|
|
|
|
2013-08-31 01:31:40 -04:00
|
|
|
#include <list> // mingw wants it to be before tr1/unordered_*...
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2013-08-31 01:31:40 -04:00
|
|
|
#if BGFX_CONFIG_USE_TINYSTL
|
2013-09-23 00:40:17 -04:00
|
|
|
namespace bgfx
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2013-09-23 00:40:17 -04:00
|
|
|
struct TinyStlAllocator
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
static void* static_allocate(size_t _bytes);
|
|
|
|
static void static_deallocate(void* _ptr, size_t /*_bytes*/);
|
|
|
|
};
|
2013-09-23 00:40:17 -04:00
|
|
|
} // namespace bgfx
|
|
|
|
# define TINYSTL_ALLOCATOR bgfx::TinyStlAllocator
|
|
|
|
# include <tinystl/string.h>
|
|
|
|
# include <tinystl/unordered_map.h>
|
|
|
|
# include <tinystl/unordered_set.h>
|
2012-08-05 17:51:49 -04:00
|
|
|
namespace stl = tinystl;
|
|
|
|
#else
|
|
|
|
# include <string>
|
|
|
|
# include <unordered_map>
|
2013-01-22 22:56:40 -05:00
|
|
|
# include <unordered_set>
|
2013-08-31 01:31:40 -04:00
|
|
|
namespace stl
|
|
|
|
{
|
2013-02-13 01:36:50 -05:00
|
|
|
using namespace std;
|
|
|
|
using namespace std::tr1;
|
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
#endif // BGFX_CONFIG_USE_TINYSTL
|
|
|
|
|
2013-04-19 00:16:09 -04:00
|
|
|
#if BX_PLATFORM_ANDROID
|
|
|
|
# include <android/native_window.h>
|
|
|
|
#elif BX_PLATFORM_WINDOWS
|
2013-02-26 21:17:20 -05:00
|
|
|
# include <windows.h>
|
|
|
|
#elif BX_PLATFORM_XBOX360
|
|
|
|
# include <malloc.h>
|
|
|
|
# include <xtl.h>
|
|
|
|
#endif // BX_PLATFORM_*
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
#include <bx/cpu.h>
|
2012-12-02 21:29:28 -05:00
|
|
|
#include <bx/thread.h>
|
2012-08-05 17:51:49 -04:00
|
|
|
#include <bx/timer.h>
|
|
|
|
|
2012-12-28 20:09:34 -05:00
|
|
|
#include "vertexdecl.h"
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
#define BGFX_DEFAULT_WIDTH 1280
|
|
|
|
#define BGFX_DEFAULT_HEIGHT 720
|
|
|
|
|
2014-07-20 23:27:13 -04:00
|
|
|
#define BGFX_MAX_COMPUTE_BINDINGS 8
|
|
|
|
|
2014-04-10 00:23:27 -04:00
|
|
|
#define BGFX_SAMPLER_DEFAULT_FLAGS UINT32_C(0x10000000)
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2014-10-11 14:24:37 -04:00
|
|
|
#define BGFX_RENDERER_DIRECT3D9_NAME "Direct3D 9"
|
2014-05-26 17:09:26 -04:00
|
|
|
#define BGFX_RENDERER_DIRECT3D11_NAME "Direct3D 11"
|
2014-10-11 14:24:37 -04:00
|
|
|
#define BGFX_RENDERER_DIRECT3D12_NAME "Direct3D 12"
|
2015-03-05 23:34:39 -05:00
|
|
|
#define BGFX_RENDERER_VULKAN_NAME "Vulkan"
|
2014-05-26 17:09:26 -04:00
|
|
|
#define BGFX_RENDERER_NULL_NAME "NULL"
|
|
|
|
|
|
|
|
#if BGFX_CONFIG_RENDERER_OPENGL
|
2014-08-26 23:56:53 -04:00
|
|
|
# if BGFX_CONFIG_RENDERER_OPENGL >= 31 && BGFX_CONFIG_RENDERER_OPENGL <= 33
|
|
|
|
# if BGFX_CONFIG_RENDERER_OPENGL == 31
|
|
|
|
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 3.1"
|
|
|
|
# elif BGFX_CONFIG_RENDERER_OPENGL == 32
|
|
|
|
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 3.2"
|
|
|
|
# else
|
|
|
|
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 3.3"
|
|
|
|
# endif // 31+
|
|
|
|
# elif BGFX_CONFIG_RENDERER_OPENGL >= 40 && BGFX_CONFIG_RENDERER_OPENGL <= 45
|
|
|
|
# if BGFX_CONFIG_RENDERER_OPENGL == 40
|
|
|
|
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 4.0"
|
|
|
|
# elif BGFX_CONFIG_RENDERER_OPENGL == 41
|
|
|
|
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 4.1"
|
|
|
|
# elif BGFX_CONFIG_RENDERER_OPENGL == 42
|
|
|
|
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 4.2"
|
|
|
|
# elif BGFX_CONFIG_RENDERER_OPENGL == 43
|
|
|
|
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 4.3"
|
|
|
|
# elif BGFX_CONFIG_RENDERER_OPENGL == 44
|
|
|
|
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 4.4"
|
|
|
|
# else
|
|
|
|
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 4.5"
|
|
|
|
# endif // 40+
|
2013-03-04 01:30:50 -05:00
|
|
|
# else
|
2014-05-26 17:09:26 -04:00
|
|
|
# define BGFX_RENDERER_OPENGL_NAME "OpenGL 2.1"
|
2013-03-04 01:30:50 -05:00
|
|
|
# endif // BGFX_CONFIG_RENDERER_OPENGL
|
2014-03-30 02:12:15 -04:00
|
|
|
#elif BGFX_CONFIG_RENDERER_OPENGLES
|
2014-03-31 23:08:32 -04:00
|
|
|
# if BGFX_CONFIG_RENDERER_OPENGLES == 30
|
2014-05-26 17:09:26 -04:00
|
|
|
# define BGFX_RENDERER_OPENGL_NAME "OpenGL ES 3.0"
|
2014-05-10 23:51:44 -04:00
|
|
|
# elif BGFX_CONFIG_RENDERER_OPENGLES >= 31
|
2014-05-26 17:09:26 -04:00
|
|
|
# define BGFX_RENDERER_OPENGL_NAME "OpenGL ES 3.1"
|
2014-03-31 23:08:32 -04:00
|
|
|
# else
|
2014-05-26 17:09:26 -04:00
|
|
|
# define BGFX_RENDERER_OPENGL_NAME "OpenGL ES 2.0"
|
2014-03-31 23:08:32 -04:00
|
|
|
# endif // BGFX_CONFIG_RENDERER_OPENGLES
|
2014-05-26 17:34:58 -04:00
|
|
|
#else
|
|
|
|
# define BGFX_RENDERER_OPENGL_NAME "OpenGL"
|
2014-05-26 17:09:26 -04:00
|
|
|
#endif //
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
namespace bgfx
|
|
|
|
{
|
2013-04-19 00:16:09 -04:00
|
|
|
#if BX_PLATFORM_ANDROID
|
|
|
|
extern ::ANativeWindow* g_bgfxAndroidWindow;
|
2013-07-21 17:44:53 -04:00
|
|
|
#elif BX_PLATFORM_IOS
|
|
|
|
extern void* g_bgfxEaglLayer;
|
2015-04-10 10:11:55 -04:00
|
|
|
#elif BX_PLATFORM_LINUX || BX_PLATFORM_FREEBSD
|
2015-02-06 19:49:04 -05:00
|
|
|
extern void* g_bgfxX11Display;
|
|
|
|
extern uint32_t g_bgfxX11Window;
|
2015-03-25 02:21:32 -04:00
|
|
|
extern void* g_bgfxGLX;
|
2013-01-15 23:37:07 -05:00
|
|
|
#elif BX_PLATFORM_OSX
|
|
|
|
extern void* g_bgfxNSWindow;
|
2015-03-25 01:19:21 -04:00
|
|
|
extern void* g_bgfxNSGL;
|
2013-04-19 00:16:09 -04:00
|
|
|
#elif BX_PLATFORM_WINDOWS
|
|
|
|
extern ::HWND g_bgfxHwnd;
|
2014-11-14 08:19:33 -05:00
|
|
|
#elif BX_PLATFORM_WINRT
|
2014-12-04 23:32:19 -05:00
|
|
|
extern ::IUnknown* g_bgfxCoreWindow;
|
2013-01-15 23:37:07 -05:00
|
|
|
#endif // BX_PLATFORM_*
|
2013-01-13 21:39:25 -05:00
|
|
|
|
2015-04-09 22:38:51 -04:00
|
|
|
#if BGFX_CONFIG_MAX_DRAW_CALLS < (64<<10)
|
|
|
|
typedef uint16_t RenderItemCount;
|
|
|
|
#else
|
|
|
|
typedef uint32_t RenderItemCount;
|
|
|
|
#endif // BGFX_CONFIG_MAX_DRAW_CALLS < (64<<10)
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
struct Clear
|
|
|
|
{
|
2015-01-11 21:00:08 -05:00
|
|
|
uint8_t m_index[8];
|
|
|
|
float m_depth;
|
|
|
|
uint8_t m_stencil;
|
|
|
|
uint16_t m_flags;
|
2012-08-05 17:51:49 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Rect
|
|
|
|
{
|
2013-08-04 19:56:07 -04:00
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
m_x =
|
|
|
|
m_y =
|
|
|
|
m_width =
|
|
|
|
m_height = 0;
|
|
|
|
}
|
|
|
|
|
2013-07-14 17:32:09 -04:00
|
|
|
bool isZero() const
|
|
|
|
{
|
|
|
|
uint64_t ui64 = *( (uint64_t*)this);
|
|
|
|
return UINT64_C(0) == ui64;
|
|
|
|
}
|
|
|
|
|
2013-08-02 01:55:26 -04:00
|
|
|
void intersect(const Rect& _a, const Rect& _b)
|
|
|
|
{
|
2014-03-31 23:08:32 -04:00
|
|
|
using namespace bx;
|
|
|
|
const uint16_t sx = uint16_max(_a.m_x, _b.m_x);
|
|
|
|
const uint16_t sy = uint16_max(_a.m_y, _b.m_y);
|
|
|
|
const uint16_t ex = uint16_min(_a.m_x + _a.m_width, _b.m_x + _b.m_width );
|
|
|
|
const uint16_t ey = uint16_min(_a.m_y + _a.m_height, _b.m_y + _b.m_height);
|
2013-08-02 01:55:26 -04:00
|
|
|
m_x = sx;
|
|
|
|
m_y = sy;
|
2014-03-31 23:08:32 -04:00
|
|
|
m_width = (uint16_t)uint32_satsub(ex, sx);
|
|
|
|
m_height = (uint16_t)uint32_satsub(ey, sy);
|
2013-08-02 01:55:26 -04:00
|
|
|
}
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
uint16_t m_x;
|
|
|
|
uint16_t m_y;
|
|
|
|
uint16_t m_width;
|
|
|
|
uint16_t m_height;
|
|
|
|
};
|
|
|
|
|
2012-11-04 00:36:17 -04:00
|
|
|
struct TextureCreate
|
2012-08-13 00:02:11 -04:00
|
|
|
{
|
|
|
|
uint32_t m_flags;
|
|
|
|
uint16_t m_width;
|
2013-01-06 20:53:45 -05:00
|
|
|
uint16_t m_height;
|
|
|
|
uint16_t m_sides;
|
2012-08-13 00:02:11 -04:00
|
|
|
uint16_t m_depth;
|
|
|
|
uint8_t m_numMips;
|
2012-11-25 21:24:50 -05:00
|
|
|
uint8_t m_format;
|
2012-08-13 00:02:11 -04:00
|
|
|
bool m_cubeMap;
|
|
|
|
const Memory* m_mem;
|
|
|
|
};
|
|
|
|
|
2013-06-11 01:41:03 -04:00
|
|
|
extern const uint32_t g_uniformTypeSize[UniformType::Count+1];
|
2012-12-30 23:52:47 -05:00
|
|
|
extern CallbackI* g_callback;
|
2013-09-17 00:40:30 -04:00
|
|
|
extern bx::ReallocatorI* g_allocator;
|
2013-10-10 21:29:57 -04:00
|
|
|
extern Caps g_caps;
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2013-10-19 01:34:23 -04:00
|
|
|
void setGraphicsDebuggerPresent(bool _present);
|
|
|
|
bool isGraphicsDebuggerPresent();
|
2012-08-05 17:51:49 -04:00
|
|
|
void release(const Memory* _mem);
|
|
|
|
const char* getAttribName(Attrib::Enum _attr);
|
2015-04-13 21:39:38 -04:00
|
|
|
void getTextureSizeFromRatio(BackbufferRatio::Enum _ratio, uint16_t& _width, uint16_t& _height);
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2013-09-02 19:22:53 -04:00
|
|
|
inline uint32_t castfu(float _value)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
union { float fl; uint32_t ui; } un;
|
|
|
|
un.fl = _value;
|
|
|
|
return un.ui;
|
|
|
|
}
|
|
|
|
|
2012-11-10 22:59:23 -05:00
|
|
|
inline uint64_t packStencil(uint32_t _fstencil, uint32_t _bstencil)
|
|
|
|
{
|
|
|
|
return (uint64_t(_bstencil)<<32)|uint64_t(_fstencil);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32_t unpackStencil(uint8_t _0or1, uint64_t _stencil)
|
|
|
|
{
|
|
|
|
return uint32_t( (_stencil >> (32*_0or1) ) );
|
|
|
|
}
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
void dump(const VertexDecl& _decl);
|
|
|
|
|
|
|
|
struct TextVideoMem
|
|
|
|
{
|
|
|
|
TextVideoMem()
|
|
|
|
: m_mem(NULL)
|
|
|
|
, m_size(0)
|
|
|
|
, m_width(0)
|
|
|
|
, m_height(0)
|
|
|
|
, m_small(false)
|
|
|
|
{
|
|
|
|
resize();
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2013-09-21 02:19:21 -04:00
|
|
|
~TextVideoMem()
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2013-09-17 00:40:30 -04:00
|
|
|
BX_FREE(g_allocator, m_mem);
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2015-04-07 20:58:14 -04:00
|
|
|
void resize(bool _small = false, uint32_t _width = BGFX_DEFAULT_WIDTH, uint32_t _height = BGFX_DEFAULT_HEIGHT)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2013-08-04 01:15:13 -04:00
|
|
|
uint32_t width = bx::uint32_max(1, _width/8);
|
|
|
|
uint32_t height = bx::uint32_max(1, _height/(_small ? 8 : 16) );
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
if (NULL == m_mem
|
|
|
|
|| m_width != width
|
|
|
|
|| m_height != height
|
|
|
|
|| m_small != _small)
|
|
|
|
{
|
2015-04-07 20:58:14 -04:00
|
|
|
m_small = _small;
|
|
|
|
m_width = (uint16_t)width;
|
2012-08-05 17:51:49 -04:00
|
|
|
m_height = (uint16_t)height;
|
2012-08-13 00:02:11 -04:00
|
|
|
|
|
|
|
uint32_t size = m_size;
|
2012-08-05 17:51:49 -04:00
|
|
|
m_size = m_width * m_height * 2;
|
|
|
|
|
2013-09-17 00:40:30 -04:00
|
|
|
m_mem = (uint8_t*)BX_REALLOC(g_allocator, m_mem, m_size);
|
2012-08-13 00:02:11 -04:00
|
|
|
|
|
|
|
if (size < m_size)
|
|
|
|
{
|
|
|
|
memset(&m_mem[size], 0, m_size-size);
|
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear(uint8_t _attr = 0)
|
|
|
|
{
|
|
|
|
uint8_t* mem = m_mem;
|
|
|
|
for (uint32_t ii = 0, num = m_size/2; ii < num; ++ii)
|
|
|
|
{
|
|
|
|
mem[0] = 0;
|
|
|
|
mem[1] = _attr;
|
|
|
|
mem += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void printfVargs(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, va_list _argList)
|
|
|
|
{
|
|
|
|
if (_x < m_width && _y < m_height)
|
|
|
|
{
|
|
|
|
char* temp = (char*)alloca(m_width);
|
|
|
|
|
2013-04-28 15:27:35 -04:00
|
|
|
uint32_t num = bx::vsnprintf(temp, m_width, _format, _argList);
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
uint8_t* mem = &m_mem[(_y*m_width+_x)*2];
|
|
|
|
for (uint32_t ii = 0, xx = _x; ii < num && xx < m_width; ++ii, ++xx)
|
|
|
|
{
|
|
|
|
mem[0] = temp[ii];
|
|
|
|
mem[1] = _attr;
|
|
|
|
mem += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void printf(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, ...)
|
|
|
|
{
|
|
|
|
va_list argList;
|
|
|
|
va_start(argList, _format);
|
|
|
|
printfVargs(_x, _y, _attr, _format, argList);
|
|
|
|
va_end(argList);
|
|
|
|
}
|
|
|
|
|
2014-12-04 23:32:19 -05:00
|
|
|
void image(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const void* _data, uint16_t _pitch)
|
|
|
|
{
|
|
|
|
if (_x < m_width && _y < m_height)
|
|
|
|
{
|
|
|
|
uint8_t* dst = &m_mem[(_y*m_width+_x)*2];
|
|
|
|
const uint8_t* src = (const uint8_t*)_data;
|
|
|
|
const uint32_t width = (bx::uint32_min(m_width, _width +_x)-_x)*2;
|
|
|
|
const uint32_t height = bx::uint32_min(m_height, _height+_y)-_y;
|
|
|
|
const uint32_t dstPitch = m_width*2;
|
|
|
|
|
|
|
|
for (uint32_t yy = 0; yy < height; ++yy)
|
|
|
|
{
|
|
|
|
memcpy(dst, src, width);
|
|
|
|
dst += dstPitch;
|
|
|
|
src += _pitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
uint8_t* m_mem;
|
|
|
|
uint32_t m_size;
|
|
|
|
uint16_t m_width;
|
|
|
|
uint16_t m_height;
|
|
|
|
bool m_small;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TextVideoMemBlitter
|
|
|
|
{
|
|
|
|
void init();
|
2013-01-28 00:52:29 -05:00
|
|
|
void shutdown();
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
TextureHandle m_texture;
|
|
|
|
TransientVertexBuffer* m_vb;
|
|
|
|
TransientIndexBuffer* m_ib;
|
|
|
|
VertexDecl m_decl;
|
2012-09-16 20:36:08 -04:00
|
|
|
ProgramHandle m_program;
|
2013-01-28 00:52:29 -05:00
|
|
|
bool m_init;
|
2012-08-05 17:51:49 -04:00
|
|
|
};
|
|
|
|
|
2014-05-26 17:09:26 -04:00
|
|
|
struct RendererContextI;
|
|
|
|
|
|
|
|
extern void blit(RendererContextI* _renderCtx, TextVideoMemBlitter& _blitter, const TextVideoMem& _mem);
|
|
|
|
|
|
|
|
inline void blit(RendererContextI* _renderCtx, TextVideoMemBlitter& _blitter, const TextVideoMem* _mem)
|
|
|
|
{
|
|
|
|
blit(_renderCtx, _blitter, *_mem);
|
|
|
|
}
|
|
|
|
|
2013-03-03 00:35:09 -05:00
|
|
|
template <uint32_t maxKeys>
|
|
|
|
struct UpdateBatchT
|
|
|
|
{
|
|
|
|
UpdateBatchT()
|
|
|
|
: m_num(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(uint32_t _key, uint32_t _value)
|
|
|
|
{
|
|
|
|
uint32_t num = m_num++;
|
|
|
|
m_keys[num] = _key;
|
|
|
|
m_values[num] = _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sort()
|
|
|
|
{
|
|
|
|
if (0 < m_num)
|
|
|
|
{
|
|
|
|
uint32_t* tempKeys = (uint32_t*)alloca(sizeof(m_keys) );
|
|
|
|
uint32_t* tempValues = (uint32_t*)alloca(sizeof(m_values) );
|
|
|
|
bx::radixSort32(m_keys, tempKeys, m_values, tempValues, m_num);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isFull() const
|
|
|
|
{
|
|
|
|
return m_num >= maxKeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
m_num = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t m_num;
|
|
|
|
uint32_t m_keys[maxKeys];
|
|
|
|
uint32_t m_values[maxKeys];
|
|
|
|
};
|
|
|
|
|
2012-07-29 16:50:23 -04:00
|
|
|
struct ClearQuad
|
|
|
|
{
|
2014-04-19 21:16:26 -04:00
|
|
|
ClearQuad()
|
|
|
|
{
|
|
|
|
for (uint32_t ii = 0; ii < BX_COUNTOF(m_program); ++ii)
|
|
|
|
{
|
|
|
|
m_program[ii].idx = invalidHandle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-29 16:50:23 -04:00
|
|
|
void init();
|
2013-01-28 00:52:29 -05:00
|
|
|
void shutdown();
|
2012-07-29 16:50:23 -04:00
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
TransientVertexBuffer* m_vb;
|
|
|
|
VertexDecl m_decl;
|
2014-03-26 02:07:51 -04:00
|
|
|
ProgramHandle m_program[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS];
|
2012-07-29 16:50:23 -04:00
|
|
|
};
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
struct PredefinedUniform
|
|
|
|
{
|
|
|
|
enum Enum
|
|
|
|
{
|
|
|
|
ViewRect,
|
|
|
|
ViewTexel,
|
|
|
|
View,
|
2014-05-10 23:51:44 -04:00
|
|
|
InvView,
|
|
|
|
Proj,
|
|
|
|
InvProj,
|
2012-08-05 17:51:49 -04:00
|
|
|
ViewProj,
|
2014-05-10 23:51:44 -04:00
|
|
|
InvViewProj,
|
2012-08-05 17:51:49 -04:00
|
|
|
Model,
|
2012-10-07 23:41:18 -04:00
|
|
|
ModelView,
|
2012-08-05 17:51:49 -04:00
|
|
|
ModelViewProj,
|
|
|
|
AlphaRef,
|
|
|
|
Count
|
|
|
|
};
|
|
|
|
|
2012-12-29 16:46:25 -05:00
|
|
|
uint32_t m_loc;
|
2012-08-05 17:51:49 -04:00
|
|
|
uint16_t m_count;
|
2012-12-29 16:46:25 -05:00
|
|
|
uint8_t m_type;
|
2012-08-05 17:51:49 -04:00
|
|
|
};
|
|
|
|
|
2014-04-15 22:10:56 -04:00
|
|
|
const char* getUniformTypeName(UniformType::Enum _enum);
|
|
|
|
UniformType::Enum nameToUniformTypeEnum(const char* _name);
|
2012-08-05 17:51:49 -04:00
|
|
|
const char* getPredefinedUniformName(PredefinedUniform::Enum _enum);
|
|
|
|
PredefinedUniform::Enum nameToPredefinedUniformEnum(const char* _name);
|
|
|
|
|
|
|
|
struct CommandBuffer
|
|
|
|
{
|
|
|
|
CommandBuffer()
|
|
|
|
: m_pos(0)
|
|
|
|
, m_size(BGFX_CONFIG_MAX_COMMAND_BUFFER_SIZE)
|
|
|
|
{
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Enum
|
|
|
|
{
|
|
|
|
RendererInit,
|
2013-01-28 00:52:29 -05:00
|
|
|
RendererShutdownBegin,
|
2012-08-05 17:51:49 -04:00
|
|
|
CreateVertexDecl,
|
|
|
|
CreateIndexBuffer,
|
|
|
|
CreateVertexBuffer,
|
|
|
|
CreateDynamicIndexBuffer,
|
|
|
|
UpdateDynamicIndexBuffer,
|
|
|
|
CreateDynamicVertexBuffer,
|
|
|
|
UpdateDynamicVertexBuffer,
|
2014-03-29 22:42:57 -04:00
|
|
|
CreateShader,
|
2012-09-16 20:36:08 -04:00
|
|
|
CreateProgram,
|
2012-08-05 17:51:49 -04:00
|
|
|
CreateTexture,
|
2012-08-13 00:02:11 -04:00
|
|
|
UpdateTexture,
|
2015-04-13 21:39:38 -04:00
|
|
|
ResizeTexture,
|
2014-02-06 02:07:11 -05:00
|
|
|
CreateFrameBuffer,
|
2012-08-05 17:51:49 -04:00
|
|
|
CreateUniform,
|
2013-06-09 18:28:25 -04:00
|
|
|
UpdateViewName,
|
2012-08-05 17:51:49 -04:00
|
|
|
End,
|
2013-01-28 00:52:29 -05:00
|
|
|
RendererShutdownEnd,
|
2012-08-05 17:51:49 -04:00
|
|
|
DestroyVertexDecl,
|
|
|
|
DestroyIndexBuffer,
|
|
|
|
DestroyVertexBuffer,
|
|
|
|
DestroyDynamicIndexBuffer,
|
|
|
|
DestroyDynamicVertexBuffer,
|
2014-03-29 22:42:57 -04:00
|
|
|
DestroyShader,
|
2012-09-16 20:36:08 -04:00
|
|
|
DestroyProgram,
|
2012-08-05 17:51:49 -04:00
|
|
|
DestroyTexture,
|
2014-02-06 02:07:11 -05:00
|
|
|
DestroyFrameBuffer,
|
2012-08-05 17:51:49 -04:00
|
|
|
DestroyUniform,
|
|
|
|
SaveScreenShot,
|
|
|
|
};
|
|
|
|
|
|
|
|
void write(const void* _data, uint32_t _size)
|
|
|
|
{
|
2013-10-05 21:07:44 -04:00
|
|
|
BX_CHECK(m_size == BGFX_CONFIG_MAX_COMMAND_BUFFER_SIZE, "Called write outside start/finish?");
|
2012-08-05 17:51:49 -04:00
|
|
|
BX_CHECK(m_pos < m_size, "");
|
|
|
|
memcpy(&m_buffer[m_pos], _data, _size);
|
|
|
|
m_pos += _size;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
void write(const Type& _in)
|
|
|
|
{
|
2014-02-06 02:07:11 -05:00
|
|
|
align(BX_ALIGNOF(Type) );
|
2012-08-05 17:51:49 -04:00
|
|
|
write(reinterpret_cast<const uint8_t*>(&_in), sizeof(Type) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void read(void* _data, uint32_t _size)
|
|
|
|
{
|
|
|
|
BX_CHECK(m_pos < m_size, "");
|
|
|
|
memcpy(_data, &m_buffer[m_pos], _size);
|
|
|
|
m_pos += _size;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
void read(Type& _in)
|
|
|
|
{
|
2014-02-06 02:07:11 -05:00
|
|
|
align(BX_ALIGNOF(Type) );
|
2012-08-05 17:51:49 -04:00
|
|
|
read(reinterpret_cast<uint8_t*>(&_in), sizeof(Type) );
|
|
|
|
}
|
|
|
|
|
2013-06-09 18:28:25 -04:00
|
|
|
const uint8_t* skip(uint32_t _size)
|
2013-03-03 00:35:09 -05:00
|
|
|
{
|
|
|
|
BX_CHECK(m_pos < m_size, "");
|
2013-06-09 18:28:25 -04:00
|
|
|
const uint8_t* result = &m_buffer[m_pos];
|
2013-03-03 00:35:09 -05:00
|
|
|
m_pos += _size;
|
2013-06-09 18:28:25 -04:00
|
|
|
return result;
|
2013-03-03 00:35:09 -05:00
|
|
|
}
|
|
|
|
|
2014-02-06 02:07:11 -05:00
|
|
|
template<typename Type>
|
|
|
|
void skip()
|
|
|
|
{
|
|
|
|
align(BX_ALIGNOF(Type) );
|
|
|
|
skip(sizeof(Type) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void align(uint32_t _alignment)
|
|
|
|
{
|
|
|
|
const uint32_t mask = _alignment-1;
|
|
|
|
const uint32_t pos = (m_pos+mask) & (~mask);
|
|
|
|
m_pos = pos;
|
|
|
|
}
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
m_pos = 0;
|
|
|
|
}
|
|
|
|
|
2013-10-05 21:07:44 -04:00
|
|
|
void start()
|
|
|
|
{
|
|
|
|
m_pos = 0;
|
|
|
|
m_size = BGFX_CONFIG_MAX_COMMAND_BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
void finish()
|
|
|
|
{
|
|
|
|
uint8_t cmd = End;
|
|
|
|
write(cmd);
|
2013-10-05 21:07:44 -04:00
|
|
|
m_size = m_pos;
|
2012-08-05 17:51:49 -04:00
|
|
|
m_pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t m_pos;
|
|
|
|
uint32_t m_size;
|
|
|
|
uint8_t m_buffer[BGFX_CONFIG_MAX_COMMAND_BUFFER_SIZE];
|
|
|
|
|
|
|
|
private:
|
|
|
|
CommandBuffer(const CommandBuffer&);
|
|
|
|
void operator=(const CommandBuffer&);
|
|
|
|
};
|
|
|
|
|
2015-04-02 14:58:24 -04:00
|
|
|
#define SORT_KEY_RENDER_DRAW (UINT64_C(1)<<0x36)
|
2015-02-21 18:40:51 -05:00
|
|
|
#define SORT_KEY_VIEW_SHIFT UINT8_C(0x37)
|
|
|
|
#define SORT_KEY_VIEW_MASK ( (uint64_t(BGFX_CONFIG_MAX_VIEWS-1) )<<SORT_KEY_VIEW_SHIFT)
|
2014-09-07 20:17:38 -04:00
|
|
|
|
2015-02-21 18:40:51 -05:00
|
|
|
BX_STATIC_ASSERT(BGFX_CONFIG_MAX_VIEWS <= 256);
|
2014-09-07 20:17:38 -04:00
|
|
|
BX_STATIC_ASSERT( (BGFX_CONFIG_MAX_PROGRAMS & (BGFX_CONFIG_MAX_PROGRAMS-1) ) == 0); // must be power of 2
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
struct SortKey
|
|
|
|
{
|
2014-07-20 23:27:13 -04:00
|
|
|
uint64_t encodeDraw()
|
|
|
|
{
|
|
|
|
// | 3 2 1 0|
|
|
|
|
// |fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210|
|
2015-04-02 14:58:24 -04:00
|
|
|
// | vvvvvvvvdsssssssssssttpppppppppdddddddddddddddddddddddddddddddd|
|
|
|
|
// | ^^ ^ ^ ^ ^|
|
|
|
|
// | || | | | ||
|
|
|
|
// | view-+| seq-+ +-trans +-program depth-+|
|
|
|
|
// | +-draw |
|
2014-07-20 23:27:13 -04:00
|
|
|
|
|
|
|
const uint64_t depth = m_depth;
|
2015-01-28 14:52:58 -05:00
|
|
|
const uint64_t program = uint64_t(m_program)<<0x20;
|
|
|
|
const uint64_t trans = uint64_t(m_trans )<<0x29;
|
2015-04-02 14:58:24 -04:00
|
|
|
const uint64_t seq = uint64_t(m_seq )<<0x2b;
|
2015-02-21 18:40:51 -05:00
|
|
|
const uint64_t view = uint64_t(m_view )<<SORT_KEY_VIEW_SHIFT;
|
2014-07-20 23:27:13 -04:00
|
|
|
const uint64_t key = depth|program|trans|SORT_KEY_RENDER_DRAW|seq|view;
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t encodeCompute()
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
// | 3 2 1 0|
|
|
|
|
// |fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210|
|
2015-04-02 14:58:24 -04:00
|
|
|
// | vvvvvvvvdsssssssssssppppppppp |
|
|
|
|
// | ^^ ^ ^ |
|
|
|
|
// | || | | |
|
|
|
|
// | view-+| seq-+ +-program |
|
|
|
|
// | +-draw |
|
2015-01-28 14:52:58 -05:00
|
|
|
|
|
|
|
const uint64_t program = uint64_t(m_program)<<0x22;
|
2015-04-02 14:58:24 -04:00
|
|
|
const uint64_t seq = uint64_t(m_seq )<<0x2b;
|
2015-02-21 18:40:51 -05:00
|
|
|
const uint64_t view = uint64_t(m_view )<<SORT_KEY_VIEW_SHIFT;
|
2014-07-20 23:27:13 -04:00
|
|
|
const uint64_t key = program|seq|view;
|
2012-08-05 17:51:49 -04:00
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2014-07-20 23:27:13 -04:00
|
|
|
/// Returns true if item is command.
|
|
|
|
bool decode(uint64_t _key)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-01-28 14:52:58 -05:00
|
|
|
m_seq = (_key>>0x2c)& 0x7ff;
|
2015-02-21 18:40:51 -05:00
|
|
|
m_view = uint8_t( (_key&SORT_KEY_VIEW_MASK)>>SORT_KEY_VIEW_SHIFT);
|
2014-07-20 23:27:13 -04:00
|
|
|
if (_key & SORT_KEY_RENDER_DRAW)
|
|
|
|
{
|
|
|
|
m_depth = _key & 0xffffffff;
|
2015-01-28 14:52:58 -05:00
|
|
|
m_program = (_key>>0x20)&(BGFX_CONFIG_MAX_PROGRAMS-1);
|
|
|
|
m_trans = (_key>>0x29)& 0x3;
|
2014-07-20 23:27:13 -04:00
|
|
|
return false; // draw
|
|
|
|
}
|
|
|
|
|
2015-01-28 14:52:58 -05:00
|
|
|
m_program = (_key>>0x22)&(BGFX_CONFIG_MAX_PROGRAMS-1);
|
2014-07-20 23:27:13 -04:00
|
|
|
return true; // compute
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2015-02-21 18:40:51 -05:00
|
|
|
bool decode(uint64_t _key, uint8_t _viewRemap[BGFX_CONFIG_MAX_VIEWS])
|
|
|
|
{
|
|
|
|
bool compute = decode(_key);
|
|
|
|
m_view = _viewRemap[m_view];
|
|
|
|
return compute;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t remapView(uint64_t _key, uint8_t _viewRemap[BGFX_CONFIG_MAX_VIEWS])
|
|
|
|
{
|
|
|
|
const uint8_t oldView = uint8_t( (_key & SORT_KEY_VIEW_MASK) >> SORT_KEY_VIEW_SHIFT);
|
|
|
|
const uint64_t view = uint64_t(_viewRemap[oldView]) << SORT_KEY_VIEW_SHIFT;
|
|
|
|
const uint64_t key = (_key & ~SORT_KEY_VIEW_MASK) | view;
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
void reset()
|
|
|
|
{
|
2014-04-13 00:25:38 -04:00
|
|
|
m_depth = 0;
|
2012-09-16 20:36:08 -04:00
|
|
|
m_program = 0;
|
2014-04-13 00:25:38 -04:00
|
|
|
m_seq = 0;
|
|
|
|
m_view = 0;
|
|
|
|
m_trans = 0;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2015-01-28 14:52:58 -05:00
|
|
|
uint32_t m_depth;
|
2012-09-16 20:36:08 -04:00
|
|
|
uint16_t m_program;
|
2012-08-05 17:51:49 -04:00
|
|
|
uint16_t m_seq;
|
2014-07-20 23:27:13 -04:00
|
|
|
uint8_t m_view;
|
|
|
|
uint8_t m_trans;
|
2012-08-05 17:51:49 -04:00
|
|
|
};
|
2014-08-13 00:47:01 -04:00
|
|
|
#undef SORT_KEY_RENDER_DRAW
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2014-10-14 00:31:18 -04:00
|
|
|
BX_ALIGN_DECL_16(struct) Matrix4
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2014-01-19 02:33:00 -05:00
|
|
|
union
|
|
|
|
{
|
|
|
|
float val[16];
|
2014-01-19 17:58:05 -05:00
|
|
|
bx::float4x4_t f4x4;
|
2014-01-19 02:33:00 -05:00
|
|
|
} un;
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
void setIdentity()
|
|
|
|
{
|
2014-01-19 02:33:00 -05:00
|
|
|
memset(un.val, 0, sizeof(un.val) );
|
|
|
|
un.val[0] = un.val[5] = un.val[10] = un.val[15] = 1.0f;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MatrixCache
|
|
|
|
{
|
|
|
|
MatrixCache()
|
|
|
|
: m_num(1)
|
|
|
|
{
|
|
|
|
m_cache[0].setIdentity();
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
m_num = 1;
|
|
|
|
}
|
|
|
|
|
2014-10-08 23:13:57 -04:00
|
|
|
uint32_t reserve(uint16_t* _num)
|
|
|
|
{
|
2014-10-10 12:26:58 -04:00
|
|
|
uint32_t num = *_num;
|
|
|
|
BX_CHECK(m_num+num < BGFX_CONFIG_MAX_MATRIX_CACHE, "Matrix cache overflow. %d (max: %d)", m_num+num, BGFX_CONFIG_MAX_MATRIX_CACHE);
|
|
|
|
num = bx::uint32_min(num, BGFX_CONFIG_MAX_MATRIX_CACHE-m_num);
|
2014-10-08 23:13:57 -04:00
|
|
|
uint32_t first = m_num;
|
|
|
|
m_num += num;
|
|
|
|
*_num = (uint16_t)num;
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
uint32_t add(const void* _mtx, uint16_t _num)
|
|
|
|
{
|
|
|
|
if (NULL != _mtx)
|
|
|
|
{
|
2014-10-08 23:13:57 -04:00
|
|
|
uint32_t first = reserve(&_num);
|
|
|
|
memcpy(&m_cache[first], _mtx, sizeof(Matrix4)*_num);
|
2012-08-05 17:51:49 -04:00
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-08 23:13:57 -04:00
|
|
|
float* toPtr(uint32_t _cacheIdx)
|
|
|
|
{
|
|
|
|
return m_cache[_cacheIdx].un.val;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t fromPtr(const void* _ptr) const
|
|
|
|
{
|
|
|
|
return uint32_t( (const Matrix4*)_ptr - m_cache);
|
|
|
|
}
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
Matrix4 m_cache[BGFX_CONFIG_MAX_MATRIX_CACHE];
|
|
|
|
uint32_t m_num;
|
|
|
|
};
|
|
|
|
|
2013-07-27 18:27:54 -04:00
|
|
|
struct RectCache
|
|
|
|
{
|
|
|
|
RectCache()
|
|
|
|
: m_num(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
m_num = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t add(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
|
|
|
|
{
|
|
|
|
BX_CHECK(m_num+1 < BGFX_CONFIG_MAX_RECT_CACHE, "Rect cache overflow. %d (max: %d)", m_num, BGFX_CONFIG_MAX_RECT_CACHE);
|
|
|
|
|
|
|
|
uint32_t first = m_num;
|
|
|
|
Rect& rect = m_cache[m_num];
|
|
|
|
|
|
|
|
rect.m_x = _x;
|
|
|
|
rect.m_y = _y;
|
|
|
|
rect.m_width = _width;
|
|
|
|
rect.m_height = _height;
|
|
|
|
|
|
|
|
m_num++;
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect m_cache[BGFX_CONFIG_MAX_RECT_CACHE];
|
|
|
|
uint32_t m_num;
|
|
|
|
};
|
|
|
|
|
2013-06-11 01:41:03 -04:00
|
|
|
#define CONSTANT_OPCODE_TYPE_SHIFT 27
|
|
|
|
#define CONSTANT_OPCODE_TYPE_MASK UINT32_C(0xf8000000)
|
|
|
|
#define CONSTANT_OPCODE_LOC_SHIFT 11
|
|
|
|
#define CONSTANT_OPCODE_LOC_MASK UINT32_C(0x07fff800)
|
|
|
|
#define CONSTANT_OPCODE_NUM_SHIFT 1
|
|
|
|
#define CONSTANT_OPCODE_NUM_MASK UINT32_C(0x000007fe)
|
|
|
|
#define CONSTANT_OPCODE_COPY_SHIFT 0
|
|
|
|
#define CONSTANT_OPCODE_COPY_MASK UINT32_C(0x00000001)
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2013-01-06 01:34:31 -05:00
|
|
|
#define BGFX_UNIFORM_FRAGMENTBIT UINT8_C(0x10)
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
class ConstantBuffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static ConstantBuffer* create(uint32_t _size)
|
|
|
|
{
|
2013-08-04 01:15:13 -04:00
|
|
|
uint32_t size = BX_ALIGN_16(bx::uint32_max(_size, sizeof(ConstantBuffer) ) );
|
2013-09-17 00:40:30 -04:00
|
|
|
void* data = BX_ALLOC(g_allocator, size);
|
2012-08-05 17:51:49 -04:00
|
|
|
return ::new(data) ConstantBuffer(_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy(ConstantBuffer* _constantBuffer)
|
|
|
|
{
|
|
|
|
_constantBuffer->~ConstantBuffer();
|
2013-09-17 00:40:30 -04:00
|
|
|
BX_FREE(g_allocator, _constantBuffer);
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2012-10-28 00:34:41 -04:00
|
|
|
static uint32_t encodeOpcode(UniformType::Enum _type, uint16_t _loc, uint16_t _num, uint16_t _copy)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2013-06-11 01:41:03 -04:00
|
|
|
const uint32_t type = _type << CONSTANT_OPCODE_TYPE_SHIFT;
|
|
|
|
const uint32_t loc = _loc << CONSTANT_OPCODE_LOC_SHIFT;
|
|
|
|
const uint32_t num = _num << CONSTANT_OPCODE_NUM_SHIFT;
|
|
|
|
const uint32_t copy = _copy << CONSTANT_OPCODE_COPY_SHIFT;
|
|
|
|
return type|loc|num|copy;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2012-10-28 00:34:41 -04:00
|
|
|
static void decodeOpcode(uint32_t _opcode, UniformType::Enum& _type, uint16_t& _loc, uint16_t& _num, uint16_t& _copy)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2013-06-11 01:41:03 -04:00
|
|
|
const uint32_t type = (_opcode&CONSTANT_OPCODE_TYPE_MASK) >> CONSTANT_OPCODE_TYPE_SHIFT;
|
2014-08-07 23:31:35 -04:00
|
|
|
const uint32_t loc = (_opcode&CONSTANT_OPCODE_LOC_MASK ) >> CONSTANT_OPCODE_LOC_SHIFT;
|
|
|
|
const uint32_t num = (_opcode&CONSTANT_OPCODE_NUM_MASK ) >> CONSTANT_OPCODE_NUM_SHIFT;
|
|
|
|
const uint32_t copy = (_opcode&CONSTANT_OPCODE_COPY_MASK); // >> CONSTANT_OPCODE_COPY_SHIFT;
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2013-06-11 01:41:03 -04:00
|
|
|
_type = (UniformType::Enum)(type);
|
2012-08-05 17:51:49 -04:00
|
|
|
_copy = (uint16_t)copy;
|
2014-08-07 23:31:35 -04:00
|
|
|
_num = (uint16_t)num;
|
|
|
|
_loc = (uint16_t)loc;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void write(const void* _data, uint32_t _size)
|
|
|
|
{
|
|
|
|
BX_CHECK(m_pos + _size < m_size, "Write would go out of bounds. pos %d + size %d > max size: %d).", m_pos, _size, m_size);
|
|
|
|
|
|
|
|
if (m_pos + _size < m_size)
|
|
|
|
{
|
|
|
|
memcpy(&m_buffer[m_pos], _data, _size);
|
|
|
|
m_pos += _size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void write(uint32_t _value)
|
|
|
|
{
|
|
|
|
write(&_value, sizeof(uint32_t) );
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* read(uint32_t _size)
|
|
|
|
{
|
|
|
|
BX_CHECK(m_pos < m_size, "Out of bounds %d (size: %d).", m_pos, m_size);
|
|
|
|
const char* result = &m_buffer[m_pos];
|
|
|
|
m_pos += _size;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t read()
|
|
|
|
{
|
2014-08-07 23:31:35 -04:00
|
|
|
uint32_t result;
|
|
|
|
memcpy(&result, read(sizeof(uint32_t) ), sizeof(uint32_t) );
|
|
|
|
return result;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isEmpty() const
|
|
|
|
{
|
|
|
|
return 0 == m_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t getPos() const
|
|
|
|
{
|
|
|
|
return m_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset(uint32_t _pos = 0)
|
|
|
|
{
|
|
|
|
m_pos = _pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void finish()
|
|
|
|
{
|
2012-10-28 00:34:41 -04:00
|
|
|
write(UniformType::End);
|
2012-08-05 17:51:49 -04:00
|
|
|
m_pos = 0;
|
|
|
|
}
|
|
|
|
|
2012-10-28 00:34:41 -04:00
|
|
|
void writeUniform(UniformType::Enum _type, uint16_t _loc, const void* _value, uint16_t _num = 1);
|
2014-04-15 22:10:56 -04:00
|
|
|
void writeUniformHandle(UniformType::Enum _type, uint16_t _loc, UniformHandle _handle, uint16_t _num = 1);
|
2013-06-11 01:41:03 -04:00
|
|
|
void writeMarker(const char* _marker);
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
ConstantBuffer(uint32_t _size)
|
|
|
|
: m_size(_size-sizeof(m_buffer) )
|
|
|
|
, m_pos(0)
|
|
|
|
{
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
~ConstantBuffer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t m_size;
|
|
|
|
uint32_t m_pos;
|
|
|
|
char m_buffer[8];
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef const void* (*UniformFn)(const void* _data);
|
|
|
|
|
|
|
|
struct UniformInfo
|
|
|
|
{
|
|
|
|
const void* m_data;
|
|
|
|
UniformFn m_func;
|
2014-04-15 22:10:56 -04:00
|
|
|
UniformHandle m_handle;
|
2012-08-05 17:51:49 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
class UniformRegistry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UniformRegistry()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~UniformRegistry()
|
|
|
|
{
|
|
|
|
}
|
2013-02-13 01:36:50 -05:00
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
const UniformInfo* find(const char* _name) const
|
|
|
|
{
|
|
|
|
UniformHashMap::const_iterator it = m_uniforms.find(_name);
|
|
|
|
if (it != m_uniforms.end() )
|
|
|
|
{
|
|
|
|
return &it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-04-15 22:10:56 -04:00
|
|
|
const UniformInfo& add(UniformHandle _handle, const char* _name, const void* _data, UniformFn _func = NULL)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2012-10-28 00:34:41 -04:00
|
|
|
UniformHashMap::iterator it = m_uniforms.find(_name);
|
2012-08-05 17:51:49 -04:00
|
|
|
if (it == m_uniforms.end() )
|
|
|
|
{
|
|
|
|
UniformInfo info;
|
2014-04-15 22:10:56 -04:00
|
|
|
info.m_data = _data;
|
|
|
|
info.m_func = _func;
|
|
|
|
info.m_handle = _handle;
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
stl::pair<UniformHashMap::iterator, bool> result = m_uniforms.insert(UniformHashMap::value_type(_name, info) );
|
2013-02-13 01:36:50 -05:00
|
|
|
return result.first->second;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2014-04-15 22:10:56 -04:00
|
|
|
UniformInfo& info = it->second;
|
|
|
|
info.m_data = _data;
|
|
|
|
info.m_func = _func;
|
|
|
|
info.m_handle = _handle;
|
|
|
|
|
|
|
|
return info;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2015-01-14 01:34:48 -05:00
|
|
|
private:
|
|
|
|
typedef stl::unordered_map<stl::string, UniformInfo> UniformHashMap;
|
|
|
|
UniformHashMap m_uniforms;
|
|
|
|
};
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2015-01-14 01:34:48 -05:00
|
|
|
struct Binding
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-01-14 01:34:48 -05:00
|
|
|
enum Enum
|
|
|
|
{
|
|
|
|
Image,
|
|
|
|
IndexBuffer,
|
|
|
|
VertexBuffer,
|
|
|
|
Texture,
|
|
|
|
|
|
|
|
Count
|
|
|
|
};
|
|
|
|
|
2014-07-20 23:27:13 -04:00
|
|
|
uint16_t m_idx;
|
2015-01-14 01:34:48 -05:00
|
|
|
uint8_t m_type;
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
2015-01-15 00:31:08 -05:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
uint32_t m_flags;
|
|
|
|
} m_draw;
|
|
|
|
|
2015-01-14 01:34:48 -05:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
uint8_t m_format;
|
|
|
|
uint8_t m_access;
|
|
|
|
uint8_t m_mip;
|
|
|
|
} m_compute;
|
2015-01-15 00:31:08 -05:00
|
|
|
|
2015-01-14 01:34:48 -05:00
|
|
|
} m_un;
|
2014-07-20 23:27:13 -04:00
|
|
|
};
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2014-07-20 23:27:13 -04:00
|
|
|
struct RenderDraw
|
|
|
|
{
|
2012-08-05 17:51:49 -04:00
|
|
|
void clear()
|
|
|
|
{
|
2014-12-28 14:36:36 -05:00
|
|
|
m_constBegin = 0;
|
|
|
|
m_constEnd = 0;
|
|
|
|
m_flags = BGFX_STATE_DEFAULT;
|
|
|
|
m_stencil = packStencil(BGFX_STENCIL_DEFAULT, BGFX_STENCIL_DEFAULT);
|
|
|
|
m_rgba = 0;
|
|
|
|
m_matrix = 0;
|
|
|
|
m_startIndex = 0;
|
|
|
|
m_numIndices = UINT32_MAX;
|
2012-08-05 17:51:49 -04:00
|
|
|
m_startVertex = 0;
|
2013-03-30 01:58:50 -04:00
|
|
|
m_numVertices = UINT32_MAX;
|
2012-08-05 17:51:49 -04:00
|
|
|
m_instanceDataOffset = 0;
|
|
|
|
m_instanceDataStride = 0;
|
2014-12-28 14:36:36 -05:00
|
|
|
m_numInstances = 1;
|
|
|
|
m_num = 1;
|
|
|
|
m_flags = BGFX_SUBMIT_EYE_FIRST;
|
2013-07-27 18:27:54 -04:00
|
|
|
m_scissor = UINT16_MAX;
|
2014-12-28 14:36:36 -05:00
|
|
|
m_vertexBuffer.idx = invalidHandle;
|
|
|
|
m_vertexDecl.idx = invalidHandle;
|
|
|
|
m_indexBuffer.idx = invalidHandle;
|
2012-08-05 17:51:49 -04:00
|
|
|
m_instanceDataBuffer.idx = invalidHandle;
|
2013-02-13 01:36:50 -05:00
|
|
|
|
2014-07-30 23:37:54 -04:00
|
|
|
for (uint32_t ii = 0; ii < BGFX_CONFIG_MAX_TEXTURE_SAMPLERS; ++ii)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-01-14 01:34:48 -05:00
|
|
|
m_bind[ii].m_idx = invalidHandle;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 00:31:08 -05:00
|
|
|
Binding m_bind[BGFX_CONFIG_MAX_TEXTURE_SAMPLERS];
|
2012-08-05 17:51:49 -04:00
|
|
|
uint64_t m_flags;
|
2012-11-10 22:59:23 -05:00
|
|
|
uint64_t m_stencil;
|
2013-03-30 01:58:50 -04:00
|
|
|
uint32_t m_rgba;
|
2012-08-05 17:51:49 -04:00
|
|
|
uint32_t m_constBegin;
|
|
|
|
uint32_t m_constEnd;
|
|
|
|
uint32_t m_matrix;
|
|
|
|
uint32_t m_startIndex;
|
|
|
|
uint32_t m_numIndices;
|
|
|
|
uint32_t m_startVertex;
|
|
|
|
uint32_t m_numVertices;
|
|
|
|
uint32_t m_instanceDataOffset;
|
|
|
|
uint16_t m_instanceDataStride;
|
|
|
|
uint16_t m_numInstances;
|
|
|
|
uint16_t m_num;
|
2013-07-14 17:32:09 -04:00
|
|
|
uint16_t m_scissor;
|
2014-12-28 14:36:36 -05:00
|
|
|
uint8_t m_submitFlags;
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
VertexBufferHandle m_vertexBuffer;
|
2014-07-20 23:27:13 -04:00
|
|
|
VertexDeclHandle m_vertexDecl;
|
|
|
|
IndexBufferHandle m_indexBuffer;
|
2012-08-05 17:51:49 -04:00
|
|
|
VertexBufferHandle m_instanceDataBuffer;
|
2014-07-20 23:27:13 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct RenderCompute
|
|
|
|
{
|
|
|
|
void clear()
|
|
|
|
{
|
2014-12-28 14:36:36 -05:00
|
|
|
m_constBegin = 0;
|
|
|
|
m_constEnd = 0;
|
|
|
|
m_matrix = 0;
|
|
|
|
m_numX = 0;
|
|
|
|
m_numY = 0;
|
|
|
|
m_numZ = 0;
|
|
|
|
m_num = 0;
|
|
|
|
m_submitFlags = BGFX_SUBMIT_EYE_FIRST;
|
2014-07-20 23:27:13 -04:00
|
|
|
|
|
|
|
for (uint32_t ii = 0; ii < BGFX_MAX_COMPUTE_BINDINGS; ++ii)
|
|
|
|
{
|
|
|
|
m_bind[ii].m_idx = invalidHandle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 00:31:08 -05:00
|
|
|
Binding m_bind[BGFX_MAX_COMPUTE_BINDINGS];
|
2014-07-20 23:27:13 -04:00
|
|
|
uint32_t m_constBegin;
|
|
|
|
uint32_t m_constEnd;
|
2014-12-27 22:00:41 -05:00
|
|
|
uint32_t m_matrix;
|
2014-07-20 23:27:13 -04:00
|
|
|
|
|
|
|
uint16_t m_numX;
|
|
|
|
uint16_t m_numY;
|
|
|
|
uint16_t m_numZ;
|
2014-12-27 22:00:41 -05:00
|
|
|
uint16_t m_num;
|
2014-12-28 14:36:36 -05:00
|
|
|
uint8_t m_submitFlags;
|
2014-07-20 23:27:13 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
union RenderItem
|
|
|
|
{
|
|
|
|
RenderDraw draw;
|
|
|
|
RenderCompute compute;
|
|
|
|
};
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
struct Resolution
|
|
|
|
{
|
|
|
|
Resolution()
|
|
|
|
: m_width(BGFX_DEFAULT_WIDTH)
|
|
|
|
, m_height(BGFX_DEFAULT_HEIGHT)
|
|
|
|
, m_flags(BGFX_RESET_NONE)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t m_width;
|
|
|
|
uint32_t m_height;
|
|
|
|
uint32_t m_flags;
|
|
|
|
};
|
|
|
|
|
2014-12-10 02:16:27 -05:00
|
|
|
struct VertexBuffer
|
|
|
|
{
|
|
|
|
uint16_t m_stride;
|
|
|
|
};
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
struct DynamicIndexBuffer
|
|
|
|
{
|
|
|
|
IndexBufferHandle m_handle;
|
|
|
|
uint32_t m_offset;
|
|
|
|
uint32_t m_size;
|
2015-01-24 01:18:07 -05:00
|
|
|
uint32_t m_startIndex;
|
2015-01-28 20:56:29 -05:00
|
|
|
uint8_t m_flags;
|
2012-08-05 17:51:49 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct DynamicVertexBuffer
|
|
|
|
{
|
|
|
|
VertexBufferHandle m_handle;
|
|
|
|
uint32_t m_offset;
|
|
|
|
uint32_t m_size;
|
|
|
|
uint32_t m_startVertex;
|
|
|
|
uint32_t m_numVertices;
|
2014-12-10 02:16:27 -05:00
|
|
|
uint16_t m_stride;
|
2012-08-05 17:51:49 -04:00
|
|
|
VertexDeclHandle m_decl;
|
2014-12-10 02:16:27 -05:00
|
|
|
uint8_t m_flags;
|
2012-08-05 17:51:49 -04:00
|
|
|
};
|
|
|
|
|
2014-10-14 00:31:18 -04:00
|
|
|
BX_ALIGN_DECL_CACHE_LINE(struct) Frame
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
Frame()
|
2014-10-29 18:50:41 -04:00
|
|
|
: m_hmdEnabled(false)
|
|
|
|
, m_waitSubmit(0)
|
2014-01-13 00:43:47 -05:00
|
|
|
, m_waitRender(0)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2014-10-29 01:08:55 -04:00
|
|
|
SortKey term;
|
|
|
|
term.reset();
|
|
|
|
term.m_program = invalidHandle;
|
|
|
|
m_sortKeys[BGFX_CONFIG_MAX_DRAW_CALLS] = term.encodeDraw();
|
|
|
|
m_sortValues[BGFX_CONFIG_MAX_DRAW_CALLS] = BGFX_CONFIG_MAX_DRAW_CALLS;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
~Frame()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void create()
|
|
|
|
{
|
|
|
|
m_constantBuffer = ConstantBuffer::create(BGFX_CONFIG_MAX_CONSTANT_BUFFER_SIZE);
|
|
|
|
reset();
|
2013-10-05 21:07:44 -04:00
|
|
|
start();
|
2013-09-21 01:13:58 -04:00
|
|
|
m_textVideoMem = BX_NEW(g_allocator, TextVideoMem);
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void destroy()
|
|
|
|
{
|
|
|
|
ConstantBuffer::destroy(m_constantBuffer);
|
2013-09-21 01:13:58 -04:00
|
|
|
BX_DELETE(g_allocator, m_textVideoMem);
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void reset()
|
2013-10-05 21:07:44 -04:00
|
|
|
{
|
|
|
|
start();
|
|
|
|
finish();
|
|
|
|
resetFreeHandles();
|
|
|
|
}
|
|
|
|
|
|
|
|
void start()
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2013-09-21 01:13:58 -04:00
|
|
|
m_flags = BGFX_STATE_NONE;
|
2014-07-20 23:27:13 -04:00
|
|
|
m_constBegin = 0;
|
|
|
|
m_constEnd = 0;
|
|
|
|
m_draw.clear();
|
|
|
|
m_compute.clear();
|
2012-08-05 17:51:49 -04:00
|
|
|
m_matrixCache.reset();
|
2013-07-27 18:27:54 -04:00
|
|
|
m_rectCache.reset();
|
2012-08-05 17:51:49 -04:00
|
|
|
m_key.reset();
|
|
|
|
m_num = 0;
|
2014-07-20 23:27:13 -04:00
|
|
|
m_numRenderItems = 0;
|
2012-08-05 17:51:49 -04:00
|
|
|
m_numDropped = 0;
|
|
|
|
m_iboffset = 0;
|
|
|
|
m_vboffset = 0;
|
2013-10-05 21:07:44 -04:00
|
|
|
m_cmdPre.start();
|
|
|
|
m_cmdPost.start();
|
2012-08-05 17:51:49 -04:00
|
|
|
m_constantBuffer->reset();
|
|
|
|
m_discard = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void finish()
|
|
|
|
{
|
|
|
|
m_cmdPre.finish();
|
|
|
|
m_cmdPost.finish();
|
2013-10-05 21:07:44 -04:00
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
m_constantBuffer->finish();
|
|
|
|
|
|
|
|
if (0 < m_numDropped)
|
|
|
|
{
|
2013-04-28 15:27:35 -04:00
|
|
|
BX_TRACE("Too many draw calls: %d, dropped %d (max: %d)"
|
|
|
|
, m_num+m_numDropped
|
|
|
|
, m_numDropped
|
|
|
|
, BGFX_CONFIG_MAX_DRAW_CALLS
|
|
|
|
);
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-11 01:41:03 -04:00
|
|
|
void setMarker(const char* _name)
|
|
|
|
{
|
|
|
|
m_constantBuffer->writeMarker(_name);
|
|
|
|
}
|
|
|
|
|
2013-03-30 01:58:50 -04:00
|
|
|
void setState(uint64_t _state, uint32_t _rgba)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
uint8_t blend = ( (_state&BGFX_STATE_BLEND_MASK)>>BGFX_STATE_BLEND_SHIFT)&0xff;
|
2013-03-30 11:47:40 -04:00
|
|
|
// transparency sort order table
|
|
|
|
m_key.m_trans = "\x0\x1\x1\x2\x2\x1\x2\x1\x2\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1"[( (blend)&0xf) + (!!blend)];
|
2014-07-20 23:27:13 -04:00
|
|
|
m_draw.m_flags = _state;
|
|
|
|
m_draw.m_rgba = _rgba;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2012-11-10 22:59:23 -05:00
|
|
|
void setStencil(uint32_t _fstencil, uint32_t _bstencil)
|
|
|
|
{
|
2014-07-20 23:27:13 -04:00
|
|
|
m_draw.m_stencil = packStencil(_fstencil, _bstencil);
|
2012-11-10 22:59:23 -05:00
|
|
|
}
|
|
|
|
|
2013-07-14 17:32:09 -04:00
|
|
|
uint16_t setScissor(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
|
|
|
|
{
|
2014-01-21 00:49:36 -05:00
|
|
|
uint16_t scissor = (uint16_t)m_rectCache.add(_x, _y, _width, _height);
|
2014-07-20 23:27:13 -04:00
|
|
|
m_draw.m_scissor = scissor;
|
2013-07-27 18:27:54 -04:00
|
|
|
return scissor;
|
2013-07-14 17:32:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void setScissor(uint16_t _cache)
|
|
|
|
{
|
2014-07-20 23:27:13 -04:00
|
|
|
m_draw.m_scissor = _cache;
|
2013-07-14 17:32:09 -04:00
|
|
|
}
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
uint32_t setTransform(const void* _mtx, uint16_t _num)
|
|
|
|
{
|
2014-07-20 23:27:13 -04:00
|
|
|
m_draw.m_matrix = m_matrixCache.add(_mtx, _num);
|
2014-10-08 23:13:57 -04:00
|
|
|
m_draw.m_num = _num;
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2014-07-20 23:27:13 -04:00
|
|
|
return m_draw.m_matrix;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2014-10-11 13:12:27 -04:00
|
|
|
uint32_t allocTransform(Transform* _transform, uint16_t _num)
|
2014-10-08 23:13:57 -04:00
|
|
|
{
|
|
|
|
uint32_t first = m_matrixCache.reserve(&_num);
|
|
|
|
_transform->data = m_matrixCache.toPtr(first);
|
|
|
|
_transform->num = _num;
|
2014-10-11 13:12:27 -04:00
|
|
|
|
|
|
|
return first;
|
2014-10-08 23:13:57 -04:00
|
|
|
}
|
|
|
|
|
2014-10-11 13:12:27 -04:00
|
|
|
void setTransform(uint32_t _cache, uint16_t _num)
|
2014-10-08 23:13:57 -04:00
|
|
|
{
|
2014-10-11 13:12:27 -04:00
|
|
|
m_draw.m_matrix = _cache;
|
2014-10-08 23:13:57 -04:00
|
|
|
m_draw.m_num = _num;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex, uint32_t _numIndices)
|
|
|
|
{
|
2014-10-08 23:13:57 -04:00
|
|
|
m_draw.m_startIndex = _firstIndex;
|
|
|
|
m_draw.m_numIndices = _numIndices;
|
2014-07-20 23:27:13 -04:00
|
|
|
m_draw.m_indexBuffer = _handle;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2015-01-24 01:18:07 -05:00
|
|
|
void setIndexBuffer(const DynamicIndexBuffer& _dib, uint32_t _firstIndex, uint32_t _numIndices)
|
|
|
|
{
|
2015-04-08 22:59:48 -04:00
|
|
|
const uint32_t indexSize = 0 == (_dib.m_flags & BGFX_BUFFER_INDEX32) ? 2 : 4;
|
2015-01-24 01:18:07 -05:00
|
|
|
m_draw.m_startIndex = _dib.m_startIndex + _firstIndex;
|
2015-04-08 22:59:48 -04:00
|
|
|
m_draw.m_numIndices = bx::uint32_min(_numIndices, _dib.m_size/indexSize);
|
2015-01-24 01:18:07 -05:00
|
|
|
m_draw.m_indexBuffer = _dib.m_handle;
|
|
|
|
}
|
|
|
|
|
2014-04-27 02:48:41 -04:00
|
|
|
void setIndexBuffer(const TransientIndexBuffer* _tib, uint32_t _firstIndex, uint32_t _numIndices)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2014-07-20 23:27:13 -04:00
|
|
|
m_draw.m_indexBuffer = _tib->handle;
|
2014-10-08 23:13:57 -04:00
|
|
|
m_draw.m_startIndex = _firstIndex;
|
|
|
|
m_draw.m_numIndices = _numIndices;
|
2012-08-05 17:51:49 -04:00
|
|
|
m_discard = 0 == _numIndices;
|
|
|
|
}
|
|
|
|
|
2014-04-11 01:09:17 -04:00
|
|
|
void setVertexBuffer(VertexBufferHandle _handle, uint32_t _startVertex, uint32_t _numVertices)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2014-10-08 23:13:57 -04:00
|
|
|
m_draw.m_startVertex = _startVertex;
|
|
|
|
m_draw.m_numVertices = _numVertices;
|
2014-07-20 23:27:13 -04:00
|
|
|
m_draw.m_vertexBuffer = _handle;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2012-11-10 22:59:23 -05:00
|
|
|
void setVertexBuffer(const DynamicVertexBuffer& _dvb, uint32_t _numVertices)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2014-10-08 23:13:57 -04:00
|
|
|
m_draw.m_startVertex = _dvb.m_startVertex;
|
|
|
|
m_draw.m_numVertices = bx::uint32_min(_dvb.m_numVertices, _numVertices);
|
2014-07-20 23:27:13 -04:00
|
|
|
m_draw.m_vertexBuffer = _dvb.m_handle;
|
2014-10-08 23:13:57 -04:00
|
|
|
m_draw.m_vertexDecl = _dvb.m_decl;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2014-04-27 02:48:41 -04:00
|
|
|
void setVertexBuffer(const TransientVertexBuffer* _tvb, uint32_t _startVertex, uint32_t _numVertices)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-01-24 01:40:04 -05:00
|
|
|
m_draw.m_startVertex = _tvb->startVertex + _startVertex;
|
2014-10-08 23:13:57 -04:00
|
|
|
m_draw.m_numVertices = bx::uint32_min(_tvb->size/_tvb->stride, _numVertices);
|
2014-07-20 23:27:13 -04:00
|
|
|
m_draw.m_vertexBuffer = _tvb->handle;
|
2014-10-08 23:13:57 -04:00
|
|
|
m_draw.m_vertexDecl = _tvb->decl;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2014-12-10 02:16:27 -05:00
|
|
|
void setInstanceDataBuffer(const InstanceDataBuffer* _idb, uint32_t _num)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2014-07-20 23:27:13 -04:00
|
|
|
m_draw.m_instanceDataOffset = _idb->offset;
|
|
|
|
m_draw.m_instanceDataStride = _idb->stride;
|
2015-04-07 20:58:14 -04:00
|
|
|
m_draw.m_numInstances = uint16_t(bx::uint32_min(_idb->num, _num) );
|
2014-07-20 23:27:13 -04:00
|
|
|
m_draw.m_instanceDataBuffer = _idb->handle;
|
2013-09-17 00:40:30 -04:00
|
|
|
BX_FREE(g_allocator, const_cast<InstanceDataBuffer*>(_idb) );
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2014-12-10 02:16:27 -05:00
|
|
|
void setInstanceDataBuffer(VertexBufferHandle _handle, uint32_t _startVertex, uint32_t _num, uint16_t _stride)
|
|
|
|
{
|
|
|
|
m_draw.m_instanceDataOffset = _startVertex * _stride;
|
|
|
|
m_draw.m_instanceDataStride = _stride;
|
2015-04-07 20:58:14 -04:00
|
|
|
m_draw.m_numInstances = uint16_t(_num);
|
2014-12-10 02:16:27 -05:00
|
|
|
m_draw.m_instanceDataBuffer = _handle;
|
|
|
|
}
|
|
|
|
|
2012-09-16 20:36:08 -04:00
|
|
|
void setProgram(ProgramHandle _handle)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2012-09-16 20:36:08 -04:00
|
|
|
m_key.m_program = _handle.idx;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2013-07-25 00:59:59 -04:00
|
|
|
void setTexture(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle, uint32_t _flags)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-01-15 00:31:08 -05:00
|
|
|
Binding& sampler = m_draw.m_bind[_stage];
|
|
|
|
sampler.m_idx = _handle.idx;
|
2015-03-26 02:33:35 -04:00
|
|
|
sampler.m_un.m_draw.m_flags = (_flags&BGFX_SAMPLER_DEFAULT_FLAGS)
|
|
|
|
? BGFX_SAMPLER_DEFAULT_FLAGS
|
|
|
|
: _flags
|
|
|
|
;
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2014-04-15 22:10:56 -04:00
|
|
|
if (isValid(_sampler)
|
|
|
|
&& (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES) ) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
uint32_t stage = _stage;
|
|
|
|
setUniform(_sampler, &stage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-10 23:38:47 -05:00
|
|
|
void setBuffer(uint8_t _stage, IndexBufferHandle _handle, Access::Enum _access)
|
2014-12-10 02:16:27 -05:00
|
|
|
{
|
2015-01-14 01:34:48 -05:00
|
|
|
Binding& bind = m_compute.m_bind[_stage];
|
2014-12-10 02:16:27 -05:00
|
|
|
bind.m_idx = _handle.idx;
|
2015-01-14 01:34:48 -05:00
|
|
|
bind.m_type = uint8_t(Binding::IndexBuffer);
|
|
|
|
bind.m_un.m_compute.m_format = 0;
|
|
|
|
bind.m_un.m_compute.m_access = uint8_t(_access);
|
|
|
|
bind.m_un.m_compute.m_mip = 0;
|
2015-01-10 23:38:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void setBuffer(uint8_t _stage, VertexBufferHandle _handle, Access::Enum _access)
|
|
|
|
{
|
2015-01-14 01:34:48 -05:00
|
|
|
Binding& bind = m_compute.m_bind[_stage];
|
|
|
|
bind.m_idx = _handle.idx;
|
|
|
|
bind.m_type = uint8_t(Binding::VertexBuffer);
|
|
|
|
bind.m_un.m_compute.m_format = 0;
|
|
|
|
bind.m_un.m_compute.m_access = uint8_t(_access);
|
|
|
|
bind.m_un.m_compute.m_mip = 0;
|
2014-12-10 02:16:27 -05:00
|
|
|
}
|
|
|
|
|
2015-02-23 20:25:06 -05:00
|
|
|
void setImage(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle, uint8_t _mip, Access::Enum _access, TextureFormat::Enum _format)
|
2014-07-20 23:27:13 -04:00
|
|
|
{
|
2015-01-14 01:34:48 -05:00
|
|
|
Binding& bind = m_compute.m_bind[_stage];
|
|
|
|
bind.m_idx = _handle.idx;
|
|
|
|
bind.m_type = uint8_t(Binding::Image);
|
|
|
|
bind.m_un.m_compute.m_format = uint8_t(_format);
|
|
|
|
bind.m_un.m_compute.m_access = uint8_t(_access);
|
|
|
|
bind.m_un.m_compute.m_mip = _mip;
|
2014-07-20 23:27:13 -04:00
|
|
|
|
|
|
|
if (isValid(_sampler)
|
|
|
|
&& (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES) ) )
|
|
|
|
{
|
|
|
|
uint32_t stage = _stage;
|
|
|
|
setUniform(_sampler, &stage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-29 22:01:29 -04:00
|
|
|
void discard()
|
|
|
|
{
|
|
|
|
m_discard = false;
|
2014-07-20 23:27:13 -04:00
|
|
|
m_draw.clear();
|
|
|
|
m_compute.clear();
|
2013-07-29 22:01:29 -04:00
|
|
|
m_flags = BGFX_STATE_NONE;
|
|
|
|
}
|
|
|
|
|
2013-10-21 23:37:02 -04:00
|
|
|
uint32_t submit(uint8_t _id, int32_t _depth);
|
2014-12-28 14:36:36 -05:00
|
|
|
uint32_t dispatch(uint8_t _id, ProgramHandle _handle, uint16_t _ngx, uint16_t _ngy, uint16_t _ngz, uint8_t _flags);
|
2012-08-05 17:51:49 -04:00
|
|
|
void sort();
|
|
|
|
|
2013-05-03 01:16:33 -04:00
|
|
|
bool checkAvailTransientIndexBuffer(uint32_t _num)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
uint32_t offset = m_iboffset;
|
|
|
|
uint32_t iboffset = offset + _num*sizeof(uint16_t);
|
2013-08-04 01:15:13 -04:00
|
|
|
iboffset = bx::uint32_min(iboffset, BGFX_CONFIG_TRANSIENT_INDEX_BUFFER_SIZE);
|
2012-08-05 17:51:49 -04:00
|
|
|
uint32_t num = (iboffset-offset)/sizeof(uint16_t);
|
|
|
|
return num == _num;
|
|
|
|
}
|
|
|
|
|
2013-05-03 01:16:33 -04:00
|
|
|
uint32_t allocTransientIndexBuffer(uint32_t& _num)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-04-14 20:01:35 -04:00
|
|
|
uint32_t offset = bx::strideAlign(m_iboffset, sizeof(uint16_t) );
|
|
|
|
m_iboffset = offset + _num*sizeof(uint16_t);
|
2013-08-04 01:15:13 -04:00
|
|
|
m_iboffset = bx::uint32_min(m_iboffset, BGFX_CONFIG_TRANSIENT_INDEX_BUFFER_SIZE);
|
2013-05-03 01:16:33 -04:00
|
|
|
_num = (m_iboffset-offset)/sizeof(uint16_t);
|
2012-08-05 17:51:49 -04:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2013-05-03 01:16:33 -04:00
|
|
|
bool checkAvailTransientVertexBuffer(uint32_t _num, uint16_t _stride)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-04-12 20:31:33 -04:00
|
|
|
uint32_t offset = bx::strideAlign(m_vboffset, _stride);
|
2012-08-05 17:51:49 -04:00
|
|
|
uint32_t vboffset = offset + _num * _stride;
|
2013-08-04 01:15:13 -04:00
|
|
|
vboffset = bx::uint32_min(vboffset, BGFX_CONFIG_TRANSIENT_VERTEX_BUFFER_SIZE);
|
2012-08-05 17:51:49 -04:00
|
|
|
uint32_t num = (vboffset-offset)/_stride;
|
|
|
|
return num == _num;
|
|
|
|
}
|
|
|
|
|
2013-05-03 01:16:33 -04:00
|
|
|
uint32_t allocTransientVertexBuffer(uint32_t& _num, uint16_t _stride)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-04-12 20:31:33 -04:00
|
|
|
uint32_t offset = bx::strideAlign(m_vboffset, _stride);
|
2015-04-14 20:01:35 -04:00
|
|
|
m_vboffset = offset + _num * _stride;
|
2013-08-04 01:15:13 -04:00
|
|
|
m_vboffset = bx::uint32_min(m_vboffset, BGFX_CONFIG_TRANSIENT_VERTEX_BUFFER_SIZE);
|
2013-05-03 01:16:33 -04:00
|
|
|
_num = (m_vboffset-offset)/_stride;
|
2012-08-05 17:51:49 -04:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2014-04-15 22:10:56 -04:00
|
|
|
void writeUniform(UniformType::Enum _type, UniformHandle _handle, const void* _value, uint16_t _num)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
m_constantBuffer->writeUniform(_type, _handle.idx, _value, _num);
|
|
|
|
}
|
|
|
|
|
|
|
|
void free(IndexBufferHandle _handle)
|
|
|
|
{
|
|
|
|
m_freeIndexBufferHandle[m_numFreeIndexBufferHandles] = _handle;
|
|
|
|
++m_numFreeIndexBufferHandles;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free(VertexDeclHandle _handle)
|
|
|
|
{
|
|
|
|
m_freeVertexDeclHandle[m_numFreeVertexDeclHandles] = _handle;
|
|
|
|
++m_numFreeVertexDeclHandles;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free(VertexBufferHandle _handle)
|
|
|
|
{
|
|
|
|
m_freeVertexBufferHandle[m_numFreeVertexBufferHandles] = _handle;
|
|
|
|
++m_numFreeVertexBufferHandles;
|
|
|
|
}
|
|
|
|
|
2014-03-29 22:42:57 -04:00
|
|
|
void free(ShaderHandle _handle)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2014-03-29 22:42:57 -04:00
|
|
|
m_freeShaderHandle[m_numFreeShaderHandles] = _handle;
|
|
|
|
++m_numFreeShaderHandles;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2012-09-16 20:36:08 -04:00
|
|
|
void free(ProgramHandle _handle)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2012-09-16 20:36:08 -04:00
|
|
|
m_freeProgramHandle[m_numFreeProgramHandles] = _handle;
|
|
|
|
++m_numFreeProgramHandles;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void free(TextureHandle _handle)
|
|
|
|
{
|
|
|
|
m_freeTextureHandle[m_numFreeTextureHandles] = _handle;
|
|
|
|
++m_numFreeTextureHandles;
|
|
|
|
}
|
|
|
|
|
2014-02-06 02:07:11 -05:00
|
|
|
void free(FrameBufferHandle _handle)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2014-02-06 02:07:11 -05:00
|
|
|
m_freeFrameBufferHandle[m_numFreeFrameBufferHandles] = _handle;
|
|
|
|
++m_numFreeFrameBufferHandles;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void free(UniformHandle _handle)
|
|
|
|
{
|
|
|
|
m_freeUniformHandle[m_numFreeUniformHandles] = _handle;
|
|
|
|
++m_numFreeUniformHandles;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resetFreeHandles()
|
|
|
|
{
|
2014-09-07 20:17:38 -04:00
|
|
|
m_numFreeIndexBufferHandles = 0;
|
|
|
|
m_numFreeVertexDeclHandles = 0;
|
2012-08-05 17:51:49 -04:00
|
|
|
m_numFreeVertexBufferHandles = 0;
|
2014-09-07 20:17:38 -04:00
|
|
|
m_numFreeShaderHandles = 0;
|
|
|
|
m_numFreeProgramHandles = 0;
|
|
|
|
m_numFreeTextureHandles = 0;
|
|
|
|
m_numFreeFrameBufferHandles = 0;
|
|
|
|
m_numFreeUniformHandles = 0;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
SortKey m_key;
|
|
|
|
|
2015-02-21 18:40:51 -05:00
|
|
|
uint8_t m_viewRemap[BGFX_CONFIG_MAX_VIEWS];
|
2014-02-06 02:07:11 -05:00
|
|
|
FrameBufferHandle m_fb[BGFX_CONFIG_MAX_VIEWS];
|
2012-08-05 17:51:49 -04:00
|
|
|
Clear m_clear[BGFX_CONFIG_MAX_VIEWS];
|
2014-09-01 14:24:51 -04:00
|
|
|
float m_clearColor[BGFX_CONFIG_MAX_CLEAR_COLOR_PALETTE][4];
|
2012-08-05 17:51:49 -04:00
|
|
|
Rect m_rect[BGFX_CONFIG_MAX_VIEWS];
|
2013-07-14 17:32:09 -04:00
|
|
|
Rect m_scissor[BGFX_CONFIG_MAX_VIEWS];
|
2012-08-05 17:51:49 -04:00
|
|
|
Matrix4 m_view[BGFX_CONFIG_MAX_VIEWS];
|
2014-10-29 01:08:55 -04:00
|
|
|
Matrix4 m_proj[2][BGFX_CONFIG_MAX_VIEWS];
|
|
|
|
uint8_t m_viewFlags[BGFX_CONFIG_MAX_VIEWS];
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2014-10-29 01:08:55 -04:00
|
|
|
uint64_t m_sortKeys[BGFX_CONFIG_MAX_DRAW_CALLS+1];
|
2015-04-09 22:38:51 -04:00
|
|
|
RenderItemCount m_sortValues[BGFX_CONFIG_MAX_DRAW_CALLS+1];
|
2014-10-29 01:08:55 -04:00
|
|
|
RenderItem m_renderItem[BGFX_CONFIG_MAX_DRAW_CALLS+1];
|
2014-07-20 23:27:13 -04:00
|
|
|
RenderDraw m_draw;
|
|
|
|
RenderCompute m_compute;
|
2012-08-05 17:51:49 -04:00
|
|
|
uint64_t m_flags;
|
2014-07-20 23:27:13 -04:00
|
|
|
uint32_t m_constBegin;
|
|
|
|
uint32_t m_constEnd;
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
ConstantBuffer* m_constantBuffer;
|
|
|
|
|
2015-04-09 22:38:51 -04:00
|
|
|
RenderItemCount m_num;
|
|
|
|
RenderItemCount m_numRenderItems;
|
|
|
|
RenderItemCount m_numDropped;
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
MatrixCache m_matrixCache;
|
2013-07-27 18:27:54 -04:00
|
|
|
RectCache m_rectCache;
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
uint32_t m_iboffset;
|
|
|
|
uint32_t m_vboffset;
|
|
|
|
TransientIndexBuffer* m_transientIb;
|
|
|
|
TransientVertexBuffer* m_transientVb;
|
|
|
|
|
|
|
|
Resolution m_resolution;
|
|
|
|
uint32_t m_debug;
|
|
|
|
|
|
|
|
CommandBuffer m_cmdPre;
|
|
|
|
CommandBuffer m_cmdPost;
|
|
|
|
|
|
|
|
uint16_t m_numFreeIndexBufferHandles;
|
|
|
|
uint16_t m_numFreeVertexDeclHandles;
|
|
|
|
uint16_t m_numFreeVertexBufferHandles;
|
2014-03-29 22:42:57 -04:00
|
|
|
uint16_t m_numFreeShaderHandles;
|
2012-09-16 20:36:08 -04:00
|
|
|
uint16_t m_numFreeProgramHandles;
|
2012-08-05 17:51:49 -04:00
|
|
|
uint16_t m_numFreeTextureHandles;
|
2014-02-06 02:07:11 -05:00
|
|
|
uint16_t m_numFreeFrameBufferHandles;
|
2012-08-05 17:51:49 -04:00
|
|
|
uint16_t m_numFreeUniformHandles;
|
2014-09-07 20:17:38 -04:00
|
|
|
uint16_t m_numFreeWindowHandles;
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
IndexBufferHandle m_freeIndexBufferHandle[BGFX_CONFIG_MAX_INDEX_BUFFERS];
|
|
|
|
VertexDeclHandle m_freeVertexDeclHandle[BGFX_CONFIG_MAX_VERTEX_DECLS];
|
|
|
|
VertexBufferHandle m_freeVertexBufferHandle[BGFX_CONFIG_MAX_VERTEX_BUFFERS];
|
2014-03-29 22:42:57 -04:00
|
|
|
ShaderHandle m_freeShaderHandle[BGFX_CONFIG_MAX_SHADERS];
|
2012-09-16 20:36:08 -04:00
|
|
|
ProgramHandle m_freeProgramHandle[BGFX_CONFIG_MAX_PROGRAMS];
|
2012-08-05 17:51:49 -04:00
|
|
|
TextureHandle m_freeTextureHandle[BGFX_CONFIG_MAX_TEXTURES];
|
2014-02-06 02:07:11 -05:00
|
|
|
FrameBufferHandle m_freeFrameBufferHandle[BGFX_CONFIG_MAX_FRAME_BUFFERS];
|
2012-08-05 17:51:49 -04:00
|
|
|
UniformHandle m_freeUniformHandle[BGFX_CONFIG_MAX_UNIFORMS];
|
|
|
|
TextVideoMem* m_textVideoMem;
|
2014-10-29 01:08:55 -04:00
|
|
|
HMD m_hmd;
|
|
|
|
bool m_hmdEnabled;
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
int64_t m_waitSubmit;
|
|
|
|
int64_t m_waitRender;
|
|
|
|
|
|
|
|
bool m_discard;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct VertexDeclRef
|
|
|
|
{
|
|
|
|
VertexDeclRef()
|
2013-03-09 17:57:53 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void init()
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
memset(m_vertexDeclRef, 0, sizeof(m_vertexDeclRef) );
|
|
|
|
memset(m_vertexBufferRef, 0xff, sizeof(m_vertexBufferRef) );
|
|
|
|
}
|
|
|
|
|
2013-09-21 01:13:58 -04:00
|
|
|
template <uint16_t MaxHandlesT>
|
|
|
|
void shutdown(bx::HandleAllocT<MaxHandlesT>& _handleAlloc)
|
2013-03-09 17:57:53 -05:00
|
|
|
{
|
|
|
|
for (VertexDeclMap::iterator it = m_vertexDeclMap.begin(), itEnd = m_vertexDeclMap.end(); it != itEnd; ++it)
|
|
|
|
{
|
|
|
|
_handleAlloc.free(it->second.idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_vertexDeclMap.clear();
|
|
|
|
}
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
VertexDeclHandle find(uint32_t _hash)
|
|
|
|
{
|
|
|
|
VertexDeclMap::const_iterator it = m_vertexDeclMap.find(_hash);
|
|
|
|
if (it != m_vertexDeclMap.end() )
|
|
|
|
{
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
VertexDeclHandle result = BGFX_INVALID_HANDLE;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(VertexBufferHandle _handle, VertexDeclHandle _declHandle, uint32_t _hash)
|
|
|
|
{
|
|
|
|
m_vertexBufferRef[_handle.idx] = _declHandle;
|
|
|
|
m_vertexDeclRef[_declHandle.idx]++;
|
|
|
|
m_vertexDeclMap.insert(stl::make_pair(_hash, _declHandle) );
|
|
|
|
}
|
|
|
|
|
|
|
|
VertexDeclHandle release(VertexBufferHandle _handle)
|
|
|
|
{
|
|
|
|
VertexDeclHandle declHandle = m_vertexBufferRef[_handle.idx];
|
2013-10-03 23:36:40 -04:00
|
|
|
if (isValid(declHandle) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2013-10-03 23:36:40 -04:00
|
|
|
m_vertexDeclRef[declHandle.idx]--;
|
|
|
|
|
|
|
|
if (0 != m_vertexDeclRef[declHandle.idx])
|
|
|
|
{
|
|
|
|
VertexDeclHandle invalid = BGFX_INVALID_HANDLE;
|
|
|
|
return invalid;
|
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return declHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef stl::unordered_map<uint32_t, VertexDeclHandle> VertexDeclMap;
|
|
|
|
VertexDeclMap m_vertexDeclMap;
|
|
|
|
uint16_t m_vertexDeclRef[BGFX_CONFIG_MAX_VERTEX_DECLS];
|
|
|
|
VertexDeclHandle m_vertexBufferRef[BGFX_CONFIG_MAX_VERTEX_BUFFERS];
|
|
|
|
};
|
|
|
|
|
|
|
|
// First-fit non-local allocator.
|
|
|
|
class NonLocalAllocator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static const uint64_t invalidBlock = UINT64_MAX;
|
|
|
|
|
|
|
|
NonLocalAllocator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~NonLocalAllocator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
m_free.clear();
|
|
|
|
m_used.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(uint64_t _ptr, uint32_t _size)
|
|
|
|
{
|
|
|
|
m_free.push_back(Free(_ptr, _size) );
|
|
|
|
}
|
|
|
|
|
2015-02-22 00:51:06 -05:00
|
|
|
uint64_t remove()
|
|
|
|
{
|
|
|
|
BX_CHECK(0 == m_used.size(), "");
|
|
|
|
|
|
|
|
if (0 < m_free.size() )
|
|
|
|
{
|
2015-04-04 22:10:35 -04:00
|
|
|
Free freeBlock = m_free.front();
|
2015-02-22 00:51:06 -05:00
|
|
|
m_free.pop_front();
|
2015-04-04 22:10:35 -04:00
|
|
|
return freeBlock.m_ptr;
|
2015-02-22 00:51:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
uint64_t alloc(uint32_t _size)
|
|
|
|
{
|
|
|
|
for (FreeList::iterator it = m_free.begin(), itEnd = m_free.end(); it != itEnd; ++it)
|
|
|
|
{
|
|
|
|
if (it->m_size >= _size)
|
|
|
|
{
|
|
|
|
uint64_t ptr = it->m_ptr;
|
|
|
|
|
|
|
|
m_used.insert(stl::make_pair(ptr, _size) );
|
|
|
|
|
|
|
|
if (it->m_size != _size)
|
|
|
|
{
|
|
|
|
it->m_size -= _size;
|
2014-12-10 02:16:27 -05:00
|
|
|
it->m_ptr += _size;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_free.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// there is no block large enough.
|
|
|
|
return invalidBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free(uint64_t _block)
|
|
|
|
{
|
|
|
|
UsedList::iterator it = m_used.find(_block);
|
|
|
|
if (it != m_used.end() )
|
|
|
|
{
|
|
|
|
m_free.push_front(Free(it->first, it->second) );
|
|
|
|
m_used.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-22 00:51:06 -05:00
|
|
|
bool compact()
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
m_free.sort();
|
|
|
|
|
|
|
|
for (FreeList::iterator it = m_free.begin(), next = it, itEnd = m_free.end(); next != itEnd;)
|
|
|
|
{
|
|
|
|
if ( (it->m_ptr + it->m_size) == next->m_ptr)
|
|
|
|
{
|
|
|
|
it->m_size += next->m_size;
|
|
|
|
next = m_free.erase(next);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
it = next;
|
|
|
|
++next;
|
|
|
|
}
|
|
|
|
}
|
2015-02-22 00:51:06 -05:00
|
|
|
|
|
|
|
return 0 == m_used.size();
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Free
|
|
|
|
{
|
|
|
|
Free(uint64_t _ptr, uint32_t _size)
|
|
|
|
: m_ptr(_ptr)
|
|
|
|
, m_size(_size)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const Free& rhs) const
|
|
|
|
{
|
|
|
|
return m_ptr < rhs.m_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t m_ptr;
|
|
|
|
uint32_t m_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::list<Free> FreeList;
|
|
|
|
FreeList m_free;
|
|
|
|
|
|
|
|
typedef stl::unordered_map<uint64_t, uint32_t> UsedList;
|
|
|
|
UsedList m_used;
|
|
|
|
};
|
|
|
|
|
2014-05-26 17:09:26 -04:00
|
|
|
struct BX_NO_VTABLE RendererContextI
|
|
|
|
{
|
|
|
|
virtual ~RendererContextI() = 0;
|
|
|
|
virtual RendererType::Enum getRendererType() const = 0;
|
|
|
|
virtual const char* getRendererName() const = 0;
|
|
|
|
virtual void flip() = 0;
|
2015-01-10 23:56:38 -05:00
|
|
|
virtual void createIndexBuffer(IndexBufferHandle _handle, Memory* _mem, uint8_t _flags) = 0;
|
2014-05-26 17:09:26 -04:00
|
|
|
virtual void destroyIndexBuffer(IndexBufferHandle _handle) = 0;
|
|
|
|
virtual void createVertexDecl(VertexDeclHandle _handle, const VertexDecl& _decl) = 0;
|
|
|
|
virtual void destroyVertexDecl(VertexDeclHandle _handle) = 0;
|
2014-12-10 02:16:27 -05:00
|
|
|
virtual void createVertexBuffer(VertexBufferHandle _handle, Memory* _mem, VertexDeclHandle _declHandle, uint8_t _flags) = 0;
|
2014-05-26 17:09:26 -04:00
|
|
|
virtual void destroyVertexBuffer(VertexBufferHandle _handle) = 0;
|
2015-01-10 23:38:47 -05:00
|
|
|
virtual void createDynamicIndexBuffer(IndexBufferHandle _handle, uint32_t _size, uint8_t _flags) = 0;
|
2014-05-26 17:09:26 -04:00
|
|
|
virtual void updateDynamicIndexBuffer(IndexBufferHandle _handle, uint32_t _offset, uint32_t _size, Memory* _mem) = 0;
|
|
|
|
virtual void destroyDynamicIndexBuffer(IndexBufferHandle _handle) = 0;
|
2014-12-10 02:16:27 -05:00
|
|
|
virtual void createDynamicVertexBuffer(VertexBufferHandle _handle, uint32_t _size, uint8_t _flags) = 0;
|
2014-05-26 17:09:26 -04:00
|
|
|
virtual void updateDynamicVertexBuffer(VertexBufferHandle _handle, uint32_t _offset, uint32_t _size, Memory* _mem) = 0;
|
|
|
|
virtual void destroyDynamicVertexBuffer(VertexBufferHandle _handle) = 0;
|
|
|
|
virtual void createShader(ShaderHandle _handle, Memory* _mem) = 0;
|
|
|
|
virtual void destroyShader(ShaderHandle _handle) = 0;
|
|
|
|
virtual void createProgram(ProgramHandle _handle, ShaderHandle _vsh, ShaderHandle _fsh) = 0;
|
|
|
|
virtual void destroyProgram(ProgramHandle _handle) = 0;
|
|
|
|
virtual void createTexture(TextureHandle _handle, Memory* _mem, uint32_t _flags, uint8_t _skip) = 0;
|
|
|
|
virtual void updateTextureBegin(TextureHandle _handle, uint8_t _side, uint8_t _mip) = 0;
|
|
|
|
virtual void updateTexture(TextureHandle _handle, uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem) = 0;
|
|
|
|
virtual void updateTextureEnd() = 0;
|
2015-04-13 21:39:38 -04:00
|
|
|
virtual void resizeTexture(TextureHandle _handle, uint16_t _width, uint16_t _height) = 0;
|
2014-05-26 17:09:26 -04:00
|
|
|
virtual void destroyTexture(TextureHandle _handle) = 0;
|
|
|
|
virtual void createFrameBuffer(FrameBufferHandle _handle, uint8_t _num, const TextureHandle* _textureHandles) = 0;
|
2014-09-07 20:17:38 -04:00
|
|
|
virtual void createFrameBuffer(FrameBufferHandle _handle, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _depthFormat) = 0;
|
2014-05-26 17:09:26 -04:00
|
|
|
virtual void destroyFrameBuffer(FrameBufferHandle _handle) = 0;
|
|
|
|
virtual void createUniform(UniformHandle _handle, UniformType::Enum _type, uint16_t _num, const char* _name) = 0;
|
|
|
|
virtual void destroyUniform(UniformHandle _handle) = 0;
|
|
|
|
virtual void saveScreenShot(const char* _filePath) = 0;
|
|
|
|
virtual void updateViewName(uint8_t _id, const char* _name) = 0;
|
|
|
|
virtual void updateUniform(uint16_t _loc, const void* _data, uint32_t _size) = 0;
|
|
|
|
virtual void setMarker(const char* _marker, uint32_t _size) = 0;
|
|
|
|
virtual void submit(Frame* _render, ClearQuad& _clearQuad, TextVideoMemBlitter& _textVideoMemBlitter) = 0;
|
|
|
|
virtual void blitSetup(TextVideoMemBlitter& _blitter) = 0;
|
|
|
|
virtual void blitRender(TextVideoMemBlitter& _blitter, uint32_t _numIndices) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline RendererContextI::~RendererContextI()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void rendererUpdateUniforms(RendererContextI* _renderCtx, ConstantBuffer* _constantBuffer, uint32_t _begin, uint32_t _end);
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
#if BGFX_CONFIG_DEBUG
|
2014-05-26 17:09:26 -04:00
|
|
|
# define BGFX_API_FUNC(_func) BX_NO_INLINE _func
|
2013-10-05 02:45:05 -04:00
|
|
|
#else
|
2014-05-26 17:09:26 -04:00
|
|
|
# define BGFX_API_FUNC(_func) _func
|
2013-10-05 02:45:05 -04:00
|
|
|
#endif // BGFX_CONFIG_DEBUG
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
struct Context
|
|
|
|
{
|
|
|
|
Context()
|
2012-12-04 23:07:14 -05:00
|
|
|
: m_render(&m_frame[0])
|
2012-08-05 17:51:49 -04:00
|
|
|
, m_submit(&m_frame[1])
|
2013-07-13 02:28:25 -04:00
|
|
|
, m_numFreeDynamicIndexBufferHandles(0)
|
|
|
|
, m_numFreeDynamicVertexBufferHandles(0)
|
2014-09-01 14:24:51 -04:00
|
|
|
, m_clearColorDirty(0)
|
2014-01-21 00:49:36 -05:00
|
|
|
, m_instBufferCount(0)
|
2012-08-05 17:51:49 -04:00
|
|
|
, m_frames(0)
|
|
|
|
, m_debug(BGFX_DEBUG_NONE)
|
2014-10-22 02:38:16 -04:00
|
|
|
, m_renderCtx(NULL)
|
2012-08-05 17:51:49 -04:00
|
|
|
, m_rendererInitialized(false)
|
|
|
|
, m_exit(false)
|
2015-04-15 14:49:20 -04:00
|
|
|
, m_flipAfterRender(false)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~Context()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-04-06 21:22:15 -04:00
|
|
|
static int32_t renderThread(void* /*_userData*/)
|
2013-01-15 23:37:07 -05:00
|
|
|
{
|
2013-04-21 02:13:44 -04:00
|
|
|
BX_TRACE("render thread start");
|
2015-04-06 21:22:15 -04:00
|
|
|
while (RenderFrame::Exiting != bgfx::renderFrame() ) {};
|
2013-04-21 02:13:44 -04:00
|
|
|
BX_TRACE("render thread exit");
|
2012-12-02 21:29:28 -05:00
|
|
|
return EXIT_SUCCESS;
|
2013-01-15 23:37:07 -05:00
|
|
|
}
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
// game thread
|
2014-05-26 17:09:26 -04:00
|
|
|
void init(RendererType::Enum _type);
|
2012-08-05 17:51:49 -04:00
|
|
|
void shutdown();
|
|
|
|
|
|
|
|
CommandBuffer& getCommandBuffer(CommandBuffer::Enum _cmd)
|
|
|
|
{
|
|
|
|
CommandBuffer& cmdbuf = _cmd < CommandBuffer::End ? m_submit->m_cmdPre : m_submit->m_cmdPost;
|
|
|
|
uint8_t cmd = (uint8_t)_cmd;
|
|
|
|
cmdbuf.write(cmd);
|
|
|
|
return cmdbuf;
|
|
|
|
}
|
|
|
|
|
2013-10-05 21:07:44 -04:00
|
|
|
BGFX_API_FUNC(void reset(uint32_t _width, uint32_t _height, uint32_t _flags) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2013-01-29 22:23:56 -05:00
|
|
|
BX_WARN(0 != _width && 0 != _height, "Frame buffer resolution width or height cannot be 0 (width %d, height %d).", _width, _height);
|
2014-09-23 23:35:39 -04:00
|
|
|
m_resolution.m_width = bx::uint32_max(1, _width);
|
2013-08-04 01:15:13 -04:00
|
|
|
m_resolution.m_height = bx::uint32_max(1, _height);
|
2014-09-23 23:35:39 -04:00
|
|
|
m_resolution.m_flags = _flags;
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2015-04-15 14:49:20 -04:00
|
|
|
m_flipAfterRender = !!(_flags & BGFX_RESET_FLIP_AFTER_RENDER);
|
2015-04-14 23:03:05 -04:00
|
|
|
|
2014-02-06 02:07:11 -05:00
|
|
|
memset(m_fb, 0xff, sizeof(m_fb) );
|
2015-04-13 21:39:38 -04:00
|
|
|
|
|
|
|
for (uint16_t ii = 0, num = m_textureHandle.getNumHandles(); ii < num; ++ii)
|
|
|
|
{
|
|
|
|
uint16_t textureIdx = m_textureHandle.getHandleAt(ii);
|
|
|
|
const TextureRef& textureRef = m_textureRef[textureIdx];
|
|
|
|
if (BackbufferRatio::None != textureRef.m_bbRatio)
|
|
|
|
{
|
|
|
|
TextureHandle handle = { textureIdx };
|
2015-04-14 01:13:16 -04:00
|
|
|
resizeTexture(handle
|
|
|
|
, uint16_t(m_resolution.m_width)
|
|
|
|
, uint16_t(m_resolution.m_height)
|
|
|
|
);
|
2015-04-13 21:39:38 -04:00
|
|
|
}
|
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void setDebug(uint32_t _debug) )
|
|
|
|
{
|
|
|
|
m_debug = _debug;
|
|
|
|
}
|
|
|
|
|
|
|
|
BGFX_API_FUNC(void dbgTextClear(uint8_t _attr, bool _small) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2013-02-13 02:54:32 -05:00
|
|
|
m_submit->m_textVideoMem->resize(_small, (uint16_t)m_resolution.m_width, (uint16_t)m_resolution.m_height);
|
2012-08-05 17:51:49 -04:00
|
|
|
m_submit->m_textVideoMem->clear(_attr);
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void dbgTextPrintfVargs(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, va_list _argList) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
m_submit->m_textVideoMem->printfVargs(_x, _y, _attr, _format, _argList);
|
|
|
|
}
|
|
|
|
|
2014-12-04 23:32:19 -05:00
|
|
|
BGFX_API_FUNC(void dbgTextImage(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const void* _data, uint16_t _pitch) )
|
|
|
|
{
|
|
|
|
m_submit->m_textVideoMem->image(_x, _y, _width, _height, _data, _pitch);
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:08:55 -04:00
|
|
|
BGFX_API_FUNC(const HMD* getHMD() )
|
|
|
|
{
|
|
|
|
if (m_submit->m_hmdEnabled)
|
|
|
|
{
|
|
|
|
return &m_submit->m_hmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-01-10 23:56:38 -05:00
|
|
|
BGFX_API_FUNC(IndexBufferHandle createIndexBuffer(const Memory* _mem, uint8_t _flags) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
IndexBufferHandle handle = { m_indexBufferHandle.alloc() };
|
2013-01-19 13:11:42 -05:00
|
|
|
|
2013-09-30 00:33:50 -04:00
|
|
|
BX_WARN(isValid(handle), "Failed to allocate index buffer handle.");
|
|
|
|
if (isValid(handle) )
|
2013-01-19 13:11:42 -05:00
|
|
|
{
|
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateIndexBuffer);
|
|
|
|
cmdbuf.write(handle);
|
|
|
|
cmdbuf.write(_mem);
|
2015-01-10 23:56:38 -05:00
|
|
|
cmdbuf.write(_flags);
|
2013-01-19 13:11:42 -05:00
|
|
|
}
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void destroyIndexBuffer(IndexBufferHandle _handle) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("destroyIndexBuffer", m_indexBufferHandle, _handle);
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::DestroyIndexBuffer);
|
|
|
|
cmdbuf.write(_handle);
|
|
|
|
m_submit->free(_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
VertexDeclHandle findVertexDecl(const VertexDecl& _decl)
|
|
|
|
{
|
|
|
|
VertexDeclHandle declHandle = m_declRef.find(_decl.m_hash);
|
|
|
|
|
2013-09-30 00:33:50 -04:00
|
|
|
if (!isValid(declHandle) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
VertexDeclHandle temp = { m_vertexDeclHandle.alloc() };
|
|
|
|
declHandle = temp;
|
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateVertexDecl);
|
|
|
|
cmdbuf.write(declHandle);
|
|
|
|
cmdbuf.write(_decl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return declHandle;
|
|
|
|
}
|
|
|
|
|
2014-12-10 02:16:27 -05:00
|
|
|
BGFX_API_FUNC(VertexBufferHandle createVertexBuffer(const Memory* _mem, const VertexDecl& _decl, uint8_t flags) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
VertexBufferHandle handle = { m_vertexBufferHandle.alloc() };
|
|
|
|
|
2013-09-30 00:33:50 -04:00
|
|
|
BX_WARN(isValid(handle), "Failed to allocate vertex buffer handle.");
|
|
|
|
if (isValid(handle) )
|
2013-01-19 13:11:42 -05:00
|
|
|
{
|
|
|
|
VertexDeclHandle declHandle = findVertexDecl(_decl);
|
|
|
|
m_declRef.add(handle, declHandle, _decl.m_hash);
|
|
|
|
|
2014-12-10 02:16:27 -05:00
|
|
|
m_vertexBuffers[handle.idx].m_stride = _decl.m_stride;
|
|
|
|
|
2013-01-19 13:11:42 -05:00
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateVertexBuffer);
|
|
|
|
cmdbuf.write(handle);
|
|
|
|
cmdbuf.write(_mem);
|
|
|
|
cmdbuf.write(declHandle);
|
2014-12-10 02:16:27 -05:00
|
|
|
cmdbuf.write(flags);
|
2013-01-19 13:11:42 -05:00
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void destroyVertexBuffer(VertexBufferHandle _handle) )
|
2013-10-03 23:36:40 -04:00
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("destroyVertexBuffer", m_vertexBufferHandle, _handle);
|
|
|
|
|
2013-10-03 23:36:40 -04:00
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::DestroyVertexBuffer);
|
|
|
|
cmdbuf.write(_handle);
|
|
|
|
m_submit->free(_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroyVertexBufferInternal(VertexBufferHandle _handle)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
VertexDeclHandle declHandle = m_declRef.release(_handle);
|
2013-09-30 00:33:50 -04:00
|
|
|
if (isValid(declHandle) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::DestroyVertexDecl);
|
|
|
|
cmdbuf.write(declHandle);
|
|
|
|
}
|
|
|
|
|
2013-10-03 23:36:40 -04:00
|
|
|
m_vertexBufferHandle.free(_handle.idx);
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2015-01-28 20:56:29 -05:00
|
|
|
uint64_t allocDynamicIndexBuffer(uint32_t _size, uint8_t _flags)
|
|
|
|
{
|
|
|
|
uint64_t ptr = m_dynIndexBufferAllocator.alloc(_size);
|
|
|
|
if (ptr == NonLocalAllocator::invalidBlock)
|
|
|
|
{
|
|
|
|
IndexBufferHandle indexBufferHandle = { m_indexBufferHandle.alloc() };
|
|
|
|
BX_WARN(isValid(indexBufferHandle), "Failed to allocate index buffer handle.");
|
|
|
|
if (!isValid(indexBufferHandle))
|
|
|
|
{
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateDynamicIndexBuffer);
|
|
|
|
cmdbuf.write(indexBufferHandle);
|
|
|
|
cmdbuf.write(BGFX_CONFIG_DYNAMIC_INDEX_BUFFER_SIZE);
|
|
|
|
cmdbuf.write(_flags);
|
|
|
|
|
|
|
|
m_dynIndexBufferAllocator.add(uint64_t(indexBufferHandle.idx) << 32, BGFX_CONFIG_DYNAMIC_INDEX_BUFFER_SIZE);
|
|
|
|
ptr = m_dynIndexBufferAllocator.alloc(_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2015-01-24 01:18:07 -05:00
|
|
|
BGFX_API_FUNC(DynamicIndexBufferHandle createDynamicIndexBuffer(uint32_t _num, uint8_t _flags) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
DynamicIndexBufferHandle handle = BGFX_INVALID_HANDLE;
|
2015-04-08 22:59:48 -04:00
|
|
|
const uint32_t indexSize = 0 == (_flags & BGFX_BUFFER_INDEX32) ? 2 : 4;
|
2015-04-14 20:01:35 -04:00
|
|
|
uint32_t size = BX_ALIGN_16(_num*indexSize);
|
2015-01-10 23:38:47 -05:00
|
|
|
|
|
|
|
uint64_t ptr = 0;
|
2015-01-28 20:56:29 -05:00
|
|
|
if (0 != (_flags & BGFX_BUFFER_COMPUTE_WRITE) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-01-28 20:56:29 -05:00
|
|
|
IndexBufferHandle indexBufferHandle = { m_indexBufferHandle.alloc() };
|
|
|
|
if (!isValid(indexBufferHandle))
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2015-01-28 20:56:29 -05:00
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateDynamicIndexBuffer);
|
|
|
|
cmdbuf.write(indexBufferHandle);
|
2015-01-10 23:38:47 -05:00
|
|
|
cmdbuf.write(size);
|
|
|
|
cmdbuf.write(_flags);
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2015-01-28 20:56:29 -05:00
|
|
|
ptr = uint64_t(indexBufferHandle.idx) << 32;
|
2015-01-10 23:38:47 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-28 20:56:29 -05:00
|
|
|
ptr = allocDynamicIndexBuffer(size, _flags);
|
2015-01-10 23:38:47 -05:00
|
|
|
if (ptr == NonLocalAllocator::invalidBlock)
|
|
|
|
{
|
2015-01-28 20:56:29 -05:00
|
|
|
return handle;
|
2015-01-10 23:38:47 -05:00
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
handle.idx = m_dynamicIndexBufferHandle.alloc();
|
2013-09-30 00:33:50 -04:00
|
|
|
BX_WARN(isValid(handle), "Failed to allocate dynamic index buffer handle.");
|
|
|
|
if (!isValid(handle) )
|
2013-04-07 16:44:10 -04:00
|
|
|
{
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
DynamicIndexBuffer& dib = m_dynamicIndexBuffers[handle.idx];
|
|
|
|
dib.m_handle.idx = uint16_t(ptr>>32);
|
2015-01-24 01:18:07 -05:00
|
|
|
dib.m_offset = uint32_t(ptr);
|
|
|
|
dib.m_size = size;
|
2015-04-12 20:31:33 -04:00
|
|
|
dib.m_startIndex = bx::strideAlign(dib.m_offset, indexSize)/indexSize;
|
2015-01-28 20:56:29 -05:00
|
|
|
dib.m_flags = _flags;
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2015-01-28 20:56:29 -05:00
|
|
|
BGFX_API_FUNC(DynamicIndexBufferHandle createDynamicIndexBuffer(const Memory* _mem, uint8_t _flags) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-01-28 20:56:29 -05:00
|
|
|
BX_CHECK(0 == (_flags & BGFX_BUFFER_COMPUTE_READ_WRITE), "Cannot initialize compute buffer from CPU.");
|
2015-04-08 22:59:48 -04:00
|
|
|
const uint32_t indexSize = 0 == (_flags & BGFX_BUFFER_INDEX32) ? 2 : 4;
|
|
|
|
DynamicIndexBufferHandle handle = createDynamicIndexBuffer(_mem->size/indexSize, _flags);
|
2013-09-30 00:33:50 -04:00
|
|
|
if (isValid(handle) )
|
2013-01-19 13:11:42 -05:00
|
|
|
{
|
|
|
|
updateDynamicIndexBuffer(handle, _mem);
|
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void updateDynamicIndexBuffer(DynamicIndexBufferHandle _handle, const Memory* _mem) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("updateDynamicIndexBuffer", m_dynamicIndexBufferHandle, _handle);
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
DynamicIndexBuffer& dib = m_dynamicIndexBuffers[_handle.idx];
|
2015-01-28 20:56:29 -05:00
|
|
|
BX_CHECK(0 == (dib.m_flags & BGFX_BUFFER_COMPUTE_READ_WRITE), "Can't update GPU buffer from CPU.");
|
2015-04-08 22:59:48 -04:00
|
|
|
const uint32_t indexSize = 0 == (dib.m_flags & BGFX_BUFFER_INDEX32) ? 2 : 4;
|
2015-01-28 20:56:29 -05:00
|
|
|
|
|
|
|
if (dib.m_size < _mem->size
|
|
|
|
&& 0 != (dib.m_flags & BGFX_BUFFER_ALLOW_RESIZE) )
|
|
|
|
{
|
|
|
|
m_dynIndexBufferAllocator.free(uint64_t(dib.m_handle.idx)<<32 | dib.m_offset);
|
|
|
|
m_dynIndexBufferAllocator.compact();
|
|
|
|
|
|
|
|
uint64_t ptr = allocDynamicIndexBuffer(_mem->size, dib.m_flags);
|
|
|
|
dib.m_handle.idx = uint16_t(ptr>>32);
|
|
|
|
dib.m_offset = uint32_t(ptr);
|
|
|
|
dib.m_size = _mem->size;
|
2015-04-12 20:31:33 -04:00
|
|
|
dib.m_startIndex = bx::strideAlign(dib.m_offset, indexSize)/indexSize;
|
2015-01-28 20:56:29 -05:00
|
|
|
}
|
|
|
|
|
2015-04-08 22:59:48 -04:00
|
|
|
uint32_t offset = dib.m_startIndex*indexSize;
|
2015-01-28 20:56:29 -05:00
|
|
|
uint32_t size = bx::uint32_min(dib.m_size, _mem->size);
|
|
|
|
BX_CHECK(_mem->size <= size, "Truncating dynamic index buffer update (size %d, mem size %d)."
|
|
|
|
, size
|
|
|
|
, _mem->size
|
|
|
|
);
|
2012-08-05 17:51:49 -04:00
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::UpdateDynamicIndexBuffer);
|
|
|
|
cmdbuf.write(dib.m_handle);
|
2015-01-28 20:56:29 -05:00
|
|
|
cmdbuf.write(offset);
|
|
|
|
cmdbuf.write(size);
|
2012-08-05 17:51:49 -04:00
|
|
|
cmdbuf.write(_mem);
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void destroyDynamicIndexBuffer(DynamicIndexBufferHandle _handle) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("destroyDynamicIndexBuffer", m_dynamicIndexBufferHandle, _handle);
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
m_freeDynamicIndexBufferHandle[m_numFreeDynamicIndexBufferHandles++] = _handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroyDynamicIndexBufferInternal(DynamicIndexBufferHandle _handle)
|
|
|
|
{
|
|
|
|
DynamicIndexBuffer& dib = m_dynamicIndexBuffers[_handle.idx];
|
2015-01-28 20:56:29 -05:00
|
|
|
|
|
|
|
if (0 != (dib.m_flags & BGFX_BUFFER_COMPUTE_WRITE) )
|
|
|
|
{
|
|
|
|
destroyIndexBuffer(dib.m_handle);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_dynIndexBufferAllocator.free(uint64_t(dib.m_handle.idx)<<32 | dib.m_offset);
|
2015-02-22 00:51:06 -05:00
|
|
|
if (m_dynIndexBufferAllocator.compact() )
|
|
|
|
{
|
|
|
|
for (uint64_t ptr = m_dynIndexBufferAllocator.remove(); 0 != ptr; ptr = m_dynIndexBufferAllocator.remove() )
|
|
|
|
{
|
|
|
|
IndexBufferHandle handle = { uint16_t(ptr>>32) };
|
|
|
|
destroyIndexBuffer(handle);
|
|
|
|
}
|
|
|
|
}
|
2015-01-28 20:56:29 -05:00
|
|
|
}
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
m_dynamicIndexBufferHandle.free(_handle.idx);
|
|
|
|
}
|
|
|
|
|
2015-01-28 20:56:29 -05:00
|
|
|
uint64_t allocDynamicVertexBuffer(uint32_t _size, uint8_t _flags)
|
|
|
|
{
|
|
|
|
uint64_t ptr = m_dynVertexBufferAllocator.alloc(_size);
|
|
|
|
if (ptr == NonLocalAllocator::invalidBlock)
|
|
|
|
{
|
|
|
|
VertexBufferHandle vertexBufferHandle = { m_vertexBufferHandle.alloc() };
|
|
|
|
|
|
|
|
BX_WARN(isValid(vertexBufferHandle), "Failed to allocate dynamic vertex buffer handle.");
|
|
|
|
if (!isValid(vertexBufferHandle) )
|
|
|
|
{
|
|
|
|
return NonLocalAllocator::invalidBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateDynamicVertexBuffer);
|
|
|
|
cmdbuf.write(vertexBufferHandle);
|
|
|
|
cmdbuf.write(BGFX_CONFIG_DYNAMIC_VERTEX_BUFFER_SIZE);
|
|
|
|
cmdbuf.write(_flags);
|
|
|
|
|
|
|
|
m_dynVertexBufferAllocator.add(uint64_t(vertexBufferHandle.idx)<<32, BGFX_CONFIG_DYNAMIC_VERTEX_BUFFER_SIZE);
|
|
|
|
ptr = m_dynVertexBufferAllocator.alloc(_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2015-02-16 22:01:32 -05:00
|
|
|
BGFX_API_FUNC(DynamicVertexBufferHandle createDynamicVertexBuffer(uint32_t _num, const VertexDecl& _decl, uint8_t _flags) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
DynamicVertexBufferHandle handle = BGFX_INVALID_HANDLE;
|
2015-04-14 20:01:35 -04:00
|
|
|
uint32_t size = bx::strideAlign16(_num*_decl.m_stride, _decl.m_stride);
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2014-12-10 02:16:27 -05:00
|
|
|
uint64_t ptr = 0;
|
|
|
|
if (0 != (_flags & BGFX_BUFFER_COMPUTE_WRITE) )
|
|
|
|
{
|
2014-12-11 00:09:13 -05:00
|
|
|
VertexBufferHandle vertexBufferHandle = { m_vertexBufferHandle.alloc() };
|
|
|
|
if (!isValid(vertexBufferHandle) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2014-12-11 00:09:13 -05:00
|
|
|
return handle;
|
|
|
|
}
|
2014-12-10 02:16:27 -05:00
|
|
|
|
2014-12-11 00:09:13 -05:00
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateDynamicVertexBuffer);
|
|
|
|
cmdbuf.write(vertexBufferHandle);
|
|
|
|
cmdbuf.write(size);
|
|
|
|
cmdbuf.write(_flags);
|
2014-12-10 02:16:27 -05:00
|
|
|
|
2014-12-11 00:09:13 -05:00
|
|
|
ptr = uint64_t(vertexBufferHandle.idx)<<32;
|
2014-12-10 02:16:27 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-28 20:56:29 -05:00
|
|
|
ptr = allocDynamicVertexBuffer(size, _flags);
|
2014-12-10 02:16:27 -05:00
|
|
|
if (ptr == NonLocalAllocator::invalidBlock)
|
|
|
|
{
|
2015-01-28 20:56:29 -05:00
|
|
|
return handle;
|
2014-12-10 02:16:27 -05:00
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VertexDeclHandle declHandle = findVertexDecl(_decl);
|
|
|
|
|
|
|
|
handle.idx = m_dynamicVertexBufferHandle.alloc();
|
|
|
|
DynamicVertexBuffer& dvb = m_dynamicVertexBuffers[handle.idx];
|
2014-12-11 00:09:13 -05:00
|
|
|
dvb.m_handle.idx = uint16_t(ptr>>32);
|
|
|
|
dvb.m_offset = uint32_t(ptr);
|
|
|
|
dvb.m_size = size;
|
2015-04-12 20:31:33 -04:00
|
|
|
dvb.m_startVertex = bx::strideAlign(dvb.m_offset, _decl.m_stride)/_decl.m_stride;
|
2012-08-05 17:51:49 -04:00
|
|
|
dvb.m_numVertices = dvb.m_size/_decl.m_stride;
|
2014-12-11 00:09:13 -05:00
|
|
|
dvb.m_stride = _decl.m_stride;
|
|
|
|
dvb.m_decl = declHandle;
|
|
|
|
dvb.m_flags = _flags;
|
2012-08-05 17:51:49 -04:00
|
|
|
m_declRef.add(dvb.m_handle, declHandle, _decl.m_hash);
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2015-01-30 13:31:50 -05:00
|
|
|
BGFX_API_FUNC(DynamicVertexBufferHandle createDynamicVertexBuffer(const Memory* _mem, const VertexDecl& _decl, uint8_t _flags) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2014-07-25 00:30:57 -04:00
|
|
|
uint32_t numVertices = _mem->size/_decl.m_stride;
|
|
|
|
BX_CHECK(numVertices <= UINT16_MAX, "Num vertices exceeds maximum (num %d, max %d).", numVertices, UINT16_MAX);
|
2015-01-30 13:31:50 -05:00
|
|
|
DynamicVertexBufferHandle handle = createDynamicVertexBuffer(uint16_t(numVertices), _decl, _flags);
|
2013-09-30 00:33:50 -04:00
|
|
|
if (isValid(handle) )
|
2013-01-19 13:11:42 -05:00
|
|
|
{
|
|
|
|
updateDynamicVertexBuffer(handle, _mem);
|
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void updateDynamicVertexBuffer(DynamicVertexBufferHandle _handle, const Memory* _mem) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("updateDynamicVertexBuffer", m_dynamicVertexBufferHandle, _handle);
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
DynamicVertexBuffer& dvb = m_dynamicVertexBuffers[_handle.idx];
|
2015-01-28 20:56:29 -05:00
|
|
|
BX_CHECK(0 == (dvb.m_flags & BGFX_BUFFER_COMPUTE_READ_WRITE), "Can't update GPU buffer from CPU.");
|
|
|
|
|
|
|
|
if (dvb.m_size < _mem->size
|
|
|
|
&& 0 != (dvb.m_flags & BGFX_BUFFER_ALLOW_RESIZE) )
|
|
|
|
{
|
|
|
|
m_dynVertexBufferAllocator.free(uint64_t(dvb.m_handle.idx)<<32 | dvb.m_offset);
|
|
|
|
m_dynVertexBufferAllocator.compact();
|
|
|
|
|
|
|
|
uint64_t ptr = allocDynamicVertexBuffer(_mem->size, dvb.m_flags);
|
|
|
|
dvb.m_handle.idx = uint16_t(ptr>>32);
|
|
|
|
dvb.m_offset = uint32_t(ptr);
|
|
|
|
dvb.m_size = _mem->size;
|
2015-04-12 20:31:33 -04:00
|
|
|
dvb.m_startVertex = bx::strideAlign(dvb.m_offset, dvb.m_stride)/dvb.m_stride;
|
2015-01-28 20:56:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t offset = dvb.m_startVertex*dvb.m_stride;
|
|
|
|
uint32_t size = bx::uint32_min(dvb.m_size, _mem->size);
|
|
|
|
BX_CHECK(_mem->size <= size, "Truncating dynamic vertex buffer update (size %d, mem size %d)."
|
|
|
|
, dvb.m_size
|
|
|
|
, _mem->size
|
|
|
|
);
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::UpdateDynamicVertexBuffer);
|
|
|
|
cmdbuf.write(dvb.m_handle);
|
2015-01-28 20:56:29 -05:00
|
|
|
cmdbuf.write(offset);
|
|
|
|
cmdbuf.write(size);
|
2012-08-05 17:51:49 -04:00
|
|
|
cmdbuf.write(_mem);
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void destroyDynamicVertexBuffer(DynamicVertexBufferHandle _handle) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("destroyDynamicVertexBuffer", m_dynamicVertexBufferHandle, _handle);
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
m_freeDynamicVertexBufferHandle[m_numFreeDynamicVertexBufferHandles++] = _handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroyDynamicVertexBufferInternal(DynamicVertexBufferHandle _handle)
|
|
|
|
{
|
|
|
|
DynamicVertexBuffer& dvb = m_dynamicVertexBuffers[_handle.idx];
|
|
|
|
|
|
|
|
VertexDeclHandle declHandle = m_declRef.release(dvb.m_handle);
|
|
|
|
if (invalidHandle != declHandle.idx)
|
|
|
|
{
|
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::DestroyVertexDecl);
|
|
|
|
cmdbuf.write(declHandle);
|
|
|
|
}
|
|
|
|
|
2014-12-10 02:16:27 -05:00
|
|
|
if (0 != (dvb.m_flags & BGFX_BUFFER_COMPUTE_WRITE) )
|
|
|
|
{
|
2014-12-11 00:09:13 -05:00
|
|
|
destroyVertexBuffer(dvb.m_handle);
|
2014-12-10 02:16:27 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-10 23:38:47 -05:00
|
|
|
m_dynVertexBufferAllocator.free(uint64_t(dvb.m_handle.idx)<<32 | dvb.m_offset);
|
2015-02-22 00:51:06 -05:00
|
|
|
if (m_dynVertexBufferAllocator.compact() )
|
|
|
|
{
|
|
|
|
for (uint64_t ptr = m_dynVertexBufferAllocator.remove(); 0 != ptr; ptr = m_dynVertexBufferAllocator.remove() )
|
|
|
|
{
|
|
|
|
VertexBufferHandle handle = { uint16_t(ptr>>32) };
|
|
|
|
destroyVertexBuffer(handle);
|
|
|
|
}
|
|
|
|
}
|
2014-12-10 02:16:27 -05:00
|
|
|
}
|
2015-01-28 20:56:29 -05:00
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
m_dynamicVertexBufferHandle.free(_handle.idx);
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(bool checkAvailTransientIndexBuffer(uint32_t _num) const)
|
|
|
|
{
|
|
|
|
return m_submit->checkAvailTransientIndexBuffer(_num);
|
|
|
|
}
|
|
|
|
|
|
|
|
BGFX_API_FUNC(bool checkAvailTransientVertexBuffer(uint32_t _num, uint16_t _stride) const)
|
|
|
|
{
|
|
|
|
return m_submit->checkAvailTransientVertexBuffer(_num, _stride);
|
|
|
|
}
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
TransientIndexBuffer* createTransientIndexBuffer(uint32_t _size)
|
|
|
|
{
|
2015-01-29 22:54:23 -05:00
|
|
|
TransientIndexBuffer* tib = NULL;
|
2013-01-19 13:11:42 -05:00
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
IndexBufferHandle handle = { m_indexBufferHandle.alloc() };
|
2013-09-30 00:33:50 -04:00
|
|
|
BX_WARN(isValid(handle), "Failed to allocate transient index buffer handle.");
|
|
|
|
if (isValid(handle) )
|
2013-01-19 13:11:42 -05:00
|
|
|
{
|
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateDynamicIndexBuffer);
|
|
|
|
cmdbuf.write(handle);
|
|
|
|
cmdbuf.write(_size);
|
2015-01-28 20:56:29 -05:00
|
|
|
cmdbuf.write(uint8_t(BGFX_BUFFER_NONE) );
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2015-01-29 22:54:23 -05:00
|
|
|
tib = (TransientIndexBuffer*)BX_ALLOC(g_allocator, sizeof(TransientIndexBuffer)+_size);
|
|
|
|
tib->data = (uint8_t*)&tib[1];
|
|
|
|
tib->size = _size;
|
|
|
|
tib->handle = handle;
|
2013-01-19 13:11:42 -05:00
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2015-01-29 22:54:23 -05:00
|
|
|
return tib;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2015-01-29 22:54:23 -05:00
|
|
|
void destroyTransientIndexBuffer(TransientIndexBuffer* _tib)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::DestroyDynamicIndexBuffer);
|
2015-01-29 22:54:23 -05:00
|
|
|
cmdbuf.write(_tib->handle);
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2015-01-29 22:54:23 -05:00
|
|
|
m_submit->free(_tib->handle);
|
|
|
|
BX_FREE(g_allocator, _tib);
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void allocTransientIndexBuffer(TransientIndexBuffer* _tib, uint32_t _num) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
uint32_t offset = m_submit->allocTransientIndexBuffer(_num);
|
|
|
|
|
2015-04-08 22:59:48 -04:00
|
|
|
TransientIndexBuffer& tib = *m_submit->m_transientIb;
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2015-04-08 22:59:48 -04:00
|
|
|
_tib->data = &tib.data[offset];
|
2015-01-29 22:54:23 -05:00
|
|
|
_tib->size = _num * 2;
|
2015-04-08 22:59:48 -04:00
|
|
|
_tib->handle = tib.handle;
|
2015-04-12 20:31:33 -04:00
|
|
|
_tib->startIndex = bx::strideAlign(offset, 2)/2;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TransientVertexBuffer* createTransientVertexBuffer(uint32_t _size, const VertexDecl* _decl = NULL)
|
|
|
|
{
|
2015-01-29 22:54:23 -05:00
|
|
|
TransientVertexBuffer* tvb = NULL;
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2013-01-19 13:11:42 -05:00
|
|
|
VertexBufferHandle handle = { m_vertexBufferHandle.alloc() };
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2013-09-30 00:33:50 -04:00
|
|
|
BX_WARN(isValid(handle), "Failed to allocate transient vertex buffer handle.");
|
|
|
|
if (isValid(handle) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2013-01-19 13:11:42 -05:00
|
|
|
uint16_t stride = 0;
|
|
|
|
VertexDeclHandle declHandle = BGFX_INVALID_HANDLE;
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2013-01-19 13:11:42 -05:00
|
|
|
if (NULL != _decl)
|
|
|
|
{
|
|
|
|
declHandle = findVertexDecl(*_decl);
|
|
|
|
m_declRef.add(handle, declHandle, _decl->m_hash);
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2013-01-19 13:11:42 -05:00
|
|
|
stride = _decl->m_stride;
|
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2013-01-19 13:11:42 -05:00
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateDynamicVertexBuffer);
|
|
|
|
cmdbuf.write(handle);
|
|
|
|
cmdbuf.write(_size);
|
2014-12-10 02:16:27 -05:00
|
|
|
cmdbuf.write(false);
|
2013-01-19 13:11:42 -05:00
|
|
|
|
2015-01-29 22:54:23 -05:00
|
|
|
tvb = (TransientVertexBuffer*)BX_ALLOC(g_allocator, sizeof(TransientVertexBuffer)+_size);
|
|
|
|
tvb->data = (uint8_t*)&tvb[1];
|
|
|
|
tvb->size = _size;
|
|
|
|
tvb->startVertex = 0;
|
|
|
|
tvb->stride = stride;
|
|
|
|
tvb->handle = handle;
|
|
|
|
tvb->decl = declHandle;
|
2013-01-19 13:11:42 -05:00
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2015-01-29 22:54:23 -05:00
|
|
|
return tvb;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2015-01-29 22:54:23 -05:00
|
|
|
void destroyTransientVertexBuffer(TransientVertexBuffer* _tvb)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::DestroyDynamicVertexBuffer);
|
2015-01-29 22:54:23 -05:00
|
|
|
cmdbuf.write(_tvb->handle);
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2015-01-29 22:54:23 -05:00
|
|
|
m_submit->free(_tvb->handle);
|
|
|
|
BX_FREE(g_allocator, _tvb);
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void allocTransientVertexBuffer(TransientVertexBuffer* _tvb, uint32_t _num, const VertexDecl& _decl) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
VertexDeclHandle declHandle = m_declRef.find(_decl.m_hash);
|
|
|
|
|
|
|
|
TransientVertexBuffer& dvb = *m_submit->m_transientVb;
|
|
|
|
|
2013-09-30 00:33:50 -04:00
|
|
|
if (!isValid(declHandle) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
VertexDeclHandle temp = { m_vertexDeclHandle.alloc() };
|
|
|
|
declHandle = temp;
|
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateVertexDecl);
|
|
|
|
cmdbuf.write(declHandle);
|
|
|
|
cmdbuf.write(_decl);
|
|
|
|
m_declRef.add(dvb.handle, declHandle, _decl.m_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t offset = m_submit->allocTransientVertexBuffer(_num, _decl.m_stride);
|
|
|
|
|
2012-11-10 22:59:23 -05:00
|
|
|
_tvb->data = &dvb.data[offset];
|
2015-01-29 22:54:23 -05:00
|
|
|
_tvb->size = _num * _decl.m_stride;
|
2015-04-12 20:31:33 -04:00
|
|
|
_tvb->startVertex = bx::strideAlign(offset, _decl.m_stride)/_decl.m_stride;
|
2012-11-10 22:59:23 -05:00
|
|
|
_tvb->stride = _decl.m_stride;
|
|
|
|
_tvb->handle = dvb.handle;
|
2014-12-10 02:16:27 -05:00
|
|
|
_tvb->decl = declHandle;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(const InstanceDataBuffer* allocInstanceDataBuffer(uint32_t _num, uint16_t _stride) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2014-01-21 00:49:36 -05:00
|
|
|
++m_instBufferCount;
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
uint16_t stride = BX_ALIGN_16(_stride);
|
|
|
|
uint32_t offset = m_submit->allocTransientVertexBuffer(_num, stride);
|
|
|
|
|
|
|
|
TransientVertexBuffer& dvb = *m_submit->m_transientVb;
|
2013-09-17 00:40:30 -04:00
|
|
|
InstanceDataBuffer* idb = (InstanceDataBuffer*)BX_ALLOC(g_allocator, sizeof(InstanceDataBuffer) );
|
2014-12-10 02:16:27 -05:00
|
|
|
idb->data = &dvb.data[offset];
|
|
|
|
idb->size = _num * stride;
|
2012-08-05 17:51:49 -04:00
|
|
|
idb->offset = offset;
|
2014-12-10 02:16:27 -05:00
|
|
|
idb->num = _num;
|
2012-08-05 17:51:49 -04:00
|
|
|
idb->stride = stride;
|
|
|
|
idb->handle = dvb.handle;
|
|
|
|
|
|
|
|
return idb;
|
|
|
|
}
|
|
|
|
|
2014-03-29 22:42:57 -04:00
|
|
|
BGFX_API_FUNC(ShaderHandle createShader(const Memory* _mem) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2012-11-25 21:24:50 -05:00
|
|
|
bx::MemoryReader reader(_mem->data, _mem->size);
|
|
|
|
|
|
|
|
uint32_t magic;
|
|
|
|
bx::read(&reader, magic);
|
|
|
|
|
2014-05-10 23:51:44 -04:00
|
|
|
if (BGFX_CHUNK_MAGIC_CSH != magic
|
|
|
|
&& BGFX_CHUNK_MAGIC_FSH != magic
|
|
|
|
&& BGFX_CHUNK_MAGIC_VSH != magic)
|
2012-11-25 21:24:50 -05:00
|
|
|
{
|
2014-11-13 01:16:19 -05:00
|
|
|
BX_WARN(false, "Invalid shader signature! %c%c%c%d."
|
|
|
|
, ( (uint8_t*)&magic)[0]
|
|
|
|
, ( (uint8_t*)&magic)[1]
|
|
|
|
, ( (uint8_t*)&magic)[2]
|
|
|
|
, ( (uint8_t*)&magic)[3]
|
|
|
|
);
|
2014-03-29 22:42:57 -04:00
|
|
|
ShaderHandle invalid = BGFX_INVALID_HANDLE;
|
2012-11-25 21:24:50 -05:00
|
|
|
return invalid;
|
|
|
|
}
|
|
|
|
|
2014-03-29 22:42:57 -04:00
|
|
|
ShaderHandle handle = { m_shaderHandle.alloc() };
|
2012-11-25 21:24:50 -05:00
|
|
|
|
2014-03-29 22:42:57 -04:00
|
|
|
BX_WARN(isValid(handle), "Failed to allocate shader handle.");
|
2013-09-30 00:33:50 -04:00
|
|
|
if (isValid(handle) )
|
2013-01-19 13:11:42 -05:00
|
|
|
{
|
2014-04-15 22:10:56 -04:00
|
|
|
uint32_t iohash;
|
|
|
|
bx::read(&reader, iohash);
|
|
|
|
|
|
|
|
uint16_t count;
|
|
|
|
bx::read(&reader, count);
|
|
|
|
|
|
|
|
ShaderRef& sr = m_shaderRef[handle.idx];
|
|
|
|
sr.m_refCount = 1;
|
|
|
|
sr.m_hash = iohash;
|
|
|
|
sr.m_num = 0;
|
|
|
|
sr.m_uniforms = NULL;
|
|
|
|
|
|
|
|
UniformHandle* uniforms = (UniformHandle*)alloca(count*sizeof(UniformHandle) );
|
|
|
|
|
|
|
|
for (uint32_t ii = 0; ii < count; ++ii)
|
|
|
|
{
|
|
|
|
uint8_t nameSize;
|
|
|
|
bx::read(&reader, nameSize);
|
|
|
|
|
|
|
|
char name[256];
|
|
|
|
bx::read(&reader, &name, nameSize);
|
|
|
|
name[nameSize] = '\0';
|
|
|
|
|
|
|
|
uint8_t type;
|
|
|
|
bx::read(&reader, type);
|
|
|
|
type &= ~BGFX_UNIFORM_FRAGMENTBIT;
|
|
|
|
|
|
|
|
uint8_t num;
|
|
|
|
bx::read(&reader, num);
|
|
|
|
|
|
|
|
uint16_t regIndex;
|
|
|
|
bx::read(&reader, regIndex);
|
|
|
|
|
|
|
|
uint16_t regCount;
|
|
|
|
bx::read(&reader, regCount);
|
|
|
|
|
|
|
|
PredefinedUniform::Enum predefined = nameToPredefinedUniformEnum(name);
|
|
|
|
if (PredefinedUniform::Count == predefined)
|
|
|
|
{
|
|
|
|
uniforms[sr.m_num] = createUniform(name, UniformType::Enum(type), regCount);
|
|
|
|
sr.m_num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != sr.m_num)
|
|
|
|
{
|
|
|
|
uint32_t size = sr.m_num*sizeof(UniformHandle);
|
|
|
|
sr.m_uniforms = (UniformHandle*)BX_ALLOC(g_allocator, size);
|
|
|
|
memcpy(sr.m_uniforms, uniforms, size);
|
|
|
|
}
|
2013-01-19 13:11:42 -05:00
|
|
|
|
2014-03-29 22:42:57 -04:00
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateShader);
|
2013-01-19 13:11:42 -05:00
|
|
|
cmdbuf.write(handle);
|
|
|
|
cmdbuf.write(_mem);
|
|
|
|
}
|
2012-11-25 21:24:50 -05:00
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2014-04-15 22:10:56 -04:00
|
|
|
BGFX_API_FUNC(uint16_t getShaderUniforms(ShaderHandle _handle, UniformHandle* _uniforms, uint16_t _max) )
|
|
|
|
{
|
|
|
|
if (!isValid(_handle) )
|
|
|
|
{
|
|
|
|
BX_WARN(false, "Passing invalid shader handle to bgfx::getShaderUniforms.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ShaderRef& sr = m_shaderRef[_handle.idx];
|
|
|
|
if (NULL != _uniforms)
|
|
|
|
{
|
|
|
|
memcpy(_uniforms, sr.m_uniforms, bx::uint16_min(_max, sr.m_num)*sizeof(UniformHandle) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return sr.m_num;
|
|
|
|
}
|
|
|
|
|
2014-03-29 22:42:57 -04:00
|
|
|
BGFX_API_FUNC(void destroyShader(ShaderHandle _handle) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("destroyShader", m_shaderHandle, _handle);
|
|
|
|
|
2013-09-30 00:33:50 -04:00
|
|
|
if (!isValid(_handle) )
|
2013-01-06 01:34:31 -05:00
|
|
|
{
|
2014-04-15 22:10:56 -04:00
|
|
|
BX_WARN(false, "Passing invalid shader handle to bgfx::destroyShader.");
|
2013-01-06 01:34:31 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-29 22:42:57 -04:00
|
|
|
shaderDecRef(_handle);
|
2012-08-05 23:38:17 -04:00
|
|
|
}
|
|
|
|
|
2014-03-29 22:42:57 -04:00
|
|
|
void shaderIncRef(ShaderHandle _handle)
|
2012-08-05 23:38:17 -04:00
|
|
|
{
|
2014-03-29 22:42:57 -04:00
|
|
|
ShaderRef& sr = m_shaderRef[_handle.idx];
|
|
|
|
++sr.m_refCount;
|
2012-08-05 23:38:17 -04:00
|
|
|
}
|
|
|
|
|
2014-03-29 22:42:57 -04:00
|
|
|
void shaderDecRef(ShaderHandle _handle)
|
2012-08-05 23:38:17 -04:00
|
|
|
{
|
2014-03-29 22:42:57 -04:00
|
|
|
ShaderRef& sr = m_shaderRef[_handle.idx];
|
|
|
|
int32_t refs = --sr.m_refCount;
|
2012-08-05 23:38:17 -04:00
|
|
|
if (0 == refs)
|
|
|
|
{
|
2014-03-29 22:42:57 -04:00
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::DestroyShader);
|
2012-08-05 23:38:17 -04:00
|
|
|
cmdbuf.write(_handle);
|
|
|
|
m_submit->free(_handle);
|
2014-04-15 22:10:56 -04:00
|
|
|
|
|
|
|
if (0 != sr.m_num)
|
|
|
|
{
|
|
|
|
for (uint32_t ii = 0, num = sr.m_num; ii < num; ++ii)
|
|
|
|
{
|
|
|
|
destroyUniform(sr.m_uniforms[ii]);
|
|
|
|
}
|
|
|
|
|
|
|
|
BX_FREE(g_allocator, sr.m_uniforms);
|
|
|
|
sr.m_uniforms = NULL;
|
|
|
|
sr.m_num = 0;
|
|
|
|
}
|
2012-08-05 23:38:17 -04:00
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2014-03-29 22:42:57 -04:00
|
|
|
BGFX_API_FUNC(ProgramHandle createProgram(ShaderHandle _vsh, ShaderHandle _fsh) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2013-09-30 00:33:50 -04:00
|
|
|
if (!isValid(_vsh)
|
|
|
|
|| !isValid(_fsh) )
|
2013-01-06 01:34:31 -05:00
|
|
|
{
|
|
|
|
BX_WARN(false, "Vertex/fragment shader is invalid (vsh %d, fsh %d).", _vsh.idx, _fsh.idx);
|
|
|
|
ProgramHandle invalid = BGFX_INVALID_HANDLE;
|
|
|
|
return invalid;
|
|
|
|
}
|
|
|
|
|
2014-03-29 22:42:57 -04:00
|
|
|
const ShaderRef& vsr = m_shaderRef[_vsh.idx];
|
|
|
|
const ShaderRef& fsr = m_shaderRef[_fsh.idx];
|
|
|
|
if (vsr.m_hash != fsr.m_hash)
|
2012-11-25 21:24:50 -05:00
|
|
|
{
|
2014-03-29 22:42:57 -04:00
|
|
|
BX_WARN(vsr.m_hash == fsr.m_hash, "Vertex shader output doesn't match fragment shader input.");
|
2012-11-25 21:24:50 -05:00
|
|
|
ProgramHandle invalid = BGFX_INVALID_HANDLE;
|
|
|
|
return invalid;
|
|
|
|
}
|
|
|
|
|
2012-09-16 20:36:08 -04:00
|
|
|
ProgramHandle handle;
|
|
|
|
handle.idx = m_programHandle.alloc();
|
2012-08-05 23:38:17 -04:00
|
|
|
|
2013-09-30 00:33:50 -04:00
|
|
|
BX_WARN(isValid(handle), "Failed to allocate program handle.");
|
|
|
|
if (isValid(handle) )
|
2013-01-19 13:11:42 -05:00
|
|
|
{
|
2014-03-29 22:42:57 -04:00
|
|
|
shaderIncRef(_vsh);
|
|
|
|
shaderIncRef(_fsh);
|
2014-12-16 23:12:09 -05:00
|
|
|
ProgramRef& pr = m_programRef[handle.idx];
|
|
|
|
pr.m_vsh = _vsh;
|
|
|
|
pr.m_fsh = _fsh;
|
2013-01-19 13:11:42 -05:00
|
|
|
|
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateProgram);
|
|
|
|
cmdbuf.write(handle);
|
|
|
|
cmdbuf.write(_vsh);
|
|
|
|
cmdbuf.write(_fsh);
|
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2014-07-20 23:27:13 -04:00
|
|
|
BGFX_API_FUNC(ProgramHandle createProgram(ShaderHandle _vsh) )
|
|
|
|
{
|
|
|
|
if (!isValid(_vsh) )
|
|
|
|
{
|
|
|
|
BX_WARN(false, "Vertex/fragment shader is invalid (vsh %d).", _vsh.idx);
|
|
|
|
ProgramHandle invalid = BGFX_INVALID_HANDLE;
|
|
|
|
return invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramHandle handle;
|
|
|
|
handle.idx = m_programHandle.alloc();
|
|
|
|
|
|
|
|
BX_WARN(isValid(handle), "Failed to allocate program handle.");
|
|
|
|
if (isValid(handle) )
|
|
|
|
{
|
|
|
|
shaderIncRef(_vsh);
|
2014-12-16 23:12:09 -05:00
|
|
|
ProgramRef& pr = m_programRef[handle.idx];
|
|
|
|
pr.m_vsh = _vsh;
|
|
|
|
ShaderHandle fsh = BGFX_INVALID_HANDLE;
|
|
|
|
pr.m_fsh = fsh;
|
2014-07-20 23:27:13 -04:00
|
|
|
|
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateProgram);
|
|
|
|
cmdbuf.write(handle);
|
|
|
|
cmdbuf.write(_vsh);
|
2014-12-16 23:12:09 -05:00
|
|
|
cmdbuf.write(fsh);
|
2014-07-20 23:27:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void destroyProgram(ProgramHandle _handle) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("destroyProgram", m_programHandle, _handle);
|
|
|
|
|
2012-09-16 20:36:08 -04:00
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::DestroyProgram);
|
2012-08-05 17:51:49 -04:00
|
|
|
cmdbuf.write(_handle);
|
|
|
|
m_submit->free(_handle);
|
2012-08-05 23:38:17 -04:00
|
|
|
|
2014-12-16 23:12:09 -05:00
|
|
|
const ProgramRef& pr = m_programRef[_handle.idx];
|
|
|
|
shaderDecRef(pr.m_vsh);
|
|
|
|
|
|
|
|
if (isValid(pr.m_fsh) )
|
|
|
|
{
|
|
|
|
shaderDecRef(pr.m_fsh);
|
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2015-04-13 21:39:38 -04:00
|
|
|
BGFX_API_FUNC(TextureHandle createTexture(const Memory* _mem, uint32_t _flags, uint8_t _skip, TextureInfo* _info, BackbufferRatio::Enum _ratio) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-02-23 20:25:06 -05:00
|
|
|
TextureInfo ti;
|
|
|
|
if (NULL == _info)
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-02-23 20:25:06 -05:00
|
|
|
_info = &ti;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageContainer imageContainer;
|
|
|
|
if (imageParse(imageContainer, _mem->data, _mem->size) )
|
|
|
|
{
|
|
|
|
calcTextureSize(*_info
|
|
|
|
, (uint16_t)imageContainer.m_width
|
|
|
|
, (uint16_t)imageContainer.m_height
|
|
|
|
, (uint16_t)imageContainer.m_depth
|
|
|
|
, imageContainer.m_cubeMap
|
|
|
|
, imageContainer.m_numMips
|
|
|
|
, TextureFormat::Enum(imageContainer.m_format)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_info->format = TextureFormat::Unknown;
|
|
|
|
_info->storageSize = 0;
|
|
|
|
_info->width = 0;
|
|
|
|
_info->height = 0;
|
|
|
|
_info->depth = 0;
|
|
|
|
_info->numMips = 0;
|
|
|
|
_info->bitsPerPixel = 0;
|
|
|
|
_info->cubeMap = false;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureHandle handle = { m_textureHandle.alloc() };
|
2013-09-30 00:33:50 -04:00
|
|
|
BX_WARN(isValid(handle), "Failed to allocate texture handle.");
|
|
|
|
if (isValid(handle) )
|
2013-01-19 13:11:42 -05:00
|
|
|
{
|
2014-02-06 02:07:11 -05:00
|
|
|
TextureRef& ref = m_textureRef[handle.idx];
|
|
|
|
ref.m_refCount = 1;
|
2015-04-13 21:39:38 -04:00
|
|
|
ref.m_bbRatio = uint8_t(_ratio);
|
2015-02-23 20:25:06 -05:00
|
|
|
ref.m_format = uint8_t(_info->format);
|
2014-02-06 02:07:11 -05:00
|
|
|
|
2013-01-19 13:11:42 -05:00
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateTexture);
|
|
|
|
cmdbuf.write(handle);
|
|
|
|
cmdbuf.write(_mem);
|
|
|
|
cmdbuf.write(_flags);
|
2014-02-20 01:34:53 -05:00
|
|
|
cmdbuf.write(_skip);
|
2013-01-19 13:11:42 -05:00
|
|
|
}
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void destroyTexture(TextureHandle _handle) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("destroyTexture", m_textureHandle, _handle);
|
|
|
|
|
2014-02-06 02:07:11 -05:00
|
|
|
if (!isValid(_handle) )
|
|
|
|
{
|
|
|
|
BX_WARN(false, "Passing invalid texture handle to bgfx::destroyTexture");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
textureDecRef(_handle);
|
|
|
|
}
|
|
|
|
|
2015-04-13 21:39:38 -04:00
|
|
|
void resizeTexture(TextureHandle _handle, uint16_t _width, uint16_t _height)
|
|
|
|
{
|
|
|
|
const TextureRef& textureRef = m_textureRef[_handle.idx];
|
|
|
|
BX_CHECK(BackbufferRatio::None != textureRef.m_bbRatio, "");
|
|
|
|
|
|
|
|
getTextureSizeFromRatio(BackbufferRatio::Enum(textureRef.m_bbRatio), _width, _height);
|
|
|
|
|
|
|
|
BX_TRACE("Resize %3d: %4dx%d %s"
|
|
|
|
, _handle.idx
|
|
|
|
, _width
|
|
|
|
, _height
|
|
|
|
, getName(TextureFormat::Enum(textureRef.m_format) )
|
|
|
|
);
|
|
|
|
|
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::ResizeTexture);
|
|
|
|
cmdbuf.write(_handle);
|
|
|
|
cmdbuf.write(_width);
|
|
|
|
cmdbuf.write(_height);
|
|
|
|
}
|
|
|
|
|
2014-02-06 02:07:11 -05:00
|
|
|
void textureIncRef(TextureHandle _handle)
|
|
|
|
{
|
|
|
|
TextureRef& ref = m_textureRef[_handle.idx];
|
|
|
|
++ref.m_refCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void textureDecRef(TextureHandle _handle)
|
|
|
|
{
|
|
|
|
TextureRef& ref = m_textureRef[_handle.idx];
|
|
|
|
int32_t refs = --ref.m_refCount;
|
|
|
|
if (0 == refs)
|
|
|
|
{
|
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::DestroyTexture);
|
|
|
|
cmdbuf.write(_handle);
|
|
|
|
m_submit->free(_handle);
|
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2013-11-08 01:59:17 -05:00
|
|
|
BGFX_API_FUNC(void updateTexture(TextureHandle _handle, uint8_t _side, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _z, uint16_t _width, uint16_t _height, uint16_t _depth, uint16_t _pitch, const Memory* _mem) )
|
2012-08-13 00:02:11 -04:00
|
|
|
{
|
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::UpdateTexture);
|
|
|
|
cmdbuf.write(_handle);
|
2012-08-19 21:50:23 -04:00
|
|
|
cmdbuf.write(_side);
|
2012-08-13 00:02:11 -04:00
|
|
|
cmdbuf.write(_mip);
|
|
|
|
Rect rect;
|
|
|
|
rect.m_x = _x;
|
|
|
|
rect.m_y = _y;
|
|
|
|
rect.m_width = _width;
|
|
|
|
rect.m_height = _height;
|
|
|
|
cmdbuf.write(rect);
|
2012-08-19 21:50:23 -04:00
|
|
|
cmdbuf.write(_z);
|
|
|
|
cmdbuf.write(_depth);
|
2013-11-08 01:59:17 -05:00
|
|
|
cmdbuf.write(_pitch);
|
2012-08-13 00:02:11 -04:00
|
|
|
cmdbuf.write(_mem);
|
|
|
|
}
|
|
|
|
|
2014-02-06 02:07:11 -05:00
|
|
|
BGFX_API_FUNC(FrameBufferHandle createFrameBuffer(uint8_t _num, TextureHandle* _handles) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2014-02-06 02:07:11 -05:00
|
|
|
FrameBufferHandle handle = { m_frameBufferHandle.alloc() };
|
2014-09-07 20:17:38 -04:00
|
|
|
BX_WARN(isValid(handle), "Failed to allocate frame buffer handle.");
|
2013-01-19 13:11:42 -05:00
|
|
|
|
2013-09-30 00:33:50 -04:00
|
|
|
if (isValid(handle) )
|
2013-01-19 13:11:42 -05:00
|
|
|
{
|
2014-02-06 02:07:11 -05:00
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateFrameBuffer);
|
2013-01-19 13:11:42 -05:00
|
|
|
cmdbuf.write(handle);
|
2014-09-07 20:17:38 -04:00
|
|
|
cmdbuf.write(false);
|
2014-02-06 02:07:11 -05:00
|
|
|
cmdbuf.write(_num);
|
|
|
|
|
|
|
|
FrameBufferRef& ref = m_frameBufferRef[handle.idx];
|
2014-09-07 20:17:38 -04:00
|
|
|
ref.m_window = false;
|
|
|
|
memset(ref.un.m_th, 0xff, sizeof(ref.un.m_th) );
|
2015-04-14 20:03:07 -04:00
|
|
|
BackbufferRatio::Enum bbRatio = BackbufferRatio::Enum(m_textureRef[_handles[0].idx].m_bbRatio);
|
2014-02-06 02:07:11 -05:00
|
|
|
for (uint32_t ii = 0; ii < _num; ++ii)
|
|
|
|
{
|
2014-11-22 17:34:09 -05:00
|
|
|
TextureHandle texHandle = _handles[ii];
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("createFrameBuffer texture handle", m_textureHandle, texHandle);
|
2015-04-14 20:03:07 -04:00
|
|
|
BX_CHECK(bbRatio == m_textureRef[texHandle.idx].m_bbRatio, "Mismatch in texture back-buffer ratio.");
|
2015-04-15 14:49:20 -04:00
|
|
|
BX_UNUSED(bbRatio);
|
2014-02-06 02:07:11 -05:00
|
|
|
|
2014-11-22 17:34:09 -05:00
|
|
|
cmdbuf.write(texHandle);
|
2014-02-06 02:07:11 -05:00
|
|
|
|
2014-11-22 17:34:09 -05:00
|
|
|
ref.un.m_th[ii] = texHandle;
|
|
|
|
textureIncRef(texHandle);
|
2014-02-06 02:07:11 -05:00
|
|
|
}
|
2013-01-19 13:11:42 -05:00
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2014-09-07 20:17:38 -04:00
|
|
|
BGFX_API_FUNC(FrameBufferHandle createFrameBuffer(void* _nwh, uint16_t _width, uint16_t _height, TextureFormat::Enum _depthFormat) )
|
|
|
|
{
|
|
|
|
FrameBufferHandle handle = { m_frameBufferHandle.alloc() };
|
|
|
|
BX_WARN(isValid(handle), "Failed to allocate frame buffer handle.");
|
|
|
|
|
|
|
|
if (isValid(handle) )
|
|
|
|
{
|
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateFrameBuffer);
|
|
|
|
cmdbuf.write(handle);
|
|
|
|
cmdbuf.write(true);
|
|
|
|
cmdbuf.write(_nwh);
|
|
|
|
cmdbuf.write(_width);
|
|
|
|
cmdbuf.write(_height);
|
|
|
|
cmdbuf.write(_depthFormat);
|
|
|
|
|
|
|
|
FrameBufferRef& ref = m_frameBufferRef[handle.idx];
|
|
|
|
ref.m_window = true;
|
|
|
|
ref.un.m_nwh = _nwh;
|
|
|
|
}
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2014-02-06 02:07:11 -05:00
|
|
|
BGFX_API_FUNC(void destroyFrameBuffer(FrameBufferHandle _handle) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("destroyFrameBuffer", m_frameBufferHandle, _handle);
|
|
|
|
|
2014-02-06 02:07:11 -05:00
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::DestroyFrameBuffer);
|
2012-08-05 17:51:49 -04:00
|
|
|
cmdbuf.write(_handle);
|
|
|
|
m_submit->free(_handle);
|
2014-02-06 02:07:11 -05:00
|
|
|
|
|
|
|
FrameBufferRef& ref = m_frameBufferRef[_handle.idx];
|
2014-09-07 20:17:38 -04:00
|
|
|
if (!ref.m_window)
|
2014-02-06 02:07:11 -05:00
|
|
|
{
|
2014-09-07 20:17:38 -04:00
|
|
|
for (uint32_t ii = 0; ii < BX_COUNTOF(ref.un.m_th); ++ii)
|
2014-02-06 02:07:11 -05:00
|
|
|
{
|
2014-09-07 20:17:38 -04:00
|
|
|
TextureHandle th = ref.un.m_th[ii];
|
|
|
|
if (isValid(th) )
|
|
|
|
{
|
|
|
|
textureDecRef(th);
|
|
|
|
}
|
2014-02-06 02:07:11 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(UniformHandle createUniform(const char* _name, UniformType::Enum _type, uint16_t _num) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2013-01-03 00:55:19 -05:00
|
|
|
BX_WARN(PredefinedUniform::Count == nameToPredefinedUniformEnum(_name), "%s is predefined uniform name.", _name);
|
|
|
|
if (PredefinedUniform::Count != nameToPredefinedUniformEnum(_name) )
|
|
|
|
{
|
|
|
|
UniformHandle handle = BGFX_INVALID_HANDLE;
|
|
|
|
return handle;
|
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2013-10-05 21:07:44 -04:00
|
|
|
UniformHashMap::iterator it = m_uniformHashMap.find(_name);
|
|
|
|
if (it != m_uniformHashMap.end() )
|
|
|
|
{
|
|
|
|
UniformHandle handle = it->second;
|
|
|
|
UniformRef& uniform = m_uniformRef[handle.idx];
|
2014-04-15 22:10:56 -04:00
|
|
|
|
|
|
|
uint32_t oldsize = g_uniformTypeSize[uniform.m_type];
|
|
|
|
uint32_t newsize = g_uniformTypeSize[_type];
|
|
|
|
|
|
|
|
if (oldsize < newsize
|
|
|
|
|| uniform.m_num < _num)
|
|
|
|
{
|
|
|
|
uniform.m_type = oldsize < newsize ? _type : uniform.m_type;
|
|
|
|
uniform.m_num = bx::uint16_max(uniform.m_num, _num);
|
|
|
|
|
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateUniform);
|
|
|
|
cmdbuf.write(handle);
|
|
|
|
cmdbuf.write(uniform.m_type);
|
|
|
|
cmdbuf.write(uniform.m_num);
|
|
|
|
uint8_t len = (uint8_t)strlen(_name)+1;
|
|
|
|
cmdbuf.write(len);
|
|
|
|
cmdbuf.write(_name, len);
|
|
|
|
}
|
|
|
|
|
2013-10-05 21:07:44 -04:00
|
|
|
++uniform.m_refCount;
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
UniformHandle handle = { m_uniformHandle.alloc() };
|
|
|
|
|
2013-09-30 00:33:50 -04:00
|
|
|
BX_WARN(isValid(handle), "Failed to allocate uniform handle.");
|
|
|
|
if (isValid(handle) )
|
2013-01-19 13:11:42 -05:00
|
|
|
{
|
2014-08-23 13:37:25 -04:00
|
|
|
BX_TRACE("Creating uniform (handle %3d) %s", handle.idx, _name);
|
|
|
|
|
2013-10-05 21:07:44 -04:00
|
|
|
UniformRef& uniform = m_uniformRef[handle.idx];
|
|
|
|
uniform.m_refCount = 1;
|
2013-01-19 13:11:42 -05:00
|
|
|
uniform.m_type = _type;
|
2014-04-15 22:10:56 -04:00
|
|
|
uniform.m_num = _num;
|
2013-01-19 13:11:42 -05:00
|
|
|
|
2013-10-05 21:07:44 -04:00
|
|
|
m_uniformHashMap.insert(stl::make_pair(stl::string(_name), handle) );
|
|
|
|
|
2013-01-19 13:11:42 -05:00
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::CreateUniform);
|
|
|
|
cmdbuf.write(handle);
|
|
|
|
cmdbuf.write(_type);
|
|
|
|
cmdbuf.write(_num);
|
2013-06-09 18:28:25 -04:00
|
|
|
uint8_t len = (uint8_t)strlen(_name)+1;
|
2013-01-19 13:11:42 -05:00
|
|
|
cmdbuf.write(len);
|
|
|
|
cmdbuf.write(_name, len);
|
|
|
|
}
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void destroyUniform(UniformHandle _handle) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("destroyUniform", m_uniformHandle, _handle);
|
|
|
|
|
2013-10-05 21:07:44 -04:00
|
|
|
UniformRef& uniform = m_uniformRef[_handle.idx];
|
|
|
|
BX_CHECK(uniform.m_refCount > 0, "Destroying already destroyed uniform %d.", _handle.idx);
|
|
|
|
int32_t refs = --uniform.m_refCount;
|
|
|
|
|
|
|
|
if (0 == refs)
|
|
|
|
{
|
2015-02-17 20:08:08 -05:00
|
|
|
for (UniformHashMap::iterator it = m_uniformHashMap.begin(), itEnd = m_uniformHashMap.end(); it != itEnd; ++it)
|
|
|
|
{
|
|
|
|
if (it->second.idx == _handle.idx)
|
|
|
|
{
|
|
|
|
m_uniformHashMap.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-05 21:07:44 -04:00
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::DestroyUniform);
|
|
|
|
cmdbuf.write(_handle);
|
|
|
|
m_submit->free(_handle);
|
|
|
|
}
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void saveScreenShot(const char* _filePath) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::SaveScreenShot);
|
2013-06-09 18:28:25 -04:00
|
|
|
uint16_t len = (uint16_t)strlen(_filePath)+1;
|
|
|
|
cmdbuf.write(len);
|
|
|
|
cmdbuf.write(_filePath, len);
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2014-09-01 14:24:51 -04:00
|
|
|
BGFX_API_FUNC(void setClearColor(uint8_t _index, const float _rgba[4]) )
|
|
|
|
{
|
|
|
|
BX_CHECK(_index < BGFX_CONFIG_MAX_CLEAR_COLOR_PALETTE, "Clear color palette index out of bounds %d (max: %d)."
|
|
|
|
, _index
|
|
|
|
, BGFX_CONFIG_MAX_CLEAR_COLOR_PALETTE
|
|
|
|
);
|
|
|
|
memcpy(&m_clearColor[_index][0], _rgba, 16);
|
|
|
|
m_clearColorDirty = 2;
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void setViewName(uint8_t _id, const char* _name) )
|
2013-06-09 18:28:25 -04:00
|
|
|
{
|
|
|
|
CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::UpdateViewName);
|
|
|
|
cmdbuf.write(_id);
|
|
|
|
uint16_t len = (uint16_t)strlen(_name)+1;
|
|
|
|
cmdbuf.write(len);
|
|
|
|
cmdbuf.write(_name, len);
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void setViewRect(uint8_t _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
Rect& rect = m_rect[_id];
|
|
|
|
rect.m_x = _x;
|
|
|
|
rect.m_y = _y;
|
2014-09-07 20:17:38 -04:00
|
|
|
rect.m_width = bx::uint16_max(_width, 1);
|
2013-08-04 01:15:13 -04:00
|
|
|
rect.m_height = bx::uint16_max(_height, 1);
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void setViewScissor(uint8_t _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height) )
|
2013-07-14 17:32:09 -04:00
|
|
|
{
|
|
|
|
Rect& scissor = m_scissor[_id];
|
|
|
|
scissor.m_x = _x;
|
|
|
|
scissor.m_y = _y;
|
2014-09-07 20:17:38 -04:00
|
|
|
scissor.m_width = _width;
|
2013-07-14 17:32:09 -04:00
|
|
|
scissor.m_height = _height;
|
|
|
|
}
|
|
|
|
|
2015-01-11 21:00:08 -05:00
|
|
|
BGFX_API_FUNC(void setViewClear(uint8_t _id, uint16_t _flags, uint32_t _rgba, float _depth, uint8_t _stencil) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
Clear& clear = m_clear[_id];
|
|
|
|
clear.m_flags = _flags;
|
2015-04-07 20:58:14 -04:00
|
|
|
clear.m_index[0] = uint8_t(_rgba>>24);
|
|
|
|
clear.m_index[1] = uint8_t(_rgba>>16);
|
|
|
|
clear.m_index[2] = uint8_t(_rgba>> 8);
|
|
|
|
clear.m_index[3] = uint8_t(_rgba>> 0);
|
2014-09-01 14:24:51 -04:00
|
|
|
clear.m_depth = _depth;
|
|
|
|
clear.m_stencil = _stencil;
|
|
|
|
}
|
|
|
|
|
2015-01-11 21:00:08 -05:00
|
|
|
BGFX_API_FUNC(void setViewClear(uint8_t _id, uint16_t _flags, float _depth, uint8_t _stencil, uint8_t _0, uint8_t _1, uint8_t _2, uint8_t _3, uint8_t _4, uint8_t _5, uint8_t _6, uint8_t _7) )
|
2014-09-01 14:24:51 -04:00
|
|
|
{
|
|
|
|
Clear& clear = m_clear[_id];
|
2015-01-11 00:39:45 -05:00
|
|
|
clear.m_flags = (_flags & ~BGFX_CLEAR_COLOR)
|
2015-01-11 21:00:08 -05:00
|
|
|
| (0xff != (_0&_1&_2&_3&_4&_5&_6&_7) ? BGFX_CLEAR_COLOR|BGFX_CLEAR_COLOR_USE_PALETTE : 0)
|
2014-09-01 14:24:51 -04:00
|
|
|
;
|
|
|
|
clear.m_index[0] = _0;
|
|
|
|
clear.m_index[1] = _1;
|
|
|
|
clear.m_index[2] = _2;
|
|
|
|
clear.m_index[3] = _3;
|
|
|
|
clear.m_index[4] = _4;
|
|
|
|
clear.m_index[5] = _5;
|
|
|
|
clear.m_index[6] = _6;
|
|
|
|
clear.m_index[7] = _7;
|
|
|
|
clear.m_depth = _depth;
|
|
|
|
clear.m_stencil = _stencil;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void setViewSeq(uint8_t _id, bool _enabled) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
|
|
|
m_seqMask[_id] = _enabled ? 0xffff : 0x0;
|
|
|
|
}
|
|
|
|
|
2014-02-06 02:07:11 -05:00
|
|
|
BGFX_API_FUNC(void setViewFrameBuffer(uint8_t _id, FrameBufferHandle _handle) )
|
2012-08-05 17:51:49 -04:00
|
|
|
{
|
2015-03-26 18:01:09 -04:00
|
|
|
BGFX_CHECK_HANDLE_INVALID_OK("setViewFrameBuffer", m_frameBufferHandle, _handle);
|
2014-02-06 02:07:11 -05:00
|
|
|
m_fb[_id] = _handle;
|
2012-08-05 17:51:49 -04:00
|
|
|
}
|
|
|
|
|
2014-10-29 01:08:55 -04:00
|
|
|
BGFX_API_FUNC(void setViewTransform(uint8_t _id, const void* _view, const void* _proj, uint8_t _flags, const void* _proj1) )
|
2013-03-28 00:18:29 -04:00
|
|
|
{
|
2014-10-29 01:08:55 -04:00
|
|
|
m_viewFlags[_id] = _flags;
|
|
|
|
|
2013-03-28 00:18:29 -04:00
|
|
|
if (NULL != _view)
|
|
|
|
{
|
2014-01-19 02:33:00 -05:00
|
|
|
memcpy(m_view[_id].un.val, _view, sizeof(Matrix4) );
|
2013-03-28 00:18:29 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_view[_id].setIdentity();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL != _proj)
|
|
|
|
{
|
2014-10-29 01:08:55 -04:00
|
|
|
memcpy(m_proj[0][_id].un.val, _proj, sizeof(Matrix4) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_proj[0][_id].setIdentity();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL != _proj1)
|
|
|
|
{
|
|
|
|
memcpy(m_proj[1][_id].un.val, _proj1, sizeof(Matrix4) );
|
2013-03-28 00:18:29 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-29 01:08:55 -04:00
|
|
|
memcpy(m_proj[1][_id].un.val, m_proj[0][_id].un.val, sizeof(Matrix4) );
|
2013-03-28 00:18:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-21 18:40:51 -05:00
|
|
|
BGFX_API_FUNC(void setViewRemap(uint8_t _id, uint8_t _num, const void* _remap) )
|
|
|
|
{
|
2015-03-23 20:10:33 -04:00
|
|
|
const uint32_t num = bx::uint32_min(_id + _num, BGFX_CONFIG_MAX_VIEWS) - _id;
|
2015-02-21 18:40:51 -05:00
|
|
|
if (NULL == _remap)
|
|
|
|
{
|
|
|
|
for (uint32_t ii = 0; ii < num; ++ii)
|
|
|
|
{
|
|
|
|
uint8_t id = uint8_t(ii+_id);
|
|
|
|
m_viewRemap[id] = id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(&m_viewRemap[_id], _remap, num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void setMarker(const char* _marker) )
|
|
|
|
{
|
|
|
|
m_submit->setMarker(_marker);
|
|
|
|
}
|
|
|
|
|
|
|
|
BGFX_API_FUNC(void setState(uint64_t _state, uint32_t _rgba) )
|
|
|
|
{
|
|
|
|
m_submit->setState(_state, _rgba);
|
|
|
|
}
|
|
|
|
|
|
|
|
BGFX_API_FUNC(void setStencil(uint32_t _fstencil, uint32_t _bstencil) )
|
|
|
|
{
|
|
|
|
m_submit->setStencil(_fstencil, _bstencil);
|
|
|
|
}
|
|
|
|
|
|
|
|
BGFX_API_FUNC(uint16_t setScissor(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height) )
|
|
|
|
{
|
|
|
|
return m_submit->setScissor(_x, _y, _width, _height);
|
|
|
|
}
|
|
|
|
|
|
|
|
BGFX_API_FUNC(void setScissor(uint16_t _cache) )
|
|
|
|
{
|
|
|
|
m_submit->setScissor(_cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
BGFX_API_FUNC(uint32_t setTransform(const void* _mtx, uint16_t _num) )
|
|
|
|
{
|
|
|
|
return m_submit->setTransform(_mtx, _num);
|
|
|
|
}
|
|
|
|
|
2014-10-11 13:12:27 -04:00
|
|
|
BGFX_API_FUNC(uint32_t allocTransform(Transform* _transform, uint16_t _num) )
|
2014-10-08 23:13:57 -04:00
|
|
|
{
|
2014-10-11 13:12:27 -04:00
|
|
|
return m_submit->allocTransform(_transform, _num);
|
2014-10-08 23:13:57 -04:00
|
|
|
}
|
|
|
|
|
2014-10-11 13:12:27 -04:00
|
|
|
BGFX_API_FUNC(void setTransform(uint32_t _cache, uint16_t _num) )
|
2014-10-08 23:13:57 -04:00
|
|
|
{
|
2014-10-11 13:12:27 -04:00
|
|
|
m_submit->setTransform(_cache, _num);
|
2014-10-08 23:13:57 -04:00
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void setUniform(UniformHandle _handle, const void* _value, uint16_t _num) )
|
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("setUniform", m_uniformHandle, _handle);
|
2013-10-05 21:07:44 -04:00
|
|
|
UniformRef& uniform = m_uniformRef[_handle.idx];
|
2013-10-05 02:45:05 -04:00
|
|
|
BX_CHECK(uniform.m_num >= _num, "Truncated uniform update. %d (max: %d)", _num, uniform.m_num);
|
2014-04-15 22:10:56 -04:00
|
|
|
m_submit->writeUniform(uniform.m_type, _handle, _value, bx::uint16_min(uniform.m_num, _num) );
|
2013-10-05 02:45:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
BGFX_API_FUNC(void setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex, uint32_t _numIndices) )
|
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("setIndexBuffer", m_indexBufferHandle, _handle);
|
2013-10-05 02:45:05 -04:00
|
|
|
m_submit->setIndexBuffer(_handle, _firstIndex, _numIndices);
|
|
|
|
}
|
|
|
|
|
|
|
|
BGFX_API_FUNC(void setIndexBuffer(DynamicIndexBufferHandle _handle, uint32_t _firstIndex, uint32_t _numIndices) )
|
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("setIndexBuffer", m_dynamicIndexBufferHandle, _handle);
|
2015-01-24 01:18:07 -05:00
|
|
|
m_submit->setIndexBuffer(m_dynamicIndexBuffers[_handle.idx], _firstIndex, _numIndices);
|
2013-10-05 02:45:05 -04:00
|
|
|
}
|
|
|
|
|
2014-04-27 02:48:41 -04:00
|
|
|
BGFX_API_FUNC(void setIndexBuffer(const TransientIndexBuffer* _tib, uint32_t _firstIndex, uint32_t _numIndices) )
|
2013-10-05 02:45:05 -04:00
|
|
|
{
|
2014-04-27 02:48:41 -04:00
|
|
|
m_submit->setIndexBuffer(_tib, _firstIndex, _numIndices);
|
2013-10-05 02:45:05 -04:00
|
|
|
}
|
|
|
|
|
2015-01-24 01:18:07 -05:00
|
|
|
BGFX_API_FUNC(void setVertexBuffer(VertexBufferHandle _handle, uint32_t _startVertex, uint32_t _numVertices) )
|
2013-10-05 02:45:05 -04:00
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("setVertexBuffer", m_vertexBufferHandle, _handle);
|
2015-01-24 01:18:07 -05:00
|
|
|
m_submit->setVertexBuffer(_handle, _startVertex, _numVertices);
|
2013-10-05 02:45:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
BGFX_API_FUNC(void setVertexBuffer(DynamicVertexBufferHandle _handle, uint32_t _numVertices) )
|
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("setVertexBuffer", m_dynamicVertexBufferHandle, _handle);
|
2013-10-05 02:45:05 -04:00
|
|
|
m_submit->setVertexBuffer(m_dynamicVertexBuffers[_handle.idx], _numVertices);
|
|
|
|
}
|
|
|
|
|
2014-04-27 02:48:41 -04:00
|
|
|
BGFX_API_FUNC(void setVertexBuffer(const TransientVertexBuffer* _tvb, uint32_t _startVertex, uint32_t _numVertices) )
|
2013-10-05 02:45:05 -04:00
|
|
|
{
|
2014-04-27 02:48:41 -04:00
|
|
|
m_submit->setVertexBuffer(_tvb, _startVertex, _numVertices);
|
2013-10-05 02:45:05 -04:00
|
|
|
}
|
|
|
|
|
2014-12-10 02:16:27 -05:00
|
|
|
BGFX_API_FUNC(void setInstanceDataBuffer(const InstanceDataBuffer* _idb, uint32_t _num) )
|
2013-10-05 02:45:05 -04:00
|
|
|
{
|
2014-01-21 00:49:36 -05:00
|
|
|
--m_instBufferCount;
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
m_submit->setInstanceDataBuffer(_idb, _num);
|
|
|
|
}
|
|
|
|
|
2014-12-10 02:16:27 -05:00
|
|
|
BGFX_API_FUNC(void setInstanceDataBuffer(VertexBufferHandle _handle, uint32_t _startVertex, uint32_t _num) )
|
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("setInstanceDataBuffer", m_vertexBufferHandle, _handle);
|
2014-12-10 02:16:27 -05:00
|
|
|
const VertexBuffer& vb = m_vertexBuffers[_handle.idx];
|
|
|
|
m_submit->setInstanceDataBuffer(_handle, _startVertex, _num, vb.m_stride);
|
|
|
|
}
|
|
|
|
|
|
|
|
BGFX_API_FUNC(void setInstanceDataBuffer(DynamicVertexBufferHandle _handle, uint32_t _startVertex, uint32_t _num) )
|
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("setInstanceDataBuffer", m_dynamicVertexBufferHandle, _handle);
|
2014-12-10 02:16:27 -05:00
|
|
|
const DynamicVertexBuffer& dvb = m_dynamicVertexBuffers[_handle.idx];
|
|
|
|
m_submit->setInstanceDataBuffer(dvb.m_handle
|
|
|
|
, dvb.m_startVertex + _startVertex
|
|
|
|
, _num
|
|
|
|
, dvb.m_stride
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void setProgram(ProgramHandle _handle) )
|
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("setProgram", m_programHandle, _handle);
|
2013-10-05 02:45:05 -04:00
|
|
|
m_submit->setProgram(_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
BGFX_API_FUNC(void setTexture(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle, uint32_t _flags) )
|
|
|
|
{
|
2015-03-27 22:52:31 -04:00
|
|
|
BGFX_CHECK_HANDLE_INVALID_OK("setTexture/TextureHandle", m_textureHandle, _handle);
|
2013-10-05 02:45:05 -04:00
|
|
|
m_submit->setTexture(_stage, _sampler, _handle, _flags);
|
|
|
|
}
|
|
|
|
|
2014-02-06 02:07:11 -05:00
|
|
|
BGFX_API_FUNC(void setTexture(uint8_t _stage, UniformHandle _sampler, FrameBufferHandle _handle, uint8_t _attachment, uint32_t _flags) )
|
2013-10-05 02:45:05 -04:00
|
|
|
{
|
2015-03-27 22:52:31 -04:00
|
|
|
BGFX_CHECK_HANDLE_INVALID_OK("setTexture/FrameBufferHandle", m_frameBufferHandle, _handle);
|
2014-02-06 02:07:11 -05:00
|
|
|
BX_CHECK(_attachment < g_caps.maxFBAttachments, "Frame buffer attachment index %d is invalid.", _attachment);
|
|
|
|
TextureHandle textureHandle = BGFX_INVALID_HANDLE;
|
|
|
|
if (isValid(_handle) )
|
|
|
|
{
|
2014-09-07 20:17:38 -04:00
|
|
|
const FrameBufferRef& ref = m_frameBufferRef[_handle.idx];
|
|
|
|
BX_CHECK(!ref.m_window, "Can't sample window frame buffer.");
|
|
|
|
textureHandle = ref.un.m_th[_attachment];
|
2014-02-06 02:07:11 -05:00
|
|
|
BX_CHECK(isValid(textureHandle), "Frame buffer texture %d is invalid.", _attachment);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_submit->setTexture(_stage, _sampler, textureHandle, _flags);
|
2013-10-05 02:45:05 -04:00
|
|
|
}
|
|
|
|
|
2013-10-21 23:37:02 -04:00
|
|
|
BGFX_API_FUNC(uint32_t submit(uint8_t _id, int32_t _depth) )
|
2013-10-05 02:45:05 -04:00
|
|
|
{
|
2013-10-21 23:37:02 -04:00
|
|
|
return m_submit->submit(_id, _depth);
|
2013-10-05 02:45:05 -04:00
|
|
|
}
|
|
|
|
|
2015-01-10 23:38:47 -05:00
|
|
|
BGFX_API_FUNC(void setBuffer(uint8_t _stage, IndexBufferHandle _handle, Access::Enum _access) )
|
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("setBuffer", m_indexBufferHandle, _handle);
|
2015-01-10 23:38:47 -05:00
|
|
|
m_submit->setBuffer(_stage, _handle, _access);
|
|
|
|
}
|
|
|
|
|
2014-12-10 02:16:27 -05:00
|
|
|
BGFX_API_FUNC(void setBuffer(uint8_t _stage, VertexBufferHandle _handle, Access::Enum _access) )
|
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("setBuffer", m_vertexBufferHandle, _handle);
|
2014-12-10 02:16:27 -05:00
|
|
|
m_submit->setBuffer(_stage, _handle, _access);
|
|
|
|
}
|
|
|
|
|
2015-01-10 23:38:47 -05:00
|
|
|
BGFX_API_FUNC(void setBuffer(uint8_t _stage, DynamicIndexBufferHandle _handle, Access::Enum _access) )
|
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("setBuffer", m_dynamicIndexBufferHandle, _handle);
|
2015-01-10 23:38:47 -05:00
|
|
|
const DynamicIndexBuffer& dib = m_dynamicIndexBuffers[_handle.idx];
|
|
|
|
m_submit->setBuffer(_stage, dib.m_handle, _access);
|
|
|
|
}
|
|
|
|
|
2014-12-10 02:16:27 -05:00
|
|
|
BGFX_API_FUNC(void setBuffer(uint8_t _stage, DynamicVertexBufferHandle _handle, Access::Enum _access) )
|
|
|
|
{
|
2015-03-26 02:33:35 -04:00
|
|
|
BGFX_CHECK_HANDLE("setBuffer", m_dynamicVertexBufferHandle, _handle);
|
2014-12-10 02:16:27 -05:00
|
|
|
const DynamicVertexBuffer& dvb = m_dynamicVertexBuffers[_handle.idx];
|
|
|
|
m_submit->setBuffer(_stage, dvb.m_handle, _access);
|
|
|
|
}
|
|
|
|
|
2015-02-23 20:25:06 -05:00
|
|
|
BGFX_API_FUNC(void setImage(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle, uint8_t _mip, Access::Enum _access, TextureFormat::Enum _format) )
|
2014-07-20 23:27:13 -04:00
|
|
|
{
|
2015-02-23 20:25:06 -05:00
|
|
|
_format = TextureFormat::Count == _format ? TextureFormat::Enum(m_textureRef[_handle.idx].m_format) : _format;
|
2015-03-05 22:31:20 -05:00
|
|
|
BX_CHECK(_format != TextureFormat::BGRA8
|
|
|
|
, "Can't use TextureFormat::BGRA8 with compute, use TextureFormat::RGBA8 instead."
|
|
|
|
);
|
2015-02-23 20:25:06 -05:00
|
|
|
m_submit->setImage(_stage, _sampler, _handle, _mip, _access, _format);
|
2014-07-20 23:27:13 -04:00
|
|
|
}
|
|
|
|
|
2015-02-23 20:25:06 -05:00
|
|
|
BGFX_API_FUNC(void setImage(uint8_t _stage, UniformHandle _sampler, FrameBufferHandle _handle, uint8_t _attachment, Access::Enum _access, TextureFormat::Enum _format) )
|
2014-07-20 23:27:13 -04:00
|
|
|
{
|
|
|
|
BX_CHECK(_attachment < g_caps.maxFBAttachments, "Frame buffer attachment index %d is invalid.", _attachment);
|
|
|
|
TextureHandle textureHandle = BGFX_INVALID_HANDLE;
|
|
|
|
if (isValid(_handle) )
|
|
|
|
{
|
2014-09-07 20:17:38 -04:00
|
|
|
const FrameBufferRef& ref = m_frameBufferRef[_handle.idx];
|
|
|
|
BX_CHECK(!ref.m_window, "Can't sample window frame buffer.");
|
|
|
|
textureHandle = ref.un.m_th[_attachment];
|
2014-07-20 23:27:13 -04:00
|
|
|
BX_CHECK(isValid(textureHandle), "Frame buffer texture %d is invalid.", _attachment);
|
|
|
|
}
|
|
|
|
|
2015-02-23 20:25:06 -05:00
|
|
|
setImage(_stage, _sampler, textureHandle, 0, _access, _format);
|
2014-07-20 23:27:13 -04:00
|
|
|
}
|
|
|
|
|
2014-12-28 14:36:36 -05:00
|
|
|
BGFX_API_FUNC(uint32_t dispatch(uint8_t _id, ProgramHandle _handle, uint16_t _numX, uint16_t _numY, uint16_t _numZ, uint8_t _flags) )
|
2014-07-20 23:27:13 -04:00
|
|
|
{
|
2014-12-28 14:36:36 -05:00
|
|
|
return m_submit->dispatch(_id, _handle, _numX, _numY, _numZ, _flags);
|
2014-07-20 23:27:13 -04:00
|
|
|
}
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
BGFX_API_FUNC(void discard() )
|
|
|
|
{
|
|
|
|
m_submit->discard();
|
|
|
|
}
|
|
|
|
|
2013-10-21 23:37:02 -04:00
|
|
|
BGFX_API_FUNC(uint32_t frame() );
|
|
|
|
|
2013-10-05 21:07:44 -04:00
|
|
|
void dumpViewStats();
|
|
|
|
void freeDynamicBuffers();
|
|
|
|
void freeAllHandles(Frame* _frame);
|
|
|
|
void frameNoRenderWait();
|
|
|
|
void swap();
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2013-10-05 22:46:57 -04:00
|
|
|
// render thread
|
|
|
|
bool renderFrame();
|
2013-10-05 21:07:44 -04:00
|
|
|
void flushTextureUpdateBatch(CommandBuffer& _cmdbuf);
|
|
|
|
void rendererExecCommands(CommandBuffer& _cmdbuf);
|
2012-08-05 17:51:49 -04:00
|
|
|
|
|
|
|
#if BGFX_CONFIG_MULTITHREADED
|
|
|
|
void gameSemPost()
|
|
|
|
{
|
|
|
|
m_gameSem.post();
|
|
|
|
}
|
|
|
|
|
|
|
|
void gameSemWait()
|
|
|
|
{
|
|
|
|
int64_t start = bx::getHPCounter();
|
2013-09-01 17:02:12 -04:00
|
|
|
bool ok = m_gameSem.wait();
|
|
|
|
BX_CHECK(ok, "Semaphore wait failed."); BX_UNUSED(ok);
|
2012-08-05 17:51:49 -04:00
|
|
|
m_render->m_waitSubmit = bx::getHPCounter()-start;
|
|
|
|
}
|
|
|
|
|
|
|
|
void renderSemPost()
|
|
|
|
{
|
|
|
|
m_renderSem.post();
|
|
|
|
}
|
|
|
|
|
|
|
|
void renderSemWait()
|
|
|
|
{
|
|
|
|
int64_t start = bx::getHPCounter();
|
2013-09-01 17:02:12 -04:00
|
|
|
bool ok = m_renderSem.wait();
|
|
|
|
BX_CHECK(ok, "Semaphore wait failed."); BX_UNUSED(ok);
|
2012-08-05 17:51:49 -04:00
|
|
|
m_submit->m_waitRender = bx::getHPCounter() - start;
|
|
|
|
}
|
|
|
|
|
2013-08-04 01:15:13 -04:00
|
|
|
bx::Semaphore m_renderSem;
|
|
|
|
bx::Semaphore m_gameSem;
|
2014-03-16 19:49:59 -04:00
|
|
|
bx::Thread m_thread;
|
2012-08-05 17:51:49 -04:00
|
|
|
#else
|
|
|
|
void gameSemPost()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void gameSemWait()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void renderSemPost()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void renderSemWait()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif // BGFX_CONFIG_MULTITHREADED
|
|
|
|
|
|
|
|
Frame m_frame[2];
|
|
|
|
Frame* m_render;
|
|
|
|
Frame* m_submit;
|
|
|
|
|
|
|
|
uint64_t m_tempKeys[BGFX_CONFIG_MAX_DRAW_CALLS];
|
2015-04-09 22:38:51 -04:00
|
|
|
RenderItemCount m_tempValues[BGFX_CONFIG_MAX_DRAW_CALLS];
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2014-12-10 02:16:27 -05:00
|
|
|
VertexBuffer m_vertexBuffers[BGFX_CONFIG_MAX_VERTEX_BUFFERS];
|
|
|
|
|
2015-01-24 01:18:07 -05:00
|
|
|
DynamicIndexBuffer m_dynamicIndexBuffers[BGFX_CONFIG_MAX_DYNAMIC_INDEX_BUFFERS];
|
2012-08-05 17:51:49 -04:00
|
|
|
DynamicVertexBuffer m_dynamicVertexBuffers[BGFX_CONFIG_MAX_DYNAMIC_VERTEX_BUFFERS];
|
|
|
|
|
|
|
|
uint16_t m_numFreeDynamicIndexBufferHandles;
|
|
|
|
uint16_t m_numFreeDynamicVertexBufferHandles;
|
|
|
|
DynamicIndexBufferHandle m_freeDynamicIndexBufferHandle[BGFX_CONFIG_MAX_DYNAMIC_INDEX_BUFFERS];
|
|
|
|
DynamicVertexBufferHandle m_freeDynamicVertexBufferHandle[BGFX_CONFIG_MAX_DYNAMIC_VERTEX_BUFFERS];
|
|
|
|
|
2015-01-10 23:38:47 -05:00
|
|
|
NonLocalAllocator m_dynIndexBufferAllocator;
|
2013-09-21 01:13:58 -04:00
|
|
|
bx::HandleAllocT<BGFX_CONFIG_MAX_DYNAMIC_INDEX_BUFFERS> m_dynamicIndexBufferHandle;
|
2015-01-10 23:38:47 -05:00
|
|
|
NonLocalAllocator m_dynVertexBufferAllocator;
|
2013-09-21 01:13:58 -04:00
|
|
|
bx::HandleAllocT<BGFX_CONFIG_MAX_DYNAMIC_VERTEX_BUFFERS> m_dynamicVertexBufferHandle;
|
|
|
|
|
|
|
|
bx::HandleAllocT<BGFX_CONFIG_MAX_INDEX_BUFFERS> m_indexBufferHandle;
|
|
|
|
bx::HandleAllocT<BGFX_CONFIG_MAX_VERTEX_DECLS > m_vertexDeclHandle;
|
|
|
|
|
|
|
|
bx::HandleAllocT<BGFX_CONFIG_MAX_VERTEX_BUFFERS> m_vertexBufferHandle;
|
2014-03-29 22:42:57 -04:00
|
|
|
bx::HandleAllocT<BGFX_CONFIG_MAX_SHADERS> m_shaderHandle;
|
2013-09-21 01:13:58 -04:00
|
|
|
bx::HandleAllocT<BGFX_CONFIG_MAX_PROGRAMS> m_programHandle;
|
|
|
|
bx::HandleAllocT<BGFX_CONFIG_MAX_TEXTURES> m_textureHandle;
|
2014-02-06 02:07:11 -05:00
|
|
|
bx::HandleAllocT<BGFX_CONFIG_MAX_FRAME_BUFFERS> m_frameBufferHandle;
|
2013-09-21 01:13:58 -04:00
|
|
|
bx::HandleAllocT<BGFX_CONFIG_MAX_UNIFORMS> m_uniformHandle;
|
2012-08-05 17:51:49 -04:00
|
|
|
|
2014-03-29 22:42:57 -04:00
|
|
|
struct ShaderRef
|
2012-11-25 21:24:50 -05:00
|
|
|
{
|
2014-04-15 22:10:56 -04:00
|
|
|
UniformHandle* m_uniforms;
|
2014-03-29 22:42:57 -04:00
|
|
|
uint32_t m_hash;
|
2014-04-15 22:10:56 -04:00
|
|
|
int16_t m_refCount;
|
|
|
|
uint16_t m_num;
|
2013-10-05 21:07:44 -04:00
|
|
|
};
|
2014-11-22 17:34:09 -05:00
|
|
|
|
2012-09-16 20:36:08 -04:00
|
|
|
struct ProgramRef
|
2012-08-05 23:38:17 -04:00
|
|
|
{
|
2014-03-29 22:42:57 -04:00
|
|
|
ShaderHandle m_vsh;
|
|
|
|
ShaderHandle m_fsh;
|
2012-08-05 23:38:17 -04:00
|
|
|
};
|
2013-09-30 23:09:09 -04:00
|
|
|
|
2013-10-05 21:07:44 -04:00
|
|
|
struct UniformRef
|
|
|
|
{
|
|
|
|
UniformType::Enum m_type;
|
|
|
|
uint16_t m_num;
|
2014-02-06 02:07:11 -05:00
|
|
|
int16_t m_refCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TextureRef
|
|
|
|
{
|
|
|
|
int16_t m_refCount;
|
2015-04-13 21:39:38 -04:00
|
|
|
uint8_t m_bbRatio;
|
2015-02-23 20:25:06 -05:00
|
|
|
uint8_t m_format;
|
2014-02-06 02:07:11 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
struct FrameBufferRef
|
|
|
|
{
|
2014-09-07 20:17:38 -04:00
|
|
|
union un
|
|
|
|
{
|
|
|
|
TextureHandle m_th[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS];
|
|
|
|
void* m_nwh;
|
|
|
|
} un;
|
|
|
|
bool m_window;
|
2013-10-05 21:07:44 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef stl::unordered_map<stl::string, UniformHandle> UniformHashMap;
|
|
|
|
UniformHashMap m_uniformHashMap;
|
|
|
|
UniformRef m_uniformRef[BGFX_CONFIG_MAX_UNIFORMS];
|
2014-03-29 22:42:57 -04:00
|
|
|
ShaderRef m_shaderRef[BGFX_CONFIG_MAX_SHADERS];
|
2012-09-16 20:36:08 -04:00
|
|
|
ProgramRef m_programRef[BGFX_CONFIG_MAX_PROGRAMS];
|
2014-02-06 02:07:11 -05:00
|
|
|
TextureRef m_textureRef[BGFX_CONFIG_MAX_TEXTURES];
|
|
|
|
FrameBufferRef m_frameBufferRef[BGFX_CONFIG_MAX_FRAME_BUFFERS];
|
2012-08-05 17:51:49 -04:00
|
|
|
VertexDeclRef m_declRef;
|
|
|
|
|
2015-02-21 18:40:51 -05:00
|
|
|
uint8_t m_viewRemap[BGFX_CONFIG_MAX_VIEWS];
|
2014-02-06 02:07:11 -05:00
|
|
|
FrameBufferHandle m_fb[BGFX_CONFIG_MAX_VIEWS];
|
2012-08-05 17:51:49 -04:00
|
|
|
Clear m_clear[BGFX_CONFIG_MAX_VIEWS];
|
2014-09-01 14:24:51 -04:00
|
|
|
|
|
|
|
float m_clearColor[BGFX_CONFIG_MAX_CLEAR_COLOR_PALETTE][4];
|
2012-08-05 17:51:49 -04:00
|
|
|
Rect m_rect[BGFX_CONFIG_MAX_VIEWS];
|
2013-07-14 17:32:09 -04:00
|
|
|
Rect m_scissor[BGFX_CONFIG_MAX_VIEWS];
|
2013-03-28 00:18:29 -04:00
|
|
|
Matrix4 m_view[BGFX_CONFIG_MAX_VIEWS];
|
2014-10-29 01:08:55 -04:00
|
|
|
Matrix4 m_proj[2][BGFX_CONFIG_MAX_VIEWS];
|
|
|
|
uint8_t m_viewFlags[BGFX_CONFIG_MAX_VIEWS];
|
2012-08-05 17:51:49 -04:00
|
|
|
uint16_t m_seq[BGFX_CONFIG_MAX_VIEWS];
|
|
|
|
uint16_t m_seqMask[BGFX_CONFIG_MAX_VIEWS];
|
|
|
|
|
2014-09-01 14:24:51 -04:00
|
|
|
uint8_t m_clearColorDirty;
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
Resolution m_resolution;
|
2014-01-21 00:49:36 -05:00
|
|
|
int32_t m_instBufferCount;
|
2012-08-05 17:51:49 -04:00
|
|
|
uint32_t m_frames;
|
|
|
|
uint32_t m_debug;
|
|
|
|
|
|
|
|
TextVideoMemBlitter m_textVideoMemBlitter;
|
|
|
|
ClearQuad m_clearQuad;
|
|
|
|
|
2014-05-26 17:09:26 -04:00
|
|
|
RendererContextI* m_renderCtx;
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
bool m_rendererInitialized;
|
|
|
|
bool m_exit;
|
2015-04-15 14:49:20 -04:00
|
|
|
bool m_flipAfterRender;
|
2013-03-03 00:35:09 -05:00
|
|
|
|
|
|
|
typedef UpdateBatchT<256> TextureUpdateBatch;
|
2014-10-14 00:31:18 -04:00
|
|
|
BX_ALIGN_DECL_CACHE_LINE(TextureUpdateBatch m_textureUpdateBatch);
|
2012-08-05 17:51:49 -04:00
|
|
|
};
|
|
|
|
|
2013-10-05 02:45:05 -04:00
|
|
|
#undef BGFX_API_FUNC
|
|
|
|
|
2012-08-05 17:51:49 -04:00
|
|
|
} // namespace bgfx
|
|
|
|
|
2013-11-14 00:54:36 -05:00
|
|
|
#endif // BGFX_P_H_HEADER_GUARD
|