【发布时间】:2014-10-11 15:23:24
【问题描述】:
我正在尝试将数据从两个数组和两个变量复制到一个字节缓冲区。字节缓冲区将在片段着色器中为统一的块结构保存这些数据。我可以很好地复制第一个,但第二个总是会产生索引超出范围错误。
我尝试过使用 .asFloatBuffer,尝试将缓冲区初始化为所需大小的两倍,并尝试使用 FloatBuffer(我需要一个 ByteBuffer,但我想我会尝试修复此错误,然后按我的方式返回)
片段着色器的结构:
layout (binding = 0) uniform BlobSettings {
vec4 InnerColor;
vec4 OuterColor;
float RadiusInner;
float RadiusOuter;
};
这就是我现在的代码(有点乱,但你明白了......):
//create a buffer for the data
//blockB.get(0) contains the 'size' of the data structure I need to copy (value is 48)
//FloatBuffer blockBuffer = BufferUtil.newFloatBuffer(blockB.get(0));
ByteBuffer blockBuffer = ByteBuffer.allocateDirect(blockB.get(0) * 4);//.asFloatBuffer();
//the following data will be copied to the buffer
float outerColor[] = {0.1f,0.1f,0.1f,0.1f};
float innerColor[] = {1.0f,1.0f,0.75f,1.0f};
float innerRadius = 0.25f;
float outerRadius = 0.45f;
//copy data to buffer at appropriate offsets
//params contains the offsets (0, 16, 32, 36)
//following 4 lines using a FloatBuffer (maybe convert to ByteBuffer after loading?)
blockBuffer.put(outerColor, params.get(0), outerColor.length);
blockBuffer.put(innerColor, params.get(1), innerColor.length); //idx out of range here...
blockBuffer.put(params.get(2), innerRadius);
blockBuffer.put(params.get(3), outerRadius);
//when using ByteBuffer directly - maybe something like the following?
for (int idx=0;idx<4;idx++){
blockBuffer.putFloat(params.get(0) + idx, outerColor[idx]) //????
}
谁能告诉我如何正确地将这些数据放入 ByteBuffer 中?
【问题讨论】:
-
这是标准的 Java
ByteBuffer吗?我在ByteBuffer文档中找不到将float[]作为第一个参数的put()方法。 -
是的,.put 来自我尝试使用浮动缓冲区的尝试 - 例如。 ByteBuffer.allocateDirect(blockB.get(0) * 4).asFloatBuffer();但是,我需要将数据放在 ByteBuffer 中。在 Bytebuffer 上,我查看了 blockBuffer.putFloat(index, value) ,我可以在其中迭代并插入数据,但我不完全了解如何正确地将浮点数据放入 Bytebuffer 中......
-
我已经编辑了我的问题,以显示(部分)我在 FloatBuffer 或 ByteBuffer 上复制数据的尝试。