Skip to content

Commit

Permalink
Implemented hw fogging into modern renderer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinnegatamante committed Aug 4, 2020
1 parent 57bc56e commit 04582ce
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 34 deletions.
49 changes: 25 additions & 24 deletions Source/HLEGraphics/BaseRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "Graphics/ColourValue.h"
#include "Utility/Preferences.h"

#if defined(DAEDALUS_PSP)
#include <pspgu.h>
#elif defined(DAEDALUS_VITA)
extern bool gUseRendererLegacy;

#include <vitaGL.h>
#else
#include "SysGL/GL.h"
#endif

#define HD_SCALE 0.754166f

Expand Down Expand Up @@ -131,9 +127,7 @@ ALIGNED_TYPE(struct, DaedalusLight, 16)
f32 qa; // Used by MM(GBI2 point light)
u32 Pad0; // Padding
};
#ifdef DAEDALUS_PSP
DAEDALUS_STATIC_ASSERT( sizeof( DaedalusLight ) == 64 ); //Size=64 bytes and order is important or VFPU ASM for PSP will fail
#endif

// Order here should be the same as in TnLMode
enum ETnLModeFlags
{
Expand Down Expand Up @@ -232,15 +226,26 @@ class BaseRenderer
inline void SetTextureEnable(bool enable) { mTnL.Flags.Texture = enable; }
inline void SetTextureTile(u32 tile) { mTextureTile = tile; }
inline u32 GetTextureTile() const { return mTextureTile; }
#ifdef DAEDALUS_VITA
inline void SetCullMode(bool enable, bool mode) { enable ? glEnable(GL_CULL_FACE) : glDisable(GL_CULL_FACE); mode ? glCullFace(GL_BACK) : glCullFace(GL_FRONT); }
#else
inline void SetCullMode(bool enable, bool mode) { mTnL.Flags.TriCull = enable; mTnL.Flags.CullBack = mode; }
#endif

// Fog stuff
inline void SetFogMultOffs(f32 Mult, f32 Offs) { mTnL.FogMult=Mult/255.0f; mTnL.FogOffs=Offs/255.0f;}
inline void SetFogMinMax(f32 fog_near, f32 fog_far) { glFogf(GL_FOG_START, fog_near / 1000.0f); glFogf(GL_FOG_END, fog_far / 1000.0f); }
inline void SetFogColour( c32 colour ) { float fog_clr[4] = {colour.GetRf(), colour.GetGf(), colour.GetBf(), colour.GetAf()}; glFogfv(GL_FOG_COLOR, &fog_clr[0]); }
inline void SetFogMinMax(f32 fog_near, f32 fog_far) {
if (gUseRendererLegacy) {
glFogf(GL_FOG_START, fog_near / 1000.0f); glFogf(GL_FOG_END, fog_far / 1000.0f);
} else {
mFogNear = fog_near / 1000.0f;
mFogFar = fog_far / 1000.0f;
}
}
inline void SetFogColour( c32 colour ) {
if (gUseRendererLegacy) {
float fog_clr[4] = {colour.GetRf(), colour.GetGf(), colour.GetBf(), colour.GetAf()};
glFogfv(GL_FOG_COLOR, &fog_clr[0]);
} else {
mFogColour = colour;
}
}

// PrimDepth will replace the z value if depth_source=1 (z range 32767-0 while PSP depthbuffer range 0-65535)//Corn
inline void SetPrimitiveDepth( u32 z ) { mPrimDepth = (f32)((int)z - 0x4000) / 16384.0f;}
Expand Down Expand Up @@ -280,13 +285,12 @@ class BaseRenderer
void SetScissor( u32 x0, u32 y0, u32 x1, u32 y1 );

void ForceViewport(float w, float h);
#ifdef DAEDALUS_VITA

void SetNegativeViewport();
void SetPositiveViewport();

virtual void DoGamma(float gamma) = 0;
virtual void DrawUITexture() = 0;
#endif

void ResetMatrices(u32 size);
void SetDKRMat(const u32 address, bool mul, u32 idx);
Expand Down Expand Up @@ -346,13 +350,8 @@ class BaseRenderer
v2 mVpTrans;

protected:
#if defined(DAEDALUS_VITA)
inline void UpdateFogEnable() { mTnL.Flags.Fog ? glEnable(GL_FOG) : glDisable(GL_FOG); }
inline void UpdateFogEnable() { if (gUseRendererLegacy) { mTnL.Flags.Fog ? glEnable(GL_FOG) : glDisable(GL_FOG); } }
inline void UpdateShadeModel() {}
#else
inline void UpdateFogEnable() { if(gFogEnabled) mTnL.Flags.Fog ? glEnable(GL_FOG) : glDisable(GL_FOG); }
inline void UpdateShadeModel() { glShadeModel( mTnL.Flags.Shade ? GL_SMOOTH : GL_FLAT ); }
#endif
void UpdateTileSnapshots( u32 tile_idx );
void UpdateTileSnapshot( u32 index, u32 tile_idx );

Expand Down Expand Up @@ -415,7 +414,9 @@ class BaseRenderer

f32 mPrimDepth;
f32 mPrimLODFraction;


f32 mFogNear;
f32 mFogFar;
c32 mFogColour; // Blender
c32 mPrimitiveColour; // Combiner
c32 mEnvColour; // Combiner
Expand Down
43 changes: 33 additions & 10 deletions Source/SysVita/HLEGraphics/RendererModern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ struct ShaderConfiguration
{
u64 Mux;
u32 CycleType : 2;
u8 AlphaThreshold;
u32 HasFog : 1;
float AlphaThreshold;
};


Expand All @@ -81,6 +82,7 @@ inline bool operator==(const ShaderConfiguration & a, const ShaderConfiguration
return
a.Mux == b.Mux &&
a.CycleType == b.CycleType &&
a.HasFog == b.HasFog &&
a.AlphaThreshold == b.AlphaThreshold;
}

Expand All @@ -92,6 +94,9 @@ struct ShaderProgram
GLint uloc_primcol;
GLint uloc_envcol;
GLint uloc_primlodfrac;
GLint uloc_fognear;
GLint uloc_fogfar;
GLint uloc_fogcolor;
};
static std::vector<ShaderProgram *> gShaders;

