Skip to content

Commit

Permalink
Also apply this to OpenGL and D3D9
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Jan 11, 2023
1 parent 67bd550 commit b524ded
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 43 deletions.
18 changes: 18 additions & 0 deletions Common/Data/Convert/SmallDataConvert.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cstdint>
#include <cstring>
#include <cmath>

#include "Common/Common.h"
#include "ppsspp_config.h"
Expand Down Expand Up @@ -227,6 +228,23 @@ inline void ExpandFloat24x3ToFloat4(float dest[4], const uint32_t src[3]) {
#endif
}

// Note: If length is 0.0, it's gonna be left as 0.0 instead of trying to normalize. This is important.
inline void ExpandFloat24x3ToFloat4AndNormalize(float dest[4], const uint32_t src[3]) {
float temp[4];
ExpandFloat24x3ToFloat4(temp, src);
// TODO: Reuse code from NormalizedOr001 and optimize
float x = temp[0];
float y = temp[1];
float z = temp[2];
float len = sqrtf(x * x + y * y + z * z);
if (len != 0.0f)
len = 1.0f / len;
dest[0] = x * len;
dest[1] = y * len;
dest[2] = z * len;
dest[3] = 0.0f;
}

inline uint32_t BytesToUint32(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
return (a) | (b << 8) | (c << 16) | (d << 24);
}
Expand Down
19 changes: 0 additions & 19 deletions GPU/Common/ShaderUniforms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,25 +306,6 @@ uint32_t PackLightControlBits() {
return lightControl;
}

// Note: If length is 0.0, it's gonna be left as 0.0 instead of normalized.
void ExpandFloat24x3ToFloat4AndNormalize(float dest[4], const uint32_t src[3]) {
float temp[4];
ExpandFloat24x3ToFloat4(temp, src);
// TODO: Reuse code from NormalizedOr001 and optimize
float x = temp[0];
float y = temp[1];
float z = temp[2];
float len = sqrtf(x * x + y * y + z * z);
if (len == 0.0f)
return;

len = 1.0f / len;
dest[0] = x * len;
dest[1] = y * len;
dest[2] = z * len;
dest[3] = 0.0f;
}

