Skip to content

Commit

Permalink
envpipe fixes; UG compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
aap committed Dec 31, 2019
1 parent 376c304 commit 4f272eb
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 70 deletions.
55 changes: 44 additions & 11 deletions shaders/ps/envCarPS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,65 @@ uniform sampler2D tex0 : register(s0);
uniform sampler2D tex1 : register(s1);
uniform sampler2D tex2 : register(s2);

float3 eye : register(c0);
float3 sunDir : register(c1);
float4 surfProps : register(c0);
float4 fxParams : register(c1);
float3 eye : register(c2);
//float3 directDir : register(c3);

float4 directCol : register(c5);
float4 lightCol[6] : register(c6);
float3 directDir : register(c12);
float3 lightDir[6] : register(c13);


//#define fresnel (fxParams.x)
#define power (fxParams.y)
#define lightmult (fxParams.z)
//#define shininess (fxParams.w)

//#define surfAmb (surfProps.x)
//#define surfDiff (surfProps.y)
#define surfSpec (surfProps.z)
//#define surfPrelight (surfProps.w)

struct PS_INPUT
{
float3 texcoord0 : TEXCOORD0;
float3 texcoord1 : TEXCOORD1;
float3 WorldNormal : TEXCOORD1;
float3 WorldPos : TEXCOORD2;
float4 color : COLOR0;
float4 envColor : COLOR1;
};

float
specTerm(float3 reflVec, float3 light, float pwr)
{
return pow(max(dot(reflVec, light), 0.0), pwr);
}

float4
main(PS_INPUT IN) : COLOR
{
float2 ReflPos = normalize(IN.texcoord1.xy) * (IN.texcoord1.z*0.5 + 0.5);
ReflPos = ReflPos*float2(0.5, -0.5) + float2(0.5, 0.5);
float4 env = tex2D(tex1, ReflPos);
float3 ViewVector = normalize(IN.WorldPos - eye);
float3 ReflVector = normalize(ViewVector - 2.0*dot(ViewVector, IN.WorldNormal)*IN.WorldNormal);
float3 EnvTex = ReflVector;

float4 spec = directCol*specTerm(ReflVector, -directDir, power);
// for(int i = 0; i < 6; i++) // too much for PS 2.0
for(int i = 0; i < 5; i++)
spec += lightCol[i]*specTerm(ReflVector, -lightDir[i], power*2) * 0.8;
spec *= surfSpec;

float4 diff = tex2D(tex0, IN.texcoord0.xy);
// float4 env = tex2D(tex1, IN.texcoord1.xy);
float4 mask = tex2D(tex2, IN.texcoord1.xy*0.5 + 0.5);
float4 mask = tex2D(tex2, EnvTex.xy*0.5 + 0.5);
float2 ReflPos = normalize(EnvTex.xy) * (EnvTex.z*0.5 + 0.5);
ReflPos = ReflPos*float2(0.5, -0.5) + float2(0.5, 0.5);
float4 env = tex2D(tex1, ReflPos)*lightmult*2;

float4 diffpass = diff*IN.color;
float4 envpass = env*mask;;
float4 final = lerp(diffpass, envpass, IN.envColor.r) + IN.envColor.g;
// final.rgb = IN.envColor.r;
float4 envpass = env*mask;
float4 specpass = spec;
float4 final = lerp(diffpass, envpass, IN.envColor) + specpass;
final.a = diffpass.a;
return final;
}
31 changes: 14 additions & 17 deletions shaders/vs/envCarVS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ float4 fxParams : register(c21);
float4x4 world : register(c30);
float3 eye : register(c34);

// spec test
float3x3 specmat : register(c40);

#define fresnel (fxParams.x)
#define power (fxParams.y)
#define lightmult (fxParams.z)
//#define power (fxParams.y)
//#define lightmult (fxParams.z)
#define shininess (fxParams.w)

