Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DirectX 11 mipmaps

Tags:

directx-11

How can i create texture mipmaps in DirectX? This is my code, in which i tried do this, but it doesn't work:

    D3D11_TEXTURE2D_DESC desc{};
    desc.Width = dims.X;!
    desc.Height = dims.Y;
    desc.ArraySize = 1;
    desc.SampleDesc.Count = 1;
    desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
    desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
    desc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;

    D3D11_SUBRESOURCE_DATA initData{};
    initData.pSysMem = pixels;
    initData.SysMemPitch = sizeof(unsigned char) * dims.X * 4;

    D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc{};
    srvDesc.Format = desc.Format;
    srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
    srvDesc.Texture2D.MipLevels = 1;

    Device->CreateTexture2D(&desc, nullptr, Texture.GetAddressOf());
    Device->CreateShaderResourceView(Texture.Get(), &srvDesc, ShaderResource.GetAddressOf());
    DeviceContext->UpdateSubresource(Texture.Get(), 0, 0, pixels, initData.SysMemPitch, 0);
    DeviceContext->GenerateMips(ShaderResource.Get());

Texture now looks like this

like image 254
ly000 Avatar asked Oct 20 '25 14:10

ly000


1 Answers

Ok, i changed srvDesc.Texture2D.MipLevels to -1 and now it works. Thanks.

like image 180
ly000 Avatar answered Oct 26 '25 18:10

ly000



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!