diff --git a/Android.mk b/Android.mk index b2c3191..ca2261c 100644 --- a/Android.mk +++ b/Android.mk @@ -5,7 +5,8 @@ LOCAL_CPP_EXTENSION := .cpp .cc LOCAL_MODULE := SkyGfxMobile LOCAL_SRC_FILES := main.cpp colorfilter.cpp shader.cpp shading.cpp pipeline.cpp effects.cpp plantsurfprop.cpp shadows.cpp rtshadowman.cpp mod/logger.cpp mod/config.cpp LOCAL_CXXFLAGS = -O3 -mfloat-abi=softfp -mfpu=neon -DNDEBUG -#LOCAL_CXXFLAGS += -DNEW_LIGHTING # Nah, too dark... +LOCAL_CXXFLAGS += -DNEW_LIGHTING # Nah, too dark... +#LOCAL_CXXFLAGS += -DSTOCHASTIC_TEX # Does not work for some reason. And causes a lag, heavy thing. LOCAL_C_INCLUDES += ./include LOCAL_LDLIBS += -llog include $(BUILD_SHARED_LIBRARY) \ No newline at end of file diff --git a/gtasa_things.h b/gtasa_things.h index 46860d6..8e2746d 100644 --- a/gtasa_things.h +++ b/gtasa_things.h @@ -7,4 +7,4 @@ extern void* hGTASA; extern int nColorFilter; extern uintptr_t pGTASA; -void Redirect(uintptr_t addr, uintptr_t to); \ No newline at end of file +#define Redirect aml->Redirect \ No newline at end of file diff --git a/main.cpp b/main.cpp index edefa9b..557dc4c 100644 --- a/main.cpp +++ b/main.cpp @@ -51,6 +51,7 @@ ConfigEntry* pVehicleShadowDistance; ConfigEntry* pMaxRTShadows; ConfigEntry* pDynamicObjectsShadows; ConfigEntry* pAllowPlayerClassicShadow; +ConfigEntry* pNewShaderLighting; /* Patch Saves */ //const uint32_t sunCoronaRet = 0xF0F7ECE0; @@ -60,29 +61,6 @@ const uint32_t sunCoronaDel = 0xBF00BF00; void* hGTASA = NULL; uintptr_t pGTASA = 0; -//////////////////////////////////////////////////////////////////////// -// Was taken from TheOfficialFloW's git repo (will be in AML 1.0.0.6) // -//////////////////////////////////////////////////////////////////////// -void Redirect(uintptr_t addr, uintptr_t to) -{ - if(!addr) return; - uint32_t hook[2] = {0xE51FF004, to}; - if(addr & 1) - { - addr &= ~1; - if (addr & 2) - { - aml->PlaceNOP(addr, 1); - addr += 2; - } - hook[0] = 0xF000F8DF; - } - aml->Write(addr, (uintptr_t)hook, sizeof(hook)); -} -//////////////////////////////////////////////////////////////////////// -// Was taken from TheOfficialFloW's git repo (will be in AML 1.0.0.6) // -//////////////////////////////////////////////////////////////////////// - extern DECL_HOOKv(PlantMgrInit); extern DECL_HOOKv(PlantMgrRender); extern DECL_HOOKv(PlantSurfPropMgrInit); @@ -156,6 +134,11 @@ extern "C" void OnModLoad() cfg->Bind("IdeasFrom", "", "About")->SetString("aap, TheOfficialFloW"); cfg->Bind("Discord", "", "About")->SetString("https://discord.gg/2MY7W39kBg"); cfg->Bind("GitHub", "", "About")->SetString("https://github.com/AndroidModLoader/GTASA_SkyGfxMobile"); + + // Config: Information + cfg->Bind("About_PS2_Reflections", "Works only on low reflections setting with PS2 Shading enabled", "About"); + cfg->Bind("Colorfilter_Values", "default none ps2 pc", "About"); + cfg->Save(); // Config: Render @@ -209,10 +192,6 @@ extern "C" void OnModLoad() //pDynamicObjectsShadows = cfg->Bind("DynamicObjectsShadows", false, "Shadows"); //pAllowPlayerClassicShadow = cfg->Bind("AllowPlayerClassicShadow", false, "Shadows"); - // Config: Information - cfg->Bind("About_PS2_Reflections", "Works only on low reflections setting with PS2 Shading enabled", "About"); - cfg->Bind("Colorfilter_Values", "default none ps2 pc", "About"); - HOOKPLT(InitRW, pGTASA + 0x66F2D0); // Patches @@ -243,6 +222,7 @@ extern "C" void OnModLoad() SET_TO(emu_glEnable, aml->GetSym(hGTASA, "_Z12emu_glEnablej")); SET_TO(emu_glDisable, aml->GetSym(hGTASA, "_Z13emu_glDisablej")); #ifdef NEW_LIGHTING + pNewShaderLighting = cfg->Bind("PS2_Shading_NewLight", true, "Render"); Redirect(aml->GetSym(hGTASA, "_Z36_rwOpenGLLightsSetMaterialPropertiesPK10RpMaterialj"), (uintptr_t)_rwOpenGLLightsSetMaterialProperties); #endif // NEW_LIGHTING Redirect(aml->GetSym(hGTASA, "_Z28SetLightsWithTimeOfDayColourP7RpWorld"), (uintptr_t)SetLightsWithTimeOfDayColour); @@ -468,6 +448,7 @@ extern "C" void OnModLoad() SET_TO(RpClumpDestroy, aml->GetSym(hGTASA, "_Z14RpClumpDestroyP7RpClump")); SET_TO(RwFrameCreate, aml->GetSym(hGTASA, "_Z13RwFrameCreatev")); SET_TO(RpAtomicSetFrame, aml->GetSym(hGTASA, "_Z16RpAtomicSetFrameP8RpAtomicP7RwFrame")); + SET_TO(RenderAtomicWithAlpha, aml->GetSym(hGTASA, "_ZN18CVisibilityPlugins21RenderAtomicWithAlphaEP8RpAtomici")); SET_TO(PlantMgr_rwOpenGLSetRenderState, aml->GetSym(hGTASA, "_Z23_rwOpenGLSetRenderState13RwRenderStatePv")); SET_TO(IsSphereVisibleForCamera, aml->GetSym(hGTASA, "_ZN7CCamera15IsSphereVisibleERK7CVectorf")); diff --git a/plantsurfprop.cpp b/plantsurfprop.cpp index 3cac482..b5b47d8 100644 --- a/plantsurfprop.cpp +++ b/plantsurfprop.cpp @@ -199,6 +199,7 @@ RpAtomic* (*RpAtomicClone)(RpAtomic*); void (*RpClumpDestroy)(RpClump*); RwFrame* (*RwFrameCreate)(); void (*RpAtomicSetFrame)(RpAtomic*, RwFrame*); +void (*RenderAtomicWithAlpha)(RpAtomic*, int alphaVal); RpMaterial* SetGrassMaterial(RpMaterial* material, void* rgba) { material->color = *(RwRGBA*)rgba; @@ -257,7 +258,7 @@ void InitPlantManager() logger->Error("Failed to load ENVGRASS.TEXDB!"); } - StreamingMakeSpaceFor(0x8800); + StreamingMakeSpaceFor(0x8800); // Does nothing PC_PlantTextureTab0[0] = GetTextureFromTextureDB("txgrass0_0"); PC_PlantTextureTab0[1] = GetTextureFromTextureDB("txgrass0_1"); @@ -325,6 +326,8 @@ DECL_HOOKv(PlantMgrRender) PlantMgr_rwOpenGLSetRenderState(rwRENDERSTATEALPHATESTFUNCTIONREF, 0); PlantMgr_rwOpenGLSetRenderState(rwRENDERSTATEALPHATESTFUNCTION, 8u); + RenderAtomicWithAlpha(PC_PlantModelsTab0[0], 128); + for(int type = 0; type < DDDAFSF; ++type) { static PPTriPlant plant = {0}; diff --git a/plantsurfprop.h b/plantsurfprop.h index bb9b397..6b976e3 100644 --- a/plantsurfprop.h +++ b/plantsurfprop.h @@ -43,6 +43,7 @@ extern RpAtomic* (*RpAtomicClone)(RpAtomic*); extern void (*RpClumpDestroy)(RpClump*); extern RwFrame* (*RwFrameCreate)(); extern void (*RpAtomicSetFrame)(RpAtomic*, RwFrame*); +extern void (*RenderAtomicWithAlpha)(RpAtomic*, int alphaVal); extern void (*PlantMgr_rwOpenGLSetRenderState)(RwRenderState, int); extern bool (*IsSphereVisibleForCamera)(CCamera*, const CVector*, float); diff --git a/shader.cpp b/shader.cpp index a35135c..b8a799c 100644 --- a/shader.cpp +++ b/shader.cpp @@ -29,6 +29,9 @@ extern ConfigEntry* pPS2Sun; extern ConfigEntry* pPS2Shading; extern ConfigEntry* pPS2Reflections; extern ConfigEntry* pExponentialFog; +#ifdef NEW_LIGHTING + extern ConfigEntry* pNewShaderLighting; +#endif // NEW_LIGHTING char pxlbuf[4096], vtxbuf[4096]; @@ -47,7 +50,25 @@ void BuildPixelSource_SkyGfx(int flags) int ped_spec = pDisablePedSpec->GetBool() ? 0 : (FLAG_BONE3 | FLAG_BONE4); PXL_EMIT("#version 100"); + + #ifdef STOCHASTIC_TEX + PXL_EMIT("#extension GL_OES_standard_derivatives : enable"); + #endif // STOCHASTIC_TEX + PXL_EMIT("precision mediump float;"); + + #ifdef STOCHASTIC_TEX // Not working, huge performance impact + PXL_EMIT("vec2 hash2D2D(vec2 s) { return fract(sin(mod(vec2(dot(s, vec2(127.1, 311.7)), dot(s, vec2(269.5, 183.3))), 3.14159)) * 43758.5453); }"); + PXL_EMIT("vec4 tex2DStochastic(sampler2D tex, vec2 UV) { mat4 BW_vx;"); + PXL_EMIT("vec2 skewUV = (mat2(vec2(1.0, 0.0), vec2(-0.57735027, 1.15470054)) * UV * 3.464);"); + PXL_EMIT("vec2 vxID = floor(skewUV); vec3 barry = vec3(fract(skewUV), 0); barry.z = 1.0 - barry.x - barry.y;"); + PXL_EMIT("BW_vx = ((barry.z > 0.0) ? mat4(vec4(vxID, 0, 0), vec4(vxID + vec2(0, 1), 0, 0), vec4(vxID + vec2(1, 0), 0, 0), vec4(barry.zyx, 0)) : mat4(vec4(vxID + vec2(1, 1), 0, 0), vec4(vxID + vec2(1, 0), 0, 0), vec4(vxID + vec2(0, 1), 0, 0), vec4(-barry.z, 1.0 - barry.y, 1.0 - barry.x, 0)));"); + PXL_EMIT("return (texture2D(tex, UV + hash2D2D(BW_vx[0].xy)) * BW_vx[3].x) + (texture2D(tex, UV + hash2D2D(BW_vx[1].xy)) * BW_vx[3].y) + (texture2D(tex, UV + hash2D2D(BW_vx[2].xy)) * BW_vx[3].z); }"); + /*PXL_EMIT("vec2 dx = dFdx(UV); vec2 dy = dFdy(UV);"); + PXL_EMIT("return (texture2DLod(tex, UV + hash2D2D(BW_vx[0].xy), dx, dy) * BW_vx[3].x) + (texture2DLod(tex, UV + hash2D2D(BW_vx[1].xy), dx, dy) * BW_vx[3].y) + (texture2DLod(tex, UV + hash2D2D(BW_vx[2].xy), dx, dy) * BW_vx[3].z); }");*/ + PXL_EMIT("vec4 tex2DStochastic(sampler2D tex, vec2 UV, float bias) { return tex2DStochastic(tex, UV); }"); + #endif // STOCHASTIC_TEX + if(flags & FLAG_TEX0) { PXL_EMIT("uniform sampler2D Diffuse;\nvarying mediump vec2 Out_Tex0;"); @@ -100,18 +121,33 @@ void BuildPixelSource_SkyGfx(int flags) PXL_EMIT("\tlowp vec4 fcolor;"); if(flags & FLAG_TEX0) { - if(flags & FLAG_TEXBIAS) - { - PXL_EMIT("\tlowp vec4 diffuseColor = texture2D(Diffuse, Out_Tex0, -1.5);"); - } - else if(RQCaps->isSlowGPU != 0) - { - PXL_EMIT("\tlowp vec4 diffuseColor = texture2D(Diffuse, Out_Tex0);"); - } - else - { - PXL_EMIT("\tlowp vec4 diffuseColor = texture2D(Diffuse, Out_Tex0, -0.5);"); - } + #ifdef STOCHASTIC_TEX + if(flags & FLAG_TEXBIAS) + { + PXL_EMIT("\tlowp vec4 diffuseColor = tex2DStochastic(Diffuse, Out_Tex0, -1.5);"); + } + else if(RQCaps->isSlowGPU != 0) + { + PXL_EMIT("\tlowp vec4 diffuseColor = tex2DStochastic(Diffuse, Out_Tex0);"); + } + else + { + PXL_EMIT("\tlowp vec4 diffuseColor = tex2DStochastic(Diffuse, Out_Tex0, -0.5);"); + } + #else + if(flags & FLAG_TEXBIAS) + { + PXL_EMIT("\tlowp vec4 diffuseColor = texture2D(Diffuse, Out_Tex0, -1.5);"); + } + else if(RQCaps->isSlowGPU != 0) + { + PXL_EMIT("\tlowp vec4 diffuseColor = texture2D(Diffuse, Out_Tex0);"); + } + else + { + PXL_EMIT("\tlowp vec4 diffuseColor = texture2D(Diffuse, Out_Tex0, -0.5);"); + } + #endif // !STOCHASTIC_TEX PXL_EMIT("\tfcolor = diffuseColor;"); if(flags & (FLAG_COLOR | FLAG_LIGHTING)) @@ -374,9 +410,12 @@ void BuildVertexSource_SkyGfx(int flags) } #ifdef NEW_LIGHTING - VTX_EMIT("#define SurfAmb (MaterialAmbient.x)"); - VTX_EMIT("#define SurfDiff (MaterialAmbient.y)"); - VTX_EMIT("#define ColorScale (MaterialDiffuse)"); + if(pNewShaderLighting->GetBool()) + { + VTX_EMIT("#define SurfAmb (MaterialAmbient.x)"); + VTX_EMIT("#define SurfDiff (MaterialAmbient.y)"); + VTX_EMIT("#define ColorScale (MaterialDiffuse)"); + } #endif // NEW_LIGHTING /////////////////////////////////////// @@ -533,93 +572,103 @@ void BuildVertexSource_SkyGfx(int flags) { // Ambient light #ifdef NEW_LIGHTING - VTX_EMIT("vec3 AmbEmissLight, DiffLight;"); - if(flags & FLAG_COLOR_EMISSIVE) + if(pNewShaderLighting->GetBool()) { - if(flags & FLAG_CAMERA_BASED_NORMALS) + VTX_EMIT("vec3 AmbEmissLight, DiffLight;"); + if(flags & FLAG_COLOR_EMISSIVE) { - VTX_EMIT_V("AmbEmissLight += clamp(%s.xyz, 0.0, 0.5);", _tmp); - } - else - { - VTX_EMIT_V("AmbEmissLight += %s.xyz;", _tmp); + if(flags & FLAG_CAMERA_BASED_NORMALS) + { + VTX_EMIT_V("AmbEmissLight += clamp(%s.xyz, 0.0, 0.5);", _tmp); + } + else + { + VTX_EMIT_V("AmbEmissLight += %s.xyz;", _tmp); + } } + VTX_EMIT("AmbEmissLight += AmbientLightColor * SurfAmb;"); } - VTX_EMIT("AmbEmissLight += AmbientLightColor * SurfAmb;"); - #else - VTX_EMIT("vec3 Out_LightingColor;"); - if(flags & FLAG_COLOR_EMISSIVE) + else + #endif // NEW_LIGHTING { - if(flags & FLAG_CAMERA_BASED_NORMALS) // 3D markers hack from psvita port - { - VTX_EMIT_V("Out_LightingColor = AmbientLightColor * MaterialAmbient.xyz + %s.xyz * MaterialDiffuse.xyz;", _tmp); - } - else if(flags & FLAG_CAMERA_BASED_NORMALS) + VTX_EMIT("vec3 Out_LightingColor;"); + if(flags & FLAG_COLOR_EMISSIVE) { - VTX_EMIT("Out_LightingColor = AmbientLightColor * MaterialAmbient.xyz * 1.5;"); + if(flags & FLAG_CAMERA_BASED_NORMALS) // 3D markers hack from psvita port + { + VTX_EMIT_V("Out_LightingColor = AmbientLightColor * MaterialAmbient.xyz + %s.xyz * MaterialDiffuse.xyz;", _tmp); + } + else if(flags & FLAG_CAMERA_BASED_NORMALS) + { + VTX_EMIT("Out_LightingColor = AmbientLightColor * MaterialAmbient.xyz * 1.5;"); + } + else + { + VTX_EMIT_V("Out_LightingColor = AmbientLightColor * MaterialAmbient.xyz + %s.xyz;", _tmp); + } } else { - VTX_EMIT_V("Out_LightingColor = AmbientLightColor * MaterialAmbient.xyz + %s.xyz;", _tmp); + VTX_EMIT("Out_LightingColor = AmbientLightColor * MaterialAmbient.xyz + MaterialEmissive.xyz;"); } } - else - { - VTX_EMIT("Out_LightingColor = AmbientLightColor * MaterialAmbient.xyz + MaterialEmissive.xyz;"); - } - #endif // NEW_LIGHTING // DIFFUSE if(flags & (FLAG_LIGHT3 | FLAG_LIGHT2 | FLAG_LIGHT1)) { #ifdef NEW_LIGHTING - if(flags & FLAG_LIGHT1) + if(pNewShaderLighting->GetBool()) { - if(GetMobileEffectSetting() == 3 && (flags & (FLAG_BACKLIGHT | FLAG_BONE4 | FLAG_BONE3))) + if(flags & FLAG_LIGHT1) { - VTX_EMIT("DiffLight += (max(dot(DirLightDirection, WorldNormal), 0.0) + max(dot(DirBackLightDirection, WorldNormal), 0.0)) * DirLightDiffuseColor;"); + if(GetMobileEffectSetting() == 3 && (flags & (FLAG_BACKLIGHT | FLAG_BONE4 | FLAG_BONE3))) + { + VTX_EMIT("DiffLight += (max(dot(DirLightDirection, WorldNormal), 0.0) + max(dot(DirBackLightDirection, WorldNormal), 0.0)) * DirLightDiffuseColor;"); + } + else + { + VTX_EMIT("DiffLight += max(dot(DirLightDirection, WorldNormal), 0.0) * DirLightDiffuseColor;"); + } } - else + if(flags & FLAG_LIGHT2) { - VTX_EMIT("DiffLight += max(dot(DirLightDirection, WorldNormal), 0.0) * DirLightDiffuseColor;"); + VTX_EMIT("DiffLight += max(dot(DirLight2Direction, WorldNormal), 0.0) * DirLight2DiffuseColor;"); } - } - if(flags & FLAG_LIGHT2) - { - VTX_EMIT("DiffLight += max(dot(DirLight2Direction, WorldNormal), 0.0) * DirLight2DiffuseColor;"); - } - if(flags & FLAG_LIGHT3) - { - VTX_EMIT("DiffLight += max(dot(DirLight3Direction, WorldNormal), 0.0) * DirLight3DiffuseColor;"); - } - VTX_EMIT("DiffLight *= SurfDiff;"); - #else - if(flags & FLAG_LIGHT1) - { - if(GetMobileEffectSetting() == 3 && (flags & (FLAG_BACKLIGHT | FLAG_BONE4 | FLAG_BONE3))) + if(flags & FLAG_LIGHT3) { - VTX_EMIT("Out_LightingColor += (max(dot(DirLightDirection, WorldNormal), 0.0) + max(dot(DirBackLightDirection, WorldNormal), 0.0)) * DirLightDiffuseColor;"); + VTX_EMIT("DiffLight += max(dot(DirLight3Direction, WorldNormal), 0.0) * DirLight3DiffuseColor;"); } - else + VTX_EMIT("DiffLight *= SurfDiff;"); + } + else + #endif // NEW_LIGHTING + { + if(flags & FLAG_LIGHT1) { - VTX_EMIT("Out_LightingColor += max(dot(DirLightDirection, WorldNormal), 0.0) * DirLightDiffuseColor;"); + if(GetMobileEffectSetting() == 3 && (flags & (FLAG_BACKLIGHT | FLAG_BONE4 | FLAG_BONE3))) + { + VTX_EMIT("Out_LightingColor += (max(dot(DirLightDirection, WorldNormal), 0.0) + max(dot(DirBackLightDirection, WorldNormal), 0.0)) * DirLightDiffuseColor;"); + } + else + { + VTX_EMIT("Out_LightingColor += max(dot(DirLightDirection, WorldNormal), 0.0) * DirLightDiffuseColor;"); + } + + if(!(flags & FLAG_LIGHT2)) + { + if(flags & FLAG_LIGHT3) + VTX_EMIT("Out_LightingColor += max(dot(DirLight3Direction, WorldNormal), 0.0) * DirLight3DiffuseColor;"); + goto LABEL_89; + } } - - if(!(flags & FLAG_LIGHT2)) + else if(!(flags & FLAG_LIGHT2)) { if(flags & FLAG_LIGHT3) VTX_EMIT("Out_LightingColor += max(dot(DirLight3Direction, WorldNormal), 0.0) * DirLight3DiffuseColor;"); goto LABEL_89; } + VTX_EMIT("Out_LightingColor += max(dot(DirLight2Direction, WorldNormal), 0.0) * DirLight2DiffuseColor;"); } - else if(!(flags & FLAG_LIGHT2)) - { - if(flags & FLAG_LIGHT3) - VTX_EMIT("Out_LightingColor += max(dot(DirLight3Direction, WorldNormal), 0.0) * DirLight3DiffuseColor;"); - goto LABEL_89; - } - VTX_EMIT("Out_LightingColor += max(dot(DirLight2Direction, WorldNormal), 0.0) * DirLight2DiffuseColor;"); - #endif // NEW_LIGHTING } LABEL_89: @@ -627,14 +676,17 @@ void BuildVertexSource_SkyGfx(int flags) if(flags & (FLAG_COLOR | FLAG_LIGHTING)) { #ifdef NEW_LIGHTING - VTX_EMIT("Out_Color.xyz = AmbEmissLight + DiffLight;"); - if(flags & FLAG_COLOR2) - VTX_EMIT("Out_Color.w = Color2.w;"); + if(pNewShaderLighting->GetBool()) + { + VTX_EMIT("Out_Color.xyz = AmbEmissLight + DiffLight;"); + if(flags & FLAG_COLOR2) + VTX_EMIT("Out_Color.w = Color2.w;"); + else + VTX_EMIT("Out_Color.w = GlobalColor.w;"); + } else - VTX_EMIT("Out_Color.w = GlobalColor.w;"); - #else - VTX_EMIT_V("Out_Color = vec4(Out_LightingColor * MaterialDiffuse.xyz, MaterialAmbient.w * %s.w);", _tmp); #endif // NEW_LIGHTING + VTX_EMIT_V("Out_Color = vec4(Out_LightingColor * MaterialDiffuse.xyz, MaterialAmbient.w * %s.w);", _tmp); VTX_EMIT("Out_Color = clamp(Out_Color, 0.0, 1.0);"); } }