From d67b1487121d155d3de351f1607dbc482d94c3ef Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Sat, 10 Oct 2015 16:41:19 +0200 Subject: [PATCH] Add math dataconv funcs for packed values. --- GPU/Directx9/ShaderManagerDX9.cpp | 4 +- GPU/GLES/StateMapping.cpp | 6 +-- ext/native/math/dataconv.h | 88 +++++++++++++++++++++++++++++++ ext/native/native.vcxproj | 1 + ext/native/native.vcxproj.filters | 3 ++ ext/native/thin3d/thin3d_gl.cpp | 10 +--- 6 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 ext/native/math/dataconv.h diff --git a/GPU/Directx9/ShaderManagerDX9.cpp b/GPU/Directx9/ShaderManagerDX9.cpp index 40831cba10..0d470ef4cf 100644 --- a/GPU/Directx9/ShaderManagerDX9.cpp +++ b/GPU/Directx9/ShaderManagerDX9.cpp @@ -404,12 +404,12 @@ void ShaderManagerDX9::VSUpdateUniforms(int dirtyUniforms) { } if (allDirty) { // Set them all with one call - glUniformMatrix4fv(u_bone, numBones, GL_FALSE, allBones); + //glUniformMatrix4fv(u_bone, numBones, GL_FALSE, allBones); } else { // Set them one by one. Could try to coalesce two in a row etc but too lazy. for (int i = 0; i < numBones; i++) { if (dirtyUniforms & (DIRTY_BONEMATRIX0 << i)) { - glUniformMatrix4fv(u_bone + i, 1, GL_FALSE, allBones + 16 * i); + //glUniformMatrix4fv(u_bone + i, 1, GL_FALSE, allBones + 16 * i); } } } diff --git a/GPU/GLES/StateMapping.cpp b/GPU/GLES/StateMapping.cpp index 1d3c5adc0a..e693bd3768 100644 --- a/GPU/GLES/StateMapping.cpp +++ b/GPU/GLES/StateMapping.cpp @@ -77,7 +77,7 @@ static const GLushort cullingMode[] = { GL_BACK, }; -static const GLushort ztests[] = { +static const GLushort compareOps[] = { GL_NEVER, GL_ALWAYS, GL_EQUAL, GL_NOTEQUAL, GL_LESS, GL_LEQUAL, GL_GREATER, GL_GEQUAL, }; @@ -289,7 +289,7 @@ void TransformDrawEngine::ApplyDrawState(int prim) { // Depth Test if (gstate.isDepthTestEnabled()) { glstate.depthTest.enable(); - glstate.depthFunc.set(ztests[gstate.getDepthTestFunction()]); + glstate.depthFunc.set(compareOps[gstate.getDepthTestFunction()]); glstate.depthWrite.set(gstate.isDepthWriteEnabled() || alwaysDepthWrite ? GL_TRUE : GL_FALSE); if (gstate.isDepthWriteEnabled() || alwaysDepthWrite) { framebufferManager_->SetDepthUpdated(); @@ -337,7 +337,7 @@ void TransformDrawEngine::ApplyDrawState(int prim) { // Stencil Test if (stencilState.enabled) { glstate.stencilTest.enable(); - glstate.stencilFunc.set(ztests[stencilState.testFunc], stencilState.testRef, stencilState.testMask); + glstate.stencilFunc.set(compareOps[stencilState.testFunc], stencilState.testRef, stencilState.testMask); glstate.stencilOp.set(stencilOps[stencilState.sFail], stencilOps[stencilState.zFail], stencilOps[stencilState.zPass]); glstate.stencilMask.set(stencilState.writeMask); } else { diff --git a/ext/native/math/dataconv.h b/ext/native/math/dataconv.h new file mode 100644 index 0000000000..046bb63f35 --- /dev/null +++ b/ext/native/math/dataconv.h @@ -0,0 +1,88 @@ +#pragma once + +#include +#include + +// Utilities useful for filling in std140-layout uniform buffers. + + +// LSBs in f[0], etc. +// Could be SSE optimized. +inline void Uint8x4ToFloat4(float f[4], uint32_t u) { + f[0] = ((u >> 0) & 0xFF) * (1.0f / 255.0f); + f[1] = ((u >> 8) & 0xFF) * (1.0f / 255.0f); + f[2] = ((u >> 16) & 0xFF) * (1.0f / 255.0f); + f[3] = ((u >> 24) & 0xFF) * (1.0f / 255.0f); +} + +inline void Uint8x3ToFloat4(float f[4], uint32_t u) { + f[0] = ((u >> 0) & 0xFF) * (1.0f / 255.0f); + f[1] = ((u >> 8) & 0xFF) * (1.0f / 255.0f); + f[2] = ((u >> 16) & 0xFF) * (1.0f / 255.0f); + f[3] = 0.0f; +} + +inline void Uint8x3ToInt4(int i[4], uint32_t u) { + i[0] = ((u >> 0) & 0xFF); + i[1] = ((u >> 8) & 0xFF); + i[2] = ((u >> 16) & 0xFF); + i[3] = 0; +} + +inline void Uint8x3ToFloat4_Alpha(float f[4], uint32_t u, float alpha) { + f[0] = ((u >> 0) & 0xFF) * (1.0f / 255.0f); + f[1] = ((u >> 8) & 0xFF) * (1.0f / 255.0f); + f[2] = ((u >> 16) & 0xFF) * (1.0f / 255.0f); + f[3] = alpha; +} + +inline void Uint8x3ToFloat4_AlphaUint8(float f[4], uint32_t u, uint8_t alpha) { + f[0] = ((u >> 0) & 0xFF) * (1.0f / 255.0f); + f[1] = ((u >> 8) & 0xFF) * (1.0f / 255.0f); + f[2] = ((u >> 16) & 0xFF) * (1.0f / 255.0f); + f[3] = alpha * (1.0f / 255.0f); +} + +inline void Uint8x1ToFloat4(float f[4], uint32_t u) { + f[0] = ((u >> 0) & 0xFF) * (1.0f / 255.0f); + f[1] = 0.0f; + f[2] = 0.0f; + f[3] = 0.0f; +} + +// These are just for readability. + +inline void CopyFloat2(float dest[2], const float src[2]) { + memcpy(dest, src, sizeof(float) * 2); +} + +inline void CopyFloat1To4(float dest[4], const float src) { + dest[0] = src; + dest[1] = 0.0f; + dest[2] = 0.0f; + dest[3] = 0.0f; +} + +inline void CopyFloat2To4(float dest[4], const float src[2]) { + memcpy(dest, src, sizeof(float) * 2); + dest[2] = 0.0f; + dest[3] = 0.0f; +} + +inline void CopyFloat3To4(float dest[4], const float src[3]) { + memcpy(dest, src, sizeof(float) * 3); + dest[3] = 0.0f; +} + +inline void CopyFloat4(float dest[4], const float src[4]) { + memcpy(dest, src, sizeof(float) * 4); +} + +inline void CopyMatrix4x4(float dest[16], const float src[16]) { + memcpy(dest, src, sizeof(float) * 16); +} + +inline void ExpandFloat24x3ToFloat4(float dest[4], uint32_t src[3]) { + uint32_t temp[4] = { src[0] << 8, src[1] << 8, src[2] << 8, 0 }; + memcpy(dest, temp, sizeof(float) * 4); +} \ No newline at end of file diff --git a/ext/native/native.vcxproj b/ext/native/native.vcxproj index d4527121a9..93d7860e03 100644 --- a/ext/native/native.vcxproj +++ b/ext/native/native.vcxproj @@ -247,6 +247,7 @@ + diff --git a/ext/native/native.vcxproj.filters b/ext/native/native.vcxproj.filters index 87ee09ad91..57404cf71c 100644 --- a/ext/native/native.vcxproj.filters +++ b/ext/native/native.vcxproj.filters @@ -305,6 +305,9 @@ file + + math + diff --git a/ext/native/thin3d/thin3d_gl.cpp b/ext/native/thin3d/thin3d_gl.cpp index 6ee2c5fcb7..04330a2c96 100644 --- a/ext/native/thin3d/thin3d_gl.cpp +++ b/ext/native/thin3d/thin3d_gl.cpp @@ -5,6 +5,7 @@ #include "base/logging.h" #include "image/zim_load.h" +#include "math/dataconv.h" #include "math/lin/matrix4x4.h" #include "thin3d/thin3d.h" #include "gfx/gl_common.h" @@ -74,13 +75,6 @@ static const char *glsl_fragment_prelude = "precision mediump float;\n" "#endif\n"; -static inline void Uint32ToFloat4(uint32_t u, float f[4]) { - f[0] = ((u >> 0) & 0xFF) * (1.0f / 255.0f); - f[1] = ((u >> 8) & 0xFF) * (1.0f / 255.0f); - f[2] = ((u >> 16) & 0xFF) * (1.0f / 255.0f); - f[3] = ((u >> 24) & 0xFF) * (1.0f / 255.0f); -} - class Thin3DGLBlendState : public Thin3DBlendState { public: bool enabled; @@ -810,7 +804,7 @@ void Thin3DGLContext::DrawUP(T3DPrimitive prim, Thin3DShaderSet *shaderSet, Thin void Thin3DGLContext::Clear(int mask, uint32_t colorval, float depthVal, int stencilVal) { float col[4]; - Uint32ToFloat4(colorval, col); + Uint8x4ToFloat4(col, colorval); GLuint glMask = 0; if (mask & T3DClear::COLOR) { glClearColor(col[0], col[1], col[2], col[3]);