private void PerformStreamOut()
        {
            var commandList = Game.GraphicsContext.CommandList;

            commandList.SetPipelineState(pipelineState);

            var meshDraw = renderedMesh.Draw;

            /* vertex buffer(s) */
            for (int i = 0; i < meshDraw.VertexBuffers.Length; i++)
            {
                var vertexBufferBinding = meshDraw.VertexBuffers[i];
                commandList.SetVertexBuffer(i, vertexBufferBinding.Buffer, meshDraw.StartLocation, vertexBufferBinding.Declaration.VertexStride);
            }

            var indexBuffer = meshDraw.IndexBuffer.Buffer;

            commandList.SetIndexBuffer(indexBuffer, 0, meshDraw.IndexBuffer.Is32Bit);

            commandList.SetStreamTargets(streamOutBufferBinding.Buffer);

            streamShader.Parameters.Set(MultiMeshShaderKeys.ModelTransforms, transformBuffer);
            streamShader.Parameters.Set(MultiMeshShaderKeys.ModelColors, colorBuffer);
            streamShader.Apply(Game.GraphicsContext);

            /* finally write to our streamout buffer */
            commandList.DrawIndexedInstanced(indexBuffer.ElementCount, objects.Count);
            commandList.SetStreamTargets(null);
        }
Beispiel #2
0
        /// <summary>
        /// Begins text rendering (swaps and maps the vertex buffer to write to).
        /// </summary>
        /// <param name="graphicsContext">The current GraphicsContext.</param>
        public void End([NotNull] GraphicsContext graphicsContext)
        {
            if (graphicsContext == null)
            {
                throw new ArgumentNullException(nameof(graphicsContext));
            }

            // Unmap the vertex buffer
            graphicsContext.CommandList.UnmapSubresource(mappedVertexBuffer);
            mappedVertexBufferPointer = IntPtr.Zero;

            // Update pipeline state
            pipelineState.State.SetDefaults();
            pipelineState.State.RootSignature     = simpleEffect.RootSignature;
            pipelineState.State.EffectBytecode    = simpleEffect.Effect.Bytecode;
            pipelineState.State.DepthStencilState = DepthStencilStates.None;
            pipelineState.State.BlendState        = BlendStates.AlphaBlend;
            pipelineState.State.Output.CaptureState(graphicsContext.CommandList);
            pipelineState.State.InputElements = inputElementDescriptions[activeVertexBufferIndex];
            pipelineState.Update();

            graphicsContext.CommandList.SetPipelineState(pipelineState.CurrentState);

            // Update effect
            simpleEffect.UpdateEffect(graphicsContext.CommandList.GraphicsDevice);
            simpleEffect.Apply(graphicsContext);

            // Bind and draw
            graphicsContext.CommandList.SetVertexBuffer(0, vertexBuffersBinding[activeVertexBufferIndex].Buffer, vertexBuffersBinding[activeVertexBufferIndex].Offset, vertexBuffersBinding[activeVertexBufferIndex].Stride);
            graphicsContext.CommandList.SetIndexBuffer(indexBufferBinding.Buffer, 0, indexBufferBinding.Is32Bit);

            graphicsContext.CommandList.DrawIndexed(charsToRenderCount * 6);
        }
        protected override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            var cl = GraphicsContext.CommandList;

            cl.ResetTargets();
            var gp = GraphicsDevice.Presenter;

            cl.Clear(gp.BackBuffer, Color.Blue);
            cl.Clear(gp.DepthStencilBuffer, DepthStencilClearOptions.DepthBuffer | DepthStencilClearOptions.Stencil);
            // Render to the backbuffer
            cl.SetRenderTargetAndViewport(gp.DepthStencilBuffer, gp.BackBuffer);

            if (_tribuf == null)
            {
                Init();
            }

            simpleEffect.UpdateEffect(GraphicsDevice);

            pipelineState.State.RootSignature  = simpleEffect.RootSignature;
            pipelineState.State.EffectBytecode = simpleEffect.Effect.Bytecode;
            pipelineState.State.BlendState     = BlendStates.Default;
            pipelineState.State.Output.CaptureState(cl);
            pipelineState.Update();

            cl.SetPipelineState(pipelineState.CurrentState);

            // Apply the effect
            simpleEffect.Apply(GraphicsContext);

            cl.SetVertexBuffer(0, _tribuf, 0, 16);
            cl.Draw(3, 0);
        }
Beispiel #4
0
        private void DrawTextureSampling()
        {
            // Clears the screen
            GraphicsContext.CommandList.Clear(GraphicsDevice.Presenter.BackBuffer, Color.LightBlue);
            GraphicsContext.CommandList.Clear(GraphicsDevice.Presenter.DepthStencilBuffer, DepthStencilClearOptions.DepthBuffer | DepthStencilClearOptions.Stencil);
            GraphicsContext.CommandList.SetRenderTargetAndViewport(GraphicsDevice.Presenter.DepthStencilBuffer, GraphicsDevice.Presenter.BackBuffer);

            // Create pipeline state
            pipelineState.State.SetDefaults();
            pipelineState.State.RootSignature   = simpleEffect.RootSignature;
            pipelineState.State.EffectBytecode  = simpleEffect.Effect.Bytecode;
            pipelineState.State.InputElements   = mesh.Draw.VertexBuffers.CreateInputElements();
            pipelineState.State.PrimitiveType   = PrimitiveType.TriangleList;
            pipelineState.State.RasterizerState = RasterizerStates.CullNone;
            pipelineState.State.Output.CaptureState(GraphicsContext.CommandList);
            pipelineState.Update();

            // Set pipeline state
            GraphicsContext.CommandList.SetPipelineState(pipelineState.CurrentState);

            // Set vertex/index buffers
            GraphicsContext.CommandList.SetVertexBuffer(0, mesh.Draw.VertexBuffers[0].Buffer, mesh.Draw.VertexBuffers[0].Offset, mesh.Draw.VertexBuffers[0].Stride);
            GraphicsContext.CommandList.SetIndexBuffer(mesh.Draw.IndexBuffer.Buffer, mesh.Draw.IndexBuffer.Offset, mesh.Draw.IndexBuffer.Is32Bit);

            for (var i = 0; i < myDraws.Length; ++i)
            {
                simpleEffect.Parameters.Set(TexturingKeys.Sampler, myDraws[i].Sampler);
                simpleEffect.Parameters.Set(SpriteBaseKeys.MatrixTransform, myDraws[i].Transform);
                simpleEffect.Apply(GraphicsContext);
                GraphicsContext.CommandList.DrawIndexed(6);
            }
        }
Beispiel #5
0
        private void DrawPrimitives()
        {
            // Clears the screen with the Color.CornflowerBlue
            GraphicsContext.CommandList.Clear(GraphicsDevice.Presenter.BackBuffer, Color.CornflowerBlue);
            GraphicsContext.CommandList.Clear(GraphicsDevice.Presenter.DepthStencilBuffer, DepthStencilClearOptions.DepthBuffer | DepthStencilClearOptions.Stencil);
            GraphicsContext.CommandList.SetRenderTargetAndViewport(GraphicsDevice.Presenter.DepthStencilBuffer, GraphicsDevice.Presenter.BackBuffer);

            // Render each primitive
            for (int i = 0; i < Math.Min(primitives.Count, 8); i++)
            {
                var primitive = primitives[i + primitiveStartOffset];

                // Calculate the translation
                float dx = (i % 4);
                float dy = (i >> 2);

                float x = (dx - 1.5f) * 1.7f;
                float y = 1.0f - 2.0f * dy;

                var time = timeSeconds + i;

                // Setup the World matrice for this primitive
                var world = Matrix.Scaling((float)Math.Sin(time * 1.5f) * 0.2f + 1.0f) * Matrix.RotationX(time) * Matrix.RotationY(time * 2.0f) * Matrix.RotationZ(time * .7f) * Matrix.Translation(x, y, 0);

                // Disable Cull only for the plane primitive, otherwise use standard culling
                var defaultRasterizerState = i == 0 ? RasterizerStates.CullNone : RasterizerStates.CullBack;
                // TODO GRAPHICS REFACTOR
                //GraphicsDevice.SetRasterizerState(isWireframe? wireframeState: defaultRasterizerState);

                // Draw the primitive using BasicEffect
                simpleEffect.Parameters.Set(SpriteBaseKeys.MatrixTransform, Matrix.Multiply(world, Matrix.Multiply(view, projection)));
                simpleEffect.Apply(GraphicsContext);
                primitive.Draw(GraphicsContext.CommandList, simpleEffect);
            }
        }
Beispiel #6
0
        protected override void DrawCore(RenderDrawContext context)
        {
            if (string.IsNullOrEmpty(ShaderSourceName))
            {
                return;
            }

            Parameters.Set(ComputeEffectShaderKeys.ThreadNumbers, ThreadNumbers);
            Parameters.Set(ComputeEffectShaderKeys.ComputeShaderName, ShaderSourceName);
            Parameters.Set(ComputeShaderBaseKeys.ThreadGroupCountGlobal, ThreadGroupCounts);

            if (pipelineStateDirty)
            {
                EffectInstance.UpdateEffect(GraphicsDevice);

                pipelineState.State.SetDefaults();
                pipelineState.State.RootSignature  = EffectInstance.RootSignature;
                pipelineState.State.EffectBytecode = EffectInstance.Effect.Bytecode;
                pipelineState.Update();
                pipelineStateDirty = false;
            }

            // Apply pipeline state
            context.CommandList.SetPipelineState(pipelineState.CurrentState);

            // Apply the effect
            EffectInstance.Apply(context.GraphicsContext);

            // Draw a full screen quad
            context.CommandList.Dispatch(ThreadGroupCounts.X, ThreadGroupCounts.Y, ThreadGroupCounts.Z);

            // Un-apply
            //throw new InvalidOperationException();
            //EffectInstance.Effect.UnbindResources(GraphicsDevice);
        }
Beispiel #7
0
        protected override void DrawCore(RenderDrawContext context)
        {
            if (EffectInstance.UpdateEffect(GraphicsDevice) || pipelineStateDirty || previousBytecode != EffectInstance.Effect.Bytecode)
            {
                // The EffectInstance might have been updated from outside
                previousBytecode = EffectInstance.Effect.Bytecode;

                pipelineState.State.SetDefaults();
                pipelineState.State.RootSignature  = EffectInstance.RootSignature;
                pipelineState.State.EffectBytecode = EffectInstance.Effect.Bytecode;
                pipelineState.State.InputElements  = PrimitiveQuad.VertexDeclaration.CreateInputElements();
                pipelineState.State.PrimitiveType  = PrimitiveQuad.PrimitiveType;
                pipelineState.State.BlendState     = blendState;
                pipelineState.State.Output.CaptureState(context.CommandList);
                pipelineState.Update();
                pipelineStateDirty = false;
            }

            context.CommandList.SetPipelineState(pipelineState.CurrentState);

            EffectInstance.Apply(context.GraphicsContext);

            // Draw a full screen quad
            context.GraphicsDevice.PrimitiveQuad.Draw(context.CommandList);
        }
Beispiel #8
0
        protected override unsafe void DrawCore(RenderDrawContext context)
        {
            // Clear render targets if there is a dependency conflict (D3D11 warning)
            if (delaySetRenderTargets)
            {
                context.CommandList.ResetTargets();
            }

            if (EffectInstance.UpdateEffect(GraphicsDevice) || pipelineStateDirty || previousBytecode != EffectInstance.Effect.Bytecode)
            {
                // The EffectInstance might have been updated from outside
                previousBytecode = EffectInstance.Effect.Bytecode;

                pipelineState.State.RootSignature     = EffectInstance.RootSignature;
                pipelineState.State.EffectBytecode    = EffectInstance.Effect.Bytecode;
                pipelineState.State.BlendState        = blendState;
                pipelineState.State.DepthStencilState = depthStencilState;

                var renderTargetCount = OutputCount;
                if (renderTargetCount > 0)
                {
                    // Special case: texture cube
                    var isTextureCube = GetOutput(0).ViewDimension == TextureDimension.TextureCube;
                    if (isTextureCube)
                    {
                        renderTargetCount = 6;
                    }

                    // Capture output state manually (since render targets might not be bound if delaySetRenderTargets is set to true)
                    pipelineState.State.Output.RenderTargetCount = renderTargetCount;
                    fixed(PixelFormat *pixelFormatStart = &pipelineState.State.Output.RenderTargetFormat0)
                    for (int i = 0; i < renderTargetCount; ++i)
                    {
                        pixelFormatStart[i] = GetOutput(isTextureCube ? 0 : i).ViewFormat;
                    }
                }

                if (HasDepthStencilOutput)
                {
                    pipelineState.State.Output.DepthStencilFormat = DepthStencil.Format;
                }

                pipelineState.Update();
                pipelineStateDirty = false;
            }

            context.CommandList.SetPipelineState(pipelineState.CurrentState);

            EffectInstance.Apply(context.GraphicsContext);

            // Now that resources are bound, set render targets
            if (delaySetRenderTargets)
            {
                SetRenderTargets(context);
            }

            // Draw a full screen quad
            context.GraphicsDevice.PrimitiveQuad.Draw(context.CommandList);
        }
Beispiel #9
0
        void RenderDrawLists(ImDrawDataPtr drawData)
        {
            // view proj
            var surfaceSize = input.Mouse.SurfaceSize;
            var projMatrix  = Matrix.OrthoRH(surfaceSize.X, -surfaceSize.Y, -1, 1);

            CheckBuffers(drawData);  // potentially resize buffers first if needed
            UpdateBuffers(drawData); // updeet em now

            // set pipeline stuff
            var is32Bits = false;

            commandList.SetPipelineState(imPipeline);
            commandList.SetVertexBuffer(0, vertexBinding.Buffer, 0, Unsafe.SizeOf <ImDrawVert>());
            commandList.SetIndexBuffer(indexBinding.Buffer, 0, is32Bits);
            imShader.Parameters.Set(ImGuiShaderKeys.tex, fontTexture);

            int vtxOffset = 0;
            int idxOffset = 0;

            for (int n = 0; n < drawData.CmdListsCount; n++)
            {
                ImDrawListPtr cmdList = drawData.CmdListsRange[n];

                for (int i = 0; i < cmdList.CmdBuffer.Size; i++)
                {
                    ImDrawCmdPtr cmd = cmdList.CmdBuffer[i];

                    if (cmd.TextureId != IntPtr.Zero)
                    {
                        // imShader.Parameters.Set(ImGuiShaderKeys.tex, fontTexture);
                    }
                    else
                    {
                        commandList.SetScissorRectangle(
                            new Rectangle(
                                (int)cmd.ClipRect.X,
                                (int)cmd.ClipRect.Y,
                                (int)(cmd.ClipRect.Z - cmd.ClipRect.X),
                                (int)(cmd.ClipRect.W - cmd.ClipRect.Y)
                                )
                            );

                        imShader.Parameters.Set(ImGuiShaderKeys.tex, fontTexture);
                        imShader.Parameters.Set(ImGuiShaderKeys.proj, ref projMatrix);
                        imShader.Apply(context);

                        commandList.DrawIndexed((int)cmd.ElemCount, idxOffset, vtxOffset);
                    }

                    idxOffset += (int)cmd.ElemCount;
                }

                vtxOffset += cmdList.VtxBuffer.Size;
            }
        }
        /// <summary>
        /// Draws a fullscreen quad with the specified effect and parameters.
        /// </summary>
        /// <param name="device">The device.</param>
        /// <param name="effectInstance">The effect instance.</param>
        /// <exception cref="System.ArgumentNullException">effect</exception>
        public static void DrawQuad(this GraphicsContext graphicsContext, EffectInstance effectInstance)
        {
            if (effectInstance == null)
            {
                throw new ArgumentNullException("effectInstance");
            }

            // Apply the effect
            effectInstance.Apply(graphicsContext);

            // Draw a full screen quad
            graphicsContext.CommandList.GraphicsDevice.PrimitiveQuad.Draw(graphicsContext.CommandList, effectInstance);
        }
