Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 3d70b48

Browse files
committed
compute buffer setup - convexfill
1 parent 78c034a commit 3d70b48

File tree

6 files changed

+135
-66
lines changed

6 files changed

+135
-66
lines changed
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
1+
half4 _color;
2+
13
struct vdata
24
{
35
float2 vertex;
46
float2 uv;
57
};
68

7-
struct psInput
9+
struct v2f
810
{
9-
float4 position : SV_POSITION;
11+
float4 vertex : SV_POSITION;
12+
float2 ftcoord : TEXCOORD0;
13+
float2 fpos : TEXCOORD1;
1014
};
1115

1216
StructuredBuffer<vdata> databuffer;
1317
StructuredBuffer<int> indexbuffer;
1418
float4 _viewport;
1519
int _startVertex;
1620

17-
psInput vert (uint vertex_id: SV_VertexID, uint instance_id: SV_InstanceID)
21+
v2f vert (uint vertex_id: SV_VertexID, uint instance_id: SV_InstanceID)
1822
{
19-
psInput o = (psInput)0;
20-
o.position = float4(databuffer[indexbuffer[_startVertex + vertex_id]].vertex.x * 2.0 / _viewport.z - 1.0, databuffer[indexbuffer[_startVertex + vertex_id]].vertex.y * 2.0 / _viewport.w - 1.0, 0, 1);
23+
v2f o = (v2f)0;
24+
vdata v = databuffer[indexbuffer[_startVertex + vertex_id]];
25+
o.vertex = float4(v.vertex.x * 2.0 / _viewport.z - 1.0, v.vertex.y * 2.0 / _viewport.w - 1.0, 0, 1);
26+
o.ftcoord = v.uv;
27+
o.fpos = v.vertex;
2128
return o;
2229
}
2330

24-
fixed4 frag (psInput i) : SV_Target
31+
fixed4 frag (v2f i) : SV_Target
2532
{
26-
return float4(0, 1, 0, 1);
33+
return _color;
2734
}

Runtime/Resources/UIWidgets_canvas_cb.cginc.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Resources/UIWidgets_canvas_convexFill_cb.shader

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ Shader "UIWidgets/canvas_convexFill_cb"
99

