Dx11 Createtexture From Char*

Started by
1 comment, last by MJP 7 years, 8 months ago

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

Advertisement

 // Get bitmap
            int bitLen = (obj.m_textureHeight * obj.m_textureWidth) * (obj.m_bpp / 8);
            byte[] bits = new byte[bitLen];

            bits = reader.ReadBytes(bitLen);
            reader.Close();

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

                buffer.Dispose();

                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)
            {
                m_context.GenerateMips(obj.m_d3dTexture);
            }

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

Indie game developer - Game WIP

Strafe (Working Title) - Currently in need of another developer and modeler/graphic artist (professional & amateur's artists welcome)

Insane Software Facebook

You can just look at the tail end of the WIC texture loader from DirectXTex for an example.

This topic is closed to new replies.

Advertisement