mellinoe / veldrid-samples

Sample projects for Veldrid
https://mellinoe.github.io/veldrid-docs/
121 stars 49 forks source link

Add PBR sample ported from Nadrin/PBR #40

Open etinquis opened 2 years ago

etinquis commented 2 years ago

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

Yet to do:

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

etinquis commented 2 years ago

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 commented 2 years ago

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

etinquis commented 2 years ago

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 commented 2 years ago

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.

etinquis commented 2 years ago

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

etinquis commented 2 years ago

MoltenVK looking better now

Screen Shot 2021-12-21 at 2 48 16 PM
etinquis commented 2 years ago

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 commented 2 years ago

Now works without the hack :+1:

etinquis commented 2 years ago

Metal looking better now, but still scuffed

Screen Shot 2021-12-22 at 1 59 32 PM
etinquis commented 2 years ago

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.

etinquis commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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.