Skip to content

Pbr sky fixes metal #911

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

Merged
merged 6 commits into from
Jun 15, 2020
Merged

Pbr sky fixes metal #911

merged 6 commits into from
Jun 15, 2020

Conversation

jessebarker
Copy link
Contributor

Please read

Purpose of this PR

The constant buffer for the physically based sky does not have a proper layout for non-DirectX API platforms. 3-component vectors and 4-component vectors share the same size/padding constraints on other APIs, so we had a mismatch in the data layout between C# and the shader binding. This fixes that.

Testing status

Manual Tests: What did you do?

  • [ x] Opened test project + Run graphic tests locally
  • [ x] Built a player
  • Checked new UI names with UX convention
  • Tested UI multi-edition + Undo/Redo + Prefab overrides + Alignment in Preset
  • [ x] C# and shader warnings (supress shader cache to see them)
  • Checked new resources path for the reloader (in developer mode, you have a button at end of resources that check the paths)
  • Other:

Automated Tests: 5004, 5005, 5006 tests should now run properly.

Yamato: (Select your branch):
https://yamato.prd.cds.internal.unity3d.com/jobs/902-Graphics

Any test projects to go with this to help reviewers?
Should fix https://fogbugz.unity3d.com/f/cases/1253448/ as well as allow graphics tests 5004, 5005, and 5006 to be enabled at least for Mac.

Comments to reviewers

Notes for the reviewers you have assigned.

