Exemplo n.º 1
0
        public static InputElementDescription[] CreateInputElements(this VertexBufferBinding[] vertexBuffers)
        {
            var inputElementCount = 0;

            foreach (var vertexBuffer in vertexBuffers)
            {
                inputElementCount += vertexBuffer.Declaration.VertexElements.Length;
            }

            var inputElements     = new InputElementDescription[inputElementCount];
            var inputElementIndex = 0;

            for (int inputSlot = 0; inputSlot < vertexBuffers.Length; inputSlot++)
            {
                var vertexBuffer = vertexBuffers[inputSlot];
                foreach (var element in vertexBuffer.Declaration.EnumerateWithOffsets())
                {
                    inputElements[inputElementIndex++] = new InputElementDescription
                    {
                        SemanticName      = element.VertexElement.SemanticName,
                        SemanticIndex     = element.VertexElement.SemanticIndex,
                        Format            = element.VertexElement.Format,
                        InputSlot         = inputSlot,
                        AlignedByteOffset = element.Offset,
                    };
                }
            }

            return(inputElements);
        }
Exemplo n.º 2
0
        public static InputElementDescription[] CreateInputElements(this VertexDeclaration vertexDeclaration)
        {
            var inputElements     = new InputElementDescription[vertexDeclaration.VertexElements.Length];
            var inputElementIndex = 0;

            foreach (var element in vertexDeclaration.EnumerateWithOffsets())
            {
                inputElements[inputElementIndex++] = new InputElementDescription
                {
                    SemanticName      = element.VertexElement.SemanticName,
                    SemanticIndex     = element.VertexElement.SemanticIndex,
                    Format            = element.VertexElement.Format,
                    InputSlot         = 0,
                    AlignedByteOffset = element.Offset,
                };
            }

            return(inputElements);
        }
Exemplo n.º 3
0
        public void CreatePipeline()
        {
            InputElementDescription[] inputElementDescs = new InputElementDescription[]
            {
                new InputElementDescription("POSITION", 0, Format.R32G32B32_Float, 0, 0),
                new InputElementDescription("COLOR", 0, Format.R32G32B32A32_Float, 12, 0),
            };

            var psoDesc = new GraphicsPipelineStateDescription()
            {
                RootSignature = RootSignature,

                InputLayout           = new InputLayoutDescription(inputElementDescs),
                SampleMask            = uint.MaxValue,
                PrimitiveTopologyType = PrimitiveTopologyType.Triangle,
                RasterizerState       = RasterizerDescription.CullCounterClockwise,
                BlendState            = BlendDescription.Opaque,
                DepthStencilState     = DepthStencilDescription.None,
                RenderTargetFormats   = new[] { Format.R8G8B8A8_UNorm },
                DepthStencilFormat    = Format.Unknown,
                SampleDescription     = new SampleDescription(1, 0),
                //IndexBufferStripCutValue = IndexBufferStripCutValue.Value0xFFFF
                StreamOutput = new StreamOutputDescription()
                {
                    //RasterizedStream =
                },


                HullShader = PipelineStateDescription.HullShader == null ? null : PipelineStateDescription.HullShader.Data,

                GeometryShader = PipelineStateDescription.GeometryShader == null ? null : PipelineStateDescription.GeometryShader.Data,

                DomainShader = PipelineStateDescription.DomainShader == null ? null : PipelineStateDescription.DomainShader.Data,

                VertexShader = PipelineStateDescription.VertexShader == null ? null : PipelineStateDescription.VertexShader.Data,

                PixelShader = PipelineStateDescription.PixelShader == null ? null : PipelineStateDescription.PixelShader.Data,
            };

            oldPipelineState = GraphicsDevice.NativeDevice.CreateGraphicsPipelineState <ID3D12PipelineState>(psoDesc);
        }
