1 year ago

#378467

test-img

SexyDOT

The image does not load (DirectX11)

How do I solve this? I think there is a problem with the part that binds to the pipeline. It was confirmed that the bitmap was loaded.I handed over the buffer containing pixel information to pSysmem, checked DXGI_FORMAT, and wrote the shader correctly (probably). I also checked the register number.

LoadBitmap


    Gdiplus::Bitmap bitmap(path.c_str());
    if (bitmap.GetLastStatus() != Gdiplus::Status::Ok)
    {
        MessageBox(nullptr, "Load Image Failed", "Failed", MB_OK);
    }

    _width = bitmap.GetWidth();
    _height = bitmap.GetHeight();
    _buffer = std::make_unique<Color[]>(_width * _height);

    for (UINT y = 0; y < _height; ++y)
    {
        for (UINT x = 0; x < _width; ++x)
        {
            Gdiplus::Color c;
            bitmap.GetPixel(x, y, &c);
            _buffer[y * _width + x] = c.GetValue();
        }
    }

CreateTexture

    D3D11_TEXTURE2D_DESC tex2DDesc = {};
    tex2DDesc.Width = _width;
    tex2DDesc.Height = _height;
    tex2DDesc.MipLevels = 1;
    tex2DDesc.ArraySize = 1;
    tex2DDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
    tex2DDesc.SampleDesc.Count = 1;
    tex2DDesc.SampleDesc.Quality = 0;
    tex2DDesc.Usage = D3D11_USAGE_DEFAULT;
    tex2DDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
    tex2DDesc.CPUAccessFlags = 0;
    tex2DDesc.MiscFlags = 0;
    D3D11_SUBRESOURCE_DATA sd = {};
    sd.pSysMem = _buffer.get();
    sd.SysMemPitch = _width * sizeof(Texture::Color);
    DEVICE->CreateTexture2D(&tex2DDesc, &sd, _tex2D.GetAddressOf());

    D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
    srvDesc.Format = tex2DDesc.Format;
    srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
    srvDesc.Texture2D.MostDetailedMip = 0;
    srvDesc.Texture2D.MipLevels = 1;
    DEVICE->CreateShaderResourceView(_tex2D.Get(), &srvDesc, _srv.GetAddressOf());
    CONTEXT->PSSetShaderResources(0u, 1u, _srv.GetAddressOf());

CreateSamplerState

    D3D11_SAMPLER_DESC samplerDesc = {};
    samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
    samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
    samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
    samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;

    DEVICE->CreateSamplerState(&samplerDesc, _samplerState.GetAddressOf());
    CONTEXT->PSSetSamplers(0u, 1u, _samplerState.GetAddressOf());

inputLayout

    D3D11_INPUT_ELEMENT_DESC desc[] =
    {
        { "Position",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
        { "TexCoord", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0}
    };

    DEVICE->CreateInputLayout(desc, std::size(desc), _vsBlob->GetBufferPointer(), _vsBlob->GetBufferSize(), _inputLayout.GetAddressOf());

VertexShader

cbuffer CBuf
{
    matrix transform;
};

struct VSOut
{
    float4 pos : SV_Position;
    float2 uv : TEXCOORD;
};

VSOut main(float3 pos : POSITION, float2 uv : TEXCOORD)
{
    VSOut vso;
    vso.pos = mul(float4(pos, 1.f), transform);
    vso.uv = uv;
    return vso;
}

PixelShader

Texture2D texture_0 : register(t0);
SamplerState sample_0 : register(s0);

float4 main(float2 uv : TEXCOORD) : SV_Target
{
    float4 color = texture_0.Sample(sample_0, uv);
    return color;
}

c++

directx

directx-11

directx-9

directx-12

0 Answers

Your Answer

Accepted video resources