如何在没有(“<<”||“>>”)运算符的情况下高效地执行位移?

发布于 2024-09-14 16:41:15 字数 359 浏览 2 评论 0 原文

我正在开发 OpenGL ES 2.0 着色器,并且我有紧密打包的数据,例如两个字节块内的三个 5 位无符号整数。要解压这些数据,我显然需要位移位,但这在 OpenGL ES 着色语言中不受支持(请参见第 29 页http://www.opengl.org/registry/doc/GLSLangSpec.Full.1.20.8.pdf

因此我执行了许多 *2 和 /2 操作模拟位移位。

有谁知道更有效/优雅的方法来做到这一点?有什么我不知道的技巧吗?

谢谢!

I am working on a OpenGL ES 2.0 shader and I have tightly packed data e.g. three 5-bit unsigned integers within a block of two bytes. To unpack this data I obviously need bit-shifting, but this is not supported in OpenGL ES Shading Language (see page 29 http://www.opengl.org/registry/doc/GLSLangSpec.Full.1.20.8.pdf)

Consequently I perform a number of *2 and /2 operations to emulate bit shifting.

Does anyone know a more efficient/elegant way to do this? Is there a trick I am not aware of?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初雪 2024-09-21 16:41:15

如果您执行多个班次,则可以使用电源操作。位移位是乘法或除以 2n,我认为幂运算比多个乘法或除法运算更具可读性,但我不确定性能。我认为这是一种更优雅的解决方案,但可能不是更有效的解决方案。

If you are performing multiple shifts, you can use power operations. A bit shift is a multiplication or division by 2n, and a power operation would be more readable than multiple multiplication or division operations, I think, but I'm not sure about the performance. I suppose this is a more elegant solution, but probably not a more efficient one.

桃气十足 2024-09-21 16:41:15

根据您正在做的事情,这些线程可能会有一些用处:

GLSL:将法线打包在单个浮点数中
链接

将多个浮点数打包为一个浮点数浮点值
链接

将浮点数打包到各种位深度目标
(必须搜索 OpenGL.org 论坛,因为堆栈溢出不允许新用户使用超过 2 个链接)

Depending on what you are doing, these threads may be of some use:

GLSL: packing a normal in a single float
link

Packing multiple floats into a single float value
link

Packing floats to various bit depth targets
(have to search the OpenGL.org forum as Stack overflow does not allow more than 2 links for new users)

你如我软肋 2024-09-21 16:41:15

我从未使用过 OpenGL,但最有效的方法是为每种类型使用 16 位查找表(如果您的环境支持的话)。您需要在启动时填充该表一次,但这应该非常快。您可以为每种类型使用单独的表或二维表,例如 theTable[65536][3]。

I've never used OpenGL, but the most efficient method would be a 16 bit lookup table for each type if your environment supports it. You would need to populate the table once on startup, but this should be very quick. You could use seperate tables for each type or a 2 dimensional table, eg, theTable[65536][3].

请你别敷衍 2024-09-21 16:41:15

从你的问题来看,我不确定,但你可以使用 & (位与),对吗?那么写起来应该很快:

processing(variable & 11111); processing(variable & 1111100000); (...)

当然,这些位掩码应该保存为整数常量。

From your question I'm not sure, but you can use & (bit-and), right? Then it should be pretty fast to write:

processing(variable & 11111); processing(variable & 1111100000); (...)

Those bit-masks should be saved as integral constants, of course.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文