Example #1
0
        protected override unsafe ID3D12RootSignature *CreateRootSignature()
        {
            using ComPtr <ID3D10Blob> signature = null;
            using ComPtr <ID3D10Blob> error     = null;

            var rootSignatureDesc = new RootSignatureDesc
            {
                Flags = RootSignatureFlags.RootSignatureFlagAllowInputAssemblerInputLayout
            };

            SilkMarshal.ThrowHResult
            (
                D3D12.SerializeRootSignature
                (
                    &rootSignatureDesc, D3DRootSignatureVersion.D3DRootSignatureVersion1, signature.GetAddressOf(),
                    error.GetAddressOf()
                )
            );

            ID3D12RootSignature *rootSignature;

            var iid = ID3D12RootSignature.Guid;

            SilkMarshal.ThrowHResult
            (
                D3DDevice->CreateRootSignature
                (
                    nodeMask: 0, signature.Get().GetBufferPointer(), signature.Get().GetBufferSize(), &iid,
                    (void **)&rootSignature
                )
            );

            return(rootSignature);
        }
        public void Execute(
            ReadOnlySpan <GpuCommandSet> lists,
            bool insertFence
            )
        {
            // TODO make the lists blittable to elide this copy
            ComPtr <ID3D12GraphicsCommandList> *pLists = stackalloc ComPtr <ID3D12GraphicsCommandList> [lists.Length];

            for (var i = 0; i < lists.Length; i++)
            {
                pLists[i] = lists[i].List.Move();
            }

            if (lists.IsEmpty)
            {
                return;
            }

            _queue.Get()->ExecuteCommandLists((uint)lists.Length, (ID3D12CommandList **)pLists);

            if (insertFence)
            {
                _marker++;
                Guard.ThrowIfFailed(_queue.Get()->Signal(_fence.Get(), _marker.FenceValue));
            }
        }
    /// <summary>
    /// Compiles a new HLSL shader from the input source code.
    /// </summary>
    /// <param name="hlslSourceAscii">The HLSL source code to compile (in ASCII).</param>
    /// <param name="entryPointAscii">The entry point of the shader being compiled (in ASCII).</param>
    /// <param name="shaderProfile">The shader profile to use to compile the shader.</param>
    /// <param name="options">The compiler options to use to compile the shader.</param>
    /// <returns>The bytecode for the compiled shader.</returns>
    /// <exception cref="FxcCompilationException">Thrown if the compilation fails.</exception>
    public static unsafe ReadOnlyMemory <byte> Compile(
        ReadOnlySpan <byte> hlslSourceAscii,
        ReadOnlySpan <byte> entryPointAscii,
        D2D1ShaderProfile shaderProfile,
        D2D1CompileOptions options)
    {
        // Check linking support
        bool enableLinking = (options & D2D1CompileOptions.EnableLinking) == D2D1CompileOptions.EnableLinking;

        // Remove the linking flag to make the options blittable to flags
        options &= ~D2D1CompileOptions.EnableLinking;

        // Compile the standalone D2D1 full shader
        using ComPtr <ID3DBlob> d3DBlobFullShader = D3DCompiler.CompileShader(
                  source: hlslSourceAscii,
                  macro: D3DCompiler.ASCII.D2D_FULL_SHADER,
                  d2DEntry: entryPointAscii,
                  entryPoint: entryPointAscii,
                  target: D3DCompiler.ASCII.GetPixelShaderProfile(shaderProfile),
                  flags: (uint)options);

        if (!enableLinking)
        {
            void *blobFullShaderPtr  = d3DBlobFullShader.Get()->GetBufferPointer();
            nuint blobFullShaderSize = d3DBlobFullShader.Get()->GetBufferSize();

            return(new Span <byte>(blobFullShaderPtr, (int)blobFullShaderSize).ToArray());
        }

        // Compile the export function
        using ComPtr <ID3DBlob> d3DBlobFunction = D3DCompiler.CompileShader(
                  source: hlslSourceAscii,
                  macro: D3DCompiler.ASCII.D2D_FUNCTION,
                  d2DEntry: entryPointAscii,
                  entryPoint: default,
Example #4
0
    /// <summary>
    /// Saves a given image buffer to a target path.
    /// </summary>
    /// <param name="filename">The path to save the image to.</param>
    /// <param name="width">The image width.</param>
    /// <param name="height">The image height.</param>
    /// <param name="strideInBytes">The image stride in bytes.</param>
    /// <param name="buffer">The image pixel buffer.</param>
    public static unsafe void SaveBitmapToFile(string filename, uint width, uint height, uint strideInBytes, byte *buffer)
    {
        using ComPtr <IWICImagingFactory2> wicImagingFactory2 = default;

        // Create a Windows Imaging Component (WIC) factory
        Windows.CoCreateInstance(
            rclsid: (Guid *)Unsafe.AsPointer(ref Unsafe.AsRef(in CLSID.CLSID_WICImagingFactory2)),
            pUnkOuter: null,
            dwClsContext: (uint)CLSCTX.CLSCTX_INPROC_SERVER,
            riid: Windows.__uuidof <IWICImagingFactory2>(),
            ppv: (void **)wicImagingFactory2.GetAddressOf()).Assert();

        using ComPtr <IWICBitmapEncoder> wicBitmapEncoder = default;

        // Create the image encoder
        wicImagingFactory2.Get()->CreateEncoder(
            guidContainerFormat: (Guid *)Unsafe.AsPointer(ref Unsafe.AsRef(in GUID.GUID_ContainerFormatPng)),
            pguidVendor: null,
            ppIEncoder: wicBitmapEncoder.GetAddressOf()).Assert();

        using ComPtr <IWICStream> wicStream = default;

        // Create and initialize a stream to the target file
        wicImagingFactory2.Get()->CreateStream(wicStream.GetAddressOf()).Assert();

        fixed(char *p = filename)
        {
            // Initialize the stream to the target file
            wicStream.Get()->InitializeFromFilename((ushort *)p, Windows.GENERIC_WRITE);
        }

        // Initialize the encoder
        wicBitmapEncoder.Get()->Initialize(
            pIStream: (IStream *)wicStream.Get(),
            cacheOption: WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache).Assert();

        using ComPtr <IWICBitmapFrameEncode> wicBitmapFrameEncode = default;

        // Create the image frame and initialize it
        wicBitmapEncoder.Get()->CreateNewFrame(wicBitmapFrameEncode.GetAddressOf(), null).Assert();

        Guid wicTargetPixelFormatGuid = GUID.GUID_WICPixelFormat32bppPBGRA;

        // Set the encoding properties
        wicBitmapFrameEncode.Get()->Initialize(null).Assert();
        wicBitmapFrameEncode.Get()->SetSize(width, height).Assert();
        wicBitmapFrameEncode.Get()->SetPixelFormat(&wicTargetPixelFormatGuid).Assert();

        // Encode the target image
        wicBitmapFrameEncode.Get()->WritePixels(
            lineCount: height,
            cbStride: strideInBytes,
            cbBufferSize: strideInBytes * height,
            pbPixels: buffer).Assert();

        // Flush the changes
        wicBitmapFrameEncode.Get()->Commit();
        wicBitmapEncoder.Get()->Commit();
    }
Example #5
0
        /// <summary>
        /// Blocks the calling thread until the synchronization event has occured
        /// </summary>
        public void Block()
        {
            if (!_fence.Exists)
            {
                return;
            }

            _fence.Get()->SetEventOnCompletion(_completionValue.FenceValue, default);
        }
Example #6
0
        public Image Load(byte *buffer, uint size)
        {
            using ComPtr <IWICStream> stream = default;
            CheckAndThrow(_factory.Get()->CreateStream(stream.GetAddressOf()), nameof(IWICImagingFactory.CreateStream));
            CheckAndThrow(stream.Get()->InitializeFromMemory(buffer, size), nameof(IWICStream.InitializeFromMemory));

            using ComPtr <IWICBitmapDecoder> decoder = default;
            CheckAndThrow(_factory.Get()->CreateDecoderFromStream((IStream *)stream.Get(), null, WICDecodeMetadataCacheOnDemand, decoder.GetAddressOf()), nameof(IWICImagingFactory.CreateDecoderFromStream));
            return(LoadInternal(decoder));
        }
Example #7
0
        public ComPtr <IDxcBlob> CompileShader(ReadOnlySpan <char> source)
        {
            using ComPtr <IDxcBlobEncoding> dxcBlobEncoding       = default;
            using ComPtr <IDxcOperationResult> dxcOperationResult = default;
            using ComPtr <IDxcBlob> dxcBlobBytecode = default;

            // Get the encoded blob from the source code
            fixed(char *p = source)
            {
                DxcLibrary.Get()->CreateBlobWithEncodingOnHeapCopy(
                    p,
                    (uint)source.Length * 2,
                    1200,
                    dxcBlobEncoding.GetAddressOf()).Assert();
            }

            // Try to compile the new compute shader
            fixed(char *shaderName = "")
            fixed(char *entryPoint       = nameof(IComputeShader.Execute))
            fixed(char *shaderProfile    = "cs_6_0")
            fixed(char *optimization     = "-O3")
            fixed(char *rowMajor         = "-Zpr")
            fixed(char *warningsAsErrors = "-Werror")
            {
                char **arguments = stackalloc char *[3] {
                    optimization, rowMajor, warningsAsErrors
                };

                DxcCompiler.Get()->Compile(
                    (IDxcBlob *)dxcBlobEncoding.Get(),
                    (ushort *)shaderName,
                    (ushort *)entryPoint,
                    (ushort *)shaderProfile,
                    (ushort **)arguments,
                    3,
                    null,
                    0,
                    DxcIncludeHandler.Get(),
                    dxcOperationResult.GetAddressOf()).Assert();
            }

            int status;

            dxcOperationResult.Get()->GetStatus(&status).Assert();

            // The compilation was successful, so we can extract the shader bytecode
            if (status == 0)
            {
                dxcOperationResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf()).Assert();

                return(dxcBlobBytecode.Move());
            }

            return(ThrowHslsCompilationException(dxcOperationResult));
        }
Example #8
0
 internal void Resize(uint width, uint height)
 {
     if (height != 0)
     {
         Height = height;
     }
     if (width != 0)
     {
         Width = width;
     }
     _swapChain.Get()->ResizeBuffers(0, width, height, DXGI_FORMAT.DXGI_FORMAT_UNKNOWN, 0);
 }
Example #9
0
        bool TryCompilePixelShader(string shaderName, out ComPtr <ID3DBlob> shaderBlob, out ComPtr <ID3D11PixelShader> pixelShader)
        {
            try
            {
                shaderBlob = new ComPtr <ID3DBlob>();
                ID3DBlob *errorBlob;
                uint      flags = D3DCOMPILE_ENABLE_STRICTNESS;
#if DEBUG
                flags |= D3DCOMPILE_DEBUG;
#endif
                // Prefer higher CS shader profile when possible as CS 5.0 provides better performance on 11-class hardware.
                var     profile         = (m_d3dDevice.Get()->GetFeatureLevel() >= D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0) ? "ps_5_0" : "ps_4_0_level_9_1";
                HRESULT shaderCompileHr = D3DCompileFromFile(
                    GetShaderFilePath(shaderName).Select(c => (ushort)c).ToArray().AsSpan().AsPointer(),
                    null,
                    D3D_COMPILE_STANDARD_FILE_INCLUDE,
                    Encoding.ASCII.GetBytes(shaderName).Select(b => (sbyte)b).ToArray().AsSpan().AsPointer(),
                    profile.Select(c => (sbyte)c).ToArray().AsSpan().AsPointer(),
                    flags,
                    0,
                    shaderBlob.GetAddressOf(),
                    &errorBlob
                    );
                if (shaderCompileHr.FAILED)
                {
                    var errorStr = Marshal.PtrToStringAnsi(new IntPtr(errorBlob->GetBufferPointer()));
                    errorBlob->Release();
                    throw new Exception(errorStr);
                }

                pixelShader = new ComPtr <ID3D11PixelShader>();
                Marshal.ThrowExceptionForHR(
                    m_d3dDevice.Get()->CreatePixelShader(
                        shaderBlob.Get()->GetBufferPointer(),
                        shaderBlob.Get()->GetBufferSize(),
                        null,
                        pixelShader.GetAddressOf()
                        )
                    );

                return(true);
            }
            catch
            {
                shaderBlob  = null;
                pixelShader = null;
                return(false);
            }
        }
Example #10
0
    /// <summary>
    /// Creates an <see cref="ID2D1Bitmap"/> instance.
    /// </summary>
    /// <param name="d2D1DeviceContext">The input <see cref="ID2D1DeviceContext"/> instance to use to create the bitmap source.</param>
    /// <param name="width">The width of the bitmap to create.</param>
    /// <param name="height">The height of the bitmap to create.</param>
    /// <returns>A new <see cref="ID2D1Bitmap"/> instance.</returns>
    public static unsafe ComPtr <ID2D1Bitmap> CreateD2D1BitmapAndSetAsTarget(ID2D1DeviceContext *d2D1DeviceContext, uint width, uint height)
    {
        D2D_SIZE_U d2DSize;

        d2DSize.width  = width;
        d2DSize.height = height;

        D2D1_BITMAP_PROPERTIES1 d2DBitmapProperties1Target = default;

        d2DBitmapProperties1Target.pixelFormat.format    = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM;
        d2DBitmapProperties1Target.pixelFormat.alphaMode = D2D1_ALPHA_MODE.D2D1_ALPHA_MODE_PREMULTIPLIED;
        d2DBitmapProperties1Target.bitmapOptions         =
            D2D1_BITMAP_OPTIONS.D2D1_BITMAP_OPTIONS_TARGET |
            D2D1_BITMAP_OPTIONS.D2D1_BITMAP_OPTIONS_CANNOT_DRAW;

        using ComPtr <ID2D1Bitmap> d2D1Bitmap1Target = default;

        // Create a target D2D1 bitmap
        d2D1DeviceContext->CreateBitmap(
            size: d2DSize,
            sourceData: null,
            pitch: 0,
            bitmapProperties: &d2DBitmapProperties1Target,
            bitmap: (ID2D1Bitmap1 **)d2D1Bitmap1Target.GetAddressOf()).Assert();

        d2D1DeviceContext->SetTarget((ID2D1Image *)d2D1Bitmap1Target.Get());

        return(d2D1Bitmap1Target.Move());
    }
Example #11
0
 public void Reset()
 {
     resource.Get()->Release();
     allocation.reset();
     size     = UINT64_MAX;
     dataSeed = 0;
 }
Example #12
0
        private DescriptorHeap(ID3D12Device *device, D3D12_DESCRIPTOR_HEAP_DESC desc)
        {
            ComPtr <ID3D12DescriptorHeap> heap = default;

            Guard.ThrowIfFailed(device->CreateDescriptorHeap(&desc, heap.Guid, (void **)&heap));

            _heap = heap.Move();
            var cpu = _heap.Get()->GetCPUDescriptorHandleForHeapStart();
            var gpu = _heap.Get()->GetGPUDescriptorHandleForHeapStart();

            FirstDescriptor = new DescriptorHandle(cpu, gpu, desc.Type);

            Type           = desc.Type;
            NumDescriptors = desc.NumDescriptors;

            DirectXHelpers.SetObjectName(_heap.Get(), nameof(ID3D12DescriptorHeap));
        }
Example #13
0
    public static unsafe uint AddRef <T>(this ComPtr <T> ptr)
#if NET6_0_OR_GREATER
        where T : unmanaged, IUnknown.Interface
#else
        where T : unmanaged
#endif
    {
        if (ptr.Get() is not null)
        {
#if NET6_0_OR_GREATER
            return(ptr.Get()->AddRef());
#else
            return(((IUnknown *)ptr.Get())->AddRef());
#endif
        }

        return(0);
    }
Example #14
0
        public static void BreakOnDeletion <T>(ComPtr <T> ptr, object?data = null) where T : unmanaged
        {
            if (data is null)
            {
                data = $"'{typeof(T).Name} with name '{DirectXHelpers.GetObjectName(ptr.Get())}' is being deleted";
            }

            RegisterForDeletionCallback(ptr, BreakOnDeletion, data);
        }
Example #15
0
        private static ComPtr <IDxcBlob> ThrowHslsCompilationException(IDxcOperationResult *dxcOperationResult)
        {
            using ComPtr <IDxcBlobEncoding> dxcBlobEncodingError = default;

            dxcOperationResult->GetErrorBuffer(dxcBlobEncodingError.GetAddressOf()).Assert();

            string message = new((sbyte *)dxcBlobEncodingError.Get()->GetBufferPointer());

            throw new HlslCompilationException(message);
        }
Example #16
0
    public SourceVoice(ComPtr <IXAudio2> xAudio2, WAVEFORMATEX format)
    {
        _callback = IXAudio2VoiceCallback.Create(this);
        fixed(IXAudio2SourceVoice **ppVoice = &_voice)
        {
            Common.CheckAndThrow(xAudio2.Get()->CreateSourceVoice(ppVoice, &format, pCallback: _callback), nameof(IXAudio2.CreateSourceVoice));
        }

        _voice->SetVolume(0.1f);
    }
Example #17
0
        public void ResourceFromBuffer(Device device, Type bufferType)
        {
            using Buffer <float> buffer = device.Get().AllocateBuffer <float>(bufferType, 128);

            using ComPtr <ID3D12Resource> d3D12Resource = default;

            InteropServices.GetID3D12Resource(buffer, FX.__uuidof <ID3D12Resource>(), (void **)d3D12Resource.GetAddressOf());

            Assert.IsTrue(d3D12Resource.Get() != null);
            Assert.AreEqual(d3D12Resource.Get()->GetDesc().Dimension, D3D12_RESOURCE_DIMENSION_BUFFER);

            d3D12Resource.Dispose();

            int hResult = InteropServices.TryGetID3D12Resource(buffer, FX.__uuidof <ID3D12Resource>(), (void **)d3D12Resource.GetAddressOf());

            Assert.AreEqual(hResult, FX.S_OK);
            Assert.IsTrue(d3D12Resource.Get() != null);
            Assert.AreEqual(d3D12Resource.Get()->GetDesc().Dimension, D3D12_RESOURCE_DIMENSION_BUFFER);
        }
    public void ResourceFromTexture3D(Device device, Type bufferType)
    {
        using Texture3D <float> buffer = device.Get().AllocateTexture3D <float>(bufferType, 16, 16, 4);

        using ComPtr <ID3D12Resource> d3D12Resource = default;

        InteropServices.GetID3D12Resource(buffer, Windows.__uuidof <ID3D12Resource>(), (void **)d3D12Resource.GetAddressOf());

        Assert.IsTrue(d3D12Resource.Get() != null);
        Assert.AreEqual(d3D12Resource.Get()->GetDesc().Dimension, D3D12_RESOURCE_DIMENSION_TEXTURE3D);

        d3D12Resource.Dispose();

        int hResult = InteropServices.TryGetID3D12Resource(buffer, Windows.__uuidof <ID3D12Resource>(), (void **)d3D12Resource.GetAddressOf());

        Assert.AreEqual(hResult, S.S_OK);
        Assert.IsTrue(d3D12Resource.Get() != null);
        Assert.AreEqual(d3D12Resource.Get()->GetDesc().Dimension, D3D12_RESOURCE_DIMENSION_TEXTURE3D);
    }
Example #19
0
        protected override ComPtr <ID3D12GraphicsCommandList> Create(ListCreationParams state)
        {
            using ComPtr <ID3D12GraphicsCommandList> list = default;
            Guard.ThrowIfFailed(_device.Get()->CreateCommandList(
                                    0, // TODO: MULTI-GPU
                                    state.Type,
                                    state.Allocator,
                                    state.Pso,
                                    list.Guid,
                                    ComPtr.GetVoidAddressOf(&list)
                                    ));

            DirectXHelpers.SetObjectName(list.Get(), $"Pooled list #{_listCount++}");

            // 'ManageRent' expects closed list
            Guard.ThrowIfFailed(list.Get()->Close());

            return(list.Move());
        }
Example #20
0
    protected override unsafe ID3D12RootSignature *CreateRootSignature()
    {
        using ComPtr <ID3DBlob> signature = null;
        using ComPtr <ID3DBlob> error     = null;

        var featureData = new D3D12_FEATURE_DATA_ROOT_SIGNATURE {
            // This is the highest version the sample supports. If CheckFeatureSupport succeeds, the HighestVersion returned will not be greater than this.
            HighestVersion = D3D_ROOT_SIGNATURE_VERSION_1_1
        };

        if (D3DDevice->CheckFeatureSupport(D3D12_FEATURE_ROOT_SIGNATURE, &featureData, (uint)sizeof(D3D12_FEATURE)).FAILED)
        {
            featureData.HighestVersion = D3D_ROOT_SIGNATURE_VERSION_1_0;
        }

        const int RangesCount = 1;
        var       ranges      = stackalloc D3D12_DESCRIPTOR_RANGE1[RangesCount];

        ranges[0].Init(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 0, 0, D3D12_DESCRIPTOR_RANGE_FLAG_DATA_STATIC);

        const int RootParametersCount = 1;
        var       rootParameters      = stackalloc D3D12_ROOT_PARAMETER1[RootParametersCount];

        rootParameters[0].InitAsDescriptorTable(1, ranges, D3D12_SHADER_VISIBILITY_PIXEL);

        var sampler = new D3D12_STATIC_SAMPLER_DESC {
            Filter           = D3D12_FILTER.D3D12_FILTER_MIN_MAG_MIP_POINT,
            AddressU         = D3D12_TEXTURE_ADDRESS_MODE_BORDER,
            AddressV         = D3D12_TEXTURE_ADDRESS_MODE_BORDER,
            AddressW         = D3D12_TEXTURE_ADDRESS_MODE_BORDER,
            MipLODBias       = 0,
            MaxAnisotropy    = 0,
            ComparisonFunc   = D3D12_COMPARISON_FUNC_NEVER,
            BorderColor      = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK,
            MinLOD           = 0.0f,
            MaxLOD           = D3D12_FLOAT32_MAX,
            ShaderRegister   = 0,
            RegisterSpace    = 0,
            ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL,
        };

        var rootSignatureDesc = new D3D12_VERSIONED_ROOT_SIGNATURE_DESC();

        rootSignatureDesc.Init_1_1(RootParametersCount, rootParameters, 1, &sampler, D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT);

        ThrowIfFailed(D3D12SerializeVersionedRootSignature(
                          &rootSignatureDesc, featureData.HighestVersion, signature.GetAddressOf(), error.GetAddressOf()));

        ID3D12RootSignature *rootSignature;

        ThrowIfFailed(D3DDevice->CreateRootSignature(0, signature.Get()->GetBufferPointer(), signature.Get()->GetBufferSize(), __uuidof <ID3D12RootSignature>(), (void **)&rootSignature));

        return(rootSignature);
    }
Example #21
0
        /// <inheritdoc cref="IEnumerator.MoveNext"/>
        public bool MoveNext()
        {
            Current.Dispose();

            IDXGIAdapter1 *p;

            Guard.ThrowIfFailed(_factory.Get()->EnumAdapters1(_index++, &p));

            Current = CreateAdapter(p);
            return(p != null);
        }
Example #22
0
        private AllocatorHeap CreateNewHeap(GpuMemoryType type)
        {
            D3D12_HEAP_PROPERTIES props = new((D3D12_HEAP_TYPE)type);
            D3D12_HEAP_DESC       desc  = new(InitialHeapSize, props);

            ComPtr <ID3D12Heap> heap = default;

            _device.Get()->CreateHeap(&desc, heap.Guid, ComPtr.GetVoidAddressOf(&heap));

            var allocatorHeap = new AllocatorHeap {
                Heap = heap.Move(), FreeBlocks = new()
            };

            allocatorHeap.FreeBlocks.Add(new HeapBlock {
                Offset = 0, Size = desc.SizeInBytes
            });
            _heaps[type].Add(allocatorHeap);

            return(allocatorHeap);
        }
Example #23
0
        /// <inheritdoc cref="IEnumerator.MoveNext"/>
        public bool MoveNext()
        {
            Current.Dispose();

            IDXGIOutput *p;

            Guard.ThrowIfFailed(_adapter.Get()->EnumOutputs(_index++, &p));

            Current = CreateOutput(p);
            return(p != null);
        }
    /// <summary>
    /// Applies the actual resize logic that was scheduled from <see cref="OnResize"/>.
    /// </summary>
    private unsafe void ApplyResize()
    {
        this.d3D12CommandQueue.Get()->Signal(this.d3D12Fence.Get(), this.nextD3D12FenceValue);

        // Wait for the fence again to ensure there are no pending operations
        this.d3D12Fence.Get()->SetEventOnCompletion(this.nextD3D12FenceValue, default);

        this.nextD3D12FenceValue++;

        // Dispose the old buffers before resizing the buffer
        this.d3D12Resource0.Dispose();
        this.d3D12Resource1.Dispose();

        // Resize the swap chain buffers
        this.dxgiSwapChain1.Get()->ResizeBuffers(0, 0, 0, DXGI_FORMAT.DXGI_FORMAT_UNKNOWN, 0);

        // Get the index of the initial back buffer
        using (ComPtr <IDXGISwapChain3> dxgiSwapChain3 = default)
        {
            _ = this.dxgiSwapChain1.CopyTo(dxgiSwapChain3.GetAddressOf());

            this.currentBufferIndex = dxgiSwapChain3.Get()->GetCurrentBackBufferIndex();
        }

        // Retrieve the back buffers for the swap chain
        fixed(ID3D12Resource **d3D12Resource0 = this.d3D12Resource0)
        fixed(ID3D12Resource **d3D12Resource1 = this.d3D12Resource1)
        {
            _ = dxgiSwapChain1.Get()->GetBuffer(0, Windows.__uuidof <ID3D12Resource>(), (void **)d3D12Resource0);
            _ = dxgiSwapChain1.Get()->GetBuffer(1, Windows.__uuidof <ID3D12Resource>(), (void **)d3D12Resource1);
        }

        this.texture?.Dispose();

        D3D12_RESOURCE_DESC d3D12Resource0Description = this.d3D12Resource0.Get()->GetDesc();

        // Create the 2D texture to use to generate frames to display
        this.texture = GraphicsDevice.Default.AllocateReadWriteTexture2D <Rgba32, float4>(
            (int)d3D12Resource0Description.Width,
            (int)d3D12Resource0Description.Height);
    }
Example #25
0
        public static ComPtr <TUp> UpCast <T, TUp>(ComPtr <T> comPtr)
            where T : unmanaged
            where TUp : unmanaged
        {
            // if this is hit, your cast is invalid. either use TryCast or have a valid type
#if DEBUG
            Debug.Assert(comPtr.TryQueryInterface(out ComPtr <TUp> assertion));
            assertion.Dispose();
#endif

            return((TUp *)comPtr.Get());
        }
Example #26
0
        void ClearBuffers()
        {
            var devCon = m_d3dContext.Get();

            devCon->ClearState();

            // clear the back buffer and depth / stencil buffer
            float[] color = new[] { 0.0f, 0.0f, 0.0f, 0.0f };
            devCon->ClearRenderTargetView(m_renderTargetView.Get(), color.AsSpan().AsPointer());
            devCon->ClearDepthStencilView(
                m_depthStencilView.Get(), (uint)(D3D11_CLEAR_FLAG.D3D11_CLEAR_DEPTH | D3D11_CLEAR_FLAG.D3D11_CLEAR_STENCIL), 1.0f, 0);
        }
Example #27
0
    /// <summary>
    /// Creates an <see cref="ID2D1Bitmap1"/> instance and copies data from a source bitmap.
    /// </summary>
    /// <param name="d2D1DeviceContext">The input <see cref="ID2D1DeviceContext"/> instance to use to create the bitmap.</param>
    /// <param name="d2D1Bitmap">The input <see cref="ID2D1Bitmap1"/> to read data from.</param>
    /// <param name="d2D1MappedRect">The resulting <see cref="D2D1_MAPPED_RECT"/> for the bitmap.</param>
    /// <returns>A new <see cref="ID2D1Bitmap1"/> instance.</returns>
    public static unsafe ComPtr <ID2D1Bitmap1> CreateD2D1Bitmap1Buffer(ID2D1DeviceContext *d2D1DeviceContext, ID2D1Bitmap *d2D1Bitmap, out D2D1_MAPPED_RECT d2D1MappedRect)
    {
        D2D_SIZE_U d2DSize = d2D1Bitmap->GetPixelSize();

        D2D1_BITMAP_PROPERTIES1 d2DBitmapProperties1Buffer = default;

        d2DBitmapProperties1Buffer.pixelFormat.format    = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM;
        d2DBitmapProperties1Buffer.pixelFormat.alphaMode = D2D1_ALPHA_MODE.D2D1_ALPHA_MODE_PREMULTIPLIED;
        d2DBitmapProperties1Buffer.bitmapOptions         = D2D1_BITMAP_OPTIONS.D2D1_BITMAP_OPTIONS_CPU_READ | D2D1_BITMAP_OPTIONS.D2D1_BITMAP_OPTIONS_CANNOT_DRAW;

        using ComPtr <ID2D1Bitmap1> d2D1Bitmap1Buffer = default;

        // Create a buffer D2D1 bitmap
        d2D1DeviceContext->CreateBitmap(
            size: d2DSize,
            sourceData: null,
            pitch: 0,
            bitmapProperties: &d2DBitmapProperties1Buffer,
            bitmap: d2D1Bitmap1Buffer.GetAddressOf()).Assert();

        D2D_POINT_2U d2DPointDestination = default;
        D2D_RECT_U   d2DRectSource       = new(0, 0, d2DSize.width, d2DSize.height);

        // Copy the image from the target to the readback bitmap
        d2D1Bitmap1Buffer.Get()->CopyFromBitmap(
            destPoint: &d2DPointDestination,
            bitmap: d2D1Bitmap,
            srcRect: &d2DRectSource);

        fixed(D2D1_MAPPED_RECT *d2D1MappedRectPtr = &d2D1MappedRect)
        {
            // Map the buffer bitmap
            d2D1Bitmap1Buffer.Get()->Map(
                options: D2D1_MAP_OPTIONS.D2D1_MAP_OPTIONS_READ,
                mappedRect: d2D1MappedRectPtr).Assert();
        }

        return(d2D1Bitmap1Buffer.Move());
    }
        protected override ComPtr <ID3D12CommandAllocator> Create(ExecutionContext state)
        {
            using ComPtr <ID3D12CommandAllocator> allocator = default;
            Guard.ThrowIfFailed(_device.Get()->CreateCommandAllocator(
                                    (D3D12_COMMAND_LIST_TYPE)state,
                                    allocator.Guid,
                                    ComPtr.GetVoidAddressOf(&allocator)
                                    ));

            DirectXHelpers.SetObjectName(allocator.Get(), $"Pooled allocator #{_allocatorCount++}");

            return(allocator.Move());
        }
Example #29
0
    /// <summary>
    /// Creates an <see cref="ID2D1Bitmap"/> instance.
    /// </summary>
    /// <param name="d2D1DeviceContext">The input <see cref="ID2D1DeviceContext"/> instance to use to create the bitmap source.</param>
    /// <param name="wicBitmap">The input <see cref="IWICBitmap"/> to use to create the bitmap source.</param>
    /// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> to set the source for.</param>
    /// <returns>A new <see cref="ID2D1Bitmap"/> instance.</returns>
    public static unsafe ComPtr <ID2D1Bitmap> CreateD2D1BitmapAndSetAsSource(ID2D1DeviceContext *d2D1DeviceContext, IWICBitmap *wicBitmap, ID2D1Effect *d2D1Effect)
    {
        using ComPtr <ID2D1Bitmap> d2D1BitmapSource = default;

        // Create a source D2D1 bitmap from the WIC bitmap
        d2D1DeviceContext->CreateBitmapFromWicBitmap(
            wicBitmapSource: (IWICBitmapSource *)wicBitmap,
            bitmap: d2D1BitmapSource.GetAddressOf()).Assert();

        d2D1Effect->SetInput(0, (ID2D1Image *)d2D1BitmapSource.Get());

        return(d2D1BitmapSource.Move());
    }
Example #30
0
    public static HRESULT CreateRootSignature(ID3D12Device *device, [NativeTypeName("const D3D12_ROOT_SIGNATURE_DESC *")] D3D12_ROOT_SIGNATURE_DESC *rootSignatureDesc, ID3D12RootSignature **rootSignature)
    {
        using ComPtr <ID3DBlob> pSignature = new ComPtr <ID3DBlob>();
        using ComPtr <ID3DBlob> pError     = new ComPtr <ID3DBlob>();

        HRESULT hr = D3D12SerializeRootSignature(rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, pSignature.GetAddressOf(), pError.GetAddressOf());

        if (SUCCEEDED(hr))
        {
            hr = device->CreateRootSignature(0, pSignature.Get()->GetBufferPointer(), pSignature.Get()->GetBufferSize(), __uuidof <ID3D12RootSignature>(), (void **)rootSignature);
        }

        return(hr);
    }