Jump to content
  • Advertisement
Sign in to follow this  

DX11 Dx11 Createtexture From Char*

This topic is 706 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

who can show sample to use CreateTexture2D . I have loaded texture by a non-DirectxTex loader to byte array.

Share this post

Link to post
Share on other sites
 // Get bitmap
            int bitLen = (obj.m_textureHeight * obj.m_textureWidth) * (obj.m_bpp / 8);
            byte[] bits = new byte[bitLen];

            bits = reader.ReadBytes(bitLen);

            D3D11.Texture2DDescription desc = new D3D11.Texture2DDescription()
                ArraySize = 1,
                BindFlags = D3D11.BindFlags.ShaderResource,
                CpuAccessFlags = D3D11.CpuAccessFlags.Write,
                Format = DXGI.Format.B8G8R8A8_UNorm,
                Height = obj.m_textureHeight,
                MipLevels = 1,
                SampleDescription = new DXGI.SampleDescription(1, 0),
                Usage = D3D11.ResourceUsage.Dynamic,
                Width = obj.m_textureWidth


            if (a_mipmapped)
                desc.Usage = D3D11.ResourceUsage.Default;
                desc.CpuAccessFlags = D3D11.CpuAccessFlags.None;  // its none because we load the data into the texture as a full resource update, not update the contents after with map resource.
                desc.Format = DXGI.Format.R8G8B8A8_UNorm_SRgb;
                desc.MipLevels = 0;
                desc.OptionFlags = D3D11.ResourceOptionFlags.GenerateMipMaps;
                desc.SampleDescription.Count = 1;
                desc.SampleDescription.Quality = 0;
                desc.BindFlags = D3D11.BindFlags.RenderTarget | D3D11.BindFlags.ShaderResource;

            if (desc.MipLevels != 0)
                // if we arent doing mip maps, then load the resource directly
                DataStream buffer = new DataStream(obj.m_textureHeight * obj.m_textureWidth * 4, true, true);

                load24BitBitmapInto32BitTexture(bits, buffer, obj);

                DataRectangle rect = new DataRectangle(buffer.DataPointer, obj.m_textureWidth * 4);
                tex = new D3D11.Texture2D(m_device, desc, rect); /// this creates a texture and populates it,

                obj.m_d3dTexture = new D3D11.ShaderResourceView(m_device, tex);
                tex = new D3D11.Texture2D(m_device, desc); /// we need to create a blank one then populate it with the mip mapped texture.

                int stride = obj.m_textureWidth * 4;
                var buffer = new DataStream(obj.m_textureHeight * stride, true, true);

                load24BitBitmapInto32BitTexture(bits, buffer, obj);

                DataBox box = new DataBox(buffer.DataPointer, stride, 1);

                m_device.ImmediateContext.UpdateSubresource(box, tex, D3D11.Resource.CalculateSubResourceIndex(0, 0, 4));


                D3D11.ShaderResourceViewDescription srvDesc = new D3D11.ShaderResourceViewDescription();
                srvDesc.Format = desc.Format;
                srvDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D;
                srvDesc.Texture2D.MostDetailedMip = 0;
                srvDesc.Texture2D.MipLevels = -1;

                obj.m_d3dTexture = new D3D11.ShaderResourceView(m_device, tex, srvDesc);

            if (a_mipmapped)

This is in Sharpdx and c# but this can be a mip mapped or non mip mapped texture.  Up to you.

Just change your char to a byte array and voila

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!