2016-08-03 72 views
1

我正在使用DirectX 12,試圖使用無人機進行渲染。這裏是我的像素着色器代碼:爲什麼在引用RWStructuredBuffer時不會編譯HLSL像素着色器?

struct PSInput 
{ 
    float4 position : SV_POSITION; 
    float4 color : COLOR; 
}; 

struct FragmentDataStruct 
{ 
    float4 color; 
    float depth; 
}; 

struct FragmentAndLinkStruct 
{ 
    FragmentDataStruct fragmentData; 
    uint nextFragment; 
}; 

RWStructuredBuffer <FragmentAndLinkStruct> FLBuffer : register(u0); 

RWByteAddressBuffer StartOffsetBuffer : register(u1); 

float4 PSMain(PSInput input) : SV_TARGET 
{ 
    input.color.x = float(FLBuffer[0].nextFragment); 

    return input.color; 
} 

它使用D3DCompileFromFile功能時失敗編譯。

當我替換此行:

input.color.x = float(FLBuffer[0].nextFragment); 

像這樣的東西:

FragmentAndLinkStruct otherThing; 
otherThing.nextFragment = 1; 
input.color.x = float(otherThing.nextFragment); 

有沒有提到RWStructuredBuffer的,然後它編譯就好了,並呈現正常。

我不認爲我有任何綁定數據的問題(VS圖形調試器顯示兩個無人機正確綁定)。不過,我認爲這不會影響着色器的編譯。

任何時候我參考FLBuffer或​​,它不會編譯。

什麼會導致此問題?

回答

0

我在弄清楚如何讀取編譯器給出的錯誤後解決了這個問題。這裏的問題是渲染目標輸出使用着色器寄存器u0

爲了找到這一點,我通過使用代碼將其轉換爲一個char*陣列的下方,其中errorID3DBlob*讀出從D3DCompileFromFile本身給出作爲ID3DBlob*錯誤:

char* error2; 
int size = error->GetBufferSize(); 
error2 = new char[size/4]; 
error2 = static_cast<char*> (error->GetBufferPointer()); 

一旦我轉換LPVOID指針char*陣列,我的最後一行之後立即設置斷點和簡單地讀出從當地人標籤在VS錯誤消息:

error X4509: UAV registers live in the same name space as outputs, so they 
must be bound to at least u1, manual bind to slot u0 failed 

錯誤消息非常清晰和簡潔。很有幫助。

更新的代碼:

struct PSInput 
{ 
    float4 position : SV_POSITION; 
    float4 color : COLOR; 
}; 

struct FragmentDataStruct 
{ 
    float4 color; 
    float depth; 
}; 

struct FragmentAndLinkStruct 
{ 
    FragmentDataStruct fragmentData; 
    uint nextFragment; 
}; 

RWStructuredBuffer <FragmentAndLinkStruct> FLBuffer : register(u1); 

RWByteAddressBuffer StartOffsetBuffer : register(u2); 

float4 PSMain(PSInput input) : SV_TARGET 
{ 
    uint pixelCount = FLBuffer.IncrementCounter(); 
    input.color.x = float(pixelCount); 

    return input.color; 
} 

作爲一個說明,編譯器將連看都不看代碼,不會有助於return輸出。我通過在與FLBuffer交互的PSMain函數中添加一些代碼,而不影響返回的input.color來測試此操作。看着反彙編,所有多餘的代碼被刪除,不編譯。

另外,由於在工作時我沒有參考FLBuffer,它沒有在輸出着色器寄存器和我綁定的着色器寄存器之間建立任何連接,所以沒有出現問題。

然後,我必須假設它在健全性檢查代碼之前擺脫了多餘的代碼,這在尋找效率時很有意義。一旦它進行了健全性檢查,然後繼續進行編譯。

希望這可以幫助有人在以後學習DirectX或HLSL。