Exemplo n.º 1
0
        private static void CreateBitmapTexture(D3D11Device device, string fileName, out D3D11ShaderResourceView textureView)
        {
            int width;
            int height;

            byte[] bytes;

            using (var file = new Bitmap(fileName))
            {
                var rect   = new Rectangle(0, 0, file.Width, file.Height);
                int length = file.Width * file.Height;

                width  = file.Width;
                height = file.Height;
                bytes  = new byte[length * 4];

                using (var bitmap = file.Clone(rect, PixelFormat.Format32bppArgb))
                {
                    BitmapData data = bitmap.LockBits(rect, ImageLockMode.ReadOnly, bitmap.PixelFormat);

                    try
                    {
                        Marshal.Copy(data.Scan0, bytes, 0, length * 4);
                    }
                    finally
                    {
                        bitmap.UnlockBits(data);
                    }
                }
            }

            D3D11SubResourceData[] textureSubResData = new[]
            {
                new D3D11SubResourceData(bytes, (uint)width * 4)
            };

            var textureDesc = new D3D11Texture2DDesc(DxgiFormat.B8G8R8A8UNorm, (uint)width, (uint)height, 1, 1);

            using (var texture = device.CreateTexture2D(textureDesc, textureSubResData))
            {
                var textureViewDesc = new D3D11ShaderResourceViewDesc
                {
                    Format        = textureDesc.Format,
                    ViewDimension = D3D11SrvDimension.Texture2D,
                    Texture2D     = new D3D11Texture2DSrv
                    {
                        MipLevels       = textureDesc.MipLevels,
                        MostDetailedMip = 0
                    }
                };

                textureView = device.CreateShaderResourceView(texture, textureViewDesc);
            }
        }