Expand Down Expand Up @@ -229,8 +234,12 @@ static const char* default_vertex_shader =
static const char* default_fragment_shader_fmt =
"void main(float2 sti : TEXCOORD0,\n"
" float4 v_col : COLOR0,\n"
" float4 coords : WPOS,\n"
" uniform sampler2D uTexture0 : TEXUNIT0,\n"
" uniform sampler2D uTexture1 : TEXUNIT1,\n"
" uniform float fog_near,\n"
" uniform float fog_far,\n"
" uniform float4 fog_color,\n"
" uniform float4 uPrimColour,\n"
" uniform float4 uEnvColour,\n"
" uniform float uPrimLODFrac,\n"
Expand Down Expand Up @@ -275,7 +284,7 @@ static void SprintShader(char (&frag_shader)[2048], const ShaderConfiguration &
u32 cA1 = (mux1>>18)&0x07; // c2 a3 // Ac1
u32 dA1 = (mux1 )&0x07; // c2 a4 // Ad1

char body[1024];
char body[2048];

u32 cycle_type = config.CycleType;

Expand Down Expand Up @@ -315,7 +324,13 @@ static void SprintShader(char (&frag_shader)[2048], const ShaderConfiguration &
if (config.AlphaThreshold > 0)
{
char * p = body + strlen(body);
sprintf(p, "\tif(col.a < %f) discard;\n", (float)config.AlphaThreshold / 255.f);
sprintf(p, "\tif(col.a < %f) discard;\n", config.AlphaThreshold);
}

if (config.HasFog)
{
char * p = body + strlen(body);
sprintf(p, "%s", "\tfloat vFog = clamp((fog_far - coords.z) / (fog_far - fog_near), 0.0, 1.0);\n\tcol.rgb = lerp(fog_color.rgb, col.rgb, vFog);\n");
}

sprintf(frag_shader, default_fragment_shader_fmt, body);
Expand All @@ -329,26 +344,27 @@ static void InitShaderProgram(ShaderProgram * program, const ShaderConfiguration
program->uloc_primcol = glGetUniformLocation(shader_program, "uPrimColour");
program->uloc_envcol = glGetUniformLocation(shader_program, "uEnvColour");
program->uloc_primlodfrac = glGetUniformLocation(shader_program, "uPrimLODFrac");
program->uloc_fognear = glGetUniformLocation(shader_program, "fog_near");
program->uloc_fogfar = glGetUniformLocation(shader_program, "fog_far");
program->uloc_fogcolor = glGetUniformLocation(shader_program, "fog_color");
}

void RendererModern::MakeShaderConfigFromCurrentState(ShaderConfiguration * config) const
{
config->Mux = mMux;
config->CycleType = gRDPOtherMode.cycle_type;
config->AlphaThreshold = 0;
config->HasFog = mTnL.Flags.Fog;

// Initiate Alpha test
if( (gRDPOtherMode.alpha_compare == G_AC_THRESHOLD) && !gRDPOtherMode.alpha_cvg_sel )
{
// G_AC_THRESHOLD || G_AC_DITHER
// FIXME(strmnnrmn): alpha func: (mAlphaThreshold | g_ROM.ALPHA_HACK) ? GL_GEQUAL : GL_GREATER
config->AlphaThreshold = mBlendColour.GetA();
u8 alpha_threshold = mBlendColour.GetA();
config->AlphaThreshold = (alpha_threshold || g_ROM.ALPHA_HACK) ? mBlendColour.GetAf() - 0.001f : mBlendColour.GetAf();
}
else if (gRDPOtherMode.cvg_x_alpha)
{
// Going over 0x70 brakes OOT, but going lesser than that makes lines on games visible...ex: Paper Mario.
// ALso going over 0x30 breaks the birds in Tarzan :(. Need to find a better way to leverage this.
config->AlphaThreshold = 0x70;
config->AlphaThreshold = 0.4392f;
}
else
{
Expand All @@ -364,6 +380,7 @@ void RendererModern::MakeShaderConfigFromCurrentState(ShaderConfiguration * conf
// Not sure about this. Should CYCLE_FILL have alpha kill?
if (cycle_type == CYCLE_FILL)
config->AlphaThreshold = 0;

}

static ShaderProgram * GetShaderForConfig(const ShaderConfiguration & config)
Expand Down Expand Up @@ -653,7 +670,13 @@ void RendererModern::PrepareRenderState(const float (&mat_project)[16], bool dis
glUniform4f(program->uloc_primcol, mPrimitiveColour.GetRf(), mPrimitiveColour.GetGf(), mPrimitiveColour.GetBf(), mPrimitiveColour.GetAf());
glUniform4f(program->uloc_envcol, mEnvColour.GetRf(), mEnvColour.GetGf(), mEnvColour.GetBf(), mEnvColour.GetAf());
glUniform1f(program->uloc_primlodfrac, mPrimLODFraction);


if (mTnL.Flags.Fog) {
glUniform1f(program->uloc_fognear, mFogNear);
glUniform1f(program->uloc_fogfar, mFogFar);
glUniform4f(program->uloc_fogcolor, mFogColour.GetRf(), mFogColour.GetGf(), mFogColour.GetBf(), mFogColour.GetAf());
}

// Second texture is sampled in 2 cycle mode if text_lod is clear (when set,
// gRDPOtherMode.text_lod enables mipmapping, but we just set lod_frac to 0.
bool use_t1 = cycle_mode == CYCLE_2CYCLE;
Expand Down

0 comments on commit 04582ce

Please sign in to comment.