void LightUpdateUniforms(UB_VS_Lights *ub, uint64_t dirtyUniforms) {
// Lighting
if (dirtyUniforms & DIRTY_AMBIENT) {
Expand Down
20 changes: 8 additions & 12 deletions GPU/Directx9/ShaderManagerDX9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ void ShaderManagerDX9::VSSetFloat24Uniform3(int creg, const u32 data[3]) {
device_->SetVertexShaderConstantF(creg, f, 1);
}

void ShaderManagerDX9::VSSetFloat24Uniform3Normalized(int creg, const u32 data[3]) {
float f[4];
ExpandFloat24x3ToFloat4AndNormalize(f, data);
device_->SetVertexShaderConstantF(creg, f, 1);
}

void ShaderManagerDX9::VSSetColorUniform3Alpha(int creg, u32 color, u8 alpha) {
float f[4];
Uint8x3ToFloat4_AlphaUint8(f, color, alpha);
Expand Down Expand Up @@ -495,21 +501,11 @@ void ShaderManagerDX9::VSUpdateUniforms(u64 dirtyUniforms) {
for (int i = 0; i < 4; i++) {
if (dirtyUniforms & (DIRTY_LIGHT0 << i)) {
if (gstate.isDirectionalLight(i)) {
// Prenormalize
float x = getFloat24(gstate.lpos[i * 3 + 0]);
float y = getFloat24(gstate.lpos[i * 3 + 1]);
float z = getFloat24(gstate.lpos[i * 3 + 2]);
float len = sqrtf(x*x + y*y + z*z);
if (len == 0.0f)
len = 1.0f;
else
len = 1.0f / len;
float vec[3] = { x * len, y * len, z * len };
VSSetFloatArray(CONST_VS_LIGHTPOS + i, vec, 3);
VSSetFloat24Uniform3Normalized(CONST_VS_LIGHTPOS + i, &gstate.lpos[i * 3]);
} else {
VSSetFloat24Uniform3(CONST_VS_LIGHTPOS + i, &gstate.lpos[i * 3]);
}
VSSetFloat24Uniform3(CONST_VS_LIGHTDIR + i, &gstate.ldir[i * 3]);
VSSetFloat24Uniform3Normalized(CONST_VS_LIGHTDIR + i, &gstate.ldir[i * 3]);
VSSetFloat24Uniform3(CONST_VS_LIGHTATT + i, &gstate.latt[i * 3]);
float angle_spotCoef[4] = { getFloat24(gstate.lcutoff[i]), getFloat24(gstate.lconv[i]) };
VSSetFloatUniform4(CONST_VS_LIGHTANGLE_SPOTCOEF + i, angle_spotCoef);
Expand Down
1 change: 1 addition & 0 deletions GPU/Directx9/ShaderManagerDX9.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class ShaderManagerDX9 : public ShaderManagerCommon {
void VSSetFloat(int creg, float value);
void VSSetFloatArray(int creg, const float *value, int count);
void VSSetFloat24Uniform3(int creg, const u32 data[3]);
void VSSetFloat24Uniform3Normalized(int creg, const u32 data[3]);
void VSSetFloatUniform4(int creg, const float data[4]);

void Clear();
Expand Down
21 changes: 9 additions & 12 deletions GPU/GLES/ShaderManagerGLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ static void SetFloat24Uniform3(GLRenderManager *render, GLint *uniform, const ui
render->SetUniformF(uniform, 3, f);
}

static void SetFloat24Uniform3Normalized(GLRenderManager *render, GLint *uniform, const uint32_t data[3]) {
float f[4];
ExpandFloat24x3ToFloat4AndNormalize(f, data);
render->SetUniformF(uniform, 3, f);
}

static void SetFloatUniform4(GLRenderManager *render, GLint *uniform, float data[4]) {
render->SetUniformF(uniform, 4, data);
}
Expand Down Expand Up @@ -650,21 +656,12 @@ void LinkedShader::UpdateUniforms(const ShaderID &vsid, bool useBufferedRenderin
for (int i = 0; i < 4; i++) {
if (dirty & (DIRTY_LIGHT0 << i)) {
if (gstate.isDirectionalLight(i)) {
// Prenormalize
float x = getFloat24(gstate.lpos[i * 3 + 0]);
float y = getFloat24(gstate.lpos[i * 3 + 1]);
float z = getFloat24(gstate.lpos[i * 3 + 2]);
float len = sqrtf(x*x + y*y + z*z);
if (len == 0.0f)
len = 1.0f;
else
len = 1.0f / len;
float vec[3] = { x * len, y * len, z * len };
render_->SetUniformF(&u_lightpos[i], 3, vec);
// Prenormalize for cheaper calculations in shader
SetFloat24Uniform3Normalized(render_, &u_lightpos[i], &gstate.lpos[i * 3]);
} else {
SetFloat24Uniform3(render_, &u_lightpos[i], &gstate.lpos[i * 3]);
}
if (u_lightdir[i] != -1) SetFloat24Uniform3(render_, &u_lightdir[i], &gstate.ldir[i * 3]);
if (u_lightdir[i] != -1) SetFloat24Uniform3Normalized(render_, &u_lightdir[i], &gstate.ldir[i * 3]);
if (u_lightatt[i] != -1) SetFloat24Uniform3(render_, &u_lightatt[i], &gstate.latt[i * 3]);
if (u_lightangle_spotCoef[i] != -1) {
float lightangle_spotCoef[2] = { getFloat24(gstate.lcutoff[i]), getFloat24(gstate.lconv[i]) };
Expand Down

0 comments on commit b524ded

Please sign in to comment.