diff --git a/GPU/Directx9/ShaderManagerDX9.cpp b/GPU/Directx9/ShaderManagerDX9.cpp index 2a232b951c..05da5bba0b 100644 --- a/GPU/Directx9/ShaderManagerDX9.cpp +++ b/GPU/Directx9/ShaderManagerDX9.cpp @@ -25,6 +25,7 @@ #include "i18n/i18n.h" #include "math/lin/matrix4x4.h" #include "math/math_util.h" +#include "math/dataconv.h" #include "util/text/utf8.h" #include "Common/Common.h" @@ -140,23 +141,15 @@ std::string VSShader::GetShaderString(DebugShaderStringType type) const { } void ShaderManagerDX9::PSSetColorUniform3(int creg, u32 color) { - const float col[4] = { - ((color & 0xFF)) * (1.0f / 255.0f), - ((color & 0xFF00) >> 8) * (1.0f / 255.0f), - ((color & 0xFF0000) >> 16) * (1.0f / 255.0f), - 0.0f - }; - device_->SetPixelShaderConstantF(creg, col, 1); + float f[4]; + Uint8x3ToFloat4(f, color); + device_->SetPixelShaderConstantF(creg, f, 1); } void ShaderManagerDX9::PSSetColorUniform3Alpha255(int creg, u32 color, u8 alpha) { - const float col[4] = { - (float)((color & 0xFF)), - (float)((color & 0xFF00) >> 8), - (float)((color & 0xFF0000) >> 16), - (float)alpha, - }; - device_->SetPixelShaderConstantF(creg, col, 1); + float f[4]; + Uint8x3ToFloat4_AlphaUint8(f, color, alpha); + device_->SetPixelShaderConstantF(creg, f, 1); } void ShaderManagerDX9::PSSetFloat(int creg, float value) { @@ -187,13 +180,9 @@ void ShaderManagerDX9::VSSetFloatArray(int creg, const float *value, int count) // Utility void ShaderManagerDX9::VSSetColorUniform3(int creg, u32 color) { - const float col[4] = { - ((color & 0xFF)) / 255.0f, - ((color & 0xFF00) >> 8) / 255.0f, - ((color & 0xFF0000) >> 16) / 255.0f, - 0.0f - }; - device_->SetVertexShaderConstantF(creg, col, 1); + float f[4]; + Uint8x3ToFloat4(f, color); + device_->SetVertexShaderConstantF(creg, f, 1); } void ShaderManagerDX9::VSSetFloatUniform4(int creg, float data[4]) { @@ -201,20 +190,15 @@ void ShaderManagerDX9::VSSetFloatUniform4(int creg, float data[4]) { } void ShaderManagerDX9::VSSetFloat24Uniform3(int creg, const u32 data[3]) { - const u32 col[4] = { - data[0] << 8, data[1] << 8, data[2] << 8, 0 - }; - device_->SetVertexShaderConstantF(creg, (const float *)&col[0], 1); + float f[4]; + ExpandFloat24x3ToFloat4(f, data); + device_->SetVertexShaderConstantF(creg, f, 1); } void ShaderManagerDX9::VSSetColorUniform3Alpha(int creg, u32 color, u8 alpha) { - const float col[4] = { - ((color & 0xFF)) / 255.0f, - ((color & 0xFF00) >> 8) / 255.0f, - ((color & 0xFF0000) >> 16) / 255.0f, - alpha/255.0f - }; - device_->SetVertexShaderConstantF(creg, col, 1); + float f[4]; + Uint8x3ToFloat4_AlphaUint8(f, color, alpha); + device_->SetVertexShaderConstantF(creg, f, 1); } void ShaderManagerDX9::VSSetColorUniform3ExtraFloat(int creg, u32 color, float extra) { diff --git a/GPU/Directx9/ShaderManagerDX9.h b/GPU/Directx9/ShaderManagerDX9.h index 2bbf49877f..39932e6c91 100644 --- a/GPU/Directx9/ShaderManagerDX9.h +++ b/GPU/Directx9/ShaderManagerDX9.h @@ -95,16 +95,16 @@ public: private: void PSUpdateUniforms(u64 dirtyUniforms); void VSUpdateUniforms(u64 dirtyUniforms); - void PSSetColorUniform3Alpha255(int creg, u32 color, u8 alpha); - void PSSetColorUniform3(int creg, u32 color); - void PSSetFloat(int creg, float value); - void PSSetFloatArray(int creg, const float *value, int count); + inline void PSSetColorUniform3Alpha255(int creg, u32 color, u8 alpha); + inline void PSSetColorUniform3(int creg, u32 color); + inline void PSSetFloat(int creg, float value); + inline void PSSetFloatArray(int creg, const float *value, int count); void VSSetMatrix4x3(int creg, const float *m4x3); void VSSetMatrix4x3_3(int creg, const float *m4x3); - void VSSetColorUniform3(int creg, u32 color); - void VSSetColorUniform3ExtraFloat(int creg, u32 color, float extra); - void VSSetColorUniform3Alpha(int creg, u32 color, u8 alpha); + inline void VSSetColorUniform3(int creg, u32 color); + inline void VSSetColorUniform3ExtraFloat(int creg, u32 color, float extra); + inline void VSSetColorUniform3Alpha(int creg, u32 color, u8 alpha); void VSSetMatrix(int creg, const float* pMatrix); void VSSetFloat(int creg, float value); void VSSetFloatArray(int creg, const float *value, int count); diff --git a/GPU/GLES/ShaderManagerGLES.cpp b/GPU/GLES/ShaderManagerGLES.cpp index 1fde946b53..f7ef3692a9 100644 --- a/GPU/GLES/ShaderManagerGLES.cpp +++ b/GPU/GLES/ShaderManagerGLES.cpp @@ -22,6 +22,7 @@ #include #include +#include "math/dataconv.h" #include "base/logging.h" #include "base/timeutil.h" #include "gfx/gl_debug_log.h" @@ -324,22 +325,15 @@ LinkedShader::~LinkedShader() { // Utility static void SetColorUniform3(int uniform, u32 color) { - const float col[3] = { - ((color & 0xFF)) / 255.0f, - ((color & 0xFF00) >> 8) / 255.0f, - ((color & 0xFF0000) >> 16) / 255.0f - }; - glUniform3fv(uniform, 1, col); + float f[4]; + Uint8x4ToFloat4(f, color); + glUniform3fv(uniform, 1, f); } static void SetColorUniform3Alpha(int uniform, u32 color, u8 alpha) { - const float col[4] = { - ((color & 0xFF)) / 255.0f, - ((color & 0xFF00) >> 8) / 255.0f, - ((color & 0xFF0000) >> 16) / 255.0f, - alpha/255.0f - }; - glUniform4fv(uniform, 1, col); + float f[4]; + Uint8x3ToFloat4_AlphaUint8(f, color, alpha); + glUniform4fv(uniform, 1, f); } // This passes colors unscaled (e.g. 0 - 255 not 0 - 1.) @@ -383,11 +377,10 @@ static void SetColorUniform3ExtraFloat(int uniform, u32 color, float extra) { glUniform4fv(uniform, 1, col); } -static void SetFloat24Uniform3(int uniform, const u32 data[3]) { - const u32 col[3] = { - data[0] << 8, data[1] << 8, data[2] << 8 - }; - glUniform3fv(uniform, 1, (const GLfloat *)&col[0]); +static void SetFloat24Uniform3(int uniform, const uint32_t data[3]) { + float f[4]; + ExpandFloat24x3ToFloat4(f, data); + glUniform3fv(uniform, 1, f); } static void SetFloatUniform4(int uniform, float data[4]) { diff --git a/ext/native/math/dataconv.h b/ext/native/math/dataconv.h index 8c93fffed0..316d8fc1f7 100644 --- a/ext/native/math/dataconv.h +++ b/ext/native/math/dataconv.h @@ -134,7 +134,7 @@ 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]) { +inline void ExpandFloat24x3ToFloat4(float dest[4], const uint32_t src[3]) { #ifdef _M_SSE __m128i values = _mm_slli_epi32(_mm_load_si128((const __m128i *)src), 8); _mm_storeu_si128((__m128i *)dest, values);