🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

SharpDX Direct3D11 ShaderResourceView

Started by
0 comments, last by bogdan7 2 years, 9 months ago

I tried to upload a texture2d to pixel shader using:

deviceContext.PixelShader.SetSampler(0, samplerState);
deviceContext.PixelShader.SetShaderResource(0, view);
                
public void Init(){
SamplerStateDescription samplerStateDescription = new SamplerStateDescription
            {
                Filter = SharpDX.Direct3D11.Filter.Anisotropic,
                AddressU = TextureAddressMode.Wrap,
                AddressV = TextureAddressMode.Wrap,
                AddressW = TextureAddressMode.Wrap,
                ComparisonFunction = Comparison.Never,
                MinimumLod = 0,
                MaximumLod = float.MaxValue,

            };
            ImagingFactory factory = new ImagingFactory();
            BitmapSource bitmap = LoadBitmap(factory, "1.bmp");

            Texture2D texture = new Texture2D(device, new Texture2DDescription()
            {
                Width = bitmap.Size.Width,
                Height = bitmap.Size.Height,
                ArraySize = 1,
                BindFlags = BindFlags.ShaderResource,
                Usage = SharpDX.Direct3D11.ResourceUsage.Default,
                CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
                Format = SharpDX.DXGI.Format.R10G10B10A2_UNorm,
                MipLevels = CountMips(bitmap.Size.Width, bitmap.Size.Height),
                OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.GenerateMipMaps,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
            });

            int stride = bitmap.Size.Width * 4;
            var buffer = new DataStream(bitmap.Size.Height * stride, true, true);
            bitmap.CopyPixels(stride, buffer);
            DataBox box = new DataBox(buffer.DataPointer, stride, 1);
            deviceContext.UpdateSubresource(box, texture, Resource.CalculateSubResourceIndex(0, 0, CountMips(bitmap.Size.Width, bitmap.Size.Height)));

            view = new ShaderResourceView(device, texture);

            samplerState = new SamplerState(device, samplerStateDescription);
            RenderLoop.Run(renderForm, Render);
            }
public static 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;
        }
        private static int CountMips(int width, int height)
        {
            //lifted from SharpDX Toolkit
            int mipLevels = 1;

            while (height > 1 || width > 1)
            {
                ++mipLevels;

                if (height > 1)
                    height >>= 1;

                if (width > 1)
                    width >>= 1;
            }

            return mipLevels;
        }

I sampled it in hlsl and the mesh is invisible, the texture is missing

The code above is from here https://gamedev.net/forums/topic/643673-sharpdxloading-multiple-images-into-a-texture-array/5065577/,​ however I don't need a texture array, just 1 texture

This topic is closed to new replies.

Advertisement