Mip mapping not working?
Hi, I noticed that in the tutorials, specifically the series 2 terrain tutorials, that mip mapping is not working, all the terrain textures look grainy due to aliasing. How would you go about fixing this issue?
So I was able to solve the textures not mip mapping, use the answer in this post: https://gamedev.stackexchange.com/questions/158962/grainy-texture-from-distance/159005#159005
in DDX11.cs, change the type of device to Device3, then on line 78 change it to
SharpDX.Direct3D11.Device3.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, swapChainDesc, out device, out swapChain);
add a new line under it with
SharpDX.Direct3D11.Device3 device3 = new SharpDX.Direct3D11.Device3(device.NativePointer);
then the next line that says Device = device, change it to
Device = device3
Then in DTexture.cs, here is what I used to get it working:
using System;
using SharpDX.Direct3D11;
using SharpDX.WIC;
using SharpDX;
namespace DSharpDXRastertek.Series2.TutTerr03.Graphics.Models
{
public class DTexture
{
public ShaderResourceView TextureResource { get; private set; }
public bool Initialize(Device device, DeviceContext context, string fileName)
{
try
{
Texture2DDescription1 description = new Texture2DDescription1()
{
ArraySize = 1,
Usage = ResourceUsage.Default,
CpuAccessFlags = CpuAccessFlags.None,
Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
MipLevels = 0,
OptionFlags = ResourceOptionFlags.GenerateMipMaps,
SampleDescription = new SharpDX.DXGI.SampleDescription(1,0),
BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource
};
BitmapSource bitmapSource = LoadBitmap(new SharpDX.WIC.ImagingFactory(), fileName);
description.Width = bitmapSource.Size.Width;
description.Height = bitmapSource.Size.Height;
Texture2D texture = CreateTex2DFromBitmap(bitmapSource, device as Device3, context, description);
ShaderResourceViewDescription srvDesc = new ShaderResourceViewDescription()
{
Format = texture.Description.Format,
Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D,
};
srvDesc.Texture2D.MostDetailedMip = 0;
srvDesc.Texture2D.MipLevels = -1;
TextureResource = new ShaderResourceView(device, texture, srvDesc);
context.GenerateMips(TextureResource);
texture.Dispose();
// TextureResource = ShaderResourceView.FromFile(device, fileName);
return true;
}
catch
{
return false;
}
}
public Texture2D1 CreateTex2DFromBitmap(SharpDX.WIC.BitmapSource bsource, Device3 a_device, DeviceContext a_context, Texture2DDescription1 a_desc)
{
Texture2D1 t2d = null;
try
{
int stride = bsource.Size.Width * 4;
var buffer = new DataStream(bsource.Size.Height * stride, true, true);
if (a_desc.MipLevels != 0)
{
// if we arent doing mip maps, then load the resource directly
DataRectangle rect = new DataRectangle(buffer.DataPointer, stride);
bsource.CopyPixels(stride, buffer);
t2d = new Texture2D1(a_device, a_desc, rect); /// this creates a texture and populates it, we need to create a blank one then populate it.
}
else
{
t2d = new Texture2D1(a_device, a_desc); /// this creates a texture and populates it, we need to create a blank one then populate it.
bsource.CopyPixels(stride, buffer);
DataBox box = new DataBox(buffer.DataPointer, stride, 1);
a_context.UpdateSubresource(box, t2d, Resource.CalculateSubResourceIndex(0, 0, CountMips(bsource.Size.Width, bsource.Size.Height)));
}
buffer.Dispose();
}
catch (Exception ex)
{
}
return t2d;
}
private int CountMips(int width, int height)
{
return (int) Math.Log(width, 2);
}
public void ShutDown()
{
TextureResource?.Dispose();
TextureResource = null;
}
public BitmapSource LoadBitmap(ImagingFactory factory, string filename)
{
var bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
factory,
filename,
SharpDX.WIC.DecodeOptions.CacheOnDemand
);
var result = new SharpDX.WIC.FormatConverter(factory);
result.Initialize(
bitmapDecoder.GetFrame(0),
SharpDX.WIC.PixelFormat.Format32bppPRGBA,
SharpDX.WIC.BitmapDitherType.None,
null,
0.0,
SharpDX.WIC.BitmapPaletteType.Custom);
return result;
}
}
}
Actually, no need to change to device3, just turn all the device3's into device, texture2ddescription1s into texture2ddescription, and texutre2d1 into texture2d