Exemplo n.º 4
0
        private ID3D11InputLayout CreateNewInputLayout(VertexLayoutDescription[] vertexLayouts, byte[] vsBytecode)
        {
            int totalCount = 0;

            for (int i = 0; i < vertexLayouts.Length; i++)
            {
                totalCount += vertexLayouts[i].Elements.Length;
            }

            int element = 0; // Total element index across slots.

            InputElementDescription[] elements = new InputElementDescription[totalCount];
            SemanticIndices           si       = new SemanticIndices();

            for (int slot = 0; slot < vertexLayouts.Length; slot++)
            {
                VertexElementDescription[] elementDescs = vertexLayouts[slot].Elements;
                uint stepRate      = vertexLayouts[slot].InstanceStepRate;
                int  currentOffset = 0;
                for (int i = 0; i < elementDescs.Length; i++)
                {
                    VertexElementDescription desc = elementDescs[i];
                    elements[element] = new InputElementDescription(
                        GetSemanticString(desc.Semantic),
                        SemanticIndices.GetAndIncrement(ref si, desc.Semantic),
                        D3D11Formats.ToDxgiFormat(desc.Format),
                        desc.Offset != 0 ? (int)desc.Offset : currentOffset,
                        slot,
                        stepRate == 0 ? InputClassification.PerVertexData : InputClassification.PerInstanceData,
                        (int)stepRate);

                    currentOffset += (int)FormatHelpers.GetSizeInBytes(desc.Format);
                    element       += 1;
                }
            }

            return(_device.CreateInputLayout(elements, vsBytecode));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Init device and required resources
        /// </summary>
        private void InitDevice()
        {
            // device creation
            device = D3DDevice.CreateDeviceAndSwapChain(host.Handle);
            swapChain = device.SwapChain;
            deviceContext = device.ImmediateContext;

            SetViews();

            // vertex shader & layout            
            // Open precompiled vertex shader
            // This file was compiled using: fxc Render.hlsl /T vs_4_0 /EVertShader /FoRender.vs
            using (Stream stream = Application.ResourceAssembly.GetManifestResourceStream("Microsoft.WindowsAPICodePack.Samples.Direct3D11.Render.vs"))
            {
                vertexShader = device.CreateVertexShader(stream);
                deviceContext.VS.Shader = vertexShader;

                // input layout is for the vert shader
                InputElementDescription inputElementDescription = new InputElementDescription();
                inputElementDescription.SemanticName = "POSITION";
                inputElementDescription.SemanticIndex = 0;
                inputElementDescription.Format = Format.R32G32B32Float;
                inputElementDescription.InputSlot = 0;
                inputElementDescription.AlignedByteOffset = 0;
                inputElementDescription.InputSlotClass = InputClassification.PerVertexData;
                inputElementDescription.InstanceDataStepRate = 0;
                stream.Position = 0;
                InputLayout inputLayout = device.CreateInputLayout(
                    new InputElementDescription[] { inputElementDescription },
                    stream);
                deviceContext.IA.InputLayout = inputLayout;
            }

            // Open precompiled vertex shader
            // This file was compiled using: fxc Render.hlsl /T ps_4_0 /EPixShader /FoRender.ps
            using (Stream stream = Application.ResourceAssembly.GetManifestResourceStream("Microsoft.WindowsAPICodePack.Samples.Direct3D11.Render.ps"))
            {
                pixelShader = device.CreatePixelShader(stream);
            }
            deviceContext.PS.SetShader(pixelShader, null);

            // create some geometry to draw (1 triangle)
            SimpleVertexArray vertex = new SimpleVertexArray();

            // put the vertices into a vertex buffer

            BufferDescription bufferDescription = new BufferDescription();
            bufferDescription.Usage = Usage.Default;
            bufferDescription.ByteWidth = (uint)Marshal.SizeOf(vertex);
            bufferDescription.BindingOptions = BindingOptions.VertexBuffer;

            SubresourceData subresourceData = new SubresourceData();

            IntPtr vertexData = Marshal.AllocCoTaskMem(Marshal.SizeOf(vertex));
            Marshal.StructureToPtr(vertex, vertexData, false);

            subresourceData.SystemMemory = vertexData;
            vertexBuffer = device.CreateBuffer(bufferDescription, subresourceData);


            deviceContext.IA.SetVertexBuffers(0, new D3DBuffer[] { vertexBuffer }, new uint[] { 12 }, new uint[] { 0 });
            deviceContext.IA.PrimitiveTopology = PrimitiveTopology.TriangleList;

            Marshal.FreeCoTaskMem(vertexData);
        }
Exemplo n.º 6
0
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            // device creation
            device        = D3DDevice.CreateDeviceAndSwapChain(Handle);
            swapChain     = device.SwapChain;
            deviceContext = device.ImmediateContext;

            SetViews();

            // Open precompiled vertex shader
            // This file was compiled using: fxc Render.hlsl /T vs_4_0 /EVertShader /FoRender.vs
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.WindowsAPICodePack.Samples.Direct3D11.Render.vs"))
            {
                vertexShader = device.CreateVertexShader(stream);
            }

            deviceContext.VS.SetShader(vertexShader, null);

            // input layout is for the vert shader
            InputElementDescription inputElementDescription = new InputElementDescription();

            inputElementDescription.SemanticName         = "POSITION";
            inputElementDescription.SemanticIndex        = 0;
            inputElementDescription.Format               = Format.R32G32B32Float;
            inputElementDescription.InputSlot            = 0;
            inputElementDescription.AlignedByteOffset    = 0;
            inputElementDescription.InputSlotClass       = InputClassification.PerVertexData;
            inputElementDescription.InstanceDataStepRate = 0;

            InputLayout inputLayout;

            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.WindowsAPICodePack.Samples.Direct3D11.Render.vs"))
            {
                inputLayout = device.CreateInputLayout(new InputElementDescription[] { inputElementDescription }, stream);
            }
            deviceContext.IA.InputLayout = inputLayout;

            // Open precompiled vertex shader
            // This file was compiled using: fxc Render.hlsl /T ps_4_0 /EPixShader /FoRender.ps
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.WindowsAPICodePack.Samples.Direct3D11.Render.ps"))
            {
                pixelShader = device.CreatePixelShader(stream);
            }
            deviceContext.PS.SetShader(pixelShader, null);


            // create some geometry to draw (1 triangle)
            SimpleVertexArray vertex = new SimpleVertexArray();

            // put the vertices into a vertex buffer

            BufferDescription bufferDescription = new BufferDescription();

            bufferDescription.Usage          = Usage.Default;
            bufferDescription.ByteWidth      = (uint)Marshal.SizeOf(vertex);
            bufferDescription.BindingOptions = BindingOptions.VertexBuffer;

            SubresourceData subresourceData = new SubresourceData();

            IntPtr vertexData = Marshal.AllocCoTaskMem(Marshal.SizeOf(vertex));

            Marshal.StructureToPtr(vertex, vertexData, false);

            subresourceData.SystemMemory = vertexData;
            vertexBuffer = device.CreateBuffer(bufferDescription, subresourceData);


            deviceContext.IA.SetVertexBuffers(0, new D3DBuffer[] { vertexBuffer }, new uint[] { 12 }, new uint[] { 0 });
            deviceContext.IA.PrimitiveTopology = PrimitiveTopology.TriangleList;

            Marshal.FreeCoTaskMem(vertexData);
        }
