public D3D11_VIEWPORT(ID3D11Buffer *param0, ID3D11RenderTargetView *pRTView, [NativeTypeName("FLOAT")] float topLeftX = 0.0f, [NativeTypeName("FLOAT")] float minDepth = D3D11_MIN_DEPTH, [NativeTypeName("FLOAT")] float maxDepth = D3D11_MAX_DEPTH)
        {
            D3D11_RENDER_TARGET_VIEW_DESC RTVDesc;

            pRTView->GetDesc(&RTVDesc);

            uint NumElements = 0;

            switch (RTVDesc.ViewDimension)
            {
            case D3D11_RTV_DIMENSION_BUFFER:
            {
                NumElements = RTVDesc.Anonymous.Buffer.Anonymous2.NumElements;
                break;
            }

            default:
            {
                break;
            }
            }

            TopLeftX = topLeftX;
            TopLeftY = 0.0f;
            Width    = NumElements - topLeftX;
            Height   = 1.0f;
            MinDepth = minDepth;
            MaxDepth = maxDepth;
        }
    public D3D11_RENDER_TARGET_VIEW_DESC1(ID3D11Buffer *param0, DXGI_FORMAT format, uint firstElement, uint numElements)
    {
        Format        = format;
        ViewDimension = D3D11_RTV_DIMENSION_BUFFER;
        Anonymous     = default;

        Anonymous.Buffer.Anonymous1.FirstElement = firstElement;
        Anonymous.Buffer.Anonymous2.NumElements  = numElements;
    }
Exemple #3
0
        public D3D11_SHADER_RESOURCE_VIEW_DESC(ID3D11Buffer *param0, DXGI_FORMAT format, [NativeTypeName("UINT")] uint firstElement, [NativeTypeName("UINT")] uint numElements, [NativeTypeName("UINT")] uint flags = 0)
        {
            Format        = format;
            ViewDimension = D3D11_SRV_DIMENSION_BUFFEREX;
            Anonymous     = default;

            Anonymous.BufferEx.FirstElement = firstElement;
            Anonymous.BufferEx.NumElements  = numElements;
            Anonymous.BufferEx.Flags        = flags;
        }
    public D3D11_UNORDERED_ACCESS_VIEW_DESC(ID3D11Buffer *param0, DXGI_FORMAT format, uint firstElement, uint numElements, uint flags = 0)
    {
        Format        = format;
        ViewDimension = D3D11_UAV_DIMENSION_BUFFER;
        Anonymous     = default;

        Anonymous.Buffer.FirstElement = firstElement;
        Anonymous.Buffer.NumElements  = numElements;
        Anonymous.Buffer.Flags        = flags;
    }
    protected virtual void DestroyBuffers()
    {
        DestroyVertexBuffer();

        void DestroyVertexBuffer()
        {
            var vertexBuffer = _vertexBuffer;

            if (vertexBuffer != null)
            {
                _vertexBuffer = null;
                _             = vertexBuffer->Release();
            }
        }
    }
 protected virtual void CreateBuffers()
 {
     _vertexBuffer = CreateVertexBuffer();
 }
Exemple #7
0
        static void InitializeDirect3D()
        {
            var desc = new DXGI_SWAP_CHAIN_DESC
            {
                BufferDesc = new DXGI_MODE_DESC
                {
                    Width  = WIDTH,
                    Height = HEIGHT,
                    Format = DXGI_FORMAT_R8G8B8A8_UNORM,
                },
                SampleDesc = new DXGI_SAMPLE_DESC
                {
                    Count = 1
                },
                BufferCount  = 1,
                Windowed     = TRUE,
                OutputWindow = form.Handle,
                BufferUsage  = DXGI_USAGE_RENDER_TARGET_OUTPUT,
                Flags        = (uint)DXGI_SWAP_CHAIN_FLAG.DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH
            };

            uint creationFlags = 0;

#if DEBUG
            creationFlags |= (uint)D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_DEBUG;
#endif

            fixed(IDXGISwapChain **swapchain = &_swapchain)
            fixed(ID3D11Device **device         = &_d3dDevice)
            fixed(ID3D11DeviceContext **context = &_d3dctx)
            {
                ThrowIfFailed(D3D11CreateDeviceAndSwapChain(null,
                                                            D3D_DRIVER_TYPE_HARDWARE,
                                                            IntPtr.Zero,
                                                            creationFlags,
                                                            null,
                                                            0,
                                                            D3D11_SDK_VERSION,
                                                            &desc,
                                                            swapchain,
                                                            device,
                                                            null,
                                                            context));
            }

            ID3D10Multithread *pMultithread;
            var iid = IID_ID3D10Multithread;

            ThrowIfFailed(_d3dDevice->QueryInterface(&iid, (void **)&pMultithread));
            pMultithread->SetMultithreadProtected(TRUE);
            pMultithread->Release();

            var viewport = new D3D11_VIEWPORT
            {
                Height = HEIGHT,
                Width  = WIDTH
            };

            _d3dctx->RSSetViewports(1, &viewport);

            fixed(ID3D11Device **device = &_d3deviceVLC)
            fixed(ID3D11DeviceContext **context = &_d3dctxVLC)
            {
                ThrowIfFailed(D3D11CreateDevice(null,
                                                D3D_DRIVER_TYPE_HARDWARE,
                                                IntPtr.Zero,
                                                creationFlags | (uint)D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_VIDEO_SUPPORT, /* needed for hardware decoding */
                                                null, 0,
                                                D3D11_SDK_VERSION,
                                                device, null, context));
            }

            using ComPtr <ID3D11Resource> pBackBuffer = null;

            iid = IID_ID3D11Texture2D;
            ThrowIfFailed(_swapchain->GetBuffer(0, &iid, (void **)pBackBuffer.GetAddressOf()));

            fixed(ID3D11RenderTargetView **swapchainRenderTarget = &_swapchainRenderTarget)
            ThrowIfFailed(_d3dDevice->CreateRenderTargetView(pBackBuffer.Get(), null, swapchainRenderTarget));

            pBackBuffer.Dispose();

            fixed(ID3D11RenderTargetView **swapchainRenderTarget = &_swapchainRenderTarget)
            _d3dctx->OMSetRenderTargets(1, swapchainRenderTarget, null);

            ID3DBlob *VS, PS, pErrBlob;

            using ComPtr <ID3DBlob> vertexShaderBlob = null;

            fixed(byte *shader = Encoding.ASCII.GetBytes(DefaultShaders.HLSL))
            fixed(byte *vshader = Encoding.ASCII.GetBytes("VShader"))
            fixed(byte *vs4     = Encoding.ASCII.GetBytes("vs_4_0"))
            fixed(byte *pshader = Encoding.ASCII.GetBytes("PShader"))
            fixed(byte *ps4     = Encoding.ASCII.GetBytes("ps_4_0"))
            {
                var result = D3DCompile(shader, (nuint)DefaultShaders.HLSL.Length, null, null, null, (sbyte *)vshader, (sbyte *)vs4, 0, 0, &VS, &pErrBlob);

                if (FAILED(result) && pErrBlob != null)
                {
                    var errorMessage = Encoding.ASCII.GetString((byte *)pErrBlob->GetBufferPointer(), (int)pErrBlob->GetBufferSize());
                    Debug.WriteLine(errorMessage);
                    ThrowIfFailed(result);
                }

                result = D3DCompile(shader, (nuint)DefaultShaders.HLSL.Length, null, null, null, (sbyte *)pshader, (sbyte *)ps4, 0, 0, &PS, &pErrBlob);
                if (FAILED(result) && pErrBlob != null)
                {
                    var errorMessage = Encoding.ASCII.GetString((byte *)pErrBlob->GetBufferPointer(), (int)pErrBlob->GetBufferSize());
                    Debug.WriteLine(errorMessage);
                    ThrowIfFailed(result);
                }
            }

            fixed(ID3D11VertexShader **vertexShader = &pVS)
            fixed(ID3D11PixelShader **pixelShader = &pPS)
            {
                ThrowIfFailed(_d3dDevice->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), null, vertexShader));
                ThrowIfFailed(_d3dDevice->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), null, pixelShader));
            }

            fixed(byte *position = Encoding.ASCII.GetBytes("POSITION"))
            fixed(byte *textcoord = Encoding.ASCII.GetBytes("TEXCOORD"))
            fixed(ID3D11InputLayout **shadersInputLayout = &pShadersInputLayout)
            {
                var inputElementDescs = stackalloc D3D11_INPUT_ELEMENT_DESC[2];

                {
                    inputElementDescs[0] = new D3D11_INPUT_ELEMENT_DESC
                    {
                        SemanticName         = (sbyte *)position,
                        SemanticIndex        = 0,
                        Format               = DXGI_FORMAT_R32G32B32_FLOAT,
                        InputSlot            = 0,
                        AlignedByteOffset    = D3D11_APPEND_ALIGNED_ELEMENT,
                        InputSlotClass       = D3D11_INPUT_PER_VERTEX_DATA,
                        InstanceDataStepRate = 0
                    };

                    inputElementDescs[1] = new D3D11_INPUT_ELEMENT_DESC
                    {
                        SemanticName         = (sbyte *)textcoord,
                        SemanticIndex        = 0,
                        Format               = DXGI_FORMAT_R32G32_FLOAT,
                        InputSlot            = 0,
                        AlignedByteOffset    = D3D11_APPEND_ALIGNED_ELEMENT,
                        InputSlotClass       = D3D11_INPUT_PER_VERTEX_DATA,
                        InstanceDataStepRate = 0
                    };
                }

                ThrowIfFailed(_d3dDevice->CreateInputLayout(inputElementDescs, 2, VS->GetBufferPointer(), VS->GetBufferSize(), shadersInputLayout));
            }

            var ourVerticles = new ShaderInput[4];

            ourVerticles[0] = new ShaderInput
            {
                position = new Position
                {
                    x = BORDER_LEFT,
                    y = BORDER_BOTTOM,
                    z = 0.0f
                },
                texture = new Texture {
                    x = 0.0f, y = 1.0f
                }
            };

            ourVerticles[1] = new ShaderInput
            {
                position = new Position
                {
                    x = BORDER_RIGHT,
                    y = BORDER_BOTTOM,
                    z = 0.0f
                },
                texture = new Texture {
                    x = 1.0f, y = 1.0f
                }
            };

            ourVerticles[2] = new ShaderInput
            {
                position = new Position
                {
                    x = BORDER_RIGHT,
                    y = BORDER_TOP,
                    z = 0.0f
                },
                texture = new Texture {
                    x = 1.0f, y = 0.0f
                }
            };

            ourVerticles[3] = new ShaderInput
            {
                position = new Position
                {
                    x = BORDER_LEFT,
                    y = BORDER_TOP,
                    z = 0.0f
                },
                texture = new Texture {
                    x = 0.0f, y = 0.0f
                }
            };

            var verticlesSize = (uint)sizeof(ShaderInput) * 4;

            var bd = new D3D11_BUFFER_DESC
            {
                Usage          = D3D11_USAGE_DYNAMIC,
                ByteWidth      = verticlesSize,
                BindFlags      = (uint)D3D11_BIND_VERTEX_BUFFER,
                CPUAccessFlags = (uint)D3D11_CPU_ACCESS_WRITE
            };

            pVertexBuffer      = CreateBuffer(bd);
            vertexBufferStride = Marshal.SizeOf(ourVerticles[0]);

            D3D11_MAPPED_SUBRESOURCE ms;

            ID3D11Resource *res;
            iid = IID_ID3D11Resource;

            ThrowIfFailed(pVertexBuffer->QueryInterface(&iid, (void **)&res));

            ThrowIfFailed(_d3dctx->Map(res, 0, D3D11_MAP_WRITE_DISCARD, 0, &ms));
            for (var i = 0; i < ourVerticles.Length; i++)
            {
                Marshal.StructureToPtr(ourVerticles[i], (IntPtr)ms.pData + (i * vertexBufferStride), false);
            }
            //Buffer.MemoryCopy(ms.pData, ourVerticles, verticlesSize, verticlesSize);
            _d3dctx->Unmap(res, 0);

            quadIndexCount = 6;

            var bufferDesc = new D3D11_BUFFER_DESC
            {
                Usage          = D3D11_USAGE_DYNAMIC,
                ByteWidth      = sizeof(ushort) * quadIndexCount,
                BindFlags      = (uint)D3D11_BIND_INDEX_BUFFER,
                CPUAccessFlags = (uint)D3D11_CPU_ACCESS_WRITE
            };

            pIndexBuffer = CreateBuffer(bufferDesc);

            ThrowIfFailed(pIndexBuffer->QueryInterface(&iid, (void **)&res));

            ThrowIfFailed(_d3dctx->Map(res, 0, D3D11_MAP_WRITE_DISCARD, 0, &ms));
            Marshal.WriteInt16((IntPtr)ms.pData, 0 * sizeof(ushort), 3);
            Marshal.WriteInt16((IntPtr)ms.pData, 1 * sizeof(ushort), 1);
            Marshal.WriteInt16((IntPtr)ms.pData, 2 * sizeof(ushort), 0);
            Marshal.WriteInt16((IntPtr)ms.pData, 3 * sizeof(ushort), 2);
            Marshal.WriteInt16((IntPtr)ms.pData, 4 * sizeof(ushort), 1);
            Marshal.WriteInt16((IntPtr)ms.pData, 5 * sizeof(ushort), 3);

            _d3dctx->Unmap(res, 0);

            _d3dctx->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
            _d3dctx->IASetInputLayout(pShadersInputLayout);
            uint offset = 0;

            var vv = (uint)vertexBufferStride;

            fixed(ID3D11Buffer **buffer = &pVertexBuffer)
            _d3dctx->IASetVertexBuffers(0, 1, buffer, &vv, &offset);

            _d3dctx->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R16_UINT, 0);

            _d3dctx->VSSetShader(pVS, null, 0);
            _d3dctx->PSSetShader(pPS, null, 0);

            var samplerDesc = new D3D11_SAMPLER_DESC
            {
                Filter         = D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT,
                AddressU       = D3D11_TEXTURE_ADDRESS_CLAMP,
                AddressV       = D3D11_TEXTURE_ADDRESS_CLAMP,
                AddressW       = D3D11_TEXTURE_ADDRESS_CLAMP,
                ComparisonFunc = D3D11_COMPARISON_ALWAYS,
                MinLOD         = 0,
                MaxLOD         = D3D11_FLOAT32_MAX
            };

            fixed(ID3D11SamplerState **ss = &samplerState)
            {
                ThrowIfFailed(_d3dDevice->CreateSamplerState(&samplerDesc, ss));
                _d3dctx->PSSetSamplers(0, 1, ss);
            }

            fixed(RTL_CRITICAL_SECTION *sl = &sizeLock)
            InitializeCriticalSection(sl);
        }
Exemple #8
0
 public void CopyStructureCount([NativeTypeName("ID3D11Buffer *")] ID3D11Buffer *pDstBuffer, [NativeTypeName("UINT")] uint DstAlignedByteOffset, [NativeTypeName("ID3D11UnorderedAccessView *")] ID3D11UnorderedAccessView *pSrcView)
 {
     ((delegate * stdcall <ID3D11DeviceContext *, ID3D11Buffer *, uint, ID3D11UnorderedAccessView *, void>)(lpVtbl[49]))((ID3D11DeviceContext *)Unsafe.AsPointer(ref this), pDstBuffer, DstAlignedByteOffset, pSrcView);
 }
Exemple #9
0
 public void DispatchIndirect([NativeTypeName("ID3D11Buffer *")] ID3D11Buffer *pBufferForArgs, [NativeTypeName("UINT")] uint AlignedByteOffsetForArgs)
 {
     ((delegate * stdcall <ID3D11DeviceContext *, ID3D11Buffer *, uint, void>)(lpVtbl[42]))((ID3D11DeviceContext *)Unsafe.AsPointer(ref this), pBufferForArgs, AlignedByteOffsetForArgs);
 }
Exemple #10
0
 public void IASetIndexBuffer([NativeTypeName("ID3D11Buffer *")] ID3D11Buffer *pIndexBuffer, DXGI_FORMAT Format, [NativeTypeName("UINT")] uint Offset)
 {
     ((delegate * stdcall <ID3D11DeviceContext *, ID3D11Buffer *, DXGI_FORMAT, uint, void>)(lpVtbl[19]))((ID3D11DeviceContext *)Unsafe.AsPointer(ref this), pIndexBuffer, Format, Offset);
 }
Exemple #11
0
        protected override void Dispose(bool isDisposing)
        {
            var vertexBuffer = _vertexBuffer;

            if (vertexBuffer != null)
            {
                _vertexBuffer = null;
                _             = vertexBuffer->Release();
            }

            var inputLayout = _inputLayout;

            if (inputLayout != null)
            {
                _inputLayout = null;
                _            = inputLayout->Release();
            }

            var pixelShader = _pixelShader;

            if (pixelShader != null)
            {
                _pixelShader = null;
                _            = pixelShader->Release();
            }

            var vertexShader = _vertexShader;

            if (vertexShader != null)
            {
                _vertexShader = null;
                _             = vertexShader->Release();
            }

            var swapChain = _swapChain;

            if (swapChain != null)
            {
                _swapChain = null;
                _          = swapChain->Release();
            }

            var immediateContext = _immediateContext;

            if (immediateContext != null)
            {
                _immediateContext = null;
                _ = immediateContext->Release();
            }

            var device = _device;

            if (device != null)
            {
                _device = null;
                _       = device->Release();
            }

            var renderTarget = _renderTarget;

            if (renderTarget != null)
            {
                _renderTarget = null;
                _             = renderTarget->Release();
            }

            base.Dispose(isDisposing);
        }