1
\$\begingroup\$

I'm here with another question about direct 3d 11 (sharpdx). My terrain engine is working good, but theres a problem with texture mip maps or filter which I can't figure out :/

The problem is basic. Near, the textures look OK:

enter image description here

Far away they're kinda ugly?

enter image description here

Loading it:

 internal Texture2D CreateTexture2DFromBitmap(BitmapSource bitmapSource)
       {
            int Byte = bitmapSource.Size.Width * 4;
            using (var buffer = new DataStream(bitmapSource.Size.Height * Byte, true, true))
            {
                bitmapSource.CopyPixels(Byte, buffer);
                var tex = new Texture2D(device, new Texture2DDescription()
                {
                    Width = bitmapSource.Size.Width,
                    Height = bitmapSource.Size.Height,
                    ArraySize = 1,
                    BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                    Usage = ResourceUsage.Default,
                    CpuAccessFlags = CpuAccessFlags.None,
                    Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm_SRgb,
                    MipLevels = 1,
                    OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.Shared,
                    SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0)
                },
                new DataRectangle(buffer.DataPointer, Byte));
                return tex;
            }
       }

Rendering it:

 public override void Create(long assetID, Importeur content, DeviceContext context)
        {
            try
            {
                _diffuseTexture = content.LoadTexture(assetID, TextureType.DiffuseMap);
                context.GenerateMips(_diffuseTexture.MapView);
                context.PixelShader.SetShaderResource(0, _diffuseTexture.MapView);
            }

            catch (Exception e)
            {
                throw e;
            }
        }

Filter:

var samplerStateDescription = new SamplerStateDescription
            {
                AddressU = TextureAddressMode.Wrap,
                AddressV = TextureAddressMode.Wrap,
                AddressW = TextureAddressMode.Wrap,
                Filter = Filter.Anisotropic,
                MaximumLod = float.MaxValue,
                MinimumLod = 0
            };

Finally got it working, the problem was a thread block code line from my engine.

Screenshot

Thanks for helping!

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

ID3D11DeviceContext::GenerateMips method

If the base resource wasn't created with D3D11_BIND_RENDER_TARGET, D3D11_BIND_SHADER_RESOURCE, and D3D11_RESOURCE_MISC_GENERATE_MIPS, the call to GenerateMips has no effect.

However, if we also look at the documentation for the D3D11_RESOURCE_MISC_FLAG enumeration we see the following remark:

D3D11_RESOURCE_MISC_SHARED
Enables resource data sharing between two or more Direct3D devices. The only resources that can be shared are 2D non-mipmapped textures.

So you basically have two problems:

  1. You're not setting D3D11_RESOURCE_MISC_GENERATE_MIPS, so you can't use GenerateMips, but,
  2. You are setting D3D11_RESOURCE_MISC_SHARED which is illegal to use with mipmapped textures.

To resolve, and assuming that you don't actually need D3D11_RESOURCE_MISC_SHARED (it's not clear why you're setting it), you need to replace this:

OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.Shared

With the SharpDX equivalent of D3D11_RESOURCE_MISC_GENERATE_MIPS; probably something like this:

OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.GenerateMips

(Unfortunately the SharpDX documentation is a bit lacking in this regard, but you should be able to figure it out.)

If however you do need D3D11_RESOURCE_MISC_SHARED then you're out of luck.

\$\endgroup\$
8
  • \$\begingroup\$ Hello, thank you for your answer. I have set the OptionFlags to this now (see above) but it still throws the wrong parameter error :/ \$\endgroup\$ Commented Apr 14, 2018 at 20:26
  • \$\begingroup\$ msdn.microsoft.com/en-us/library/windows/desktop/… D3D11_RESOURCE_MISC_SHARED Enables resource data sharing between two or more Direct3D devices. The only resources that can be shared are 2D non-mipmapped textures. Any reason why you're setting this flag? \$\endgroup\$ Commented Apr 14, 2018 at 20:38
  • \$\begingroup\$ Yeah i was setting it resource sharing - for multiscreen engine views - like u said. But even if i removed it now, it still throws that error! It just wont work :(( \$\endgroup\$ Commented Apr 14, 2018 at 20:44
  • \$\begingroup\$ Well lets just forget the multlple resource sharing. But with the config above it should work - but still: Wrong Parameter!!! This is making me crazy :D \$\endgroup\$ Commented Apr 14, 2018 at 20:50
  • \$\begingroup\$ Well i didnt fixed it yet - but i will accept ur answer cause it helped me alot! I think i will find the solution :) Thank u very much for ur help! \$\endgroup\$ Commented Apr 14, 2018 at 21:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .