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

Add PBR sample ported from Nadrin/PBR #40

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

Conversation

etinquis
Copy link

@etinquis etinquis commented Dec 18, 2021

Sample is based on https://github.com/Nadrin/PBR/.

Yet to do:

  • Check that existing samples are running properly
  • Run through asset ProcessorFormats to make sure they're necessary and sufficient
  • Address PR comments
  • Merge dependent PRs and update dependency versions
  • (nice to have) add the additional pass(es) in the original sample that haven't been ported

Original text:

image

I've focused on Vulkan mainly thus far. There were some cubemap-related fixes required in the veldrid repo, so it will need to be locally built off https://github.com/etinquis/veldrid/tree/fix/pbr-cubemap-fixes and updated here. I will submit a PR to the veldrid repo once I'm done.

It fails in Direct3D11 with Compilation failed: RWTextureCube does not exist in HLSL, presumably due to use of a generated cube map.

The compute shaders and cube maps are misbehaving in OpenGL at the moment:
image

Martyn York added 2 commits December 17, 2021 16:21
* Implements missing field (de)serialization for ProcessedModel
* Adds basic support for passing extra data into AssetProcessor
  - this allows the e.g. the AssimpProcessor to accept as input the
    vertex data layout that it should use
* Adds StbImageSharp reference and support for loading .hdr images
* Removes direct Assimp interaction from AnimatedMesh, and replaces it
  with use of ProcessedModel
@etinquis etinquis marked this pull request as draft December 18, 2021 02:46
@etinquis
Copy link
Author

OpenGL progress
image

Added cubemap tests to veldrid that the cover the cubemap generation that was previously broken. Passing in OpenGL and Vulkan, failing in D3D11.
image

There appears to be a bug in full texture copy for cubemaps in Vulkan. There's a workaround in the GraphicsDeviceTestBase.GetReadBack(Texture) to work around this for now, but the failing Vulkan test covers it so once that's fixed it can be reverted.

specularBRDF_LUT generation is still broken. Looking into this next.

@etinquis
Copy link
Author

OpenGL working now; was an issue with resource naming in the sample.
image

@etinquis
Copy link
Author

Most of the d3d11 test failures were problems with the tests. I was ignoring the existence of row pitches, causing reads of memory outside of the intended bounds of the mapped resource.

image

Still investigating remaining failures.

@etinquis
Copy link
Author

etinquis commented Dec 21, 2021

Tests passing now
image

Been focusing on OSX. Added https://github.com/etinquis/veldrid-spirv/tree/feature/setshaderversion to the list of dependency changes (this adds the ability to set shader version when cross-compiling, and lets me up the MSL version to 2.1 for some necessary feature support).

Metal is looking... metal...
Screen Shot 2021-12-20 at 7 59 31 PM

MoltenVK is slightly better
Screen Shot 2021-12-20 at 8 13 51 PM

There were some more veldrid changes necessary to get that far. Will continue to experiment with this.

…h 6 layers) in places where we need to write to them in a compute shader so as to support d3d11
@etinquis
Copy link
Author

Direct3D11 is no longer completely unsupported, though it's looking stellar
image

@etinquis
Copy link
Author

MoltenVK looking better now

Screen Shot 2021-12-21 at 2 48 16 PM

swapping out shaders with hlsl shader; there's some badness happening
between the vert and frag shaders when using the cross-compiled glsl
versions
@etinquis
Copy link
Author

D3D11 kinda sorta working
image

I'm swapping out for the hlsl shader version in case of d3d.

I have yet to figure out whats causing the cross-compiled shaders to look like this
image

@etinquis
Copy link
Author

Now works without the hack 👍

@etinquis
Copy link
Author

Metal looking better now, but still scuffed
Screen Shot 2021-12-22 at 1 59 32 PM

@etinquis
Copy link
Author

Well, I think I know what the problem is, but not quite sure where it's coming from yet.

Metal is seeing this fragment shader (translated from pbr_fs.glsl)

