浏览代码

compute buffer setup - convexfill

/main
xingwei.zhu 5 年前
当前提交
9b220fdf
共有 6 个文件被更改,包括 135 次插入66 次删除
  1. 21
      Runtime/Resources/UIWidgets_canvas_cb.cginc
  2. 8
      Runtime/Resources/UIWidgets_canvas_convexFill_cb.shader
  3. 114
      Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_computebuffer_utils.cs
  4. 40
      Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_impl.cs
  5. 9
      Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_shader.cs
  6. 9
      Runtime/Resources/UIWidgets_canvas_cb.cginc.meta

21
Runtime/Resources/UIWidgets_canvas_cb.cginc


half4 _color;
struct vdata
{
float2 vertex;

struct psInput
struct v2f
float4 position : SV_POSITION;
float4 vertex : SV_POSITION;
float2 ftcoord : TEXCOORD0;
float2 fpos : TEXCOORD1;
};
StructuredBuffer<vdata> databuffer;

psInput vert (uint vertex_id: SV_VertexID, uint instance_id: SV_InstanceID)
v2f vert (uint vertex_id: SV_VertexID, uint instance_id: SV_InstanceID)
psInput o = (psInput)0;
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);
v2f o = (v2f)0;
vdata v = databuffer[indexbuffer[_startVertex + vertex_id]];
o.vertex = float4(v.vertex.x * 2.0 / _viewport.z - 1.0, v.vertex.y * 2.0 / _viewport.w - 1.0, 0, 1);
o.ftcoord = v.uv;
o.fpos = v.vertex;
fixed4 frag (psInput i) : SV_Target
fixed4 frag (v2f i) : SV_Target
return float4(0, 1, 0, 1);
return _color;
}

8
Runtime/Resources/UIWidgets_canvas_convexFill_cb.shader


SubShader
{
ZTest Always
ZWrite Off
Stencil {
Ref 128
Comp [_StencilComp]
}
Pass { // 0, color
CGPROGRAM
#define UIWIDGETS_COLOR

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


int startIndex;
Material material;
public void DrawBuffer(CommandBuffer cmdBuf)
{
if (this.computeBuffer == null)
{
var stride = Marshal.SizeOf(typeof(TVertex));
this.computeBuffer = new ComputeBuffer(1024 * 1024, stride);
this.tvertexes = new List<TVertex>();
bool supportComputeBuffer;
void setupComputeBuffer() {
this.supportComputeBuffer = this._isMainCanvas && CanvasShader.supportComputeBuffer;
}
void initComputeBuffer() {
var stride = Marshal.SizeOf(typeof(TVertex));
this.computeBuffer = new ComputeBuffer(1024 * 1024, stride);
this.tvertexes = new List<TVertex>();
this.indexBuffer = new ComputeBuffer(1024 * 1024, Marshal.SizeOf(typeof(int)));
this.indexes = new List<int>();
this.indexBuffer = new ComputeBuffer(1024 * 1024, Marshal.SizeOf(typeof(int)));
this.indexes = new List<int>();
}
void resetComputeBuffer() {
if (!this.supportComputeBuffer) return;
if (this.computeBuffer == null) {
this.initComputeBuffer();
}
this.tvertexes.Clear();

}
void bindComputeBuffer() {
if (!this.supportComputeBuffer) return;
this.computeBuffer.SetData(this.tvertexes);
this.indexBuffer.SetData(this.indexes);
}
void addMeshToComputeBuffer(List<Vector3> vertex, List<Vector2> uv, List<int> triangles) {
if (!this.supportComputeBuffer) return;
this.startVertex = this.tvertexes.Count;
this.startIndex = this.indexes.Count;
var hasUv = uv != null;
for (int i = 0; i < vertex.Count; i++) {
this.tvertexes.Add(new TVertex {
position = new Vector2(vertex[i].x, vertex[i].y),
uv = hasUv ? uv[i] : Vector2.zero
});
}
foreach (var triangleId in triangles) {
this.indexes.Add(triangleId + this.startVertex);
}
}
public void DrawBuffer(CommandBuffer cmdBuf)
{
if (this.computeBuffer == null)
{
this.initComputeBuffer();
}
this.resetComputeBuffer();
if (this.material == null) {
this.material = new Material(Shader.Find("UIWidgets/canvas_convexFill_cb"));

var width = size;
var height = size;
this.startVertex = this.tvertexes.Count;
this.startIndex = this.indexes.Count;
this.tvertexes.AddRange(new[]
{
new TVertex
{
position = new Vector2(centerX - width / 2, centerY - height / 2),
uv = new Vector2(0, 0)
},
new TVertex
{
position = new Vector2(centerX + width / 2, centerY - height / 2),
uv = new Vector2(0, 0)
},
new TVertex
{
position = new Vector2(centerX + width / 2, centerY + height / 2),
uv = new Vector2(0, 0)
},
new TVertex
{
position = new Vector2(centerX - width / 2, centerY + height / 2),
uv = new Vector2(0, 0)
}
});
this.indexes.AddRange(new []
{
this.startVertex, this.startVertex + 1, this.startVertex + 2, this.startVertex, this.startVertex + 2, this.startVertex + 3
});
var vert = new List<Vector3> {
new Vector3(centerX - width / 2, centerY - height / 2),
new Vector3(centerX + width / 2, centerY - height / 2),
new Vector3(centerX + width / 2, centerY + height / 2),
new Vector3(centerX - width / 2, centerY + height / 2)
};
var index = new List<int> {
0, 1, 2, 0, 2, 3
};
this.addMeshToComputeBuffer(vert, null, index);
var mpb = new MaterialPropertyBlock();
mpb.SetBuffer("databuffer", this.computeBuffer);

}
}
this.computeBuffer.SetData(this.tvertexes);
this.indexBuffer.SetData(this.indexes);
this.bindComputeBuffer();
}
}
}

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


this.___drawTextDrawMeshCallback = this._drawTextDrawMeshCallback;
this.___drawPathDrawMeshCallback2 = this._drawPathDrawMeshCallback2;
this.___drawPathDrawMeshCallback = this._drawPathDrawMeshCallback;
this.setupComputeBuffer();
}
readonly _drawPathDrawMeshCallbackDelegate ___drawTextDrawMeshCallback;

