Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sun and lens flares #37

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

niligulmohar
Copy link
Contributor

Whether the sun and lens flares are drawn are determined by the result of
an occlusion query. If the query returns a larger number of pixels drawn
than expected, which often happens when running in a higher resolution
than the default, the sun and lens flares might not be drawn.

This change wraps the IDirect3DQuery9 objects used for occlusion queries
and modifies the returned number of pixels drawn to correspond with the
default resolution, which makes the sun and lens flares appear as
expected.

Initialization of GAUSS (the DOF blur filter) would fail and cause a crash
at startup if dofBlurAmount was set to anything larger than zero and the
dofOverrideResolution was zero or unspecified.

This change treats the DOF resolution as 360 when configured as zero for
the purpose of initializing the blur filter and detecting the DOF render
targets.
Whether the sun and lens flares are drawn are determined by the result of
an occlusion query. If the query returns a larger number of pixels drawn
than expected, which often happens when running in a higher resolution
than the default, the sun and lens flares might not be drawn.

This change wraps the IDirect3DQuery9 objects used for occlusion queries
and modifies the returned number of pixels drawn to correspond with the
default resolution, which makes the sun and lens flares appear as
expected.
@Areteic
Copy link

Areteic commented Jan 2, 2017

If I understand correctly this should fix Sun appearing/disappearing randomly after object passes in between camera and Sun? I am try to test this but so far Sun still flickers on and off. What DSfix.ini settings you have?

renderWidth 1920
renderHeight 1080
presentWidth 0
presentHeight 0
aaQuality 0
dofOverrideResolution 1079
disableDofScaling 0
dofBlurAmount 1

@niligulmohar
Copy link
Contributor Author

@SalamatiQus The render resolution should be the only setting that affects the result. Mine is 2560x1440.

I suspect there is a rounding issue with some resolutions. Let me see if i can find the problem.

@niligulmohar
Copy link
Contributor Author

@SalamatiQus I used a DSfix.ini containing only your settings above, and tried repeatedly blocking and unblocking the sun on the balcony with Solaire, but the sun always behaved correctly.

Does it work better for you if you make this addition?

diff --git a/d3d9query.cpp b/d3d9query.cpp
index 6c3e478..e0f77c6 100644
--- a/d3d9query.cpp
+++ b/d3d9query.cpp
@@ -41,6 +41,10 @@ HRESULT APIENTRY hkIDirect3DQuery9::GetData(void* pData, DWORD dwSize, DWORD dwG
        if (result == D3D_OK) {
                auto pixelsDrawn = reinterpret_cast<DWORD*>(pData);
                pixelsDrawn[0] = static_cast<DWORD>(pixelsDrawn[0] / RSManager::get().getAreaScale());
+               SDLOG(0, "pixelsDrawn: %d\n", pixelsDrawn[0]);
+               if (pixelsDrawn[0] > 368) {
+                       pixelsDrawn[0] = 368;
+               }
        }
        return result;
 }
\ No newline at end of file

@Areteic
Copy link

Areteic commented Jan 3, 2017

With this addition Sun now shows properly. Tried different locations, different render and dof resolutions. Praise the Sun :)

@niligulmohar
Copy link
Contributor Author

@SalamatiQus Great! I'm curious why you need that fix, though.

If you enable logging, what numbers come from the pixelsDrawn log message?

@Areteic
Copy link

Areteic commented Jan 4, 2017

It seems that DSFix only creates empty dsfix.log file with any value of logLevel from 0 to 11. Is there anything else am I missing or can be done from my side?

@niligulmohar
Copy link
Contributor Author

@SalamatiQus You probably need to comment out this line:

diff --git a/main.h b/main.h
index 0b02c30..4ac1a55 100644
--- a/main.h
+++ b/main.h
@@ -20,7 +20,7 @@

 #define VERSION "2.4"

-#define RELEASE_VER
+//#define RELEASE_VER

 #define WITHOUT_GFWL_LIB

@Areteic
Copy link

Areteic commented Jan 5, 2017

That worked.
In a initial patch I do not get persistent Sun and lens flare, and logfile doesn't have any mention of pixelsDrawn.
With additional fix Sun and lens flare are persistent and there are mentions (very short run of DaS) of pixelsDrawn in log file. All of them have value of 0

 BeginScene
 SetRenderTarget     0, 25997AC0
 Storing RT as main RT: 25997AC0
 MainRT uses: 0 + 1
 SetDepthStencilSurface 25997D80
 SetViewport X / Y - W x H :    0 /    0  -  1920 x 1080
 SetScissorRect RECT[   0/   0/1920/1080]
 pixelsDrawn: 0
 pixelsDrawn: 0
 SetRenderTarget     0, 25997600
 SetDepthStencilSurface 00000000
 SetRenderTarget     0, 25997840
 SetDepthStencilSurface 25997A80
 SetViewport X / Y - W x H :    0 /    0  -  1024 x 1024
 SetScissorRect RECT[   0/   0/1024/1024]
 SetVertexShader: 1B776DA0
 Updating pixel shader constant register c164
 Fixing half resolution register

@Areteic
Copy link

Areteic commented Jan 5, 2017

I think I have found reason why I needed fix. I have forced antialiasing for Dark Souls through nvidiaInspector tool. If there is no forced AA then Sun is working with initial patch, however setting any kind of AA (with exception of post-process AA) through drivers will make Sun disappear again. Tried MSAA, CSAA, SGSSAA and TrSSAA.
Additional fix makes it possible to have Sun behave correctly and have forced AA through drivers.

@niligulmohar
Copy link
Contributor Author

@SalamatiQus I've tried reproducing the problem by forcing AA through Nvidia profile inspector, but i have not been able to make forced AA have any effect. What settings do you use?

@Areteic
Copy link

Areteic commented Jan 5, 2017

I have Antialiasing compatibility set to 0x004000C0 as it makes AA work more properly, without blur. Exact value is taken from community AA guide. I see now that issue is not reproducible if AA is forced but Antialiasing compatibility left at default value. I should have clarified this earlier, my apologies.

Occlusion queries might return larger values than expected if
driver-enforced antialiasing is enabled.

This change makes an occlusion query for a square of a known size to
determine what result that should be expected. This is used by the
IDirect3DQuery9 wrapper to scale the result of further queries
appropriately, considering both the rendering resolution and unexpected
multisampling enforced by the driver.
@niligulmohar
Copy link
Contributor Author

@SalamatiQus Thanks! I've improved the fix now so it should work with forced anti aliasing without requiring the fix that caps the value at 368. This should handle partial hiding of the sun properly, and wont' break if this is used in another place where larger values are expected.

@niligulmohar
Copy link
Contributor Author

9b834cc uses ATL just for CComPtr. ATL isn't included with VS2012 by default. I'll remove use of CComPtr so that it doesn't have any unnecessary extra dependencies.

@prototype99
Copy link

where did you get d3d9query.h? i can't seem to find it

@niligulmohar
Copy link
Contributor Author

@prototype99 It was added in this PR.

@prototype99
Copy link

Oh whoops guess I should pay more attention to file names

@Areteic
Copy link

Areteic commented Jul 6, 2017

I think I found new case where Sun and Flares still disappear. It happened with following DSfix settings

renderWidth 3840
renderHeight 2160
presentWidth 1920
presentHeight 1080
dofOverrideResolution 2161
dofBlurAmount 2

@niligulmohar
Copy link
Contributor Author

@SalamatiQus I can confirm that the bug still appears with the settings you specify. I will take a look at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants