Skip to content

Commit

Permalink
implemented Env vehicle pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
aap committed Dec 24, 2019
1 parent 3a1b08b commit 376c304
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 8 deletions.
2 changes: 1 addition & 1 deletion premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ workspace "skygfx"

defines { "rsc_CompanyName=\"aap\"" }
defines { "rsc_LegalCopyright=\"\""}
defines { "rsc_FileVersion=\"4.0.0.0\"", "rsc_ProductVersion=\"4.0.0.0\"" }
defines { "rsc_FileVersion=\"4.1.0.0\"", "rsc_ProductVersion=\"4.1.0.0\"" }
defines { "rsc_InternalName=\"%{prj.name}\"", "rsc_ProductName=\"%{prj.name}\"", "rsc_OriginalFilename=\"%{prj.name}.dll\"" }
defines { "rsc_FileDescription=\"https://github.com/aap\"" }
defines { "rsc_UpdateUrl=\"https://github.com/aap/skygfx\"" }
Expand Down
Binary file modified resources/Resource.rc
Binary file not shown.
Binary file modified resources/VersionInfo.rc
Binary file not shown.
Binary file modified resources/resource.h
Binary file not shown.
33 changes: 33 additions & 0 deletions shaders/ps/envCarPS.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
uniform sampler2D tex0 : register(s0);
uniform sampler2D tex1 : register(s1);
uniform sampler2D tex2 : register(s2);

float3 eye : register(c0);
float3 sunDir : register(c1);

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

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);

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 diffpass = diff*IN.color;
float4 envpass = env*mask;;
float4 final = lerp(diffpass, envpass, IN.envColor.r) + IN.envColor.g;
// final.rgb = IN.envColor.r;
final.a = diffpass.a;
return final;
}
70 changes: 70 additions & 0 deletions shaders/vs/envCarVS.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
float4x4 combined : register(c0);
float3 ambient : register(c4);
float3 directCol[7] : register(c5);
float3 directDir[7] : register(c12);
float4 matCol : register(c19);
float4 surfProps : register(c20);
float4 fxParams : register(c21);

float4x4 world : register(c30);
float3 eye : register(c34);

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

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

struct VS_INPUT {
float4 Position : POSITION;
float3 Normal : NORMAL;
float4 Color : COLOR;
float2 Texcoord0: TEXCOORD0;
};

struct VS_OUTPUT {
float4 Position : POSITION;
float2 Texcoord0 : TEXCOORD0;
float3 Texcoord1 : TEXCOORD1;
float4 Color : COLOR0;
float4 EnvColor : COLOR1;
};

