Skip to content

Commit

Permalink
Merge pull request #8039 from Unity-Technologies/internal/2021.3/staging
Browse files Browse the repository at this point in the history
Internal/2021.3/staging
  • Loading branch information
UnityAljosha authored Feb 27, 2024
2 parents 7d9afb0 + 8b03488 commit 094993c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# Converting your shaders

Shaders written for the Built-in Render Pipeline are not compatible with the URP shaders.
Shaders written for the Built-in Render Pipeline are not compatible with Universal Render Pipeline (URP) shaders. Unity renders objects with the [default magenta error shader](https://docs.unity3d.com/Manual/shader-error.html) if they use Built-In Render Pipeline shaders.

For an overview of the mapping between built-in shaders and URP shaders, see [Shader mappings](#shader-mappings).

Use the [Render Pipeline Converter](features/rp-converter.md) to apply the shader mappings automatically.
Use the [Render Pipeline Converter](features/rp-converter.md) to convert any of Unity's built-in Built-In Render Pipeline materials and shaders to a URP material and shader. Refer to [Shader mappings](#shader-mappings) for more information.

**Note**: The Render Pipeline Converter makes irreversible changes to the project. Back up your project before the conversion.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ This guide demonstrates how to upgrade a custom unlit shader from Built-In Rende

The following shader is a simple unlit shader that works with the Built-In Render Pipeline. This guide demonstrates how to upgrade this shader to be compatible with URP.

```hlsl
```c++
Shader "Custom/UnlitShader"
{
Properties
Expand Down Expand Up @@ -81,23 +81,23 @@ The following steps show how to solve these issues and make a shader compatible
1. Change `CGPROGRAM` and `ENDCG` to `HLSLPROGRAM` and `ENDHLSL`.
2. Update the include statement to reference the `Core.hlsl` file.

```hlsl
```c++
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
```

> **Note**: `Core.hlsl` includes the core SRP library, URP shader variables, and matrix defines and transformations, but it does not include lighting functions or default structs.

3. Add `"RenderPipeline" = "UniversalPipeline"` to the shader tags.

```hlsl
```c++
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
```

> **Note**: URP does not support all ShaderLab tags. For more information on which tags URP supports, refer to [URP ShaderLab Pass tags](./urp-shaderlab-pass-tags.md).

4. Replace the `struct v2f` code block with the following `struct Varyings` code block. This changes the struct to use the URP naming convention of `Varyings` instead of `v2f`, and updates the shader to use the correct variables for URP.

```hlsl
```c++
struct Varyings
{
// The positions in this struct must have the SV_POSITION semantic.
Expand All @@ -109,7 +109,7 @@ The following steps show how to solve these issues and make a shader compatible
5. Beneath the include statement and above the `Varyings` struct, define a new struct with the name `Attributes`. This is equivalent to the Built-In Render Pipeline's appdata structs but with the new URP naming conventions.
6. Add the variables shown below to the `Attributes` struct.

```hlsl
```c++
struct Attributes
{
float4 positionOS : POSITION;
Expand All @@ -119,13 +119,13 @@ The following steps show how to solve these issues and make a shader compatible

7. Update the `v2f vert` function definition to use the new `Varyings` struct and take an instance of the `Attributes` struct as an input, as shown below.

```hlsl
```c++
Varyings vert(Attributes IN)
```

8. Update the vert function to output an instance of the `Varyings` struct and use the `TransformObjectToHClip` function to convert from object space to clip space. The function also needs to take the input `Attributes` UV and pass it to the output `Varyings` UV.

```hlsl
```c++
Varyings vert(Attributes IN)
{
Varyings OUT;
Expand All @@ -141,7 +141,7 @@ The following steps show how to solve these issues and make a shader compatible

9. Place a `CBUFFER` code block around the properties the shader uses, along with the `UnityPerMaterial` parameter.

```hlsl
```c++
CBUFFER_START(UnityPerMaterial)
float4 _Color;
sampler2D _MainTex;
Expand All @@ -152,7 +152,7 @@ The following steps show how to solve these issues and make a shader compatible

10. Update the `frag` function to use the `Varyings` input and the type `half4`, as shown below. The `frag` function must now use this type, as URP shaders do not support fixed types.

```hlsl
```c++
half4 frag(Varyings IN) : SV_Target
{
half4 texel = tex2D(_MainTex, IN.uv);
Expand All @@ -173,7 +173,7 @@ Although the shader is now compatible with URP and the SRP Batcher, you can't us
2. Remove the `[NoScaleOffset]` ShaderLab attribute from the `_BaseMap` property. You can now see **Tiling** and **Offset** properties in the shader's Inspector window.
3. Add the `[MainTexture]` ShaderLab attribute to the `_BaseMap` property and the `[MainColor]` attribute to the `_Color` property. This tells the Editor which property to return when you request the main texture or main color from another part of your project or in the Editor. The `Properties` section of your shader should now look as follows:

```hlsl
```c++
Properties
{
[MainTexture] _BaseMap("Main Texture", 2D) = "white" {}
Expand All @@ -183,14 +183,14 @@ Although the shader is now compatible with URP and the SRP Batcher, you can't us

4. Add the `TEXTURE2D(_BaseMap)` and `SAMPLER(sampler_BaseMap)` macros above the `CBUFFER` block. These macros define the texture and sampler state variables for use later. For more information on sampler states, refer to [Using sampler states](xref:SL-SamplerStates).

```hlsl
```c++
TEXTURE2D(_BaseMap);
SAMPLER(sampler_BaseMap);
```

5. Change the `sampler2D _BaseMap` variable inside the `CBUFFER` block to `float4 _BaseMap_ST`. This variable now stores the tiling and offset values set in the Inspector.

```hlsl
```c++
CBUFFER_START(UnityPerMaterial)
float4 _Color;
float4 _BaseMap_ST;
Expand All @@ -199,7 +199,7 @@ Although the shader is now compatible with URP and the SRP Batcher, you can't us

6. Change the `frag` function to access the texture with a macro instead of `tex2D` directly. To do this, replace `tex2D` with the `SAMPLE_TEXTURE2D` macro and add `sampler_BaseMap` as an additional parameter, as shown below:

```hlsl
```c++
half4 frag(Varyings IN) : SV_Target
{
half4 texel = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv);
Expand All @@ -209,7 +209,7 @@ Although the shader is now compatible with URP and the SRP Batcher, you can't us

7. In the `vert` function, change `OUT.uv` to use a macro instead of passing the texture coordinates as `IN.uv` directly. To do this, replace `IN.uv` with `TRANSFORM_TEX(IN.uv, _BaseMap)`. Your `vert` function should now look like the following example:

```hlsl
```c++
Varyings vert(Attributes IN)
{
Varyings OUT;
Expand All @@ -229,7 +229,7 @@ To see an example of the complete shader code, refer to the [Complete shader cod

## Complete shader code

```hlsl
```c++
Shader "Custom/UnlitShader"
{
Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ void CSMain(uint3 groupId : SV_GroupID,
{
${VFXLoadSize}
#if HAS_STRIPS
size3 += size3 < 0.0f ? -VFX_EPSILON : VFX_EPSILON; // Add an epsilon so that size is never 0 for strips
// Add an epsilon so that size is never 0 for strips
size3.x += size3.x < 0.0f ? -VFX_EPSILON : VFX_EPSILON;
size3.y += size3.y < 0.0f ? -VFX_EPSILON : VFX_EPSILON;
size3.z += size3.z < 0.0f ? -VFX_EPSILON : VFX_EPSILON;

#endif
float4x4 elementToVFX = GetElementToVFXMatrix(
attributes.axisX,
Expand Down

0 comments on commit 094993c

Please sign in to comment.