Skip to content

Commit

Permalink
dc
Browse files Browse the repository at this point in the history
  • Loading branch information
afeilo committed Aug 28, 2020
1 parent 9f0c1b2 commit 2595489
Show file tree
Hide file tree
Showing 26 changed files with 1,687 additions and 7 deletions.
12 changes: 8 additions & 4 deletions Assets/Custom/CameraRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public partial class CameraRender
name = bufferName
};

public void Render(ScriptableRenderContext context,Camera camera)
public void Render(ScriptableRenderContext context,Camera camera, bool useDynamicBatching, bool useGPUInstancing)
{
this.context = context;
this.camera = camera;
Expand All @@ -27,7 +27,7 @@ public void Render(ScriptableRenderContext context,Camera camera)
return;
}
Setup();
DrawVisibleGeometry();
DrawVisibleGeometry(useDynamicBatching, useGPUInstancing);
DrawUnsupportedShaders();
DrawGizmos();
Submit();
Expand Down Expand Up @@ -64,11 +64,15 @@ void ExecuteBuffer()
buffer.Clear();
}

void DrawVisibleGeometry() {
void DrawVisibleGeometry(bool useDynamicBatching,bool useGPUInstancing) {
var sortingSettings = new SortingSettings(camera) {
criteria = SortingCriteria.CommonOpaque
};
var drawingSettings = new DrawingSettings(unlitShaderTagId, sortingSettings);
var drawingSettings = new DrawingSettings(unlitShaderTagId, sortingSettings)
{
enableDynamicBatching = useDynamicBatching,
enableInstancing = useGPUInstancing,
};
var filteringSettings = new FilteringSettings(RenderQueueRange.opaque);
context.DrawRenderers(cullingResults, ref drawingSettings, ref filteringSettings);
context.DrawSkybox(camera);
Expand Down
3 changes: 3 additions & 0 deletions Assets/Custom/MyPipeline.asset
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: a4f143bba98e484448db26f45bb4bf63, type: 3}
m_Name: MyPipeline
m_EditorClassIdentifier:
useDynamicBatching: 1
useGPUInstancing: 1
useSRPBatcher: 1
8 changes: 6 additions & 2 deletions Assets/Custom/MyPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
public class MyPipeline : RenderPipeline
{
CameraRender cameraRender;
public MyPipeline() : base()
bool useDynamicBatching, useGPUInstancing;
public MyPipeline(bool useDynamicBatching, bool useGPUInstancing, bool useSRPBatcher) : base()
{
this.useDynamicBatching = useDynamicBatching;
this.useGPUInstancing = useGPUInstancing;
cameraRender = new CameraRender();
GraphicsSettings.useScriptableRenderPipelineBatching = useSRPBatcher;
}

protected override void Render(ScriptableRenderContext context, Camera[] cameras)
{
foreach (var camera in cameras)
{
cameraRender.Render(context, camera);
cameraRender.Render(context, camera, useDynamicBatching, useGPUInstancing);
}
}
public void Dispose()
Expand Down
4 changes: 3 additions & 1 deletion Assets/Custom/MyPipelineAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
[CreateAssetMenu(menuName = "Rendering/My Pipeline")]
public class MyPipelineAsset : RenderPipelineAsset
{
[SerializeField]
bool useDynamicBatching = true, useGPUInstancing = true, useSRPBatcher = true;
protected override RenderPipeline CreatePipeline()
{
return new MyPipeline();
return new MyPipeline(useDynamicBatching,useGPUInstancing,useSRPBatcher);
}


Expand Down
8 changes: 8 additions & 0 deletions Assets/Custom/ShaderLibrary.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Assets/Custom/ShaderLibrary/Common.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef CUSTOM_COMMON_INCLUDED
#define CUSTOM_COMMON_INCLUDED
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "UnityInput.hlsl"
#define UNITY_MATRIX_M unity_ObjectToWorld
#define UNITY_MATRIX_I_M unity_WorldToObject
#define UNITY_MATRIX_V unity_MatrixV
#define UNITY_MATRIX_VP unity_MatrixVP
#define UNITY_MATRIX_P glstate_matrix_projection

#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl"

#endif
9 changes: 9 additions & 0 deletions Assets/Custom/ShaderLibrary/Common.hlsl.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions Assets/Custom/ShaderLibrary/UnityInput.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef CUSTOM_UNITY_INPUT_INCLUDED
#define CUSTOM_UNITY_INPUT_INCLUDED
CBUFFER_START(UnityPerDraw)
float4x4 unity_ObjectToWorld;
float4x4 unity_WorldToObject;
float4 unity_LODFade;
real4 unity_WorldTransformParams;
CBUFFER_END
float4x4 unity_MatrixVP;
float4x4 unity_MatrixV;
float4x4 glstate_matrix_projection;

#endif
9 changes: 9 additions & 0 deletions Assets/Custom/ShaderLibrary/UnityInput.hlsl.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Custom/batch.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions Assets/Custom/batch/MeshBall.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using UnityEngine;

public class MeshBall : MonoBehaviour
{
static int baseColorId = Shader.PropertyToID("_BaseColor");

[SerializeField]
Mesh mesh = default;

[SerializeField]
Material material = default;

static MaterialPropertyBlock block;

Matrix4x4[] matrices = new Matrix4x4[1023];
Vector4[] colors = new Vector4[1023];
private void Awake()
{
for (int i = 0; i < matrices.Length; i++)
{
matrices[i] = Matrix4x4.TRS(Random.insideUnitSphere * 10f, Quaternion.identity, Vector3.one);
colors[i] = new Vector4(Random.value, Random.value, Random.value, 1);
}
}

void Update()
{
if (null == block)
{
block = new MaterialPropertyBlock();
block.SetVectorArray(baseColorId, colors);
}
Graphics.DrawMeshInstanced(mesh, 0, material, matrices, 1023, block);
}
}
11 changes: 11 additions & 0 deletions Assets/Custom/batch/MeshBall.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions Assets/Custom/batch/PerObjectMaterialProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using UnityEngine;

[DisallowMultipleComponent]
public class PerObjectMaterialProperties : MonoBehaviour
{
static int baseColorId = Shader.PropertyToID("_BaseColor");
[SerializeField]
Color baseColor = Color.white;

static MaterialPropertyBlock block;
void Awake()
{
OnValidate();
}
private void OnValidate()
{
if (null == block)
{
block = new MaterialPropertyBlock();
}
block.SetColor(baseColorId, baseColor);
GetComponent<Renderer>().SetPropertyBlock(block);
}
}
11 changes: 11 additions & 0 deletions Assets/Custom/batch/PerObjectMaterialProperties.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions Assets/Custom/batch/Unlit.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Unlit
m_Shader: {fileID: 4800000, guid: fcd16ff2db67f9d41bf5ead2b2fef676, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 1
m_DoubleSidedGI: 0
m_CustomRenderQueue: 3000
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BaseMap:
m_Texture: {fileID: 2800000, guid: 429938808ed85064dbbb99ebfa6d369e, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _DstBlend: 10
- _SrcBlend: 5
- _ZWrite: 0
m_Colors:
- _BaseColor: {r: 0, g: 0.735849, b: 0.56452936, a: 0.9019608}
8 changes: 8 additions & 0 deletions Assets/Custom/batch/Unlit.mat.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions Assets/Custom/batch/Unlit.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Shader "Custom RP/Unlit"
{
Properties
{
_BaseMap("Texture",2D) = "white" {}
_BaseColor("Color",Color) = (1.0,1.0,1.0,1.0)
_Cutoff("Cutoff",Range(0.0, 1.0)) = 0.2
[Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend("Src Blend", Float) = 1
[Enum(UnityEngine.Rendering.BlendMode)] _DstBlend("Dst Blend", Float) = 0
[Enum(Off, 0, On, 1)] _ZWrite("Z Write", Float) = 1
[Toggle(_CLIPPING)] _Clipping("Alpha Clipping", Float) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100

Pass
{
Blend[_SrcBlend][_DstBlend]
ZWrite [_ZWrite]
HLSLPROGRAM
#pragma multi_compile_instancing
#pragma shader_feature _CLIPPING
#pragma vertex UnlitPassVertex
#pragma fragment UnlitPassFragment
#include "UnlitPass.hlsl"
ENDHLSL
}
}
}
9 changes: 9 additions & 0 deletions Assets/Custom/batch/Unlit.shader.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions Assets/Custom/batch/Unlit2.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Unlit2
m_Shader: {fileID: 4800000, guid: fcd16ff2db67f9d41bf5ead2b2fef676, type: 3}
m_ShaderKeywords: _CLIPPING
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: 3000
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BaseMap:
m_Texture: {fileID: 2800000, guid: 429938808ed85064dbbb99ebfa6d369e, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Clipping: 1
- _Cutoff: 0.2
- _DetailNormalMapScale: 1
- _DstBlend: 10
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 5
- _UVSec: 0
- _ZWrite: 0
m_Colors:
- _BaseColor: {r: 1, g: 1, b: 1, a: 1}
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
Loading

0 comments on commit 2595489

Please sign in to comment.