【发布时间】:2016-04-11 15:35:45
【问题描述】:
现在,当读取 Internet 中的不同资源时,如果您要按顺序处理大型数组,数组结构似乎是一种非常高效的数据存储方式。
例如在 C++ 中
struct CoordFrames
{
float* x_pos;
float* y_pos;
float* z_pos;
float* scaleFactor;
float* x_quat;
float* y_quat;
float* z_quat;
float* w_quat;
};
允许比数组更快地处理大型数组(感谢 SIMD)
struct CoordFrame
{
glm::vec3 position;
float scaleFactor;
glm::quat quaternion;
};
GPU 是为大规模并行计算而设计的处理器。 SIMD 是这里的“必备”。所以结论是数组结构在这里最有用。
但是……
-
我从来没有在任何地方看到过这样的 GLSL 着色器(我只是觉得不对劲):
#define NUM_POINT_LIGHTS 16 uniform float point_light_x[NUM_POINT_LIGHTS]; uniform float point_light_y[NUM_POINT_LIGHTS]; uniform float point_light_z[NUM_POINT_LIGHTS]; uniform float point_light_radius[NUM_POINT_LIGHTS]; uniform float point_light_color_r[NUM_POINT_LIGHTS]; uniform float point_light_color_g[NUM_POINT_LIGHTS]; uniform float point_light_color_b[NUM_POINT_LIGHTS]; uniform float point_light_power[NUM_POINT_LIGHTS];或类似的东西也很少见:
#define NUM_POINT_LIGHTS 16 uniform vec3 point_light_pos[NUM_POINT_LIGHTS]; uniform float point_light_radius[NUM_POINT_LIGHTS]; uniform vec3 point_light_color[NUM_POINT_LIGHTS]; uniform float point_light_power[NUM_POINT_LIGHTS];每个人,包括我在内,似乎都更喜欢这样写 GLSL:
#define NUM_POINT_LIGHTS 16 struct PointLight { vec3 origin; float radius; vec3 color; float power; }; uniform PointLight pointLights[NUM_POINT_LIGHTS]; -
另外,在阅读原始 OpenGL Wiki 关于顶点数组数据时,我突然想知道,interleaved data should be preferred:
作为一般规则,您应该尽可能使用交错属性。
问题:在 GLSL 着色器中尝试使用数组结构有意义吗?
什么是真的? GPU 是否针对我们喜欢编写着色器的方式进行了高度优化,但实际上并没有任何区别?
【问题讨论】:
标签: arrays performance glsl gpu data-oriented-design