Question

I am trying to set a chess board texture with green and blue texels for a square. Instead my square is black. I don't get any compile or runtime errors. Is there any problem with my texture code?

    vector<unsigned char> texelsBuffer;

for(int i = 0; i < N; ++i)
    for(int j = 0; j < N; ++j)
    {
        texelsBuffer.push_back(0.0f);

        if((i+j) % 2 == 0) {
            texelsBuffer.push_back(1.0f);
            texelsBuffer.push_back(0.0f);
        }
        else {
            texelsBuffer.push_back(0.0f);
            texelsBuffer.push_back(1.0f);
        }
        texelsBuffer.push_back(1.0f);
    }

ID3D11Texture2D* tex = 0;

D3D11_TEXTURE2D_DESC textureDescr;
ZeroMemory(&textureDescr, sizeof(D3D11_TEXTURE2D_DESC));
textureDescr.ArraySize = 1;
textureDescr.BindFlags = D3D11_BIND_SHADER_RESOURCE;
textureDescr.CPUAccessFlags = 0;
textureDescr.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
textureDescr.Height = N;
textureDescr.Width = N;
textureDescr.MipLevels = 1;
textureDescr.MiscFlags = 0;
textureDescr.SampleDesc.Count = 1;
textureDescr.SampleDesc.Quality = 0;
textureDescr.Usage = D3D11_USAGE_IMMUTABLE;

D3D11_SUBRESOURCE_DATA texInitData;
ZeroMemory(&texInitData, sizeof(D3D11_SUBRESOURCE_DATA));
texInitData.pSysMem = (void*)&texelsBuffer[0];
texInitData.SysMemPitch = sizeof(unsigned char) * N;
texInitData.SysMemSlicePitch = 0;

g_pd3dDevice->CreateTexture2D(&textureDescr, &texInitData, &tex);
g_pd3dDevice->CreateShaderResourceView(tex, NULL, &g_pTextureRV);

Or my sampler code?

D3D11_SAMPLER_DESC sampDesc;
ZeroMemory( &sampDesc, sizeof(sampDesc) );
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
sampDesc.MinLOD = 0;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX;

g_pd3dDevice->CreateSamplerState(&sampDesc, &g_pSamplerLinear);
Was it helpful?

Solution

You're populating texelsBuffer with floating-point values. Use 255 or 0xFF to set the channel to 1.0 in UNORM encoding.

You should also consider making your chess squares larger than one texel; unless you are always going to be rendering the texture at a 1:1 pixel:texel ratio, you will end up blending between the two colors for most sample locations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top