浏览代码

optimize string builder (render texture key)

optimize gaussiankernel key
/main
xingwei.zhu 5 年前
当前提交
daccbfd5
共有 3 个文件被更改,包括 43 次插入70 次删除
  1. 12
      Runtime/ui/painting/txt/text_blob.cs
  2. 29
      Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_impl.cs
  3. 72
      Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_utils.cs

12
Runtime/ui/painting/txt/text_blob.cs


}
public Rect boundsInText {
get { return this.bounds.shift(new Offset(this.positions[0].x, this.positions[0].y)); }
get {
if (this._boundsInText == null) {
this._boundsInText = this.bounds.shift(new Offset(this.positions[0].x, this.positions[0].y));
}
return this._boundsInText;
}
}
static long _nextInstanceId = 0;

internal readonly int textSize;
internal readonly Vector2d[] positions;
internal readonly TextStyle style;
internal readonly Rect bounds; // bounds with positions[start] as origin
internal readonly Rect bounds; // bounds with positions[start] as origin
Rect _boundsInText;
}
public class TextBlobBuilder {

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


RenderLayer _currentLayer;
uiRect? _lastScissor;
readonly List<string> _renderTextureKeys = new List<string>();
int _curRenderTextureId = 0;
string _getNewRenderTextureKey() {
if (this._curRenderTextureId == this._renderTextureKeys.Count) {
for (int i = 0; i < 32; i++) {
this._renderTextureKeys.Add($"_RtKey_{i + this._curRenderTextureId}");
}
}
var ret = this._renderTextureKeys[this._curRenderTextureId];
this._curRenderTextureId++;
return ret;
}
void _resetRenderTextureId() {
this._curRenderTextureId = 0;
}
public void dispose() {
if (this._currentLayer != null) {
this._clearLayer(this._currentLayer);

}
var layer = RenderLayer.create(
rtID: Shader.PropertyToID("_rtID_" + this._layers.Count + "_" + parentLayer.layers.Count),
rtID: Shader.PropertyToID(this._getNewRenderTextureKey()),
width: textureWidth,
height: textureHeight,
layerBounds: bounds,

}
var maskLayer = RenderLayer.create(
rtID: Shader.PropertyToID("_rtID_" + this._layers.Count + "_" + parentLayer.layers.Count),
rtID: Shader.PropertyToID(this._getNewRenderTextureKey()),
width: textureWidth,
height: textureHeight,
layerBounds: maskBounds,

}
var blurXLayer = RenderLayer.create(
rtID: Shader.PropertyToID("_rtID_" + this._layers.Count + "_" + parentLayer.layers.Count),
rtID: Shader.PropertyToID(this._getNewRenderTextureKey()),
width: textureWidth,
height: textureHeight,
layerBounds: maskLayer.layerBounds,

parentLayer.addLayer(blurXLayer);
var blurYLayer = RenderLayer.create(
rtID: Shader.PropertyToID("_rtID_" + this._layers.Count + "_" + parentLayer.layers.Count),
rtID: Shader.PropertyToID(this._getNewRenderTextureKey()),
width: textureWidth,
height: textureHeight,
layerBounds: maskLayer.layerBounds,

var blurMesh = ImageMeshGenerator.imageMesh(null, uiRectHelper.one, maskBounds);
if (!this._applyClip(blurMesh.bounds)) {
blurMesh.dispose();
this._drawPathDrawMeshQuit(mesh);
return;
}

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

72
Runtime/ui/utils/renderer/cmdbufferCanvas/rendering/canvas_utils.cs


namespace Unity.UIWidgets.ui {
static class BlurUtils {
static readonly Dictionary<_GaussianKernelKey, float[]> _gaussianKernels
= new Dictionary<_GaussianKernelKey, float[]>();
static readonly Dictionary<int, float[]> _gaussianKernels
= new Dictionary<int, float[]>();
var key = new _GaussianKernelKey(gaussianSigma, radius);
//round gaussian sigma to 0.1
gaussianSigma = Mathf.Round(gaussianSigma * 10) / 10f;
//assume radius < 10000
D.assert(radius < 10000);
int key = (int)(gaussianSigma * 1000000) + radius;
return _gaussianKernels.putIfAbsent(key, () => {
var kernel = new float[25];
float twoSigmaSqrd = 2.0f * gaussianSigma * gaussianSigma;

radius = Mathf.CeilToInt(sigma * 3.0f);
D.assert(radius <= 3 * MAX_BLUR_SIGMA);
return sigma;
}
class _GaussianKernelKey : IEquatable<_GaussianKernelKey> {
public readonly float gaussianSigma;
public readonly int radius;
public _GaussianKernelKey(float gaussianSigma, int radius) {
this.gaussianSigma = gaussianSigma;
this.radius = radius;
}
public bool Equals(_GaussianKernelKey other) {
if (ReferenceEquals(null, other)) {
return false;
}
if (ReferenceEquals(this, other)) {
return true;
}
return this.gaussianSigma.Equals(other.gaussianSigma) &&
this.radius == other.radius;
}
public override bool Equals(object obj) {
if (ReferenceEquals(null, obj)) {
return false;
}
if (ReferenceEquals(this, obj)) {
return true;
}
if (obj.GetType() != this.GetType()) {
return false;
}
return this.Equals((_GaussianKernelKey) obj);
}
public override int GetHashCode() {
unchecked {
var hashCode = this.gaussianSigma.GetHashCode();
hashCode = (hashCode * 397) ^ this.radius;
return hashCode;
}
}
public static bool operator ==(_GaussianKernelKey left, _GaussianKernelKey right) {
return Equals(left, right);
}
public static bool operator !=(_GaussianKernelKey left, _GaussianKernelKey right) {
return !Equals(left, right);
}
public override string ToString() {
return $"_GaussianKernelKey(gaussianSigma: {this.gaussianSigma:F1}, radius: {this.radius})";
}
}
}

正在加载...
取消
保存