Пример #1
0
 public ShaderResourceView CreateTextureView(int width, int height, FillColor4 fill)
 {
     using (var texture = CreateTexture(width, height, fill)) {
         return(new ShaderResourceView(Device, texture, new ShaderResourceViewDescription {
             Format = texture.Description.Format,
             Dimension = ShaderResourceViewDimension.Texture2D,
             MostDetailedMip = 0,
             MipLevels = 1
         }));
     }
 }
Пример #2
0
        public ShaderResourceView CreateTexture(int width, int height, FillColor4 fill)
        {
            var texture = new Texture2D(Device, new Texture2DDescription {
                SampleDescription = new SampleDescription(1, 0),
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = Format.R32G32B32A32_Float,
                Usage             = ResourceUsage.Dynamic,
                BindFlags         = BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.Write
            });

            var rect = DeviceContext.MapSubresource(texture, 0, MapMode.WriteDiscard, MapFlags.None);

            if (rect.Data.CanWrite)
            {
                for (var y = 0; y < width; y++)
                {
                    var rowStart = y * rect.RowPitch;
                    rect.Data.Seek(rowStart, System.IO.SeekOrigin.Begin);
                    for (var x = 0; x < width; x++)
                    {
                        var c = fill.Invoke(x, y);
                        rect.Data.Write(c.Red);
                        rect.Data.Write(c.Green);
                        rect.Data.Write(c.Blue);
                        rect.Data.Write(c.Alpha);
                    }
                }
            }

            DeviceContext.UnmapSubresource(texture, 0);

            var view = new ShaderResourceView(Device, texture, new ShaderResourceViewDescription {
                Format          = texture.Description.Format,
                Dimension       = ShaderResourceViewDimension.Texture2D,
                MostDetailedMip = 0,
                MipLevels       = 1
            });

            texture.Dispose();
            return(view);
        }