SceneViewerWPF.DxTextureManager.CreateTexArray C# (CSharp) Method

CreateTexArray() public method

public CreateTexArray ( string arrayName ) : ShaderResourceView
arrayName string
return ShaderResourceView
        public ShaderResourceView CreateTexArray(string arrayName, params string[] filenames)
        {
            if (_textures.ContainsKey(arrayName))
                return _textures[arrayName];

            //
            // Load the texture elements individually from file.  These textures
            // won't be used by the GPU (0 bind flags), they are just used to
            // load the image data from file.  We use the STAGING usage so the
            // CPU can read the resource.
            //

            int arraySize = filenames.Length;

            var srcTex = new Texture2D[arraySize];
            for(int i = 0; i < arraySize; ++i)
            {
                var loadInfo = new ImageLoadInformation
                                   {
                                       Usage = ResourceUsage.Staging,
                                       BindFlags = BindFlags.None,
                                       CpuAccessFlags = CpuAccessFlags.Read | CpuAccessFlags.Write,
                                       OptionFlags = ResourceOptionFlags.None,
                                       Format = Format.R8G8B8A8_UNorm,
                                       FilterFlags = FilterFlags.None,
                                       MipFilterFlags = FilterFlags.None
                                   };

                srcTex[i] = Texture2D.FromFile(_dxDevice, filenames[i], loadInfo);
            }

            //
            // Create the texture array.  Each element in the texture
            // array has the same format/dimensions.
            //
            var texElementDesc = srcTex[0].Description;
            var texArrayDesc = new Texture2DDescription
                                   {
                                       Width = texElementDesc.Width,
                                       Height = texElementDesc.Height,
                                       MipLevels = texElementDesc.MipLevels,
                                       ArraySize = arraySize,
                                       Format = Format.R8G8B8A8_UNorm,
                                       SampleDescription = new SampleDescription(1, 0),
                                       Usage = ResourceUsage.Default,
                                       BindFlags = BindFlags.ShaderResource,
                                       CpuAccessFlags = CpuAccessFlags.None,
                                       OptionFlags = ResourceOptionFlags.None
                                   };

            var texArray = new Texture2D(_dxDevice, texArrayDesc);

            //
            // Copy individual texture elements into texture array.
            //

            // for each texture element...
            for (int i = 0; i < arraySize; ++i)
            {
                // for each mipmap level...
                for (int j = 0; j < texElementDesc.MipLevels; ++j)
                {
                    var mappedTex2D = srcTex[i].Map(j, MapMode.Read, MapFlags.None);

                    _dxDevice.UpdateSubresource(
                        new DataBox(mappedTex2D.Pitch, 0, mappedTex2D.Data), texArray,
                        Resource.CalculateSubresourceIndex(j, i, texElementDesc.MipLevels));

                    srcTex[i].Unmap(j);
                }
            }

            //
            // Create a resource view to the texture array.
            //

            var viewDesc = new ShaderResourceViewDescription();
            viewDesc.Format = texArrayDesc.Format;
            viewDesc.Dimension = ShaderResourceViewDimension.Texture2DArray;
            viewDesc.MostDetailedMip = 0;
            viewDesc.MipLevels = texArrayDesc.MipLevels;
            viewDesc.FirstArraySlice = 0;
            viewDesc.ArraySize = arraySize;

            var texArrayRV = new ShaderResourceView(_dxDevice, texArray, viewDesc);

            //
            // Cleanup--we only need the resource view.
            //
            texArray.Dispose();

            for(int i = 0; i < arraySize; ++i)
                srcTex[i].Dispose();

            _textures.Add(arrayName, texArrayRV);

            return texArrayRV;
        }

Usage Example

Esempio n. 1
0
        private void InitD3D()
        {
            _dxDevice = new Device(DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport, FeatureLevel.Level_10_0);

            EnsureOutputBuffers();

            FontDescription fontDesc = new FontDescription
            {
                Height         = 24,
                Width          = 0,
                Weight         = 0,
                MipLevels      = 1,
                IsItalic       = false,
                CharacterSet   = FontCharacterSet.Default,
                Precision      = FontPrecision.Default,
                Quality        = FontQuality.Default,
                PitchAndFamily = FontPitchAndFamily.Default | FontPitchAndFamily.DontCare,
                FaceName       = "Times New Roman"
            };

            _dxFont = new Font(_dxDevice, fontDesc);

            _textureManager = new DxTextureManager(_dxDevice);

//            _dxEffect = new DxEffect(_dxDevice);
            _dxCube = new DxCube(_dxDevice);
            CrateKinectPointsRenderer(KinectPointsRendererType.Billboard);

            ShaderResourceView texArray = _textureManager.CreateTexArray("flares", @"Assets\flare0.dds");

            _fire = new DxParticleSystemRenderer(_dxDevice, texArray, 500);

            _dxDevice.Flush();
        }
All Usage Examples Of SceneViewerWPF.DxTextureManager::CreateTexArray