簡體   English   中英

着色器將 uint8 轉換為浮動,並將其重新解釋回 uint

[英]Shaders casting uint8 to float, and reinterpretting it back to uint

我有一個頂點屬性被我的着色器非常奇怪地咀嚼。 它作為(uint8)1上傳到 VBO,但是當片段着色器看到它時,它被解釋為106535321600x3F800000 ,你們中的一些人可能會將其識別為浮點1.0f的位模式。

我不知道為什么? 我可以確認它以 1 ( 0x00000001 ) 的形式上傳到 VBO。

頂點屬性定義為:

struct Vertex{
    ...
    glm::u8vec4 c2; // attribute with problems
};
// not-normalized
glVertexAttribPointer(aColor2, 4, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, c2));

雖然着色器具有綁定的屬性

glBindAttribLocation(programID, aColor2, "c2");

頂點着色器非常順利地傳遞屬性:

#version 330
in lowp uvec4 c2; // <-- this value is uploaded to the VBO as 0x00, 0x00, 0x00, 0x01;
flat out lowp uvec4 indices;

void main(){
    indices = c2;
}

最后片段着色器得到了它:

flat in lowp uvec4 indices; // <-- this value is now 0, 0, 0, 0x3F800000
out lowp vec4 fragColor;
void main(){
    fragColor = vec4(indices) / 256.0;
}

根據我的着色器檢查器, indices的變化使頂點着色器為0x3F800000indices.w ,所以那里發生了一些奇怪的事情? 這可能是什么原因造成的?

如果頂點屬性的類型是不可分割的,那么您必須使用glVertexAttribIPointer而不是glVertexAttribPointer (關注I )。 請參閱glVertexAttribPointer

glVertexAttribPointer中指定的類型是源緩沖區中數據的類型,並沒有指定着色器中的目標屬性類型。 如果使用glVertexAttribPointer ,則假定着色器程序中的屬性類型為浮點數,並轉換整數數據。
如果您使用glVertexAttribIPointer ,則值保留為 integer 值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM