GLSL索引成均匀阵列与可变长度 [英] GLSL indexing into uniform array with variable length

查看:261
本文介绍了GLSL索引成均匀阵列与可变长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我传递一个统一的阵列几何着色器,并采用可变想要索引到它。我可以使用可变长度阵列和放大器;固定号码(数字常数),或者我可以定义一个固定长度的数组和功放指数;指数使用va​​rible。但是我不能索引使用可变长度可变的数组。

I am passing an uniform array to geometry shader and want to index into it using a variable. I can use variable length array & index with fixed number (numeric constant) OR I can define a fixed length array & index using varible. However I can't index into variable length array using a variable.

下面是伪code为几何着色器与工作和放大器的案件;不工作的情况下

Below is pseudo code for geometry shader with cases that work & case that doesn't work

本作品:

    uniform vec2 dimensions[2];
    // some code which computes index which is an int
    float dimX = dimensions[index].x;

本作品:

    uniform vec2 dimensions[];
    // some code which computes index which is an int
    float dimX = dimensions[0].x;

这不工作:

    uniform vec2 dimensions[];
    // some code which computes index which is an int
    float dimX = dimensions[index].x; 

是否有可能做这样的事情?

Is it possible to do something like this?

推荐答案

可悲的是,没有这个是不可能的。你不包括你的目标,但没有提及几何着色器的GLSL版本,所以我已经包括GLSL 1.5规范的相关部分。如下:

Sadly, no this is not possible. You did not include the GLSL version you are targeting but did mention geometry shaders, so I have included the relevant part of the GLSL 1.5 spec. below:

同类型的变量可以通过声明一个名称,后跟括号([])包围一个可选尺寸聚合成阵列。当在一个声明中指定数组大小,它必须是一个整型常量前pression(见第4.3.3节恒前pressions)大于零。 如果将数组索引和前pression这不是一个整型常量前pression,或者如果一个数组作为参数传递给函数,那么它的大小必须在使用之前定义任何这种使用。

Variables of the same type can be aggregated into arrays by declaring a name followed by brackets ( [ ] ) enclosing an optional size. When an array size is specified in a declaration, it must be an integral constant expression (see Section 4.3.3 "Constant Expressions" ) greater than zero. If an array is indexed with an expression that is not an integral constant expression, or if an array is passed as an argument to a function, then its size must be declared before any such use.

尽管桌面GLSL要宽容得多,当谈到与非const前pressions比GLSL ES索引阵列,你仍然有一定的局限性内工作。同样的方式,纹理查找经常被用来克服OpenGL ES的非const数组索引,你可以通过在你的几何着色器使用一维纹理查找来解决这个问题。我想知道,如果你真的需要这个功能的是非常虽然?

While desktop GLSL is much more forgiving when it comes to indexing arrays with non-const expressions than GLSL ES, you still have to work within some limitations. The same way that texture lookups are often used to overcome non-const array indexing in OpenGL ES, you may be able to work around this by using a 1D texture lookup in your geometry shader. I have to wonder if you really need this functionality that badly though?

这是一个好主意,定义一个上限的均匀阵列,无论如何,因为GLSL规范。只需要一个实现提供1024统一的组件(例如1024 浮动 256 vec4 64 mat4 或几何着色器的每个阶段的某种组合)。如果您的阵列在编译时已知的最大尺寸可避免以后的麻烦下来不知不觉中超过这一限制相关的道路。

It is a good idea to define an upper limit to your uniform array anyway, because the GLSL spec. only requires an implementation provide 1024 uniform components (e.g. 1024 float, 256 vec4, 64 mat4 or some combination of each) in the geometry shader stage. If your array has a known maximum size at compile time you can avoid trouble later down the road associated with unknowingly exceeding this limitation.

既然你提到GLSL 4.x的,我想指出,在OpenGL中较新的功能,被称为着色器存储缓冲区对象。使用SSBOs,有可能使用在运行时动态长度数组你的目的。您可以查询使用。长度()着色器中的一个SSBO的长度,以及处理自己范围的验证。不过,我觉得这可能是矫枉过正,但值得一提的仍。

Since you mentioned GLSL 4.x, I would like to point out a newer feature in OpenGL known as Shader Storage Buffer Objects. Using SSBOs, it may be possible to use an array with dynamic length at run-time for your purposes. You can query the length of an SSBO using .length () in the shader, and handle range validation yourself. However, I think this is probably overkill but worth mentioning nevertheless.

这篇关于GLSL索引成均匀阵列与可变长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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