2015-06-10 27 views
1

我在創建和填充DirectX11中的Texture2DArray時遇到了一些問題。創建Texture2DArray並用固定值填充它

我想在屏幕上渲染很多四邊形,繪圖是通過實例完成的,但是每個四邊形都應該有他自己的紋理。所以我嘗試創建一個Texture Array併爲實例數據格式添加一個索引值。

D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[4].SysMemPitch cannot be 0 [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA] 
D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[6].pSysMem cannot be NULL. [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA] 
D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[7].pSysMem cannot be NULL. [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA] 
D3D11 ERROR: ID3D11Device::CreateTexture2D: Returning E_INVALIDARG, meaning invalid parameters were passed. [ STATE_CREATION ERROR #104: CREATETEXTURE2D_INVALIDARG_RETURN] 

這裏是我使用的代碼:

的問題是,當我嘗試創建兩個不同的紋理入陣中,當我嘗試調用該函數CreateTexture2D並出現以下錯誤失敗生成紋理:

//Create array for two textures 
const int size = 256 * 256 * 4; 
unsigned char color[size*2]; 

//First Texture white 
for (int i = 0; i < size; i++) { 
    color[i] = 255; 
} 

//Second Texture black 
for (int i = size; i < size*2; i++) { 
    color[i] = 0; 
} 


//Creating Texture2D description 
D3D11_TEXTURE2D_DESC desc; 
ZeroMemory(&desc, sizeof(D3D11_TEXTURE2D_DESC)); 
desc.Width = 256; 
desc.Height = 256; 
desc.MipLevels = 1; 
desc.ArraySize = 2; 
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; 
desc.SampleDesc.Count = 1; 
desc.SampleDesc.Quality = 0; 
desc.Usage = D3D11_USAGE_DEFAULT; 
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; 
desc.CPUAccessFlags = 0; 
desc.MiscFlags = 0; 

ID3D11Texture2D *pTexture; 
D3D11_SUBRESOURCE_DATA texture[2]; 
//ZeroMemory(&texture, sizeof(D3D11_SUBRESOURCE_DATA)); 

//Defining the texture start points 
texture[0].pSysMem = color; 
texture[0].SysMemPitch = sizeof(unsigned char) * 4; 
texture[0].SysMemSlicePitch = 0; 
texture[1].pSysMem = &color[size]; 
texture[1].SysMemPitch = sizeof(unsigned char) * 4; 
texture[1].SysMemSlicePitch = 0; 
result = device->CreateTexture2D(&desc, texture, &pTexture); 

我不知道爲什麼在陣列位置的錯誤狀態,我在沒有明確的點,我只有兩個尺寸的紋理數組。 也許我在做初始化或填充數據時出錯。

我希望有人能幫助我。

回答

1

好吧,我現在修改了一些東西,函數CreateTexture2D的調用返回S_OK,但始終存在另一個問題。函數CreateShaderResourceView也返回S_OK,但結果是我的ShaderRessourceView數組的第一個索引是有效的,第二個已損壞。

在這裏,固定碼:

//Defining the texture start points 
texture[0].pSysMem = black; 
texture[0].SysMemPitch = sizeof(unsigned char) * 4; 
texture[0].SysMemSlicePitch = 256 * 256 * 4; 
texture[1].pSysMem = &black[size]; 
texture[1].SysMemPitch = sizeof(unsigned char) * 4; 
texture[1].SysMemSlicePitch = 256*256 *4; 
result = device->CreateTexture2D(&desc, texture, &pTexture); 

和代碼,其帶來的問題:

D3D11_SHADER_RESOURCE_VIEW_DESC srDesc; 
srDesc.Format = desc.Format; 
srDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY; 
srDesc.Texture2DArray.MostDetailedMip = 0; 
srDesc.Texture2DArray.MipLevels = 1; 
srDesc.Texture2DArray.ArraySize = 2; 
srDesc.Texture2DArray.FirstArraySlice = 0; 

ID3D11ShaderResourceView *pShaderResView[2]; 
result = device->CreateShaderResourceView(pTexture, &srDesc, pShaderResView); 

它調用PSSetShaderResources(0,2,pShaderResView)時生成以下錯誤;

D3D11 CORRUPTION: ID3D11DeviceContext::PSSetShaderResources: Third parameter, array index 1 corrupt or unexpectedly NULL. [ MISCELLANEOUS CORRUPTION #15: CORRUPTED_PARAMETER3] 

我希望初始化的Texture2D和Subressource_Data正確的,我有一些問題了解SysMemPitch和SysMemSlicePitch。

但現在我不知道爲什麼第二個索引損壞。

1

CreateShaderResourceView只初始化一個ID3D11ShaderResourceView *,你不應該給它一個兩個指針的數組。

您已經創建了一個尺寸爲256x256x2的紋理,並創建了一個可以查看兩個切片的SRV。當你來調用PSSetShaderResources時,你只需要設置一個SRV。

刪除第二個SRV並且只設置一個,因爲您已經查看了這兩個切片。