var layer = this._currentLayer;
if (convex) {
layer.draws.Add(CanvasShader.convexFill(layer, p, fillMesh));
layer.draws.Add(CanvasShader.convexFill(layer, p, fillMesh, this.supportComputeBuffer));
}
else {
layer.draws.Add(CanvasShader.fill0(layer, fillMesh));

public void flush(uiPicture picture) {
this._reset();
this._resetRenderTextureId();
this.resetComputeBuffer();
this._drawUIPicture(picture, false);

using (var cmdBuf = new CommandBuffer()) {
cmdBuf.name = "CommandBufferCanvas";
//this._lastRtID = -1;
//this._drawLayer(layer, cmdBuf);
cmdBuf.SetRenderTarget(this._renderTexture);
cmdBuf.ClearRenderTarget(true, true, UnityEngine.Color.grey);
this.DrawBuffer(cmdBuf);
this._lastRtID = -1;
this._drawLayer(layer, cmdBuf);
//cmdBuf.SetRenderTarget(this._renderTexture);
//cmdBuf.ClearRenderTarget(true, true, UnityEngine.Color.grey);
//this.DrawBuffer(cmdBuf);
this.bindComputeBuffer();
Graphics.ExecuteCommandBuffer(cmdBuf);
}

if (mesh == null) {
continue;
}
D.assert(mesh.vertices.Count > 0);
cmd.meshObj.SetVertices(mesh.vertices?.data);
cmd.meshObj.SetTriangles(mesh.triangles?.data, 0, false);
cmd.meshObj.SetUVs(0, mesh.uv?.data);
if (mesh.matrix == null) {
cmd.properties.SetFloatArray(CmdDraw.matId, CmdDraw.idMat3.fMat);
}

cmd.properties.SetFloatArray(CmdDraw.matId, this._drawLayer_matArray);
}
cmdBuf.DrawMesh(cmd.meshObj, CmdDraw.idMat, cmd.material, 0, cmd.pass, cmd.properties.mpb);
D.assert(mesh.vertices.Count > 0);
if (this.supportComputeBuffer && CanvasShader.computeShader == cmd.material.shader) {
this.addMeshToComputeBuffer(mesh.vertices?.data, mesh.uv?.data, mesh.triangles?.data);
cmd.properties.mpb.SetBuffer("databuffer", this.computeBuffer);
cmd.properties.mpb.SetBuffer("indexbuffer", this.indexBuffer);
cmd.properties.mpb.SetInt("_startVertex", this.startIndex);
cmdBuf.DrawProcedural(Matrix4x4.identity, cmd.material, 0, MeshTopology.Triangles, mesh.triangles.Count, 1, cmd.properties.mpb);
}
else {
cmd.meshObj.SetVertices(mesh.vertices?.data);
cmd.meshObj.SetTriangles(mesh.triangles?.data, 0, false);
cmd.meshObj.SetUVs(0, mesh.uv?.data);
cmdBuf.DrawMesh(cmd.meshObj, CmdDraw.idMat, cmd.material, 0, cmd.pass, cmd.properties.mpb);
}
if (cmd.layerId != null) {
cmdBuf.SetGlobalTexture(CmdDraw.texId, BuiltinRenderTextureType.None);
}

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


public static readonly bool supportComputeBuffer;
public static Shader computeShader;
static CanvasShader() {
var convexFillShader = GetShader("UIWidgets/canvas_convexFill");
var fill0Shader = GetShader("UIWidgets/canvas_fill0");

var shadowRBoxShader = GetShader("UIWidgets/ShadowRBox");
var strokeAlphaShader = GetShader("UIWidgets/canvas_strokeAlpha");
var convexFillShaderCompute = GetShader("UIWidgets/canvas_convexFill_cb");
computeShader = convexFillShaderCompute;
_convexFillMat = new MaterialByBlendModeStencilComp(convexFillShader);
_fill0Mat = new MaterialByStencilComp(fill0Shader);

}
public static PictureFlusher.CmdDraw convexFill(PictureFlusher.RenderLayer layer, uiPaint paint,
uiMeshMesh mesh) {
var mat = _convexFillMat.getMaterial(paint.blendMode, layer.ignoreClip);
uiMeshMesh mesh, bool supportComputeBuffer = false) {
var mat = supportComputeBuffer ? _convexFillMat_cb.getMaterial(paint.blendMode, layer.ignoreClip) : _convexFillMat.getMaterial(paint.blendMode, layer.ignoreClip);
_getShaderPassAndProps(layer, paint, mesh.matrix, 1.0f, 0.0f, out var pass, out var props);
return PictureFlusher.CmdDraw.create(

9
Runtime/Resources/UIWidgets_canvas_cb.cginc.meta


fileFormatVersion: 2
guid: 1f1fccf97e9184468a5eba95bd74f2e5
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存