我应该计算GPU上还是CPU上的矩阵? [英] Should I calculate matrices on the GPU or on the CPU?

查看:644
本文介绍了我应该计算GPU上还是CPU上的矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该更喜欢计算CPU或GPU上的矩阵吗?



假设我有以下矩阵 P * V * M ,我应该在CPU上计算我可以将最终矩阵发送到GPU(GLSL),或者应该将这三个矩阵分别发送到GPU,以便GLSL可以计算最终矩阵?



<这种情况下GLSL将必须计算每个顶点的MVP矩阵,所以它可能更快地预计算它在CPU上。



但是我们假设GLSL只需要计算一次MVP矩阵,GPU是否会比CPU计算最终矩阵更快?

解决方案

一般规则:如果您可以以统一形式将其传递到着色器,始终 CPU;没有例外。在着色器侧的计算仅对于在顶点和片段之间变化的值有意义。在整批顶点中的一切都是最有效的处理在CPU上。



GPU不是魔术可以做更快的一切机器。存在某些任务,其中CPU可以容易地胜过GPU,即使对于非常大的数据集。所以一个非常简单的指导是:如果你可以将它移动到CPU,而不花费更多的CPU时间做计算,而不是处理它的总开销需要的,然后在CPU上。在这些任务中计算单个矩阵。


Should I prefer to calculate matrices on the CPU or GPU?

Let's say I have the following matrices P * V * M , should I calculate them on the CPU so that I can send the final matrix to the GPU (GLSL) or should I send those three matrices separately to the GPU so that GLSL can calculate the final matrix?

I mean in this case GLSL would have to calculate the MVP matrix for every vertex, so it is probably faster to precompute it on the CPU.

But let's say that GLSL only has to calculate he MVP matrix once, would the GPU calculate the final matrix faster than the CPU?

解决方案

General rule: If you can pass it to a shader in form of a uniform, always precalculate on the CPU; no exceptions. Calculations on the shader side make sense only for values that vary between vertices and fragments. Everything that's constant among a whole batch of vertices is most efficiently dealt with on the CPU.

GPUs are not magic "can do faster everything" machines. There are certain tasks where a CPU can easily outperform a GPU, even for very large datasets. So a very simple guideline is: If you can move it to the CPU without spending more CPU time doing the calculation than it takes for the GPU in total overhead to process it, then do it on the CPU. The calculation of a single matrix is among those tasks.

这篇关于我应该计算GPU上还是CPU上的矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