From 7b98546fd7d44818007993f5846156254c977714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Thu, 20 Dec 2018 21:49:10 -0800 Subject: [PATCH] Removing old vector math. --- examples/13-stencil/stencil.cpp | 66 +++++++++------------ examples/14-shadowvolumes/shadowvolumes.cpp | 29 ++++----- examples/16-shadowmaps/shadowmaps.cpp | 11 ++-- examples/27-terrain/terrain.cpp | 26 ++++---- 4 files changed, 57 insertions(+), 75 deletions(-) diff --git a/examples/13-stencil/stencil.cpp b/examples/13-stencil/stencil.cpp index 5308757e8..721bc89d5 100644 --- a/examples/13-stencil/stencil.cpp +++ b/examples/13-stencil/stencil.cpp @@ -162,40 +162,39 @@ void setViewRectMask(uint32_t _viewMask, uint16_t _x, uint16_t _y, uint16_t _wid } } -void mtxReflected(float*__restrict _result - , const float* __restrict _p /* plane */ - , const float* __restrict _n /* normal */ - ) +void mtxReflected(float* _result, const bx::Vec3& _pos, const bx::Vec3& _normal) { - float dot = bx::vec3Dot(_p, _n); + const float nx = _normal.x; + const float ny = _normal.y; + const float nz = _normal.z; - _result[ 0] = 1.0f - 2.0f * _n[0] * _n[0]; //1-2Nx^2 - _result[ 1] = -2.0f * _n[0] * _n[1]; //-2*Nx*Ny - _result[ 2] = -2.0f * _n[0] * _n[2]; //-2*NxNz - _result[ 3] = 0.0f; //0 + _result[ 0] = 1.0f - 2.0f * nx * nx; + _result[ 1] = - 2.0f * nx * ny; + _result[ 2] = - 2.0f * nx * nz; + _result[ 3] = 0.0f; - _result[ 4] = -2.0f * _n[0] * _n[1]; //-2*NxNy - _result[ 5] = 1.0f - 2.0f * _n[1] * _n[1]; //1-2*Ny^2 - _result[ 6] = -2.0f * _n[1] * _n[2]; //-2*NyNz - _result[ 7] = 0.0f; //0 + _result[ 4] = - 2.0f * nx * ny; + _result[ 5] = 1.0f - 2.0f * ny * ny; + _result[ 6] = - 2.0f * ny * nz; + _result[ 7] = 0.0f; - _result[ 8] = -2.0f * _n[0] * _n[2]; //-2*NxNz - _result[ 9] = -2.0f * _n[1] * _n[2]; //-2NyNz - _result[10] = 1.0f - 2.0f * _n[2] * _n[2]; //1-2*Nz^2 - _result[11] = 0.0f; //0 + _result[ 8] = - 2.0f * nx * nz; + _result[ 9] = - 2.0f * ny * nz; + _result[10] = 1.0f - 2.0f * nz * nz; + _result[11] = 0.0f; - _result[12] = 2.0f * dot * _n[0]; //2*dot*Nx - _result[13] = 2.0f * dot * _n[1]; //2*dot*Ny - _result[14] = 2.0f * dot * _n[2]; //2*dot*Nz - _result[15] = 1.0f; //1 + const float dot = bx::dot(_pos, _normal); + + _result[12] = 2.0f * dot * nx; + _result[13] = 2.0f * dot * ny; + _result[14] = 2.0f * dot * nz; + _result[15] = 1.0f; } -void mtxShadow(float* __restrict _result - , const float* __restrict _ground - , const float* __restrict _light - ) +void mtxShadow(float* _result, const float* _ground, const float* _light) { - float dot = _ground[0] * _light[0] + const float dot = + _ground[0] * _light[0] + _ground[1] * _light[1] + _ground[2] * _light[2] + _ground[3] * _light[3] @@ -222,10 +221,7 @@ void mtxShadow(float* __restrict _result _result[15] = dot - _light[3] * _ground[3]; } -void mtxBillboard(float* __restrict _result - , const float* __restrict _view - , const float* __restrict _pos - , const float* __restrict _scale) +void mtxBillboard(float* _result, const float* _view, const float* _pos, const float* _scale) { _result[ 0] = _view[0] * _scale[0]; _result[ 1] = _view[4] * _scale[0]; @@ -1125,9 +1121,7 @@ public: // Compute reflected matrix. float reflectMtx[16]; - float plane_pos[3] = { 0.0f, 0.01f, 0.0f }; - float normal[3] = { 0.0f, 1.0f, 0.0f }; - mtxReflected(reflectMtx, plane_pos, normal); + mtxReflected(reflectMtx, { 0.0f, 0.01f, 0.0f }, { 0.0f, 1.0f, 0.0f }); // Reflect lights. float reflectedLights[MAX_NUM_LIGHTS][4]; @@ -1226,11 +1220,7 @@ public: } // Ground plane. - float ground[4]; - float plane_pos[3] = { 0.0f, 0.0f, 0.0f }; - float normal[3] = { 0.0f, 1.0f, 0.0f }; - bx::memCopy(ground, normal, sizeof(float) * 3); - ground[3] = -bx::vec3Dot(plane_pos, normal) - 0.01f; // - 0.01 against z-fighting + float ground[4] = { 0.0f, 1.0f, 0.0f, -bx::dot(bx::Vec3{ 0.0f, 0.0f, 0.0f }, bx::Vec3{ 0.0f, 1.0f, 0.0f }) - 0.01f }; for (uint8_t ii = 0, viewId = RENDER_VIEWID_RANGE5_PASS_6; ii < numLights; ++ii, ++viewId) { diff --git a/examples/14-shadowvolumes/shadowvolumes.cpp b/examples/14-shadowvolumes/shadowvolumes.cpp index da04a3a42..05e633690 100644 --- a/examples/14-shadowvolumes/shadowvolumes.cpp +++ b/examples/14-shadowvolumes/shadowvolumes.cpp @@ -1381,7 +1381,7 @@ void shadowVolumeCreate(ShadowVolume& _shadowVolume const Face& face = *iter; bool frontFacing = false; - float f = bx::vec3Dot(face.m_plane, _light) + face.m_plane[3]; + const float f = bx::dot(bx::load(face.m_plane), bx::load(_light) ) + face.m_plane[3]; if (f > 0.0f) { frontFacing = true; @@ -1569,8 +1569,8 @@ void shadowVolumeCreate(ShadowVolume& _shadowVolume const Edge& edge = edges[ii]; const Plane* edgePlane = &edgePlanes[ii*2]; - int16_t s0 = ( (vec3Dot(edgePlane[0].m_plane, _light) + edgePlane[0].m_plane[3]) > 0.0f) ^ edge.m_faceReverseOrder[0]; - int16_t s1 = ( (vec3Dot(edgePlane[1].m_plane, _light) + edgePlane[1].m_plane[3]) > 0.0f) ^ edge.m_faceReverseOrder[1]; + int16_t s0 = ( (bx::dot(bx::load(edgePlane[0].m_plane), bx::load(_light) ) + edgePlane[0].m_plane[3]) > 0.0f) ^ edge.m_faceReverseOrder[0]; + int16_t s1 = ( (bx::dot(bx::load(edgePlane[1].m_plane), bx::load(_light) ) + edgePlane[1].m_plane[3]) > 0.0f) ^ edge.m_faceReverseOrder[1]; int16_t kk = ( (s0 + s1) << 1) - 2; if (kk != 0) @@ -1607,7 +1607,7 @@ void shadowVolumeCreate(ShadowVolume& _shadowVolume { const Face& face = *iter; - float f = bx::vec3Dot(face.m_plane, _light) + face.m_plane[3]; + const float f = bx::dot(bx::load(face.m_plane), bx::load(_light) ) + face.m_plane[3]; bool frontFacing = (f > 0.0f); for (uint8_t ii = 0, num = 1 + uint8_t(!_textureAsStencil); ii < num; ++ii) @@ -1699,8 +1699,8 @@ void createNearClipVolume(float* __restrict _outPlanes24f const float delta = 0.1f; - float nearNormal[4] = { 0.0f, 0.0f, 1.0f, _near }; - float d = bx::vec3Dot(lightPosV, nearNormal) + lightPosV[3] * nearNormal[3]; + const float nearNormal[4] = { 0.0f, 0.0f, 1.0f, _near }; + const float d = bx::dot(bx::load(lightPosV), bx::load(nearNormal) ) + lightPosV[3] * nearNormal[3]; // Light is: // 1.0f - in front of near plane @@ -1755,17 +1755,14 @@ void createNearClipVolume(float* __restrict _outPlanes24f bx::vec4MulMtx(volumePlanes[4], nearPlaneV, mtxViewTrans); float* lightPlane = volumePlanes[5]; - float lightPlaneNormal[3] = { 0.0f, 0.0f, -_near * lightSide }; - float tmp[3]; - bx::vec3MulMtx(tmp, lightPlaneNormal, mtxViewInv); - bx::vec3Sub(lightPlaneNormal, tmp, _lightPos); + const bx::Vec3 lightPlaneNormal = bx::sub(bx::mul({ 0.0f, 0.0f, -_near * lightSide }, mtxViewInv), bx::load(_lightPos) ); - float lenInv = 1.0f / bx::sqrt(bx::vec3Dot(lightPlaneNormal, lightPlaneNormal) ); + float lenInv = 1.0f / bx::sqrt(bx::dot(lightPlaneNormal, lightPlaneNormal) ); - lightPlane[0] = lightPlaneNormal[0] * lenInv; - lightPlane[1] = lightPlaneNormal[1] * lenInv; - lightPlane[2] = lightPlaneNormal[2] * lenInv; - lightPlane[3] = -bx::vec3Dot(lightPlaneNormal, _lightPos) * lenInv; + lightPlane[0] = lightPlaneNormal.x * lenInv; + lightPlane[1] = lightPlaneNormal.y * lenInv; + lightPlane[2] = lightPlaneNormal.z * lenInv; + lightPlane[3] = -bx::dot(lightPlaneNormal, bx::load(_lightPos) ) * lenInv; } bool clipTest(const float* _planes, uint8_t _planeNum, const Mesh& _mesh, const float* _scale, const float* _translate) @@ -1789,7 +1786,7 @@ bool clipTest(const float* _planes, uint8_t _planeNum, const Mesh& _mesh, const { const float* plane = volumePlanes[ii]; - float positiveSide = bx::vec3Dot(plane, sphere.m_center) + plane[3] + sphere.m_radius; + float positiveSide = bx::dot(bx::load(plane), bx::load(sphere.m_center) ) + plane[3] + sphere.m_radius; if (positiveSide < 0.0f) { diff --git a/examples/16-shadowmaps/shadowmaps.cpp b/examples/16-shadowmaps/shadowmaps.cpp index 4c9a02060..391e8a5b0 100644 --- a/examples/16-shadowmaps/shadowmaps.cpp +++ b/examples/16-shadowmaps/shadowmaps.cpp @@ -2333,9 +2333,8 @@ public: lightProj[ProjType::Horizontal][14] /= currentSmSettings->m_far; } - float at[3]; - bx::vec3Add(at, m_pointLight.m_position.m_v, m_pointLight.m_spotDirectionInner.m_v); - bx::mtxLookAt(lightView[TetrahedronFaces::Green], bx::load(m_pointLight.m_position.m_v), bx::load(at) ); + const bx::Vec3 at = bx::add(bx::load(m_pointLight.m_position.m_v), bx::load(m_pointLight.m_spotDirectionInner.m_v) ); + bx::mtxLookAt(lightView[TetrahedronFaces::Green], bx::load(m_pointLight.m_position.m_v), at); } else if (LightType::PointLight == m_settings.m_lightType) { @@ -2404,9 +2403,9 @@ public: float tmp[3] = { - -bx::vec3Dot(m_pointLight.m_position.m_v, &mtxTmp[0]), - -bx::vec3Dot(m_pointLight.m_position.m_v, &mtxTmp[4]), - -bx::vec3Dot(m_pointLight.m_position.m_v, &mtxTmp[8]), + -bx::dot(bx::load(m_pointLight.m_position.m_v), bx::load(&mtxTmp[0]) ), + -bx::dot(bx::load(m_pointLight.m_position.m_v), bx::load(&mtxTmp[4]) ), + -bx::dot(bx::load(m_pointLight.m_position.m_v), bx::load(&mtxTmp[8]) ), }; bx::mtxTranspose(mtxYpr[ii], mtxTmp); diff --git a/examples/27-terrain/terrain.cpp b/examples/27-terrain/terrain.cpp index 535bdff97..25e4161a7 100644 --- a/examples/27-terrain/terrain.cpp +++ b/examples/27-terrain/terrain.cpp @@ -357,30 +357,26 @@ public: float ray_world[4]; bx::vec4MulMtx(ray_world, ray_eye, invViewMtx); - float ray_dir[3]; - bx::store(ray_dir, bx::normalize(bx::load(ray_world) ) ); - ray_dir[0] *= -1.0; - ray_dir[1] *= -1.0; - ray_dir[2] *= -1.0; + const bx::Vec3 rayDir = bx::mul(bx::normalize(bx::load(ray_world) ), -1.0f); - float pos[3]; - cameraGetPosition(pos); + bx::Vec3 pos; + cameraGetPosition(&pos.x); for (int i = 0; i < 1000; ++i) { - bx::vec3Add(pos, pos, ray_dir); + pos = bx::add(pos, rayDir); - if (pos[0] < 0 - || pos[0] >= s_terrainSize - || pos[2] < 0 - || pos[2] >= s_terrainSize) + if (pos.x < 0 + || pos.x >= s_terrainSize + || pos.z < 0 + || pos.z >= s_terrainSize) { continue; } - uint32_t heightMapPos = ( (uint32_t)pos[2] * s_terrainSize) + (uint32_t)pos[0]; - if ( pos[1] < m_terrain.m_heightMap[heightMapPos] ) + uint32_t heightMapPos = ( (uint32_t)pos.z * s_terrainSize) + (uint32_t)pos.x; + if (pos.y < m_terrain.m_heightMap[heightMapPos]) { - paintTerrainHeight( (uint32_t)pos[0], (uint32_t)pos[2]); + paintTerrainHeight( (uint32_t)pos.x, (uint32_t)pos.z); return; } }