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

Not an issue - Amazing Project! Would love to have a URP version, even payed! #14

Open
giantgrey opened this issue Dec 14, 2023 · 9 comments · May be fixed by #18
Open

Not an issue - Amazing Project! Would love to have a URP version, even payed! #14

giantgrey opened this issue Dec 14, 2023 · 9 comments · May be fixed by #18

Comments

@giantgrey
Copy link

Hello,
this is not an issue. I just try to reach out to you this way. :)
First of all, I want to say this project is amazing! Compared to other gpu instancing solutions this one is by far the best!
I would love to have URP support. Is there any way you could help me in this regards? I tried to convert the shaders myself but without any luck. I would definitely be willing to pay for a URP version.

@mkrebser
Copy link
Owner

mkrebser commented Dec 16, 2023

I don't have enough time to do this personally. If you are writing a URP shader all you need is to make sure the instancing procedural setup function is being called.

Here is the simplest shader:
https://github.com/mkrebser/GPUInstance/blob/master/Assets/Resources/GPUInstance/instancemeshdefault.shader

To have the instancing working, it needs the #pragma instancing_options procedural:setup for defining the setup function to be invoked, it needs to include the cg include file with the setup function, then it needs to just call it

the .cginclude has some other useful but non required function in it too https://github.com/mkrebser/GPUInstance/blob/master/Assets/Resources/GPUInstance/gpuinstance_includes.cginc#L61

The only required function is the do_instance_setup(). This function is what is actually setting the object2World for the vertex shader step. This function assumes that unity_InstanceID is being populated by unity3d engine.

@giantgrey
Copy link
Author

Thank you so much for your time, really appreciate it!
I've tried to port the simplest shader to URP, without any luck. Shader code is at the bottom.
I've included #pragma instancing_options procedural:setup and the .cginclude (in URP it must be renamed to .hlsl)
But then I came accross this official Unity documentation
which says (in the table) that custom GPU instanced shaders are not supported on any SRP-Pipeline?! So this basically means that it'll never work on URP anyways?

Shader "Instanced/URP/Instance Default"
{
    Properties
    {
        [MainColor] _BaseColor("BaseColor", Color) = (1,1,1,1)
        [MainTexture] _BaseMap("BaseMap", 2D) = "white" {}
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalRenderPipeline"}

        // Include material cbuffer for all passes. 
        // The cbuffer has to be the same for all passes to make this shader SRP batcher compatible.
        HLSLINCLUDE
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
        CBUFFER_START(UnityPerMaterial)
        float4 _BaseMap_ST;
        half4 _BaseColor;
        CBUFFER_END
        ENDHLSL

        Pass
        {
            Tags { "LightMode"="UniversalForward" }

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            // -------------------------------------
            #pragma multi_compile_instancing
            #pragma instancing_options procedural:setup
            

            #include "gpuinstance_includes.hlsl"

            struct Attributes
            {
                float4 positionOS : POSITION;
                float4 tangent : TANGENT;
                float3 normal : NORMAL;
                float2 texcoord : TEXCOORD0;
                float2 texcoord1 : TEXCOORD1;
                float2 texcoord2 : TEXCOORD2;
                float2 texcoord3 : TEXCOORD3;
                float4 color : COLOR;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            }; 

            struct Varyings 
            {
                float4 positionCS   : SV_POSITION;
                float2 uv           : TEXCOORD0;
                float4 color        : COLOR;
            };

            TEXTURE2D(_BaseMap);
            SAMPLER(sampler_BaseMap);

           void setup()
	   {
		do_instance_setup();
	   }

            Varyings vert( Attributes IN)
            {
                Varyings OUT;

                // GetVertexPositionInputs computes position in different spaces (ViewSpace, WorldSpace, Homogeneous Clip Space)
                VertexPositionInputs positionInputs = GetVertexPositionInputs(IN.positionOS.xyz);
                OUT.positionCS = positionInputs.positionCS;     
                OUT.uv = TRANSFORM_TEX(IN.texcoord, _BaseMap);
                int id = get_instance_id();
                OUT.color = get_instance_color(id);

                return OUT;
            }

            half4 frag(Varyings IN) : SV_Target
            {
                float4 c = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv) * IN.color;
                return c * _BaseColor * IN.color;
            }
            ENDHLSL
        }
    }
}

@mkrebser
Copy link
Owner

mkrebser commented Dec 17, 2023

Oof yeah, it would appear URP can't be done based off that page

Not supported on the latest unity version either https://docs.unity3d.com/2023.2/Documentation/Manual/gpu-instancing-shader.html

@giantgrey
Copy link
Author

That's very unfortunate! Anyway, thanks for your time! :) Gonna have to find another solution...

@giantgrey
Copy link
Author

Alright maybe there's hope!
I've found an interesting tutorial on Youtube which shows how to use gpu instancing with shader graph. It's kind of a "hacky" way but I've managed to partially make it work! The meshes are now rendering on the correct position but the vertecies are not being animated / deformed. So maybe you can help me here again?
The tutorial is basically this one here: https://www.youtube.com/watch?v=bny9f4zw5JE
He's using two shader graph custom function nodes to enable gpu instancing.

@Bigcodersplanet
Copy link

Bigcodersplanet commented Mar 25, 2024

Alright maybe there's hope! I've found an interesting tutorial on Youtube which shows how to use gpu instancing with shader graph. It's kind of a "hacky" way but I've managed to partially make it work! The meshes are now rendering on the correct position but the vertecies are not being animated / deformed. So maybe you can help me here again? The tutorial is basically this one here: https://www.youtube.com/watch?v=bny9f4zw5JE He's using two shader graph custom function nodes to enable gpu instancing.

Hi, has anything else come up? The project is really great. Unfortunately I haven't found a way to get it running in URP. Best regards. @giantgrey can you upload your almost running project?

@Bigcodersplanet
Copy link

After a bit of experimenting with Unity's Shader Graph, the project also works in URP. Runs really great. The tip with the video and GPU instancing really helped.

@QQ1273459693
Copy link

在对 Unity 的 Shader Graph 进行一些实验后,该项目也可以在 URP 中运行。运行起来真的很棒。视频和 GPU 实例的技巧确实很有帮助。

Can you share with us? please! Thank you for being the greatest!

@Bigcodersplanet
Copy link

在对 Unity 的 Shader Graph 进行一些实验后,该项目也可以在 URP 中运行。运行起来真的很棒。视频和 GPU 实例的技巧确实很有帮助。

Can you share with us? please! Thank you for being the greatest!

Yes, of course. I'll put together a PullRequest. For now, I've implemented the most necessary shaders for our project so that the crowddemo runs. However, you should also be able to rebuild all the others with the custom nodes in Shader Graph. Regards

Bigcodersplanet pushed a commit to Bigcodersplanet/GPUInstance that referenced this issue Apr 12, 2024
+ URP version
+ Shaders for SkinnedMesh and default materials.
+ All others can be created in the Shader Graph. The nodes can simply be transferred for this purpose.
closes mkrebser#14
@Bigcodersplanet Bigcodersplanet linked a pull request Apr 12, 2024 that will close this issue
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 a pull request may close this issue.

4 participants