#define surfAmb (surfProps.x)
#define surfDiff (surfProps.y)
#define surfSpec (surfProps.z)
#define surfRefl (surfProps.w)
//#define surfSpec (surfProps.z)
#define surfPrelight (surfProps.w)

struct VS_INPUT {
float4 Position : POSITION;
Expand All @@ -28,7 +32,8 @@ struct VS_INPUT {
struct VS_OUTPUT {
float4 Position : POSITION;
float2 Texcoord0 : TEXCOORD0;
float3 Texcoord1 : TEXCOORD1;
float3 WorldNormal : TEXCOORD1;
float3 WorldPos : TEXCOORD2;
float4 Color : COLOR0;
float4 EnvColor : COLOR1;
};
Expand All @@ -40,31 +45,23 @@ main(VS_INPUT IN)
OUT.Position = mul(IN.Position, combined);
OUT.Texcoord0 = IN.Texcoord0;

OUT.Color = float4(0.0, 0.0, 0.0, 1.0);
OUT.Color = float4(IN.Color.rgb*surfPrelight, 1.0);
OUT.Color.xyz += ambient*surfAmb;
for(int i = 0; i < 7; i++){
float l = max(0.0, dot(IN.Normal, -directDir[i]));
OUT.Color.xyz += l*directCol[i]*surfDiff;
}
OUT.Color = saturate(OUT.Color)*matCol;

// Sphere map
float4 WorldPos = mul(IN.Position, world);
float3 WorldNormal = normalize(mul(IN.Normal, (float3x3)world));
float3 ViewVector = normalize(WorldPos.xyz - eye);
float3 ReflVector = ViewVector - 2.0*dot(ViewVector, WorldNormal)*WorldNormal;
OUT.Texcoord1 = ReflVector;

// Specular
float specAmt = pow(max(dot(ReflVector, mul(-directDir[0], (float3x3)world)), 0.0), power);
OUT.EnvColor.g = specAmt*surfSpec*lightmult;
OUT.WorldPos = WorldPos;
OUT.WorldNormal = WorldNormal;

// reflection intensity
float b = 1.0 - saturate(dot(-ViewVector, WorldNormal));
OUT.EnvColor.r = lerp(b*b*b*b*b, 1.0f, fresnel)*surfRefl*lightmult;

OUT.EnvColor.b = 0.0;
OUT.EnvColor.a = 0.0;
OUT.EnvColor = lerp(1.0f, b*b*b*b*b, fresnel)*shininess;

return OUT;
}
7 changes: 0 additions & 7 deletions shaders/vs/specCarFxVS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,5 @@ main(VS_INPUT IN)
OUT.Speccolor.rgb = spec*3 * float3(0.75, 0.75, 0.75)*specularity*lightmult;
OUT.Speccolor.a = 1.0;

/* to simulate ps2 specdot:
OUT.Speccolor.r = spec;
OUT.Speccolor.g = specularity;
OUT.Speccolor.b = lightmult;
OUT.Speccolor.a = 0.0;
*/

return OUT;
}
7 changes: 6 additions & 1 deletion src/envmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,15 @@ WRAPPER void CEntity::GetBoundCentre(CVector *v) { EAXJMP(0x534290); }
WRAPPER bool CEntity::GetIsOnScreen_orig(void) { EAXJMP(0x534540); }

// TODO: FLAify this?
CBaseModelInfo **CModelInfo__ms_modelInfoPtrs = (CBaseModelInfo**)0xA9B0C8;
CBaseModelInfo **CModelInfo__ms_modelInfoPtrs;// = (CBaseModelInfo**)0xA9B0C8;
CBaseModelInfo*
GetModelInfo(CEntity *e)
{
static bool init;
if(!init){
CModelInfo__ms_modelInfoPtrs = *(CBaseModelInfo***)(0x4C5960 + 3);
init = true;
}
return CModelInfo__ms_modelInfoPtrs[e->m_nModelIndex];
}

