[英]How much data does texImage2D in WebGL need?
I created a DataTexture in ThreeJS, which in turn will call texImage2D. 我在ThreeJS中创建了一个DataTexture,然后调用texImage2D。
var twidth = 50;
var theight = 50;
var tsize = twidth * theight;
var tileData = new Uint8Array( tsize * 3);
//fill it with some data
tileDataTexture = new THREE.DataTexture(this.tileData, twidth, theight, THREE.RGBFormat);
As you can see, I used a size of 50x50 Texels and three 8Bit channels (THREE.RGB in threejs). 正如你所看到的,我使用了50x50 Texels和3个8Bit通道的大小(三个中的THREE.RGB)。 When I used a UInt8Array of size 7500 (50*50*3), Firefox tells me, that it needs more data:
当我使用大小为7500(50 * 50 * 3)的UInt8Array时,Firefox告诉我,它需要更多数据:
Error: WebGL: texImage2D: not enough data for operation (need 7598, have 7500)
I would like to know: where do this extra 98 bytes come from? 我想知道:这98个字节来自哪里? I would guess alignment, but 7598 is not even divisible by 4 while 7500 is.
我猜测对齐,但7598甚至不能被4整除,而7500则是。 (Now that I think of it, it is not divisible by 3, my number of channels, either)
(现在我想起来了,它不能被3整除,我的通道数也是如此)
7600 would make sense, since that would mean 2 Bytes of padding per row, is the last row not padded? 7600是有意义的,因为这意味着每行2个字节的填充,是最后一行没有填充?
(I get that one should use only multiples of four for the dimensions, still I would like to know the answer) (我觉得那个尺寸应该只使用四个的倍数,我仍然想知道答案)
The row lengths are aligned to multiples of 4, except for the last row. 除最后一行外,行长度与4的倍数对齐。 In your case this means each row needs 2 bytes of padding, for a total of 152 bytes (
3 * 50 + 2
) per row. 在您的情况下,这意味着每行需要2个字节的填充,每行总共152个字节(
3 * 50 + 2
)。
152 * (50 - 1) + 3 * 50 = 7598
For reference, see the source code of Gecko . 供参考,请参阅Gecko的源代码 。
As user1421750 pointed out the rows are padded 正如user1421750指出的那样,行被填充
But, you can set how much padding per row by setting texture.unpackAlignment
. 但是,您可以通过设置
texture.unpackAlignment
来设置每行填充多少。 it defaults to 4 but you can set it to 8, 4, 2 or 1. 它默认为4,但您可以将其设置为8,4,2或1。
Personally I would have made the default 1 because I think you'd be less likely to be surprised like you were 就个人而言,我会默认1,因为我觉得你不太可能像你一样感到惊讶
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.