Exemplo n.º 2
0
        static D3D11ShaderResourceView CreateBufferSRV(D3D11Device pDevice, D3D11Buffer pBuffer)
        {
            var descBuf = pBuffer.Description;

            D3D11ShaderResourceViewDesc desc;

            if (descBuf.MiscOptions.HasFlag(D3D11ResourceMiscOptions.BufferAllowRawViews))
            {
                // This is a Raw Buffer
                desc = new D3D11ShaderResourceViewDesc(pBuffer, DxgiFormat.R32Typeless, 0, descBuf.ByteWidth / 4, D3D11BufferExSrvOptions.Raw);
            }
            else if (descBuf.MiscOptions.HasFlag(D3D11ResourceMiscOptions.BufferStructured))
            {
                // This is a Structured Buffer
                desc = new D3D11ShaderResourceViewDesc(pBuffer, DxgiFormat.Unknown, 0, descBuf.ByteWidth / descBuf.StructureByteStride, D3D11BufferExSrvOptions.None);
            }
            else
            {
                throw new InvalidOperationException();
            }

            return(pDevice.CreateShaderResourceView(pBuffer, desc));
        }
        private void CreateTexture(byte[] data, uint width, uint height, out D3D11Texture2D texture, out D3D11ShaderResourceView textureView)
        {
            D3D11Texture2DDesc textureDesc = new D3D11Texture2DDesc(DxgiFormat.R8G8B8A8UNorm, width, height, 1, 1);

            D3D11SubResourceData[] textureSubResData = new[]
            {
                new D3D11SubResourceData(data, width * 4)
            };

            D3D11Texture2D          texture2D = this.d3dDevice.CreateTexture2D(textureDesc, textureSubResData);
            D3D11ShaderResourceView shaderResourceView;

            try
            {
                D3D11ShaderResourceViewDesc textureViewDesc = new D3D11ShaderResourceViewDesc
                {
                    Format        = textureDesc.Format,
                    ViewDimension = D3D11SrvDimension.Texture2D,
                    Texture2D     = new D3D11Texture2DSrv
                    {
                        MipLevels       = textureDesc.MipLevels,
                        MostDetailedMip = 0
                    }
                };

                shaderResourceView = this.d3dDevice.CreateShaderResourceView(texture2D, textureViewDesc);
            }
            catch
            {
                D3D11Utils.DisposeAndNull(ref texture2D);
                throw;
            }

            texture     = texture2D;
            textureView = shaderResourceView;
        }
        private static void CreateTextureFromDDS(
            D3D11Device device,
            D3D11DeviceContext context,
            DdsFile dds,
            byte[] bitData,
            int maxSize,
            D3D11Usage usage,
            D3D11BindOptions bindOptions,
            D3D11CpuAccessOptions cpuAccessOptions,
            D3D11ResourceMiscOptions miscOptions,
            bool forceSRGB,
            out D3D11Resource texture,
            out D3D11ShaderResourceView textureView)
        {
            int width  = dds.Width;
            int height = dds.Height;
            int depth  = dds.Depth;

            D3D11ResourceDimension resDim = (D3D11ResourceDimension)dds.ResourceDimension;
            int        arraySize          = Math.Max(1, dds.ArraySize);
            DxgiFormat format             = (DxgiFormat)dds.Format;
            bool       isCubeMap          = false;

            if (dds.Format == DdsFormat.Unknown)
            {
                if (dds.PixelFormat.RgbBitCount == 32)
                {
                    if (IsBitMask(dds.PixelFormat, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000))
                    {
                        format = DxgiFormat.B8G8R8X8UNorm;
                        int length = bitData.Length / 4;
                        var bytes  = new byte[length * 4];

                        for (int i = 0; i < length; i++)
                        {
                            bytes[i * 4 + 0] = bitData[i * 4 + 2];
                            bytes[i * 4 + 1] = bitData[i * 4 + 1];
                            bytes[i * 4 + 2] = bitData[i * 4 + 0];
                        }

                        bitData = bytes;
                    }
                }
                else if (dds.PixelFormat.RgbBitCount == 24)
                {
                    if (IsBitMask(dds.PixelFormat, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000))
                    {
                        format = DxgiFormat.B8G8R8X8UNorm;
                        int length = bitData.Length / 3;
                        var bytes  = new byte[length * 4];

                        for (int i = 0; i < length; i++)
                        {
                            bytes[i * 4 + 0] = bitData[i * 3 + 0];
                            bytes[i * 4 + 1] = bitData[i * 3 + 1];
                            bytes[i * 4 + 2] = bitData[i * 3 + 2];
                        }

                        bitData = bytes;
                    }
                }
            }

            int mipCount = Math.Max(1, dds.MipmapCount);

            switch (format)
            {
            case DxgiFormat.AI44:
            case DxgiFormat.IA44:
            case DxgiFormat.P8:
            case DxgiFormat.A8P8:
                throw new NotSupportedException(format.ToString() + " format is not supported.");

            default:
                if (DdsHelpers.GetBitsPerPixel((DdsFormat)format) == 0)
                {
                    throw new NotSupportedException(format.ToString() + " format is not supported.");
                }

                break;
            }

            switch (resDim)
            {
            case D3D11ResourceDimension.Texture1D:
                // D3DX writes 1D textures with a fixed Height of 1
                if ((dds.Options & DdsOptions.Height) != 0 && height != 1)
                {
                    throw new InvalidDataException();
                }

                height = 1;
                depth  = 1;
                break;

            case D3D11ResourceDimension.Texture2D:
                if ((dds.ResourceMiscOptions & DdsResourceMiscOptions.TextureCube) != 0)
                {
                    arraySize *= 6;
                    isCubeMap  = true;
                }

                depth = 1;
                break;

            case D3D11ResourceDimension.Texture3D:
                if ((dds.Options & DdsOptions.Depth) == 0)
                {
                    throw new InvalidDataException();
                }

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

            default:
                if ((dds.Options & DdsOptions.Depth) != 0)
                {
                    resDim = D3D11ResourceDimension.Texture3D;
                }
                else
                {
                    if ((dds.Caps2 & DdsAdditionalCaps.CubeMap) != 0)
                    {
                        // We require all six faces to be defined
                        if ((dds.Caps2 & DdsAdditionalCaps.CubeMapAllFaces) != DdsAdditionalCaps.CubeMapAllFaces)
                        {
                            throw new NotSupportedException();
                        }

                        arraySize = 6;
                        isCubeMap = true;
                    }

                    depth  = 1;
                    resDim = D3D11ResourceDimension.Texture2D;

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

                break;
            }

            if ((miscOptions & D3D11ResourceMiscOptions.TextureCube) != 0 &&
                resDim == D3D11ResourceDimension.Texture2D &&
                (arraySize % 6 == 0))
            {
                isCubeMap = true;
            }

            // Bound sizes (for security purposes we don't trust DDS file metadata larger than the D3D 11.x hardware requirements)
            if (mipCount > D3D11Constants.ReqMipLevels)
            {
                throw new NotSupportedException();
            }

            switch (resDim)
            {
            case D3D11ResourceDimension.Texture1D:
                if (arraySize > D3D11Constants.ReqTexture1DArrayAxisDimension ||
                    width > D3D11Constants.ReqTexture1DDimension)
                {
                    throw new NotSupportedException();
                }

                break;

            case D3D11ResourceDimension.Texture2D:
                if (isCubeMap)
                {
                    // This is the right bound because we set arraySize to (NumCubes*6) above
                    if (arraySize > D3D11Constants.ReqTexture2DArrayAxisDimension ||
                        width > D3D11Constants.ReqTextureCubeDimension ||
                        height > D3D11Constants.ReqTextureCubeDimension)
                    {
                        throw new NotSupportedException();
                    }
                }
                else if (arraySize > D3D11Constants.ReqTexture2DArrayAxisDimension ||
                         width > D3D11Constants.ReqTexture2DDimension ||
                         height > D3D11Constants.ReqTexture2DDimension)
                {
                    throw new NotSupportedException();
                }

                break;

            case D3D11ResourceDimension.Texture3D:
                if (arraySize > 1 ||
                    width > D3D11Constants.ReqTexture3DDimension ||
                    height > D3D11Constants.ReqTexture3DDimension ||
                    depth > D3D11Constants.ReqTexture3DDimension)
                {
                    throw new NotSupportedException();
                }

                break;

            default:
                throw new NotSupportedException();
            }

            bool autogen = false;

            if (mipCount == 1)
            {
                // See if format is supported for auto-gen mipmaps (varies by feature level)
                if (!device.CheckFormatSupport(format, out D3D11FormatSupport fmtSupport) &&
                    (fmtSupport & D3D11FormatSupport.MipAutogen) != 0)
                {
                    // 10level9 feature levels do not support auto-gen mipgen for volume textures
                    if (resDim != D3D11ResourceDimension.Texture3D ||
                        device.FeatureLevel >= D3D11FeatureLevel.FeatureLevel100)
                    {
                        autogen = true;
                    }
                }
            }

            if (autogen)
            {
                // Create texture with auto-generated mipmaps
                CreateD3DResources(
                    device,
                    resDim,
                    width,
                    height,
                    depth,
                    0,
                    arraySize,
                    format,
                    usage,
                    bindOptions | D3D11BindOptions.RenderTarget,
                    cpuAccessOptions,
                    miscOptions | D3D11ResourceMiscOptions.GenerateMips,
                    forceSRGB,
                    isCubeMap,
                    null,
                    out texture,
                    out textureView);

                try
                {
                    DdsHelpers.GetSurfaceInfo(width, height, (DdsFormat)format, out int numBytes, out int rowBytes, out int numRows);

                    if (numBytes > bitData.Length)
                    {
                        throw new EndOfStreamException();
                    }

                    D3D11ShaderResourceViewDesc desc = textureView.Description;
                    uint mipLevels = 1;

                    switch (desc.ViewDimension)
                    {
                    case D3D11SrvDimension.Texture1D:
                        mipLevels = desc.Texture1D.MipLevels;
                        break;

                    case D3D11SrvDimension.Texture1DArray:
                        mipLevels = desc.Texture1DArray.MipLevels;
                        break;

                    case D3D11SrvDimension.Texture2D:
                        mipLevels = desc.Texture2D.MipLevels;
                        break;

                    case D3D11SrvDimension.Texture2DArray:
                        mipLevels = desc.Texture2DArray.MipLevels;
                        break;

                    case D3D11SrvDimension.TextureCube:
                        mipLevels = desc.TextureCube.MipLevels;
                        break;

                    case D3D11SrvDimension.TextureCubeArray:
                        mipLevels = desc.TextureCubeArray.MipLevels;
                        break;

                    case D3D11SrvDimension.Texture3D:
                        mipLevels = desc.Texture3D.MipLevels;
                        break;

                    default:
                        throw new InvalidDataException();
                    }

                    if (arraySize > 1)
                    {
                        int pSrcBits = 0;

                        for (uint item = 0; item < (uint)arraySize; item++)
                        {
                            if (pSrcBits + numBytes > bitData.Length)
                            {
                                throw new EndOfStreamException();
                            }

                            var data = new byte[numBytes];
                            Array.Copy(bitData, pSrcBits, data, 0, numBytes);

                            uint res = D3D11Utils.CalcSubresource(0, item, mipLevels);
                            context.UpdateSubresource(texture, res, null, data, (uint)rowBytes, (uint)numBytes);

                            pSrcBits += numBytes;
                        }
                    }
                    else
                    {
                        context.UpdateSubresource(texture, 0, null, bitData, (uint)rowBytes, (uint)numBytes);
                    }

                    context.GenerateMips(textureView);
                }
                catch
                {
                    D3D11Utils.DisposeAndNull(ref textureView);
                    D3D11Utils.DisposeAndNull(ref texture);
                    throw;
                }
            }
            else
            {
                // Create the texture

                if (!FillInitData(
                        width,
                        height,
                        depth,
                        mipCount,
                        arraySize,
                        format,
                        maxSize,
                        bitData,
                        out int twidth,
                        out int theight,
                        out int tdepth,
                        out int skipMip,
                        out D3D11SubResourceData[] initData))
        private static void CreateD3DResources(
            D3D11Device device,
            D3D11ResourceDimension resDim,
            int width,
            int height,
            int depth,
            int mipCount,
            int arraySize,
            DxgiFormat format,
            D3D11Usage usage,
            D3D11BindOptions bindFlags,
            D3D11CpuAccessOptions cpuAccessFlags,
            D3D11ResourceMiscOptions miscFlags,
            bool forceSRGB,
            bool isCubeMap,
            D3D11SubResourceData[] initData,
            out D3D11Resource texture,
            out D3D11ShaderResourceView textureView)
        {
            texture     = null;
            textureView = null;

            if (forceSRGB)
            {
                format = (DxgiFormat)DdsHelpers.MakeSrgb((DdsFormat)format);
            }

            switch (resDim)
            {
            case D3D11ResourceDimension.Texture1D:
            {
                var desc = new D3D11Texture1DDesc(
                    format,
                    (uint)width,
                    (uint)arraySize,
                    (uint)mipCount,
                    bindFlags,
                    usage,
                    cpuAccessFlags,
                    miscFlags & ~D3D11ResourceMiscOptions.TextureCube);

                texture = device.CreateTexture1D(desc, initData);

                try
                {
                    var SRVDesc = new D3D11ShaderResourceViewDesc
                    {
                        Format = format
                    };

                    if (arraySize > 1)
                    {
                        SRVDesc.ViewDimension  = D3D11SrvDimension.Texture1DArray;
                        SRVDesc.Texture1DArray = new D3D11Texture1DArraySrv
                        {
                            MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels,
                            ArraySize = (uint)arraySize
                        };
                    }
                    else
                    {
                        SRVDesc.ViewDimension = D3D11SrvDimension.Texture1D;
                        SRVDesc.Texture1D     = new D3D11Texture1DSrv
                        {
                            MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels
                        };
                    }

                    textureView = device.CreateShaderResourceView(texture, SRVDesc);
                }
                catch
                {
                    D3D11Utils.DisposeAndNull(ref texture);
                    throw;
                }

                break;
            }

            case D3D11ResourceDimension.Texture2D:
            {
                var desc = new D3D11Texture2DDesc(
                    format,
                    (uint)width,
                    (uint)height,
                    (uint)arraySize,
                    (uint)mipCount,
                    bindFlags,
                    usage,
                    cpuAccessFlags,
                    1,
                    0);

                if (isCubeMap)
                {
                    desc.MiscOptions = miscFlags | D3D11ResourceMiscOptions.TextureCube;
                }
                else
                {
                    desc.MiscOptions = miscFlags & ~D3D11ResourceMiscOptions.TextureCube;
                }

                if (format == DxgiFormat.BC1UNorm || format == DxgiFormat.BC2UNorm || format == DxgiFormat.BC3UNorm)
                {
                    if ((width & 3) != 0 || (height & 3) != 0)
                    {
                        desc.Width  = (uint)(width + 3) & ~3U;
                        desc.Height = (uint)(height + 3) & ~3U;
                    }
                }

                texture = device.CreateTexture2D(desc, initData);

                try
                {
                    var SRVDesc = new D3D11ShaderResourceViewDesc
                    {
                        Format = format
                    };

                    if (isCubeMap)
                    {
                        if (arraySize > 6)
                        {
                            SRVDesc.ViewDimension    = D3D11SrvDimension.TextureCubeArray;
                            SRVDesc.TextureCubeArray = new D3D11TextureCubeArraySrv
                            {
                                MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels,
                                // Earlier we set arraySize to (NumCubes * 6)
                                NumCubes = (uint)arraySize / 6
                            };
                        }
                        else
                        {
                            SRVDesc.ViewDimension = D3D11SrvDimension.TextureCube;
                            SRVDesc.TextureCube   = new D3D11TextureCubeSrv
                            {
                                MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels,
                            };
                        }
                    }
                    else if (arraySize > 1)
                    {
                        SRVDesc.ViewDimension  = D3D11SrvDimension.Texture2DArray;
                        SRVDesc.Texture2DArray = new D3D11Texture2DArraySrv
                        {
                            MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels,
                            ArraySize = (uint)arraySize
                        };
                    }
                    else
                    {
                        SRVDesc.ViewDimension = D3D11SrvDimension.Texture2D;
                        SRVDesc.Texture2D     = new D3D11Texture2DSrv
                        {
                            MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels,
                        };
                    }

                    textureView = device.CreateShaderResourceView(texture, SRVDesc);
                }
                catch
                {
                    D3D11Utils.DisposeAndNull(ref texture);
                    throw;
                }

                break;
            }

            case D3D11ResourceDimension.Texture3D:
            {
                var desc = new D3D11Texture3DDesc(
                    format,
                    (uint)width,
                    (uint)height,
                    (uint)depth,
                    (uint)mipCount,
                    bindFlags,
                    usage,
                    cpuAccessFlags,
                    miscFlags & ~D3D11ResourceMiscOptions.TextureCube);

                texture = device.CreateTexture3D(desc, initData);

                try
                {
                    var SRVDesc = new D3D11ShaderResourceViewDesc
                    {
                        Format        = format,
                        ViewDimension = D3D11SrvDimension.Texture3D,
                        Texture3D     = new D3D11Texture3DSrv
                        {
                            MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels,
                        }
                    };

                    textureView = device.CreateShaderResourceView(texture, SRVDesc);
                }
                catch
                {
                    D3D11Utils.DisposeAndNull(ref texture);
                    throw;
                }

                break;
            }
            }
        }
Exemplo n.º 6
0
        static void CreateResources()
        {
            var d3dDevice = deviceResources.D3DDevice;

            // Create the Bitonic Sort Compute Shader
            g_pComputeShaderBitonic = d3dDevice.CreateComputeShader(File.ReadAllBytes("CSSortBitonic.cso"), null);

            // Create the Matrix Transpose Compute Shader
            g_pComputeShaderTranspose = d3dDevice.CreateComputeShader(File.ReadAllBytes("CSSortTranspose.cso"), null);

            // Create the Const Buffer
            var constantBufferDesc = new D3D11BufferDesc(ConstantBufferData.Size, D3D11BindOptions.ConstantBuffer);

            g_pCB = d3dDevice.CreateBuffer(constantBufferDesc);

            // Create the Buffer of Elements
            // Create 2 buffers for switching between when performing the transpose
            var bufferDesc = new D3D11BufferDesc(
                NUM_ELEMENTS * sizeof(uint),
                D3D11BindOptions.UnorderedAccess | D3D11BindOptions.ShaderResource,
                D3D11Usage.Default,
                D3D11CpuAccessOptions.None,
                D3D11ResourceMiscOptions.BufferStructured,
                sizeof(uint));

            g_pBuffer1 = d3dDevice.CreateBuffer(bufferDesc);
            g_pBuffer2 = d3dDevice.CreateBuffer(bufferDesc);

            // Create the Shader Resource View for the Buffers
            // This is used for reading the buffer during the transpose
            var srvbufferDesc = new D3D11ShaderResourceViewDesc(D3D11SrvDimension.Buffer, DxgiFormat.Unknown)
            {
                Buffer = new D3D11BufferSrv {
                    ElementWidth = NUM_ELEMENTS
                }
            };

            g_pBuffer1SRV = d3dDevice.CreateShaderResourceView(g_pBuffer1, srvbufferDesc);
            g_pBuffer2SRV = d3dDevice.CreateShaderResourceView(g_pBuffer2, srvbufferDesc);

            // Create the Unordered Access View for the Buffers
            // This is used for writing the buffer during the sort and transpose
            var uavbufferDesc = new D3D11UnorderedAccessViewDesc(D3D11UavDimension.Buffer, DxgiFormat.Unknown)
            {
                Buffer = new D3D11BufferUav {
                    NumElements = NUM_ELEMENTS
                }
            };

            g_pBuffer1UAV = d3dDevice.CreateUnorderedAccessView(g_pBuffer1, uavbufferDesc);
            g_pBuffer2UAV = d3dDevice.CreateUnorderedAccessView(g_pBuffer2, uavbufferDesc);

            // Create the Readback Buffer
            // This is used to read the results back to the CPU
            var readbackBufferDesc = new D3D11BufferDesc(
                NUM_ELEMENTS * sizeof(uint),
                D3D11BindOptions.None,
                D3D11Usage.Staging,
                D3D11CpuAccessOptions.Read,
                D3D11ResourceMiscOptions.None,
                sizeof(uint));

            g_pReadBackBuffer = d3dDevice.CreateBuffer(readbackBufferDesc);
        }
Exemplo n.º 7
0
        protected override void CreateDeviceDependentResources()
        {
            base.CreateDeviceDependentResources();

            byte[] vertexShaderBytecode = File.ReadAllBytes(Lesson4Game.ShadersDirectory + "Textures.VertexShader.cso");
            this.vertexShader = this.DeviceResources.D3DDevice.CreateVertexShader(vertexShaderBytecode, null);

            D3D11InputElementDesc[] basicVertexLayoutDesc = new D3D11InputElementDesc[]
            {
                new D3D11InputElementDesc
                {
                    SemanticName         = "POSITION",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 0,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                },
                new D3D11InputElementDesc
                {
                    SemanticName         = "NORMAL",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 12,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                },
                new D3D11InputElementDesc
                {
                    SemanticName         = "TEXCOORD",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 24,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                }
            };

            this.inputLayout = this.DeviceResources.D3DDevice.CreateInputLayout(basicVertexLayoutDesc, vertexShaderBytecode);

            byte[] pixelShaderBytecode = File.ReadAllBytes(Lesson4Game.ShadersDirectory + "Textures.PixelShader.cso");
            this.pixelShader = this.DeviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null);

            var vertexBufferDesc = D3D11BufferDesc.From(cubeVertices, D3D11BindOptions.VertexBuffer);

            this.vertexBuffer = this.DeviceResources.D3DDevice.CreateBuffer(vertexBufferDesc, cubeVertices, 0, 0);

            var indexBufferDesc = D3D11BufferDesc.From(cubeIndices, D3D11BindOptions.IndexBuffer);

            this.indexBuffer = this.DeviceResources.D3DDevice.CreateBuffer(indexBufferDesc, cubeIndices, 0, 0);

            var constantBufferDesc = new D3D11BufferDesc(ConstantBufferData.Size, D3D11BindOptions.ConstantBuffer);

            this.constantBuffer = this.DeviceResources.D3DDevice.CreateBuffer(constantBufferDesc);

            this.constantBufferData.View = new Float4X4(
                -1.00000000f, 0.00000000f, 0.00000000f, 0.00000000f,
                0.00000000f, 0.89442718f, 0.44721359f, 0.00000000f,
                0.00000000f, 0.44721359f, -0.89442718f, -2.23606800f,
                0.00000000f, 0.00000000f, 0.00000000f, 1.00000000f
                );

            byte[] textureData = File.ReadAllBytes("../../texturedata.bin");

            D3D11Texture2DDesc textureDesc = new D3D11Texture2DDesc(DxgiFormat.R8G8B8A8UNorm, 256, 256, 1, 1);

            D3D11SubResourceData[] textureSubResData = new[]
            {
                new D3D11SubResourceData(textureData, 1024)
            };

            using (var texture = this.DeviceResources.D3DDevice.CreateTexture2D(textureDesc, textureSubResData))
            {
                D3D11ShaderResourceViewDesc textureViewDesc = new D3D11ShaderResourceViewDesc
                {
                    Format        = textureDesc.Format,
                    ViewDimension = D3D11SrvDimension.Texture2D,
                    Texture2D     = new D3D11Texture2DSrv
                    {
                        MipLevels       = textureDesc.MipLevels,
                        MostDetailedMip = 0
                    }
                };

                this.textureView = this.DeviceResources.D3DDevice.CreateShaderResourceView(texture, textureViewDesc);
            }

            D3D11SamplerDesc samplerDesc = new D3D11SamplerDesc(
                D3D11Filter.Anisotropic,
                D3D11TextureAddressMode.Wrap,
                D3D11TextureAddressMode.Wrap,
                D3D11TextureAddressMode.Wrap,
                0.0f,
                this.DeviceResources.D3DFeatureLevel > D3D11FeatureLevel.FeatureLevel91 ? D3D11Constants.DefaultMaxAnisotropy : D3D11Constants.FeatureLevel91DefaultMaxAnisotropy,
                D3D11ComparisonFunction.Never,
                new float[] { 0.0f, 0.0f, 0.0f, 0.0f },
                0.0f,
                float.MaxValue);

            this.sampler = this.DeviceResources.D3DDevice.CreateSamplerState(samplerDesc);
        }
        private void CreateTextures(OptFile opt)
        {
            foreach (var textureKey in opt.Textures)
            {
                var optTextureName = textureKey.Key;
                var optTexture     = textureKey.Value;

                int mipLevels = optTexture.MipmapsCount;

                if (mipLevels == 0)
                {
                    continue;
                }

                D3D11SubResourceData[] textureSubResData = new D3D11SubResourceData[mipLevels];

                int bpp = optTexture.BitsPerPixel;

                if (bpp == 8)
                {
                    List <XMUInt3> palette = Enumerable.Range(0, 256)
                                             .Select(i =>
                    {
                        ushort c = BitConverter.ToUInt16(optTexture.Palette, 8 * 512 + i * 2);

                        byte r = (byte)((c & 0xF800) >> 11);
                        byte g = (byte)((c & 0x7E0) >> 5);
                        byte b = (byte)(c & 0x1F);

                        r = (byte)((r * (0xffU * 2) + 0x1fU) / (0x1fU * 2));
                        g = (byte)((g * (0xffU * 2) + 0x3fU) / (0x3fU * 2));
                        b = (byte)((b * (0xffU * 2) + 0x1fU) / (0x1fU * 2));

                        return(new XMUInt3(r, g, b));
                    })
                                             .ToList();

                    for (int level = 0; level < mipLevels; level++)
                    {
                        byte[] imageData = optTexture.GetMipmapImageData(level, out int width, out int height);
                        byte[] alphaData = optTexture.GetMipmapAlphaData(level, out int w, out int h);

                        int    size        = width * height;
                        byte[] textureData = new byte[size * 4];

                        for (int i = 0; i < size; i++)
                        {
                            int     colorIndex = imageData[i];
                            XMUInt3 color      = palette[colorIndex];

                            textureData[i * 4 + 0] = (byte)color.Z;
                            textureData[i * 4 + 1] = (byte)color.Y;
                            textureData[i * 4 + 2] = (byte)color.X;
                            textureData[i * 4 + 3] = alphaData != null ? alphaData[i] : (byte)255;
                        }

                        textureSubResData[level] = new D3D11SubResourceData(textureData, (uint)width * 4);
                    }
                }
                else if (bpp == 32)
                {
                    for (int level = 0; level < mipLevels; level++)
                    {
                        byte[] imageData = optTexture.GetMipmapImageData(level, out int width, out int height);

                        textureSubResData[level] = new D3D11SubResourceData(imageData, (uint)width * 4);
                    }
                }
                else
                {
                    continue;
                }

                D3D11Texture2DDesc textureDesc = new D3D11Texture2DDesc(DxgiFormat.B8G8R8A8UNorm, (uint)optTexture.Width, (uint)optTexture.Height, 1, (uint)textureSubResData.Length);

                using (var texture = this.deviceResources.D3DDevice.CreateTexture2D(textureDesc, textureSubResData))
                {
                    D3D11ShaderResourceViewDesc textureViewDesc = new D3D11ShaderResourceViewDesc
                    {
                        Format        = textureDesc.Format,
                        ViewDimension = D3D11SrvDimension.Texture2D,
                        Texture2D     = new D3D11Texture2DSrv
                        {
                            MipLevels       = textureDesc.MipLevels,
                            MostDetailedMip = 0
                        }
                    };

                    this.textureViews.Add(optTextureName, this.deviceResources.D3DDevice.CreateShaderResourceView(texture, textureViewDesc));
                }
            }
        }
Exemplo n.º 9
0
        private void CreateParticlePosVeloBuffers()
        {
            var d3dDevice = this.deviceResources.D3DDevice;

            // Initialize the data in the buffers
            Particle[] pData1 = new Particle[MaxParticles];

            Random rand = this.Random ?? new Random(0);

            if (this.DiskGalaxyFormationType == 0)
            {
                // Disk Galaxy Formation
                float fCenterSpread = Spread * 0.50f;

                LoadParticles(
                    rand,
                    pData1,
                    0,
                    new XMFloat3(fCenterSpread, 0, 0),
                    new XMFloat4(0, 0, -20, 1 / 10000.0f / 10000.0f),
                    Spread,
                    pData1.Length / 2);

                LoadParticles(
                    rand,
                    pData1,
                    pData1.Length / 2,
                    new XMFloat3(-fCenterSpread, 0, 0),
                    new XMFloat4(0, 0, 20, 1 / 10000.0f / 10000.0f),
                    Spread,
                    pData1.Length - pData1.Length / 2);
            }
            else
            {
                // Disk Galaxy Formation with impacting third cluster
                LoadParticles(
                    rand,
                    pData1,
                    0,
                    new XMFloat3(Spread, 0, 0),
                    new XMFloat4(0, 0, -8, 1 / 10000.0f / 10000.0f),
                    Spread,
                    pData1.Length / 3);

                LoadParticles(
                    rand,
                    pData1,
                    pData1.Length / 3,
                    new XMFloat3(-Spread, 0, 0),
                    new XMFloat4(0, 0, 8, 1 / 10000.0f / 10000.0f),
                    Spread,
                    pData1.Length / 3);

                LoadParticles(
                    rand,
                    pData1,
                    2 * pData1.Length / 3,
                    new XMFloat3(0, 0, Spread * 15.0f),
                    new XMFloat4(0, 0, -60, 1 / 10000.0f / 10000.0f),
                    Spread,
                    pData1.Length - 2 * pData1.Length / 3);
            }

            D3D11BufferDesc desc = D3D11BufferDesc.From(pData1, D3D11BindOptions.UnorderedAccess | D3D11BindOptions.ShaderResource);

            desc.MiscOptions         = D3D11ResourceMiscOptions.BufferStructured;
            desc.StructureByteStride = Particle.Size;

            this.g_pParticlePosVelo0 = d3dDevice.CreateBuffer(desc, pData1, 0, 0);
            this.g_pParticlePosVelo1 = d3dDevice.CreateBuffer(desc, pData1, 0, 0);

            D3D11ShaderResourceViewDesc DescRV = new D3D11ShaderResourceViewDesc(
                this.g_pParticlePosVelo0,
                DxgiFormat.Unknown,
                0,
                desc.ByteWidth / desc.StructureByteStride);

            this.g_pParticlePosVeloRV0 = d3dDevice.CreateShaderResourceView(this.g_pParticlePosVelo0, DescRV);
            this.g_pParticlePosVeloRV1 = d3dDevice.CreateShaderResourceView(this.g_pParticlePosVelo1, DescRV);

            D3D11UnorderedAccessViewDesc DescUAV = new D3D11UnorderedAccessViewDesc(
                this.g_pParticlePosVelo0,
                DxgiFormat.Unknown,
                0,
                desc.ByteWidth / desc.StructureByteStride);

            this.g_pParticlePosVeloUAV0 = d3dDevice.CreateUnorderedAccessView(this.g_pParticlePosVelo0, DescUAV);
            this.g_pParticlePosVeloUAV1 = d3dDevice.CreateUnorderedAccessView(this.g_pParticlePosVelo1, DescUAV);
        }
Exemplo n.º 10
0
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            byte[] renderSceneVertexShaderBytecode = File.ReadAllBytes("RenderSceneVertexShader.cso");
            this.g_pSceneVS = this.deviceResources.D3DDevice.CreateVertexShader(renderSceneVertexShaderBytecode, null);

            D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[]
            {
                new D3D11InputElementDesc
                {
                    SemanticName         = "POSITION",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 0,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                },
                new D3D11InputElementDesc
                {
                    SemanticName         = "NORMAL",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32B32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 12,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                },
                new D3D11InputElementDesc
                {
                    SemanticName         = "TEXTURE",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 24,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                }
            };

            this.g_pSceneVertexLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, renderSceneVertexShaderBytecode);

            byte[] renderScenePixelShaderBytecode = File.ReadAllBytes("RenderScenePixelShader.cso");
            this.g_pScenePS = this.deviceResources.D3DDevice.CreatePixelShader(renderScenePixelShaderBytecode, null);

            byte[] renderSceneShadowMapVertexShaderBytecode = File.ReadAllBytes("RenderSceneShadowMapVertexShader.cso");
            this.g_pShadowMapVS = this.deviceResources.D3DDevice.CreateVertexShader(renderSceneShadowMapVertexShaderBytecode, null);

            this.g_SceneMesh = SdkMeshFile.FromFile(this.deviceResources.D3DDevice, this.deviceResources.D3DContext, @"ColumnScene\scene.sdkmesh");
            this.g_Poles     = SdkMeshFile.FromFile(this.deviceResources.D3DDevice, this.deviceResources.D3DContext, @"ColumnScene\poles.sdkmesh");

            this.g_pcbConstants = this.deviceResources.D3DDevice.CreateBuffer(
                new D3D11BufferDesc(ConstantBufferConstants.Size, D3D11BindOptions.ConstantBuffer));

            D3D11SamplerDesc samplerDesc = new D3D11SamplerDesc(
                D3D11Filter.ComparisonMinMagMipPoint,
                D3D11TextureAddressMode.Border,
                D3D11TextureAddressMode.Border,
                D3D11TextureAddressMode.Border,
                0.0f,
                1,
                D3D11ComparisonFunction.LessEqual,
                new float[] { 1.0f, 1.0f, 1.0f, 1.0f },
                0.0f,
                float.MaxValue);

            // PointCmp
            this.g_pSamplePointCmp = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc);

            // Point
            samplerDesc.Filter             = D3D11Filter.MinMagMipPoint;
            samplerDesc.ComparisonFunction = D3D11ComparisonFunction.Always;
            this.g_pSamplePoint            = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc);

            // Linear
            samplerDesc.Filter   = D3D11Filter.MinMagMipLinear;
            samplerDesc.AddressU = D3D11TextureAddressMode.Wrap;
            samplerDesc.AddressV = D3D11TextureAddressMode.Wrap;
            samplerDesc.AddressW = D3D11TextureAddressMode.Wrap;
            this.g_pSampleLinear = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc);

            // Create a blend state to disable alpha blending
            D3D11BlendDesc blendDesc = D3D11BlendDesc.Default;

            blendDesc.IsIndependentBlendEnabled = false;
            D3D11RenderTargetBlendDesc[] blendDescRenderTargets = blendDesc.GetRenderTargets();
            blendDescRenderTargets[0].IsBlendEnabled = false;

            blendDescRenderTargets[0].RenderTargetWriteMask = D3D11ColorWriteEnables.All;
            blendDesc.SetRenderTargets(blendDescRenderTargets);
            this.g_pBlendStateNoBlend = this.deviceResources.D3DDevice.CreateBlendState(blendDesc);

            blendDescRenderTargets[0].RenderTargetWriteMask = D3D11ColorWriteEnables.None;
            blendDesc.SetRenderTargets(blendDescRenderTargets);
            this.g_pBlendStateColorWritesOff = this.deviceResources.D3DDevice.CreateBlendState(blendDesc);

            // textures / rts
            D3D11Texture2DDesc TDesc = new D3D11Texture2DDesc(
                DxgiFormat.R16Typeless,
                (uint)g_fShadowMapWidth,
                (uint)g_fShadowMapHeight,
                1,
                1,
                D3D11BindOptions.DepthStencil | D3D11BindOptions.ShaderResource);

            this.g_pShadowMapDepthStencilTexture = this.deviceResources.D3DDevice.CreateTexture2D(TDesc);

            D3D11ShaderResourceViewDesc SRVDesc = new D3D11ShaderResourceViewDesc
            {
                Format        = DxgiFormat.R16UNorm,
                ViewDimension = D3D11SrvDimension.Texture2D,
                Texture2D     = new D3D11Texture2DSrv
                {
                    MipLevels       = 1,
                    MostDetailedMip = 0
                }
            };

            this.g_pDepthTextureSRV = this.deviceResources.D3DDevice.CreateShaderResourceView(this.g_pShadowMapDepthStencilTexture, SRVDesc);

            D3D11DepthStencilViewDesc DSVDesc = new D3D11DepthStencilViewDesc
            {
                Format        = DxgiFormat.D16UNorm,
                ViewDimension = D3D11DsvDimension.Texture2D,
                Options       = D3D11DepthStencilViewOptions.None,
                Texture2D     = new D3D11Texture2DDsv
                {
                    MipSlice = 0
                }
            };

            this.g_pDepthStencilTextureDSV = this.deviceResources.D3DDevice.CreateDepthStencilView(this.g_pShadowMapDepthStencilTexture, DSVDesc);

            XMFloat3 vecEye = new XMFloat3(0.95f, 5.83f, -14.48f);
            XMFloat3 vecAt  = new XMFloat3(0.90f, 5.44f, -13.56f);
            XMFloat3 vecUp  = new XMFloat3(0.0f, 1.0f, 0.0f);

            this.ViewMatrix  = XMMatrix.LookAtLH(vecEye, vecAt, vecUp);
            this.WorldMatrix = XMMatrix.Identity;

            XMFloat3 vecEyeL = new XMFloat3(0, 0, 0);
            XMFloat3 vecAtL  = new XMFloat3(0, -0.5f, 1);
            XMFloat3 vecUUpL = new XMFloat3(0.0f, 1.0f, 0.0f);

            this.LightViewMatrix  = XMMatrix.LookAtLH(vecEyeL, vecAtL, vecUUpL);
            this.LightWorldMatrix = XMMatrix.Identity;
        }