summaryrefslogtreecommitdiff
path: root/UnityEngine.PostProcessing/GraphicsUtils.cs
blob: 9062c77da6b07ff5aa0bcabb5458bbcb1c09a137 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
namespace UnityEngine.PostProcessing;

public static class GraphicsUtils
{
	private static Texture2D s_WhiteTexture;

	private static Mesh s_Quad;

	public static bool isLinearColorSpace => QualitySettings.activeColorSpace == ColorSpace.Linear;

	public static bool supportsDX11 => SystemInfo.graphicsShaderLevel >= 50 && SystemInfo.supportsComputeShaders;

	public static Texture2D whiteTexture
	{
		get
		{
			if (s_WhiteTexture != null)
			{
				return s_WhiteTexture;
			}
			s_WhiteTexture = new Texture2D(1, 1, TextureFormat.ARGB32, mipmap: false);
			s_WhiteTexture.SetPixel(0, 0, new Color(1f, 1f, 1f, 1f));
			s_WhiteTexture.Apply();
			return s_WhiteTexture;
		}
	}

	public static Mesh quad
	{
		get
		{
			if (s_Quad != null)
			{
				return s_Quad;
			}
			Vector3[] vertices = new Vector3[4]
			{
				new Vector3(-1f, -1f, 0f),
				new Vector3(1f, 1f, 0f),
				new Vector3(1f, -1f, 0f),
				new Vector3(-1f, 1f, 0f)
			};
			Vector2[] uv = new Vector2[4]
			{
				new Vector2(0f, 0f),
				new Vector2(1f, 1f),
				new Vector2(1f, 0f),
				new Vector2(0f, 1f)
			};
			int[] triangles = new int[6] { 0, 1, 2, 1, 0, 3 };
			Mesh mesh = new Mesh();
			mesh.vertices = vertices;
			mesh.uv = uv;
			mesh.triangles = triangles;
			s_Quad = mesh;
			s_Quad.RecalculateNormals();
			s_Quad.RecalculateBounds();
			return s_Quad;
		}
	}

	public static void Blit(Material material, int pass)
	{
		GL.PushMatrix();
		GL.LoadOrtho();
		material.SetPass(pass);
		GL.Begin(5);
		GL.TexCoord2(0f, 0f);
		GL.Vertex3(0f, 0f, 0.1f);
		GL.TexCoord2(1f, 0f);
		GL.Vertex3(1f, 0f, 0.1f);
		GL.TexCoord2(0f, 1f);
		GL.Vertex3(0f, 1f, 0.1f);
		GL.TexCoord2(1f, 1f);
		GL.Vertex3(1f, 1f, 0.1f);
		GL.End();
		GL.PopMatrix();
	}

	public static void ClearAndBlit(Texture source, RenderTexture destination, Material material, int pass, bool clearColor = true, bool clearDepth = false)
	{
		RenderTexture active = RenderTexture.active;
		RenderTexture.active = destination;
		GL.Clear(clearDepth: false, clearColor, Color.clear);
		GL.PushMatrix();
		GL.LoadOrtho();
		material.SetTexture("_MainTex", source);
		material.SetPass(pass);
		GL.Begin(5);
		GL.TexCoord2(0f, 0f);
		GL.Vertex3(0f, 0f, 0.1f);
		GL.TexCoord2(1f, 0f);
		GL.Vertex3(1f, 0f, 0.1f);
		GL.TexCoord2(0f, 1f);
		GL.Vertex3(0f, 1f, 0.1f);
		GL.TexCoord2(1f, 1f);
		GL.Vertex3(1f, 1f, 0.1f);
		GL.End();
		GL.PopMatrix();
		RenderTexture.active = active;
	}

	public static void Destroy(Object obj)
	{
		if (obj != null)
		{
			Object.Destroy(obj);
		}
	}

	public static void Dispose()
	{
		Destroy(s_Quad);
	}
}