Exemple #1
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);
    }
Exemple #2
0
 public DdsProcessor(DdsHeader ddsHeader, DdsHeaderDxt10 ddsHeaderDxt10)
 {
     this.DdsHeader      = ddsHeader;
     this.DdsHeaderDxt10 = ddsHeaderDxt10;
 }