Skip to content

Commit

Permalink
Merge pull request #218 from RainbowZerg/xd_dev
Browse files Browse the repository at this point in the history
Correct sunlight direction & mipmap bias control for DX10.
  • Loading branch information
Xottab-DUTY committed Jun 27, 2018
2 parents 9fd2d99 + 4a76ded commit 90864ad
Show file tree
Hide file tree
Showing 30 changed files with 140 additions and 198 deletions.
6 changes: 3 additions & 3 deletions src/Layers/xrRender/LightTrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,9 @@ void CROS_impl::update_smooth(IRenderable* O)
void CROS_impl::calc_sun_value(Fvector& position, IGameObject* _object)
{
#if RENDER == R_R1
light* sun = (light*)RImplementation.L_DB->sun_adapted._get();
light* sun = (light*)RImplementation.L_DB->sun._get();
#else
light* sun = (light*)RImplementation.Lights.sun_adapted._get();
light* sun = (light*)RImplementation.Lights.sun._get();
#endif
if (MODE & IRender_ObjectSpecific::TRACE_SUN)
{
Expand Down Expand Up @@ -530,7 +530,7 @@ void CROS_impl::prepare_lights(Fvector& position, IRenderable* O)
}

#if RENDER == R_R1
light* sun = (light*)RImplementation.L_DB->sun_adapted._get();
light* sun = (light*)RImplementation.L_DB->sun._get();

// Sun
float E = sun_smooth * sun->color.intensity();
Expand Down
149 changes: 43 additions & 106 deletions src/Layers/xrRender/Light_DB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,36 @@ CLight_DB::CLight_DB() {}
CLight_DB::~CLight_DB() {}
void CLight_DB::Load(IReader* fs)
{
IReader* F = 0;

// Lights itself
sun_original = NULL;
sun_adapted = NULL;
IReader* F = fs->open_chunk(fsL_LIGHT_DYNAMIC);
{
F = fs->open_chunk(fsL_LIGHT_DYNAMIC);
// Light itself
sun = NULL;

u32 size = F->length();
u32 size = F->length();
u32 element = sizeof(Flight) + 4;
u32 count = size / element;
VERIFY(count * element == size);
u32 count = (size / element);
VERIFY((count * element) == size);
v_static.reserve(count);
for (u32 i = 0; i < count; i++)
for (u32 i = 0; i < count; ++i)
{
Flight Ldata;
light* L = Create();
L->flags.bStatic = true;
L->set_type(IRender_Light::POINT);

#if RENDER == R_R1
L->set_shadow(false);
#else
L->set_shadow(true);
#endif
u32 controller = 0;
F->r(&controller, 4);
F->r(&Ldata, sizeof(Flight));

light* L = Create();
L->flags.bStatic = true;

if (Ldata.type == D3DLIGHT_DIRECTIONAL)
{
Fvector tmp_R;
tmp_R.set(1, 0, 0);

// directional (base)
sun_original = L;
L->set_type(IRender_Light::DIRECT);
L->set_shadow(true);
L->set_rotation(Ldata.direction, tmp_R);

// copy to env-sun
sun_adapted = L = Create();
L->flags.bStatic = true;
L->set_type(IRender_Light::DIRECT);
L->set_shadow(true);
L->set_rotation(Ldata.direction, tmp_R);
sun = L;
}
else
{
Expand All @@ -63,36 +48,25 @@ void CLight_DB::Load(IReader* fs)
tmp_R.set(1, 0, 0); // right

// point
v_static.push_back(L);
L->set_type(IRender_Light::POINT);
L->set_position(Ldata.position);
L->set_rotation(tmp_D, tmp_R);
L->set_range(Ldata.range);
L->set_color(Ldata.diffuse);
#if RENDER == R_R1
L->set_shadow(false);
#else
L->set_shadow(true);
#endif
L->set_active(true);
// R_ASSERT (L->spatial.sector );

v_static.push_back(L);
}
}

F->close();
}
R_ASSERT2(sun_original && sun_adapted, "Where is sun?");
F->close();

// fake spot
/*
if (0)
{
Fvector P; P.set(-5.58f, -0.00f + 2, -3.63f);
Fvector D; D.set(0,-1,0);
light* fake = Create();
fake->set_type (IRender_Light::SPOT);
fake->set_color (1,1,1);
fake->set_cone (deg2rad(60.f));
fake->set_direction (D);
fake->set_position (P);
fake->set_range (3.f);
fake->set_active (true);
}
*/
R_ASSERT2(sun, "Where is sun?");
}

#if RENDER != R_R1
Expand All @@ -102,9 +76,9 @@ void CLight_DB::LoadHemi()
if (FS.exist(fn_game, "$level$", "build.lights"))
{
IReader* F = FS.r_open(fn_game);

{
IReader* chunk = F->open_chunk(1); // Hemispheric light chunk
// Hemispheric light chunk
IReader* chunk = F->open_chunk(fsL_HEADER);

if (chunk)
{
Expand All @@ -116,38 +90,32 @@ void CLight_DB::LoadHemi()
for (u32 i = 0; i < count; i++)
{
R_Light Ldata;

chunk->r(&Ldata, sizeof(R_Light));

if (Ldata.type == D3DLIGHT_POINT)
// if (Ldata.type!=0)
{
light* L = Create();
L->flags.bStatic = true;
L->set_type(IRender_Light::POINT);

Fvector tmp_D, tmp_R;
tmp_D.set(0, 0, -1); // forward
tmp_R.set(1, 0, 0); // right

// point
v_hemi.push_back(L);
light* L = Create();
L->flags.bStatic = true;
L->set_type(IRender_Light::POINT);
L->set_position(Ldata.position);
L->set_rotation(tmp_D, tmp_R);
L->set_range(Ldata.range);
L->set_color(Ldata.diffuse.x, Ldata.diffuse.y, Ldata.diffuse.z);
L->set_active(true);
L->spatial.type = STYPE_LIGHTSOURCEHEMI;
L->set_attenuation_params(
Ldata.attenuation0, Ldata.attenuation1, Ldata.attenuation2, Ldata.falloff);
L->spatial.type = STYPE_LIGHTSOURCEHEMI;
// R_ASSERT (L->spatial.sector );

v_hemi.push_back(L);
}
}

chunk->close();
}
}

FS.r_close(F);
}
}
Expand All @@ -157,8 +125,7 @@ void CLight_DB::Unload()
{
v_static.clear();
v_hemi.clear();
sun_original.destroy();
sun_adapted.destroy();
sun.destroy();
}