VS_OUTPUT
main(VS_INPUT IN)
{
VS_OUTPUT OUT;
OUT.Position = mul(IN.Position, combined);
OUT.Texcoord0 = IN.Texcoord0;

OUT.Color = float4(0.0, 0.0, 0.0, 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;

// 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;

return OUT;
}
22 changes: 17 additions & 5 deletions src/envmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ RwRGBAReal spheremapfog;
void
RenderSphereReflections(void)
{
if(iCanHasbuildingPipe && config->vehiclePipe == CAR_MOBILE){
if(iCanHasbuildingPipe && (config->vehiclePipe == CAR_MOBILE || config->vehiclePipe == CAR_ENV)){
MakeEnvmapRasters();

RwCamera *cam = Scene.camera;
Expand All @@ -408,10 +408,22 @@ RenderSphereReflections(void)
RwCameraSetFarClipPlane(cam, sphereRadius);
RwCameraSetFogDistance(cam, sphereRadius*0.75f);

RwRGBA color = { skyTopRed, skyTopGreen, skyTopBlue, 255 };
if(color.red < 64) color.red = 64;
if(color.green < 64) color.green = 64;
if(color.blue < 64) color.blue = 64;
RwRGBA color;
if(config->vehiclePipe == CAR_ENV){
// like Neo
static RwRGBA skyBot = { skyBotRed, skyBotGreen, skyBotBlue, 255 };
color = skyBot;
// blend a bit of white into the sky color, otherwise it tends to be very blue
color.red = color.red*0.6f + 255*0.4f;
color.green = color.green*0.6f + 255*0.4f;
color.blue = color.blue*0.6f + 255*0.4f;
}else{
static RwRGBA skyTop = { skyTopRed, skyTopGreen, skyTopBlue, 255 };
color = skyTop;
if(color.red < 64) color.red = 64;
if(color.green < 64) color.green = 64;
if(color.blue < 64) color.blue = 64;
}
RwCameraClear(cam, &color, rwCAMERACLEARIMAGE | rwCAMERACLEARZ);
RwRGBARealFromRwRGBA(&spheremapfog, &color);

Expand Down
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,7 @@ readIni(int n)
{"LCS", CAR_LCS},
{"VCS", CAR_VCS},
{"Mobile", CAR_MOBILE},
{"Env", CAR_ENV},
{"", -1},
};
c->vehiclePipe = StrAssoc::get(vehPipeMap, cfg.get("SkyGfx", "vehiclePipe", "").c_str());
Expand Down Expand Up @@ -1204,7 +1205,7 @@ installMenu(void)
if(DebugMenuLoad()){
static const char *ps2pcStr[] = { "PS2", "PC" };
static const char *buildPipeStr[] = { "PS2", "Xbox", "Mobile" };
static const char *vehPipeStr[] = { "PS2", "PC", "Xbox", "Spec", "Mobile", "Neo", "LCS", "VCS" };
static const char *vehPipeStr[] = { "PS2", "PC", "Xbox", "Spec", "Mobile", "Neo", "LCS", "VCS", "Env" };
static const char *colFilterStr[] = { "None", "PS2", "PC", "Mobile", "III", "VC", "VCS" };
static const char *lightningStr[] = { "Sky only", "Sky and objects" };
static const char *shadStr[] = { "Default", "PS2", "PC" };
Expand Down
3 changes: 2 additions & 1 deletion src/neoCarpipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ CarPipe::DiffusePass(RxD3D9ResEntryHeader *header, RpAtomic *atomic)

RwD3D9SetTexture(reflectionTex, 1);
RwD3D9SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_LERP);
// RwD3D9SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
RwD3D9SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
RwD3D9SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
RwD3D9SetTextureStageState(1, D3DTSS_COLORARG0, D3DTA_SPECULAR);
Expand Down Expand Up @@ -356,7 +357,7 @@ CarPipe::DiffusePass(RxD3D9ResEntryHeader *header, RpAtomic *atomic)
RwD3D9SetVertexShaderConstant(LOC_surfProps, &surfprops, 1);

float reflProps[4];
reflProps[0] = hasEnv && !noRefl ? envData->shininess/255.0f * 8.0f * config->neoShininessMult : 0.0f;
reflProps[0] = hasEnv && !noRefl ? envData->GetShininess() * 8.0f * config->neoShininessMult : 0.0f;
reflProps[1] = fresnel.Get();
reflProps[3] = power.Get();
RwD3D9SetVertexShaderConstant(LOC_reflProps, (void*)reflProps, 1);
Expand Down
9 changes: 9 additions & 0 deletions src/pipelinecommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ pipeGetCameraTransformMatrix(float *out)
memcpy(out, &combined, 64);
}

// Have to call the above before!
void
pipeGetWorldMatrix(float *out)
{
memcpy(out, &pipeWorldMat, 64);
}

void
pipeGetLeedsEnvMapMatrix(RpAtomic *atomic, float *out)
{
Expand Down Expand Up @@ -289,6 +296,8 @@ CreateShaders(void)
makeVS(IDR_LEEDSCARFXVS, &leedsCarFxVS);
makeVS(IDR_MOBILEVEHICLEVS, &mobileVehiclePipeVS);
makePS(IDR_MOBILEVEHICLEPS, &mobileVehiclePipePS);
makeVS(IDR_ENVCARVS, &envCarVS);
makePS(IDR_ENVCARPS, &envCarPS);

// building
makeVS(IDR_PS2BUILDINGVS, &ps2BuildingVS);
Expand Down
3 changes: 3 additions & 0 deletions src/skygfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ enum CarPipeline
CAR_NEO,
CAR_LCS,
CAR_VCS,
CAR_ENV,

NUMCARPIPES
};
Expand Down Expand Up @@ -283,6 +284,7 @@ extern void *simplePS;
extern void *vehiclePipeVS, *ps2CarFxVS;
extern void *ps2EnvSpecFxPS; // also used by the building pipeline
extern void *specCarFxVS, *specCarFxPS;
extern void *envCarVS, *envCarPS;
extern void *xboxCarVS;
extern void *leedsCarFxVS;
extern void *mobileVehiclePipeVS, *mobileVehiclePipePS;
Expand All @@ -300,6 +302,7 @@ void CreateShaders(void);
void RwToD3DMatrix(void *d3d, RwMatrix *rw);
void MakeProjectionMatrix(void *d3d, RwCamera *cam, float nbias = 0.0f, float fbias = 0.0f);
void pipeGetComposedTransformMatrix(RpAtomic *atomic, float *out);
void pipeGetWorldMatrix(float *out);
void pipeGetCameraTransformMatrix(float *out);
void pipeGetLeedsEnvMapMatrix(RpAtomic *atomic, float *out);
void pipeUploadMatCol(int flags, RpMaterial *m, int loc);
Expand Down
135 changes: 135 additions & 0 deletions src/vehiclePipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ enum {
void *vehiclePipeVS, *ps2CarFxVS;
void *ps2EnvSpecFxPS; // also used by the building pipeline
void *specCarFxVS, *specCarFxPS;
void *envCarVS, *envCarPS;
void *xboxCarVS;
void *leedsCarFxVS;
void *mobileVehiclePipeVS, *mobileVehiclePipePS;
Expand Down Expand Up @@ -1233,6 +1234,136 @@ CCustomCarEnvMapPipeline__CustomPipeRenderCB_mobile(RwResEntry *repEntry, void *
RwD3D9SetTextureStageState(1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
}

void
CCustomCarEnvMapPipeline__CustomPipeRenderCB_Env(RwResEntry *repEntry, void *object, RwUInt8 type, RwUInt32 flags)
{
RxD3D9ResEntryHeader *resEntryHeader;
RxD3D9InstanceData *instancedData;
RpAtomic *atomic;
RwInt32 numMeshes;
RwBool noFx;
CustomEnvMapPipeMaterialData *envData;
CustomSpecMapPipeMaterialData *specData;
RpMaterial *material;
RwUInt32 materialFlags;
RwBool hasEnv1, hasEnv2, hasSpec, hasAlpha;
struct {
float fresnel;
float power;
float lightmult;
} fxParams;
struct {
float ambient;
float diffuse;
float specular;
float reflection;
} surfProps;
RwV3d eye;
RwMatrix lightmat;
float transform[16];

atomic = (RpAtomic*)object;

_rwD3D9EnableClippingIfNeeded(object, type);

pipeGetComposedTransformMatrix(atomic, transform);
RwD3D9SetVertexShaderConstant(0, transform, 4);
pipeGetWorldMatrix(transform);
RwD3D9SetVertexShaderConstant(30, transform, 4);
eye = RwFrameGetLTM(RwCameraGetFrame((RwCamera*)RWSRCGLOBAL(curCamera)))->pos;
RwD3D9SetVertexShaderConstant(34, &eye, 1);
RwMatrixInvert(&lightmat, RwFrameGetLTM(RpAtomicGetFrame(atomic)));
if(flags & rpGEOMETRYLIGHT)
uploadLights(&lightmat);
else
uploadNoLights();

resEntryHeader = (RxD3D9ResEntryHeader *)(repEntry + 1);
instancedData = (RxD3D9InstanceData *)(resEntryHeader + 1);
if(resEntryHeader->indexBuffer != NULL)
RwD3D9SetIndices(resEntryHeader->indexBuffer);
_rwD3D9SetStreams(resEntryHeader->vertexStream,resEntryHeader->useOffsets);
RwD3D9SetVertexDeclaration(resEntryHeader->vertexDeclaration);
numMeshes = resEntryHeader->numMeshes;

int alphafunc;
int src, dst;
int fog;
RwRenderStateGet(rwRENDERSTATEALPHATESTFUNCTION, &alphafunc);
RwRenderStateGet(rwRENDERSTATESRCBLEND, &src);
RwRenderStateGet(rwRENDERSTATEDESTBLEND, &dst);
RwRenderStateGet(rwRENDERSTATEFOGCOLOR, &fog);

noFx = CVisibilityPlugins__GetAtomicId(atomic) & 0x6000;
fxParams.fresnel = 0.3f;
fxParams.power = 10.0f;
fxParams.lightmult = CCustomCarEnvMapPipeline__m_EnvMapLightingMult;
RwD3D9SetVertexShaderConstant(21, &fxParams, 1);

pipeSetTexture(reflectionTex, 1);
pipeSetTexture(CarPipe::reflectionMask, 2);

for(; numMeshes--; instancedData++){
material = instancedData->material;

if(instancedData->material->color.alpha == 0)
continue;

pipeSetTexture(material->texture, 0);

hasAlpha = instancedData->vertexAlpha || instancedData->material->color.alpha != 255;
RwRenderStateSet(rwRENDERSTATEVERTEXALPHAENABLE, (void*)hasAlpha);

envData = *GETENVMAP(material);
specData = *GETSPECMAP(material);

surfProps.reflection = 0.0f;
surfProps.specular = 0.0f;
if(!noFx){
materialFlags = *(RwUInt32*)&material->surfaceProps.specular;
hasEnv1 = !!(materialFlags & 1);
hasEnv2 = !!(materialFlags & 2);
hasSpec = !!(materialFlags & 4) && !renderingWheel;
if(RpMatFXMaterialGetEffects(material) != rpMATFXEFFECTENVMAP){
hasEnv1 = false;
hasEnv2 = false;
hasSpec = false;
}

if(hasEnv1 || hasEnv2){
surfProps.reflection = envData->GetShininess();
surfProps.reflection *= 15.0f * config->neoShininessMult;
}

if(hasSpec){
surfProps.specular = specData->specularity;
surfProps.specular *= 3.0f * config->neoSpecularityMult;
}
}

pipeUploadMatCol(flags, material, REG_matCol);
surfProps.ambient = material->surfaceProps.ambient;
surfProps.diffuse = material->surfaceProps.diffuse;
if(surfProps.ambient > 0.1f && surfProps.ambient < 0.8f)
surfProps.ambient = max(surfProps.ambient, 0.8f);
RwD3D9SetVertexShaderConstant(REG_surfProps, &surfProps, 1);
RwD3D9SetVertexShaderConstant(21, &fxParams, 1);

RwD3D9SetVertexShader(envCarVS);
RwD3D9SetPixelShader(envCarPS);

D3D9RenderDual(config->dualPassVehicle, resEntryHeader, instancedData);
}
RwD3D9SetVertexShader(NULL);
RwD3D9SetPixelShader(NULL);
RwD3D9SetTexture(NULL, 1);
RwD3D9SetTexture(NULL, 2);
RwD3D9SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
RwD3D9SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
RwD3D9SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);
RwD3D9SetTextureStageState(1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
}

void
CCustomCarEnvMapPipeline__CustomPipeRenderCB_Switch(RwResEntry *repEntry, void *object, RwUInt8 type, RwUInt32 flags)
{
Expand Down Expand Up @@ -1262,6 +1393,10 @@ CCustomCarEnvMapPipeline__CustomPipeRenderCB_Switch(RwResEntry *repEntry, void *
if(iCanHasbuildingPipe)
CCustomCarEnvMapPipeline__CustomPipeRenderCB_mobile(repEntry, object, type, flags);
break;
case CAR_ENV:
if(iCanHasbuildingPipe && iCanHasNeoCar)
CCustomCarEnvMapPipeline__CustomPipeRenderCB_Env(repEntry, object, type, flags);
break;
}
fixSAMP();
}
Expand Down

0 comments on commit 376c304

Please sign in to comment.