示例#1
0
        private unsafe Texture Build(float[] data, uint width, uint height)
        {
            Veldrid.Texture texture = _components.Factory.CreateTexture(TextureDescription.Texture2D(
                                                                            width, height, 1, 1, PixelFormat.R32_Float, TextureUsage.Sampled));

            Span <float> span             = data; //Not needed, as conversion of simple array implicit to span
            var          pixelSizeInBytes = 4u;   //Not needed, but just explains what the magic four would be for, geddit? :)

            fixed(void *pin = &MemoryMarshal.GetReference(span))
            {
                _components.Device.UpdateTexture(
                    texture,
                    (IntPtr)pin,
                    pixelSizeInBytes * width * height,
                    0,
                    0,
                    0,
                    width,
                    height,
                    1,
                    0,
                    0);
            }

            return(texture);
        }
示例#2
0
        public Texture(ImageSharpTexture imageTexture, Veldrid.Texture deviceTexture, TextureView textureView, long hash)
        {
            _imageTexture  = imageTexture;
            _deviceTexture = deviceTexture;
            _textureView   = textureView;
            _hash          = hash;

            _size = new Vector2(_imageTexture.Width, _imageTexture.Height);
        }
示例#3
0
        public static unsafe IntPtr Create(Image <Rgba32> image)
        {
            lock (LockObject)
            {
                Veldrid.Texture texture = GraphicsDevice.ResourceFactory.CreateTexture(TextureDescription.Texture2D((uint)image.Width, (uint)image.Height, 1, 1, PixelFormat.R8_G8_B8_A8_UNorm, TextureUsage.Sampled));
                fixed(void *pin = &MemoryMarshal.GetReference(image.GetPixelRowSpan(0)))
                {
                    GraphicsDevice.UpdateTexture(texture, (IntPtr)pin, (uint)(4 * image.Width * image.Height), 0, 0, 0, (uint)image.Width, (uint)image.Height, 1, 0, 0);
                }

                return(Controller.GetOrCreateImGuiBinding(GraphicsDevice.ResourceFactory, texture));
            }
        }
示例#4
0
        public Texture(RenderContext context, int width, int height, byte[] data, TextureSampleType sampleType)
        {
            uint w = (uint)width, h = (uint)height;

            uint numMips = 4;

            if (w < 16 || h < 16)
            {
                numMips = 1;
            }
            var device = context.Device;

            _texture = device.ResourceFactory.CreateTexture(TextureDescription.Texture2D(
                                                                w, h, numMips, 1,
                                                                PixelFormat.B8_G8_R8_A8_UNorm,
                                                                TextureUsage.Sampled | TextureUsage.GenerateMipmaps
                                                                ));

            try
            {
                device.UpdateTexture(_texture, data, 0, 0, 0, w, h, _texture.Depth, 0, 0);
            }
            catch
            {
                // Error updating texture, the texture may have been disposed
            }
            _mipsGenerated = false;

            var sampler = context.ResourceLoader.TextureSampler;

            if (sampleType == TextureSampleType.Point)
            {
                sampler = context.ResourceLoader.OverlaySampler;
            }

            _view = device.ResourceFactory.CreateTextureView(_texture);
            _set  = device.ResourceFactory.CreateResourceSet(new ResourceSetDescription(
                                                                 context.ResourceLoader.TextureLayout, _view, sampler
                                                                 ));
        }
示例#5
0
        private void PostImageLoad()
        {
            uint tx = (uint)Size.X;
            uint ty = (uint)Size.Y;

            Texture = Renderer.VeldridFactory.CreateTexture(new Veldrid.TextureDescription(
                                                                tx, ty, 1,
                                                                1, 1,
                                                                Veldrid.PixelFormat.R8_G8_B8_A8_UNorm, Veldrid.TextureUsage.Sampled, Veldrid.TextureType.Texture2D));

            Veldrid.Texture StagingTexture = Renderer.VeldridFactory.CreateTexture(new Veldrid.TextureDescription(
                                                                                       tx, ty, 1,
                                                                                       1, 1,
                                                                                       Veldrid.PixelFormat.R8_G8_B8_A8_UNorm, Veldrid.TextureUsage.Staging, Veldrid.TextureType.Texture2D));

            Span <Rgba32> Pixels;

            InternalTexture.TryGetSinglePixelSpan(out Pixels);

            Span <byte> Bytes = MemoryMarshal.AsBytes(Pixels);


            //TODO: Fix this copy (Bytes.ToArray()) and use the memory directly.  UpdateTexture takes an intptr so get that
            Renderer.GraphicsDevice.UpdateTexture(StagingTexture, Bytes.ToArray(),
                                                  0, 0, 0,
                                                  tx, ty, 1,
                                                  0, 0);

            using (Veldrid.CommandList cl = Renderer.VeldridFactory.CreateCommandList())
            {
                cl.Begin();
                cl.CopyTexture(StagingTexture, Texture);
                cl.End();
                Renderer.GraphicsDevice.SubmitCommands(cl);
                Renderer.GraphicsDevice.WaitForIdle();
            }
        }
示例#6
0
 internal static ulong ComputeSubresourceOffset(Texture tex, uint mipLevel, uint arrayLayer)
 {
     Debug.Assert((tex.Usage & TextureUsage.Staging) == TextureUsage.Staging);
     return(ComputeArrayLayerOffset(tex, arrayLayer) + ComputeMipOffset(tex, mipLevel));
 }
示例#7
0
 internal static void GetMipDimensions(Texture tex, uint mipLevel, out uint width, out uint height, out uint depth)
 {
     width  = GetDimension(tex.Width, mipLevel);
     height = GetDimension(tex.Height, mipLevel);
     depth  = GetDimension(tex.Depth, mipLevel);
 }
示例#8
0
 internal static void GetMipLevelAndArrayLayer(Texture tex, uint subresource, out uint mipLevel, out uint arrayLayer)
 {
     arrayLayer = subresource / tex.MipLevels;
     mipLevel   = subresource - (arrayLayer * tex.MipLevels);
 }