Beispiel #11
0
        /// <summary>
        /// Draws a quad. The effect must have been applied before calling this method with pixel shader having the signature float2:TEXCOORD.
        /// </summary>
        /// <param name="texture"></param>
        public void Draw(GraphicsContext graphicsContext, EffectInstance effectInstance)
        {
            effectInstance.UpdateEffect(GraphicsDevice);

            pipelineState.State.RootSignature  = effectInstance.RootSignature;
            pipelineState.State.EffectBytecode = effectInstance.Effect.Bytecode;
            pipelineState.State.BlendState     = BlendStates.Default;
            pipelineState.State.Output.CaptureState(graphicsContext.CommandList);
            pipelineState.Update();

            graphicsContext.CommandList.SetPipelineState(pipelineState.CurrentState);

            // Apply the effect
            effectInstance.Apply(graphicsContext);

            Draw(graphicsContext.CommandList);
        }
        /// <summary>
        /// Dispatches the compute shader and sets the counter value, change per iteration effect parameters right before this call.
        /// </summary>
        public void DrawIteration()
        {
            if (FCompiled && drawCommandList != null && drawRenderDrawContext != null)
            {
                // Apply the effect, TODO: only update parameters here and Apply only once in Draw
                EffectInstance.Apply(drawRenderDrawContext.GraphicsContext);

                // Dispatch compute shader
                if (IsIndirect)
                {
                    drawCommandList.DispatchIndirect(IndirectArgsBuffer, ArgsBufferAlignedByteOffset);
                }
                else
                {
                    drawCommandList.Dispatch(ThreadGroupCounts.X, ThreadGroupCounts.Y, ThreadGroupCounts.Z);
                }
            }
        }
Beispiel #13
0
        /// <summary>
        /// Draws this <see cref="GeometricPrimitive" />.
        /// </summary>
        /// <param name="commandList">The command list.</param>
        public void Draw(GraphicsContext graphicsContext, EffectInstance effectInstance)
        {
            var commandList = graphicsContext.CommandList;

            // Update pipeline state
            PipelineState.State.RootSignature  = effectInstance.RootSignature;
            PipelineState.State.EffectBytecode = effectInstance.Effect.Bytecode;
            PipelineState.State.Output.CaptureState(commandList);
            PipelineState.Update();
            commandList.SetPipelineState(PipelineState.CurrentState);

            effectInstance.Apply(graphicsContext);

            // Setup the Vertex Buffer
            commandList.SetIndexBuffer(IndexBuffer, 0, IsIndex32Bits);
            commandList.SetVertexBuffer(0, VertexBuffer, 0, VertexBufferBinding.Stride);

            // Finally Draw this mesh
            commandList.DrawIndexed(IndexBuffer.ElementCount);
        }
Beispiel #14
0
        private void Draw(RenderDrawContext context, MeshDraw drawData, Vector4 color, DepthStencilStateDescription depthStencilState)
        {
            pipelineState.State.DepthStencilState = depthStencilState;
            pipelineState.State.Output.CaptureState(context.CommandList);
            pipelineState.Update();

            context.CommandList.SetIndexBuffer(drawData.IndexBuffer.Buffer, drawData.IndexBuffer.Offset, drawData.IndexBuffer.Is32Bit);
            context.CommandList.SetPipelineState(pipelineState.CurrentState);

            shader.Parameters.Set(SelectionWireframeShaderKeys.LineColor, color);
            shader.Apply(context.GraphicsContext);

            if (drawData.IndexBuffer != null)
            {
                context.CommandList.DrawIndexed(drawData.DrawCount, drawData.StartLocation);
            }
            else
            {
                context.CommandList.Draw(drawData.DrawCount, drawData.StartLocation);
            }
        }
Beispiel #15
0
        private void PerformStreamOut()
        {
            var commandList = Game.GraphicsContext.CommandList;

            /* TODO: this currently assumes a single vertex buffer, is this always the case? */
            var vertexBuffer = renderedMesh.Draw.VertexBuffers[0].Buffer;
            var indexBuffer  = renderedMesh.Draw.IndexBuffer.Buffer;

            commandList.SetPipelineState(pipelineState);

            /* TODO: this currently assumes a single vertex buffer, is this always the case? */
            commandList.SetVertexBuffer(0, vertexBuffer, renderedMesh.Draw.StartLocation, renderedMesh.Draw.VertexBuffers[0].Stride);
            commandList.SetIndexBuffer(indexBuffer, 0, renderedMesh.Draw.IndexBuffer.Is32Bit);
            commandList.SetStreamTargets(streamOutBufferBinding.Buffer);

            streamShader.Parameters.Set(MultiMeshShaderKeys.ModelTransforms, transformBuffer);
            streamShader.Apply(Game.GraphicsContext);

            /* finally write to our streamout buffer */
            commandList.DrawIndexedInstanced(indexBuffer.ElementCount == 0 ? indexBuffer.Description.SizeInBytes / 2 : indexBuffer.ElementCount, transforms.Count);
            commandList.SetStreamTargets(null);
        }
