浏览代码

Merge pull request #324 from UnityTech/dev

Dev
/main
GitHub 5 年前
当前提交
e3e8b270
共有 7 个文件被更改,包括 82 次插入149 次删除
  1. 28
      Runtime/engine/UIWidgetsPanel.cs
  2. 74
      Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_computebuffer_utils.cs
  3. 29
      Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_impl.cs
  4. 6
      Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_shader.cs
  5. 83
      Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_shader_initializer.cs
  6. 5
      Runtime/ui/renderer/common/geometry/rect.cs
  7. 6
      Runtime/ui/renderer/common/picture.cs

28
Runtime/engine/UIWidgetsPanel.cs


));
}
public Vector2? getPointPosition(PointerEventData eventData) {
if (eventData.enterEventCamera == null && this.canvas.renderMode == RenderMode.ScreenSpaceCamera) {
return null;
Camera getActiveCamera() {
//refer to: https://zhuanlan.zhihu.com/p/37127981
Camera eventCamera = null;
if (this.canvas.renderMode != RenderMode.ScreenSpaceOverlay) {
eventCamera = this.canvas.GetComponent<GraphicRaycaster>().eventCamera;
return eventCamera;
}
Vector2? getPointPosition(PointerEventData eventData) {
Camera camera = this.getActiveCamera();
eventData.enterEventCamera, out localPoint);
camera, out localPoint);
var scaleFactor = this.canvas.scaleFactor;
localPoint.x = (localPoint.x - this.rectTransform.rect.min.x) * scaleFactor;
localPoint.y = (this.rectTransform.rect.max.y - localPoint.y) * scaleFactor;

public Vector2? getPointPosition(Vector2 position) {
Vector2? getPointPosition(Vector2 position) {
Camera eventCamera = null;
if (this.canvas.renderMode != RenderMode.ScreenSpaceCamera) {
eventCamera = this.canvas.GetComponent<GraphicRaycaster>().eventCamera;
}
if (eventCamera == null && this.canvas.renderMode == RenderMode.ScreenSpaceCamera) {
return null;
}
Camera eventCamera = this.getActiveCamera();
RectTransformUtility.ScreenPointToLocalPointInRectangle(this.rectTransform, position,
eventCamera, out localPoint);

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


public Vector2 uv;
}
static ComputeBuffer _computeBuffer;
static List<uiVertex> _vertices;
ComputeBuffer _computeBuffer;
List<uiVertex> _vertices;
static ComputeBuffer _indexBuffer;
static List<int> _indices;
ComputeBuffer _indexBuffer;
List<int> _indices;
static int _startVertex;
static int _startIndex;
static int _instanceNum;
int _startVertex;
int _startIndex;
static void _releaseComputeBuffer() {
if (!CanvasShader.supportComputeBuffer) {
return;
}
bool supportComputeBuffer {
get { return CanvasShader.supportComputeBuffer; }
}
if (_computeBuffer == null) {
void _releaseComputeBuffer() {
if (!this.supportComputeBuffer) {
if (_instanceNum != 0) {
if (this._computeBuffer == null) {
_computeBuffer.Dispose();
_indexBuffer.Dispose();
_vertices = null;
_indices = null;
_computeBuffer = null;
_indexBuffer = null;
this._computeBuffer.Dispose();
this._indexBuffer.Dispose();
this._vertices = null;
this._indices = null;
this._computeBuffer = null;
this._indexBuffer = null;
_computeBuffer = new ComputeBuffer(COMPUTE_BUFFER_MAX_ITEM_NUM, stride);
_vertices = new List<uiVertex>();
this._computeBuffer = new ComputeBuffer(COMPUTE_BUFFER_MAX_ITEM_NUM, stride);
this._vertices = new List<uiVertex>();
_indexBuffer = new ComputeBuffer(COMPUTE_BUFFER_MAX_ITEM_NUM, strideIndex);
_indices = new List<int>();
this._indexBuffer = new ComputeBuffer(COMPUTE_BUFFER_MAX_ITEM_NUM, strideIndex);
this._indices = new List<int>();
if (!CanvasShader.supportComputeBuffer) return;
if (!this.supportComputeBuffer) return;
if (_computeBuffer == null) {
if (this._computeBuffer == null) {
_vertices.Clear();
_indices.Clear();
_startVertex = 0;
_startIndex = 0;
this._vertices.Clear();
this._indices.Clear();
this._startVertex = 0;
this._startIndex = 0;
if (!CanvasShader.supportComputeBuffer) return;
if (!this.supportComputeBuffer) return;
_computeBuffer.SetData(_vertices);
_indexBuffer.SetData(_indices);
this._computeBuffer.SetData(this._vertices);
this._indexBuffer.SetData(this._indices);
if (!CanvasShader.supportComputeBuffer) return;
if (!this.supportComputeBuffer) return;
_startVertex = _vertices.Count;
_startIndex = _indices.Count;
this._startVertex = this._vertices.Count;
this._startIndex = this._indices.Count;
_vertices.Add(new uiVertex {
this._vertices.Add(new uiVertex {
position = new Vector2(vertex[i].x, vertex[i].y),
uv = hasUv ? uv[i] : Vector2.zero
});

_indices.Add(triangleId + _startVertex);
this._indices.Add(triangleId + this._startVertex);
}
}
}

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


this._lastScissor = null;
this._layers.Clear();
}
_instanceNum--;
_releaseComputeBuffer();
this._releaseComputeBuffer();
}
public PictureFlusher(RenderTexture renderTexture, float devicePixelRatio, MeshPool meshPool) {

this.___drawTextDrawMeshCallback = this._drawTextDrawMeshCallback;
this.___drawPathDrawMeshCallback2 = this._drawPathDrawMeshCallback2;
this.___drawPathDrawMeshCallback = this._drawPathDrawMeshCallback;
_instanceNum++;
}
readonly _drawPathDrawMeshCallbackDelegate ___drawTextDrawMeshCallback;

void _clipRect(Rect rect) {
var path = uiPath.create();
path.addRect(uiRectHelper.fromRect(rect));
path.addRect(uiRectHelper.fromRect(rect).Value);
this._clipPath(path);
uiPathCacheManager.putToCache(path);
}

break;
case DrawSaveLayer cmd: {
saveCount++;
this._saveLayer(uiRectHelper.fromRect(cmd.rect), uiPaint.fromPaint(cmd.paint));
this._saveLayer(uiRectHelper.fromRect(cmd.rect).Value, uiPaint.fromPaint(cmd.paint));
break;
}

}
case DrawImageRect cmd: {
this._drawImageRect(cmd.image, uiRectHelper.fromRect(cmd.src), uiRectHelper.fromRect(cmd.dst),
this._drawImageRect(cmd.image, uiRectHelper.fromRect(cmd.src), uiRectHelper.fromRect(cmd.dst).Value,
uiPaint.fromPaint(cmd.paint));
break;
}

uiRectHelper.fromRect(cmd.center), uiRectHelper.fromRect(cmd.dst),
uiRectHelper.fromRect(cmd.center).Value, uiRectHelper.fromRect(cmd.dst).Value,
uiPaint.fromPaint(cmd.paint));
break;
}

matrix.preTranslate(offset.dx, offset.dy);
var mesh = TextBlobMesh.create(textBlob.Value, scale, matrix);
var textBlobBounds = matrix.mapRect(uiRectHelper.fromRect(textBlob.Value.boundsInText));
var textBlobBounds = matrix.mapRect(uiRectHelper.fromRect(textBlob.Value.boundsInText).Value);
// request font texture so text mesh could be generated correctly
var style = textBlob.Value.style;

}
public void flush(uiPicture picture) {
if (!CanvasShader.isReady()) {
return;
}
this._reset();
this._resetRenderTextureId();
this._resetComputeBuffer();

}
D.assert(mesh.vertices.Count > 0);
if (CanvasShader.supportComputeBuffer) {
if (this.supportComputeBuffer) {
cmd.properties.SetBuffer(CmdDraw.vertexBufferId, _computeBuffer);
cmd.properties.SetBuffer(CmdDraw.indexBufferId, _indexBuffer);
cmd.properties.SetInt(CmdDraw.startIndexId, _startIndex);
cmd.properties.SetBuffer(CmdDraw.vertexBufferId, this._computeBuffer);
cmd.properties.SetBuffer(CmdDraw.indexBufferId, this._indexBuffer);
cmd.properties.SetInt(CmdDraw.startIndexId, this._startIndex);
cmdBuf.DrawProcedural(Matrix4x4.identity, cmd.material, cmd.pass, MeshTopology.Triangles, mesh.triangles.Count, 1, cmd.properties.mpb);
}
else {

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


return shader;
}
public static bool enableComputeBuffer = true;
public static bool supportComputeBuffer;
InitShaders();
}
static readonly int _viewportId = Shader.PropertyToID("_viewport");

83
Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_shader_initializer.cs


using System;
using Unity.UIWidgets.foundation;
enum InitStage {
NotPrepared,
Prepared,
Ready
}
static InitStage initStage = InitStage.NotPrepared;
static int initialFrameCount;
static Shader testShader;
const string testShaderName = "UIWidgets/canvas_convexFill_cb";
static bool OnNotPrepared() {
initStage = InitStage.Prepared;
initialFrameCount = Time.frameCount;
testShader = GetShader(testShaderName);
var material = new Material(testShader);
//for Unity 2018 or below, shader is compiled after Shader.Find() call immediately,
//therefore we can just skip the manually preload if the compilation fails
if (!material.shader.isSupported) {
ObjectUtils.SafeDestroy(material);
return OnPrepared(true);
}
const bool enableComputeBuffer = false;
using (var cmdBuf = new CommandBuffer()) {
var renderTarget = new RenderTexture(1, 1, 1);
cmdBuf.SetRenderTarget(renderTarget);
const bool enableDebugLog = false;
var mesh = new Mesh {
vertices = new[] {new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 1, 0)},
uv = new[] {new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1)},
triangles = new[] {0, 1, 2}
};
cmdBuf.DrawMesh(mesh, Matrix4x4.identity, material);
cmdBuf.DisableScissorRect();
Graphics.ExecuteCommandBuffer(cmdBuf);
public static bool supportComputeBuffer;
ObjectUtils.SafeDestroy(renderTarget);
ObjectUtils.SafeDestroy(mesh);
static void DebugAssert(bool condition, string logMsg) {
if (enableDebugLog && !condition) {
Debug.Log(logMsg);
ObjectUtils.SafeDestroy(material);
return false;
static bool OnPrepared(bool forceReady = false) {
D.assert(initStage == InitStage.Prepared);
if (!forceReady && initialFrameCount >= Time.frameCount) {
return false;
}
initStage = InitStage.Ready;
DoPrepare();
return true;
static bool IsShaderSupported() {
return SystemInfo.graphicsDeviceType == GraphicsDeviceType.Metal ||
SystemInfo.graphicsDeviceType == GraphicsDeviceType.Vulkan ||
SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D12;
static void DoPrepare() {
D.assert(testShader != null);
var isShaderSupported = testShader.isSupported;
testShader = null;
supportComputeBuffer = enableComputeBuffer && SystemInfo.supportsComputeShaders && isShaderSupported;
static void InitShaders() {
supportComputeBuffer = enableComputeBuffer && SystemInfo.supportsComputeShaders && IsShaderSupported();
DebugAssert(false, "init default shaders");
var convexFillShader = GetShader("UIWidgets/canvas_convexFill");
var fill0Shader = GetShader("UIWidgets/canvas_fill0");
var fill1Shader = GetShader("UIWidgets/canvas_fill1");

_shadowRBox = new Material(shadowRBoxShader) {hideFlags = HideFlags.HideAndDontSave};
}
else {
DebugAssert(false, "init computebuffer shaders");
var convexFillShaderCompute = GetShader("UIWidgets/canvas_convexFill_cb");
var fill0ShaderCompute = GetShader("UIWidgets/canvas_fill0_cb");
var fill1ShaderCompute = GetShader("UIWidgets/canvas_fill1_cb");

_filterMat = new Material(filterShaderCompute) {hideFlags = HideFlags.HideAndDontSave};
_shadowBox = new Material(shadowBoxShaderCompute) {hideFlags = HideFlags.HideAndDontSave};
_shadowRBox = new Material(shadowRBoxShaderCompute) {hideFlags = HideFlags.HideAndDontSave};
}
}
public static bool isReady() {
switch (initStage) {
case InitStage.NotPrepared:
return OnNotPrepared();
case InitStage.Prepared:
return OnPrepared();
case InitStage.Ready:
return true;
default:
throw new ArgumentOutOfRangeException();
}
}
}

5
Runtime/ui/renderer/common/geometry/rect.cs


}
public static class uiRectHelper {
public static uiRect fromRect(Rect rect) {
public static uiRect? fromRect(Rect rect) {
if (rect == null) {
return null;
}
return new uiRect(rect.left, rect.top, rect.right, rect.bottom);
}

6
Runtime/ui/renderer/common/picture.cs


case uiDrawClipRRect cmd: {
var state = this._getState();
var rect = state.xform.mapRect(uiRectHelper.fromRect(cmd.rrect.outerRect));
var rect = state.xform.mapRect(uiRectHelper.fromRect(cmd.rrect.outerRect).Value);
state.scissor = state.scissor == null ? rect : state.scissor.Value.intersect(rect);
this._setState(state);
break;

}
case uiDrawPicture cmd: {
var state = this._getState();
var rect = state.xform.mapRect(uiRectHelper.fromRect(cmd.picture.paintBounds));
var rect = state.xform.mapRect(uiRectHelper.fromRect(cmd.picture.paintBounds).Value);
this._addPaintBounds(rect);
break;
}

var rect = uiRectHelper.fromRect(
cmd.textBlob.Value.shiftedBoundsInText(cmd.offset.Value.dx, cmd.offset.Value.dy));
cmd.textBlob.Value.shiftedBoundsInText(cmd.offset.Value.dx, cmd.offset.Value.dy)).Value;
rect = state.xform.mapRect(rect);
var paint = cmd.paint;

正在加载...
取消
保存