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

Commit fed0017

Browse files
author
Yuncong Zhang
authored
Merge pull request #324 from UnityTech/dev
Dev
2 parents c5bdf08 + 719f8c7 commit fed0017

File tree

7 files changed

+82
-149
lines changed

7 files changed

+82
-149
lines changed

Runtime/engine/UIWidgetsPanel.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -312,31 +312,29 @@ public void OnPointerUp(PointerEventData eventData) {
312312
));
313313
}
314314

315-
public Vector2? getPointPosition(PointerEventData eventData) {
316-
if (eventData.enterEventCamera == null && this.canvas.renderMode == RenderMode.ScreenSpaceCamera) {
317-
return null;
315+
Camera getActiveCamera() {
316+
//refer to: https://zhuanlan.zhihu.com/p/37127981
317+
Camera eventCamera = null;
318+
if (this.canvas.renderMode != RenderMode.ScreenSpaceOverlay) {
319+
eventCamera = this.canvas.GetComponent<GraphicRaycaster>().eventCamera;
318320
}
319-
321+
return eventCamera;
322+
}
323+
324+
Vector2? getPointPosition(PointerEventData eventData) {
325+
Camera camera = this.getActiveCamera();
320326
Vector2 localPoint;
321327
RectTransformUtility.ScreenPointToLocalPointInRectangle(this.rectTransform, eventData.position,
322-
eventData.enterEventCamera, out localPoint);
328+
camera, out localPoint);
323329
var scaleFactor = this.canvas.scaleFactor;
324330
localPoint.x = (localPoint.x - this.rectTransform.rect.min.x) * scaleFactor;
325331
localPoint.y = (this.rectTransform.rect.max.y - localPoint.y) * scaleFactor;
326332
return localPoint;
327333
}
328334

329-
public Vector2? getPointPosition(Vector2 position) {
335+
Vector2? getPointPosition(Vector2 position) {
330336
Vector2 localPoint;
331-
Camera eventCamera = null;
332-
333-
if (this.canvas.renderMode != RenderMode.ScreenSpaceCamera) {
334-
eventCamera = this.canvas.GetComponent<GraphicRaycaster>().eventCamera;
335-
}
336-
337-
if (eventCamera == null && this.canvas.renderMode == RenderMode.ScreenSpaceCamera) {
338-
return null;
339-
}
337+
Camera eventCamera = this.getActiveCamera();
340338

341339
RectTransformUtility.ScreenPointToLocalPointInRectangle(this.rectTransform, position,
342340
eventCamera, out localPoint);

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

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,87 +11,85 @@ struct uiVertex
1111
public Vector2 uv;
1212
}
1313

14-
static ComputeBuffer _computeBuffer;
15-
static List<uiVertex> _vertices;
14+
ComputeBuffer _computeBuffer;
15+
List<uiVertex> _vertices;
1616

17-
static ComputeBuffer _indexBuffer;
18-
static List<int> _indices;
17+
ComputeBuffer _indexBuffer;
18+
List<int> _indices;
1919

20-
static int _startVertex;
21-
static int _startIndex;
22-
23-
static int _instanceNum;
20+
int _startVertex;
21+
int _startIndex;
2422

2523
public const int COMPUTE_BUFFER_MAX_ITEM_NUM = 1024 * 1024; // maxsize = 1M vertex/index
2624

27-
static void _releaseComputeBuffer() {
28-
if (!CanvasShader.supportComputeBuffer) {
29-
return;
30-
}
25+
bool supportComputeBuffer {
26+
get { return CanvasShader.supportComputeBuffer; }
27+
}
3128

32-
if (_computeBuffer == null) {
29+
void _releaseComputeBuffer() {
30+
if (!this.supportComputeBuffer) {
3331
return;
3432
}
3533

36-
if (_instanceNum != 0) {
34+
if (this._computeBuffer == null) {
3735
return;
3836
}
3937

40-
_computeBuffer.Dispose();
41-
_indexBuffer.Dispose();
42-
_vertices = null;
43-
_indices = null;
44-
_computeBuffer = null;
45-
_indexBuffer = null;
38+
this._computeBuffer.Dispose();
39+
this._indexBuffer.Dispose();
40+
this._vertices = null;
41+
this._indices = null;
42+
this._computeBuffer = null;
43+
this._indexBuffer = null;
4644
}
4745

4846
void _initComputeBuffer() {
4947
var stride = Marshal.SizeOf(typeof(uiVertex));
5048
var strideIndex = Marshal.SizeOf(typeof(int));
51-
_computeBuffer = new ComputeBuffer(COMPUTE_BUFFER_MAX_ITEM_NUM, stride);
52-
_vertices = new List<uiVertex>();
49+
this._computeBuffer = new ComputeBuffer(COMPUTE_BUFFER_MAX_ITEM_NUM, stride);
50+
this._vertices = new List<uiVertex>();
5351

54-
_indexBuffer = new ComputeBuffer(COMPUTE_BUFFER_MAX_ITEM_NUM, strideIndex);
55-
_indices = new List<int>();
52+
this._indexBuffer = new ComputeBuffer(COMPUTE_BUFFER_MAX_ITEM_NUM, strideIndex);
53+
this._indices = new List<int>();
5654
}
5755

5856
void _resetComputeBuffer() {
59-
if (!CanvasShader.supportComputeBuffer) return;
57+
if (!this.supportComputeBuffer) return;
6058

61-
if (_computeBuffer == null) {
59+
if (this._computeBuffer == null) {
6260
this._initComputeBuffer();
6361
}
6462

65-
_vertices.Clear();
66-
_indices.Clear();
67-
_startVertex = 0;
68-
_startIndex = 0;
63+
this._vertices.Clear();
64+
this._indices.Clear();
65+
this._startVertex = 0;
66+
this._startIndex = 0;
6967
}
7068

7169
void _bindComputeBuffer() {
72-
if (!CanvasShader.supportComputeBuffer) return;
70+
if (!this.supportComputeBuffer) return;
7371

74-
_computeBuffer.SetData(_vertices);
75-
_indexBuffer.SetData(_indices);
72+
this._computeBuffer.SetData(this._vertices);
73+
this._indexBuffer.SetData(this._indices);
7674
}
7775

7876
void _addMeshToComputeBuffer(List<Vector3> vertex, List<Vector2> uv, List<int> triangles) {
79-
if (!CanvasShader.supportComputeBuffer) return;
77+
if (!this.supportComputeBuffer) return;
8078

81-
_startVertex = _vertices.Count;
82-
_startIndex = _indices.Count;
79+
this._startVertex = this._vertices.Count;
80+
this._startIndex = this._indices.Count;
8381

8482
var hasUv = uv != null;
8583

8684
for (int i = 0; i < vertex.Count; i++) {
87-
_vertices.Add(new uiVertex {
85+
this._vertices.Add(new uiVertex {
8886
position = new Vector2(vertex[i].x, vertex[i].y),
8987
uv = hasUv ? uv[i] : Vector2.zero
9088
});
9189
}
9290

9391
foreach (var triangleId in triangles) {
94-
_indices.Add(triangleId + _startVertex);
92+
this._indices.Add(triangleId + this._startVertex);
9593
}
9694
}
9795
}

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

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ public void dispose() {
4242
this._lastScissor = null;
4343
this._layers.Clear();
4444
}
45-
46-
_instanceNum--;
47-
_releaseComputeBuffer();
45+
46+
this._releaseComputeBuffer();
4847
}
4948

5049
public PictureFlusher(RenderTexture renderTexture, float devicePixelRatio, MeshPool meshPool) {
@@ -60,8 +59,6 @@ public PictureFlusher(RenderTexture renderTexture, float devicePixelRatio, MeshP
6059
this.___drawTextDrawMeshCallback = this._drawTextDrawMeshCallback;
6160
this.___drawPathDrawMeshCallback2 = this._drawPathDrawMeshCallback2;
6261
this.___drawPathDrawMeshCallback = this._drawPathDrawMeshCallback;
63-
64-
_instanceNum++;
6562
}
6663

6764
readonly _drawPathDrawMeshCallbackDelegate ___drawTextDrawMeshCallback;
@@ -284,7 +281,7 @@ void _setMatrix(uiMatrix3 matrix) {
284281

285282
void _clipRect(Rect rect) {
286283
var path = uiPath.create();
287-
path.addRect(uiRectHelper.fromRect(rect));
284+
path.addRect(uiRectHelper.fromRect(rect).Value);
288285
this._clipPath(path);
289286
uiPathCacheManager.putToCache(path);
290287
}
@@ -774,7 +771,7 @@ void _drawPicture(Picture picture, bool needsSave = true) {
774771
break;
775772
case DrawSaveLayer cmd: {
776773
saveCount++;
777-
this._saveLayer(uiRectHelper.fromRect(cmd.rect), uiPaint.fromPaint(cmd.paint));
774+
this._saveLayer(uiRectHelper.fromRect(cmd.rect).Value, uiPaint.fromPaint(cmd.paint));
778775
break;
779776
}
780777

@@ -852,14 +849,14 @@ void _drawPicture(Picture picture, bool needsSave = true) {
852849
}
853850

854851
case DrawImageRect cmd: {
855-
this._drawImageRect(cmd.image, uiRectHelper.fromRect(cmd.src), uiRectHelper.fromRect(cmd.dst),
852+
this._drawImageRect(cmd.image, uiRectHelper.fromRect(cmd.src), uiRectHelper.fromRect(cmd.dst).Value,
856853
uiPaint.fromPaint(cmd.paint));
857854
break;
858855
}
859856

860857
case DrawImageNine cmd: {
861858
this._drawImageNine(cmd.image, uiRectHelper.fromRect(cmd.src),
862-
uiRectHelper.fromRect(cmd.center), uiRectHelper.fromRect(cmd.dst),
859+
uiRectHelper.fromRect(cmd.center).Value, uiRectHelper.fromRect(cmd.dst).Value,
863860
uiPaint.fromPaint(cmd.paint));
864861
break;
865862
}
@@ -1022,7 +1019,7 @@ void _drawTextBlob(TextBlob? textBlob, uiOffset offset, uiPaint paint) {
10221019
matrix.preTranslate(offset.dx, offset.dy);
10231020

10241021
var mesh = TextBlobMesh.create(textBlob.Value, scale, matrix);
1025-
var textBlobBounds = matrix.mapRect(uiRectHelper.fromRect(textBlob.Value.boundsInText));
1022+
var textBlobBounds = matrix.mapRect(uiRectHelper.fromRect(textBlob.Value.boundsInText).Value);
10261023

10271024
// request font texture so text mesh could be generated correctly
10281025
var style = textBlob.Value.style;
@@ -1046,10 +1043,6 @@ void _drawTextBlob(TextBlob? textBlob, uiOffset offset, uiPaint paint) {
10461043
}
10471044

10481045
public void flush(uiPicture picture) {
1049-
if (!CanvasShader.isReady()) {
1050-
return;
1051-
}
1052-
10531046
this._reset();
10541047
this._resetRenderTextureId();
10551048
this._resetComputeBuffer();
@@ -1174,11 +1167,11 @@ void _drawLayer(RenderLayer layer, CommandBuffer cmdBuf) {
11741167
}
11751168

11761169
D.assert(mesh.vertices.Count > 0);
1177-
if (CanvasShader.supportComputeBuffer) {
1170+
if (this.supportComputeBuffer) {
11781171
this._addMeshToComputeBuffer(mesh.vertices?.data, mesh.uv?.data, mesh.triangles?.data);
1179-
cmd.properties.SetBuffer(CmdDraw.vertexBufferId, _computeBuffer);
1180-
cmd.properties.SetBuffer(CmdDraw.indexBufferId, _indexBuffer);
1181-
cmd.properties.SetInt(CmdDraw.startIndexId, _startIndex);
1172+
cmd.properties.SetBuffer(CmdDraw.vertexBufferId, this._computeBuffer);
1173+
cmd.properties.SetBuffer(CmdDraw.indexBufferId, this._indexBuffer);
1174+
cmd.properties.SetInt(CmdDraw.startIndexId, this._startIndex);
11821175
cmdBuf.DrawProcedural(Matrix4x4.identity, cmd.material, cmd.pass, MeshTopology.Triangles, mesh.triangles.Count, 1, cmd.properties.mpb);
11831176
}
11841177
else {

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,8 @@ static Shader GetShader(string shaderName) {
169169
return shader;
170170
}
171171

172-
public static bool enableComputeBuffer = true;
173-
174-
public static bool supportComputeBuffer;
175-
176172
static CanvasShader() {
177-
173+
InitShaders();
178174
}
179175

180176
static readonly int _viewportId = Shader.PropertyToID("_viewport");
Lines changed: 14 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,31 @@
1-
using System;
2-
using Unity.UIWidgets.foundation;
31
using UnityEngine;
42
using UnityEngine.Rendering;
53

64
namespace Unity.UIWidgets.ui {
7-
enum InitStage {
8-
NotPrepared,
9-
Prepared,
10-
Ready
11-
}
12-
135
static partial class CanvasShader {
14-
static InitStage initStage = InitStage.NotPrepared;
15-
static int initialFrameCount;
16-
static Shader testShader;
17-
const string testShaderName = "UIWidgets/canvas_convexFill_cb";
18-
19-
static bool OnNotPrepared() {
20-
initStage = InitStage.Prepared;
6+
const bool enableComputeBuffer = false;
217

22-
initialFrameCount = Time.frameCount;
23-
testShader = GetShader(testShaderName);
24-
var material = new Material(testShader);
25-
//for Unity 2018 or below, shader is compiled after Shader.Find() call immediately,
26-
//therefore we can just skip the manually preload if the compilation fails
27-
if (!material.shader.isSupported) {
28-
ObjectUtils.SafeDestroy(material);
29-
return OnPrepared(true);
30-
}
31-
32-
using (var cmdBuf = new CommandBuffer()) {
33-
var renderTarget = new RenderTexture(1, 1, 1);
34-
cmdBuf.SetRenderTarget(renderTarget);
8+
const bool enableDebugLog = false;
359

36-
var mesh = new Mesh {
37-
vertices = new[] {new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 1, 0)},
38-
uv = new[] {new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1)},
39-
triangles = new[] {0, 1, 2}
40-
};
41-
cmdBuf.DrawMesh(mesh, Matrix4x4.identity, material);
42-
cmdBuf.DisableScissorRect();
43-
Graphics.ExecuteCommandBuffer(cmdBuf);
10+
public static bool supportComputeBuffer;
4411

45-
ObjectUtils.SafeDestroy(renderTarget);
46-
ObjectUtils.SafeDestroy(mesh);
12+
static void DebugAssert(bool condition, string logMsg) {
13+
if (enableDebugLog && !condition) {
14+
Debug.Log(logMsg);
4715
}
48-
49-
ObjectUtils.SafeDestroy(material);
50-
51-
return false;
5216
}
5317

54-
static bool OnPrepared(bool forceReady = false) {
55-
D.assert(initStage == InitStage.Prepared);
56-
if (!forceReady && initialFrameCount >= Time.frameCount) {
57-
return false;
58-
}
59-
60-
initStage = InitStage.Ready;
61-
DoPrepare();
62-
return true;
18+
static bool IsShaderSupported() {
19+
return SystemInfo.graphicsDeviceType == GraphicsDeviceType.Metal ||
20+
SystemInfo.graphicsDeviceType == GraphicsDeviceType.Vulkan ||
21+
SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D12;
6322
}
6423

65-
static void DoPrepare() {
66-
D.assert(testShader != null);
67-
var isShaderSupported = testShader.isSupported;
68-
testShader = null;
69-
supportComputeBuffer = enableComputeBuffer && SystemInfo.supportsComputeShaders && isShaderSupported;
24+
static void InitShaders() {
25+
supportComputeBuffer = enableComputeBuffer && SystemInfo.supportsComputeShaders && IsShaderSupported();
7026

7127
if (!supportComputeBuffer) {
28+
DebugAssert(false, "init default shaders");
7229
var convexFillShader = GetShader("UIWidgets/canvas_convexFill");
7330
var fill0Shader = GetShader("UIWidgets/canvas_fill0");
7431
var fill1Shader = GetShader("UIWidgets/canvas_fill1");
@@ -94,6 +51,7 @@ static void DoPrepare() {
9451
_shadowRBox = new Material(shadowRBoxShader) {hideFlags = HideFlags.HideAndDontSave};
9552
}
9653
else {
54+
DebugAssert(false, "init computebuffer shaders");
9755
var convexFillShaderCompute = GetShader("UIWidgets/canvas_convexFill_cb");
9856
var fill0ShaderCompute = GetShader("UIWidgets/canvas_fill0_cb");
9957
var fill1ShaderCompute = GetShader("UIWidgets/canvas_fill1_cb");
@@ -119,18 +77,5 @@ static void DoPrepare() {
11977
_shadowRBox = new Material(shadowRBoxShaderCompute) {hideFlags = HideFlags.HideAndDontSave};
12078
}
12179
}
122-
123-
public static bool isReady() {
124-
switch (initStage) {
125-
case InitStage.NotPrepared:
126-
return OnNotPrepared();
127-
case InitStage.Prepared:
128-
return OnPrepared();
129-
case InitStage.Ready:
130-
return true;
131-
default:
132-
throw new ArgumentOutOfRangeException();
133-
}
134-
}
13580
}
13681
}

0 commit comments

Comments
 (0)