Search code examples
c#slimdxtexture2d

Creating a texture from a RGBA value with SlimDX


this is my first post to stack overflow! I'm using SlimDX for a game that my team is making and I've run into an issue. I'm trying to create a ShaderResourceView from RGBA values in a Color4 object. I've searched looked for answers to my issue and this is as far as I have gotten.

    private ShaderResourceView GetTexture(Device device, int width, int height, Color4 color)
    {
        //create the texture
        Texture2D texture = null;
        Texture2DDescription desc2 = new Texture2DDescription();
        desc2.SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0); 
        desc2.Width = width;
        desc2.Height = height;
        desc2.MipLevels = 1;
        desc2.ArraySize = 1;
        desc2.Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm;
        desc2.Usage = ResourceUsage.Dynamic;
        desc2.BindFlags = BindFlags.ShaderResource;
        desc2.CpuAccessFlags = CpuAccessFlags.Write;
        texture = new Texture2D(device, desc2);


        // fill the texture with rgba values
        DataRectangle rect = texture.Map(0, MapMode.WriteDiscard, MapFlags.None);
        if (rect.Data.CanWrite)
        {
            for (int row = 0; row < texture.Description.Height; row++)
            {
                int rowStart = row * rect.Pitch;
                rect.Data.Seek(rowStart, System.IO.SeekOrigin.Begin);
                for (int col = 0; col < texture.Description.Width; col++)
                {
                    rect.Data.WriteByte((byte)color.Red);
                    rect.Data.WriteByte((byte)color.Green);
                    rect.Data.WriteByte((byte)color.Blue);
                    rect.Data.WriteByte((byte)color.Alpha);
                }
            }
        }
        texture.Unmap(0);

        // create shader resource that is what the renderer needs
        ShaderResourceViewDescription desc = new ShaderResourceViewDescription();
        desc.Format = texture.Description.Format;
        desc.Dimension = ShaderResourceViewDimension.Texture2D;
        desc.MostDetailedMip = 0;
        desc.MipLevels = 1;
        ShaderResourceView srv = new ShaderResourceView(device, texture, desc);

        return srv;
    }

I believe that the texture's data is being set but I can't be sure because nothing is being displayed. I know that my renderer works as I can load texture from a file perfectly fine but I seem to have a problem that I cannot find. Thanks for your help in advance!


Solution

  • I've had the code right all along. I feel like an idiot but I found out my problem, I wasn't setting the alpha value for the texture so it was actually being drawn I just couldn't see it >_<; Simple mistakes always eh? Thanks to all that viewed however.