AngularJS自定义过滤器调用两次并在第二次调用时删除输入数据(AngularJS custom filter called twice and delete input data on second call)
这是代码。
var app = angular.module("nameApp", ["ngRoute"]); app.controller("ctrlname", function ($scope, $http, $filter, apiKey, apiUrl) { $scope.data = {}; $scope.currentPage = 1; $scope.pageSize = 5; });
$ scope.data将包含来自HTTP GET请求的数据数组。 以下是用于分页结果的自定义过滤器的代码。 基本上,这会将结果限制为仅5.分页按钮将更新$ scope.currentPage的值。
app.filter("limitResults", function ($filter, $log) { return function (data, page, size) { if (angular.isArray(data) & angular.isNumber(page) && angular.isNumber(size)) { var startPage = (page - 1) * size; if (data.length < startPage) { return []; } else { $log.info(data); $log.info(page); $log.info(size); $log.info(startPage); return $filter("limitTo")(data.splice(startPage), size); } } else { return data; } } });
这是将呈现数据的HTML页面。
<div class="row resultItems" ng-repeat="video in data.videos | limitResults:currentPage:pageSize"> <div class="col-sm-3 testing"> <img ng-src="{{video.snippet.thumbnails.default.url}}"> </div> <div class="col-sm-9 testing"> <h5> {{video.snippet.title}} </h5> <p> {{video.snippet.channelTitle}} </p> <p> {{video.snippet.description}} </p> </div> </div>
我在自定义过滤器中放了几行$ log.info代码,以便了解应用过滤器时到底发生了什么。 过滤器运行两次,这是正常行为。
我发现令人困惑的是,当自定义过滤器第一次运行时,$ log.info(data)将从HTTP GET调用接收的原始数据记录到控制台。 但是,当自定义筛选器第二次运行时,$ log.info(data)会将一个空数组记录到控制台。
鉴于“$ log.info(data); $ log.info(page); $ log.info(size);” 登录到控制台,显然第二个IF语句(if(data.length <startPage))被评估为TRUE和过滤器(返回$ filter(“limitTo”)(data.splice(startPage),size) ;) 被申请;被应用。
我只是不明白为什么数组,即传递给自定义过滤器的数据,在过滤器第二次运行时被清空。
Here are the codes.
var app = angular.module("nameApp", ["ngRoute"]); app.controller("ctrlname", function ($scope, $http, $filter, apiKey, apiUrl) { $scope.data = {}; $scope.currentPage = 1; $scope.pageSize = 5; });
The $scope.data will contain an array of data from an HTTP GET request. The following is a code for a custom filter for the purpose of pagination of results. Basically, this will limit the results to only 5. Buttons for pagination will update $scope.currentPage's value.
app.filter("limitResults", function ($filter, $log) { return function (data, page, size) { if (angular.isArray(data) & angular.isNumber(page) && angular.isNumber(size)) { var startPage = (page - 1) * size; if (data.length < startPage) { return []; } else { $log.info(data); $log.info(page); $log.info(size); $log.info(startPage); return $filter("limitTo")(data.splice(startPage), size); } } else { return data; } } });
This is the HTML page that will render the data.
<div class="row resultItems" ng-repeat="video in data.videos | limitResults:currentPage:pageSize"> <div class="col-sm-3 testing"> <img ng-src="{{video.snippet.thumbnails.default.url}}"> </div> <div class="col-sm-9 testing"> <h5> {{video.snippet.title}} </h5> <p> {{video.snippet.channelTitle}} </p> <p> {{video.snippet.description}} </p> </div> </div>
I put a few lines of $log.info code in the custom filter in order to see what really happens when the filter is applied. The filter runs twice, which is a normal behaviour.
What I find confusing is that when the custom filter runs for the first time, $log.info(data) logs the original data received from a HTTP GET call to the console. However, when the custom filter runs for the second time, $log.info(data) logs an empty array to the console.
Given the fact that "$log.info(data); $log.info(page); $log.info(size);" get logged to the console, it is obvious that the second IF statement (if (data.length < startPage)) is evaluated to TRUE and the filter (return $filter("limitTo")(data.splice(startPage), size);) is applied.
I just don't understand why the array, which is the data passed to the custom filter, gets emptied when the filter runs the second time.
原文:https://stackoverflow.com/questions/36584663
最满意答案
我的直觉是,你只是填充率。
你的100个精灵覆盖了多少像素? GPU具有有限的计算像素容量(特别是在混合的情况下 - 你说有alpha,因为它需要读取和写入帧缓冲区)。 如果你生成太多,你的帧速率将受到严重影响。 最糟糕的情况是每个精灵都覆盖整个屏幕,大约是屏幕总像素数的100倍。 (那100倍是我们所谓的透支因素)。
另一个选择是你是着色器。 你的片段着色器做什么? 如果用简单的恒定颜色输出替换它会发生什么?
我不认为几何提交与你的性能问题有关(不是100个精灵)。
最重要的是,要考虑性能,您需要使用性能分析工具。 我不打算对ipad进行编码。 SDK是否提供了分析性能的工具?
My gut feeling is that you're simply fill-rate bound.
How many pixels do your 100 sprites cover ? The GPU has a limited capacity of computing pixels (especially with blending on - you have alpha you said- since it requires reading and writing the framebuffer). And if you generate too many of them, your frame rate will suffer dramatically. the worst case for you would be that each sprite covers your whole screen, incurring ~100x the total pixel count of your screen. (that 100x is what we call the overdraw factor).
Another alternative is that you're shader bound. What does your fragment shader do ? What happens if you replace it with a simple constant color output ?
I don't think that the geometry submission has anything to do with your perf issues (not for 100 sprites).
Bottom line is, to look at performance, you want to use performance analysis tools. I don't code myself against the ipad. Does the SDK provide any tool to analyze perf ?
相关问答
更多-
是否使用OpenGL ES 1.1或2.0取决于您想要在应用程序中执行的操作,以及您需要与之兼容的设备数量。 所有的iOS设备都支持OpenGL ES 1.1,其中只有iPhone 3G S和更新的设备(iPhone 3GS,iPhone 4,iPad以及第三代和第四代iPod touch)支持OpenGL ES 2.0。 不过,苹果目前出货的所有iOS设备都与OpenGL ES 2.0兼容。 不支持OpenGL ES 2.0的设备的百分比每天都在下降。 所有的iPad都支持OpenGL ES 2.0,所以 ...
-
终于找到了解决办法。 当我在每个渲染圆圈后调用glFlush()时,它工作正常。 我没有在不同的纹理通道上渲染每个飞机,它迄今为止完美。 谢谢您的帮助。 Finally found the solution. When i call glFlush() after every rendering circle, it works fine. I no render every plane on a different texture channel, it works perfectly so far. T ...
-
来自http://shivavg.svn.sourceforge.net/viewvc/shivavg/trunk/src/shPipeline.c?revision=14&view=markup : static void shDrawVertices(SHPath *p, GLenum mode) { int start = 0; int size = 0; /* We separate vertex arrays by contours to properly handle the fill mod ...
-
我非常依赖翻译和缩放 请注意,如果您在绘图调用之间更改模型视图矩阵,则无法批处理任何内容。 (ES2没有改变这一点)。 Vbo a可从opengl ES 1.1获得。 它们可能适用于您定位的设备。 即使对于ES1.0(ARB_vertex_buffer_object) 你可以用世界空间几何创建一个大的VBO(=用cpu解析缩放和转换)并绘制它。 就我个人而言,即使每次更新这个vbo,它都足够快。 发送成千上万的小电话几乎总是最慢的。 从一个固定的管道移动到一个完整的顶点/片段着色器管线是不容易的。 它需要大 ...
-
在Android上使用OpenGL ES 2.0的Jerky sprite(2d)运动(Jerky sprite (2d) movement with OpenGL ES 2.0 on Android)[2023-02-14]
听起来像垃圾收集器运行。 要验证这一点,只需看一下logcat ......生涩的时刻是否与标记为“dalvikvm”的行相关并带有“GC_”? Sounds like the garbage collector running. To verify this, just look at logcat... do the jerky moments correlate to lines tagged "dalvikvm" and lead with "GC_"? -
需要OpenGL ES 2.0仿真器(Need OpenGL ES 2.0 Emulator)[2022-03-27]
你或许可以说服梅萨这样做。 You may be able to convince Mesa to act as such. -
有关视网膜的评论是正确的。 由于2倍分辨率的视网膜屏幕,缩放倍数的组合产生了意想不到的结果。 这导致点精灵在OpenGL 3上渲染比在OpenGL ES 2.0上大16倍。 当在着色器中写入原始值时,Point Sprite实际上大2倍,填充了该区域的4倍。 要纠正扩展问题,从规范中读取它是有帮助的: 点光栅化为每个帧缓冲像素生成一个片段,其中心位于以点(xw; yw)为中心的正方形内,边长等于当前点大小。 The comments were correct about retina. A combinat ...
-
OpenGL ES 2.0 sprite batcher为所有批次绘制一个纹理(OpenGL ES 2.0 sprite batcher is drawing one texture for all batches)[2023-11-17]
GraphicsDevice.DrawUserIndexedPrimitivesImpl方法中存在问题。 如此处所示,该方法没有考虑索引或顶点偏移。 它还错误地确定要绘制的元素数量。 因此,它将为每个绘制调用绘制索引数组(~12000个索引)。 尚未启用混合,因此黑色背景上的透明纹理无法明确这一点。 通过正确计算索引数组的偏移地址和长度来解决该问题。 protected override void DrawUserIndexedPrimitivesImpl (PrimitiveType primi ... -
从3GS开始,OpenGL支持最高2048x2048的纹理。 4S和iPad2支持4096x4096。 但是,如果不是这种情况,那么您的选项围绕打包像素和有损压缩或使用多个纹理,这两个纹理都可以在GLSL中或通过几何体修复(需要付费)。 最明显的选择是上传为四个图块。 您在每个方向上的输出大小1936允许您上传具有一些重叠的纹理,这样您即使使用线性纹理过滤也应该能够获得无缝连接。 假设您只需要水平分割,就可以确保一个纹理的底部两行与另一个纹理的顶部两行相同,并将几何连接放在这两行之间。 其他选项不仅不太明显 ...
-
我的直觉是,你只是填充率。 你的100个精灵覆盖了多少像素? GPU具有有限的计算像素容量(特别是在混合的情况下 - 你说有alpha,因为它需要读取和写入帧缓冲区)。 如果你生成太多,你的帧速率将受到严重影响。 最糟糕的情况是每个精灵都覆盖整个屏幕,大约是屏幕总像素数的100倍。 (那100倍是我们所谓的透支因素)。 另一个选择是你是着色器。 你的片段着色器做什么? 如果用简单的恒定颜色输出替换它会发生什么? 我不认为几何提交与你的性能问题有关(不是100个精灵)。 最重要的是,要考虑性能,您需要使用性能 ...