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

Commit 78c034a

Browse files
committed
compute buffer setup
1 parent 7f0a2ac commit 78c034a

File tree

10 files changed

+236
-9
lines changed

10 files changed

+236
-9
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
struct vdata
2+
{
3+
float2 vertex;
4+
float2 uv;
5+
};
6+
7+
struct psInput
8+
{
9+
float4 position : SV_POSITION;
10+
};
11+
12+
StructuredBuffer<vdata> databuffer;
13+
StructuredBuffer<int> indexbuffer;
14+
float4 _viewport;
15+
int _startVertex;
16+
17+
psInput vert (uint vertex_id: SV_VertexID, uint instance_id: SV_InstanceID)
18+
{
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);
21+
return o;
22+
}
23+
24+
fixed4 frag (psInput i) : SV_Target
25+
{
26+
return float4(0, 1, 0, 1);
27+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Shader "UIWidgets/canvas_convexFill_cb"
2+
{
3+
Properties
4+
{
5+
_SrcBlend("_SrcBlend", Int) = 1 // One
6+
_DstBlend("_DstBlend", Int) = 10 // OneMinusSrcAlpha
7+
_StencilComp("_StencilComp", Float) = 3 // - Equal, 8 - Always
8+
}
9+
10+
SubShader
11+
{
12+
Blend [_SrcBlend] [_DstBlend]
13+
Pass { // 0, color
14+
CGPROGRAM
15+
#define UIWIDGETS_COLOR
16+
#include "UIWidgets_canvas_cb.cginc"
17+
#pragma vertex vert
18+
#pragma fragment frag
19+
ENDCG
20+
}
21+
22+
Pass { // 1, linear
23+
CGPROGRAM
24+
#define UIWIDGETS_LINEAR
25+
#include "UIWidgets_canvas_cb.cginc"
26+
#pragma vertex vert
27+
#pragma fragment frag
28+
ENDCG
29+
}
30+
31+
Pass { // 2, radial
32+
CGPROGRAM
33+
#define UIWIDGETS_RADIAL
34+
#include "UIWidgets_canvas_cb.cginc"
35+
#pragma vertex vert
36+
#pragma fragment frag
37+
ENDCG
38+
}
39+
40+
Pass { // 3, sweep
41+
CGPROGRAM
42+
#define UIWIDGETS_SWEEP
43+
#include "UIWidgets_canvas_cb.cginc"
44+
#pragma vertex vert
45+
#pragma fragment frag
46+
ENDCG
47+
}
48+
49+
Pass { // 4, image
50+
CGPROGRAM
51+
#define UIWIDGETS_IMAGE
52+
#include "UIWidgets_canvas_cb.cginc"
53+
#pragma vertex vert
54+
#pragma fragment frag
55+
ENDCG
56+
}
57+
}
58+
}

Runtime/Resources/UIWidgets_canvas_convexFill_cb.shader.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/editor/surface.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public RenderTexture getRenderTexture() {
189189
public Canvas getCanvas() {
190190
if (this._canvas == null) {
191191
this._canvas = new CommandBufferCanvas(
192-
this._renderTexture, this.devicePixelRatio, this._meshPool);
192+
this._renderTexture, this.devicePixelRatio, this._meshPool, true);
193193
}
194194

195195
return this._canvas;

Runtime/ui/renderer/cmdbufferCanvas/command_buffer_canvas.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace Unity.UIWidgets.ui {
44
public class CommandBufferCanvas : uiRecorderCanvas {
55
readonly PictureFlusher _flusher;
66

7-
public CommandBufferCanvas(RenderTexture renderTexture, float devicePixelRatio, MeshPool meshPool)
7+
public CommandBufferCanvas(RenderTexture renderTexture, float devicePixelRatio, MeshPool meshPool, bool isMainCanvas = false)
88
: base(new uiPictureRecorder()) {
9-
this._flusher = new PictureFlusher(renderTexture, devicePixelRatio, meshPool);
9+
this._flusher = new PictureFlusher(renderTexture, devicePixelRatio, meshPool, isMainCanvas);
1010
}
1111

1212
public override float getDevicePixelRatio() {
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System.Collections.Generic;
2+
using System.Runtime.InteropServices;
3+
using UnityEngine;
4+
using UnityEngine.Rendering;
5+
using Random = System.Random;
6+
7+
namespace Unity.UIWidgets.ui {
8+
public partial class PictureFlusher {
9+
10+
struct TVertex
11+
{
12+
public Vector2 position;
13+
public Vector2 uv;
14+
}
15+
16+
ComputeBuffer computeBuffer;
17+
List<TVertex> tvertexes;
18+
19+
ComputeBuffer indexBuffer;
20+
List<int> indexes;
21+
22+
int startVertex;
23+
int startIndex;
24+
25+
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>();
34+
35+
this.indexBuffer = new ComputeBuffer(1024 * 1024, Marshal.SizeOf(typeof(int)));
36+
this.indexes = new List<int>();
37+
}
38+
39+
this.tvertexes.Clear();
40+
this.indexes.Clear();
41+
this.startVertex = 0;
42+
this.startIndex = 0;
43+
44+
if (this.material == null) {
45+
this.material = new Material(Shader.Find("UIWidgets/canvas_convexFill_cb"));
46+
this.material.SetVector("_viewport", new Vector4(0, 0, 500, 500));
47+
}
48+
49+
var random = new Random();
50+
var num = 5;
51+
var size = 30;
52+
53+
for (var i = 0; i < num; i++)
54+
{
55+
for (var j = 0; j < num; j++)
56+
{
57+
var offsetY = i * size;
58+
var offsetX = j * size;
59+
var centerX = offsetX + size / 2;
60+
var centerY = offsetY + size / 2;
61+
var width = size;
62+
var height = size;
63+
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+
});
96+
97+
var mpb = new MaterialPropertyBlock();
98+
mpb.SetBuffer("databuffer", this.computeBuffer);
99+
mpb.SetBuffer("indexbuffer", this.indexBuffer);
100+
mpb.SetInt("_startVertex", this.startIndex);
101+
cmdBuf.DrawProcedural(Matrix4x4.identity, this.material, 0, MeshTopology.Triangles, 6, 1, mpb);
102+
}
103+
}
104+
105+
this.computeBuffer.SetData(this.tvertexes);
106+
this.indexBuffer.SetData(this.indexes);
107+
}
108+
}
109+
}

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

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

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public partial class PictureFlusher {
1111
readonly float _devicePixelRatio;
1212
readonly MeshPool _meshPool;
1313

14+
readonly bool _isMainCanvas;
15+
1416
readonly List<RenderLayer> _layers = new List<RenderLayer>();
1517
RenderLayer _currentLayer;
1618
uiRect? _lastScissor;
@@ -44,7 +46,7 @@ public void dispose() {
4446
}
4547
}
4648

47-
public PictureFlusher(RenderTexture renderTexture, float devicePixelRatio, MeshPool meshPool) {
49+
public PictureFlusher(RenderTexture renderTexture, float devicePixelRatio, MeshPool meshPool, bool isMainCanvas) {
4850
D.assert(renderTexture);
4951
D.assert(devicePixelRatio > 0);
5052
D.assert(meshPool != null);
@@ -53,6 +55,7 @@ public PictureFlusher(RenderTexture renderTexture, float devicePixelRatio, MeshP
5355
this._fringeWidth = 1.0f / devicePixelRatio;
5456
this._devicePixelRatio = devicePixelRatio;
5557
this._meshPool = meshPool;
58+
this._isMainCanvas = isMainCanvas;
5659

5760
this.___drawTextDrawMeshCallback = this._drawTextDrawMeshCallback;
5861
this.___drawPathDrawMeshCallback2 = this._drawPathDrawMeshCallback2;
@@ -1042,8 +1045,11 @@ public void flush(uiPicture picture) {
10421045
using (var cmdBuf = new CommandBuffer()) {
10431046
cmdBuf.name = "CommandBufferCanvas";
10441047

1045-
this._lastRtID = -1;
1046-
this._drawLayer(layer, cmdBuf);
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);
10471053

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

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ static class CanvasShader {
155155
static readonly MaterialByBlendModeStencilComp _strokeAlphaMat;
156156
static readonly Material _shadowBox;
157157
static readonly Material _shadowRBox;
158+
159+
static readonly MaterialByBlendModeStencilComp _convexFillMat_cb;
158160

159161
static Shader GetShader(string shaderName) {
160162
var shader = Shader.Find(shaderName);
@@ -165,6 +167,8 @@ static Shader GetShader(string shaderName) {
165167
return shader;
166168
}
167169

170+
public static readonly bool supportComputeBuffer;
171+
168172
static CanvasShader() {
169173
var convexFillShader = GetShader("UIWidgets/canvas_convexFill");
170174
var fill0Shader = GetShader("UIWidgets/canvas_fill0");
@@ -177,6 +181,7 @@ static CanvasShader() {
177181
var shadowBoxShader = GetShader("UIWidgets/ShadowBox");
178182
var shadowRBoxShader = GetShader("UIWidgets/ShadowRBox");
179183
var strokeAlphaShader = GetShader("UIWidgets/canvas_strokeAlpha");
184+
var convexFillShaderCompute = GetShader("UIWidgets/canvas_convexFill_cb");
180185

181186
_convexFillMat = new MaterialByBlendModeStencilComp(convexFillShader);
182187
_fill0Mat = new MaterialByStencilComp(fill0Shader);
@@ -189,9 +194,11 @@ static CanvasShader() {
189194
_filterMat = new Material(filterShader) {hideFlags = HideFlags.HideAndDontSave};
190195
_shadowBox = new Material(shadowBoxShader) {hideFlags = HideFlags.HideAndDontSave};
191196
_shadowRBox = new Material(shadowRBoxShader) {hideFlags = HideFlags.HideAndDontSave};
192-
}
197+
198+
_convexFillMat_cb = new MaterialByBlendModeStencilComp(convexFillShaderCompute);
193199

194-
public static Material shadowBox => _shadowBox;
200+
supportComputeBuffer = convexFillShaderCompute.isSupported;
201+
}
195202

196203
static readonly int _viewportId = Shader.PropertyToID("_viewport");
197204
static readonly int _alphaId = Shader.PropertyToID("_alpha");

Runtime/ui/renderer/compositeCanvas/flow/raster_cache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ RasterCacheResult _rasterizePicture(Picture picture, Matrix3 transform, float de
242242
var renderTexture = new RenderTexture(desc);
243243
renderTexture.hideFlags = HideFlags.HideAndDontSave;
244244

245-
var canvas = new CommandBufferCanvas(renderTexture, devicePixelRatio, meshPool);
245+
var canvas = new CommandBufferCanvas(renderTexture, devicePixelRatio, meshPool, false);
246246
canvas.translate(-bounds.left, -bounds.top);
247247
canvas.concat(transform);
248248
canvas.drawPicture(picture);

0 commit comments

Comments
 (0)