1010
SubShader
1111
{
12+
ZTest Always
13+
ZWrite Off
1214
Blend [_SrcBlend] [_DstBlend]
15+
16+
Stencil {
17+
Ref 128
18+
Comp [_StencilComp]
19+
}
20+
1321
Pass { // 0, color
1422
CGPROGRAM
1523
#define UIWIDGETS_COLOR

Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_computebuffer_utils.cs

Lines changed: 70 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,70 @@ struct TVertex
2323
int startIndex;
2424

2525
Material material;
26-
27-
public void DrawBuffer(CommandBuffer cmdBuf)
28-
{
29-
if (this.computeBuffer == null)
30-
{
31-
var stride = Marshal.SizeOf(typeof(TVertex));
32-
this.computeBuffer = new ComputeBuffer(1024 * 1024, stride);
33-
this.tvertexes = new List<TVertex>();
26+
27+
bool supportComputeBuffer;
28+
29+
void setupComputeBuffer() {
30+
this.supportComputeBuffer = this._isMainCanvas && CanvasShader.supportComputeBuffer;
31+
}
32+
33+
void initComputeBuffer() {
34+
var stride = Marshal.SizeOf(typeof(TVertex));
35+
this.computeBuffer = new ComputeBuffer(1024 * 1024, stride);
36+
this.tvertexes = new List<TVertex>();
3437

35-
this.indexBuffer = new ComputeBuffer(1024 * 1024, Marshal.SizeOf(typeof(int)));
36-
this.indexes = new List<int>();
38+
this.indexBuffer = new ComputeBuffer(1024 * 1024, Marshal.SizeOf(typeof(int)));
39+
this.indexes = new List<int>();
40+
}
41+
42+
void resetComputeBuffer() {
43+
if (!this.supportComputeBuffer) return;
44+
45+
if (this.computeBuffer == null) {
46+
this.initComputeBuffer();
3747
}
3848

3949
this.tvertexes.Clear();
4050
this.indexes.Clear();
4151
this.startVertex = 0;
4252
this.startIndex = 0;
53+
}
54+
55+
void bindComputeBuffer() {
56+
if (!this.supportComputeBuffer) return;
57+
58+
this.computeBuffer.SetData(this.tvertexes);
59+
this.indexBuffer.SetData(this.indexes);
60+
}
61+
62+
void addMeshToComputeBuffer(List<Vector3> vertex, List<Vector2> uv, List<int> triangles) {
63+
if (!this.supportComputeBuffer) return;
64+
65+
this.startVertex = this.tvertexes.Count;
66+
this.startIndex = this.indexes.Count;
67+
68+
var hasUv = uv != null;
69+
70+
for (int i = 0; i < vertex.Count; i++) {
71+
this.tvertexes.Add(new TVertex {
72+
position = new Vector2(vertex[i].x, vertex[i].y),
73+
uv = hasUv ? uv[i] : Vector2.zero
74+
});
75+
}
76+
77+
foreach (var triangleId in triangles) {
78+
this.indexes.Add(triangleId + this.startVertex);
79+
}
80+
}
81+
82+
public void DrawBuffer(CommandBuffer cmdBuf)
83+
{
84+
if (this.computeBuffer == null)
85+
{
86+
this.initComputeBuffer();
87+
}
88+
89+
this.resetComputeBuffer();
4390

4491
if (this.material == null) {
4592
this.material = new Material(Shader.Find("UIWidgets/canvas_convexFill_cb"));
@@ -61,38 +108,18 @@ public void DrawBuffer(CommandBuffer cmdBuf)
61108
var width = size;
62109
var height = size;
63110

64-
this.startVertex = this.tvertexes.Count;
65-
this.startIndex = this.indexes.Count;
66-
67-
68-
this.tvertexes.AddRange(new[]
69-
{
70-
new TVertex
71-
{
72-
position = new Vector2(centerX - width / 2, centerY - height / 2),
73-
uv = new Vector2(0, 0)
74-
},
75-
new TVertex
76-
{
77-
position = new Vector2(centerX + width / 2, centerY - height / 2),
78-
uv = new Vector2(0, 0)
79-
},
80-
new TVertex
81-
{
82-
position = new Vector2(centerX + width / 2, centerY + height / 2),
83-
uv = new Vector2(0, 0)
84-
},
85-
new TVertex
86-
{
87-
position = new Vector2(centerX - width / 2, centerY + height / 2),
88-
uv = new Vector2(0, 0)
89-
}
90-
});
91-
92-
this.indexes.AddRange(new []
93-
{
94-
this.startVertex, this.startVertex + 1, this.startVertex + 2, this.startVertex, this.startVertex + 2, this.startVertex + 3
95-
});
111+
var vert = new List<Vector3> {
112+
new Vector3(centerX - width / 2, centerY - height / 2),
113+
new Vector3(centerX + width / 2, centerY - height / 2),
114+
new Vector3(centerX + width / 2, centerY + height / 2),
115+
new Vector3(centerX - width / 2, centerY + height / 2)
116+
};
117+
118+
var index = new List<int> {
119+
0, 1, 2, 0, 2, 3
120+
};
121+
122+
this.addMeshToComputeBuffer(vert, null, index);
96123

97124
var mpb = new MaterialPropertyBlock();
98125
mpb.SetBuffer("databuffer", this.computeBuffer);
@@ -102,8 +129,7 @@ public void DrawBuffer(CommandBuffer cmdBuf)
102129
}
103130
}
104131

105-
this.computeBuffer.SetData(this.tvertexes);
106-
this.indexBuffer.SetData(this.indexes);
132+
this.bindComputeBuffer();
107133
}
108134
}
109135
}

Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_impl.cs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public PictureFlusher(RenderTexture renderTexture, float devicePixelRatio, MeshP
6060
this.___drawTextDrawMeshCallback = this._drawTextDrawMeshCallback;
6161
this.___drawPathDrawMeshCallback2 = this._drawPathDrawMeshCallback2;
6262
this.___drawPathDrawMeshCallback = this._drawPathDrawMeshCallback;
63+
64+
this.setupComputeBuffer();
6365
}
6466

6567
readonly _drawPathDrawMeshCallbackDelegate ___drawTextDrawMeshCallback;
@@ -550,7 +552,7 @@ void _drawPathDrawMeshCallback(uiPaint p, uiMeshMesh fillMesh, uiMeshMesh stroke
550552

551553
var layer = this._currentLayer;
552554
if (convex) {
553-
layer.draws.Add(CanvasShader.convexFill(layer, p, fillMesh));
555+
layer.draws.Add(CanvasShader.convexFill(layer, p, fillMesh, this.supportComputeBuffer));
554556
}
555557
else {
556558
layer.draws.Add(CanvasShader.fill0(layer, fillMesh));
@@ -1035,6 +1037,7 @@ void _drawTextBlob(TextBlob? textBlob, uiOffset offset, uiPaint paint) {
10351037
public void flush(uiPicture picture) {
10361038
this._reset();
10371039
this._resetRenderTextureId();
1040+
this.resetComputeBuffer();
10381041

10391042
this._drawUIPicture(picture, false);
10401043

@@ -1045,15 +1048,16 @@ public void flush(uiPicture picture) {
10451048
using (var cmdBuf = new CommandBuffer()) {
10461049
cmdBuf.name = "CommandBufferCanvas";
10471050

1048-
//this._lastRtID = -1;
1049-
//this._drawLayer(layer, cmdBuf);
1050-
cmdBuf.SetRenderTarget(this._renderTexture);
1051-
cmdBuf.ClearRenderTarget(true, true, UnityEngine.Color.grey);
1052-
this.DrawBuffer(cmdBuf);
1051+
this._lastRtID = -1;
1052+
this._drawLayer(layer, cmdBuf);
1053+
//cmdBuf.SetRenderTarget(this._renderTexture);
1054+
//cmdBuf.ClearRenderTarget(true, true, UnityEngine.Color.grey);
1055+
//this.DrawBuffer(cmdBuf);
10531056

10541057
// this is necessary for webgl2. not sure why... just to be safe to disable the scissor.
10551058
cmdBuf.DisableScissorRect();
10561059

1060+
this.bindComputeBuffer();
10571061
Graphics.ExecuteCommandBuffer(cmdBuf);
10581062
}
10591063

@@ -1142,12 +1146,7 @@ void _drawLayer(RenderLayer layer, CommandBuffer cmdBuf) {
11421146
if (mesh == null) {
11431147
continue;
11441148
}
1145-
1146-
D.assert(mesh.vertices.Count > 0);
1147-
cmd.meshObj.SetVertices(mesh.vertices?.data);
1148-
cmd.meshObj.SetTriangles(mesh.triangles?.data, 0, false);
1149-
cmd.meshObj.SetUVs(0, mesh.uv?.data);
1150-
1149+
11511150
if (mesh.matrix == null) {
11521151
cmd.properties.SetFloatArray(CmdDraw.matId, CmdDraw.idMat3.fMat);
11531152
}
@@ -1166,7 +1165,22 @@ void _drawLayer(RenderLayer layer, CommandBuffer cmdBuf) {
11661165
cmd.properties.SetFloatArray(CmdDraw.matId, this._drawLayer_matArray);
11671166
}
11681167

1169-
cmdBuf.DrawMesh(cmd.meshObj, CmdDraw.idMat, cmd.material, 0, cmd.pass, cmd.properties.mpb);
1168+
D.assert(mesh.vertices.Count > 0);
1169+
if (this.supportComputeBuffer && CanvasShader.computeShader == cmd.material.shader) {
1170+
this.addMeshToComputeBuffer(mesh.vertices?.data, mesh.uv?.data, mesh.triangles?.data);
1171+
cmd.properties.mpb.SetBuffer("databuffer", this.computeBuffer);
1172+
cmd.properties.mpb.SetBuffer("indexbuffer", this.indexBuffer);
1173+
cmd.properties.mpb.SetInt("_startVertex", this.startIndex);
1174+
cmdBuf.DrawProcedural(Matrix4x4.identity, cmd.material, 0, MeshTopology.Triangles, mesh.triangles.Count, 1, cmd.properties.mpb);
1175+
}
1176+
else {
1177+
cmd.meshObj.SetVertices(mesh.vertices?.data);
1178+
cmd.meshObj.SetTriangles(mesh.triangles?.data, 0, false);
1179+
cmd.meshObj.SetUVs(0, mesh.uv?.data);
1180+
1181+
cmdBuf.DrawMesh(cmd.meshObj, CmdDraw.idMat, cmd.material, 0, cmd.pass, cmd.properties.mpb);
1182+
}
1183+
11701184
if (cmd.layerId != null) {
11711185
cmdBuf.SetGlobalTexture(CmdDraw.texId, BuiltinRenderTextureType.None);
11721186
}

Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_shader.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ static Shader GetShader(string shaderName) {
169169

170170
public static readonly bool supportComputeBuffer;
171171

172+
public static Shader computeShader;
173+
172174
static CanvasShader() {
173175
var convexFillShader = GetShader("UIWidgets/canvas_convexFill");
174176
var fill0Shader = GetShader("UIWidgets/canvas_fill0");
@@ -183,6 +185,8 @@ static CanvasShader() {
183185
var strokeAlphaShader = GetShader("UIWidgets/canvas_strokeAlpha");
184186
var convexFillShaderCompute = GetShader("UIWidgets/canvas_convexFill_cb");
185187

188+
computeShader = convexFillShaderCompute;
189+
186190
_convexFillMat = new MaterialByBlendModeStencilComp(convexFillShader);
187191
_fill0Mat = new MaterialByStencilComp(fill0Shader);
188192
_fill1Mat = new MaterialByBlendMode(fill1Shader);
@@ -310,8 +314,9 @@ static void _getShaderPassAndProps(
310314
}
311315

312316
public static PictureFlusher.CmdDraw convexFill(PictureFlusher.RenderLayer layer, uiPaint paint,
313-
uiMeshMesh mesh) {
314-
var mat = _convexFillMat.getMaterial(paint.blendMode, layer.ignoreClip);
317+
uiMeshMesh mesh, bool supportComputeBuffer = false) {
318+
var mat = supportComputeBuffer ? _convexFillMat_cb.getMaterial(paint.blendMode, layer.ignoreClip) : _convexFillMat.getMaterial(paint.blendMode, layer.ignoreClip);
319+
315320
_getShaderPassAndProps(layer, paint, mesh.matrix, 1.0f, 0.0f, out var pass, out var props);
316321

317322
return PictureFlusher.CmdDraw.create(

0 commit comments

Comments
 (0)