sebastienlagarde and others added 5 commits June 15, 2020 19:09
    - Update Vector3 to Vector4 to reflect actual padding/layout for non-DX
      platforms (both are 16 bytes in the shader space, but 12 bytes in C#)
    - Updates to shader code to reference only the float3 data being set up
@EvgeniiG
Copy link
Contributor

EvgeniiG commented Jun 15, 2020

I'm not sure I fully understand the issue here.

First of all, what happens when we declare

float3 A;
float B;

in a constant buffer on Metal? What kind of layout is it?

Vectors are 16-byte aligned in DX, so this results in optimal packing.

Secondly, I can understand if we were generating suboptimal code, but it sounds like we are generating incorrect code? This seems highly error-prone to me. Imagine writing some C code, you declare a struct, re-order a couple of members, your code compiles, but it's complete nonsense... Is it a part of a Metal spec that unless you manually pad everything, you get garbage results? Should we perhaps make the HLSL generation script take care of this for us?

I'd rather avoid tons of error-prone manual labor. It's also important that we do not encounter the same problem again in the future.

@EvgeniiG
Copy link
Contributor

I would also like to avoid wasting space in the constant buffer (and constant cache) if possible.

@sebastienlagarde
Copy link
Contributor

This RP fix current issue with Metal and PBR sky:
https://yamato-artifactviewer.prd.cds.internal.unity3d.com/904f27b8-e05d-4dc2-95b8-0a4097a162ee%2Flogs%2FTestProjects%2FHDRP_Tests%2Ftest-results%2Ftest-results/TestReportV1.html

Agree that we should fix the root cause of the issue but pretty sure that Jesse already have looked at it.
Yes, we can certainly save some space in constant buffer sky. Could you help @EvgeniiG with this, as you know better the code than Jesse. Thanks.

@EvgeniiG
Copy link
Contributor

EvgeniiG commented Jun 15, 2020

I have no issues with the current PR and I am sure Jesse tested it and it works. My issue is the manual approach to solving this problem since the issue is not limited to the PBR sky (as well as the wasted space, and it seems we can not use float3 at all anymore just because of Metal?).

@sebastienlagarde sebastienlagarde merged commit 452cd6b into HDRP/staging Jun 15, 2020
@sebastienlagarde sebastienlagarde deleted the pbr-sky-fixes-metal branch June 15, 2020 21:52
@sebastienlagarde sebastienlagarde mentioned this pull request Jun 15, 2020
6 tasks
sebastienlagarde added a commit that referenced this pull request Jun 15, 2020
* Adding purge of unused resources in render graph. (#872)

* First round of auto-exposure docs updates (#858)

* start of volume docs (need switching branch)

* moar docs

* Reviewed exposure override

* Reviewed render pipeline debug window doc and changed some formatting

* Changed html links to md

* Update HDRP-Camera.md

* Removed rogue space

* Small changes to the exposure page

Co-authored-by: Lewis Jordan <lewis.jordan@hotmail.co.uk>

* Fix depth offset and transparent motion vector for unlit transparent (#896)

* Fix depth offset and transparent motion vector for unlit transparent

* cleanup and fix the shadow pre/post alpha clip

* Fixed an exception occuring when a camera doesn't have an HDAdditionalCameraData (1254383). (#846)

Co-authored-by: sebastienlagarde <sebastien@unity3d.com>

* Fix Generates Include command issue with Physically Based Sky (#908)

* Split GenerateHLSL code from PhysicallyBasedSkyRenderer in a separate file to fix Generator Includes tool

* Update ShaderVariablesPhysicallyBasedSky.cs.hlsl

* Fix ray tracing with XR single-pass (#891)

* removed warnings related to ray binning in XR

* fix RenderRayTracedReflections with XR

* add errors if Path Tracing is used with XR single-pass

* add missing shader macros

* add entry to changelog

Co-authored-by: sebastienlagarde <sebastien@unity3d.com>

* Hdrp/clean shader stacks fields (#906)

* Cleanup the HD fields

* Fix distortion appearing two times in the UI

* Pbr sky fixes metal (#911)

* Split GenerateHLSL code from PhysicallyBasedSkyRenderer in a separate file to fix Generator Includes tool

* Update ShaderVariablesPhysicallyBasedSky.cs.hlsl

* Fix constant buffer layout for physically based sky
    - Update Vector3 to Vector4 to reflect actual padding/layout for non-DX
      platforms (both are 16 bytes in the shader space, but 12 bytes in C#)
    - Updates to shader code to reference only the float3 data being set up

* Update references screenshots

Co-authored-by: Sebastien Lagarde <sebastien@unity3d.com>

Co-authored-by: JulienIgnace-Unity <julien@unity3d.com>
Co-authored-by: FrancescoC-unity <43168857+FrancescoC-unity@users.noreply.github.com>
Co-authored-by: Lewis Jordan <lewis.jordan@hotmail.co.uk>
Co-authored-by: anisunity <42026998+anisunity@users.noreply.github.com>
Co-authored-by: Fabien Houlmann <44069206+fabien-unity@users.noreply.github.com>
Co-authored-by: Antoine Lelievre <antoinel@unity3d.com>
Co-authored-by: Jesse Barker <jesseb@unity3d.com>
@jessebarker
Copy link
Contributor Author

I'm not sure I fully understand the issue here.

First of all, what happens when we declare

float3 A; float B;

in a constant buffer on Metal? What kind of layout is it?

float3 and float4 both align to 16 bytes. Not just with Metal, but also with Vulkan.
So, your layout is:

'A' has offsetof(0)
'B' has offsetof(16)

Vectors are 16-byte aligned in DX, so this results in optimal packing.

Secondly, I can understand if we were generating suboptimal code, but it sounds like we are generating incorrect code? This seems highly error-prone to me. Imagine writing some C code, you declare a struct, re-order a couple of members, your code compiles, but it's complete nonsense... Is it a part of a Metal spec that unless you manually pad everything, you get garbage results? Should we perhaps make the HLSL generation script take care of this for us?

This is a problem for the cases where we are managing the constant buffers directly via the constant buffers API (i.e., C# code is providing the whole buffer and just telling the graphics device to make it available to the GPU for a given shader stage). As you can see from the above example, you will have put the data for 'B' in the wrong place. Further, we are potentially allocating the wrong size buffer as well because we are not rounding the size up according to the correct alignment constraints. We are also creating suboptimal buffer layouts all over the place, but because those "non-global" buffers are updated member by member, there are no issues with sizing and offsets, we're just not packing things the way we think we are.

@EvgeniiG
Copy link
Contributor

EvgeniiG commented Jun 16, 2020

HLSL also aligns all vectors to 16 bytes. See https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-packing-rules

'B' is not a vector, though, so it should be aligned with the next available 4-byte boundary rather than the next 16-byte boundary.

I suppose what you're saying is that all vectors are not just 16-byte aligned but are also 16 bytes in size?

@jessebarker
Copy link
Contributor Author

In Metal, yes, the float3 and float4 are both actually sized and aligned to 16 bytes, whereas for Vulkan/SPIR-V, I believe only the alignment is the same (the data is the data, but it must be aligned, so you can still have some unexpected results if you're not careful). Metal Shading Language specification section 2.2, and Vulkan specification chapter 14.5 have the gnarly details.

sebastienlagarde added a commit that referenced this pull request Jun 16, 2020
* Adding purge of unused resources in render graph. (#872)

* First round of auto-exposure docs updates (#858)

* start of volume docs (need switching branch)

* moar docs

* Reviewed exposure override

* Reviewed render pipeline debug window doc and changed some formatting

* Changed html links to md

* Update HDRP-Camera.md

* Removed rogue space

* Small changes to the exposure page

Co-authored-by: Lewis Jordan <lewis.jordan@hotmail.co.uk>

* Fix depth offset and transparent motion vector for unlit transparent (#896)

* Fix depth offset and transparent motion vector for unlit transparent

* cleanup and fix the shadow pre/post alpha clip

* Fixed an exception occuring when a camera doesn't have an HDAdditionalCameraData (1254383). (#846)

Co-authored-by: sebastienlagarde <sebastien@unity3d.com>

* Fix Generates Include command issue with Physically Based Sky (#908)

* Split GenerateHLSL code from PhysicallyBasedSkyRenderer in a separate file to fix Generator Includes tool

* Update ShaderVariablesPhysicallyBasedSky.cs.hlsl

* Fix ray tracing with XR single-pass (#891)

* removed warnings related to ray binning in XR

* fix RenderRayTracedReflections with XR

* add errors if Path Tracing is used with XR single-pass

* add missing shader macros

* add entry to changelog

Co-authored-by: sebastienlagarde <sebastien@unity3d.com>

* Hdrp/clean shader stacks fields (#906)

* Cleanup the HD fields

* Fix distortion appearing two times in the UI

* Pbr sky fixes metal (#911)

* Split GenerateHLSL code from PhysicallyBasedSkyRenderer in a separate file to fix Generator Includes tool

* Update ShaderVariablesPhysicallyBasedSky.cs.hlsl

* Fix constant buffer layout for physically based sky
    - Update Vector3 to Vector4 to reflect actual padding/layout for non-DX
      platforms (both are 16 bytes in the shader space, but 12 bytes in C#)
    - Updates to shader code to reference only the float3 data being set up

* Update references screenshots

Co-authored-by: Sebastien Lagarde <sebastien@unity3d.com>

* Cleaun shader config for shadow (#917)

* Move HDShadowFilteringQuality to main package as it is not used anymore

* Update Upgrading-from-2020.1-to-2020.2.md

* Add comment for _IncludeIndirectLighting for emissive mesh and clean ShaderPas template code

* Fix shader warning

* Fix shader config in DXR projects

* Fix warnings (#920)

* Enable render graph test (#873)

* re revert the PR

* Fixed planar probes with render graph.

* Fixed an issue with msaa resolve pass when movecs aren't enabled.

Co-authored-by: Julien Ignace <julien@unity3d.com>

* Add light layer on indirect lighting controller (#777)

* Add support for reflection controller.

* Fix compilation

* Fixed compilation issue and naming

* Fixed compilation issue and naming

* Update IndirectLightingControllerEditor.cs

* Update CHANGELOG.md

* Update Override-Indirect-Lighting-Controller.md

* Update Override-Indirect-Lighting-Controller.md

* Update Override-Indirect-Lighting-Controller.md

* Update. Indirect controller also affect planar

Co-authored-by: Sebastien Lagarde <sebastien@unity3d.com>

* Update IndirectLightingController.cs

* Compute shader stripping (#919)

* Compute shader stripping

* changelog

Co-authored-by: sebastienlagarde <sebastien@unity3d.com>

* Enable DXR automated tests for XR (#915)

* disable unsupported dxr tests in xr

* enable dxr tests in xr on Yamato

* add HDRP_DXR playmode_XR to ABV

* Change guards around ESRAM code  (#922)

* Change unity version guards for ESRAM

* move commas

* Bump min version (#923)

* Add a comment about packing float3/float4

Co-authored-by: JulienIgnace-Unity <julien@unity3d.com>
Co-authored-by: FrancescoC-unity <43168857+FrancescoC-unity@users.noreply.github.com>
Co-authored-by: Lewis Jordan <lewis.jordan@hotmail.co.uk>
Co-authored-by: anisunity <42026998+anisunity@users.noreply.github.com>
Co-authored-by: Fabien Houlmann <44069206+fabien-unity@users.noreply.github.com>
Co-authored-by: Antoine Lelievre <antoinel@unity3d.com>
Co-authored-by: Jesse Barker <jesseb@unity3d.com>
Co-authored-by: Evgenii Golubev <EvgeniiG@users.noreply.github.com>
@EvgeniiG
Copy link
Contributor

I see. That makes sense. Thank you.
So sounds like we should avoid using float3 in a constant buffer if we want optimal performance on Metal.
Vulkan appears to behave the same way as DX according to the spec (correct me if I am wrong).

@jessebarker
Copy link
Contributor Author

I might be misunderstanding the DX packing/alignment rules, but in Vulkan/SPIR-V the actual size of the data is as declared (12 bytes for float3, 16 bytes for float4), but the alignment is according to the 4-component vector (so, a float3 would have a 16-byte alignment).

@EvgeniiG
Copy link
Contributor

EvgeniiG commented Jun 17, 2020

Actually, you're right, it's slightly different in DX. See https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-packing-rules

In particular,

//  1 x 16byte elements
cbuffer IE
{
    float1 Val1;
    float3 Val2;
};

which would be 32 bytes in VK (if I understand it correctly, section 14.5.4).

So it's not really an alignment restriction but rather the "does not cross a 16-byte boundary" as the docs say.

@jessebarker
Copy link
Contributor Author

So, the data still occupies 16 bytes in that example, but it occupies it sparsely in a sense. 'Val1' would be at offset 0 and 'Val2' would be at offset 16, but I believe the buffer could technically be 28 bytes because 'Val2' is still only 12 bytes in actual size. For Metal, the buffer would definitely have to be 32 bytes because 'Val2' would actually be 16 bytes in size in that case.

sebastienlagarde added a commit that referenced this pull request Jun 18, 2020
* Adding purge of unused resources in render graph. (#872)

* First round of auto-exposure docs updates (#858)

* start of volume docs (need switching branch)

* moar docs

* Reviewed exposure override

* Reviewed render pipeline debug window doc and changed some formatting

* Changed html links to md

* Update HDRP-Camera.md

* Removed rogue space

* Small changes to the exposure page

Co-authored-by: Lewis Jordan <lewis.jordan@hotmail.co.uk>

* Fix depth offset and transparent motion vector for unlit transparent (#896)

* Fix depth offset and transparent motion vector for unlit transparent

* cleanup and fix the shadow pre/post alpha clip

* Fixed an exception occuring when a camera doesn't have an HDAdditionalCameraData (1254383). (#846)

Co-authored-by: sebastienlagarde <sebastien@unity3d.com>

* Fix Generates Include command issue with Physically Based Sky (#908)

* Split GenerateHLSL code from PhysicallyBasedSkyRenderer in a separate file to fix Generator Includes tool

* Update ShaderVariablesPhysicallyBasedSky.cs.hlsl

* Fix ray tracing with XR single-pass (#891)

* removed warnings related to ray binning in XR

* fix RenderRayTracedReflections with XR

* add errors if Path Tracing is used with XR single-pass

* add missing shader macros

* add entry to changelog

Co-authored-by: sebastienlagarde <sebastien@unity3d.com>

* Hdrp/clean shader stacks fields (#906)

* Cleanup the HD fields

* Fix distortion appearing two times in the UI

* Pbr sky fixes metal (#911)

* Split GenerateHLSL code from PhysicallyBasedSkyRenderer in a separate file to fix Generator Includes tool

* Update ShaderVariablesPhysicallyBasedSky.cs.hlsl

* Fix constant buffer layout for physically based sky
    - Update Vector3 to Vector4 to reflect actual padding/layout for non-DX
      platforms (both are 16 bytes in the shader space, but 12 bytes in C#)
    - Updates to shader code to reference only the float3 data being set up

* Update references screenshots

Co-authored-by: Sebastien Lagarde <sebastien@unity3d.com>

* Cleaun shader config for shadow (#917)

* Move HDShadowFilteringQuality to main package as it is not used anymore

* Update Upgrading-from-2020.1-to-2020.2.md

* Add comment for _IncludeIndirectLighting for emissive mesh and clean ShaderPas template code

* Fix shader warning

* Fix shader config in DXR projects

* Fix warnings (#920)

* Enable render graph test (#873)

* re revert the PR

* Fixed planar probes with render graph.

* Fixed an issue with msaa resolve pass when movecs aren't enabled.

Co-authored-by: Julien Ignace <julien@unity3d.com>

* Add light layer on indirect lighting controller (#777)

* Add support for reflection controller.

* Fix compilation

* Fixed compilation issue and naming

* Fixed compilation issue and naming

* Update IndirectLightingControllerEditor.cs

* Update CHANGELOG.md

* Update Override-Indirect-Lighting-Controller.md

* Update Override-Indirect-Lighting-Controller.md

* Update Override-Indirect-Lighting-Controller.md

* Update. Indirect controller also affect planar

Co-authored-by: Sebastien Lagarde <sebastien@unity3d.com>

* Update IndirectLightingController.cs

* Compute shader stripping (#919)

* Compute shader stripping

* changelog

Co-authored-by: sebastienlagarde <sebastien@unity3d.com>

* Enable DXR automated tests for XR (#915)

* disable unsupported dxr tests in xr

* enable dxr tests in xr on Yamato

* add HDRP_DXR playmode_XR to ABV

* Change guards around ESRAM code  (#922)

* Change unity version guards for ESRAM

* move commas

* Bump min version (#923)

* Add a comment about packing float3/float4

* Added Opaque Cull Mode option for materials and ShaderGraph (#918)

* Added the CullMode property for opaque objects

* Updated changelog

* Updated Opaque 8101 test

* Fix SG preview and settings indent level

* Add culmode opaque unlit

Co-authored-by: sebastienlagarde <sebastien@unity3d.com>

* Add test scene for SG vertex normal & tangent (#905)

* Add test scene for vertex normal and vertex tangent

* Add references images for other API

* Updated the 2020.1 upgrade guide for custom pass volumes (#924)

* HDRP Upgrade documentation for terrain new upgrade path (#901)

* Upgrading documentation update

* Typo

* Added link to Terrain class

* Fix warning in HDAdditionalLightData OnValidate (#885)

* fix warning in HDAdditionalLightData OnValidate

* Changelog

* Avoid calling update

Co-authored-by: sebastienlagarde <sebastien@unity3d.com>

* [10.x.x@ Update screenshots of test 8101 Opaque for HDRP test for Linux and Win Vulkank

* Add the list of HDRP forbidden keywords in SG blackboard (#926)

* Add the list of forbidden keywords to use in SG blackboard in the doc

* Update Customizing-HDRP-materials-with-Shader-Graph.md

* Update Customizing-HDRP-materials-with-Shader-Graph.md

* Add [MainColor] and [MainTexture] on property (#939)

* Scene exposure override (#889)

* Scene Exposure Override

* Changelog

* better changelog

* fix

Co-authored-by: sebastienlagarde <sebastien@unity3d.com>

* Exposure curve remapping can specify curves for min/max limits (#882)

* Port code

* changelog

* Revert HDRP asset

Co-authored-by: sebastienlagarde <sebastien@unity3d.com>

* IES Code path refactoring to support multiple SRP (#929)

* Refactor

* Refacto to support multiple SRPs

* Missing doc + Re-enable PreviewGUI in the Inspector

* Add Icons for IES Profiles

* Rename Compositor to Graphics Compositor (#950)

* Rename (HDRP) Compositor to Graphics Compositor

* Missed a few occurancies

* Ray traced reflection presets and fixes (#912)

* - Fixed a bug related to denoising ray traced reflections.
- Added presets for ray traced reflections.

* Testing the asset of the ray tracing usage parameters in ssr

* Mistake in history rejection for RTR

* Only use one texture for ray traced reflection upscaling
Adjust the upscale radius based on the roughness value

* Adjusted the preset values

* Updating the screenshots after changing the upscale radius metric.

Co-authored-by: sebastienlagarde <sebastien@unity3d.com>

* Fixed a warning in the ray tracing ambient occlusion compute shader. (#953)

* - Fixed a warning in the ray tracing ambient occlusion compute shader.

* Update CHANGELOG.md

Co-authored-by: sebastienlagarde <sebastien@unity3d.com>

* - Changed the way the filter size is decided for directional, point and spot shadows (#951)

Co-authored-by: sebastienlagarde <sebastien@unity3d.com>

* Various code cleanup in HDRP indirect diffuse lighting + Start remove macro RAYTRACING (#937)

* Change code to have a indirectDiffuseMode and start to clean RaytracingMacro

* Little extra cleanup

* Final Image Histogram Debug View (#887)

* Final image histogram ported

* changelog

Co-authored-by: sebastienlagarde <sebastien@unity3d.com>

* Cleanup area shadow code and Macro + remove more Raytracing macro (#945)

* Change code to have a indirectDiffuseMode and start to clean RaytracingMacro

* Little extra cleanup

* Clean area shadow code and remove usless macro

* update shader config files for projects

* Better naming for SKIP_RASTERIZED_AREA_SHADOWS

* Update Upgrading-from-2020.1-to-2020.2.md

* remove useless comment

* HDRP: Fix shader warning in RaytracingAmbientOcclusion.compute

* HDRP: Fix standalone build (Break in Exposure scene settings PR)

* HDRP: Update HDRenderPipeineResources.asset not up to date

* Cleanup HAVE_RECURSIVE_RENDERING usage (#958)

* Change code to have a indirectDiffuseMode and start to clean RaytracingMacro

* Little extra cleanup

* Clean area shadow code and remove usless macro

* update shader config files for projects

* Better naming for SKIP_RASTERIZED_AREA_SHADOWS

* Update Upgrading-from-2020.1-to-2020.2.md

* remove useless comment

* cleanup have deferred rendering

* Move Screen Space Shadow to multicompile and remove config requirement for DXR (#960)

* Change code to have a indirectDiffuseMode and start to clean RaytracingMacro

* Little extra cleanup

* Clean area shadow code and remove usless macro

* update shader config files for projects

* Better naming for SKIP_RASTERIZED_AREA_SHADOWS

* Update Upgrading-from-2020.1-to-2020.2.md

* remove useless comment

* cleanup have deferred rendering

* Clean RaytracingIndirectDiffuse.compute code

* Move screen space shadow to a multicompile + remove Raytracing macro

* Remove config package from DXR Test

Co-authored-by: JulienIgnace-Unity <julien@unity3d.com>
Co-authored-by: FrancescoC-unity <43168857+FrancescoC-unity@users.noreply.github.com>
Co-authored-by: Lewis Jordan <lewis.jordan@hotmail.co.uk>
Co-authored-by: anisunity <42026998+anisunity@users.noreply.github.com>
Co-authored-by: Fabien Houlmann <44069206+fabien-unity@users.noreply.github.com>
Co-authored-by: Antoine Lelievre <antoinel@unity3d.com>
Co-authored-by: Jesse Barker <jesseb@unity3d.com>
Co-authored-by: Evgenii Golubev <EvgeniiG@users.noreply.github.com>
Co-authored-by: remi-chapelain <57442369+remi-chapelain@users.noreply.github.com>
Co-authored-by: Pavlos Mavridis <pavlos.mavridis@unity3d.com>
Co-authored-by: skhiat <55133890+skhiat@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants