diff --git a/examples/03-raymarch/raymarch.cpp b/examples/03-raymarch/raymarch.cpp index 5e1abb629..bf50c78eb 100644 --- a/examples/03-raymarch/raymarch.cpp +++ b/examples/03-raymarch/raymarch.cpp @@ -32,25 +32,12 @@ bgfx::VertexDecl PosColorTexCoord0Vertex::ms_decl; static bool s_flipV = false; -bool allocTransientBuffers(bgfx::TransientVertexBuffer* _tvb, const bgfx::VertexDecl& _decl, uint16_t _numVertices, bgfx::TransientIndexBuffer* _tib, uint16_t _numIndices) -{ - if (bgfx::checkAvailTransientVertexBuffer(_numVertices, _decl) - && bgfx::checkAvailTransientIndexBuffer(_numIndices) ) - { - bgfx::allocTransientVertexBuffer(_tvb, _numVertices, _decl); - bgfx::allocTransientIndexBuffer(_tib, _numIndices); - return true; - } - - return false; -} - void renderScreenSpaceQuad(uint32_t _view, bgfx::ProgramHandle _program, float _x, float _y, float _width, float _height) { bgfx::TransientVertexBuffer tvb; bgfx::TransientIndexBuffer tib; - if (allocTransientBuffers(&tvb, PosColorTexCoord0Vertex::ms_decl, 4, &tib, 6) ) + if (bgfx::allocTransientBuffers(&tvb, PosColorTexCoord0Vertex::ms_decl, 4, &tib, 6) ) { PosColorTexCoord0Vertex* vertex = (PosColorTexCoord0Vertex*)tvb.data; diff --git a/tools/geometryc/bounds.cpp b/examples/common/bounds.cpp similarity index 87% rename from tools/geometryc/bounds.cpp rename to examples/common/bounds.cpp index 4fa87a486..dce2c1a42 100644 --- a/tools/geometryc/bounds.cpp +++ b/examples/common/bounds.cpp @@ -5,7 +5,7 @@ #include #include "bounds.h" -#include "math.h" +#include "fpumath.h" void aabbToObb(Obb& _obb, const Aabb& _aabb) { @@ -19,6 +19,20 @@ void aabbToObb(Obb& _obb, const Aabb& _aabb) _obb.m_mtx[15] = 1.0f; } +void sphereToAabb(Aabb& _aabb, const Sphere& _sphere) +{ + float xx = _sphere.m_center[0]; + float yy = _sphere.m_center[1]; + float zz = _sphere.m_center[2]; + float radius = _sphere.m_radius; + _aabb.m_min[0] = xx - radius; + _aabb.m_min[1] = yy - radius; + _aabb.m_min[2] = zz - radius; + _aabb.m_max[0] = xx + radius; + _aabb.m_max[1] = yy + radius; + _aabb.m_max[2] = zz + radius; +} + void aabbTransformToObb(Obb& _obb, const Aabb& _aabb, const float* _mtx) { aabbToObb(_obb, _aabb); @@ -53,12 +67,12 @@ void calcAabb(Aabb& _aabb, const void* _vertices, uint32_t _numVertices, uint32_ float xx = position[0]; float yy = position[1]; float zz = position[2]; - min[0] = fmin(xx, min[0]); - min[1] = fmin(yy, min[1]); - min[2] = fmin(zz, min[2]); - max[0] = fmax(xx, max[0]); - max[1] = fmax(yy, max[1]); - max[2] = fmax(zz, max[2]); + min[0] = fminf(xx, min[0]); + min[1] = fminf(yy, min[1]); + min[2] = fminf(zz, min[2]); + max[0] = fmaxf(xx, max[0]); + max[1] = fmaxf(yy, max[1]); + max[2] = fmaxf(zz, max[2]); } _aabb.m_min[0] = min[0]; @@ -89,12 +103,12 @@ void calcAabb(Aabb& _aabb, const float* _mtx, const void* _vertices, uint32_t _n float xx = position[0]; float yy = position[1]; float zz = position[2]; - min[0] = fmin(xx, min[0]); - min[1] = fmin(yy, min[1]); - min[2] = fmin(zz, min[2]); - max[0] = fmax(xx, max[0]); - max[1] = fmax(yy, max[1]); - max[2] = fmax(zz, max[2]); + min[0] = fminf(xx, min[0]); + min[1] = fminf(yy, min[1]); + min[2] = fminf(zz, min[2]); + max[0] = fmaxf(xx, max[0]); + max[1] = fmaxf(yy, max[1]); + max[2] = fmaxf(zz, max[2]); } _aabb.m_min[0] = min[0]; @@ -176,7 +190,7 @@ void calcMaxBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _num float zz = position[2] - center[2]; float distSq = xx*xx + yy*yy + zz*zz; - maxDistSq = fmax(distSq, maxDistSq); + maxDistSq = fmaxf(distSq, maxDistSq); } _sphere.m_center[0] = center[0]; diff --git a/tools/geometryc/bounds.h b/examples/common/bounds.h similarity index 92% rename from tools/geometryc/bounds.h rename to examples/common/bounds.h index 22d11a0d0..fc12a6900 100644 --- a/tools/geometryc/bounds.h +++ b/examples/common/bounds.h @@ -26,6 +26,9 @@ struct Sphere /// Convert axis aligned bounding box to oriented bounding box. void aabbToObb(Obb& _obb, const Aabb& _aabb); +/// Convert sphere to axis aligned bounding box. +void sphereToAabb(Aabb& _aabb, const Sphere& _sphere); + /// Calculate surface area of axis aligned bounding box. float calcAabbArea(Aabb& _aabb); diff --git a/include/bgfx.h b/include/bgfx.h index 9cc395671..95d6dfbed 100644 --- a/include/bgfx.h +++ b/include/bgfx.h @@ -745,6 +745,10 @@ namespace bgfx void dbgTextPrintf(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, ...); /// Create static index buffer. + /// + /// NOTE: + /// Only 16-bit index buffer is supported. + /// IndexBufferHandle createIndexBuffer(const Memory* _mem); /// Destroy static index buffer. @@ -768,12 +772,18 @@ namespace bgfx /// /// @param _num Number of indices. /// + /// NOTE: + /// Only 16-bit index buffer is supported. + /// DynamicIndexBufferHandle createDynamicIndexBuffer(uint32_t _num); /// Create dynamic index buffer and initialized it. /// /// @param _mem Index buffer data. /// + /// NOTE: + /// Only 16-bit index buffer is supported. + /// DynamicIndexBufferHandle createDynamicIndexBuffer(const Memory* _mem); /// Update dynamic index buffer. @@ -846,8 +856,9 @@ namespace bgfx /// @param _num Number of indices to allocate. /// /// NOTE: - /// You must call setIndexBuffer after alloc in order to avoid memory - /// leak. + /// 1. You must call setIndexBuffer after alloc in order to avoid memory + /// leak. + /// 2. Only 16-bit index buffer is supported. /// void allocTransientIndexBuffer(TransientIndexBuffer* _tib, uint32_t _num); @@ -865,6 +876,15 @@ namespace bgfx /// void allocTransientVertexBuffer(TransientVertexBuffer* _tvb, uint32_t _num, const VertexDecl& _decl); + /// Check for required space and allocate transient vertex and index + /// buffers. If both space requirements are satisfied function returns + /// true. + /// + /// NOTE: + /// Only 16-bit index buffer is supported. + /// + bool allocTransientBuffers(bgfx::TransientVertexBuffer* _tvb, const bgfx::VertexDecl& _decl, uint16_t _numVertices, bgfx::TransientIndexBuffer* _tib, uint16_t _numIndices); + /// Allocate instance data buffer. /// /// NOTE: diff --git a/premake/geometryc.lua b/premake/geometryc.lua index a185a3f13..ec9eea337 100644 --- a/premake/geometryc.lua +++ b/premake/geometryc.lua @@ -11,6 +11,7 @@ project "geometryc" BX_DIR .. "include", BGFX_DIR .. "include", BGFX_DIR .. "3rdparty/forsyth-too", + BGFX_DIR .. "examples/common", } files { @@ -19,6 +20,7 @@ project "geometryc" BGFX_DIR .. "src/vertexdecl.**", BGFX_DIR .. "tools/geometryc/**.cpp", BGFX_DIR .. "tools/geometryc/**.h", + BGFX_DIR .. "examples/common/bounds.**", } configuration { "osx" } diff --git a/src/bgfx.cpp b/src/bgfx.cpp index c9ff1cf3a..0714a6ea9 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -1912,6 +1912,18 @@ namespace bgfx return s_ctx->allocTransientVertexBuffer(_tvb, _num, _decl); } + bool allocTransientBuffers(bgfx::TransientVertexBuffer* _tvb, const bgfx::VertexDecl& _decl, uint16_t _numVertices, bgfx::TransientIndexBuffer* _tib, uint16_t _numIndices) + { + if (checkAvailTransientBuffers(_numVertices, _decl, _numIndices) ) + { + allocTransientVertexBuffer(_tvb, _numVertices, _decl); + allocTransientIndexBuffer(_tib, _numIndices); + return true; + } + + return false; + } + const InstanceDataBuffer* allocInstanceDataBuffer(uint32_t _num, uint16_t _stride) { BGFX_CHECK_MAIN_THREAD(); diff --git a/src/config.h b/src/config.h index 389f71707..ffd476fa1 100644 --- a/src/config.h +++ b/src/config.h @@ -126,7 +126,7 @@ #endif // BGFX_CONFIG_MAX_MATRIX_CACHE #ifndef BGFX_CONFIG_MAX_RECT_CACHE -# define BGFX_CONFIG_MAX_RECT_CACHE 512 +# define BGFX_CONFIG_MAX_RECT_CACHE (4<<10) #endif // BGFX_CONFIG_MAX_RECT_CACHE #ifndef BGFX_CONFIG_MAX_VIEWS diff --git a/tools/geometryc/geometryc.cpp b/tools/geometryc/geometryc.cpp index 399104734..2c348ed38 100644 --- a/tools/geometryc/geometryc.cpp +++ b/tools/geometryc/geometryc.cpp @@ -60,7 +60,7 @@ #include "tokenizecmd.h" #include "bounds.h" -#include "math.h" +#include "fpumath.h" struct Vector3 { diff --git a/tools/geometryc/math.h b/tools/geometryc/math.h deleted file mode 100644 index 2eb0ca63e..000000000 --- a/tools/geometryc/math.h +++ /dev/null @@ -1,349 +0,0 @@ -/* - * Copyright 2011-2014 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -// FPU math lib - -#ifndef FPU_MATH_H_HEADER_GUARD -#define FPU_MATH_H_HEADER_GUARD - -#define _USE_MATH_DEFINES -#include -#include - -inline float fmin(float _a, float _b) -{ - return _a < _b ? _a : _b; -} - -inline float fmax(float _a, float _b) -{ - return _a > _b ? _a : _b; -} - -inline float flerp(float _a, float _b, float _t) -{ - return _a + (_b - _a) * _t; -} - -inline void vec3Add(float* __restrict _result, const float* __restrict _a, const float* __restrict _b) -{ - _result[0] = _a[0] + _b[0]; - _result[1] = _a[1] + _b[1]; - _result[2] = _a[2] + _b[2]; -} - -inline void vec3Sub(float* __restrict _result, const float* __restrict _a, const float* __restrict _b) -{ - _result[0] = _a[0] - _b[0]; - _result[1] = _a[1] - _b[1]; - _result[2] = _a[2] - _b[2]; -} - -inline void vec3Mul(float* __restrict _result, const float* __restrict _a, const float* __restrict _b) -{ - _result[0] = _a[0] * _b[0]; - _result[1] = _a[1] * _b[1]; - _result[2] = _a[2] * _b[2]; -} - -inline void vec3Mul(float* __restrict _result, const float* __restrict _a, float _b) -{ - _result[0] = _a[0] * _b; - _result[1] = _a[1] * _b; - _result[2] = _a[2] * _b; -} - -inline float vec3Dot(const float* __restrict _a, const float* __restrict _b) -{ - return _a[0]*_b[0] + _a[1]*_b[1] + _a[2]*_b[2]; -} - -inline void vec3Cross(float* __restrict _result, const float* __restrict _a, const float* __restrict _b) -{ - _result[0] = _a[1]*_b[2] - _a[2]*_b[1]; - _result[1] = _a[2]*_b[0] - _a[0]*_b[2]; - _result[2] = _a[0]*_b[1] - _a[1]*_b[0]; -} - -inline void vec3Norm(float* __restrict _result, const float* __restrict _a) -{ - float scale = 1.0f/sqrtf(vec3Dot(_a, _a) ); - _result[0] = _a[0] * scale; - _result[1] = _a[1] * scale; - _result[2] = _a[2] * scale; -} - -inline void mtxIdentity(float* _result) -{ - memset(_result, 0, sizeof(float)*16); - _result[0] = _result[5] = _result[10] = _result[15] = 1.0f; -} - -inline void mtxLookAt(float* __restrict _result, const float* __restrict _eye, const float* __restrict _at) -{ - float tmp[4]; - vec3Sub(tmp, _at, _eye); - - float view[4]; - vec3Norm(view, tmp); - - float up[3] = { 0.0f, 1.0f, 0.0f }; - vec3Cross(tmp, up, view); - - float right[4]; - vec3Norm(right, tmp); - - vec3Cross(up, view, right); - - memset(_result, 0, sizeof(float)*16); - _result[ 0] = right[0]; - _result[ 1] = up[0]; - _result[ 2] = view[0]; - - _result[ 4] = right[1]; - _result[ 5] = up[1]; - _result[ 6] = view[1]; - - _result[ 8] = right[2]; - _result[ 9] = up[2]; - _result[10] = view[2]; - - _result[12] = -vec3Dot(right, _eye); - _result[13] = -vec3Dot(up, _eye); - _result[14] = -vec3Dot(view, _eye); - _result[15] = 1.0f; -} - -inline void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far) -{ - float height = 1.0f/tanf(_fovy*( (float)M_PI/180.0f)*0.5f); - float width = height * 1.0f/_aspect; - float aa = _far/(_far-_near); - float bb = -_near * aa; - - memset(_result, 0, sizeof(float)*16); - _result[0] = width; - _result[5] = height; - _result[10] = aa; - _result[11] = 1.0f; - _result[14] = bb; -} - -inline void mtxOrtho(float* _result, float _left, float _right, float _bottom, float _top, float _near, float _far) -{ - const float aa = 2.0f/(_right - _left); - const float bb = 2.0f/(_top - _bottom); - const float cc = 1.0f/(_far - _near); - const float dd = (_left + _right)/(_left - _right); - const float ee = (_top + _bottom)/(_bottom - _top); - const float ff = _near / (_near - _far); - - memset(_result, 0, sizeof(float)*16); - _result[0] = aa; - _result[5] = bb; - _result[10] = cc; - _result[12] = dd; - _result[13] = ee; - _result[14] = ff; - _result[15] = 1.0f; -} - -inline void mtxRotateX(float* _result, float _ax) -{ - float sx = sinf(_ax); - float cx = cosf(_ax); - - memset(_result, 0, sizeof(float)*16); - _result[ 0] = 1.0f; - _result[ 5] = cx; - _result[ 6] = -sx; - _result[ 9] = sx; - _result[10] = cx; - _result[15] = 1.0f; -} - -inline void mtxRotateY(float* _result, float _ay) -{ - float sy = sinf(_ay); - float cy = cosf(_ay); - - memset(_result, 0, sizeof(float)*16); - _result[ 0] = cy; - _result[ 2] = sy; - _result[ 5] = 1.0f; - _result[ 8] = -sy; - _result[10] = cy; - _result[15] = 1.0f; -} - -inline void mtxRotateZ(float* _result, float _az) -{ - float sz = sinf(_az); - float cz = cosf(_az); - - memset(_result, 0, sizeof(float)*16); - _result[ 0] = cz; - _result[ 1] = -sz; - _result[ 4] = sz; - _result[ 5] = cz; - _result[10] = 1.0f; - _result[15] = 1.0f; -} - -inline void mtxRotateXY(float* _result, float _ax, float _ay) -{ - float sx = sinf(_ax); - float cx = cosf(_ax); - float sy = sinf(_ay); - float cy = cosf(_ay); - - memset(_result, 0, sizeof(float)*16); - _result[ 0] = cy; - _result[ 2] = -sy; - _result[ 4] = -sx*sy; - _result[ 5] = cx; - _result[ 6] = -sx*cy; - _result[ 8] = cx*sy; - _result[ 9] = sx; - _result[10] = cx*cy; - _result[15] = 1.0f; -} - -inline void mtxRotateXYZ(float* _result, float _ax, float _ay, float _az) -{ - float sx = sinf(_ax); - float cx = cosf(_ax); - float sy = sinf(_ay); - float cy = cosf(_ay); - float sz = sinf(_az); - float cz = cosf(_az); - - memset(_result, 0, sizeof(float)*16); - _result[ 0] = cy*cz; - _result[ 1] = -cy*sz; - _result[ 2] = sy; - _result[ 4] = cz*sx*sy + cx*sz; - _result[ 5] = cx*cz - sx*sy*sz; - _result[ 6] = -cy*sx; - _result[ 8] = -cx*cz*sy + sx*sz; - _result[ 9] = cz*sx + cx*sy*sz; - _result[10] = cx*cy; - _result[15] = 1.0f; -} - -inline void mtxRotateZYX(float* _result, float _ax, float _ay, float _az) -{ - float sx = sinf(_ax); - float cx = cosf(_ax); - float sy = sinf(_ay); - float cy = cosf(_ay); - float sz = sinf(_az); - float cz = cosf(_az); - - memset(_result, 0, sizeof(float)*16); - _result[ 0] = cy*cz; - _result[ 1] = cz*sx*sy-cx*sz; - _result[ 2] = cx*cz*sy+sx*sz; - _result[ 4] = cy*sz; - _result[ 5] = cx*cz + sx*sy*sz; - _result[ 6] = -cz*sx + cx*sy*sz; - _result[ 8] = -sy; - _result[ 9] = cy*sx; - _result[10] = cx*cy; - _result[15] = 1.0f; -}; - -inline void vec3MulMtx(float* __restrict _result, const float* __restrict _vec, const float* __restrict _mat) -{ - _result[0] = _vec[0] * _mat[ 0] + _vec[1] * _mat[4] + _vec[2] * _mat[ 8] + _mat[12]; - _result[1] = _vec[0] * _mat[ 1] + _vec[1] * _mat[5] + _vec[2] * _mat[ 9] + _mat[13]; - _result[2] = _vec[0] * _mat[ 2] + _vec[1] * _mat[6] + _vec[2] * _mat[10] + _mat[14]; -} - -inline void vec4MulMtx(float* __restrict _result, const float* __restrict _vec, const float* __restrict _mat) -{ - _result[0] = _vec[0] * _mat[ 0] + _vec[1] * _mat[4] + _vec[2] * _mat[ 8] + _vec[3] * _mat[12]; - _result[1] = _vec[0] * _mat[ 1] + _vec[1] * _mat[5] + _vec[2] * _mat[ 9] + _vec[3] * _mat[13]; - _result[2] = _vec[0] * _mat[ 2] + _vec[1] * _mat[6] + _vec[2] * _mat[10] + _vec[3] * _mat[14]; - _result[3] = _vec[0] * _mat[ 3] + _vec[1] * _mat[7] + _vec[2] * _mat[11] + _vec[3] * _mat[15]; -} - -inline void mtxMul(float* __restrict _result, const float* __restrict _a, const float* __restrict _b) -{ - vec4MulMtx(&_result[ 0], &_a[ 0], _b); - vec4MulMtx(&_result[ 4], &_a[ 4], _b); - vec4MulMtx(&_result[ 8], &_a[ 8], _b); - vec4MulMtx(&_result[12], &_a[12], _b); -} - -inline void mtxTranspose(float* __restrict _result, const float* __restrict _a) -{ - _result[ 0] = _a[ 0]; - _result[ 4] = _a[ 1]; - _result[ 8] = _a[ 2]; - _result[12] = _a[ 3]; - _result[ 1] = _a[ 4]; - _result[ 5] = _a[ 5]; - _result[ 9] = _a[ 6]; - _result[13] = _a[ 7]; - _result[ 2] = _a[ 8]; - _result[ 6] = _a[ 9]; - _result[10] = _a[10]; - _result[14] = _a[11]; - _result[ 3] = _a[12]; - _result[ 7] = _a[13]; - _result[11] = _a[14]; - _result[15] = _a[15]; -} - -inline void mtxInverse(float* __restrict _result, const float* __restrict _a) -{ - float xx = _a[ 0]; - float xy = _a[ 1]; - float xz = _a[ 2]; - float xw = _a[ 3]; - float yx = _a[ 4]; - float yy = _a[ 5]; - float yz = _a[ 6]; - float yw = _a[ 7]; - float zx = _a[ 8]; - float zy = _a[ 9]; - float zz = _a[10]; - float zw = _a[11]; - float wx = _a[12]; - float wy = _a[13]; - float wz = _a[14]; - float ww = _a[15]; - - float det = 0.0f; - det += xx * (yy*(zz*ww - zw*wz) - yz*(zy*ww - zw*wy) + yw*(zy*wz - zz*wy) ); - det -= xy * (yx*(zz*ww - zw*wz) - yz*(zx*ww - zw*wx) + yw*(zx*wz - zz*wx) ); - det += xz * (yx*(zy*ww - zw*wy) - yy*(zx*ww - zw*wx) + yw*(zx*wy - zy*wx) ); - det -= xw * (yx*(zy*wz - zz*wy) - yy*(zx*wz - zz*wx) + yz*(zx*wy - zy*wx) ); - - float invDet = 1.0f/det; - - _result[ 0] = +(yy*(zz*ww - wz*zw) - yz*(zy*ww - wy*zw) + yw*(zy*wz - wy*zz) ) * invDet; - _result[ 1] = -(xy*(zz*ww - wz*zw) - xz*(zy*ww - wy*zw) + xw*(zy*wz - wy*zz) ) * invDet; - _result[ 2] = +(xy*(yz*ww - wz*yw) - xz*(yy*ww - wy*yw) + xw*(yy*wz - wy*yz) ) * invDet; - _result[ 3] = -(xy*(yz*zw - zz*yw) - xz*(yy*zw - zy*yw) + xw*(yy*zz - zy*yz) ) * invDet; - - _result[ 4] = -(yx*(zz*ww - wz*zw) - yz*(zx*ww - wx*zw) + yw*(zx*wz - wx*zz) ) * invDet; - _result[ 5] = +(xx*(zz*ww - wz*zw) - xz*(zx*ww - wx*zw) + xw*(zx*wz - wx*zz) ) * invDet; - _result[ 6] = -(xx*(yz*ww - wz*yw) - xz*(yx*ww - wx*yw) + xw*(yx*wz - wx*yz) ) * invDet; - _result[ 7] = +(xx*(yz*zw - zz*yw) - xz*(yx*zw - zx*yw) + xw*(yx*zz - zx*yz) ) * invDet; - - _result[ 8] = +(yx*(zy*ww - wy*zw) - yy*(zx*ww - wx*zw) + yw*(zx*wy - wx*zy) ) * invDet; - _result[ 9] = -(xx*(zy*ww - wy*zw) - xy*(zx*ww - wx*zw) + xw*(zx*wy - wx*zy) ) * invDet; - _result[10] = +(xx*(yy*ww - wy*yw) - xy*(yx*ww - wx*yw) + xw*(yx*wy - wx*yy) ) * invDet; - _result[11] = -(xx*(yy*zw - zy*yw) - xy*(yx*zw - zx*yw) + xw*(yx*zy - zx*yy) ) * invDet; - - _result[12] = -(yx*(zy*wz - wy*zz) - yy*(zx*wz - wx*zz) + yz*(zx*wy - wx*zy) ) * invDet; - _result[13] = +(xx*(zy*wz - wy*zz) - xy*(zx*wz - wx*zz) + xz*(zx*wy - wx*zy) ) * invDet; - _result[14] = -(xx*(yy*wz - wy*yz) - xy*(yx*wz - wx*yz) + xz*(yx*wy - wx*yy) ) * invDet; - _result[15] = +(xx*(yy*zz - zy*yz) - xy*(yx*zz - zx*yz) + xz*(yx*zy - zx*yy) ) * invDet; -} - -#endif // FPU_MATH_H_HEADER_GUARD