我试图通过提供一个 rgba 值数组,使用该数组创建一个 ID3D11Texture2D 资源,然后将其映射到我的 20 x 20 正方形,在屏幕上创建一个白色的 20 x 20 像素正方形。
这是创建方形纹理和着色器资源视图的代码:
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 纹理进行此操作,并且它有效,但为什么我不能以编程方式创建自己的纹理?我究竟做错了什么?