Esempio n. 1
0
        public static TextureSampler Create(ResourceDimension dimension)
        {
            switch (dimension)
            {
            case ResourceDimension.Texture1D:
                throw new NotImplementedException();

            case ResourceDimension.Texture1DArray:
                throw new NotImplementedException();

            case ResourceDimension.Texture2D:
                return(new Texture2DSampler());

            case ResourceDimension.Texture2DArray:
                return(new Texture2DArraySampler());

            case ResourceDimension.Texture3D:
                throw new NotImplementedException();

            case ResourceDimension.TextureCube:
                return(new TextureCubeSampler());

            case ResourceDimension.TextureCubeArray:
                throw new NotImplementedException();

            default:
                throw new ArgumentOutOfRangeException("dimension");
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ResourceDescription1"/> struct.
 /// </summary>
 /// <param name="dimension"></param>
 /// <param name="alignment"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 /// <param name="depthOrArraySize"></param>
 /// <param name="mipLevels"></param>
 /// <param name="format"></param>
 /// <param name="sampleCount"></param>
 /// <param name="sampleQuality"></param>
 /// <param name="layout"></param>
 /// <param name="flags"></param>
 /// <param name="samplerFeedbackMipRegionWidth"></param>
 /// <param name="samplerFeedbackMipRegionHeight"></param>
 /// <param name="samplerFeedbackMipRegionDepth"></param>
 public ResourceDescription1(
     ResourceDimension dimension,
     ulong alignment,
     ulong width,
     int height,
     ushort depthOrArraySize,
     ushort mipLevels,
     Format format,
     int sampleCount,
     int sampleQuality,
     TextureLayout layout,
     ResourceFlags flags,
     int samplerFeedbackMipRegionWidth  = 0,
     int samplerFeedbackMipRegionHeight = 0,
     int samplerFeedbackMipRegionDepth  = 0)
 {
     Dimension                = dimension;
     Alignment                = alignment;
     Width                    = width;
     Height                   = height;
     DepthOrArraySize         = depthOrArraySize;
     MipLevels                = mipLevels;
     Format                   = format;
     SampleDescription        = new SampleDescription(sampleCount, sampleQuality);
     Layout                   = layout;
     Flags                    = flags;
     SamplerFeedbackMipRegion = new MipRegion(samplerFeedbackMipRegionWidth, samplerFeedbackMipRegionHeight, samplerFeedbackMipRegionDepth);
 }
Esempio n. 3
0
        private CTextureSurface(ICDevice device, ResourceDimension dimension, Texture2DDescription description, SubresourceData[] initialData, Action<CTextureSurface> onRelease)
            : base(device, dimension, description, t => onRelease((CTextureSurface)t))
        {
            CreateNative();

            if (initialData != null)
                throw new NotSupportedException("Initial newData is not supported for render target and depth-stencil resources by D3D9 implementation of Beholder");
        }
 public override string ToString()
 {
     return(string.Format("{0}_{1}{2} ({3}) {4}{5}",
                          TypeDescription,
                          ResourceDimension.GetDescription(),
                          (IsMultiSampled) ? "(" + SampleCount + ")" : string.Empty,
                          ReturnType,
                          Operand,
                          IsSM51 ? $", space={SpaceIndex}" : ""));
 }
 public override string ToString()
 {
     return(string.Format("{0}_{1}{2}{3} ({4}) {5}{6}",
                          Header.OpcodeType.GetDescription(),
                          ResourceDimension.GetDescription(),
                          Coherency.GetDescription(),
                          GetRasterOrderedAccessDescription(),
                          ReturnType,
                          Operand,
                          IsSM51 ? $", space={SpaceIndex}" : ""));
 }
Esempio n. 6
0
 /// <summary>To initialize this structure with default value, use this constructor. The argument value is ignored.</summary>
 /// <remarks>This is only here because C# doesn’t support parameterless constructors for structures.</remarks>
 public TextureViewDesc(bool unused)
 {
     baseStruct      = new DeviceObjectAttribs(true);
     ViewType        = TextureViewType.Undefined;
     TextureDim      = ResourceDimension.Undefined;
     Format          = TextureFormat.Unknown;
     MostDetailedMip = 0;
     NumMipLevels    = 0;
     FirstArraySlice = 0;
     FirstDepthSlice = default(int);
     AccessFlags     = UavAccessFlag.Unspecified;
     Flags           = TextureViewFlags.None;
 }
Esempio n. 7
0
 public ResourceDescription(ResourceDimension dimension, long alignment, long width, int height, short depthOrArraySize, short mipLevels, Format format, int sampleCount, int sampleQuality, TextureLayout layout, ResourceFlags optionFlags)
 {
     Dimension = dimension;
     Alignment = alignment;
     Width = width;
     Height = height;
     DepthOrArraySize = depthOrArraySize;
     MipLevels = mipLevels;
     Format = format;
     SampleDescription = new SampleDescription(sampleCount, sampleQuality);
     Layout = layout;
     Flags = optionFlags;
 }
Esempio n. 8
0
 public ResourceDescription(ResourceDimension dimension, long alignment, long width, int height, short depthOrArraySize, short mipLevels, Format format, int sampleCount, int sampleQuality, TextureLayout layout, ResourceOptionFlags optionFlags)
 {
     Dimension         = dimension;
     Alignment         = alignment;
     Width             = width;
     Height            = height;
     DepthOrArraySize  = depthOrArraySize;
     MipLevels         = mipLevels;
     Format            = format;
     SampleDescription = new SampleDescription(sampleCount, sampleQuality);
     Layout            = layout;
     OptionFlags       = optionFlags;
 }
Esempio n. 9
0
        protected CTexture(ICDevice device, ResourceDimension dimension, Texture2DDescription description, Action<CTexture> onDispose)
        {
            this.device = device;
            this.dimension = dimension;
            this.desc = description;
            this.onDispose = onDispose;

            if (desc.BindFlags.HasFlag(BindFlags.RenderTarget))
                rtvs = new List<CRenderTargetView>();
            if (desc.BindFlags.HasFlag(BindFlags.DepthStencil))
                dsvs = new List<CDepthStencilView>();
            if (desc.BindFlags.HasFlag(BindFlags.ShaderResource))
                srvs = new List<CShaderResourceView>();
        }
Esempio n. 10
0
 /// <summary>Create and set fields to their defaults</summary>
 public TextureDesc(bool unused)
 {
     baseStruct       = new DeviceObjectAttribs(true);
     Type             = ResourceDimension.Undefined;
     Size             = new CSize(0, 0);
     ArraySizeOrDepth = 1;
     Format           = TextureFormat.Unknown;
     MipLevels        = 1;
     SampleCount      = 1;
     Usage            = Usage.Default;
     BindFlags        = BindFlags.None;
     CPUAccessFlags   = CpuAccessFlags.None;
     MiscFlags        = MiscTextureFlags.None;
     ClearValue       = new OptimizedClearValue(true);
     CommandQueueMask = 1;
 }
Esempio n. 11
0
        public static TextureType FromDirectX(this ResourceDimension dimension)
        {
            switch (dimension)
            {
            case ResourceDimension.Texture1D:
                return(TextureType.Texture1D);

            case ResourceDimension.Texture2D:
                return(TextureType.Texture2D);

            case ResourceDimension.Texture3D:
                return(TextureType.Texture3D);

            default:
                return(TextureType.Unknown);
            }
        }
Esempio n. 12
0
        public static TextureType Convert(ResourceDimension dimension)
        {
            switch (dimension)
            {
            case ResourceDimension.Texture1D:
                return(TextureType.Texture1D);

            case ResourceDimension.Texture2D:
                return(TextureType.Texture2D);

            case ResourceDimension.Texture3D:
                return(TextureType.Texture3D);

            default:
                return(TextureType.Unknown);
            }
        }
Esempio n. 13
0
        /// <summary>
        /// テクスチャーリソースからセットする。ただし、idxは順番(連番)にセットすること
        /// </summary>
        /// <param name="idx">0~7</param>
        /// <param name="resource">Resource</param>
        /// <returns></returns>
        protected int SetTextureResource(int idx, ref SharpDX.Direct3D11.Resource resource)
        {
            if (idx > 7 || idx != m_createdTxNum)
            {
                Debug.Assert(false); throw new ArgumentException();
            }
            if (m_aResourceTx[idx] != null)
            {
                Debug.Assert(false); throw new CreatedException();
            }

            m_type = resource.Dimension;
            switch (m_type)
            {
            case ResourceDimension.Unknown:
                Debug.Assert(false);
                return(-1);

            case ResourceDimension.Texture1D:
                m_aTexture[idx].d1 = (Texture1D)resource;
                m_descTx.D1        = m_aTexture[idx].d1.Description;
                break;

            case ResourceDimension.Texture2D:
                m_aTexture[idx].d2 = (Texture2D)resource;
                m_descTx.D2        = m_aTexture[idx].d2.Description;
                break;

            case ResourceDimension.Texture3D:
                m_aTexture[idx].d3 = (Texture3D)resource;
                m_descTx.D3        = m_aTexture[idx].d3.Description;
                break;

            default:
                Debug.Assert(false);
                throw new NotExistenceException();
            }
            m_aResourceTx[idx] = resource;
            return(0);
        }
        public static TextureSampler Create(ResourceDimension dimension)
        {
            switch (dimension)
            {
				case ResourceDimension.Texture1D:
                    throw new NotImplementedException();
				case ResourceDimension.Texture1DArray:
                    throw new NotImplementedException();
				case ResourceDimension.Texture2D:
                    return new Texture2DSampler();
				case ResourceDimension.Texture2DArray:
					return new Texture2DArraySampler();
				case ResourceDimension.Texture3D:
                    throw new NotImplementedException();
				case ResourceDimension.TextureCube:
					return new TextureCubeSampler();
				case ResourceDimension.TextureCubeArray:
                    throw new NotImplementedException();
                default:
                    throw new ArgumentOutOfRangeException("dimension");
            }
        }
Esempio n. 15
0
        /// <summary>
        /// 3Dテクスチャーを作成する。ただし、idxは順番(連番)にセットすること
        /// </summary>
        /// <param name="device"></param>
        /// <param name="idx">0~7</param>
        /// <param name="desc"></param>
        /// <returns></returns>
        internal int CreateTexture3D(int idx, Texture123DDesc desc = null)
        {
            if (idx > 7 || idx != m_createdTxNum)
            {
                Debug.Assert(false); throw new ArgumentException();
            }
            if (m_aResourceTx[idx] != null)
            {
                Debug.Assert(false); throw new CreatedException();
            }

            m_sourceLocation = MC_IMG_SOURCELOCATION.DEFAULT;

            m_aTexture[idx].d3           = new Texture3D(App.DXDevice, desc.D3);
            m_aTexture[idx].d3.DebugName = "Texture3D:" + m_name;

            m_descTx.D3        = m_aTexture[idx].d3.Description;
            m_type             = ResourceDimension.Texture3D;;
            m_aResourceTx[idx] = (SharpDX.Direct3D11.Resource)m_aTexture[idx].d3;


            ++m_createdTxNum;
            return(0);
        }
Esempio n. 16
0
            public static bool ValidateTexture(DdsHeader header, HeaderDXT10?header10, out int depth, out PixelFormat format, out ResourceDimension resDim, out int arraySize, out bool isCubeMap)
            {
                if (header10.HasValue)
                {
                    return(ValidateTexture(header10.Value, header.Flags, out depth, out format, out resDim, out arraySize, out isCubeMap));
                }

                else
                {
                    return(ValidateTexture(header, out depth, out format, out resDim, out arraySize, out isCubeMap));
                }
            }
Esempio n. 17
0
        public DdsTexture(
            DxgiFormat dxgiFormat,
            int width,
            int height    = 1,
            int depth     = 1,
            int arraySize = 1,
            int mipCount  = 0,
            ResourceDimension dimension = ResourceDimension.Unknown,
            ResourceMiscFlags miscFlags = 0)
        {
            if (dxgiFormat == DxgiFormat.UNKNOWN)
            {
                throw new ArgumentException("Formats can not be 'unknown'");
            }

            if (width <= 0)
            {
                throw new ArgumentException("Width must be greater than zero");
            }

            if (height <= 0)
            {
                throw new ArgumentException("Height must be greater than zero");
            }

            if (depth <= 0)
            {
                throw new ArgumentException("Depth must be greater than zero");
            }

            if (arraySize <= 0)
            {
                throw new ArgumentException("Array size must be greater than zero");
            }

            if (mipCount < 0)
            {
                throw new ArgumentException("Mip count must can not be negative");
            }

            if (dimension != ResourceDimension.Unknown && dimension != ResourceDimension.Texture3D && depth != 1)
            {
                throw new ArgumentException(string.Format("Dimension10 is {0}, but Depth is not 1", dimension));
            }

            if ((dimension == ResourceDimension.Buffer || dimension == ResourceDimension.Texture1D) && height != 1)
            {
                throw new ArgumentException(string.Format("Dimension10 is {0}, but Height is not 1", dimension));
            }

            if (dimension == ResourceDimension.Buffer && mipCount > 1)
            {
                throw new ArgumentException("Dimension10 is Buffer, but MipCount is not 0 or 1");
            }

            this.Width  = width;
            this.Height = height;
            this.Depth  = depth;

            /*
             * if (depth != 1)
             *  Dimension10 = ResourceDimension.Texture3D;
             * else if (height != 1)
             *  Dimension10 = ResourceDimension.Texture2D;
             * else
             *  Dimension10 = ResourceDimension.Texture1D;*/

            this.MiscFlags10 = miscFlags;

            this.D3DFormat = Helper.D3DFormatFromDxgi(dxgiFormat);

            if (mipCount == 0)
            {
                int mipWidth  = width;
                int mipHeight = height;
                int mipDepth  = depth;

                mipCount = 1;
                while (mipWidth != 1 || mipHeight != 1 || mipDepth != 1)
                {
                    if (mipWidth != 1)
                    {
                        mipWidth /= 2;
                    }

                    if (mipHeight != 1)
                    {
                        mipHeight /= 2;
                    }

                    if (mipDepth != 1)
                    {
                        mipDepth /= 2;
                    }

                    mipCount++;
                }
            }

            this.Data = new byte[arraySize][];

            int chainSize;

            this.CalculateMipInfos(mipCount, -1, -1, out chainSize);

            for (int i = 0; i < this.Data.Length; i++)
            {
                this.Data[i] = new byte[chainSize];
            }
        }
Esempio n. 18
0
    private static void CreateTextureFromDDS(
        Device d3dDevice,
        DdsHeader header,
        DdsHeaderDxt10?headerDxt10,
        IntPtr bitData,
        int bitSize,
        int maxsize,
        ResourceUsage usage,
        BindFlags bindFlags,
        CpuAccessFlags cpuAccessFlags,
        ResourceOptionFlags miscFlags,
        bool forceSRGB,
        out Resource texture,
        out ShaderResourceView textureView)
    {
        Result hr = Result.Ok;

        int width  = (int)header.width;
        int height = (int)header.height;
        int depth  = (int)header.depth;

        ResourceDimension resDim = ResourceDimension.Unknown;
        uint arraySize           = 1;

        SharpDX.DXGI.Format format = SharpDX.DXGI.Format.Unknown;
        bool isCubeMap             = false;

        int mipCount = (int)header.mipMapCount;

        if (0 == mipCount)
        {
            mipCount = 1;
        }

        if (headerDxt10.HasValue)
        {
            DdsHeaderDxt10 d3d10ext = headerDxt10.Value;

            arraySize = d3d10ext.arraySize;
            if (arraySize == 0)
            {
                throw new SharpDXException(ErrorCodeHelper.ToResult(ErrorCode.InvalidData));
            }

            switch (d3d10ext.dxgiFormat)
            {
            case Format.AI44:
            case Format.IA44:
            case Format.P8:
            case Format.A8P8:
                throw new SharpDXException(ErrorCodeHelper.ToResult(ErrorCode.NotSupported));

            default:
                if (BitsPerPixel(d3d10ext.dxgiFormat) == 0)
                {
                    throw new SharpDXException(ErrorCodeHelper.ToResult(ErrorCode.NotSupported));
                }
                break;
            }

            format = d3d10ext.dxgiFormat;

            switch (d3d10ext.resourceDimension)
            {
            case ResourceDimension.Texture1D:
                // D3DX writes 1D textures with a fixed Height of 1
                if ((header.flags & DDS_HEIGHT) != 0 && height != 1)
                {
                    throw new SharpDXException(ErrorCodeHelper.ToResult(ErrorCode.InvalidData));
                }
                height = depth = 1;
                break;

            case ResourceDimension.Texture2D:
                if ((d3d10ext.miscFlag & ResourceOptionFlags.TextureCube) != 0)
                {
                    arraySize *= 6;
                    isCubeMap  = true;
                }
                depth = 1;
                break;

            case ResourceDimension.Texture3D:
                if ((header.flags & DDS_HEADER_FLAGS_VOLUME) == 0)
                {
                    throw new SharpDXException(ErrorCodeHelper.ToResult(ErrorCode.InvalidData));
                }

                if (arraySize > 1)
                {
                    throw new SharpDXException(ErrorCodeHelper.ToResult(ErrorCode.InvalidData));
                }
                break;

            default:
                throw new SharpDXException(ErrorCodeHelper.ToResult(ErrorCode.InvalidData));
            }

            resDim = d3d10ext.resourceDimension;
        }
        else
        {
            format = GetDXGIFormat(header.ddspf);

            if (format == Format.Unknown)
            {
                throw new SharpDXException(ErrorCodeHelper.ToResult(ErrorCode.NotSupported));
            }

            if ((header.flags & DDS_HEADER_FLAGS_VOLUME) != 0)
            {
                resDim = ResourceDimension.Texture3D;
            }
            else
            {
                if ((header.caps2 & DDS_CUBEMAP) != 0)
                {
                    // We require all six faces to be defined
                    if ((header.caps2 & DDS_CUBEMAP_ALLFACES) != DDS_CUBEMAP_ALLFACES)
                    {
                        throw new SharpDXException(ErrorCodeHelper.ToResult(ErrorCode.NotSupported));
                    }

                    arraySize = 6;
                    isCubeMap = true;
                }

                depth  = 1;
                resDim = ResourceDimension.Texture2D;

                // Note there's no way for a legacy Direct3D 9 DDS to express a '1D' texture
            }

            Debug.Assert(BitsPerPixel(format) != 0);
        }

        // Bound sizes (for security purposes we don't trust DDS file metadata larger than the D3D 11.x hardware requirements)
        if (mipCount > Resource.MaximumMipLevels)
        {
            throw new SharpDXException(ErrorCodeHelper.ToResult(ErrorCode.NotSupported));
        }

        switch (resDim)
        {
        case ResourceDimension.Texture1D:
            if ((arraySize > Resource.MaximumTexture1DArraySize) ||
                (width > Resource.MaximumTexture1DSize))
            {
                throw new SharpDXException(ErrorCodeHelper.ToResult(ErrorCode.NotSupported));
            }
            break;

        case ResourceDimension.Texture2D:
            if (isCubeMap)
            {
                // This is the right bound because we set arraySize to (NumCubes*6) above
                if ((arraySize > Resource.MaximumTexture2DArraySize) ||
                    (width > Resource.MaximumTextureCubeSize) ||
                    (height > Resource.MaximumTextureCubeSize))
                {
                    throw new SharpDXException(ErrorCodeHelper.ToResult(ErrorCode.NotSupported));
                }
            }
            else if ((arraySize > Resource.MaximumTexture2DArraySize) ||
                     (width > Resource.MaximumTexture2DSize) ||
                     (height > Resource.MaximumTexture2DSize))
            {
                throw new SharpDXException(ErrorCodeHelper.ToResult(ErrorCode.NotSupported));
            }
            break;

        case ResourceDimension.Texture3D:
            if ((arraySize > 1) ||
                (width > Resource.MaximumTexture3DSize) ||
                (height > Resource.MaximumTexture3DSize) ||
                (depth > Resource.MaximumTexture3DSize))
            {
                throw new SharpDXException(ErrorCodeHelper.ToResult(ErrorCode.NotSupported));
            }
            break;

        default:
            throw new SharpDXException(ErrorCodeHelper.ToResult(ErrorCode.NotSupported));
        }

        // Create the texture
        DataBox[] initData = new DataBox[mipCount * arraySize];

        FillInitData(width, height, depth, mipCount, (int)arraySize, format, maxsize, bitSize, bitData,
                     out int twidth, out int theight, out int tdepth, out int skipMip, initData);

        CreateD3DResources(d3dDevice, resDim, twidth, theight, tdepth, mipCount - skipMip, (int)arraySize,
                           format, usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
                           isCubeMap, initData, out texture, out textureView);
    }
Esempio n. 19
0
    private static void CreateD3DResources(
        Device d3dDevice,
        ResourceDimension resDim,
        int width,
        int height,
        int depth,
        int mipCount,
        int arraySize,
        Format format,
        ResourceUsage usage,
        BindFlags bindFlags,
        CpuAccessFlags cpuAccessFlags,
        ResourceOptionFlags miscFlags,
        bool forceSRGB,
        bool isCubeMap,
        DataBox[] initData,
        out Resource texture,
        out ShaderResourceView textureView)
    {
        if (d3dDevice == null)
        {
            throw new SharpDXException(Result.InvalidPointer);
        }

        if (forceSRGB)
        {
            format = MakeSRGB(format);
        }

        switch (resDim)
        {
        case ResourceDimension.Texture1D: {
            Texture1DDescription desc;
            desc.Width          = width;
            desc.MipLevels      = mipCount;
            desc.ArraySize      = arraySize;
            desc.Format         = format;
            desc.Usage          = usage;
            desc.BindFlags      = bindFlags;
            desc.CpuAccessFlags = cpuAccessFlags;
            desc.OptionFlags    = miscFlags & ~ResourceOptionFlags.TextureCube;

            using (Texture1D tex = new Texture1D(d3dDevice, desc, initData)) {
                ShaderResourceViewDescription SRVDesc = default(ShaderResourceViewDescription);
                SRVDesc.Format = format;

                if (arraySize > 1)
                {
                    SRVDesc.Dimension = ShaderResourceViewDimension.Texture1DArray;
                    SRVDesc.Texture1DArray.MipLevels = (mipCount == 0) ? -1 : desc.MipLevels;
                    SRVDesc.Texture1DArray.ArraySize = arraySize;
                }
                else
                {
                    SRVDesc.Dimension           = ShaderResourceViewDimension.Texture1D;
                    SRVDesc.Texture1D.MipLevels = (mipCount == 0) ? -1 : desc.MipLevels;
                }

                textureView = new ShaderResourceView(d3dDevice, tex, SRVDesc);
                texture     = tex.QueryInterface <Texture1D>();
            }
        }
        break;

        case ResourceDimension.Texture2D: {
            Texture2DDescription desc;
            desc.Width     = width;
            desc.Height    = height;
            desc.MipLevels = mipCount;
            desc.ArraySize = arraySize;
            desc.Format    = format;
            desc.SampleDescription.Count   = 1;
            desc.SampleDescription.Quality = 0;
            desc.Usage          = usage;
            desc.BindFlags      = bindFlags;
            desc.CpuAccessFlags = cpuAccessFlags;
            if (isCubeMap)
            {
                desc.OptionFlags = miscFlags | ResourceOptionFlags.TextureCube;
            }
            else
            {
                desc.OptionFlags = miscFlags & ~ResourceOptionFlags.TextureCube;
            }

            using (Texture2D tex = new Texture2D(d3dDevice, desc, initData)) {
                ShaderResourceViewDescription SRVDesc = default(ShaderResourceViewDescription);
                SRVDesc.Format = format;

                if (isCubeMap)
                {
                    if (arraySize > 6)
                    {
                        SRVDesc.Dimension = ShaderResourceViewDimension.TextureCubeArray;
                        SRVDesc.TextureCubeArray.MipLevels = (mipCount == 0) ? -1 : desc.MipLevels;

                        // Earlier we set arraySize to (NumCubes * 6)
                        SRVDesc.TextureCubeArray.CubeCount = arraySize / 6;
                    }
                    else
                    {
                        SRVDesc.Dimension             = ShaderResourceViewDimension.TextureCube;
                        SRVDesc.TextureCube.MipLevels = (mipCount == 0) ? -1 : desc.MipLevels;
                    }
                }
                else if (arraySize > 1)
                {
                    SRVDesc.Dimension = ShaderResourceViewDimension.Texture2DArray;
                    SRVDesc.Texture2DArray.MipLevels = (mipCount == 0) ? -1 : desc.MipLevels;
                    SRVDesc.Texture2DArray.ArraySize = arraySize;
                }
                else
                {
                    SRVDesc.Dimension           = ShaderResourceViewDimension.Texture2D;
                    SRVDesc.Texture2D.MipLevels = (mipCount == 0) ? -1 : desc.MipLevels;
                }

                textureView = new ShaderResourceView(d3dDevice, tex, SRVDesc);
                texture     = tex.QueryInterface <Texture2D>();
            }
        }
        break;

        case ResourceDimension.Texture3D: {
            Texture3DDescription desc;
            desc.Width          = width;
            desc.Height         = height;
            desc.Depth          = depth;
            desc.MipLevels      = mipCount;
            desc.Format         = format;
            desc.Usage          = usage;
            desc.BindFlags      = bindFlags;
            desc.CpuAccessFlags = cpuAccessFlags;
            desc.OptionFlags    = miscFlags & ~ResourceOptionFlags.TextureCube;

            using (Texture3D tex = new Texture3D(d3dDevice, desc, initData)) {
                ShaderResourceViewDescription SRVDesc = default(ShaderResourceViewDescription);
                SRVDesc.Format = format;

                SRVDesc.Dimension           = ShaderResourceViewDimension.Texture3D;
                SRVDesc.Texture3D.MipLevels = (mipCount == 0) ? -1 : desc.MipLevels;

                textureView = new ShaderResourceView(d3dDevice, tex, SRVDesc);
                texture     = tex.QueryInterface <Texture3D>();
            }
        }
        break;

        default:
            throw new SharpDXException(Result.Fail);
        }
    }
Esempio n. 20
0
            private static bool ValidateTexture(HeaderDXT10 header, HeaderFlags flags, out int depth, out PixelFormat format, out ResourceDimension resDim, out int arraySize, out bool isCubeMap)
            {
                depth     = 0;
                format    = Renderer.PixelFormat.Unknown;
                resDim    = ResourceDimension.Unknown;
                arraySize = 0x1;
                isCubeMap = false;

                arraySize = header.ArraySize;
                if (arraySize == 0)
                {
                    return(false);
                }


                if (header.MiscFlag2 != FlagsDX10.AlphaModeUnknown)
                {
                    return(false);
                }


                if (DDSPixelFormat.BitsPerPixel(header.DXGIFormat) == 0)
                {
                    return(false);
                }


                format = header.DXGIFormat;

                switch (header.Dimension)
                {
                case ResourceDimension.Texture1D:
                    depth = 1;
                    break;

                case ResourceDimension.Texture2D:
                    if (header.MiscFlag.HasFlag(ResourceOptionFlags.TextureCube))
                    {
                        arraySize *= 6;
                        isCubeMap  = true;
                    }
                    depth = 1;
                    break;

                case ResourceDimension.Texture3D:
                    if (!flags.HasFlag(HeaderFlags.Depth))
                    {
                        return(false);
                    }


                    if (arraySize > 1)
                    {
                        return(false);
                    }

                    break;

                default:
                    return(false);
                }

                resDim = header.Dimension;

                return(true);
            }
Esempio n. 21
0
            private static bool ValidateTexture(DdsHeader header, out int depth, out PixelFormat format, out ResourceDimension resDim, out int arraySize, out bool isCubeMap)
            {
                depth     = 0x0;
                format    = Renderer.PixelFormat.Unknown;
                resDim    = ResourceDimension.Unknown;
                arraySize = 1;
                isCubeMap = false;

                format = header.PixelFormat.GetDXGIFormat();
                if (format == Renderer.PixelFormat.Unknown)
                {
                    return(false);
                }


                if (header.Flags.HasFlag(HeaderFlags.Depth))
                {
                    resDim = ResourceDimension.Texture3D;
                }

                else
                {
                    if (header.SurfaceFlags.HasFlag(SurfaceFlags.Cubemap))
                    {
                        // We require all six faces to be defined
                        if ((header.CubemapFlags & CubemapFlags.AllFaces) != CubemapFlags.AllFaces)
                        {
                            return(false);
                        }


                        arraySize = 6;
                        isCubeMap = true;
                    }

                    depth  = 1;
                    resDim = ResourceDimension.Texture2D;
                }

                return(true);
            }
Esempio n. 22
0
        private Result CaptureTextureFix(DeviceContext deviceContext,
                                         Texture2D source,
                                         out Texture2DDescription desc,
                                         out Texture2D staging)
        {
            desc    = new Texture2DDescription();
            staging = null;

            if (deviceContext == null || source == null)
            {
                return(Result.InvalidArg);
            }

            ResourceDimension resType = source.Dimension;

            if (resType != ResourceDimension.Texture2D)
            {
                //string message = SharpDX.Diagnostics.ErrorManager.GetErrorMessage(0);
                //return Result.GetResultFromWin32Error(ERROR_NOT_SUPPORTED)
            }

            desc = source.Description;

            Device d3dDevice = deviceContext.Device;

            //Texture2D staging = null;

            if (desc.SampleDescription.Count > 1)
            {
                desc.SampleDescription.Count   = 1;
                desc.SampleDescription.Quality = 0;

                Texture2D temp;

                try {
                    temp = new Texture2D(d3dDevice, desc);
                } catch (SharpDXException e) {
                    return(e.ResultCode);
                }

                DXGI.Format fmt = EnsureNotTypeless(desc.Format);

                FormatSupport support = FormatSupport.None;
                try {
                    support = d3dDevice.CheckFormatSupport(fmt);
                } catch (SharpDXException e) {
                    return(e.ResultCode);
                }

                if ((support & FormatSupport.MultisampleResolve) == 0)
                {
                    return(Result.Fail);
                }

                for (int item = 0; item < desc.ArraySize; ++item)
                {
                    for (int level = 0; level < desc.MipLevels; ++level)
                    {
                        int index = Resource.CalculateSubResourceIndex(level, item, desc.MipLevels);
                        deviceContext.ResolveSubresource(temp, index, source, index, fmt);
                    }
                }

                desc.BindFlags      = 0;
                desc.OptionFlags   &= ResourceOptionFlags.TextureCube;
                desc.CpuAccessFlags = CpuAccessFlags.Read;
                desc.Usage          = ResourceUsage.Staging;

                try {
                    staging = new Texture2D(d3dDevice, desc);
                    deviceContext.CopyResource(temp, staging);
                } catch (SharpDXException e) {
                    return(e.ResultCode);
                }
            }
            else if (desc.Usage == ResourceUsage.Staging && desc.CpuAccessFlags == CpuAccessFlags.Read)
            {
                staging = source;
            }
            else
            {
                desc.BindFlags      = 0;
                desc.OptionFlags   &= ResourceOptionFlags.TextureCube;
                desc.CpuAccessFlags = CpuAccessFlags.Read;
                desc.Usage          = ResourceUsage.Staging;

                try {
                    staging = new Texture2D(d3dDevice, desc);
                    deviceContext.CopyResource(source, staging);
                } catch (SharpDXException e) {
                    return(e.ResultCode);
                }
            }

            return(Result.Ok);
        }
Esempio n. 23
0
        public DdsTexture(DxgiFormat dxgiFormat,
            int width, int height = 1, int depth = 1, 
            int arraySize = 1, int mipCount = 0,
            ResourceDimension dimension = ResourceDimension.Unknown, 
            ResourceMiscFlags miscFlags = 0
            )
        {
            if (dxgiFormat == DxgiFormat.UNKNOWN)
                throw new ArgumentException("Formats can not be 'unknown'");
            if (width <= 0)
                throw new ArgumentException("Width must be greater than zero");
            if (height <= 0)
                throw new ArgumentException("Height must be greater than zero");
            if (depth <= 0)
                throw new ArgumentException("Depth must be greater than zero");
            if (arraySize <= 0)
                throw new ArgumentException("Array size must be greater than zero");
            if (mipCount < 0)
                throw new ArgumentException("Mip count must can not be negative");
            if (dimension != ResourceDimension.Unknown && dimension != ResourceDimension.Texture3D && depth != 1)
                throw new ArgumentException(string.Format("Dimension10 is {0}, but Depth is not 1", dimension));
            if ((dimension == ResourceDimension.Buffer || dimension == ResourceDimension.Texture1D) && height != 1)
                throw new ArgumentException(string.Format("Dimension10 is {0}, but Height is not 1", dimension));
            if (dimension == ResourceDimension.Buffer && mipCount > 1)
                throw new ArgumentException("Dimension10 is Buffer, but MipCount is not 0 or 1");

            Width = width;
            Height = height;
            Depth = depth;
            /*
            if (depth != 1)
                Dimension10 = ResourceDimension.Texture3D;
            else if (height != 1)
                Dimension10 = ResourceDimension.Texture2D;
            else
                Dimension10 = ResourceDimension.Texture1D;*/

            MiscFlags10 = miscFlags;

            D3DFormat = Helper.D3DFormatFromDxgi(dxgiFormat);

            Data = new byte[arraySize][];

            if (mipCount == 0)
            {
                int mipWidth = width;
                int mipHeight = height;
                int mipDepth = depth;

                mipCount = 1;
                while (mipWidth != 1 || mipHeight != 1 || mipDepth != 1)
                {
                    if (mipWidth != 1) mipWidth /= 2;
                    if (mipHeight != 1) mipHeight /= 2;
                    if (mipDepth != 1) mipDepth /= 2;
                    mipCount++;
                }
            }

            int chainSize;
            CalculateMipInfos(mipCount, -1, -1, out chainSize);

            for (int i = 0; i < Data.Length; i++)
                Data[i] = new byte[chainSize];
        }
Esempio n. 24
0
        /// <summary>
        /// 建立一個臨時的 Texture 以便擷取資源
        /// </summary>
        /// <param name="source">來源texture</param>
        /// <param name="staging">複本texture</param>
        /// <returns></returns>
        private static Result CreateStagingTexture(DeviceContext deviceContext, Resource source, out Texture2DDescription desc, out Texture2D staging)
        {
            desc    = new Texture2DDescription();
            staging = null;
            if (deviceContext == null && source == null)
            {
                return(Result.InvalidArg);
            }

            ResourceDimension resourceDimension = source.Dimension;

            if (resourceDimension != ResourceDimension.Texture2D)
            {
                return(Result.InvalidArg);
            }

            if (!(source.QueryInterface <Texture2D>() is Texture2D src))
            {
                return(Result.Fail);
            }
            desc = src.Description;
            var d3dDevice = deviceContext.Device;

            if (desc.SampleDescription.Count > 1)
            {
                desc.SampleDescription.Count   = 1;
                desc.SampleDescription.Quality = 0;

                Texture2D temp;

                try {
                    temp = new Texture2D(d3dDevice, desc);
                } catch (SharpDXException e) {
                    return(e.ResultCode);
                }

                DXGI.Format fmt = desc.Format.EnsureNotTypeless();

                FormatSupport support = FormatSupport.None;
                try {
                    support = d3dDevice.CheckFormatSupport(fmt);
                } catch (SharpDXException e) {
                    return(e.ResultCode);
                }

                if ((support & FormatSupport.MultisampleResolve) == 0)
                {
                    return(Result.Fail);
                }

                for (int item = 0; item < desc.ArraySize; ++item)
                {
                    for (int level = 0; level < desc.MipLevels; ++level)
                    {
                        int index = Resource.CalculateSubResourceIndex(level, item, desc.MipLevels);
                        deviceContext.ResolveSubresource(temp, index, source, index, fmt);
                    }
                }

                desc.BindFlags      = BindFlags.None;
                desc.OptionFlags   &= ResourceOptionFlags.TextureCube;
                desc.CpuAccessFlags = CpuAccessFlags.Read;
                desc.Usage          = ResourceUsage.Staging;

                try {
                    staging = new Texture2D(d3dDevice, desc);
                    deviceContext.CopyResource(temp, staging);
                } catch (SharpDXException e) {
                    return(e.ResultCode);
                }
            }
            else if (desc.Usage == ResourceUsage.Staging && desc.CpuAccessFlags == CpuAccessFlags.Read)
            {
                staging = source.QueryInterface <Texture2D>();
            }
            else
            {
                desc.BindFlags      = BindFlags.None;
                desc.OptionFlags   &= ResourceOptionFlags.TextureCube;
                desc.CpuAccessFlags = CpuAccessFlags.Read;
                desc.Usage          = ResourceUsage.Staging;

                try {
                    staging = new Texture2D(d3dDevice, desc);
                    if (staging != null)
                    {
                        deviceContext.CopyResource(source, staging);
                    }
                    else
                    {
                        return(Result.Fail);
                    }
                } catch (SharpDXException e) {
                    return(e.ResultCode);
                } catch (Exception ex) {
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
                    return(Result.Fail);
                }
            }

            return(Result.Ok);
        }
        static Resource CreateTextureFromDDS(Device d3dDevice, DDS_HEADER header, DDS_HEADER_DXT10?header10, byte[] bitData, int offset, int maxsize, out bool isCubeMap)
        {
            int width  = header.width;
            int height = header.height;
            int depth  = header.depth;

            ResourceDimension resDim = ResourceDimension.Unknown;
            int    arraySize         = 1;
            Format format            = Format.Unknown;

            isCubeMap = false;

            int mipCount = header.mipMapCount;

            if (0 == mipCount)
            {
                mipCount = 1;
            }

            if (((header.ddspf.flags & DDS_FOURCC) > 0) && (MAKEFOURCC('D', 'X', '1', '0') == header.ddspf.fourCC))
            {
                DDS_HEADER_DXT10 d3d10ext = header10.Value;

                arraySize = d3d10ext.arraySize;
                if (arraySize == 0)
                {
                    throw new Exception();
                }

                if (BitsPerPixel(d3d10ext.dxgiFormat) == 0)
                {
                    throw new Exception();
                }

                format = d3d10ext.dxgiFormat;

                switch ((ResourceDimension)d3d10ext.resourceDimension)
                {
                case ResourceDimension.Texture1D:
                    // D3DX writes 1D textures with a fixed Height of 1
                    if ((header.flags & DDS_HEIGHT) > 0 && height != 1)
                    {
                        throw new Exception();
                    }
                    height = depth = 1;
                    break;

                case ResourceDimension.Texture2D:
                    //D3D11_RESOURCE_MISC_TEXTURECUBE
                    if ((d3d10ext.miscFlag & 0x4) > 0)
                    {
                        arraySize *= 6;
                        isCubeMap  = true;
                    }
                    depth = 1;
                    break;

                case ResourceDimension.Texture3D:
                    if (!((header.flags & DDS_HEADER_FLAGS_VOLUME) > 0))
                    {
                        throw new Exception();
                    }

                    if (arraySize > 1)
                    {
                        throw new Exception();
                    }
                    break;

                default:
                    throw new Exception();
                }

                resDim = (ResourceDimension)d3d10ext.resourceDimension;
            }
            else
            {
                format = GetDXGIFormat(header.ddspf);

                if (format == Format.Unknown)
                {
                    throw new Exception();
                }

                if ((header.flags & DDS_HEADER_FLAGS_VOLUME) > 0)
                {
                    resDim = ResourceDimension.Texture3D;
                }
                else
                {
                    if ((header.caps2 & DDS_CUBEMAP) > 0)
                    {
                        // We require all six faces to be defined
                        if ((header.caps2 & DDS_CUBEMAP_ALLFACES) != DDS_CUBEMAP_ALLFACES)
                        {
                            throw new Exception();
                        }

                        arraySize = 6;
                        isCubeMap = true;
                    }

                    depth  = 1;
                    resDim = ResourceDimension.Texture2D;
                }
            }
            var resource = d3dDevice.CreateCommittedResource(new HeapProperties(CpuPageProperty.WriteBack, MemoryPool.L0), HeapFlags.None,
                                                             new ResourceDescription()
            {
                //Alignment = -1,
                Dimension         = resDim,
                DepthOrArraySize  = (short)arraySize,
                Flags             = ResourceFlags.None,
                Format            = format,
                Height            = height,
                Layout            = TextureLayout.Unknown,
                MipLevels         = (short)mipCount,
                SampleDescription = new SampleDescription(1, 0),
                Width             = width,
            },
                                                             ResourceStates.GenericRead);

            FillInitData(resource, width, height, depth, mipCount, arraySize, format, 0, 0, bitData, offset);

            return(resource);
        }
Esempio n. 26
0
 public override string ToString()
 {
     return(string.Format("{0}_{1}{2} ({3}) t{4}", TypeDescription, ResourceDimension.GetDescription(),
                          (IsMultiSampled) ? "(" + SampleCount + ")" : string.Empty, ReturnType, Operand.Indices[0].Value));
 }
Esempio n. 27
0
        /// <summary>
        /// 深度テクスチャーを作成する。単体で使用する場合はdimensionとdescを入力し、
        ///  CreateTextureXX,SetSourcFileTexture,SetSourcMemoryTextureは、呼び出していないものとする。
        ///  呼び出していた場合は、エラーが発生する
        /// </summary>
        /// <param name="device">デバイス</param>
        /// <param name="dimension">1D,2Dのみ(単体で作り場合のみ使用) 省略時、ResourceDimension.Unknown</param>
        /// <param name="desc">1D,2Dのみ(単体で作り場合のみ使用) 省略時、null</param>
        /// <param name="isCreateShaderResourceView">シェーダーリソースビューを作成するか? 省略時、true</param>
        /// <returns></returns>
        internal int CreateDepthTexture(ResourceDimension dimension = ResourceDimension.Unknown, Texture123DDesc desc = null, bool isCreateShaderResourceView = true)
        {
            if (m_resourceDepth != null)
            {
                Debug.Assert(false); throw new CreatedException();
            }
            if (m_createdTxNum == 0 && (dimension != ResourceDimension.Unknown || desc == null))
            {
                Debug.Assert(false); throw new ArgumentException();
            }

            if (m_createdTxNum == 0 && m_type == ResourceDimension.Unknown)
            {
                m_type = dimension;
            }


            try
            {
                DepthStencilViewDescription   descDSV = new DepthStencilViewDescription();
                ShaderResourceViewDescription srvd    = new ShaderResourceViewDescription();
                switch (m_type)
                {
                case ResourceDimension.Texture1D:
                {
                    Texture1DDescription descDepth = desc != null ? desc.D1 : m_descTx.D1;
                    descDepth.BindFlags = BindFlags.DepthStencil | (isCreateShaderResourceView ? BindFlags.ShaderResource : 0);
                    descDepth.Format    = Format.R32_Typeless;
                    m_depth.D1          = new Texture1D(App.DXDevice, descDepth);
                    if (m_depth.D1 == null)
                    {
                        throw new CreateFailedException();
                    }

                    m_depth.D1.DebugName     = "Depth Texture1D" + m_name;
                    m_descDepth.d1           = descDepth;
                    m_resourceDepth          = (SharpDX.Direct3D11.Resource)(m_depth.D1);
                    descDSV.Dimension        = DepthStencilViewDimension.Texture1D;
                    srvd.Dimension           = ShaderResourceViewDimension.Texture1D;
                    srvd.Texture2D.MipLevels = descDepth.MipLevels;
                    break;
                }

                case ResourceDimension.Texture2D:
                {
                    Texture2DDescription descDepth = desc != null ? desc.D2 : m_descTx.D2;
                    descDepth.BindFlags = BindFlags.DepthStencil | (isCreateShaderResourceView ? BindFlags.ShaderResource : 0);
                    descDepth.Format    = Format.R32_Typeless;
                    m_depth.D2          = new Texture2D(App.DXDevice, descDepth);
                    if (m_depth.D2 == null)
                    {
                        throw new CreateFailedException();
                    }

                    m_depth.D2.DebugName     = "Depth Texture2D" + m_name;
                    m_descDepth.d2           = descDepth;
                    m_resourceDepth          = (SharpDX.Direct3D11.Resource)(m_depth.D2);
                    descDSV.Dimension        = DepthStencilViewDimension.Texture2D;
                    srvd.Dimension           = ShaderResourceViewDimension.Texture2D;
                    srvd.Texture2D.MipLevels = descDepth.MipLevels;
                    break;
                }

                default:
                    throw new CreateFailedException();
                }
                // 深度ステンシルビュー
                descDSV.Format             = Format.D32_Float;
                descDSV.Flags              = 0;
                descDSV.Texture2D.MipSlice = 0;
                m_depthStencilView         = new DepthStencilView(App.DXDevice, m_resourceDepth, descDSV);

                if (m_depthStencilView == null)
                {
                    throw new CreateFailedException();
                }

                // リソースビュー
                if (isCreateShaderResourceView)
                {
                    srvd.Format = Format.R32_Float;
                    srvd.Texture2D.MostDetailedMip = 0;
                    m_depthResourceView            = new ShaderResourceView(App.DXDevice, m_resourceDepth, srvd);
                    if (m_depthResourceView == null)
                    {
                        throw new CreateFailedException();
                    }
                }
                m_depthStencilView.DebugName = "DepthStencilView=>" + m_name;
                m_resourceDepth.DebugName    = m_name;
            }
            catch
            {
                if (m_depthStencilView != null)
                {
                    m_depthStencilView.Dispose();
                }
                if (m_resourceDepth != null)
                {
                    m_resourceDepth.Dispose();
                }
                if (m_depth.D1 != null)
                {
                    m_depth.D1.Dispose();
                }
                if (m_depth.D2 != null)
                {
                    m_depth.D2.Dispose();
                }
                m_depthStencilView = null;
                m_resourceDepth    = null;
                m_depth.D1         = null;
                m_depth.D2         = null;

                HasDepthStencil = false;
                return(-1);
            }

            HasDepthStencil = true;
            return(0);
        }
Esempio n. 28
0
        private static Result CreateTextureFromDDS(
            Device d3dDevice,
            DeviceContext deviceContext,
            DDS_HEADER header,
            DDS_HEADER_DXT10?header10,
            IntPtr bitData,
            int bitsize,
            int maxsize,
            ResourceUsage usage,
            BindFlags bindFlags,
            CpuAccessFlags cpuAccessFlags,
            ResourceOptionFlags resourceOptionFlags,
            bool forceSRGB,
            out Resource texture, out ShaderResourceView textureView)
        {
            texture     = null;
            textureView = null;

            int width  = Convert.ToInt32(header.width);
            int height = Convert.ToInt32(header.height);
            int depth  = Convert.ToInt32(header.depth);

            ResourceDimension resourceDimension = ResourceDimension.Unknown;
            int arraySize = 1;

            DXGI.Format format    = DXGI.Format.Unknown;
            bool        isCubeMap = false;

            int mipCount = Convert.ToInt32(header.mipMapCount);

            if (0 == mipCount)
            {
                mipCount = 1;
            }

            if (header.IsDX10)
            {
                var d3d10ext = header10.Value;
                arraySize = Convert.ToInt32(d3d10ext.arraySize);
                if (arraySize == 0)
                {
                    return(Result.InvalidArg);
                }

                switch (d3d10ext.dxgiFormat)
                {
                case DXGI.Format.AI44:
                case DXGI.Format.IA44:
                case DXGI.Format.P8:
                case DXGI.Format.A8P8:
                    return(Result.InvalidArg);

                default:
                    if (d3d10ext.dxgiFormat.BitPerPixel() == 0)
                    {
                        return(Result.InvalidArg);
                    }
                    break;
                }

                format = d3d10ext.dxgiFormat;

                switch (d3d10ext.resourceDimension)
                {
                case ResourceDimension.Texture1D:
                    // D3DX writes 1D textures with a fixed Height of 1
                    if ((header.flags.HasFlag(DDS_Header.Height)) && height != 1)
                    {
                        return(Result.InvalidArg);
                    }
                    height = depth = 1;
                    break;

                case ResourceDimension.Texture2D:
                    if (d3d10ext.miscFlag.HasFlag(ResourceOptionFlags.TextureCube))
                    {
                        arraySize *= 6;
                        isCubeMap  = true;
                    }
                    depth = 1;
                    break;

                case ResourceDimension.Texture3D:
                    if (!(header.flags.HasFlag(DDS_Header.Volume)))
                    {
                        return(Result.InvalidArg);
                    }

                    if (arraySize > 1)
                    {
                        return(Result.InvalidArg);
                    }
                    break;

                default:
                    return(Result.InvalidArg);
                }
                resourceDimension = d3d10ext.resourceDimension;
            }
            else
            {
                format = header.ddsPixelFormat.Format;

                if (format == DXGI.Format.Unknown)
                {
                    return(Result.InvalidArg);
                }

                if (header.flags.HasFlag(DDS_Header.Volume))
                {
                    resourceDimension = ResourceDimension.Texture3D;
                }
                else
                {
                    if (header.Caps2.HasFlag(DDS_CubeMap.CubeMap))
                    {
                        // We require all six faces to be defined
                        if (header.Caps2.HasFlag(DDS_CubeMap.AllFaces))
                        {
                            return(Result.InvalidArg);
                        }

                        arraySize = 6;
                        isCubeMap = true;
                    }

                    depth             = 1;
                    resourceDimension = ResourceDimension.Texture2D;

                    // Note there's no way for a legacy Direct3D 9 DDS to express a '1D' texture
                }

                if (format.BitPerPixel() == 0)
                {
                    return(Result.InvalidArg);
                }
            }

            // Bound sizes (for security purposes we don't trust DDS file metadata larger than the D3D 11.x hardware requirements)
            if (mipCount > SharpDX.Direct3D11.Resource.MaximumMipLevels)
            {
                return(Result.InvalidArg);
            }

            switch (resourceDimension)
            {
            case ResourceDimension.Texture1D:
                if (arraySize > Resource.MaximumTexture1DArraySize || width > Resource.MaximumTexture1DSize)
                {
                    return(Result.InvalidArg);
                }
                break;

            case ResourceDimension.Texture2D:
                if (isCubeMap)
                {
                    if (arraySize > Resource.MaximumTexture2DArraySize || width > Resource.MaximumTexture2DSize || height > Resource.MaximumTexture2DSize)
                    {
                        return(Result.InvalidArg);
                    }
                }
                break;

            case ResourceDimension.Texture3D:
                if (arraySize > 1 ||
                    width > Resource.MaximumTexture3DSize ||
                    height > Resource.MaximumTexture3DSize ||
                    depth > Resource.MaximumTexture3DSize)
                {
                    return(Result.InvalidArg);
                }
                break;
            }

            // auto generate mipmaps
            bool autoGen = false;

            ////
            ////

            if (autoGen)
            {
            }
            else
            {
                DataBox[] initData = new DataBox[mipCount * arraySize];

                if (FillInitData(width, height, depth, mipCount, arraySize, format, bitData, bitsize, maxsize, out int twidth, out int theight, out int tdepth, out int skipMip, initData) == Result.Ok)
                {
                    var result = CreateD3DResources(d3dDevice, resourceDimension, twidth, theight, tdepth, mipCount - skipMip, arraySize,
                                                    format, usage, bindFlags, cpuAccessFlags, resourceOptionFlags, forceSRGB, isCubeMap, initData, out texture, out textureView);

                    if (!result.Success && maxsize == 0 && (mipCount > 1))
                    {
                        // Retry with a maxsize determined by feature level

                        switch (d3dDevice.FeatureLevel)
                        {
                        case FeatureLevel.Level_9_1:
                        case FeatureLevel.Level_9_2:
                            if (isCubeMap)
                            {
                                maxsize = 512 /*D3D_FL9_1_REQ_TEXTURECUBE_DIMENSION*/;
                            }
                            else
                            {
                                maxsize = (resourceDimension == ResourceDimension.Texture3D)
                                        ? 256 /*D3D_FL9_1_REQ_TEXTURE3D_U_V_OR_W_DIMENSION*/
                                        : 2048 /*D3D_FL9_1_REQ_TEXTURE2D_U_OR_V_DIMENSION*/;
                            }
                            break;

                        case FeatureLevel.Level_9_3:
                            maxsize = (resourceDimension == ResourceDimension.Texture3D)
                                    ? 256 /*D3D_FL9_1_REQ_TEXTURE3D_U_V_OR_W_DIMENSION*/
                                    : 4096 /*D3D_FL9_3_REQ_TEXTURE2D_U_OR_V_DIMENSION*/;
                            break;

                        default:           // D3D_FEATURE_LEVEL_10_0 & D3D_FEATURE_LEVEL_10_1
                            maxsize = (resourceDimension == ResourceDimension.Texture3D)
                                    ? 2048 /*D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION*/
                                    : 8192 /*D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION*/;
                            break;
                        }

                        if (FillInitData(width, height, depth, mipCount, arraySize, format, bitData, bitsize, maxsize,
                                         out twidth, out theight, out tdepth, out skipMip, initData).Success)
                        {
                            var hr = CreateD3DResources(d3dDevice, resourceDimension, twidth, theight, tdepth, mipCount - skipMip, arraySize,
                                                        format, usage, bindFlags, cpuAccessFlags, resourceOptionFlags, forceSRGB,
                                                        isCubeMap, initData, out texture, out textureView);
                        }
                    }
                }
            }

            return(Result.False);
        }
Esempio n. 29
0
        private static ShaderResourceView InitTextureFromData(Device d3dDevice, DeviceContext context, DDS_HEADER header, DDS_HEADER_DXT10?header10, byte[] bitData, int offset, int maxsize, out bool isCubeMap)
        {
            int width  = header.width;
            int height = header.height;
            int depth  = header.depth;

            ResourceDimension resDim = ResourceDimension.Unknown;
            int    arraySize         = 1;
            Format format            = Format.Unknown;

            isCubeMap = false;

            int mipCount = header.mipMapCount;

            if (0 == mipCount)
            {
                mipCount = 1;
            }

            if (((header.ddspf.flags & DDS_FOURCC) > 0) && (MAKEFOURCC('D', 'X', '1', '0') == header.ddspf.fourCC))
            {
                DDS_HEADER_DXT10 d3d10ext = header10.Value;

                arraySize = d3d10ext.arraySize;
                if (arraySize == 0)
                {
                    throw new Exception();
                }

                if (BitsPerPixel(d3d10ext.dxgiFormat) == 0)
                {
                    throw new Exception();
                }

                format = d3d10ext.dxgiFormat;

                switch ((ResourceDimension)d3d10ext.resourceDimension)
                {
                case ResourceDimension.Texture1D:
                    // D3DX writes 1D textures with a fixed Height of 1
                    if ((header.flags & DDS_HEIGHT) > 0 && height != 1)
                    {
                        throw new Exception();
                    }
                    height = depth = 1;
                    break;

                case ResourceDimension.Texture2D:
                    //D3D11_RESOURCE_MISC_TEXTURECUBE
                    if ((d3d10ext.miscFlag & 0x4) > 0)
                    {
                        arraySize *= 6;
                        isCubeMap  = true;
                    }
                    depth = 1;
                    break;

                case ResourceDimension.Texture3D:
                    if (!((header.flags & DDS_HEADER_FLAGS_VOLUME) > 0))
                    {
                        throw new Exception();
                    }

                    if (arraySize > 1)
                    {
                        throw new Exception();
                    }
                    break;

                default:
                    throw new Exception();
                }

                resDim = (ResourceDimension)d3d10ext.resourceDimension;
            }
            else
            {
                format = GetDXGIFormat(header.ddspf);

                if (format == Format.Unknown)
                {
                    throw new Exception();
                }

                if ((header.flags & DDS_HEADER_FLAGS_VOLUME) > 0)
                {
                    resDim = ResourceDimension.Texture3D;
                }
                else
                {
                    if ((header.caps2 & DDS_CUBEMAP) > 0)
                    {
                        // We require all six faces to be defined
                        if ((header.caps2 & DDS_CUBEMAP_ALLFACES) != DDS_CUBEMAP_ALLFACES)
                        {
                            throw new Exception();
                        }

                        arraySize = 6;
                        isCubeMap = true;
                    }

                    depth  = 1;
                    resDim = ResourceDimension.Texture2D;
                }
            }

            Resource resource = null;


            GCHandle pinnedArray = GCHandle.Alloc(bitData, GCHandleType.Pinned);
            IntPtr   pointer     = pinnedArray.AddrOfPinnedObject();
            var      boxes       = FillInitData(pointer, width, height, depth, mipCount, arraySize, format, 0, 0, offset);


            switch (resDim)
            {
            case ResourceDimension.Unknown:
                break;

            case ResourceDimension.Buffer:
                break;

            case ResourceDimension.Texture1D:
                resource = new Texture1D(d3dDevice, new Texture1DDescription()
                {
                    BindFlags      = BindFlags.ShaderResource,
                    Format         = format,
                    ArraySize      = arraySize,
                    Width          = width,
                    CpuAccessFlags = CpuAccessFlags.None,
                    MipLevels      = mipCount,
                    OptionFlags    = ResourceOptionFlags.None,
                    Usage          = ResourceUsage.Default,
                }, boxes.ToArray());
                break;

            case ResourceDimension.Texture2D:
                resource = new Texture2D(d3dDevice, new Texture2DDescription()
                {
                    ArraySize         = arraySize,
                    BindFlags         = BindFlags.ShaderResource,
                    Format            = format,
                    Height            = height,
                    Width             = width,
                    CpuAccessFlags    = CpuAccessFlags.None,
                    MipLevels         = mipCount,
                    OptionFlags       = ResourceOptionFlags.None,
                    SampleDescription = new SampleDescription(1, 0),
                    Usage             = ResourceUsage.Default
                }, boxes.ToArray());
                break;

            case ResourceDimension.Texture3D:
                resource = new Texture3D(d3dDevice, new Texture3DDescription()
                {
                    Depth          = depth,
                    BindFlags      = BindFlags.ShaderResource,
                    Format         = format,
                    Height         = height,
                    Width          = width,
                    CpuAccessFlags = CpuAccessFlags.None,
                    MipLevels      = mipCount,
                    OptionFlags    = ResourceOptionFlags.None,
                    Usage          = ResourceUsage.Default
                }, boxes.ToArray());
                break;

            default:
                break;
            }
            pinnedArray.Free();


            var resourceView = new ShaderResourceView(d3dDevice, resource);

            return(resourceView);
        }
Esempio n. 30
0
        private static Result CreateD3DResources(Device d3dDevice,
                                                 ResourceDimension resourceDimension,
                                                 int width,
                                                 int height,
                                                 int depth,
                                                 int mipCount,
                                                 int arraySize,
                                                 DXGI.Format format,
                                                 ResourceUsage usage,
                                                 BindFlags bindFlags,
                                                 CpuAccessFlags cpuAccessFlags,
                                                 ResourceOptionFlags miscFlags,
                                                 bool forceSRGB,
                                                 bool isCubeMap,
                                                 DataBox[] initData,
                                                 out Resource texture,
                                                 out ShaderResourceView textureView)
        {
            texture     = null;
            textureView = null;
            if (d3dDevice == null)
            {
                return(Result.InvalidArg);
            }

            if (forceSRGB)
            {
                format = format.MakeSRgb();
            }

            ShaderResourceViewDescription SRVDesc = new ShaderResourceViewDescription {
                Format = format
            };

            switch (resourceDimension)
            {
            case ResourceDimension.Texture1D: {
                Texture1DDescription desc = new Texture1DDescription {
                    Width          = width,
                    MipLevels      = mipCount,
                    ArraySize      = arraySize,
                    Format         = format,
                    Usage          = usage,
                    BindFlags      = bindFlags,
                    CpuAccessFlags = cpuAccessFlags,
                    OptionFlags    = miscFlags & ~ResourceOptionFlags.TextureCube
                };

                Texture1D tex = new Texture1D(d3dDevice, desc, initData);

                if (tex != null)
                {
                    if (arraySize > 1)
                    {
                        SRVDesc.Dimension = ShaderResourceViewDimension.Texture1DArray;
                        SRVDesc.Texture1DArray.MipLevels = (mipCount == 0) ? -1 : desc.MipLevels;
                        SRVDesc.Texture1DArray.ArraySize = arraySize;
                    }
                    else
                    {
                        SRVDesc.Dimension           = ShaderResourceViewDimension.Texture1D;
                        SRVDesc.Texture1D.MipLevels = (mipCount == 0) ? -1 : desc.MipLevels;
                    }

                    textureView = new ShaderResourceView(d3dDevice, tex, SRVDesc);

                    if (textureView == null)
                    {
                        tex.Dispose();
                        texture = null;
                        return(Result.Fail);
                    }
                    texture = tex;
                }
                else
                {
                    return(Result.Fail);
                }
            }
            break;

            case ResourceDimension.Texture2D: {
                Texture2DDescription desc = new Texture2DDescription {
                    Width             = width,
                    Height            = height,
                    MipLevels         = mipCount,
                    ArraySize         = arraySize,
                    Format            = format,
                    SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                    Usage             = usage,
                    BindFlags         = bindFlags,
                    CpuAccessFlags    = cpuAccessFlags,
                };
                if (isCubeMap)
                {
                    desc.OptionFlags = miscFlags | ResourceOptionFlags.TextureCube;
                }
                else
                {
                    desc.OptionFlags = miscFlags & ~ResourceOptionFlags.TextureCube;
                }

                Texture2D tex = new Texture2D(d3dDevice, desc, initData);

                if (tex != null)
                {
                    if (isCubeMap)
                    {
                        if (arraySize > 6)
                        {
                            SRVDesc.Dimension = ShaderResourceViewDimension.TextureCubeArray;
                            SRVDesc.TextureCubeArray.MipLevels = (mipCount == 0) ? -1 : desc.MipLevels;
                            // Earlier we set arraySize to (NumCubes * 6)
                            SRVDesc.TextureCubeArray.CubeCount = arraySize / 6;
                        }
                        else
                        {
                            SRVDesc.Dimension             = ShaderResourceViewDimension.TextureCube;
                            SRVDesc.TextureCube.MipLevels = (mipCount == 0) ? -1 : desc.MipLevels;
                        }
                    }
                    else if (arraySize > 1)
                    {
                        SRVDesc.Dimension = ShaderResourceViewDimension.Texture2DArray;
                        SRVDesc.Texture2DArray.MipLevels = (mipCount == 0) ? -1 : desc.MipLevels;
                        SRVDesc.Texture2DArray.ArraySize = arraySize;
                    }
                    else
                    {
                        SRVDesc.Dimension           = ShaderResourceViewDimension.Texture2D;
                        SRVDesc.Texture2D.MipLevels = (mipCount == 0) ? -1 : desc.MipLevels;
                    }

                    textureView = new ShaderResourceView(d3dDevice, tex, SRVDesc);

                    if (textureView == null)
                    {
                        tex.Dispose();
                        texture = null;
                        return(Result.Fail);
                    }
                    texture = tex;
                }
                else
                {
                    return(Result.Fail);
                }
            }
            break;

            case ResourceDimension.Texture3D: {
                Texture3DDescription desc = new Texture3DDescription {
                    Width          = width,
                    Height         = height,
                    Depth          = depth,
                    MipLevels      = mipCount,
                    Format         = format,
                    Usage          = usage,
                    BindFlags      = bindFlags,
                    CpuAccessFlags = cpuAccessFlags,
                    OptionFlags    = miscFlags & ~ResourceOptionFlags.TextureCube
                };

                Texture3D tex = new Texture3D(d3dDevice, desc, initData);

                if (tex != null)
                {
                    SRVDesc.Dimension           = ShaderResourceViewDimension.Texture3D;
                    SRVDesc.Texture3D.MipLevels = (mipCount == 0) ? -1 : desc.MipLevels;

                    textureView = new ShaderResourceView(d3dDevice, tex, SRVDesc);

                    if (textureView == null)
                    {
                        tex.Dispose();
                        texture = null;
                        return(Result.Fail);
                    }
                    texture = tex;
                }
                else
                {
                    return(Result.Fail);
                }
            }
            break;
            }

            return(Result.Ok);
        }