示例#1
0
        public ShaderResourceView CreateTexture(int width, int height, FillColor fill)
        {
            var texture = new Texture2D(Device, new Texture2DDescription {
                SampleDescription = new SampleDescription(1, 0),
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = Format.R8G8B8A8_UNorm,
                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.WriteByte(c.R);
                        rect.Data.WriteByte(c.G);
                        rect.Data.WriteByte(c.B);
                        rect.Data.WriteByte(c.A);
                    }
                }
            }

            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);
        }