4
我試圖通過提供一個rgba值的數組,創建一個ID3D11Texture2D資源與該數組,然後將其映射到我的20 x 20正方形在屏幕上創建一個白色20 x 20像素的正方形。以編程方式創建紋理DirectX
下面是創建方形紋理的代碼,並且着色器資源視圖:
void Square::CreateSquareTexture(float *color)
{
float *texArray = (float *)malloc(4 * 20 * 20 * sizeof(float));
for (int i = 0; i < 20 * 20 * 4; i++)
texArray[i] = 1.0f;
ID3D11Texture2D *boxTex = 0;
D3D11_TEXTURE2D_DESC boxTexDesc;
ZeroMemory(&boxTexDesc, sizeof(D3D11_TEXTURE2D_DESC));
boxTexDesc.ArraySize = 1;
boxTexDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
boxTexDesc.CPUAccessFlags = 0;
boxTexDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
boxTexDesc.Height = 20;
boxTexDesc.MipLevels = 1;
boxTexDesc.MiscFlags = 0;
boxTexDesc.SampleDesc.Count = 4;
boxTexDesc.SampleDesc.Quality = m_4xMsaaQuality - 1;
boxTexDesc.Usage = D3D11_USAGE_DEFAULT;
boxTexDesc.Width = 20;
D3D11_SUBRESOURCE_DATA boxTexInitData;
ZeroMemory(&boxTexInitData, sizeof(D3D11_SUBRESOURCE_DATA));
boxTexInitData.pSysMem = texArray;
m_d3dDevice->CreateTexture2D(&boxTexDesc, &boxTexInitData, &boxTex);
m_d3dDevice->CreateShaderResourceView(boxTex, NULL, &m_d3dSquareSRV);
}
這裏是我的像素着色器:
Texture2D squareMap : register(t0);
SamplerState samLinear : register(s0);
struct PixelIn
{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD;
};
float4 main(PixelIn pin) : SV_TARGET
{
float4 texColor = squareMap.Sample(samLinear, pin.Tex);
return texColor;
}
但是,我方出現全黑。我已經用File的正常.dds紋理嘗試過了,它能工作,但爲什麼我不能以編程方式創建自己的紋理?我究竟做錯了什麼?