Exemplo n.º 7
0
        private void InitDevice()
        {
            // device creation
            //device = D3DDevice.CreateDeviceAndSwapChain(
            //    null,
            //    DriverType.Hardware,
            //    null,
            //    CreateDeviceFlag.Default,
            //    new []{FeatureLevel.FeatureLevel_10_1},
            //    new SwapChainDescription {
            //        BufferCount = 1
            //    },
            //    out swapChain);
            device = D3DDevice.CreateDeviceAndSwapChain(directControl.Handle, out swapChain);
            deviceContext = device.GetImmediateContext();

            SetViews();

            // Open precompiled vertex shader
            // This file was compiled using: fxc Render.hlsl /T vs_4_0 /EVertShader /FoRender.vs
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.WindowsAPICodePack.Samples.Direct3D11.Render.vs"))
            {
                vertexShader = device.CreateVertexShader(stream);
            }

            deviceContext.VS.SetShader(vertexShader, null);

            // input layout is for the vert shader
            InputElementDescription inputElementDescription = new InputElementDescription();
            inputElementDescription.SemanticName = "POSITION";
            inputElementDescription.SemanticIndex = 0;
            inputElementDescription.Format = Format.R32G32B32_FLOAT;
            inputElementDescription.InputSlot = 0;
            inputElementDescription.AlignedByteOffset = 0;
            inputElementDescription.InputSlotClass = InputClassification.PerVertexData;
            inputElementDescription.InstanceDataStepRate = 0;

            InputLayout inputLayout;
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.WindowsAPICodePack.Samples.Direct3D11.Render.vs"))
            {
                inputLayout = device.CreateInputLayout(new [] { inputElementDescription }, stream);
            }
            deviceContext.IA.SetInputLayout(inputLayout);

            // Open precompiled pixel shader
            // This file was compiled using: fxc Render.hlsl /T ps_4_0 /EPixShader /FoRender.ps
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.WindowsAPICodePack.Samples.Direct3D11.Render.ps"))
            {
                pixelShader = device.CreatePixelShader(stream);
            }
            deviceContext.PS.SetShader(pixelShader, null);


            // create some geometry to draw (1 triangle)
            SimpleVertexArray vertex = new SimpleVertexArray();

            // put the vertices into a vertex buffer

            BufferDescription bufferDescription = new BufferDescription();
            bufferDescription.Usage = Usage.Default;
            bufferDescription.ByteWidth = (uint)Marshal.SizeOf(vertex);
            bufferDescription.BindFlags = BindFlag.VertexBuffer;

            SubresourceData subresourceData = new SubresourceData();

            IntPtr vertexData = Marshal.AllocCoTaskMem(Marshal.SizeOf(vertex));
            Marshal.StructureToPtr(vertex, vertexData, false);

            subresourceData.SysMem = vertexData;
            vertexBuffer = device.CreateBuffer(bufferDescription, subresourceData);


            deviceContext.IA.SetVertexBuffers(0, new [] { vertexBuffer }, new uint[] { 12 }, new uint[] { 0 });
            deviceContext.IA.SetPrimitiveTopology(PrimitiveTopology.TriangleList);

            Marshal.FreeCoTaskMem(vertexData);
        }