I am trying to create a white 20 x 20 pixel square on the screen by providing an array of rgba values, creating a ID3D11Texture2D resource with that array, and then mapping it to my 20 x 20 square.
Here is the code for creating the square texture, and the Shader Resource View:
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);
}
And here is my pixel shader:
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;
}
But, my square appears all black. I've tried this with a normal .dds texture from File, and it worked, but why can't I programmatically create my own texture? What am I doing wrong?
Your texture array doesn't match the format you specified.
DXGI_FORMAT_R8G8B8A8_UNORM A four-component, 32-bit unsigned-normalized-integer format that supports 8 bits per channel including alpha.
You are passing in four floats for each channel for texArray (try unsigned char instead)