From 18d59a9e6750303a7ab55bde6993ab1e80fd9e9f Mon Sep 17 00:00:00 2001 From: Alexey Orlov Date: Thu, 13 Sep 2018 12:14:11 +0300 Subject: [PATCH 1/4] open 2019-dev From 208fe61348b8bd534b2221546478a6194e900d6e Mon Sep 17 00:00:00 2001 From: Alexey Orlov Date: Mon, 17 Sep 2018 11:41:16 +0300 Subject: [PATCH 2/4] added sample for using METAL_BUFFER_INPUT in native metal shaders --- .../Assets/MetalSimpleTexture.shader | 11 ++++++-- .../Assets/TestMetalShader.cs | 25 ++++++++++++++++--- Graphics/MetalNativeShader/README.md | 10 +++++--- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/Graphics/MetalNativeShader/Assets/MetalSimpleTexture.shader b/Graphics/MetalNativeShader/Assets/MetalSimpleTexture.shader index 63c11c7..f141a3d 100644 --- a/Graphics/MetalNativeShader/Assets/MetalSimpleTexture.shader +++ b/Graphics/MetalNativeShader/Assets/MetalSimpleTexture.shader @@ -31,6 +31,11 @@ METAL_CONST_VECTOR(half, 4, _Color); }; + struct ColorInput + { + float4 color; + }; + struct InputVP { float4 pos METAL_VERTEX_INPUT(0); @@ -53,10 +58,12 @@ output.uv = input.uv; return output; } - fragment OutputFS frag(constant Globals& glob [[ buffer(0) ]], OutputVP input [[ stage_in ]], METAL_TEX_INPUT(texture2d, 0, _MainTex)) + fragment OutputFS frag(constant Globals& glob [[ buffer(0) ]], METAL_BUFFER_INPUT(ColorInput, 1, _ColorBuffer), + METAL_TEX_INPUT(texture2d, 0, _MainTex), + OutputVP input [[ stage_in ]]) { OutputFS output; - output.color.rgb = glob._Color.rgb * _MainTex.sample(sampler__MainTex, input.uv).xyz; + output.color.rgb = glob._Color.rgb * (half3)_ColorBuffer[0].color.xyz * _MainTex.sample(sampler__MainTex, input.uv).xyz; output.color.a = 1; return output; } diff --git a/Graphics/MetalNativeShader/Assets/TestMetalShader.cs b/Graphics/MetalNativeShader/Assets/TestMetalShader.cs index 56d9218..094dd85 100644 --- a/Graphics/MetalNativeShader/Assets/TestMetalShader.cs +++ b/Graphics/MetalNativeShader/Assets/TestMetalShader.cs @@ -6,6 +6,10 @@ public class TestMetalShader : MonoBehaviour { public Shader shader; + private Material mat; + private Texture tex; + private ComputeBuffer buf; + private Texture2D CreateTexture(int ext) { Texture2D tex = new Texture2D(ext,ext,TextureFormat.RGBA32, false,false); @@ -21,6 +25,7 @@ private Texture2D CreateTexture(int ext) } } tex.SetPixels(pixels); + tex.wrapMode = TextureWrapMode.Clamp; tex.Apply(false, false); return tex; @@ -28,9 +33,23 @@ private Texture2D CreateTexture(int ext) void Start() { - Material mat = new Material(shader); - mat.mainTexture = CreateTexture(64); - mat.color = new Color(1,0,0,1); + buf = new ComputeBuffer(1, 4*sizeof(float)); + buf.SetData(new float[]{0.0f,1.0f,0.0f,1.0f}); + + tex = CreateTexture(32); + + mat = new Material(shader); + mat.mainTexture = tex; + mat.SetBuffer("_ColorBuffer", buf); + mat.color = new Color(1,1,0,1); + GetComponent().material = mat; } + + void OnDisable() + { + DestroyImmediate(mat); + buf.Release(); + DestroyImmediate(tex); + } } diff --git a/Graphics/MetalNativeShader/README.md b/Graphics/MetalNativeShader/README.md index deb7c7e..abde6f4 100644 --- a/Graphics/MetalNativeShader/README.md +++ b/Graphics/MetalNativeShader/README.md @@ -8,7 +8,7 @@ This is a sample of using Metal Shading Language in Unity Shaders. ##Prerequisites -Unity: 2017 +Unity: 2019 ## How does it work @@ -29,9 +29,13 @@ Use `METAL_VERTEX_INPUT` to mark vertex data. Arguments are: 0 for position, 1 - float2 uv METAL_VERTEX_INPUT(3); }; -Use `METAL_TEX_INPUT` to mark used textures. Arguments are: first is metal type to use, second bind point and third texture property name. +Use `METAL_TEX_INPUT` to mark used textures. Arguments are: first is metal type to use, second is the bind point and third is the property name. - fragment OutputFS frag(<...>, METAL_TEX_INPUT(texture2d, 0, _MainTex)) + METAL_TEX_INPUT(texture2d, 0, _MainTex) + +Use `METAL_BUFFER_INPUT` to mark used buffers. HLSL analogue is StructuredBuffer, but unlike HLSL you need to pass element count yourself. Arguments are: first is the type of element, second is the bind point and third is the property name. + + METAL_BUFFER_INPUT(ColorInput, 1, _ColorBuffer) Use `METAL_CONST_MATRIX` and `METAL_CONST_VECTOR` to mark uniform declarations. Arguments are: `METAL_CONST_VECTOR(type, dim, name)` and `METAL_CONST_MATRIX(type, rows, cols, name)` From 25ba08183c044d36dcbba35f8e8f83960ad92a95 Mon Sep 17 00:00:00 2001 From: Alexey Orlov Date: Mon, 17 Sep 2018 11:47:47 +0300 Subject: [PATCH 3/4] update METAL_VERTEX_INPUT due to (internal unity) changes to vertex input numbering --- Graphics/MetalNativeShader/Assets/MetalSimpleTexture.shader | 2 +- Graphics/MetalNativeShader/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Graphics/MetalNativeShader/Assets/MetalSimpleTexture.shader b/Graphics/MetalNativeShader/Assets/MetalSimpleTexture.shader index f141a3d..9854419 100644 --- a/Graphics/MetalNativeShader/Assets/MetalSimpleTexture.shader +++ b/Graphics/MetalNativeShader/Assets/MetalSimpleTexture.shader @@ -39,7 +39,7 @@ struct InputVP { float4 pos METAL_VERTEX_INPUT(0); - float2 uv METAL_VERTEX_INPUT(3); + float2 uv METAL_VERTEX_INPUT(4); }; struct OutputVP { diff --git a/Graphics/MetalNativeShader/README.md b/Graphics/MetalNativeShader/README.md index abde6f4..89b29f4 100644 --- a/Graphics/MetalNativeShader/README.md +++ b/Graphics/MetalNativeShader/README.md @@ -21,7 +21,7 @@ Please note that you specify entry points for vertex program and fragment shader To connect your shaders with Unity you need to mark vertex inputs, uniforms and textures. Please note that for uniforms only one (shared between vertex program and fragment shaders) uniform buffer is supported. -Use `METAL_VERTEX_INPUT` to mark vertex data. Arguments are: 0 for position, 1 - normal, 2 - color, 3-6 - uvs, 7 - tangent. +Use `METAL_VERTEX_INPUT` to mark vertex data. Arguments are: 0 for position, 1 - normal, 2 - tangent, 3 - color, 4-7 - uvs struct InputVP { From 0c155b56b79649ed840e6cf35608040828e6d687 Mon Sep 17 00:00:00 2001 From: Alexey Orlov Date: Tue, 18 Sep 2018 15:20:47 +0300 Subject: [PATCH 4/4] use METAL_BUFFER_INPUT_DATA to extract actual data pointer from buffer (as we have extra space allocated for counter). As we care only about "input" buffers for now we dont need counter at all. --- Graphics/MetalNativeShader/Assets/MetalSimpleTexture.shader | 2 ++ Graphics/MetalNativeShader/README.md | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/Graphics/MetalNativeShader/Assets/MetalSimpleTexture.shader b/Graphics/MetalNativeShader/Assets/MetalSimpleTexture.shader index 9854419..9803dff 100644 --- a/Graphics/MetalNativeShader/Assets/MetalSimpleTexture.shader +++ b/Graphics/MetalNativeShader/Assets/MetalSimpleTexture.shader @@ -62,6 +62,8 @@ METAL_TEX_INPUT(texture2d, 0, _MainTex), OutputVP input [[ stage_in ]]) { + METAL_BUFFER_INPUT_DATA(ColorInput, _ColorBuffer) + OutputFS output; output.color.rgb = glob._Color.rgb * (half3)_ColorBuffer[0].color.xyz * _MainTex.sample(sampler__MainTex, input.uv).xyz; output.color.a = 1; diff --git a/Graphics/MetalNativeShader/README.md b/Graphics/MetalNativeShader/README.md index 89b29f4..d93f197 100644 --- a/Graphics/MetalNativeShader/README.md +++ b/Graphics/MetalNativeShader/README.md @@ -37,6 +37,11 @@ Use `METAL_BUFFER_INPUT` to mark used buffers. HLSL analogue is StructuredBuffer METAL_BUFFER_INPUT(ColorInput, 1, _ColorBuffer) +As metal do not have implicit UAV counters, unity is forced to allocate extra space in all Compute Buffers, so you also need `METAL_BUFFER_INPUT_DATA` to extract data pointer. Arguments are: first is the type of element, second is the property name. + + METAL_BUFFER_INPUT_DATA(ColorInput, _ColorBuffer) + + Use `METAL_CONST_MATRIX` and `METAL_CONST_VECTOR` to mark uniform declarations. Arguments are: `METAL_CONST_VECTOR(type, dim, name)` and `METAL_CONST_MATRIX(type, rows, cols, name)` struct Globals