1

I have a BYTE buffer of a 32 bit red colored simple texture. I want to create a texture in DirectX 11 where target texture format is DXGI_FORMAT_R32_FLOAT. I was trying with device context Map() function but while saving the resource to dds file it shows black. Would someone give me a guideline how can I write a simple 32 bit red texture in DirectX 11 ?

1 Answers1

0

If you want a 1x1 texture in the DXGI_FORMAT_R32_FLOAT format with a red pixel, you can just create it in code:

    using Microsoft::WRL::ComPtr;

    static const float s_pixel = 1.f;

    D3D11_SUBRESOURCE_DATA initData = { &s_pixel, sizeof(float), 0 };

    D3D11_TEXTURE2D_DESC desc = {};
    desc.Width = desc.Height = desc.MipLevels = desc.ArraySize = 1;
    desc.Format = DXGI_FORMAT_R32_FLOAT;
    desc.SampleDesc.Count = 1;
    desc.Usage = D3D11_USAGE_IMMUTABLE;
    desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;

    ComPtr<ID3D11Texture2D> tex;
    HRESULT hr = mDevice->CreateTexture2D(&desc, &initData, tex.GetAddressOf());

    if (SUCCEEDED(hr))
    {
        D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {};
        SRVDesc.Format = desc.Format;
        SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
        SRVDesc.Texture2D.MipLevels = 1;

        hr = mDevice->CreateShaderResourceView(tex.Get(), &SRVDesc, pResult);
    }

    return hr;

The reason for the 'black texture' has already been addressed here. You are using the wrong kind of texture and map flags.

Chuck Walbourn
  • 38,259
  • 2
  • 58
  • 81