light* CLight_DB::Create()
Expand All @@ -170,40 +137,31 @@ light* CLight_DB::Create()
return L;
}

#if RENDER == R_R1
void CLight_DB::add_light(light* L)
{
if (Device.dwFrame == L->frame_render)
return;
L->frame_render = Device.dwFrame;
#if RENDER == R_R1
if (L->flags.bStatic)
return; // skip static lighting, 'cause they are in lmaps
if (ps_r1_flags.test(R1FLAG_DLIGHTS))
RImplementation.L_Dynamic->add(L);
}
#endif

#if (RENDER == R_R2) || (RENDER == R_R3) || (RENDER == R_R4) || (RENDER == R_GL)
void CLight_DB::add_light(light* L)
{
if (Device.dwFrame == L->frame_render)
return;
L->frame_render = Device.dwFrame;
#else
if (RImplementation.o.noshadows)
L->flags.bShadow = FALSE;
if (L->flags.bStatic && !ps_r2_ls_flags.test(R2FLAG_R1LIGHTS))
return;
L->Export(package);
#endif
}
#endif // (RENDER==R_R2) || (RENDER==R_R3) || (RENDER==R_R4) || (RENDER==R_GL)

void CLight_DB::Update()
{
// set sun params
if (sun_original && sun_adapted)
if (sun)
{
light* _sun_original = (light*)sun_original._get();
light* _sun_adapted = (light*)sun_adapted._get();
light* _sun = (light*)sun._get();
CEnvDescriptor& E = *g_pGamePersistent->Environment().CurrentEnv;
VERIFY(_valid(E.sun_dir));
#ifdef DEBUG
Expand All @@ -226,37 +184,16 @@ void CLight_DB::Update()
#endif

VERIFY2(E.sun_dir.y < 0, "Invalid sun direction settings in evironment-config");
Fvector OD, OP, AD, AP;
OD.set(E.sun_dir).normalize();
OP.mad(Device.vCameraPosition, OD, -500.f);
AD.set(0, -.75f, 0).add(E.sun_dir);
Fvector dir, pos;
dir.set(E.sun_dir).normalize();
pos.mad(Device.vCameraPosition, dir, -500.f);

// for some reason E.sun_dir can point-up
int counter = 0;
while (AD.magnitude() < 0.001 && counter < 10)
{
AD.add(E.sun_dir);
counter++;
}
AD.normalize();
AP.mad(Device.vCameraPosition, AD, -500.f);
sun_original->set_rotation(OD, _sun_original->right);
sun_original->set_position(OP);
sun_original->set_color(E.sun_color.x, E.sun_color.y, E.sun_color.z);
sun_original->set_range(600.f);
sun_adapted->set_rotation(AD, _sun_adapted->right);
sun_adapted->set_position(AP);
sun_adapted->set_color(
sun->set_rotation(dir, _sun->right);
sun->set_position(pos);
sun->set_color(
E.sun_color.x * ps_r2_sun_lumscale, E.sun_color.y * ps_r2_sun_lumscale, E.sun_color.z * ps_r2_sun_lumscale);
sun_adapted->set_range(600.f);

if (!GEnv.Render->is_sun_static())
{
sun_adapted->set_rotation(OD, _sun_original->right);
sun_adapted->set_position(OP);
}
sun->set_range(600.f);
}

// Clear selection
package.clear();
}
3 changes: 1 addition & 2 deletions src/Layers/xrRender/Light_DB.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class CLight_DB
xr_vector<ref_light> v_hemi;

public:
ref_light sun_original;
ref_light sun_adapted;
ref_light sun;
light_Package package;

public:
Expand Down
13 changes: 7 additions & 6 deletions src/Layers/xrRender/r__dsgraph_build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,15 +837,16 @@ void D3DXRenderBase::Reset(HWND hWnd, u32& dwWidth, u32& dwHeight, float& fWidth
void D3DXRenderBase::SetupStates()
{
HW.Caps.Update();
#if defined(USE_DX10) || defined(USE_DX11) || defined(USE_OGL)
// TODO: DX10: Implement Resetting of render states into default mode
// VERIFY(!"D3DXRenderBase::SetupStates not implemented.");
#if defined(USE_OGL)
// TODO: OGL: Implement SetupStates().
#elif defined(USE_DX10) || defined(USE_DX11)
SSManager.SetMaxAnisotropy(ps_r__tf_Anisotropic);
SSManager.SetMipLODBias(ps_r__tf_Mipbias);
#else // USE_DX10
for (u32 i = 0; i < HW.Caps.raster.dwStages; i++)
{
float fBias = -.5f;
CHK_DX(HW.pDevice->SetSamplerState(i, D3DSAMP_MAXANISOTROPY, 4));
CHK_DX(HW.pDevice->SetSamplerState(i, D3DSAMP_MIPMAPLODBIAS, *(LPDWORD)&fBias));
CHK_DX(HW.pDevice->SetSamplerState(i, D3DSAMP_MAXANISOTROPY, ps_r__tf_Anisotropic));
CHK_DX(HW.pDevice->SetSamplerState(i, D3DSAMP_MIPMAPLODBIAS, *(LPDWORD)&ps_r__tf_Mipbias));
CHK_DX(HW.pDevice->SetSamplerState(i, D3DSAMP_MINFILTER, D3DTEXF_LINEAR));
CHK_DX(HW.pDevice->SetSamplerState(i, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR));
CHK_DX(HW.pDevice->SetSamplerState(i, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR));
Expand Down
23 changes: 6 additions & 17 deletions src/Layers/xrRender/xrRender_console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ float ps_r__ssaDONTSORT = 32.f; // RO
float ps_r__ssaHZBvsTEX = 96.f; // RO

int ps_r__tf_Anisotropic = 8;
float ps_r__tf_Mipbias = 0.0f;

// R1
float ps_r1_ssaLOD_A = 64.f;
float ps_r1_ssaLOD_B = 48.f;
float ps_r1_tf_Mipbias = 0.0f;
Flags32 ps_r1_flags = {R1FLAG_DLIGHTS}; // r1-only
float ps_r1_lmodel_lerp = 0.1f;
float ps_r1_dlights_clip = 40.f;
Expand All @@ -119,7 +119,6 @@ int ps_r1_SoftwareSkinning = 0; // r1-only
// R2
float ps_r2_ssaLOD_A = 64.f;
float ps_r2_ssaLOD_B = 48.f;
float ps_r2_tf_Mipbias = 0.0f;

// R2-specific
Flags32 ps_r2_ls_flags = {R2FLAG_SUN
Expand Down Expand Up @@ -299,9 +298,10 @@ class CCC_tf_MipBias : public CCC_Float
if (nullptr == HW.pDevice)
return;

#if defined(USE_DX10) || defined(USE_DX11) || defined(USE_OGL)
// TODO: DX10: Implement mip bias control
//VERIFY(!"apply not implmemented.");
#if defined(USE_OGL)
// TODO: OGL: Implement mipmap bias control.
#elif defined(USE_DX10) || defined(USE_DX11)
SSManager.SetMipLODBias(*value);
#else // USE_DX10
for (u32 i = 0; i < HW.Caps.raster.dwStages; i++)
CHK_DX(HW.pDevice->SetSamplerState(i, D3DSAMP_MIPMAPLODBIAS, *((LPDWORD)value)));
Expand Down Expand Up @@ -725,12 +725,12 @@ void xrRender_initconsole()
#endif // DEBUG

CMD2(CCC_tf_Aniso, "r__tf_aniso", &ps_r__tf_Anisotropic); // {1..16}
CMD2(CCC_tf_MipBias, "r__tf_mipbias", &ps_r__tf_Mipbias); // {-3 +3}

// R1
CMD4(CCC_Float, "r1_ssa_lod_a", &ps_r1_ssaLOD_A, 16, 96);
CMD4(CCC_Float, "r1_ssa_lod_b", &ps_r1_ssaLOD_B, 16, 64);
CMD4(CCC_Float, "r1_lmodel_lerp", &ps_r1_lmodel_lerp, 0, 0.333f);
CMD2(CCC_tf_MipBias, "r1_tf_mipbias", &ps_r1_tf_Mipbias); // {-3 +3}
CMD3(CCC_Mask, "r1_dlights", &ps_r1_flags, R1FLAG_DLIGHTS);
CMD4(CCC_Float, "r1_dlights_clip", &ps_r1_dlights_clip, 10.f, 150.f);
CMD4(CCC_Float, "r1_pps_u", &ps_r1_pps_u, -1.f, +1.f);
Expand All @@ -751,7 +751,6 @@ void xrRender_initconsole()
// R2
CMD4(CCC_Float, "r2_ssa_lod_a", &ps_r2_ssaLOD_A, 16, 96);
CMD4(CCC_Float, "r2_ssa_lod_b", &ps_r2_ssaLOD_B, 32, 64);
CMD2(CCC_tf_MipBias, "r2_tf_mipbias", &ps_r2_tf_Mipbias);

// R2-specific
CMD2(CCC_R2GM, "r2em", &ps_r2_gmaterial);
Expand Down Expand Up @@ -920,14 +919,4 @@ void xrRender_initconsole()
//CMD3(CCC_Mask, "r2_sun_ignore_portals", &ps_r2_ls_flags, R2FLAG_SUN_IGNORE_PORTALS);
}

void xrRender_apply_tf()
{
Console->Execute("r__tf_aniso");
#if RENDER == R_R1
Console->Execute("r1_tf_mipbias");
#else
Console->Execute("r2_tf_mipbias");
#endif
}

#endif
Loading

0 comments on commit 90864ad

Please sign in to comment.