Expand Down
69 changes: 58 additions & 11 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ bool iCanHasSunGlare = true;
bool privateHooks;

int fixingSAMP;
HMODULE UG_mod;
typedef bool(*UG_EventHook)(void* pData);
void (*UG_RegisterEventCallback)(const char *type, UG_EventHook hook);

int numConfigs;
int currentConfig = 0;
Expand Down Expand Up @@ -407,11 +410,23 @@ clamp(float f, float max)
RwRGBAReal &AmbientLightColourForFrame_PedsCarsAndObjects = *(RwRGBAReal*)0xC886C4;
RwRGBAReal &DirectionalLightColourForFrame = *(RwRGBAReal*)0xC886B4;

uint8 *ambRed = (uint8*)0xB7C3C8;
uint8 *ambGreen = (uint8*)0xB7C310;
uint8 *ambBlue = (uint8*)0xB7C258;

void (*SetLightsWithTimeOfDayColour_orig)(RpWorld*);
void
SetLightsWithTimeOfDayColour(RpWorld *world)
{
if(GetAsyncKeyState(VK_F5) & 0x8000){
memset(ambRed, 0xFF, 184);
memset(ambGreen, 0xFF, 184);
memset(ambBlue, 0xFF, 184);
}else{
memset(ambRed, 0, 184);
memset(ambGreen, 0, 184);
memset(ambBlue, 0, 184);
}
SetLightsWithTimeOfDayColour_orig(world);
return;

Expand Down Expand Up @@ -717,20 +732,30 @@ void RenderReflectionMap_leeds(void);
void RenderReflectionScene(void);
void DrawDebugEnvMap(void);

void
RenderScene_hook(void)
bool
RenderScene_before(void*)
{
// Do this because far and fog plane are set AFTER calling BeingUpdate in Idle()
RwCameraEndUpdate(Scene.camera);
RwCameraBeginUpdate(Scene.camera);

RenderScene();

return true;
}
bool
RenderScene_after(void*)
{
if(config->vehiclePipe == CAR_NEO)
CarPipe::RenderEnvTex();
else if(config->vehiclePipe == CAR_LCS || config->vehiclePipe == CAR_VCS)
RenderReflectionMap_leeds();
DrawDebugEnvMap();
return true;
}
void
RenderScene_hook(void)
{
RenderScene_before(nil);
RenderScene();
RenderScene_after(nil);
}

int (*PipelinePluginAttach)(void);
Expand All @@ -745,8 +770,12 @@ void
InitialiseGame_hook(void)
{
ONCE;
// _controlfp(_PC_53,_MCW_PC); // no use, reset somewhere else
InterceptCall(&RenderScene_A, RenderScene_hook, 0x53EABF);
if(!UG_RegisterEventCallback)
InterceptCall(&RenderScene_A, RenderScene_hook, 0x53EABF);
else{
UG_RegisterEventCallback("EVENT_BEFORE_RENDERSCENE", RenderScene_before);
UG_RegisterEventCallback("EVENT_AFTER_RENDERSCENE", RenderScene_after);
}
void envmaphooks(void);
envmaphooks();
neoInit();
Expand Down Expand Up @@ -933,6 +962,10 @@ readIni(int n)
c->leedsShininessMult = readfloat(cfg.get("SkyGfx", "leedsShininessMult", ""), 1.0);
c->neoShininessMult = readfloat(cfg.get("SkyGfx", "neoShininessMult", ""), 1.0);
c->neoSpecularityMult = readfloat(cfg.get("SkyGfx", "neoSpecularityMult", ""), 1.0);
c->envShininessMult = readfloat(cfg.get("SkyGfx", "envShininessMult", ""), 1.0);
c->envSpecularityMult = readfloat(cfg.get("SkyGfx", "envSpecularityMult", ""), 1.0);
c->envPower = readfloat(cfg.get("SkyGfx", "envPower", ""), 20.0);
c->envFresnel = readfloat(cfg.get("SkyGfx", "envFresnel", ""), 0.7);
c->envMapSize = readint(cfg.get("SkyGfx", "envMapSize", ""), 256);
int i = 1;
while(i < c->envMapSize) i *= 2;
Expand Down Expand Up @@ -964,7 +997,7 @@ readIni(int n)
disableClouds = readint(cfg.get("SkyGfx", "disableClouds", ""), 0);
disableGamma = readint(cfg.get("SkyGfx", "disableGamma", ""), 0);
transparentLockon = readint(cfg.get("SkyGfx", "transparentLockon", ""), 0);
fixShadows = readint(cfg.get("SkyGfx", "fixShadows", ""), 1);
fixShadows = readint(cfg.get("SkyGfx", "fixShadows", ""), 0);
c->lightningIlluminatesWorld = readint(cfg.get("SkyGfx", "lightningIlluminatesWorld", ""), 0);

static StrAssoc colorFilterMap[] = {
Expand Down Expand Up @@ -1104,6 +1137,10 @@ afterStreamIni(void)
X(leedsShininessMult) \
X(neoShininessMult) \
X(neoSpecularityMult) \
X(envShininessMult) \
X(envSpecularityMult) \
X(envPower) \
X(envFresnel) \
X(doglare) \
X(fixGrassPlacement) \
X(grassAddAmbient) \
Expand Down Expand Up @@ -1254,6 +1291,10 @@ installMenu(void)
menu.leedsShininessMult = DebugMenuAddVar("SkyGFX|Misc", "Leeds Car Shininess", &config->leedsShininessMult, nil, 0.1f, 0.0f, 10.0f);
menu.neoShininessMult = DebugMenuAddVar("SkyGFX|Misc", "Neo Car Shininess", &config->neoShininessMult, nil, 0.1f, 0.0f, 10.0f);
menu.neoSpecularityMult = DebugMenuAddVar("SkyGFX|Misc", "Neo Car Specularity", &config->neoSpecularityMult, nil, 0.1f, 0.0f, 10.0f);
menu.envShininessMult = DebugMenuAddVar("SkyGFX|Misc", "Env Car Shininess", &config->envShininessMult, nil, 0.1f, 0.0f, 10.0f);
menu.envSpecularityMult = DebugMenuAddVar("SkyGFX|Misc", "Env Car Specularity", &config->envSpecularityMult, nil, 0.1f, 0.0f, 10.0f);
menu.envPower = DebugMenuAddVar("SkyGFX|Misc", "Env Car Power", &config->envPower, nil, 1.0f, 0.0f, 2000.0f);
menu.envFresnel = DebugMenuAddVar("SkyGFX|Misc", "Env Car Fresnel", &config->envFresnel, nil, 0.1f, 0.0f, 10.0f);
menu.fixGrassPlacement = DebugMenuAddVarBool32("SkyGFX|Misc", "Fix Grass Placement", &config->fixGrassPlacement, nil);
menu.lightningIlluminatesWorld = DebugMenuAddVar("SkyGFX|Misc", "Lightning illuminates", &config->lightningIlluminatesWorld, nil, 1, 0, 1, lightningStr);
DebugMenuEntrySetWrap(menu.lightningIlluminatesWorld, true);
Expand Down Expand Up @@ -1311,7 +1352,14 @@ InjectDelayedPatches()
// only load one ini for now, others are loaded later by readInis()

fixingSAMP = ModuleList().Get(L"samp") || ModuleList().Get(L"SAMPGraphicRestore");
UG_mod = ModuleList().Get(L"Underground_Core");
if(UG_mod)
UG_RegisterEventCallback = (void (*)(const char*, UG_EventHook))GetProcAddress(UG_mod, "RegisterEventCallback");

if(UG_RegisterEventCallback)
UG_RegisterEventCallback("EVENT_INITPOSTEFFECTS", CPostEffects::Initialise_skygfx);
else
InterceptCall(&CPostEffects::Initialise_orig, CPostEffects::Initialise, 0x5BD779);
InterceptCall(&InitialiseGame, InitialiseGame_hook, 0x748CFB);

// Stop timecycle from converting colour filter alphas
Expand Down Expand Up @@ -1509,7 +1557,6 @@ DllMain(HINSTANCE hInst, DWORD reason, LPVOID)
*(void**)0xA9AD78 = (void*)TagRenderCB; /* This is the (unused) material pipeline of player tags */

// postfx
InterceptCall(&CPostEffects::Initialise_orig, CPostEffects::Initialise, 0x5BD779);
InjectHook(0x704D1E, CPostEffects::ColourFilter_switch);
InjectHook(0x704D5D, CPostEffects::Radiosity);
InjectHook(0x704FB3, CPostEffects::Radiosity);
Expand Down Expand Up @@ -1582,13 +1629,13 @@ DllMain(HINSTANCE hInst, DWORD reason, LPVOID)
Nop(0x6E716B, 6);
Nop(0x6E7176, 6);

// Hook Skin Pipe so we can easily cull peds
// Hook Skpin Pipe so we can easily cull peds
// TODO: do this with other pipelines too
// No longer needed
// __rwSkinD3D9AtomicAllInOneNode_orig = nodeD3D9SkinAtomicAllInOneCSL->nodeMethods.nodeBody;
// nodeD3D9SkinAtomicAllInOneCSL->nodeMethods.nodeBody = __rwSkinD3D9AtomicAllInOneNode_hook;

InterceptCall(&SetLightsWithTimeOfDayColour_orig, SetLightsWithTimeOfDayColour, 0x53E997);
// InterceptCall(&SetLightsWithTimeOfDayColour_orig, SetLightsWithTimeOfDayColour, 0x53E997);


// Camera planes in CRenderer::RenderEverythingBarRoads
Expand Down
36 changes: 36 additions & 0 deletions src/pipelinecommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ pipeUploadZero(int loc)
RwD3D9SetVertexShaderConstant(loc, (void*)z, 1);
}

void
pipeUploadZeroPS(int loc)
{
static float z[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
RwD3D9SetPixelShaderConstant(loc, (void*)z, 1);
}

void
pipeUploadLightColor(RpLight *light, int loc)
{
Expand All @@ -212,6 +219,20 @@ pipeUploadLightColor(RpLight *light, int loc)
pipeUploadZero(loc);
}

void
pipeUploadLightColorPS(RpLight *light, int loc)
{
float c[4];
if(RpLightGetFlags(light) & rpLIGHTLIGHTATOMICS){
c[0] = light->color.red;
c[1] = light->color.green;
c[2] = light->color.blue;
c[3] = 1.0f;
RwD3D9SetPixelShaderConstant(loc, (void*)c, 1);
}else
pipeUploadZeroPS(loc);
}

void
pipeUploadLightDirection(RpLight *light, int loc)
{
Expand All @@ -227,6 +248,21 @@ pipeUploadLightDirection(RpLight *light, int loc)
pipeUploadZero(loc);
}

void
pipeUploadLightDirectionPS(RpLight *light, int loc)
{
float c[4];
if(RpLightGetFlags(light) & rpLIGHTLIGHTATOMICS){
RwV3d *at = RwMatrixGetAt(RwFrameGetLTM(RpLightGetFrame(light)));
c[0] = at->x;
c[1] = at->y;
c[2] = at->z;
c[3] = 1.0f;
RwD3D9SetPixelShaderConstant(loc, (void*)c, 1);
}else
pipeUploadZeroPS(loc);
}

void
pipeUploadLightDirectionLocal(RpLight *light, RwMatrix *m, int loc)
{
Expand Down
Loading

0 comments on commit 4f272eb

Please sign in to comment.