Beispiel #16
0
        /// <summary>
        /// Begins text rendering (swaps and maps the vertex buffer to write to).
        /// </summary>
        /// <param name="graphicsContext">The current GraphicsContext.</param>
        public void End([NotNull] GraphicsContext graphicsContext)
        {
            if (graphicsContext == null)
            {
                throw new ArgumentNullException(nameof(graphicsContext));
            }

            // Reallocate buffers if max number of characters is exceeded
            if (charsToRenderCount > maxCharacterCount)
            {
                maxCharacterCount = (int)(1.5f * charsToRenderCount);
                Initialize(graphicsContext, maxCharacterCount);
            }

            // Set the rendering parameters
            simpleEffect.Parameters.Set(TexturingKeys.Texture0, DebugSpriteFont);
            simpleEffect.Parameters.Set(SpriteEffectKeys.Color, TextColor);

            // Swap vertex buffer
            activeVertexBufferIndex = ++activeVertexBufferIndex >= VertexBufferCount ? 0 : activeVertexBufferIndex;

            // Map the vertex buffer to write to
            mappedVertexBuffer        = graphicsContext.CommandList.MapSubresource(vertexBuffers[activeVertexBufferIndex], 0, MapMode.WriteDiscard);
            mappedVertexBufferPointer = mappedVertexBuffer.DataBox.DataPointer;

            unsafe
            {
                // Clear buffer first (because of the buffer mapping mode used)
                Utilities.ClearMemory(mappedVertexBufferPointer, 0x0, VertexBufferLength * sizeof(VertexPositionNormalTexture));

                charsToRenderCount = 0;

                //Draw the strings
                var constantInfos = new RectangleF(GlyphWidth, GlyphHeight, DebugSpriteWidth, DebugSpriteHeight);
                foreach (var textInfo in stringsToDraw)
                {
                    var textLength        = textInfo.Text.Length;
                    var textLengthPointer = new IntPtr(&textLength);

                    Native.NativeInvoke.xnGraphicsFastTextRendererGenerateVertices(constantInfos, textInfo.RenderingInfo, textInfo.Text, out textLengthPointer, out mappedVertexBufferPointer);

                    charsToRenderCount += *(int *)textLengthPointer.ToPointer();
                }
            }

            // Unmap the vertex buffer
            graphicsContext.CommandList.UnmapSubresource(mappedVertexBuffer);
            mappedVertexBufferPointer = IntPtr.Zero;

            // Update pipeline state
            pipelineState.State.SetDefaults();
            pipelineState.State.RootSignature     = simpleEffect.RootSignature;
            pipelineState.State.EffectBytecode    = simpleEffect.Effect.Bytecode;
            pipelineState.State.DepthStencilState = DepthStencilStates.None;
            pipelineState.State.BlendState        = BlendStates.AlphaBlend;
            pipelineState.State.Output.CaptureState(graphicsContext.CommandList);
            pipelineState.State.InputElements = inputElementDescriptions[activeVertexBufferIndex];
            pipelineState.Update();

            graphicsContext.CommandList.SetPipelineState(pipelineState.CurrentState);

            // Update effect
            simpleEffect.UpdateEffect(graphicsContext.CommandList.GraphicsDevice);
            simpleEffect.Apply(graphicsContext);

            // Bind and draw
            graphicsContext.CommandList.SetVertexBuffer(0, vertexBuffersBinding[activeVertexBufferIndex].Buffer, vertexBuffersBinding[activeVertexBufferIndex].Offset, vertexBuffersBinding[activeVertexBufferIndex].Stride);
            graphicsContext.CommandList.SetIndexBuffer(indexBufferBinding.Buffer, 0, indexBufferBinding.Is32Bit);

            graphicsContext.CommandList.DrawIndexed(charsToRenderCount * 6);
        }
Beispiel #17
0
 private void DrawGeometry()
 {
     simpleEffect.Parameters.Set(SpriteBaseKeys.MatrixTransform, worldViewProjection);
     simpleEffect.Apply(GraphicsContext);
     geometry.Draw(GraphicsContext.CommandList, simpleEffect);
 }