[snip]
fragment main0_out main0(main0_in in [[stage_in]], constant _164& _166 [[buffer(0)]], constant _200& _202 [[buffer(1)]], texture2d<float> _177 [[texture(0)]], texture2d<float> _185 [[texture(1)]], texture2d<float> _191 [[texture(2)]], texture2d<float> _215 [[texture(3)]], texturecube<float> _349 [[texture(4)]], texturecube<float> _373 [[texture(5)]], texture2d<float> _386 [[texture(6)]], sampler _61 [[sampler(0)]])
{
    main0_out out = {};
    float3x3 _226 = {};
    _226[0] = in.m_226_0;
    _226[1] = in.m_226_1;
    _226[2] = in.m_226_2;
    float4 _426 = _177.sample(_61, in.m_179);
    float3 _183 = _426.xyz;
    float4 _433 = _185.sample(_61, in.m_179);
    float4 _440 = _191.sample(_61, in.m_179);
    float _195 = _440.x;
    float3 _212 = normalize(_202._m1.xyz - in.m_209);
    float4 _447 = _215.sample(_61, in.m_179);
    float3 _230 = normalize(_226 * normalize((_447.xyz * 2.0) - float3(1.0)));
[snip]

This is translated from this:

[snip]
void main()
{
	float alpha = clamp(viewProjectionMatrix[0][0], 0.0f, 1.0f) + 1.0f; // access something in TransformUniforms so it isn't omitted in msl fragment shader
	// Sample input textures to get shading model params.
	vec3 albedo = texture(albedoTexture, vin_texcoord).rgb;
	float metalness = texture(metalnessTexture, vin_texcoord).r;
	float roughness = texture(roughnessTexture, vin_texcoord).r;

	// Outgoing light direction (vector from world-space fragment position to the "eye").
	vec3 Lo = normalize(eyePosition.xyz - vin_pos);

	// Get current fragment's normal and transform to world space.
	vec3 N = normalize(2.0 * texture(normalTexture, vin_texcoord).rgb - 1.0);
	N = normalize(vin_tangentBasis * N);
[snip]

The issue appears to be the texture (_215) that's being referenced in

float4 _447 = _215.sample(_61, in.m_179);
float3 _230 = normalize(_226 * normalize((_447.xyz * 2.0) - float3(1.0)));

According to the MSL shader, _215 is texture(3) which is actually the roughness texture. The normal texture that it's supposed to be sampling would be texture(1).

So it seems something in the process is getting the textures confused. At the moment it appears to me to be something either in shaderc or spirv-cross.

…generated; it appears that texture binding order isn't respected somewhere which causes the code-side bindings not to match the MSL shader-side order
@etinquis
Copy link
Author

etinquis commented Dec 23, 2021

Rearranging all the bindings to appease the cross compilation gods appears to work around the problem.

It's ALIVE!

D3D11
image

OpenGL
image

Vulkan
image

Metal
Screen Shot 2021-12-22 at 6 48 59 PM

@etinquis etinquis marked this pull request as ready for review December 23, 2021 03:08
@@ -55,7 +55,7 @@ public void Run()
#if DEBUG
options.Debug = true;
#endif
_gd = VeldridStartup.CreateGraphicsDevice(_window, options, GraphicsBackend.Direct3D11);
_gd = VeldridStartup.CreateGraphicsDevice(_window, options, GraphicsBackend.OpenGL);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert

@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2010
# Visual Studio Version 17
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert


void main()
{
float alpha = clamp(viewProjectionMatrix[0][0], 0.0f, 1.0f) + 1.0f; // access something in TransformUniforms so it isn't omitted in msl fragment shader
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double-check if this is still necessary

layout(location=3) in vec3 bitangent;
layout(location=4) in vec2 texcoord;

#if VULKAN
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get rid of these

return textureSize(samplerCube(tex, textureSampler), lod);
}

#define PARAM_LEVEL pushConstants.level
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the #define VULKAN that necessitated these has been removed, they can be inlined.


if (GraphicsDevice.GetVulkanInfo(out var info))
{
info.TransitionImageLayout(specularEnvironmentMapTexture,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these are supposed to be necessary. I don't think I've fixed anything yet that would make them redundant but there may be something yet to fix in the library re: transitioning states after compute shaders are run?

@etinquis
Copy link
Author

etinquis commented Jan 3, 2022

Now running on Android in GLES (Samsung Galaxy A5)
Screenshot_20220102-190318_PBRAndroid

Can't test on device with Vulkan. VulkanUtils.TryLoadVulkan vkEnumerateInstanceExtensionProperties((byte*)null, &propCount, null) currently SIGABRTs

Forwarding debugger port 8857
This debug engine does not support exception conditions. The condition(s) will be ignored.
Detecting existing process
> am start -a "android.intent.action.MAIN" -c "android.intent.category.LAUNCHER" -n "PBR.Android.PBR.Android/crc64e474e6b4092e0b54.MainActivity"
> Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=PBR.Android.PBR.Android/crc64e474e6b4092e0b54.MainActivity }
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/PBR.Android.dll
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/Mono.Android.dll [External]
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/System.Runtime.dll [External]
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/Java.Interop.dll [External]
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/System.Collections.dll [External]
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/System.Threading.dll [External]
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/System.Runtime.InteropServices.dll [External]
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/System.Threading.Thread.dll [External]
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/System.Diagnostics.StackTrace.dll [External]
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/System.Reflection.Emit.Lightweight.dll [External]
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/System.Reflection.Emit.ILGeneration.dll [External]
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/System.Reflection.Primitives.dll [External]
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/SampleBase.Android.dll
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/mscorlib.dll [External]
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/Veldrid.dll
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/netstandard.dll [External]
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/PBR.dll
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/SampleBase.dll
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/System.dll [External]
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/Veldrid.SDL2.dll
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/AssetPrimitives.dll
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/System.Runtime.InteropServices.RuntimeInformation.dll [External]
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/vk.dll [External]
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/System.Collections.Concurrent.dll [External]
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/Veldrid.MetalBindings.dll
Loaded assembly: /data/data/PBR.Android.PBR.Android/files/.__override__/NativeLibraryLoader.dll [External]
Resolved pending breakpoint at 'VulkanUtil.cs:106,1' to bool Veldrid.Vk.VulkanUtil.TryLoadVulkan () [0x00000].
[Zygote] isWhitelistProcess - Process is Whitelisted
[libpersona] scanKnoxPersonas
[libpersona] Couldn't open the File - /data/system/users/0/personalist.xml - No such file or directory
[SELinux] SELinux selinux_android_compute_policy_index : Policy Index[2],  Con:u:r:zygote:s0 RAM:SEPF_SM-A520W_8.0.0_0018, [-1 -1 -1 -1 0 1]
[SELinux] SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=PBR.Android.PBR.Android 
[zygote64] Late-enabling -Xcheck:jni
[zygote64] no shared libraies, dex_files: 1
[debug-app-helper] Checking if libmonodroid was unpacked to /data/app/PBR.Android.PBR.Android-w_lUzVrXe87AH_fhadD_0g==/lib/arm64/libmonodroid.so
[debug-app-helper] Native libs extracted to /data/app/PBR.Android.PBR.Android-w_lUzVrXe87AH_fhadD_0g==/lib/arm64, assuming application/android:extractNativeLibs == true
[debug-app-helper] Setting up for DSO lookup in app data directories
[debug-app-helper] Added filesystem DSO lookup location: /data/app/PBR.Android.PBR.Android-w_lUzVrXe87AH_fhadD_0g==/lib/arm64
[debug-app-helper] Using runtime path: /data/app/PBR.Android.PBR.Android-w_lUzVrXe87AH_fhadD_0g==/lib/arm64
[debug-app-helper] checking directory: `/data/user/0/PBR.Android.PBR.Android/files/.__override__/lib`
[debug-app-helper] directory does not exist: `/data/user/0/PBR.Android.PBR.Android/files/.__override__/lib`
[debug-app-helper] Checking whether Mono runtime exists at: /data/user/0/PBR.Android.PBR.Android/files/.__override__/libmonosgen-2.0.so
[debug-app-helper] Checking whether Mono runtime exists at: /data/app/PBR.Android.PBR.Android-w_lUzVrXe87AH_fhadD_0g==/lib/arm64/libmonosgen-2.0.so
[debug-app-helper] Mono runtime found at: /data/app/PBR.Android.PBR.Android-w_lUzVrXe87AH_fhadD_0g==/lib/arm64/libmonosgen-2.0.so
[zygote64] Attempt to remove non-JNI local reference, dumping thread
[DOTNET] JNI_OnLoad: JNI_OnLoad in pal_jni.c
[DOTNET] GetOptionalMethod: optional method setApplicationProtocols ([Ljava/lang/String;)V was not found
[DOTNET] GetOptionalMethod: optional method getApplicationProtocol ()Ljava/lang/String; was not found
[monodroid] Creating public update directory: `/data/user/0/PBR.Android.PBR.Android/files/.__override__`
[zygote64] Attempt to remove non-JNI local reference, dumping thread
[monodroid-debug] Trying to initialize the debugger with options: --debugger-agent=transport=dt_socket,loglevel=0,address=127.0.0.1:8857,server=y,embedding=1
[monodroid-assembly] open_from_bundles: failed to load assembly PBR.Android.dll
[monodroid-gc] GREF GC Threshold: 46080
[monodroid-assembly] open_from_bundles: failed to load assembly Mono.Android.dll
[monodroid-assembly] open_from_bundles: failed to load assembly System.Runtime.dll
[monodroid-assembly] open_from_bundles: failed to load assembly Java.Interop.dll
[monodroid-assembly] open_from_bundles: failed to load assembly System.Collections.dll
[monodroid-assembly] open_from_bundles: failed to load assembly System.Threading.dll
[monodroid-assembly] open_from_bundles: failed to load assembly System.Runtime.InteropServices.dll
[monodroid-assembly] open_from_bundles: failed to load assembly System.Threading.Thread.dll
[monodroid-assembly] open_from_bundles: failed to load assembly System.Diagnostics.StackTrace.dll
[zygote64] Attempt to remove non-JNI local reference, dumping thread
[monodroid-assembly] open_from_bundles: failed to load assembly System.Reflection.Emit.Lightweight.dll
[monodroid-assembly] open_from_bundles: failed to load assembly System.Reflection.Emit.ILGeneration.dll
[monodroid-assembly] open_from_bundles: failed to load assembly System.Reflection.Primitives.dll
[monodroid-assembly] open_from_bundles: failed to load assembly SampleBase.Android.dll
[monodroid-assembly] open_from_bundles: failed to load assembly mscorlib.dll
[monodroid-assembly] open_from_bundles: failed to load assembly Veldrid.dll
[monodroid-assembly] open_from_bundles: failed to load assembly netstandard.dll
[monodroid-assembly] open_from_bundles: failed to load assembly PBR.dll
[monodroid-assembly] open_from_bundles: failed to load assembly SampleBase.dll
[monodroid-assembly] open_from_bundles: failed to load assembly System.dll
[monodroid-assembly] open_from_bundles: failed to load assembly Veldrid.SDL2.dll
[monodroid-assembly] open_from_bundles: failed to load assembly AssetPrimitives.dll
[monodroid-assembly] open_from_bundles: failed to load assembly System.Runtime.InteropServices.RuntimeInformation.dll
[monodroid-assembly] open_from_bundles: failed to load assembly vk.dll
[monodroid-assembly] open_from_bundles: failed to load assembly System.Collections.Concurrent.dll
[monodroid-assembly] open_from_bundles: failed to load assembly Veldrid.MetalBindings.dll
[monodroid-assembly] open_from_bundles: failed to load assembly NativeLibraryLoader.dll
[monodroid-assembly] Shared library 'libdl' not loaded, p/invoke 'dlerror' may fail
[libc] Invalid address 0x7452ffa2c8 passed to free: value not allocated
[libc] Fatal signal 6 (SIGABRT), code -6 in tid 30513 (oid.PBR.Android)

@TechPizzaDev
Copy link

TechPizzaDev commented Jan 6, 2022

Hmm I'm quite certain I've gotten a SIGABRT error with an very similar log but in net6-android.
Absolutely stellar work being done with this sample nonetheless.

I have a big suspicion that NativeLibraryLoader is outdated for net6 and not finding the right things.

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.

2 participants