简体   繁体   English

创建一个Texture2DArray并用实体值填充

[英]Creating a Texture2DArray and populate it with solid values

I have some problems in creating and filling a Texture2DArray in DirectX11. 我在DirectX11中创建和填充Texture2DArray时遇到一些问题。

I want to render many quads onto the screen, the drawing is done by instancing, but every quad should get his own texture. 我想在屏幕上渲染许多四边形,通过实例化完成绘制,但是每个四边形都应具有自己的纹理。 So I tried to create a Texture Array and added a index value to the instances data format. 因此,我尝试创建一个纹理数组并将索引值添加到实例数据格式。

The problem is, when I try to create two different textures into the array, it fails when I try to call the function CreateTexture2D with the following error: 问题是,当我尝试在数组中创建两个不同的纹理时,当我尝试使用以下错误调用函数CreateTexture2D时,它失败了:

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]

Here is the code which I use to generate the texture: 这是我用来生成纹理的代码:

//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);

I have no idea why the error states at array positions I have at no point defined, I have only a two sized texture array. 我不知道为什么我在没有定义点的数组位置处的错误状态,我只有两个大小的纹理数组。 Maybe I'm doing something wrong with the initialization or filling of the data. 也许我在初始化或填充数据时做错了。

I hope someone can help me. 我希望有一个人可以帮助我。

Ok I fixed something now the call of the function CreateTexture2D returns S_OK, but as always another problem occur. 好的,我现在修复了一些问题,对函数CreateTexture2D的调用返回了S_OK,但总是出现另一个问题。 The function CreateShaderResourceView returns also S_OK, but the result is that the first index of my ShaderRessourceView array is valid and the second is corrupt. 函数CreateShaderResourceView也返回S_OK,但是结果是我的ShaderRessourceView数组的第一个索引有效,而第二个索引已损坏。

Here the fixed code: 这里是固定代码:

//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);

And the code which brings the problem: 和带来问题的代码:

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);

It generates the following error when calling PSSetShaderResources(0, 2, pShaderResView); 调用PSSetShaderResources(0,2,pShaderResView)时,它将生成以下错误。

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

I hope I initialized the Texture2D and Subressource_Data correctly, I had some problems understand the SysMemPitch and SysMemSlicePitch. 我希望我正确地初始化了Texture2D和Subressource_Data,我在理解SysMemPitch和SysMemSlicePitch时遇到了一些问题。

But for now I have no idea why the second index is corrupted. 但是现在我不知道为什么第二个索引被破坏了。

CreateShaderResourceView only initialises one ID3D11ShaderResourceView*, you shouldn't be giving it an array of two pointers. CreateShaderResourceView仅初始化一个ID3D11ShaderResourceView *,您不应为其提供两个指针的数组。

You've created one texture of dimensions 256x256x2 and have created one SRV that views both slices. 您已经创建了一个尺寸为256x256x2的纹理,并创建了一个可以查看两个切片的SRV。 When you come to call PSSetShaderResources, you only need to set one SRV. 调用PSSetShaderResources时,只需设置一个SRV。

Remove the second SRV and only set one since the one you have already views both slices. 删除第二个SRV,仅设置一个,因为您已经查看了两个切片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM